• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // Utility functions for working with FlatBuffers.
18 
19 #ifndef LIBTEXTCLASSIFIER_UTILS_FLATBUFFERS_FLATBUFFERS_H_
20 #define LIBTEXTCLASSIFIER_UTILS_FLATBUFFERS_FLATBUFFERS_H_
21 
22 #include <string>
23 
24 #include "annotator/model_generated.h"
25 #include "flatbuffers/flatbuffers.h"
26 
27 namespace libtextclassifier3 {
28 
29 // Loads and interprets the buffer as 'FlatbufferMessage' and verifies its
30 // integrity.
31 template <typename FlatbufferMessage>
LoadAndVerifyFlatbuffer(const void * buffer,int size)32 const FlatbufferMessage* LoadAndVerifyFlatbuffer(const void* buffer, int size) {
33   const FlatbufferMessage* message =
34       flatbuffers::GetRoot<FlatbufferMessage>(buffer);
35   if (message == nullptr) {
36     return nullptr;
37   }
38   flatbuffers::Verifier verifier(reinterpret_cast<const uint8_t*>(buffer),
39                                  size);
40   if (message->Verify(verifier)) {
41     return message;
42   } else {
43     return nullptr;
44   }
45 }
46 
47 // Same as above but takes string.
48 template <typename FlatbufferMessage>
LoadAndVerifyFlatbuffer(const std::string & buffer)49 const FlatbufferMessage* LoadAndVerifyFlatbuffer(const std::string& buffer) {
50   return LoadAndVerifyFlatbuffer<FlatbufferMessage>(buffer.c_str(),
51                                                     buffer.size());
52 }
53 
54 // Loads and interprets the buffer as 'FlatbufferMessage', verifies its
55 // integrity and returns its mutable version.
56 template <typename FlatbufferMessage>
57 std::unique_ptr<typename FlatbufferMessage::NativeTableType>
LoadAndVerifyMutableFlatbuffer(const void * buffer,int size)58 LoadAndVerifyMutableFlatbuffer(const void* buffer, int size) {
59   const FlatbufferMessage* message =
60       LoadAndVerifyFlatbuffer<FlatbufferMessage>(buffer, size);
61   if (message == nullptr) {
62     return nullptr;
63   }
64   return std::unique_ptr<typename FlatbufferMessage::NativeTableType>(
65       message->UnPack());
66 }
67 
68 // Same as above but takes string.
69 template <typename FlatbufferMessage>
70 std::unique_ptr<typename FlatbufferMessage::NativeTableType>
LoadAndVerifyMutableFlatbuffer(const std::string & buffer)71 LoadAndVerifyMutableFlatbuffer(const std::string& buffer) {
72   return LoadAndVerifyMutableFlatbuffer<FlatbufferMessage>(buffer.c_str(),
73                                                            buffer.size());
74 }
75 
76 template <typename FlatbufferMessage>
FlatbufferFileIdentifier()77 const char* FlatbufferFileIdentifier() {
78   return nullptr;
79 }
80 
81 template <>
82 inline const char* FlatbufferFileIdentifier<Model>() {
83   return ModelIdentifier();
84 }
85 
86 // Packs the mutable flatbuffer message to string.
87 template <typename FlatbufferMessage>
PackFlatbuffer(const typename FlatbufferMessage::NativeTableType * mutable_message)88 std::string PackFlatbuffer(
89     const typename FlatbufferMessage::NativeTableType* mutable_message) {
90   flatbuffers::FlatBufferBuilder builder;
91   builder.Finish(FlatbufferMessage::Pack(builder, mutable_message),
92                  FlatbufferFileIdentifier<FlatbufferMessage>());
93   return std::string(reinterpret_cast<const char*>(builder.GetBufferPointer()),
94                      builder.GetSize());
95 }
96 
97 // A convenience flatbuffer object with its underlying buffer.
98 template <typename T, typename B = flatbuffers::DetachedBuffer>
99 class OwnedFlatbuffer {
100  public:
OwnedFlatbuffer(B && buffer)101   explicit OwnedFlatbuffer(B&& buffer) : buffer_(std::move(buffer)) {}
102 
103   // Cast as flatbuffer type.
get()104   const T* get() const { return flatbuffers::GetRoot<T>(buffer_.data()); }
105 
buffer()106   const B& buffer() const { return buffer_; }
107 
108   const T* operator->() const {
109     return flatbuffers::GetRoot<T>(buffer_.data());
110   }
111 
112  private:
113   B buffer_;
114 };
115 
116 }  // namespace libtextclassifier3
117 
118 #endif  // LIBTEXTCLASSIFIER_UTILS_FLATBUFFERS_FLATBUFFERS_H_
119