|
| 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