ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_compress.c
Go to the documentation of this file.
1
17
18#include "ra8_compress.h"
19
20#include <stddef.h>
21#include <stdint.h>
22#include <string.h>
23
24#include "miniz.h"
25#include "ra8_attributes.h"
26#include "ra8_check.h"
27#include "ra8_err.h"
28
30static const char* const s_tag = "ra8_compress";
31
37typedef struct {
38 uint8_t* out;
39 uint32_t cap;
40 uint32_t len;
41 uint8_t overflow;
43
67RA8_INTERNAL static mz_bool internal_compress_put(const void* buf, int len, void* user)
68{
70 const uint32_t n = (uint32_t)len;
71 if (n > (c->cap - c->len)) {
72 c->overflow = 1U;
73 return MZ_FALSE;
74 }
75 (void)memcpy(&c->out[c->len], buf, (size_t)n);
76 c->len += n;
77 return MZ_TRUE;
78}
79
109 const uint8_t* out,
110 const void* scratch,
111 uint32_t scratch_len,
112 const uint32_t* out_len)
113{
114 RA8_CHECK_NULL_PTR(src, s_tag, "src must not be nullptr");
115 RA8_CHECK_NULL_PTR(out, s_tag, "out must not be nullptr");
116 RA8_CHECK_NULL_PTR(scratch, s_tag, "scratch must not be nullptr");
117 RA8_CHECK_NULL_PTR(out_len, s_tag, "out_len must not be nullptr");
118 if (scratch_len < (uint32_t)k_ra8_compress_scratch_bytes) {
120 }
121 return k_ra8_ok;
122}
123
153 compress_ctx_t* ctx,
154 const uint8_t* src,
155 uint32_t src_len,
156 int flags,
157 uint32_t* out_len)
158{
159 if (tdefl_init(d, internal_compress_put, ctx, flags) != TDEFL_STATUS_OKAY) {
160 return k_ra8_fail;
161 }
162 const tdefl_status st = tdefl_compress_buffer(d, src, (size_t)src_len, TDEFL_FINISH);
163 if (ctx->overflow != 0U) {
164 return k_ra8_err_no_mem;
165 }
166 if (st != TDEFL_STATUS_DONE) {
167 return k_ra8_fail;
168 }
169 *out_len = ctx->len;
170 return k_ra8_ok;
171}
172
173ra8_err_t ra8_compress(const uint8_t* src,
174 uint32_t src_len,
175 uint8_t* out,
176 uint32_t out_cap,
177 void* scratch,
178 uint32_t scratch_len,
179 uint32_t* out_len)
180{
181 const ra8_err_t vr = internal_ra8_compress_validate(src, out, scratch, scratch_len, out_len);
182 if (vr != k_ra8_ok) {
183 return vr;
184 }
185 tdefl_compressor* d = (tdefl_compressor*)scratch;
186 compress_ctx_t ctx = {.out = out, .cap = out_cap, .len = 0, .overflow = 0};
187 return internal_ra8_compress_drive(d, &ctx, src, src_len, (int)TDEFL_DEFAULT_MAX_PROBES, out_len);
188}
189
190ra8_err_t ra8_compress_zlib(const uint8_t* src,
191 uint32_t src_len,
192 uint8_t* out,
193 uint32_t out_cap,
194 void* scratch,
195 uint32_t scratch_len,
196 uint32_t* out_len)
197{
198 const ra8_err_t vr = internal_ra8_compress_validate(src, out, scratch, scratch_len, out_len);
199 if (vr != k_ra8_ok) {
200 return vr;
201 }
202 tdefl_compressor* d = (tdefl_compressor*)scratch;
203 compress_ctx_t ctx = {.out = out, .cap = out_cap, .len = 0, .overflow = 0};
204 const int flags = (int)TDEFL_DEFAULT_MAX_PROBES | (int)TDEFL_WRITE_ZLIB_HEADER;
205 return internal_ra8_compress_drive(d, &ctx, src, src_len, flags, out_len);
206}
207
208ra8_err_t ra8_decompress(const uint8_t* src,
209 uint32_t src_len,
210 uint8_t* out,
211 uint32_t out_cap,
212 uint32_t* out_len)
213{
214 RA8_CHECK_NULL_PTR(src, s_tag, "src must not be nullptr");
215 RA8_CHECK_NULL_PTR(out, s_tag, "out must not be nullptr");
216 RA8_CHECK_NULL_PTR(out_len, s_tag, "out_len must not be nullptr");
217 const size_t n =
218 tinfl_decompress_mem_to_mem((void*)out, (size_t)out_cap, src, (size_t)src_len, 0);
219 if (n == TINFL_DECOMPRESS_MEM_TO_MEM_FAILED) {
220 return k_ra8_err_no_mem;
221 }
222 *out_len = (uint32_t)n;
223 return k_ra8_ok;
224}
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
ra8_err_t ra8_decompress(const uint8_t *src, uint32_t src_len, uint8_t *out, uint32_t out_cap, uint32_t *out_len)
Raw-DEFLATE decompress src into out.
ra8_err_t ra8_compress_zlib(const uint8_t *src, uint32_t src_len, uint8_t *out, uint32_t out_cap, void *scratch, uint32_t scratch_len, uint32_t *out_len)
Zlib-wrap and DEFLATE-compress one bounded byte range.
static ra8_err_t internal_ra8_compress_validate(const uint8_t *src, const uint8_t *out, const void *scratch, uint32_t scratch_len, const uint32_t *out_len)
Validate ra8_compress arguments before driving the compressor.
static mz_bool internal_compress_put(const void *buf, int len, void *user)
tdefl put-buffer callback: append len bytes to the bounded output.
static ra8_err_t internal_ra8_compress_drive(tdefl_compressor *d, compress_ctx_t *ctx, const uint8_t *src, uint32_t src_len, int flags, uint32_t *out_len)
Drive the miniz tdefl engine over src into the bounded sink.
ra8_err_t ra8_compress(const uint8_t *src, uint32_t src_len, uint8_t *out, uint32_t out_cap, void *scratch, uint32_t scratch_len, uint32_t *out_len)
Raw-DEFLATE compress src into out.
App-domain DEFLATE compress / decompress (zero-heap, firmware-safe).
@ k_ra8_compress_scratch_bytes
Minimum compress scratch size (one miniz tdefl_compressor).
Error Code Definitions for ra8-firmware.
@ k_ra8_err_no_mem
Static buffer exhausted (no dynamic memory on this project).
Definition ra8_err.h:142
@ k_ra8_fail
Generic unspecified failure.
Definition ra8_err.h:133
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
Definition ra8_err.h:167
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
Bounded output sink for the tdefl put-buffer callback.
uint32_t len
Bytes written so far.
uint8_t * out
Destination buffer.
uint8_t overflow
Set when out filled up.
uint32_t cap
Destination capacity.