xref: /aosp_15_r20/external/flatbuffers/src/annotated_binary_text_gen.h (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker /*
2*890232f2SAndroid Build Coastguard Worker  * Copyright 2021 Google Inc. All rights reserved.
3*890232f2SAndroid Build Coastguard Worker  *
4*890232f2SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*890232f2SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*890232f2SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*890232f2SAndroid Build Coastguard Worker  *
8*890232f2SAndroid Build Coastguard Worker  *     http://www.apache.org/licenses/LICENSE-2.0
9*890232f2SAndroid Build Coastguard Worker  *
10*890232f2SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*890232f2SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*890232f2SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*890232f2SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*890232f2SAndroid Build Coastguard Worker  * limitations under the License.
15*890232f2SAndroid Build Coastguard Worker  */
16*890232f2SAndroid Build Coastguard Worker 
17*890232f2SAndroid Build Coastguard Worker #ifndef FLATBUFFERS_ANNOTATED_BINARY_TEXT_GEN_H_
18*890232f2SAndroid Build Coastguard Worker #define FLATBUFFERS_ANNOTATED_BINARY_TEXT_GEN_H_
19*890232f2SAndroid Build Coastguard Worker 
20*890232f2SAndroid Build Coastguard Worker #include <map>
21*890232f2SAndroid Build Coastguard Worker #include <memory>
22*890232f2SAndroid Build Coastguard Worker #include <string>
23*890232f2SAndroid Build Coastguard Worker 
24*890232f2SAndroid Build Coastguard Worker #include "binary_annotator.h"
25*890232f2SAndroid Build Coastguard Worker 
26*890232f2SAndroid Build Coastguard Worker namespace flatbuffers {
27*890232f2SAndroid Build Coastguard Worker 
28*890232f2SAndroid Build Coastguard Worker class AnnotatedBinaryTextGenerator {
29*890232f2SAndroid Build Coastguard Worker  public:
30*890232f2SAndroid Build Coastguard Worker   struct Options {
31*890232f2SAndroid Build Coastguard Worker     // The maximum number of raw bytes to print per line in the output. 8 is a
32*890232f2SAndroid Build Coastguard Worker     // good default due to the largest type (double) being 8 bytes long.
33*890232f2SAndroid Build Coastguard Worker     size_t max_bytes_per_line = 8;
34*890232f2SAndroid Build Coastguard Worker 
35*890232f2SAndroid Build Coastguard Worker     // The output file postfix, appended between the filename and the extension.
36*890232f2SAndroid Build Coastguard Worker     // Example binary1.bin -> binary1_annotated.bin
37*890232f2SAndroid Build Coastguard Worker     std::string output_postfix = "";
38*890232f2SAndroid Build Coastguard Worker 
39*890232f2SAndroid Build Coastguard Worker     // The output file extension, replacing any extension given. If empty, don't
40*890232f2SAndroid Build Coastguard Worker     // change the provided extension. AFB = Annotated Flatbuffer Binary
41*890232f2SAndroid Build Coastguard Worker     //
42*890232f2SAndroid Build Coastguard Worker     // Example: binary1.bin -> binary1.afb
43*890232f2SAndroid Build Coastguard Worker     std::string output_extension = "afb";
44*890232f2SAndroid Build Coastguard Worker   };
45*890232f2SAndroid Build Coastguard Worker 
AnnotatedBinaryTextGenerator(const Options & options,std::map<uint64_t,BinarySection> annotations,const uint8_t * const binary,const int64_t binary_length)46*890232f2SAndroid Build Coastguard Worker   explicit AnnotatedBinaryTextGenerator(
47*890232f2SAndroid Build Coastguard Worker       const Options &options, std::map<uint64_t, BinarySection> annotations,
48*890232f2SAndroid Build Coastguard Worker       const uint8_t *const binary, const int64_t binary_length)
49*890232f2SAndroid Build Coastguard Worker       : annotations_(std::move(annotations)),
50*890232f2SAndroid Build Coastguard Worker         binary_(binary),
51*890232f2SAndroid Build Coastguard Worker         binary_length_(binary_length),
52*890232f2SAndroid Build Coastguard Worker         options_(options) {}
53*890232f2SAndroid Build Coastguard Worker 
54*890232f2SAndroid Build Coastguard Worker   // Generate the annotated binary for the given `filename`. Returns true if the
55*890232f2SAndroid Build Coastguard Worker   // annotated binary was successfully saved.
56*890232f2SAndroid Build Coastguard Worker   bool Generate(const std::string &filename,
57*890232f2SAndroid Build Coastguard Worker                 const std::string &schema_filename);
58*890232f2SAndroid Build Coastguard Worker 
59*890232f2SAndroid Build Coastguard Worker  private:
60*890232f2SAndroid Build Coastguard Worker   const std::map<uint64_t, BinarySection> annotations_;
61*890232f2SAndroid Build Coastguard Worker 
62*890232f2SAndroid Build Coastguard Worker   // The binary data itself.
63*890232f2SAndroid Build Coastguard Worker   const uint8_t *binary_;
64*890232f2SAndroid Build Coastguard Worker   const int64_t binary_length_;
65*890232f2SAndroid Build Coastguard Worker 
66*890232f2SAndroid Build Coastguard Worker   // Output configuration
67*890232f2SAndroid Build Coastguard Worker   const Options options_;
68*890232f2SAndroid Build Coastguard Worker };
69*890232f2SAndroid Build Coastguard Worker 
70*890232f2SAndroid Build Coastguard Worker }  // namespace flatbuffers
71*890232f2SAndroid Build Coastguard Worker 
72*890232f2SAndroid Build Coastguard Worker #endif  // FLATBUFFERS_ANNOTATED_BINARY_TEXT_GEN_H_
73