• 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 #ifndef NLP_SAFT_COMPONENTS_COMMON_MOBILE_FLATBUFFERS_MODEL_UTILS_H_
18 #define NLP_SAFT_COMPONENTS_COMMON_MOBILE_FLATBUFFERS_MODEL_UTILS_H_
19 
20 #include <stddef.h>
21 
22 #include <string>
23 
24 #include "lang_id/common/fel/task-context.h"
25 #include "lang_id/common/flatbuffers/model_generated.h"
26 #include "lang_id/common/lite_base/integral-types.h"
27 #include "lang_id/common/lite_strings/stringpiece.h"
28 
29 namespace libtextclassifier3 {
30 namespace saft_fbs {
31 
32 // Verifies that the |num_bytes| bytes that start at |data| represent a valid
33 // Model flatbuffer.  If so, returns that Model.  Otherwise, returns nullptr.
34 //
35 // Note: if the Model has the crc32 field, this method checks that the Model
36 // checksum matches that field; if they don't match, the Model is considered
37 // invalid, and this function returns nullptr.  The checksum test is in addition
38 // to the standard flatbuffer validity checking.
39 const Model *GetVerifiedModelFromBytes(const char *data, size_t num_bytes);
40 
41 // Convenience StringPiece version of GetVerifiedModelFromBytes.
GetVerifiedModelFromBytes(mobile::StringPiece bytes)42 inline const Model *GetVerifiedModelFromBytes(mobile::StringPiece bytes) {
43   return GetVerifiedModelFromBytes(bytes.data(), bytes.size());
44 }
45 
46 // Returns the |model| input with specified |name|.  Returns nullptr if no such
47 // input exists.  If |model| contains multiple inputs with that |name|, returns
48 // the first one (model builders should avoid building such models).
49 const ModelInput *GetInputByName(const Model *model, const string &name);
50 
51 // Returns a StringPiece pointing to the bytes for the content of |input|.  In
52 // case of errors, returns StringPiece(nullptr, 0).
53 mobile::StringPiece GetInputBytes(const ModelInput *input);
54 
55 // Fills parameters from |context|, based on the parameters from |model|.
56 // Returns false if any error is encountered, true otherwise.  In the case of an
57 // error, some parameters may have been added to |context| (e.g., if we find a
58 // problem with the 3rd parameter, the first 2 have been added).
59 bool FillParameters(const Model &model, mobile::TaskContext *context);
60 
61 // Returns the CRC32 checksum of |model|.  This checksum is computed over the
62 // entire information from the model (including the bytes of the inputs),
63 // *except* the crc32 field.  Hence, when a model is build, one can store the
64 // result of this function into that field; on the user side, one can check that
65 // the result of this function matches the crc32 field, to guard against model
66 // corruption.  GetVerifiedModelFromBytes performs this check.
67 mobile::uint32 ComputeCrc2Checksum(const Model *model);
68 
69 }  // namespace saft_fbs
70 }  // namespace nlp_saft
71 
72 #endif  // NLP_SAFT_COMPONENTS_COMMON_MOBILE_FLATBUFFERS_MODEL_UTILS_H_
73