yeptris 0.6.64.0-riscv64-linux-gnu

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.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/README.adoc +150 -0
  3. data/ext/build_windows_native.rb +60 -0
  4. data/ext/libyeptris/extconf.rb +57 -0
  5. data/ext/yeptris_native/cbor_ruby.c +198 -0
  6. data/ext/yeptris_native/extconf.rb +99 -0
  7. data/ext/yeptris_native/json_ruby.c +414 -0
  8. data/ext/yeptris_native/version.h +29 -0
  9. data/ext/yeptris_native/yeptris_native.c +507 -0
  10. data/lib/yeptris/3.3/native.so +0 -0
  11. data/lib/yeptris/cbor.rb +169 -0
  12. data/lib/yeptris/document.rb +319 -0
  13. data/lib/yeptris/ffi.rb +572 -0
  14. data/lib/yeptris/json/descriptor.rb +266 -0
  15. data/lib/yeptris/json.rb +502 -0
  16. data/lib/yeptris/materializer.rb +389 -0
  17. data/lib/yeptris/node.rb +482 -0
  18. data/lib/yeptris/psych/class_loader.rb +24 -0
  19. data/lib/yeptris/psych/coder_shim.rb +55 -0
  20. data/lib/yeptris/psych/drop_in.rb +63 -0
  21. data/lib/yeptris/psych/encodable.rb +14 -0
  22. data/lib/yeptris/psych/handler.rb +95 -0
  23. data/lib/yeptris/psych/parser.rb +114 -0
  24. data/lib/yeptris/psych/scalar_scanner.rb +149 -0
  25. data/lib/yeptris/psych/tree_builder.rb +119 -0
  26. data/lib/yeptris/psych/visitors.rb +670 -0
  27. data/lib/yeptris/psych.rb +796 -0
  28. data/lib/yeptris/schema.rb +253 -0
  29. data/lib/yeptris/validation.rb +320 -0
  30. data/lib/yeptris/valueml.rb +477 -0
  31. data/lib/yeptris/yaml/descriptor.rb +56 -0
  32. data/lib/yeptris/yaml.rb +668 -0
  33. data/lib/yeptris.rb +130 -0
  34. data/libyeptris.so +0 -0
  35. data/vendor/libyeptris/CMakeLists.txt +280 -0
  36. data/vendor/libyeptris/cmake/yeptris-config.cmake.in +5 -0
  37. data/vendor/libyeptris/cmake/yeptris.pc.in +11 -0
  38. data/vendor/libyeptris/src/CMakeLists.txt +183 -0
  39. data/vendor/libyeptris/src/include/yeptris/api.h +32 -0
  40. data/vendor/libyeptris/src/include/yeptris/cbor.h +174 -0
  41. data/vendor/libyeptris/src/include/yeptris/dom.h +196 -0
  42. data/vendor/libyeptris/src/include/yeptris/emit.h +128 -0
  43. data/vendor/libyeptris/src/include/yeptris/error.h +42 -0
  44. data/vendor/libyeptris/src/include/yeptris/events.h +171 -0
  45. data/vendor/libyeptris/src/include/yeptris/json.h +57 -0
  46. data/vendor/libyeptris/src/include/yeptris/json.hpp +258 -0
  47. data/vendor/libyeptris/src/include/yeptris/jsonc_compat.h +115 -0
  48. data/vendor/libyeptris/src/include/yeptris/marshal.h +75 -0
  49. data/vendor/libyeptris/src/include/yeptris/parse.h +121 -0
  50. data/vendor/libyeptris/src/include/yeptris/plan.h +100 -0
  51. data/vendor/libyeptris/src/include/yeptris/resolve.h +81 -0
  52. data/vendor/libyeptris/src/include/yeptris/schema.h +121 -0
  53. data/vendor/libyeptris/src/include/yeptris/tape.h +128 -0
  54. data/vendor/libyeptris/src/include/yeptris/types.h +38 -0
  55. data/vendor/libyeptris/src/include/yeptris/validate.h +401 -0
  56. data/vendor/libyeptris/src/include/yeptris/values.h +108 -0
  57. data/vendor/libyeptris/src/include/yeptris/version.h.in +29 -0
  58. data/vendor/libyeptris/src/include/yeptris/visit.h +77 -0
  59. data/vendor/libyeptris/src/include/yeptris/yajl_compat.h +135 -0
  60. data/vendor/libyeptris/src/include/yeptris.h +24 -0
  61. data/vendor/libyeptris/src/yeptris/build.c +393 -0
  62. data/vendor/libyeptris/src/yeptris/cbor/cbor.h +35 -0
  63. data/vendor/libyeptris/src/yeptris/cbor/decode.c +1054 -0
  64. data/vendor/libyeptris/src/yeptris/cbor/encode.c +836 -0
  65. data/vendor/libyeptris/src/yeptris/cbor/encode_priv.h +32 -0
  66. data/vendor/libyeptris/src/yeptris/cbor/from_marshal.c +982 -0
  67. data/vendor/libyeptris/src/yeptris/cbor/sink.h +36 -0
  68. data/vendor/libyeptris/src/yeptris/common/chartype.c +41 -0
  69. data/vendor/libyeptris/src/yeptris/common/chartype.h +77 -0
  70. data/vendor/libyeptris/src/yeptris/common/cpu.c +81 -0
  71. data/vendor/libyeptris/src/yeptris/common/cpu.h +40 -0
  72. data/vendor/libyeptris/src/yeptris/common/error.c +65 -0
  73. data/vendor/libyeptris/src/yeptris/common/error.h +81 -0
  74. data/vendor/libyeptris/src/yeptris/common/mutex.h +45 -0
  75. data/vendor/libyeptris/src/yeptris/common/nametab.c +275 -0
  76. data/vendor/libyeptris/src/yeptris/common/nametab.h +68 -0
  77. data/vendor/libyeptris/src/yeptris/common/port.h +74 -0
  78. data/vendor/libyeptris/src/yeptris/common/simd_text.c +49 -0
  79. data/vendor/libyeptris/src/yeptris/common/simd_text.h +342 -0
  80. data/vendor/libyeptris/src/yeptris/common/simd_text_avx2.c +672 -0
  81. data/vendor/libyeptris/src/yeptris/common/simd_text_neon.c +793 -0
  82. data/vendor/libyeptris/src/yeptris/common/simd_text_scalar.c +324 -0
  83. data/vendor/libyeptris/src/yeptris/common/string_view.c +34 -0
  84. data/vendor/libyeptris/src/yeptris/common/string_view.h +93 -0
  85. data/vendor/libyeptris/src/yeptris/doc.h +86 -0
  86. data/vendor/libyeptris/src/yeptris/dom/dom.c +1312 -0
  87. data/vendor/libyeptris/src/yeptris/dom/dom.h +332 -0
  88. data/vendor/libyeptris/src/yeptris/dom/hpool.c +107 -0
  89. data/vendor/libyeptris/src/yeptris/dom/mapindex.c +173 -0
  90. data/vendor/libyeptris/src/yeptris/dom/mapindex.h +61 -0
  91. data/vendor/libyeptris/src/yeptris/dom/mutate.c +431 -0
  92. data/vendor/libyeptris/src/yeptris/dom/ytape.c +2323 -0
  93. data/vendor/libyeptris/src/yeptris/dom/ytape.h +96 -0
  94. data/vendor/libyeptris/src/yeptris/emit/emit.c +670 -0
  95. data/vendor/libyeptris/src/yeptris/emit/float/api.h +43 -0
  96. data/vendor/libyeptris/src/yeptris/emit/float/dragon.c +405 -0
  97. data/vendor/libyeptris/src/yeptris/emit/float/floatint.h +231 -0
  98. data/vendor/libyeptris/src/yeptris/emit/float/fpconv.c +428 -0
  99. data/vendor/libyeptris/src/yeptris/emit/float/print.c +396 -0
  100. data/vendor/libyeptris/src/yeptris/emit/style.c +21 -0
  101. data/vendor/libyeptris/src/yeptris/emit/style.h +32 -0
  102. data/vendor/libyeptris/src/yeptris/emit/writer.c +1951 -0
  103. data/vendor/libyeptris/src/yeptris/emit/writer.h +111 -0
  104. data/vendor/libyeptris/src/yeptris/encoding/bom.c +31 -0
  105. data/vendor/libyeptris/src/yeptris/encoding/encoding.h +63 -0
  106. data/vendor/libyeptris/src/yeptris/encoding/transcode.c +157 -0
  107. data/vendor/libyeptris/src/yeptris/encoding/utf8_validate.c +178 -0
  108. data/vendor/libyeptris/src/yeptris/events/capture.c +166 -0
  109. data/vendor/libyeptris/src/yeptris/events/capture.h +39 -0
  110. data/vendor/libyeptris/src/yeptris/events/iterparse.c +298 -0
  111. data/vendor/libyeptris/src/yeptris/events/pull.c +117 -0
  112. data/vendor/libyeptris/src/yeptris/events/push.c +151 -0
  113. data/vendor/libyeptris/src/yeptris/events/recorder.c +144 -0
  114. data/vendor/libyeptris/src/yeptris/events/values.c +630 -0
  115. data/vendor/libyeptris/src/yeptris/events/values_priv.h +53 -0
  116. data/vendor/libyeptris/src/yeptris/events/yaml_compat.c +86 -0
  117. data/vendor/libyeptris/src/yeptris/events/yaml_compat.h +90 -0
  118. data/vendor/libyeptris/src/yeptris/jsonapi/jsonc_compat.c +687 -0
  119. data/vendor/libyeptris/src/yeptris/jsonapi/yajl_compat.c +728 -0
  120. data/vendor/libyeptris/src/yeptris/marshal/to_json.c +650 -0
  121. data/vendor/libyeptris/src/yeptris/marshal.c +988 -0
  122. data/vendor/libyeptris/src/yeptris/memory/allocator.c +20 -0
  123. data/vendor/libyeptris/src/yeptris/memory/allocator.h +43 -0
  124. data/vendor/libyeptris/src/yeptris/memory/arena.c +150 -0
  125. data/vendor/libyeptris/src/yeptris/memory/arena.h +52 -0
  126. data/vendor/libyeptris/src/yeptris/memory/pool.c +133 -0
  127. data/vendor/libyeptris/src/yeptris/memory/pool.h +36 -0
  128. data/vendor/libyeptris/src/yeptris/parse/engine.c +6528 -0
  129. data/vendor/libyeptris/src/yeptris/parse/engine.h +96 -0
  130. data/vendor/libyeptris/src/yeptris/parse/events.h +137 -0
  131. data/vendor/libyeptris/src/yeptris/parse/numbers.c +374 -0
  132. data/vendor/libyeptris/src/yeptris/parse/numbers.h +33 -0
  133. data/vendor/libyeptris/src/yeptris/parse/scalars.c +435 -0
  134. data/vendor/libyeptris/src/yeptris/parse/scalars.h +87 -0
  135. data/vendor/libyeptris/src/yeptris/parse/stream_iter.c +358 -0
  136. data/vendor/libyeptris/src/yeptris/parse.c +1074 -0
  137. data/vendor/libyeptris/src/yeptris/plan.c +824 -0
  138. data/vendor/libyeptris/src/yeptris/resolve/compat11.c +339 -0
  139. data/vendor/libyeptris/src/yeptris/resolve/core12.c +153 -0
  140. data/vendor/libyeptris/src/yeptris/resolve/full12.c +201 -0
  141. data/vendor/libyeptris/src/yeptris/resolve/json12.c +109 -0
  142. data/vendor/libyeptris/src/yeptris/resolve/resolver.h +103 -0
  143. data/vendor/libyeptris/src/yeptris/resolve/tags.c +250 -0
  144. data/vendor/libyeptris/src/yeptris/scan/json.c +839 -0
  145. data/vendor/libyeptris/src/yeptris/scan/json.h +116 -0
  146. data/vendor/libyeptris/src/yeptris/scan/scan.c +862 -0
  147. data/vendor/libyeptris/src/yeptris/scan/scan.h +177 -0
  148. data/vendor/libyeptris/src/yeptris/schema.c +309 -0
  149. data/vendor/libyeptris/src/yeptris/tape.c +2375 -0
  150. data/vendor/libyeptris/src/yeptris/tape_in.h +39 -0
  151. data/vendor/libyeptris/src/yeptris/validate/compile.c +306 -0
  152. data/vendor/libyeptris/src/yeptris/validate/internal.h +347 -0
  153. data/vendor/libyeptris/src/yeptris/validate/jsonschema.c +1922 -0
  154. data/vendor/libyeptris/src/yeptris/validate/output.c +246 -0
  155. data/vendor/libyeptris/src/yeptris/validate/regexp.c +1600 -0
  156. data/vendor/libyeptris/src/yeptris/validate/registry.c +118 -0
  157. data/vendor/libyeptris/src/yeptris/validate/resources.c +601 -0
  158. data/vendor/libyeptris/src/yeptris/validate/stream.c +1155 -0
  159. data/vendor/libyeptris/src/yeptris/validate/validate.c +1043 -0
  160. data/vendor/libyeptris/src/yeptris/version.c +7 -0
  161. data/vendor/libyeptris/src/yeptris/visit/dom_visit.c +115 -0
  162. data/vendor/libyeptris/src/yeptris/visit/json_visit.c +293 -0
  163. data/vendor/libyeptris/src/yeptris/visit/yaml_visit.c +127 -0
  164. metadata +221 -0
