Skip to content

Commit f219551

Browse files
authored
compat.wuffs 0.3.6: Wuffs, memory-safe decoders for untrusted formats (#855)
* compat.wuffs 0.3.6: Wuffs, memory-safe decoders for untrusted formats (the release file compiled as C++: the C API and wuffs_aux) * compat.wuffs: the CN mirror (mcpp-res/wuffs, the same bytes)
1 parent 67b1022 commit f219551

5 files changed

Lines changed: 253 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
### Added
1111

12+
- **compat.wuffs 0.3.6:Google 的 Wuffs,面向不可信文件格式的内存安全解码器(Apache-2.0)。** 上游只发布单文件 `release/c/wuffs-v0.3.c`:本包取 tag 归档,以一个生成的翻译单元在 `WUFFS_IMPLEMENTATION` 下按 C++ 编译,C API(`extern "C"`)与只在 C++ 下定义的 `wuffs_aux`(DecodeImage、DecodeJson、DecodeCbor)都在同一个库里;使用者按上游写法 `#include <wuffs-v0.3.c>`。成员 tests/examples/wuffs:版本串、gzip 经 C API、CRC-32 与 Adler-32 的标准校验值、PNG 经 DecodeImage 逐像素、截断 PNG 被拒绝、JSON 遍历。0.4 线上游仍为 alpha。
1213
- **openkal-platform 0.31.2:版本组的补丁,一个成员。** openkal-image 0.7.2(载体 openkal-run、工具 okld/okrun/okprof 与构建依赖
1314
openkal-tools):程序的副本按副本自己结束它的方式结束,映像以 fork 加 exec 启动的程序不再停在副本的 `kal_exit` 中
1415
(mcpplibs/openkal-image#44、#45)。其余成员不变;平台的引擎下界仍为 mcpp 2026.11.17.1。

‎mcpp.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ members = [
246246
"tests/examples/tl-optional",
247247
"tests/examples/tray",
248248
"tests/examples/xsimd",
249+
"tests/examples/wuffs",
249250
"tests/examples/xxhash",
250251
"tests/examples/yaml-cpp",
251252
"tests/examples/yaml-cpp-v080",

‎pkgs/c/compat.wuffs.lua‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
-- compat.wuffs — Wuffs, Google's memory-safe decoders for untrusted file
2+
-- formats (Apache-2.0): images (PNG, GIF, BMP, TGA, WBMP, NIE), compression
3+
-- (DEFLATE, gzip, zlib, LZW), checksums (Adler-32, CRC-32) and JSON / CBOR.
4+
--
5+
-- Wuffs is written in its own language and transpiled to C; the release is
6+
-- ONE file per version line, release/c/wuffs-v0.3.c, used like a single-file
7+
-- header library: included plainly it is the interface, included after
8+
-- `#define WUFFS_IMPLEMENTATION` it is also the implementation. The version
9+
-- is the tag's (0.3.6), whose release/c/wuffs-v0.3.c carries
10+
-- WUFFS_VERSION_STRING "0.3.6"; the 0.4 line is still alpha upstream.
11+
--
12+
-- ─────────────────────────────────────────────────────────────────────────
13+
-- THE IMPLEMENTATION IS COMPILED AS C++
14+
--
15+
-- The C API sits in `extern "C"` blocks, so a C++ build gives the very same
16+
-- symbols as a C one, and it ALSO compiles wuffs_aux, the C++ convenience
17+
-- API (wuffs_aux::DecodeImage, DecodeJson, DecodeCbor …), which the file
18+
-- defines only `#if defined(__cplusplus)`. A C build would drop it. One
19+
-- generated translation unit, the shape compat.dr_libs uses; every module
20+
-- of the release is built (no WUFFS_CONFIG__MODULES), as upstream's
21+
-- library builds do.
22+
--
23+
-- Consumers include the interface as upstream documents it:
24+
--
25+
-- #include <wuffs-v0.3.c>
26+
--
27+
-- without WUFFS_IMPLEMENTATION (this package owns the one definition).
28+
--
29+
-- The tag archive is the source: upstream publishes no release assets. It
30+
-- also carries the test data (test/data) and the examples a consumer can
31+
-- check its decoding against.
32+
package = {
33+
spec = "1",
34+
namespace = "compat",
35+
name = "wuffs",
36+
description = "Wuffs — memory-safe decoders for untrusted formats (PNG, GIF, BMP, DEFLATE, gzip, JSON …), C API plus wuffs_aux C++",
37+
licenses = {"Apache-2.0"},
38+
repo = "https://github.com/google/wuffs",
39+
type = "package",
40+
41+
extensions = {
42+
autoupdate = {
43+
tag = "v${version}",
44+
url = "https://github.com/google/wuffs/archive/refs/tags/${tag}.tar.gz",
45+
},
46+
},
47+
48+
xpm = {
49+
linux = {
50+
["0.3.6"] = {
51+
url = {
52+
GLOBAL = "https://github.com/google/wuffs/archive/refs/tags/v0.3.6.tar.gz",
53+
CN = "https://gitcode.com/mcpp-res/wuffs/releases/download/0.3.6/wuffs-0.3.6.tar.gz",
54+
},
55+
sha256 = "e669099e0f67123efa1d24f88f4ec26ef9875bc7d8957e0d4d9df9803ff5c039",
56+
},
57+
},
58+
macosx = {
59+
["0.3.6"] = {
60+
url = {
61+
GLOBAL = "https://github.com/google/wuffs/archive/refs/tags/v0.3.6.tar.gz",
62+
CN = "https://gitcode.com/mcpp-res/wuffs/releases/download/0.3.6/wuffs-0.3.6.tar.gz",
63+
},
64+
sha256 = "e669099e0f67123efa1d24f88f4ec26ef9875bc7d8957e0d4d9df9803ff5c039",
65+
},
66+
},
67+
windows = {
68+
["0.3.6"] = {
69+
url = {
70+
GLOBAL = "https://github.com/google/wuffs/archive/refs/tags/v0.3.6.tar.gz",
71+
CN = "https://gitcode.com/mcpp-res/wuffs/releases/download/0.3.6/wuffs-0.3.6.tar.gz",
72+
},
73+
sha256 = "e669099e0f67123efa1d24f88f4ec26ef9875bc7d8957e0d4d9df9803ff5c039",
74+
},
75+
},
76+
},
77+
78+
mcpp = {
79+
language = "c++23",
80+
import_std = false,
81+
82+
-- <wuffs-v0.3.c>, the release file, as consumers include it.
83+
include_dirs = { "*/release/c" },
84+
85+
generated_files = {
86+
["mcpp_generated/wuffs_impl.cc"] = [==[
87+
// The one definition of Wuffs (compat.wuffs): the C API and, as C++, wuffs_aux.
88+
#define WUFFS_IMPLEMENTATION
89+
#include <wuffs-v0.3.c>
90+
]==],
91+
},
92+
sources = { "mcpp_generated/wuffs_impl.cc" },
93+
94+
-- Linked into shared objects by its consumers: position independent.
95+
linux = { cxxflags = { "-fPIC" } },
96+
macosx = { cxxflags = { "-fPIC" } },
97+
98+
targets = { wuffs = { kind = "lib" } },
99+
deps = {},
100+
},
101+
}

‎tests/examples/wuffs/mcpp.toml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# compat.wuffs — Wuffs' decoders, its C API and the wuffs_aux C++ API, on data
2+
# whose decoding is known, on every platform the descriptor declares.
3+
[package]
4+
name = "wuffs-tests"
5+
version = "0.1.0"
6+
standard = "c++23"
7+
8+
[dependencies]
9+
compat.wuffs = "0.3.6"
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// compat.wuffs — Wuffs' decoders on data whose decoding is known.
2+
//
3+
// WHAT THIS ASSERTS AND WHY
4+
//
5+
// The package is one translation unit, wuffs-v0.3.c under WUFFS_IMPLEMENTATION
6+
// compiled as C++. That claim has two halves, and each check below needs one:
7+
//
8+
// the release WUFFS_VERSION_STRING is 0.3.6's, and the header the
9+
// consumer includes is the file the library was built from
10+
// the C API gzip (DEFLATE under its framing) decoded by
11+
// wuffs_gzip__decoder, CRC-32 and Adler-32 by their hashers,
12+
// each with the standard check value
13+
// wuffs_aux (C++) a PNG decoded by DecodeImage to BGRA pixels, JSON walked
14+
// by DecodeJson — these exist only if the implementation
15+
// was compiled as C++, which a C build would silently drop
16+
//
17+
// The error path is checked too: a truncated PNG is refused with Wuffs' own
18+
// status message, not decoded into garbage.
19+
20+
#include <wuffs-v0.3.c>
21+
22+
#include <cstdint>
23+
#include <cstdio>
24+
#include <cstring>
25+
#include <string>
26+
27+
namespace {
28+
29+
int failures = 0;
30+
31+
void check(bool ok, const char* what) {
32+
std::printf("%s %s\n", ok ? "ok " : "FAIL", what);
33+
if (!ok) ++failures;
34+
}
35+
36+
constexpr std::uint8_t png_2x2[] = {
37+
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
38+
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x08, 0x02, 0x00, 0x00, 0x00, 0xfd, 0xd4, 0x9a,
39+
0x73, 0x00, 0x00, 0x00, 0x12, 0x49, 0x44, 0x41, 0x54, 0x78, 0xda, 0x63, 0xf8, 0xcf, 0xc0, 0xc0,
40+
0x00, 0xc2, 0x0c, 0xff, 0x81, 0x00, 0x00, 0x1f, 0xee, 0x05, 0xfb, 0xf1, 0xab, 0xba, 0x77, 0x00,
41+
0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
42+
};
43+
constexpr std::uint8_t gzip_text[] = {
44+
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x0b, 0x2f, 0x4d, 0x4b, 0x2b, 0x56,
45+
0x48, 0x49, 0x4d, 0xce, 0x4f, 0x49, 0x2d, 0x56, 0x28, 0xcd, 0x2b, 0x29, 0x2a, 0x2d, 0x2e, 0x49,
46+
0x4d, 0x51, 0x48, 0x49, 0x2c, 0x49, 0xd4, 0xe3, 0x0a, 0xa7, 0x99, 0x2c, 0x00, 0x67, 0x4e, 0x26,
47+
0x2d, 0x78, 0x00, 0x00, 0x00
48+
};
49+
constexpr char gzip_plain[] =
50+
"Wuffs decodes untrusted data.\n" "Wuffs decodes untrusted data.\n"
51+
"Wuffs decodes untrusted data.\n" "Wuffs decodes untrusted data.\n";
52+
53+
std::string gunzip(const std::uint8_t* src_ptr, std::size_t src_len) {
54+
auto dec = wuffs_gzip__decoder::alloc();
55+
if (!dec) return "alloc failed";
56+
std::uint8_t out[256];
57+
std::uint8_t work[1]; // gzip needs no work buffer (workbuf_len is 0)
58+
wuffs_base__io_buffer src = wuffs_base__ptr_u8__reader(const_cast<std::uint8_t*>(src_ptr), src_len, true);
59+
wuffs_base__io_buffer dst = wuffs_base__ptr_u8__writer(out, sizeof out);
60+
wuffs_base__status status = dec->transform_io(&dst, &src, wuffs_base__make_slice_u8(work, 0));
61+
if (!status.is_ok()) return std::string("error: ") + status.message();
62+
return std::string(reinterpret_cast<const char*>(out), dst.meta.wi);
63+
}
64+
65+
// Collects a JSON document's scalars in order, with their nesting.
66+
struct Walk : wuffs_aux::DecodeJsonCallbacks {
67+
std::string seen;
68+
std::string AppendNull() override { seen += "null "; return ""; }
69+
std::string AppendBool(bool v) override { seen += v ? "true " : "false "; return ""; }
70+
std::string AppendF64(double v) override { seen += std::to_string(v) + " "; return ""; }
71+
std::string AppendI64(std::int64_t v) override { seen += std::to_string(v) + " "; return ""; }
72+
std::string AppendTextString(std::string&& v) override { seen += '"' + v + "\" "; return ""; }
73+
std::string Push(std::uint32_t) override { seen += "[ "; return ""; }
74+
std::string Pop(std::uint32_t) override { seen += "] "; return ""; }
75+
};
76+
77+
} // namespace
78+
79+
int main() {
80+
// The release.
81+
check(std::strcmp(WUFFS_VERSION_STRING, "0.3.6+3407.20261020") == 0, "WUFFS_VERSION_STRING is 0.3.6's");
82+
check(WUFFS_VERSION == 0x000030006, "WUFFS_VERSION");
83+
84+
// The C API: gzip, CRC-32, Adler-32.
85+
check(gunzip(gzip_text, sizeof gzip_text) == gzip_plain, "wuffs_gzip__decoder inflates a gzip member");
86+
{
87+
auto crc = wuffs_crc32__ieee_hasher::alloc();
88+
const char* nine = "123456789";
89+
std::uint32_t v = crc->update_u32(wuffs_base__make_slice_u8(
90+
reinterpret_cast<std::uint8_t*>(const_cast<char*>(nine)), 9));
91+
check(v == 0xCBF43926u, "CRC-32/IEEE of \"123456789\" is CBF43926");
92+
}
93+
{
94+
auto adler = wuffs_adler32__hasher::alloc();
95+
const char* text = "Wikipedia";
96+
std::uint32_t v = adler->update_u32(wuffs_base__make_slice_u8(
97+
reinterpret_cast<std::uint8_t*>(const_cast<char*>(text)), 9));
98+
check(v == 0x11E60398u, "Adler-32 of \"Wikipedia\" is 11E60398");
99+
}
100+
101+
// wuffs_aux: an image.
102+
{
103+
wuffs_aux::DecodeImageCallbacks callbacks;
104+
wuffs_aux::sync_io::MemoryInput input(png_2x2, sizeof png_2x2);
105+
wuffs_aux::DecodeImageResult res = wuffs_aux::DecodeImage(callbacks, input);
106+
check(res.error_message.empty(), "DecodeImage decodes the PNG");
107+
if (res.error_message.empty()) {
108+
wuffs_base__table_u8 plane = res.pixbuf.plane(0);
109+
const std::uint8_t* row0 = plane.ptr;
110+
const std::uint8_t* row1 = plane.ptr + plane.stride;
111+
check(res.pixbuf.pixcfg.width() == 2 && res.pixbuf.pixcfg.height() == 2, "2x2");
112+
// BGRA (premultiplied; every pixel is opaque): red, green / blue, white.
113+
check(row0[0] == 0 && row0[1] == 0 && row0[2] == 255 && row0[3] == 255, "pixel (0,0) is red");
114+
check(row0[4] == 0 && row0[5] == 255 && row0[6] == 0, "pixel (1,0) is green");
115+
check(row1[0] == 255 && row1[1] == 0 && row1[2] == 0, "pixel (0,1) is blue");
116+
check(row1[4] == 255 && row1[5] == 255 && row1[6] == 255, "pixel (1,1) is white");
117+
}
118+
}
119+
{
120+
wuffs_aux::DecodeImageCallbacks callbacks;
121+
wuffs_aux::sync_io::MemoryInput input(png_2x2, 40); // cut inside IDAT
122+
wuffs_aux::DecodeImageResult res = wuffs_aux::DecodeImage(callbacks, input);
123+
std::printf(" truncated PNG: %s\n", res.error_message.c_str());
124+
check(!res.error_message.empty(), "a truncated PNG is refused");
125+
}
126+
127+
// wuffs_aux: JSON.
128+
{
129+
Walk walk;
130+
const char* doc = R"({"format":"png","sizes":[16,32],"alpha":true,"note":null})";
131+
wuffs_aux::sync_io::MemoryInput input(doc, std::strlen(doc));
132+
wuffs_aux::DecodeJsonResult res = wuffs_aux::DecodeJson(walk, input);
133+
std::printf(" json: %s\n", walk.seen.c_str());
134+
check(res.error_message.empty() &&
135+
walk.seen == "[ \"format\" \"png\" \"sizes\" [ 16 32 ] \"alpha\" true \"note\" null ] ",
136+
"DecodeJson walks the document");
137+
}
138+
139+
std::printf("%s\n", failures ? "FAILED" : "all passed");
140+
return failures ? 1 : 0;
141+
}

0 commit comments

Comments
 (0)