1 // Copyright 2019 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <lib/inspect/limits.h> 6 #include <lib/inspect/scanner.h> 7 8 namespace inspect { 9 namespace internal { 10 ScanBlocks(const uint8_t * buffer,size_t size,fbl::Function<void (BlockIndex,const Block *)> callback)11zx_status_t ScanBlocks(const uint8_t* buffer, size_t size, 12 fbl::Function<void(BlockIndex, const Block*)> callback) { 13 size_t offset = 0; 14 while (offset < size) { 15 const Block* block = reinterpret_cast<const Block*>(buffer + offset); 16 if (size - offset < sizeof(Block)) { 17 // Block header does not fit in remaining space. 18 return ZX_ERR_OUT_OF_RANGE; 19 } 20 BlockOrder order = GetOrder(block); 21 if (order > kMaxOrderShift) { 22 return ZX_ERR_OUT_OF_RANGE; 23 } 24 if (size - offset < OrderToSize(order)) { 25 // Block header specifies an order that is too large to fit 26 // in the remainder of the buffer. 27 return ZX_ERR_OUT_OF_RANGE; 28 } 29 30 callback(IndexForOffset(offset), block); 31 offset += OrderToSize(order); 32 } 33 34 return ZX_OK; 35 } 36 37 } // namespace internal 38 } // namespace inspect 39