FwPatch: in-place delta update (detools 0.53.0 vendored), dry run predicts the real run
This commit is contained in:
1 parent
3cb3a458ce
commit
8d3e554ca3
14 files changed
+5159
No files matched your search
@@ -0,0 +1,200 @@
|
||||
// CUSTOM (TeTeHacko): in-place delta update, see FwPatch.h.
|
||||
//
|
||||
// This translation unit compiles the vendored detools + heatshrink decoder
|
||||
// (src/helpers/fwpatch/detools, unmodified, see README.tth.md there) together
|
||||
// with the driver below. On the node tools/ota_ramlib.py moves the whole object
|
||||
// to RAM and redirects memcpy/memset to the local copies at the bottom, so not a
|
||||
// single instruction or table of the real run lives in the flash it rewrites
|
||||
// (tools/ota_check_ramfunc.sh checks the result).
|
||||
|
||||
// --- vendored code, configured here instead of editing it ------------------------
|
||||
#define HEATSHRINK_CONFIG_H // skip the vendored config, these values win
|
||||
#define HEATSHRINK_DYNAMIC_ALLOC 0
|
||||
#define HEATSHRINK_STATIC_INPUT_BUFFER_SIZE 256
|
||||
#define HEATSHRINK_STATIC_WINDOW_BITS 11 // tools/ota_pack.py must use the same
|
||||
#define HEATSHRINK_STATIC_LOOKAHEAD_BITS 10
|
||||
#define HEATSHRINK_DEBUGGING_LOGS 0
|
||||
#define HEATSHRINK_USE_INDEX 0
|
||||
|
||||
#define DETOOLS_CONFIG_FILE_IO 0
|
||||
#define DETOOLS_CONFIG_COMPRESSION_NONE 0
|
||||
#define DETOOLS_CONFIG_COMPRESSION_LZMA 0
|
||||
#define DETOOLS_CONFIG_COMPRESSION_CRLE 0
|
||||
#define DETOOLS_CONFIG_COMPRESSION_HEATSHRINK 1
|
||||
|
||||
#include "FwPatch.h"
|
||||
|
||||
#include "fwpatch/detools/detools.c"
|
||||
#include "fwpatch/detools/heatshrink/heatshrink_decoder.c"
|
||||
|
||||
// --- driver --------------------------------------------------------------------
|
||||
|
||||
static uint8_t g_page[FWP_PAGE];
|
||||
static struct detools_apply_patch_in_place_t g_dt;
|
||||
|
||||
#define NO_PAGE ((uintptr_t)-1)
|
||||
|
||||
static void fwp_copy(uint8_t *d, const uint8_t *s, uint32_t n) {
|
||||
while (n--)
|
||||
*d++ = *s++;
|
||||
}
|
||||
|
||||
static void flush_page(fwp_job *j) {
|
||||
if (j->page_addr == NO_PAGE) return;
|
||||
if (!j->dry) j->write(j->mem_base + j->page_addr, g_page, FWP_PAGE);
|
||||
j->page_addr = NO_PAGE;
|
||||
}
|
||||
|
||||
static int in_shift_phase(void) {
|
||||
return g_dt.state == detools_apply_patch_state_init_t;
|
||||
}
|
||||
|
||||
static int cb_read(void *arg, void *dst, uintptr_t src, size_t size) {
|
||||
fwp_job *j = (fwp_job *)arg;
|
||||
uint8_t *d = (uint8_t *)dst;
|
||||
if (src + size > j->mem_size) return j->err = FWP_ERR_ARGS;
|
||||
if (!in_shift_phase() && src < j->written_end) return j->err = FWP_ERR_OVERREAD;
|
||||
while (size > 0) {
|
||||
uintptr_t a = src;
|
||||
size_t n = size;
|
||||
if (j->dry && src >= j->shift_low && src < j->shift_high) {
|
||||
// the real run moved this page up by `shift`; the original still sits there
|
||||
a = src - j->shift;
|
||||
if (src + n > j->shift_high) n = j->shift_high - src;
|
||||
} else if (j->dry && src < j->shift_low && src + n > j->shift_low) {
|
||||
n = j->shift_low - src;
|
||||
}
|
||||
fwp_copy(d, (const uint8_t *)(j->mem_base + a), (uint32_t)n);
|
||||
d += n;
|
||||
src += n;
|
||||
size -= n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cb_erase(void *arg, uintptr_t addr, size_t size) {
|
||||
fwp_job *j = (fwp_job *)arg;
|
||||
if ((addr % FWP_PAGE) != 0 || size == 0 || size > FWP_PAGE || addr + FWP_PAGE > j->mem_size)
|
||||
return j->err = FWP_ERR_ARGS;
|
||||
flush_page(j);
|
||||
if (j->tick) j->tick();
|
||||
if (in_shift_phase()) {
|
||||
if (j->dry) {
|
||||
if (addr < j->shift_low) j->shift_low = (uint32_t)addr;
|
||||
if (addr + FWP_PAGE > j->shift_high) j->shift_high = (uint32_t)(addr + FWP_PAGE);
|
||||
}
|
||||
} else {
|
||||
if (addr != j->written_end) return j->err = FWP_ERR_ORDER;
|
||||
j->written_end = (uint32_t)(addr + size);
|
||||
}
|
||||
if (!j->dry) j->erase(j->mem_base + addr);
|
||||
for (uint32_t i = 0; i < FWP_PAGE; i++)
|
||||
g_page[i] = 0xFF;
|
||||
j->page_addr = addr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cb_write(void *arg, uintptr_t dst, void *src, size_t size) {
|
||||
fwp_job *j = (fwp_job *)arg;
|
||||
if (j->page_addr == NO_PAGE || dst < j->page_addr || dst + size > j->page_addr + FWP_PAGE)
|
||||
return j->err = FWP_ERR_ORDER;
|
||||
fwp_copy(&g_page[dst - j->page_addr], (const uint8_t *)src, (uint32_t)size);
|
||||
if (j->dry && !in_shift_phase()) j->sink(j->sink_ctx, (const uint8_t *)src, (uint32_t)size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int get_usize(const uint8_t *p, uint32_t n, uint32_t *pos, uint32_t *out) {
|
||||
uint32_t v = 0, off = 0;
|
||||
uint8_t b;
|
||||
if (*pos >= n) return -1;
|
||||
b = p[(*pos)++];
|
||||
if (b & 0x40) return -1; // negative
|
||||
v = b & 0x3f;
|
||||
off = 6;
|
||||
while (b & 0x80) {
|
||||
if (*pos >= n || off > 28) return -1;
|
||||
b = p[(*pos)++];
|
||||
v |= (uint32_t)(b & 0x7f) << off;
|
||||
off += 7;
|
||||
}
|
||||
*out = v;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fwp_header(const uint8_t *patch, uint32_t n, uint32_t *memory_size, uint32_t *segment_size,
|
||||
uint32_t *shift, uint32_t *from_size, uint32_t *to_size) {
|
||||
uint32_t pos = 1;
|
||||
if (n < 1 || ((patch[0] >> 4) & 0x7) != PATCH_TYPE_IN_PLACE) return -1;
|
||||
if ((patch[0] & 0xf) != COMPRESSION_HEATSHRINK) return -2;
|
||||
if (get_usize(patch, n, &pos, memory_size) || get_usize(patch, n, &pos, segment_size) ||
|
||||
get_usize(patch, n, &pos, shift) || get_usize(patch, n, &pos, from_size) ||
|
||||
get_usize(patch, n, &pos, to_size))
|
||||
return -3;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fwp_run(fwp_job *j) {
|
||||
uint32_t mem, seg, shift, from, to;
|
||||
int res;
|
||||
|
||||
j->err = 0;
|
||||
j->detools_err = 0;
|
||||
j->to_size = 0;
|
||||
j->page_addr = NO_PAGE;
|
||||
j->written_end = 0;
|
||||
j->shift_low = (uint32_t)-1;
|
||||
j->shift_high = 0;
|
||||
j->dt = &g_dt;
|
||||
|
||||
if ((j->mem_base % FWP_PAGE) != 0 || (j->mem_size % FWP_PAGE) != 0) return FWP_ERR_ARGS;
|
||||
if (j->dry ? j->sink == 0 : (j->erase == 0 || j->write == 0)) return FWP_ERR_ARGS;
|
||||
if (fwp_header(j->patch, j->patch_size, &mem, &seg, &shift, &from, &to) != 0) return FWP_ERR_ARGS;
|
||||
if (mem != j->mem_size || from != j->from_size || seg != FWP_PAGE) return FWP_ERR_SIZE;
|
||||
// shift 0 would make detools erase a page before reading it (shift loop)
|
||||
if (shift == 0 || (shift % FWP_PAGE) != 0 || to == 0 || to > mem) return FWP_ERR_SHIFT;
|
||||
j->shift = shift;
|
||||
|
||||
detools_apply_patch_in_place_init(&g_dt, cb_read, cb_write, cb_erase, NULL, NULL, j->patch_size, j);
|
||||
res = detools_apply_patch_in_place_process(&g_dt, j->patch, j->patch_size);
|
||||
if (res == 0) res = detools_apply_patch_in_place_finalize(&g_dt);
|
||||
if (j->err) return j->err; // ours is the more specific diagnosis
|
||||
if (res < 0) {
|
||||
j->detools_err = -res;
|
||||
return FWP_ERR_DETOOLS - (-res);
|
||||
}
|
||||
flush_page(j);
|
||||
if ((uint32_t)res != to || j->written_end < to) return FWP_ERR_SIZE;
|
||||
j->to_size = to;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --- RAM copies of the two libc calls the compiler and detools emit -----------
|
||||
// tools/ota_ramlib.py renames memcpy/memset/memmove in this object to these, so
|
||||
// the real run never jumps back into flash libc.
|
||||
void *fwp_memcpy(void *d, const void *s, size_t n) {
|
||||
uint8_t *dd = (uint8_t *)d;
|
||||
const uint8_t *ss = (const uint8_t *)s;
|
||||
while (n--)
|
||||
*dd++ = *ss++;
|
||||
return d;
|
||||
}
|
||||
void *fwp_memset(void *d, int c, size_t n) {
|
||||
uint8_t *dd = (uint8_t *)d;
|
||||
while (n--)
|
||||
*dd++ = (uint8_t)c;
|
||||
return d;
|
||||
}
|
||||
void *fwp_memmove(void *d, const void *s, size_t n) {
|
||||
uint8_t *dd = (uint8_t *)d;
|
||||
const uint8_t *ss = (const uint8_t *)s;
|
||||
if (dd < ss) {
|
||||
while (n--)
|
||||
*dd++ = *ss++;
|
||||
} else {
|
||||
dd += n;
|
||||
ss += n;
|
||||
while (n--)
|
||||
*--dd = *--ss;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#pragma once
|
||||
|
||||
// CUSTOM (TeTeHacko): in-place delta update of the running image, the core of
|
||||
// FwUpdate's `format 2` packages. Plain C on purpose: the very same file runs in
|
||||
// the native unit test (fake flash) and on the node, where its code is moved to
|
||||
// RAM (tools/ota_ramlib.py), because a real run rewrites the flash the firmware
|
||||
// executes from.
|
||||
//
|
||||
// The patch is a detools "in-place" patch (heatshrink 11/10, segment = one flash
|
||||
// page). detools first moves the old image up by `shift` bytes, then writes the
|
||||
// new image page by page from the start, reading old bytes only above the page
|
||||
// it is writing. Two modes, one code path:
|
||||
//
|
||||
// dry no flash is touched. Reads from the shifted area are redirected to the
|
||||
// untouched original, every page of the new image goes to `sink` in
|
||||
// order (hash + CRC). A read the real run could not serve (data already
|
||||
// overwritten) fails the run. This is `fw verify`.
|
||||
// real erase/write through the callbacks, pages buffered in RAM so the flash
|
||||
// only ever sees whole, word-aligned pages. This is `fw apply`.
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define FWP_PAGE 4096u
|
||||
|
||||
// Error codes (negative). detools errors come through as -(100 + detools code).
|
||||
#define FWP_ERR_ARGS -1 // bad job (alignment, sizes)
|
||||
#define FWP_ERR_SHIFT -2 // shift not a positive multiple of a page
|
||||
#define FWP_ERR_OVERREAD -3 // the patch reads data a real run would have overwritten
|
||||
#define FWP_ERR_ORDER -4 // a write outside the page being built
|
||||
#define FWP_ERR_SIZE -5 // header sizes do not match the job
|
||||
#define FWP_ERR_DETOOLS -100
|
||||
|
||||
typedef struct fwp_job {
|
||||
// in
|
||||
uintptr_t mem_base; // address of memory offset 0 (APP_START on the node)
|
||||
uint32_t mem_size; // must equal the patch's memory_size
|
||||
uint32_t from_size; // must equal the patch's from_size (the running image)
|
||||
const uint8_t *patch; // memory-mapped patch
|
||||
uint32_t patch_size;
|
||||
int dry;
|
||||
void (*erase)(uintptr_t addr); // real: one page
|
||||
void (*write)(uintptr_t addr, const uint8_t *src, uint32_t n); // real: one page
|
||||
void (*sink)(void *ctx, const uint8_t *p, uint32_t n); // dry: new image, in order
|
||||
void *sink_ctx;
|
||||
void (*tick)(void); // optional, called once per page (watchdog)
|
||||
|
||||
// out
|
||||
uint32_t to_size; // size of the new image
|
||||
uint32_t shift;
|
||||
int detools_err;
|
||||
|
||||
// private
|
||||
void *dt; // struct detools_apply_patch_in_place_t *
|
||||
uintptr_t page_addr; // page being built (memory offset), or UINTPTR_MAX
|
||||
uint32_t shift_low; // shifted area so far, [shift_low, shift_high) (dry)
|
||||
uint32_t shift_high;
|
||||
uint32_t written_end; // end of the new image written so far (memory offset)
|
||||
int err;
|
||||
} fwp_job;
|
||||
|
||||
// Runs the whole patch. 0 = success (to_size set), negative = FWP_ERR_*.
|
||||
int fwp_run(fwp_job *j);
|
||||
|
||||
// Reads only the patch header: memory_size, segment_size, shift_size, from_size,
|
||||
// to_size. 0 on success. Used before the transfer completes and on the PC side
|
||||
// tests.
|
||||
int fwp_header(const uint8_t *patch, uint32_t n, uint32_t *memory_size, uint32_t *segment_size,
|
||||
uint32_t *shift, uint32_t *from_size, uint32_t *to_size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
bsdiff/sais.c based on sais-lite 2.4.1 <https://sites.google.com/site/yuta256/sais>:
|
||||
Copyright (c) 2008-2010 Yuta Mori. (MIT License)
|
||||
|
||||
Other files:
|
||||
|
||||
BSD 2-Clause License
|
||||
|
||||
Copyright 2003-2005, Colin Percival (Original C implementation)
|
||||
Copyright (c) 2019, Erik Moqvist
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
@@ -0,0 +1,15 @@
|
||||
# Vendored: detools 0.53.0 (C apply side) + heatshrink decoder
|
||||
|
||||
Source: https://github.com/eerimoq/detools, commit `53d1f47` (2025-12-17),
|
||||
files `c/detools.{c,h}` and `c/heatshrink/*`, copied **unmodified**.
|
||||
Licences: detools BSD-2-Clause (`LICENSE`, includes bsdiff/sais notices),
|
||||
heatshrink ISC (`heatshrink/README.rst`).
|
||||
|
||||
Used only by `src/helpers/FwPatch.c`, which `#include`s both `.c` files and
|
||||
configures them there (heatshrink 11/10 static, heatshrink-only detools, no
|
||||
file I/O) -- so nothing in this directory needs editing. Nothing here is in any
|
||||
`build_src_filter` on its own.
|
||||
|
||||
Updating: copy the same files from a new tag, then `pio test -e native -f
|
||||
test_fwpatch` and `-e native_asan`, and `tools/ota_check_ramfunc.sh` on a
|
||||
device build (the real run executes this code from RAM).
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,640 @@
|
||||
/**
|
||||
* BSD 2-Clause License
|
||||
*
|
||||
* Copyright (c) 2019-2020, Erik Moqvist
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef DETOOLS_H
|
||||
#define DETOOLS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Configuration.
|
||||
*
|
||||
* Define any of the defines below to 0 to disable given feature.
|
||||
*/
|
||||
|
||||
#ifndef DETOOLS_CONFIG_FILE_IO
|
||||
# define DETOOLS_CONFIG_FILE_IO 1
|
||||
#endif
|
||||
|
||||
#ifndef DETOOLS_CONFIG_COMPRESSION_NONE
|
||||
# define DETOOLS_CONFIG_COMPRESSION_NONE 1
|
||||
#endif
|
||||
|
||||
#ifndef DETOOLS_CONFIG_COMPRESSION_LZMA
|
||||
# define DETOOLS_CONFIG_COMPRESSION_LZMA 1
|
||||
#endif
|
||||
|
||||
#ifndef DETOOLS_CONFIG_COMPRESSION_CRLE
|
||||
# define DETOOLS_CONFIG_COMPRESSION_CRLE 1
|
||||
#endif
|
||||
|
||||
#ifndef DETOOLS_CONFIG_COMPRESSION_HEATSHRINK
|
||||
# define DETOOLS_CONFIG_COMPRESSION_HEATSHRINK 1
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define DETOOLS_VERSION "0.53.0"
|
||||
|
||||
/* Error codes. */
|
||||
#define DETOOLS_OK 0
|
||||
#define DETOOLS_NOT_IMPLEMENTED 1
|
||||
#define DETOOLS_NOT_DONE 2
|
||||
#define DETOOLS_BAD_PATCH_TYPE 3
|
||||
#define DETOOLS_BAD_COMPRESSION 4
|
||||
#define DETOOLS_INTERNAL_ERROR 5
|
||||
#define DETOOLS_LZMA_INIT 6
|
||||
#define DETOOLS_LZMA_DECODE 7
|
||||
#define DETOOLS_OUT_OF_MEMORY 8
|
||||
#define DETOOLS_CORRUPT_PATCH 9
|
||||
#define DETOOLS_IO_FAILED 10
|
||||
#define DETOOLS_ALREADY_DONE 11
|
||||
#define DETOOLS_FILE_OPEN_FAILED 12
|
||||
#define DETOOLS_FILE_CLOSE_FAILED 13
|
||||
#define DETOOLS_FILE_READ_FAILED 14
|
||||
#define DETOOLS_FILE_WRITE_FAILED 15
|
||||
#define DETOOLS_FILE_SEEK_FAILED 16
|
||||
#define DETOOLS_FILE_TELL_FAILED 17
|
||||
#define DETOOLS_SHORT_HEADER 18
|
||||
#define DETOOLS_NOT_ENOUGH_PATCH_DATA 19
|
||||
#define DETOOLS_HEATSHRINK_SINK 20
|
||||
#define DETOOLS_HEATSHRINK_POLL 21
|
||||
#define DETOOLS_STEP_SET_FAILED 22
|
||||
#define DETOOLS_STEP_GET_FAILED 23
|
||||
#define DETOOLS_ALREADY_FAILED 24
|
||||
#define DETOOLS_CORRUPT_PATCH_OVERFLOW 25
|
||||
#define DETOOLS_CORRUPT_PATCH_CRLE_KIND 26
|
||||
#define DETOOLS_HEATSHRINK_HEADER 27
|
||||
|
||||
/**
|
||||
* Read callback.
|
||||
*
|
||||
* @param[in] arg_p User data passed to detools_apply_patch_init().
|
||||
* @param[out] buf_p Buffer to read into.
|
||||
* @param[in] size Number of bytes to read.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
typedef int (*detools_read_t)(void *arg_p, uint8_t *buf_p, size_t size);
|
||||
|
||||
/**
|
||||
* Write callback.
|
||||
*
|
||||
* @param[in] arg_p User data passed to detools_apply_patch_init().
|
||||
* @param[in] buf_p Buffer to write.
|
||||
* @param[in] size Number of bytes to write.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
typedef int (*detools_write_t)(void *arg_p, const uint8_t *buf_p, size_t size);
|
||||
|
||||
/**
|
||||
* Seek from current position callback.
|
||||
*
|
||||
* @param[in] arg_p User data passed to detools_apply_patch_init().
|
||||
* @param[in] offset Offset to seek to from current position.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
typedef int (*detools_seek_t)(void *arg_p, int offset);
|
||||
|
||||
/**
|
||||
* Memory read callback.
|
||||
*
|
||||
* @param[in] arg_p User data passed to detools_apply_patch_init().
|
||||
* @param[out] dst_p Buffer to read into.
|
||||
* @param[in] src Address to read from.
|
||||
* @param[in] size Number of bytes to read.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
typedef int (*detools_mem_read_t)(void *arg_p,
|
||||
void *dst_p,
|
||||
uintptr_t src,
|
||||
size_t size);
|
||||
|
||||
/**
|
||||
* Memory write callback.
|
||||
*
|
||||
* @param[in] arg_p User data passed to detools_apply_patch_init().
|
||||
* @param[in] dst Address to write to.
|
||||
* @param[in] addr src_p Buffer to write from.
|
||||
* @param[in] size Number of bytes to write.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
typedef int (*detools_mem_write_t)(void *arg_p,
|
||||
uintptr_t dst,
|
||||
void *src_p,
|
||||
size_t size);
|
||||
|
||||
/**
|
||||
* Memory erase callback.
|
||||
*
|
||||
* @param[in] arg_p User data passed to detools_apply_patch_init().
|
||||
* @param[in] addr Address to erase from.
|
||||
* @param[in] size Number of bytes to erase.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
typedef int (*detools_mem_erase_t)(void *arg_p, uintptr_t addr, size_t size);
|
||||
|
||||
/**
|
||||
* State read callback.
|
||||
*
|
||||
* @param[in] arg_p User data passed to detools_apply_patch_init().
|
||||
* @param[out] buf_p Buffer to read into.
|
||||
* @param[in] size Number of bytes to read.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
typedef int (*detools_state_read_t)(void *arg_p, void *buf_p, size_t size);
|
||||
|
||||
/**
|
||||
* State write callback.
|
||||
*
|
||||
* @param[in] arg_p User data passed to detools_apply_patch_init().
|
||||
* @param[in] buf_p Buffer to write.
|
||||
* @param[in] size Number of bytes to write.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
typedef int (*detools_state_write_t)(void *arg_p, const void *buf_p, size_t size);
|
||||
|
||||
/**
|
||||
* Step set callback.
|
||||
*
|
||||
* @param[in] arg_p User data passed to detools_apply_patch_init().
|
||||
* @param[in] step Step to set. Later read by the step get callback.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
typedef int (*detools_step_set_t)(void *arg_p, int step);
|
||||
|
||||
/**
|
||||
* Step get callback.
|
||||
*
|
||||
* @param[in] arg_p User data passed to detools_apply_patch_init().
|
||||
* @param[out] step_p Outputs the most recently set step by the set
|
||||
* callback, or zero(0) if not yet set.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
typedef int (*detools_step_get_t)(void *arg_p, int *step_p);
|
||||
|
||||
struct detools_apply_patch_size_t {
|
||||
int state;
|
||||
int value;
|
||||
int offset;
|
||||
bool is_signed;
|
||||
};
|
||||
|
||||
struct detools_apply_patch_patch_reader_none_t {
|
||||
size_t patch_size;
|
||||
size_t patch_offset;
|
||||
};
|
||||
|
||||
#if DETOOLS_CONFIG_COMPRESSION_LZMA == 1
|
||||
|
||||
#include <lzma.h>
|
||||
|
||||
struct detools_apply_patch_patch_reader_lzma_t {
|
||||
lzma_stream stream;
|
||||
uint8_t *input_p;
|
||||
uint8_t *output_p;
|
||||
size_t output_size;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#if DETOOLS_CONFIG_COMPRESSION_HEATSHRINK == 1
|
||||
|
||||
#include "heatshrink_decoder.h"
|
||||
|
||||
struct detools_apply_patch_patch_reader_heatshrink_t {
|
||||
int8_t window_sz2;
|
||||
int8_t lookahead_sz2;
|
||||
heatshrink_decoder *decoder_p;
|
||||
#if HEATSHRINK_DYNAMIC_ALLOC == 0
|
||||
heatshrink_decoder decoder;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
enum detools_unpack_usize_state_t {
|
||||
detools_unpack_usize_state_first_t = 0,
|
||||
detools_unpack_usize_state_consecutive_t
|
||||
};
|
||||
|
||||
struct detools_unpack_usize_t {
|
||||
enum detools_unpack_usize_state_t state;
|
||||
int value;
|
||||
int offset;
|
||||
};
|
||||
|
||||
enum detools_crle_state_t {
|
||||
detools_crle_state_idle_t = 0,
|
||||
detools_crle_state_scattered_size_t,
|
||||
detools_crle_state_scattered_data_t,
|
||||
detools_crle_state_repeated_repetitions_t,
|
||||
detools_crle_state_repeated_data_t,
|
||||
detools_crle_state_repeated_data_read_t
|
||||
};
|
||||
|
||||
struct detools_apply_patch_patch_reader_crle_t {
|
||||
enum detools_crle_state_t state;
|
||||
union {
|
||||
struct {
|
||||
size_t number_of_bytes_left;
|
||||
struct detools_unpack_usize_t size;
|
||||
} scattered;
|
||||
struct {
|
||||
uint8_t value;
|
||||
size_t number_of_bytes_left;
|
||||
struct detools_unpack_usize_t size;
|
||||
} repeated;
|
||||
} kind;
|
||||
};
|
||||
|
||||
struct detools_apply_patch_patch_reader_t {
|
||||
struct detools_apply_patch_chunk_t *patch_chunk_p;
|
||||
struct detools_apply_patch_size_t size;
|
||||
union {
|
||||
#if DETOOLS_CONFIG_COMPRESSION_NONE == 1
|
||||
struct detools_apply_patch_patch_reader_none_t none;
|
||||
#endif
|
||||
#if DETOOLS_CONFIG_COMPRESSION_LZMA == 1
|
||||
struct detools_apply_patch_patch_reader_lzma_t lzma;
|
||||
#endif
|
||||
#if DETOOLS_CONFIG_COMPRESSION_CRLE == 1
|
||||
struct detools_apply_patch_patch_reader_crle_t crle;
|
||||
#endif
|
||||
#if DETOOLS_CONFIG_COMPRESSION_HEATSHRINK == 1
|
||||
struct detools_apply_patch_patch_reader_heatshrink_t heatshrink;
|
||||
#endif
|
||||
} compression;
|
||||
int (*destroy)(struct detools_apply_patch_patch_reader_t *self_p);
|
||||
int (*decompress)(struct detools_apply_patch_patch_reader_t *self_p,
|
||||
uint8_t *buf_p,
|
||||
size_t *size_p);
|
||||
};
|
||||
|
||||
struct detools_apply_patch_chunk_t {
|
||||
const uint8_t *buf_p;
|
||||
size_t size;
|
||||
size_t offset;
|
||||
};
|
||||
|
||||
enum detools_apply_patch_state_t {
|
||||
detools_apply_patch_state_init_t = 0,
|
||||
detools_apply_patch_state_dfpatch_size_t,
|
||||
detools_apply_patch_state_diff_size_t,
|
||||
detools_apply_patch_state_diff_data_t,
|
||||
detools_apply_patch_state_extra_size_t,
|
||||
detools_apply_patch_state_extra_data_t,
|
||||
detools_apply_patch_state_adjustment_t,
|
||||
detools_apply_patch_state_done_t,
|
||||
detools_apply_patch_state_failed_t
|
||||
};
|
||||
|
||||
enum detools_apply_patch_init_state_t {
|
||||
detools_apply_patch_init_state_fixed_header_t = 0,
|
||||
detools_apply_patch_init_state_to_size_t
|
||||
};
|
||||
|
||||
/**
|
||||
* The apply patch data structure.
|
||||
*/
|
||||
struct detools_apply_patch_t {
|
||||
detools_read_t from_read;
|
||||
detools_seek_t from_seek;
|
||||
size_t patch_size;
|
||||
detools_write_t to_write;
|
||||
void *arg_p;
|
||||
enum detools_apply_patch_state_t state;
|
||||
enum detools_apply_patch_init_state_t init_state;
|
||||
int compression;
|
||||
size_t patch_offset;
|
||||
size_t to_offset;
|
||||
size_t to_size;
|
||||
int from_offset;
|
||||
size_t chunk_size;
|
||||
struct detools_apply_patch_patch_reader_t patch_reader;
|
||||
struct detools_apply_patch_chunk_t chunk;
|
||||
struct detools_apply_patch_size_t size;
|
||||
};
|
||||
|
||||
enum detools_apply_patch_in_place_init_state_t {
|
||||
detools_apply_patch_in_place_init_state_fixed_header_t = 0,
|
||||
detools_apply_patch_in_place_init_state_memory_size_t,
|
||||
detools_apply_patch_in_place_init_state_segment_size_t,
|
||||
detools_apply_patch_in_place_init_state_shift_size_t,
|
||||
detools_apply_patch_in_place_init_state_from_size_t,
|
||||
detools_apply_patch_in_place_init_state_to_size_t
|
||||
};
|
||||
|
||||
/**
|
||||
* The in-place apply patch data structure.
|
||||
*/
|
||||
struct detools_apply_patch_in_place_t {
|
||||
detools_mem_read_t mem_read;
|
||||
detools_mem_write_t mem_write;
|
||||
detools_mem_erase_t mem_erase;
|
||||
detools_step_set_t step_set;
|
||||
detools_step_get_t step_get;
|
||||
size_t patch_size;
|
||||
void *arg_p;
|
||||
enum detools_apply_patch_state_t state;
|
||||
enum detools_apply_patch_in_place_init_state_t init_state;
|
||||
int compression;
|
||||
int ongoing_step;
|
||||
size_t to_pos;
|
||||
size_t to_size;
|
||||
size_t from_size;
|
||||
size_t memory_size;
|
||||
size_t segment_size;
|
||||
size_t shift_size;
|
||||
size_t chunk_size;
|
||||
struct {
|
||||
size_t index;
|
||||
int from_offset;
|
||||
size_t to_offset;
|
||||
size_t to_size;
|
||||
size_t to_pos;
|
||||
} segment;
|
||||
struct detools_apply_patch_patch_reader_t patch_reader;
|
||||
struct detools_apply_patch_chunk_t chunk;
|
||||
struct detools_apply_patch_size_t size;
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize given apply patch object.
|
||||
*
|
||||
* @param[out] self_p Apply patch object to initialize.
|
||||
* @param[in] from_read Callback to read from-data.
|
||||
* @param[in] from_seek Callback to seek from current position in from-data.
|
||||
* @param[in] patch_size Patch size in bytes. Not used if
|
||||
* `detools_apply_patch_restore()` is called
|
||||
* immediately after this function.
|
||||
* @param[in] to_write Destination callback.
|
||||
* @param[in] arg_p Argument passed to the callbacks.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
int detools_apply_patch_init(struct detools_apply_patch_t *self_p,
|
||||
detools_read_t from_read,
|
||||
detools_seek_t from_seek,
|
||||
size_t patch_size,
|
||||
detools_write_t to_write,
|
||||
void *arg_p);
|
||||
|
||||
/**
|
||||
* Dump given apply patch object state. Call
|
||||
* `detools_apply_patch_restore()` to restore an apply patch object to
|
||||
* the dumped state.
|
||||
*
|
||||
* @param[in] self_p Apply patch object to dump.
|
||||
* @param[in] write Write callback.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
int detools_apply_patch_dump(struct detools_apply_patch_t *self_p,
|
||||
detools_state_write_t state_write);
|
||||
|
||||
/**
|
||||
* Restore given apply patch object to given dumped
|
||||
* state.
|
||||
*
|
||||
* `detools_apply_patch_get_to_offset()` and
|
||||
* `detools_apply_patch_get_patch_offset()` are often called after
|
||||
* this function to restore the to and patch streams.
|
||||
*
|
||||
* @param[in,out] self_p Initialized apply patch object to restore.
|
||||
* @param[in] read Callback to read the dumped state.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
int detools_apply_patch_restore(struct detools_apply_patch_t *self_p,
|
||||
detools_state_read_t state_read);
|
||||
|
||||
/**
|
||||
* Get the current to stream offset. Often used to restore the to
|
||||
* stream after restore.
|
||||
*
|
||||
* @param[in] self_p Apply patch object.
|
||||
*
|
||||
* @return The current to stream offset.
|
||||
*/
|
||||
size_t detools_apply_patch_get_to_offset(struct detools_apply_patch_t *self_p);
|
||||
|
||||
/**
|
||||
* Get the current patch stream offset. Often used to restore the
|
||||
* patch stream after restore.
|
||||
*
|
||||
* @param[in] self_p Apply patch object.
|
||||
*
|
||||
* @return The current patch stream offset.
|
||||
*/
|
||||
size_t detools_apply_patch_get_patch_offset(struct detools_apply_patch_t *self_p);
|
||||
|
||||
/**
|
||||
* Call this function repeatedly until all patch data has been
|
||||
* processed or an error occurres. Call detools_apply_patch_finalize()
|
||||
* to finalize the patching, even if an error occurred.
|
||||
*
|
||||
* @param[in,out] self_p Initialized apply patch object.
|
||||
* @param[in] patch_p Next chunk of the patch.
|
||||
* @param[in] size Patch buffer size.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
int detools_apply_patch_process(struct detools_apply_patch_t *self_p,
|
||||
const uint8_t *patch_p,
|
||||
size_t size);
|
||||
|
||||
/**
|
||||
* Call once after all data has been processed to finalize the
|
||||
* patching. The value returned from this function should be ignored
|
||||
* if an error occurred in detools_apply_patch_process().
|
||||
*
|
||||
* @param[in,out] self_p Initialized apply patch object.
|
||||
*
|
||||
* @return Size of to-data in bytes if the patch was applied
|
||||
* successfully, or negative error code.
|
||||
*/
|
||||
int detools_apply_patch_finalize(struct detools_apply_patch_t *self_p);
|
||||
|
||||
/**
|
||||
* Initialize given in-place apply patch object.
|
||||
*
|
||||
* @param[out] self_p In-place apply patch object to initialize.
|
||||
* @param[in] mem_read Callback to read data.
|
||||
* @param[in] mem_write Callback to write data.
|
||||
* @param[in] mem_erase Callback to erase data.
|
||||
* @param[in] step_set Callback to set the step.
|
||||
* @param[in] step_get Callback to get the step.
|
||||
* @param[in] patch_size Patch size in bytes.
|
||||
* @param[in] arg_p Argument passed to the callbacks.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
int detools_apply_patch_in_place_init(
|
||||
struct detools_apply_patch_in_place_t *self_p,
|
||||
detools_mem_read_t mem_read,
|
||||
detools_mem_write_t mem_write,
|
||||
detools_mem_erase_t mem_erase,
|
||||
detools_step_set_t step_set,
|
||||
detools_step_get_t step_get,
|
||||
size_t patch_size,
|
||||
void *arg_p);
|
||||
|
||||
/**
|
||||
* Call this function repeatedly until all patch data has been
|
||||
* processed or an error occurres. Call
|
||||
* detools_apply_patch_in_place_finalize() to finalize the patching,
|
||||
* even if an error occurred.
|
||||
*
|
||||
* @param[in,out] self_p Initialized apply patch object.
|
||||
* @param[in] patch_p Next chunk of the patch.
|
||||
* @param[in] size Patch buffer size.
|
||||
*
|
||||
* @return zero(0) or negative error code.
|
||||
*/
|
||||
int detools_apply_patch_in_place_process(
|
||||
struct detools_apply_patch_in_place_t *self_p,
|
||||
const uint8_t *patch_p,
|
||||
size_t size);
|
||||
|
||||
/**
|
||||
* Call once after all data has been processed to finalize the
|
||||
* patching. The value returned from this function should be ignored
|
||||
* if an error occurred in detools_apply_patch_in_place_process().
|
||||
*
|
||||
* @param[in,out] self_p Initialized apply patch object.
|
||||
*
|
||||
* @return Size of to-data in bytes if the patch was applied
|
||||
* successfully, or negative error code.
|
||||
*/
|
||||
int detools_apply_patch_in_place_finalize(
|
||||
struct detools_apply_patch_in_place_t *self_p);
|
||||
|
||||
/**
|
||||
* Apply given patch using read, write and seek callbacks.
|
||||
*
|
||||
* @param[in] from_read Source read callback.
|
||||
* @param[in] from_seek Source seek callback.
|
||||
* @param[in] patch_read Patch read callback.
|
||||
* @param[in] patch_size Patch size in bytes.
|
||||
* @param[in] to_write Destination write callback.
|
||||
* @param[in] arg_p Argument passed to all callbacks.
|
||||
*
|
||||
* @return Size of to-data in bytes or negative error code.
|
||||
*/
|
||||
int detools_apply_patch_callbacks(detools_read_t from_read,
|
||||
detools_seek_t from_seek,
|
||||
detools_read_t patch_read,
|
||||
size_t patch_size,
|
||||
detools_write_t to_write,
|
||||
void *arg_p);
|
||||
|
||||
/**
|
||||
* Apply given in-place patch using read, write and erase callbacks.
|
||||
*
|
||||
* @param[in] mem_read Callback to read data.
|
||||
* @param[in] mem_write Callback to write data.
|
||||
* @param[in] mem_erase Callback to erase data.
|
||||
* @param[in] step_set Callback to set the step.
|
||||
* @param[in] step_get Callback to get the step.
|
||||
* @param[in] patch_read Patch read callback.
|
||||
* @param[in] patch_size Patch size in bytes.
|
||||
* @param[in] arg_p Argument passed to the callbacks.
|
||||
*
|
||||
* @return Size of to-data in bytes or negative error code.
|
||||
*/
|
||||
int detools_apply_patch_in_place_callbacks(detools_mem_read_t mem_read,
|
||||
detools_mem_write_t mem_write,
|
||||
detools_mem_erase_t mem_erase,
|
||||
detools_step_set_t step_set,
|
||||
detools_step_get_t step_get,
|
||||
detools_read_t patch_read,
|
||||
size_t patch_size,
|
||||
void *arg_p);
|
||||
|
||||
#if DETOOLS_CONFIG_FILE_IO == 1
|
||||
|
||||
/**
|
||||
* Apply given patch file to given from file and write the output to
|
||||
* given to file.
|
||||
*
|
||||
* @param[in] from_p Source file name.
|
||||
* @param[in] patch_p Patch file name.
|
||||
* @param[in] to_p Destination file name.
|
||||
*
|
||||
* @return Size of to-data in bytes or negative error code.
|
||||
*/
|
||||
int detools_apply_patch_filenames(const char *from_p,
|
||||
const char *patch_p,
|
||||
const char *to_p);
|
||||
|
||||
/**
|
||||
* Apply given patch file to given memory file.
|
||||
*
|
||||
* @param[in] memory_p Memory file name.
|
||||
* @param[in] patch_p Patch file name.
|
||||
* @param[in] step_set Callback to set the step.
|
||||
* @param[in] step_get Callback to get the step.
|
||||
*
|
||||
* @return Size of to-data in bytes or negative error code.
|
||||
*/
|
||||
int detools_apply_patch_in_place_filenames(const char *memory_p,
|
||||
const char *patch_p,
|
||||
detools_step_set_t step_set,
|
||||
detools_step_get_t step_get);
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Get the error string for given error code.
|
||||
*
|
||||
* @param[in] Error code.
|
||||
*
|
||||
* @return Error string.
|
||||
*/
|
||||
const char *detools_error_as_string(int error);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,4 @@
|
||||
The files in this folder are copied from the heatshrink project,
|
||||
https://github.com/atomicobject/heatshrink.
|
||||
|
||||
Licensed under the ISC License.
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef HEATSHRINK_H
|
||||
#define HEATSHRINK_H
|
||||
|
||||
#define HEATSHRINK_AUTHOR "Scott Vokes <scott.vokes@atomicobject.com>"
|
||||
#define HEATSHRINK_URL "https://github.com/atomicobject/heatshrink"
|
||||
|
||||
/* Version 0.4.1 */
|
||||
#define HEATSHRINK_VERSION_MAJOR 0
|
||||
#define HEATSHRINK_VERSION_MINOR 4
|
||||
#define HEATSHRINK_VERSION_PATCH 1
|
||||
|
||||
#define HEATSHRINK_MIN_WINDOW_BITS 4
|
||||
#define HEATSHRINK_MAX_WINDOW_BITS 15
|
||||
|
||||
#define HEATSHRINK_MIN_LOOKAHEAD_BITS 3
|
||||
|
||||
#define HEATSHRINK_LITERAL_MARKER 0x01
|
||||
#define HEATSHRINK_BACKREF_MARKER 0x00
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef HEATSHRINK_CONFIG_H
|
||||
#define HEATSHRINK_CONFIG_H
|
||||
|
||||
/* Should functionality assuming dynamic allocation be used? */
|
||||
#ifndef HEATSHRINK_DYNAMIC_ALLOC
|
||||
#define HEATSHRINK_DYNAMIC_ALLOC 0
|
||||
#endif
|
||||
|
||||
#if HEATSHRINK_DYNAMIC_ALLOC
|
||||
/* Optional replacement of malloc/free */
|
||||
#define HEATSHRINK_MALLOC(SZ) malloc(SZ)
|
||||
#define HEATSHRINK_FREE(P, SZ) free(P)
|
||||
#else
|
||||
/* Required parameters for static configuration */
|
||||
#define HEATSHRINK_STATIC_INPUT_BUFFER_SIZE 256
|
||||
#define HEATSHRINK_STATIC_WINDOW_BITS 8
|
||||
#define HEATSHRINK_STATIC_LOOKAHEAD_BITS 7
|
||||
#endif
|
||||
|
||||
/* Turn on logging for debugging. */
|
||||
#define HEATSHRINK_DEBUGGING_LOGS 0
|
||||
|
||||
/* Use indexing for faster compression. (This requires additional space.) */
|
||||
#define HEATSHRINK_USE_INDEX 0
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,367 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "heatshrink_decoder.h"
|
||||
|
||||
/* States for the polling state machine. */
|
||||
typedef enum {
|
||||
HSDS_TAG_BIT, /* tag bit */
|
||||
HSDS_YIELD_LITERAL, /* ready to yield literal byte */
|
||||
HSDS_BACKREF_INDEX_MSB, /* most significant byte of index */
|
||||
HSDS_BACKREF_INDEX_LSB, /* least significant byte of index */
|
||||
HSDS_BACKREF_COUNT_MSB, /* most significant byte of count */
|
||||
HSDS_BACKREF_COUNT_LSB, /* least significant byte of count */
|
||||
HSDS_YIELD_BACKREF, /* ready to yield back-reference */
|
||||
} HSD_state;
|
||||
|
||||
#if HEATSHRINK_DEBUGGING_LOGS
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
#define LOG(...) fprintf(stderr, __VA_ARGS__)
|
||||
#define ASSERT(X) assert(X)
|
||||
static const char *state_names[] = {
|
||||
"tag_bit",
|
||||
"yield_literal",
|
||||
"backref_index_msb",
|
||||
"backref_index_lsb",
|
||||
"backref_count_msb",
|
||||
"backref_count_lsb",
|
||||
"yield_backref",
|
||||
};
|
||||
#else
|
||||
#define LOG(...) /* no-op */
|
||||
#define ASSERT(X) /* no-op */
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint8_t *buf; /* output buffer */
|
||||
size_t buf_size; /* buffer size */
|
||||
size_t *output_size; /* bytes pushed to buffer, so far */
|
||||
} output_info;
|
||||
|
||||
#define NO_BITS ((uint16_t)-1)
|
||||
|
||||
/* Forward references. */
|
||||
static uint16_t get_bits(heatshrink_decoder *hsd, uint8_t count);
|
||||
static void push_byte(heatshrink_decoder *hsd, output_info *oi, uint8_t byte);
|
||||
|
||||
#if HEATSHRINK_DYNAMIC_ALLOC
|
||||
heatshrink_decoder *heatshrink_decoder_alloc(uint16_t input_buffer_size,
|
||||
uint8_t window_sz2,
|
||||
uint8_t lookahead_sz2) {
|
||||
if ((window_sz2 < HEATSHRINK_MIN_WINDOW_BITS) ||
|
||||
(window_sz2 > HEATSHRINK_MAX_WINDOW_BITS) ||
|
||||
(input_buffer_size == 0) ||
|
||||
(lookahead_sz2 < HEATSHRINK_MIN_LOOKAHEAD_BITS) ||
|
||||
(lookahead_sz2 >= window_sz2)) {
|
||||
return NULL;
|
||||
}
|
||||
size_t buffers_sz = (1 << window_sz2) + input_buffer_size;
|
||||
size_t sz = sizeof(heatshrink_decoder) + buffers_sz;
|
||||
heatshrink_decoder *hsd = HEATSHRINK_MALLOC(sz);
|
||||
if (hsd == NULL) { return NULL; }
|
||||
hsd->input_buffer_size = input_buffer_size;
|
||||
hsd->window_sz2 = window_sz2;
|
||||
hsd->lookahead_sz2 = lookahead_sz2;
|
||||
heatshrink_decoder_reset(hsd);
|
||||
LOG("-- allocated decoder with buffer size of %zu (%zu + %u + %u)\n",
|
||||
sz, sizeof(heatshrink_decoder), (1 << window_sz2), input_buffer_size);
|
||||
return hsd;
|
||||
}
|
||||
|
||||
void heatshrink_decoder_free(heatshrink_decoder *hsd) {
|
||||
size_t buffers_sz = (1 << hsd->window_sz2) + hsd->input_buffer_size;
|
||||
size_t sz = sizeof(heatshrink_decoder) + buffers_sz;
|
||||
HEATSHRINK_FREE(hsd, sz);
|
||||
(void)sz; /* may not be used by free */
|
||||
}
|
||||
#endif
|
||||
|
||||
void heatshrink_decoder_reset(heatshrink_decoder *hsd) {
|
||||
size_t buf_sz = 1 << HEATSHRINK_DECODER_WINDOW_BITS(hsd);
|
||||
size_t input_sz = HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(hsd);
|
||||
memset(hsd->buffers, 0, buf_sz + input_sz);
|
||||
hsd->state = HSDS_TAG_BIT;
|
||||
hsd->input_size = 0;
|
||||
hsd->input_index = 0;
|
||||
hsd->bit_index = 0x00;
|
||||
hsd->current_byte = 0x00;
|
||||
hsd->output_count = 0;
|
||||
hsd->output_index = 0;
|
||||
hsd->head_index = 0;
|
||||
}
|
||||
|
||||
/* Copy SIZE bytes into the decoder's input buffer, if it will fit. */
|
||||
HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd,
|
||||
uint8_t *in_buf, size_t size, size_t *input_size) {
|
||||
if ((hsd == NULL) || (in_buf == NULL) || (input_size == NULL)) {
|
||||
return HSDR_SINK_ERROR_NULL;
|
||||
}
|
||||
|
||||
size_t rem = HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(hsd) - hsd->input_size;
|
||||
if (rem == 0) {
|
||||
*input_size = 0;
|
||||
return HSDR_SINK_FULL;
|
||||
}
|
||||
|
||||
size = rem < size ? rem : size;
|
||||
LOG("-- sinking %zd bytes\n", size);
|
||||
/* copy into input buffer (at head of buffers) */
|
||||
memcpy(&hsd->buffers[hsd->input_size], in_buf, size);
|
||||
hsd->input_size += size;
|
||||
*input_size = size;
|
||||
return HSDR_SINK_OK;
|
||||
}
|
||||
|
||||
|
||||
/*****************
|
||||
* Decompression *
|
||||
*****************/
|
||||
|
||||
#define BACKREF_COUNT_BITS(HSD) (HEATSHRINK_DECODER_LOOKAHEAD_BITS(HSD))
|
||||
#define BACKREF_INDEX_BITS(HSD) (HEATSHRINK_DECODER_WINDOW_BITS(HSD))
|
||||
|
||||
// States
|
||||
static HSD_state st_tag_bit(heatshrink_decoder *hsd);
|
||||
static HSD_state st_yield_literal(heatshrink_decoder *hsd,
|
||||
output_info *oi);
|
||||
static HSD_state st_backref_index_msb(heatshrink_decoder *hsd);
|
||||
static HSD_state st_backref_index_lsb(heatshrink_decoder *hsd);
|
||||
static HSD_state st_backref_count_msb(heatshrink_decoder *hsd);
|
||||
static HSD_state st_backref_count_lsb(heatshrink_decoder *hsd);
|
||||
static HSD_state st_yield_backref(heatshrink_decoder *hsd,
|
||||
output_info *oi);
|
||||
|
||||
HSD_poll_res heatshrink_decoder_poll(heatshrink_decoder *hsd,
|
||||
uint8_t *out_buf, size_t out_buf_size, size_t *output_size) {
|
||||
if ((hsd == NULL) || (out_buf == NULL) || (output_size == NULL)) {
|
||||
return HSDR_POLL_ERROR_NULL;
|
||||
}
|
||||
*output_size = 0;
|
||||
|
||||
output_info oi;
|
||||
oi.buf = out_buf;
|
||||
oi.buf_size = out_buf_size;
|
||||
oi.output_size = output_size;
|
||||
|
||||
while (1) {
|
||||
LOG("-- poll, state is %d (%s), input_size %d\n",
|
||||
hsd->state, state_names[hsd->state], hsd->input_size);
|
||||
uint8_t in_state = hsd->state;
|
||||
switch (in_state) {
|
||||
case HSDS_TAG_BIT:
|
||||
hsd->state = st_tag_bit(hsd);
|
||||
break;
|
||||
case HSDS_YIELD_LITERAL:
|
||||
hsd->state = st_yield_literal(hsd, &oi);
|
||||
break;
|
||||
case HSDS_BACKREF_INDEX_MSB:
|
||||
hsd->state = st_backref_index_msb(hsd);
|
||||
break;
|
||||
case HSDS_BACKREF_INDEX_LSB:
|
||||
hsd->state = st_backref_index_lsb(hsd);
|
||||
break;
|
||||
case HSDS_BACKREF_COUNT_MSB:
|
||||
hsd->state = st_backref_count_msb(hsd);
|
||||
break;
|
||||
case HSDS_BACKREF_COUNT_LSB:
|
||||
hsd->state = st_backref_count_lsb(hsd);
|
||||
break;
|
||||
case HSDS_YIELD_BACKREF:
|
||||
hsd->state = st_yield_backref(hsd, &oi);
|
||||
break;
|
||||
default:
|
||||
return HSDR_POLL_ERROR_UNKNOWN;
|
||||
}
|
||||
|
||||
/* If the current state cannot advance, check if input or output
|
||||
* buffer are exhausted. */
|
||||
if (hsd->state == in_state) {
|
||||
if (*output_size == out_buf_size) { return HSDR_POLL_MORE; }
|
||||
return HSDR_POLL_EMPTY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static HSD_state st_tag_bit(heatshrink_decoder *hsd) {
|
||||
uint32_t bits = get_bits(hsd, 1); // get tag bit
|
||||
if (bits == NO_BITS) {
|
||||
return HSDS_TAG_BIT;
|
||||
} else if (bits) {
|
||||
return HSDS_YIELD_LITERAL;
|
||||
} else if (HEATSHRINK_DECODER_WINDOW_BITS(hsd) > 8) {
|
||||
return HSDS_BACKREF_INDEX_MSB;
|
||||
} else {
|
||||
hsd->output_index = 0;
|
||||
return HSDS_BACKREF_INDEX_LSB;
|
||||
}
|
||||
}
|
||||
|
||||
static HSD_state st_yield_literal(heatshrink_decoder *hsd,
|
||||
output_info *oi) {
|
||||
/* Emit a repeated section from the window buffer, and add it (again)
|
||||
* to the window buffer. (Note that the repetition can include
|
||||
* itself.)*/
|
||||
if (*oi->output_size < oi->buf_size) {
|
||||
uint16_t byte = get_bits(hsd, 8);
|
||||
if (byte == NO_BITS) { return HSDS_YIELD_LITERAL; } /* out of input */
|
||||
uint8_t *buf = &hsd->buffers[HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(hsd)];
|
||||
uint16_t mask = (1 << HEATSHRINK_DECODER_WINDOW_BITS(hsd)) - 1;
|
||||
uint8_t c = byte & 0xFF;
|
||||
LOG("-- emitting literal byte 0x%02x ('%c')\n", c, isprint(c) ? c : '.');
|
||||
buf[hsd->head_index++ & mask] = c;
|
||||
push_byte(hsd, oi, c);
|
||||
return HSDS_TAG_BIT;
|
||||
} else {
|
||||
return HSDS_YIELD_LITERAL;
|
||||
}
|
||||
}
|
||||
|
||||
static HSD_state st_backref_index_msb(heatshrink_decoder *hsd) {
|
||||
uint8_t bit_ct = BACKREF_INDEX_BITS(hsd);
|
||||
ASSERT(bit_ct > 8);
|
||||
uint16_t bits = get_bits(hsd, bit_ct - 8);
|
||||
LOG("-- backref index (msb), got 0x%04x (+1)\n", bits);
|
||||
if (bits == NO_BITS) { return HSDS_BACKREF_INDEX_MSB; }
|
||||
hsd->output_index = bits << 8;
|
||||
return HSDS_BACKREF_INDEX_LSB;
|
||||
}
|
||||
|
||||
static HSD_state st_backref_index_lsb(heatshrink_decoder *hsd) {
|
||||
uint8_t bit_ct = BACKREF_INDEX_BITS(hsd);
|
||||
uint16_t bits = get_bits(hsd, bit_ct < 8 ? bit_ct : 8);
|
||||
LOG("-- backref index (lsb), got 0x%04x (+1)\n", bits);
|
||||
if (bits == NO_BITS) { return HSDS_BACKREF_INDEX_LSB; }
|
||||
hsd->output_index |= bits;
|
||||
hsd->output_index++;
|
||||
uint8_t br_bit_ct = BACKREF_COUNT_BITS(hsd);
|
||||
hsd->output_count = 0;
|
||||
return (br_bit_ct > 8) ? HSDS_BACKREF_COUNT_MSB : HSDS_BACKREF_COUNT_LSB;
|
||||
}
|
||||
|
||||
static HSD_state st_backref_count_msb(heatshrink_decoder *hsd) {
|
||||
uint8_t br_bit_ct = BACKREF_COUNT_BITS(hsd);
|
||||
ASSERT(br_bit_ct > 8);
|
||||
uint16_t bits = get_bits(hsd, br_bit_ct - 8);
|
||||
LOG("-- backref count (msb), got 0x%04x (+1)\n", bits);
|
||||
if (bits == NO_BITS) { return HSDS_BACKREF_COUNT_MSB; }
|
||||
hsd->output_count = bits << 8;
|
||||
return HSDS_BACKREF_COUNT_LSB;
|
||||
}
|
||||
|
||||
static HSD_state st_backref_count_lsb(heatshrink_decoder *hsd) {
|
||||
uint8_t br_bit_ct = BACKREF_COUNT_BITS(hsd);
|
||||
uint16_t bits = get_bits(hsd, br_bit_ct < 8 ? br_bit_ct : 8);
|
||||
LOG("-- backref count (lsb), got 0x%04x (+1)\n", bits);
|
||||
if (bits == NO_BITS) { return HSDS_BACKREF_COUNT_LSB; }
|
||||
hsd->output_count |= bits;
|
||||
hsd->output_count++;
|
||||
return HSDS_YIELD_BACKREF;
|
||||
}
|
||||
|
||||
static HSD_state st_yield_backref(heatshrink_decoder *hsd,
|
||||
output_info *oi) {
|
||||
size_t count = oi->buf_size - *oi->output_size;
|
||||
if (count > 0) {
|
||||
size_t i = 0;
|
||||
if (hsd->output_count < count) count = hsd->output_count;
|
||||
uint8_t *buf = &hsd->buffers[HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(hsd)];
|
||||
uint16_t mask = (1 << HEATSHRINK_DECODER_WINDOW_BITS(hsd)) - 1;
|
||||
uint16_t neg_offset = hsd->output_index;
|
||||
LOG("-- emitting %zu bytes from -%u bytes back\n", count, neg_offset);
|
||||
ASSERT(neg_offset <= mask + 1);
|
||||
ASSERT(count <= (size_t)(1 << BACKREF_COUNT_BITS(hsd)));
|
||||
|
||||
for (i=0; i<count; i++) {
|
||||
uint8_t c = buf[(hsd->head_index - neg_offset) & mask];
|
||||
push_byte(hsd, oi, c);
|
||||
buf[hsd->head_index & mask] = c;
|
||||
hsd->head_index++;
|
||||
LOG(" -- ++ 0x%02x\n", c);
|
||||
}
|
||||
hsd->output_count -= count;
|
||||
if (hsd->output_count == 0) { return HSDS_TAG_BIT; }
|
||||
}
|
||||
return HSDS_YIELD_BACKREF;
|
||||
}
|
||||
|
||||
/* Get the next COUNT bits from the input buffer, saving incremental progress.
|
||||
* Returns NO_BITS on end of input, or if more than 15 bits are requested. */
|
||||
static uint16_t get_bits(heatshrink_decoder *hsd, uint8_t count) {
|
||||
uint16_t accumulator = 0;
|
||||
int i = 0;
|
||||
if (count > 15) { return NO_BITS; }
|
||||
LOG("-- popping %u bit(s)\n", count);
|
||||
|
||||
/* If we aren't able to get COUNT bits, suspend immediately, because we
|
||||
* don't track how many bits of COUNT we've accumulated before suspend. */
|
||||
if (hsd->input_size == 0) {
|
||||
if (hsd->bit_index < (1 << (count - 1))) { return NO_BITS; }
|
||||
}
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
if (hsd->bit_index == 0x00) {
|
||||
if (hsd->input_size == 0) {
|
||||
LOG(" -- out of bits, suspending w/ accumulator of %u (0x%02x)\n",
|
||||
accumulator, accumulator);
|
||||
return NO_BITS;
|
||||
}
|
||||
hsd->current_byte = hsd->buffers[hsd->input_index++];
|
||||
LOG(" -- pulled byte 0x%02x\n", hsd->current_byte);
|
||||
if (hsd->input_index == hsd->input_size) {
|
||||
hsd->input_index = 0; /* input is exhausted */
|
||||
hsd->input_size = 0;
|
||||
}
|
||||
hsd->bit_index = 0x80;
|
||||
}
|
||||
accumulator <<= 1;
|
||||
if (hsd->current_byte & hsd->bit_index) {
|
||||
accumulator |= 0x01;
|
||||
if (0) {
|
||||
LOG(" -- got 1, accumulator 0x%04x, bit_index 0x%02x\n",
|
||||
accumulator, hsd->bit_index);
|
||||
}
|
||||
} else {
|
||||
if (0) {
|
||||
LOG(" -- got 0, accumulator 0x%04x, bit_index 0x%02x\n",
|
||||
accumulator, hsd->bit_index);
|
||||
}
|
||||
}
|
||||
hsd->bit_index >>= 1;
|
||||
}
|
||||
|
||||
if (count > 1) { LOG(" -- accumulated %08x\n", accumulator); }
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
HSD_finish_res heatshrink_decoder_finish(heatshrink_decoder *hsd) {
|
||||
if (hsd == NULL) { return HSDR_FINISH_ERROR_NULL; }
|
||||
switch (hsd->state) {
|
||||
case HSDS_TAG_BIT:
|
||||
return hsd->input_size == 0 ? HSDR_FINISH_DONE : HSDR_FINISH_MORE;
|
||||
|
||||
/* If we want to finish with no input, but are in these states, it's
|
||||
* because the 0-bit padding to the last byte looks like a backref
|
||||
* marker bit followed by all 0s for index and count bits. */
|
||||
case HSDS_BACKREF_INDEX_LSB:
|
||||
case HSDS_BACKREF_INDEX_MSB:
|
||||
case HSDS_BACKREF_COUNT_LSB:
|
||||
case HSDS_BACKREF_COUNT_MSB:
|
||||
return hsd->input_size == 0 ? HSDR_FINISH_DONE : HSDR_FINISH_MORE;
|
||||
|
||||
/* If the output stream is padded with 0xFFs (possibly due to being in
|
||||
* flash memory), also explicitly check the input size rather than
|
||||
* uselessly returning MORE but yielding 0 bytes when polling. */
|
||||
case HSDS_YIELD_LITERAL:
|
||||
return hsd->input_size == 0 ? HSDR_FINISH_DONE : HSDR_FINISH_MORE;
|
||||
|
||||
default:
|
||||
return HSDR_FINISH_MORE;
|
||||
}
|
||||
}
|
||||
|
||||
static void push_byte(heatshrink_decoder *hsd, output_info *oi, uint8_t byte) {
|
||||
LOG(" -- pushing byte: 0x%02x ('%c')\n", byte, isprint(byte) ? byte : '.');
|
||||
oi->buf[(*oi->output_size)++] = byte;
|
||||
(void)hsd;
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
#ifndef HEATSHRINK_DECODER_H
|
||||
#define HEATSHRINK_DECODER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "heatshrink_common.h"
|
||||
#include "heatshrink_config.h"
|
||||
|
||||
typedef enum {
|
||||
HSDR_SINK_OK, /* data sunk, ready to poll */
|
||||
HSDR_SINK_FULL, /* out of space in internal buffer */
|
||||
HSDR_SINK_ERROR_NULL=-1, /* NULL argument */
|
||||
} HSD_sink_res;
|
||||
|
||||
typedef enum {
|
||||
HSDR_POLL_EMPTY, /* input exhausted */
|
||||
HSDR_POLL_MORE, /* more data remaining, call again w/ fresh output buffer */
|
||||
HSDR_POLL_ERROR_NULL=-1, /* NULL arguments */
|
||||
HSDR_POLL_ERROR_UNKNOWN=-2,
|
||||
} HSD_poll_res;
|
||||
|
||||
typedef enum {
|
||||
HSDR_FINISH_DONE, /* output is done */
|
||||
HSDR_FINISH_MORE, /* more output remains */
|
||||
HSDR_FINISH_ERROR_NULL=-1, /* NULL arguments */
|
||||
} HSD_finish_res;
|
||||
|
||||
#if HEATSHRINK_DYNAMIC_ALLOC
|
||||
#define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(BUF) \
|
||||
((BUF)->input_buffer_size)
|
||||
#define HEATSHRINK_DECODER_WINDOW_BITS(BUF) \
|
||||
((BUF)->window_sz2)
|
||||
#define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF) \
|
||||
((BUF)->lookahead_sz2)
|
||||
#else
|
||||
#define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_) \
|
||||
HEATSHRINK_STATIC_INPUT_BUFFER_SIZE
|
||||
#define HEATSHRINK_DECODER_WINDOW_BITS(_) \
|
||||
(HEATSHRINK_STATIC_WINDOW_BITS)
|
||||
#define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF) \
|
||||
(HEATSHRINK_STATIC_LOOKAHEAD_BITS)
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint16_t input_size; /* bytes in input buffer */
|
||||
uint16_t input_index; /* offset to next unprocessed input byte */
|
||||
uint16_t output_count; /* how many bytes to output */
|
||||
uint16_t output_index; /* index for bytes to output */
|
||||
uint16_t head_index; /* head of window buffer */
|
||||
uint8_t state; /* current state machine node */
|
||||
uint8_t current_byte; /* current byte of input */
|
||||
uint8_t bit_index; /* current bit index */
|
||||
|
||||
#if HEATSHRINK_DYNAMIC_ALLOC
|
||||
/* Fields that are only used if dynamically allocated. */
|
||||
uint8_t window_sz2; /* window buffer bits */
|
||||
uint8_t lookahead_sz2; /* lookahead bits */
|
||||
uint16_t input_buffer_size; /* input buffer size */
|
||||
|
||||
/* Input buffer, then expansion window buffer */
|
||||
uint8_t buffers[];
|
||||
#else
|
||||
/* Input buffer, then expansion window buffer */
|
||||
uint8_t buffers[(1 << HEATSHRINK_DECODER_WINDOW_BITS(_))
|
||||
+ HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_)];
|
||||
#endif
|
||||
} heatshrink_decoder;
|
||||
|
||||
#if HEATSHRINK_DYNAMIC_ALLOC
|
||||
/* Allocate a decoder with an input buffer of INPUT_BUFFER_SIZE bytes,
|
||||
* an expansion buffer size of 2^WINDOW_SZ2, and a lookahead
|
||||
* size of 2^lookahead_sz2. (The window buffer and lookahead sizes
|
||||
* must match the settings used when the data was compressed.)
|
||||
* Returns NULL on error. */
|
||||
heatshrink_decoder *heatshrink_decoder_alloc(uint16_t input_buffer_size,
|
||||
uint8_t expansion_buffer_sz2, uint8_t lookahead_sz2);
|
||||
|
||||
/* Free a decoder. */
|
||||
void heatshrink_decoder_free(heatshrink_decoder *hsd);
|
||||
#endif
|
||||
|
||||
/* Reset a decoder. */
|
||||
void heatshrink_decoder_reset(heatshrink_decoder *hsd);
|
||||
|
||||
/* Sink at most SIZE bytes from IN_BUF into the decoder. *INPUT_SIZE is set to
|
||||
* indicate how many bytes were actually sunk (in case a buffer was filled). */
|
||||
HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd,
|
||||
uint8_t *in_buf, size_t size, size_t *input_size);
|
||||
|
||||
/* Poll for output from the decoder, copying at most OUT_BUF_SIZE bytes into
|
||||
* OUT_BUF (setting *OUTPUT_SIZE to the actual amount copied). */
|
||||
HSD_poll_res heatshrink_decoder_poll(heatshrink_decoder *hsd,
|
||||
uint8_t *out_buf, size_t out_buf_size, size_t *output_size);
|
||||
|
||||
/* Notify the dencoder that the input stream is finished.
|
||||
* If the return value is HSDR_FINISH_MORE, there is still more output, so
|
||||
* call heatshrink_decoder_poll and repeat. */
|
||||
HSD_finish_res heatshrink_decoder_finish(heatshrink_decoder *hsd);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user