src/storage/blobfs/compression/zstd-seekable-blob.cc
// ZSTD Seekable Format API function for `ZSTD_seekable_customFile`.
ZSTD Seekable Format API function for `ZSTD_seekable_customFile`.
ZSTD Seekable Format API function for ` ZSTD_seekable_customFile ` .
int ZSTDRead(void* void_ptr_zstd_seekable_file, void* buf, size_t num_bytes) {
  ZX_DEBUG_ASSERT(void_ptr_zstd_seekable_file);
  auto* file = static_cast<ZSTDSeekableFile*>(void_ptr_zstd_seekable_file);
  if (file->status != ZX_OK) {
    return -1;
  }
  TRACE_DURATION("blobfs", "ZSTDRead", "byte_offset", file->byte_offset, "bytes", num_bytes);
  if (num_bytes == 0) {
    return 0;
  }
  uint32_t data_block_offset;
  uint32_t num_blocks;
  uint64_t data_byte_offset;
  int result = ComputeOffsetAndNumBytesForRead(file, num_bytes, &data_block_offset, &num_blocks,
                                               &data_byte_offset);
  if (result != 0) {
    return result;
  }
  zx_status_t status = file->blocks->Read(data_block_offset, num_blocks);
  if (status != ZX_OK) {
    FS_TRACE_ERROR("[blobfs][zstd-seekable] Failed to read blocks: %s\n",
                   zx_status_get_string(status));
    file->status = status;
    return -1;
  }
  {
    TRACE_DURATION("blobfs", "ZSTDRead::Copy", "byte_offset", file->byte_offset, "bytes",
                   num_bytes);
    uint32_t start = static_cast<uint32_t>(data_byte_offset % kBlobfsBlockSize);
    static_assert(sizeof(const void*) == sizeof(uint64_t));
    uint64_t start_ptr;
    uint64_t end_ptr;
    if (add_overflow(reinterpret_cast<uint64_t>(file->blob->decompressed_data_start()), start,
                     &start_ptr) ||
        add_overflow(start_ptr, num_bytes, &end_ptr)) {
      FS_TRACE_ERROR("[blobfs][zstd-seekable] VMO offset overflow: offset=%u length=%zu\n", start,
                     num_bytes);
      file->status = ZX_ERR_OUT_OF_RANGE;
      return -1;
    }
    memcpy(buf, file->blob->decompressed_data_start() + start, num_bytes);
  }
  unsigned long long new_byte_offset;
  if (add_overflow(file->byte_offset, num_bytes, &new_byte_offset)) {
    FS_TRACE_ERROR("[blobfs][zstd-seekable] Byte offset overflow: file_offset=%llu increment=%lu\n",
                   file->byte_offset, num_bytes);
    file->status = ZX_ERR_OUT_OF_RANGE;
    return -1;
  }
  file->byte_offset = new_byte_offset;
  return 0;
}
int ZSTDRead ( void * void_ptr_zstd_seekable_file , void * buf , size_t num_bytes ) { ZX_DEBUG_ASSERT ( void_ptr_zstd_seekable_file ) ; auto * file = static_cast < ZSTDSeekableFile * > ( void_ptr_zstd_seekable_file ) ; if ( file -> status != ZX_OK ) { return -1 ; } TRACE_DURATION ( " " , " " , " " , file -> byte_offset , " " , num_bytes ) ; if ( num_bytes == 0 ) { return 0 ; } uint32_t data_block_offset ; uint32_t num_blocks ; uint64_t data_byte_offset ; int result = ComputeOffsetAndNumBytesForRead ( file , num_bytes , & data_block_offset , & num_blocks , & data_byte_offset ) ; if ( result != 0 ) { return result ; } zx_status_t status = file -> blocks -> Read ( data_block_offset , num_blocks ) ; if ( status != ZX_OK ) { FS_TRACE_ERROR ( " \n " , zx_status_get_string ( status ) ) ; file -> status = status ; return -1 ; } { TRACE_DURATION ( " " , " " , " " , file -> byte_offset , " " , num_bytes ) ; uint32_t start = static_cast < uint32_t > ( data_byte_offset % kBlobfsBlockSize ) ; static_assert ( sizeof ( const void * ) == sizeof ( uint64_t ) ) ; uint64_t start_ptr ; uint64_t end_ptr ; if ( add_overflow ( reinterpret_cast < uint64_t > ( file -> blob -> decompressed_data_start ( ) ) , start , & start_ptr ) || add_overflow ( start_ptr , num_bytes , & end_ptr ) ) { FS_TRACE_ERROR ( " \n " , start , num_bytes ) ; file -> status = ZX_ERR_OUT_OF_RANGE ; return -1 ; } memcpy ( buf , file -> blob -> decompressed_data_start ( ) + start , num_bytes ) ; } unsigned long long new_byte_offset ; if ( add_overflow ( file -> byte_offset , num_bytes , & new_byte_offset ) ) { FS_TRACE_ERROR ( " \n " , file -> byte_offset , num_bytes ) ; file -> status = ZX_ERR_OUT_OF_RANGE ; return -1 ; } file -> byte_offset = new_byte_offset ; return 0 ; }
ZSTD Seekable Format API function for `ZSTD_seekable_customFile`.
ZSTD Seekable Format API function for ` ZSTD_seekable_customFile ` .
// Give up if any file operation has ever failed. // Note: |zx_status_t|, tracing/logging managed by |ComputeOffsetAndNumBytesForRead|. // Delegate block-level read to compressed block collection. // Copy from transfer buffer to |buf|. // Advance byte offset in file.