diff --git a/cmd/internal/cgen/cgen.go b/cmd/internal/cgen/cgen.go index d70b5090..66a5472b 100644 --- a/cmd/internal/cgen/cgen.go +++ b/cmd/internal/cgen/cgen.go @@ -29,10 +29,14 @@ func (g *Generator) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byt b.WriteString("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n") b.WriteString("// ---------------- Status Codes\n\n") + b.WriteString("// Status codes are non-positive integers.\n") + b.WriteString("//\n") + b.WriteString("// The least significant bit indicates a fatal status code: an error.\n") b.WriteString("typedef enum {\n") fmt.Fprintf(b, "puffs_%s_status_ok = 0,\n", pkgName) - fmt.Fprintf(b, "puffs_%s_status_short_dst = -1,\n", pkgName) - fmt.Fprintf(b, "puffs_%s_status_short_src = -2,\n", pkgName) + fmt.Fprintf(b, "puffs_%s_error_bad_version = -1,\n", pkgName) + fmt.Fprintf(b, "puffs_%s_status_short_dst = -4,\n", pkgName) + fmt.Fprintf(b, "puffs_%s_status_short_src = -6,\n", pkgName) fmt.Fprintf(b, "} puffs_%s_status;\n\n", pkgName) b.WriteString("// ---------------- Structs\n\n") @@ -92,14 +96,22 @@ func writeStruct(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) error return nil } +func writeCtorSignature(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct, ctor string) { + structName := n.Name().String(m) + fmt.Fprintf(b, "void puffs_%s_%s_%s(puffs_%s_%s *self", pkgName, structName, ctor, pkgName, structName) + if ctor == "constructor" { + fmt.Fprintf(b, ", uint32_t puffs_version") + } + fmt.Fprintf(b, ")") +} + func writeCtorPrototypes(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) error { if !n.Suspendible() { return nil } - structName := n.Name().String(m) for _, ctor := range []string{"constructor", "destructor"} { - fmt.Fprintf(b, "void puffs_%s_%s_%s(puffs_%s_%s *self);\n\n", - pkgName, structName, ctor, pkgName, structName) + writeCtorSignature(b, pkgName, m, n, ctor) + b.WriteString(";\n\n") } return nil } @@ -108,14 +120,20 @@ func writeCtorImpls(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) er if !n.Suspendible() { return nil } - structName := n.Name().String(m) for _, ctor := range []string{"constructor", "destructor"} { - fmt.Fprintf(b, "void puffs_%s_%s_%s(puffs_%s_%s *self) {\n", - pkgName, structName, ctor, pkgName, structName) + writeCtorSignature(b, pkgName, m, n, ctor) + fmt.Fprintf(b, "{\n") + if ctor == "constructor" { + fmt.Fprintf(b, "if (puffs_version != PUFFS_VERSION) {\n") + fmt.Fprintf(b, "self->status = puffs_%s_error_bad_version;\n", pkgName) + fmt.Fprintf(b, "return;\n") + fmt.Fprintf(b, "}\n") + b.WriteString("memset(self, 0, sizeof(*self));\n") // TODO: set any non-zero default values. } + // TODO: call any ctor/dtors on sub-structures. b.WriteString("}\n\n") } diff --git a/cmd/internal/cgen/data.go b/cmd/internal/cgen/data.go index f0ab28f3..e578f08c 100644 --- a/cmd/internal/cgen/data.go +++ b/cmd/internal/cgen/data.go @@ -6,5 +6,5 @@ package cgen const preamble = "" + - "// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include \n#include \n#include \n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n" + + "// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include \n#include \n#include \n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n\n// PUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API / ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in preamble.h.\n#define PUFFS_VERSION (0x00001)\n" + "" diff --git a/cmd/internal/cgen/preamble.h b/cmd/internal/cgen/preamble.h index 55bcc138..ef2994b6 100644 --- a/cmd/internal/cgen/preamble.h +++ b/cmd/internal/cgen/preamble.h @@ -14,3 +14,14 @@ #if __WORDSIZE < 32 #error "Puffs requires a word size of at least 32 bits" #endif + +// PUFFS_VERSION is the major.minor version number as a uint32. The major +// number is the high 16 bits. The minor number is the low 16 bits. +// +// The intention is to bump the version number at least on every API / ABI +// backwards incompatible change. +// +// For now, the API and ABI are simply unstable and can change at any time. +// +// TODO: don't hard code this in preamble.h. +#define PUFFS_VERSION (0x00001) diff --git a/gen/c/gif.c b/gen/c/gif.c index 925d4b90..7211b8d0 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -15,16 +15,31 @@ #error "Puffs requires a word size of at least 32 bits" #endif +// PUFFS_VERSION is the major.minor version number as a uint32. The major +// number is the high 16 bits. The minor number is the low 16 bits. +// +// The intention is to bump the version number at least on every API / ABI +// backwards incompatible change. +// +// For now, the API and ABI are simply unstable and can change at any time. +// +// TODO: don't hard code this in preamble.h. +#define PUFFS_VERSION (0x00001) + #ifdef __cplusplus extern "C" { #endif // ---------------- Status Codes +// Status codes are non-positive integers. +// +// The least significant bit indicates a fatal status code: an error. typedef enum { puffs_gif_status_ok = 0, - puffs_gif_status_short_dst = -1, - puffs_gif_status_short_src = -2, + puffs_gif_error_bad_version = -1, + puffs_gif_status_short_dst = -4, + puffs_gif_status_short_src = -6, } puffs_gif_status; // ---------------- Structs @@ -39,7 +54,8 @@ typedef struct { // ---------------- Constructor and Destructor Prototypes -void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self); +void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self, + uint32_t puffs_version); void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self); @@ -49,7 +65,12 @@ void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self); // ---------------- Constructor and Destructor Implementations -void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self) { +void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self, + uint32_t puffs_version) { + if (puffs_version != PUFFS_VERSION) { + self->status = puffs_gif_error_bad_version; + return; + } memset(self, 0, sizeof(*self)); } diff --git a/gen/h/gif.h b/gen/h/gif.h index c37e8aed..1be6ee4a 100644 --- a/gen/h/gif.h +++ b/gen/h/gif.h @@ -15,16 +15,31 @@ #error "Puffs requires a word size of at least 32 bits" #endif +// PUFFS_VERSION is the major.minor version number as a uint32. The major +// number is the high 16 bits. The minor number is the low 16 bits. +// +// The intention is to bump the version number at least on every API / ABI +// backwards incompatible change. +// +// For now, the API and ABI are simply unstable and can change at any time. +// +// TODO: don't hard code this in preamble.h. +#define PUFFS_VERSION (0x00001) + #ifdef __cplusplus extern "C" { #endif // ---------------- Status Codes +// Status codes are non-positive integers. +// +// The least significant bit indicates a fatal status code: an error. typedef enum { puffs_gif_status_ok = 0, - puffs_gif_status_short_dst = -1, - puffs_gif_status_short_src = -2, + puffs_gif_error_bad_version = -1, + puffs_gif_status_short_dst = -4, + puffs_gif_status_short_src = -6, } puffs_gif_status; // ---------------- Structs @@ -39,7 +54,8 @@ typedef struct { // ---------------- Constructor and Destructor Prototypes -void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self); +void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self, + uint32_t puffs_version); void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self); diff --git a/test/c/gif/basic.c b/test/c/gif/basic.c index bcd2209b..3f35517d 100644 --- a/test/c/gif/basic.c +++ b/test/c/gif/basic.c @@ -20,15 +20,30 @@ const char* test_filename = "gif/basic.c"; void test_default_literal_width() { puffs_gif_lzw_decoder dec; - puffs_gif_lzw_decoder_constructor(&dec); + puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION); if (dec.f_literal_width != 0) { FAIL("test_default_literal_width: got %d, want %d", dec.f_literal_width, 0); - puffs_gif_lzw_decoder_destructor(&dec); - return; + goto cleanup0; } +cleanup0: puffs_gif_lzw_decoder_destructor(&dec); } +void test_puffs_version_bad() { + puffs_gif_lzw_decoder dec; + puffs_gif_lzw_decoder_constructor(&dec, 0); // 0 is not PUFFS_VERSION. + if (dec.status != puffs_gif_error_bad_version) { + FAIL("test_puffs_version_bad: got %d, want %d", dec.status, + puffs_gif_error_bad_version); + goto cleanup0; + } +cleanup0: + puffs_gif_lzw_decoder_destructor(&dec); +} + +// The empty comments forces clang-format to place one element per line. test tests[] = { - test_default_literal_width, NULL, + test_default_literal_width, // + test_puffs_version_bad, // + NULL, // };