das2C
das core C utilities (v3)
Loading...
Searching...
No Matches
codex.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Chris Piker <chris-piker@uiowa.edu>
2 *
3 * This file is part of das2C, the Core Das2 C Library.
4 *
5 * Das2C is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License version 2.1 as published
7 * by the Free Software Foundation.
8 *
9 * Das2C is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * version 2.1 along with das2C; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18/* Written by: Claude Opus 4.8 (Anthropic). Anthropic makes no warranty as to
19 * this file's fitness for any purpose; accountability for its inclusion and use
20 * rests with the repository author.
21 */
22
28#ifndef _das_codex_h_
29#define _das_codex_h_
30
31#include <stdbool.h>
32#include <stddef.h>
33
34#include <das3/value.h>
35#include <das3/array.h>
36#include <das3/buffer.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/* Integration sketch -- what this implies elsewhere. All of it is deferred to
43 * the first real codec (png, v3.1); for v3.0 the functions below are do-nothing
44 * stubs so an undecodable blob fails loud with a named error:
45 *
46 * codec.h : DasCodec gains `DasCodex* pCodex;` (NULL = builtin only)
47 * codec.c : DasCodec_init -> new_DasCodex() when a mime is present + supported
48 * DasCodec_deInit -> del_DasCodex(pThis->pCodex)
49 * DasCodec_decode -> DasCodex_decode() for the blob when set
50 * DasCodec_encode -> DasCodex_encode() per record when set
51 * dataset_hdr3.c : parse <packet mime="...">, and on encoding="blob" + mime +
52 * !das_codex_supported(mime) fail loud -- the message now in
53 * var_ary.c moves to this decode site, where the mime is in hand.
54 */
55
56/* ------------------------------------------------------------------------- *
57 * Registry lifecycle -- DEFERRED to the first real codec (png, v3.1)
58 *
59 * Nothing can be registered in v3.0, so the registration/seal/unload API is
60 * commented out below, kept for the design record. When it lands, the intended
61 * shape is: das_init() -> das_codex_reg()xN -> das_codices_ready(). The map
62 * seals at ready() and is immutable after (so post-ready lookups need no lock),
63 * and new_DasCodex succeeds only post-seal. Apps that use no codecs never call
64 * any of it.
65 *
66 * SECURITY (future contract): the registered sFile MUST be an application-
67 * controlled absolute path and must NEVER derive from stream content -- a stream
68 * that can name the .so to load is a code-execution hole. das_codex_reg will
69 * reject non-absolute paths, and the seal means nothing a stream triggers can
70 * register a codec mid-read; but the library cannot verify a path's provenance,
71 * so that stays the caller's contract.
72 * ------------------------------------------------------------------------- */
73
74/* Note: That sMime may have hints, a la: "image/png; gamma=2.2; interlace=adam7" */
75/* DAS_API DasErrCode das_codex_reg(const char* sMime, const char* sFile); */
76/* DAS_API DasErrCode das_codices_ready(void); */
77/* DAS_API void das_codices_unload(void); */
78
79/* Active v3.0 stubs -- always report "nothing registered" so an undecodable blob
80 * fails loud at the reader (dataset_hdr3 / var_ary). */
81
83DAS_API bool das_codex_supported(const char* sMime);
84
86DAS_API const char* das_codex_path(const char* sMime);
87
88/* ------------------------------------------------------------------------- *
89 * Per-variable codec instance
90 *
91 * Reach every member through the DasCodex_* / del_DasCodex macros below, never
92 * `pThis->member(...)`. The macros are the public API and keep call sites
93 * reading like ordinary das2C functions (same idiom as DasVar_get, etc.).
94 * ------------------------------------------------------------------------- */
95
99typedef struct das_codex {
100
101 void* pState; /* instance-private state, freed by del */
102
103 /* Introspection (const). */
104 das_val_type (*storage)(const struct das_codex* pThis);
105 DasErrCode (*path)(const struct das_codex* pThis, char* sBuf, size_t uLen);
106 DasErrCode (*mime)(const struct das_codex* pThis, char* sBuf, size_t uLen);
107
108 /* Write the codec's own properties (e.g. image dims, colorspace) into a
109 dense rank-2 ubyte array in DasDesc's packed layout; *pPropsOut = count. */
110 DasErrCode (*properties)(
111 const struct das_codex* pThis, DasAry* pOut, size_t* pPropsOut
112 );
113
114 /* One blob -> samples appended to pAry; *pAppended = items written. */
115 DasErrCode (*decode)(
116 struct das_codex* pThis, const ubyte* pBlob, size_t uLen,
117 DasAry* pAry, size_t* pAppended
118 );
119
120 /* One record (at pLoc) -> blob bytes appended to pBuf; *pAppended = bytes.
121 Caller guarantees pLoc subsumes enough items to form one object. */
122 DasErrCode (*encode)(
123 struct das_codex* pThis, const DasAry* pItems, const ptrdiff_t* pLoc,
124 DasBuf* pBuf, size_t* pAppended
125 );
126
127 void (*del)(struct das_codex* pThis); /* free pState + the instance */
128
129} DasCodex;
130
159 const char* sMime, das_val_type vtStore, int nRank, const ptrdiff_t* pShape
160);
161
162/* Member access -- the public surface. Arguments are evaluated more than once
163 * (as with the other das2C accessor macros); pass plain pointers. */
164
165
169#define DasCodex_storage(p) ((p)->storage(p))
173#define DasCodex_path(p, sBuf, uLen) ((p)->path((p), (sBuf), (uLen)))
174
178#define DasCodex_mime(p, sBuf, uLen) ((p)->mime((p), (sBuf), (uLen)))
179
183#define DasCodex_properties(p, pOut, pN) ((p)->properties((p), (pOut), (pN)))
184
188#define DasCodex_decode(p, pBlob, uLen, pAry, pN) \
189 ((p)->decode((p), (pBlob), (uLen), (pAry), (pN)))
190
194#define DasCodex_encode(p, pItems, pLoc, pBuf, pN) \
195 ((p)->encode((p), (pItems), (pLoc), (pBuf), (pN)))
196
200#define del_DasCodex(p) ((p)->del(p))
201
202#ifdef __cplusplus
203}
204#endif
205
206#endif /* _das_codex_h_ */
A dynamic buffer with multi-dimensional array style access.
Utility to assist with encode and decode operations.
DAS_API const char * das_codex_path(const char *sMime)
The absolute path registered for a mime, or NULL.
DAS_API bool das_codex_supported(const char *sMime)
True if a codec for this mime is registered and loaded.
int DasErrCode
return code type 0 indicates success, negative integer indicates failure
Definition defs.h:184
das_val_type
Enumeration of types stored in Das Array (DasAry) objects from value.h.
Definition value.h:81
Dynamic recursive ragged arrays.
Definition array.h:271
Buffer class to handle accumulating byte streams.
Definition buffer.h:47
One decode/encode context, made by new_DasCodex and owned by the DasCodec that made it.
Definition codex.h:99
DAS_API DasCodex * new_DasCodex(const char *sMime, das_val_type vtStore, int nRank, const ptrdiff_t *pShape)
Make a codec extension (codex) instance for a given mime type to a specific output type.
A generic value type for use in arrays, datums and variables.