mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
1547 lines
48 KiB
C
1547 lines
48 KiB
C
#ifndef PUFFS_GIF_H
|
|
#define PUFFS_GIF_H
|
|
|
|
// Code generated by puffs-c. DO NOT EDIT.
|
|
|
|
#ifndef PUFFS_BASE_HEADER_H
|
|
#define PUFFS_BASE_HEADER_H
|
|
|
|
// Copyright 2017 The Puffs Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
// Puffs requires a word size of at least 32 bits because it assumes that
|
|
// converting a u32 to usize will never overflow. For example, the size of a
|
|
// decoded image is often represented, explicitly or implicitly in an image
|
|
// file, as a u32, and it is convenient to compare that to a buffer size.
|
|
//
|
|
// Similarly, the word size is at most 64 bits because it assumes that
|
|
// converting a usize to u64 will never overflow.
|
|
#if __WORDSIZE < 32
|
|
#error "Puffs requires a word size of at least 32 bits"
|
|
#elif __WORDSIZE > 64
|
|
#error "Puffs requires a word size of at most 64 bits"
|
|
#endif
|
|
|
|
// PUFFS_VERSION is the major.minor version number as a uint32. The major
|
|
// number is the high 16 bits. The minor number is the low 16 bits.
|
|
//
|
|
// The intention is to bump the version number at least on every API / ABI
|
|
// backwards incompatible change.
|
|
//
|
|
// For now, the API and ABI are simply unstable and can change at any time.
|
|
//
|
|
// TODO: don't hard code this in base-header.h.
|
|
#define PUFFS_VERSION (0x00001)
|
|
|
|
// puffs_base_slice_u8 is a 1-dimensional buffer (a pointer and length).
|
|
//
|
|
// A value with all fields NULL or zero is a valid, empty slice.
|
|
typedef struct {
|
|
uint8_t* ptr;
|
|
size_t len;
|
|
} puffs_base_slice_u8;
|
|
|
|
// puffs_base_buf1 is a 1-dimensional buffer (a pointer and length), plus
|
|
// additional indexes into that buffer, plus an opened / closed flag.
|
|
//
|
|
// A value with all fields NULL or zero is a valid, empty buffer.
|
|
typedef struct {
|
|
uint8_t* ptr; // Pointer.
|
|
size_t len; // Length.
|
|
size_t wi; // Write index. Invariant: wi <= len.
|
|
size_t ri; // Read index. Invariant: ri <= wi.
|
|
bool closed; // No further writes are expected.
|
|
} puffs_base_buf1;
|
|
|
|
// puffs_base_limit1 provides a limited view of a 1-dimensional byte stream:
|
|
// its first N bytes. That N can be greater than a buffer's current read or
|
|
// write capacity. N decreases naturally over time as bytes are read from or
|
|
// written to the stream.
|
|
//
|
|
// A value with all fields NULL or zero is a valid, unlimited view.
|
|
typedef struct puffs_base_limit1 {
|
|
uint64_t* ptr_to_len; // Pointer to N.
|
|
struct puffs_base_limit1* next; // Linked list of limits.
|
|
} puffs_base_limit1;
|
|
|
|
typedef struct {
|
|
puffs_base_buf1* buf;
|
|
puffs_base_limit1 limit;
|
|
} puffs_base_reader1;
|
|
|
|
typedef struct {
|
|
puffs_base_buf1* buf;
|
|
puffs_base_limit1 limit;
|
|
} puffs_base_writer1;
|
|
|
|
#endif // PUFFS_BASE_HEADER_H
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
// ---------------- Status Codes
|
|
|
|
// Status codes are int32_t values:
|
|
// - the sign bit indicates a non-recoverable status code: an error
|
|
// - bits 10-30 hold the packageid: a namespace
|
|
// - bits 8-9 are reserved
|
|
// - bits 0-7 are a package-namespaced numeric code
|
|
//
|
|
// Do not manipulate these bits directly. Use the API functions such as
|
|
// puffs_gif_status_is_error instead.
|
|
typedef int32_t puffs_gif_status;
|
|
|
|
#define puffs_gif_packageid 1017222 // 0x000f8586
|
|
|
|
#define PUFFS_GIF_STATUS_OK 0 // 0x00000000
|
|
#define PUFFS_GIF_ERROR_BAD_PUFFS_VERSION -2147483647 // 0x80000001
|
|
#define PUFFS_GIF_ERROR_BAD_RECEIVER -2147483646 // 0x80000002
|
|
#define PUFFS_GIF_ERROR_BAD_ARGUMENT -2147483645 // 0x80000003
|
|
#define PUFFS_GIF_ERROR_INITIALIZER_NOT_CALLED -2147483644 // 0x80000004
|
|
#define PUFFS_GIF_ERROR_CLOSED_FOR_WRITES -2147483643 // 0x80000005
|
|
#define PUFFS_GIF_ERROR_UNEXPECTED_EOF -2147483642 // 0x80000006
|
|
#define PUFFS_GIF_SUSPENSION_SHORT_READ 7 // 0x00000007
|
|
#define PUFFS_GIF_SUSPENSION_SHORT_WRITE 8 // 0x00000008
|
|
|
|
#define PUFFS_GIF_ERROR_BAD_GIF_BLOCK -1105848320 // 0xbe161800
|
|
#define PUFFS_GIF_ERROR_BAD_GIF_EXTENSION_LABEL -1105848319 // 0xbe161801
|
|
#define PUFFS_GIF_ERROR_BAD_GIF_HEADER -1105848318 // 0xbe161802
|
|
#define PUFFS_GIF_ERROR_BAD_LZW_LITERAL_WIDTH -1105848317 // 0xbe161803
|
|
#define PUFFS_GIF_ERROR_TODO_UNSUPPORTED_LOCAL_COLOR_TABLE \
|
|
-1105848316 // 0xbe161804
|
|
#define PUFFS_GIF_ERROR_LZW_CODE_IS_OUT_OF_RANGE -1105848315 // 0xbe161805
|
|
#define PUFFS_GIF_ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL -1105848314 // 0xbe161806
|
|
|
|
bool puffs_gif_status_is_error(puffs_gif_status s);
|
|
|
|
const char* puffs_gif_status_string(puffs_gif_status s);
|
|
|
|
// ---------------- Public Consts
|
|
|
|
// ---------------- Structs
|
|
|
|
typedef struct {
|
|
// Do not access the private_impl's fields directly. There is no API/ABI
|
|
// compatibility or safety guarantee if you do so. Instead, use the
|
|
// puffs_gif_lzw_decoder_etc functions.
|
|
//
|
|
// In C++, these fields would be "private", but C does not support that.
|
|
//
|
|
// It is a struct, not a struct*, so that it can be stack allocated.
|
|
struct {
|
|
puffs_gif_status status;
|
|
uint32_t magic;
|
|
|
|
uint32_t f_literal_width;
|
|
uint8_t f_stack[4096];
|
|
uint8_t f_suffixes[4096];
|
|
uint16_t f_prefixes[4096];
|
|
|
|
struct {
|
|
uint32_t coro_susp_point;
|
|
uint32_t v_clear_code;
|
|
uint32_t v_end_code;
|
|
uint32_t v_save_code;
|
|
uint32_t v_prev_code;
|
|
uint32_t v_width;
|
|
uint32_t v_bits;
|
|
uint32_t v_n_bits;
|
|
uint32_t v_code;
|
|
uint32_t v_s;
|
|
uint32_t v_c;
|
|
uint64_t v_n_copied;
|
|
} c_decode[1];
|
|
} private_impl;
|
|
} puffs_gif_lzw_decoder;
|
|
|
|
typedef struct {
|
|
// Do not access the private_impl's fields directly. There is no API/ABI
|
|
// compatibility or safety guarantee if you do so. Instead, use the
|
|
// puffs_gif_decoder_etc functions.
|
|
//
|
|
// In C++, these fields would be "private", but C does not support that.
|
|
//
|
|
// It is a struct, not a struct*, so that it can be stack allocated.
|
|
struct {
|
|
puffs_gif_status status;
|
|
uint32_t magic;
|
|
|
|
uint32_t f_width;
|
|
uint32_t f_height;
|
|
uint8_t f_background_color_index;
|
|
uint8_t f_gct[768];
|
|
puffs_gif_lzw_decoder f_lzw;
|
|
|
|
struct {
|
|
uint32_t coro_susp_point;
|
|
uint8_t v_c;
|
|
} c_decode[1];
|
|
struct {
|
|
uint32_t coro_susp_point;
|
|
uint8_t v_c[6];
|
|
uint32_t v_i;
|
|
} c_decode_header[1];
|
|
struct {
|
|
uint32_t coro_susp_point;
|
|
uint8_t v_c[7];
|
|
uint32_t v_i;
|
|
uint32_t v_gct_size;
|
|
} c_decode_lsd[1];
|
|
struct {
|
|
uint32_t coro_susp_point;
|
|
uint8_t v_label;
|
|
uint8_t v_block_size;
|
|
uint64_t scratch;
|
|
} c_decode_extension[1];
|
|
struct {
|
|
uint32_t coro_susp_point;
|
|
uint8_t v_c[9];
|
|
uint32_t v_i;
|
|
bool v_interlace;
|
|
uint8_t v_lw;
|
|
uint8_t v_block_size;
|
|
uint64_t l_lzw_src;
|
|
puffs_base_reader1 v_lzw_src;
|
|
} c_decode_id[1];
|
|
} private_impl;
|
|
} puffs_gif_decoder;
|
|
|
|
// ---------------- Public Initializer Prototypes
|
|
|
|
// puffs_gif_lzw_decoder_initialize is an initializer function.
|
|
//
|
|
// It should be called before any other puffs_gif_lzw_decoder_* function.
|
|
//
|
|
// Pass PUFFS_VERSION and 0 for puffs_version and for_internal_use_only.
|
|
void puffs_gif_lzw_decoder_initialize(puffs_gif_lzw_decoder* self,
|
|
uint32_t puffs_version,
|
|
uint32_t for_internal_use_only);
|
|
|
|
// puffs_gif_decoder_initialize is an initializer function.
|
|
//
|
|
// It should be called before any other puffs_gif_decoder_* function.
|
|
//
|
|
// Pass PUFFS_VERSION and 0 for puffs_version and for_internal_use_only.
|
|
void puffs_gif_decoder_initialize(puffs_gif_decoder* self,
|
|
uint32_t puffs_version,
|
|
uint32_t for_internal_use_only);
|
|
|
|
// ---------------- Public Function Prototypes
|
|
|
|
puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self,
|
|
puffs_base_writer1 a_dst,
|
|
puffs_base_reader1 a_src);
|
|
|
|
void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self,
|
|
uint32_t a_lw);
|
|
|
|
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
|
puffs_base_writer1 a_dst,
|
|
puffs_base_reader1 a_src);
|
|
|
|
#ifdef __cplusplus
|
|
} // extern "C"
|
|
#endif
|
|
|
|
#endif // PUFFS_GIF_H
|
|
|
|
// C HEADER ENDS HERE.
|
|
|
|
#ifndef PUFFS_BASE_IMPL_H
|
|
#define PUFFS_BASE_IMPL_H
|
|
|
|
// Copyright 2017 The Puffs Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)
|
|
|
|
// Use switch cases for coroutine suspension points, similar to the technique
|
|
// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
|
|
//
|
|
// We use a trivial macro instead of an explicit assignment and case statement
|
|
// so that clang-format doesn't get confused by the unusual "case"s.
|
|
#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \
|
|
coro_susp_point = n; \
|
|
case n:;
|
|
|
|
#define PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
|
|
if (status < 0) { \
|
|
goto exit; \
|
|
} else if (status == 0) { \
|
|
goto ok; \
|
|
} \
|
|
coro_susp_point = n; \
|
|
goto suspend; \
|
|
case n:;
|
|
|
|
// Clang also defines "__GNUC__".
|
|
#if defined(__GNUC__)
|
|
#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))
|
|
#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))
|
|
// Declare the printf prototype. The generated code shouldn't need this at all,
|
|
// but it's useful for manual printf debugging.
|
|
extern int printf(const char* __restrict __format, ...);
|
|
#else
|
|
#define PUFFS_LIKELY(expr) (expr)
|
|
#define PUFFS_UNLIKELY(expr) (expr)
|
|
#endif
|
|
|
|
// ---------------- Static Inline Functions
|
|
//
|
|
// The helpers below are functions, instead of macros, because their arguments
|
|
// can be an expression that we shouldn't evaluate more than once.
|
|
//
|
|
// They are in base-impl.h and hence copy/pasted into every generated C file,
|
|
// instead of being in some "base.c" file, since a design goal is that users of
|
|
// the generated C code can often just #include a single .c file, such as
|
|
// "gif.c", without having to additionally include or otherwise build and link
|
|
// a "base.c" file.
|
|
//
|
|
// They are static, so that linking multiple puffs .o files won't complain about
|
|
// duplicate function definitions.
|
|
//
|
|
// They are explicitly marked inline, even if modern compilers don't use the
|
|
// inline attribute to guide optimizations such as inlining, to avoid the
|
|
// -Wunused-function warning, and we like to compile with -Wall -Werror.
|
|
|
|
static inline uint16_t puffs_base_load_u16be(uint8_t* p) {
|
|
return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0);
|
|
}
|
|
|
|
static inline uint16_t puffs_base_load_u16le(uint8_t* p) {
|
|
return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);
|
|
}
|
|
|
|
static inline uint32_t puffs_base_load_u32be(uint8_t* p) {
|
|
return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |
|
|
((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);
|
|
}
|
|
|
|
static inline uint32_t puffs_base_load_u32le(uint8_t* p) {
|
|
return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |
|
|
((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);
|
|
}
|
|
|
|
static inline puffs_base_slice_u8 puffs_base_slice_u8_subslice_i(
|
|
puffs_base_slice_u8 s,
|
|
uint64_t i) {
|
|
if ((i <= SIZE_MAX) && (i <= s.len)) {
|
|
return ((puffs_base_slice_u8){
|
|
.ptr = s.ptr + i,
|
|
.len = s.len - i,
|
|
});
|
|
}
|
|
return ((puffs_base_slice_u8){});
|
|
}
|
|
|
|
static inline puffs_base_slice_u8 puffs_base_slice_u8_subslice_j(
|
|
puffs_base_slice_u8 s,
|
|
uint64_t j) {
|
|
if ((j <= SIZE_MAX) && (j <= s.len)) {
|
|
return ((puffs_base_slice_u8){.ptr = s.ptr, .len = j});
|
|
}
|
|
return ((puffs_base_slice_u8){});
|
|
}
|
|
|
|
static inline puffs_base_slice_u8
|
|
puffs_base_slice_u8_subslice_ij(puffs_base_slice_u8 s, uint64_t i, uint64_t j) {
|
|
if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {
|
|
return ((puffs_base_slice_u8){
|
|
.ptr = s.ptr + i,
|
|
.len = j - i,
|
|
});
|
|
}
|
|
return ((puffs_base_slice_u8){});
|
|
}
|
|
|
|
// puffs_base_slice_u8_prefix returns up to the first up_to bytes of s.
|
|
static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(
|
|
puffs_base_slice_u8 s,
|
|
uint64_t up_to) {
|
|
if ((uint64_t)(s.len) > up_to) {
|
|
s.len = up_to;
|
|
}
|
|
return s;
|
|
}
|
|
|
|
// puffs_base_slice_u8_suffix returns up to the last up_to bytes of s.
|
|
static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(
|
|
puffs_base_slice_u8 s,
|
|
uint64_t up_to) {
|
|
if ((uint64_t)(s.len) > up_to) {
|
|
s.ptr += (uint64_t)(s.len) - up_to;
|
|
s.len = up_to;
|
|
}
|
|
return s;
|
|
}
|
|
|
|
// puffs_base_slice_u8_copy_from_slice calls memmove(dst.ptr, src.ptr, length)
|
|
// where length is the minimum of dst.len and src.len.
|
|
//
|
|
// Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty
|
|
// slice) is valid and results in a no-op.
|
|
static inline uint64_t puffs_base_slice_u8_copy_from_slice(
|
|
puffs_base_slice_u8 dst,
|
|
puffs_base_slice_u8 src) {
|
|
size_t length = dst.len < src.len ? dst.len : src.len;
|
|
if (length > 0) {
|
|
memmove(dst.ptr, src.ptr, length);
|
|
}
|
|
return length;
|
|
}
|
|
|
|
static inline uint32_t puffs_base_writer1_copy_from_history32(uint8_t** ptr_ptr,
|
|
uint8_t* start,
|
|
uint8_t* end,
|
|
uint32_t distance,
|
|
uint32_t length) {
|
|
uint8_t* ptr = *ptr_ptr;
|
|
size_t d = ptr - start;
|
|
if ((d == 0) || (d < (size_t)(distance))) {
|
|
return 0;
|
|
}
|
|
start = ptr - distance;
|
|
size_t n = end - ptr;
|
|
if ((size_t)(length) > n) {
|
|
length = n;
|
|
} else {
|
|
n = length;
|
|
}
|
|
// TODO: is manual unrolling actually helpful?
|
|
for (; n >= 8; n -= 8) {
|
|
*ptr++ = *start++;
|
|
*ptr++ = *start++;
|
|
*ptr++ = *start++;
|
|
*ptr++ = *start++;
|
|
*ptr++ = *start++;
|
|
*ptr++ = *start++;
|
|
*ptr++ = *start++;
|
|
*ptr++ = *start++;
|
|
}
|
|
for (; n; n--) {
|
|
*ptr++ = *start++;
|
|
}
|
|
*ptr_ptr = ptr;
|
|
return length;
|
|
}
|
|
|
|
static inline uint32_t puffs_base_writer1_copy_from_reader32(uint8_t** ptr_wptr,
|
|
uint8_t* wend,
|
|
uint8_t** ptr_rptr,
|
|
uint8_t* rend,
|
|
uint32_t length) {
|
|
uint8_t* wptr = *ptr_wptr;
|
|
size_t n = length;
|
|
if (n > wend - wptr) {
|
|
n = wend - wptr;
|
|
}
|
|
uint8_t* rptr = *ptr_rptr;
|
|
if (n > rend - rptr) {
|
|
n = rend - rptr;
|
|
}
|
|
if (n > 0) {
|
|
memmove(wptr, rptr, n);
|
|
*ptr_wptr += n;
|
|
*ptr_rptr += n;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
static inline uint64_t puffs_base_writer1_copy_from_slice(
|
|
uint8_t** ptr_wptr,
|
|
uint8_t* wend,
|
|
puffs_base_slice_u8 src) {
|
|
uint8_t* wptr = *ptr_wptr;
|
|
size_t n = src.len;
|
|
if (n > wend - wptr) {
|
|
n = wend - wptr;
|
|
}
|
|
if (n > 0) {
|
|
memmove(wptr, src.ptr, n);
|
|
*ptr_wptr += n;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
static inline uint32_t puffs_base_writer1_copy_from_slice32(
|
|
uint8_t** ptr_wptr,
|
|
uint8_t* wend,
|
|
puffs_base_slice_u8 src,
|
|
uint32_t length) {
|
|
uint8_t* wptr = *ptr_wptr;
|
|
size_t n = src.len;
|
|
if (n > length) {
|
|
n = length;
|
|
}
|
|
if (n > wend - wptr) {
|
|
n = wend - wptr;
|
|
}
|
|
if (n > 0) {
|
|
memmove(wptr, src.ptr, n);
|
|
*ptr_wptr += n;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
#endif // PUFFS_BASE_IMPL_H
|
|
|
|
// ---------------- Status Codes Implementations
|
|
|
|
bool puffs_gif_status_is_error(puffs_gif_status s) {
|
|
return s < 0;
|
|
}
|
|
|
|
const char* puffs_gif_status_strings0[9] = {
|
|
"gif: ok",
|
|
"gif: bad puffs version",
|
|
"gif: bad receiver",
|
|
"gif: bad argument",
|
|
"gif: initializer not called",
|
|
"gif: closed for writes",
|
|
"gif: unexpected EOF",
|
|
"gif: short read",
|
|
"gif: short write",
|
|
};
|
|
|
|
const char* puffs_gif_status_strings1[7] = {
|
|
"gif: bad GIF block",
|
|
"gif: bad GIF extension label",
|
|
"gif: bad GIF header",
|
|
"gif: bad LZW literal width",
|
|
"gif: TODO: unsupported Local Color Table",
|
|
"gif: LZW code is out of range",
|
|
"gif: LZW prefix chain is cyclical",
|
|
};
|
|
|
|
const char* puffs_gif_status_string(puffs_gif_status s) {
|
|
const char** a = NULL;
|
|
uint32_t n = 0;
|
|
switch ((s >> 10) & 0x1fffff) {
|
|
case 0:
|
|
a = puffs_gif_status_strings0;
|
|
n = 9;
|
|
break;
|
|
case puffs_gif_packageid:
|
|
a = puffs_gif_status_strings1;
|
|
n = 7;
|
|
break;
|
|
}
|
|
uint32_t i = s & 0xff;
|
|
return i < n ? a[i] : "gif: unknown status";
|
|
}
|
|
|
|
// ---------------- Private Consts
|
|
|
|
// ---------------- Private Initializer Prototypes
|
|
|
|
// ---------------- Private Function Prototypes
|
|
|
|
puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self,
|
|
puffs_base_reader1 a_src);
|
|
|
|
puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self,
|
|
puffs_base_reader1 a_src);
|
|
|
|
puffs_gif_status puffs_gif_decoder_decode_extension(puffs_gif_decoder* self,
|
|
puffs_base_reader1 a_src);
|
|
|
|
puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self,
|
|
puffs_base_writer1 a_dst,
|
|
puffs_base_reader1 a_src);
|
|
|
|
// ---------------- Initializer Implementations
|
|
|
|
// PUFFS_MAGIC is a magic number to check that initializers are called. It's
|
|
// not foolproof, given C doesn't automatically zero memory before use, but it
|
|
// should catch 99.99% of cases.
|
|
//
|
|
// Its (non-zero) value is arbitrary, based on md5sum("puffs").
|
|
#define PUFFS_MAGIC (0xCB3699CCU)
|
|
|
|
// PUFFS_ALREADY_ZEROED is passed from a container struct's initializer to a
|
|
// containee struct's initializer when the container has already zeroed the
|
|
// containee's memory.
|
|
//
|
|
// Its (non-zero) value is arbitrary, based on md5sum("zeroed").
|
|
#define PUFFS_ALREADY_ZEROED (0x68602EF1U)
|
|
|
|
void puffs_gif_lzw_decoder_initialize(puffs_gif_lzw_decoder* self,
|
|
uint32_t puffs_version,
|
|
uint32_t for_internal_use_only) {
|
|
if (!self) {
|
|
return;
|
|
}
|
|
if (puffs_version != PUFFS_VERSION) {
|
|
self->private_impl.status = PUFFS_GIF_ERROR_BAD_PUFFS_VERSION;
|
|
return;
|
|
}
|
|
if (for_internal_use_only != PUFFS_ALREADY_ZEROED) {
|
|
memset(self, 0, sizeof(*self));
|
|
}
|
|
self->private_impl.magic = PUFFS_MAGIC;
|
|
self->private_impl.f_literal_width = 8;
|
|
}
|
|
|
|
void puffs_gif_decoder_initialize(puffs_gif_decoder* self,
|
|
uint32_t puffs_version,
|
|
uint32_t for_internal_use_only) {
|
|
if (!self) {
|
|
return;
|
|
}
|
|
if (puffs_version != PUFFS_VERSION) {
|
|
self->private_impl.status = PUFFS_GIF_ERROR_BAD_PUFFS_VERSION;
|
|
return;
|
|
}
|
|
if (for_internal_use_only != PUFFS_ALREADY_ZEROED) {
|
|
memset(self, 0, sizeof(*self));
|
|
}
|
|
self->private_impl.magic = PUFFS_MAGIC;
|
|
puffs_gif_lzw_decoder_initialize(&self->private_impl.f_lzw, PUFFS_VERSION,
|
|
PUFFS_ALREADY_ZEROED);
|
|
}
|
|
|
|
// ---------------- Function Implementations
|
|
|
|
puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self,
|
|
puffs_base_writer1 a_dst,
|
|
puffs_base_reader1 a_src) {
|
|
if (!self) {
|
|
return PUFFS_GIF_ERROR_BAD_RECEIVER;
|
|
}
|
|
if (self->private_impl.magic != PUFFS_MAGIC) {
|
|
self->private_impl.status = PUFFS_GIF_ERROR_INITIALIZER_NOT_CALLED;
|
|
}
|
|
if (self->private_impl.status < 0) {
|
|
return self->private_impl.status;
|
|
}
|
|
puffs_gif_status status = PUFFS_GIF_STATUS_OK;
|
|
|
|
uint8_t v_c;
|
|
|
|
uint8_t* b_rptr_src = NULL;
|
|
uint8_t* b_rstart_src = NULL;
|
|
uint8_t* b_rend_src = NULL;
|
|
if (a_src.buf) {
|
|
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
|
b_rstart_src = b_rptr_src;
|
|
size_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
|
len = *lim->ptr_to_len;
|
|
}
|
|
}
|
|
b_rend_src = b_rptr_src + len;
|
|
}
|
|
|
|
uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point;
|
|
if (coro_susp_point) {
|
|
v_c = self->private_impl.c_decode[0].v_c;
|
|
}
|
|
switch (coro_susp_point) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
|
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(1);
|
|
if (a_src.buf) {
|
|
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
|
a_src.buf->ri += n;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
}
|
|
status = puffs_gif_decoder_decode_header(self, a_src);
|
|
if (a_src.buf) {
|
|
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
|
size_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
|
len = *lim->ptr_to_len;
|
|
}
|
|
}
|
|
b_rend_src = b_rptr_src + len;
|
|
}
|
|
if (status) {
|
|
goto suspend;
|
|
}
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(2);
|
|
if (a_src.buf) {
|
|
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
|
a_src.buf->ri += n;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
}
|
|
status = puffs_gif_decoder_decode_lsd(self, a_src);
|
|
if (a_src.buf) {
|
|
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
|
size_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
|
len = *lim->ptr_to_len;
|
|
}
|
|
}
|
|
b_rend_src = b_rptr_src + len;
|
|
}
|
|
if (status) {
|
|
goto suspend;
|
|
}
|
|
while (true) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_0 = *b_rptr_src++;
|
|
v_c = t_0;
|
|
if (v_c == 33) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(4);
|
|
if (a_src.buf) {
|
|
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
|
a_src.buf->ri += n;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
}
|
|
status = puffs_gif_decoder_decode_extension(self, a_src);
|
|
if (a_src.buf) {
|
|
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
|
size_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
|
len = *lim->ptr_to_len;
|
|
}
|
|
}
|
|
b_rend_src = b_rptr_src + len;
|
|
}
|
|
if (status) {
|
|
goto suspend;
|
|
}
|
|
} else if (v_c == 44) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(5);
|
|
if (a_src.buf) {
|
|
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
|
a_src.buf->ri += n;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
}
|
|
status = puffs_gif_decoder_decode_id(self, a_dst, a_src);
|
|
if (a_src.buf) {
|
|
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
|
size_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
|
len = *lim->ptr_to_len;
|
|
}
|
|
}
|
|
b_rend_src = b_rptr_src + len;
|
|
}
|
|
if (status) {
|
|
goto suspend;
|
|
}
|
|
} else if (v_c == 59) {
|
|
status = PUFFS_GIF_STATUS_OK;
|
|
goto ok;
|
|
} else {
|
|
status = PUFFS_GIF_ERROR_BAD_GIF_BLOCK;
|
|
goto exit;
|
|
}
|
|
}
|
|
|
|
goto ok;
|
|
ok:
|
|
self->private_impl.c_decode[0].coro_susp_point = 0;
|
|
goto exit;
|
|
}
|
|
|
|
goto suspend;
|
|
suspend:
|
|
self->private_impl.c_decode[0].coro_susp_point = coro_susp_point;
|
|
self->private_impl.c_decode[0].v_c = v_c;
|
|
|
|
exit:
|
|
if (a_src.buf) {
|
|
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
|
a_src.buf->ri += n;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
|
}
|
|
|
|
self->private_impl.status = status;
|
|
return status;
|
|
|
|
short_read_src:
|
|
if (a_src.buf && a_src.buf->closed && !a_src.limit.ptr_to_len) {
|
|
status = PUFFS_GIF_ERROR_UNEXPECTED_EOF;
|
|
goto exit;
|
|
}
|
|
status = PUFFS_GIF_SUSPENSION_SHORT_READ;
|
|
goto suspend;
|
|
}
|
|
|
|
puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self,
|
|
puffs_base_reader1 a_src) {
|
|
puffs_gif_status status = PUFFS_GIF_STATUS_OK;
|
|
|
|
uint8_t v_c[6];
|
|
uint32_t v_i;
|
|
|
|
uint8_t* b_rptr_src = NULL;
|
|
uint8_t* b_rstart_src = NULL;
|
|
uint8_t* b_rend_src = NULL;
|
|
if (a_src.buf) {
|
|
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
|
b_rstart_src = b_rptr_src;
|
|
size_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
|
len = *lim->ptr_to_len;
|
|
}
|
|
}
|
|
b_rend_src = b_rptr_src + len;
|
|
}
|
|
|
|
uint32_t coro_susp_point =
|
|
self->private_impl.c_decode_header[0].coro_susp_point;
|
|
if (coro_susp_point) {
|
|
memcpy(v_c, self->private_impl.c_decode_header[0].v_c, sizeof(v_c));
|
|
v_i = self->private_impl.c_decode_header[0].v_i;
|
|
}
|
|
switch (coro_susp_point) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
|
|
|
memset(v_c, 0, sizeof(v_c));
|
|
v_i = 0;
|
|
while (v_i < 6) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(1);
|
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_0 = *b_rptr_src++;
|
|
v_c[v_i] = t_0;
|
|
v_i += 1;
|
|
}
|
|
if ((v_c[0] != 71) || (v_c[1] != 73) || (v_c[2] != 70) || (v_c[3] != 56) ||
|
|
((v_c[4] != 55) && (v_c[4] != 57)) || (v_c[5] != 97)) {
|
|
status = PUFFS_GIF_ERROR_BAD_GIF_HEADER;
|
|
goto exit;
|
|
}
|
|
|
|
goto ok;
|
|
ok:
|
|
self->private_impl.c_decode_header[0].coro_susp_point = 0;
|
|
goto exit;
|
|
}
|
|
|
|
goto suspend;
|
|
suspend:
|
|
self->private_impl.c_decode_header[0].coro_susp_point = coro_susp_point;
|
|
memcpy(self->private_impl.c_decode_header[0].v_c, v_c, sizeof(v_c));
|
|
self->private_impl.c_decode_header[0].v_i = v_i;
|
|
|
|
exit:
|
|
if (a_src.buf) {
|
|
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
|
a_src.buf->ri += n;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
|
}
|
|
|
|
return status;
|
|
|
|
short_read_src:
|
|
if (a_src.buf && a_src.buf->closed && !a_src.limit.ptr_to_len) {
|
|
status = PUFFS_GIF_ERROR_UNEXPECTED_EOF;
|
|
goto exit;
|
|
}
|
|
status = PUFFS_GIF_SUSPENSION_SHORT_READ;
|
|
goto suspend;
|
|
}
|
|
|
|
puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self,
|
|
puffs_base_reader1 a_src) {
|
|
puffs_gif_status status = PUFFS_GIF_STATUS_OK;
|
|
|
|
uint8_t v_c[7];
|
|
uint32_t v_i;
|
|
uint32_t v_gct_size;
|
|
|
|
uint8_t* b_rptr_src = NULL;
|
|
uint8_t* b_rstart_src = NULL;
|
|
uint8_t* b_rend_src = NULL;
|
|
if (a_src.buf) {
|
|
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
|
b_rstart_src = b_rptr_src;
|
|
size_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
|
len = *lim->ptr_to_len;
|
|
}
|
|
}
|
|
b_rend_src = b_rptr_src + len;
|
|
}
|
|
|
|
uint32_t coro_susp_point = self->private_impl.c_decode_lsd[0].coro_susp_point;
|
|
if (coro_susp_point) {
|
|
memcpy(v_c, self->private_impl.c_decode_lsd[0].v_c, sizeof(v_c));
|
|
v_i = self->private_impl.c_decode_lsd[0].v_i;
|
|
v_gct_size = self->private_impl.c_decode_lsd[0].v_gct_size;
|
|
}
|
|
switch (coro_susp_point) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
|
|
|
memset(v_c, 0, sizeof(v_c));
|
|
v_i = 0;
|
|
while (v_i < 7) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(1);
|
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_0 = *b_rptr_src++;
|
|
v_c[v_i] = t_0;
|
|
v_i += 1;
|
|
}
|
|
self->private_impl.f_width =
|
|
(((uint32_t)(v_c[0])) | (((uint32_t)(v_c[1])) << 8));
|
|
self->private_impl.f_height =
|
|
(((uint32_t)(v_c[2])) | (((uint32_t)(v_c[3])) << 8));
|
|
self->private_impl.f_background_color_index = v_c[5];
|
|
if ((v_c[4] & 128) != 0) {
|
|
v_gct_size = (((uint32_t)(1)) << (1 + (v_c[4] & 7)));
|
|
v_i = 0;
|
|
while (v_i < v_gct_size) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(2);
|
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_1 = *b_rptr_src++;
|
|
self->private_impl.f_gct[(3 * v_i) + 0] = t_1;
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_2 = *b_rptr_src++;
|
|
self->private_impl.f_gct[(3 * v_i) + 1] = t_2;
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(4);
|
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_3 = *b_rptr_src++;
|
|
self->private_impl.f_gct[(3 * v_i) + 2] = t_3;
|
|
v_i += 1;
|
|
}
|
|
}
|
|
|
|
goto ok;
|
|
ok:
|
|
self->private_impl.c_decode_lsd[0].coro_susp_point = 0;
|
|
goto exit;
|
|
}
|
|
|
|
goto suspend;
|
|
suspend:
|
|
self->private_impl.c_decode_lsd[0].coro_susp_point = coro_susp_point;
|
|
memcpy(self->private_impl.c_decode_lsd[0].v_c, v_c, sizeof(v_c));
|
|
self->private_impl.c_decode_lsd[0].v_i = v_i;
|
|
self->private_impl.c_decode_lsd[0].v_gct_size = v_gct_size;
|
|
|
|
exit:
|
|
if (a_src.buf) {
|
|
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
|
a_src.buf->ri += n;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
|
}
|
|
|
|
return status;
|
|
|
|
short_read_src:
|
|
if (a_src.buf && a_src.buf->closed && !a_src.limit.ptr_to_len) {
|
|
status = PUFFS_GIF_ERROR_UNEXPECTED_EOF;
|
|
goto exit;
|
|
}
|
|
status = PUFFS_GIF_SUSPENSION_SHORT_READ;
|
|
goto suspend;
|
|
}
|
|
|
|
puffs_gif_status puffs_gif_decoder_decode_extension(puffs_gif_decoder* self,
|
|
puffs_base_reader1 a_src) {
|
|
puffs_gif_status status = PUFFS_GIF_STATUS_OK;
|
|
|
|
uint8_t v_label;
|
|
uint8_t v_block_size;
|
|
|
|
uint8_t* b_rptr_src = NULL;
|
|
uint8_t* b_rstart_src = NULL;
|
|
uint8_t* b_rend_src = NULL;
|
|
if (a_src.buf) {
|
|
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
|
b_rstart_src = b_rptr_src;
|
|
size_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
|
len = *lim->ptr_to_len;
|
|
}
|
|
}
|
|
b_rend_src = b_rptr_src + len;
|
|
}
|
|
|
|
uint32_t coro_susp_point =
|
|
self->private_impl.c_decode_extension[0].coro_susp_point;
|
|
if (coro_susp_point) {
|
|
v_label = self->private_impl.c_decode_extension[0].v_label;
|
|
v_block_size = self->private_impl.c_decode_extension[0].v_block_size;
|
|
}
|
|
switch (coro_susp_point) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
|
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(1);
|
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_0 = *b_rptr_src++;
|
|
v_label = t_0;
|
|
if (v_label == 1) {
|
|
} else if (v_label == 249) {
|
|
} else if (v_label == 254) {
|
|
} else if (v_label == 255) {
|
|
} else {
|
|
status = PUFFS_GIF_ERROR_BAD_GIF_EXTENSION_LABEL;
|
|
goto exit;
|
|
}
|
|
while (true) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(2);
|
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_1 = *b_rptr_src++;
|
|
v_block_size = t_1;
|
|
if (v_block_size == 0) {
|
|
goto label_0_break;
|
|
}
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
|
self->private_impl.c_decode_extension[0].scratch =
|
|
((uint32_t)(v_block_size));
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(4);
|
|
if (self->private_impl.c_decode_extension[0].scratch >
|
|
b_rend_src - b_rptr_src) {
|
|
self->private_impl.c_decode_extension[0].scratch -=
|
|
b_rend_src - b_rptr_src;
|
|
b_rptr_src = b_rend_src;
|
|
goto short_read_src;
|
|
}
|
|
b_rptr_src += self->private_impl.c_decode_extension[0].scratch;
|
|
}
|
|
label_0_break:;
|
|
|
|
goto ok;
|
|
ok:
|
|
self->private_impl.c_decode_extension[0].coro_susp_point = 0;
|
|
goto exit;
|
|
}
|
|
|
|
goto suspend;
|
|
suspend:
|
|
self->private_impl.c_decode_extension[0].coro_susp_point = coro_susp_point;
|
|
self->private_impl.c_decode_extension[0].v_label = v_label;
|
|
self->private_impl.c_decode_extension[0].v_block_size = v_block_size;
|
|
|
|
exit:
|
|
if (a_src.buf) {
|
|
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
|
a_src.buf->ri += n;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
|
}
|
|
|
|
return status;
|
|
|
|
short_read_src:
|
|
if (a_src.buf && a_src.buf->closed && !a_src.limit.ptr_to_len) {
|
|
status = PUFFS_GIF_ERROR_UNEXPECTED_EOF;
|
|
goto exit;
|
|
}
|
|
status = PUFFS_GIF_SUSPENSION_SHORT_READ;
|
|
goto suspend;
|
|
}
|
|
|
|
puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self,
|
|
puffs_base_writer1 a_dst,
|
|
puffs_base_reader1 a_src) {
|
|
puffs_gif_status status = PUFFS_GIF_STATUS_OK;
|
|
|
|
uint8_t v_c[9];
|
|
uint32_t v_i;
|
|
bool v_interlace;
|
|
uint8_t v_lw;
|
|
uint8_t v_block_size;
|
|
uint64_t l_lzw_src;
|
|
puffs_base_reader1 v_lzw_src;
|
|
|
|
uint8_t* b_rptr_src = NULL;
|
|
uint8_t* b_rstart_src = NULL;
|
|
uint8_t* b_rend_src = NULL;
|
|
if (a_src.buf) {
|
|
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
|
b_rstart_src = b_rptr_src;
|
|
size_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
|
len = *lim->ptr_to_len;
|
|
}
|
|
}
|
|
b_rend_src = b_rptr_src + len;
|
|
}
|
|
|
|
uint32_t coro_susp_point = self->private_impl.c_decode_id[0].coro_susp_point;
|
|
if (coro_susp_point) {
|
|
memcpy(v_c, self->private_impl.c_decode_id[0].v_c, sizeof(v_c));
|
|
v_i = self->private_impl.c_decode_id[0].v_i;
|
|
v_interlace = self->private_impl.c_decode_id[0].v_interlace;
|
|
v_lw = self->private_impl.c_decode_id[0].v_lw;
|
|
v_block_size = self->private_impl.c_decode_id[0].v_block_size;
|
|
l_lzw_src = self->private_impl.c_decode_id[0].l_lzw_src;
|
|
v_lzw_src = self->private_impl.c_decode_id[0].v_lzw_src;
|
|
}
|
|
switch (coro_susp_point) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
|
|
|
memset(v_c, 0, sizeof(v_c));
|
|
v_i = 0;
|
|
while (v_i < 9) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(1);
|
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_0 = *b_rptr_src++;
|
|
v_c[v_i] = t_0;
|
|
v_i += 1;
|
|
}
|
|
v_interlace = ((v_c[8] & 64) != 0);
|
|
if (v_interlace) {
|
|
}
|
|
if ((v_c[8] & 128) != 0) {
|
|
status = PUFFS_GIF_ERROR_TODO_UNSUPPORTED_LOCAL_COLOR_TABLE;
|
|
goto exit;
|
|
}
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(2);
|
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_1 = *b_rptr_src++;
|
|
v_lw = t_1;
|
|
if ((v_lw < 2) || (8 < v_lw)) {
|
|
status = PUFFS_GIF_ERROR_BAD_LZW_LITERAL_WIDTH;
|
|
goto exit;
|
|
}
|
|
puffs_gif_lzw_decoder_set_literal_width(&self->private_impl.f_lzw,
|
|
((uint32_t)(v_lw)));
|
|
while (true) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_2 = *b_rptr_src++;
|
|
v_block_size = t_2;
|
|
if (v_block_size == 0) {
|
|
goto label_0_break;
|
|
}
|
|
l_lzw_src = ((uint64_t)(v_block_size));
|
|
v_lzw_src = (puffs_base_reader1){.buf = a_src.buf,
|
|
.limit = (puffs_base_limit1){
|
|
.ptr_to_len = &l_lzw_src,
|
|
.next = &a_src.limit,
|
|
}};
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(4);
|
|
if (a_src.buf) {
|
|
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
|
a_src.buf->ri += n;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
}
|
|
status = puffs_gif_lzw_decoder_decode(&self->private_impl.f_lzw, a_dst,
|
|
v_lzw_src);
|
|
if (a_src.buf) {
|
|
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
|
size_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
|
len = *lim->ptr_to_len;
|
|
}
|
|
}
|
|
b_rend_src = b_rptr_src + len;
|
|
}
|
|
if (l_lzw_src && status) {
|
|
goto suspend;
|
|
}
|
|
}
|
|
label_0_break:;
|
|
|
|
goto ok;
|
|
ok:
|
|
self->private_impl.c_decode_id[0].coro_susp_point = 0;
|
|
goto exit;
|
|
}
|
|
|
|
goto suspend;
|
|
suspend:
|
|
self->private_impl.c_decode_id[0].coro_susp_point = coro_susp_point;
|
|
memcpy(self->private_impl.c_decode_id[0].v_c, v_c, sizeof(v_c));
|
|
self->private_impl.c_decode_id[0].v_i = v_i;
|
|
self->private_impl.c_decode_id[0].v_interlace = v_interlace;
|
|
self->private_impl.c_decode_id[0].v_lw = v_lw;
|
|
self->private_impl.c_decode_id[0].v_block_size = v_block_size;
|
|
self->private_impl.c_decode_id[0].l_lzw_src = l_lzw_src;
|
|
self->private_impl.c_decode_id[0].v_lzw_src = v_lzw_src;
|
|
|
|
exit:
|
|
if (a_src.buf) {
|
|
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
|
a_src.buf->ri += n;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
|
}
|
|
|
|
return status;
|
|
|
|
short_read_src:
|
|
if (a_src.buf && a_src.buf->closed && !a_src.limit.ptr_to_len) {
|
|
status = PUFFS_GIF_ERROR_UNEXPECTED_EOF;
|
|
goto exit;
|
|
}
|
|
status = PUFFS_GIF_SUSPENSION_SHORT_READ;
|
|
goto suspend;
|
|
}
|
|
|
|
void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self,
|
|
uint32_t a_lw) {
|
|
if (!self) {
|
|
return;
|
|
}
|
|
if (self->private_impl.magic != PUFFS_MAGIC) {
|
|
self->private_impl.status = PUFFS_GIF_ERROR_INITIALIZER_NOT_CALLED;
|
|
}
|
|
if (self->private_impl.status < 0) {
|
|
return;
|
|
}
|
|
if (a_lw < 2 || a_lw > 8) {
|
|
self->private_impl.status = PUFFS_GIF_ERROR_BAD_ARGUMENT;
|
|
return;
|
|
}
|
|
|
|
self->private_impl.f_literal_width = a_lw;
|
|
}
|
|
|
|
puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
|
puffs_base_writer1 a_dst,
|
|
puffs_base_reader1 a_src) {
|
|
if (!self) {
|
|
return PUFFS_GIF_ERROR_BAD_RECEIVER;
|
|
}
|
|
if (self->private_impl.magic != PUFFS_MAGIC) {
|
|
self->private_impl.status = PUFFS_GIF_ERROR_INITIALIZER_NOT_CALLED;
|
|
}
|
|
if (self->private_impl.status < 0) {
|
|
return self->private_impl.status;
|
|
}
|
|
puffs_gif_status status = PUFFS_GIF_STATUS_OK;
|
|
|
|
uint32_t v_clear_code;
|
|
uint32_t v_end_code;
|
|
uint32_t v_save_code;
|
|
uint32_t v_prev_code;
|
|
uint32_t v_width;
|
|
uint32_t v_bits;
|
|
uint32_t v_n_bits;
|
|
uint32_t v_code;
|
|
uint32_t v_s;
|
|
uint32_t v_c;
|
|
puffs_base_slice_u8 v_expansion;
|
|
uint64_t v_n_copied;
|
|
|
|
uint8_t* b_wptr_dst = NULL;
|
|
uint8_t* b_wstart_dst = NULL;
|
|
uint8_t* b_wend_dst = NULL;
|
|
if (a_dst.buf) {
|
|
b_wptr_dst = a_dst.buf->ptr + a_dst.buf->wi;
|
|
b_wstart_dst = b_wptr_dst;
|
|
b_wend_dst = b_wptr_dst;
|
|
if (!a_dst.buf->closed) {
|
|
size_t len = a_dst.buf->len - a_dst.buf->wi;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_dst.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
|
len = *lim->ptr_to_len;
|
|
}
|
|
}
|
|
b_wend_dst += len;
|
|
}
|
|
}
|
|
uint8_t* b_rptr_src = NULL;
|
|
uint8_t* b_rstart_src = NULL;
|
|
uint8_t* b_rend_src = NULL;
|
|
if (a_src.buf) {
|
|
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
|
b_rstart_src = b_rptr_src;
|
|
size_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
|
len = *lim->ptr_to_len;
|
|
}
|
|
}
|
|
b_rend_src = b_rptr_src + len;
|
|
}
|
|
|
|
uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point;
|
|
if (coro_susp_point) {
|
|
v_clear_code = self->private_impl.c_decode[0].v_clear_code;
|
|
v_end_code = self->private_impl.c_decode[0].v_end_code;
|
|
v_save_code = self->private_impl.c_decode[0].v_save_code;
|
|
v_prev_code = self->private_impl.c_decode[0].v_prev_code;
|
|
v_width = self->private_impl.c_decode[0].v_width;
|
|
v_bits = self->private_impl.c_decode[0].v_bits;
|
|
v_n_bits = self->private_impl.c_decode[0].v_n_bits;
|
|
v_code = self->private_impl.c_decode[0].v_code;
|
|
v_s = self->private_impl.c_decode[0].v_s;
|
|
v_c = self->private_impl.c_decode[0].v_c;
|
|
v_expansion = ((puffs_base_slice_u8){});
|
|
v_n_copied = self->private_impl.c_decode[0].v_n_copied;
|
|
}
|
|
switch (coro_susp_point) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
|
|
|
v_clear_code = (((uint32_t)(1)) << self->private_impl.f_literal_width);
|
|
v_end_code = (v_clear_code + 1);
|
|
v_save_code = v_end_code;
|
|
v_prev_code = 0;
|
|
v_width = (self->private_impl.f_literal_width + 1);
|
|
v_bits = 0;
|
|
v_n_bits = 0;
|
|
label_0_continue:;
|
|
while (true) {
|
|
while (v_n_bits < v_width) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(1);
|
|
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_0 = *b_rptr_src++;
|
|
v_bits |= (((uint32_t)(t_0)) << v_n_bits);
|
|
v_n_bits += 8;
|
|
}
|
|
v_code = ((v_bits) & ((1 << (v_width)) - 1));
|
|
v_bits >>= v_width;
|
|
v_n_bits -= v_width;
|
|
if (v_code < v_clear_code) {
|
|
PUFFS_COROUTINE_SUSPENSION_POINT(2);
|
|
if (b_wptr_dst == b_wend_dst) {
|
|
status = PUFFS_GIF_SUSPENSION_SHORT_WRITE;
|
|
goto suspend;
|
|
}
|
|
*b_wptr_dst++ = ((uint8_t)(v_code));
|
|
if (v_save_code <= 4095) {
|
|
self->private_impl.f_suffixes[v_save_code] = ((uint8_t)(v_code));
|
|
self->private_impl.f_prefixes[v_save_code] =
|
|
((uint16_t)(v_prev_code));
|
|
}
|
|
} else if (v_code == v_clear_code) {
|
|
v_save_code = v_end_code;
|
|
v_prev_code = 0;
|
|
v_width = (self->private_impl.f_literal_width + 1);
|
|
goto label_0_continue;
|
|
} else if (v_code == v_end_code) {
|
|
status = PUFFS_GIF_STATUS_OK;
|
|
goto ok;
|
|
} else if (v_code <= v_save_code) {
|
|
v_s = 4095;
|
|
v_c = v_code;
|
|
if (v_code == v_save_code) {
|
|
v_s -= 1;
|
|
v_c = v_prev_code;
|
|
}
|
|
while (v_c >= v_clear_code) {
|
|
self->private_impl.f_stack[v_s] = self->private_impl.f_suffixes[v_c];
|
|
if (v_s == 0) {
|
|
status = PUFFS_GIF_ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL;
|
|
goto exit;
|
|
}
|
|
v_s -= 1;
|
|
v_c = ((uint32_t)(self->private_impl.f_prefixes[v_c]));
|
|
}
|
|
self->private_impl.f_stack[v_s] = ((uint8_t)(v_c));
|
|
if (v_code == v_save_code) {
|
|
self->private_impl.f_stack[4095] = ((uint8_t)(v_c));
|
|
}
|
|
while (true) {
|
|
v_expansion = puffs_base_slice_u8_subslice_i(
|
|
((puffs_base_slice_u8){.ptr = self->private_impl.f_stack,
|
|
.len = 4096}),
|
|
v_s);
|
|
v_n_copied = puffs_base_writer1_copy_from_slice(
|
|
&b_wptr_dst, b_wend_dst, v_expansion);
|
|
if (v_n_copied == ((uint64_t)(v_expansion.len))) {
|
|
goto label_1_break;
|
|
}
|
|
v_s = ((v_s + ((uint32_t)((v_n_copied & 4095)))) & 4095);
|
|
status = PUFFS_GIF_SUSPENSION_SHORT_WRITE;
|
|
PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3);
|
|
}
|
|
label_1_break:;
|
|
if (v_save_code <= 4095) {
|
|
self->private_impl.f_suffixes[v_save_code] = ((uint8_t)(v_c));
|
|
self->private_impl.f_prefixes[v_save_code] =
|
|
((uint16_t)(v_prev_code));
|
|
}
|
|
} else {
|
|
status = PUFFS_GIF_ERROR_LZW_CODE_IS_OUT_OF_RANGE;
|
|
goto exit;
|
|
}
|
|
if (v_save_code <= 4095) {
|
|
v_save_code += 1;
|
|
if ((v_save_code == (((uint32_t)(1)) << v_width)) && (v_width < 12)) {
|
|
v_width += 1;
|
|
}
|
|
}
|
|
v_prev_code = v_code;
|
|
}
|
|
|
|
goto ok;
|
|
ok:
|
|
self->private_impl.c_decode[0].coro_susp_point = 0;
|
|
goto exit;
|
|
}
|
|
|
|
goto suspend;
|
|
suspend:
|
|
self->private_impl.c_decode[0].coro_susp_point = coro_susp_point;
|
|
self->private_impl.c_decode[0].v_clear_code = v_clear_code;
|
|
self->private_impl.c_decode[0].v_end_code = v_end_code;
|
|
self->private_impl.c_decode[0].v_save_code = v_save_code;
|
|
self->private_impl.c_decode[0].v_prev_code = v_prev_code;
|
|
self->private_impl.c_decode[0].v_width = v_width;
|
|
self->private_impl.c_decode[0].v_bits = v_bits;
|
|
self->private_impl.c_decode[0].v_n_bits = v_n_bits;
|
|
self->private_impl.c_decode[0].v_code = v_code;
|
|
self->private_impl.c_decode[0].v_s = v_s;
|
|
self->private_impl.c_decode[0].v_c = v_c;
|
|
self->private_impl.c_decode[0].v_n_copied = v_n_copied;
|
|
|
|
exit:
|
|
if (a_dst.buf) {
|
|
size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi);
|
|
a_dst.buf->wi += n;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_dst.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wstart_dst);
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wend_dst);
|
|
}
|
|
if (a_src.buf) {
|
|
size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);
|
|
a_src.buf->ri += n;
|
|
puffs_base_limit1* lim;
|
|
for (lim = &a_src.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
|
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
|
}
|
|
|
|
self->private_impl.status = status;
|
|
return status;
|
|
|
|
short_read_src:
|
|
if (a_src.buf && a_src.buf->closed && !a_src.limit.ptr_to_len) {
|
|
status = PUFFS_GIF_ERROR_UNEXPECTED_EOF;
|
|
goto exit;
|
|
}
|
|
status = PUFFS_GIF_SUSPENSION_SHORT_READ;
|
|
goto suspend;
|
|
}
|