mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 09:01:35 +01:00
Refactor cgen's base/base-{private,public}.h
This commit is contained in:
1 parent
3f3772e74b
commit
86b8408223
15 files changed
+1150
-1013
No files matched your search
+25
-3
@@ -1,6 +1,6 @@
|
||||
// After editing this file, run "go generate" in the parent directory.
|
||||
|
||||
// Copyright 2018 The Wuffs Authors.
|
||||
// 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.
|
||||
@@ -24,12 +24,34 @@
|
||||
|
||||
// !! WUFFS MONOLITHIC RELEASE DISCARDS EVERYTHING ABOVE.
|
||||
|
||||
// !! INSERT base-public.h.
|
||||
// !! INSERT base/copyright
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// !! INSERT base/all-public.h.
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
// WUFFS C HEADER ENDS HERE.
|
||||
#ifdef WUFFS_IMPLEMENTATION
|
||||
|
||||
// !! INSERT base-private.h.
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// !! INSERT base/all-private.h.
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE)
|
||||
|
||||
-337
@@ -14,10 +14,6 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)
|
||||
|
||||
static inline void wuffs_base__ignore_check_wuffs_version_status(
|
||||
@@ -428,336 +424,3 @@ wuffs_base__table_u8__row(wuffs_base__table_u8 t, uint32_t y) {
|
||||
}
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
|
||||
// ---------------- Ranges and Rects
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__range_ii_u32__get_min_incl(const wuffs_base__range_ii_u32* r) {
|
||||
return r->min_incl;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__range_ii_u32__get_max_incl(const wuffs_base__range_ii_u32* r) {
|
||||
return r->max_incl;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__range_ie_u32__get_min_incl(const wuffs_base__range_ie_u32* r) {
|
||||
return r->min_incl;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__range_ie_u32__get_max_excl(const wuffs_base__range_ie_u32* r) {
|
||||
return r->max_excl;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__range_ii_u64__get_min_incl(const wuffs_base__range_ii_u64* r) {
|
||||
return r->min_incl;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__range_ii_u64__get_max_incl(const wuffs_base__range_ii_u64* r) {
|
||||
return r->max_incl;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__range_ie_u64__get_min_incl(const wuffs_base__range_ie_u64* r) {
|
||||
return r->min_incl;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__range_ie_u64__get_max_excl(const wuffs_base__range_ie_u64* r) {
|
||||
return r->max_excl;
|
||||
}
|
||||
|
||||
// ---------------- Utility
|
||||
|
||||
static inline wuffs_base__range_ii_u32 //
|
||||
wuffs_base__utility__make_range_ii_u32(const wuffs_base__utility* ignored,
|
||||
uint32_t min_incl,
|
||||
uint32_t max_incl) {
|
||||
return ((wuffs_base__range_ii_u32){
|
||||
.min_incl = min_incl,
|
||||
.max_incl = max_incl,
|
||||
});
|
||||
}
|
||||
|
||||
static inline wuffs_base__range_ie_u32 //
|
||||
wuffs_base__utility__make_range_ie_u32(const wuffs_base__utility* ignored,
|
||||
uint32_t min_incl,
|
||||
uint32_t max_excl) {
|
||||
return ((wuffs_base__range_ie_u32){
|
||||
.min_incl = min_incl,
|
||||
.max_excl = max_excl,
|
||||
});
|
||||
}
|
||||
|
||||
static inline wuffs_base__range_ii_u64 //
|
||||
wuffs_base__utility__make_range_ii_u64(const wuffs_base__utility* ignored,
|
||||
uint64_t min_incl,
|
||||
uint64_t max_incl) {
|
||||
return ((wuffs_base__range_ii_u64){
|
||||
.min_incl = min_incl,
|
||||
.max_incl = max_incl,
|
||||
});
|
||||
}
|
||||
|
||||
static inline wuffs_base__range_ie_u64 //
|
||||
wuffs_base__utility__make_range_ie_u64(const wuffs_base__utility* ignored,
|
||||
uint64_t min_incl,
|
||||
uint64_t max_excl) {
|
||||
return ((wuffs_base__range_ie_u64){
|
||||
.min_incl = min_incl,
|
||||
.max_excl = max_excl,
|
||||
});
|
||||
}
|
||||
|
||||
static inline wuffs_base__rect_ii_u32 //
|
||||
wuffs_base__utility__make_rect_ii_u32(const wuffs_base__utility* ignored,
|
||||
uint32_t min_incl_x,
|
||||
uint32_t min_incl_y,
|
||||
uint32_t max_incl_x,
|
||||
uint32_t max_incl_y) {
|
||||
return ((wuffs_base__rect_ii_u32){
|
||||
.min_incl_x = min_incl_x,
|
||||
.min_incl_y = min_incl_y,
|
||||
.max_incl_x = max_incl_x,
|
||||
.max_incl_y = max_incl_y,
|
||||
});
|
||||
}
|
||||
|
||||
static inline wuffs_base__rect_ie_u32 //
|
||||
wuffs_base__utility__make_rect_ie_u32(const wuffs_base__utility* ignored,
|
||||
uint32_t min_incl_x,
|
||||
uint32_t min_incl_y,
|
||||
uint32_t max_excl_x,
|
||||
uint32_t max_excl_y) {
|
||||
return ((wuffs_base__rect_ie_u32){
|
||||
.min_incl_x = min_incl_x,
|
||||
.min_incl_y = min_incl_y,
|
||||
.max_excl_x = max_excl_x,
|
||||
.max_excl_y = max_excl_y,
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------- I/O
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__io_buffer__is_valid(wuffs_base__io_buffer buf) {
|
||||
return (buf.data.ptr || (buf.data.len == 0)) &&
|
||||
(buf.data.len >= buf.meta.wi) && (buf.meta.wi >= buf.meta.ri);
|
||||
}
|
||||
|
||||
// TODO: wuffs_base__io_reader__is_eof is no longer used by Wuffs per se, but
|
||||
// it might be handy to programs that use Wuffs. Either delete it, or promote
|
||||
// it to the public API.
|
||||
//
|
||||
// If making this function public (i.e. moving it to base-header.h), it also
|
||||
// needs to allow NULL (i.e. implicit, callee-calculated) mark/limit.
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__io_reader__is_eof(wuffs_base__io_reader o) {
|
||||
wuffs_base__io_buffer* buf = o.private_impl.buf;
|
||||
return buf && buf->meta.closed &&
|
||||
(buf->data.ptr + buf->meta.wi == o.private_impl.limit);
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__io_reader__is_valid(wuffs_base__io_reader o) {
|
||||
wuffs_base__io_buffer* buf = o.private_impl.buf;
|
||||
// Note: if making this function public (i.e. moving it to base-header.h), it
|
||||
// also needs to allow NULL (i.e. implicit, callee-calculated) mark/limit.
|
||||
return buf ? ((buf->data.ptr <= o.private_impl.mark) &&
|
||||
(o.private_impl.mark <= o.private_impl.limit) &&
|
||||
(o.private_impl.limit <= buf->data.ptr + buf->data.len))
|
||||
: ((o.private_impl.mark == NULL) &&
|
||||
(o.private_impl.limit == NULL));
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__io_writer__is_valid(wuffs_base__io_writer o) {
|
||||
wuffs_base__io_buffer* buf = o.private_impl.buf;
|
||||
// Note: if making this function public (i.e. moving it to base-header.h), it
|
||||
// also needs to allow NULL (i.e. implicit, callee-calculated) mark/limit.
|
||||
return buf ? ((buf->data.ptr <= o.private_impl.mark) &&
|
||||
(o.private_impl.mark <= o.private_impl.limit) &&
|
||||
(o.private_impl.limit <= buf->data.ptr + buf->data.len))
|
||||
: ((o.private_impl.mark == NULL) &&
|
||||
(o.private_impl.limit == NULL));
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__io_writer__copy_n_from_history(uint8_t** ptr_ptr,
|
||||
uint8_t* start,
|
||||
uint8_t* end,
|
||||
uint32_t length,
|
||||
uint32_t distance) {
|
||||
if (!distance) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t* ptr = *ptr_ptr;
|
||||
if ((size_t)(ptr - start) < (size_t)(distance)) {
|
||||
return 0;
|
||||
}
|
||||
start = ptr - distance;
|
||||
size_t n = end - ptr;
|
||||
if ((size_t)(length) > n) {
|
||||
length = n;
|
||||
} else {
|
||||
n = length;
|
||||
}
|
||||
// TODO: unrolling by 3 seems best for the std/deflate benchmarks, but that
|
||||
// is mostly because 3 is the minimum length for the deflate format. This
|
||||
// function implementation shouldn't overfit to that one format. Perhaps the
|
||||
// copy_n_from_history Wuffs method should also take an unroll hint argument,
|
||||
// and the cgen can look if that argument is the constant expression '3'.
|
||||
//
|
||||
// See also wuffs_base__io_writer__copy_n_from_history_fast below.
|
||||
//
|
||||
// Alternatively, or additionally, have a sloppy_copy_n_from_history method
|
||||
// that copies 8 bytes at a time, possibly writing more than length bytes?
|
||||
for (; n >= 3; n -= 3) {
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
for (; n; n--) {
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
*ptr_ptr = ptr;
|
||||
return length;
|
||||
}
|
||||
|
||||
// wuffs_base__io_writer__copy_n_from_history_fast is like the
|
||||
// wuffs_base__io_writer__copy_n_from_history function above, but has stronger
|
||||
// pre-conditions. The caller needs to prove that:
|
||||
// - distance > 0
|
||||
// - distance <= (*ptr_ptr - start)
|
||||
// - length <= (end - *ptr_ptr)
|
||||
static inline uint32_t //
|
||||
wuffs_base__io_writer__copy_n_from_history_fast(uint8_t** ptr_ptr,
|
||||
uint8_t* start,
|
||||
uint8_t* end,
|
||||
uint32_t length,
|
||||
uint32_t distance) {
|
||||
uint8_t* ptr = *ptr_ptr;
|
||||
start = ptr - distance;
|
||||
uint32_t n = length;
|
||||
for (; n >= 3; n -= 3) {
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
for (; n; n--) {
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
*ptr_ptr = ptr;
|
||||
return length;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__io_writer__copy_n_from_reader(uint8_t** ptr_ioptr_w,
|
||||
uint8_t* iobounds1_w,
|
||||
uint32_t length,
|
||||
uint8_t** ptr_ioptr_r,
|
||||
uint8_t* iobounds1_r) {
|
||||
uint8_t* ioptr_w = *ptr_ioptr_w;
|
||||
size_t n = length;
|
||||
if (n > ((size_t)(iobounds1_w - ioptr_w))) {
|
||||
n = iobounds1_w - ioptr_w;
|
||||
}
|
||||
uint8_t* ioptr_r = *ptr_ioptr_r;
|
||||
if (n > ((size_t)(iobounds1_r - ioptr_r))) {
|
||||
n = iobounds1_r - ioptr_r;
|
||||
}
|
||||
if (n > 0) {
|
||||
memmove(ioptr_w, ioptr_r, n);
|
||||
*ptr_ioptr_w += n;
|
||||
*ptr_ioptr_r += n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__io_writer__copy_from_slice(uint8_t** ptr_ioptr_w,
|
||||
uint8_t* iobounds1_w,
|
||||
wuffs_base__slice_u8 src) {
|
||||
uint8_t* ioptr_w = *ptr_ioptr_w;
|
||||
size_t n = src.len;
|
||||
if (n > ((size_t)(iobounds1_w - ioptr_w))) {
|
||||
n = iobounds1_w - ioptr_w;
|
||||
}
|
||||
if (n > 0) {
|
||||
memmove(ioptr_w, src.ptr, n);
|
||||
*ptr_ioptr_w += n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__io_writer__copy_n_from_slice(uint8_t** ptr_ioptr_w,
|
||||
uint8_t* iobounds1_w,
|
||||
uint32_t length,
|
||||
wuffs_base__slice_u8 src) {
|
||||
uint8_t* ioptr_w = *ptr_ioptr_w;
|
||||
size_t n = src.len;
|
||||
if (n > length) {
|
||||
n = length;
|
||||
}
|
||||
if (n > ((size_t)(iobounds1_w - ioptr_w))) {
|
||||
n = iobounds1_w - ioptr_w;
|
||||
}
|
||||
if (n > 0) {
|
||||
memmove(ioptr_w, src.ptr, n);
|
||||
*ptr_ioptr_w += n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline wuffs_base__empty_struct //
|
||||
wuffs_base__io_reader__set_limit(wuffs_base__io_reader* o,
|
||||
uint8_t* ioptr_r,
|
||||
uint64_t limit) {
|
||||
if (o && (((size_t)(o->private_impl.limit - ioptr_r)) > limit)) {
|
||||
o->private_impl.limit = ioptr_r + limit;
|
||||
}
|
||||
return ((wuffs_base__empty_struct){});
|
||||
}
|
||||
|
||||
static inline wuffs_base__empty_struct //
|
||||
wuffs_base__io_reader__set_mark(wuffs_base__io_reader* o, uint8_t* mark) {
|
||||
o->private_impl.mark = mark;
|
||||
return ((wuffs_base__empty_struct){});
|
||||
}
|
||||
|
||||
static inline wuffs_base__empty_struct //
|
||||
wuffs_base__io_writer__set(wuffs_base__io_writer* o,
|
||||
wuffs_base__io_buffer* b,
|
||||
uint8_t** ioptr1_ptr,
|
||||
uint8_t** ioptr2_ptr,
|
||||
wuffs_base__slice_u8 s) {
|
||||
b->data.ptr = s.ptr;
|
||||
b->data.len = s.len;
|
||||
b->meta.wi = 0;
|
||||
b->meta.ri = 0;
|
||||
b->meta.pos = 0;
|
||||
b->meta.closed = false;
|
||||
|
||||
o->private_impl.buf = b;
|
||||
o->private_impl.mark = s.ptr;
|
||||
o->private_impl.limit = s.ptr + s.len;
|
||||
*ioptr1_ptr = s.ptr;
|
||||
*ioptr2_ptr = s.ptr + s.len;
|
||||
return ((wuffs_base__empty_struct){});
|
||||
}
|
||||
|
||||
static inline wuffs_base__empty_struct //
|
||||
wuffs_base__io_writer__set_mark(wuffs_base__io_writer* o, uint8_t* mark) {
|
||||
o->private_impl.mark = mark;
|
||||
return ((wuffs_base__empty_struct){});
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
@@ -0,0 +1,335 @@
|
||||
// After editing this file, run "go generate" in the parent directory.
|
||||
|
||||
// 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.
|
||||
|
||||
// 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
|
||||
|
||||
// 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 version extension
|
||||
// (such as "", "beta" or "rc.1") is part of the string representation (such as
|
||||
// "1.2.3-beta") but not the uint64_t representation.
|
||||
//
|
||||
// All three of major, minor and patch being zero means that this is a
|
||||
// work-in-progress version, not a release version, and has no backwards or
|
||||
// forwards compatibility guarantees.
|
||||
//
|
||||
// !! Some code generation programs can override WUFFS_VERSION.
|
||||
#define WUFFS_VERSION ((uint64_t)0)
|
||||
#define WUFFS_VERSION_MAJOR ((uint64_t)0)
|
||||
#define WUFFS_VERSION_MINOR ((uint64_t)0)
|
||||
#define WUFFS_VERSION_PATCH ((uint64_t)0)
|
||||
#define WUFFS_VERSION_EXTENSION ""
|
||||
#define WUFFS_VERSION_STRING "0.0.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
|
||||
#ifdef WUFFS_CONFIG__STATIC_FUNCTIONS
|
||||
#define WUFFS_BASE__MAYBE_STATIC static
|
||||
#else
|
||||
#define WUFFS_BASE__MAYBE_STATIC
|
||||
#endif
|
||||
|
||||
// Clang also defines "__GNUC__".
|
||||
#if defined(__GNUC__)
|
||||
#define WUFFS_BASE__WARN_UNUSED_RESULT __attribute__((warn_unused_result))
|
||||
#else
|
||||
#define WUFFS_BASE__WARN_UNUSED_RESULT
|
||||
#endif
|
||||
|
||||
// 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 {
|
||||
// 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;
|
||||
|
||||
// wuffs_base__utility is a placeholder receiver type. It enables what Java
|
||||
// calls static methods, as opposed to regular methods.
|
||||
typedef 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;
|
||||
|
||||
// --------
|
||||
|
||||
// A status is either NULL (meaning OK) or a string message. That message is
|
||||
// human-readable, for programmers, but it is not for end users. It is not
|
||||
// localized, and does not contain additional contextual information such as a
|
||||
// source filename.
|
||||
//
|
||||
// Status strings are statically allocated and should never be free'd. They can
|
||||
// be compared by the == operator and not just by strcmp.
|
||||
//
|
||||
// Statuses come in four categories:
|
||||
// - OK: the request was completed, successfully.
|
||||
// - Warnings: the request was completed, unsuccessfully.
|
||||
// - Suspensions: the request was not completed, but can be re-tried.
|
||||
// - Errors: the request was not completed, permanently.
|
||||
//
|
||||
// When a function returns an incomplete status, a suspension means that that
|
||||
// function should be called again within a new context, such as after flushing
|
||||
// or re-filling an I/O buffer. An error means that an irrecoverable failure
|
||||
// state was reached.
|
||||
typedef const char* wuffs_base__status;
|
||||
|
||||
// !! INSERT wuffs_base__status names.
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_complete(wuffs_base__status z) {
|
||||
return (z == NULL) || ((*z != '$') && (*z != '?'));
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_error(wuffs_base__status z) {
|
||||
return z && (*z == '?');
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_ok(wuffs_base__status z) {
|
||||
return z == NULL;
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_suspension(wuffs_base__status z) {
|
||||
return z && (*z == '$');
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_warning(wuffs_base__status z) {
|
||||
return z && (*z != '$') && (*z != '?');
|
||||
}
|
||||
|
||||
// --------
|
||||
|
||||
// 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
|
||||
|
||||
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 = x + y;
|
||||
res |= -(res < x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint8_t //
|
||||
wuffs_base__u8__sat_sub(uint8_t x, uint8_t y) {
|
||||
uint8_t res = x - y;
|
||||
res &= -(res <= x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint16_t //
|
||||
wuffs_base__u16__sat_add(uint16_t x, uint16_t y) {
|
||||
uint16_t res = x + y;
|
||||
res |= -(res < x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint16_t //
|
||||
wuffs_base__u16__sat_sub(uint16_t x, uint16_t y) {
|
||||
uint16_t res = x - y;
|
||||
res &= -(res <= x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__u32__sat_add(uint32_t x, uint32_t y) {
|
||||
uint32_t res = x + y;
|
||||
res |= -(res < x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__u32__sat_sub(uint32_t x, uint32_t y) {
|
||||
uint32_t res = x - y;
|
||||
res &= -(res <= x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__u64__sat_add(uint64_t x, uint64_t y) {
|
||||
uint64_t res = x + y;
|
||||
res |= -(res < x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
|
||||
uint64_t res = x - y;
|
||||
res &= -(res <= x);
|
||||
return res;
|
||||
}
|
||||
|
||||
// ---------------- 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;
|
||||
|
||||
// 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__slice_u8){
|
||||
.ptr = s.ptr + i,
|
||||
.len = s.len - i,
|
||||
});
|
||||
}
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
|
||||
// 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__slice_u8){
|
||||
.ptr = s.ptr,
|
||||
.len = j,
|
||||
});
|
||||
}
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
|
||||
// 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__slice_u8){
|
||||
.ptr = s.ptr + i,
|
||||
.len = j - i,
|
||||
});
|
||||
}
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// After editing this file, run "go generate" in the parent directory.
|
||||
|
||||
// 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.
|
||||
|
||||
// ---------------- Images
|
||||
@@ -14,10 +14,6 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// ---------------- Images
|
||||
|
||||
// wuffs_base__color_u32_argb_premul is an 8 bit per channel premultiplied
|
||||
@@ -904,7 +900,3 @@ typedef struct {
|
||||
#ifdef __cplusplus
|
||||
|
||||
#endif // __cplusplus
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
@@ -0,0 +1,232 @@
|
||||
// After editing this file, run "go generate" in the parent directory.
|
||||
|
||||
// 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.
|
||||
|
||||
// ---------------- I/O
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__io_buffer__is_valid(wuffs_base__io_buffer buf) {
|
||||
return (buf.data.ptr || (buf.data.len == 0)) &&
|
||||
(buf.data.len >= buf.meta.wi) && (buf.meta.wi >= buf.meta.ri);
|
||||
}
|
||||
|
||||
// TODO: wuffs_base__io_reader__is_eof is no longer used by Wuffs per se, but
|
||||
// it might be handy to programs that use Wuffs. Either delete it, or promote
|
||||
// it to the public API.
|
||||
//
|
||||
// If making this function public (i.e. moving it to base-header.h), it also
|
||||
// needs to allow NULL (i.e. implicit, callee-calculated) mark/limit.
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__io_reader__is_eof(wuffs_base__io_reader o) {
|
||||
wuffs_base__io_buffer* buf = o.private_impl.buf;
|
||||
return buf && buf->meta.closed &&
|
||||
(buf->data.ptr + buf->meta.wi == o.private_impl.limit);
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__io_reader__is_valid(wuffs_base__io_reader o) {
|
||||
wuffs_base__io_buffer* buf = o.private_impl.buf;
|
||||
// Note: if making this function public (i.e. moving it to base-header.h), it
|
||||
// also needs to allow NULL (i.e. implicit, callee-calculated) mark/limit.
|
||||
return buf ? ((buf->data.ptr <= o.private_impl.mark) &&
|
||||
(o.private_impl.mark <= o.private_impl.limit) &&
|
||||
(o.private_impl.limit <= buf->data.ptr + buf->data.len))
|
||||
: ((o.private_impl.mark == NULL) &&
|
||||
(o.private_impl.limit == NULL));
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__io_writer__is_valid(wuffs_base__io_writer o) {
|
||||
wuffs_base__io_buffer* buf = o.private_impl.buf;
|
||||
// Note: if making this function public (i.e. moving it to base-header.h), it
|
||||
// also needs to allow NULL (i.e. implicit, callee-calculated) mark/limit.
|
||||
return buf ? ((buf->data.ptr <= o.private_impl.mark) &&
|
||||
(o.private_impl.mark <= o.private_impl.limit) &&
|
||||
(o.private_impl.limit <= buf->data.ptr + buf->data.len))
|
||||
: ((o.private_impl.mark == NULL) &&
|
||||
(o.private_impl.limit == NULL));
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__io_writer__copy_n_from_history(uint8_t** ptr_ptr,
|
||||
uint8_t* start,
|
||||
uint8_t* end,
|
||||
uint32_t length,
|
||||
uint32_t distance) {
|
||||
if (!distance) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t* ptr = *ptr_ptr;
|
||||
if ((size_t)(ptr - start) < (size_t)(distance)) {
|
||||
return 0;
|
||||
}
|
||||
start = ptr - distance;
|
||||
size_t n = end - ptr;
|
||||
if ((size_t)(length) > n) {
|
||||
length = n;
|
||||
} else {
|
||||
n = length;
|
||||
}
|
||||
// TODO: unrolling by 3 seems best for the std/deflate benchmarks, but that
|
||||
// is mostly because 3 is the minimum length for the deflate format. This
|
||||
// function implementation shouldn't overfit to that one format. Perhaps the
|
||||
// copy_n_from_history Wuffs method should also take an unroll hint argument,
|
||||
// and the cgen can look if that argument is the constant expression '3'.
|
||||
//
|
||||
// See also wuffs_base__io_writer__copy_n_from_history_fast below.
|
||||
//
|
||||
// Alternatively, or additionally, have a sloppy_copy_n_from_history method
|
||||
// that copies 8 bytes at a time, possibly writing more than length bytes?
|
||||
for (; n >= 3; n -= 3) {
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
for (; n; n--) {
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
*ptr_ptr = ptr;
|
||||
return length;
|
||||
}
|
||||
|
||||
// wuffs_base__io_writer__copy_n_from_history_fast is like the
|
||||
// wuffs_base__io_writer__copy_n_from_history function above, but has stronger
|
||||
// pre-conditions. The caller needs to prove that:
|
||||
// - distance > 0
|
||||
// - distance <= (*ptr_ptr - start)
|
||||
// - length <= (end - *ptr_ptr)
|
||||
static inline uint32_t //
|
||||
wuffs_base__io_writer__copy_n_from_history_fast(uint8_t** ptr_ptr,
|
||||
uint8_t* start,
|
||||
uint8_t* end,
|
||||
uint32_t length,
|
||||
uint32_t distance) {
|
||||
uint8_t* ptr = *ptr_ptr;
|
||||
start = ptr - distance;
|
||||
uint32_t n = length;
|
||||
for (; n >= 3; n -= 3) {
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
for (; n; n--) {
|
||||
*ptr++ = *start++;
|
||||
}
|
||||
*ptr_ptr = ptr;
|
||||
return length;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__io_writer__copy_n_from_reader(uint8_t** ptr_ioptr_w,
|
||||
uint8_t* iobounds1_w,
|
||||
uint32_t length,
|
||||
uint8_t** ptr_ioptr_r,
|
||||
uint8_t* iobounds1_r) {
|
||||
uint8_t* ioptr_w = *ptr_ioptr_w;
|
||||
size_t n = length;
|
||||
if (n > ((size_t)(iobounds1_w - ioptr_w))) {
|
||||
n = iobounds1_w - ioptr_w;
|
||||
}
|
||||
uint8_t* ioptr_r = *ptr_ioptr_r;
|
||||
if (n > ((size_t)(iobounds1_r - ioptr_r))) {
|
||||
n = iobounds1_r - ioptr_r;
|
||||
}
|
||||
if (n > 0) {
|
||||
memmove(ioptr_w, ioptr_r, n);
|
||||
*ptr_ioptr_w += n;
|
||||
*ptr_ioptr_r += n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__io_writer__copy_from_slice(uint8_t** ptr_ioptr_w,
|
||||
uint8_t* iobounds1_w,
|
||||
wuffs_base__slice_u8 src) {
|
||||
uint8_t* ioptr_w = *ptr_ioptr_w;
|
||||
size_t n = src.len;
|
||||
if (n > ((size_t)(iobounds1_w - ioptr_w))) {
|
||||
n = iobounds1_w - ioptr_w;
|
||||
}
|
||||
if (n > 0) {
|
||||
memmove(ioptr_w, src.ptr, n);
|
||||
*ptr_ioptr_w += n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__io_writer__copy_n_from_slice(uint8_t** ptr_ioptr_w,
|
||||
uint8_t* iobounds1_w,
|
||||
uint32_t length,
|
||||
wuffs_base__slice_u8 src) {
|
||||
uint8_t* ioptr_w = *ptr_ioptr_w;
|
||||
size_t n = src.len;
|
||||
if (n > length) {
|
||||
n = length;
|
||||
}
|
||||
if (n > ((size_t)(iobounds1_w - ioptr_w))) {
|
||||
n = iobounds1_w - ioptr_w;
|
||||
}
|
||||
if (n > 0) {
|
||||
memmove(ioptr_w, src.ptr, n);
|
||||
*ptr_ioptr_w += n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline wuffs_base__empty_struct //
|
||||
wuffs_base__io_reader__set_limit(wuffs_base__io_reader* o,
|
||||
uint8_t* ioptr_r,
|
||||
uint64_t limit) {
|
||||
if (o && (((size_t)(o->private_impl.limit - ioptr_r)) > limit)) {
|
||||
o->private_impl.limit = ioptr_r + limit;
|
||||
}
|
||||
return ((wuffs_base__empty_struct){});
|
||||
}
|
||||
|
||||
static inline wuffs_base__empty_struct //
|
||||
wuffs_base__io_reader__set_mark(wuffs_base__io_reader* o, uint8_t* mark) {
|
||||
o->private_impl.mark = mark;
|
||||
return ((wuffs_base__empty_struct){});
|
||||
}
|
||||
|
||||
static inline wuffs_base__empty_struct //
|
||||
wuffs_base__io_writer__set(wuffs_base__io_writer* o,
|
||||
wuffs_base__io_buffer* b,
|
||||
uint8_t** ioptr1_ptr,
|
||||
uint8_t** ioptr2_ptr,
|
||||
wuffs_base__slice_u8 s) {
|
||||
b->data.ptr = s.ptr;
|
||||
b->data.len = s.len;
|
||||
b->meta.wi = 0;
|
||||
b->meta.ri = 0;
|
||||
b->meta.pos = 0;
|
||||
b->meta.closed = false;
|
||||
|
||||
o->private_impl.buf = b;
|
||||
o->private_impl.mark = s.ptr;
|
||||
o->private_impl.limit = s.ptr + s.len;
|
||||
*ioptr1_ptr = s.ptr;
|
||||
*ioptr2_ptr = s.ptr + s.len;
|
||||
return ((wuffs_base__empty_struct){});
|
||||
}
|
||||
|
||||
static inline wuffs_base__empty_struct //
|
||||
wuffs_base__io_writer__set_mark(wuffs_base__io_writer* o, uint8_t* mark) {
|
||||
o->private_impl.mark = mark;
|
||||
return ((wuffs_base__empty_struct){});
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
// After editing this file, run "go generate" in the parent directory.
|
||||
|
||||
// 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.
|
||||
|
||||
// ---------------- I/O
|
||||
|
||||
struct wuffs_base__io_buffer__struct;
|
||||
|
||||
typedef struct {
|
||||
// Do not access the private_impl's fields directly. There is no API/ABI
|
||||
// compatibility or safety guarantee if you do so.
|
||||
struct {
|
||||
struct wuffs_base__io_buffer__struct* buf;
|
||||
// The bounds values are typically NULL, when created by the Wuffs public
|
||||
// API. NULL means that the callee substitutes the implicit bounds derived
|
||||
// from buf.
|
||||
uint8_t* mark;
|
||||
uint8_t* limit;
|
||||
} private_impl;
|
||||
} wuffs_base__io_reader;
|
||||
|
||||
typedef struct {
|
||||
// Do not access the private_impl's fields directly. There is no API/ABI
|
||||
// compatibility or safety guarantee if you do so.
|
||||
struct {
|
||||
struct wuffs_base__io_buffer__struct* buf;
|
||||
// The bounds values are typically NULL, when created by the Wuffs public
|
||||
// API. NULL means that the callee substitutes the implicit bounds derived
|
||||
// from buf.
|
||||
uint8_t* mark;
|
||||
uint8_t* limit;
|
||||
} private_impl;
|
||||
} wuffs_base__io_writer;
|
||||
|
||||
// wuffs_base__io_buffer_meta is the metadata for a wuffs_base__io_buffer's
|
||||
// data.
|
||||
typedef 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__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 void compact();
|
||||
inline wuffs_base__io_reader reader();
|
||||
inline wuffs_base__io_writer writer();
|
||||
inline uint64_t reader_io_position();
|
||||
inline uint64_t writer_io_position();
|
||||
#endif // __cplusplus
|
||||
|
||||
} wuffs_base__io_buffer;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
static inline wuffs_base__io_reader //
|
||||
wuffs_base__io_buffer__reader(wuffs_base__io_buffer* buf) {
|
||||
wuffs_base__io_reader ret = ((wuffs_base__io_reader){});
|
||||
ret.private_impl.buf = buf;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline wuffs_base__io_writer //
|
||||
wuffs_base__io_buffer__writer(wuffs_base__io_buffer* buf) {
|
||||
wuffs_base__io_writer ret = ((wuffs_base__io_writer){});
|
||||
ret.private_impl.buf = buf;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__io_buffer__reader_io_position(wuffs_base__io_buffer* buf) {
|
||||
return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri) : 0;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__io_buffer__writer_io_position(wuffs_base__io_buffer* buf) {
|
||||
return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.wi) : 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
inline void //
|
||||
wuffs_base__io_buffer__struct::compact() {
|
||||
wuffs_base__io_buffer__compact(this);
|
||||
}
|
||||
|
||||
inline wuffs_base__io_reader //
|
||||
wuffs_base__io_buffer__struct::reader() {
|
||||
return wuffs_base__io_buffer__reader(this);
|
||||
}
|
||||
|
||||
inline wuffs_base__io_writer //
|
||||
wuffs_base__io_buffer__struct::writer() {
|
||||
return wuffs_base__io_buffer__writer(this);
|
||||
}
|
||||
|
||||
inline uint64_t //
|
||||
wuffs_base__io_buffer__struct::reader_io_position() {
|
||||
return wuffs_base__io_buffer__reader_io_position(this);
|
||||
}
|
||||
|
||||
inline uint64_t //
|
||||
wuffs_base__io_buffer__struct::writer_io_position() {
|
||||
return wuffs_base__io_buffer__writer_io_position(this);
|
||||
}
|
||||
|
||||
#endif // __cplusplus
|
||||
@@ -0,0 +1,17 @@
|
||||
// After editing this file, run "go generate" in the parent directory.
|
||||
|
||||
// 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.
|
||||
|
||||
// ---------------- Memory Allocation
|
||||
@@ -0,0 +1,86 @@
|
||||
// After editing this file, run "go generate" in the parent directory.
|
||||
|
||||
// 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.
|
||||
|
||||
// ---------------- 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__slice_u8){
|
||||
.ptr = (uint8_t*)(p),
|
||||
.len = num_u8,
|
||||
});
|
||||
}
|
||||
}
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
|
||||
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__slice_u16){
|
||||
.ptr = (uint16_t*)(p),
|
||||
.len = num_u16,
|
||||
});
|
||||
}
|
||||
}
|
||||
return ((wuffs_base__slice_u16){});
|
||||
}
|
||||
|
||||
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__slice_u32){
|
||||
.ptr = (uint32_t*)(p),
|
||||
.len = num_u32,
|
||||
});
|
||||
}
|
||||
}
|
||||
return ((wuffs_base__slice_u32){});
|
||||
}
|
||||
|
||||
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__slice_u64){
|
||||
.ptr = (uint64_t*)(p),
|
||||
.len = num_u64,
|
||||
});
|
||||
}
|
||||
}
|
||||
return ((wuffs_base__slice_u64){});
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
// After editing this file, run "go generate" in the parent directory.
|
||||
|
||||
// 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.
|
||||
|
||||
// ---------------- Ranges and Rects
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__range_ii_u32__get_min_incl(const wuffs_base__range_ii_u32* r) {
|
||||
return r->min_incl;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__range_ii_u32__get_max_incl(const wuffs_base__range_ii_u32* r) {
|
||||
return r->max_incl;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__range_ie_u32__get_min_incl(const wuffs_base__range_ie_u32* r) {
|
||||
return r->min_incl;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__range_ie_u32__get_max_excl(const wuffs_base__range_ie_u32* r) {
|
||||
return r->max_excl;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__range_ii_u64__get_min_incl(const wuffs_base__range_ii_u64* r) {
|
||||
return r->min_incl;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__range_ii_u64__get_max_incl(const wuffs_base__range_ii_u64* r) {
|
||||
return r->max_incl;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__range_ie_u64__get_min_incl(const wuffs_base__range_ie_u64* r) {
|
||||
return r->min_incl;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__range_ie_u64__get_max_excl(const wuffs_base__range_ie_u64* r) {
|
||||
return r->max_excl;
|
||||
}
|
||||
|
||||
// ---------------- Utility
|
||||
|
||||
static inline wuffs_base__range_ii_u32 //
|
||||
wuffs_base__utility__make_range_ii_u32(const wuffs_base__utility* ignored,
|
||||
uint32_t min_incl,
|
||||
uint32_t max_incl) {
|
||||
return ((wuffs_base__range_ii_u32){
|
||||
.min_incl = min_incl,
|
||||
.max_incl = max_incl,
|
||||
});
|
||||
}
|
||||
|
||||
static inline wuffs_base__range_ie_u32 //
|
||||
wuffs_base__utility__make_range_ie_u32(const wuffs_base__utility* ignored,
|
||||
uint32_t min_incl,
|
||||
uint32_t max_excl) {
|
||||
return ((wuffs_base__range_ie_u32){
|
||||
.min_incl = min_incl,
|
||||
.max_excl = max_excl,
|
||||
});
|
||||
}
|
||||
|
||||
static inline wuffs_base__range_ii_u64 //
|
||||
wuffs_base__utility__make_range_ii_u64(const wuffs_base__utility* ignored,
|
||||
uint64_t min_incl,
|
||||
uint64_t max_incl) {
|
||||
return ((wuffs_base__range_ii_u64){
|
||||
.min_incl = min_incl,
|
||||
.max_incl = max_incl,
|
||||
});
|
||||
}
|
||||
|
||||
static inline wuffs_base__range_ie_u64 //
|
||||
wuffs_base__utility__make_range_ie_u64(const wuffs_base__utility* ignored,
|
||||
uint64_t min_incl,
|
||||
uint64_t max_excl) {
|
||||
return ((wuffs_base__range_ie_u64){
|
||||
.min_incl = min_incl,
|
||||
.max_excl = max_excl,
|
||||
});
|
||||
}
|
||||
|
||||
static inline wuffs_base__rect_ii_u32 //
|
||||
wuffs_base__utility__make_rect_ii_u32(const wuffs_base__utility* ignored,
|
||||
uint32_t min_incl_x,
|
||||
uint32_t min_incl_y,
|
||||
uint32_t max_incl_x,
|
||||
uint32_t max_incl_y) {
|
||||
return ((wuffs_base__rect_ii_u32){
|
||||
.min_incl_x = min_incl_x,
|
||||
.min_incl_y = min_incl_y,
|
||||
.max_incl_x = max_incl_x,
|
||||
.max_incl_y = max_incl_y,
|
||||
});
|
||||
}
|
||||
|
||||
static inline wuffs_base__rect_ie_u32 //
|
||||
wuffs_base__utility__make_rect_ie_u32(const wuffs_base__utility* ignored,
|
||||
uint32_t min_incl_x,
|
||||
uint32_t min_incl_y,
|
||||
uint32_t max_excl_x,
|
||||
uint32_t max_excl_y) {
|
||||
return ((wuffs_base__rect_ie_u32){
|
||||
.min_incl_x = min_incl_x,
|
||||
.min_incl_y = min_incl_y,
|
||||
.max_excl_x = max_excl_x,
|
||||
.max_excl_y = max_excl_y,
|
||||
});
|
||||
}
|
||||
-529
@@ -14,334 +14,6 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#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
|
||||
|
||||
// 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 version extension
|
||||
// (such as "", "beta" or "rc.1") is part of the string representation (such as
|
||||
// "1.2.3-beta") but not the uint64_t representation.
|
||||
//
|
||||
// All three of major, minor and patch being zero means that this is a
|
||||
// work-in-progress version, not a release version, and has no backwards or
|
||||
// forwards compatibility guarantees.
|
||||
//
|
||||
// !! Some code generation programs can override WUFFS_VERSION.
|
||||
#define WUFFS_VERSION ((uint64_t)0)
|
||||
#define WUFFS_VERSION_MAJOR ((uint64_t)0)
|
||||
#define WUFFS_VERSION_MINOR ((uint64_t)0)
|
||||
#define WUFFS_VERSION_PATCH ((uint64_t)0)
|
||||
#define WUFFS_VERSION_EXTENSION ""
|
||||
#define WUFFS_VERSION_STRING "0.0.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
|
||||
#ifdef WUFFS_CONFIG__STATIC_FUNCTIONS
|
||||
#define WUFFS_BASE__MAYBE_STATIC static
|
||||
#else
|
||||
#define WUFFS_BASE__MAYBE_STATIC
|
||||
#endif
|
||||
|
||||
// Clang also defines "__GNUC__".
|
||||
#if defined(__GNUC__)
|
||||
#define WUFFS_BASE__WARN_UNUSED_RESULT __attribute__((warn_unused_result))
|
||||
#else
|
||||
#define WUFFS_BASE__WARN_UNUSED_RESULT
|
||||
#endif
|
||||
|
||||
// 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 {
|
||||
// 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;
|
||||
|
||||
// wuffs_base__utility is a placeholder receiver type. It enables what Java
|
||||
// calls static methods, as opposed to regular methods.
|
||||
typedef 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;
|
||||
|
||||
// --------
|
||||
|
||||
// A status is either NULL (meaning OK) or a string message. That message is
|
||||
// human-readable, for programmers, but it is not for end users. It is not
|
||||
// localized, and does not contain additional contextual information such as a
|
||||
// source filename.
|
||||
//
|
||||
// Status strings are statically allocated and should never be free'd. They can
|
||||
// be compared by the == operator and not just by strcmp.
|
||||
//
|
||||
// Statuses come in four categories:
|
||||
// - OK: the request was completed, successfully.
|
||||
// - Warnings: the request was completed, unsuccessfully.
|
||||
// - Suspensions: the request was not completed, but can be re-tried.
|
||||
// - Errors: the request was not completed, permanently.
|
||||
//
|
||||
// When a function returns an incomplete status, a suspension means that that
|
||||
// function should be called again within a new context, such as after flushing
|
||||
// or re-filling an I/O buffer. An error means that an irrecoverable failure
|
||||
// state was reached.
|
||||
typedef const char* wuffs_base__status;
|
||||
|
||||
// !! INSERT wuffs_base__status names.
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_complete(wuffs_base__status z) {
|
||||
return (z == NULL) || ((*z != '$') && (*z != '?'));
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_error(wuffs_base__status z) {
|
||||
return z && (*z == '?');
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_ok(wuffs_base__status z) {
|
||||
return z == NULL;
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_suspension(wuffs_base__status z) {
|
||||
return z && (*z == '$');
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_warning(wuffs_base__status z) {
|
||||
return z && (*z != '$') && (*z != '?');
|
||||
}
|
||||
|
||||
// --------
|
||||
|
||||
// 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
|
||||
|
||||
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 = x + y;
|
||||
res |= -(res < x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint8_t //
|
||||
wuffs_base__u8__sat_sub(uint8_t x, uint8_t y) {
|
||||
uint8_t res = x - y;
|
||||
res &= -(res <= x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint16_t //
|
||||
wuffs_base__u16__sat_add(uint16_t x, uint16_t y) {
|
||||
uint16_t res = x + y;
|
||||
res |= -(res < x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint16_t //
|
||||
wuffs_base__u16__sat_sub(uint16_t x, uint16_t y) {
|
||||
uint16_t res = x - y;
|
||||
res &= -(res <= x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__u32__sat_add(uint32_t x, uint32_t y) {
|
||||
uint32_t res = x + y;
|
||||
res |= -(res < x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__u32__sat_sub(uint32_t x, uint32_t y) {
|
||||
uint32_t res = x - y;
|
||||
res &= -(res <= x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__u64__sat_add(uint64_t x, uint64_t y) {
|
||||
uint64_t res = x + y;
|
||||
res |= -(res < x);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__u64__sat_sub(uint64_t x, uint64_t y) {
|
||||
uint64_t res = x - y;
|
||||
res &= -(res <= x);
|
||||
return res;
|
||||
}
|
||||
|
||||
// ---------------- 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;
|
||||
|
||||
// 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__slice_u8){
|
||||
.ptr = s.ptr + i,
|
||||
.len = s.len - i,
|
||||
});
|
||||
}
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
|
||||
// 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__slice_u8){
|
||||
.ptr = s.ptr,
|
||||
.len = j,
|
||||
});
|
||||
}
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
|
||||
// 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__slice_u8){
|
||||
.ptr = s.ptr + i,
|
||||
.len = j - i,
|
||||
});
|
||||
}
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
|
||||
// ---------------- Ranges and Rects
|
||||
|
||||
// Ranges are either inclusive ("range_ii") or exclusive ("range_ie") on the
|
||||
@@ -991,204 +663,3 @@ wuffs_base__rect_ie_u32::height() {
|
||||
}
|
||||
|
||||
#endif // __cplusplus
|
||||
|
||||
// ---------------- I/O
|
||||
|
||||
struct wuffs_base__io_buffer__struct;
|
||||
|
||||
typedef struct {
|
||||
// Do not access the private_impl's fields directly. There is no API/ABI
|
||||
// compatibility or safety guarantee if you do so.
|
||||
struct {
|
||||
struct wuffs_base__io_buffer__struct* buf;
|
||||
// The bounds values are typically NULL, when created by the Wuffs public
|
||||
// API. NULL means that the callee substitutes the implicit bounds derived
|
||||
// from buf.
|
||||
uint8_t* mark;
|
||||
uint8_t* limit;
|
||||
} private_impl;
|
||||
} wuffs_base__io_reader;
|
||||
|
||||
typedef struct {
|
||||
// Do not access the private_impl's fields directly. There is no API/ABI
|
||||
// compatibility or safety guarantee if you do so.
|
||||
struct {
|
||||
struct wuffs_base__io_buffer__struct* buf;
|
||||
// The bounds values are typically NULL, when created by the Wuffs public
|
||||
// API. NULL means that the callee substitutes the implicit bounds derived
|
||||
// from buf.
|
||||
uint8_t* mark;
|
||||
uint8_t* limit;
|
||||
} private_impl;
|
||||
} wuffs_base__io_writer;
|
||||
|
||||
// wuffs_base__io_buffer_meta is the metadata for a wuffs_base__io_buffer's
|
||||
// data.
|
||||
typedef 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__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 void compact();
|
||||
inline wuffs_base__io_reader reader();
|
||||
inline wuffs_base__io_writer writer();
|
||||
inline uint64_t reader_io_position();
|
||||
inline uint64_t writer_io_position();
|
||||
#endif // __cplusplus
|
||||
|
||||
} wuffs_base__io_buffer;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
static inline wuffs_base__io_reader //
|
||||
wuffs_base__io_buffer__reader(wuffs_base__io_buffer* buf) {
|
||||
wuffs_base__io_reader ret = ((wuffs_base__io_reader){});
|
||||
ret.private_impl.buf = buf;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline wuffs_base__io_writer //
|
||||
wuffs_base__io_buffer__writer(wuffs_base__io_buffer* buf) {
|
||||
wuffs_base__io_writer ret = ((wuffs_base__io_writer){});
|
||||
ret.private_impl.buf = buf;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__io_buffer__reader_io_position(wuffs_base__io_buffer* buf) {
|
||||
return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri) : 0;
|
||||
}
|
||||
|
||||
static inline uint64_t //
|
||||
wuffs_base__io_buffer__writer_io_position(wuffs_base__io_buffer* buf) {
|
||||
return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.wi) : 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
inline void //
|
||||
wuffs_base__io_buffer__struct::compact() {
|
||||
wuffs_base__io_buffer__compact(this);
|
||||
}
|
||||
|
||||
inline wuffs_base__io_reader //
|
||||
wuffs_base__io_buffer__struct::reader() {
|
||||
return wuffs_base__io_buffer__reader(this);
|
||||
}
|
||||
|
||||
inline wuffs_base__io_writer //
|
||||
wuffs_base__io_buffer__struct::writer() {
|
||||
return wuffs_base__io_buffer__writer(this);
|
||||
}
|
||||
|
||||
inline uint64_t //
|
||||
wuffs_base__io_buffer__struct::reader_io_position() {
|
||||
return wuffs_base__io_buffer__reader_io_position(this);
|
||||
}
|
||||
|
||||
inline uint64_t //
|
||||
wuffs_base__io_buffer__struct::writer_io_position() {
|
||||
return wuffs_base__io_buffer__writer_io_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__slice_u8){
|
||||
.ptr = (uint8_t*)(p),
|
||||
.len = num_u8,
|
||||
});
|
||||
}
|
||||
}
|
||||
return ((wuffs_base__slice_u8){});
|
||||
}
|
||||
|
||||
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__slice_u16){
|
||||
.ptr = (uint16_t*)(p),
|
||||
.len = num_u16,
|
||||
});
|
||||
}
|
||||
}
|
||||
return ((wuffs_base__slice_u16){});
|
||||
}
|
||||
|
||||
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__slice_u32){
|
||||
.ptr = (uint32_t*)(p),
|
||||
.len = num_u32,
|
||||
});
|
||||
}
|
||||
}
|
||||
return ((wuffs_base__slice_u32){});
|
||||
}
|
||||
|
||||
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__slice_u64){
|
||||
.ptr = (uint64_t*)(p),
|
||||
.len = num_u64,
|
||||
});
|
||||
}
|
||||
}
|
||||
return ((wuffs_base__slice_u64){});
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
Reference in new issue
Block a user