bpak_pkg — BPAK package

This documents the BPAK package API


Source code: include/bpak/pkg.h, lib/pkg.c


BPAK - Bit Packer

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

SPDX-License-Identifier: BSD-3-Clause

Functions

int bpak_pkg_open(struct bpak_package *pkg, const char *filename, const char *mode)

Open a package for reading or writing

Parameters:
  • pkg[in] Package pointer

  • filename[in] Filename

  • mode[in] File mode

Returns:

BPAK_OK on success

int bpak_pkg_close(struct bpak_package *pkg)

Close a package

Parameters:

pkg[in] Package pointer

Returns:

BPAK_OK on success

int bpak_pkg_update_hash(struct bpak_package *pkg, char *output, size_t *size)

Computes the package header hash. This function also updates the payload hash in the header

Parameters:
  • pkg[in] Package pointer

  • out[out] Optional header output hash

  • size[inout] Optional input size of output buffer and result of computation size is stored here as well

Returns:

BPAK_OK on success

size_t bpak_pkg_installed_size(struct bpak_package *pkg)

Computes the package size after transport decoding

Parameters:

pkg[in] Package pointer

Returns:

Size in bytes

size_t bpak_pkg_size(struct bpak_package *pkg)

Computes the package size before transport decoding

Parameters:

pkg[in] Package pointer

Returns:

Size in bytes

struct bpak_header *bpak_pkg_header(struct bpak_package *pkg)

Get the package header pointer

Parameters:

pkg[in] Package pointer

Returns:

Pointer to the header

int bpak_pkg_write_raw_signature(struct bpak_package *pkg, const uint8_t *signature, size_t size)

Populate the signature data array

Parameters:
  • pkg[in] Package pointer

  • signature[in] DER encoded signature

  • size[in] Size of signature in bytes

Returns:

BPAK_OK on success

int bpak_pkg_sign(struct bpak_package *pkg, const char *key_filename)

Sign the package

Parameters:
  • pkg[in] Package pointer

  • key_filename[in] Full path of key to be used for signing

Returns:

BPAK_OK on success

int bpak_pkg_verify(struct bpak_package *pkg, struct bpak_key *key)

Verify the package

Parameters:
  • pkg[in] Package pointer

  • key[in] Verification key

Returns:

BPAK_OK on success

int bpak_pkg_part_sha256(struct bpak_package *pkg, uint8_t *hash_buffer, size_t hash_buffer_length, bpak_id_t part_id)

Compute sha256 hash of part data

Parameters:
  • pkg[in] Package pointer

  • hash_buffer[out] Output buffer

  • hash_buffer_length[in] length of hash buffer

  • part_id[in] Id of part to be hashed

Returns:

BPAK_OK on success

int bpak_pkg_transport_encode(struct bpak_package *input, struct bpak_package *output, struct bpak_package *origin)

Transport encode package

Parameters:
  • input[in] BPAK Package input stream

  • output[in] BPAK Package output, the result

  • origin[in] BPAK Package origin data

Returns:

BPAK_OK on success

int bpak_pkg_transport_decode(struct bpak_package *input, struct bpak_package *output, struct bpak_package *origin)

Transport decode package

Parameters:
  • input[in] BPAK Package input stream

  • output[in] BPAK Package output, the result

  • origin[in] BPAK Package origin data

Returns:

BPAK_OK on success

int bpak_pkg_write_header(struct bpak_package *pkg)

Writes current header to file

Parameters:

hdr[in] BPAK Header

Returns:

BPAK_OK on success

int bpak_pkg_add_file(struct bpak_package *pkg, const char *filename, const char *part_name, uint8_t flags)

Add a file part to a package

Parameters:
  • pkg[in] Pointer to a bpak package

  • filename[in] Full path to the file that should be added to the archive

  • part_name[in] Name of part to be created

  • flags[in] Optional flags for part meta data header

Returns:

BPAK_OK on success or a negative number

int bpak_pkg_add_file_with_merkle_tree(struct bpak_package *pkg, const char *filename, const char *part_name, uint8_t flags)

Add a file part to a package and create a separate part with a merkle hash tree.

The hash tree part name will be bpak_id(‘part_name’-hash-tree’)

Parameters:
  • pkg[in] Pointer to a bpak package

  • filename[in] Full path to the file that should be added to the archive

  • part_name[in] Name of part to be created

  • flags[in] Optional flags for part meta data header

Returns:

BPAK_OK on success or a negative number

int bpak_pkg_add_key(struct bpak_package *pkg, const char *filename, const char *part_name, uint8_t flags)

Add a crypto key to the archive. This can add both PEM and DER encoded keys

Parameters:
  • pkg[in] Pointer to a bpak package

  • filename[in] Full path to the file that should be added to the archive

  • part_name[in] Name of part to be created

  • flags[in] Optional flags for part meta data header

Returns:

BPAK_OK on success or a negative number

int bpak_pkg_extract_file(struct bpak_package *pkg, bpak_id_t part_id, const char *filename)

Extract a part from the archive to a file

Parameters:
  • pkg[in] Pointer to a bpak package

  • part_id[in] ID of part to be extracted

  • filename[in] Full path to the file that the part should be extracted to

Returns:

BPAK_OK on success or a negative number

int bpak_pkg_delete_part(struct bpak_package *pkg, bpak_id_t part_id, bool remove_meta)

Delete a part from the archive

Parameters:
  • pkg[in] Pointer to a bpak package

  • part_id[in] ID of part to be deleted

  • remove_meta[in] If true any metadata associated with part_id will also be removed

Returns:

BPAK_OK on success or a negative number

int bpak_pkg_delete_all_parts(struct bpak_package *pkg, bool remove_meta)

Delete all parts from the archive

Parameters:
  • pkg[in] Pointer to a bpak package

  • remove_meta[in] If true any metadata associated with part_id will also be removed

Returns:

BPAK_OK on success or a negative number

struct bpak_package
#include <pkg.h>

BPAK Package

Public Members

FILE *fp

I/O Stream for package

const char *filename

Filename

struct bpak_header header

BPAK Header