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,111 @@
1
+ /* writer.h — internal emitter engine (TODO.impl/13). */
2
+ #ifndef YEP_EMIT_WRITER_H
3
+ #define YEP_EMIT_WRITER_H
4
+
5
+ #include <stdint.h>
6
+
7
+ #include "../common/nametab.h"
8
+ #include "../doc.h"
9
+ #include "../dom/dom.h"
10
+
11
+ /* Streaming high-water mark: the writer flushes when len crosses it
12
+ * (append-only output makes any point safe; 13C). */
13
+ #define YEP_EMIT_WATERMARK (1u << 16)
14
+
15
+ typedef int (*yep_emit_flush)(void* ctx, const char* bytes, size_t len);
16
+
17
+ typedef struct yep_writer {
18
+ char* p; /* output (wet) */
19
+ size_t len; /* bytes written (or counted, dry) */
20
+ int dry; /* 1: size only */
21
+ char last; /* last byte written ('\0' at start) — dry-safe */
22
+ int force_flow; /* complex-key emission: collections render flow */
23
+ yep_emit_flush sink; /* 13C: streaming flush (NULL = buffered) */
24
+ void* sink_ctx;
25
+ size_t watermark;
26
+ size_t flushed; /* total bytes handed to the sink */
27
+ int sink_aborted; /* a sink returned nonzero: stop, report 0 */
28
+ int canonical;
29
+ int explicit_doc_start;
30
+ /* every document carries the --- marker */ /* 13B: fixed canonical form — flow collections,
31
+ * quoted strings, typed words, shortest floats
32
+ */
33
+ int json; /* 21: JSON output — canonical minus YAML words:
34
+ * null (not ~), no document markers, JSON escapes */
35
+ int json_compact; /* json-c to_json_string: no padding spaces */
36
+ int json_pretty; /* json-c PRETTY: 2-space indent, per-entry lines */
37
+ int pretty_depth; /* json_pretty nesting level (indent = 2*depth) */
38
+ const char* json_indent; /* json_pretty indent unit; NULL = 2 spaces */
39
+ int json_indent_len; /* json_pretty unit byte length */
40
+ int best_width; /* 13B: 0 = default 80; flow lines wrap after a
41
+ * value past this width; with block_fold it is
42
+ * also the block scalar fold column (#903) */
43
+ int indent; /* #903: block indent per level (2..9, default
44
+ * 2): nested map content and seq item columns */
45
+ int block_fold; /* #903: plain/quoted block scalars fold at
46
+ * best_width (libyaml's plain-fold walk) */
47
+ int col; /* current output column (wr_* maintains) */
48
+ const char* sv_input; /* compact-view decode bases (dom regions) */
49
+ const char* sv_arena;
50
+ /* #352: per-scalar route decisions, derived once in the dry
51
+ * sizing pass and consumed by the wet write pass (one byte per
52
+ * scalar, indexed by consumption order; NULL = derive per pass) */
53
+ uint8_t* sc_dec;
54
+ uint32_t sc_i;
55
+ /* #352 slice 2: the growable single-pass route — the exact-sizing
56
+ * dry pass measured 0.54-0.73x of the one-pass stream route on
57
+ * CI-fresh runners, so serialize_ex writes wet into a buffer that
58
+ * doubles on demand (cap/grow), like the streaming window. The
59
+ * fixed-buffer routes keep grow=0 and today's trust-the-count
60
+ * behavior. oom marks a failed growth: writes stop touching memory
61
+ * but the byte count keeps running so callers can fail cleanly. */
62
+ size_t cap;
63
+ int grow;
64
+ int oom;
65
+ int bounded; /* #352: caller-owned fixed buffer — an oversized write
66
+ sets oom and keeps counting (the needed size falls
67
+ out of the same pass); wr_grow is never called */
68
+ /* #739 lane 1: writable bytes guaranteed at w->p + w->len. The
69
+ * grow/flush checks run once per refill instead of once per byte;
70
+ * 0 at run entry and whenever the guarantee lapses. */
71
+ size_t reserve;
72
+ int in_flow; /* scalars inside flow collections: plain must drop
73
+ ',', '[', ']', '{', '}' — flow terminators */
74
+ int flow_item; /* the NEXT scalar is a flow-SEQ item: a null must
75
+ emit '~' (a bare nothing reads as "[, x]") */
76
+ int unserializable; /* an anchor name that cannot ride raw in flow
77
+ context: the document has no safe spelling —
78
+ serialize fails rather than corrupt */
79
+ const struct yep_resolver* resolver; /* the document's implicit-typing
80
+ family (NULL = core12): the INT arm's decode needs the family's
81
+ own forms (leading-0 is octal in compat, decimal in core) */
82
+ } yep_writer;
83
+
84
+ typedef struct yep_emitter {
85
+ const yeptris_document* doc;
86
+ yep_writer w;
87
+ yep_nametab canon_names; /* canonical anchor renaming: view -> index */
88
+ } yep_emitter;
89
+
90
+ /* One pass over the document set. dry=1 counts exactly. */
91
+ size_t yep_emit_run(yep_emitter* em, int dry);
92
+
93
+ /* The write primitives, shared with the marshal reader
94
+ * (marshal/to_json.c — the move keeps ONE writer engine; #904 item 5).
95
+ * Internal: never in a public header. */
96
+ void wr_col_update(yep_writer* w, const char* p, uint32_t n);
97
+ void wr_reserve(yep_writer* w, size_t need);
98
+ void wr_put(yep_writer* w, const char* p, uint32_t n);
99
+ void wr_byte(yep_writer* w, char c);
100
+ void emit_dq_json(yep_writer* w, const char* p, uint32_t n);
101
+
102
+ /* Compact single-line JSON (internal entries for the json-c compat
103
+ * layer's to_json_string forms). pretty selects json-c's
104
+ * JSON_C_TO_STRING_PRETTY layout: newline + 2-space indent per
105
+ * nesting level, one entry per line. */
106
+ char* yep_serialize_json_compact(const yeptris_document* doc, size_t* len);
107
+ char* yep_serialize_json_pretty(const yeptris_document* doc, size_t* len);
108
+ char* yep_serialize_json_pretty_ex(const yeptris_document* doc, size_t* len, const char* indent,
109
+ size_t indent_len);
110
+
111
+ #endif /* YEP_EMIT_WRITER_H */
@@ -0,0 +1,31 @@
1
+ /* bom.c — byte-order-mark sniffing (TODO.impl/05). */
2
+
3
+ #include "encoding.h"
4
+
5
+ size_t yep_bom_sniff(const unsigned char* p, size_t len, yep_encoding* out) {
6
+ yep_encoding enc = YEP_ENC_UNKNOWN;
7
+ size_t consumed = 0;
8
+
9
+ /* UTF-32LE (FF FE 00 00) must be tested before UTF-16LE (FF FE). */
10
+ if (len >= 4 && p[0] == 0xFF && p[1] == 0xFE && p[2] == 0x00 && p[3] == 0x00) {
11
+ enc = YEP_ENC_UTF32LE;
12
+ consumed = 4;
13
+ } else if (len >= 3 && p[0] == 0xEF && p[1] == 0xBB && p[2] == 0xBF) {
14
+ enc = YEP_ENC_UTF8;
15
+ consumed = 3;
16
+ } else if (len >= 4 && p[0] == 0x00 && p[1] == 0x00 && p[2] == 0xFE && p[3] == 0xFF) {
17
+ enc = YEP_ENC_UTF32BE;
18
+ consumed = 4;
19
+ } else if (len >= 2 && p[0] == 0xFF && p[1] == 0xFE) {
20
+ enc = YEP_ENC_UTF16LE;
21
+ consumed = 2;
22
+ } else if (len >= 2 && p[0] == 0xFE && p[1] == 0xFF) {
23
+ enc = YEP_ENC_UTF16BE;
24
+ consumed = 2;
25
+ }
26
+
27
+ if (out != NULL) {
28
+ *out = enc;
29
+ }
30
+ return consumed;
31
+ }
@@ -0,0 +1,63 @@
1
+ /* encoding.h — the encoding front-end (TODO.impl/05).
2
+ *
3
+ * MECE law: charset truth lives ONLY here. The scan layer receives a
4
+ * buffer guaranteed valid UTF-8 and never re-validates; output is always
5
+ * UTF-8 (the libleptris serialization guarantee).
6
+ *
7
+ * Zero-copy policy: UTF-8 input is validated in place and borrowed;
8
+ * non-UTF-8 input is transcoded once (into a buffer allocated via the
9
+ * caller's allocator — item 07/11 re-home it into the document arena).
10
+ *
11
+ * v1 scope decisions (recorded in TODO.impl/05): validation is scalar
12
+ * with a SWAR ASCII fast path — the SIMD kernel lands only if item 06's
13
+ * profiles demand it; transcoding rejects ill-formed input (strict) —
14
+ * compat replacement policy arrives with the parse-options item (10).
15
+ */
16
+ #ifndef YEP_ENCODING_H
17
+ #define YEP_ENCODING_H
18
+
19
+ #include <stddef.h>
20
+
21
+ #include "memory/allocator.h"
22
+
23
+ #ifdef __cplusplus
24
+ extern "C" {
25
+ #endif
26
+
27
+ typedef enum {
28
+ YEP_ENC_UNKNOWN = 0,
29
+ YEP_ENC_UTF8,
30
+ YEP_ENC_UTF16LE,
31
+ YEP_ENC_UTF16BE,
32
+ YEP_ENC_UTF32LE,
33
+ YEP_ENC_UTF32BE,
34
+ } yep_encoding;
35
+
36
+ /* Sniffs a byte-order mark. Sets *out (UNKNOWN when none) and returns the
37
+ * BOM length in bytes (0 when none). Partial BOMs at end-of-input match
38
+ * nothing. UTF-32LE is checked before UTF-16LE (FF FE 00 00 is both).
39
+ * out may be NULL. */
40
+ size_t yep_bom_sniff(const unsigned char* p, size_t len, yep_encoding* out);
41
+
42
+ /* Validates UTF-8: 1 when [p, p+len) is well-formed (no overlongs, no
43
+ * surrogates, no > U+10FFFF, no truncation), else 0 with *err_pos set to
44
+ * the offset of the first offending byte (err_pos may be NULL). */
45
+ int yep_utf8_validate(const unsigned char* p, size_t len, size_t* err_pos);
46
+
47
+ /* Well-formed UTF-8 AND the YAML 1.2 c-printable charset: raw C0
48
+ * controls except TAB/LF/CR, DEL, C1 except NEL, U+FFFE/U+FFFF are
49
+ * rejected (charset SSOT — encoding/ is the only place that decides). */
50
+ int yep_printable_validate(const unsigned char* p, size_t len, size_t* err_pos);
51
+
52
+ /* Transcodes UTF-16/32 (any endianness) into freshly allocated UTF-8.
53
+ * On success returns 0, stores the buffer (owned by the caller, freed
54
+ * with the allocator) and its length. Errors: -1 allocation failure,
55
+ * -2 ill-formed input with *err_pos at the offending unit offset. */
56
+ int yep_transcode_to_utf8(const yep_allocator* sys, yep_encoding enc, const unsigned char* src,
57
+ size_t len, unsigned char** out, size_t* out_len, size_t* err_pos);
58
+
59
+ #ifdef __cplusplus
60
+ }
61
+ #endif
62
+
63
+ #endif /* YEP_ENCODING_H */
@@ -0,0 +1,157 @@
1
+ /* transcode.c — UTF-16/UTF-32 (either endianness) → UTF-8 (TODO.impl/05).
2
+ *
3
+ * Strict v1: ill-formed input (unpaired surrogates, values above
4
+ * U+10FFFF, truncated units) is an error at the offending unit; the
5
+ * compat replacement policy arrives with parse options (10).
6
+ * Worst-case sizing: UTF-16 unit 2 bytes → 3 UTF-8 bytes (1.5x);
7
+ * UTF-32 unit 4 → 4 bytes (1x).
8
+ */
9
+
10
+ #include <stdint.h>
11
+
12
+ #include "encoding.h"
13
+
14
+ static unsigned char* yep_emit_utf8(unsigned char* o, uint32_t cp) {
15
+ if (cp < 0x80) {
16
+ *o++ = (unsigned char)cp;
17
+ } else if (cp < 0x800) {
18
+ *o++ = (unsigned char)(0xC0 | (cp >> 6));
19
+ *o++ = (unsigned char)(0x80 | (cp & 0x3F));
20
+ } else if (cp < 0x10000) {
21
+ *o++ = (unsigned char)(0xE0 | (cp >> 12));
22
+ *o++ = (unsigned char)(0x80 | ((cp >> 6) & 0x3F));
23
+ *o++ = (unsigned char)(0x80 | (cp & 0x3F));
24
+ } else {
25
+ *o++ = (unsigned char)(0xF0 | (cp >> 18));
26
+ *o++ = (unsigned char)(0x80 | ((cp >> 12) & 0x3F));
27
+ *o++ = (unsigned char)(0x80 | ((cp >> 6) & 0x3F));
28
+ *o++ = (unsigned char)(0x80 | (cp & 0x3F));
29
+ }
30
+ return o;
31
+ }
32
+
33
+ static uint16_t rd16(const unsigned char* p, int big_endian) {
34
+ return big_endian ? (uint16_t)((p[0] << 8) | p[1]) : (uint16_t)((p[1] << 8) | p[0]);
35
+ }
36
+
37
+ static int yep_is_surrogate(uint32_t cp) {
38
+ return cp >= 0xD800 && cp <= 0xDFFF;
39
+ }
40
+
41
+ static int yep_transcode_utf16(const yep_allocator* sys, int big_endian, const unsigned char* src,
42
+ size_t len, unsigned char** out, size_t* out_len, size_t* err_pos) {
43
+ if (len % 2 != 0) { /* trailing partial unit */
44
+ if (err_pos != NULL) {
45
+ *err_pos = len - 1;
46
+ }
47
+ return -2;
48
+ }
49
+
50
+ size_t units = len / 2;
51
+ size_t max_out = units * 3;
52
+ unsigned char* dst = yep_alloc(sys, max_out ? max_out : 1);
53
+ if (dst == NULL) {
54
+ return -1;
55
+ }
56
+
57
+ unsigned char* o = dst;
58
+ for (size_t i = 0; i < units; i++) {
59
+ uint16_t u = rd16(src + i * 2, big_endian);
60
+ if (u >= 0xD800 && u <= 0xDBFF) {
61
+ if (i + 1 >= units) {
62
+ if (err_pos != NULL) {
63
+ *err_pos = i * 2;
64
+ }
65
+ yep_free(sys, dst);
66
+ return -2; /* lone high surrogate */
67
+ }
68
+ uint16_t v = rd16(src + (i + 1) * 2, big_endian);
69
+ if (v < 0xDC00 || v > 0xDFFF) {
70
+ if (err_pos != NULL) {
71
+ *err_pos = (i + 1) * 2;
72
+ }
73
+ yep_free(sys, dst);
74
+ return -2; /* high surrogate not followed by low */
75
+ }
76
+ uint32_t cp = 0x10000 + (((uint32_t)(u - 0xD800)) << 10) + (uint32_t)(v - 0xDC00);
77
+ o = yep_emit_utf8(o, cp);
78
+ i++;
79
+ } else if (u >= 0xDC00 && u <= 0xDFFF) {
80
+ if (err_pos != NULL) {
81
+ *err_pos = i * 2;
82
+ }
83
+ yep_free(sys, dst);
84
+ return -2; /* lone low surrogate */
85
+ } else {
86
+ o = yep_emit_utf8(o, u);
87
+ }
88
+ }
89
+
90
+ *out = dst;
91
+ *out_len = (size_t)(o - dst);
92
+ return 0;
93
+ }
94
+
95
+ static int yep_transcode_utf32(const yep_allocator* sys, int big_endian, const unsigned char* src,
96
+ size_t len, unsigned char** out, size_t* out_len, size_t* err_pos) {
97
+ size_t units = len / 4;
98
+ if (len % 4 != 0) {
99
+ /* a dangling final unit is an error, not a silent truncation
100
+ * (the UTF-16 twin rejects its own; the header contract says
101
+ * truncated units error at the offending byte) */
102
+ if (err_pos != NULL) {
103
+ *err_pos = units * 4;
104
+ }
105
+ return -2;
106
+ }
107
+ unsigned char* dst = yep_alloc(sys, len ? len : 1);
108
+ if (dst == NULL) {
109
+ return -1;
110
+ }
111
+
112
+ unsigned char* o = dst;
113
+ for (size_t i = 0; i < units; i++) {
114
+ const unsigned char* p = src + i * 4;
115
+ uint32_t cp = big_endian ? ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) |
116
+ ((uint32_t)p[2] << 8) | (uint32_t)p[3]
117
+ : ((uint32_t)p[3] << 24) | ((uint32_t)p[2] << 16) |
118
+ ((uint32_t)p[1] << 8) | (uint32_t)p[0];
119
+ if (cp > 0x10FFFF || yep_is_surrogate(cp)) {
120
+ if (err_pos != NULL) {
121
+ *err_pos = i * 4;
122
+ }
123
+ yep_free(sys, dst);
124
+ return -2;
125
+ }
126
+ o = yep_emit_utf8(o, cp);
127
+ }
128
+
129
+ *out = dst;
130
+ *out_len = (size_t)(o - dst);
131
+ return 0;
132
+ }
133
+
134
+ int yep_transcode_to_utf8(const yep_allocator* sys, yep_encoding enc, const unsigned char* src,
135
+ size_t len, unsigned char** out, size_t* out_len, size_t* err_pos) {
136
+ if (sys == NULL || out == NULL || out_len == NULL || (src == NULL && len != 0)) {
137
+ return -1;
138
+ }
139
+ if (len == 0) {
140
+ *out = NULL;
141
+ *out_len = 0;
142
+ return 0;
143
+ }
144
+
145
+ switch (enc) {
146
+ case YEP_ENC_UTF16LE:
147
+ return yep_transcode_utf16(sys, 0, src, len, out, out_len, err_pos);
148
+ case YEP_ENC_UTF16BE:
149
+ return yep_transcode_utf16(sys, 1, src, len, out, out_len, err_pos);
150
+ case YEP_ENC_UTF32LE:
151
+ return yep_transcode_utf32(sys, 0, src, len, out, out_len, err_pos);
152
+ case YEP_ENC_UTF32BE:
153
+ return yep_transcode_utf32(sys, 1, src, len, out, out_len, err_pos);
154
+ default:
155
+ return -2; /* not a transcoding source */
156
+ }
157
+ }
@@ -0,0 +1,178 @@
1
+ /* utf8_validate.c — well-formedness check with a SIMD-gated fast path
2
+ * (TODO.impl/05; TODO.restructure/69). Clean 32-byte chunks ride the
3
+ * gate_scan kernel (0 = every byte printable ASCII — validated in one
4
+ * pass); dirty chunks fall to the exact per-byte walk, which resumes
5
+ * the kernel at the next sequence boundary. The original SWAR path
6
+ * broke at the first non-ASCII byte and never recovered.
7
+ *
8
+ * Rules enforced (Unicode 15 well-formedness): no overlong encodings, no
9
+ * UTF-16 surrogate code points (U+D800..U+DFFF), nothing above U+10FFFF,
10
+ * continuation bytes only where a sequence expects them.
11
+ *
12
+ * yep_printable_validate additionally enforces the YAML 1.2 c-printable
13
+ * charset on the stream (the charset SSOT lives here): raw C0 controls
14
+ * except TAB/LF/CR, DEL, C1 except NEL (U+0085), and U+FFFE/U+FFFF are
15
+ * rejected wherever they appear. Escape sequences are decoded later —
16
+ * printable applies to source bytes only.
17
+ */
18
+
19
+ #include <stdint.h>
20
+ #include <string.h>
21
+
22
+ #include "../common/simd_text.h"
23
+ #include "encoding.h"
24
+
25
+ static int ascii_ok(unsigned char b, int printable) {
26
+ if (!printable) {
27
+ return 1;
28
+ }
29
+ /* c-printable ASCII: TAB, LF, CR, 0x20..0x7E */
30
+ if (b == '\t' || b == '\n' || b == '\r') {
31
+ return 1;
32
+ }
33
+ return b >= 0x20 && b <= 0x7E;
34
+ }
35
+
36
+ /* Code point >= 0x80 allowed by c-printable? (surrogates are already
37
+ * rejected by the well-formedness rules above). */
38
+ static int cp_printable(uint32_t cp) {
39
+ return cp == 0x85 || (cp >= 0xA0 && cp <= 0xFFFD) || cp >= 0x10000;
40
+ }
41
+
42
+ /* The exact per-byte walk: ASCII gate check plus the multibyte DFA.
43
+ * Advances *i by one byte or one whole sequence; returns 0 on error
44
+ * (with *err_pos set), 1 to continue. */
45
+ static int step(const unsigned char* p, size_t len, size_t* i, size_t* err_pos, int printable) {
46
+ size_t at = *i;
47
+ unsigned char b = p[at];
48
+ if (b < 0x80) {
49
+ if (!ascii_ok(b, printable)) {
50
+ if (err_pos != NULL) {
51
+ *err_pos = at;
52
+ }
53
+ return 0;
54
+ }
55
+ *i = at + 1;
56
+ return 1;
57
+ }
58
+
59
+ size_t need; /* continuation bytes expected */
60
+ unsigned char lo, hi; /* allowed range of the first continuation */
61
+ if (b >= 0xC2 && b <= 0xDF) {
62
+ need = 1;
63
+ lo = 0x80;
64
+ hi = 0xBF;
65
+ } else if (b == 0xE0) {
66
+ need = 2;
67
+ lo = 0xA0; /* no overlong two-byte */
68
+ hi = 0xBF;
69
+ } else if ((b >= 0xE1 && b <= 0xEC) || b == 0xEE || b == 0xEF) {
70
+ need = 2;
71
+ lo = 0x80;
72
+ hi = 0xBF;
73
+ } else if (b == 0xED) {
74
+ need = 2;
75
+ lo = 0x80;
76
+ hi = 0x9F; /* no surrogates */
77
+ } else if (b == 0xF0) {
78
+ need = 3;
79
+ lo = 0x90; /* no overlong three-byte */
80
+ hi = 0xBF;
81
+ } else if (b >= 0xF1 && b <= 0xF3) {
82
+ need = 3;
83
+ lo = 0x80;
84
+ hi = 0xBF;
85
+ } else if (b == 0xF4) {
86
+ need = 3;
87
+ lo = 0x80;
88
+ hi = 0x8F; /* no > U+10FFFF */
89
+ } else {
90
+ if (err_pos != NULL) {
91
+ *err_pos = at;
92
+ }
93
+ return 0;
94
+ }
95
+
96
+ /* The sequence spans [at, at + need]; all those bytes must exist. */
97
+ if (at + need >= len) {
98
+ if (err_pos != NULL) {
99
+ *err_pos = at;
100
+ }
101
+ return 0;
102
+ }
103
+
104
+ unsigned char c1 = p[at + 1];
105
+ if (c1 < lo || c1 > hi) {
106
+ if (err_pos != NULL) {
107
+ *err_pos = at + 1;
108
+ }
109
+ return 0;
110
+ }
111
+ for (size_t k = 2; k <= need; k++) {
112
+ if (p[at + k] < 0x80 || p[at + k] > 0xBF) {
113
+ if (err_pos != NULL) {
114
+ *err_pos = at + k;
115
+ }
116
+ return 0;
117
+ }
118
+ }
119
+ if (printable) {
120
+ uint32_t cp;
121
+ if (need == 1) {
122
+ cp = ((uint32_t)(b & 0x1F) << 6) | (uint32_t)(c1 & 0x3F);
123
+ } else if (need == 2) {
124
+ cp = ((uint32_t)(b & 0x0F) << 12) | ((uint32_t)(c1 & 0x3F) << 6) |
125
+ (uint32_t)(p[at + 2] & 0x3F);
126
+ } else {
127
+ cp = ((uint32_t)(b & 0x07) << 18) | ((uint32_t)(c1 & 0x3F) << 12) |
128
+ ((uint32_t)(p[at + 2] & 0x3F) << 6) | (uint32_t)(p[at + 3] & 0x3F);
129
+ }
130
+ if (!cp_printable(cp)) {
131
+ if (err_pos != NULL) {
132
+ *err_pos = at;
133
+ }
134
+ return 0;
135
+ }
136
+ }
137
+ *i = at + 1 + need;
138
+ return 1;
139
+ }
140
+
141
+ static int validate(const unsigned char* p, size_t len, size_t* err_pos, int printable) {
142
+ if (err_pos != NULL) {
143
+ *err_pos = 0;
144
+ }
145
+ if (p == NULL && len != 0) {
146
+ return 0;
147
+ }
148
+
149
+ size_t i = 0;
150
+
151
+ while (i + 32 <= len) {
152
+ if (yep_text_active()->gate_scan((const char*)p + i, 32) == 0) {
153
+ i += 32;
154
+ continue;
155
+ }
156
+ size_t edge = i + 32;
157
+ while (i < edge) {
158
+ if (!step(p, len, &i, err_pos, printable)) {
159
+ return 0;
160
+ }
161
+ }
162
+ }
163
+
164
+ while (i < len) {
165
+ if (!step(p, len, &i, err_pos, printable)) {
166
+ return 0;
167
+ }
168
+ }
169
+ return 1;
170
+ }
171
+
172
+ int yep_utf8_validate(const unsigned char* p, size_t len, size_t* err_pos) {
173
+ return validate(p, len, err_pos, 0);
174
+ }
175
+
176
+ int yep_printable_validate(const unsigned char* p, size_t len, size_t* err_pos) {
177
+ return validate(p, len, err_pos, 1);
178
+ }