c0data 0.3.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +122 -24
- data/c0-c/c0.h +2309 -1509
- data/c0-c/c0_diff.c +551 -0
- data/c0-c/c0_diff.h +176 -0
- data/c0-c/c0_strbuf.h +49 -0
- data/ext/c0/c0_diff_impl.c +5 -0
- data/ext/c0/c0_ext.c +354 -69
- data/ext/c0/extconf.rb +2 -1
- data/lib/c0/body.rb +158 -38
- data/lib/c0/builder.rb +18 -13
- data/lib/c0/diff.rb +193 -0
- data/lib/c0/document.rb +49 -25
- data/lib/c0/stream.rb +71 -13
- data/lib/c0.rb +18 -11
- metadata +6 -1
data/c0-c/c0.h
CHANGED
|
@@ -12,11 +12,16 @@
|
|
|
12
12
|
* caller-owned buffer and hand back (pointer, length) slices into it. The
|
|
13
13
|
* hot loop outside text is a single comparison, `byte < 0x20`.
|
|
14
14
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
15
|
+
* Parse is validate: `c0_document_read`, `c0_documents` / `c0_next_document`
|
|
16
|
+
* and `c0_body_read` apply the whole grammar (GRAMMAR.md) to every unit and
|
|
17
|
+
* level and fail on malformed input; `c0_validate` is the same check on a
|
|
18
|
+
* whole buffer, `c0_canonical` adds the canonical constraints. The cursors
|
|
19
|
+
* (files, groups, records, units) then assume well-formed input and report
|
|
20
|
+
* the result model: a dead element (BEL before it) is parsed but not in the
|
|
21
|
+
* tree; a sub-unit (`␞a␟b␚␟c`: SUB before US makes `c` a child of `b`) is
|
|
22
|
+
* not a unit of its record and is reached through `c0_record_unit` and
|
|
23
|
+
* `c0_unit_subs`. Writing is `c0_builder`, which owns a growable buffer and
|
|
24
|
+
* spells every value in its one canonical form.
|
|
20
25
|
*
|
|
21
26
|
* Spec: https://github.com/c0data/c0-spec (DESIGN.md, GRAMMAR.md, DIFF.md).
|
|
22
27
|
* License: MIT.
|
|
@@ -31,21 +36,21 @@
|
|
|
31
36
|
extern "C" {
|
|
32
37
|
#endif
|
|
33
38
|
|
|
34
|
-
#define C0_VERSION "0.
|
|
39
|
+
#define C0_VERSION "1.0.0"
|
|
35
40
|
|
|
36
41
|
/* ---- Assigned control codes (DESIGN.md "Assigned Control Codes") ------ */
|
|
37
42
|
#define C0_SOH 0x01 /**< Heading: a key for the value after it; SOH RS a header row */
|
|
38
43
|
#define C0_STX 0x02 /**< Start of text (a literal value) */
|
|
39
44
|
#define C0_ETX 0x03 /**< End of text */
|
|
40
45
|
#define C0_EOT 0x04 /**< End of document */
|
|
41
|
-
#define C0_ENQ 0x05 /**< Reference
|
|
46
|
+
#define C0_ENQ 0x05 /**< Reference: a group name, or a pattern (ENQ DLE … CAN) */
|
|
42
47
|
#define C0_BEL 0x07 /**< Comment: a note, or marks the next element dead */
|
|
43
48
|
#define C0_DLE 0x10 /**< Recurse (opens a nested level) outside text; escape inside */
|
|
44
49
|
#define C0_SYN 0x16 /**< Start of document, then the heading */
|
|
45
50
|
#define C0_ETB 0x17 /**< Commit marker (stream mode) */
|
|
46
51
|
#define C0_CAN 0x18 /**< Close a nested level */
|
|
47
52
|
#define C0_EM 0x19 /**< End of medium: a raw blob follows */
|
|
48
|
-
#define C0_SUB 0x1a /**<
|
|
53
|
+
#define C0_SUB 0x1a /**< Subordinate: the element after is a child of the element before (GS: subsection, US: sub-unit) */
|
|
49
54
|
#define C0_FS 0x1c /**< File separator */
|
|
50
55
|
#define C0_GS 0x1d /**< Group separator */
|
|
51
56
|
#define C0_RS 0x1e /**< Record separator */
|
|
@@ -69,7 +74,7 @@ typedef enum {
|
|
|
69
74
|
C0_ERR_SYNTAX, /**< malformed input (see c0_error.msg / .pos) */
|
|
70
75
|
C0_ERR_UNASSIGNED, /**< an unassigned control code outside text */
|
|
71
76
|
C0_ERR_UNEXPECTED_END, /**< input ended inside text, a level, or after DLE/BEL */
|
|
72
|
-
C0_ERR_TOO_DEEP, /**< nesting deeper than the
|
|
77
|
+
C0_ERR_TOO_DEEP, /**< nesting deeper than the parser's limit */
|
|
73
78
|
C0_ERR_NOT_SCALAR, /**< c0_value on a level or reference */
|
|
74
79
|
C0_ERR_NOT_LEVEL, /**< c0_unit_level on a unit that is not DLE … CAN */
|
|
75
80
|
C0_ERR_NOT_LIST, /**< c0_unit_list on a level that is not a list */
|
|
@@ -89,11 +94,13 @@ typedef struct {
|
|
|
89
94
|
const char *msg;
|
|
90
95
|
} c0_error;
|
|
91
96
|
|
|
92
|
-
/**
|
|
93
|
-
#define C0_WARN_NO_SYN
|
|
94
|
-
#define C0_WARN_NO_EOT
|
|
95
|
-
#define C0_WARN_UNKNOWN_KEY
|
|
96
|
-
#define C0_WARN_DUPLICATE_KEY
|
|
97
|
+
/** Loud warnings a document reader raises (DESIGN.md "Warnings"), as bits. */
|
|
98
|
+
#define C0_WARN_NO_SYN 0x01u /**< no SYN at the start */
|
|
99
|
+
#define C0_WARN_NO_EOT 0x02u /**< no EOT and the shape is not stream */
|
|
100
|
+
#define C0_WARN_UNKNOWN_KEY 0x04u /**< a heading key the reader does not know */
|
|
101
|
+
#define C0_WARN_DUPLICATE_KEY 0x08u /**< a duplicate heading key (last wins) */
|
|
102
|
+
#define C0_WARN_UNKNOWN_SHAPE 0x10u /**< a shape other than diff or stream */
|
|
103
|
+
#define C0_WARN_UNKNOWN_VERSION 0x20u /**< a version other than 1 */
|
|
97
104
|
|
|
98
105
|
/* ---- Bytewise helpers -------------------------------------------------- */
|
|
99
106
|
|
|
@@ -131,11 +138,13 @@ size_t c0_scan_plain(const uint8_t *buf, size_t pos, size_t stop);
|
|
|
131
138
|
size_t c0_scan_text(const uint8_t *buf, size_t pos, size_t stop, c0_error *err);
|
|
132
139
|
/** At a DLE: the position after the matching CAN. */
|
|
133
140
|
size_t c0_scan_level(const uint8_t *buf, size_t pos, size_t stop, c0_error *err);
|
|
134
|
-
/** From just after a US (or a key's US): the end of the unit's value
|
|
141
|
+
/** From just after a US (or a key's US): the end of the unit's value (its
|
|
142
|
+
* sub-units, SUB … US …, are not part of it). */
|
|
135
143
|
size_t c0_scan_unit(const uint8_t *buf, size_t pos, size_t stop, c0_error *err);
|
|
136
|
-
/** At a BEL: past the note, or past the whole dead element. */
|
|
144
|
+
/** At a BEL: past the note, or past the whole dead element's own line. */
|
|
137
145
|
size_t c0_scan_comment(const uint8_t *buf, size_t pos, size_t stop, c0_error *err);
|
|
138
|
-
/** From just after an RS: the end of the record
|
|
146
|
+
/** From just after an RS: the end of the record (a sub-unit, a SUB run then
|
|
147
|
+
* US, stays inside; a SUB run before a line code ends it). */
|
|
139
148
|
size_t c0_scan_record_end(const uint8_t *buf, size_t pos, size_t stop, c0_error *err);
|
|
140
149
|
|
|
141
150
|
/* ---- Validation -------------------------------------------------------- */
|
|
@@ -144,9 +153,10 @@ size_t c0_scan_record_end(const uint8_t *buf, size_t pos, size_t stop, c0_error
|
|
|
144
153
|
* GRAMMAR.md. Returns C0_OK or the first error (detail in *err when given). */
|
|
145
154
|
c0_status c0_validate(const uint8_t *buf, size_t len, c0_error *err);
|
|
146
155
|
|
|
147
|
-
/** Whether bytes are a canonical
|
|
156
|
+
/** Whether bytes are a canonical span (GRAMMAR.md "Canonical constraints"):
|
|
148
157
|
* well-formed, no layout, one spelling per value, unique keys in each keyed
|
|
149
|
-
* record, no framing, no
|
|
158
|
+
* record, no framing, no notes or dead elements. A SUB prefix is structure
|
|
159
|
+
* and is kept. */
|
|
150
160
|
int c0_canonical(const uint8_t *buf, size_t len);
|
|
151
161
|
|
|
152
162
|
/* ---- Units and values -------------------------------------------------- */
|
|
@@ -156,7 +166,7 @@ typedef enum {
|
|
|
156
166
|
C0_UNIT_PLAIN = 0, /**< a plain value, the empty value, or a unit holding only a note */
|
|
157
167
|
C0_UNIT_TEXT, /**< STX … ETX */
|
|
158
168
|
C0_UNIT_LEVEL, /**< DLE … CAN */
|
|
159
|
-
C0_UNIT_REF /**< ENQ name, or ENQ
|
|
169
|
+
C0_UNIT_REF /**< ENQ name, or ENQ DLE … CAN (a pattern) */
|
|
160
170
|
} c0_unit_kind;
|
|
161
171
|
|
|
162
172
|
c0_unit_kind c0_unit_type(c0_bytes raw);
|
|
@@ -165,38 +175,56 @@ c0_unit_kind c0_unit_type(c0_bytes raw);
|
|
|
165
175
|
* The logical value of a raw unit (as yielded by c0_next_unit, c0_next_pair,
|
|
166
176
|
* c0_record_field …): for a plain value the slice with edge layout removed;
|
|
167
177
|
* for text the inside with its escapes decoded; for a unit holding only a
|
|
168
|
-
* note, the empty value. *out points into the buffer when
|
|
169
|
-
* needed; when the text holds DLE escapes it is decoded into
|
|
170
|
-
* (room for raw.len bytes always suffices) and *out points there.
|
|
171
|
-
* NULL scratch such a unit answers C0_ERR_NEEDS_DECODE. A level or
|
|
172
|
-
* answers C0_ERR_NOT_SCALAR; malformed input C0_ERR_SYNTAX.
|
|
178
|
+
* note or a dead level, the empty value. *out points into the buffer when
|
|
179
|
+
* no decoding is needed; when the text holds DLE escapes it is decoded into
|
|
180
|
+
* `scratch` (room for raw.len bytes always suffices) and *out points there.
|
|
181
|
+
* With a NULL scratch such a unit answers C0_ERR_NEEDS_DECODE. A level or
|
|
182
|
+
* reference answers C0_ERR_NOT_SCALAR; malformed input C0_ERR_SYNTAX.
|
|
173
183
|
*/
|
|
174
184
|
c0_status c0_value(c0_bytes raw, uint8_t *scratch, c0_bytes *out);
|
|
175
185
|
|
|
176
186
|
/** A body: a document body or the inside of a nested level, as a range of
|
|
177
|
-
* the buffer. Build one with
|
|
187
|
+
* the buffer. Build one with c0_body_read, c0_document_read or c0_unit_level. */
|
|
178
188
|
typedef struct {
|
|
179
189
|
const uint8_t *buf;
|
|
180
190
|
size_t start;
|
|
181
191
|
size_t stop;
|
|
182
192
|
} c0_body;
|
|
183
193
|
|
|
184
|
-
/** A body over a whole buffer (a slice with no SYN: records, groups, files)
|
|
194
|
+
/** A body over a whole buffer (a slice with no SYN: records, groups, files),
|
|
195
|
+
* unchecked: pair it with c0_validate, or use c0_body_read. */
|
|
185
196
|
c0_body c0_body_view(const uint8_t *buf, size_t len);
|
|
186
197
|
|
|
198
|
+
/** Parse a slice as a body (no SYN, heading or EOT): the grammar is applied
|
|
199
|
+
* to all of it; C0_OK and *out on success, else the first error. */
|
|
200
|
+
c0_status c0_body_read(const uint8_t *buf, size_t len, c0_body *out, c0_error *err);
|
|
201
|
+
|
|
187
202
|
/** A raw unit as a nested level: the body between its DLE and CAN. */
|
|
188
203
|
c0_status c0_unit_level(c0_bytes raw, c0_body *out);
|
|
189
204
|
|
|
190
|
-
/** A parsed reference
|
|
191
|
-
*
|
|
205
|
+
/** A parsed reference (DESIGN.md "References"): a group name written plain,
|
|
206
|
+
* or a pattern, the level `ENQ DLE … CAN` that is one branch of the target
|
|
207
|
+
* with its values left out, reported as the body it is written as. */
|
|
192
208
|
typedef struct {
|
|
193
|
-
c0_bytes
|
|
194
|
-
|
|
209
|
+
c0_bytes name; /**< the group name of a plain reference (empty for a pattern) */
|
|
210
|
+
c0_body pattern; /**< the pattern's body (the inside of its level) when is_pattern */
|
|
211
|
+
int is_pattern;
|
|
195
212
|
} c0_ref;
|
|
196
213
|
|
|
197
|
-
/** A raw unit as a reference. Checks the form only
|
|
214
|
+
/** A raw unit as a reference. Checks the form only; a pattern is checked
|
|
215
|
+
* against the pattern rules (one branch, names only, no values). */
|
|
198
216
|
c0_status c0_unit_ref(c0_bytes raw, c0_ref *out);
|
|
199
217
|
|
|
218
|
+
/** The steps of a reference in walking order: the group name alone, or the
|
|
219
|
+
* pattern's file, group names, record id, field name and the inner
|
|
220
|
+
* pattern's steps (a keyed step contributes its key). Fills up to `max`
|
|
221
|
+
* steps and returns the total count. */
|
|
222
|
+
size_t c0_ref_path(const c0_ref *r, c0_bytes *steps, size_t max);
|
|
223
|
+
|
|
224
|
+
/** Whether the reference names a group as a whole (a plain name, or a
|
|
225
|
+
* pattern that is a bare group line); *name gets the group's name. */
|
|
226
|
+
int c0_ref_group(const c0_ref *r, c0_bytes *name);
|
|
227
|
+
|
|
200
228
|
/* ---- Records ------------------------------------------------------------ */
|
|
201
229
|
|
|
202
230
|
/** A record: the bytes after its RS, trailing layout excluded. Positional
|
|
@@ -207,7 +235,7 @@ typedef struct {
|
|
|
207
235
|
size_t stop;
|
|
208
236
|
} c0_record;
|
|
209
237
|
|
|
210
|
-
/** Whether the record is keyed (
|
|
238
|
+
/** Whether the record is keyed (it holds a keyed unit, live or dead). */
|
|
211
239
|
int c0_record_keyed(c0_record r);
|
|
212
240
|
|
|
213
241
|
/** Cursor over the positional units of a record, or the items of a list. */
|
|
@@ -219,13 +247,46 @@ typedef struct {
|
|
|
219
247
|
c0_error err;
|
|
220
248
|
} c0_unit_iter;
|
|
221
249
|
|
|
222
|
-
/**
|
|
223
|
-
* layout; pass it to c0_value / c0_unit_level /
|
|
224
|
-
*
|
|
225
|
-
*
|
|
250
|
+
/** The record's positional units. Each `raw` is the unit's value bytes with
|
|
251
|
+
* its layout (its sub-units excluded); pass it to c0_value / c0_unit_level /
|
|
252
|
+
* c0_unit_ref. An empty record yields one empty unit. A keyed record yields
|
|
253
|
+
* nothing. A sub-unit is not a unit: `␞a␟b␚␟c` yields `a` and `b`. */
|
|
226
254
|
c0_unit_iter c0_record_units(c0_record r);
|
|
227
255
|
int c0_next_unit(c0_unit_iter *it, c0_bytes *raw);
|
|
228
256
|
|
|
257
|
+
/** A unit with its sub-units (DESIGN.md "Subordinates"): SUB before US
|
|
258
|
+
* makes the unit after a child of the unit before, `␞a␟b␚␟c` is two units
|
|
259
|
+
* and `b` holds `c`; `␞a␚␟b␚␟c` is `a` holding `b` and `c`; `␞a␚␟b␚␚␟c`
|
|
260
|
+
* is `a` holding `b` holding `c` (the SUB run's length is the depth). A
|
|
261
|
+
* keyed unit's value takes sub-units the same way. */
|
|
262
|
+
typedef struct {
|
|
263
|
+
c0_bytes raw; /**< the value with its layout: pass to c0_value / c0_unit_level / c0_unit_ref */
|
|
264
|
+
const uint8_t *buf;
|
|
265
|
+
size_t subs_start; /**< its sub-units: [subs_start, subs_end), SUB runs each followed by US and a value */
|
|
266
|
+
size_t subs_end;
|
|
267
|
+
size_t depth; /**< 0 for a unit of a record; a sub-unit's depth is the length of its SUB run */
|
|
268
|
+
} c0_unit;
|
|
269
|
+
|
|
270
|
+
/** The next positional unit with its sub-units (the walk of c0_next_unit). */
|
|
271
|
+
int c0_next_unit_with_subs(c0_unit_iter *it, c0_unit *out);
|
|
272
|
+
/** Unit n (0-based) with its sub-units, or C0_ERR_NOT_FOUND. */
|
|
273
|
+
c0_status c0_record_unit(c0_record r, size_t n, c0_unit *out);
|
|
274
|
+
|
|
275
|
+
/** Cursor over the direct sub-units of a unit (those one deeper than it), in
|
|
276
|
+
* written order; each comes with its own sub-units. */
|
|
277
|
+
typedef struct {
|
|
278
|
+
const uint8_t *buf;
|
|
279
|
+
size_t pos;
|
|
280
|
+
size_t stop;
|
|
281
|
+
size_t depth; /**< the parent's depth */
|
|
282
|
+
c0_error err;
|
|
283
|
+
} c0_sub_iter;
|
|
284
|
+
|
|
285
|
+
c0_sub_iter c0_unit_subs(c0_unit u);
|
|
286
|
+
int c0_next_sub(c0_sub_iter *it, c0_unit *out);
|
|
287
|
+
/** Number of direct sub-units. */
|
|
288
|
+
size_t c0_unit_sub_count(c0_unit u);
|
|
289
|
+
|
|
229
290
|
/** Number of positional units. */
|
|
230
291
|
size_t c0_record_count(c0_record r);
|
|
231
292
|
/** Raw unit n (0-based), or C0_ERR_NOT_FOUND. */
|
|
@@ -240,13 +301,18 @@ typedef struct {
|
|
|
240
301
|
} c0_pair_iter;
|
|
241
302
|
|
|
242
303
|
/** Keyed units in written order, duplicates included, dead units skipped.
|
|
243
|
-
* `key` is the name; `raw` the unit's raw value. */
|
|
304
|
+
* `key` is the name; `raw` the unit's raw value (its sub-units excluded). */
|
|
244
305
|
c0_pair_iter c0_record_pairs(c0_record r);
|
|
245
306
|
int c0_next_pair(c0_pair_iter *it, c0_bytes *key, c0_bytes *raw);
|
|
307
|
+
/** The same, the value as a unit with its sub-units. */
|
|
308
|
+
int c0_next_pair_with_subs(c0_pair_iter *it, c0_bytes *key, c0_unit *out);
|
|
246
309
|
|
|
247
310
|
/** Raw value for a key (last wins), or C0_ERR_NOT_FOUND. */
|
|
248
311
|
c0_status c0_record_get(c0_record r, const uint8_t *key, size_t klen, c0_bytes *raw);
|
|
249
312
|
c0_status c0_record_get_str(c0_record r, const char *key, c0_bytes *raw);
|
|
313
|
+
/** The unit under a key with its sub-units (last wins), or C0_ERR_NOT_FOUND. */
|
|
314
|
+
c0_status c0_record_unit_get(c0_record r, const uint8_t *key, size_t klen, c0_unit *out);
|
|
315
|
+
c0_status c0_record_unit_get_str(c0_record r, const char *key, c0_unit *out);
|
|
250
316
|
|
|
251
317
|
/** A raw unit as a flat list: a level holding at most one positional record.
|
|
252
318
|
* On C0_OK `items` iterates the items (none for the empty level). A level
|
|
@@ -255,18 +321,21 @@ c0_status c0_unit_list(c0_bytes raw, c0_unit_iter *items);
|
|
|
255
321
|
|
|
256
322
|
/* ---- Bodies: files, groups, records ------------------------------------ */
|
|
257
323
|
|
|
258
|
-
/** A file within a body: its name and the range of its contents.
|
|
324
|
+
/** A file within a body: its name and the range of its contents. The
|
|
325
|
+
* unnamed file before the first FS is `leading`. */
|
|
259
326
|
typedef struct {
|
|
260
327
|
const uint8_t *buf;
|
|
261
328
|
size_t name_start;
|
|
262
329
|
size_t name_end;
|
|
263
330
|
size_t start;
|
|
264
331
|
size_t stop;
|
|
332
|
+
int leading;
|
|
265
333
|
} c0_file;
|
|
266
334
|
|
|
267
|
-
/** A group: its depth (
|
|
268
|
-
* a file or level), name, and the
|
|
269
|
-
* records; deeper
|
|
335
|
+
/** A group: its depth (one plus the SUB prefix of its GS line: `␝a` 1, `␚␝b`
|
|
336
|
+
* 2; 0 for the unnamed group at the start of a file or level), name, and the
|
|
337
|
+
* range of its contents (header row and records; deeper subsections are
|
|
338
|
+
* separate groups). */
|
|
270
339
|
typedef struct {
|
|
271
340
|
const uint8_t *buf;
|
|
272
341
|
size_t depth;
|
|
@@ -279,9 +348,10 @@ typedef struct {
|
|
|
279
348
|
c0_bytes c0_file_name(c0_file f);
|
|
280
349
|
c0_bytes c0_group_name(c0_group g);
|
|
281
350
|
|
|
282
|
-
/** Cursor over the files of a body
|
|
283
|
-
* form an unnamed file, reported only when
|
|
284
|
-
*
|
|
351
|
+
/** Cursor over the files of a body as the result model has them. Records
|
|
352
|
+
* and groups before the first FS form an unnamed file, reported only when
|
|
353
|
+
* it has content or no FS follows. A dead file (BEL FS) is dropped with
|
|
354
|
+
* everything it holds. */
|
|
285
355
|
typedef struct {
|
|
286
356
|
const uint8_t *buf;
|
|
287
357
|
size_t pos;
|
|
@@ -296,7 +366,8 @@ int c0_next_file(c0_file_iter *it, c0_file *out);
|
|
|
296
366
|
|
|
297
367
|
/** Cursor over groups: every GS line at any depth, in order, plus the
|
|
298
368
|
* unnamed leading group of a file or level when it holds a header row or
|
|
299
|
-
* records. A dead
|
|
369
|
+
* records. A dead group is dropped with its header row, records and deeper
|
|
370
|
+
* sections. */
|
|
300
371
|
typedef struct {
|
|
301
372
|
const uint8_t *buf;
|
|
302
373
|
size_t pos;
|
|
@@ -325,7 +396,7 @@ int c0_body_bare(c0_body b);
|
|
|
325
396
|
int c0_group_header(c0_group g, c0_record *out);
|
|
326
397
|
|
|
327
398
|
/** Cursor over the live records of a group (header rows, notes, commits and
|
|
328
|
-
* dead
|
|
399
|
+
* dead records skipped). */
|
|
329
400
|
typedef struct {
|
|
330
401
|
const uint8_t *buf;
|
|
331
402
|
size_t pos;
|
|
@@ -355,11 +426,11 @@ typedef struct {
|
|
|
355
426
|
size_t heading_end; /**< … up to the first body code */
|
|
356
427
|
c0_body body;
|
|
357
428
|
unsigned warnings; /**< C0_WARN_* bits */
|
|
429
|
+
size_t dead_elements; /**< dead records, header rows, groups and files met in the body */
|
|
358
430
|
} c0_document;
|
|
359
431
|
|
|
360
|
-
/** Read the document starting at `start` (after any layout)
|
|
361
|
-
*
|
|
362
|
-
* end. Shapes inside the body are not checked here: use c0_validate. */
|
|
432
|
+
/** Read the document starting at `start` (after any layout), applying the
|
|
433
|
+
* whole grammar to it: C0_OK, or the first error. */
|
|
363
434
|
c0_status c0_document_read(const uint8_t *buf, size_t len, size_t start, c0_document *out, c0_error *err);
|
|
364
435
|
|
|
365
436
|
/** The heading's keyed units, in written order (dead units skipped). */
|
|
@@ -408,23 +479,25 @@ c0_status c0_blob_str(const uint8_t *buf, size_t len, const char *name, c0_bytes
|
|
|
408
479
|
* call c0_builder_free when done. Once `status` is set, further writes are
|
|
409
480
|
* no-ops; check it before using the bytes. Values are byte-transparent and
|
|
410
481
|
* written in their one spelling: plain when they can be, text otherwise with
|
|
411
|
-
* only the seven codes escaped. Names (file, group, header, key, reference
|
|
412
|
-
*
|
|
482
|
+
* only the seven codes escaped. Names (file, group, header, key, reference,
|
|
483
|
+
* ETB payload) must be plain, or C0_ERR_BAD_NAME is set.
|
|
413
484
|
*
|
|
414
485
|
* Two layers. The primitives, `c0_builder_<code>`, are one function per
|
|
415
|
-
* code (fs gs rs us sub soh dle can bel enq syn eot etb em); each writes its
|
|
416
|
-
* and, when given, what grammatically follows it, so a sequence of
|
|
417
|
-
* reads like the bytes:
|
|
486
|
+
* code (fs gs rs us sub soh dle can bel enq syn eot etb em); each writes its
|
|
487
|
+
* code and, when given, what grammatically follows it, so a sequence of
|
|
488
|
+
* calls reads like the bytes:
|
|
418
489
|
*
|
|
419
490
|
* c0_builder_rs_str(&b, "Alice"); c0_builder_us_str(&b, "1502.30"); ␞Alice␟1502.30
|
|
420
491
|
* c0_builder_rs(&b); c0_builder_soh_str(&b, "host"); c0_builder_us_str(&b, "localhost");
|
|
421
492
|
* c0_builder_bel(&b); c0_builder_gs_str(&b, "old"); a dead group
|
|
493
|
+
* c0_builder_sub(&b); c0_builder_gs_str(&b, "s"); ␚␝s: a subsection (depth 2)
|
|
494
|
+
* c0_builder_rs_str(&b, "old"); c0_builder_sub(&b); c0_builder_us_str(&b, "new"); ␞old␚␟new: a sub-unit
|
|
422
495
|
*
|
|
423
|
-
* The composed functions, `c0_build_<what>` (file, group,
|
|
424
|
-
* field, map, key, level, list, ref, note), are built on the
|
|
425
|
-
* cannot write ill-formed output. The primitives write what
|
|
426
|
-
* well-formedness is then yours to keep. Both layers share the
|
|
427
|
-
* so they mix.
|
|
496
|
+
* The composed functions, `c0_build_<what>` (file, group, section, header,
|
|
497
|
+
* record, field, sub_unit, map, key, level, list, ref, note), are built on the
|
|
498
|
+
* primitives and cannot write ill-formed output. The primitives write what
|
|
499
|
+
* you spell; well-formedness is then yours to keep. Both layers share the
|
|
500
|
+
* record state, so they mix.
|
|
428
501
|
*
|
|
429
502
|
* A record is opened by c0_build_record or c0_builder_rs (or implicitly by
|
|
430
503
|
* the first c0_build_field / c0_build_key / c0_build_level_open /
|
|
@@ -454,22 +527,21 @@ c0_bytes c0_builder_bytes(const c0_builder *b);
|
|
|
454
527
|
* follows it; `_str` is that form on a NUL-terminated string. Values take
|
|
455
528
|
* their one spelling (plain or text); names must be plain. */
|
|
456
529
|
void c0_builder_fs(c0_builder *b, const uint8_t *name, size_t nlen); /**< FS name; ends any open record */
|
|
457
|
-
void c0_builder_gs(c0_builder *b); /**< GS alone:
|
|
530
|
+
void c0_builder_gs(c0_builder *b); /**< GS alone: an empty-named group (`␝␝b` is group "" then group b) */
|
|
458
531
|
void c0_builder_gs_name(c0_builder *b, const uint8_t *name, size_t nlen); /**< GS name; ends any open record */
|
|
459
532
|
void c0_builder_rs(c0_builder *b); /**< RS: open a record */
|
|
460
533
|
void c0_builder_rs_value(c0_builder *b, const uint8_t *value, size_t vlen); /**< RS and the record's first value */
|
|
461
534
|
void c0_builder_us(c0_builder *b); /**< US alone (an empty unit, or before DLE / ENQ) */
|
|
462
535
|
void c0_builder_us_value(c0_builder *b, const uint8_t *value, size_t vlen); /**< US and the value that follows it */
|
|
463
|
-
void c0_builder_sub(c0_builder *b); /**< SUB
|
|
464
|
-
void c0_builder_sub_value(c0_builder *b, const uint8_t *value, size_t vlen); /**< SUB and the new side of a substitution (C0-DIFF only): rs old, sub new is a replace-all */
|
|
536
|
+
void c0_builder_sub(c0_builder *b); /**< SUB: the element after the next separator is subordinate to the one before (before GS depth, before US a sub-unit) */
|
|
465
537
|
void c0_builder_soh(c0_builder *b); /**< SOH alone: before RS, a header row */
|
|
466
538
|
void c0_builder_soh_name(c0_builder *b, const uint8_t *name, size_t nlen); /**< SOH key */
|
|
467
539
|
void c0_builder_dle(c0_builder *b); /**< DLE: open a level (saves the record state) */
|
|
468
540
|
void c0_builder_can(c0_builder *b); /**< CAN: close the innermost level; C0_ERR_BAD_STATE if none is open */
|
|
469
541
|
void c0_builder_bel(c0_builder *b); /**< BEL alone: the element that follows is dead */
|
|
470
542
|
void c0_builder_bel_text(c0_builder *b, const uint8_t *text, size_t tlen); /**< BEL and a note; empty text is C0_ERR_BAD_STATE */
|
|
471
|
-
void c0_builder_enq(c0_builder *b, const uint8_t *name, size_t nlen); /**< ENQ name */
|
|
472
|
-
void
|
|
543
|
+
void c0_builder_enq(c0_builder *b, const uint8_t *name, size_t nlen); /**< ENQ name: a group reference */
|
|
544
|
+
void c0_builder_enq_dle(c0_builder *b); /**< ENQ DLE: a pattern; write its lines, then c0_builder_can */
|
|
473
545
|
void c0_builder_syn(c0_builder *b); /**< SYN; soh_name + us_value pairs that follow are the heading */
|
|
474
546
|
void c0_builder_eot(c0_builder *b); /**< EOT */
|
|
475
547
|
void c0_builder_etb(c0_builder *b); /**< ETB alone */
|
|
@@ -480,11 +552,9 @@ void c0_builder_fs_str(c0_builder *b, const char *name);
|
|
|
480
552
|
void c0_builder_gs_str(c0_builder *b, const char *name);
|
|
481
553
|
void c0_builder_rs_str(c0_builder *b, const char *value);
|
|
482
554
|
void c0_builder_us_str(c0_builder *b, const char *value);
|
|
483
|
-
void c0_builder_sub_str(c0_builder *b, const char *value);
|
|
484
555
|
void c0_builder_soh_str(c0_builder *b, const char *name);
|
|
485
556
|
void c0_builder_bel_str(c0_builder *b, const char *text);
|
|
486
557
|
void c0_builder_enq_str(c0_builder *b, const char *name);
|
|
487
|
-
void c0_builder_enq_path_str(c0_builder *b, const char *const *segments, size_t count);
|
|
488
558
|
void c0_builder_etb_str(c0_builder *b, const char *payload);
|
|
489
559
|
void c0_builder_em_str(c0_builder *b, const char *tail);
|
|
490
560
|
|
|
@@ -499,20 +569,22 @@ void c0_build_em(c0_builder *b, const uint8_t *tail, size_t tlen); /**< EM a
|
|
|
499
569
|
/* structure */
|
|
500
570
|
void c0_build_file(c0_builder *b, const uint8_t *name, size_t nlen); /**< FS name */
|
|
501
571
|
void c0_build_group(c0_builder *b, const uint8_t *name, size_t nlen); /**< GS name */
|
|
502
|
-
void c0_build_section(c0_builder *b, const uint8_t *name, size_t nlen, size_t depth); /**<
|
|
572
|
+
void c0_build_section(c0_builder *b, const uint8_t *name, size_t nlen, size_t depth); /**< SUB×(depth−1) GS name: a group at `depth` (1 is a plain GS line) */
|
|
503
573
|
void c0_build_header(c0_builder *b, const c0_bytes *names, size_t count); /**< SOH RS name US name … */
|
|
504
574
|
|
|
505
575
|
/* records */
|
|
506
576
|
void c0_build_record(c0_builder *b, const c0_bytes *fields, size_t count); /**< RS and `count` units; count 0 opens an empty record */
|
|
507
577
|
void c0_build_field(c0_builder *b, const uint8_t *value, size_t vlen); /**< next unit of the open record (opens one if none) */
|
|
578
|
+
void c0_build_sub_unit(c0_builder *b, const uint8_t *value, size_t vlen, size_t depth); /**< SUB×depth US value: a sub-unit of the unit just written (depth 2: of the sub-unit before); not a unit of the record */
|
|
508
579
|
void c0_build_map(c0_builder *b, const c0_bytes *keys, const c0_bytes *values, size_t count); /**< RS and keyed units in the order given */
|
|
509
580
|
void c0_build_key(c0_builder *b, const uint8_t *key, size_t klen, const uint8_t *value, size_t vlen); /**< SOH key US value, in the open record or the heading */
|
|
510
581
|
void c0_build_level_open(c0_builder *b); /**< DLE: a nested level as the next unit; inside, the builder is at the top of a fresh body */
|
|
511
582
|
void c0_build_key_level_open(c0_builder *b, const uint8_t *key, size_t klen); /**< SOH key US DLE */
|
|
512
583
|
void c0_build_level_close(c0_builder *b); /**< CAN */
|
|
513
584
|
void c0_build_list(c0_builder *b, const c0_bytes *items, size_t count); /**< a flat list as the next unit: DLE RS item US item … CAN, or DLE CAN */
|
|
514
|
-
void c0_build_ref(c0_builder *b, const uint8_t *name, size_t nlen); /**< ENQ name as the next unit */
|
|
515
|
-
void
|
|
585
|
+
void c0_build_ref(c0_builder *b, const uint8_t *name, size_t nlen); /**< ENQ name as the next unit: a group reference */
|
|
586
|
+
void c0_build_ref_open(c0_builder *b); /**< ENQ DLE as the next unit: a pattern; write its lines, then c0_build_level_close */
|
|
587
|
+
void c0_build_ref_record(c0_builder *b, const uint8_t *group, size_t glen, const uint8_t *record, size_t rlen, const uint8_t *field, size_t flen); /**< ␅␐␝group␞record[␟field]␘ as the next unit (field NULL to omit) */
|
|
516
588
|
void c0_build_note(c0_builder *b, const uint8_t *text, size_t tlen); /**< BEL and the note (plain, or text when it must be) */
|
|
517
589
|
|
|
518
590
|
/* document mode */
|
|
@@ -527,22 +599,40 @@ void c0_build_section_str(c0_builder *b, const char *name, size_t depth);
|
|
|
527
599
|
void c0_build_header_str(c0_builder *b, const char *const *names, size_t count);
|
|
528
600
|
void c0_build_record_str(c0_builder *b, const char *const *fields, size_t count);
|
|
529
601
|
void c0_build_field_str(c0_builder *b, const char *value);
|
|
602
|
+
void c0_build_sub_unit_str(c0_builder *b, const char *value, size_t depth);
|
|
530
603
|
void c0_build_map_str(c0_builder *b, const char *const *keys, const char *const *values, size_t count);
|
|
531
604
|
void c0_build_key_str(c0_builder *b, const char *key, const char *value);
|
|
532
605
|
void c0_build_key_level_open_str(c0_builder *b, const char *key);
|
|
533
606
|
void c0_build_list_str(c0_builder *b, const char *const *items, size_t count);
|
|
534
607
|
void c0_build_ref_str(c0_builder *b, const char *name);
|
|
535
|
-
void
|
|
608
|
+
void c0_build_ref_record_str(c0_builder *b, const char *group, const char *record, const char *field);
|
|
536
609
|
void c0_build_note_str(c0_builder *b, const char *text);
|
|
537
610
|
void c0_build_block_str(c0_builder *b, const char *text);
|
|
538
611
|
void c0_build_item_str(c0_builder *b, const char *text);
|
|
539
612
|
|
|
540
613
|
/* ---- Stream mode (ETB commits) ----------------------------------------- */
|
|
541
614
|
|
|
615
|
+
/** Parser state carried from one committed block to the next (every field
|
|
616
|
+
* is internal). A damaged block leaves it untouched, so what follows is
|
|
617
|
+
* read in the context before the damage. */
|
|
618
|
+
typedef struct {
|
|
619
|
+
int phase;
|
|
620
|
+
int first;
|
|
621
|
+
int in_level;
|
|
622
|
+
int leading;
|
|
623
|
+
int dead_file;
|
|
624
|
+
int dead_depth;
|
|
625
|
+
int prev_depth;
|
|
626
|
+
int header_ok;
|
|
627
|
+
int file_has_groups;
|
|
628
|
+
size_t nfiles;
|
|
629
|
+
} c0_parse_state;
|
|
630
|
+
|
|
542
631
|
/** A scan of an append-only log. An unescaped ETB is always a commit, inside
|
|
543
|
-
* text or a level too
|
|
544
|
-
*
|
|
545
|
-
*
|
|
632
|
+
* text or a level too. A committed block that is not well-formed, cut by its
|
|
633
|
+
* ETB or holding any other error, is damaged: it delivers nothing and adds
|
|
634
|
+
* nothing to the parse state. The tail after the last commit is torn unless
|
|
635
|
+
* it holds only layout, notes, EOT or EM. */
|
|
546
636
|
typedef struct {
|
|
547
637
|
const uint8_t *buf;
|
|
548
638
|
size_t len;
|
|
@@ -550,6 +640,9 @@ typedef struct {
|
|
|
550
640
|
int torn;
|
|
551
641
|
size_t block_count;
|
|
552
642
|
size_t damaged_count;
|
|
643
|
+
int has_damaged; /**< whether any block is damaged */
|
|
644
|
+
size_t first_damaged; /**< index of the first damaged block */
|
|
645
|
+
size_t resume_offset; /**< the offset after that block's ETB and payload, where reading can go on */
|
|
553
646
|
} c0_stream;
|
|
554
647
|
|
|
555
648
|
c0_stream c0_stream_read(const uint8_t *buf, size_t len);
|
|
@@ -557,24 +650,32 @@ c0_bytes c0_stream_committed(const c0_stream *s);
|
|
|
557
650
|
c0_bytes c0_stream_tail(const c0_stream *s);
|
|
558
651
|
|
|
559
652
|
/** Cursor over the committed blocks (previous commit to ETB, marker and
|
|
560
|
-
* payload excluded); *damaged is set for a block
|
|
653
|
+
* payload excluded); *damaged is set for a block that is not well-formed in
|
|
654
|
+
* the context of the sound blocks before it. */
|
|
561
655
|
typedef struct {
|
|
562
656
|
const uint8_t *buf;
|
|
563
657
|
size_t stop;
|
|
564
658
|
size_t pos;
|
|
565
659
|
size_t block_start;
|
|
566
660
|
int in_text;
|
|
567
|
-
size_t
|
|
661
|
+
size_t index;
|
|
662
|
+
c0_parse_state st;
|
|
568
663
|
} c0_block_iter;
|
|
569
664
|
|
|
570
665
|
c0_block_iter c0_stream_blocks(const c0_stream *s);
|
|
571
666
|
int c0_next_block(c0_block_iter *it, c0_bytes *out, int *damaged);
|
|
572
667
|
|
|
573
668
|
/** Copy the undamaged committed blocks, without their ETB markers and
|
|
574
|
-
* payloads, into `out` (room for committed_end bytes suffices)
|
|
575
|
-
*
|
|
669
|
+
* payloads, into `out` (room for committed_end bytes suffices), continuing
|
|
670
|
+
* past every damaged block. Returns the length. Read the result with
|
|
671
|
+
* c0_document_read / c0_documents to get the records. */
|
|
576
672
|
size_t c0_stream_undamaged(const c0_stream *s, uint8_t *out);
|
|
577
673
|
|
|
674
|
+
/** The same, stopping at the first damaged block: the default reading, which
|
|
675
|
+
* delivers the records before the damage; `resume_offset` says where to go
|
|
676
|
+
* on. Returns the length. */
|
|
677
|
+
size_t c0_stream_before_damage(const c0_stream *s, uint8_t *out);
|
|
678
|
+
|
|
578
679
|
/** Position just past the last commit, found by a backward scan (a shortcut
|
|
579
680
|
* for well-formed input; c0_stream_read is authoritative). */
|
|
580
681
|
size_t c0_stream_last_commit_end(const uint8_t *buf, size_t len);
|
|
@@ -784,6 +885,40 @@ static int c0__bel_kills_element(const uint8_t *buf, size_t pos, size_t stop) {
|
|
|
784
885
|
return p2 < stop && buf[p2] < 0x20 && buf[p2] != C0_STX && buf[p2] != C0_DLE;
|
|
785
886
|
}
|
|
786
887
|
|
|
888
|
+
/* Is the SOH at pos followed (after layout) by RS: a header row? */
|
|
889
|
+
static int c0__soh_is_header(const uint8_t *buf, size_t pos, size_t stop) {
|
|
890
|
+
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
891
|
+
return p2 < stop && buf[p2] == C0_RS;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
/* A SUB run at pos (layout may follow each SUB): *run gets its length and
|
|
895
|
+
* *next the position of the code after it (stop when the input ends). */
|
|
896
|
+
static void c0__sub_run(const uint8_t *buf, size_t pos, size_t stop, size_t *run, size_t *next) {
|
|
897
|
+
size_t n = 0;
|
|
898
|
+
while (pos < stop && buf[pos] == C0_SUB) {
|
|
899
|
+
n++;
|
|
900
|
+
pos = c0__layout(buf, pos + 1, stop);
|
|
901
|
+
}
|
|
902
|
+
*run = n;
|
|
903
|
+
*next = pos;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
static size_t c0__unit(const uint8_t *buf, size_t pos, size_t stop, c0_error *err);
|
|
907
|
+
|
|
908
|
+
/* Past the sub-units after a value ending at pos: SUB runs, each followed by
|
|
909
|
+
* US and a value. Returns the position after the last; a SUB run before
|
|
910
|
+
* anything but US (a depth prefix on the next line) is left where it is. */
|
|
911
|
+
static size_t c0__skip_subs(const uint8_t *buf, size_t pos, size_t stop, c0_error *err) {
|
|
912
|
+
for (;;) {
|
|
913
|
+
size_t run, nx, p = c0__layout(buf, pos, stop);
|
|
914
|
+
if (!(p < stop && buf[p] == C0_SUB)) return pos;
|
|
915
|
+
c0__sub_run(buf, p, stop, &run, &nx);
|
|
916
|
+
if (!(nx < stop && buf[nx] == C0_US)) return pos;
|
|
917
|
+
pos = c0__unit(buf, nx + 1, stop, err);
|
|
918
|
+
if (err->code) return stop;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
|
|
787
922
|
/* From just after a US (or a key's US): the end of one unit's value, i.e.
|
|
788
923
|
* the next control code at this depth that does not belong to the value.
|
|
789
924
|
* Layout at the edges stays inside the returned range. */
|
|
@@ -811,7 +946,8 @@ static size_t c0__unit(const uint8_t *buf, size_t pos, size_t stop, c0_error *er
|
|
|
811
946
|
return pos;
|
|
812
947
|
}
|
|
813
948
|
|
|
814
|
-
/* At a BEL: skip the note (plain or text) or the
|
|
949
|
+
/* At a BEL: skip the note (plain or text) or the dead element's own line
|
|
950
|
+
* (a dead group or file line only, not what it holds). */
|
|
815
951
|
static size_t c0__comment(const uint8_t *buf, size_t pos, size_t stop, c0_error *err) {
|
|
816
952
|
uint8_t b;
|
|
817
953
|
pos = c0__layout(buf, pos + 1, stop);
|
|
@@ -827,11 +963,15 @@ static size_t c0__comment(const uint8_t *buf, size_t pos, size_t stop, c0_error
|
|
|
827
963
|
if (b == C0_SOH) {
|
|
828
964
|
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
829
965
|
if (p2 < stop && buf[p2] == C0_RS) return c0__record_end(buf, p2 + 1, stop, err);
|
|
830
|
-
return c0__keyed_unit_end(buf, pos + 1, stop, err);
|
|
831
|
-
}
|
|
832
|
-
if (b == C0_GS)
|
|
833
|
-
|
|
834
|
-
|
|
966
|
+
return c0__skip_subs(buf, c0__keyed_unit_end(buf, pos + 1, stop, err), stop, err); /* a dead keyed unit takes its sub-units */
|
|
967
|
+
}
|
|
968
|
+
if (b == C0_GS) return c0__plain(buf, c0__layout(buf, pos + 1, stop), stop);
|
|
969
|
+
if (b == C0_SUB) { /* a dead group line with its depth prefix */
|
|
970
|
+
size_t run, nx;
|
|
971
|
+
c0__sub_run(buf, pos, stop, &run, &nx);
|
|
972
|
+
if (nx < stop && buf[nx] == C0_GS) return c0__plain(buf, c0__layout(buf, nx + 1, stop), stop);
|
|
973
|
+
c0__fail(err, C0_ERR_SYNTAX, pos, "BEL before a code that is not an element");
|
|
974
|
+
return stop;
|
|
835
975
|
}
|
|
836
976
|
if (b == C0_FS) return c0__plain(buf, pos + 1, stop);
|
|
837
977
|
c0__fail(err, C0_ERR_SYNTAX, pos, "BEL before a code that is not an element");
|
|
@@ -839,16 +979,21 @@ static size_t c0__comment(const uint8_t *buf, size_t pos, size_t stop, c0_error
|
|
|
839
979
|
}
|
|
840
980
|
|
|
841
981
|
/* From just after an RS: the end of the record, i.e. the next record-level
|
|
842
|
-
* code at this depth.
|
|
982
|
+
* code at this depth. A sub-unit (a SUB run then US) stays inside the
|
|
983
|
+
* record; a SUB run before a line code ends it. */
|
|
843
984
|
static size_t c0__record_end(const uint8_t *buf, size_t pos, size_t stop, c0_error *err) {
|
|
844
985
|
while (pos < stop) {
|
|
845
986
|
uint8_t b = buf[pos];
|
|
846
987
|
if (b >= 0x20 || c0_is_layout(b) || b == C0_US || b == C0_ENQ) {
|
|
847
988
|
pos++;
|
|
848
989
|
} else if (b == C0_SOH) {
|
|
849
|
-
|
|
850
|
-
if (p2 < stop && buf[p2] == C0_RS) return pos; /* a header row */
|
|
990
|
+
if (c0__soh_is_header(buf, pos, stop)) return pos; /* a header row */
|
|
851
991
|
pos++;
|
|
992
|
+
} else if (b == C0_SUB) {
|
|
993
|
+
size_t run, nx;
|
|
994
|
+
c0__sub_run(buf, pos, stop, &run, &nx);
|
|
995
|
+
if (nx < stop && buf[nx] == C0_US) pos = nx;
|
|
996
|
+
else return pos;
|
|
852
997
|
} else if (b == C0_STX) {
|
|
853
998
|
pos = c0__text(buf, pos, stop, err);
|
|
854
999
|
if (err->code) return stop;
|
|
@@ -857,11 +1002,8 @@ static size_t c0__record_end(const uint8_t *buf, size_t pos, size_t stop, c0_err
|
|
|
857
1002
|
if (err->code) return stop;
|
|
858
1003
|
} else if (b == C0_BEL) {
|
|
859
1004
|
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
860
|
-
if (p2 < stop && (buf[p2] == C0_RS || buf[p2] == C0_GS || buf[p2] == C0_FS)) return pos;
|
|
861
|
-
if (p2 < stop && buf[p2] == C0_SOH)
|
|
862
|
-
size_t p3 = c0__layout(buf, p2 + 1, stop);
|
|
863
|
-
if (p3 < stop && buf[p3] == C0_RS) return pos;
|
|
864
|
-
}
|
|
1005
|
+
if (p2 < stop && (buf[p2] == C0_RS || buf[p2] == C0_GS || buf[p2] == C0_FS || buf[p2] == C0_SUB)) return pos;
|
|
1006
|
+
if (p2 < stop && buf[p2] == C0_SOH && c0__soh_is_header(buf, p2, stop)) return pos;
|
|
865
1007
|
pos = c0__comment(buf, pos, stop, err);
|
|
866
1008
|
if (err->code) return stop;
|
|
867
1009
|
} else {
|
|
@@ -902,6 +1044,11 @@ C0__SCAN_WRAP(c0_scan_unit, c0__unit)
|
|
|
902
1044
|
C0__SCAN_WRAP(c0_scan_comment, c0__comment)
|
|
903
1045
|
C0__SCAN_WRAP(c0_scan_record_end, c0__record_end)
|
|
904
1046
|
|
|
1047
|
+
static int c0__eq_str(c0_bytes b, const char *s) {
|
|
1048
|
+
size_t n = strlen(s);
|
|
1049
|
+
return b.len == n && (n == 0 || memcmp(b.ptr, s, n) == 0);
|
|
1050
|
+
}
|
|
1051
|
+
|
|
905
1052
|
/* ---- values ------------------------------------------------------------ */
|
|
906
1053
|
|
|
907
1054
|
c0_unit_kind c0_unit_type(c0_bytes raw) {
|
|
@@ -995,38 +1142,6 @@ c0_status c0_unit_level(c0_bytes raw, c0_body *out) {
|
|
|
995
1142
|
return C0_OK;
|
|
996
1143
|
}
|
|
997
1144
|
|
|
998
|
-
c0_status c0_unit_ref(c0_bytes raw, c0_ref *out) {
|
|
999
|
-
c0_bytes t = c0_trim(raw);
|
|
1000
|
-
size_t p;
|
|
1001
|
-
out->count = 0;
|
|
1002
|
-
if (!(t.len > 0 && t.ptr[0] == C0_ENQ)) return C0_ERR_NOT_REFERENCE;
|
|
1003
|
-
p = c0__layout(t.ptr, 1, t.len);
|
|
1004
|
-
if (p < t.len && t.ptr[p] == C0_STX) {
|
|
1005
|
-
c0_error err = {C0_OK, 0, ""};
|
|
1006
|
-
size_t stop = c0__text(t.ptr, p, t.len, &err), s, i;
|
|
1007
|
-
if (err.code) return err.code;
|
|
1008
|
-
s = p + 1;
|
|
1009
|
-
for (i = s; i <= stop - 1; i++) {
|
|
1010
|
-
if (i == stop - 1 || t.ptr[i] == C0_US) {
|
|
1011
|
-
if (out->count == 3) return C0_ERR_SYNTAX; /* more than three segments */
|
|
1012
|
-
if (i == s) return C0_ERR_SYNTAX; /* empty segment */
|
|
1013
|
-
out->seg[out->count] = c0__slice(t.ptr, s, i);
|
|
1014
|
-
if (!c0_is_name(out->seg[out->count].ptr, out->seg[out->count].len)) return C0_ERR_SYNTAX;
|
|
1015
|
-
out->count++;
|
|
1016
|
-
s = i + 1;
|
|
1017
|
-
}
|
|
1018
|
-
}
|
|
1019
|
-
return C0_OK;
|
|
1020
|
-
} else {
|
|
1021
|
-
c0_bytes name = c0_trim(c0__slice(t.ptr, p, t.len));
|
|
1022
|
-
if (name.len == 0) return C0_ERR_SYNTAX; /* reference without a name */
|
|
1023
|
-
if (!c0_is_name(name.ptr, name.len)) return C0_ERR_SYNTAX;
|
|
1024
|
-
out->seg[0] = name;
|
|
1025
|
-
out->count = 1;
|
|
1026
|
-
return C0_OK;
|
|
1027
|
-
}
|
|
1028
|
-
}
|
|
1029
|
-
|
|
1030
1145
|
/* ---- records ----------------------------------------------------------- */
|
|
1031
1146
|
|
|
1032
1147
|
int c0_record_keyed(c0_record r) {
|
|
@@ -1034,12 +1149,12 @@ int c0_record_keyed(c0_record r) {
|
|
|
1034
1149
|
c0_error err = {C0_OK, 0, ""};
|
|
1035
1150
|
while (p < r.stop && r.buf[p] == C0_BEL) {
|
|
1036
1151
|
size_t p2 = c0__layout(r.buf, p + 1, r.stop);
|
|
1037
|
-
if (p2 < r.stop && r.buf[p2] == C0_SOH) return
|
|
1038
|
-
if (p2
|
|
1152
|
+
if (p2 < r.stop && r.buf[p2] == C0_SOH) return !c0__soh_is_header(r.buf, p2, r.stop);
|
|
1153
|
+
if (p2 >= r.stop || (r.buf[p2] < 0x20 && r.buf[p2] != C0_STX)) break;
|
|
1039
1154
|
p = c0__layout(r.buf, c0__comment(r.buf, p, r.stop, &err), r.stop);
|
|
1040
1155
|
if (err.code) return 0;
|
|
1041
1156
|
}
|
|
1042
|
-
return p < r.stop && r.buf[p] == C0_SOH;
|
|
1157
|
+
return p < r.stop && r.buf[p] == C0_SOH && !c0__soh_is_header(r.buf, p, r.stop);
|
|
1043
1158
|
}
|
|
1044
1159
|
|
|
1045
1160
|
c0_unit_iter c0_record_units(c0_record r) {
|
|
@@ -1052,24 +1167,112 @@ c0_unit_iter c0_record_units(c0_record r) {
|
|
|
1052
1167
|
return it;
|
|
1053
1168
|
}
|
|
1054
1169
|
|
|
1055
|
-
|
|
1056
|
-
|
|
1170
|
+
static c0_unit c0__unit_at(const uint8_t *buf, size_t s, size_t e, size_t subs_end, size_t depth) {
|
|
1171
|
+
c0_unit u;
|
|
1172
|
+
u.raw = c0__slice(buf, s, e);
|
|
1173
|
+
u.buf = buf;
|
|
1174
|
+
u.subs_start = e;
|
|
1175
|
+
u.subs_end = subs_end;
|
|
1176
|
+
u.depth = depth;
|
|
1177
|
+
return u;
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
int c0_next_unit_with_subs(c0_unit_iter *it, c0_unit *out) {
|
|
1181
|
+
size_t s, e, nx;
|
|
1057
1182
|
if (it->done) return 0;
|
|
1058
1183
|
s = it->pos;
|
|
1059
1184
|
e = c0__unit(it->buf, s, it->stop, &it->err);
|
|
1185
|
+
nx = e;
|
|
1186
|
+
if (!it->err.code) nx = c0__skip_subs(it->buf, e, it->stop, &it->err);
|
|
1060
1187
|
if (it->err.code) {
|
|
1061
1188
|
it->done = 1;
|
|
1062
1189
|
return 0;
|
|
1063
1190
|
}
|
|
1064
|
-
*
|
|
1065
|
-
if (
|
|
1066
|
-
it->pos =
|
|
1191
|
+
*out = c0__unit_at(it->buf, s, e, nx, 0);
|
|
1192
|
+
if (nx < it->stop && it->buf[nx] == C0_US) {
|
|
1193
|
+
it->pos = nx + 1;
|
|
1067
1194
|
} else {
|
|
1068
1195
|
it->done = 1;
|
|
1069
1196
|
}
|
|
1070
1197
|
return 1;
|
|
1071
1198
|
}
|
|
1072
1199
|
|
|
1200
|
+
int c0_next_unit(c0_unit_iter *it, c0_bytes *raw) {
|
|
1201
|
+
c0_unit u;
|
|
1202
|
+
if (!c0_next_unit_with_subs(it, &u)) return 0;
|
|
1203
|
+
*raw = u.raw;
|
|
1204
|
+
return 1;
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
c0_status c0_record_unit(c0_record r, size_t n, c0_unit *out) {
|
|
1208
|
+
c0_unit_iter it = c0_record_units(r);
|
|
1209
|
+
size_t i = 0;
|
|
1210
|
+
while (c0_next_unit_with_subs(&it, out)) {
|
|
1211
|
+
if (i == n) return C0_OK;
|
|
1212
|
+
i++;
|
|
1213
|
+
}
|
|
1214
|
+
return it.err.code ? it.err.code : C0_ERR_NOT_FOUND;
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
c0_sub_iter c0_unit_subs(c0_unit u) {
|
|
1218
|
+
c0_sub_iter it;
|
|
1219
|
+
it.buf = u.buf;
|
|
1220
|
+
it.pos = u.subs_start;
|
|
1221
|
+
it.stop = u.subs_end;
|
|
1222
|
+
it.depth = u.depth;
|
|
1223
|
+
c0__err_clear(&it.err);
|
|
1224
|
+
return it;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
/* Within the parent's sub-unit span the direct children are the sub-units
|
|
1228
|
+
* one deeper than the parent; a run deeper than that belongs to the child
|
|
1229
|
+
* before it (the parser has refused skipped depths). */
|
|
1230
|
+
int c0_next_sub(c0_sub_iter *it, c0_unit *out) {
|
|
1231
|
+
const uint8_t *buf = it->buf;
|
|
1232
|
+
size_t run, nx, s, e, p;
|
|
1233
|
+
if (it->err.code) return 0;
|
|
1234
|
+
p = c0__layout(buf, it->pos, it->stop);
|
|
1235
|
+
if (!(p < it->stop && buf[p] == C0_SUB)) {
|
|
1236
|
+
it->pos = it->stop;
|
|
1237
|
+
return 0;
|
|
1238
|
+
}
|
|
1239
|
+
c0__sub_run(buf, p, it->stop, &run, &nx);
|
|
1240
|
+
if (!(nx < it->stop && buf[nx] == C0_US) || run != it->depth + 1) {
|
|
1241
|
+
c0__fail(&it->err, C0_ERR_SYNTAX, p, "malformed sub-unit");
|
|
1242
|
+
it->pos = it->stop;
|
|
1243
|
+
return 0;
|
|
1244
|
+
}
|
|
1245
|
+
s = nx + 1;
|
|
1246
|
+
e = c0__unit(buf, s, it->stop, &it->err);
|
|
1247
|
+
if (it->err.code) {
|
|
1248
|
+
it->pos = it->stop;
|
|
1249
|
+
return 0;
|
|
1250
|
+
}
|
|
1251
|
+
p = e;
|
|
1252
|
+
for (;;) { /* the child's own sub-units: the deeper runs that follow */
|
|
1253
|
+
size_t q = c0__layout(buf, p, it->stop), r2, n2;
|
|
1254
|
+
if (!(q < it->stop && buf[q] == C0_SUB)) break;
|
|
1255
|
+
c0__sub_run(buf, q, it->stop, &r2, &n2);
|
|
1256
|
+
if (r2 <= run || !(n2 < it->stop && buf[n2] == C0_US)) break;
|
|
1257
|
+
p = c0__unit(buf, n2 + 1, it->stop, &it->err);
|
|
1258
|
+
if (it->err.code) {
|
|
1259
|
+
it->pos = it->stop;
|
|
1260
|
+
return 0;
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
*out = c0__unit_at(buf, s, e, p, run);
|
|
1264
|
+
it->pos = p;
|
|
1265
|
+
return 1;
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
size_t c0_unit_sub_count(c0_unit u) {
|
|
1269
|
+
c0_sub_iter it = c0_unit_subs(u);
|
|
1270
|
+
c0_unit s;
|
|
1271
|
+
size_t n = 0;
|
|
1272
|
+
while (c0_next_sub(&it, &s)) n++;
|
|
1273
|
+
return n;
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1073
1276
|
size_t c0_record_count(c0_record r) {
|
|
1074
1277
|
c0_unit_iter it = c0_record_units(r);
|
|
1075
1278
|
c0_bytes u;
|
|
@@ -1099,13 +1302,13 @@ static c0_pair_iter c0__pairs(const uint8_t *buf, size_t start, size_t stop) {
|
|
|
1099
1302
|
|
|
1100
1303
|
c0_pair_iter c0_record_pairs(c0_record r) { return c0__pairs(r.buf, r.start, r.stop); }
|
|
1101
1304
|
|
|
1102
|
-
int
|
|
1305
|
+
int c0_next_pair_with_subs(c0_pair_iter *it, c0_bytes *key, c0_unit *out) {
|
|
1103
1306
|
const uint8_t *buf = it->buf;
|
|
1104
1307
|
while (it->pos < it->stop) {
|
|
1105
1308
|
uint8_t b = buf[it->pos];
|
|
1106
1309
|
if (b == C0_SOH) {
|
|
1107
1310
|
size_t ks = c0__layout(buf, it->pos + 1, it->stop);
|
|
1108
|
-
size_t ke = c0__plain(buf, ks, it->stop), vs, ve;
|
|
1311
|
+
size_t ke = c0__plain(buf, ks, it->stop), vs, ve, nx;
|
|
1109
1312
|
if (!(ke < it->stop && buf[ke] == C0_US)) {
|
|
1110
1313
|
c0__fail(&it->err, C0_ERR_SYNTAX, ke, "key without a value (no US)");
|
|
1111
1314
|
it->pos = it->stop;
|
|
@@ -1113,13 +1316,15 @@ int c0_next_pair(c0_pair_iter *it, c0_bytes *key, c0_bytes *raw) {
|
|
|
1113
1316
|
}
|
|
1114
1317
|
vs = ke + 1;
|
|
1115
1318
|
ve = c0__unit(buf, vs, it->stop, &it->err);
|
|
1319
|
+
nx = ve;
|
|
1320
|
+
if (!it->err.code) nx = c0__skip_subs(buf, ve, it->stop, &it->err);
|
|
1116
1321
|
if (it->err.code) {
|
|
1117
1322
|
it->pos = it->stop;
|
|
1118
1323
|
return 0;
|
|
1119
1324
|
}
|
|
1325
|
+
it->pos = nx;
|
|
1120
1326
|
*key = c0_trim(c0__slice(buf, ks, ke));
|
|
1121
|
-
*
|
|
1122
|
-
it->pos = ve;
|
|
1327
|
+
*out = c0__unit_at(buf, vs, ve, nx, 0);
|
|
1123
1328
|
return 1;
|
|
1124
1329
|
} else if (b == C0_BEL) {
|
|
1125
1330
|
it->pos = c0__comment(buf, it->pos, it->stop, &it->err);
|
|
@@ -1134,6 +1339,13 @@ int c0_next_pair(c0_pair_iter *it, c0_bytes *key, c0_bytes *raw) {
|
|
|
1134
1339
|
return 0;
|
|
1135
1340
|
}
|
|
1136
1341
|
|
|
1342
|
+
int c0_next_pair(c0_pair_iter *it, c0_bytes *key, c0_bytes *raw) {
|
|
1343
|
+
c0_unit u;
|
|
1344
|
+
if (!c0_next_pair_with_subs(it, key, &u)) return 0;
|
|
1345
|
+
*raw = u.raw;
|
|
1346
|
+
return 1;
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1137
1349
|
static c0_status c0__pairs_get(c0_pair_iter it, const uint8_t *key, size_t klen, c0_bytes *raw) {
|
|
1138
1350
|
c0_bytes k, v;
|
|
1139
1351
|
int found = 0;
|
|
@@ -1155,20 +1367,40 @@ c0_status c0_record_get_str(c0_record r, const char *key, c0_bytes *raw) {
|
|
|
1155
1367
|
return c0_record_get(r, (const uint8_t *)key, strlen(key), raw);
|
|
1156
1368
|
}
|
|
1157
1369
|
|
|
1370
|
+
c0_status c0_record_unit_get(c0_record r, const uint8_t *key, size_t klen, c0_unit *out) {
|
|
1371
|
+
c0_pair_iter it = c0_record_pairs(r);
|
|
1372
|
+
c0_bytes k;
|
|
1373
|
+
c0_unit u;
|
|
1374
|
+
int found = 0;
|
|
1375
|
+
while (c0_next_pair_with_subs(&it, &k, &u)) {
|
|
1376
|
+
if (k.len == klen && (klen == 0 || memcmp(k.ptr, key, klen) == 0)) {
|
|
1377
|
+
*out = u;
|
|
1378
|
+
found = 1;
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
if (it.err.code) return it.err.code;
|
|
1382
|
+
return found ? C0_OK : C0_ERR_NOT_FOUND;
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
c0_status c0_record_unit_get_str(c0_record r, const char *key, c0_unit *out) {
|
|
1386
|
+
return c0_record_unit_get(r, (const uint8_t *)key, strlen(key), out);
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1158
1389
|
/* ---- element scanner over a body range ---------------------------------
|
|
1159
1390
|
* Yields the next record-level element at this depth: a record, a header
|
|
1160
|
-
* row, a group line or a file line, live or dead. Notes and
|
|
1161
|
-
* skipped.
|
|
1391
|
+
* row, a group line or a file line, live or dead. Notes and
|
|
1392
|
+
* commits are skipped. The cursors built on it assume well-formed input
|
|
1393
|
+
* (the parser below has checked it) and report the result model. */
|
|
1162
1394
|
|
|
1163
1395
|
typedef enum { C0__EL_END = 0, C0__EL_RECORD, C0__EL_HEADER, C0__EL_GROUP, C0__EL_FILE } c0__el_kind;
|
|
1164
1396
|
|
|
1165
1397
|
typedef struct {
|
|
1166
1398
|
c0__el_kind kind;
|
|
1167
|
-
int dead;
|
|
1168
|
-
size_t depth; /* GROUP:
|
|
1399
|
+
int dead; /* BEL before it */
|
|
1400
|
+
size_t depth; /* GROUP: one plus its SUB prefix */
|
|
1169
1401
|
size_t a, b; /* RECORD/HEADER: the record's range; GROUP/FILE: the name's range */
|
|
1170
|
-
size_t at; /* where the element (or its BEL) begins */
|
|
1171
|
-
size_t next; /* position after the element */
|
|
1402
|
+
size_t at; /* where the element (or its BEL / SUB) begins */
|
|
1403
|
+
size_t next; /* position after the element's own line */
|
|
1172
1404
|
} c0__el;
|
|
1173
1405
|
|
|
1174
1406
|
static void c0__el_record(const uint8_t *buf, size_t rs, size_t stop, c0__el *el, c0__el_kind kind, c0_error *err) {
|
|
@@ -1208,6 +1440,17 @@ static void c0__next_el(const uint8_t *buf, size_t pos, size_t stop, c0__el *el,
|
|
|
1208
1440
|
return;
|
|
1209
1441
|
}
|
|
1210
1442
|
}
|
|
1443
|
+
if (b == C0_SUB) { /* a group line's depth prefix */
|
|
1444
|
+
size_t run, p2;
|
|
1445
|
+
c0__sub_run(buf, pos, stop, &run, &p2);
|
|
1446
|
+
if (!(p2 < stop && buf[p2] == C0_GS)) {
|
|
1447
|
+
c0__fail(err, C0_ERR_SYNTAX, pos, "SUB before a byte that is not GS or US");
|
|
1448
|
+
return;
|
|
1449
|
+
}
|
|
1450
|
+
el->depth = run;
|
|
1451
|
+
pos = p2;
|
|
1452
|
+
b = C0_GS;
|
|
1453
|
+
}
|
|
1211
1454
|
if (b == C0_RS) {
|
|
1212
1455
|
c0__el_record(buf, pos + 1, stop, el, C0__EL_RECORD, err);
|
|
1213
1456
|
return;
|
|
@@ -1222,10 +1465,8 @@ static void c0__next_el(const uint8_t *buf, size_t pos, size_t stop, c0__el *el,
|
|
|
1222
1465
|
return;
|
|
1223
1466
|
}
|
|
1224
1467
|
if (b == C0_GS) {
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
pos++;
|
|
1228
|
-
}
|
|
1468
|
+
el->depth++;
|
|
1469
|
+
pos = c0__layout(buf, pos + 1, stop);
|
|
1229
1470
|
el->kind = C0__EL_GROUP;
|
|
1230
1471
|
el->a = pos;
|
|
1231
1472
|
el->b = el->next = c0__plain(buf, pos, stop);
|
|
@@ -1257,6 +1498,32 @@ static void c0__next_el(const uint8_t *buf, size_t pos, size_t stop, c0__el *el,
|
|
|
1257
1498
|
}
|
|
1258
1499
|
}
|
|
1259
1500
|
|
|
1501
|
+
/* After a dead group line (el): skip what it holds. Returns the position of
|
|
1502
|
+
* the element that ends its span (a group line at its depth or shallower, a
|
|
1503
|
+
* file line, or the end), which the caller reads next. */
|
|
1504
|
+
static size_t c0__skip_dead_group(const uint8_t *buf, const c0__el *dead, size_t stop, c0_error *err) {
|
|
1505
|
+
size_t pos = dead->next;
|
|
1506
|
+
c0__el el;
|
|
1507
|
+
for (;;) {
|
|
1508
|
+
c0__next_el(buf, pos, stop, &el, err);
|
|
1509
|
+
if (el.kind == C0__EL_END || el.kind == C0__EL_FILE) return el.at;
|
|
1510
|
+
if (el.kind == C0__EL_GROUP && el.depth <= dead->depth) return el.at;
|
|
1511
|
+
pos = el.next;
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
/* After a dead file line: skip what it holds, up to the next file line or
|
|
1516
|
+
* the end. */
|
|
1517
|
+
static size_t c0__skip_dead_file(const uint8_t *buf, const c0__el *dead, size_t stop, c0_error *err) {
|
|
1518
|
+
size_t pos = dead->next;
|
|
1519
|
+
c0__el el;
|
|
1520
|
+
for (;;) {
|
|
1521
|
+
c0__next_el(buf, pos, stop, &el, err);
|
|
1522
|
+
if (el.kind == C0__EL_END || el.kind == C0__EL_FILE) return el.at;
|
|
1523
|
+
pos = el.next;
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1260
1527
|
/* ---- bodies: files ------------------------------------------------------ */
|
|
1261
1528
|
|
|
1262
1529
|
c0_bytes c0_file_name(c0_file f) { return c0_trim(c0__slice(f.buf, f.name_start, f.name_end)); }
|
|
@@ -1273,53 +1540,67 @@ c0_file_iter c0_body_files(c0_body b) {
|
|
|
1273
1540
|
return it;
|
|
1274
1541
|
}
|
|
1275
1542
|
|
|
1276
|
-
/* Scan from pos to the next
|
|
1277
|
-
*
|
|
1543
|
+
/* Scan a file's contents from pos to the next file line (live or dead) or
|
|
1544
|
+
* the end; *has says whether a live element (outside dead spans) was passed. */
|
|
1278
1545
|
static void c0__part_end(const uint8_t *buf, size_t pos, size_t stop, c0__el *el, int *has, c0_error *err) {
|
|
1279
1546
|
*has = 0;
|
|
1280
1547
|
for (;;) {
|
|
1281
1548
|
c0__next_el(buf, pos, stop, el, err);
|
|
1282
|
-
if (el->kind == C0__EL_END) return;
|
|
1283
|
-
if (el->
|
|
1284
|
-
|
|
1549
|
+
if (el->kind == C0__EL_END || el->kind == C0__EL_FILE || err->code) return;
|
|
1550
|
+
if (el->dead) {
|
|
1551
|
+
pos = el->kind == C0__EL_GROUP ? c0__skip_dead_group(buf, el, stop, err) : el->next;
|
|
1552
|
+
continue;
|
|
1553
|
+
}
|
|
1554
|
+
*has = 1;
|
|
1285
1555
|
pos = el->next;
|
|
1286
1556
|
}
|
|
1287
1557
|
}
|
|
1288
1558
|
|
|
1289
1559
|
int c0_next_file(c0_file_iter *it, c0_file *out) {
|
|
1290
1560
|
c0__el el;
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
it->first
|
|
1296
|
-
|
|
1297
|
-
|
|
1561
|
+
for (;;) {
|
|
1562
|
+
int have = 0, has;
|
|
1563
|
+
if (it->done || it->err.code) return 0;
|
|
1564
|
+
out->buf = it->buf;
|
|
1565
|
+
if (it->first) {
|
|
1566
|
+
it->first = 0;
|
|
1567
|
+
c0__part_end(it->buf, it->pos, it->stop, &el, &has, &it->err);
|
|
1568
|
+
out->leading = 1;
|
|
1298
1569
|
out->name_start = out->name_end = it->pos;
|
|
1299
1570
|
out->start = it->pos;
|
|
1300
1571
|
out->stop = el.at;
|
|
1572
|
+
have = has;
|
|
1573
|
+
} else {
|
|
1574
|
+
c0__next_el(it->buf, it->pos, it->stop, &el, &it->err);
|
|
1575
|
+
if (el.kind != C0__EL_FILE) {
|
|
1576
|
+
it->done = 1;
|
|
1577
|
+
return 0;
|
|
1578
|
+
}
|
|
1579
|
+
out->leading = 0;
|
|
1580
|
+
out->name_start = el.a;
|
|
1581
|
+
out->name_end = el.b;
|
|
1582
|
+
out->start = el.next;
|
|
1583
|
+
c0__part_end(it->buf, el.next, it->stop, &el, &has, &it->err);
|
|
1584
|
+
out->stop = el.at;
|
|
1585
|
+
have = 1;
|
|
1586
|
+
}
|
|
1587
|
+
/* dead files after this one are dropped with their contents */
|
|
1588
|
+
while (el.kind == C0__EL_FILE && el.dead && !it->err.code) {
|
|
1589
|
+
size_t p = c0__skip_dead_file(it->buf, &el, it->stop, &it->err);
|
|
1590
|
+
c0__next_el(it->buf, p, it->stop, &el, &it->err);
|
|
1591
|
+
}
|
|
1592
|
+
if (it->err.code) return 0;
|
|
1593
|
+
if (el.kind == C0__EL_END) {
|
|
1594
|
+
/* a body with no live file, every element dead included, is an
|
|
1595
|
+
* empty body: it has one empty leading file */
|
|
1596
|
+
if (out->leading) have = 1;
|
|
1597
|
+
it->done = 1;
|
|
1598
|
+
it->pos = it->stop;
|
|
1599
|
+
} else {
|
|
1301
1600
|
it->pos = el.at;
|
|
1302
|
-
if (el.kind == C0__EL_END) it->done = 1;
|
|
1303
|
-
return 1;
|
|
1304
1601
|
}
|
|
1305
|
-
|
|
1602
|
+
if (have) return 1;
|
|
1306
1603
|
}
|
|
1307
|
-
c0__next_el(it->buf, it->pos, it->stop, &el, &it->err);
|
|
1308
|
-
if (el.kind != C0__EL_FILE) {
|
|
1309
|
-
it->done = 1;
|
|
1310
|
-
return 0;
|
|
1311
|
-
}
|
|
1312
|
-
out->name_start = el.a;
|
|
1313
|
-
out->name_end = el.b;
|
|
1314
|
-
out->start = el.next;
|
|
1315
|
-
{
|
|
1316
|
-
int has;
|
|
1317
|
-
c0__part_end(it->buf, el.next, it->stop, &el, &has, &it->err);
|
|
1318
|
-
out->stop = el.at;
|
|
1319
|
-
it->pos = el.at;
|
|
1320
|
-
if (el.kind == C0__EL_END) it->done = 1;
|
|
1321
|
-
}
|
|
1322
|
-
return 1;
|
|
1323
1604
|
}
|
|
1324
1605
|
|
|
1325
1606
|
/* ---- bodies: groups ---------------------------------------------------- */
|
|
@@ -1338,15 +1619,15 @@ static c0_group_iter c0__groups(const uint8_t *buf, size_t start, size_t stop) {
|
|
|
1338
1619
|
c0_group_iter c0_file_groups(c0_file f) { return c0__groups(f.buf, f.start, f.stop); }
|
|
1339
1620
|
c0_group_iter c0_body_groups(c0_body b) { return c0__groups(b.buf, b.start, b.stop); }
|
|
1340
1621
|
|
|
1341
|
-
/* Scan group contents from pos: stop at the next
|
|
1342
|
-
* end; *has says whether a live header row or record
|
|
1622
|
+
/* Scan a group's own contents from pos: stop at the next group or file line
|
|
1623
|
+
* (live or dead) or the end; *has says whether a live header row or record
|
|
1624
|
+
* was passed. */
|
|
1343
1625
|
static void c0__group_end(const uint8_t *buf, size_t pos, size_t stop, c0__el *el, int *has, c0_error *err) {
|
|
1344
1626
|
*has = 0;
|
|
1345
1627
|
for (;;) {
|
|
1346
1628
|
c0__next_el(buf, pos, stop, el, err);
|
|
1347
|
-
if (el->kind == C0__EL_END) return;
|
|
1348
|
-
if (
|
|
1349
|
-
if (!el->dead && (el->kind == C0__EL_RECORD || el->kind == C0__EL_HEADER)) *has = 1;
|
|
1629
|
+
if (el->kind == C0__EL_END || el->kind == C0__EL_GROUP || el->kind == C0__EL_FILE || err->code) return;
|
|
1630
|
+
if (!el->dead) *has = 1;
|
|
1350
1631
|
pos = el->next;
|
|
1351
1632
|
}
|
|
1352
1633
|
}
|
|
@@ -1356,32 +1637,39 @@ int c0_next_group(c0_group_iter *it, c0_group *out) {
|
|
|
1356
1637
|
int has;
|
|
1357
1638
|
out->buf = it->buf;
|
|
1358
1639
|
for (;;) {
|
|
1359
|
-
if (it->done) return 0;
|
|
1640
|
+
if (it->done || it->err.code) return 0;
|
|
1360
1641
|
if (it->first) {
|
|
1642
|
+
size_t start = it->pos;
|
|
1361
1643
|
it->first = 0;
|
|
1362
|
-
c0__group_end(it->buf,
|
|
1363
|
-
|
|
1644
|
+
c0__group_end(it->buf, start, it->stop, &el, &has, &it->err);
|
|
1645
|
+
it->pos = el.at;
|
|
1646
|
+
if (el.kind == C0__EL_END) it->done = 1;
|
|
1647
|
+
if (has) { /* the unnamed group: from where the scan began */
|
|
1364
1648
|
out->depth = 0;
|
|
1365
|
-
out->name_start = out->name_end =
|
|
1366
|
-
out->start = it->pos;
|
|
1649
|
+
out->name_start = out->name_end = out->start = start;
|
|
1367
1650
|
out->stop = el.at;
|
|
1368
|
-
it->pos = el.at;
|
|
1369
|
-
if (el.kind == C0__EL_END) it->done = 1;
|
|
1370
1651
|
return 1;
|
|
1371
1652
|
}
|
|
1372
|
-
|
|
1653
|
+
continue;
|
|
1373
1654
|
}
|
|
1374
1655
|
c0__next_el(it->buf, it->pos, it->stop, &el, &it->err);
|
|
1375
|
-
if (el.kind == C0__EL_END) {
|
|
1656
|
+
if (el.kind == C0__EL_END || it->err.code) {
|
|
1376
1657
|
it->done = 1;
|
|
1377
1658
|
return 0;
|
|
1378
1659
|
}
|
|
1379
1660
|
if (el.kind == C0__EL_FILE) { /* body-wide iteration: a new part begins */
|
|
1380
|
-
|
|
1381
|
-
|
|
1661
|
+
if (el.dead) {
|
|
1662
|
+
it->pos = c0__skip_dead_file(it->buf, &el, it->stop, &it->err);
|
|
1663
|
+
} else {
|
|
1664
|
+
it->first = 1;
|
|
1665
|
+
it->pos = el.next;
|
|
1666
|
+
}
|
|
1667
|
+
continue;
|
|
1668
|
+
}
|
|
1669
|
+
if (el.dead) {
|
|
1670
|
+
it->pos = c0__skip_dead_group(it->buf, &el, it->stop, &it->err);
|
|
1382
1671
|
continue;
|
|
1383
1672
|
}
|
|
1384
|
-
/* a live group line (dead ones are passed by c0__group_end) */
|
|
1385
1673
|
out->depth = el.depth;
|
|
1386
1674
|
out->name_start = el.a;
|
|
1387
1675
|
out->name_end = el.b;
|
|
@@ -1414,7 +1702,7 @@ int c0_body_bare(c0_body b) {
|
|
|
1414
1702
|
c0_group g;
|
|
1415
1703
|
size_t n = 0;
|
|
1416
1704
|
if (!c0_next_file(&fi, &f)) return 0;
|
|
1417
|
-
if (
|
|
1705
|
+
if (!f.leading) return 0;
|
|
1418
1706
|
if (c0_next_file(&fi, &f)) return 0;
|
|
1419
1707
|
gi = c0_file_groups(f);
|
|
1420
1708
|
while (c0_next_group(&gi, &g)) {
|
|
@@ -1473,7 +1761,7 @@ int c0_next_record(c0_record_iter *it, c0_record *out) {
|
|
|
1473
1761
|
if (it->done) return 0;
|
|
1474
1762
|
for (;;) {
|
|
1475
1763
|
c0__next_el(it->buf, it->pos, it->stop, &el, &it->err);
|
|
1476
|
-
if (el.kind == C0__EL_END) {
|
|
1764
|
+
if (el.kind == C0__EL_END || it->err.code) {
|
|
1477
1765
|
it->done = 1;
|
|
1478
1766
|
return 0;
|
|
1479
1767
|
}
|
|
@@ -1520,7 +1808,7 @@ c0_status c0_unit_list(c0_bytes raw, c0_unit_iter *items) {
|
|
|
1520
1808
|
c0__err_clear(&items->err);
|
|
1521
1809
|
fi = c0_body_files(body);
|
|
1522
1810
|
if (!c0_next_file(&fi, &f)) return C0_OK; /* an empty level: no items */
|
|
1523
|
-
if (
|
|
1811
|
+
if (!f.leading || c0_next_file(&fi, &f)) return C0_ERR_NOT_LIST;
|
|
1524
1812
|
gi = c0_file_groups(f);
|
|
1525
1813
|
if (!c0_next_group(&gi, &g)) return C0_OK;
|
|
1526
1814
|
if (g.depth != 0 || c0_next_group(&gi, &g)) return C0_ERR_NOT_LIST;
|
|
@@ -1533,1558 +1821,2038 @@ c0_status c0_unit_list(c0_bytes raw, c0_unit_iter *items) {
|
|
|
1533
1821
|
return C0_OK;
|
|
1534
1822
|
}
|
|
1535
1823
|
|
|
1536
|
-
/* ----
|
|
1824
|
+
/* ---- the parser (GRAMMAR.md; parser.cr) ---------------------------------
|
|
1825
|
+
* One walk of the grammar under everything else. It raises on the first
|
|
1826
|
+
* structural error (recorded in err; every function then returns `stop` so
|
|
1827
|
+
* loops end), notes every canonical violation when asked, and counts the
|
|
1828
|
+
* dead elements met in bodies for tools above the format. Body level is a
|
|
1829
|
+
* flat state machine over lines
|
|
1830
|
+
* (file line, group line, header row, record, note, commit) whose state is
|
|
1831
|
+
* a c0_parse_state, so a stream reader can carry it from block to block;
|
|
1832
|
+
* records and their values are recursive, since a level holds a body. */
|
|
1537
1833
|
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1834
|
+
#define C0__MAX_DEPTH 512
|
|
1835
|
+
#define C0__GS_DEPTH_MAX 256
|
|
1836
|
+
|
|
1837
|
+
typedef struct {
|
|
1838
|
+
const uint8_t *buf;
|
|
1839
|
+
int canonical; /* check the canonical constraints too */
|
|
1840
|
+
int ok; /* no canonical violation met */
|
|
1841
|
+
size_t dead_elements;
|
|
1842
|
+
size_t depth; /* open levels */
|
|
1843
|
+
c0_error err;
|
|
1844
|
+
} c0__parser;
|
|
1845
|
+
|
|
1846
|
+
#define C0__P_ERR(p) ((p)->err.code != C0_OK)
|
|
1847
|
+
|
|
1848
|
+
enum { C0__PH_START = 0, C0__PH_BODY, C0__PH_AFTER_EOT, C0__PH_EM };
|
|
1849
|
+
|
|
1850
|
+
static void c0__p_init(c0__parser *p, const uint8_t *buf, int canonical) {
|
|
1851
|
+
p->buf = buf;
|
|
1852
|
+
p->canonical = canonical;
|
|
1853
|
+
p->ok = 1;
|
|
1854
|
+
p->dead_elements = 0;
|
|
1855
|
+
p->depth = 0;
|
|
1856
|
+
c0__err_clear(&p->err);
|
|
1857
|
+
}
|
|
1858
|
+
|
|
1859
|
+
static void c0__p_nc(c0__parser *p) { p->ok = 0; }
|
|
1860
|
+
|
|
1861
|
+
static void c0__p_fail(c0__parser *p, c0_status code, size_t pos, const char *msg) { c0__fail(&p->err, code, pos, msg); }
|
|
1862
|
+
|
|
1863
|
+
static size_t c0__p_layout(c0__parser *p, size_t pos, size_t stop) {
|
|
1864
|
+
size_t p2 = c0__layout(p->buf, pos, stop);
|
|
1865
|
+
if (p2 != pos) c0__p_nc(p);
|
|
1866
|
+
return p2;
|
|
1867
|
+
}
|
|
1868
|
+
|
|
1869
|
+
/* The body state at the start of a document. */
|
|
1870
|
+
static void c0__st_body_reset(c0_parse_state *s, int in_level) {
|
|
1871
|
+
s->in_level = in_level;
|
|
1872
|
+
s->leading = 1;
|
|
1873
|
+
s->dead_file = 0;
|
|
1874
|
+
s->dead_depth = 0;
|
|
1875
|
+
s->prev_depth = 0;
|
|
1876
|
+
s->header_ok = 1;
|
|
1877
|
+
s->file_has_groups = 0;
|
|
1878
|
+
s->nfiles = 0;
|
|
1879
|
+
}
|
|
1880
|
+
|
|
1881
|
+
static void c0__st_init(c0_parse_state *s) {
|
|
1882
|
+
s->phase = C0__PH_START;
|
|
1883
|
+
s->first = 1;
|
|
1884
|
+
c0__st_body_reset(s, 0);
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1887
|
+
static size_t c0__p_body(c0__parser *p, c0_parse_state *s, size_t pos, size_t stop);
|
|
1888
|
+
static size_t c0__p_record(c0__parser *p, size_t pos, size_t stop);
|
|
1889
|
+
static size_t c0__p_value(c0__parser *p, size_t pos, size_t stop, int heading);
|
|
1890
|
+
static size_t c0__p_level(c0__parser *p, size_t pos, size_t stop);
|
|
1891
|
+
static size_t c0__p_reference(c0__parser *p, size_t pos, size_t stop);
|
|
1892
|
+
|
|
1893
|
+
/* The content of a note, from the byte after BEL and layout. */
|
|
1894
|
+
static size_t c0__p_note_content(c0__parser *p, size_t pos, size_t stop) {
|
|
1895
|
+
return p->buf[pos] == C0_STX ? c0__text(p->buf, pos, stop, &p->err) : c0__plain(p->buf, pos, stop);
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
/* Notes after a value. A BEL before a code is left for the caller. */
|
|
1899
|
+
static size_t c0__p_trailing_notes(c0__parser *p, size_t pos, size_t stop) {
|
|
1900
|
+
const uint8_t *buf = p->buf;
|
|
1901
|
+
while (pos < stop && buf[pos] == C0_BEL) {
|
|
1902
|
+
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
1903
|
+
uint8_t c;
|
|
1904
|
+
if (p2 >= stop) {
|
|
1905
|
+
c0__p_fail(p, C0_ERR_UNEXPECTED_END, p2, "BEL at end of input");
|
|
1906
|
+
return stop;
|
|
1558
1907
|
}
|
|
1908
|
+
c = buf[p2];
|
|
1909
|
+
if (!(c >= 0x20 || c == C0_STX)) break;
|
|
1910
|
+
c0__p_nc(p);
|
|
1911
|
+
pos = c0__p_note_content(p, p2, stop);
|
|
1912
|
+
if (C0__P_ERR(p)) return stop;
|
|
1913
|
+
pos = c0__p_layout(p, pos, stop);
|
|
1914
|
+
}
|
|
1915
|
+
return pos;
|
|
1916
|
+
}
|
|
1917
|
+
|
|
1918
|
+
static int c0__value_start(const uint8_t *buf, size_t pos, size_t stop) {
|
|
1919
|
+
uint8_t b;
|
|
1920
|
+
if (pos >= stop) return 0;
|
|
1921
|
+
b = buf[pos];
|
|
1922
|
+
return b >= 0x20 || b == C0_STX || b == C0_DLE || b == C0_ENQ;
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
/* A label or path: plain, never text. *ns / *ne get the trimmed name. */
|
|
1926
|
+
static size_t c0__p_name(c0__parser *p, size_t pos, size_t stop, size_t *ns, size_t *ne) {
|
|
1927
|
+
const uint8_t *buf = p->buf;
|
|
1928
|
+
size_t e;
|
|
1929
|
+
pos = c0__p_layout(p, pos, stop);
|
|
1930
|
+
if (pos < stop && buf[pos] == C0_STX) {
|
|
1931
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "a name cannot be text");
|
|
1932
|
+
return stop;
|
|
1559
1933
|
}
|
|
1934
|
+
*ns = pos;
|
|
1935
|
+
pos = c0__plain(buf, pos, stop);
|
|
1936
|
+
e = c0__trim_end(buf, *ns, pos);
|
|
1937
|
+
if (e != pos) c0__p_nc(p);
|
|
1938
|
+
*ne = e;
|
|
1560
1939
|
return pos;
|
|
1561
1940
|
}
|
|
1562
1941
|
|
|
1563
|
-
/* After
|
|
1564
|
-
*
|
|
1565
|
-
|
|
1942
|
+
/* After SOH RS: names, each followed by notes, separated by US. */
|
|
1943
|
+
static size_t c0__p_header_row(c0__parser *p, size_t pos, size_t stop) {
|
|
1944
|
+
const uint8_t *buf = p->buf;
|
|
1945
|
+
size_t ns, ne;
|
|
1566
1946
|
for (;;) {
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
if (
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1947
|
+
pos = c0__p_name(p, pos, stop, &ns, &ne);
|
|
1948
|
+
if (C0__P_ERR(p)) return stop;
|
|
1949
|
+
pos = c0__p_trailing_notes(p, c0__p_layout(p, pos, stop), stop);
|
|
1950
|
+
if (C0__P_ERR(p)) return stop;
|
|
1951
|
+
if (!(pos < stop && buf[pos] == C0_US)) break;
|
|
1952
|
+
pos++;
|
|
1953
|
+
}
|
|
1954
|
+
if (pos < stop && buf[pos] == C0_SUB) {
|
|
1955
|
+
size_t run, p2;
|
|
1956
|
+
c0__sub_run(buf, pos, stop, &run, &p2);
|
|
1957
|
+
if (p2 < stop && buf[p2] == C0_US) {
|
|
1958
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "a header row admits no sub-units");
|
|
1959
|
+
return stop;
|
|
1960
|
+
}
|
|
1961
|
+
}
|
|
1962
|
+
return pos;
|
|
1963
|
+
}
|
|
1964
|
+
|
|
1965
|
+
/* One unit's value, possibly empty, with the notes after it. Returns the
|
|
1966
|
+
* position of the next code at this depth. */
|
|
1967
|
+
static size_t c0__p_value(c0__parser *p, size_t pos, size_t stop, int heading) {
|
|
1968
|
+
const uint8_t *buf = p->buf;
|
|
1969
|
+
size_t after;
|
|
1970
|
+
uint8_t b;
|
|
1971
|
+
pos = c0__p_layout(p, pos, stop);
|
|
1972
|
+
if (pos >= stop) return pos;
|
|
1973
|
+
b = buf[pos];
|
|
1974
|
+
if (b >= 0x20) {
|
|
1975
|
+
pos = c0__plain(buf, pos, stop);
|
|
1976
|
+
if (buf[pos - 1] == C0_SP) c0__p_nc(p);
|
|
1977
|
+
} else if (b == C0_STX) {
|
|
1978
|
+
size_t start = pos;
|
|
1979
|
+
pos = c0__text(buf, pos, stop, &p->err);
|
|
1980
|
+
if (C0__P_ERR(p)) return stop;
|
|
1981
|
+
if (p->canonical) {
|
|
1982
|
+
size_t n = pos - start - 2, i;
|
|
1983
|
+
int plain = n > 0 && !c0_is_layout(buf[start + 1]) && !c0_is_layout(buf[pos - 2]);
|
|
1984
|
+
for (i = 0; plain && i < n; i++) {
|
|
1985
|
+
if (buf[start + 1 + i] < 0x20) plain = 0; /* a control byte, or a DLE escaping one */
|
|
1578
1986
|
}
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1987
|
+
if (n == 0 || plain) c0__p_nc(p); /* empty or gratuitous text */
|
|
1988
|
+
}
|
|
1989
|
+
} else if (b == C0_DLE) {
|
|
1990
|
+
pos = c0__p_level(p, pos, stop);
|
|
1991
|
+
if (C0__P_ERR(p)) return stop;
|
|
1992
|
+
} else if (b == C0_ENQ) {
|
|
1993
|
+
if (heading) {
|
|
1994
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "reference in a heading");
|
|
1995
|
+
return stop;
|
|
1996
|
+
}
|
|
1997
|
+
pos = c0__p_reference(p, pos, stop);
|
|
1998
|
+
if (C0__P_ERR(p)) return stop;
|
|
1999
|
+
} else if (b == C0_BEL) {
|
|
2000
|
+
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2001
|
+
uint8_t c;
|
|
2002
|
+
if (p2 >= stop) {
|
|
2003
|
+
c0__p_fail(p, C0_ERR_UNEXPECTED_END, p2, "BEL at end of input");
|
|
2004
|
+
return stop;
|
|
2005
|
+
}
|
|
2006
|
+
c = buf[p2];
|
|
2007
|
+
if (c == C0_DLE) {
|
|
2008
|
+
c0__p_nc(p); /* a dead level: the unit is empty */
|
|
2009
|
+
pos = c0__p_level(p, p2, stop);
|
|
2010
|
+
if (C0__P_ERR(p)) return stop;
|
|
2011
|
+
} else if (c >= 0x20 || c == C0_STX) {
|
|
2012
|
+
pos = c0__p_trailing_notes(p, pos, stop);
|
|
2013
|
+
if (C0__P_ERR(p)) return stop;
|
|
2014
|
+
if (c0__value_start(buf, pos, stop)) {
|
|
2015
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "value after a note in the same unit");
|
|
2016
|
+
return stop;
|
|
1583
2017
|
}
|
|
1584
|
-
pos = c0__unit(buf, pos + 1, len, err);
|
|
1585
|
-
if (err->code) return len;
|
|
1586
|
-
} else if (b == C0_BEL) {
|
|
1587
|
-
pos = c0__comment(buf, pos, len, err);
|
|
1588
|
-
if (err->code) return len;
|
|
1589
|
-
} else if (b >= 0x20) {
|
|
1590
|
-
c0__fail(err, C0_ERR_SYNTAX, pos, "plain text directly after SYN");
|
|
1591
|
-
return len;
|
|
1592
|
-
} else {
|
|
1593
2018
|
return pos;
|
|
2019
|
+
} else {
|
|
2020
|
+
return pos; /* a dead element after this empty unit: the caller's business */
|
|
1594
2021
|
}
|
|
2022
|
+
} else {
|
|
2023
|
+
return pos; /* an empty unit */
|
|
1595
2024
|
}
|
|
2025
|
+
after = c0__p_trailing_notes(p, c0__p_layout(p, pos, stop), stop);
|
|
2026
|
+
if (C0__P_ERR(p)) return stop;
|
|
2027
|
+
if (c0__value_start(buf, after, stop)) {
|
|
2028
|
+
c0__p_fail(p, C0_ERR_SYNTAX, after, "two values in one unit");
|
|
2029
|
+
return stop;
|
|
2030
|
+
}
|
|
2031
|
+
return after;
|
|
1596
2032
|
}
|
|
1597
2033
|
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
2034
|
+
/* After SOH (not a header row): key, US, value. *ks / *ke get the key. */
|
|
2035
|
+
static size_t c0__p_keyed_unit(c0__parser *p, size_t pos, size_t stop, int heading, size_t *ks, size_t *ke, size_t *vs) {
|
|
2036
|
+
pos = c0__p_name(p, pos, stop, ks, ke);
|
|
2037
|
+
if (C0__P_ERR(p)) return stop;
|
|
2038
|
+
if (!(pos < stop && p->buf[pos] == C0_US)) {
|
|
2039
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "key without a value (no US)");
|
|
2040
|
+
return stop;
|
|
2041
|
+
}
|
|
2042
|
+
*vs = pos + 1;
|
|
2043
|
+
return c0__p_value(p, *vs, stop, heading);
|
|
1601
2044
|
}
|
|
1602
2045
|
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
pos
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
if (
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
out->warnings |= C0_WARN_NO_SYN;
|
|
1621
|
-
out->heading_start = out->heading_end = pos;
|
|
1622
|
-
}
|
|
1623
|
-
out->body.buf = buf;
|
|
1624
|
-
out->body.start = pos;
|
|
1625
|
-
out->body.stop = c0__body_end(buf, pos, len, err);
|
|
1626
|
-
if (err->code) return err->code;
|
|
1627
|
-
pos = c0__layout(buf, out->body.stop, len);
|
|
1628
|
-
out->has_eot = pos < len && buf[pos] == C0_EOT;
|
|
1629
|
-
if (out->has_eot) {
|
|
1630
|
-
pos++;
|
|
1631
|
-
} else if (!c0__eq_str(c0_heading_shape(out), "stream")) {
|
|
1632
|
-
out->warnings |= C0_WARN_NO_EOT;
|
|
2046
|
+
/* Does the record starting here begin with a keyed unit, live or dead? */
|
|
2047
|
+
static int c0__p_keyed_start(c0__parser *p, size_t pos, size_t stop) {
|
|
2048
|
+
const uint8_t *buf = p->buf;
|
|
2049
|
+
for (;;) {
|
|
2050
|
+
uint8_t b;
|
|
2051
|
+
size_t p2;
|
|
2052
|
+
pos = c0__layout(buf, pos, stop);
|
|
2053
|
+
if (pos >= stop) return 0;
|
|
2054
|
+
b = buf[pos];
|
|
2055
|
+
if (b == C0_SOH) return !c0__soh_is_header(buf, pos, stop);
|
|
2056
|
+
if (b != C0_BEL) return 0;
|
|
2057
|
+
p2 = c0__layout(buf, pos + 1, stop);
|
|
2058
|
+
if (p2 >= stop) return 0;
|
|
2059
|
+
if (buf[p2] == C0_SOH) return !c0__soh_is_header(buf, p2, stop);
|
|
2060
|
+
if (!(buf[p2] >= 0x20 || buf[p2] == C0_STX)) return 0;
|
|
2061
|
+
pos = c0__p_note_content(p, p2, stop); /* a note: look past it */
|
|
2062
|
+
if (C0__P_ERR(p)) return 0;
|
|
1633
2063
|
}
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
2064
|
+
}
|
|
2065
|
+
|
|
2066
|
+
/* The sub-units after a unit's value at pos: runs of SUB, each then US and
|
|
2067
|
+
* a value, the run's length the sub-unit's depth, at most one deeper than
|
|
2068
|
+
* the sub-unit before it. Returns the position after the last. A SUB run
|
|
2069
|
+
* that prefixes a line code (RS, GS, FS) is left for the body, which
|
|
2070
|
+
* refuses the reserved grains; the other reserved or stray SUBs fail here. */
|
|
2071
|
+
static size_t c0__p_sub_units(c0__parser *p, size_t pos, size_t stop) {
|
|
2072
|
+
const uint8_t *buf = p->buf;
|
|
2073
|
+
size_t prev = 0;
|
|
2074
|
+
for (;;) {
|
|
2075
|
+
size_t start, run = 0, nx;
|
|
2076
|
+
uint8_t c;
|
|
2077
|
+
start = c0__p_layout(p, pos, stop);
|
|
2078
|
+
if (!(start < stop && buf[start] == C0_SUB)) return pos;
|
|
2079
|
+
nx = start;
|
|
2080
|
+
while (nx < stop && buf[nx] == C0_SUB) {
|
|
2081
|
+
run++;
|
|
2082
|
+
nx = c0__p_layout(p, nx + 1, stop);
|
|
1650
2083
|
}
|
|
1651
|
-
if (
|
|
1652
|
-
|
|
1653
|
-
return
|
|
2084
|
+
if (nx >= stop) {
|
|
2085
|
+
c0__p_fail(p, C0_ERR_SYNTAX, start, "SUB at the end of input");
|
|
2086
|
+
return stop;
|
|
2087
|
+
}
|
|
2088
|
+
c = buf[nx];
|
|
2089
|
+
if (c == C0_US) {
|
|
2090
|
+
if (run > prev + 1) {
|
|
2091
|
+
c0__p_fail(p, C0_ERR_SYNTAX, start, "depth skipped: a sub-unit is at most one deeper than the sub-unit before it");
|
|
2092
|
+
return stop;
|
|
2093
|
+
}
|
|
2094
|
+
prev = run;
|
|
2095
|
+
pos = c0__p_value(p, nx + 1, stop, 0);
|
|
2096
|
+
if (C0__P_ERR(p)) return stop;
|
|
2097
|
+
} else if (c == C0_RS || c == C0_GS || c == C0_FS) {
|
|
2098
|
+
return pos; /* a prefix before the next line: the body's business */
|
|
2099
|
+
} else if (c == C0_SOH) {
|
|
2100
|
+
if (c0__soh_is_header(buf, nx, stop)) c0__p_fail(p, C0_ERR_SYNTAX, start, "a header row admits no SUB");
|
|
2101
|
+
else c0__p_fail(p, C0_ERR_SYNTAX, start, "SUB before SOH is reserved (a sub-key)");
|
|
2102
|
+
return stop;
|
|
2103
|
+
} else if (c == C0_BEL) {
|
|
2104
|
+
c0__p_fail(p, C0_ERR_SYNTAX, start, "SUB before BEL");
|
|
2105
|
+
return stop;
|
|
2106
|
+
} else {
|
|
2107
|
+
c0__p_fail(p, C0_ERR_SYNTAX, start, "SUB before a byte that is not a separator");
|
|
2108
|
+
return stop;
|
|
1654
2109
|
}
|
|
1655
2110
|
}
|
|
1656
|
-
return C0_OK;
|
|
1657
2111
|
}
|
|
1658
2112
|
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
2113
|
+
static size_t c0__p_positional(c0__parser *p, size_t pos, size_t stop) {
|
|
2114
|
+
const uint8_t *buf = p->buf;
|
|
2115
|
+
for (;;) {
|
|
2116
|
+
uint8_t b;
|
|
2117
|
+
pos = c0__p_value(p, pos, stop, 0);
|
|
2118
|
+
if (C0__P_ERR(p)) return stop;
|
|
2119
|
+
pos = c0__p_sub_units(p, pos, stop);
|
|
2120
|
+
if (C0__P_ERR(p)) return stop;
|
|
2121
|
+
pos = c0__p_layout(p, pos, stop);
|
|
2122
|
+
if (pos >= stop) break;
|
|
2123
|
+
b = buf[pos];
|
|
2124
|
+
if (b == C0_US) {
|
|
2125
|
+
pos++;
|
|
2126
|
+
} else if (b == C0_SUB) {
|
|
2127
|
+
break; /* a prefix before the next line: the body's business */
|
|
2128
|
+
} else if (b == C0_SOH) {
|
|
2129
|
+
if (c0__soh_is_header(buf, pos, stop)) break; /* a header row ends the record */
|
|
2130
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "keyed unit in a positional record (mixed record)");
|
|
2131
|
+
return stop;
|
|
2132
|
+
} else {
|
|
2133
|
+
break;
|
|
2134
|
+
}
|
|
2135
|
+
}
|
|
2136
|
+
return pos;
|
|
1677
2137
|
}
|
|
1678
2138
|
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
2139
|
+
/* Keys in [start, stop) must be unique (any order): canonical only. */
|
|
2140
|
+
static void c0__p_check_keys(c0__parser *p, size_t start, size_t stop) {
|
|
2141
|
+
c0_pair_iter a = c0__pairs(p->buf, start, stop);
|
|
2142
|
+
c0_bytes k, k2, raw;
|
|
2143
|
+
while (c0_next_pair(&a, &k, &raw)) {
|
|
2144
|
+
c0_pair_iter rest = a;
|
|
2145
|
+
while (c0_next_pair(&rest, &k2, &raw)) {
|
|
2146
|
+
if (k2.len == k.len && (k.len == 0 || memcmp(k.ptr, k2.ptr, k.len) == 0)) c0__p_nc(p);
|
|
2147
|
+
}
|
|
1688
2148
|
}
|
|
1689
|
-
return n;
|
|
1690
2149
|
}
|
|
1691
2150
|
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
2151
|
+
static size_t c0__p_keyed(c0__parser *p, size_t pos, size_t stop) {
|
|
2152
|
+
const uint8_t *buf = p->buf;
|
|
2153
|
+
size_t start = pos, ks, ke, vs;
|
|
2154
|
+
for (;;) {
|
|
2155
|
+
uint8_t b;
|
|
2156
|
+
pos = c0__p_layout(p, pos, stop);
|
|
2157
|
+
if (pos >= stop) break;
|
|
2158
|
+
b = buf[pos];
|
|
2159
|
+
if (b == C0_SOH) {
|
|
2160
|
+
if (c0__soh_is_header(buf, pos, stop)) break;
|
|
2161
|
+
pos = c0__p_keyed_unit(p, pos + 1, stop, 0, &ks, &ke, &vs);
|
|
2162
|
+
if (C0__P_ERR(p)) return stop;
|
|
2163
|
+
pos = c0__p_sub_units(p, pos, stop);
|
|
2164
|
+
if (C0__P_ERR(p)) return stop;
|
|
2165
|
+
} else if (b == C0_BEL) {
|
|
2166
|
+
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2167
|
+
uint8_t c;
|
|
2168
|
+
if (p2 >= stop) {
|
|
2169
|
+
c0__p_fail(p, C0_ERR_UNEXPECTED_END, p2, "BEL at end of input");
|
|
2170
|
+
return stop;
|
|
2171
|
+
}
|
|
2172
|
+
c = buf[p2];
|
|
2173
|
+
if (c == C0_SOH) {
|
|
2174
|
+
if (c0__soh_is_header(buf, p2, stop)) break;
|
|
2175
|
+
c0__p_nc(p); /* a dead keyed unit */
|
|
2176
|
+
pos = c0__p_keyed_unit(p, p2 + 1, stop, 0, &ks, &ke, &vs);
|
|
2177
|
+
if (C0__P_ERR(p)) return stop;
|
|
2178
|
+
pos = c0__p_sub_units(p, pos, stop);
|
|
2179
|
+
if (C0__P_ERR(p)) return stop;
|
|
2180
|
+
} else if (c >= 0x20 || c == C0_STX) {
|
|
2181
|
+
pos = c0__p_trailing_notes(p, pos, stop);
|
|
2182
|
+
if (C0__P_ERR(p)) return stop;
|
|
2183
|
+
} else if (c == C0_SUB) {
|
|
2184
|
+
c0__p_fail(p, C0_ERR_SYNTAX, p2, "BEL before a sub-unit: a unit cannot be dead");
|
|
2185
|
+
return stop;
|
|
2186
|
+
} else {
|
|
2187
|
+
break;
|
|
2188
|
+
}
|
|
2189
|
+
} else if (b == C0_SUB) {
|
|
2190
|
+
break; /* a prefix before the next line: the body's business */
|
|
2191
|
+
} else {
|
|
2192
|
+
break;
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
if (pos < stop && buf[pos] == C0_US) {
|
|
2196
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "US after a keyed value (mixed record)");
|
|
2197
|
+
return stop;
|
|
2198
|
+
}
|
|
2199
|
+
if (p->canonical) c0__p_check_keys(p, start, pos);
|
|
2200
|
+
return pos;
|
|
1697
2201
|
}
|
|
1698
2202
|
|
|
1699
|
-
/*
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
c0_doc_iter it;
|
|
1703
|
-
it.buf = buf;
|
|
1704
|
-
it.len = len;
|
|
1705
|
-
it.pos = 0;
|
|
1706
|
-
it.has_em = 0;
|
|
1707
|
-
it.em_pos = len;
|
|
1708
|
-
it.done = 0;
|
|
1709
|
-
c0__err_clear(&it.err);
|
|
1710
|
-
return it;
|
|
2203
|
+
/* After RS. Returns the position after the record. */
|
|
2204
|
+
static size_t c0__p_record(c0__parser *p, size_t pos, size_t stop) {
|
|
2205
|
+
return c0__p_keyed_start(p, pos, stop) ? c0__p_keyed(p, pos, stop) : c0__p_positional(p, pos, stop);
|
|
1711
2206
|
}
|
|
1712
2207
|
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
it->has_em = 1;
|
|
1723
|
-
it->em_pos = pos;
|
|
1724
|
-
it->done = 1;
|
|
1725
|
-
return 0;
|
|
1726
|
-
}
|
|
1727
|
-
if (it->pos > 0 && it->buf[pos] != C0_SYN) {
|
|
1728
|
-
c0__fail(&it->err, C0_ERR_SYNTAX, pos, "a document after the first must begin with SYN");
|
|
1729
|
-
it->done = 1;
|
|
1730
|
-
return 0;
|
|
2208
|
+
/* DLE at pos: a nested level. Returns the position after its CAN. */
|
|
2209
|
+
static size_t c0__p_level(c0__parser *p, size_t pos, size_t stop) {
|
|
2210
|
+
const uint8_t *buf = p->buf;
|
|
2211
|
+
size_t start = pos;
|
|
2212
|
+
uint8_t b;
|
|
2213
|
+
c0_parse_state st;
|
|
2214
|
+
if (++p->depth > C0__MAX_DEPTH) {
|
|
2215
|
+
c0__p_fail(p, C0_ERR_TOO_DEEP, pos, "levels nested too deep");
|
|
2216
|
+
return stop;
|
|
1731
2217
|
}
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
2218
|
+
pos = c0__p_layout(p, pos + 1, stop);
|
|
2219
|
+
if (pos >= stop) {
|
|
2220
|
+
c0__p_fail(p, C0_ERR_UNEXPECTED_END, start, "level is not closed");
|
|
2221
|
+
return stop;
|
|
1735
2222
|
}
|
|
1736
|
-
|
|
1737
|
-
if (
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
return 0;
|
|
2223
|
+
b = buf[pos];
|
|
2224
|
+
if (b == C0_CAN) {
|
|
2225
|
+
p->depth--;
|
|
2226
|
+
return pos + 1;
|
|
1741
2227
|
}
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
c0_bytes c0_doc_iter_tail(const c0_doc_iter *it) {
|
|
1746
|
-
c0_bytes t;
|
|
1747
|
-
t.ptr = it->buf + it->len;
|
|
1748
|
-
t.len = 0;
|
|
1749
|
-
if (it->has_em) {
|
|
1750
|
-
t.ptr = it->buf + it->em_pos + 1;
|
|
1751
|
-
t.len = it->len - it->em_pos - 1;
|
|
2228
|
+
if (!(b == C0_FS || b == C0_GS || b == C0_RS || b == C0_SOH || b == C0_BEL || b == C0_SUB)) {
|
|
2229
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "DLE must be followed by a structural code");
|
|
2230
|
+
return stop;
|
|
1752
2231
|
}
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
2232
|
+
c0__st_body_reset(&st, 1);
|
|
2233
|
+
pos = c0__p_body(p, &st, pos, stop);
|
|
2234
|
+
if (C0__P_ERR(p)) return stop;
|
|
2235
|
+
pos = c0__p_layout(p, pos, stop);
|
|
2236
|
+
if (pos >= stop) {
|
|
2237
|
+
c0__p_fail(p, C0_ERR_UNEXPECTED_END, start, "level is not closed");
|
|
2238
|
+
return stop;
|
|
1760
2239
|
}
|
|
1761
|
-
if (
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
return it.err.code;
|
|
1765
|
-
}
|
|
1766
|
-
|
|
1767
|
-
static int c0__to_size(c0_bytes v, size_t *out) {
|
|
1768
|
-
size_t i, n = 0;
|
|
1769
|
-
if (v.len == 0) return 0;
|
|
1770
|
-
for (i = 0; i < v.len; i++) {
|
|
1771
|
-
if (v.ptr[i] < '0' || v.ptr[i] > '9') return 0;
|
|
1772
|
-
n = n * 10 + (size_t)(v.ptr[i] - '0');
|
|
2240
|
+
if (buf[pos] != C0_CAN) {
|
|
2241
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "unexpected byte inside level");
|
|
2242
|
+
return stop;
|
|
1773
2243
|
}
|
|
1774
|
-
|
|
1775
|
-
return 1;
|
|
2244
|
+
p->depth--;
|
|
2245
|
+
return pos + 1;
|
|
1776
2246
|
}
|
|
1777
2247
|
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
2248
|
+
/* A pattern (DESIGN.md, References) is one branch of the target with its
|
|
2249
|
+
* values left out: the level [start, stop) has been parsed; walk it with
|
|
2250
|
+
* the cursors. `inner` is a pattern continued into a level, whose records
|
|
2251
|
+
* may stand in the unnamed group. */
|
|
2252
|
+
static void c0__p_check_pattern(c0__parser *p, const uint8_t *buf, size_t start, size_t stop, int inner, size_t at) {
|
|
2253
|
+
c0_body body;
|
|
2254
|
+
c0_file_iter fi;
|
|
2255
|
+
c0_file f;
|
|
1782
2256
|
c0_group_iter gi;
|
|
1783
|
-
c0_group g;
|
|
2257
|
+
c0_group g, last;
|
|
1784
2258
|
c0_record_iter ri;
|
|
1785
|
-
c0_record r;
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
2259
|
+
c0_record r, h;
|
|
2260
|
+
size_t ngroups = 0, prev = 0, nrecs = 0, i;
|
|
2261
|
+
int have_prev = 0;
|
|
2262
|
+
memset(&last, 0, sizeof(last));
|
|
2263
|
+
body.buf = buf;
|
|
2264
|
+
body.start = start;
|
|
2265
|
+
body.stop = stop;
|
|
2266
|
+
fi = c0_body_files(body);
|
|
2267
|
+
if (!c0_next_file(&fi, &f)) {
|
|
2268
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, inner ? "an empty level continues nothing" : "an empty level is not a reference");
|
|
2269
|
+
return;
|
|
1793
2270
|
}
|
|
1794
|
-
{
|
|
1795
|
-
|
|
1796
|
-
|
|
2271
|
+
if (c0_next_file(&fi, &f)) {
|
|
2272
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a pattern is one branch: two files");
|
|
2273
|
+
return;
|
|
2274
|
+
}
|
|
2275
|
+
gi = c0_file_groups(f);
|
|
2276
|
+
while (c0_next_group(&gi, &g)) {
|
|
2277
|
+
if (ngroups == 0 && !inner && g.depth == 0) {
|
|
2278
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a pattern begins with a file or group line");
|
|
2279
|
+
return;
|
|
2280
|
+
}
|
|
2281
|
+
if (c0_group_header(g, &h)) {
|
|
2282
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a header row is not a step of a pattern");
|
|
2283
|
+
return;
|
|
2284
|
+
}
|
|
2285
|
+
if (have_prev && g.depth <= prev) {
|
|
2286
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a pattern is one branch: two groups at one depth");
|
|
2287
|
+
return;
|
|
2288
|
+
}
|
|
2289
|
+
if (ngroups > 0) { /* the group before holds no record: nothing follows a record */
|
|
2290
|
+
ri = c0_group_records(last);
|
|
2291
|
+
if (c0_next_record(&ri, &r)) {
|
|
2292
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "nothing follows the record in a pattern");
|
|
2293
|
+
return;
|
|
2294
|
+
}
|
|
1797
2295
|
}
|
|
2296
|
+
prev = g.depth;
|
|
2297
|
+
have_prev = 1;
|
|
2298
|
+
last = g;
|
|
2299
|
+
ngroups++;
|
|
1798
2300
|
}
|
|
1799
|
-
if (
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
tail = c0_doc_iter_tail(&it);
|
|
1803
|
-
st = c0_heading_blob(&first, &table);
|
|
1804
|
-
if (st) return st;
|
|
1805
|
-
gi = c0_body_groups(table);
|
|
1806
|
-
if (!c0_next_group(&gi, &g)) return C0_ERR_NOT_FOUND;
|
|
1807
|
-
c0_group_column(g, (const uint8_t *)"name", 4, &ni);
|
|
1808
|
-
c0_group_column(g, (const uint8_t *)"offset", 6, &oi);
|
|
1809
|
-
c0_group_column(g, (const uint8_t *)"length", 6, &li);
|
|
1810
|
-
ri = c0_group_records(g);
|
|
1811
|
-
while (c0_next_record(&ri, &r)) {
|
|
1812
|
-
c0_bytes raw, v;
|
|
1813
|
-
size_t off, length;
|
|
1814
|
-
if (c0_record_field(r, ni, &raw) || c0_value(raw, NULL, &v)) continue;
|
|
1815
|
-
if (!(v.len == nlen && (nlen == 0 || memcmp(v.ptr, name, nlen) == 0))) continue;
|
|
1816
|
-
if (c0_record_field(r, oi, &raw) || c0_value(raw, NULL, &v) || !c0__to_size(v, &off)) continue;
|
|
1817
|
-
if (c0_record_field(r, li, &raw) || c0_value(raw, NULL, &v) || !c0__to_size(v, &length)) continue;
|
|
1818
|
-
if (off > tail.len || length > tail.len - off) continue;
|
|
1819
|
-
out->ptr = tail.ptr + off;
|
|
1820
|
-
out->len = length;
|
|
1821
|
-
found = 1; /* last wins */
|
|
2301
|
+
if (ngroups == 0) {
|
|
2302
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, inner ? "an empty level continues nothing" : "an empty level is not a reference");
|
|
2303
|
+
return;
|
|
1822
2304
|
}
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
}
|
|
1829
|
-
|
|
1830
|
-
/*
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
if (
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
2305
|
+
ri = c0_group_records(last);
|
|
2306
|
+
while (c0_next_record(&ri, &r)) nrecs++;
|
|
2307
|
+
if (nrecs > 1) {
|
|
2308
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a pattern is one branch: two records");
|
|
2309
|
+
return;
|
|
2310
|
+
}
|
|
2311
|
+
if (nrecs == 0) {
|
|
2312
|
+
if (!inner) c0__p_nc(p); /* a group reference written as a pattern */
|
|
2313
|
+
return;
|
|
2314
|
+
}
|
|
2315
|
+
ri = c0_group_records(last);
|
|
2316
|
+
c0_next_record(&ri, &r);
|
|
2317
|
+
if (c0_record_keyed(r)) {
|
|
2318
|
+
c0_pair_iter pi = c0_record_pairs(r);
|
|
2319
|
+
c0_bytes k, val;
|
|
2320
|
+
c0_unit u;
|
|
2321
|
+
size_t n = 0;
|
|
2322
|
+
while (c0_next_pair_with_subs(&pi, &k, &u)) {
|
|
2323
|
+
n++;
|
|
2324
|
+
if (n > 1) {
|
|
2325
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a keyed record in a pattern holds one keyed unit");
|
|
2326
|
+
return;
|
|
2327
|
+
}
|
|
2328
|
+
if (u.subs_end != u.subs_start) {
|
|
2329
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a sub-unit is not a step of a pattern");
|
|
2330
|
+
return;
|
|
2331
|
+
}
|
|
2332
|
+
if (c0_value(u.raw, NULL, &val) != C0_OK || val.len != 0) {
|
|
2333
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a keyed unit in a pattern has no value");
|
|
2334
|
+
return;
|
|
2335
|
+
}
|
|
2336
|
+
if (k.len == 0 || !c0_is_name(k.ptr, k.len)) {
|
|
2337
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a step of a pattern is a name");
|
|
2338
|
+
return;
|
|
2339
|
+
}
|
|
2340
|
+
}
|
|
2341
|
+
if (n == 0) c0__p_fail(p, C0_ERR_SYNTAX, at, "a keyed record in a pattern holds one keyed unit");
|
|
2342
|
+
return;
|
|
2343
|
+
}
|
|
2344
|
+
{
|
|
2345
|
+
c0_unit_iter ui = c0_record_units(r);
|
|
2346
|
+
c0_unit u;
|
|
2347
|
+
c0_bytes v;
|
|
2348
|
+
for (i = 0; c0_next_unit_with_subs(&ui, &u); i++) {
|
|
2349
|
+
c0_bytes t = c0_trim(u.raw);
|
|
2350
|
+
if (i >= 3) {
|
|
2351
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a pattern record holds at most a record id, a field name and a level");
|
|
2352
|
+
return;
|
|
2353
|
+
}
|
|
2354
|
+
if (u.subs_end != u.subs_start) {
|
|
2355
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a sub-unit is not a step of a pattern");
|
|
2356
|
+
return;
|
|
2357
|
+
}
|
|
2358
|
+
if (i == 2) {
|
|
2359
|
+
c0_body lv;
|
|
2360
|
+
if (!(t.len > 0 && t.ptr[0] == C0_DLE) || c0_unit_level(t, &lv) != C0_OK) {
|
|
2361
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "the third unit of a pattern record is a level");
|
|
2362
|
+
return;
|
|
2363
|
+
}
|
|
2364
|
+
c0__p_check_pattern(p, lv.buf, lv.start, lv.stop, 1, at);
|
|
2365
|
+
if (C0__P_ERR(p)) return;
|
|
2366
|
+
} else {
|
|
2367
|
+
if (t.len > 0 && (t.ptr[0] == C0_DLE || t.ptr[0] == C0_ENQ || t.ptr[0] == C0_STX)) {
|
|
2368
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a step of a pattern is a name");
|
|
2369
|
+
return;
|
|
2370
|
+
}
|
|
2371
|
+
if (c0_value(t, NULL, &v) != C0_OK || v.len == 0 || !c0_is_name(v.ptr, v.len)) {
|
|
2372
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a step of a pattern is a name");
|
|
2373
|
+
return;
|
|
2374
|
+
}
|
|
2375
|
+
}
|
|
1844
2376
|
}
|
|
1845
|
-
b->data = nd;
|
|
1846
|
-
b->cap = ncap;
|
|
1847
2377
|
}
|
|
1848
|
-
return 1;
|
|
1849
2378
|
}
|
|
1850
2379
|
|
|
1851
|
-
|
|
1852
|
-
|
|
2380
|
+
/* ENQ at pos: a group name, or a pattern. Returns the position after it. */
|
|
2381
|
+
static size_t c0__p_reference(c0__parser *p, size_t pos, size_t stop) {
|
|
2382
|
+
const uint8_t *buf = p->buf;
|
|
2383
|
+
size_t at = pos;
|
|
2384
|
+
uint8_t b;
|
|
2385
|
+
pos = c0__p_layout(p, pos + 1, stop);
|
|
2386
|
+
if (pos >= stop) {
|
|
2387
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "reference without a name");
|
|
2388
|
+
return stop;
|
|
2389
|
+
}
|
|
2390
|
+
b = buf[pos];
|
|
2391
|
+
if (b == C0_DLE) {
|
|
2392
|
+
size_t dead0 = p->dead_elements, after;
|
|
2393
|
+
after = c0__p_level(p, pos, stop);
|
|
2394
|
+
if (C0__P_ERR(p)) return stop;
|
|
2395
|
+
if (p->dead_elements != dead0) {
|
|
2396
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "a dead element is not a step of a pattern");
|
|
2397
|
+
return stop;
|
|
2398
|
+
}
|
|
2399
|
+
c0__p_check_pattern(p, buf, pos + 1, after - 1, 0, pos);
|
|
2400
|
+
return C0__P_ERR(p) ? stop : after;
|
|
2401
|
+
}
|
|
2402
|
+
if (b == C0_STX) {
|
|
2403
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "a reference is a name or a pattern, not text");
|
|
2404
|
+
return stop;
|
|
2405
|
+
}
|
|
2406
|
+
if (b >= 0x20) {
|
|
2407
|
+
size_t ns, ne;
|
|
2408
|
+
pos = c0__p_name(p, pos, stop, &ns, &ne);
|
|
2409
|
+
if (ne == ns) {
|
|
2410
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "reference without a name");
|
|
2411
|
+
return stop;
|
|
2412
|
+
}
|
|
2413
|
+
return pos;
|
|
2414
|
+
}
|
|
2415
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "reference without a name");
|
|
2416
|
+
return stop;
|
|
1853
2417
|
}
|
|
1854
2418
|
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
2419
|
+
/* The body from pos to stop. At depth zero commits are legal and the body
|
|
2420
|
+
* ends at EOT, SYN, EM or stop; in a level it ends at CAN. Returns the
|
|
2421
|
+
* position of the code that ended it (or stop). */
|
|
2422
|
+
static size_t c0__p_body(c0__parser *p, c0_parse_state *s, size_t pos, size_t stop) {
|
|
2423
|
+
const uint8_t *buf = p->buf;
|
|
2424
|
+
int dead = 0; /* BEL seen: the next element is dead */
|
|
2425
|
+
size_t prefix = 0; /* SUB run read before a GS: the subsection's depth less one */
|
|
2426
|
+
size_t mark_at = pos;
|
|
2427
|
+
for (;;) {
|
|
2428
|
+
uint8_t b;
|
|
2429
|
+
pos = c0__p_layout(p, pos, stop);
|
|
2430
|
+
if (pos >= stop) break;
|
|
2431
|
+
b = buf[pos];
|
|
2432
|
+
switch (b) {
|
|
2433
|
+
case C0_RS:
|
|
2434
|
+
pos = c0__p_record(p, pos + 1, stop);
|
|
2435
|
+
if (C0__P_ERR(p)) return stop;
|
|
2436
|
+
if (dead) {
|
|
2437
|
+
p->dead_elements++;
|
|
2438
|
+
} else {
|
|
2439
|
+
if (!s->dead_file && !s->dead_depth) s->file_has_groups = 1;
|
|
2440
|
+
s->header_ok = 0;
|
|
2441
|
+
}
|
|
2442
|
+
dead = 0;
|
|
2443
|
+
break;
|
|
2444
|
+
case C0_SOH: {
|
|
2445
|
+
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2446
|
+
if (!(p2 < stop && buf[p2] == C0_RS)) {
|
|
2447
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "keyed unit outside a record");
|
|
2448
|
+
return stop;
|
|
2449
|
+
}
|
|
2450
|
+
if (!s->header_ok) {
|
|
2451
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "header row not at the start of its group");
|
|
2452
|
+
return stop;
|
|
2453
|
+
}
|
|
2454
|
+
pos = c0__p_header_row(p, p2 + 1, stop);
|
|
2455
|
+
if (C0__P_ERR(p)) return stop;
|
|
2456
|
+
if (dead) {
|
|
2457
|
+
p->dead_elements++;
|
|
2458
|
+
} else {
|
|
2459
|
+
if (!s->dead_file && !s->dead_depth) s->file_has_groups = 1;
|
|
2460
|
+
s->header_ok = 0;
|
|
2461
|
+
}
|
|
2462
|
+
dead = 0;
|
|
2463
|
+
break;
|
|
2464
|
+
}
|
|
2465
|
+
case C0_GS: {
|
|
2466
|
+
size_t depth = 1 + prefix, ns, ne;
|
|
2467
|
+
prefix = 0;
|
|
2468
|
+
if (depth >= C0__GS_DEPTH_MAX) {
|
|
2469
|
+
c0__p_fail(p, C0_ERR_TOO_DEEP, pos, "group depth too deep");
|
|
2470
|
+
return stop;
|
|
2471
|
+
}
|
|
2472
|
+
if (depth > (size_t)s->prev_depth + 1) {
|
|
2473
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "depth skipped: a group is at most one deeper than the group line before it");
|
|
2474
|
+
return stop;
|
|
2475
|
+
}
|
|
2476
|
+
s->prev_depth = (int)depth;
|
|
2477
|
+
pos = c0__p_name(p, pos + 1, stop, &ns, &ne);
|
|
2478
|
+
if (C0__P_ERR(p)) return stop;
|
|
2479
|
+
if (s->dead_depth && depth <= (size_t)s->dead_depth) s->dead_depth = 0; /* the dead group's span ends here */
|
|
2480
|
+
if (dead) {
|
|
2481
|
+
p->dead_elements++;
|
|
2482
|
+
if (!s->dead_file && !s->dead_depth) s->dead_depth = (int)depth;
|
|
2483
|
+
} else if (!s->dead_file && !s->dead_depth) {
|
|
2484
|
+
s->file_has_groups = 1;
|
|
2485
|
+
}
|
|
2486
|
+
s->header_ok = 1;
|
|
2487
|
+
dead = 0;
|
|
2488
|
+
break;
|
|
2489
|
+
}
|
|
2490
|
+
case C0_FS: {
|
|
2491
|
+
size_t ns, ne;
|
|
2492
|
+
pos = c0__p_name(p, pos + 1, stop, &ns, &ne);
|
|
2493
|
+
if (C0__P_ERR(p)) return stop;
|
|
2494
|
+
/* finish the file before: an empty leading file is dropped */
|
|
2495
|
+
if (!s->dead_file && (!s->leading || s->file_has_groups)) s->nfiles++;
|
|
2496
|
+
if (dead) {
|
|
2497
|
+
p->dead_elements++;
|
|
2498
|
+
s->dead_file = 1;
|
|
2499
|
+
} else {
|
|
2500
|
+
s->dead_file = 0;
|
|
2501
|
+
}
|
|
2502
|
+
s->leading = 0;
|
|
2503
|
+
s->file_has_groups = 0;
|
|
2504
|
+
s->dead_depth = 0;
|
|
2505
|
+
s->prev_depth = 0;
|
|
2506
|
+
s->header_ok = 1;
|
|
2507
|
+
dead = 0;
|
|
2508
|
+
break;
|
|
2509
|
+
}
|
|
2510
|
+
case C0_BEL: {
|
|
2511
|
+
size_t p2;
|
|
2512
|
+
uint8_t c;
|
|
2513
|
+
if (dead) {
|
|
2514
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "BEL before a code that is not an element");
|
|
2515
|
+
return stop;
|
|
2516
|
+
}
|
|
2517
|
+
p2 = c0__layout(buf, pos + 1, stop);
|
|
2518
|
+
if (p2 >= stop) {
|
|
2519
|
+
c0__p_fail(p, C0_ERR_UNEXPECTED_END, p2, "BEL at end of input");
|
|
2520
|
+
return stop;
|
|
2521
|
+
}
|
|
2522
|
+
c = buf[p2];
|
|
2523
|
+
if (c >= 0x20 || c == C0_STX) {
|
|
2524
|
+
c0__p_nc(p);
|
|
2525
|
+
pos = c0__p_note_content(p, p2, stop);
|
|
2526
|
+
if (C0__P_ERR(p)) return stop;
|
|
2527
|
+
} else if (c == C0_RS || c == C0_SOH || c == C0_GS || c == C0_FS) {
|
|
2528
|
+
c0__p_nc(p);
|
|
2529
|
+
dead = 1;
|
|
2530
|
+
mark_at = pos;
|
|
2531
|
+
pos = p2;
|
|
2532
|
+
} else if (c == C0_SUB) {
|
|
2533
|
+
/* BEL kills a group line whole, its SUB prefix included */
|
|
2534
|
+
size_t run, p3;
|
|
2535
|
+
c0__sub_run(buf, p2, stop, &run, &p3);
|
|
2536
|
+
if (p3 < stop && buf[p3] == C0_GS) {
|
|
2537
|
+
c0__p_nc(p);
|
|
2538
|
+
dead = 1;
|
|
2539
|
+
mark_at = pos;
|
|
2540
|
+
pos = p2;
|
|
2541
|
+
} else if (p3 < stop && buf[p3] == C0_US) {
|
|
2542
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "BEL before a sub-unit: a unit cannot be dead");
|
|
2543
|
+
return stop;
|
|
2544
|
+
} else {
|
|
2545
|
+
c0__p_fail(p, C0_ERR_SYNTAX, p2, "BEL before a code that is not an element");
|
|
2546
|
+
return stop;
|
|
2547
|
+
}
|
|
2548
|
+
} else if (c == C0_DLE) {
|
|
2549
|
+
c0__p_fail(p, C0_ERR_SYNTAX, p2, "a nested level is not an item");
|
|
2550
|
+
return stop;
|
|
2551
|
+
} else {
|
|
2552
|
+
c0__p_fail(p, C0_ERR_SYNTAX, p2, "BEL before a code that is not an element");
|
|
2553
|
+
return stop;
|
|
2554
|
+
}
|
|
2555
|
+
break;
|
|
2556
|
+
}
|
|
2557
|
+
case C0_SUB: {
|
|
2558
|
+
/* A run of SUB before GS is the subsection's depth prefix;
|
|
2559
|
+
* before RS, FS or SOH it is reserved; a sub-unit (SUB US)
|
|
2560
|
+
* lives inside a record and never reaches here. */
|
|
2561
|
+
size_t start = pos, run = 0;
|
|
2562
|
+
uint8_t c;
|
|
2563
|
+
while (pos < stop && buf[pos] == C0_SUB) {
|
|
2564
|
+
run++;
|
|
2565
|
+
pos = c0__p_layout(p, pos + 1, stop);
|
|
2566
|
+
}
|
|
2567
|
+
if (pos >= stop) {
|
|
2568
|
+
c0__p_fail(p, C0_ERR_SYNTAX, start, "SUB at the end of input");
|
|
2569
|
+
return stop;
|
|
2570
|
+
}
|
|
2571
|
+
c = buf[pos];
|
|
2572
|
+
if (c == C0_GS) {
|
|
2573
|
+
prefix = run;
|
|
2574
|
+
} else if (c == C0_RS) {
|
|
2575
|
+
c0__p_fail(p, C0_ERR_SYNTAX, start, "SUB before RS is reserved (a sub-record)");
|
|
2576
|
+
return stop;
|
|
2577
|
+
} else if (c == C0_FS) {
|
|
2578
|
+
c0__p_fail(p, C0_ERR_SYNTAX, start, "SUB before FS is reserved (a sub-file)");
|
|
2579
|
+
return stop;
|
|
2580
|
+
} else if (c == C0_SOH) {
|
|
2581
|
+
c0__p_fail(p, C0_ERR_SYNTAX, start, "SUB before SOH is reserved (a sub-key); a header row admits none");
|
|
2582
|
+
return stop;
|
|
2583
|
+
} else if (c == C0_US) {
|
|
2584
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "US outside a record");
|
|
2585
|
+
return stop;
|
|
2586
|
+
} else if (c == C0_BEL) {
|
|
2587
|
+
c0__p_fail(p, C0_ERR_SYNTAX, start, "SUB before BEL");
|
|
2588
|
+
return stop;
|
|
2589
|
+
} else {
|
|
2590
|
+
c0__p_fail(p, C0_ERR_SYNTAX, start, "SUB before a byte that is not a separator");
|
|
2591
|
+
return stop;
|
|
2592
|
+
}
|
|
2593
|
+
break;
|
|
2594
|
+
}
|
|
2595
|
+
case C0_ETB:
|
|
2596
|
+
if (s->in_level) {
|
|
2597
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "commit inside a nested level");
|
|
2598
|
+
return stop;
|
|
2599
|
+
}
|
|
2600
|
+
if (dead) {
|
|
2601
|
+
c0__p_fail(p, C0_ERR_SYNTAX, mark_at, "BEL before ETB: framing cannot be commented out");
|
|
2602
|
+
return stop;
|
|
2603
|
+
}
|
|
2604
|
+
c0__p_nc(p);
|
|
2605
|
+
pos = c0__plain(buf, c0__p_layout(p, pos + 1, stop), stop);
|
|
2606
|
+
break;
|
|
2607
|
+
case C0_EOT:
|
|
2608
|
+
case C0_SYN:
|
|
2609
|
+
case C0_EM:
|
|
2610
|
+
case C0_CAN:
|
|
2611
|
+
if (b == C0_CAN ? !s->in_level : s->in_level) {
|
|
2612
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, b == C0_CAN ? "CAN with no open level" : "framing inside open level");
|
|
2613
|
+
return stop;
|
|
2614
|
+
}
|
|
2615
|
+
if (dead) {
|
|
2616
|
+
c0__p_fail(p, C0_ERR_SYNTAX, mark_at, "BEL before framing");
|
|
2617
|
+
return stop;
|
|
2618
|
+
}
|
|
2619
|
+
return pos;
|
|
2620
|
+
case C0_US:
|
|
2621
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "US outside a record");
|
|
2622
|
+
return stop;
|
|
2623
|
+
case C0_STX:
|
|
2624
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "text outside a record");
|
|
2625
|
+
return stop;
|
|
2626
|
+
case C0_DLE:
|
|
2627
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "a nested level is not an item");
|
|
2628
|
+
return stop;
|
|
2629
|
+
case C0_ENQ:
|
|
2630
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "a reference outside a record");
|
|
2631
|
+
return stop;
|
|
2632
|
+
default:
|
|
2633
|
+
if (b >= 0x20) c0__p_fail(p, C0_ERR_SYNTAX, pos, "plain text outside a record");
|
|
2634
|
+
else c0__p_fail(p, C0_ERR_UNASSIGNED, pos, "unassigned control code");
|
|
2635
|
+
return stop;
|
|
2636
|
+
}
|
|
2637
|
+
}
|
|
2638
|
+
if (dead) {
|
|
2639
|
+
c0__p_fail(p, C0_ERR_UNEXPECTED_END, mark_at, "BEL at end of input");
|
|
2640
|
+
return stop;
|
|
1859
2641
|
}
|
|
2642
|
+
return pos;
|
|
1860
2643
|
}
|
|
1861
2644
|
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
2645
|
+
/* The `schema` heading key: a plain name, or a level holding one keyed
|
|
2646
|
+
* record whose keys are drawn from name, url and hash (DESIGN.md, Start). */
|
|
2647
|
+
static void c0__p_check_schema(c0__parser *p, c0_bytes rawv, size_t at) {
|
|
2648
|
+
c0_bytes t = c0_trim(rawv), v;
|
|
2649
|
+
if (t.len == 0) {
|
|
2650
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "schema needs a value");
|
|
1865
2651
|
return;
|
|
1866
2652
|
}
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
2653
|
+
if (t.ptr[0] == C0_DLE) {
|
|
2654
|
+
c0_body body;
|
|
2655
|
+
c0_record_iter ri;
|
|
2656
|
+
c0_record r;
|
|
2657
|
+
c0_pair_iter pi;
|
|
2658
|
+
c0_bytes k;
|
|
2659
|
+
size_t n = 0;
|
|
2660
|
+
if (c0_unit_level(t, &body) != C0_OK || !c0_body_bare(body)) {
|
|
2661
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a schema level holds one keyed record");
|
|
2662
|
+
return;
|
|
2663
|
+
}
|
|
2664
|
+
ri = c0_body_records(body);
|
|
2665
|
+
if (!c0_next_record(&ri, &r)) {
|
|
2666
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a schema level holds one keyed record");
|
|
2667
|
+
return;
|
|
2668
|
+
}
|
|
2669
|
+
{
|
|
2670
|
+
c0_record r2;
|
|
2671
|
+
if (c0_next_record(&ri, &r2)) {
|
|
2672
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a schema level holds one keyed record");
|
|
2673
|
+
return;
|
|
2674
|
+
}
|
|
2675
|
+
}
|
|
2676
|
+
if (!c0_record_keyed(r)) {
|
|
2677
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a schema level holds a keyed record, not a positional one");
|
|
2678
|
+
return;
|
|
2679
|
+
}
|
|
2680
|
+
pi = c0_record_pairs(r);
|
|
2681
|
+
while (c0_next_pair(&pi, &k, &v)) {
|
|
2682
|
+
n++;
|
|
2683
|
+
if (!(c0__eq_str(k, "name") || c0__eq_str(k, "url") || c0__eq_str(k, "hash"))) {
|
|
2684
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "unknown schema key");
|
|
2685
|
+
return;
|
|
2686
|
+
}
|
|
2687
|
+
if (c0_value(v, NULL, &v) != C0_OK || v.len == 0 || !c0_is_name(v.ptr, v.len)) {
|
|
2688
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a schema name, url or hash is a name");
|
|
2689
|
+
return;
|
|
2690
|
+
}
|
|
2691
|
+
}
|
|
2692
|
+
if (n == 0) c0__p_fail(p, C0_ERR_SYNTAX, at, "a schema record needs name, url or hash");
|
|
1875
2693
|
return;
|
|
1876
2694
|
}
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
2695
|
+
if (t.ptr[0] == C0_STX) {
|
|
2696
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a schema name cannot be text");
|
|
2697
|
+
return;
|
|
2698
|
+
}
|
|
2699
|
+
if (c0_value(t, NULL, &v) != C0_OK || v.len == 0 || !c0_is_name(v.ptr, v.len)) {
|
|
2700
|
+
c0__p_fail(p, C0_ERR_SYNTAX, at, "a schema name is a name");
|
|
1881
2701
|
}
|
|
1882
|
-
c0__b_byte(b, C0_ETX);
|
|
1883
|
-
}
|
|
1884
|
-
|
|
1885
|
-
static void c0__b_end_record(c0_builder *b) {
|
|
1886
|
-
b->state = C0__B_TOP;
|
|
1887
|
-
b->units = 0;
|
|
1888
|
-
}
|
|
1889
|
-
|
|
1890
|
-
/* The first value of a record, straight after RS. */
|
|
1891
|
-
static void c0__b_first_value(c0_builder *b, const uint8_t *p, size_t n) {
|
|
1892
|
-
c0__b_value(b, p, n);
|
|
1893
|
-
b->units = 1;
|
|
1894
2702
|
}
|
|
1895
2703
|
|
|
1896
|
-
/*
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
2704
|
+
/* After SYN: the heading. Returns the position of the first body code. */
|
|
2705
|
+
static size_t c0__p_heading(c0__parser *p, size_t pos, size_t stop) {
|
|
2706
|
+
const uint8_t *buf = p->buf;
|
|
2707
|
+
size_t ks, ke, vs;
|
|
2708
|
+
for (;;) {
|
|
2709
|
+
uint8_t b;
|
|
2710
|
+
pos = c0__p_layout(p, pos, stop);
|
|
2711
|
+
if (pos >= stop) return pos;
|
|
2712
|
+
b = buf[pos];
|
|
2713
|
+
if (b == C0_SOH) {
|
|
2714
|
+
if (c0__soh_is_header(buf, pos, stop)) return pos; /* a header row begins the body */
|
|
2715
|
+
pos = c0__p_keyed_unit(p, pos + 1, stop, 1, &ks, &ke, &vs);
|
|
2716
|
+
if (C0__P_ERR(p)) return stop;
|
|
2717
|
+
if (c0__eq_str(c0__slice(buf, ks, ke), "schema")) c0__p_check_schema(p, c0__slice(buf, vs, pos), vs);
|
|
2718
|
+
if (C0__P_ERR(p)) return stop;
|
|
2719
|
+
} else if (b == C0_BEL) {
|
|
2720
|
+
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2721
|
+
uint8_t c;
|
|
2722
|
+
if (p2 >= stop) {
|
|
2723
|
+
c0__p_fail(p, C0_ERR_UNEXPECTED_END, p2, "BEL at end of input");
|
|
2724
|
+
return stop;
|
|
2725
|
+
}
|
|
2726
|
+
c = buf[p2];
|
|
2727
|
+
if (c == C0_SOH) {
|
|
2728
|
+
if (c0__soh_is_header(buf, p2, stop)) return pos; /* a dead header row begins the body */
|
|
2729
|
+
c0__p_nc(p);
|
|
2730
|
+
pos = c0__p_keyed_unit(p, p2 + 1, stop, 1, &ks, &ke, &vs);
|
|
2731
|
+
if (C0__P_ERR(p)) return stop;
|
|
2732
|
+
} else if (c >= 0x20 || c == C0_STX) {
|
|
2733
|
+
c0__p_nc(p);
|
|
2734
|
+
pos = c0__p_note_content(p, p2, stop);
|
|
2735
|
+
if (C0__P_ERR(p)) return stop;
|
|
2736
|
+
} else if (c == C0_RS || c == C0_GS || c == C0_FS || c == C0_DLE) {
|
|
2737
|
+
return pos; /* a dead body element */
|
|
2738
|
+
} else {
|
|
2739
|
+
c0__p_fail(p, C0_ERR_SYNTAX, p2, "BEL before a code that is not an element");
|
|
2740
|
+
return stop;
|
|
2741
|
+
}
|
|
2742
|
+
} else if (b == C0_SUB) {
|
|
2743
|
+
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2744
|
+
if (p2 >= stop) {
|
|
2745
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "SUB at the end of input");
|
|
2746
|
+
return stop;
|
|
2747
|
+
}
|
|
2748
|
+
if (buf[p2] == C0_US) {
|
|
2749
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "the heading's values take no sub-units");
|
|
2750
|
+
return stop;
|
|
2751
|
+
}
|
|
2752
|
+
return pos; /* a prefix before a body code: the body's business */
|
|
2753
|
+
} else if (b >= 0x20) {
|
|
2754
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "plain text directly after SYN");
|
|
2755
|
+
return stop;
|
|
2756
|
+
} else {
|
|
2757
|
+
return pos;
|
|
2758
|
+
}
|
|
2759
|
+
}
|
|
1902
2760
|
}
|
|
1903
2761
|
|
|
1904
|
-
/*
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
2762
|
+
/* Drive the container automaton over [pos, stop) from the state in st,
|
|
2763
|
+
* resumably: a stream reader calls it once per block. Returns the position
|
|
2764
|
+
* reached (stop, or where EM began the raw region). */
|
|
2765
|
+
static size_t c0__p_run(c0__parser *p, c0_parse_state *st, size_t pos, size_t stop) {
|
|
2766
|
+
const uint8_t *buf = p->buf;
|
|
2767
|
+
for (;;) {
|
|
2768
|
+
uint8_t b;
|
|
2769
|
+
if (st->phase == C0__PH_EM) return stop;
|
|
2770
|
+
pos = c0__p_layout(p, pos, stop);
|
|
2771
|
+
if (pos >= stop || C0__P_ERR(p)) return pos;
|
|
2772
|
+
b = buf[pos];
|
|
2773
|
+
switch (st->phase) {
|
|
2774
|
+
case C0__PH_START:
|
|
2775
|
+
if (b == C0_EM) {
|
|
2776
|
+
c0__p_nc(p);
|
|
2777
|
+
st->phase = C0__PH_EM;
|
|
2778
|
+
return stop;
|
|
2779
|
+
}
|
|
2780
|
+
if (b == C0_SYN) {
|
|
2781
|
+
c0__p_nc(p);
|
|
2782
|
+
pos = c0__p_heading(p, pos + 1, stop);
|
|
2783
|
+
if (C0__P_ERR(p)) return stop;
|
|
2784
|
+
} else if (!st->first) {
|
|
2785
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "a document after the first must begin with SYN");
|
|
2786
|
+
return stop;
|
|
2787
|
+
}
|
|
2788
|
+
st->first = 0;
|
|
2789
|
+
st->phase = C0__PH_BODY;
|
|
2790
|
+
c0__st_body_reset(st, 0);
|
|
2791
|
+
break;
|
|
2792
|
+
case C0__PH_BODY:
|
|
2793
|
+
pos = c0__p_body(p, st, pos, stop);
|
|
2794
|
+
if (C0__P_ERR(p)) return stop;
|
|
2795
|
+
if (pos >= stop) return pos;
|
|
2796
|
+
b = buf[pos];
|
|
2797
|
+
if (b == C0_EOT) {
|
|
2798
|
+
c0__p_nc(p);
|
|
2799
|
+
pos++;
|
|
2800
|
+
st->phase = C0__PH_AFTER_EOT;
|
|
2801
|
+
} else if (b == C0_SYN) {
|
|
2802
|
+
st->phase = C0__PH_START;
|
|
2803
|
+
} else { /* EM */
|
|
2804
|
+
c0__p_nc(p);
|
|
2805
|
+
st->phase = C0__PH_EM;
|
|
2806
|
+
return stop;
|
|
2807
|
+
}
|
|
2808
|
+
break;
|
|
2809
|
+
case C0__PH_AFTER_EOT:
|
|
2810
|
+
if (b == C0_SYN) {
|
|
2811
|
+
st->phase = C0__PH_START;
|
|
2812
|
+
} else if (b == C0_EM) {
|
|
2813
|
+
c0__p_nc(p);
|
|
2814
|
+
st->phase = C0__PH_EM;
|
|
2815
|
+
return stop;
|
|
2816
|
+
} else {
|
|
2817
|
+
c0__p_fail(p, C0_ERR_SYNTAX, pos, "unexpected byte after EOT");
|
|
2818
|
+
return stop;
|
|
2819
|
+
}
|
|
2820
|
+
break;
|
|
2821
|
+
default:
|
|
2822
|
+
return stop;
|
|
2823
|
+
}
|
|
2824
|
+
}
|
|
1908
2825
|
}
|
|
1909
2826
|
|
|
1910
|
-
|
|
1911
|
-
c0_bytes b;
|
|
1912
|
-
b.ptr = (const uint8_t *)s;
|
|
1913
|
-
b.len = strlen(s);
|
|
1914
|
-
return b;
|
|
1915
|
-
}
|
|
2827
|
+
/* ---- references -------------------------------------------------------- */
|
|
1916
2828
|
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
size_t
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
2829
|
+
c0_status c0_unit_ref(c0_bytes raw, c0_ref *out) {
|
|
2830
|
+
c0_bytes t = c0_trim(raw);
|
|
2831
|
+
c0__parser p;
|
|
2832
|
+
size_t pos, after;
|
|
2833
|
+
memset(out, 0, sizeof(*out));
|
|
2834
|
+
if (!(t.len > 0 && t.ptr[0] == C0_ENQ)) return C0_ERR_NOT_REFERENCE;
|
|
2835
|
+
c0__p_init(&p, t.ptr, 0);
|
|
2836
|
+
after = c0__p_reference(&p, 0, t.len);
|
|
2837
|
+
if (C0__P_ERR(&p)) return p.err.code;
|
|
2838
|
+
if (c0__only_notes_after(t, after) != C0_OK) return C0_ERR_SYNTAX;
|
|
2839
|
+
pos = c0__layout(t.ptr, 1, t.len);
|
|
2840
|
+
if (t.ptr[pos] == C0_DLE) {
|
|
2841
|
+
c0_error err = {C0_OK, 0, ""};
|
|
2842
|
+
size_t end = c0__level(t.ptr, pos, t.len, &err);
|
|
2843
|
+
out->is_pattern = 1;
|
|
2844
|
+
out->pattern.buf = t.ptr;
|
|
2845
|
+
out->pattern.start = pos + 1;
|
|
2846
|
+
out->pattern.stop = end - 1;
|
|
2847
|
+
out->name = c0__slice(t.ptr, pos, pos);
|
|
2848
|
+
} else {
|
|
2849
|
+
out->name = c0_trim(c0__slice(t.ptr, pos, c0__plain(t.ptr, pos, t.len)));
|
|
1924
2850
|
}
|
|
1925
|
-
|
|
1926
|
-
return out;
|
|
2851
|
+
return C0_OK;
|
|
1927
2852
|
}
|
|
1928
2853
|
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
2854
|
+
static size_t c0__ref_body_path(c0_body body, c0_bytes *steps, size_t max, size_t n) {
|
|
2855
|
+
c0_file_iter fi = c0_body_files(body);
|
|
2856
|
+
c0_file f;
|
|
2857
|
+
c0_group_iter gi;
|
|
2858
|
+
c0_group g, last;
|
|
2859
|
+
c0_record_iter ri;
|
|
2860
|
+
c0_record r;
|
|
2861
|
+
int have = 0;
|
|
2862
|
+
if (!c0_next_file(&fi, &f)) return n;
|
|
2863
|
+
if (!f.leading) {
|
|
2864
|
+
if (n < max) steps[n] = c0_file_name(f);
|
|
2865
|
+
n++;
|
|
2866
|
+
}
|
|
2867
|
+
gi = c0_file_groups(f);
|
|
2868
|
+
memset(&last, 0, sizeof(last));
|
|
2869
|
+
while (c0_next_group(&gi, &g)) {
|
|
2870
|
+
if (g.depth > 0) {
|
|
2871
|
+
if (n < max) steps[n] = c0_group_name(g);
|
|
2872
|
+
n++;
|
|
2873
|
+
}
|
|
2874
|
+
last = g;
|
|
2875
|
+
have = 1;
|
|
2876
|
+
}
|
|
2877
|
+
if (!have) return n;
|
|
2878
|
+
ri = c0_group_records(last);
|
|
2879
|
+
if (!c0_next_record(&ri, &r)) return n;
|
|
2880
|
+
if (c0_record_keyed(r)) {
|
|
2881
|
+
c0_pair_iter pi = c0_record_pairs(r);
|
|
2882
|
+
c0_bytes k, v;
|
|
2883
|
+
if (c0_next_pair(&pi, &k, &v)) {
|
|
2884
|
+
if (n < max) steps[n] = k;
|
|
2885
|
+
n++;
|
|
2886
|
+
}
|
|
2887
|
+
return n;
|
|
2888
|
+
}
|
|
2889
|
+
{
|
|
2890
|
+
c0_unit_iter ui = c0_record_units(r);
|
|
2891
|
+
c0_bytes raw, v;
|
|
2892
|
+
size_t i;
|
|
2893
|
+
for (i = 0; c0_next_unit(&ui, &raw); i++) {
|
|
2894
|
+
if (i < 2) {
|
|
2895
|
+
if (c0_value(raw, NULL, &v) != C0_OK) v = c0_trim(raw);
|
|
2896
|
+
if (n < max) steps[n] = v;
|
|
2897
|
+
n++;
|
|
2898
|
+
} else {
|
|
2899
|
+
c0_body lv;
|
|
2900
|
+
if (c0_unit_level(raw, &lv) == C0_OK) n = c0__ref_body_path(lv, steps, max, n);
|
|
2901
|
+
}
|
|
2902
|
+
}
|
|
2903
|
+
}
|
|
2904
|
+
return n;
|
|
1932
2905
|
}
|
|
1933
2906
|
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
2907
|
+
size_t c0_ref_path(const c0_ref *r, c0_bytes *steps, size_t max) {
|
|
2908
|
+
if (!r->is_pattern) {
|
|
2909
|
+
if (max > 0) steps[0] = r->name;
|
|
2910
|
+
return 1;
|
|
2911
|
+
}
|
|
2912
|
+
return c0__ref_body_path(r->pattern, steps, max, 0);
|
|
1938
2913
|
}
|
|
1939
2914
|
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
2915
|
+
int c0_ref_group(const c0_ref *r, c0_bytes *name) {
|
|
2916
|
+
c0_file_iter fi;
|
|
2917
|
+
c0_file f;
|
|
2918
|
+
c0_group_iter gi;
|
|
2919
|
+
c0_group g;
|
|
2920
|
+
c0_record_iter ri;
|
|
2921
|
+
c0_record rec;
|
|
2922
|
+
if (!r->is_pattern) {
|
|
2923
|
+
if (name) *name = r->name;
|
|
2924
|
+
return 1;
|
|
2925
|
+
}
|
|
2926
|
+
fi = c0_body_files(r->pattern);
|
|
2927
|
+
if (!c0_next_file(&fi, &f) || !f.leading) return 0;
|
|
2928
|
+
gi = c0_file_groups(f);
|
|
2929
|
+
if (!c0_next_group(&gi, &g) || g.depth == 0) return 0;
|
|
2930
|
+
if (c0_next_group(&gi, &g)) return 0;
|
|
2931
|
+
ri = c0_group_records(g);
|
|
2932
|
+
if (c0_next_record(&ri, &rec)) return 0;
|
|
2933
|
+
if (name) *name = c0_group_name(g);
|
|
2934
|
+
return 1;
|
|
1947
2935
|
}
|
|
1948
2936
|
|
|
1949
|
-
/* ----
|
|
2937
|
+
/* ---- documents --------------------------------------------------------- */
|
|
1950
2938
|
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
2939
|
+
/* Read one document with the parser p from start; the parser's counters
|
|
2940
|
+
* are reported as the document's. */
|
|
2941
|
+
static c0_status c0__read_document(c0__parser *p, size_t len, size_t start, c0_document *out) {
|
|
2942
|
+
const uint8_t *buf = p->buf;
|
|
2943
|
+
size_t pos, dead0 = p->dead_elements;
|
|
2944
|
+
c0_parse_state st;
|
|
2945
|
+
memset(out, 0, sizeof(*out));
|
|
2946
|
+
out->buf = buf;
|
|
2947
|
+
out->start = start;
|
|
2948
|
+
pos = c0__p_layout(p, start, len);
|
|
2949
|
+
out->has_syn = pos < len && buf[pos] == C0_SYN;
|
|
2950
|
+
if (out->has_syn) {
|
|
2951
|
+
c0__p_nc(p);
|
|
2952
|
+
pos++;
|
|
2953
|
+
out->heading_start = pos;
|
|
2954
|
+
pos = c0__p_heading(p, pos, len);
|
|
2955
|
+
if (C0__P_ERR(p)) return p->err.code;
|
|
2956
|
+
out->heading_end = pos;
|
|
2957
|
+
} else {
|
|
2958
|
+
out->warnings |= C0_WARN_NO_SYN;
|
|
2959
|
+
out->heading_start = out->heading_end = pos;
|
|
2960
|
+
}
|
|
2961
|
+
out->body.buf = buf;
|
|
2962
|
+
out->body.start = pos;
|
|
2963
|
+
c0__st_body_reset(&st, 0);
|
|
2964
|
+
pos = c0__p_body(p, &st, pos, len);
|
|
2965
|
+
if (C0__P_ERR(p)) return p->err.code;
|
|
2966
|
+
out->body.stop = pos;
|
|
2967
|
+
pos = c0__p_layout(p, pos, len);
|
|
2968
|
+
out->has_eot = pos < len && buf[pos] == C0_EOT;
|
|
2969
|
+
if (out->has_eot) {
|
|
2970
|
+
c0__p_nc(p);
|
|
2971
|
+
pos++;
|
|
2972
|
+
} else if (!c0__eq_str(c0_heading_shape(out), "stream")) {
|
|
2973
|
+
out->warnings |= C0_WARN_NO_EOT;
|
|
2974
|
+
}
|
|
2975
|
+
out->stop = pos;
|
|
2976
|
+
out->dead_elements = p->dead_elements - dead0;
|
|
2977
|
+
{
|
|
2978
|
+
c0_pair_iter it = c0_document_heading(out);
|
|
2979
|
+
c0_bytes k, v, other;
|
|
2980
|
+
while (c0_next_pair(&it, &k, &v)) {
|
|
2981
|
+
c0_pair_iter rest = it;
|
|
2982
|
+
c0_bytes k2;
|
|
2983
|
+
if (!c0__eq_str(k, "shape") && !c0__eq_str(k, "schema") && !c0__eq_str(k, "version") && !c0__eq_str(k, "blob")) {
|
|
2984
|
+
out->warnings |= C0_WARN_UNKNOWN_KEY;
|
|
2985
|
+
}
|
|
2986
|
+
while (c0_next_pair(&rest, &k2, &other)) {
|
|
2987
|
+
if (k2.len == k.len && memcmp(k2.ptr, k.ptr, k.len) == 0) out->warnings |= C0_WARN_DUPLICATE_KEY;
|
|
2988
|
+
}
|
|
2989
|
+
}
|
|
2990
|
+
if (it.err.code) {
|
|
2991
|
+
p->err = it.err;
|
|
2992
|
+
return p->err.code;
|
|
2993
|
+
}
|
|
2994
|
+
{
|
|
2995
|
+
c0_bytes shape = c0_heading_shape(out);
|
|
2996
|
+
if (shape.len && !c0__eq_str(shape, "diff") && !c0__eq_str(shape, "stream")) out->warnings |= C0_WARN_UNKNOWN_SHAPE;
|
|
2997
|
+
}
|
|
2998
|
+
if (c0_heading_get_str(out, "version", &v) == C0_OK) {
|
|
2999
|
+
if (c0_value(v, NULL, &v) != C0_OK || !c0__eq_str(v, "1")) out->warnings |= C0_WARN_UNKNOWN_VERSION;
|
|
3000
|
+
}
|
|
3001
|
+
}
|
|
3002
|
+
return C0_OK;
|
|
1955
3003
|
}
|
|
1956
3004
|
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
3005
|
+
c0_status c0_document_read(const uint8_t *buf, size_t len, size_t start, c0_document *out, c0_error *err) {
|
|
3006
|
+
c0__parser p;
|
|
3007
|
+
c0_status st;
|
|
3008
|
+
c0__p_init(&p, buf, 0);
|
|
3009
|
+
st = c0__read_document(&p, len, start, out);
|
|
3010
|
+
if (err) *err = p.err;
|
|
3011
|
+
return st;
|
|
1960
3012
|
}
|
|
1961
3013
|
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
3014
|
+
c0_status c0_body_read(const uint8_t *buf, size_t len, c0_body *out, c0_error *err) {
|
|
3015
|
+
c0__parser p;
|
|
3016
|
+
c0_parse_state st;
|
|
3017
|
+
size_t pos;
|
|
3018
|
+
c0__p_init(&p, buf, 0);
|
|
3019
|
+
c0__st_body_reset(&st, 0);
|
|
3020
|
+
pos = c0__p_body(&p, &st, 0, len);
|
|
3021
|
+
if (!C0__P_ERR(&p)) {
|
|
3022
|
+
pos = c0__layout(buf, pos, len);
|
|
3023
|
+
if (pos < len) c0__p_fail(&p, C0_ERR_SYNTAX, pos, "unexpected byte in body");
|
|
3024
|
+
}
|
|
3025
|
+
if (err) *err = p.err;
|
|
3026
|
+
if (p.err.code) return p.err.code;
|
|
3027
|
+
*out = c0_body_view(buf, len);
|
|
3028
|
+
return C0_OK;
|
|
1965
3029
|
}
|
|
1966
3030
|
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
b->state = C0__B_RECORD;
|
|
1970
|
-
b->units = 0;
|
|
3031
|
+
c0_pair_iter c0_document_heading(const c0_document *d) {
|
|
3032
|
+
return c0__pairs(d->buf, d->heading_start, d->heading_end);
|
|
1971
3033
|
}
|
|
1972
3034
|
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
c0__b_first_value(b, value, vlen);
|
|
3035
|
+
c0_status c0_heading_get(const c0_document *d, const uint8_t *key, size_t klen, c0_bytes *raw) {
|
|
3036
|
+
return c0__pairs_get(c0_document_heading(d), key, klen, raw);
|
|
1976
3037
|
}
|
|
1977
3038
|
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
b->units++;
|
|
3039
|
+
c0_status c0_heading_get_str(const c0_document *d, const char *key, c0_bytes *raw) {
|
|
3040
|
+
return c0_heading_get(d, (const uint8_t *)key, strlen(key), raw);
|
|
1981
3041
|
}
|
|
1982
3042
|
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
3043
|
+
c0_bytes c0_heading_shape(const c0_document *d) {
|
|
3044
|
+
c0_bytes raw, v;
|
|
3045
|
+
v.ptr = d->buf;
|
|
3046
|
+
v.len = 0;
|
|
3047
|
+
if (c0_heading_get_str(d, "shape", &raw) == C0_OK && c0_value(raw, NULL, &v) != C0_OK) v.len = 0;
|
|
3048
|
+
return v;
|
|
1987
3049
|
}
|
|
1988
3050
|
|
|
1989
|
-
|
|
1990
|
-
|
|
3051
|
+
int c0_heading_version(const c0_document *d) {
|
|
3052
|
+
c0_bytes raw, v;
|
|
3053
|
+
int n = 0;
|
|
3054
|
+
size_t i;
|
|
3055
|
+
if (c0_heading_get_str(d, "version", &raw) != C0_OK) return 1;
|
|
3056
|
+
if (c0_value(raw, NULL, &v) != C0_OK || v.len == 0) return 1;
|
|
3057
|
+
for (i = 0; i < v.len; i++) {
|
|
3058
|
+
if (v.ptr[i] < '0' || v.ptr[i] > '9') return 1;
|
|
3059
|
+
n = n * 10 + (v.ptr[i] - '0');
|
|
3060
|
+
}
|
|
3061
|
+
return n;
|
|
3062
|
+
}
|
|
1991
3063
|
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
3064
|
+
c0_status c0_heading_blob(const c0_document *d, c0_body *out) {
|
|
3065
|
+
c0_bytes raw;
|
|
3066
|
+
c0_status st = c0_heading_get_str(d, "blob", &raw);
|
|
3067
|
+
if (st) return st;
|
|
3068
|
+
return c0_unit_level(raw, out);
|
|
1995
3069
|
}
|
|
1996
3070
|
|
|
1997
|
-
|
|
3071
|
+
/* ---- containers -------------------------------------------------------- */
|
|
1998
3072
|
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
3073
|
+
c0_doc_iter c0_documents(const uint8_t *buf, size_t len) {
|
|
3074
|
+
c0_doc_iter it;
|
|
3075
|
+
it.buf = buf;
|
|
3076
|
+
it.len = len;
|
|
3077
|
+
it.pos = 0;
|
|
3078
|
+
it.has_em = 0;
|
|
3079
|
+
it.em_pos = len;
|
|
3080
|
+
it.done = 0;
|
|
3081
|
+
c0__err_clear(&it.err);
|
|
3082
|
+
return it;
|
|
2002
3083
|
}
|
|
2003
3084
|
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
}
|
|
2012
|
-
b->stack = ns;
|
|
2013
|
-
b->stack_cap = ncap;
|
|
3085
|
+
static int c0__next_document(c0_doc_iter *it, c0_document *out, c0__parser *p) {
|
|
3086
|
+
size_t pos;
|
|
3087
|
+
if (it->done) return 0;
|
|
3088
|
+
pos = c0__p_layout(p, it->pos, it->len);
|
|
3089
|
+
if (pos >= it->len) {
|
|
3090
|
+
it->done = 1;
|
|
3091
|
+
return 0;
|
|
2014
3092
|
}
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
}
|
|
2022
|
-
|
|
2023
|
-
void c0_builder_can(c0_builder *b) {
|
|
2024
|
-
if (b->depth == 0) {
|
|
2025
|
-
if (b->status == C0_OK) b->status = C0_ERR_BAD_STATE;
|
|
2026
|
-
return;
|
|
3093
|
+
if (it->buf[pos] == C0_EM) {
|
|
3094
|
+
c0__p_nc(p);
|
|
3095
|
+
it->has_em = 1;
|
|
3096
|
+
it->em_pos = pos;
|
|
3097
|
+
it->done = 1;
|
|
3098
|
+
return 0;
|
|
2027
3099
|
}
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
3100
|
+
if (it->pos > 0 && it->buf[pos] != C0_SYN) {
|
|
3101
|
+
c0__fail(&it->err, C0_ERR_SYNTAX, pos, "a document after the first must begin with SYN");
|
|
3102
|
+
it->done = 1;
|
|
3103
|
+
return 0;
|
|
3104
|
+
}
|
|
3105
|
+
if (c0__read_document(p, it->len, pos, out) != C0_OK) {
|
|
3106
|
+
it->err = p->err;
|
|
3107
|
+
it->done = 1;
|
|
3108
|
+
return 0;
|
|
3109
|
+
}
|
|
3110
|
+
it->pos = c0__layout(it->buf, out->stop, it->len);
|
|
3111
|
+
if (it->pos < it->len && it->buf[it->pos] != C0_SYN && it->buf[it->pos] != C0_EM) {
|
|
3112
|
+
c0__fail(&it->err, C0_ERR_SYNTAX, it->pos, out->has_eot ? "unexpected byte after EOT" : "unexpected byte after document");
|
|
3113
|
+
it->done = 1;
|
|
3114
|
+
return 0;
|
|
3115
|
+
}
|
|
3116
|
+
return 1;
|
|
2032
3117
|
}
|
|
2033
3118
|
|
|
2034
|
-
|
|
3119
|
+
int c0_next_document(c0_doc_iter *it, c0_document *out) {
|
|
3120
|
+
c0__parser p;
|
|
3121
|
+
c0__p_init(&p, it->buf, 0);
|
|
3122
|
+
return c0__next_document(it, out, &p);
|
|
3123
|
+
}
|
|
2035
3124
|
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
3125
|
+
c0_bytes c0_doc_iter_tail(const c0_doc_iter *it) {
|
|
3126
|
+
c0_bytes t;
|
|
3127
|
+
t.ptr = it->buf + it->len;
|
|
3128
|
+
t.len = 0;
|
|
3129
|
+
if (it->has_em) {
|
|
3130
|
+
t.ptr = it->buf + it->em_pos + 1;
|
|
3131
|
+
t.len = it->len - it->em_pos - 1;
|
|
2040
3132
|
}
|
|
2041
|
-
|
|
2042
|
-
c0__b_value(b, text, tlen);
|
|
3133
|
+
return t;
|
|
2043
3134
|
}
|
|
2044
3135
|
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
3136
|
+
c0_status c0_container_tail(const uint8_t *buf, size_t len, c0_bytes *tail, int *has_em, c0_error *err) {
|
|
3137
|
+
c0_doc_iter it = c0_documents(buf, len);
|
|
3138
|
+
c0_document d;
|
|
3139
|
+
while (c0_next_document(&it, &d)) {
|
|
3140
|
+
}
|
|
3141
|
+
if (err) *err = it.err;
|
|
3142
|
+
*tail = c0_doc_iter_tail(&it);
|
|
3143
|
+
if (has_em) *has_em = it.has_em;
|
|
3144
|
+
return it.err.code;
|
|
2049
3145
|
}
|
|
2050
3146
|
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
c0__b_byte(b, C0_ENQ);
|
|
2058
|
-
if ((count < 2 || count > 3) && b->status == C0_OK) b->status = C0_ERR_BAD_NAME;
|
|
2059
|
-
c0__b_byte(b, C0_STX);
|
|
2060
|
-
for (i = 0; i < count && i < 3; i++) {
|
|
2061
|
-
if (i) c0__b_byte(b, C0_US);
|
|
2062
|
-
if (segments[i].len == 0 && b->status == C0_OK) b->status = C0_ERR_BAD_NAME;
|
|
2063
|
-
c0__b_name(b, segments[i].ptr, segments[i].len);
|
|
3147
|
+
/* ---- validation -------------------------------------------------------- */
|
|
3148
|
+
|
|
3149
|
+
static c0_status c0__run_container(c0__parser *p, size_t len) {
|
|
3150
|
+
c0_doc_iter it = c0_documents(p->buf, len);
|
|
3151
|
+
c0_document d;
|
|
3152
|
+
while (c0__next_document(&it, &d, p)) {
|
|
2064
3153
|
}
|
|
2065
|
-
|
|
3154
|
+
if (it.err.code && !p->err.code) p->err = it.err;
|
|
3155
|
+
return p->err.code;
|
|
2066
3156
|
}
|
|
2067
3157
|
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
3158
|
+
c0_status c0_validate(const uint8_t *buf, size_t len, c0_error *err) {
|
|
3159
|
+
c0__parser p;
|
|
3160
|
+
c0__p_init(&p, buf, 0);
|
|
3161
|
+
c0__run_container(&p, len);
|
|
3162
|
+
if (err) *err = p.err;
|
|
3163
|
+
return p.err.code;
|
|
2072
3164
|
}
|
|
2073
3165
|
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
3166
|
+
int c0_canonical(const uint8_t *buf, size_t len) {
|
|
3167
|
+
c0__parser p;
|
|
3168
|
+
c0__p_init(&p, buf, 1);
|
|
3169
|
+
c0__run_container(&p, len);
|
|
3170
|
+
return p.err.code == C0_OK && p.ok;
|
|
2077
3171
|
}
|
|
2078
3172
|
|
|
2079
|
-
|
|
2080
|
-
c0__b_end_record(b);
|
|
2081
|
-
c0__b_byte(b, C0_ETB);
|
|
2082
|
-
}
|
|
3173
|
+
/* ---- blobs ------------------------------------------------------------- */
|
|
2083
3174
|
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
3175
|
+
static int c0__to_size(c0_bytes v, size_t *out) {
|
|
3176
|
+
size_t i, n = 0;
|
|
3177
|
+
if (v.len == 0) return 0;
|
|
3178
|
+
for (i = 0; i < v.len; i++) {
|
|
3179
|
+
if (v.ptr[i] < '0' || v.ptr[i] > '9') return 0;
|
|
3180
|
+
n = n * 10 + (size_t)(v.ptr[i] - '0');
|
|
3181
|
+
}
|
|
3182
|
+
*out = n;
|
|
3183
|
+
return 1;
|
|
2087
3184
|
}
|
|
2088
3185
|
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
3186
|
+
c0_status c0_blob(const uint8_t *buf, size_t len, const uint8_t *name, size_t nlen, c0_bytes *out, c0_error *err) {
|
|
3187
|
+
c0_doc_iter it = c0_documents(buf, len);
|
|
3188
|
+
c0_document first;
|
|
3189
|
+
c0_body table;
|
|
3190
|
+
c0_group_iter gi;
|
|
3191
|
+
c0_group g;
|
|
3192
|
+
c0_record_iter ri;
|
|
3193
|
+
c0_record r;
|
|
3194
|
+
c0_bytes tail;
|
|
3195
|
+
size_t ni = 0, oi = 1, li = 2;
|
|
3196
|
+
int found = 0;
|
|
3197
|
+
c0_status st;
|
|
3198
|
+
if (!c0_next_document(&it, &first)) {
|
|
3199
|
+
if (err) *err = it.err;
|
|
3200
|
+
return it.err.code ? it.err.code : C0_ERR_NOT_FOUND;
|
|
3201
|
+
}
|
|
3202
|
+
{
|
|
3203
|
+
c0_document d;
|
|
3204
|
+
while (c0_next_document(&it, &d)) {
|
|
3205
|
+
}
|
|
3206
|
+
}
|
|
3207
|
+
if (err) *err = it.err;
|
|
3208
|
+
if (it.err.code) return it.err.code;
|
|
3209
|
+
if (!it.has_em) return C0_ERR_NOT_FOUND;
|
|
3210
|
+
tail = c0_doc_iter_tail(&it);
|
|
3211
|
+
st = c0_heading_blob(&first, &table);
|
|
3212
|
+
if (st) return st;
|
|
3213
|
+
gi = c0_body_groups(table);
|
|
3214
|
+
if (!c0_next_group(&gi, &g)) return C0_ERR_NOT_FOUND;
|
|
3215
|
+
c0_group_column(g, (const uint8_t *)"name", 4, &ni);
|
|
3216
|
+
c0_group_column(g, (const uint8_t *)"offset", 6, &oi);
|
|
3217
|
+
c0_group_column(g, (const uint8_t *)"length", 6, &li);
|
|
3218
|
+
ri = c0_group_records(g);
|
|
3219
|
+
while (c0_next_record(&ri, &r)) {
|
|
3220
|
+
c0_bytes raw, v;
|
|
3221
|
+
size_t off, length;
|
|
3222
|
+
if (c0_record_field(r, ni, &raw) || c0_value(raw, NULL, &v)) continue;
|
|
3223
|
+
if (!(v.len == nlen && (nlen == 0 || memcmp(v.ptr, name, nlen) == 0))) continue;
|
|
3224
|
+
if (c0_record_field(r, oi, &raw) || c0_value(raw, NULL, &v) || !c0__to_size(v, &off)) continue;
|
|
3225
|
+
if (c0_record_field(r, li, &raw) || c0_value(raw, NULL, &v) || !c0__to_size(v, &length)) continue;
|
|
3226
|
+
if (off > tail.len || length > tail.len - off) continue;
|
|
3227
|
+
out->ptr = tail.ptr + off;
|
|
3228
|
+
out->len = length;
|
|
3229
|
+
found = 1; /* last wins */
|
|
3230
|
+
}
|
|
3231
|
+
return found ? C0_OK : C0_ERR_NOT_FOUND;
|
|
2093
3232
|
}
|
|
2094
3233
|
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
void c0_builder_rs_str(c0_builder *b, const char *value) { c0_bytes v = c0__cstr(value); c0_builder_rs_value(b, v.ptr, v.len); }
|
|
2098
|
-
void c0_builder_us_str(c0_builder *b, const char *value) { c0_bytes v = c0__cstr(value); c0_builder_us_value(b, v.ptr, v.len); }
|
|
2099
|
-
void c0_builder_sub_str(c0_builder *b, const char *value) { c0_bytes v = c0__cstr(value); c0_builder_sub_value(b, v.ptr, v.len); }
|
|
2100
|
-
void c0_builder_soh_str(c0_builder *b, const char *name) { c0_bytes n = c0__cstr(name); c0_builder_soh_name(b, n.ptr, n.len); }
|
|
2101
|
-
void c0_builder_bel_str(c0_builder *b, const char *text) { c0_bytes t = c0__cstr(text); c0_builder_bel_text(b, t.ptr, t.len); }
|
|
2102
|
-
void c0_builder_enq_str(c0_builder *b, const char *name) { c0_bytes n = c0__cstr(name); c0_builder_enq(b, n.ptr, n.len); }
|
|
2103
|
-
void c0_builder_enq_path_str(c0_builder *b, const char *const *segments, size_t count) {
|
|
2104
|
-
c0_bytes *v = c0__cstrs(b, segments, count);
|
|
2105
|
-
if (!v) return;
|
|
2106
|
-
c0_builder_enq_path(b, v, count);
|
|
2107
|
-
free(v);
|
|
3234
|
+
c0_status c0_blob_str(const uint8_t *buf, size_t len, const char *name, c0_bytes *out, c0_error *err) {
|
|
3235
|
+
return c0_blob(buf, len, (const uint8_t *)name, strlen(name), out, err);
|
|
2108
3236
|
}
|
|
2109
|
-
void c0_builder_etb_str(c0_builder *b, const char *payload) { c0_bytes p = c0__cstr(payload); c0_builder_etb_payload(b, p.ptr, p.len); }
|
|
2110
|
-
void c0_builder_em_str(c0_builder *b, const char *tail) { c0_bytes t = c0__cstr(tail); c0_builder_em(b, t.ptr, t.len); }
|
|
2111
|
-
|
|
2112
|
-
/* ---- composed: wrappers over the primitives ---- */
|
|
2113
3237
|
|
|
2114
|
-
|
|
3238
|
+
/* ---- builder ----------------------------------------------------------- */
|
|
2115
3239
|
|
|
2116
|
-
|
|
3240
|
+
enum { C0__B_TOP = 0, C0__B_HEADING = 1, C0__B_RECORD = 2 };
|
|
2117
3241
|
|
|
2118
|
-
|
|
2119
|
-
if (
|
|
2120
|
-
|
|
3242
|
+
static int c0__b_reserve(c0_builder *b, size_t extra) {
|
|
3243
|
+
if (b->status != C0_OK) return 0;
|
|
3244
|
+
if (b->len + extra > b->cap) {
|
|
3245
|
+
size_t ncap = b->cap ? b->cap : 64;
|
|
3246
|
+
uint8_t *nd;
|
|
3247
|
+
while (ncap < b->len + extra) ncap *= 2;
|
|
3248
|
+
nd = (uint8_t *)realloc(b->data, ncap);
|
|
3249
|
+
if (!nd) {
|
|
3250
|
+
b->status = C0_ERR_OOM;
|
|
3251
|
+
return 0;
|
|
3252
|
+
}
|
|
3253
|
+
b->data = nd;
|
|
3254
|
+
b->cap = ncap;
|
|
3255
|
+
}
|
|
3256
|
+
return 1;
|
|
2121
3257
|
}
|
|
2122
3258
|
|
|
2123
|
-
void
|
|
2124
|
-
|
|
2125
|
-
void c0_build_file(c0_builder *b, const uint8_t *name, size_t nlen) { c0_builder_fs(b, name, nlen); }
|
|
2126
|
-
|
|
2127
|
-
void c0_build_section(c0_builder *b, const uint8_t *name, size_t nlen, size_t depth) {
|
|
2128
|
-
size_t i;
|
|
2129
|
-
for (i = 1; i < depth; i++) c0_builder_gs(b);
|
|
2130
|
-
c0_builder_gs_name(b, name, nlen);
|
|
3259
|
+
static void c0__b_byte(c0_builder *b, uint8_t x) {
|
|
3260
|
+
if (c0__b_reserve(b, 1)) b->data[b->len++] = x;
|
|
2131
3261
|
}
|
|
2132
3262
|
|
|
2133
|
-
void
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
for (i = 0; i < count; i++) { /* validate before writing anything */
|
|
2138
|
-
if (!c0_is_name(names[i].ptr, names[i].len)) {
|
|
2139
|
-
if (b->status == C0_OK) b->status = C0_ERR_BAD_NAME;
|
|
2140
|
-
return;
|
|
2141
|
-
}
|
|
3263
|
+
static void c0__b_raw(c0_builder *b, const uint8_t *p, size_t n) {
|
|
3264
|
+
if (n && c0__b_reserve(b, n)) {
|
|
3265
|
+
memcpy(b->data + b->len, p, n);
|
|
3266
|
+
b->len += n;
|
|
2142
3267
|
}
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
if (
|
|
2148
|
-
|
|
3268
|
+
}
|
|
3269
|
+
|
|
3270
|
+
static void c0__b_name(c0_builder *b, const uint8_t *p, size_t n) {
|
|
3271
|
+
if (!c0_is_name(p, n)) {
|
|
3272
|
+
if (b->status == C0_OK) b->status = C0_ERR_BAD_NAME;
|
|
3273
|
+
return;
|
|
2149
3274
|
}
|
|
2150
|
-
|
|
3275
|
+
c0__b_raw(b, p, n);
|
|
2151
3276
|
}
|
|
2152
3277
|
|
|
2153
|
-
|
|
3278
|
+
/* A value in its one spelling. */
|
|
3279
|
+
static void c0__b_value(c0_builder *b, const uint8_t *p, size_t n) {
|
|
2154
3280
|
size_t i;
|
|
2155
|
-
|
|
2156
|
-
|
|
3281
|
+
if (c0_is_plain(p, n)) {
|
|
3282
|
+
c0__b_raw(b, p, n);
|
|
3283
|
+
return;
|
|
3284
|
+
}
|
|
3285
|
+
c0__b_byte(b, C0_STX);
|
|
3286
|
+
for (i = 0; i < n; i++) {
|
|
3287
|
+
if (c0_is_text_escaped(p[i])) c0__b_byte(b, C0_DLE);
|
|
3288
|
+
c0__b_byte(b, p[i]);
|
|
3289
|
+
}
|
|
3290
|
+
c0__b_byte(b, C0_ETX);
|
|
2157
3291
|
}
|
|
2158
3292
|
|
|
2159
|
-
void
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
else c0_builder_us_value(b, value, vlen);
|
|
3293
|
+
static void c0__b_end_record(c0_builder *b) {
|
|
3294
|
+
b->state = C0__B_TOP;
|
|
3295
|
+
b->units = 0;
|
|
2163
3296
|
}
|
|
2164
3297
|
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
3298
|
+
/* The first value of a record, straight after RS. */
|
|
3299
|
+
static void c0__b_first_value(c0_builder *b, const uint8_t *p, size_t n) {
|
|
3300
|
+
c0__b_value(b, p, n);
|
|
3301
|
+
b->units = 1;
|
|
2169
3302
|
}
|
|
2170
3303
|
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
3304
|
+
/* Position for the next unit of the open record (opening one if needed):
|
|
3305
|
+
* US between units, nothing before the first. */
|
|
3306
|
+
static void c0__b_next_unit(c0_builder *b) {
|
|
3307
|
+
if (b->state != C0__B_RECORD) c0_builder_rs(b);
|
|
3308
|
+
else if (b->units > 0) c0__b_byte(b, C0_US);
|
|
3309
|
+
b->units++;
|
|
2174
3310
|
}
|
|
2175
3311
|
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
3312
|
+
/* SOH key US, opening a record first unless one (or the heading) is open. */
|
|
3313
|
+
static void c0__b_key_head(c0_builder *b, const uint8_t *key, size_t klen) {
|
|
3314
|
+
if (b->state == C0__B_TOP) c0_builder_rs(b);
|
|
3315
|
+
c0_builder_soh_name(b, key, klen);
|
|
2179
3316
|
}
|
|
2180
3317
|
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
3318
|
+
static c0_bytes c0__cstr(const char *s) {
|
|
3319
|
+
c0_bytes b;
|
|
3320
|
+
b.ptr = (const uint8_t *)s;
|
|
3321
|
+
b.len = strlen(s);
|
|
3322
|
+
return b;
|
|
2185
3323
|
}
|
|
2186
3324
|
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
size_t i;
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
c0_builder_rs(b);
|
|
2195
|
-
for (i = 0; i < count; i++) c0_build_field(b, items[i].ptr, items[i].len);
|
|
3325
|
+
/* Build a c0_bytes array from C strings on the heap; NULL on OOM. */
|
|
3326
|
+
static c0_bytes *c0__cstrs(c0_builder *b, const char *const *strs, size_t count) {
|
|
3327
|
+
c0_bytes *out = (c0_bytes *)malloc((count ? count : 1) * sizeof(c0_bytes));
|
|
3328
|
+
size_t i;
|
|
3329
|
+
if (!out) {
|
|
3330
|
+
if (b->status == C0_OK) b->status = C0_ERR_OOM;
|
|
3331
|
+
return NULL;
|
|
2196
3332
|
}
|
|
2197
|
-
|
|
3333
|
+
for (i = 0; i < count; i++) out[i] = c0__cstr(strs[i]);
|
|
3334
|
+
return out;
|
|
2198
3335
|
}
|
|
2199
3336
|
|
|
2200
|
-
void
|
|
2201
|
-
|
|
2202
|
-
|
|
3337
|
+
void c0_builder_init(c0_builder *b) {
|
|
3338
|
+
memset(b, 0, sizeof(*b));
|
|
3339
|
+
b->status = C0_OK;
|
|
2203
3340
|
}
|
|
2204
3341
|
|
|
2205
|
-
void
|
|
2206
|
-
|
|
2207
|
-
|
|
3342
|
+
void c0_builder_free(c0_builder *b) {
|
|
3343
|
+
free(b->data);
|
|
3344
|
+
free(b->stack);
|
|
3345
|
+
memset(b, 0, sizeof(*b));
|
|
2208
3346
|
}
|
|
2209
3347
|
|
|
2210
|
-
|
|
3348
|
+
c0_status c0_builder_status(const c0_builder *b) { return b->status; }
|
|
2211
3349
|
|
|
2212
|
-
|
|
2213
|
-
c0_bytes
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
3350
|
+
c0_bytes c0_builder_bytes(const c0_builder *b) {
|
|
3351
|
+
c0_bytes r;
|
|
3352
|
+
r.ptr = b->data;
|
|
3353
|
+
r.len = b->len;
|
|
3354
|
+
return r;
|
|
2217
3355
|
}
|
|
2218
3356
|
|
|
2219
|
-
|
|
3357
|
+
/* ---- primitives: one function per code ---- */
|
|
2220
3358
|
|
|
2221
|
-
void
|
|
2222
|
-
|
|
2223
|
-
|
|
3359
|
+
void c0_builder_fs(c0_builder *b, const uint8_t *name, size_t nlen) {
|
|
3360
|
+
c0__b_end_record(b);
|
|
3361
|
+
c0__b_byte(b, C0_FS);
|
|
3362
|
+
c0__b_name(b, name, nlen);
|
|
2224
3363
|
}
|
|
2225
|
-
|
|
2226
|
-
void
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
c0_bytes *v = c0__cstrs(b, names, count);
|
|
2230
|
-
if (!v) return;
|
|
2231
|
-
c0_build_header(b, v, count);
|
|
2232
|
-
free(v);
|
|
3364
|
+
|
|
3365
|
+
void c0_builder_gs(c0_builder *b) {
|
|
3366
|
+
c0__b_end_record(b);
|
|
3367
|
+
c0__b_byte(b, C0_GS);
|
|
2233
3368
|
}
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
free(v);
|
|
3369
|
+
|
|
3370
|
+
void c0_builder_gs_name(c0_builder *b, const uint8_t *name, size_t nlen) {
|
|
3371
|
+
c0_builder_gs(b);
|
|
3372
|
+
c0__b_name(b, name, nlen);
|
|
2239
3373
|
}
|
|
2240
|
-
|
|
2241
|
-
void
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
3374
|
+
|
|
3375
|
+
void c0_builder_rs(c0_builder *b) {
|
|
3376
|
+
c0__b_byte(b, C0_RS);
|
|
3377
|
+
b->state = C0__B_RECORD;
|
|
3378
|
+
b->units = 0;
|
|
2245
3379
|
}
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
3380
|
+
|
|
3381
|
+
void c0_builder_rs_value(c0_builder *b, const uint8_t *value, size_t vlen) {
|
|
3382
|
+
c0_builder_rs(b);
|
|
3383
|
+
c0__b_first_value(b, value, vlen);
|
|
2249
3384
|
}
|
|
2250
|
-
|
|
2251
|
-
void
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
c0_build_list(b, v, count);
|
|
2255
|
-
free(v);
|
|
3385
|
+
|
|
3386
|
+
void c0_builder_us(c0_builder *b) {
|
|
3387
|
+
c0__b_byte(b, C0_US);
|
|
3388
|
+
b->units++;
|
|
2256
3389
|
}
|
|
2257
|
-
|
|
2258
|
-
void
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
free(v);
|
|
3390
|
+
|
|
3391
|
+
void c0_builder_us_value(c0_builder *b, const uint8_t *value, size_t vlen) {
|
|
3392
|
+
c0__b_byte(b, C0_US);
|
|
3393
|
+
c0__b_value(b, value, vlen);
|
|
3394
|
+
b->units++;
|
|
2263
3395
|
}
|
|
2264
|
-
void c0_build_note_str(c0_builder *b, const char *text) { c0_bytes t = c0__cstr(text); c0_build_note(b, t.ptr, t.len); }
|
|
2265
|
-
void c0_build_block_str(c0_builder *b, const char *text) { c0_bytes t = c0__cstr(text); c0_build_block(b, t.ptr, t.len); }
|
|
2266
|
-
void c0_build_item_str(c0_builder *b, const char *text) { c0_bytes t = c0__cstr(text); c0_build_item(b, t.ptr, t.len); }
|
|
2267
3396
|
|
|
2268
|
-
/*
|
|
3397
|
+
/* SUB prefixes the next code; it does not count as a unit. */
|
|
3398
|
+
void c0_builder_sub(c0_builder *b) { c0__b_byte(b, C0_SUB); }
|
|
2269
3399
|
|
|
2270
|
-
|
|
2271
|
-
* (layout after the payload belongs to the next block). */
|
|
2272
|
-
static size_t c0__payload_end(const uint8_t *buf, size_t pos, size_t len) {
|
|
2273
|
-
size_t p = c0__layout(buf, pos, len);
|
|
2274
|
-
if (p < len && buf[p] >= 0x20) {
|
|
2275
|
-
p = c0__plain(buf, p, len);
|
|
2276
|
-
while (p > pos && buf[p - 1] == C0_SP) p--;
|
|
2277
|
-
return p;
|
|
2278
|
-
}
|
|
2279
|
-
return pos;
|
|
2280
|
-
}
|
|
3400
|
+
void c0_builder_soh(c0_builder *b) { c0__b_byte(b, C0_SOH); }
|
|
2281
3401
|
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
it.stop = stop;
|
|
2286
|
-
it.pos = 0;
|
|
2287
|
-
it.block_start = 0;
|
|
2288
|
-
it.in_text = 0;
|
|
2289
|
-
it.depth = 0;
|
|
2290
|
-
return it;
|
|
3402
|
+
void c0_builder_soh_name(c0_builder *b, const uint8_t *name, size_t nlen) {
|
|
3403
|
+
c0__b_byte(b, C0_SOH);
|
|
3404
|
+
c0__b_name(b, name, nlen);
|
|
2291
3405
|
}
|
|
2292
3406
|
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
if (b == C0_DLE) {
|
|
2301
|
-
it->pos += 2;
|
|
2302
|
-
continue;
|
|
2303
|
-
} else if (b == C0_ETX) {
|
|
2304
|
-
it->in_text = 0;
|
|
2305
|
-
} else if (b == C0_ETB) {
|
|
2306
|
-
*out = c0__slice(buf, it->block_start, it->pos);
|
|
2307
|
-
if (damaged) *damaged = 1;
|
|
2308
|
-
it->pos = c0__payload_end(buf, it->pos + 1, it->stop);
|
|
2309
|
-
it->block_start = it->pos;
|
|
2310
|
-
it->in_text = 0;
|
|
2311
|
-
it->depth = 0;
|
|
2312
|
-
return 1;
|
|
2313
|
-
} else if (b == C0_SYN || b == C0_EOT || b == C0_EM) {
|
|
2314
|
-
it->in_text = 0;
|
|
2315
|
-
it->depth = 0;
|
|
2316
|
-
if (b == C0_SYN) it->block_start = it->pos;
|
|
2317
|
-
}
|
|
2318
|
-
it->pos++;
|
|
2319
|
-
} else {
|
|
2320
|
-
if (b == C0_STX) {
|
|
2321
|
-
it->in_text = 1;
|
|
2322
|
-
} else if (b == C0_DLE) {
|
|
2323
|
-
it->depth++;
|
|
2324
|
-
} else if (b == C0_CAN) {
|
|
2325
|
-
if (it->depth > 0) it->depth--;
|
|
2326
|
-
} else if (b == C0_ETB) {
|
|
2327
|
-
*out = c0__slice(buf, it->block_start, it->pos);
|
|
2328
|
-
if (damaged) *damaged = it->depth > 0;
|
|
2329
|
-
it->pos = c0__payload_end(buf, it->pos + 1, it->stop);
|
|
2330
|
-
it->block_start = it->pos;
|
|
2331
|
-
it->depth = 0;
|
|
2332
|
-
return 1;
|
|
2333
|
-
} else if (b == C0_SYN && it->pos > 0) {
|
|
2334
|
-
it->block_start = it->pos;
|
|
2335
|
-
it->depth = 0;
|
|
2336
|
-
}
|
|
2337
|
-
it->pos++;
|
|
3407
|
+
void c0_builder_dle(c0_builder *b) {
|
|
3408
|
+
if (b->depth + 2 > b->stack_cap) {
|
|
3409
|
+
size_t ncap = b->stack_cap ? b->stack_cap * 2 : 16;
|
|
3410
|
+
int *ns = (int *)realloc(b->stack, ncap * sizeof(int) * 2);
|
|
3411
|
+
if (!ns) {
|
|
3412
|
+
if (b->status == C0_OK) b->status = C0_ERR_OOM;
|
|
3413
|
+
return;
|
|
2338
3414
|
}
|
|
3415
|
+
b->stack = ns;
|
|
3416
|
+
b->stack_cap = ncap;
|
|
2339
3417
|
}
|
|
2340
|
-
|
|
3418
|
+
b->stack[2 * b->depth] = b->state;
|
|
3419
|
+
b->stack[2 * b->depth + 1] = (int)b->units;
|
|
3420
|
+
b->depth++;
|
|
3421
|
+
b->state = C0__B_TOP;
|
|
3422
|
+
b->units = 0;
|
|
3423
|
+
c0__b_byte(b, C0_DLE);
|
|
2341
3424
|
}
|
|
2342
3425
|
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
uint8_t b;
|
|
2348
|
-
pos = c0__layout(buf, pos, len);
|
|
2349
|
-
if (pos >= len) return 0;
|
|
2350
|
-
b = buf[pos];
|
|
2351
|
-
if (b == C0_EOT) {
|
|
2352
|
-
pos++;
|
|
2353
|
-
} else if (b == C0_EM) {
|
|
2354
|
-
return 0; /* the raw region is not data */
|
|
2355
|
-
} else if (b == C0_BEL) {
|
|
2356
|
-
if (c0__bel_kills_element(buf, pos, len)) return 1; /* a dead element is data */
|
|
2357
|
-
pos = c0__comment(buf, pos, len, &err);
|
|
2358
|
-
if (err.code) return 1;
|
|
2359
|
-
} else {
|
|
2360
|
-
return 1;
|
|
2361
|
-
}
|
|
3426
|
+
void c0_builder_can(c0_builder *b) {
|
|
3427
|
+
if (b->depth == 0) {
|
|
3428
|
+
if (b->status == C0_OK) b->status = C0_ERR_BAD_STATE;
|
|
3429
|
+
return;
|
|
2362
3430
|
}
|
|
3431
|
+
c0__b_byte(b, C0_CAN);
|
|
3432
|
+
b->depth--;
|
|
3433
|
+
b->state = b->stack[2 * b->depth];
|
|
3434
|
+
b->units = (size_t)b->stack[2 * b->depth + 1];
|
|
2363
3435
|
}
|
|
2364
3436
|
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
s.len = len;
|
|
2372
|
-
s.committed_end = 0;
|
|
2373
|
-
s.block_count = 0;
|
|
2374
|
-
s.damaged_count = 0;
|
|
2375
|
-
while (c0_next_block(&it, &blk, &damaged)) {
|
|
2376
|
-
s.block_count++;
|
|
2377
|
-
if (damaged) s.damaged_count++;
|
|
2378
|
-
s.committed_end = it.pos;
|
|
3437
|
+
void c0_builder_bel(c0_builder *b) { c0__b_byte(b, C0_BEL); }
|
|
3438
|
+
|
|
3439
|
+
void c0_builder_bel_text(c0_builder *b, const uint8_t *text, size_t tlen) {
|
|
3440
|
+
if (tlen == 0) { /* a note needs content: bare BEL marks the next element dead */
|
|
3441
|
+
if (b->status == C0_OK) b->status = C0_ERR_BAD_STATE;
|
|
3442
|
+
return;
|
|
2379
3443
|
}
|
|
2380
|
-
|
|
2381
|
-
|
|
3444
|
+
c0__b_byte(b, C0_BEL);
|
|
3445
|
+
c0__b_value(b, text, tlen);
|
|
2382
3446
|
}
|
|
2383
3447
|
|
|
2384
|
-
|
|
2385
|
-
|
|
3448
|
+
void c0_builder_enq(c0_builder *b, const uint8_t *name, size_t nlen) {
|
|
3449
|
+
c0__b_byte(b, C0_ENQ);
|
|
3450
|
+
if (nlen == 0 && b->status == C0_OK) b->status = C0_ERR_BAD_NAME;
|
|
3451
|
+
c0__b_name(b, name, nlen);
|
|
3452
|
+
}
|
|
2386
3453
|
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
3454
|
+
void c0_builder_enq_dle(c0_builder *b) {
|
|
3455
|
+
c0__b_byte(b, C0_ENQ);
|
|
3456
|
+
c0_builder_dle(b);
|
|
3457
|
+
}
|
|
3458
|
+
|
|
3459
|
+
void c0_builder_syn(c0_builder *b) {
|
|
3460
|
+
c0__b_end_record(b);
|
|
3461
|
+
c0__b_byte(b, C0_SYN);
|
|
3462
|
+
b->state = C0__B_HEADING;
|
|
3463
|
+
}
|
|
3464
|
+
|
|
3465
|
+
void c0_builder_eot(c0_builder *b) {
|
|
3466
|
+
c0__b_end_record(b);
|
|
3467
|
+
c0__b_byte(b, C0_EOT);
|
|
3468
|
+
}
|
|
3469
|
+
|
|
3470
|
+
void c0_builder_etb(c0_builder *b) {
|
|
3471
|
+
c0__b_end_record(b);
|
|
3472
|
+
c0__b_byte(b, C0_ETB);
|
|
3473
|
+
}
|
|
3474
|
+
|
|
3475
|
+
void c0_builder_etb_payload(c0_builder *b, const uint8_t *payload, size_t plen) {
|
|
3476
|
+
c0_builder_etb(b);
|
|
3477
|
+
c0__b_name(b, payload, plen);
|
|
2398
3478
|
}
|
|
2399
3479
|
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
if (buf[i] == C0_ETB) {
|
|
2405
|
-
size_t dles = 0, j = i;
|
|
2406
|
-
while (j > 0 && buf[j - 1] == C0_DLE) {
|
|
2407
|
-
dles++;
|
|
2408
|
-
j--;
|
|
2409
|
-
}
|
|
2410
|
-
if (dles % 2 == 0) return c0__plain(buf, i + 1, len);
|
|
2411
|
-
}
|
|
2412
|
-
}
|
|
2413
|
-
return 0;
|
|
3480
|
+
void c0_builder_em(c0_builder *b, const uint8_t *tail, size_t tlen) {
|
|
3481
|
+
c0__b_end_record(b);
|
|
3482
|
+
c0__b_byte(b, C0_EM);
|
|
3483
|
+
c0__b_raw(b, tail, tlen);
|
|
2414
3484
|
}
|
|
2415
3485
|
|
|
2416
|
-
|
|
2417
|
-
*
|
|
2418
|
-
*
|
|
2419
|
-
*
|
|
3486
|
+
void c0_builder_fs_str(c0_builder *b, const char *name) { c0_bytes n = c0__cstr(name); c0_builder_fs(b, n.ptr, n.len); }
|
|
3487
|
+
void c0_builder_gs_str(c0_builder *b, const char *name) { c0_bytes n = c0__cstr(name); c0_builder_gs_name(b, n.ptr, n.len); }
|
|
3488
|
+
void c0_builder_rs_str(c0_builder *b, const char *value) { c0_bytes v = c0__cstr(value); c0_builder_rs_value(b, v.ptr, v.len); }
|
|
3489
|
+
void c0_builder_us_str(c0_builder *b, const char *value) { c0_bytes v = c0__cstr(value); c0_builder_us_value(b, v.ptr, v.len); }
|
|
3490
|
+
void c0_builder_soh_str(c0_builder *b, const char *name) { c0_bytes n = c0__cstr(name); c0_builder_soh_name(b, n.ptr, n.len); }
|
|
3491
|
+
void c0_builder_bel_str(c0_builder *b, const char *text) { c0_bytes t = c0__cstr(text); c0_builder_bel_text(b, t.ptr, t.len); }
|
|
3492
|
+
void c0_builder_enq_str(c0_builder *b, const char *name) { c0_bytes n = c0__cstr(name); c0_builder_enq(b, n.ptr, n.len); }
|
|
3493
|
+
void c0_builder_etb_str(c0_builder *b, const char *payload) { c0_bytes p = c0__cstr(payload); c0_builder_etb_payload(b, p.ptr, p.len); }
|
|
3494
|
+
void c0_builder_em_str(c0_builder *b, const char *tail) { c0_bytes t = c0__cstr(tail); c0_builder_em(b, t.ptr, t.len); }
|
|
2420
3495
|
|
|
2421
|
-
|
|
3496
|
+
/* ---- composed: wrappers over the primitives ---- */
|
|
2422
3497
|
|
|
2423
|
-
|
|
2424
|
-
const uint8_t *buf;
|
|
2425
|
-
size_t len;
|
|
2426
|
-
int canonical;
|
|
2427
|
-
int ok;
|
|
2428
|
-
int diff;
|
|
2429
|
-
size_t depth;
|
|
2430
|
-
c0_error err;
|
|
2431
|
-
} c0__val;
|
|
3498
|
+
void c0_build_syn(c0_builder *b) { c0_builder_syn(b); }
|
|
2432
3499
|
|
|
2433
|
-
|
|
3500
|
+
void c0_build_eot(c0_builder *b) { c0_builder_eot(b); }
|
|
2434
3501
|
|
|
2435
|
-
|
|
3502
|
+
void c0_build_etb(c0_builder *b, const uint8_t *payload, size_t plen) {
|
|
3503
|
+
if (payload && plen) c0_builder_etb_payload(b, payload, plen);
|
|
3504
|
+
else c0_builder_etb(b);
|
|
3505
|
+
}
|
|
2436
3506
|
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
3507
|
+
void c0_build_em(c0_builder *b, const uint8_t *tail, size_t tlen) { c0_builder_em(b, tail, tlen); }
|
|
3508
|
+
|
|
3509
|
+
void c0_build_file(c0_builder *b, const uint8_t *name, size_t nlen) { c0_builder_fs(b, name, nlen); }
|
|
3510
|
+
|
|
3511
|
+
void c0_build_section(c0_builder *b, const uint8_t *name, size_t nlen, size_t depth) {
|
|
3512
|
+
size_t i;
|
|
3513
|
+
c0__b_end_record(b);
|
|
3514
|
+
for (i = 1; i < depth; i++) c0__b_byte(b, C0_SUB);
|
|
3515
|
+
c0_builder_gs_name(b, name, nlen);
|
|
2441
3516
|
}
|
|
2442
3517
|
|
|
2443
|
-
|
|
2444
|
-
static size_t c0__v_record(c0__val *v, size_t pos, size_t stop);
|
|
2445
|
-
static size_t c0__v_value(c0__val *v, size_t pos, size_t stop, int heading);
|
|
2446
|
-
static size_t c0__v_level(c0__val *v, size_t pos, size_t stop);
|
|
3518
|
+
void c0_build_group(c0_builder *b, const uint8_t *name, size_t nlen) { c0_builder_gs_name(b, name, nlen); }
|
|
2447
3519
|
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
while (c0_next_pair(&rest, &k2, &raw)) {
|
|
2455
|
-
if (k2.len == k.len && (k.len == 0 || memcmp(k.ptr, k2.ptr, k.len) == 0)) c0__v_nc(v);
|
|
3520
|
+
void c0_build_header(c0_builder *b, const c0_bytes *names, size_t count) {
|
|
3521
|
+
size_t i;
|
|
3522
|
+
for (i = 0; i < count; i++) { /* validate before writing anything */
|
|
3523
|
+
if (!c0_is_name(names[i].ptr, names[i].len)) {
|
|
3524
|
+
if (b->status == C0_OK) b->status = C0_ERR_BAD_NAME;
|
|
3525
|
+
return;
|
|
2456
3526
|
}
|
|
2457
3527
|
}
|
|
3528
|
+
c0__b_end_record(b);
|
|
3529
|
+
c0_builder_soh(b);
|
|
3530
|
+
c0_builder_rs(b);
|
|
3531
|
+
for (i = 0; i < count; i++) {
|
|
3532
|
+
if (i) c0_builder_us(b);
|
|
3533
|
+
c0__b_raw(b, names[i].ptr, names[i].len);
|
|
3534
|
+
}
|
|
3535
|
+
c0__b_end_record(b);
|
|
2458
3536
|
}
|
|
2459
3537
|
|
|
2460
|
-
|
|
2461
|
-
size_t
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "a name cannot be text");
|
|
2465
|
-
return stop;
|
|
2466
|
-
}
|
|
2467
|
-
start = pos;
|
|
2468
|
-
pos = c0__plain(v->buf, pos, stop);
|
|
2469
|
-
if (pos > start && c0_is_layout(v->buf[pos - 1])) c0__v_nc(v);
|
|
2470
|
-
return pos;
|
|
3538
|
+
void c0_build_record(c0_builder *b, const c0_bytes *fields, size_t count) {
|
|
3539
|
+
size_t i;
|
|
3540
|
+
c0_builder_rs(b);
|
|
3541
|
+
for (i = 0; i < count; i++) c0_build_field(b, fields[i].ptr, fields[i].len);
|
|
2471
3542
|
}
|
|
2472
3543
|
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
size_t p2;
|
|
2478
|
-
pos = c0__v_layout(v, pos, stop);
|
|
2479
|
-
if (!(pos < stop && buf[pos] == C0_BEL)) break;
|
|
2480
|
-
p2 = c0__layout(buf, pos + 1, stop);
|
|
2481
|
-
if (p2 >= stop) {
|
|
2482
|
-
c0__fail(&v->err, C0_ERR_UNEXPECTED_END, p2, "BEL at end of input");
|
|
2483
|
-
return stop;
|
|
2484
|
-
}
|
|
2485
|
-
if (buf[p2] < 0x20 && buf[p2] != C0_STX) break; /* a dead element, not a note */
|
|
2486
|
-
c0__v_nc(v);
|
|
2487
|
-
pos = buf[p2] == C0_STX ? c0__text(buf, p2, stop, &v->err) : c0__plain(buf, p2, stop);
|
|
2488
|
-
if (C0__V_ERR(v)) return stop;
|
|
2489
|
-
}
|
|
2490
|
-
return pos;
|
|
3544
|
+
void c0_build_field(c0_builder *b, const uint8_t *value, size_t vlen) {
|
|
3545
|
+
if (b->state != C0__B_RECORD) c0_builder_rs_value(b, value, vlen);
|
|
3546
|
+
else if (b->units == 0) c0__b_first_value(b, value, vlen);
|
|
3547
|
+
else c0_builder_us_value(b, value, vlen);
|
|
2491
3548
|
}
|
|
2492
3549
|
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
if (!(buf[p2] >= 0x20 || buf[p2] == C0_STX)) break;
|
|
2502
|
-
c0__v_nc(v);
|
|
2503
|
-
pos = buf[p2] == C0_STX ? c0__text(buf, p2, stop, &v->err) : c0__plain(buf, p2, stop);
|
|
2504
|
-
if (C0__V_ERR(v)) return stop;
|
|
2505
|
-
pos = c0__v_layout(v, pos, stop);
|
|
3550
|
+
/* A sub-unit hangs off the unit just written; the first unit of a record
|
|
3551
|
+
* exists even when empty, so one may follow a bare RS. It is not a unit:
|
|
3552
|
+
* the unit count is unchanged, except that the empty first unit now exists. */
|
|
3553
|
+
void c0_build_sub_unit(c0_builder *b, const uint8_t *value, size_t vlen, size_t depth) {
|
|
3554
|
+
size_t i;
|
|
3555
|
+
if (depth == 0) {
|
|
3556
|
+
if (b->status == C0_OK) b->status = C0_ERR_BAD_STATE;
|
|
3557
|
+
return;
|
|
2506
3558
|
}
|
|
2507
|
-
|
|
3559
|
+
if (b->state != C0__B_RECORD) c0_builder_rs(b);
|
|
3560
|
+
if (b->units == 0) b->units = 1;
|
|
3561
|
+
for (i = 0; i < depth; i++) c0__b_byte(b, C0_SUB);
|
|
3562
|
+
c0__b_byte(b, C0_US);
|
|
3563
|
+
c0__b_value(b, value, vlen);
|
|
2508
3564
|
}
|
|
2509
3565
|
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
if (C0__V_ERR(v)) return stop;
|
|
2515
|
-
if (!(pos < stop && v->buf[pos] == C0_US)) break;
|
|
2516
|
-
pos = c0__v_layout(v, pos + 1, stop);
|
|
2517
|
-
}
|
|
2518
|
-
return pos;
|
|
3566
|
+
void c0_build_map(c0_builder *b, const c0_bytes *keys, const c0_bytes *values, size_t count) {
|
|
3567
|
+
size_t i;
|
|
3568
|
+
c0_builder_rs(b);
|
|
3569
|
+
for (i = 0; i < count; i++) c0_build_key(b, keys[i].ptr, keys[i].len, values[i].ptr, values[i].len);
|
|
2519
3570
|
}
|
|
2520
3571
|
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
if (!(pos < stop && v->buf[pos] == C0_SOH)) return pos;
|
|
2525
|
-
p2 = c0__v_layout(v, pos + 1, stop);
|
|
2526
|
-
if (!(p2 < stop && v->buf[p2] == C0_RS)) return pos;
|
|
2527
|
-
return c0__v_header_names(v, p2 + 1, stop);
|
|
3572
|
+
void c0_build_key(c0_builder *b, const uint8_t *key, size_t klen, const uint8_t *value, size_t vlen) {
|
|
3573
|
+
c0__b_key_head(b, key, klen);
|
|
3574
|
+
c0_builder_us_value(b, value, vlen);
|
|
2528
3575
|
}
|
|
2529
3576
|
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
return stop;
|
|
2534
|
-
}
|
|
2535
|
-
c0__v_nc(v);
|
|
2536
|
-
return c0__plain(v->buf, c0__v_layout(v, pos + 1, stop), stop);
|
|
3577
|
+
void c0_build_level_open(c0_builder *b) {
|
|
3578
|
+
c0__b_next_unit(b);
|
|
3579
|
+
c0_builder_dle(b);
|
|
2537
3580
|
}
|
|
2538
3581
|
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
uint8_t b;
|
|
2544
|
-
pos = c0__v_layout(v, pos, stop);
|
|
2545
|
-
if (pos >= stop || C0__V_ERR(v)) break;
|
|
2546
|
-
b = buf[pos];
|
|
2547
|
-
if (b == C0_ETB) {
|
|
2548
|
-
pos = c0__v_commit(v, pos, stop, depth0);
|
|
2549
|
-
} else if (b == C0_BEL) {
|
|
2550
|
-
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2551
|
-
if (p2 >= stop) {
|
|
2552
|
-
c0__fail(&v->err, C0_ERR_UNEXPECTED_END, p2, "BEL at end of input");
|
|
2553
|
-
return stop;
|
|
2554
|
-
}
|
|
2555
|
-
if (buf[p2] >= 0x20 || buf[p2] == C0_STX) {
|
|
2556
|
-
pos = c0__v_notes(v, pos, stop);
|
|
2557
|
-
} else if (buf[p2] == C0_RS) {
|
|
2558
|
-
c0__v_nc(v);
|
|
2559
|
-
pos = c0__v_record(v, p2 + 1, stop);
|
|
2560
|
-
} else if (buf[p2] == C0_SOH) {
|
|
2561
|
-
size_t p3 = c0__layout(buf, p2 + 1, stop);
|
|
2562
|
-
if (p3 < stop && buf[p3] == C0_RS) {
|
|
2563
|
-
c0__v_nc(v);
|
|
2564
|
-
pos = c0__v_header_names(v, p3 + 1, stop);
|
|
2565
|
-
} else {
|
|
2566
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, p2, "keyed unit outside a record");
|
|
2567
|
-
return stop;
|
|
2568
|
-
}
|
|
2569
|
-
} else {
|
|
2570
|
-
break;
|
|
2571
|
-
}
|
|
2572
|
-
} else {
|
|
2573
|
-
break;
|
|
2574
|
-
}
|
|
2575
|
-
}
|
|
2576
|
-
return pos;
|
|
3582
|
+
void c0_build_key_level_open(c0_builder *b, const uint8_t *key, size_t klen) {
|
|
3583
|
+
c0__b_key_head(b, key, klen);
|
|
3584
|
+
c0_builder_us(b);
|
|
3585
|
+
c0_builder_dle(b);
|
|
2577
3586
|
}
|
|
2578
3587
|
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
} else if (b == C0_ETB) {
|
|
2589
|
-
pos = c0__v_commit(v, pos, stop, depth0);
|
|
2590
|
-
} else if (b == C0_BEL) {
|
|
2591
|
-
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2592
|
-
if (p2 >= stop || buf[p2] >= 0x20 || buf[p2] == C0_STX) {
|
|
2593
|
-
pos = c0__v_notes(v, pos, stop);
|
|
2594
|
-
} else if (buf[p2] == C0_RS) {
|
|
2595
|
-
c0__v_nc(v);
|
|
2596
|
-
pos = c0__v_record(v, p2 + 1, stop);
|
|
2597
|
-
} else if (buf[p2] == C0_SOH) {
|
|
2598
|
-
size_t p3 = c0__layout(buf, p2 + 1, stop);
|
|
2599
|
-
if (p3 < stop && buf[p3] == C0_RS) {
|
|
2600
|
-
c0__v_nc(v);
|
|
2601
|
-
pos = c0__v_header_names(v, p3 + 1, stop);
|
|
2602
|
-
} else {
|
|
2603
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, p2, "keyed unit outside a record");
|
|
2604
|
-
return stop;
|
|
2605
|
-
}
|
|
2606
|
-
} else if (buf[p2] == C0_DLE) {
|
|
2607
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, p2, "a nested level is not an item");
|
|
2608
|
-
return stop;
|
|
2609
|
-
} else {
|
|
2610
|
-
break; /* a dead GS or FS: the caller's business */
|
|
2611
|
-
}
|
|
2612
|
-
} else if (b == C0_SOH) {
|
|
2613
|
-
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2614
|
-
if (p2 < stop && buf[p2] == C0_RS) {
|
|
2615
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "header row not at the start of its group");
|
|
2616
|
-
} else {
|
|
2617
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "keyed unit outside a record");
|
|
2618
|
-
}
|
|
2619
|
-
return stop;
|
|
2620
|
-
} else {
|
|
2621
|
-
break;
|
|
2622
|
-
}
|
|
3588
|
+
void c0_build_level_close(c0_builder *b) { c0_builder_can(b); }
|
|
3589
|
+
|
|
3590
|
+
void c0_build_list(c0_builder *b, const c0_bytes *items, size_t count) {
|
|
3591
|
+
size_t i;
|
|
3592
|
+
c0__b_next_unit(b);
|
|
3593
|
+
c0_builder_dle(b);
|
|
3594
|
+
if (count) {
|
|
3595
|
+
c0_builder_rs(b);
|
|
3596
|
+
for (i = 0; i < count; i++) c0_build_field(b, items[i].ptr, items[i].len);
|
|
2623
3597
|
}
|
|
2624
|
-
|
|
3598
|
+
c0_builder_can(b);
|
|
3599
|
+
}
|
|
3600
|
+
|
|
3601
|
+
void c0_build_ref(c0_builder *b, const uint8_t *name, size_t nlen) {
|
|
3602
|
+
c0__b_next_unit(b);
|
|
3603
|
+
c0_builder_enq(b, name, nlen);
|
|
2625
3604
|
}
|
|
2626
3605
|
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
if (C0__V_ERR(v)) return stop;
|
|
2631
|
-
pos = c0__v_pre(v, pos, stop, depth0);
|
|
2632
|
-
pos = c0__v_header_row(v, pos, stop);
|
|
2633
|
-
return c0__v_items(v, pos, stop, depth0);
|
|
3606
|
+
void c0_build_ref_open(c0_builder *b) {
|
|
3607
|
+
c0__b_next_unit(b);
|
|
3608
|
+
c0_builder_enq_dle(b);
|
|
2634
3609
|
}
|
|
2635
3610
|
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
pos = c0__v_items(v, pos, stop, depth0);
|
|
2641
|
-
while (pos < stop && !C0__V_ERR(v)) {
|
|
2642
|
-
uint8_t b = buf[pos];
|
|
2643
|
-
if (b == C0_GS) {
|
|
2644
|
-
pos = c0__v_group(v, pos, stop, depth0);
|
|
2645
|
-
} else if (b == C0_BEL) {
|
|
2646
|
-
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2647
|
-
if (p2 < stop && buf[p2] == C0_GS) {
|
|
2648
|
-
c0__v_nc(v);
|
|
2649
|
-
pos = c0__v_group(v, p2, stop, depth0);
|
|
2650
|
-
} else {
|
|
2651
|
-
break;
|
|
2652
|
-
}
|
|
2653
|
-
} else {
|
|
2654
|
-
break;
|
|
2655
|
-
}
|
|
3611
|
+
void c0_build_ref_record(c0_builder *b, const uint8_t *group, size_t glen, const uint8_t *record, size_t rlen, const uint8_t *field, size_t flen) {
|
|
3612
|
+
if (glen == 0 || rlen == 0 || (field && flen == 0)) {
|
|
3613
|
+
if (b->status == C0_OK) b->status = C0_ERR_BAD_NAME;
|
|
3614
|
+
return;
|
|
2656
3615
|
}
|
|
2657
|
-
|
|
3616
|
+
c0_build_ref_open(b);
|
|
3617
|
+
c0_builder_gs_name(b, group, glen);
|
|
3618
|
+
c0_builder_rs(b);
|
|
3619
|
+
c0__b_name(b, record, rlen);
|
|
3620
|
+
b->units = 1;
|
|
3621
|
+
if (field) {
|
|
3622
|
+
c0_builder_us(b);
|
|
3623
|
+
c0__b_name(b, field, flen);
|
|
3624
|
+
}
|
|
3625
|
+
c0_builder_can(b);
|
|
2658
3626
|
}
|
|
2659
3627
|
|
|
2660
|
-
|
|
3628
|
+
void c0_build_note(c0_builder *b, const uint8_t *text, size_t tlen) { c0_builder_bel_text(b, text, tlen); }
|
|
2661
3629
|
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
while (pos < stop && !C0__V_ERR(v)) {
|
|
2668
|
-
uint8_t b = buf[pos];
|
|
2669
|
-
if (b == C0_FS) {
|
|
2670
|
-
pos = c0__v_plain_name(v, pos + 1, stop);
|
|
2671
|
-
pos = c0__v_part(v, pos, stop, depth0);
|
|
2672
|
-
} else if (b == C0_BEL) {
|
|
2673
|
-
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2674
|
-
if (p2 < stop && buf[p2] == C0_FS) {
|
|
2675
|
-
c0__v_nc(v);
|
|
2676
|
-
pos = c0__v_plain_name(v, p2 + 1, stop);
|
|
2677
|
-
pos = c0__v_part(v, pos, stop, depth0);
|
|
2678
|
-
} else {
|
|
2679
|
-
break;
|
|
2680
|
-
}
|
|
2681
|
-
} else {
|
|
2682
|
-
break;
|
|
2683
|
-
}
|
|
2684
|
-
}
|
|
2685
|
-
return C0__V_ERR(v) ? stop : pos;
|
|
3630
|
+
void c0_build_block(c0_builder *b, const uint8_t *text, size_t tlen) {
|
|
3631
|
+
c0_bytes f;
|
|
3632
|
+
f.ptr = text;
|
|
3633
|
+
f.len = tlen;
|
|
3634
|
+
c0_build_record(b, &f, 1);
|
|
2686
3635
|
}
|
|
2687
3636
|
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
3637
|
+
void c0_build_item(c0_builder *b, const uint8_t *text, size_t tlen) { c0_build_field(b, text, tlen); }
|
|
3638
|
+
|
|
3639
|
+
void c0_build_etb_str(c0_builder *b, const char *payload) {
|
|
3640
|
+
c0_bytes p = payload ? c0__cstr(payload) : c0__cstr("");
|
|
3641
|
+
c0_build_etb(b, p.ptr, p.len);
|
|
3642
|
+
}
|
|
3643
|
+
void c0_build_file_str(c0_builder *b, const char *name) { c0_bytes n = c0__cstr(name); c0_build_file(b, n.ptr, n.len); }
|
|
3644
|
+
void c0_build_group_str(c0_builder *b, const char *name) { c0_bytes n = c0__cstr(name); c0_build_group(b, n.ptr, n.len); }
|
|
3645
|
+
void c0_build_section_str(c0_builder *b, const char *name, size_t depth) { c0_bytes n = c0__cstr(name); c0_build_section(b, n.ptr, n.len, depth); }
|
|
3646
|
+
void c0_build_header_str(c0_builder *b, const char *const *names, size_t count) {
|
|
3647
|
+
c0_bytes *v = c0__cstrs(b, names, count);
|
|
3648
|
+
if (!v) return;
|
|
3649
|
+
c0_build_header(b, v, count);
|
|
3650
|
+
free(v);
|
|
3651
|
+
}
|
|
3652
|
+
void c0_build_record_str(c0_builder *b, const char *const *fields, size_t count) {
|
|
3653
|
+
c0_bytes *v = c0__cstrs(b, fields, count);
|
|
3654
|
+
if (!v) return;
|
|
3655
|
+
c0_build_record(b, v, count);
|
|
3656
|
+
free(v);
|
|
3657
|
+
}
|
|
3658
|
+
void c0_build_field_str(c0_builder *b, const char *value) { c0_bytes v = c0__cstr(value); c0_build_field(b, v.ptr, v.len); }
|
|
3659
|
+
void c0_build_sub_unit_str(c0_builder *b, const char *value, size_t depth) {
|
|
3660
|
+
c0_bytes v = c0__cstr(value);
|
|
3661
|
+
c0_build_sub_unit(b, v.ptr, v.len, depth);
|
|
3662
|
+
}
|
|
3663
|
+
void c0_build_map_str(c0_builder *b, const char *const *keys, const char *const *values, size_t count) {
|
|
3664
|
+
size_t i;
|
|
3665
|
+
c0_builder_rs(b);
|
|
3666
|
+
for (i = 0; i < count; i++) c0_build_key_str(b, keys[i], values[i]);
|
|
3667
|
+
}
|
|
3668
|
+
void c0_build_key_str(c0_builder *b, const char *key, const char *value) {
|
|
3669
|
+
c0_bytes k = c0__cstr(key), v = c0__cstr(value);
|
|
3670
|
+
c0_build_key(b, k.ptr, k.len, v.ptr, v.len);
|
|
3671
|
+
}
|
|
3672
|
+
void c0_build_key_level_open_str(c0_builder *b, const char *key) { c0_bytes k = c0__cstr(key); c0_build_key_level_open(b, k.ptr, k.len); }
|
|
3673
|
+
void c0_build_list_str(c0_builder *b, const char *const *items, size_t count) {
|
|
3674
|
+
c0_bytes *v = c0__cstrs(b, items, count);
|
|
3675
|
+
if (!v) return;
|
|
3676
|
+
c0_build_list(b, v, count);
|
|
3677
|
+
free(v);
|
|
3678
|
+
}
|
|
3679
|
+
void c0_build_ref_str(c0_builder *b, const char *name) { c0_bytes n = c0__cstr(name); c0_build_ref(b, n.ptr, n.len); }
|
|
3680
|
+
void c0_build_ref_record_str(c0_builder *b, const char *group, const char *record, const char *field) {
|
|
3681
|
+
c0_bytes g = c0__cstr(group), r = c0__cstr(record), f = field ? c0__cstr(field) : c0__cstr("");
|
|
3682
|
+
c0_build_ref_record(b, g.ptr, g.len, r.ptr, r.len, field ? f.ptr : NULL, f.len);
|
|
2705
3683
|
}
|
|
3684
|
+
void c0_build_note_str(c0_builder *b, const char *text) { c0_bytes t = c0__cstr(text); c0_build_note(b, t.ptr, t.len); }
|
|
3685
|
+
void c0_build_block_str(c0_builder *b, const char *text) { c0_bytes t = c0__cstr(text); c0_build_block(b, t.ptr, t.len); }
|
|
3686
|
+
void c0_build_item_str(c0_builder *b, const char *text) { c0_bytes t = c0__cstr(text); c0_build_item(b, t.ptr, t.len); }
|
|
2706
3687
|
|
|
2707
|
-
/*
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
return
|
|
2717
|
-
}
|
|
2718
|
-
vs = pos + 1;
|
|
2719
|
-
pos = c0__v_value(v, vs, stop, heading);
|
|
2720
|
-
if (heading && !C0__V_ERR(v)) {
|
|
2721
|
-
c0_bytes key = c0_trim(c0__slice(buf, ks, ke));
|
|
2722
|
-
c0_bytes val = c0_trim(c0__slice(buf, vs, pos));
|
|
2723
|
-
if (c0__eq_str(key, "shape") && c0__eq_str(val, "diff")) v->diff = 1;
|
|
3688
|
+
/* ---- stream ------------------------------------------------------------ */
|
|
3689
|
+
|
|
3690
|
+
/* After an ETB at pos-1: skip its payload; the result is committed_end
|
|
3691
|
+
* (layout after the payload belongs to the next block). */
|
|
3692
|
+
static size_t c0__payload_end(const uint8_t *buf, size_t pos, size_t len) {
|
|
3693
|
+
size_t p = c0__layout(buf, pos, len);
|
|
3694
|
+
if (p < len && buf[p] >= 0x20) {
|
|
3695
|
+
p = c0__plain(buf, p, len);
|
|
3696
|
+
while (p > pos && buf[p - 1] == C0_SP) p--;
|
|
3697
|
+
return p;
|
|
2724
3698
|
}
|
|
2725
3699
|
return pos;
|
|
2726
3700
|
}
|
|
2727
3701
|
|
|
2728
|
-
static
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
if (b == C0_SOH) {
|
|
2739
|
-
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2740
|
-
if (p2 < stop && buf[p2] == C0_RS) break;
|
|
2741
|
-
pos = c0__v_keyed_unit(v, pos + 1, stop, 0);
|
|
2742
|
-
} else if (b == C0_BEL) {
|
|
2743
|
-
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2744
|
-
if (p2 >= stop) {
|
|
2745
|
-
c0__fail(&v->err, C0_ERR_UNEXPECTED_END, p2, "BEL at end of input");
|
|
2746
|
-
return stop;
|
|
2747
|
-
}
|
|
2748
|
-
if (buf[p2] == C0_SOH) {
|
|
2749
|
-
size_t p3 = c0__layout(buf, p2 + 1, stop);
|
|
2750
|
-
if (p3 < stop && buf[p3] == C0_RS) break;
|
|
2751
|
-
c0__v_nc(v);
|
|
2752
|
-
pos = c0__v_keyed_unit(v, p2 + 1, stop, 0);
|
|
2753
|
-
} else if (buf[p2] >= 0x20 || buf[p2] == C0_STX) {
|
|
2754
|
-
pos = c0__v_notes(v, pos, stop);
|
|
2755
|
-
} else {
|
|
2756
|
-
break;
|
|
2757
|
-
}
|
|
2758
|
-
} else {
|
|
2759
|
-
break;
|
|
2760
|
-
}
|
|
2761
|
-
}
|
|
2762
|
-
if (C0__V_ERR(v)) return stop;
|
|
2763
|
-
c0__v_check_keys(v, rec_start, pos);
|
|
2764
|
-
if (pos < stop && buf[pos] == C0_US) {
|
|
2765
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "US after a keyed value (mixed record)");
|
|
2766
|
-
return stop;
|
|
2767
|
-
}
|
|
2768
|
-
return pos;
|
|
2769
|
-
}
|
|
2770
|
-
for (;;) {
|
|
2771
|
-
uint8_t b;
|
|
2772
|
-
pos = c0__v_value(v, pos, stop, 0);
|
|
2773
|
-
pos = c0__v_layout(v, pos, stop);
|
|
2774
|
-
if (pos >= stop || C0__V_ERR(v)) break;
|
|
2775
|
-
b = buf[pos];
|
|
2776
|
-
if (b == C0_US) {
|
|
2777
|
-
pos++;
|
|
2778
|
-
} else if (b == C0_SOH) {
|
|
2779
|
-
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2780
|
-
if (p2 < stop && buf[p2] == C0_RS) break;
|
|
2781
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "keyed unit in a positional record (mixed record)");
|
|
2782
|
-
return stop;
|
|
2783
|
-
} else {
|
|
2784
|
-
break;
|
|
2785
|
-
}
|
|
2786
|
-
}
|
|
2787
|
-
return C0__V_ERR(v) ? stop : pos;
|
|
3702
|
+
static c0_block_iter c0__blocks(const uint8_t *buf, size_t stop) {
|
|
3703
|
+
c0_block_iter it;
|
|
3704
|
+
it.buf = buf;
|
|
3705
|
+
it.stop = stop;
|
|
3706
|
+
it.pos = 0;
|
|
3707
|
+
it.block_start = 0;
|
|
3708
|
+
it.in_text = 0;
|
|
3709
|
+
it.index = 0;
|
|
3710
|
+
c0__st_init(&it.st);
|
|
3711
|
+
return it;
|
|
2788
3712
|
}
|
|
2789
3713
|
|
|
2790
|
-
|
|
2791
|
-
static int c0__v_inside_plain(const uint8_t *p, size_t n) {
|
|
2792
|
-
size_t i;
|
|
2793
|
-
if (n == 0) return 0;
|
|
2794
|
-
for (i = 0; i < n; i++) {
|
|
2795
|
-
if (p[i] < 0x20) return 0; /* a control byte, or a DLE escaping one */
|
|
2796
|
-
}
|
|
2797
|
-
return !c0_is_layout(p[0]) && !c0_is_layout(p[n - 1]);
|
|
2798
|
-
}
|
|
3714
|
+
c0_block_iter c0_stream_blocks(const c0_stream *s) { return c0__blocks(s->buf, s->committed_end); }
|
|
2799
3715
|
|
|
2800
|
-
|
|
2801
|
-
|
|
3716
|
+
/* Parse one block in the context carried in st: on success st advances,
|
|
3717
|
+
* on an error (the block is damaged) it is left as it was. */
|
|
3718
|
+
static int c0__block_sound(c0_parse_state *st, const uint8_t *buf, size_t start, size_t stop) {
|
|
3719
|
+
c0__parser p;
|
|
3720
|
+
c0_parse_state tmp = *st;
|
|
3721
|
+
c0__p_init(&p, buf, 0);
|
|
3722
|
+
c0__p_run(&p, &tmp, start, stop);
|
|
3723
|
+
if (C0__P_ERR(&p)) return 0;
|
|
3724
|
+
*st = tmp;
|
|
3725
|
+
return 1;
|
|
2802
3726
|
}
|
|
2803
3727
|
|
|
2804
|
-
|
|
2805
|
-
const uint8_t *buf =
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
if (n == 0 || c0__v_inside_plain(buf + start + 1, n)) c0__v_nc(v); /* empty or gratuitous text */
|
|
2821
|
-
}
|
|
2822
|
-
} else if (b == C0_DLE) {
|
|
2823
|
-
pos = c0__v_level(v, pos, stop);
|
|
2824
|
-
if (C0__V_ERR(v)) return stop;
|
|
2825
|
-
} else if (b == C0_ENQ) {
|
|
2826
|
-
if (heading) {
|
|
2827
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "reference in a heading");
|
|
2828
|
-
return stop;
|
|
2829
|
-
}
|
|
2830
|
-
pos = c0__v_layout(v, pos + 1, stop);
|
|
2831
|
-
if (pos >= stop || (buf[pos] < 0x20 && buf[pos] != C0_STX)) {
|
|
2832
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "reference without a name");
|
|
2833
|
-
return stop;
|
|
2834
|
-
}
|
|
2835
|
-
if (buf[pos] == C0_STX) {
|
|
2836
|
-
c0_ref r;
|
|
2837
|
-
size_t start = pos;
|
|
2838
|
-
pos = c0__text(buf, pos, stop, &v->err);
|
|
2839
|
-
if (C0__V_ERR(v)) return stop;
|
|
2840
|
-
if (c0_unit_ref(c0__slice(buf, start - 1, pos), &r) != C0_OK) {
|
|
2841
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, start, "reference path with an empty, extra or non-name segment");
|
|
2842
|
-
return stop;
|
|
3728
|
+
int c0_next_block(c0_block_iter *it, c0_bytes *out, int *damaged) {
|
|
3729
|
+
const uint8_t *buf = it->buf;
|
|
3730
|
+
while (it->pos < it->stop) {
|
|
3731
|
+
uint8_t b = buf[it->pos];
|
|
3732
|
+
int commit = 0;
|
|
3733
|
+
if (it->in_text) {
|
|
3734
|
+
if (b == C0_DLE) {
|
|
3735
|
+
it->pos += 2;
|
|
3736
|
+
continue;
|
|
3737
|
+
} else if (b == C0_ETX) {
|
|
3738
|
+
it->in_text = 0;
|
|
3739
|
+
} else if (b == C0_ETB) {
|
|
3740
|
+
commit = 1;
|
|
3741
|
+
} else if (b == C0_SYN || b == C0_EOT || b == C0_EM) {
|
|
3742
|
+
it->in_text = 0; /* framing cuts the text */
|
|
3743
|
+
if (b == C0_SYN) it->block_start = it->pos;
|
|
2843
3744
|
}
|
|
2844
|
-
if (r.count == 1) c0__v_nc(v); /* group reference written as text */
|
|
2845
3745
|
} else {
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
return stop;
|
|
3746
|
+
if (b == C0_STX) {
|
|
3747
|
+
it->in_text = 1;
|
|
3748
|
+
} else if (b == C0_ETB) {
|
|
3749
|
+
commit = 1;
|
|
3750
|
+
} else if (b == C0_SYN && it->pos > 0) {
|
|
3751
|
+
it->block_start = it->pos; /* tears the uncommitted bytes before it */
|
|
3752
|
+
}
|
|
2854
3753
|
}
|
|
2855
|
-
if (
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
3754
|
+
if (commit) {
|
|
3755
|
+
int sound = c0__block_sound(&it->st, buf, it->block_start, it->pos);
|
|
3756
|
+
*out = c0__slice(buf, it->block_start, it->pos);
|
|
3757
|
+
if (damaged) *damaged = !sound;
|
|
3758
|
+
it->pos = c0__payload_end(buf, it->pos + 1, it->stop);
|
|
3759
|
+
it->block_start = it->pos;
|
|
3760
|
+
it->in_text = 0;
|
|
3761
|
+
it->index++;
|
|
3762
|
+
return 1;
|
|
2863
3763
|
}
|
|
2864
|
-
|
|
2865
|
-
return c0__v_layout(v, pos, stop);
|
|
2866
|
-
} else {
|
|
2867
|
-
return pos;
|
|
2868
|
-
}
|
|
2869
|
-
after = c0__v_trailing_notes(v, c0__v_layout(v, pos, stop), stop);
|
|
2870
|
-
if (C0__V_ERR(v)) return stop;
|
|
2871
|
-
if (c0__v_second_value(buf, after, stop)) {
|
|
2872
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, after, "two values in one unit");
|
|
2873
|
-
return stop;
|
|
2874
|
-
}
|
|
2875
|
-
return after;
|
|
2876
|
-
}
|
|
2877
|
-
|
|
2878
|
-
static size_t c0__v_level(c0__val *v, size_t pos, size_t stop) {
|
|
2879
|
-
const uint8_t *buf = v->buf;
|
|
2880
|
-
uint8_t b;
|
|
2881
|
-
if (++v->depth > C0__V_MAX_DEPTH) {
|
|
2882
|
-
c0__fail(&v->err, C0_ERR_TOO_DEEP, pos, "levels nested too deep");
|
|
2883
|
-
return stop;
|
|
2884
|
-
}
|
|
2885
|
-
pos = c0__v_layout(v, pos + 1, stop);
|
|
2886
|
-
if (pos >= stop) {
|
|
2887
|
-
c0__fail(&v->err, C0_ERR_UNEXPECTED_END, pos, "level not closed");
|
|
2888
|
-
return stop;
|
|
2889
|
-
}
|
|
2890
|
-
b = buf[pos];
|
|
2891
|
-
if (b == C0_CAN) {
|
|
2892
|
-
v->depth--;
|
|
2893
|
-
return pos + 1;
|
|
2894
|
-
}
|
|
2895
|
-
if (!(b == C0_FS || b == C0_GS || b == C0_RS || b == C0_SOH || b == C0_BEL)) {
|
|
2896
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "DLE must be followed by a structural code");
|
|
2897
|
-
return stop;
|
|
2898
|
-
}
|
|
2899
|
-
pos = c0__v_body(v, pos, stop, 0);
|
|
2900
|
-
if (C0__V_ERR(v)) return stop;
|
|
2901
|
-
pos = c0__v_layout(v, pos, stop);
|
|
2902
|
-
if (pos >= stop) {
|
|
2903
|
-
c0__fail(&v->err, C0_ERR_UNEXPECTED_END, pos, "level not closed");
|
|
2904
|
-
return stop;
|
|
2905
|
-
}
|
|
2906
|
-
if (buf[pos] != C0_CAN) {
|
|
2907
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "unexpected byte inside level");
|
|
2908
|
-
return stop;
|
|
3764
|
+
it->pos++;
|
|
2909
3765
|
}
|
|
2910
|
-
|
|
2911
|
-
return pos + 1;
|
|
3766
|
+
return 0;
|
|
2912
3767
|
}
|
|
2913
3768
|
|
|
2914
|
-
/*
|
|
2915
|
-
static
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
if (b ==
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
3769
|
+
/* Is the tail after committed_end more than layout, notes, EOT or EM? */
|
|
3770
|
+
static int c0__torn_tail(const uint8_t *buf, size_t pos, size_t len) {
|
|
3771
|
+
c0_error err = {C0_OK, 0, ""};
|
|
3772
|
+
for (;;) {
|
|
3773
|
+
uint8_t b;
|
|
3774
|
+
pos = c0__layout(buf, pos, len);
|
|
3775
|
+
if (pos >= len) return 0;
|
|
3776
|
+
b = buf[pos];
|
|
3777
|
+
if (b == C0_EOT) {
|
|
3778
|
+
pos++;
|
|
3779
|
+
} else if (b == C0_EM) {
|
|
3780
|
+
return 0; /* the raw region is not data */
|
|
2926
3781
|
} else if (b == C0_BEL) {
|
|
2927
|
-
|
|
2928
|
-
pos = c0__comment(buf, pos,
|
|
2929
|
-
|
|
2930
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "plain text directly after SYN");
|
|
2931
|
-
return stop;
|
|
3782
|
+
if (c0__bel_kills_element(buf, pos, len)) return 1; /* a dead element is data */
|
|
3783
|
+
pos = c0__comment(buf, pos, len, &err);
|
|
3784
|
+
if (err.code) return 1;
|
|
2932
3785
|
} else {
|
|
2933
|
-
|
|
3786
|
+
return 1;
|
|
2934
3787
|
}
|
|
2935
|
-
pos = c0__v_layout(v, pos, stop);
|
|
2936
|
-
}
|
|
2937
|
-
if (C0__V_ERR(v)) return stop;
|
|
2938
|
-
c0__v_check_keys(v, hs, pos);
|
|
2939
|
-
return pos;
|
|
2940
|
-
}
|
|
2941
|
-
|
|
2942
|
-
/* ---- diff body ---- */
|
|
2943
|
-
|
|
2944
|
-
static size_t c0__v_dvalue(c0__val *v, size_t pos, size_t stop) {
|
|
2945
|
-
pos = c0__v_layout(v, pos, stop);
|
|
2946
|
-
if (pos >= stop) return pos;
|
|
2947
|
-
if (v->buf[pos] == C0_STX) {
|
|
2948
|
-
pos = c0__text(v->buf, pos, stop, &v->err);
|
|
2949
|
-
if (C0__V_ERR(v)) return stop;
|
|
2950
|
-
} else if (v->buf[pos] >= 0x20) {
|
|
2951
|
-
pos = c0__plain(v->buf, pos, stop);
|
|
2952
3788
|
}
|
|
2953
|
-
return c0__v_layout(v, pos, stop);
|
|
2954
3789
|
}
|
|
2955
3790
|
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
3791
|
+
c0_stream c0_stream_read(const uint8_t *buf, size_t len) {
|
|
3792
|
+
c0_stream s;
|
|
3793
|
+
c0_block_iter it = c0__blocks(buf, len);
|
|
3794
|
+
c0_bytes blk;
|
|
3795
|
+
int damaged;
|
|
3796
|
+
s.buf = buf;
|
|
3797
|
+
s.len = len;
|
|
3798
|
+
s.committed_end = 0;
|
|
3799
|
+
s.block_count = 0;
|
|
3800
|
+
s.damaged_count = 0;
|
|
3801
|
+
s.has_damaged = 0;
|
|
3802
|
+
s.first_damaged = 0;
|
|
3803
|
+
s.resume_offset = 0;
|
|
3804
|
+
while (c0_next_block(&it, &blk, &damaged)) {
|
|
3805
|
+
if (damaged) {
|
|
3806
|
+
if (!s.has_damaged) {
|
|
3807
|
+
s.has_damaged = 1;
|
|
3808
|
+
s.first_damaged = s.block_count;
|
|
3809
|
+
s.resume_offset = it.pos;
|
|
3810
|
+
}
|
|
3811
|
+
s.damaged_count++;
|
|
3812
|
+
}
|
|
3813
|
+
s.block_count++;
|
|
3814
|
+
s.committed_end = it.pos;
|
|
2964
3815
|
}
|
|
2965
|
-
|
|
3816
|
+
s.torn = c0__torn_tail(buf, s.committed_end, len);
|
|
3817
|
+
return s;
|
|
2966
3818
|
}
|
|
2967
3819
|
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
pos = c0__v_dvalue(v, pos, stop);
|
|
2971
|
-
if (C0__V_ERR(v)) return stop;
|
|
2972
|
-
if (c0_trim(c0__slice(v->buf, start, pos)).len == 0) {
|
|
2973
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, start, "replace-all with an empty old part");
|
|
2974
|
-
return stop;
|
|
2975
|
-
}
|
|
2976
|
-
if (!(pos < stop && v->buf[pos] == C0_SUB)) {
|
|
2977
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "replace-all without SUB");
|
|
2978
|
-
return stop;
|
|
2979
|
-
}
|
|
2980
|
-
return c0__v_dvalue(v, pos + 1, stop);
|
|
2981
|
-
}
|
|
3820
|
+
c0_bytes c0_stream_committed(const c0_stream *s) { return c0__slice(s->buf, 0, s->committed_end); }
|
|
3821
|
+
c0_bytes c0_stream_tail(const c0_stream *s) { return c0__slice(s->buf, s->committed_end, s->len); }
|
|
2982
3822
|
|
|
2983
|
-
static size_t
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
} else if (b == C0_RS) {
|
|
2993
|
-
pos = c0__v_replace(v, pos + 1, stop);
|
|
2994
|
-
} else if (b == C0_BEL) {
|
|
2995
|
-
size_t p2 = c0__layout(buf, pos + 1, stop);
|
|
2996
|
-
if (p2 < stop && buf[p2] == C0_FS) {
|
|
2997
|
-
pos = c0__v_plain_name(v, p2 + 1, stop);
|
|
2998
|
-
} else if (p2 < stop && buf[p2] == C0_GS) {
|
|
2999
|
-
pos = c0__v_pattern(v, p2 + 1, stop);
|
|
3000
|
-
} else if (p2 < stop && buf[p2] == C0_RS) {
|
|
3001
|
-
pos = c0__v_replace(v, p2 + 1, stop);
|
|
3002
|
-
} else {
|
|
3003
|
-
pos = c0__v_notes(v, pos, stop);
|
|
3004
|
-
}
|
|
3005
|
-
} else if (b == C0_ETB) {
|
|
3006
|
-
c0__v_nc(v);
|
|
3007
|
-
pos = c0__plain(buf, c0__v_layout(v, pos + 1, stop), stop);
|
|
3008
|
-
} else if (b == C0_EOT || b == C0_SYN || b == C0_EM) {
|
|
3009
|
-
break;
|
|
3010
|
-
} else {
|
|
3011
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "unexpected byte in a diff body");
|
|
3012
|
-
return stop;
|
|
3823
|
+
static size_t c0__stream_copy(const c0_stream *s, uint8_t *out, int stop_at_damage) {
|
|
3824
|
+
c0_block_iter it = c0_stream_blocks(s);
|
|
3825
|
+
c0_bytes blk;
|
|
3826
|
+
int damaged;
|
|
3827
|
+
size_t n = 0;
|
|
3828
|
+
while (c0_next_block(&it, &blk, &damaged)) {
|
|
3829
|
+
if (damaged) {
|
|
3830
|
+
if (stop_at_damage) break;
|
|
3831
|
+
continue;
|
|
3013
3832
|
}
|
|
3014
|
-
|
|
3833
|
+
if (blk.len) memcpy(out + n, blk.ptr, blk.len);
|
|
3834
|
+
n += blk.len;
|
|
3015
3835
|
}
|
|
3016
|
-
return
|
|
3836
|
+
return n;
|
|
3017
3837
|
}
|
|
3018
3838
|
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
while (
|
|
3025
|
-
|
|
3026
|
-
if (
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
c0__v_nc(v);
|
|
3032
|
-
return; /* the raw region follows */
|
|
3033
|
-
} else {
|
|
3034
|
-
if (!first) {
|
|
3035
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "a document after the first must begin with SYN");
|
|
3036
|
-
return;
|
|
3037
|
-
}
|
|
3038
|
-
pos = c0__v_body(v, pos, len, 1);
|
|
3039
|
-
if (C0__V_ERR(v)) return;
|
|
3040
|
-
pos = c0__v_layout(v, pos, len);
|
|
3041
|
-
first = 0;
|
|
3042
|
-
if (pos < len) {
|
|
3043
|
-
b = buf[pos];
|
|
3044
|
-
if (b == C0_EOT) {
|
|
3045
|
-
c0__v_nc(v);
|
|
3046
|
-
pos = c0__v_layout(v, pos + 1, len);
|
|
3047
|
-
if (pos < len && buf[pos] != C0_SYN && buf[pos] != C0_EM) {
|
|
3048
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "unexpected byte after EOT");
|
|
3049
|
-
return;
|
|
3050
|
-
}
|
|
3051
|
-
} else if (b == C0_SYN || b == C0_EM) {
|
|
3052
|
-
continue;
|
|
3053
|
-
} else if (b < 0x20 && !c0__assigned[b]) {
|
|
3054
|
-
c0__fail(&v->err, C0_ERR_UNASSIGNED, pos, "unassigned control code");
|
|
3055
|
-
return;
|
|
3056
|
-
} else {
|
|
3057
|
-
c0__fail(&v->err, C0_ERR_SYNTAX, pos, "unexpected byte after document");
|
|
3058
|
-
return;
|
|
3059
|
-
}
|
|
3839
|
+
size_t c0_stream_undamaged(const c0_stream *s, uint8_t *out) { return c0__stream_copy(s, out, 0); }
|
|
3840
|
+
size_t c0_stream_before_damage(const c0_stream *s, uint8_t *out) { return c0__stream_copy(s, out, 1); }
|
|
3841
|
+
|
|
3842
|
+
size_t c0_stream_last_commit_end(const uint8_t *buf, size_t len) {
|
|
3843
|
+
size_t i = len;
|
|
3844
|
+
while (i > 0) {
|
|
3845
|
+
i--;
|
|
3846
|
+
if (buf[i] == C0_ETB) {
|
|
3847
|
+
size_t dles = 0, j = i;
|
|
3848
|
+
while (j > 0 && buf[j - 1] == C0_DLE) {
|
|
3849
|
+
dles++;
|
|
3850
|
+
j--;
|
|
3060
3851
|
}
|
|
3852
|
+
if (dles % 2 == 0) return c0__plain(buf, i + 1, len);
|
|
3061
3853
|
}
|
|
3062
3854
|
}
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
static void c0__v_init(c0__val *v, const uint8_t *buf, size_t len, int canonical) {
|
|
3066
|
-
v->buf = buf;
|
|
3067
|
-
v->len = len;
|
|
3068
|
-
v->canonical = canonical;
|
|
3069
|
-
v->ok = 1;
|
|
3070
|
-
v->diff = 0;
|
|
3071
|
-
v->depth = 0;
|
|
3072
|
-
c0__err_clear(&v->err);
|
|
3073
|
-
}
|
|
3074
|
-
|
|
3075
|
-
c0_status c0_validate(const uint8_t *buf, size_t len, c0_error *err) {
|
|
3076
|
-
c0__val v;
|
|
3077
|
-
c0__v_init(&v, buf, len, 0);
|
|
3078
|
-
c0__v_run(&v);
|
|
3079
|
-
if (err) *err = v.err;
|
|
3080
|
-
return v.err.code;
|
|
3081
|
-
}
|
|
3082
|
-
|
|
3083
|
-
int c0_canonical(const uint8_t *buf, size_t len) {
|
|
3084
|
-
c0__val v;
|
|
3085
|
-
c0__v_init(&v, buf, len, 1);
|
|
3086
|
-
c0__v_run(&v);
|
|
3087
|
-
return v.err.code == C0_OK && v.ok;
|
|
3855
|
+
return 0;
|
|
3088
3856
|
}
|
|
3089
3857
|
|
|
3090
3858
|
/* ---- pretty form (pretty.cr) ------------------------------------------- */
|
|
@@ -3146,6 +3914,7 @@ typedef struct {
|
|
|
3146
3914
|
size_t depth; /* nesting depth (levels) */
|
|
3147
3915
|
size_t gdepth; /* group depth within the current level */
|
|
3148
3916
|
int ls; /* at the start of a line */
|
|
3917
|
+
int prefixed; /* a ␚ already opened this line (before a GS or FS line) */
|
|
3149
3918
|
size_t *stack;
|
|
3150
3919
|
size_t sn, scap;
|
|
3151
3920
|
c0_error *err;
|
|
@@ -3251,9 +4020,17 @@ static size_t c0__pf_units(c0__pf *f, size_t pos) {
|
|
|
3251
4020
|
c0__pf_data(f, c0_trim(c0__slice(buf, start, pos)));
|
|
3252
4021
|
} else if (c0_is_layout(b)) {
|
|
3253
4022
|
pos++;
|
|
3254
|
-
} else if (b == C0_US || b == C0_ENQ
|
|
4023
|
+
} else if (b == C0_US || b == C0_ENQ) {
|
|
3255
4024
|
c0__sb_glyph(f->o, b);
|
|
3256
4025
|
pos++;
|
|
4026
|
+
} else if (b == C0_SUB) {
|
|
4027
|
+
/* a sub-unit (a SUB run then US) is inside this record, on this
|
|
4028
|
+
* line; a SUB run before a line code prefixes the next element */
|
|
4029
|
+
size_t run, p2;
|
|
4030
|
+
c0__sub_run(buf, pos, len, &run, &p2);
|
|
4031
|
+
if (!(p2 < len && buf[p2] == C0_US)) break;
|
|
4032
|
+
while (run--) c0__sb_glyph(f->o, C0_SUB);
|
|
4033
|
+
pos = p2;
|
|
3257
4034
|
} else if (b == C0_SOH) {
|
|
3258
4035
|
size_t p2 = c0__layout(buf, pos + 1, len);
|
|
3259
4036
|
if (p2 < len && buf[p2] == C0_RS) break;
|
|
@@ -3307,6 +4084,12 @@ static size_t c0__pf_element(c0__pf *f, size_t pos) {
|
|
|
3307
4084
|
}
|
|
3308
4085
|
if (pos >= len) return pos;
|
|
3309
4086
|
return c0__pf_element(f, pos); /* a dead element, rendered on this line */
|
|
4087
|
+
} else if (b == C0_SUB) {
|
|
4088
|
+
/* a SUB before a line code stands with the element it prefixes */
|
|
4089
|
+
c0__sb_glyph(f->o, C0_SUB);
|
|
4090
|
+
pos = c0__layout(buf, pos + 1, len);
|
|
4091
|
+
if (pos >= len) return pos;
|
|
4092
|
+
return c0__pf_element(f, pos);
|
|
3310
4093
|
} else if (b == C0_SOH) {
|
|
3311
4094
|
c0__sb_glyph(f->o, C0_SOH);
|
|
3312
4095
|
pos = c0__layout(buf, pos + 1, len);
|
|
@@ -3410,28 +4193,46 @@ static void c0__pf_compact(c0__pf *f) {
|
|
|
3410
4193
|
f->gdepth = 0;
|
|
3411
4194
|
break;
|
|
3412
4195
|
case C0_FS:
|
|
3413
|
-
|
|
3414
|
-
|
|
4196
|
+
if (!f->prefixed) {
|
|
4197
|
+
c0__pf_nl(f);
|
|
4198
|
+
c0__pf_indent(f, f->depth);
|
|
4199
|
+
}
|
|
4200
|
+
f->prefixed = 0;
|
|
3415
4201
|
c0__sb_glyph(f->o, C0_FS);
|
|
3416
4202
|
pos = c0__pf_plain(f, pos + 1);
|
|
3417
4203
|
c0__sb_byte(f->o, '\n');
|
|
3418
4204
|
f->ls = 1;
|
|
3419
4205
|
f->gdepth = 1;
|
|
3420
4206
|
break;
|
|
3421
|
-
case C0_GS:
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3425
|
-
pos++;
|
|
4207
|
+
case C0_GS:
|
|
4208
|
+
if (!f->prefixed) {
|
|
4209
|
+
c0__pf_nl(f);
|
|
4210
|
+
c0__pf_indent(f, f->depth);
|
|
3426
4211
|
}
|
|
3427
|
-
|
|
3428
|
-
|
|
3429
|
-
|
|
3430
|
-
pos = c0__pf_plain(f, pos);
|
|
4212
|
+
f->prefixed = 0;
|
|
4213
|
+
c0__sb_glyph(f->o, C0_GS);
|
|
4214
|
+
pos = c0__pf_plain(f, pos + 1);
|
|
3431
4215
|
c0__sb_byte(f->o, '\n');
|
|
3432
4216
|
f->ls = 1;
|
|
3433
4217
|
f->gdepth = 1;
|
|
3434
4218
|
break;
|
|
4219
|
+
case C0_SUB: {
|
|
4220
|
+
/* a depth prefix stands on the line of the group it qualifies */
|
|
4221
|
+
size_t run, p2;
|
|
4222
|
+
c0__sub_run(buf, pos, len, &run, &p2);
|
|
4223
|
+
if (p2 < len && (buf[p2] == C0_GS || buf[p2] == C0_FS)) {
|
|
4224
|
+
c0__pf_nl(f);
|
|
4225
|
+
c0__pf_indent(f, f->depth);
|
|
4226
|
+
while (run--) c0__sb_glyph(f->o, C0_SUB);
|
|
4227
|
+
f->prefixed = 1;
|
|
4228
|
+
f->ls = 0;
|
|
4229
|
+
pos = p2;
|
|
4230
|
+
break;
|
|
4231
|
+
}
|
|
4232
|
+
c0__pf_nl(f);
|
|
4233
|
+
c0__pf_indent(f, f->depth + f->gdepth);
|
|
4234
|
+
pos = c0__pf_element(f, pos);
|
|
4235
|
+
break;
|
|
3435
4236
|
}
|
|
3436
4237
|
case C0_SOH:
|
|
3437
4238
|
case C0_RS:
|
|
@@ -3485,8 +4286,7 @@ static void c0__pf_compact(c0__pf *f) {
|
|
|
3485
4286
|
case C0_DLE:
|
|
3486
4287
|
case C0_ENQ:
|
|
3487
4288
|
case C0_US:
|
|
3488
|
-
|
|
3489
|
-
/* a unit-level code at element level (e.g. a diff pattern): inline */
|
|
4289
|
+
/* a unit-level code at element level: inline */
|
|
3490
4290
|
pos = c0__pf_units(f, pos);
|
|
3491
4291
|
c0__sb_byte(f->o, '\n');
|
|
3492
4292
|
f->ls = 1;
|