mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
1669 lines
52 KiB
C
1669 lines
52 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 {
|
|
// TODO: move buf into private_impl? As it is, it looks like users can modify
|
|
// the buf field to point to a different buffer, which can turn the limit and
|
|
// mark fields into dangling pointers.
|
|
puffs_base__buf1* buf;
|
|
// Do not access the private_impl's fields directly. There is no API/ABI
|
|
// compatibility or safety guarantee if you do so.
|
|
struct {
|
|
puffs_base__limit1 limit;
|
|
uint8_t* mark;
|
|
} private_impl;
|
|
} puffs_base__reader1;
|
|
|
|
typedef struct {
|
|
// TODO: move buf into private_impl? As it is, it looks like users can modify
|
|
// the buf field to point to a different buffer, which can turn the limit and
|
|
// mark fields into dangling pointers.
|
|
puffs_base__buf1* buf;
|
|
// Do not access the private_impl's fields directly. There is no API/ABI
|
|
// compatibility or safety guarantee if you do so.
|
|
struct {
|
|
puffs_base__limit1 limit;
|
|
uint8_t* mark;
|
|
} private_impl;
|
|
} 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_INVALID_I_O_OPERATION -2147483643 // 0x80000005
|
|
#define PUFFS_GIF__ERROR_CLOSED_FOR_WRITES -2147483642 // 0x80000006
|
|
#define PUFFS_GIF__ERROR_UNEXPECTED_EOF -2147483641 // 0x80000007
|
|
#define PUFFS_GIF__SUSPENSION_SHORT_READ 8 // 0x00000008
|
|
#define PUFFS_GIF__SUSPENSION_SHORT_WRITE 9 // 0x00000009
|
|
|
|
#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_INTERNAL_ERROR_INCONSISTENT_LIMITED_READ \
|
|
-1105848316 // 0xbe161804
|
|
#define PUFFS_GIF__ERROR_TODO_UNSUPPORTED_LOCAL_COLOR_TABLE \
|
|
-1105848315 // 0xbe161805
|
|
#define PUFFS_GIF__ERROR_LZW_CODE_IS_OUT_OF_RANGE -1105848314 // 0xbe161806
|
|
#define PUFFS_GIF__ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL -1105848313 // 0xbe161807
|
|
|
|
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;
|
|
uint64_t v_block_size;
|
|
puffs_gif__status v_z;
|
|
} 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.
|
|
|
|
// puffs_base__empty_struct is used when a Puffs function returns an empty
|
|
// struct. In C, if a function f returns void, you can't say "x = f()", but in
|
|
// Puffs, if a function g returns empty, you can say "y = g()".
|
|
typedef struct {
|
|
} puffs_base__empty_struct;
|
|
|
|
#define PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)
|
|
|
|
// PUFFS_BASE__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_BASE__MAGIC (0xCB3699CCU)
|
|
|
|
// PUFFS_BASE__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_BASE__ALREADY_ZEROED (0x68602EF1U)
|
|
|
|
// Use switch cases for coroutine suspension points, similar to the technique
|
|
// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
|
|
//
|
|
// We use trivial macros instead of an explicit assignment and case statement
|
|
// so that clang-format doesn't get confused by the unusual "case"s.
|
|
#define PUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;
|
|
#define PUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \
|
|
coro_susp_point = n; \
|
|
case n:;
|
|
|
|
#define PUFFS_BASE__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_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))
|
|
#define PUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))
|
|
#else
|
|
#define PUFFS_BASE__LIKELY(expr) (expr)
|
|
#define PUFFS_BASE__UNLIKELY(expr) (expr)
|
|
#endif
|
|
|
|
// Uncomment this #include for printf-debugging.
|
|
// #include <stdio.h>
|
|
|
|
// ---------------- 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, // May be NULL, meaning an unmarked writer1.
|
|
uint8_t* end,
|
|
uint32_t distance,
|
|
uint32_t length) {
|
|
if (!start || !distance) {
|
|
return 0;
|
|
}
|
|
uint8_t* ptr = *ptr_ptr;
|
|
if ((size_t)(ptr - start) < (size_t)(distance)) {
|
|
return 0;
|
|
}
|
|
start = ptr - distance;
|
|
size_t n = end - ptr;
|
|
if ((size_t)(length) > n) {
|
|
length = n;
|
|
} else {
|
|
n = length;
|
|
}
|
|
// TODO: unrolling by 3 seems best for the std/flate benchmarks, but that is
|
|
// mostly because 3 is the minimum length for the flate format. This function
|
|
// implementation shouldn't overfit to that one format. Perhaps the
|
|
// copy_from_history32 Puffs method should also take an unroll hint argument,
|
|
// and the cgen can look if that argument is the constant expression '3'.
|
|
//
|
|
// See also puffs_base__writer1__copy_from_history32__bco below.
|
|
//
|
|
// Alternatively, or additionally, have a sloppy_copy_from_history32 method
|
|
// that copies 8 bytes at a time, possibly writing more than length bytes?
|
|
for (; n >= 3; n -= 3) {
|
|
*ptr++ = *start++;
|
|
*ptr++ = *start++;
|
|
*ptr++ = *start++;
|
|
}
|
|
for (; n; n--) {
|
|
*ptr++ = *start++;
|
|
}
|
|
*ptr_ptr = ptr;
|
|
return length;
|
|
}
|
|
|
|
// puffs_base__writer1__copy_from_history32__bco is a Bounds Check Optimized
|
|
// version of the puffs_base__writer1__copy_from_history32 function above. The
|
|
// caller needs to prove that:
|
|
// - start != NULL
|
|
// - distance != 0
|
|
// - distance <= (*ptr_ptr - start)
|
|
// - length <= (end - *ptr_ptr)
|
|
static inline uint32_t puffs_base__writer1__copy_from_history32__bco(
|
|
uint8_t** ptr_ptr,
|
|
uint8_t* start,
|
|
uint8_t* end,
|
|
uint32_t distance,
|
|
uint32_t length) {
|
|
uint8_t* ptr = *ptr_ptr;
|
|
start = ptr - distance;
|
|
uint32_t n = length;
|
|
for (; n >= 3; n -= 3) {
|
|
*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;
|
|
}
|
|
|
|
// Note that the *__limit and *__mark methods are private (in base-impl.h) not
|
|
// public (in base-header.h). We assume that, at the boundary between user code
|
|
// and Puffs code, the reader1 and writer1's private_impl fields (including
|
|
// limit and mark) are NULL. Otherwise, some internal assumptions break down.
|
|
// For example, limits could be represented as pointers, even though
|
|
// conceptually they are counts, but that pointer-to-count correspondence
|
|
// becomes invalid if a buffer is re-used (e.g. on resuming a coroutine).
|
|
//
|
|
// Admittedly, some of the Puffs test code calls these methods, but that test
|
|
// code is still Puffs code, not user code. Other Puffs test code modifies
|
|
// private_impl fields directly.
|
|
|
|
static inline puffs_base__reader1 puffs_base__reader1__limit(
|
|
puffs_base__reader1* o,
|
|
uint64_t* ptr_to_len) {
|
|
puffs_base__reader1 ret = *o;
|
|
ret.private_impl.limit.ptr_to_len = ptr_to_len;
|
|
ret.private_impl.limit.next = &o->private_impl.limit;
|
|
return ret;
|
|
}
|
|
|
|
static inline puffs_base__empty_struct puffs_base__reader1__mark(
|
|
puffs_base__reader1* o,
|
|
uint8_t* mark) {
|
|
o->private_impl.mark = mark;
|
|
return ((puffs_base__empty_struct){});
|
|
}
|
|
|
|
// TODO: static inline puffs_base__writer1 puffs_base__writer1__limit()
|
|
|
|
static inline puffs_base__empty_struct puffs_base__writer1__mark(
|
|
puffs_base__writer1* o,
|
|
uint8_t* mark) {
|
|
o->private_impl.mark = mark;
|
|
return ((puffs_base__empty_struct){});
|
|
}
|
|
|
|
#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[10] = {
|
|
"gif: ok",
|
|
"gif: bad puffs version",
|
|
"gif: bad receiver",
|
|
"gif: bad argument",
|
|
"gif: initializer not called",
|
|
"gif: invalid I/O operation",
|
|
"gif: closed for writes",
|
|
"gif: unexpected EOF",
|
|
"gif: short read",
|
|
"gif: short write",
|
|
};
|
|
|
|
const char* puffs_gif__status__strings1[8] = {
|
|
"gif: bad GIF block",
|
|
"gif: bad GIF extension label",
|
|
"gif: bad GIF header",
|
|
"gif: bad LZW literal width",
|
|
"gif: internal error: inconsistent limited read",
|
|
"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 = 10;
|
|
break;
|
|
case puffs_gif__packageid:
|
|
a = puffs_gif__status__strings1;
|
|
n = 8;
|
|
break;
|
|
}
|
|
uint32_t i = s & 0xff;
|
|
return i < n ? a[i] : "gif: unknown status";
|
|
}
|
|
|
|
// ---------------- Private Consts
|
|
|
|
// ---------------- Private Initializer Prototypes
|
|
|
|
// ---------------- Private Function Prototypes
|
|
|
|
static puffs_gif__status puffs_gif__decoder__decode_header(
|
|
puffs_gif__decoder* self,
|
|
puffs_base__reader1 a_src);
|
|
|
|
static puffs_gif__status puffs_gif__decoder__decode_lsd(
|
|
puffs_gif__decoder* self,
|
|
puffs_base__reader1 a_src);
|
|
|
|
static puffs_gif__status puffs_gif__decoder__decode_extension(
|
|
puffs_gif__decoder* self,
|
|
puffs_base__reader1 a_src);
|
|
|
|
static puffs_gif__status puffs_gif__decoder__decode_id(
|
|
puffs_gif__decoder* self,
|
|
puffs_base__writer1 a_dst,
|
|
puffs_base__reader1 a_src);
|
|
|
|
// ---------------- Initializer Implementations
|
|
|
|
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_BASE__ALREADY_ZEROED) {
|
|
memset(self, 0, sizeof(*self));
|
|
}
|
|
self->private_impl.magic = PUFFS_BASE__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_BASE__ALREADY_ZEROED) {
|
|
memset(self, 0, sizeof(*self));
|
|
}
|
|
self->private_impl.magic = PUFFS_BASE__MAGIC;
|
|
puffs_gif__lzw_decoder__initialize(&self->private_impl.f_lzw, PUFFS_VERSION,
|
|
PUFFS_BASE__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_BASE__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;
|
|
uint64_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base__limit1* lim;
|
|
for (lim = &a_src.private_impl.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_BASE__COROUTINE_SUSPENSION_POINT_0;
|
|
|
|
PUFFS_BASE__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.private_impl.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;
|
|
}
|
|
if (status) {
|
|
goto suspend;
|
|
}
|
|
PUFFS_BASE__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.private_impl.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;
|
|
}
|
|
if (status) {
|
|
goto suspend;
|
|
}
|
|
while (true) {
|
|
{
|
|
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
|
if (PUFFS_BASE__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_BASE__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.private_impl.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;
|
|
}
|
|
if (status) {
|
|
goto suspend;
|
|
}
|
|
} else if (v_c == 44) {
|
|
PUFFS_BASE__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.private_impl.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;
|
|
}
|
|
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.private_impl.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
|
PUFFS_BASE__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.private_impl.limit.ptr_to_len) {
|
|
status = PUFFS_GIF__ERROR_UNEXPECTED_EOF;
|
|
goto exit;
|
|
}
|
|
status = PUFFS_GIF__SUSPENSION_SHORT_READ;
|
|
goto suspend;
|
|
}
|
|
|
|
static 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;
|
|
uint64_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base__limit1* lim;
|
|
for (lim = &a_src.private_impl.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_BASE__COROUTINE_SUSPENSION_POINT_0;
|
|
|
|
memset(v_c, 0, sizeof(v_c));
|
|
v_i = 0;
|
|
while (v_i < 6) {
|
|
{
|
|
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
|
if (PUFFS_BASE__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.private_impl.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
|
PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
|
}
|
|
|
|
return status;
|
|
|
|
short_read_src:
|
|
if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) {
|
|
status = PUFFS_GIF__ERROR_UNEXPECTED_EOF;
|
|
goto exit;
|
|
}
|
|
status = PUFFS_GIF__SUSPENSION_SHORT_READ;
|
|
goto suspend;
|
|
}
|
|
|
|
static 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;
|
|
uint64_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base__limit1* lim;
|
|
for (lim = &a_src.private_impl.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_BASE__COROUTINE_SUSPENSION_POINT_0;
|
|
|
|
memset(v_c, 0, sizeof(v_c));
|
|
v_i = 0;
|
|
while (v_i < 7) {
|
|
{
|
|
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
|
if (PUFFS_BASE__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_BASE__COROUTINE_SUSPENSION_POINT(2);
|
|
if (PUFFS_BASE__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_BASE__COROUTINE_SUSPENSION_POINT(3);
|
|
if (PUFFS_BASE__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_BASE__COROUTINE_SUSPENSION_POINT(4);
|
|
if (PUFFS_BASE__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.private_impl.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
|
PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
|
}
|
|
|
|
return status;
|
|
|
|
short_read_src:
|
|
if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) {
|
|
status = PUFFS_GIF__ERROR_UNEXPECTED_EOF;
|
|
goto exit;
|
|
}
|
|
status = PUFFS_GIF__SUSPENSION_SHORT_READ;
|
|
goto suspend;
|
|
}
|
|
|
|
static 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;
|
|
uint64_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base__limit1* lim;
|
|
for (lim = &a_src.private_impl.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_BASE__COROUTINE_SUSPENSION_POINT_0;
|
|
|
|
{
|
|
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
|
if (PUFFS_BASE__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_BASE__COROUTINE_SUSPENSION_POINT(2);
|
|
if (PUFFS_BASE__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_BASE__COROUTINE_SUSPENSION_POINT(3);
|
|
self->private_impl.c_decode_extension[0].scratch =
|
|
((uint32_t)(v_block_size));
|
|
PUFFS_BASE__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.private_impl.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
|
PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
|
}
|
|
|
|
return status;
|
|
|
|
short_read_src:
|
|
if (a_src.buf && a_src.buf->closed && !a_src.private_impl.limit.ptr_to_len) {
|
|
status = PUFFS_GIF__ERROR_UNEXPECTED_EOF;
|
|
goto exit;
|
|
}
|
|
status = PUFFS_GIF__SUSPENSION_SHORT_READ;
|
|
goto suspend;
|
|
}
|
|
|
|
static 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;
|
|
uint64_t v_block_size;
|
|
puffs_base__reader1 v_r;
|
|
puffs_gif__status v_z;
|
|
|
|
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;
|
|
uint64_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base__limit1* lim;
|
|
for (lim = &a_src.private_impl.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;
|
|
v_r = ((puffs_base__reader1){});
|
|
v_z = self->private_impl.c_decode_id[0].v_z;
|
|
}
|
|
switch (coro_susp_point) {
|
|
PUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
|
|
|
memset(v_c, 0, sizeof(v_c));
|
|
v_i = 0;
|
|
while (v_i < 9) {
|
|
{
|
|
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
|
if (PUFFS_BASE__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_BASE__COROUTINE_SUSPENSION_POINT(2);
|
|
if (PUFFS_BASE__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_BASE__COROUTINE_SUSPENSION_POINT(3);
|
|
if (PUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
|
|
goto short_read_src;
|
|
}
|
|
uint8_t t_2 = *b_rptr_src++;
|
|
v_block_size = ((uint64_t)(t_2));
|
|
}
|
|
if (v_block_size == 0) {
|
|
goto label_0_break;
|
|
}
|
|
while (true) {
|
|
v_r = a_src;
|
|
puffs_base__reader1__mark(&v_r, b_rptr_src);
|
|
{
|
|
PUFFS_BASE__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.private_impl.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
}
|
|
uint64_t l_rlimit0 = v_block_size;
|
|
puffs_gif__status t_3 = puffs_gif__lzw_decoder__decode(
|
|
&self->private_impl.f_lzw, a_dst,
|
|
puffs_base__reader1__limit(&v_r, &l_rlimit0));
|
|
if (a_src.buf) {
|
|
b_rptr_src = a_src.buf->ptr + a_src.buf->ri;
|
|
}
|
|
v_z = t_3;
|
|
}
|
|
if (v_z == 0) {
|
|
goto label_1_break;
|
|
}
|
|
if (v_block_size <
|
|
((uint64_t)(
|
|
((puffs_base__slice_u8){
|
|
.ptr = v_r.private_impl.mark,
|
|
.len = v_r.private_impl.mark
|
|
? (size_t)(b_rptr_src - v_r.private_impl.mark)
|
|
: 0,
|
|
})
|
|
.len))) {
|
|
status = PUFFS_GIF__ERROR_INTERNAL_ERROR_INCONSISTENT_LIMITED_READ;
|
|
goto exit;
|
|
}
|
|
v_block_size -= ((uint64_t)(
|
|
((puffs_base__slice_u8){
|
|
.ptr = v_r.private_impl.mark,
|
|
.len = v_r.private_impl.mark
|
|
? (size_t)(b_rptr_src - v_r.private_impl.mark)
|
|
: 0,
|
|
})
|
|
.len));
|
|
if ((v_block_size == 0) && (v_z == PUFFS_GIF__SUSPENSION_SHORT_READ)) {
|
|
goto label_1_break;
|
|
}
|
|
status = v_z;
|
|
PUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(5);
|
|
}
|
|
label_1_break:;
|
|
}
|
|
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].v_z = v_z;
|
|
|
|
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.private_impl.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
|
PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rend_src);
|
|
}
|
|
|
|
return status;
|
|
|
|
short_read_src:
|
|
if (a_src.buf && a_src.buf->closed && !a_src.private_impl.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_BASE__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_BASE__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) {
|
|
uint64_t len = a_dst.buf->len - a_dst.buf->wi;
|
|
puffs_base__limit1* lim;
|
|
for (lim = &a_dst.private_impl.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;
|
|
uint64_t len = a_src.buf->wi - a_src.buf->ri;
|
|
puffs_base__limit1* lim;
|
|
for (lim = &a_src.private_impl.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_BASE__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_BASE__COROUTINE_SUSPENSION_POINT(1);
|
|
if (PUFFS_BASE__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_BASE__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_BASE__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.private_impl.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wstart_dst);
|
|
PUFFS_BASE__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.private_impl.limit; lim; lim = lim->next) {
|
|
if (lim->ptr_to_len) {
|
|
*lim->ptr_to_len -= n;
|
|
}
|
|
}
|
|
PUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_rstart_src);
|
|
PUFFS_BASE__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.private_impl.limit.ptr_to_len) {
|
|
status = PUFFS_GIF__ERROR_UNEXPECTED_EOF;
|
|
goto exit;
|
|
}
|
|
status = PUFFS_GIF__SUSPENSION_SHORT_READ;
|
|
goto suspend;
|
|
}
|