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 // Controls. 46 bool include_vector_contents = true; 47 }; 48 AnnotatedBinaryTextGenerator(const Options & options,std::map<uint64_t,BinarySection> annotations,const uint8_t * const binary,const int64_t binary_length)49 explicit AnnotatedBinaryTextGenerator( 50 const Options &options, std::map<uint64_t, BinarySection> annotations, 51 const uint8_t *const binary, const int64_t binary_length) 52 : annotations_(std::move(annotations)), 53 binary_(binary), 54 binary_length_(binary_length), 55 options_(options) {} 56 57 // Generate the annotated binary for the given `filename`. Returns true if the 58 // annotated binary was successfully saved. 59 bool Generate(const std::string &filename, const std::string &schema_filename, 60 const std::string &output_filename = ""); 61 62 private: 63 const std::map<uint64_t, BinarySection> annotations_; 64 65 // The binary data itself. 66 const uint8_t *binary_; 67 const int64_t binary_length_; 68 69 // Output configuration 70 const Options options_; 71 }; 72 73 } // namespace flatbuffers 74 75 #endif // FLATBUFFERS_ANNOTATED_BINARY_TEXT_GEN_H_ 76