github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/executor/_include/flatbuffers/buffer_ref.h (about) 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_BUFFER_REF_H_ 18 #define FLATBUFFERS_BUFFER_REF_H_ 19 20 #include "flatbuffers/base.h" 21 #include "flatbuffers/verifier.h" 22 23 namespace flatbuffers { 24 25 // Convenient way to bundle a buffer and its length, to pass it around 26 // typed by its root. 27 // A BufferRef does not own its buffer. 28 struct BufferRefBase {}; // for std::is_base_of 29 30 template<typename T> struct BufferRef : BufferRefBase { 31 BufferRef() : buf(nullptr), len(0), must_free(false) {} 32 BufferRef(uint8_t *_buf, uoffset_t _len) 33 : buf(_buf), len(_len), must_free(false) {} 34 35 ~BufferRef() { 36 if (must_free) free(buf); 37 } 38 39 const T *GetRoot() const { return flatbuffers::GetRoot<T>(buf); } 40 41 bool Verify() { 42 Verifier verifier(buf, len); 43 return verifier.VerifyBuffer<T>(nullptr); 44 } 45 46 uint8_t *buf; 47 uoffset_t len; 48 bool must_free; 49 }; 50 51 } // namespace flatbuffers 52 53 #endif // FLATBUFFERS_BUFFER_REF_H_