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 #include "absl/strings/string_view.h"
29
30 namespace libtextclassifier3 {
31 namespace saft_fbs {
32
33 // Verifies that the |num_bytes| bytes that start at |data| represent a valid
34 // Model flatbuffer. If so, returns that Model. Otherwise, returns nullptr.
35 //
36 // Note: if the Model has the crc32 field, this method checks that the Model
37 // checksum matches that field; if they don't match, the Model is considered
38 // invalid, and this function returns nullptr. The checksum test is in addition
39 // to the standard flatbuffer validity checking.
40 const Model *GetVerifiedModelFromBytes(const char *data, size_t num_bytes);
41
42 // Convenience StringPiece version of GetVerifiedModelFromBytes.
GetVerifiedModelFromBytes(mobile::StringPiece bytes)43 inline const Model *GetVerifiedModelFromBytes(mobile::StringPiece bytes) {
44 return GetVerifiedModelFromBytes(bytes.data(), bytes.size());
45 }
46
47 // Returns the |model| input with specified |name|. Returns nullptr if no such
48 // input exists. If |model| contains multiple inputs with that |name|, returns
49 // the first one (model builders should avoid building such models).
50 const ModelInput *GetInputByName(const Model *model, absl::string_view name);
51
52 // Returns a StringPiece pointing to the bytes for the content of |input|. In
53 // case of errors, returns StringPiece(nullptr, 0).
54 mobile::StringPiece GetInputBytes(const ModelInput *input);
55
56 // Fills parameters from |context|, based on the parameters from |model|.
57 // Returns false if any error is encountered, true otherwise. In the case of an
58 // error, some parameters may have been added to |context| (e.g., if we find a
59 // problem with the 3rd parameter, the first 2 have been added).
60 bool FillParameters(const Model &model, mobile::TaskContext *context);
61
62 // Returns the CRC32 checksum of |model|. This checksum is computed over the
63 // entire information from the model (including the bytes of the inputs),
64 // *except* the crc32 field. Hence, when a model is build, one can store the
65 // result of this function into that field; on the user side, one can check that
66 // the result of this function matches the crc32 field, to guard against model
67 // corruption. GetVerifiedModelFromBytes performs this check.
68 mobile::uint32 ComputeCrc2Checksum(const Model *model);
69
70 // Returns true if we have clear evidence that |model| fails its checksum.
71 //
72 // E.g., if |model| has the crc32 field, and the value of that field does not
73 // match the checksum, then this function returns true. If there is no crc32
74 // field, then we don't know what the original (at build time) checksum was, so
75 // we don't know anything clear and this function returns false.
76 bool ClearlyFailsChecksum(const Model &model);
77
78 } // namespace saft_fbs
79 } // namespace nlp_saft
80
81 #endif // NLP_SAFT_COMPONENTS_COMMON_MOBILE_FLATBUFFERS_MODEL_UTILS_H_
82