@@ -0,0 +1,988 @@
1
+ /* marshal.c — Ruby Marshal 4.8 emission (TODO.restructure/21).
2
+ *
3
+ * Two passes over the value records (the emitter discipline): sizing
4
+ * computes the exact buffer, then emission is linear writes — both
5
+ * runs execute the SAME walker with the writer no-op'd, so they
6
+ * cannot drift. Per-open element counts come from one linear
7
+ * pre-pass; registration ids are assigned deterministically in
8
+ * record order.
9
+ *
10
+ * Format facts are empirical, pinned against Ruby 3.4 Marshal.dump /
11
+ * Marshal.load byte streams:
12
+ * - w_long small form: x==0 -> 0x00; |x| <= 122 -> sign*(|x|+5);
13
+ * size form: tag +-k (k = minimal magnitude bytes), payload =
14
+ * LE(m) for positives, LE(2^(8k)-m) for negatives.
15
+ * - integer 'i' while |x| < 2^30 (the dumper's boundary; the loader
16
+ * accepts wider, but byte-matching the dumper keeps streams
17
+ * diffable). Beyond: bignum 'l' + sign + w_long(limbs) + LE
18
+ * 16-bit limbs.
19
+ * - float 'f' + w_long(repr_len) + repr; inf/nan spell out.
20
+ * - UTF-8 string: 'I' '"' w_long(len) bytes 0x06 ':' 0x06 'E' 'T'.
21
+ * - object links '@' + w_long(index): the loader registers arrays,
22
+ * hashes, strings and FLOATS (0-based, stream order); integers,
23
+ * bignums, symbols and immediates never register.
24
+ *
25
+ * Semantics mirror the binding record walks exactly (the ':sym' plain
26
+ * scan, single-char y/n, dot-required floats); aliases to registered
27
+ * constructs become object links (identity preserved), aliases to
28
+ * immediates re-emit the value. Merge keys ('<<') and timestamps
29
+ * return YEPTRIS_ERROR_UNSUPPORTED — the host falls back to the walk.
30
+ */
31
+
32
+ #include <math.h>
33
+ #include <stdlib.h>
34
+ #include <string.h>
35
+
36
+ #include "common/error.h"
37
+ #include "doc.h"
38
+ #include "emit/float/api.h"
39
+ #include "events/values_priv.h"
40
+
41
+ #include <yeptris/error.h>
42
+ #include <yeptris/marshal.h>
43
+ #include <yeptris/resolve.h> /* YEPTRIS_TAG_BINARY (#168) */
44
+
45
+ typedef struct {
46
+ const char* name; /* into the value arena */
47
+ uint32_t len;
48
+ int has_obj; /* 1: registered construct -> obj_id */
49
+ uint64_t obj_id;
50
+ int is_scalar; /* copy for immediate re-emits */
51
+ YeptrisValue scalar;
52
+ } yep_anchor;
53
+
54
+ typedef struct {
55
+ const yep_value_ctx* c;
56
+ int compat; /* Psych float quirks apply ONLY under 1.1 compat
57
+ * (TODO.restructure/32): the core schema's float
58
+ * regexp has an OPTIONAL dot - 1e3 IS a core float */
59
+ char* buf;
60
+ size_t len;
61
+ size_t nreg;
62
+ int sizing; /* 1: count only (writer no-op) */
63
+ int oom;
64
+ int unsupported;
65
+ uint64_t last_reg; /* id of the most recently completed value's
66
+ * registration, if any */
67
+ int last_was_reg;
68
+ /* anchors: rebuilt identically each pass (deterministic order) */
69
+ yep_anchor* anchors;
70
+ size_t nanchors, anchors_cap;
71
+ /* per-open element counts (document order of OPEN records) */
72
+ const uint64_t* counts;
73
+ size_t open_seq;
74
+ } E;
75
+
76
+ static void e_put(E* e, const void* p, size_t n) {
77
+ if (!e->sizing && n != 0) {
78
+ memcpy(e->buf + e->len, p, n);
79
+ }
80
+ e->len += n;
81
+ }
82
+
83
+ static void e_byte(E* e, unsigned char b) {
84
+ e_put(e, &b, 1);
85
+ }
86
+
87
+ static void e_long(E* e, int64_t x) {
88
+ if (x == 0) {
89
+ e_byte(e, 0);
90
+ return;
91
+ }
92
+ int64_t m = x < 0 ? -x : x;
93
+ if (m <= 122) {
94
+ e_byte(e, (unsigned char)(x < 0 ? -(m + 5) : m + 5));
95
+ return;
96
+ }
97
+ /* width types: unsigned long is 32-bit on Windows (LLP64) and
98
+ truncates every magnitude >= 2^32 (limb-count bug: a 19-digit
99
+ int materialized as its low 32 bits). */
100
+ uint64_t um = (uint64_t)m;
101
+ int k = 0;
102
+ while (um != 0) {
103
+ um >>= 8;
104
+ k++;
105
+ }
106
+ e_byte(e, (unsigned char)(x < 0 ? -k : k));
107
+ uint64_t payload =
108
+ x < 0 ? ((k == 8 ? UINT64_C(0) : UINT64_C(1) << (8 * k)) - (uint64_t)m) : (uint64_t)m;
109
+ unsigned char b[8];
110
+ for (int i = 0; i < k; i++) {
111
+ b[i] = (unsigned char)(payload & 0xff);
112
+ payload >>= 8;
113
+ }
114
+ e_put(e, b, (size_t)k);
115
+ }
116
+
117
+ static void e_reg(E* e, uint64_t* id_out) {
118
+ uint64_t id = e->nreg++;
119
+ if (id_out != NULL) {
120
+ *id_out = id;
121
+ }
122
+ }
123
+
124
+ static void e_int(E* e, int64_t x) {
125
+ e->last_was_reg = 0;
126
+ if (x == INT64_MIN) { /* |x| overflows int64 arithmetic */
127
+ e_byte(e, 'l');
128
+ e_byte(e, '-');
129
+ e_long(e, 4); /* 2^63: 64 bits -> 4 16-bit limbs */
130
+ static const unsigned char limbs[8] = {0, 0, 0, 0, 0, 0, 0, 0x80};
131
+ e_put(e, limbs, 8);
132
+ return;
133
+ }
134
+ int64_t m = x < 0 ? -x : x;
135
+ if (m < (int64_t)1 << 30) {
136
+ e_byte(e, 'i');
137
+ e_long(e, x);
138
+ return;
139
+ }
140
+ /* bignum: sign + limb count + LE 16-bit limbs */
141
+ e_byte(e, 'l');
142
+ e_byte(e, x < 0 ? '-' : '+');
143
+ uint64_t um = (uint64_t)m;
144
+ int bits = 0;
145
+ while (um != 0) {
146
+ um >>= 1;
147
+ bits++;
148
+ }
149
+ int limbs = (bits + 15) / 16;
150
+ e_long(e, limbs);
151
+ unsigned char b[8] = {0};
152
+ uint64_t v = (uint64_t)m;
153
+ for (int i = 0; i < limbs * 2; i += 2) {
154
+ b[i] = (unsigned char)(v & 0xff);
155
+ b[i + 1] = (unsigned char)((v >> 8) & 0xff);
156
+ v >>= 16;
157
+ }
158
+ e_put(e, b, (size_t)(limbs * 2));
159
+ }
160
+
161
+ static void e_str(E* e, const char* p, uint32_t len) {
162
+ uint64_t id;
163
+ e_reg(e, &id); /* strings register at their '"' token */
164
+ e_byte(e, 'I');
165
+ e_byte(e, '"');
166
+ e_long(e, (int64_t)len);
167
+ e_put(e, p, len);
168
+ e_byte(e, 0x06); /* 1 ivar */
169
+ e_byte(e, ':');
170
+ e_byte(e, 0x06); /* :E — 1-byte symbol */
171
+ e_byte(e, 'E');
172
+ e_byte(e, 'T'); /* true: UTF-8 */
173
+ e->last_reg = id;
174
+ e->last_was_reg = 1;
175
+ }
176
+
177
+ static void e_sym(E* e, const char* p, uint32_t len) {
178
+ e->last_was_reg = 0;
179
+ e_byte(e, ':');
180
+ e_long(e, (int64_t)len);
181
+ e_put(e, p, len);
182
+ }
183
+
184
+ static void e_float(E* e, double d) {
185
+ uint64_t id;
186
+ e_reg(e, &id); /* the loader registers floats (dump links them) */
187
+ char repr[40];
188
+ const char* s = repr;
189
+ uint32_t rl;
190
+ if (isnan(d)) {
191
+ s = "nan";
192
+ rl = 3;
193
+ } else if (isinf(d)) {
194
+ s = d < 0 ? "-inf" : "inf";
195
+ rl = d < 0 ? 4 : 3;
196
+ } else {
197
+ int n = yep_d2s_shortest(d, repr);
198
+ repr[n] = '\0';
199
+ rl = (uint32_t)n;
200
+ }
201
+ e_byte(e, 'f');
202
+ e_long(e, (int64_t)rl);
203
+ e_put(e, s, rl);
204
+ e->last_reg = id;
205
+ e->last_was_reg = 1;
206
+ }
207
+
208
+ static void e_null(E* e) {
209
+ e->last_was_reg = 0;
210
+ e_byte(e, '0');
211
+ }
212
+
213
+ static void e_bool(E* e, int truthy) {
214
+ e->last_was_reg = 0;
215
+ e_byte(e, truthy ? 'T' : 'F');
216
+ }
217
+
218
+ /* Psych's plain-scalar symbol scan: implicit ':name' (not '::'). */
219
+ static int sym_scan(const char* p, uint32_t len) {
220
+ return len > 1 && p[0] == ':' && p[1] != ':';
221
+ }
222
+
223
+ /* The float dot-quirk, exactly as the walks decide it: without '.',
224
+ * ':' or a leading '.', Psych leaves the scalar a String. */
225
+ static int float_text_ok(const E* e, const char* p, uint32_t len) {
226
+ if (!e->compat) {
227
+ return 1; /* core_12: the resolver's verdict is the spec's */
228
+ }
229
+ if (len == 0 || p[0] == '.') {
230
+ return len != 0;
231
+ }
232
+ return memchr(p, '.', len) != NULL || memchr(p, ':', len) != NULL;
233
+ }
234
+
235
+ static const char* e_text(const E* e, const YeptrisValue* v) {
236
+ return v->len == 0 ? "" : e->c->arena + v->off;
237
+ }
238
+
239
+ static yep_anchor* anchor_find(E* e, const char* name, uint32_t len) {
240
+ /* newest binding wins: YAML lets a later &anchor shadow an
241
+ * earlier one of the same name (libyaml snapshot 3GZX) */
242
+ for (size_t i = e->nanchors; i > 0; i--) {
243
+ yep_anchor* a = &e->anchors[i - 1];
244
+ if (a->len == len && memcmp(a->name, name, len) == 0) {
245
+ return a;
246
+ }
247
+ }
248
+ return NULL;
249
+ }
250
+
251
+ static int anchor_add(E* e, const char* name, uint32_t len) {
252
+ if (e->nanchors == e->anchors_cap) {
253
+ size_t cap = e->anchors_cap ? e->anchors_cap * 2 : 16;
254
+ yep_anchor* na = realloc(e->anchors, cap * sizeof(*na));
255
+ if (na == NULL) {
256
+ return -1;
257
+ }
258
+ e->anchors = na;
259
+ e->anchors_cap = cap;
260
+ }
261
+ yep_anchor* a = &e->anchors[e->nanchors++];
262
+ memset(a, 0, sizeof(*a));
263
+ a->name = name;
264
+ a->len = len;
265
+ return 0;
266
+ }
267
+
268
+ /* Psych's integer shape (scalar_scanner): optional sign, no
269
+ * leading zero, digits with '_' / ',' separators only BETWEEN
270
+ * digits. Ints within int64 arrive as V_INT; this rebuilds the
271
+ * BEYOND-int64 texts Psych materializes as Integer (issue #31,
272
+ * TODO.restructure/43). */
273
+ static int psych_int_text(const char* p, uint32_t len) {
274
+ if (len == 0)
275
+ return 0;
276
+ if (p[0] == '-' || p[0] == '+') {
277
+ p++;
278
+ len--;
279
+ }
280
+ if (len == 0 || p[0] == '0')
281
+ return 0;
282
+ for (uint32_t i = 0; i < len; i++) {
283
+ char c = p[i];
284
+ if (c >= '0' && c <= '9')
285
+ continue;
286
+ if ((c == '_' || c == ',') && i + 1 < len && p[i + 1] >= '0' && p[i + 1] <= '9')
287
+ continue;
288
+ return 0;
289
+ }
290
+ return 1;
291
+ }
292
+
293
+ /* decimal text -> bignum limbs (base 2^16 LE), then Marshal's 'l':
294
+ * sign + w_long(limb count) + limbs. The gate (marshal_node: text
295
+ * under 1300 chars) bounds the digits at 1299 -> ceil(1299*log2(10)/16)
296
+ * = 270 limbs; the arrays are sized from that bound so no carry is
297
+ * ever dropped (the old 128-limb cap silently truncated 617..1299-digit
298
+ * values into wrong numbers). */
299
+ #define E_BIGNUM_LIMBS_MAX 272
300
+
301
+ static void e_bignum_text(E* e, const char* p, uint32_t len) {
302
+ int neg = 0;
303
+ if (p[0] == '-' || p[0] == '+') {
304
+ neg = p[0] == '-';
305
+ p++;
306
+ len--;
307
+ }
308
+ uint16_t limbs[E_BIGNUM_LIMBS_MAX];
309
+ memset(limbs, 0, sizeof(limbs));
310
+ int nl = 1;
311
+ for (uint32_t i = 0; i < len; i++) {
312
+ char c = p[i];
313
+ if (c == '_' || c == ',')
314
+ continue;
315
+ uint32_t carry = (uint32_t)(c - '0');
316
+ for (int j = 0; j < nl; j++) {
317
+ uint32_t t = (uint32_t)limbs[j] * 10u + carry;
318
+ limbs[j] = (uint16_t)(t & 0xffffu);
319
+ carry = t >> 16;
320
+ }
321
+ while (carry != 0 && nl < E_BIGNUM_LIMBS_MAX) {
322
+ limbs[nl++] = (uint16_t)(carry & 0xffffu);
323
+ carry >>= 16;
324
+ }
325
+ }
326
+ while (nl > 1 && limbs[nl - 1] == 0)
327
+ nl--;
328
+ e->last_was_reg = 0;
329
+ e_byte(e, 'l');
330
+ e_byte(e, neg ? '-' : '+');
331
+ e_long(e, nl);
332
+ unsigned char b[E_BIGNUM_LIMBS_MAX * 2];
333
+ for (int i = 0; i < nl; i++) {
334
+ b[2 * i] = (unsigned char)(limbs[i] & 0xff);
335
+ b[2 * i + 1] = (unsigned char)(limbs[i] >> 8);
336
+ }
337
+ e_put(e, b, (size_t)nl * 2);
338
+ }
339
+
340
+ static int e_scalar(E* e, const YeptrisValue* v) {
341
+ const char* p = e_text(e, v);
342
+ switch (v->kind) {
343
+ case YEP_V_NULL:
344
+ e_null(e);
345
+ return 0;
346
+ case YEP_V_BOOL:
347
+ if (v->len == 1) { /* Psych: single-char y/n stay Strings */
348
+ e_str(e, p, v->len);
349
+ } else {
350
+ e_bool(e, v->b);
351
+ }
352
+ return 0;
353
+ case YEP_V_INT:
354
+ e_int(e, (int64_t)v->p);
355
+ return 0;
356
+ case YEP_V_FLOAT:
357
+ if (float_text_ok(e, p, v->len)) {
358
+ double d;
359
+ memcpy(&d, &v->p, sizeof(d));
360
+ e_float(e, d);
361
+ } else {
362
+ e_str(e, p, v->len);
363
+ }
364
+ return 0;
365
+ case YEP_V_STR:
366
+ if (v->b == 1 && v->len < 1300 && v->len > 18 && psych_int_text(p, v->len)) {
367
+ e_bignum_text(e, p, v->len);
368
+ return 0;
369
+ }
370
+ if (v->b == 1 && sym_scan(p, v->len)) {
371
+ e_sym(e, p + 1, v->len - 1);
372
+ } else {
373
+ e_str(e, p, v->len);
374
+ }
375
+ return 0;
376
+ default:
377
+ return -1; /* unreachable: the pre-scan rejects timestamps */
378
+ }
379
+ }
380
+
381
+ static int e_alias(E* e, const YeptrisValue* v) {
382
+ const char* p = e_text(e, v);
383
+ yep_anchor* a = anchor_find(e, p, v->len);
384
+ if (a == NULL) {
385
+ e->unsupported = 1; /* forward reference: impossible in YAML */
386
+ return -1;
387
+ }
388
+ if (a->has_obj) {
389
+ e->last_was_reg = 0;
390
+ e_byte(e, '@');
391
+ e_long(e, (int64_t)a->obj_id);
392
+ return 0;
393
+ }
394
+ /* immediate target: re-emit the value (floats re-register, which
395
+ * matches the loader's own registration discipline) */
396
+ return e_scalar(e, &a->scalar);
397
+ }
398
+
399
+ /* One value at cursor i: scalar, alias, or OPEN..CLOSE subtree.
400
+ * Returns the cursor after the value, or -1 on bail. */
401
+ long e_value(E* e, size_t i, size_t end);
402
+
403
+ static long e_body(E* e, size_t i, size_t end) {
404
+ const YeptrisValue* v = &e->c->vals[i];
405
+ switch (v->kind) {
406
+ case YEP_V_SEQ_OPEN:
407
+ case YEP_V_MAP_OPEN: {
408
+ uint64_t id;
409
+ e_reg(e, &id);
410
+ e_byte(e, v->kind == YEP_V_SEQ_OPEN ? '[' : '{');
411
+ e_long(e, (int64_t)e->counts[e->open_seq++]); /* pre-computed */
412
+ size_t j = i + 1;
413
+ while (j < end) {
414
+ if (e->c->vals[j].kind == YEP_V_CLOSE) {
415
+ break;
416
+ }
417
+ long nx = e_value(e, j, end);
418
+ if (nx < 0) {
419
+ return nx;
420
+ }
421
+ j = (size_t)nx;
422
+ }
423
+ if (j >= end) {
424
+ return -1; /* unterminated container */
425
+ }
426
+ e->last_reg = id;
427
+ e->last_was_reg = 1;
428
+ return (long)j + 1;
429
+ }
430
+ case YEP_V_CLOSE:
431
+ case YEP_V_DOC:
432
+ return -1; /* only the driver emits around these */
433
+ case YEP_V_ALIAS:
434
+ return e_alias(e, v) == 0 ? (long)i + 1 : -1;
435
+ default:
436
+ return e_scalar(e, v) == 0 ? (long)i + 1 : -1;
437
+ }
438
+ }
439
+
440
+ /* ANCHOR decorates the value that follows: bind after it completes. */
441
+ long e_value(E* e, size_t i, size_t end) {
442
+ const YeptrisValue* v = &e->c->vals[i];
443
+ if (v->kind != YEP_V_ANCHOR) {
444
+ return e_body(e, i, end);
445
+ }
446
+ if (anchor_add(e, e->c->arena + v->off, v->len) != 0) {
447
+ e->oom = 1;
448
+ return -1;
449
+ }
450
+ /* the index is a LOCAL: the decorated value can itself contain
451
+ * anchors (e.g. &a [&b x]), and the recursion's own bindings must
452
+ * not clobber ours (ASAN: anchors[-1] write on the snapshots) */
453
+ size_t pend_idx = e->nanchors - 1;
454
+ long nx = e_value(e, i + 1, end);
455
+ if (nx >= 0) {
456
+ yep_anchor* a = &e->anchors[pend_idx];
457
+ if (e->last_was_reg) {
458
+ a->has_obj = 1;
459
+ a->obj_id = e->last_reg;
460
+ } else {
461
+ a->is_scalar = 1;
462
+ a->scalar = e->c->vals[i + 1];
463
+ }
464
+ }
465
+ e->last_was_reg = 0;
466
+ return nx;
467
+ }
468
+
469
+ /* ---- the driver ---- */
470
+
471
+ typedef struct {
472
+ size_t from, to;
473
+ } yep_seg;
474
+
475
+ /* One linear scan: rejection scan (timestamps, merge keys), the
476
+ * per-open counts, and the DOC segmentation. Returns 0 ok, -1 oom,
477
+ * -2 unsupported. */
478
+ static int pre_scan(const yep_value_ctx* c, uint64_t** counts_out, size_t* nopens_out,
479
+ yep_seg** segs_out, size_t* nsegs_out) {
480
+ const YeptrisValue* vals = c->vals;
481
+ size_t n = c->n;
482
+ size_t opens = 0;
483
+ size_t ndocs = 0;
484
+ for (size_t i = 0; i < n; i++) {
485
+ switch (vals[i].kind) {
486
+ case YEP_V_SEQ_OPEN:
487
+ case YEP_V_MAP_OPEN:
488
+ opens++;
489
+ break;
490
+ case YEP_V_DOC:
491
+ ndocs++;
492
+ break;
493
+ case YEP_V_TIMESTAMP:
494
+ return -2;
495
+ case YEP_V_STR:
496
+ if (vals[i].tag_id == YEPTRIS_TAG_BINARY) {
497
+ return -2; /* !binary: the walk base64-decodes (#168) */
498
+ }
499
+ if (vals[i].is_key == 1 && vals[i].len == 2 && c->arena[vals[i].off] == '<' &&
500
+ c->arena[vals[i].off + 1] == '<') {
501
+ return -2; /* merge key: the walk resolves these */
502
+ }
503
+ break;
504
+ default:
505
+ break;
506
+ }
507
+ }
508
+ uint64_t* counts = NULL;
509
+ if (opens > 0) {
510
+ counts = calloc(opens, sizeof(*counts));
511
+ if (counts == NULL) {
512
+ return -1;
513
+ }
514
+ }
515
+ /* counts: one stack pass — every value record credits its
516
+ * innermost open (sequences count elements, maps count pairs) */
517
+ size_t* idxs = NULL;
518
+ unsigned char* maps = NULL;
519
+ size_t sp = 0, cap = 0;
520
+ size_t oi = 0;
521
+ /* documents: content AFTER each DOC marker ([d+1, next d); no
522
+ * markers at all = one implicit document [0, n) — node subtrees
523
+ * take that path too) */
524
+ size_t* doc_pos = NULL;
525
+ size_t ndoc_seen = 0;
526
+ if (ndocs > 0) {
527
+ doc_pos = calloc(ndocs, sizeof(*doc_pos));
528
+ if (doc_pos == NULL) {
529
+ free(counts);
530
+ return -1;
531
+ }
532
+ }
533
+ yep_seg* segs = NULL;
534
+ size_t nsegs = 0;
535
+ int bad = 0;
536
+ {
537
+ size_t scap = (ndocs > 0 ? ndocs : 1);
538
+ segs = calloc(scap, sizeof(*segs));
539
+ if (segs == NULL) {
540
+ free(counts);
541
+ free(doc_pos);
542
+ return -1;
543
+ }
544
+ }
545
+ for (size_t i = 0; i < n && !bad; i++) {
546
+ unsigned char k = vals[i].kind;
547
+ if (k == YEP_V_SEQ_OPEN || k == YEP_V_MAP_OPEN) {
548
+ /* the open itself is a slot in its parent (complex keys
549
+ * aside, this is how sequences nest) */
550
+ if (sp > 0 && (maps[sp - 1] == 0 || vals[i].is_key == 1)) {
551
+ counts[idxs[sp - 1]]++;
552
+ }
553
+ if (sp == cap) {
554
+ size_t ncap = cap ? cap * 2 : 64;
555
+ size_t* ni = realloc(idxs, ncap * sizeof(*ni));
556
+ unsigned char* nm = realloc(maps, ncap);
557
+ if (ni == NULL || nm == NULL) {
558
+ free(ni);
559
+ free(nm);
560
+ bad = 1;
561
+ break;
562
+ }
563
+ idxs = ni;
564
+ maps = nm;
565
+ cap = ncap;
566
+ }
567
+ maps[sp] = (k == YEP_V_MAP_OPEN);
568
+ idxs[sp] = oi++;
569
+ counts[idxs[sp]] = 0;
570
+ sp++;
571
+ } else if (k == YEP_V_CLOSE) {
572
+ if (sp > 0) {
573
+ sp--;
574
+ }
575
+ } else if (k == YEP_V_ANCHOR) {
576
+ /* decorates the next record: no slot */
577
+ } else if (k == YEP_V_DOC) {
578
+ doc_pos[ndoc_seen++] = i;
579
+ } else if (sp > 0) {
580
+ if (maps[sp - 1] == 0 || vals[i].is_key == 1) {
581
+ counts[idxs[sp - 1]]++;
582
+ }
583
+ }
584
+ }
585
+ free(idxs);
586
+ free(maps);
587
+ if (!bad) {
588
+ if (ndocs == 0) {
589
+ segs[nsegs].from = 0;
590
+ segs[nsegs].to = n;
591
+ nsegs = 1;
592
+ } else {
593
+ for (size_t i = 0; i < ndocs; i++) {
594
+ segs[i].from = doc_pos[i] + 1;
595
+ segs[i].to = (i + 1 < ndocs) ? doc_pos[i + 1] : n;
596
+ }
597
+ nsegs = ndocs;
598
+ }
599
+ }
600
+ free(doc_pos);
601
+ if (bad) {
602
+ free(counts);
603
+ free(segs);
604
+ return -1;
605
+ }
606
+ *counts_out = counts;
607
+ *nopens_out = opens;
608
+ *segs_out = segs;
609
+ *nsegs_out = nsegs;
610
+ return 0;
611
+ }
612
+
613
+ static void e_init(E* e, const yep_value_ctx* c, int compat, const uint64_t* counts, char* buf,
614
+ int sizing) {
615
+ memset(e, 0, sizeof(*e));
616
+ e->c = c;
617
+ e->compat = compat;
618
+ e->counts = counts;
619
+ e->buf = buf;
620
+ e->sizing = sizing;
621
+ }
622
+
623
+ /* Runs the walker over one segment set per `mode`. Returns 0 ok,
624
+ * -1 oom, -2 unsupported, -3 drift (a bug). */
625
+ static int run_pass(E* e, YeptrisMarshalMode mode, int node_mode, const yep_seg* segs,
626
+ size_t nsegs) {
627
+ static const unsigned char hdr[2] = {0x04, 0x08};
628
+ e_put(e, hdr, 2);
629
+ if (node_mode) {
630
+ long nx = e_value(e, segs[0].from, segs[0].to);
631
+ return nx < 0 ? (e->unsupported ? -2 : (e->oom ? -1 : -3)) : 0;
632
+ }
633
+ if (mode == YEPTRIS_MARSHAL_FIRST_DOC) {
634
+ if (nsegs == 0 || segs[0].from == segs[0].to) {
635
+ e_null(e); /* empty stream / empty first document */
636
+ return 0;
637
+ }
638
+ long nx = e_value(e, segs[0].from, segs[0].to);
639
+ return nx < 0 ? (e->unsupported ? -2 : (e->oom ? -1 : -3)) : 0;
640
+ }
641
+ /* ALL_DOCS: one array of every document's root */
642
+ size_t ndocs = nsegs;
643
+ uint64_t id;
644
+ e_reg(e, &id);
645
+ e_byte(e, '[');
646
+ e_long(e, (int64_t)ndocs);
647
+ for (size_t i = 0; i < nsegs; i++) {
648
+ if (segs[i].from == segs[i].to) {
649
+ e_null(e); /* an empty document */
650
+ continue;
651
+ }
652
+ long nx = e_value(e, segs[i].from, segs[i].to);
653
+ if (nx < 0) {
654
+ return e->unsupported ? -2 : (e->oom ? -1 : -3);
655
+ }
656
+ }
657
+ return 0;
658
+ }
659
+
660
+ static YeptrisStatus map_fail(int rc) {
661
+ return rc == -1 ? YEPTRIS_ERROR_MEMORY : YEPTRIS_ERROR_UNSUPPORTED;
662
+ }
663
+
664
+ /* ---- strict duplicate-key pre-scan (yeptris-ruby#278) ----------------
665
+ *
666
+ * json 3 makes duplicate keys an error by DEFAULT; the marshal route
667
+ * must raise before any bytes are written, exactly like the native
668
+ * materializer's rb_hash_aref probe. One linear pass over the same
669
+ * records the writer consumes — the writer itself stays untouched
670
+ * (the dup law lives here, once). The JSON DOM keeps both pairs
671
+ * ("{\"a\":1,\"a\":2}" map_count=2), so the records see them; the
672
+ * non-strict Marshal stream stays last-wins, which is json 2.x's
673
+ * contract.
674
+ *
675
+ * Key identity is the record's typed payload: span kinds compare the
676
+ * arena bytes, the rest compare (kind, b, payload). Duplicates are
677
+ * per-map by definition, so the scan keeps ONE open-addressing table
678
+ * per open container on a frame stack: a key registers in the
679
+ * innermost frame and frames pop on CLOSE. A single table cleared on
680
+ * open cannot express this — a parent key equal to a key in an
681
+ * earlier child object was a false duplicate, and a parent duplicate
682
+ * after a nested object went unseen.
683
+ */
684
+
685
+ typedef struct {
686
+ uint64_t p; /* 0 = empty slot (payload 0 with kind 0 cannot be a key) */
687
+ uint32_t off, len;
688
+ uint8_t kind, b;
689
+ } dup_slot;
690
+
691
+ typedef struct {
692
+ dup_slot* slots;
693
+ size_t cap; /* power of two */
694
+ size_t count;
695
+ } dup_set;
696
+
697
+ typedef struct {
698
+ dup_set keys;
699
+ } dup_frame;
700
+
701
+ static uint64_t dup_hash(const dup_slot* k, const char* arena) {
702
+ uint64_t h = 1469598103934665603ull;
703
+ if (k->len != UINT32_MAX && (k->kind == YEP_V_STR || k->kind == YEP_V_TIMESTAMP ||
704
+ k->kind == YEP_V_ALIAS || k->kind == YEP_V_ANCHOR)) {
705
+ for (uint32_t i = 0; i < k->len; i++) {
706
+ h ^= (unsigned char)arena[k->off + i];
707
+ h *= 1099511628211ull;
708
+ }
709
+ }
710
+ h ^= k->kind;
711
+ h *= 1099511628211ull;
712
+ h ^= k->b;
713
+ h *= 1099511628211ull;
714
+ h ^= k->p;
715
+ h *= 1099511628211ull;
716
+ return h;
717
+ }
718
+
719
+ static int dup_eq(const dup_slot* a, const dup_slot* b, const char* arena) {
720
+ if (a->kind != b->kind || a->b != b->b || a->p != b->p) {
721
+ return 0;
722
+ }
723
+ int spanned = (a->kind == YEP_V_STR || a->kind == YEP_V_TIMESTAMP || a->kind == YEP_V_ALIAS ||
724
+ a->kind == YEP_V_ANCHOR);
725
+ if (!spanned) {
726
+ return 1;
727
+ }
728
+ if (a->len != b->len) {
729
+ return 0;
730
+ }
731
+ return a->len == 0 || memcmp(arena + a->off, arena + b->off, a->len) == 0;
732
+ }
733
+
734
+ static int dup_set_grow(dup_set* s, const char* arena) {
735
+ size_t ncap = s->cap ? s->cap * 2 : 16;
736
+ dup_slot* ns = calloc(ncap, sizeof(*ns));
737
+ if (ns == NULL) {
738
+ return -1;
739
+ }
740
+ for (size_t i = 0; i < s->cap; i++) {
741
+ dup_slot* old = &s->slots[i];
742
+ if (old->p == 0) {
743
+ continue;
744
+ }
745
+ uint64_t h = dup_hash(old, arena);
746
+ while (ns[h & (ncap - 1)].p != 0) {
747
+ h++;
748
+ }
749
+ ns[h & (ncap - 1)] = *old;
750
+ }
751
+ free(s->slots);
752
+ s->slots = ns;
753
+ s->cap = ncap;
754
+ return 0;
755
+ }
756
+
757
+ /* 1 = duplicate, 0 = inserted, -1 = oom. Load factor stays <= 1/2. */
758
+ static int dup_set_insert(dup_set* s, const dup_slot* k, const char* arena) {
759
+ if ((s->count + 1) * 2 > s->cap && dup_set_grow(s, arena) != 0) {
760
+ return -1;
761
+ }
762
+ uint64_t h = dup_hash(k, arena);
763
+ for (;;) {
764
+ dup_slot* slot = &s->slots[h & (s->cap - 1)];
765
+ if (slot->p == 0) {
766
+ *slot = *k;
767
+ s->count++;
768
+ return 0;
769
+ }
770
+ if (dup_eq(slot, k, arena)) {
771
+ return 1;
772
+ }
773
+ h++;
774
+ }
775
+ }
776
+
777
+ /* 1 = duplicate found (TLS error set), 0 = clean, -1 = oom */
778
+ static int strict_dup_scan(const yep_value_ctx* c) {
779
+ dup_frame* frames = NULL;
780
+ size_t depth = 0, cap = 0;
781
+ int rc = 0;
782
+ const YeptrisValue* vals = c->vals;
783
+ for (size_t i = 0; i < c->n && rc == 0; i++) {
784
+ const YeptrisValue* v = &vals[i];
785
+ if (v->kind == YEP_V_MAP_OPEN || v->kind == YEP_V_SEQ_OPEN) {
786
+ if (depth == cap) {
787
+ size_t ncap = cap ? cap * 2 : 16;
788
+ dup_frame* nf = realloc(frames, ncap * sizeof(*nf));
789
+ if (nf == NULL) {
790
+ rc = -1;
791
+ break;
792
+ }
793
+ frames = nf;
794
+ cap = ncap;
795
+ }
796
+ frames[depth].keys.slots = NULL;
797
+ frames[depth].keys.cap = 0;
798
+ frames[depth].keys.count = 0;
799
+ depth++;
800
+ continue;
801
+ }
802
+ if (v->kind == YEP_V_CLOSE) {
803
+ if (depth > 0) {
804
+ depth--;
805
+ free(frames[depth].keys.slots);
806
+ }
807
+ continue;
808
+ }
809
+ if (v->is_key != 1 || depth == 0) {
810
+ continue;
811
+ }
812
+ dup_slot k = {v->p, v->off, v->len, v->kind, v->b};
813
+ if (k.p == 0) {
814
+ k.p = 1; /* never aliases the empty-slot sentinel */
815
+ }
816
+ int irc = dup_set_insert(&frames[depth - 1].keys, &k, c->arena);
817
+ if (irc != 1) {
818
+ if (irc < 0) {
819
+ rc = -1;
820
+ }
821
+ continue;
822
+ }
823
+ /* name the key like the tape walk's message (json 3 names it
824
+ * too): the arena bytes are the text for the str-like kinds,
825
+ * which is every JSON key */
826
+ if (k.kind == YEP_V_STR || k.kind == YEP_V_TIMESTAMP || k.kind == YEP_V_ALIAS ||
827
+ k.kind == YEP_V_ANCHOR) {
828
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, 0,
829
+ "duplicate key \"%.*s\" in JSON object", (int)k.len,
830
+ k.len ? c->arena + k.off : "");
831
+ } else {
832
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, 0, "duplicate key in object");
833
+ }
834
+ rc = 1;
835
+ }
836
+ for (size_t j = 0; j < depth; j++) {
837
+ free(frames[j].keys.slots);
838
+ }
839
+ free(frames);
840
+ return rc;
841
+ }
842
+
843
+ static int dom_subtree_tagged(const yep_dom* d, uint32_t id);
844
+ static YeptrisStatus marshal_records(const yep_value_ctx* c, int compat, YeptrisMarshalMode mode,
845
+ int node_mode, char** out, size_t* out_len);
846
+
847
+ YEPTRIS_API YeptrisStatus yeptris_marshal_node_ex(YeptrisNode node, uint32_t flags, char** out,
848
+ size_t* out_len) {
849
+ if (node == NULL || out == NULL || out_len == NULL ||
850
+ (flags & ~YEPTRIS_MARSHAL_STRICT_DUP_KEYS) != 0) {
851
+ return YEPTRIS_ERROR_ARG;
852
+ }
853
+ *out = NULL;
854
+ *out_len = 0;
855
+ yeptris_node* h = (yeptris_node*)node;
856
+ yep_dom* dom = yep_doc_dom(h->doc); /* #378: a lazy parse carries the
857
+ * tape — the tree materializes here */
858
+ if (dom == NULL) {
859
+ return YEPTRIS_ERROR_MEMORY; /* the lazy route's retry contract */
860
+ }
861
+ if (dom_subtree_tagged(dom, h->id) != 0) {
862
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, 0,
863
+ "marshal: explicitly tagged node not expressible; "
864
+ "fall back to the value walk");
865
+ return YEPTRIS_ERROR_UNSUPPORTED;
866
+ }
867
+ yep_value_ctx* c = NULL;
868
+ if (yep_values_from_dom(dom, h->id, 0, &c) != 0) {
869
+ return YEPTRIS_ERROR_MEMORY;
870
+ }
871
+ if ((flags & YEPTRIS_MARSHAL_STRICT_DUP_KEYS) != 0) {
872
+ int drc = strict_dup_scan(c);
873
+ if (drc != 0) {
874
+ yep_value_ctx_free(c);
875
+ return drc < 0 ? YEPTRIS_ERROR_MEMORY : YEPTRIS_ERROR_PARSE;
876
+ }
877
+ }
878
+ YeptrisStatus st = marshal_records(c, h->doc->schema == YEPTRIS_SCHEMA_11_COMPAT,
879
+ YEPTRIS_MARSHAL_ALL_DOCS, 1, out, out_len);
880
+ if (st == YEPTRIS_ERROR_UNSUPPORTED) {
881
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, 0,
882
+ "marshal: merge key, timestamp or forward alias not expressible; "
883
+ "fall back to the value walk");
884
+ }
885
+ yep_value_ctx_free(c);
886
+ return st;
887
+ }
888
+
889
+ static YeptrisStatus marshal_records(const yep_value_ctx* c, int compat, YeptrisMarshalMode mode,
890
+ int node_mode, char** out, size_t* out_len) {
891
+ *out = NULL;
892
+ *out_len = 0;
893
+ uint64_t* counts = NULL;
894
+ yep_seg* segs = NULL;
895
+ size_t opens = 0, nsegs = 0;
896
+ int prc = pre_scan(c, &counts, &opens, &segs, &nsegs);
897
+ if (prc != 0) {
898
+ return map_fail(prc);
899
+ }
900
+ E e;
901
+ e_init(&e, c, compat, counts, NULL, 1);
902
+ int rc = run_pass(&e, mode, node_mode, segs, nsegs);
903
+ size_t total = e.len;
904
+ free(e.anchors);
905
+ if (rc != 0) {
906
+ free(counts);
907
+ free(segs);
908
+ return map_fail(rc);
909
+ }
910
+ char* buf = malloc(total > 0 ? total : 1);
911
+ if (buf == NULL) {
912
+ free(counts);
913
+ free(segs);
914
+ return YEPTRIS_ERROR_MEMORY;
915
+ }
916
+ e_init(&e, c, compat, counts, buf, 0);
917
+ rc = run_pass(&e, mode, node_mode, segs, nsegs);
918
+ free(e.anchors);
919
+ free(counts);
920
+ free(segs);
921
+ if (rc != 0 || e.len != total) { /* drift between passes is a bug */
922
+ free(buf);
923
+ return rc != 0 ? map_fail(rc) : YEPTRIS_ERROR_INTERNAL;
924
+ }
925
+ *out = buf;
926
+ *out_len = total;
927
+ return YEPTRIS_OK;
928
+ }
929
+
930
+ YEPTRIS_API YeptrisStatus yeptris_marshal(const char* data, size_t len, YeptrisSchema schema,
931
+ YeptrisMarshalMode mode, char** out, size_t* out_len) {
932
+ if (out == NULL || out_len == NULL || (data == NULL && len != 0) ||
933
+ (mode != YEPTRIS_MARSHAL_ALL_DOCS && mode != YEPTRIS_MARSHAL_FIRST_DOC)) {
934
+ return YEPTRIS_ERROR_ARG;
935
+ }
936
+ *out = NULL;
937
+ *out_len = 0;
938
+ yep_value_ctx* c = NULL;
939
+ int rc = yep_values_from_input(data, len, schema == YEPTRIS_SCHEMA_11_COMPAT, &c);
940
+ if (rc != 0) {
941
+ return rc == -1 ? YEPTRIS_ERROR_MEMORY : YEPTRIS_ERROR_PARSE;
942
+ }
943
+ YeptrisStatus st =
944
+ marshal_records(c, schema == YEPTRIS_SCHEMA_11_COMPAT, mode, 0, out, out_len);
945
+ if (st == YEPTRIS_ERROR_UNSUPPORTED) {
946
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, 0,
947
+ "marshal: merge key, timestamp or forward alias not expressible; "
948
+ "fall back to the value walk");
949
+ }
950
+ yep_value_ctx_free(c);
951
+ return st;
952
+ }
953
+
954
+ /* #178 (ruby #168's fast path): the value records carry the resolver's
955
+ * verdict, NOT the source's explicit tag — a `!ruby/object` mapping
956
+ * would marshal as a plain hash, silently dropping the tag (psych's
957
+ * visitor revives it). One cheap pre-walk: any explicitly tagged node
958
+ * in the subtree → UNSUPPORTED, the host walks. Explicit CORE tags
959
+ * (!!str and friends) ride the same bail — correct, just conservative;
960
+ * bulk data is untagged. */
961
+ static int dom_subtree_tagged(const yep_dom* d, uint32_t id) {
962
+ const yep_dnode* n = yep_dom_node(d, id);
963
+ if (n == NULL) {
964
+ return 0;
965
+ }
966
+ if (n->tag.len != 0) {
967
+ return 1;
968
+ }
969
+ for (uint32_t c = n->first_child; c != UINT32_MAX;) {
970
+ const yep_dnode* cn = yep_dom_node(d, c);
971
+ if (cn == NULL) {
972
+ break;
973
+ }
974
+ if (dom_subtree_tagged(d, c) != 0) {
975
+ return 1;
976
+ }
977
+ c = cn->next_sibling;
978
+ }
979
+ return 0;
980
+ }
981
+
982
+ YEPTRIS_API YeptrisStatus yeptris_marshal_node(YeptrisNode node, char** out, size_t* out_len) {
983
+ return yeptris_marshal_node_ex(node, 0, out, out_len);
984
+ }
985
+
986
+ YEPTRIS_API void yeptris_marshal_free(char* out) {
987
+ free(out);
988
+ }