xref: /aosp_15_r20/external/flatbuffers/src/annotated_binary_text_gen.h (revision 890232f25432b36107d06881e0a25aaa6b473652)
1 /*
2  * Copyright 2021 Google Inc. All rights reserved.
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 FLATBUFFERS_ANNOTATED_BINARY_TEXT_GEN_H_
18 #define FLATBUFFERS_ANNOTATED_BINARY_TEXT_GEN_H_
19 
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include "binary_annotator.h"
25 
26 namespace flatbuffers {
27 
28 class AnnotatedBinaryTextGenerator {
29  public:
30   struct Options {
31     // The maximum number of raw bytes to print per line in the output. 8 is a
32     // good default due to the largest type (double) being 8 bytes long.
33     size_t max_bytes_per_line = 8;
34 
35     // The output file postfix, appended between the filename and the extension.
36     // Example binary1.bin -> binary1_annotated.bin
37     std::string output_postfix = "";
38 
39     // The output file extension, replacing any extension given. If empty, don't
40     // change the provided extension. AFB = Annotated Flatbuffer Binary
41     //
42     // Example: binary1.bin -> binary1.afb
43     std::string output_extension = "afb";
44   };
45 
AnnotatedBinaryTextGenerator(const Options & options,std::map<uint64_t,BinarySection> annotations,const uint8_t * const binary,const int64_t binary_length)46   explicit AnnotatedBinaryTextGenerator(
47       const Options &options, std::map<uint64_t, BinarySection> annotations,
48       const uint8_t *const binary, const int64_t binary_length)
49       : annotations_(std::move(annotations)),
50         binary_(binary),
51         binary_length_(binary_length),
52         options_(options) {}
53 
54   // Generate the annotated binary for the given `filename`. Returns true if the
55   // annotated binary was successfully saved.
56   bool Generate(const std::string &filename,
57                 const std::string &schema_filename);
58 
59  private:
60   const std::map<uint64_t, BinarySection> annotations_;
61 
62   // The binary data itself.
63   const uint8_t *binary_;
64   const int64_t binary_length_;
65 
66   // Output configuration
67   const Options options_;
68 };
69 
70 }  // namespace flatbuffers
71 
72 #endif  // FLATBUFFERS_ANNOTATED_BINARY_TEXT_GEN_H_
73