blob: b8945bbeb38c1525ad9c6eb2d914aabd68409dc0 [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.
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#pragma GCC diagnostic ignored "-Wunreachable-code"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#if defined(__cplusplus)
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
#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) || defined(_MSC_VER)
#include <memory>
#define WUFFS_BASE__HAVE_EQ_DELETE
#define WUFFS_BASE__HAVE_UNIQUE_PTR
#elif defined(__GNUC__)
#warning "Wuffs' C++ code expects -std=c++11 or later"
#endif
extern "C" {
#endif
// ---------------- Version
// 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.
//
// ยก Some code generation programs can override WUFFS_VERSION.
#define WUFFS_VERSION 0
#define WUFFS_VERSION_MAJOR 0
#define WUFFS_VERSION_MINOR 0
#define WUFFS_VERSION_PATCH 0
#define WUFFS_VERSION_PRE_RELEASE_LABEL "work.in.progress"
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT 0
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_DATE 0
#define WUFFS_VERSION_STRING "0.0.0+0.00000000"
// ---------------- Configuration
// Define WUFFS_CONFIG__AVOID_CPU_ARCH to avoid any code tied to a specific CPU
// architecture, such as SSE SIMD for the x86 CPU family.
#if defined(WUFFS_CONFIG__AVOID_CPU_ARCH) // (#if-chain ref AVOID_CPU_ARCH_0)
// No-op.
#else // (#if-chain ref AVOID_CPU_ARCH_0)
// The "defined(__clang__)" isn't redundant. While vanilla clang defines
// __GNUC__, clang-cl (which mimics MSVC's cl.exe) does not.
#if defined(__GNUC__) || defined(__clang__)
#define WUFFS_BASE__MAYBE_ATTRIBUTE_TARGET(arg) __attribute__((target(arg)))
#else
#define WUFFS_BASE__MAYBE_ATTRIBUTE_TARGET(arg)
#endif // defined(__GNUC__) || defined(__clang__)
#if defined(__GNUC__) // (#if-chain ref AVOID_CPU_ARCH_1)
// To simplify Wuffs code, "cpu_arch >= arm_xxx" requires xxx but also
// unaligned little-endian load/stores.
#if defined(__ARM_FEATURE_UNALIGNED) && defined(__BYTE_ORDER__) && \
(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
// Not all gcc versions define __ARM_ACLE, even if they support crc32
// intrinsics. Look for __ARM_FEATURE_CRC32 instead.
#if defined(__ARM_FEATURE_CRC32)
#include <arm_acle.h>
#define WUFFS_BASE__CPU_ARCH__ARM_CRC32
#endif // defined(__ARM_FEATURE_CRC32)
#if defined(__ARM_NEON)
#include <arm_neon.h>
#define WUFFS_BASE__CPU_ARCH__ARM_NEON
#endif // defined(__ARM_NEON)
#endif // defined(__ARM_FEATURE_UNALIGNED) etc
// Similarly, "cpu_arch >= x86_sse42" requires SSE4.2 but also PCLMUL and
// POPCNT. This is checked at runtime via cpuid, not at compile time.
#if defined(__x86_64__)
#include <cpuid.h>
#include <x86intrin.h>
#define WUFFS_BASE__CPU_ARCH__X86_64
#endif // defined(__x86_64__)
#elif defined(_MSC_VER) // (#if-chain ref AVOID_CPU_ARCH_1)
#if defined(_M_X64)
#if defined(__AVX__) || defined(__clang__)
// We need <intrin.h> for the __cpuid function.
#include <intrin.h>
// That's not enough for X64 SIMD, with clang-cl, if we want to use
// "__attribute__((target(arg)))" without e.g. "/arch:AVX".
//
// Some web pages suggest that <immintrin.h> is all you need, as it pulls in
// the earlier SIMD families like SSE4.2, but that doesn't seem to work in
// practice, possibly for the same reason that just <intrin.h> doesn't work.
#include <immintrin.h> // AVX, AVX2, FMA, POPCNT
#include <nmmintrin.h> // SSE4.2
#include <wmmintrin.h> // AES, PCLMUL
#define WUFFS_BASE__CPU_ARCH__X86_64
#else // defined(__AVX__) || defined(__clang__)
// clang-cl (which defines both __clang__ and _MSC_VER) supports
// "__attribute__((target(arg)))".
//
// For MSVC's cl.exe (unlike clang or gcc), SIMD capability is a compile-time
// property of the source file (e.g. a /arch:AVX or -mavx compiler flag), not
// of individual functions (that can be conditionally selected at runtime).
#pragma message("Wuffs with MSVC+X64 needs /arch:AVX for best performance")
#endif // defined(__AVX__) || defined(__clang__)
#endif // defined(_M_X64)
#endif // (#if-chain ref AVOID_CPU_ARCH_1)
#endif // (#if-chain ref AVOID_CPU_ARCH_0)
// --------
// 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
#if defined(WUFFS_CONFIG__STATIC_FUNCTIONS)
#define WUFFS_BASE__MAYBE_STATIC static
#else
#define WUFFS_BASE__MAYBE_STATIC
#endif // defined(WUFFS_CONFIG__STATIC_FUNCTIONS)
// ---------------- CPU Architecture
static inline bool //
wuffs_base__cpu_arch__have_arm_crc32() {
#if defined(WUFFS_BASE__CPU_ARCH__ARM_CRC32)
return true;
#else
return false;
#endif // defined(WUFFS_BASE__CPU_ARCH__ARM_CRC32)
}
static inline bool //
wuffs_base__cpu_arch__have_arm_neon() {
#if defined(WUFFS_BASE__CPU_ARCH__ARM_NEON)
return true;
#else
return false;
#endif // defined(WUFFS_BASE__CPU_ARCH__ARM_NEON)
}
static inline bool //
wuffs_base__cpu_arch__have_x86_sse42() {
#if defined(WUFFS_BASE__CPU_ARCH__X86_64)
// GCC defines these macros but MSVC does not.
// - bit_PCLMUL = (1 << 1)
// - bit_POPCNT = (1 << 23)
// - bit_SSE4_2 = (1 << 20)
const unsigned int sse42_ecx1 = 0x00900002;
// clang defines __GNUC__ and clang-cl defines _MSC_VER (but not __GNUC__).
#if defined(__GNUC__)
unsigned int eax1 = 0;
unsigned int ebx1 = 0;
unsigned int ecx1 = 0;
unsigned int edx1 = 0;
if (__get_cpuid(1, &eax1, &ebx1, &ecx1, &edx1)) {
return (ecx1 & sse42_ecx1) == sse42_ecx1;
}
#elif defined(_MSC_VER) // defined(__GNUC__)
int x[4];
__cpuid(x, 1);
return (((unsigned int)(x[2])) & sse42_ecx1) == sse42_ecx1;
#else
#error "WUFFS_BASE__CPU_ARCH__ETC combined with an unsupported compiler"
#endif // defined(__GNUC__); defined(_MSC_VER)
#endif // defined(WUFFS_BASE__CPU_ARCH__X86_64)
return false;
}
// ---------------- Fundamentals
// 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.
#if defined(__WORDSIZE)
#if (__WORDSIZE != 32) && (__WORDSIZE != 64)
#error "Wuffs requires a word size of either 32 or 64 bits"
#endif
#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/main/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/main/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.
// Bitmap.
#define WUFFS_BASE__FOURCC__BMP 0x424D5020
// Brotli.
#define WUFFS_BASE__FOURCC__BRTL 0x4252544C
// Bzip2.
#define WUFFS_BASE__FOURCC__BZ2 0x425A3220
// Concise Binary Object Representation.
#define WUFFS_BASE__FOURCC__CBOR 0x43424F52
// Cascading Style Sheets.
#define WUFFS_BASE__FOURCC__CSS 0x43535320
// Encapsulated PostScript.
#define WUFFS_BASE__FOURCC__EPS 0x45505320
// Free Lossless Audio Codec.
#define WUFFS_BASE__FOURCC__FLAC 0x464C4143
// Graphics Interchange Format.
#define WUFFS_BASE__FOURCC__GIF 0x47494620
// GNU Zip.
#define WUFFS_BASE__FOURCC__GZ 0x475A2020
// High Efficiency Image File.
#define WUFFS_BASE__FOURCC__HEIF 0x48454946
// Hypertext Markup Language.
#define WUFFS_BASE__FOURCC__HTML 0x48544D4C
// International Color Consortium Profile.
#define WUFFS_BASE__FOURCC__ICCP 0x49434350
// Icon.
#define WUFFS_BASE__FOURCC__ICO 0x49434F20
// Icon Vector Graphics.
#define WUFFS_BASE__FOURCC__ICVG 0x49435647
// Initialization.
#define WUFFS_BASE__FOURCC__INI 0x494E4920
// Joint Photographic Experts Group.
#define WUFFS_BASE__FOURCC__JPEG 0x4A504547
// JavaScript.
#define WUFFS_BASE__FOURCC__JS 0x4A532020
// JavaScript Object Notation.
#define WUFFS_BASE__FOURCC__JSON 0x4A534F4E
// JSON With Commas and Comments.
#define WUFFS_BASE__FOURCC__JWCC 0x4A574343
// Lempelโ€“Ziv 4.
#define WUFFS_BASE__FOURCC__LZ4 0x4C5A3420
// Markdown.
#define WUFFS_BASE__FOURCC__MD 0x4D442020
// MPEG-1 Audio Layer III.
#define WUFFS_BASE__FOURCC__MP3 0x4D503320
// Naive Image.
#define WUFFS_BASE__FOURCC__NIE 0x4E494520
// Open Type Format.
#define WUFFS_BASE__FOURCC__OTF 0x4F544620
// Portable Document Format.
#define WUFFS_BASE__FOURCC__PDF 0x50444620
// Portable Network Graphics.
#define WUFFS_BASE__FOURCC__PNG 0x504E4720
// Portable Anymap.
#define WUFFS_BASE__FOURCC__PNM 0x504E4D20
// PostScript.
#define WUFFS_BASE__FOURCC__PS 0x50532020
// Random Access Compression.
#define WUFFS_BASE__FOURCC__RAC 0x52414320
// Raw.
#define WUFFS_BASE__FOURCC__RAW 0x52415720
// Resource Interchange File Format.
#define WUFFS_BASE__FOURCC__RIFF 0x52494646
// Riegeli Records.
#define WUFFS_BASE__FOURCC__RIGL 0x5249474C
// Snappy.
#define WUFFS_BASE__FOURCC__SNPY 0x534E5059
// Scalable Vector Graphics.
#define WUFFS_BASE__FOURCC__SVG 0x53564720
// Tape Archive.
#define WUFFS_BASE__FOURCC__TAR 0x54415220
// Tagged Image File Format.
#define WUFFS_BASE__FOURCC__TIFF 0x54494646
// Tom's Obvious Minimal Language.
#define WUFFS_BASE__FOURCC__TOML 0x544F4D4C
// Waveform.
#define WUFFS_BASE__FOURCC__WAVE 0x57415645
// Wireless Bitmap.
#define WUFFS_BASE__FOURCC__WBMP 0x57424D50
// Web Open Font Format.
#define WUFFS_BASE__FOURCC__WOFF 0x574F4646
// Web Picture (VP8).
#define WUFFS_BASE__FOURCC__WP8 0x57503820
// Web Picture (VP8 Lossless).
#define WUFFS_BASE__FOURCC__WP8L 0x5750384C
// Extensible Markup Language.
#define WUFFS_BASE__FOURCC__XML 0x584D4C20
// Extensible Metadata Platform.
#define WUFFS_BASE__FOURCC__XMP 0x584D5020
// Xz.
#define WUFFS_BASE__FOURCC__XZ 0x585A2020
// Zip.
#define WUFFS_BASE__FOURCC__ZIP 0x5A495020
// Zlib.
#define WUFFS_BASE__FOURCC__ZLIB 0x5A4C4942
// Zstandard.
#define WUFFS_BASE__FOURCC__ZSTD 0x5A535444
// --------
// Quirks.
#define WUFFS_BASE__QUIRK_IGNORE_CHECKSUM 1
// --------
// 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
// TODO: consider using the _mul128 intrinsic if defined(_MSC_VER).
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
// TODO: consider using the _BitScanReverse intrinsic if defined(_MSC_VER).
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__peek_u8be__no_bounds_check \
wuffs_base__peek_u8__no_bounds_check
#define wuffs_base__peek_u8le__no_bounds_check \
wuffs_base__peek_u8__no_bounds_check
static inline uint8_t //
wuffs_base__peek_u8__no_bounds_check(const uint8_t* p) {
return p[0];
}
static inline uint16_t //
wuffs_base__peek_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__peek_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__peek_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__peek_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__peek_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__peek_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__peek_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__peek_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__peek_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__peek_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__peek_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__peek_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__peek_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__peek_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__poke_u8be__no_bounds_check \
wuffs_base__poke_u8__no_bounds_check
#define wuffs_base__poke_u8le__no_bounds_check \
wuffs_base__poke_u8__no_bounds_check
static inline void //
wuffs_base__poke_u8__no_bounds_check(uint8_t* p, uint8_t x) {
p[0] = x;
}
static inline void //
wuffs_base__poke_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__poke_u16le__no_bounds_check(uint8_t* p, uint16_t x) {
#if defined(__GNUC__) && !defined(__clang__) && defined(__x86_64__)
// This seems to perform better on gcc 10 (but not clang 9). Clang also
// defines "__GNUC__".
memcpy(p, &x, 2);
#else
p[0] = (uint8_t)(x >> 0);
p[1] = (uint8_t)(x >> 8);
#endif
}
static inline void //
wuffs_base__poke_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__poke_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__poke_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__poke_u32le__no_bounds_check(uint8_t* p, uint32_t x) {
#if defined(__GNUC__) && !defined(__clang__) && defined(__x86_64__)
// This seems to perform better on gcc 10 (but not clang 9). Clang also
// defines "__GNUC__".
memcpy(p, &x, 4);
#else
p[0] = (uint8_t)(x >> 0);
p[1] = (uint8_t)(x >> 8);
p[2] = (uint8_t)(x >> 16);
p[3] = (uint8_t)(x >> 24);
#endif
}
static inline void //
wuffs_base__poke_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__poke_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__poke_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__poke_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__poke_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__poke_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__poke_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__poke_u64le__no_bounds_check(uint8_t* p, uint64_t x) {
#if defined(__GNUC__) && !defined(__clang__) && defined(__x86_64__)
// This seems to perform better on gcc 10 (but not clang 9). Clang also
// defines "__GNUC__".
memcpy(p, &x, 8);
#else
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);
#endif
}
// --------
// Load and Store functions are deprecated. Use Peek and Poke instead.
#define wuffs_base__load_u8__no_bounds_check \
wuffs_base__peek_u8__no_bounds_check
#define wuffs_base__load_u16be__no_bounds_check \
wuffs_base__peek_u16be__no_bounds_check
#define wuffs_base__load_u16le__no_bounds_check \
wuffs_base__peek_u16le__no_bounds_check
#define wuffs_base__load_u24be__no_bounds_check \
wuffs_base__peek_u24be__no_bounds_check
#define wuffs_base__load_u24le__no_bounds_check \
wuffs_base__peek_u24le__no_bounds_check
#define wuffs_base__load_u32be__no_bounds_check \
wuffs_base__peek_u32be__no_bounds_check
#define wuffs_base__load_u32le__no_bounds_check \
wuffs_base__peek_u32le__no_bounds_check
#define wuffs_base__load_u40be__no_bounds_check \
wuffs_base__peek_u40be__no_bounds_check
#define wuffs_base__load_u40le__no_bounds_check \
wuffs_base__peek_u40le__no_bounds_check
#define wuffs_base__load_u48be__no_bounds_check \
wuffs_base__peek_u48be__no_bounds_check
#define wuffs_base__load_u48le__no_bounds_check \
wuffs_base__peek_u48le__no_bounds_check
#define wuffs_base__load_u56be__no_bounds_check \
wuffs_base__peek_u56be__no_bounds_check
#define wuffs_base__load_u56le__no_bounds_check \
wuffs_base__peek_u56le__no_bounds_check
#define wuffs_base__load_u64be__no_bounds_check \
wuffs_base__peek_u64be__no_bounds_check
#define wuffs_base__load_u64le__no_bounds_check \
wuffs_base__peek_u64le__no_bounds_check
#define wuffs_base__store_u8__no_bounds_check \
wuffs_base__poke_u8__no_bounds_check
#define wuffs_base__store_u16be__no_bounds_check \
wuffs_base__poke_u16be__no_bounds_check
#define wuffs_base__store_u16le__no_bounds_check \
wuffs_base__poke_u16le__no_bounds_check
#define wuffs_base__store_u24be__no_bounds_check \
wuffs_base__poke_u24be__no_bounds_check
#define wuffs_base__store_u24le__no_bounds_check \
wuffs_base__poke_u24le__no_bounds_check
#define wuffs_base__store_u32be__no_bounds_check \
wuffs_base__poke_u32be__no_bounds_check
#define wuffs_base__store_u32le__no_bounds_check \
wuffs_base__poke_u32le__no_bounds_check
#define wuffs_base__store_u40be__no_bounds_check \
wuffs_base__poke_u40be__no_bounds_check
#define wuffs_base__store_u40le__no_bounds_check \
wuffs_base__poke_u40le__no_bounds_check
#define wuffs_base__store_u48be__no_bounds_check \
wuffs_base__poke_u48be__no_bounds_check
#define wuffs_base__store_u48le__no_bounds_check \
wuffs_base__poke_u48le__no_bounds_check
#define wuffs_base__store_u56be__no_bounds_check \
wuffs_base__poke_u56be__no_bounds_check
#define wuffs_base__store_u56le__no_bounds_check \
wuffs_base__poke_u56le__no_bounds_check
#define wuffs_base__store_u64be__no_bounds_check \
wuffs_base__poke_u64be__no_bounds_check
#define wuffs_base__store_u64le__no_bounds_check \
wuffs_base__poke_u64le__no_bounds_check
// ---------------- 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;
}
static inline bool //
wuffs_base__slice_u8__overlaps(wuffs_base__slice_u8 s, wuffs_base__slice_u8 t) {
return ((s.ptr <= t.ptr) && (t.ptr < (s.ptr + s.len))) ||
((t.ptr <= s.ptr) && (s.ptr < (t.ptr + t.len)));
}
// 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, ((size_t)(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, ((size_t)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, ((size_t)(j - i)));
}
return wuffs_base__make_slice_u8(NULL, 0);
}
// ---------------- Magic Numbers
// wuffs_base__magic_number_guess_fourcc guesses the file format of some data,
// given its opening bytes. It returns a positive FourCC value on success.
//
// It returns zero if nothing matches its hard-coded list of 'magic numbers'.
//
// It returns a negative value if a longer prefix is required for a conclusive
// result. For example, seeing a single 'B' byte is not enough to discriminate
// the BMP and BPG image file formats.
//
// It does not do a full validity check. Like any guess made from a short
// prefix of the data, it may return false positives. Data that starts with 99
// bytes of valid JPEG followed by corruption or truncation is an invalid JPEG
// image overall, but this function will still return WUFFS_BASE__FOURCC__JPEG.
//
// Another source of false positives is that some 'magic numbers' are valid
// ASCII data. A file starting with "GIF87a and GIF89a are the two versions of
// GIF" will match GIF's 'magic number' even if it's plain text, not an image.
//
// For modular builds that divide the base module into sub-modules, using this
// function requires the WUFFS_CONFIG__MODULE__BASE__MAGIC sub-module, not just
// WUFFS_CONFIG__MODULE__BASE__CORE.
WUFFS_BASE__MAYBE_STATIC int32_t //
wuffs_base__magic_number_guess_fourcc(wuffs_base__slice_u8 prefix);
// ---------------- Ranges and Rects
// See https://github.com/google/wuffs/blob/main/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/main/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);
}