1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// generated by hxc 1.0.0-acgt-compiler from src/check/json.hlx -- do not edit by hand
#[cfg(windows)]
#[allow(dead_code)]
const SYS_CURL: &str = "C:\\Windows\\System32\\curl.exe";
#[cfg(not(windows))]
#[allow(dead_code)]
const SYS_CURL: &str = "curl";
#[allow(dead_code)]
fn _helix_allowed(var: &str, what: &str) -> bool {
let ok = std::env::var(var).map(|v| v == "1").unwrap_or(false);
if !ok {
eprintln!("helix: BLOCKED {} -- permission denied (set {}=1 to allow; `hxc run` allows by default, a `hxc build`/`export-rust` binary does not)", what, var);
}
ok
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_exec(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_exec(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
c
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_net(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_net(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
c
}
// hlx:2
// Strict bounded JSON reader for scoped checks. Pure: bytes in, tree out.
// hlx:3
// Rejects invalid UTF-8, raw control characters, duplicate keys (after escape decoding), lone surrogates,
// hlx:4
// invalid numbers, trailing bytes and nesting deeper than MAX_DEPTH. Numbers keep their literal text.
// hlx:5
use std::collections::BTreeSet;
// hlx:6
pub const MAX_BYTES: usize = 1 << 20;
// hlx:7
pub const MAX_DEPTH: usize = 64;
// hlx:8
// Replay-only envelope allowance: the wrapper object adds one nesting level and a fixed-size receipt.
// hlx:9
pub const REPLAY_EXTRA_BYTES: usize = 4096;
// hlx:10
pub const REPLAY_MAX_DEPTH: usize = MAX_DEPTH + 1;
// hlx:11
#[derive(Debug, Clone, PartialEq)]
// hlx:12
pub enum J {
// hlx:13
Null,
// hlx:14
Bool(bool),
// hlx:15
Num(String),
// hlx:16
Str(String),
// hlx:17
Arr(Vec<J>),
// hlx:18
Obj(Vec<(String, J)>),
// hlx:19
}
// hlx:20
struct P<'a> {
// hlx:21
s: &'a str,
// hlx:22
b: &'a [u8],
// hlx:23
i: usize,
// hlx:24
max_depth: usize,
// hlx:25
}
// hlx:26
pub fn parse(input: &[u8]) -> Result<J, String> {
// hlx:27
parse_with_limits(input, MAX_BYTES, MAX_DEPTH)
// hlx:28
}
// hlx:29
/// Same strict grammar with explicit size and nesting caps; `parse` keeps the ordinary constants.
// hlx:30
pub fn parse_with_limits(input: &[u8], max_bytes: usize, max_depth: usize) -> Result<J, String> {
// hlx:31
if input.len() > max_bytes {
// hlx:32
return Err(if max_bytes == MAX_BYTES {
"input_too_large: more than 1 MiB".to_string()
} else {
"input_too_large: replay envelope exceeds 1 MiB plus 4096 bytes".to_string()
});
// hlx:33
}
// hlx:34
let s = std::str::from_utf8(input)
.map_err(|e| format!("invalid_json: invalid UTF-8 at byte {}", e.valid_up_to()))?;
// hlx:35
let mut p = P {
s,
b: s.as_bytes(),
i: 0,
max_depth,
};
// hlx:36
p.ws();
// hlx:37
let v = p.value(0)?;
// hlx:38
p.ws();
// hlx:39
if p.i != p.b.len() {
// hlx:40
return Err(p.err("trailing bytes after the value"));
// hlx:41
}
// hlx:42
Ok(v)
// hlx:43
}
// hlx:44
impl<'a> P<'a> {
// hlx:45
fn err(&self, what: &str) -> String {
// hlx:46
format!("invalid_json: {what} at byte {}", self.i)
// hlx:47
}
// hlx:48
fn ws(&mut self) {
// hlx:49
while self.i < self.b.len() && matches!(self.b[self.i], b' ' | b'\t' | b'\n' | b'\r') {
// hlx:50
self.i += 1;
// hlx:51
}
// hlx:52
}
// hlx:53
fn lit(&mut self, word: &str, v: J) -> Result<J, String> {
// hlx:54
if self.b[self.i..].starts_with(word.as_bytes()) {
// hlx:55
self.i += word.len();
// hlx:56
Ok(v)
// hlx:57
} else {
// hlx:58
Err(self.err("invalid literal"))
// hlx:59
}
// hlx:60
}
// hlx:61
fn value(&mut self, depth: usize) -> Result<J, String> {
// hlx:62
if self.i >= self.b.len() {
// hlx:63
return Err(self.err("unexpected end of input"));
// hlx:64
}
// hlx:65
match self.b[self.i] {
// hlx:66
b'{' => self.object(depth),
// hlx:67
b'[' => self.array(depth),
// hlx:68
b'"' => Ok(J::Str(self.string()?)),
// hlx:69
b't' => self.lit("true", J::Bool(true)),
// hlx:70
b'f' => self.lit("false", J::Bool(false)),
// hlx:71
b'n' => self.lit("null", J::Null),
// hlx:72
b'-' | b'0'..=b'9' => self.number(),
// hlx:73
_ => Err(self.err("unexpected character")),
// hlx:74
}
// hlx:75
}
// hlx:76
fn array(&mut self, depth: usize) -> Result<J, String> {
// hlx:77
if depth + 1 > self.max_depth {
// hlx:78
return Err(self.err(&format!("nesting deeper than {}", self.max_depth)));
// hlx:79
}
// hlx:80
self.i += 1;
// hlx:81
let mut out = Vec::new();
// hlx:82
self.ws();
// hlx:83
if self.i < self.b.len() && self.b[self.i] == b']' {
// hlx:84
self.i += 1;
// hlx:85
return Ok(J::Arr(out));
// hlx:86
}
// hlx:87
loop {
// hlx:88
self.ws();
// hlx:89
out.push(self.value(depth + 1)?);
// hlx:90
self.ws();
// hlx:91
match self.b.get(self.i) {
// hlx:92
Some(b',') => self.i += 1,
// hlx:93
Some(b']') => {
// hlx:94
self.i += 1;
// hlx:95
return Ok(J::Arr(out));
// hlx:96
}
// hlx:97
_ => return Err(self.err("expected ',' or ']'")),
// hlx:98
}
// hlx:99
}
// hlx:100
}
// hlx:101
fn object(&mut self, depth: usize) -> Result<J, String> {
// hlx:102
if depth + 1 > self.max_depth {
// hlx:103
return Err(self.err(&format!("nesting deeper than {}", self.max_depth)));
// hlx:104
}
// hlx:105
self.i += 1;
// hlx:106
let mut out: Vec<(String, J)> = Vec::new();
// hlx:107
let mut seen: BTreeSet<String> = BTreeSet::new();
// hlx:108
self.ws();
// hlx:109
if self.i < self.b.len() && self.b[self.i] == b'}' {
// hlx:110
self.i += 1;
// hlx:111
return Ok(J::Obj(out));
// hlx:112
}
// hlx:113
loop {
// hlx:114
self.ws();
// hlx:115
if self.b.get(self.i) != Some(&b'"') {
// hlx:116
return Err(self.err("expected a string key"));
// hlx:117
}
// hlx:118
let at = self.i;
// hlx:119
let k = self.string()?;
// hlx:120
if !seen.insert(k.clone()) {
// hlx:121
self.i = at;
// hlx:122
return Err(self.err("duplicate key"));
// hlx:123
}
// hlx:124
self.ws();
// hlx:125
if self.b.get(self.i) != Some(&b':') {
// hlx:126
return Err(self.err("expected ':'"));
// hlx:127
}
// hlx:128
self.i += 1;
// hlx:129
self.ws();
// hlx:130
let v = self.value(depth + 1)?;
// hlx:131
out.push((k, v));
// hlx:132
self.ws();
// hlx:133
match self.b.get(self.i) {
// hlx:134
Some(b',') => self.i += 1,
// hlx:135
Some(b'}') => {
// hlx:136
self.i += 1;
// hlx:137
return Ok(J::Obj(out));
// hlx:138
}
// hlx:139
_ => return Err(self.err("expected ',' or '}'")),
// hlx:140
}
// hlx:141
}
// hlx:142
}
// hlx:143
fn hex4(&mut self) -> Result<u32, String> {
// hlx:144
if self.i + 4 > self.b.len() {
// hlx:145
return Err(self.err("short \\u escape"));
// hlx:146
}
// hlx:147
let mut n = 0u32;
// hlx:148
for k in 0..4 {
// hlx:149
let d = match self.b[self.i + k] {
// hlx:150
c @ b'0'..=b'9' => c - b'0',
// hlx:151
c @ b'a'..=b'f' => c - b'a' + 10,
// hlx:152
c @ b'A'..=b'F' => c - b'A' + 10,
// hlx:153
_ => return Err(self.err("invalid \\u escape")),
// hlx:154
};
// hlx:155
n = n * 16 + u32::from(d);
// hlx:156
}
// hlx:157
self.i += 4;
// hlx:158
Ok(n)
// hlx:159
}
// hlx:160
fn string(&mut self) -> Result<String, String> {
// hlx:161
self.i += 1;
// hlx:162
let mut out = String::new();
// hlx:163
let mut run = self.i;
// hlx:164
loop {
// hlx:165
let c = match self.b.get(self.i) {
// hlx:166
Some(c) => *c,
// hlx:167
None => return Err(self.err("unterminated string")),
// hlx:168
};
// hlx:169
if c == b'"' {
// hlx:170
out.push_str(&self.s[run..self.i]);
// hlx:171
self.i += 1;
// hlx:172
return Ok(out);
// hlx:173
}
// hlx:174
if c < 0x20 {
// hlx:175
return Err(self.err("raw control character in string"));
// hlx:176
}
// hlx:177
if c != b'\\' {
// hlx:178
self.i += 1;
// hlx:179
continue;
// hlx:180
}
// hlx:181
out.push_str(&self.s[run..self.i]);
// hlx:182
self.i += 1;
// hlx:183
let e = match self.b.get(self.i) {
// hlx:184
Some(e) => *e,
// hlx:185
None => return Err(self.err("unterminated escape")),
// hlx:186
};
// hlx:187
self.i += 1;
// hlx:188
match e {
// hlx:189
b'"' => out.push('"'),
// hlx:190
b'\\' => out.push('\\'),
// hlx:191
b'/' => out.push('/'),
// hlx:192
b'b' => out.push('\u{8}'),
// hlx:193
b'f' => out.push('\u{c}'),
// hlx:194
b'n' => out.push('\n'),
// hlx:195
b'r' => out.push('\r'),
// hlx:196
b't' => out.push('\t'),
// hlx:197
b'u' => {
// hlx:198
let hi = self.hex4()?;
// hlx:199
let cp = if (0xD800..0xDC00).contains(&hi) {
// hlx:200
if self.b.get(self.i) != Some(&b'\\')
|| self.b.get(self.i + 1) != Some(&b'u')
{
// hlx:201
return Err(self.err("lone high surrogate"));
// hlx:202
}
// hlx:203
self.i += 2;
// hlx:204
let lo = self.hex4()?;
// hlx:205
if !(0xDC00..0xE000).contains(&lo) {
// hlx:206
return Err(self.err("invalid surrogate pair"));
// hlx:207
}
// hlx:208
0x10000 + ((hi - 0xD800) << 10) + (lo - 0xDC00)
// hlx:209
} else if (0xDC00..0xE000).contains(&hi) {
// hlx:210
return Err(self.err("lone low surrogate"));
// hlx:211
} else {
// hlx:212
hi
// hlx:213
};
// hlx:214
match char::from_u32(cp) {
// hlx:215
Some(ch) => out.push(ch),
// hlx:216
None => return Err(self.err("invalid code point")),
// hlx:217
}
// hlx:218
}
// hlx:219
_ => return Err(self.err("invalid escape")),
// hlx:220
}
// hlx:221
run = self.i;
// hlx:222
}
// hlx:223
}
// hlx:224
fn digits(&mut self) -> usize {
// hlx:225
let start = self.i;
// hlx:226
while self.i < self.b.len() && self.b[self.i].is_ascii_digit() {
// hlx:227
self.i += 1;
// hlx:228
}
// hlx:229
self.i - start
// hlx:230
}
// hlx:231
fn number(&mut self) -> Result<J, String> {
// hlx:232
let start = self.i;
// hlx:233
if self.b[self.i] == b'-' {
// hlx:234
self.i += 1;
// hlx:235
}
// hlx:236
match self.b.get(self.i) {
// hlx:237
Some(b'0') => self.i += 1,
// hlx:238
Some(b'1'..=b'9') => {
// hlx:239
self.digits();
// hlx:240
}
// hlx:241
_ => return Err(self.err("invalid number")),
// hlx:242
}
// hlx:243
if self.b.get(self.i) == Some(&b'.') {
// hlx:244
self.i += 1;
// hlx:245
if self.digits() == 0 {
// hlx:246
return Err(self.err("invalid number fraction"));
// hlx:247
}
// hlx:248
}
// hlx:249
if matches!(self.b.get(self.i), Some(b'e') | Some(b'E')) {
// hlx:250
self.i += 1;
// hlx:251
if matches!(self.b.get(self.i), Some(b'+') | Some(b'-')) {
// hlx:252
self.i += 1;
// hlx:253
}
// hlx:254
if self.digits() == 0 {
// hlx:255
return Err(self.err("invalid number exponent"));
// hlx:256
}
// hlx:257
}
// hlx:258
Ok(J::Num(self.s[start..self.i].to_string()))
// hlx:259
}
// hlx:260
}