bpak_transport — BPAK Transport API

The BPAK transport layer handles encoding and decoding of packages for transport. This typically means that two versions of the same package is transport encoded using the bsdiff algorithm.


Source code: include/bpak/transport.h, lib/transport_decode.c, lib/transport_encode.c


BPAK - Bit Packer

Copyright (C) 2022 Jonas Blixt jonpe960@gmail.com

SPDX-License-Identifier: BSD-3-Clause

Functions

int bpak_transport_decode_init(struct bpak_transport_decode *ctx, uint8_t *buffer, size_t buffer_length, struct bpak_header *patch_header, bpak_io_t write_output, bpak_io_t read_output, off_t output_offset, bpak_io_t write_output_header, void *user)

Initalizes the transport decode context for a BPAK package

NOTE: It’s expected that the user provied correct offsets for io callbacks. The transport decode layer assumes that reading/writing at offset zero means where the data starts.

Parameters:
  • ctx[in] Pointer to a transport decode context

  • buffer[in] Work buffer

  • buffer_length[in] Length of work buffer

  • patch_header[in] Input patch BPAK header

  • write_output[in] Callback for writing output

  • read_output[in] Callback for reading output

  • output_offset[in] Offset where output data should be written

  • write_output_header[in] Callback for writing the output header

  • user[in] User pointer for io callbacks

Returns:

BPAK_OK on success or a negative number on failure

int bpak_transport_decode_set_origin(struct bpak_transport_decode *ctx, struct bpak_header *origin_header, bpak_io_t read_origin, off_t origin_offset)

Provide an origin stream for the decoder. The patch decoder needs an origin stream to produce the target binary. However, de-compressors merkle-tree generator does not need this.

Parameters:
  • ctx[in] Pointer to a transport decode context

  • origin_header[in] Origin BPAK header

  • read_origin[in] Callback for reading origin data

  • origin_offset[in] Offset where origin data block starts

  • origin_data_offset[in] Origin data offset

Returns:

BPAK_OK on success or a negative number on failure

int bpak_transport_decode_start(struct bpak_transport_decode *ctx, struct bpak_part_header *part)

Starts the decoding process. Some parts are re-created, for example merkle hash tress, and therefore the input size is zero. In this case the start function will perform the actual decoding and no calls to ‘bpak_transport_decode_write_chunk’ are needed.

Note: This call can take a significant amount of time, for example if a large hash tree needs to be re-built.

Parameters:
  • ctx[in] Pointer to a transport decode context

  • part[in] Pointer to the BPAK part that should be processed

Returns:

BPAK_OK on success or a negative number on failure

int bpak_transport_decode_write_chunk(struct bpak_transport_decode *ctx, uint8_t *buffer, size_t length)

Write chunked input data to the decoder

Parameters:
  • ctx[in] Pointer to a transport decode context

  • buffer[in] Pointer to a buffer that holds input data

  • length[in] Length of input buffer

Returns:

BPAK_OK on success or a negative number on failure

int bpak_transport_decode_finish(struct bpak_transport_decode *ctx)

Should be called when no more data should be written to the decoder

Parameters:

ctx[in] Pointer to a transport decode context

Returns:

BPAK_OK on success or a negative number on failure

void bpak_transport_decode_free(struct bpak_transport_decode *ctx)

Free’s the decoder context

Parameters:

ctx[in] Pointer to a transport decode context

int bpak_transport_encode(FILE *input_fp, struct bpak_header *input_header, FILE *output_fp, struct bpak_header *output_header, FILE *origin_fp, struct bpak_header *origin_header)

Transport encode package stream ‘input_fp’ according to metadata in package and use package stream ‘origin_fp’ as origin data. The output package is written to ‘output_fp’.

NOTE: The transport encode function only accepts input streams where the bpak header is at the start of the stream and the data at sizeof(struct bpak_header) offset from start.

Parameters:
  • input_fp[in] Input file stream

  • input_header[in] BPAK header from input stream

  • output_fp[in] Output file stream

  • output_header[in] BPAK header from output stream

  • origin_fp[in] Origin file stream

  • origin_header[in] BPAK header from origin stream

Returns:

BPAK_OK on success or a negative number on failure

struct bpak_transport_decode

Public Members

uint8_t *buffer
size_t buffer_length
struct bpak_header *patch_header
struct bpak_header *origin_header
struct bpak_part_header *part
bpak_io_t write_output
bpak_io_t read_output
bpak_io_t read_origin
bpak_io_t write_output_header
uint32_t decoder_id
off_t copy_offset
off_t output_offset
off_t origin_offset
struct bpak_bspatch_context bspatch
union bpak_transport_decode::[anonymous] decoders
void *user