From 677feaa727bccd8fbae4c852caabb87bca2f00a9 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Fri, 2 Feb 2024 16:25:54 +1300 Subject: [PATCH] Implement a scanner on C-side (LibCMP doesn't provide it). --- cbits/transcode.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/cbits/transcode.c b/cbits/transcode.c index 4efb28c..310f314 100644 --- a/cbits/transcode.c +++ b/cbits/transcode.c @@ -382,3 +382,46 @@ bool encodeFontSet(cmp_ctx_t *bytes, FcFontSet *data) { } return true; } + +struct bytes { + uint8_t *start; + size_t offset; + size_t length; +}; + +bool bytes_reader(struct cmp_ctx_s *ctx, void *data, size_t limit) { + struct bytes *bytes = ctx->buf; + if (bytes->offset + limit > bytes->length) return false; + data = bytes->start + bytes->offset; + bytes->offset += limit; + return true; +} + +bool bytes_skipper(struct cmp_ctx_s *ctx, size_t count) { + struct bytes *bytes = ctx->buf; + if (bytes->offset + count > bytes->length) return false; + bytes->offset += count; + return true; +} + +size_t bytes_writer(struct cmp_ctx_s *ctx, const void *data, size_t count) { + struct bytes *bytes = ctx->buf; + if (bytes->offset + count > bytes->length) return 0; + memcpy(bytes->start + bytes->offset, data, count); + bytes->offset += count; + return count; +} + +void cmp_bytes_init(cmp_ctx_t *ctx, uint8_t *buf, size_t length) { + struct bytes *inner = malloc(sizeof(struct bytes)); + inner->start = buf; + inner->offset = 0; + inner->length = length; + cmp_init(ctx, inner, bytes_reader, bytes_skipper, bytes_writer); +} + +void cmp_bytes_free(cmp_ctx_t *ctx) { + struct bytes *bytes = ctx->buf; + free(bytes->start); + free(bytes); +} -- 2.30.2