blob: e5eda99387b3e1e8f36b46f55da4bf84b751d63d [file] [log] [blame]
#ifndef WUFFS_INCLUDE_GUARD
#define WUFFS_INCLUDE_GUARD
// Wuffs ships as a "single file C library" or "header file library" as per
// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
//
// To use that single file as a "foo.c"-like implementation, instead of a
// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
// compiling it.
// Wuffs' C code is generated automatically, not hand-written. These warnings'
// costs outweigh the benefits.
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#pragma clang diagnostic ignored "-Wunused-function"
#endif
// Copyright 2017 The Wuffs 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 <stdlib.h>
#include <string.h>
// Note that Clang also defines __GNUC__.
#ifdef __cplusplus
#if __cplusplus >= 201103L
#include <memory>
#elif defined(__GNUC__)
#warning "Wuffs' C++ code expects -std=c++11 or later"
#elif defined(_MSC_VER)
#pragma message("Wuffs' C++ code expects C++11 or later")
#endif
extern "C" {
#endif
// ---------------- Fundamentals
// WUFFS_VERSION is the major.minor.patch version, as per https://semver.org/,
// as a uint64_t. The major number is the high 32 bits. The minor number is the
// middle 16 bits. The patch number is the low 16 bits. The pre-release label
// and build metadata are part of the string representation (such as
// "1.2.3-beta+456.20181231") but not the uint64_t representation.
//
// WUFFS_VERSION_PRE_RELEASE_LABEL (such as "", "beta" or "rc.1") being
// non-empty denotes a developer preview, not a release version, and has no
// backwards or forwards compatibility guarantees.
//
// WUFFS_VERSION_BUILD_METADATA_XXX, if non-zero, are the number of commits and
// the last commit date in the repository used to build this library. Within
// each major.minor branch, the commit count should increase monotonically.
//
// WUFFS_VERSION was overridden by "wuffs gen -version" based on revision
// c79129a8535b29770d9ef95b4318bcdd28f1b773 committed on 2020-12-15.
#define WUFFS_VERSION 0x000030000
#define WUFFS_VERSION_MAJOR 0
#define WUFFS_VERSION_MINOR 3
#define WUFFS_VERSION_PATCH 0
#define WUFFS_VERSION_PRE_RELEASE_LABEL "alpha.18"
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT 2772
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_DATE 20201215
#define WUFFS_VERSION_STRING "0.3.0-alpha.18+2772.20201215"
// Define WUFFS_CONFIG__STATIC_FUNCTIONS to make all of Wuffs' functions have
// static storage. The motivation is discussed in the "ALLOW STATIC
// IMPLEMENTATION" section of
// https://raw.githubusercontent.com/nothings/stb/master/docs/stb_howto.txt
#ifdef WUFFS_CONFIG__STATIC_FUNCTIONS
#define WUFFS_BASE__MAYBE_STATIC static
#else
#define WUFFS_BASE__MAYBE_STATIC
#endif
// --------
// Wuffs assumes that:
// - converting a uint32_t to a size_t will never overflow.
// - converting a size_t to a uint64_t will never overflow.
#ifdef __WORDSIZE
#if (__WORDSIZE != 32) && (__WORDSIZE != 64)
#error "Wuffs requires a word size of either 32 or 64 bits"
#endif
#endif
#if defined(__clang__)
#define WUFFS_BASE__POTENTIALLY_UNUSED_FIELD __attribute__((unused))
#else
#define WUFFS_BASE__POTENTIALLY_UNUSED_FIELD
#endif
// Clang also defines "__GNUC__".
#if defined(__GNUC__)
#define WUFFS_BASE__POTENTIALLY_UNUSED __attribute__((unused))
#define WUFFS_BASE__WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
#define WUFFS_BASE__POTENTIALLY_UNUSED
#define WUFFS_BASE__WARN_UNUSED_RESULT
#endif
// --------
// Options (bitwise or'ed together) for wuffs_foo__bar__initialize functions.
#define WUFFS_INITIALIZE__DEFAULT_OPTIONS ((uint32_t)0x00000000)
// WUFFS_INITIALIZE__ALREADY_ZEROED means that the "self" receiver struct value
// has already been set to all zeroes.
#define WUFFS_INITIALIZE__ALREADY_ZEROED ((uint32_t)0x00000001)
// WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED means that, absent
// WUFFS_INITIALIZE__ALREADY_ZEROED, only some of the "self" receiver struct
// value will be set to all zeroes. Internal buffers, which tend to be a large
// proportion of the struct's size, will be left uninitialized. Internal means
// that the buffer is contained by the receiver struct, as opposed to being
// passed as a separately allocated "work buffer".
//
// For more detail, see:
// https://github.com/google/wuffs/blob/master/doc/note/initialization.md
#define WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED \
((uint32_t)0x00000002)
// --------
// wuffs_base__empty_struct is used when a Wuffs function returns an empty
// struct. In C, if a function f returns void, you can't say "x = f()", but in
// Wuffs, if a function g returns empty, you can say "y = g()".
typedef struct wuffs_base__empty_struct__struct {
// private_impl is a placeholder field. It isn't explicitly used, except that
// without it, the sizeof a struct with no fields can differ across C/C++
// compilers, and it is undefined behavior in C99. For example, gcc says that
// the sizeof an empty struct is 0, and g++ says that it is 1. This leads to
// ABI incompatibility if a Wuffs .c file is processed by one compiler and
// its .h file with another compiler.
//
// Instead, we explicitly insert an otherwise unused field, so that the
// sizeof this struct is always 1.
uint8_t private_impl;
} wuffs_base__empty_struct;
static inline wuffs_base__empty_struct //
wuffs_base__make_empty_struct() {
wuffs_base__empty_struct ret;
ret.private_impl = 0;
return ret;
}
// wuffs_base__utility is a placeholder receiver type. It enables what Java
// calls static methods, as opposed to regular methods.
typedef struct wuffs_base__utility__struct {
// private_impl is a placeholder field. It isn't explicitly used, except that
// without it, the sizeof a struct with no fields can differ across C/C++
// compilers, and it is undefined behavior in C99. For example, gcc says that
// the sizeof an empty struct is 0, and g++ says that it is 1. This leads to
// ABI incompatibility if a Wuffs .c file is processed by one compiler and
// its .h file with another compiler.
//
// Instead, we explicitly insert an otherwise unused field, so that the
// sizeof this struct is always 1.
uint8_t private_impl;
} wuffs_base__utility;
typedef struct wuffs_base__vtable__struct {
const char* vtable_name;
const void* function_pointers;
} wuffs_base__vtable;
// --------
// See https://github.com/google/wuffs/blob/master/doc/note/statuses.md
typedef struct wuffs_base__status__struct {
const char* repr;
#ifdef __cplusplus
inline bool is_complete() const;
inline bool is_error() const;
inline bool is_note() const;
inline bool is_ok() const;
inline bool is_suspension() const;
inline const char* message() const;
#endif // __cplusplus
} wuffs_base__status;
extern const char wuffs_base__note__i_o_redirect[];
extern const char wuffs_base__note__end_of_data[];
extern const char wuffs_base__note__metadata_reported[];
extern const char wuffs_base__suspension__even_more_information[];
extern const char wuffs_base__suspension__mispositioned_read[];
extern const char wuffs_base__suspension__mispositioned_write[];
extern const char wuffs_base__suspension__short_read[];
extern const char wuffs_base__suspension__short_write[];
extern const char wuffs_base__error__bad_i_o_position[];
extern const char wuffs_base__error__bad_argument_length_too_short[];
extern const char wuffs_base__error__bad_argument[];
extern const char wuffs_base__error__bad_call_sequence[];
extern const char wuffs_base__error__bad_data[];
extern const char wuffs_base__error__bad_receiver[];
extern const char wuffs_base__error__bad_restart[];
extern const char wuffs_base__error__bad_sizeof_receiver[];
extern const char wuffs_base__error__bad_vtable[];
extern const char wuffs_base__error__bad_workbuf_length[];
extern const char wuffs_base__error__bad_wuffs_version[];
extern const char wuffs_base__error__cannot_return_a_suspension[];
extern const char wuffs_base__error__disabled_by_previous_error[];
extern const char wuffs_base__error__initialize_falsely_claimed_already_zeroed[];
extern const char wuffs_base__error__initialize_not_called[];
extern const char wuffs_base__error__interleaved_coroutine_calls[];
extern const char wuffs_base__error__no_more_information[];
extern const char wuffs_base__error__not_enough_data[];
extern const char wuffs_base__error__out_of_bounds[];
extern const char wuffs_base__error__unsupported_method[];
extern const char wuffs_base__error__unsupported_option[];
extern const char wuffs_base__error__unsupported_pixel_swizzler_option[];
extern const char wuffs_base__error__too_much_data[];
static inline wuffs_base__status //
wuffs_base__make_status(const char* repr) {
wuffs_base__status z;
z.repr = repr;
return z;
}
static inline bool //
wuffs_base__status__is_complete(const wuffs_base__status* z) {
return (z->repr == NULL) || ((*z->repr != '$') && (*z->repr != '#'));
}
static inline bool //
wuffs_base__status__is_error(const wuffs_base__status* z) {
return z->repr && (*z->repr == '#');
}
static inline bool //
wuffs_base__status__is_note(const wuffs_base__status* z) {
return z->repr && (*z->repr != '$') && (*z->repr != '#');
}
static inline bool //
wuffs_base__status__is_ok(const wuffs_base__status* z) {
return z->repr == NULL;
}
static inline bool //
wuffs_base__status__is_suspension(const wuffs_base__status* z) {
return z->repr && (*z->repr == '$');
}
// wuffs_base__status__message strips the leading '$', '#' or '@'.
static inline const char* //
wuffs_base__status__message(const wuffs_base__status* z) {
if (z->repr) {
if ((*z->repr == '$') || (*z->repr == '#') || (*z->repr == '@')) {
return z->repr + 1;
}
}
return z->repr;
}
#ifdef __cplusplus
inline bool //
wuffs_base__status::is_complete() const {
return wuffs_base__status__is_complete(this);
}
inline bool //
wuffs_base__status::is_error() const {
return wuffs_base__status__is_error(this);
}
inline bool //
wuffs_base__status::is_note() const {
return wuffs_base__status__is_note(this);
}
inline bool //
wuffs_base__status::is_ok() const {
return wuffs_base__status__is_ok(this);
}
inline bool //
wuffs_base__status::is_suspension() const {
return wuffs_base__status__is_suspension(this);
}
inline const char* //
wuffs_base__status::message() const {
return wuffs_base__status__message(this);
}
#endif // __cplusplus
// --------
// WUFFS_BASE__RESULT is a result type: either a status (an error) or a value.
//
// A result with all fields NULL or zero is as valid as a zero-valued T.
#define WUFFS_BASE__RESULT(T) \
struct { \
wuffs_base__status status; \
T value; \
}
typedef WUFFS_BASE__RESULT(double) wuffs_base__result_f64;
typedef WUFFS_BASE__RESULT(int64_t) wuffs_base__result_i64;
typedef WUFFS_BASE__RESULT(uint64_t) wuffs_base__result_u64;
// --------
// wuffs_base__transform__output is the result of transforming from a src slice
// to a dst slice.
typedef struct wuffs_base__transform__output__struct {
wuffs_base__status status;
size_t num_dst;
size_t num_src;
} wuffs_base__transform__output;
// --------
// FourCC constants.
// International Color Consortium Profile.
#define WUFFS_BASE__FOURCC__ICCP 0x49434350
// Joint Photographic Experts Group.
#define WUFFS_BASE__FOURCC__JPEG 0x4A504547
// Portable Network Graphics.
#define WUFFS_BASE__FOURCC__PNG 0x504E4720
// Extensible Metadata Platform.
#define WUFFS_BASE__FOURCC__XMP 0x584D5020
// --------
// Flicks are a unit of time. One flick (frame-tick) is 1 / 705_600_000 of a
// second. See https://github.com/OculusVR/Flicks
typedef int64_t wuffs_base__flicks;
#define WUFFS_BASE__FLICKS_PER_SECOND ((uint64_t)705600000)
#define WUFFS_BASE__FLICKS_PER_MILLISECOND ((uint64_t)705600)
// ---------------- Numeric Types
// 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 static, so that linking multiple wuffs .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 int8_t //
wuffs_base__i8__min(int8_t x, int8_t y) {
return x < y ? x : y;
}
static inline int8_t //
wuffs_base__i8__max(int8_t x, int8_t y) {
return x > y ? x : y;
}
static inline int16_t //
wuffs_base__i16__min(int16_t x, int16_t y) {
return x < y ? x : y;
}
static inline int16_t //
wuffs_base__i16__max(int16_t x, int16_t y) {
return x > y ? x : y;
}
static inline int32_t //
wuffs_base__i32__min(int32_t x, int32_t y) {
return x < y ? x : y;
}
static inline int32_t //
wuffs_base__i32__max(int32_t x, int32_t y) {
return x > y ? x : y;
}
static inline int64_t //
wuffs_base__i64__min(int64_t x, int64_t y) {
return x < y ? x : y;
}
static inline int64_t //
wuffs_base__i64__max(int64_t x, int64_t y) {
return x > y ? x : y;
}
static inline uint8_t //
wuffs_base__u8__min(uint8_t x, uint8_t y) {
return x < y ? x : y;
}
static inline uint8_t //
wuffs_base__u8__max(uint8_t x, uint8_t y) {
return x > y ? x : y;
}
static inline uint16_t //
wuffs_base__u16__min(uint16_t x, uint16_t y) {
return x < y ? x : y;
}
static inline uint16_t //
wuffs_base__u16__max(uint16_t x, uint16_t y) {
return x > y ? x : y;
}
static inline uint32_t //
wuffs_base__u32__min(uint32_t x, uint32_t y) {
return x < y ? x : y;
}
static inline uint32_t //
wuffs_base__u32__max(uint32_t x, uint32_t y) {
return x > y ? x : y;
}
static inline uint64_t //
wuffs_base__u64__min(uint64_t x, uint64_t y) {
return x < y ? x : y;
}
static inline uint64_t //
wuffs_base__u64__max(uint64_t x, uint64_t y) {
return x > y ? x : y;
}
// --------
// Saturating arithmetic (sat_add, sat_sub) branchless bit-twiddling algorithms
// are per https://locklessinc.com/articles/sat_arithmetic/
//
// It is important that the underlying types are unsigned integers, as signed
// integer arithmetic overflow is undefined behavior in C.
static inline uint8_t //
wuffs_base__u8__sat_add(uint8_t x, uint8_t y) {
uint8_t res = (uint8_t)(x + y);
res |= (uint8_t)(-(res < x));
return res;
}
static inline uint8_t //
wuffs_base__u8__sat_sub(uint8_t x, uint8_t y) {
uint8_t res = (uint8_t)(x - y);
res &= (uint8_t)(-(res <= x));
return res;
}
static inline uint16_t //
wuffs_base__u16__sat_add(uint16_t x, uint16_t y) {
uint16_t res = (uint16_t)(x + y);
res |= (uint16_t)(-(res < x));
return res;
}
static inline uint16_t //
wuffs_base__u16__sat_sub(uint16_t x, uint16_t y) {
uint16_t res = (uint16_t)(x - y);
res &= (uint16_t)(-(res <= x));
return res;
}
static inline uint32_t //
wuffs_base__u32__sat_add(uint32_t x, uint32_t y) {
uint32_t res = (uint32_t)(x + y);
res |= (uint32_t)(-(res < x));
return res;
}
static inline uint32_t //
wuffs_base__u32__sat_sub(uint32_t x, uint32_t y) {
uint32_t res = (uint32_t)(x - y);
res &= (uint32_t)(-(res <= x));
return res;
}
static inline uint64_t //
wuffs_base__u64__sat_add(uint64_t x, uint64_t y) {
uint64_t res = (uint64_t)(x + y);
res |= (uint64_t)(-(res < x));
return res;
}
static inline uint64_t //
wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
uint64_t res = (uint64_t)(x - y);
res &= (uint64_t)(-(res <= x));
return res;
}
// --------
typedef struct wuffs_base__multiply_u64__output__struct {
uint64_t lo;
uint64_t hi;
} wuffs_base__multiply_u64__output;
// wuffs_base__multiply_u64 returns x*y as a 128-bit value.
//
// The maximum inclusive output hi_lo is 0xFFFFFFFFFFFFFFFE_0000000000000001.
static inline wuffs_base__multiply_u64__output //
wuffs_base__multiply_u64(uint64_t x, uint64_t y) {
#if defined(__SIZEOF_INT128__)
__uint128_t z = ((__uint128_t)x) * ((__uint128_t)y);
wuffs_base__multiply_u64__output o;
o.lo = ((uint64_t)(z));
o.hi = ((uint64_t)(z >> 64));
return o;
#else
uint64_t x0 = x & 0xFFFFFFFF;
uint64_t x1 = x >> 32;
uint64_t y0 = y & 0xFFFFFFFF;
uint64_t y1 = y >> 32;
uint64_t w0 = x0 * y0;
uint64_t t = (x1 * y0) + (w0 >> 32);
uint64_t w1 = t & 0xFFFFFFFF;
uint64_t w2 = t >> 32;
w1 += x0 * y1;
wuffs_base__multiply_u64__output o;
o.lo = x * y;
o.hi = (x1 * y1) + w2 + (w1 >> 32);
return o;
#endif
}
// --------
#if defined(__GNUC__) && (__SIZEOF_LONG__ == 8)
static inline uint32_t //
wuffs_base__count_leading_zeroes_u64(uint64_t u) {
return u ? ((uint32_t)(__builtin_clzl(u))) : 64u;
}
#else
static inline uint32_t //
wuffs_base__count_leading_zeroes_u64(uint64_t u) {
if (u == 0) {
return 64;
}
uint32_t n = 0;
if ((u >> 32) == 0) {
n |= 32;
u <<= 32;
}
if ((u >> 48) == 0) {
n |= 16;
u <<= 16;
}
if ((u >> 56) == 0) {
n |= 8;
u <<= 8;
}
if ((u >> 60) == 0) {
n |= 4;
u <<= 4;
}
if ((u >> 62) == 0) {
n |= 2;
u <<= 2;
}
if ((u >> 63) == 0) {
n |= 1;
u <<= 1;
}
return n;
}
#endif // defined(__GNUC__) && (__SIZEOF_LONG__ == 8)
// --------
#define wuffs_base__load_u8be__no_bounds_check \
wuffs_base__load_u8__no_bounds_check
#define wuffs_base__load_u8le__no_bounds_check \
wuffs_base__load_u8__no_bounds_check
static inline uint8_t //
wuffs_base__load_u8__no_bounds_check(const uint8_t* p) {
return p[0];
}
static inline uint16_t //
wuffs_base__load_u16be__no_bounds_check(const uint8_t* p) {
return (uint16_t)(((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0));
}
static inline uint16_t //
wuffs_base__load_u16le__no_bounds_check(const uint8_t* p) {
return (uint16_t)(((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8));
}
static inline uint32_t //
wuffs_base__load_u24be__no_bounds_check(const uint8_t* p) {
return ((uint32_t)(p[0]) << 16) | ((uint32_t)(p[1]) << 8) |
((uint32_t)(p[2]) << 0);
}
static inline uint32_t //
wuffs_base__load_u24le__no_bounds_check(const uint8_t* p) {
return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |
((uint32_t)(p[2]) << 16);
}
static inline uint32_t //
wuffs_base__load_u32be__no_bounds_check(const 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 //
wuffs_base__load_u32le__no_bounds_check(const 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 uint64_t //
wuffs_base__load_u40be__no_bounds_check(const uint8_t* p) {
return ((uint64_t)(p[0]) << 32) | ((uint64_t)(p[1]) << 24) |
((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 8) |
((uint64_t)(p[4]) << 0);
}
static inline uint64_t //
wuffs_base__load_u40le__no_bounds_check(const uint8_t* p) {
return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |
((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |
((uint64_t)(p[4]) << 32);
}
static inline uint64_t //
wuffs_base__load_u48be__no_bounds_check(const uint8_t* p) {
return ((uint64_t)(p[0]) << 40) | ((uint64_t)(p[1]) << 32) |
((uint64_t)(p[2]) << 24) | ((uint64_t)(p[3]) << 16) |
((uint64_t)(p[4]) << 8) | ((uint64_t)(p[5]) << 0);
}
static inline uint64_t //
wuffs_base__load_u48le__no_bounds_check(const uint8_t* p) {
return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |
((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |
((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40);
}
static inline uint64_t //
wuffs_base__load_u56be__no_bounds_check(const uint8_t* p) {
return ((uint64_t)(p[0]) << 48) | ((uint64_t)(p[1]) << 40) |
((uint64_t)(p[2]) << 32) | ((uint64_t)(p[3]) << 24) |
((uint64_t)(p[4]) << 16) | ((uint64_t)(p[5]) << 8) |
((uint64_t)(p[6]) << 0);
}
static inline uint64_t //
wuffs_base__load_u56le__no_bounds_check(const uint8_t* p) {
return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |
((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |
((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40) |
((uint64_t)(p[6]) << 48);
}
static inline uint64_t //
wuffs_base__load_u64be__no_bounds_check(const uint8_t* p) {
return ((uint64_t)(p[0]) << 56) | ((uint64_t)(p[1]) << 48) |
((uint64_t)(p[2]) << 40) | ((uint64_t)(p[3]) << 32) |
((uint64_t)(p[4]) << 24) | ((uint64_t)(p[5]) << 16) |
((uint64_t)(p[6]) << 8) | ((uint64_t)(p[7]) << 0);
}
static inline uint64_t //
wuffs_base__load_u64le__no_bounds_check(const uint8_t* p) {
return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |
((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |
((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40) |
((uint64_t)(p[6]) << 48) | ((uint64_t)(p[7]) << 56);
}
// --------
#define wuffs_base__store_u8be__no_bounds_check \
wuffs_base__store_u8__no_bounds_check
#define wuffs_base__store_u8le__no_bounds_check \
wuffs_base__store_u8__no_bounds_check
static inline void //
wuffs_base__store_u8__no_bounds_check(uint8_t* p, uint8_t x) {
p[0] = x;
}
static inline void //
wuffs_base__store_u16be__no_bounds_check(uint8_t* p, uint16_t x) {
p[0] = (uint8_t)(x >> 8);
p[1] = (uint8_t)(x >> 0);
}
static inline void //
wuffs_base__store_u16le__no_bounds_check(uint8_t* p, uint16_t x) {
p[0] = (uint8_t)(x >> 0);
p[1] = (uint8_t)(x >> 8);
}
static inline void //
wuffs_base__store_u24be__no_bounds_check(uint8_t* p, uint32_t x) {
p[0] = (uint8_t)(x >> 16);
p[1] = (uint8_t)(x >> 8);
p[2] = (uint8_t)(x >> 0);
}
static inline void //
wuffs_base__store_u24le__no_bounds_check(uint8_t* p, uint32_t x) {
p[0] = (uint8_t)(x >> 0);
p[1] = (uint8_t)(x >> 8);
p[2] = (uint8_t)(x >> 16);
}
static inline void //
wuffs_base__store_u32be__no_bounds_check(uint8_t* p, uint32_t x) {
p[0] = (uint8_t)(x >> 24);
p[1] = (uint8_t)(x >> 16);
p[2] = (uint8_t)(x >> 8);
p[3] = (uint8_t)(x >> 0);
}
static inline void //
wuffs_base__store_u32le__no_bounds_check(uint8_t* p, uint32_t x) {
p[0] = (uint8_t)(x >> 0);
p[1] = (uint8_t)(x >> 8);
p[2] = (uint8_t)(x >> 16);
p[3] = (uint8_t)(x >> 24);
}
static inline void //
wuffs_base__store_u40be__no_bounds_check(uint8_t* p, uint64_t x) {
p[0] = (uint8_t)(x >> 32);
p[1] = (uint8_t)(x >> 24);
p[2] = (uint8_t)(x >> 16);
p[3] = (uint8_t)(x >> 8);
p[4] = (uint8_t)(x >> 0);
}
static inline void //
wuffs_base__store_u40le__no_bounds_check(uint8_t* p, uint64_t x) {
p[0] = (uint8_t)(x >> 0);
p[1] = (uint8_t)(x >> 8);
p[2] = (uint8_t)(x >> 16);
p[3] = (uint8_t)(x >> 24);
p[4] = (uint8_t)(x >> 32);
}
static inline void //
wuffs_base__store_u48be__no_bounds_check(uint8_t* p, uint64_t x) {
p[0] = (uint8_t)(x >> 40);
p[1] = (uint8_t)(x >> 32);
p[2] = (uint8_t)(x >> 24);
p[3] = (uint8_t)(x >> 16);
p[4] = (uint8_t)(x >> 8);
p[5] = (uint8_t)(x >> 0);
}
static inline void //
wuffs_base__store_u48le__no_bounds_check(uint8_t* p, uint64_t x) {
p[0] = (uint8_t)(x >> 0);
p[1] = (uint8_t)(x >> 8);
p[2] = (uint8_t)(x >> 16);
p[3] = (uint8_t)(x >> 24);
p[4] = (uint8_t)(x >> 32);
p[5] = (uint8_t)(x >> 40);
}
static inline void //
wuffs_base__store_u56be__no_bounds_check(uint8_t* p, uint64_t x) {
p[0] = (uint8_t)(x >> 48);
p[1] = (uint8_t)(x >> 40);
p[2] = (uint8_t)(x >> 32);
p[3] = (uint8_t)(x >> 24);
p[4] = (uint8_t)(x >> 16);
p[5] = (uint8_t)(x >> 8);
p[6] = (uint8_t)(x >> 0);
}
static inline void //
wuffs_base__store_u56le__no_bounds_check(uint8_t* p, uint64_t x) {
p[0] = (uint8_t)(x >> 0);
p[1] = (uint8_t)(x >> 8);
p[2] = (uint8_t)(x >> 16);
p[3] = (uint8_t)(x >> 24);
p[4] = (uint8_t)(x >> 32);
p[5] = (uint8_t)(x >> 40);
p[6] = (uint8_t)(x >> 48);
}
static inline void //
wuffs_base__store_u64be__no_bounds_check(uint8_t* p, uint64_t x) {
p[0] = (uint8_t)(x >> 56);
p[1] = (uint8_t)(x >> 48);
p[2] = (uint8_t)(x >> 40);
p[3] = (uint8_t)(x >> 32);
p[4] = (uint8_t)(x >> 24);
p[5] = (uint8_t)(x >> 16);
p[6] = (uint8_t)(x >> 8);
p[7] = (uint8_t)(x >> 0);
}
static inline void //
wuffs_base__store_u64le__no_bounds_check(uint8_t* p, uint64_t x) {
p[0] = (uint8_t)(x >> 0);
p[1] = (uint8_t)(x >> 8);
p[2] = (uint8_t)(x >> 16);
p[3] = (uint8_t)(x >> 24);
p[4] = (uint8_t)(x >> 32);
p[5] = (uint8_t)(x >> 40);
p[6] = (uint8_t)(x >> 48);
p[7] = (uint8_t)(x >> 56);
}
// ---------------- Slices and Tables
// WUFFS_BASE__SLICE is a 1-dimensional buffer.
//
// len measures a number of elements, not necessarily a size in bytes.
//
// A value with all fields NULL or zero is a valid, empty slice.
#define WUFFS_BASE__SLICE(T) \
struct { \
T* ptr; \
size_t len; \
}
// WUFFS_BASE__TABLE is a 2-dimensional buffer.
//
// width height, and stride measure a number of elements, not necessarily a
// size in bytes.
//
// A value with all fields NULL or zero is a valid, empty table.
#define WUFFS_BASE__TABLE(T) \
struct { \
T* ptr; \
size_t width; \
size_t height; \
size_t stride; \
}
typedef WUFFS_BASE__SLICE(uint8_t) wuffs_base__slice_u8;
typedef WUFFS_BASE__SLICE(uint16_t) wuffs_base__slice_u16;
typedef WUFFS_BASE__SLICE(uint32_t) wuffs_base__slice_u32;
typedef WUFFS_BASE__SLICE(uint64_t) wuffs_base__slice_u64;
typedef WUFFS_BASE__TABLE(uint8_t) wuffs_base__table_u8;
typedef WUFFS_BASE__TABLE(uint16_t) wuffs_base__table_u16;
typedef WUFFS_BASE__TABLE(uint32_t) wuffs_base__table_u32;
typedef WUFFS_BASE__TABLE(uint64_t) wuffs_base__table_u64;
static inline wuffs_base__slice_u8 //
wuffs_base__make_slice_u8(uint8_t* ptr, size_t len) {
wuffs_base__slice_u8 ret;
ret.ptr = ptr;
ret.len = len;
return ret;
}
static inline wuffs_base__slice_u16 //
wuffs_base__make_slice_u16(uint16_t* ptr, size_t len) {
wuffs_base__slice_u16 ret;
ret.ptr = ptr;
ret.len = len;
return ret;
}
static inline wuffs_base__slice_u32 //
wuffs_base__make_slice_u32(uint32_t* ptr, size_t len) {
wuffs_base__slice_u32 ret;
ret.ptr = ptr;
ret.len = len;
return ret;
}
static inline wuffs_base__slice_u64 //
wuffs_base__make_slice_u64(uint64_t* ptr, size_t len) {
wuffs_base__slice_u64 ret;
ret.ptr = ptr;
ret.len = len;
return ret;
}
static inline wuffs_base__slice_u8 //
wuffs_base__empty_slice_u8() {
wuffs_base__slice_u8 ret;
ret.ptr = NULL;
ret.len = 0;
return ret;
}
static inline wuffs_base__slice_u16 //
wuffs_base__empty_slice_u16() {
wuffs_base__slice_u16 ret;
ret.ptr = NULL;
ret.len = 0;
return ret;
}
static inline wuffs_base__slice_u32 //
wuffs_base__empty_slice_u32() {
wuffs_base__slice_u32 ret;
ret.ptr = NULL;
ret.len = 0;
return ret;
}
static inline wuffs_base__slice_u64 //
wuffs_base__empty_slice_u64() {
wuffs_base__slice_u64 ret;
ret.ptr = NULL;
ret.len = 0;
return ret;
}
static inline wuffs_base__table_u8 //
wuffs_base__empty_table_u8() {
wuffs_base__table_u8 ret;
ret.ptr = NULL;
ret.width = 0;
ret.height = 0;
ret.stride = 0;
return ret;
}
static inline wuffs_base__table_u16 //
wuffs_base__empty_table_u16() {
wuffs_base__table_u16 ret;
ret.ptr = NULL;
ret.width = 0;
ret.height = 0;
ret.stride = 0;
return ret;
}
static inline wuffs_base__table_u32 //
wuffs_base__empty_table_u32() {
wuffs_base__table_u32 ret;
ret.ptr = NULL;
ret.width = 0;
ret.height = 0;
ret.stride = 0;
return ret;
}
static inline wuffs_base__table_u64 //
wuffs_base__empty_table_u64() {
wuffs_base__table_u64 ret;
ret.ptr = NULL;
ret.width = 0;
ret.height = 0;
ret.stride = 0;
return ret;
}
// wuffs_base__slice_u8__subslice_i returns s[i:].
//
// It returns an empty slice if i is out of bounds.
static inline wuffs_base__slice_u8 //
wuffs_base__slice_u8__subslice_i(wuffs_base__slice_u8 s, uint64_t i) {
if ((i <= SIZE_MAX) && (i <= s.len)) {
return wuffs_base__make_slice_u8(s.ptr + i, s.len - i);
}
return wuffs_base__make_slice_u8(NULL, 0);
}
// wuffs_base__slice_u8__subslice_j returns s[:j].
//
// It returns an empty slice if j is out of bounds.
static inline wuffs_base__slice_u8 //
wuffs_base__slice_u8__subslice_j(wuffs_base__slice_u8 s, uint64_t j) {
if ((j <= SIZE_MAX) && (j <= s.len)) {
return wuffs_base__make_slice_u8(s.ptr, j);
}
return wuffs_base__make_slice_u8(NULL, 0);
}
// wuffs_base__slice_u8__subslice_ij returns s[i:j].
//
// It returns an empty slice if i or j is out of bounds.
static inline wuffs_base__slice_u8 //
wuffs_base__slice_u8__subslice_ij(wuffs_base__slice_u8 s,
uint64_t i,
uint64_t j) {
if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {
return wuffs_base__make_slice_u8(s.ptr + i, j - i);
}
return wuffs_base__make_slice_u8(NULL, 0);
}
// ---------------- Ranges and Rects
// See https://github.com/google/wuffs/blob/master/doc/note/ranges-and-rects.md
typedef struct wuffs_base__range_ii_u32__struct {
uint32_t min_incl;
uint32_t max_incl;
#ifdef __cplusplus
inline bool is_empty() const;
inline bool equals(wuffs_base__range_ii_u32__struct s) const;
inline wuffs_base__range_ii_u32__struct intersect(
wuffs_base__range_ii_u32__struct s) const;
inline wuffs_base__range_ii_u32__struct unite(
wuffs_base__range_ii_u32__struct s) const;
inline bool contains(uint32_t x) const;
inline bool contains_range(wuffs_base__range_ii_u32__struct s) const;
#endif // __cplusplus
} wuffs_base__range_ii_u32;
static inline wuffs_base__range_ii_u32 //
wuffs_base__empty_range_ii_u32() {
wuffs_base__range_ii_u32 ret;
ret.min_incl = 0;
ret.max_incl = 0;
return ret;
}
static inline wuffs_base__range_ii_u32 //
wuffs_base__make_range_ii_u32(uint32_t min_incl, uint32_t max_incl) {
wuffs_base__range_ii_u32 ret;
ret.min_incl = min_incl;
ret.max_incl = max_incl;
return ret;
}
static inline bool //
wuffs_base__range_ii_u32__is_empty(const wuffs_base__range_ii_u32* r) {
return r->min_incl > r->max_incl;
}
static inline bool //
wuffs_base__range_ii_u32__equals(const wuffs_base__range_ii_u32* r,
wuffs_base__range_ii_u32 s) {
return (r->min_incl == s.min_incl && r->max_incl == s.max_incl) ||
(wuffs_base__range_ii_u32__is_empty(r) &&
wuffs_base__range_ii_u32__is_empty(&s));
}
static inline wuffs_base__range_ii_u32 //
wuffs_base__range_ii_u32__intersect(const wuffs_base__range_ii_u32* r,
wuffs_base__range_ii_u32 s) {
wuffs_base__range_ii_u32 t;
t.min_incl = wuffs_base__u32__max(r->min_incl, s.min_incl);
t.max_incl = wuffs_base__u32__min(r->max_incl, s.max_incl);
return t;
}
static inline wuffs_base__range_ii_u32 //
wuffs_base__range_ii_u32__unite(const wuffs_base__range_ii_u32* r,
wuffs_base__range_ii_u32 s) {
if (wuffs_base__range_ii_u32__is_empty(r)) {
return s;
}
if (wuffs_base__range_ii_u32__is_empty(&s)) {
return *r;
}
wuffs_base__range_ii_u32 t;
t.min_incl = wuffs_base__u32__min(r->min_incl, s.min_incl);
t.max_incl = wuffs_base__u32__max(r->max_incl, s.max_incl);
return t;
}
static inline bool //
wuffs_base__range_ii_u32__contains(const wuffs_base__range_ii_u32* r,
uint32_t x) {
return (r->min_incl <= x) && (x <= r->max_incl);
}
static inline bool //
wuffs_base__range_ii_u32__contains_range(const wuffs_base__range_ii_u32* r,
wuffs_base__range_ii_u32 s) {
return wuffs_base__range_ii_u32__equals(
&s, wuffs_base__range_ii_u32__intersect(r, s));
}
#ifdef __cplusplus
inline bool //
wuffs_base__range_ii_u32::is_empty() const {
return wuffs_base__range_ii_u32__is_empty(this);
}
inline bool //
wuffs_base__range_ii_u32::equals(wuffs_base__range_ii_u32 s) const {
return wuffs_base__range_ii_u32__equals(this, s);
}
inline wuffs_base__range_ii_u32 //
wuffs_base__range_ii_u32::intersect(wuffs_base__range_ii_u32 s) const {
return wuffs_base__range_ii_u32__intersect(this, s);
}
inline wuffs_base__range_ii_u32 //
wuffs_base__range_ii_u32::unite(wuffs_base__range_ii_u32 s) const {
return wuffs_base__range_ii_u32__unite(this, s);
}
inline bool //
wuffs_base__range_ii_u32::contains(uint32_t x) const {
return wuffs_base__range_ii_u32__contains(this, x);
}
inline bool //
wuffs_base__range_ii_u32::contains_range(wuffs_base__range_ii_u32 s) const {
return wuffs_base__range_ii_u32__contains_range(this, s);
}
#endif // __cplusplus
// --------
typedef struct wuffs_base__range_ie_u32__struct {
uint32_t min_incl;
uint32_t max_excl;
#ifdef __cplusplus
inline bool is_empty() const;
inline bool equals(wuffs_base__range_ie_u32__struct s) const;
inline wuffs_base__range_ie_u32__struct intersect(
wuffs_base__range_ie_u32__struct s) const;
inline wuffs_base__range_ie_u32__struct unite(
wuffs_base__range_ie_u32__struct s) const;
inline bool contains(uint32_t x) const;
inline bool contains_range(wuffs_base__range_ie_u32__struct s) const;
inline uint32_t length() const;
#endif // __cplusplus
} wuffs_base__range_ie_u32;
static inline wuffs_base__range_ie_u32 //
wuffs_base__empty_range_ie_u32() {
wuffs_base__range_ie_u32 ret;
ret.min_incl = 0;
ret.max_excl = 0;
return ret;
}
static inline wuffs_base__range_ie_u32 //
wuffs_base__make_range_ie_u32(uint32_t min_incl, uint32_t max_excl) {
wuffs_base__range_ie_u32 ret;
ret.min_incl = min_incl;
ret.max_excl = max_excl;
return ret;
}
static inline bool //
wuffs_base__range_ie_u32__is_empty(const wuffs_base__range_ie_u32* r) {
return r->min_incl >= r->max_excl;
}
static inline bool //
wuffs_base__range_ie_u32__equals(const wuffs_base__range_ie_u32* r,
wuffs_base__range_ie_u32 s) {
return (r->min_incl == s.min_incl && r->max_excl == s.max_excl) ||
(wuffs_base__range_ie_u32__is_empty(r) &&
wuffs_base__range_ie_u32__is_empty(&s));
}
static inline wuffs_base__range_ie_u32 //
wuffs_base__range_ie_u32__intersect(const wuffs_base__range_ie_u32* r,
wuffs_base__range_ie_u32 s) {
wuffs_base__range_ie_u32 t;
t.min_incl = wuffs_base__u32__max(r->min_incl, s.min_incl);
t.max_excl = wuffs_base__u32__min(r->max_excl, s.max_excl);
return t;
}
static inline wuffs_base__range_ie_u32 //
wuffs_base__range_ie_u32__unite(const wuffs_base__range_ie_u32* r,
wuffs_base__range_ie_u32 s) {
if (wuffs_base__range_ie_u32__is_empty(r)) {
return s;
}
if (wuffs_base__range_ie_u32__is_empty(&s)) {
return *r;
}
wuffs_base__range_ie_u32 t;
t.min_incl = wuffs_base__u32__min(r->min_incl, s.min_incl);
t.max_excl = wuffs_base__u32__max(r->max_excl, s.max_excl);
return t;
}
static inline bool //
wuffs_base__range_ie_u32__contains(const wuffs_base__range_ie_u32* r,
uint32_t x) {
return (r->min_incl <= x) && (x < r->max_excl);
}
static inline bool //
wuffs_base__range_ie_u32__contains_range(const wuffs_base__range_ie_u32* r,
wuffs_base__range_ie_u32 s) {
return wuffs_base__range_ie_u32__equals(
&s, wuffs_base__range_ie_u32__intersect(r, s));
}
static inline uint32_t //
wuffs_base__range_ie_u32__length(const wuffs_base__range_ie_u32* r) {
return wuffs_base__u32__sat_sub(r->max_excl, r->min_incl);
}
#ifdef __cplusplus
inline bool //
wuffs_base__range_ie_u32::is_empty() const {
return wuffs_base__range_ie_u32__is_empty(this);
}
inline bool //
wuffs_base__range_ie_u32::equals(wuffs_base__range_ie_u32 s) const {
return wuffs_base__range_ie_u32__equals(this, s);
}
inline wuffs_base__range_ie_u32 //
wuffs_base__range_ie_u32::intersect(wuffs_base__range_ie_u32 s) const {
return wuffs_base__range_ie_u32__intersect(this, s);
}
inline wuffs_base__range_ie_u32 //
wuffs_base__range_ie_u32::unite(wuffs_base__range_ie_u32 s) const {
return wuffs_base__range_ie_u32__unite(this, s);
}
inline bool //
wuffs_base__range_ie_u32::contains(uint32_t x) const {
return wuffs_base__range_ie_u32__contains(this, x);
}
inline bool //
wuffs_base__range_ie_u32::contains_range(wuffs_base__range_ie_u32 s) const {
return wuffs_base__range_ie_u32__contains_range(this, s);
}
inline uint32_t //
wuffs_base__range_ie_u32::length() const {
return wuffs_base__range_ie_u32__length(this);
}
#endif // __cplusplus
// --------
typedef struct wuffs_base__range_ii_u64__struct {
uint64_t min_incl;
uint64_t max_incl;
#ifdef __cplusplus
inline bool is_empty() const;
inline bool equals(wuffs_base__range_ii_u64__struct s) const;
inline wuffs_base__range_ii_u64__struct intersect(
wuffs_base__range_ii_u64__struct s) const;
inline wuffs_base__range_ii_u64__struct unite(
wuffs_base__range_ii_u64__struct s) const;
inline bool contains(uint64_t x) const;
inline bool contains_range(wuffs_base__range_ii_u64__struct s) const;
#endif // __cplusplus
} wuffs_base__range_ii_u64;
static inline wuffs_base__range_ii_u64 //
wuffs_base__empty_range_ii_u64() {
wuffs_base__range_ii_u64 ret;
ret.min_incl = 0;
ret.max_incl = 0;
return ret;
}
static inline wuffs_base__range_ii_u64 //
wuffs_base__make_range_ii_u64(uint64_t min_incl, uint64_t max_incl) {
wuffs_base__range_ii_u64 ret;
ret.min_incl = min_incl;
ret.max_incl = max_incl;
return ret;
}
static inline bool //
wuffs_base__range_ii_u64__is_empty(const wuffs_base__range_ii_u64* r) {
return r->min_incl > r->max_incl;
}
static inline bool //
wuffs_base__range_ii_u64__equals(const wuffs_base__range_ii_u64* r,
wuffs_base__range_ii_u64 s) {
return (r->min_incl == s.min_incl && r->max_incl == s.max_incl) ||
(wuffs_base__range_ii_u64__is_empty(r) &&
wuffs_base__range_ii_u64__is_empty(&s));
}
static inline wuffs_base__range_ii_u64 //
wuffs_base__range_ii_u64__intersect(const wuffs_base__range_ii_u64* r,
wuffs_base__range_ii_u64 s) {
wuffs_base__range_ii_u64 t;
t.min_incl = wuffs_base__u64__max(r->min_incl, s.min_incl);
t.max_incl = wuffs_base__u64__min(r->max_incl, s.max_incl);
return t;
}
static inline wuffs_base__range_ii_u64 //
wuffs_base__range_ii_u64__unite(const wuffs_base__range_ii_u64* r,
wuffs_base__range_ii_u64 s) {
if (wuffs_base__range_ii_u64__is_empty(r)) {
return s;
}
if (wuffs_base__range_ii_u64__is_empty(&s)) {
return *r;
}
wuffs_base__range_ii_u64 t;
t.min_incl = wuffs_base__u64__min(r->min_incl, s.min_incl);
t.max_incl = wuffs_base__u64__max(r->max_incl, s.max_incl);
return t;
}
static inline bool //
wuffs_base__range_ii_u64__contains(const wuffs_base__range_ii_u64* r,
uint64_t x) {
return (r->min_incl <= x) && (x <= r->max_incl);
}
static inline bool //
wuffs_base__range_ii_u64__contains_range(const wuffs_base__range_ii_u64* r,
wuffs_base__range_ii_u64 s) {
return wuffs_base__range_ii_u64__equals(
&s, wuffs_base__range_ii_u64__intersect(r, s));
}
#ifdef __cplusplus
inline bool //
wuffs_base__range_ii_u64::is_empty() const {
return wuffs_base__range_ii_u64__is_empty(this);
}
inline bool //
wuffs_base__range_ii_u64::equals(wuffs_base__range_ii_u64 s) const {
return wuffs_base__range_ii_u64__equals(this, s);
}
inline wuffs_base__range_ii_u64 //
wuffs_base__range_ii_u64::intersect(wuffs_base__range_ii_u64 s) const {
return wuffs_base__range_ii_u64__intersect(this, s);
}
inline wuffs_base__range_ii_u64 //
wuffs_base__range_ii_u64::unite(wuffs_base__range_ii_u64 s) const {
return wuffs_base__range_ii_u64__unite(this, s);
}
inline bool //
wuffs_base__range_ii_u64::contains(uint64_t x) const {
return wuffs_base__range_ii_u64__contains(this, x);
}
inline bool //
wuffs_base__range_ii_u64::contains_range(wuffs_base__range_ii_u64 s) const {
return wuffs_base__range_ii_u64__contains_range(this, s);
}
#endif // __cplusplus
// --------
typedef struct wuffs_base__range_ie_u64__struct {
uint64_t min_incl;
uint64_t max_excl;
#ifdef __cplusplus
inline bool is_empty() const;
inline bool equals(wuffs_base__range_ie_u64__struct s) const;
inline wuffs_base__range_ie_u64__struct intersect(
wuffs_base__range_ie_u64__struct s) const;
inline wuffs_base__range_ie_u64__struct unite(
wuffs_base__range_ie_u64__struct s) const;
inline bool contains(uint64_t x) const;
inline bool contains_range(wuffs_base__range_ie_u64__struct s) const;
inline uint64_t length() const;
#endif // __cplusplus
} wuffs_base__range_ie_u64;
static inline wuffs_base__range_ie_u64 //
wuffs_base__empty_range_ie_u64() {
wuffs_base__range_ie_u64 ret;
ret.min_incl = 0;
ret.max_excl = 0;
return ret;
}
static inline wuffs_base__range_ie_u64 //
wuffs_base__make_range_ie_u64(uint64_t min_incl, uint64_t max_excl) {
wuffs_base__range_ie_u64 ret;
ret.min_incl = min_incl;
ret.max_excl = max_excl;
return ret;
}
static inline bool //
wuffs_base__range_ie_u64__is_empty(const wuffs_base__range_ie_u64* r) {
return r->min_incl >= r->max_excl;
}
static inline bool //
wuffs_base__range_ie_u64__equals(const wuffs_base__range_ie_u64* r,
wuffs_base__range_ie_u64 s) {
return (r->min_incl == s.min_incl && r->max_excl == s.max_excl) ||
(wuffs_base__range_ie_u64__is_empty(r) &&
wuffs_base__range_ie_u64__is_empty(&s));
}
static inline wuffs_base__range_ie_u64 //
wuffs_base__range_ie_u64__intersect(const wuffs_base__range_ie_u64* r,
wuffs_base__range_ie_u64 s) {
wuffs_base__range_ie_u64 t;
t.min_incl = wuffs_base__u64__max(r->min_incl, s.min_incl);
t.max_excl = wuffs_base__u64__min(r->max_excl, s.max_excl);
return t;
}
static inline wuffs_base__range_ie_u64 //
wuffs_base__range_ie_u64__unite(const wuffs_base__range_ie_u64* r,
wuffs_base__range_ie_u64 s) {
if (wuffs_base__range_ie_u64__is_empty(r)) {
return s;
}
if (wuffs_base__range_ie_u64__is_empty(&s)) {
return *r;
}
wuffs_base__range_ie_u64 t;
t.min_incl = wuffs_base__u64__min(r->min_incl, s.min_incl);
t.max_excl = wuffs_base__u64__max(r->max_excl, s.max_excl);
return t;
}
static inline bool //
wuffs_base__range_ie_u64__contains(const wuffs_base__range_ie_u64* r,
uint64_t x) {
return (r->min_incl <= x) && (x < r->max_excl);
}
static inline bool //
wuffs_base__range_ie_u64__contains_range(const wuffs_base__range_ie_u64* r,
wuffs_base__range_ie_u64 s) {
return wuffs_base__range_ie_u64__equals(
&s, wuffs_base__range_ie_u64__intersect(r, s));
}
static inline uint64_t //
wuffs_base__range_ie_u64__length(const wuffs_base__range_ie_u64* r) {
return wuffs_base__u64__sat_sub(r->max_excl, r->min_incl);
}
#ifdef __cplusplus
inline bool //
wuffs_base__range_ie_u64::is_empty() const {
return wuffs_base__range_ie_u64__is_empty(this);
}
inline bool //
wuffs_base__range_ie_u64::equals(wuffs_base__range_ie_u64 s) const {
return wuffs_base__range_ie_u64__equals(this, s);
}
inline wuffs_base__range_ie_u64 //
wuffs_base__range_ie_u64::intersect(wuffs_base__range_ie_u64 s) const {
return wuffs_base__range_ie_u64__intersect(this, s);
}
inline wuffs_base__range_ie_u64 //
wuffs_base__range_ie_u64::unite(wuffs_base__range_ie_u64 s) const {
return wuffs_base__range_ie_u64__unite(this, s);
}
inline bool //
wuffs_base__range_ie_u64::contains(uint64_t x) const {
return wuffs_base__range_ie_u64__contains(this, x);
}
inline bool //
wuffs_base__range_ie_u64::contains_range(wuffs_base__range_ie_u64 s) const {
return wuffs_base__range_ie_u64__contains_range(this, s);
}
inline uint64_t //
wuffs_base__range_ie_u64::length() const {
return wuffs_base__range_ie_u64__length(this);
}
#endif // __cplusplus
// --------
typedef struct wuffs_base__rect_ii_u32__struct {
uint32_t min_incl_x;
uint32_t min_incl_y;
uint32_t max_incl_x;
uint32_t max_incl_y;
#ifdef __cplusplus
inline bool is_empty() const;
inline bool equals(wuffs_base__rect_ii_u32__struct s) const;
inline wuffs_base__rect_ii_u32__struct intersect(
wuffs_base__rect_ii_u32__struct s) const;
inline wuffs_base__rect_ii_u32__struct unite(
wuffs_base__rect_ii_u32__struct s) const;
inline bool contains(uint32_t x, uint32_t y) const;
inline bool contains_rect(wuffs_base__rect_ii_u32__struct s) const;
#endif // __cplusplus
} wuffs_base__rect_ii_u32;
static inline wuffs_base__rect_ii_u32 //
wuffs_base__empty_rect_ii_u32() {
wuffs_base__rect_ii_u32 ret;
ret.min_incl_x = 0;
ret.min_incl_y = 0;
ret.max_incl_x = 0;
ret.max_incl_y = 0;
return ret;
}
static inline wuffs_base__rect_ii_u32 //
wuffs_base__make_rect_ii_u32(uint32_t min_incl_x,
uint32_t min_incl_y,
uint32_t max_incl_x,
uint32_t max_incl_y) {
wuffs_base__rect_ii_u32 ret;
ret.min_incl_x = min_incl_x;
ret.min_incl_y = min_incl_y;
ret.max_incl_x = max_incl_x;
ret.max_incl_y = max_incl_y;
return ret;
}
static inline bool //
wuffs_base__rect_ii_u32__is_empty(const wuffs_base__rect_ii_u32* r) {
return (r->min_incl_x > r->max_incl_x) || (r->min_incl_y > r->max_incl_y);
}
static inline bool //
wuffs_base__rect_ii_u32__equals(const wuffs_base__rect_ii_u32* r,
wuffs_base__rect_ii_u32 s) {
return (r->min_incl_x == s.min_incl_x && r->min_incl_y == s.min_incl_y &&
r->max_incl_x == s.max_incl_x && r->max_incl_y == s.max_incl_y) ||
(wuffs_base__rect_ii_u32__is_empty(r) &&
wuffs_base__rect_ii_u32__is_empty(&s));
}
static inline wuffs_base__rect_ii_u32 //
wuffs_base__rect_ii_u32__intersect(const wuffs_base__rect_ii_u32* r,
wuffs_base__rect_ii_u32 s) {
wuffs_base__rect_ii_u32 t;
t.min_incl_x = wuffs_base__u32__max(r->min_incl_x, s.min_incl_x);
t.min_incl_y = wuffs_base__u32__max(r->min_incl_y, s.min_incl_y);
t.max_incl_x = wuffs_base__u32__min(r->max_incl_x, s.max_incl_x);
t.max_incl_y = wuffs_base__u32__min(r->max_incl_y, s.max_incl_y);
return t;
}
static inline wuffs_base__rect_ii_u32 //
wuffs_base__rect_ii_u32__unite(const wuffs_base__rect_ii_u32* r,
wuffs_base__rect_ii_u32 s) {
if (wuffs_base__rect_ii_u32__is_empty(r)) {
return s;
}
if (wuffs_base__rect_ii_u32__is_empty(&s)) {
return *r;
}
wuffs_base__rect_ii_u32 t;
t.min_incl_x = wuffs_base__u32__min(r->min_incl_x, s.min_incl_x);
t.min_incl_y = wuffs_base__u32__min(r->min_incl_y, s.min_incl_y);
t.max_incl_x = wuffs_base__u32__max(r->max_incl_x, s.max_incl_x);
t.max_incl_y = wuffs_base__u32__max(r->max_incl_y, s.max_incl_y);
return t;
}
static inline bool //
wuffs_base__rect_ii_u32__contains(const wuffs_base__rect_ii_u32* r,
uint32_t x,
uint32_t y) {
return (r->min_incl_x <= x) && (x <= r->max_incl_x) && (r->min_incl_y <= y) &&
(y <= r->max_incl_y);
}
static inline bool //
wuffs_base__rect_ii_u32__contains_rect(const wuffs_base__rect_ii_u32* r,
wuffs_base__rect_ii_u32 s) {
return wuffs_base__rect_ii_u32__equals(
&s, wuffs_base__rect_ii_u32__intersect(r, s));
}
#ifdef __cplusplus
inline bool //
wuffs_base__rect_ii_u32::is_empty() const {
return wuffs_base__rect_ii_u32__is_empty(this);
}
inline bool //
wuffs_base__rect_ii_u32::equals(wuffs_base__rect_ii_u32 s) const {
return wuffs_base__rect_ii_u32__equals(this, s);
}
inline wuffs_base__rect_ii_u32 //
wuffs_base__rect_ii_u32::intersect(wuffs_base__rect_ii_u32 s) const {
return wuffs_base__rect_ii_u32__intersect(this, s);
}
inline wuffs_base__rect_ii_u32 //
wuffs_base__rect_ii_u32::unite(wuffs_base__rect_ii_u32 s) const {
return wuffs_base__rect_ii_u32__unite(this, s);
}
inline bool //
wuffs_base__rect_ii_u32::contains(uint32_t x, uint32_t y) const {
return wuffs_base__rect_ii_u32__contains(this, x, y);
}
inline bool //
wuffs_base__rect_ii_u32::contains_rect(wuffs_base__rect_ii_u32 s) const {
return wuffs_base__rect_ii_u32__contains_rect(this, s);
}
#endif // __cplusplus
// --------
typedef struct wuffs_base__rect_ie_u32__struct {
uint32_t min_incl_x;
uint32_t min_incl_y;
uint32_t max_excl_x;
uint32_t max_excl_y;
#ifdef __cplusplus
inline bool is_empty() const;
inline bool equals(wuffs_base__rect_ie_u32__struct s) const;
inline wuffs_base__rect_ie_u32__struct intersect(
wuffs_base__rect_ie_u32__struct s) const;
inline wuffs_base__rect_ie_u32__struct unite(
wuffs_base__rect_ie_u32__struct s) const;
inline bool contains(uint32_t x, uint32_t y) const;
inline bool contains_rect(wuffs_base__rect_ie_u32__struct s) const;
inline uint32_t width() const;
inline uint32_t height() const;
#endif // __cplusplus
} wuffs_base__rect_ie_u32;
static inline wuffs_base__rect_ie_u32 //
wuffs_base__empty_rect_ie_u32() {
wuffs_base__rect_ie_u32 ret;
ret.min_incl_x = 0;
ret.min_incl_y = 0;
ret.max_excl_x = 0;
ret.max_excl_y = 0;
return ret;
}
static inline wuffs_base__rect_ie_u32 //
wuffs_base__make_rect_ie_u32(uint32_t min_incl_x,
uint32_t min_incl_y,
uint32_t max_excl_x,
uint32_t max_excl_y) {
wuffs_base__rect_ie_u32 ret;
ret.min_incl_x = min_incl_x;
ret.min_incl_y = min_incl_y;
ret.max_excl_x = max_excl_x;
ret.max_excl_y = max_excl_y;
return ret;
}
static inline bool //
wuffs_base__rect_ie_u32__is_empty(const wuffs_base__rect_ie_u32* r) {
return (r->min_incl_x >= r->max_excl_x) || (r->min_incl_y >= r->max_excl_y);
}
static inline bool //
wuffs_base__rect_ie_u32__equals(const wuffs_base__rect_ie_u32* r,
wuffs_base__rect_ie_u32 s) {
return (r->min_incl_x == s.min_incl_x && r->min_incl_y == s.min_incl_y &&
r->max_excl_x == s.max_excl_x && r->max_excl_y == s.max_excl_y) ||
(wuffs_base__rect_ie_u32__is_empty(r) &&
wuffs_base__rect_ie_u32__is_empty(&s));
}
static inline wuffs_base__rect_ie_u32 //
wuffs_base__rect_ie_u32__intersect(const wuffs_base__rect_ie_u32* r,
wuffs_base__rect_ie_u32 s) {
wuffs_base__rect_ie_u32 t;
t.min_incl_x = wuffs_base__u32__max(r->min_incl_x, s.min_incl_x);
t.min_incl_y = wuffs_base__u32__max(r->min_incl_y, s.min_incl_y);
t.max_excl_x = wuffs_base__u32__min(r->max_excl_x, s.max_excl_x);
t.max_excl_y = wuffs_base__u32__min(r->max_excl_y, s.max_excl_y);
return t;
}
static inline wuffs_base__rect_ie_u32 //
wuffs_base__rect_ie_u32__unite(const wuffs_base__rect_ie_u32* r,
wuffs_base__rect_ie_u32 s) {
if (wuffs_base__rect_ie_u32__is_empty(r)) {
return s;
}
if (wuffs_base__rect_ie_u32__is_empty(&s)) {
return *r;
}
wuffs_base__rect_ie_u32 t;
t.min_incl_x = wuffs_base__u32__min(r->min_incl_x, s.min_incl_x);
t.min_incl_y = wuffs_base__u32__min(r->min_incl_y, s.min_incl_y);
t.max_excl_x = wuffs_base__u32__max(r->max_excl_x, s.max_excl_x);
t.max_excl_y = wuffs_base__u32__max(r->max_excl_y, s.max_excl_y);
return t;
}
static inline bool //
wuffs_base__rect_ie_u32__contains(const wuffs_base__rect_ie_u32* r,
uint32_t x,
uint32_t y) {
return (r->min_incl_x <= x) && (x < r->max_excl_x) && (r->min_incl_y <= y) &&
(y < r->max_excl_y);
}
static inline bool //
wuffs_base__rect_ie_u32__contains_rect(const wuffs_base__rect_ie_u32* r,
wuffs_base__rect_ie_u32 s) {
return wuffs_base__rect_ie_u32__equals(
&s, wuffs_base__rect_ie_u32__intersect(r, s));
}
static inline uint32_t //
wuffs_base__rect_ie_u32__width(const wuffs_base__rect_ie_u32* r) {
return wuffs_base__u32__sat_sub(r->max_excl_x, r->min_incl_x);
}
static inline uint32_t //
wuffs_base__rect_ie_u32__height(const wuffs_base__rect_ie_u32* r) {
return wuffs_base__u32__sat_sub(r->max_excl_y, r->min_incl_y);
}
#ifdef __cplusplus
inline bool //
wuffs_base__rect_ie_u32::is_empty() const {
return wuffs_base__rect_ie_u32__is_empty(this);
}
inline bool //
wuffs_base__rect_ie_u32::equals(wuffs_base__rect_ie_u32 s) const {
return wuffs_base__rect_ie_u32__equals(this, s);
}
inline wuffs_base__rect_ie_u32 //
wuffs_base__rect_ie_u32::intersect(wuffs_base__rect_ie_u32 s) const {
return wuffs_base__rect_ie_u32__intersect(this, s);
}
inline wuffs_base__rect_ie_u32 //
wuffs_base__rect_ie_u32::unite(wuffs_base__rect_ie_u32 s) const {
return wuffs_base__rect_ie_u32__unite(this, s);
}
inline bool //
wuffs_base__rect_ie_u32::contains(uint32_t x, uint32_t y) const {
return wuffs_base__rect_ie_u32__contains(this, x, y);
}
inline bool //
wuffs_base__rect_ie_u32::contains_rect(wuffs_base__rect_ie_u32 s) const {
return wuffs_base__rect_ie_u32__contains_rect(this, s);
}
inline uint32_t //
wuffs_base__rect_ie_u32::width() const {
return wuffs_base__rect_ie_u32__width(this);
}
inline uint32_t //
wuffs_base__rect_ie_u32::height() const {
return wuffs_base__rect_ie_u32__height(this);
}
#endif // __cplusplus
// ---------------- More Information
// wuffs_base__more_information holds additional fields, typically when a Wuffs
// method returns a [note status](/doc/note/statuses.md).
//
// The flavor field follows the base38 namespace
// convention](/doc/note/base38-and-fourcc.md). The other fields' semantics
// depends on the flavor.
typedef struct wuffs_base__more_information__struct {
uint32_t flavor;
uint32_t w;
uint64_t x;
uint64_t y;
uint64_t z;
#ifdef __cplusplus
inline void set(uint32_t flavor_arg,
uint32_t w_arg,
uint64_t x_arg,
uint64_t y_arg,
uint64_t z_arg);
inline uint32_t io_redirect__fourcc() const;
inline wuffs_base__range_ie_u64 io_redirect__range() const;
inline uint64_t io_seek__position() const;
inline uint32_t metadata__fourcc() const;
inline wuffs_base__range_ie_u64 metadata__range() const;
#endif // __cplusplus
} wuffs_base__more_information;
#define WUFFS_BASE__MORE_INFORMATION__FLAVOR__IO_REDIRECT 1
#define WUFFS_BASE__MORE_INFORMATION__FLAVOR__IO_SEEK 2
#define WUFFS_BASE__MORE_INFORMATION__FLAVOR__METADATA 3
static inline wuffs_base__more_information //
wuffs_base__empty_more_information() {
wuffs_base__more_information ret;
ret.flavor = 0;
ret.w = 0;
ret.x = 0;
ret.y = 0;
ret.z = 0;
return ret;
}
static inline void //
wuffs_base__more_information__set(wuffs_base__more_information* m,
uint32_t flavor,
uint32_t w,
uint64_t x,
uint64_t y,
uint64_t z) {
if (!m) {
return;
}
m->flavor = flavor;
m->w = w;
m->x = x;
m->y = y;
m->z = z;
}
static inline uint32_t //
wuffs_base__more_information__io_redirect__fourcc(
const wuffs_base__more_information* m) {
return m->w;
}
static inline wuffs_base__range_ie_u64 //
wuffs_base__more_information__io_redirect__range(
const wuffs_base__more_information* m) {
wuffs_base__range_ie_u64 ret;
ret.min_incl = m->y;
ret.max_excl = m->z;
return ret;
}
static inline uint64_t //
wuffs_base__more_information__io_seek__position(
const wuffs_base__more_information* m) {
return m->x;
}
static inline uint32_t //
wuffs_base__more_information__metadata__fourcc(
const wuffs_base__more_information* m) {
return m->w;
}
static inline wuffs_base__range_ie_u64 //
wuffs_base__more_information__metadata__range(
const wuffs_base__more_information* m) {
wuffs_base__range_ie_u64 ret;
ret.min_incl = m->y;
ret.max_excl = m->z;
return ret;
}
#ifdef __cplusplus
inline void //
wuffs_base__more_information::set(uint32_t flavor_arg,
uint32_t w_arg,
uint64_t x_arg,
uint64_t y_arg,
uint64_t z_arg) {
wuffs_base__more_information__set(this, flavor_arg, w_arg, x_arg, y_arg,
z_arg);
}
inline uint32_t //
wuffs_base__more_information::io_redirect__fourcc() const {
return wuffs_base__more_information__io_redirect__fourcc(this);
}
inline wuffs_base__range_ie_u64 //
wuffs_base__more_information::io_redirect__range() const {
return wuffs_base__more_information__io_redirect__range(this);
}
inline uint64_t //
wuffs_base__more_information::io_seek__position() const {
return wuffs_base__more_information__io_seek__position(this);
}
inline uint32_t //
wuffs_base__more_information::metadata__fourcc() const {
return wuffs_base__more_information__metadata__fourcc(this);
}
inline wuffs_base__range_ie_u64 //
wuffs_base__more_information::metadata__range() const {
return wuffs_base__more_information__metadata__range(this);
}
#endif // __cplusplus
// ---------------- I/O
//
// See (/doc/note/io-input-output.md).
// wuffs_base__io_buffer_meta is the metadata for a wuffs_base__io_buffer's
// data.
typedef struct wuffs_base__io_buffer_meta__struct {
size_t wi; // Write index. Invariant: wi <= len.
size_t ri; // Read index. Invariant: ri <= wi.
uint64_t pos; // Buffer position (relative to the start of stream).
bool closed; // No further writes are expected.
} wuffs_base__io_buffer_meta;
// wuffs_base__io_buffer is a 1-dimensional buffer (a pointer and length) plus
// additional metadata.
//
// A value with all fields zero is a valid, empty buffer.
typedef struct wuffs_base__io_buffer__struct {
wuffs_base__slice_u8 data;
wuffs_base__io_buffer_meta meta;
#ifdef __cplusplus
inline bool is_valid() const;
inline void compact();
inline size_t reader_length() const;
inline uint8_t* reader_pointer() const;
inline uint64_t reader_position() const;
inline wuffs_base__slice_u8 reader_slice() const;
inline size_t writer_length() const;
inline uint8_t* writer_pointer() const;
inline uint64_t writer_position() const;
inline wuffs_base__slice_u8 writer_slice() const;
// Deprecated: use reader_position.
inline uint64_t reader_io_position() const;
// Deprecated: use writer_position.
inline uint64_t writer_io_position() const;
#endif // __cplusplus
} wuffs_base__io_buffer;
static inline wuffs_base__io_buffer //
wuffs_base__make_io_buffer(wuffs_base__slice_u8 data,
wuffs_base__io_buffer_meta meta) {
wuffs_base__io_buffer ret;
ret.data = data;
ret.meta = meta;
return ret;
}
static inline wuffs_base__io_buffer_meta //
wuffs_base__make_io_buffer_meta(size_t wi,
size_t ri,
uint64_t pos,
bool closed) {
wuffs_base__io_buffer_meta ret;
ret.wi = wi;
ret.ri = ri;
ret.pos = pos;
ret.closed = closed;
return ret;
}
static inline wuffs_base__io_buffer //
wuffs_base__ptr_u8__reader(uint8_t* ptr, size_t len, bool closed) {
wuffs_base__io_buffer ret;
ret.data.ptr = ptr;
ret.data.len = len;
ret.meta.wi = len;
ret.meta.ri = 0;
ret.meta.pos = 0;
ret.meta.closed = closed;
return ret;
}
static inline wuffs_base__io_buffer //
wuffs_base__ptr_u8__writer(uint8_t* ptr, size_t len) {
wuffs_base__io_buffer ret;
ret.data.ptr = ptr;
ret.data.len = len;
ret.meta.wi = 0;
ret.meta.ri = 0;
ret.meta.pos = 0;
ret.meta.closed = false;
return ret;
}
static inline wuffs_base__io_buffer //
wuffs_base__slice_u8__reader(wuffs_base__slice_u8 s, bool closed) {
wuffs_base__io_buffer ret;
ret.data.ptr = s.ptr;
ret.data.len = s.len;
ret.meta.wi = s.len;
ret.meta.ri = 0;
ret.meta.pos = 0;
ret.meta.closed = closed;
return ret;
}
static inline wuffs_base__io_buffer //
wuffs_base__slice_u8__writer(wuffs_base__slice_u8 s) {
wuffs_base__io_buffer ret;
ret.data.ptr = s.ptr;
ret.data.len = s.len;
ret.meta.wi = 0;
ret.meta.ri = 0;
ret.meta.pos = 0;
ret.meta.closed = false;
return ret;
}
static inline wuffs_base__io_buffer //
wuffs_base__empty_io_buffer() {
wuffs_base__io_buffer ret;
ret.data.ptr = NULL;
ret.data.len = 0;
ret.meta.wi = 0;
ret.meta.ri = 0;
ret.meta.pos = 0;
ret.meta.closed = false;
return ret;
}
static inline wuffs_base__io_buffer_meta //
wuffs_base__empty_io_buffer_meta() {
wuffs_base__io_buffer_meta ret;
ret.wi = 0;
ret.ri = 0;
ret.pos = 0;
ret.closed = false;
return ret;
}
static inline bool //
wuffs_base__io_buffer__is_valid(const wuffs_base__io_buffer* buf) {
if (buf) {
if (buf->data.ptr) {
return (buf->meta.ri <= buf->meta.wi) && (buf->meta.wi <= buf->data.len);
} else {
return (buf->meta.ri == 0) && (buf->meta.wi == 0) && (buf->data.len == 0);
}
}
return false;
}
// wuffs_base__io_buffer__compact moves any written but unread bytes to the
// start of the buffer.
static inline void //
wuffs_base__io_buffer__compact(wuffs_base__io_buffer* buf) {
if (!buf || (buf->meta.ri == 0)) {
return;
}
buf->meta.pos = wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri);
size_t n = buf->meta.wi - buf->meta.ri;
if (n != 0) {
memmove(buf->data.ptr, buf->data.ptr + buf->meta.ri, n);
}
buf->meta.wi = n;
buf->meta.ri = 0;
}
// Deprecated. Use wuffs_base__io_buffer__reader_position.
static inline uint64_t //
wuffs_base__io_buffer__reader_io_position(const wuffs_base__io_buffer* buf) {
return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri) : 0;
}
static inline size_t //
wuffs_base__io_buffer__reader_length(const wuffs_base__io_buffer* buf) {
return buf ? buf->meta.wi - buf->meta.ri : 0;
}
static inline uint8_t* //
wuffs_base__io_buffer__reader_pointer(const wuffs_base__io_buffer* buf) {
return buf ? (buf->data.ptr + buf->meta.ri) : NULL;
}
static inline uint64_t //
wuffs_base__io_buffer__reader_position(const wuffs_base__io_buffer* buf) {
return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri) : 0;
}
static inline wuffs_base__slice_u8 //
wuffs_base__io_buffer__reader_slice(const wuffs_base__io_buffer* buf) {
return buf ? wuffs_base__make_slice_u8(buf->data.ptr + buf->meta.ri,
buf->meta.wi - buf->meta.ri)
: wuffs_base__empty_slice_u8();
}
// Deprecated. Use wuffs_base__io_buffer__writer_position.
static inline uint64_t //
wuffs_base__io_buffer__writer_io_position(const wuffs_base__io_buffer* buf) {
return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.wi) : 0;
}
static inline size_t //
wuffs_base__io_buffer__writer_length(const wuffs_base__io_buffer* buf) {
return buf ? buf->data.len - buf->meta.wi : 0;
}
static inline uint8_t* //
wuffs_base__io_buffer__writer_pointer(const wuffs_base__io_buffer* buf) {
return buf ? (buf->data.ptr + buf->meta.wi) : NULL;
}
static inline uint64_t //
wuffs_base__io_buffer__writer_position(const wuffs_base__io_buffer* buf) {
return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.wi) : 0;
}
static inline wuffs_base__slice_u8 //
wuffs_base__io_buffer__writer_slice(const wuffs_base__io_buffer* buf) {
return buf ? wuffs_base__make_slice_u8(buf->data.ptr + buf->meta.wi,
buf->data.len - buf->meta.wi)
: wuffs_base__empty_slice_u8();
}
#ifdef __cplusplus
inline bool //
wuffs_base__io_buffer::is_valid() const {
return wuffs_base__io_buffer__is_valid(this);
}
inline void //
wuffs_base__io_buffer::compact() {
wuffs_base__io_buffer__compact(this);
}
inline uint64_t //
wuffs_base__io_buffer::reader_io_position() const {
return wuffs_base__io_buffer__reader_io_position(this);
}
inline size_t //
wuffs_base__io_buffer::reader_length() const {
return wuffs_base__io_buffer__reader_length(this);
}
inline uint8_t* //
wuffs_base__io_buffer::reader_pointer() const {
return wuffs_base__io_buffer__reader_pointer(this);
}
inline uint64_t //
wuffs_base__io_buffer::reader_position() const {
return wuffs_base__io_buffer__reader_position(this);
}
inline wuffs_base__slice_u8 //
wuffs_base__io_buffer::reader_slice() const {
return wuffs_base__io_buffer__reader_slice(this);
}
inline uint64_t //
wuffs_base__io_buffer::writer_io_position() const {
return wuffs_base__io_buffer__writer_io_position(this);
}
inline size_t //
wuffs_base__io_buffer::writer_length() const {
return wuffs_base__io_buffer__writer_length(this);
}
inline uint8_t* //
wuffs_base__io_buffer::writer_pointer() const {
return wuffs_base__io_buffer__writer_pointer(this);
}
inline uint64_t //
wuffs_base__io_buffer::writer_position() const {
return wuffs_base__io_buffer__writer_position(this);
}
inline wuffs_base__slice_u8 //
wuffs_base__io_buffer::writer_slice() const {
return wuffs_base__io_buffer__writer_slice(this);
}
#endif // __cplusplus
// ---------------- Tokens
// wuffs_base__token is an element of a byte stream's tokenization.
//
// See https://github.com/google/wuffs/blob/master/doc/note/tokens.md
typedef struct wuffs_base__token__struct {
uint64_t repr;
#ifdef __cplusplus
inline int64_t value() const;
inline int64_t value_extension() const;
inline int64_t value_major() const;
inline int64_t value_base_category() const;
inline uint64_t value_minor() const;
inline uint64_t value_base_detail() const;
inline int64_t value_base_detail__sign_extended() const;
inline bool continued() const;
inline uint64_t length() const;
#endif // __cplusplus
} wuffs_base__token;
static inline wuffs_base__token //
wuffs_base__make_token(uint64_t repr) {
wuffs_base__token ret;
ret.repr = repr;
return ret;
}
// --------
#define WUFFS_BASE__TOKEN__LENGTH__MAX_INCL 0xFFFF
#define WUFFS_BASE__TOKEN__VALUE__SHIFT 17
#define WUFFS_BASE__TOKEN__VALUE_EXTENSION__SHIFT 17
#define WUFFS_BASE__TOKEN__VALUE_MAJOR__SHIFT 42
#define WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT 17
#define WUFFS_BASE__TOKEN__VALUE_BASE_CATEGORY__SHIFT 38
#define WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT 17
#define WUFFS_BASE__TOKEN__CONTINUED__SHIFT 16
#define WUFFS_BASE__TOKEN__LENGTH__SHIFT 0
#define WUFFS_BASE__TOKEN__VALUE_EXTENSION__NUM_BITS 46
// --------
#define WUFFS_BASE__TOKEN__VBC__FILLER 0
#define WUFFS_BASE__TOKEN__VBC__STRUCTURE 1
#define WUFFS_BASE__TOKEN__VBC__STRING 2
#define WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT 3
#define WUFFS_BASE__TOKEN__VBC__LITERAL 4
#define WUFFS_BASE__TOKEN__VBC__NUMBER 5
#define WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_SIGNED 6
#define WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_UNSIGNED 7
// --------
#define WUFFS_BASE__TOKEN__VBD__FILLER__PUNCTUATION 0x00001
#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_BLOCK 0x00002
#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_LINE 0x00004
// COMMENT_ANY is a bit-wise or of COMMENT_BLOCK AND COMMENT_LINE.
#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_ANY 0x00006
// --------
#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH 0x00001
#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP 0x00002
#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_NONE 0x00010
#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST 0x00020
#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_DICT 0x00040
#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_NONE 0x01000
#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST 0x02000
#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_DICT 0x04000
// --------
// DEFINITELY_FOO means that the destination bytes (and also the source bytes,
// for 1_DST_1_SRC_COPY) are in the FOO format. Definitely means that the lack
// of the bit means "maybe FOO". It does not necessarily mean "not FOO".
//
// CHAIN_ETC means that decoding the entire token chain forms a UTF-8 or ASCII
// string, not just this current token. CHAIN_ETC_UTF_8 therefore distinguishes
// Unicode (UTF-8) strings from byte strings. MUST means that the the token
// producer (e.g. parser) must verify this. SHOULD means that the token
// consumer (e.g. renderer) should verify this.
//
// When a CHAIN_ETC_UTF_8 bit is set, the parser must ensure that non-ASCII
// code points (with multi-byte UTF-8 encodings) do not straddle token
// boundaries. Checking UTF-8 validity can inspect each token separately.
//
// The lack of any particular bit is conservative: it is valid for all-ASCII
// strings, in a single- or multi-token chain, to have none of these bits set.
#define WUFFS_BASE__TOKEN__VBD__STRING__DEFINITELY_UTF_8 0x00001
#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8 0x00002
#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_SHOULD_BE_UTF_8 0x00004
#define WUFFS_BASE__TOKEN__VBD__STRING__DEFINITELY_ASCII 0x00010
#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_ASCII 0x00020
#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_SHOULD_BE_ASCII 0x00040
// CONVERT_D_DST_S_SRC means that multiples of S source bytes (possibly padded)
// produces multiples of D destination bytes. For example,
// CONVERT_1_DST_4_SRC_BACKSLASH_X means a source like "\\x23\\x67\\xAB", where
// 12 src bytes encode 3 dst bytes.
//
// Post-processing may further transform those D destination bytes (e.g. treat
// "\\xFF" as the Unicode code point U+00FF instead of the byte 0xFF), but that
// is out of scope of this VBD's semantics.
//
// When src is the empty string, multiple conversion algorithms are applicable
// (so these bits are not necessarily mutually exclusive), all producing the
// same empty dst string.
#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP 0x00100
#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY 0x00200
#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_2_SRC_HEXADECIMAL 0x00400
#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_4_SRC_BACKSLASH_X 0x00800
#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_3_DST_4_SRC_BASE_64_STD 0x01000
#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_3_DST_4_SRC_BASE_64_URL 0x02000
#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_4_DST_5_SRC_ASCII_85 0x04000
#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_5_DST_8_SRC_BASE_32_HEX 0x08000
#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_5_DST_8_SRC_BASE_32_STD 0x10000
// --------
#define WUFFS_BASE__TOKEN__VBD__LITERAL__UNDEFINED 0x00001
#define WUFFS_BASE__TOKEN__VBD__LITERAL__NULL 0x00002
#define WUFFS_BASE__TOKEN__VBD__LITERAL__FALSE 0x00004
#define WUFFS_BASE__TOKEN__VBD__LITERAL__TRUE 0x00008
// --------
// For a source string of "123" or "0x9A", it is valid for a tokenizer to
// return any combination of:
// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT.
// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED.
// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_UNSIGNED.
//
// For a source string of "+123" or "-0x9A", only the first two are valid.
//
// For a source string of "123.", only the first one is valid.
#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT 0x00001
#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED 0x00002
#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_UNSIGNED 0x00004
#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_INF 0x00010
#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_INF 0x00020
#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_NAN 0x00040
#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_NAN 0x00080
// The number 300 might be represented as "\x01\x2C", "\x2C\x01\x00\x00" or
// "300", which are big-endian, little-endian or text. For binary formats, the
// token length (after adjusting for FORMAT_IGNORE_ETC) discriminates
// e.g. u16 little-endian vs u32 little-endian.
#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_BIG_ENDIAN 0x00100
#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_LITTLE_ENDIAN 0x00200
#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_TEXT 0x00400
#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_IGNORE_FIRST_BYTE 0x01000
// --------
// wuffs_base__token__value returns the token's high 46 bits, sign-extended. A
// negative value means an extended token, non-negative means a simple token.
static inline int64_t //
wuffs_base__token__value(const wuffs_base__token* t) {
return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE__SHIFT;
}
// wuffs_base__token__value_extension returns a negative value if the token was
// not an extended token.
static inline int64_t //
wuffs_base__token__value_extension(const wuffs_base__token* t) {
return (~(int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_EXTENSION__SHIFT;
}
// wuffs_base__token__value_major returns a negative value if the token was not
// a simple token.
static inline int64_t //
wuffs_base__token__value_major(const wuffs_base__token* t) {
return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_MAJOR__SHIFT;
}
// wuffs_base__token__value_base_category returns a negative value if the token
// was not a simple token.
static inline int64_t //
wuffs_base__token__value_base_category(const wuffs_base__token* t) {
return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_BASE_CATEGORY__SHIFT;
}
static inline uint64_t //
wuffs_base__token__value_minor(const wuffs_base__token* t) {
return (t->repr >> WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT) & 0x1FFFFFF;
}
static inline uint64_t //
wuffs_base__token__value_base_detail(const wuffs_base__token* t) {
return (t->repr >> WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT) & 0x1FFFFF;
}
static inline int64_t //
wuffs_base__token__value_base_detail__sign_extended(
const wuffs_base__token* t) {
// The VBD is 21 bits in the middle of t->repr. Left shift the high (64 - 21
// - ETC__SHIFT) bits off, then right shift (sign-extending) back down.
uint64_t u = t->repr << (43 - WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT);
return ((int64_t)u) >> 43;
}
static inline bool //
wuffs_base__token__continued(const wuffs_base__token* t) {
return t->repr & 0x10000;
}
static inline uint64_t //
wuffs_base__token__length(const wuffs_base__token* t) {
return (t->repr >> WUFFS_BASE__TOKEN__LENGTH__SHIFT) & 0xFFFF;
}
#ifdef __cplusplus
inline int64_t //
wuffs_base__token::value() const {
return wuffs_base__token__value(this);
}
inline int64_t //
wuffs_base__token::value_extension() const {
return wuffs_base__token__value_extension(this);
}
inline int64_t //
wuffs_base__token::value_major() const {
return wuffs_base__token__value_major(this);
}
inline int64_t //
wuffs_base__token::value_base_category() const {
return wuffs_base__token__value_base_category(this);
}
inline uint64_t //
wuffs_base__token::value_minor() const {
return wuffs_base__token__value_minor(this);
}
inline uint64_t //
wuffs_base__token::value_base_detail() const {
return wuffs_base__token__value_base_detail(this);
}
inline int64_t //
wuffs_base__token::value_base_detail__sign_extended() const {
return wuffs_base__token__value_base_detail__sign_extended(this);
}
inline bool //
wuffs_base__token::continued() const {
return wuffs_base__token__continued(this);
}
inline uint64_t //
wuffs_base__token::length() const {
return wuffs_base__token__length(this);
}
#endif // __cplusplus
// --------
typedef WUFFS_BASE__SLICE(wuffs_base__token) wuffs_base__slice_token;
static inline wuffs_base__slice_token //
wuffs_base__make_slice_token(wuffs_base__token* ptr, size_t len) {
wuffs_base__slice_token ret;
ret.ptr = ptr;
ret.len = len;
return ret;
}
static inline wuffs_base__slice_token //
wuffs_base__empty_slice_token() {
wuffs_base__slice_token ret;
ret.ptr = NULL;
ret.len = 0;
return ret;
}
// --------
// wuffs_base__token_buffer_meta is the metadata for a
// wuffs_base__token_buffer's data.
typedef struct wuffs_base__token_buffer_meta__struct {
size_t wi; // Write index. Invariant: wi <= len.
size_t ri; // Read index. Invariant: ri <= wi.
uint64_t pos; // Position of the buffer start relative to the stream start.
bool closed; // No further writes are expected.
} wuffs_base__token_buffer_meta;
// wuffs_base__token_buffer is a 1-dimensional buffer (a pointer and length)
// plus additional metadata.
//
// A value with all fields zero is a valid, empty buffer.
typedef struct wuffs_base__token_buffer__struct {
wuffs_base__slice_token data;
wuffs_base__token_buffer_meta meta;
#ifdef __cplusplus
inline bool is_valid() const;
inline void compact();
inline uint64_t reader_length() const;
inline wuffs_base__token* reader_pointer() const;
inline wuffs_base__slice_token reader_slice() const;
inline uint64_t reader_token_position() const;
inline uint64_t writer_length() const;
inline uint64_t writer_token_position() const;
inline wuffs_base__token* writer_pointer() const;
inline wuffs_base__slice_token writer_slice() const;
#endif // __cplusplus
} wuffs_base__token_buffer;
static inline wuffs_base__token_buffer //
wuffs_base__make_token_buffer(wuffs_base__slice_token data,
wuffs_base__token_buffer_meta meta) {
wuffs_base__token_buffer ret;
ret.data = data;
ret.meta = meta;
return ret;
}
static inline wuffs_base__token_buffer_meta //
wuffs_base__make_token_buffer_meta(size_t wi,
size_t ri,
uint64_t pos,
bool closed) {
wuffs_base__token_buffer_meta ret;
ret.wi = wi;
ret.ri = ri;
ret.pos = pos;
ret.closed = closed;
return ret;
}
static inline wuffs_base__token_buffer //
wuffs_base__slice_token__reader(wuffs_base__slice_token s, bool closed) {
wuffs_base__token_buffer ret;
ret.data.ptr = s.ptr;
ret.data.len = s.len;
ret.meta.wi = s.len;
ret.meta.ri = 0;
ret.meta.pos = 0;
ret.meta.closed = closed;
return ret;
}
static inline wuffs_base__token_buffer //
wuffs_base__slice_token__writer(wuffs_base__slice_token s) {
wuffs_base__token_buffer ret;
ret.data.ptr = s.ptr;
ret.data.len = s.len;
ret.meta.wi = 0;
ret.meta.ri = 0;
ret.meta.pos = 0;
ret.meta.closed = false;
return ret;
}
static inline wuffs_base__token_buffer //
wuffs_base__empty_token_buffer() {
wuffs_base__token_buffer ret;
ret.data.ptr = NULL;
ret.data.len = 0;
ret.meta.wi = 0;
ret.meta.ri = 0;
ret.meta.pos = 0;
ret.meta.closed = false;
return ret;
}
static inline wuffs_base__token_buffer_meta //
wuffs_base__empty_token_buffer_meta() {
wuffs_base__token_buffer_meta ret;
ret.wi = 0;
ret.ri = 0;
ret.pos = 0;
ret.closed = false;
return ret;
}
static inline bool //
wuffs_base__token_buffer__is_valid(const wuffs_base__token_buffer* buf) {
if (buf) {
if (buf->data.ptr) {
return (buf->meta.ri <= buf->meta.wi) && (buf->meta.wi <= buf->data.len);
} else {
return (buf->meta.ri == 0) && (buf->meta.wi == 0) && (buf->data.len == 0);
}
}
return false;
}
// wuffs_base__token_buffer__compact moves any written but unread tokens to the
// start of the buffer.
static inline void //
wuffs_base__token_buffer__compact(wuffs_base__token_buffer* buf) {
if (!buf || (buf->meta.ri == 0)) {
return;
}
buf->meta.pos = wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri);
size_t n = buf->meta.wi - buf->meta.ri;
if (n != 0) {
memmove(buf->data.ptr, buf->data.ptr + buf->meta.ri,
n * sizeof(wuffs_base__token));
}
buf->meta.wi = n;
buf->meta.ri = 0;
}
static inline uint64_t //
wuffs_base__token_buffer__reader_length(const wuffs_base__token_buffer* buf) {
return buf ? buf->meta.wi - buf->meta.ri : 0;
}
static inline wuffs_base__token* //
wuffs_base__token_buffer__reader_pointer(const wuffs_base__token_buffer* buf) {
return buf ? (buf->data.ptr + buf->meta.ri) : NULL;
}
static inline wuffs_base__slice_token //
wuffs_base__token_buffer__reader_slice(const wuffs_base__token_buffer* buf) {
return buf ? wuffs_base__make_slice_token(buf->data.ptr + buf->meta.ri,
buf->meta.wi - buf->meta.ri)
: wuffs_base__empty_slice_token();
}
static inline uint64_t //
wuffs_base__token_buffer__reader_token_position(
const wuffs_base__token_buffer* buf) {
return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri) : 0;
}
static inline uint64_t //
wuffs_base__token_buffer__writer_length(const wuffs_base__token_buffer* buf) {
return buf ? buf->data.len - buf->meta.wi : 0;
}
static inline wuffs_base__token* //
wuffs_base__token_buffer__writer_pointer(const wuffs_base__token_buffer* buf) {
return buf ? (buf->data.ptr + buf->meta.wi) : NULL;
}
static inline wuffs_base__slice_token //
wuffs_base__token_buffer__writer_slice(const wuffs_base__token_buffer* buf) {
return buf ? wuffs_base__make_slice_token(buf->data.ptr + buf->meta.wi,
buf->data.len - buf->meta.wi)
: wuffs_base__empty_slice_token();
}
static inline uint64_t //
wuffs_base__token_buffer__writer_token_position(
const wuffs_base__token_buffer* buf) {
return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.wi) : 0;
}
#ifdef __cplusplus
inline bool //
wuffs_base__token_buffer::is_valid() const {
return wuffs_base__token_buffer__is_valid(this);
}
inline void //
wuffs_base__token_buffer::compact() {
wuffs_base__token_buffer__compact(this);
}
inline uint64_t //
wuffs_base__token_buffer::reader_length() const {
return wuffs_base__token_buffer__reader_length(this);
}
inline wuffs_base__token* //
wuffs_base__token_buffer::reader_pointer() const {
return wuffs_base__token_buffer__reader_pointer(this);
}
inline wuffs_base__slice_token //
wuffs_base__token_buffer::reader_slice() const {
return wuffs_base__token_buffer__reader_slice(this);
}
inline uint64_t //
wuffs_base__token_buffer::reader_token_position() const {
return wuffs_base__token_buffer__reader_token_position(this);
}
inline uint64_t //
wuffs_base__token_buffer::writer_length() const {
return wuffs_base__token_buffer__writer_length(this);
}
inline wuffs_base__token* //
wuffs_base__token_buffer::writer_pointer() const {
return wuffs_base__token_buffer__writer_pointer(this);
}
inline wuffs_base__slice_token //
wuffs_base__token_buffer::writer_slice() const {
return wuffs_base__token_buffer__writer_slice(this);
}
inline uint64_t //
wuffs_base__token_buffer::writer_token_position() const {
return wuffs_base__token_buffer__writer_token_position(this);
}
#endif // __cplusplus
// ---------------- Memory Allocation
// The memory allocation related functions in this section aren't used by Wuffs
// per se, but they may be helpful to the code that uses Wuffs.
// wuffs_base__malloc_slice_uxx wraps calling a malloc-like function, except
// that it takes a uint64_t number of elements instead of a size_t size in
// bytes, and it returns a slice (a pointer and a length) instead of just a
// pointer.
//
// You can pass the C stdlib's malloc as the malloc_func.
//
// It returns an empty slice (containing a NULL ptr field) if (num_uxx *
// sizeof(uintxx_t)) would overflow SIZE_MAX.
static inline wuffs_base__slice_u8 //
wuffs_base__malloc_slice_u8(void* (*malloc_func)(size_t), uint64_t num_u8) {
if (malloc_func && (num_u8 <= (SIZE_MAX / sizeof(uint8_t)))) {
void* p = (*malloc_func)(num_u8 * sizeof(uint8_t));
if (p) {
return wuffs_base__make_slice_u8((uint8_t*)(p), num_u8);
}
}
return wuffs_base__make_slice_u8(NULL, 0);
}
static inline wuffs_base__slice_u16 //
wuffs_base__malloc_slice_u16(void* (*malloc_func)(size_t), uint64_t num_u16) {
if (malloc_func && (num_u16 <= (SIZE_MAX / sizeof(uint16_t)))) {
void* p = (*malloc_func)(num_u16 * sizeof(uint16_t));
if (p) {
return wuffs_base__make_slice_u16((uint16_t*)(p), num_u16);
}
}
return wuffs_base__make_slice_u16(NULL, 0);
}
static inline wuffs_base__slice_u32 //
wuffs_base__malloc_slice_u32(void* (*malloc_func)(size_t), uint64_t num_u32) {
if (malloc_func && (num_u32 <= (SIZE_MAX / sizeof(uint32_t)))) {
void* p = (*malloc_func)(num_u32 * sizeof(uint32_t));
if (p) {
return wuffs_base__make_slice_u32((uint32_t*)(p), num_u32);
}
}
return wuffs_base__make_slice_u32(NULL, 0);
}
static inline wuffs_base__slice_u64 //
wuffs_base__malloc_slice_u64(void* (*malloc_func)(size_t), uint64_t num_u64) {
if (malloc_func && (num_u64 <= (SIZE_MAX / sizeof(uint64_t)))) {
void* p = (*malloc_func)(num_u64 * sizeof(uint64_t));
if (p) {
return wuffs_base__make_slice_u64((uint64_t*)(p), num_u64);
}
}
return wuffs_base__make_slice_u64(NULL, 0);
}
// ---------------- Images
// wuffs_base__color_u32_argb_premul is an 8 bit per channel premultiplied
// Alpha, Red, Green, Blue color, as a uint32_t value. Its value is always
// 0xAARRGGBB (Alpha most significant, Blue least), regardless of endianness.
typedef uint32_t wuffs_base__color_u32_argb_premul;
static inline uint16_t //
wuffs_base__color_u32_argb_premul__as__color_u16_rgb_565(
wuffs_base__color_u32_argb_premul c) {
uint32_t r5 = 0xF800 & (c >> 8);
uint32_t g6 = 0x07E0 & (c >> 5);
uint32_t b5 = 0x001F & (c >> 3);
return (uint16_t)(r5 | g6 | b5);
}
static inline wuffs_base__color_u32_argb_premul //
wuffs_base__color_u16_rgb_565__as__color_u32_argb_premul(uint16_t rgb_565) {
uint32_t b5 = 0x1F & (rgb_565 >> 0);
uint32_t b = (b5 << 3) | (b5 >> 2);
uint32_t g6 = 0x3F & (rgb_565 >> 5);
uint32_t g = (g6 << 2) | (g6 >> 4);
uint32_t r5 = 0x1F & (rgb_565 >> 11);
uint32_t r = (r5 << 3) | (r5 >> 2);
return 0xFF000000 | (r << 16) | (g << 8) | (b << 0);
}
static inline uint8_t //
wuffs_base__color_u32_argb_premul__as__color_u8_gray(
wuffs_base__color_u32_argb_premul c) {
// Work in 16-bit color.
uint32_t cr = 0x101 * (0xFF & (c >> 16));
uint32_t cg = 0x101 * (0xFF & (c >> 8));
uint32_t cb = 0x101 * (0xFF & (c >> 0));
// These coefficients (the fractions 0.299, 0.587 and 0.114) are the same
// as those given by the JFIF specification.
//
// Note that 19595 + 38470 + 7471 equals 65536, also known as (1 << 16). We
// shift by 24, not just by 16, because the return value is 8-bit color, not
// 16-bit color.
uint32_t weighted_average = (19595 * cr) + (38470 * cg) + (7471 * cb) + 32768;
return (uint8_t)(weighted_average >> 24);
}
// wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul converts
// from non-premultiplied alpha to premultiplied alpha.
static inline wuffs_base__color_u32_argb_premul //
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
uint32_t argb_nonpremul) {
// Multiplying by 0x101 (twice, once for alpha and once for color) converts
// from 8-bit to 16-bit color. Shifting right by 8 undoes that.
//
// Working in the higher bit depth can produce slightly different (and
// arguably slightly more accurate) results. For example, given 8-bit blue
// and alpha of 0x80 and 0x81:
//
// - ((0x80 * 0x81 ) / 0xFF ) = 0x40 = 0x40
// - ((0x8080 * 0x8181) / 0xFFFF) >> 8 = 0x4101 >> 8 = 0x41
uint32_t a = 0xFF & (argb_nonpremul >> 24);
uint32_t a16 = a * (0x101 * 0x101);
uint32_t r = 0xFF & (argb_nonpremul >> 16);
r = ((r * a16) / 0xFFFF) >> 8;
uint32_t g = 0xFF & (argb_nonpremul >> 8);
g = ((g * a16) / 0xFFFF) >> 8;
uint32_t b = 0xFF & (argb_nonpremul >> 0);
b = ((b * a16) / 0xFFFF) >> 8;
return (a << 24) | (r << 16) | (g << 8) | (b << 0);
}
// wuffs_base__color_u32_argb_premul__as__color_u32_argb_nonpremul converts
// from premultiplied alpha to non-premultiplied alpha.
static inline uint32_t //
wuffs_base__color_u32_argb_premul__as__color_u32_argb_nonpremul(
wuffs_base__color_u32_argb_premul c) {
uint32_t a = 0xFF & (c >> 24);
if (a == 0xFF) {
return c;
} else if (a == 0) {
return 0;
}
uint32_t a16 = a * 0x101;
uint32_t r = 0xFF & (c >> 16);
r = ((r * (0x101 * 0xFFFF)) / a16) >> 8;
uint32_t g = 0xFF & (c >> 8);
g = ((g * (0x101 * 0xFFFF)) / a16) >> 8;
uint32_t b = 0xFF & (c >> 0);
b = ((b * (0x101 * 0xFFFF)) / a16) >> 8;
return (a << 24) | (r << 16) | (g << 8) | (b << 0);
}
// wuffs_base__color_u64_argb_nonpremul__as__color_u32_argb_premul converts
// from 4x16LE non-premultiplied alpha to 4x8 premultiplied alpha.
static inline wuffs_base__color_u32_argb_premul //
wuffs_base__color_u64_argb_nonpremul__as__color_u32_argb_premul(
uint64_t argb_nonpremul) {
uint32_t a16 = ((uint32_t)(0xFFFF & (argb_nonpremul >> 48)));
uint32_t r16 = ((uint32_t)(0xFFFF & (argb_nonpremul >> 32)));
r16 = (r16 * a16) / 0xFFFF;
uint32_t g16 = ((uint32_t)(0xFFFF & (argb_nonpremul >> 16)));
g16 = (g16 * a16) / 0xFFFF;
uint32_t b16 = ((uint32_t)(0xFFFF & (argb_nonpremul >> 0)));
b16 = (b16 * a16) / 0xFFFF;
return ((a16 >> 8) << 24) | ((r16 >> 8) << 16) | ((g16 >> 8) << 8) |
((b16 >> 8) << 0);
}
// wuffs_base__color_u32_argb_premul__as__color_u64_argb_nonpremul converts
// from 4x8 premultiplied alpha to 4x16LE non-premultiplied alpha.
static inline uint64_t //
wuffs_base__color_u32_argb_premul__as__color_u64_argb_nonpremul(
wuffs_base__color_u32_argb_premul c) {
uint32_t a = 0xFF & (c >> 24);
if (a == 0xFF) {
uint64_t r16 = 0x101 * (0xFF & (c >> 16));
uint64_t g16 = 0x101 * (0xFF & (c >> 8));
uint64_t b16 = 0x101 * (0xFF & (c >> 0));
return 0xFFFF000000000000u | (r16 << 32) | (g16 << 16) | (b16 << 0);
} else if (a == 0) {
return 0;
}
uint64_t a16 = a * 0x101;
uint64_t r = 0xFF & (c >> 16);
uint64_t r16 = (r * (0x101 * 0xFFFF)) / a16;
uint64_t g = 0xFF & (c >> 8);
uint64_t g16 = (g * (0x101 * 0xFFFF)) / a16;
uint64_t b = 0xFF & (c >> 0);
uint64_t b16 = (b * (0x101 * 0xFFFF)) / a16;
return (a16 << 48) | (r16 << 32) | (g16 << 16) | (b16 << 0);
}
static inline uint64_t //
wuffs_base__color_u32__as__color_u64(uint32_t c) {
uint64_t a16 = 0x101 * (0xFF & (c >> 24));
uint64_t r16 = 0x101 * (0xFF & (c >> 16));
uint64_t g16 = 0x101 * (0xFF & (c >> 8));
uint64_t b16 = 0x101 * (0xFF & (c >> 0