json-model-compiler 2.0b0__py3-none-any.whl

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 (51) hide show
  1. json_model/README.md +32 -0
  2. json_model/__init__.py +18 -0
  3. json_model/analyze.py +409 -0
  4. json_model/clang.py +563 -0
  5. json_model/convert.py +430 -0
  6. json_model/data/clang_entry.c +21 -0
  7. json_model/data/clang_free.c +10 -0
  8. json_model/data/clang_init.c +11 -0
  9. json_model/data/clang_pcre2_fun.c +6 -0
  10. json_model/data/clang_re2_fun.c +5 -0
  11. json_model/data/javascript_exe.js +4 -0
  12. json_model/data/javascript_foot.js +9 -0
  13. json_model/data/javascript_free.js +9 -0
  14. json_model/data/javascript_head.js +1 -0
  15. json_model/data/javascript_init.js +11 -0
  16. json_model/data/javascript_main.js +5 -0
  17. json_model/data/plpgsql_entry.sql +16 -0
  18. json_model/data/python_entry.py +11 -0
  19. json_model/data/python_exe.py +1 -0
  20. json_model/data/python_free.py +6 -0
  21. json_model/data/python_init.py +9 -0
  22. json_model/data/python_main.py +4 -0
  23. json_model/javascript.py +310 -0
  24. json_model/language.py +748 -0
  25. json_model/model.py +1084 -0
  26. json_model/mtypes.py +42 -0
  27. json_model/objmerge.py +165 -0
  28. json_model/optim.py +486 -0
  29. json_model/plpgsql.py +422 -0
  30. json_model/python.py +269 -0
  31. json_model/recurse.py +127 -0
  32. json_model/resolver.py +106 -0
  33. json_model/runtime/META.json +30 -0
  34. json_model/runtime/Makefile +21 -0
  35. json_model/runtime/__init__.py +32 -0
  36. json_model/runtime/json-model.c +1054 -0
  37. json_model/runtime/json-model.h +183 -0
  38. json_model/runtime/json_model--2.0.sql +261 -0
  39. json_model/runtime/json_model.control +2 -0
  40. json_model/runtime/main.c +473 -0
  41. json_model/runtime/support.py +485 -0
  42. json_model/runtime/types.py +16 -0
  43. json_model/script.py +585 -0
  44. json_model/url_cache.py +82 -0
  45. json_model/utils.py +623 -0
  46. json_model/xstatic.py +1444 -0
  47. json_model_compiler-2.0b0.dist-info/METADATA +201 -0
  48. json_model_compiler-2.0b0.dist-info/RECORD +51 -0
  49. json_model_compiler-2.0b0.dist-info/WHEEL +5 -0
  50. json_model_compiler-2.0b0.dist-info/entry_points.txt +2 -0
  51. json_model_compiler-2.0b0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1054 @@
1
+ #include <json-model.h>
2
+
3
+ #include <stdio.h>
4
+ #include <ctype.h>
5
+ #include <time.h>
6
+
7
+ char *jm_version_string = "<unknown>";
8
+
9
+ // regular expression engine
10
+ #if defined(REGEX_ENGINE_PCRE2)
11
+ # define PCRE2_CODE_UNIT_WIDTH 8
12
+ # include <pcre2.h>
13
+ #elif defined(REGEX_ENGINE_RE2)
14
+ # include <stddef.h>
15
+ # include <cre2.h>
16
+ #else
17
+ # error missing regex engine definition
18
+ #endif
19
+
20
+ /*
21
+ * JSON API extensions
22
+ */
23
+ bool
24
+ jm_json_is_scalar(const json_t *val)
25
+ {
26
+ const json_type tval = json_typeof(val);
27
+ return (tval == JSON_STRING || tval == JSON_INTEGER || tval == JSON_REAL ||
28
+ tval == JSON_TRUE || tval == JSON_FALSE || tval == JSON_NULL);
29
+ }
30
+
31
+ // fast JSON array comparison for a total order
32
+ int
33
+ jm_json_array_cmp(const json_t *v1, const json_t *v2)
34
+ {
35
+ // try size
36
+ size_t s1 = json_array_size(v1), s2 = json_array_size(v2);
37
+ if (s1 != s2)
38
+ return s2 - s1;
39
+
40
+ // else same size, use lexicographic order: first diff concludes the comparison
41
+ size_t index;
42
+ json_t *iv1;
43
+ json_array_foreach(v1, index, iv1)
44
+ {
45
+ json_t *iv2 = json_array_get(v2, index);
46
+ int cmp = jm_json_cmp(iv1, iv2);
47
+ if (cmp != 0)
48
+ return cmp;
49
+ }
50
+
51
+ // no difference found
52
+ return 0;
53
+ }
54
+
55
+ // for sorting propvals by property names
56
+ int
57
+ jm_propval_cmp(const jm_propval_t *pv1, const jm_propval_t *pv2)
58
+ {
59
+ return strcmp(pv1->prop, pv2->prop);
60
+ }
61
+
62
+ // fast JSON object comparison for a total order
63
+ int
64
+ jm_json_object_cmp(const json_t *v1, const json_t *v2)
65
+ {
66
+ // first, try size
67
+ size_t s1 = json_object_size(v1), s2 = json_object_size(v2);
68
+ if (s1 != s2)
69
+ return s2 - s1;
70
+
71
+ // else same size, extract property/value pairs
72
+ jm_propval_t pv1[s1], pv2[s2];
73
+ const char *key;
74
+ json_t *value;
75
+
76
+ // borrow properties and values from both objects
77
+ size_t index = 0;
78
+ json_object_foreach((json_t *) v1, key, value)
79
+ pv1[index++] = (jm_propval_t) { key, value };
80
+ qsort(pv1, s1, sizeof(jm_propval_t), (jm_cmp_fun_t) jm_propval_cmp);
81
+
82
+ index = 0;
83
+ json_object_foreach((json_t *) v2, key, value)
84
+ pv2[index++] = (jm_propval_t) { key, value };
85
+ qsort(pv2, s2, sizeof(jm_propval_t), (jm_cmp_fun_t) jm_propval_cmp);
86
+
87
+ // second, try lexicographic order of keys
88
+ for (size_t i = 0; i < s1; i++)
89
+ {
90
+ int cmp = strcmp(pv1[i].prop, pv2[i].prop);
91
+ if (cmp != 0)
92
+ return cmp;
93
+ }
94
+ // same keys, third, try lexicographic order of values
95
+ for (size_t i = 0; i < s1; i++)
96
+ {
97
+ int cmp = jm_json_cmp(pv1[i].val, pv2[i].val);
98
+ if (cmp != 0)
99
+ return cmp;
100
+ }
101
+ // same values
102
+ return 0;
103
+ }
104
+
105
+ int
106
+ jm_json_cmp(const json_t *v1, const json_t *v2)
107
+ {
108
+ const json_type t1 = json_typeof(v1), t2 = json_typeof(v2);
109
+ if (t1 != t2)
110
+ return t1 - t2; // inverted to have simpler types ahead
111
+
112
+ // else same type
113
+ switch (t1)
114
+ {
115
+ case JSON_NULL:
116
+ case JSON_FALSE:
117
+ case JSON_TRUE:
118
+ // one only value is equal to itself
119
+ return 0;
120
+ case JSON_INTEGER:
121
+ int64_t i1 = json_integer_value(v1), i2 = json_integer_value(v2);
122
+ // do not substract to avoid overflows
123
+ return i1 < i2 ? -1 : i1 == i2 ? 0 : 1;
124
+ case JSON_REAL:
125
+ double r1 = json_real_value(v1), r2 = json_real_value(v2);
126
+ return r1 < r2 ? -1 : r1 == r2 ? 0 : 1;
127
+ case JSON_STRING:
128
+ return strcmp(json_string_value(v1), json_string_value(v2));
129
+ case JSON_ARRAY:
130
+ return jm_json_array_cmp(v1, v2);
131
+ case JSON_OBJECT:
132
+ return jm_json_object_cmp(v1, v2);
133
+ }
134
+
135
+ // default: panic, internal error
136
+ assert(false);
137
+ return 0;
138
+ }
139
+
140
+ #if defined(_WIN64)
141
+ # define qsort_r qsort_s
142
+ #endif
143
+
144
+ #if defined(_WIN64) || defined(__APPLE__)
145
+ typedef int(*jm_cmp_r_fun_t)(void *, const void *, const void *);
146
+ #else // Linux, Unix, whatever
147
+ typedef int(*jm_cmp_r_fun_t)(const void *, const void *, void *);
148
+ #endif
149
+
150
+ // FIXME this assumes ISO, and won't work on MacOS/BSD nor Windows
151
+ // which have their own view of qsort_r, including different names and/or argument orders.
152
+ // NOTE fast version which stops comparing once a duplicate has been seen:
153
+ // hopefully an optimized qsort implementation can use this to return early.
154
+ #if defined(_WIN64) || defined(__APPLE__)
155
+ static int
156
+ jm_json_cmp_r(void *duplicate, const json_t **v1, const json_t **v2)
157
+ #else
158
+ static int
159
+ jm_json_cmp_r(const json_t **v1, const json_t **v2, void *duplicate)
160
+ #endif
161
+ {
162
+ if (*((bool *) duplicate))
163
+ return 0;
164
+ int cmp = jm_json_cmp(*v1, *v2);
165
+ if (cmp == 0)
166
+ *((bool *) duplicate) = true;
167
+ return cmp;
168
+ }
169
+
170
+ // tell whether a JSON array holds distinct values.
171
+ bool
172
+ jm_json_array_unique(const json_t *val)
173
+ {
174
+ assert(json_is_array(val));
175
+ // extract as an actual array for sorting
176
+ size_t size = json_array_size(val);
177
+ const json_t *array[size];
178
+ // borrow array items
179
+ for (size_t i = 0; i < size; i++)
180
+ array[i] = json_array_get(val, i);
181
+ // relying only on qsort works because in the end
182
+ // all items have been compared to their neighbours
183
+ bool duplicate = false;
184
+ // life is fun
185
+ qsort_r(array, size, sizeof(json_t *), (jm_cmp_r_fun_t) jm_json_cmp_r, &duplicate);
186
+ return !duplicate;
187
+ }
188
+
189
+ // idem with reporting
190
+ bool
191
+ jm_array_is_unique(const json_t *val, jm_path_t *path, jm_report_t *rep)
192
+ {
193
+ bool unique = jm_json_array_unique(val);
194
+ if (!unique && rep)
195
+ jm_report_add_entry(rep, "non unique array", path);
196
+ return unique;
197
+ }
198
+
199
+ /*
200
+ * reporting
201
+ */
202
+ void
203
+ jm_report_add_entry(jm_report_t* rep, const char *msg, jm_path_t *path)
204
+ {
205
+ jm_report_entry_t *entry = malloc(sizeof(jm_report_entry_t));
206
+ *entry = (jm_report_entry_t) { msg, NULL, rep->entry };
207
+ rep->entry = entry;
208
+ if (path)
209
+ {
210
+ // path segments are stored in reverse order
211
+ size_t size = 1;
212
+ jm_path_t *current = path;
213
+ jm_path_t *next = NULL;
214
+ // dive to compute the path length and build the back path
215
+ while (current != NULL)
216
+ {
217
+ // "." name or "." number
218
+ size += 1 + (current->name ? strlen(current->name) : (3 * sizeof(size_t)));
219
+ jm_path_t *prev = current->prev;
220
+ current->next = next; // needed to backtrack
221
+ next = current; // future next
222
+ current = prev;
223
+ }
224
+ // allocate path
225
+ char *spath = malloc(size);
226
+ char *spos = spath;
227
+ bool dot = false; // skip first
228
+ // backtrack along path upward
229
+ current = next;
230
+ while (current != NULL)
231
+ {
232
+ // append "." and segment name/number unless first
233
+ if (dot || !current->next)
234
+ *spos++ = '.';
235
+ if (current->name)
236
+ spos = stpcpy(spos, current->name);
237
+ else
238
+ spos += sprintf(spos, "%zu", current->index);
239
+ current = current->next;
240
+ dot = true;
241
+ }
242
+ *spos = '\0';
243
+ entry->path = spath;
244
+ }
245
+ }
246
+
247
+ void
248
+ jm_report_free_entries(jm_report_t *rep)
249
+ {
250
+ jm_report_entry_t *entry = rep->entry;
251
+ rep->entry = NULL;
252
+
253
+ while (entry != NULL) {
254
+ jm_report_entry_t *follow = entry->prev;
255
+ if (entry->path)
256
+ free(entry->path);
257
+ free(entry);
258
+ entry = follow;
259
+ }
260
+ }
261
+
262
+ /*
263
+ * property mapping management
264
+ */
265
+ static int
266
+ jm_cmp_propmap(const jm_propmap_t *e1, const jm_propmap_t *e2)
267
+ {
268
+ return strcmp(e1->name, e2->name);
269
+ }
270
+
271
+ void
272
+ jm_sort_propmap(jm_propmap_t *props, int nprops)
273
+ {
274
+ qsort(props, nprops, sizeof(jm_propmap_t), (jm_cmp_fun_t) jm_cmp_propmap);
275
+ }
276
+
277
+ jm_check_fun_t
278
+ jm_search_propmap(const char *name, const jm_propmap_t *props, int nprops)
279
+ {
280
+ jm_propmap_t searched = (jm_propmap_t) { name, NULL };
281
+ jm_propmap_t *entry = (jm_propmap_t *)
282
+ bsearch(&searched, props, nprops, sizeof(jm_propmap_t), (jm_cmp_fun_t) jm_cmp_propmap);
283
+ return entry ? entry->val_check : NULL;
284
+ }
285
+
286
+ /*
287
+ * set of scalar constants
288
+ */
289
+ bool
290
+ jm_set_cst(jm_constant_t *c, const json_t *val)
291
+ {
292
+ switch (json_typeof(val))
293
+ {
294
+ case JSON_STRING:
295
+ *c = (jm_constant_t) { cst_is_string, { .s = json_string_value(val) } };
296
+ break;
297
+ case JSON_INTEGER:
298
+ *c = (jm_constant_t) { cst_is_integer, { .i = json_integer_value(val) } };
299
+ break;
300
+ case JSON_REAL:
301
+ *c = (jm_constant_t) { cst_is_float, { .f = json_real_value(val) } };
302
+ break;
303
+ case JSON_TRUE:
304
+ *c = (jm_constant_t) { cst_is_bool, { .b = true } };
305
+ break;
306
+ case JSON_FALSE:
307
+ *c = (jm_constant_t) { cst_is_bool, { .b = false } };
308
+ break;
309
+ case JSON_NULL:
310
+ *c = (jm_constant_t) { cst_is_null, { .s = NULL } };
311
+ break;
312
+ default:
313
+ return false;
314
+ }
315
+ return true;
316
+ }
317
+
318
+ void
319
+ jm_dbg_cst(const jm_constant_t *c, const char *describe)
320
+ {
321
+ fprintf(stderr, "%s (%s): %s\n",
322
+ describe,
323
+ c->tag == cst_is_string ? "string" : "?",
324
+ c->tag == cst_is_string ? c->val.s : "?");
325
+ }
326
+
327
+ static int
328
+ jm_cmp_cst(const jm_constant_t *c1, const jm_constant_t *c2)
329
+ {
330
+ // sort by types: null < bool < int < float < string
331
+ if (c1->tag != c2->tag)
332
+ return c2->tag - c1->tag;
333
+ // else compare values
334
+ switch (c1->tag)
335
+ {
336
+ case cst_is_null:
337
+ return 0;
338
+ case cst_is_bool: // false < true
339
+ return c2->val.b - c1->val.b;
340
+ case cst_is_integer:
341
+ int64_t i1 = c1->val.i, i2 = c2->val.i;
342
+ return i1 < i2 ? -1 : i1 == i2 ? 0 : 1;
343
+ case cst_is_float:
344
+ double f1 = c1->val.f, f2 = c2->val.f;
345
+ return f1 < f2 ? -1 : f1 == f2 ? 0 : 1;
346
+ case cst_is_string:
347
+ return strcmp(c1->val.s, c2->val.s);
348
+ default: // internal error?!
349
+ return 0;
350
+ }
351
+ }
352
+
353
+ void
354
+ jm_sort_cst(jm_constant_t *array, size_t size)
355
+ {
356
+ qsort(array, size, sizeof(jm_constant_t), (jm_cmp_fun_t) jm_cmp_cst);
357
+ }
358
+
359
+ bool
360
+ jm_search_cst(const jm_constant_t *value, const jm_constant_t *array, size_t size)
361
+ {
362
+ return bsearch(value, array, size, sizeof(jm_constant_t), (jm_cmp_fun_t) jm_cmp_cst) != NULL;
363
+ }
364
+
365
+ /*
366
+ * constant mapping management for discriminant optimization
367
+ */
368
+ static int
369
+ jm_cmp_constmap(const jm_constmap_t *cm1, const jm_constmap_t *cm2)
370
+ {
371
+ return jm_cmp_cst(&cm1->cst, &cm2->cst);
372
+ }
373
+
374
+ void
375
+ jm_sort_constmap(jm_constmap_t *array, size_t size)
376
+ {
377
+ qsort(array, size, sizeof(jm_constmap_t), (jm_cmp_fun_t) jm_cmp_constmap);
378
+ }
379
+
380
+ jm_check_fun_t
381
+ jm_search_constmap(const jm_constant_t *val, const jm_constmap_t *array, size_t size)
382
+ {
383
+ jm_constmap_t value = (jm_constmap_t) { *val, NULL };
384
+ jm_constmap_t *found = (jm_constmap_t *)
385
+ bsearch(&value, array, size, sizeof(jm_constmap_t), (jm_cmp_fun_t) jm_cmp_constmap);
386
+ return found ? found->check_val : NULL;
387
+ }
388
+
389
+ size_t
390
+ jm_any_len(json_t *val)
391
+ {
392
+ if (json_is_object(val))
393
+ return json_object_size(val);
394
+ else if (json_is_array(val))
395
+ return json_array_size(val);
396
+ else if (json_is_string(val))
397
+ return mbstowcs(NULL, json_string_value(val), 0);
398
+ // panic
399
+ return 0;
400
+ }
401
+
402
+ // NOTE this version is beyond slow because of mktime.
403
+ bool
404
+ jm_is_valid_date_slow(const char *date, jm_path_t *path, jm_report_t *rep)
405
+ {
406
+ int year, month, day;
407
+
408
+ if (date == NULL || sscanf(date, "%4d-%2d-%2d", &year, &month, &day) != 3)
409
+ return false;
410
+
411
+ struct tm ti = (struct tm) { .tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day};
412
+ time_t t = mktime(&ti);
413
+
414
+ // checks no error *and* no "funny" normalization
415
+ return t != -1 && ti.tm_year == year - 1900 && ti.tm_mon == month - 1 && ti.tm_mday == day;
416
+ }
417
+
418
+ static bool
419
+ is_valid_date(const char *date, jm_path_t *path, jm_report_t *rep, bool full)
420
+ {
421
+ if (date == NULL)
422
+ return false;
423
+
424
+ static int MONTH_DAYS[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
425
+ int year = 0, month = 0, day = 0, idx = 0;
426
+
427
+ // manual parsing for DDDD-DD-DD and value validation
428
+ while (isdigit(date[idx]) && idx < 4)
429
+ year = year * 10 + date[idx++] - '0';
430
+
431
+ if (idx++ != 4 || date[4] != '-' || year <= 0) {
432
+ if (rep) jm_report_add_entry(rep, "unexpected date year", path);
433
+ return false;
434
+ }
435
+
436
+ while (isdigit(date[idx]) && idx < 7)
437
+ month = month * 10 + date[idx++] - '0';
438
+
439
+ if (idx++ != 7 || date[7] != '-' || month < 1 || month > 12) {
440
+ if (rep) jm_report_add_entry(rep, "unexpected date month", path);
441
+ return false;
442
+ }
443
+
444
+ while (isdigit(date[idx]) && idx < 10)
445
+ day = day * 10 + date[idx++] - '0';
446
+
447
+ if (idx != 10 || (full && date[10] != '\0') || day < 1 || day > MONTH_DAYS[month - 1]) {
448
+ if (rep) jm_report_add_entry(rep, "unexpected date day", path);
449
+ return false;
450
+ }
451
+
452
+ // allow February 29th only on leap years
453
+ if (day == 29 && month == 2) {
454
+ bool leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
455
+ if (rep && !leap) jm_report_add_entry(rep, "unexpected date, not a leap year", path);
456
+ return leap;
457
+ }
458
+
459
+ return true;
460
+ }
461
+
462
+ // NOTE this version runs about 50 times faster
463
+ bool
464
+ jm_is_valid_date_fast(const char *date, jm_path_t *path, jm_report_t *rep)
465
+ {
466
+ return is_valid_date(date, path, rep, true);
467
+ }
468
+
469
+ // default version
470
+ bool (*jm_is_valid_date)(const char *, jm_path_t *, jm_report_t *) = jm_is_valid_date_fast;
471
+
472
+ // time parsing
473
+ static bool
474
+ is_valid_time(const char *time, jm_path_t *path, jm_report_t *rep, bool sep)
475
+ {
476
+ bool with_colon;
477
+ int hour = 0, minute = 0, second = 0, idx = 0;
478
+
479
+ // handle optional or mandatory separator, possibly accepting space
480
+ if (time[idx] != 'T' && time[idx] != ' ') {
481
+ if (sep)
482
+ return false;
483
+ }
484
+ else
485
+ idx++;
486
+
487
+ // 00..23 hour
488
+ if (!isdigit(time[idx]))
489
+ return false;
490
+ hour = 10 * (time[idx++] - '0');
491
+ if (!isdigit(time[idx]))
492
+ return false;
493
+ hour += time[idx++] - '0';
494
+
495
+ if (hour >= 24) {
496
+ if (rep) jm_report_add_entry(rep, "unexpected time hour", path);
497
+ return false;
498
+ }
499
+ with_colon = time[idx] == ':';
500
+ if (with_colon)
501
+ idx++;
502
+
503
+ // 00..60 minute
504
+ if (!isdigit(time[idx]))
505
+ return false;
506
+ minute = 10 * (time[idx++] - '0');
507
+ if (!isdigit(time[idx]))
508
+ return false;
509
+ minute += time[idx++] - '0';
510
+
511
+ if (minute >= 60) {
512
+ if (rep) jm_report_add_entry(rep, "unexpected time minute", path);
513
+ return false;
514
+ }
515
+ if (with_colon && time[idx++] != ':')
516
+ return false;
517
+
518
+ // 00..60 second
519
+ if (!isdigit(time[idx]))
520
+ return false;
521
+ second = 10 * (time[idx++] - '0');
522
+ if (!isdigit(time[idx]))
523
+ return false;
524
+ second += time[idx++] - '0';
525
+ if (second >= 60) {
526
+ if (rep) jm_report_add_entry(rep, "unexpected time second", path);
527
+ return false;
528
+ }
529
+
530
+ // optional millis (3, sometimes more, let us be nice)
531
+ if (time[idx] == '.') {
532
+ idx++;
533
+ if (!isdigit(time[idx++]))
534
+ return false;
535
+ while (isdigit(time[idx]))
536
+ idx++;
537
+ }
538
+
539
+ // optional timezone
540
+ if (time[idx] == 'Z')
541
+ idx++;
542
+ else if (time[idx] == '+' || time[idx] == '-')
543
+ {
544
+ idx++;
545
+ // hours
546
+ if (!isdigit(time[idx++]))
547
+ return false;
548
+ if (!isdigit(time[idx++]))
549
+ return false;
550
+ // optional minutes
551
+ if (time[idx] == ':')
552
+ {
553
+ idx++;
554
+ if (time[idx] < '0' || time[idx] > '5')
555
+ return false;
556
+ idx++;
557
+ if (!isdigit(time[idx++]))
558
+ return false;
559
+ }
560
+ else if (isdigit(time[idx]))
561
+ {
562
+ idx++;
563
+ if (!isdigit(time[idx++]))
564
+ return false;
565
+ }
566
+ }
567
+
568
+ return time[idx] == '\0';
569
+ }
570
+
571
+ bool
572
+ jm_is_valid_time(const char *time, jm_path_t *path, jm_report_t *rep)
573
+ {
574
+ if (time == NULL)
575
+ return false;
576
+ return is_valid_time(time, path, rep, false);
577
+ }
578
+
579
+ bool
580
+ jm_is_valid_datetime(const char *datetime, jm_path_t *path, jm_report_t *rep)
581
+ {
582
+ if (!is_valid_date(datetime, path, rep, false))
583
+ return false;
584
+ return is_valid_time(datetime + 10, path, rep, true);
585
+ }
586
+
587
+ bool
588
+ jm_is_valid_uuid(const char *uuid, jm_path_t *path, jm_report_t *rep)
589
+ {
590
+ if (!uuid)
591
+ return false;
592
+
593
+ // hardcoded version for ASCII and UTF-8
594
+ size_t i = 0;
595
+
596
+ #define subsection(upto) \
597
+ while (i < upto) \
598
+ if (!isxdigit(uuid[i++])) \
599
+ return false
600
+
601
+ #define section(upto) \
602
+ subsection(upto); \
603
+ if (uuid[i++] != '-') \
604
+ return false
605
+
606
+ section(8);
607
+ section(13);
608
+ section(18);
609
+ section(23);
610
+ subsection(36);
611
+
612
+ return uuid[i] == '\0';
613
+ }
614
+
615
+ // check regex validity by attempting to compile it with PCRE2, probably not very efficient
616
+ bool
617
+ jm_is_valid_regex_slow(const char *pattern, bool extended, jm_path_t *path, jm_report_t *rep)
618
+ {
619
+ if (!pattern)
620
+ return false;
621
+ #if defined(REGEX_ENGINE_PCRE2)
622
+ int err_code;
623
+ PCRE2_SIZE err_offset;
624
+ pcre2_code *code =
625
+ pcre2_compile((PCRE2_SPTR) pattern, PCRE2_ZERO_TERMINATED,
626
+ PCRE2_UCP|PCRE2_UTF, &err_code, &err_offset, NULL);
627
+ bool valid = code != NULL;
628
+ if (code)
629
+ pcre2_code_free(code);
630
+ #elif defined(REGEX_ENGINE_RE2)
631
+ cre2_regexp_t *rx = cre2_new(pattern, strlen(pattern), NULL);
632
+ bool valid = cre2_error_code(rx) == 0;
633
+ cre2_delete(rx);
634
+ #endif
635
+ return valid;
636
+ }
637
+
638
+ // hardcoded regex parser for https://github.com/google/re2/wiki/syntax
639
+ bool
640
+ jm_is_valid_regex_fast(const char *pattern, bool extended, jm_path_t *path, jm_report_t *rep)
641
+ {
642
+ if (!pattern)
643
+ return false;
644
+
645
+ char *c = (char *) pattern;
646
+
647
+ int paren = 0;
648
+ bool start = true;
649
+ bool okay = true;
650
+
651
+ while (*c && okay)
652
+ {
653
+ if (start) {
654
+ // cannot start with a repeat
655
+ okay &= strchr("*+?{", *c) == NULL;
656
+ start = false;
657
+ }
658
+
659
+ // TODO we could check for repeat consistency? eg ?? bad but +? ok.
660
+
661
+ switch (*c) {
662
+ case '\\': // TODO \p{Greek}
663
+ c++; // skip next, whatever, possibly some character class
664
+
665
+ if ((*c == 'p' || *c == 'P') && *(c+1) == '{') // handle \p{Greek}
666
+ {
667
+ c+=2;
668
+ while (isalpha(*c))
669
+ c++;
670
+ okay &= *c == '}';
671
+ }
672
+ else
673
+ okay &= *c != '\0';
674
+ break;
675
+ case '[':
676
+ c++;
677
+ if (*c == '[' && *(c+1) == ':') // [[: something
678
+ {
679
+ c += 2;
680
+ if (*c == '^') // [[:^
681
+ c++;
682
+ while (isalpha(*c)) // caracter class name
683
+ c++;
684
+ okay &= *c == ':' && *(c+1) == ']'; // :], check for following ] below
685
+ if (okay)
686
+ c += 2;
687
+ }
688
+ else // [C] [^C]
689
+ {
690
+ // FIXME what about -? ^ in corner cases?
691
+ if (*c == '^')
692
+ c++;
693
+ while (*c && *c != ']')
694
+ c++;
695
+ }
696
+ okay &= *c == ']'; // else final ] not found!
697
+ break;
698
+ case '{': // {123} or {12,34} or {12,}
699
+ c++; // to digit
700
+ okay = okay && isdigit(*c); // must be a number
701
+ while (isdigit(*c))
702
+ c++;
703
+ if (*c == ',') { // second part, may be empty
704
+ c++; // skip , to digit
705
+ while (isdigit(*c))
706
+ c++;
707
+ }
708
+ okay &= *c == '}';
709
+ break;
710
+ case '(':
711
+ paren++;
712
+
713
+ if (extended && *(c+1) == '$') // start of JSON MODEL extension
714
+ {
715
+ c += 2;
716
+
717
+ while (isalnum(*c)) // skip name
718
+ c++;
719
+
720
+ if (*c == ':') // optional regex
721
+ start = true;
722
+ else if (*c == ')')
723
+ start = true, paren--;
724
+ else
725
+ okay = false;
726
+ }
727
+ else if (*(c+1) == '?') // start of a usual extension
728
+ {
729
+ c += 2;
730
+
731
+ if (islower(*c)) // start of flags
732
+ {
733
+ while (islower(*c))
734
+ c++;
735
+ if (*c == ':') // (?flags:re)
736
+ start = true;
737
+ else if (*c == ')') // (?flags)
738
+ start = true, paren--;
739
+ else
740
+ okay = false;
741
+ }
742
+ else if (*c == 'P' || *c == '<') // (?P<name>...) or (?<name>...)
743
+ {
744
+ if (*c == 'P')
745
+ c++;
746
+ okay &= *c == '<';
747
+ c++;
748
+ while (isalnum(*c)) // skip name
749
+ c++;
750
+ okay &= *c == '>';
751
+ start = true;
752
+ }
753
+ else if (*c == ':') // non capturing group (?:...)
754
+ start = true;
755
+ else
756
+ okay = false;
757
+ }
758
+ else
759
+ start = true;
760
+ break;
761
+ case ')':
762
+ paren--;
763
+ okay &= paren >= 0;
764
+ break;
765
+ default:
766
+ }
767
+ c++;
768
+ }
769
+
770
+ okay &= *c == '\0' && paren == 0;
771
+
772
+ return okay;
773
+ }
774
+
775
+ // default version
776
+ bool (*jm_is_valid_regex)(const char *, bool, jm_path_t *, jm_report_t *) = jm_is_valid_regex_fast;
777
+
778
+ // this is utf-8 compatible because multi-byte encoding uses chars over 128.
779
+ bool
780
+ jm_is_valid_url(const char *url, jm_path_t *path, jm_report_t *rep)
781
+ {
782
+ if (!url)
783
+ return false;
784
+ char *c = (char *) url;
785
+ while (*c) {
786
+ // check url validity (hmmm, just check for strange characters)
787
+ if (!(*c >= 33 && *c < 126 && *c != '"' && *c != '<' && *c != '>'))
788
+ return false;
789
+ c++;
790
+ }
791
+ return true;
792
+ }
793
+
794
+ static bool is_email_char(int c)
795
+ {
796
+ return isalnum(c) || c == '_' || c == '.' || c == '-';
797
+ }
798
+
799
+ // ...@... for ASCII and UTF-8
800
+ bool
801
+ jm_is_valid_email(const char *email, jm_path_t *path, jm_report_t *rep)
802
+ {
803
+ if (!email)
804
+ return false;
805
+ char *c = (char *) email;
806
+ if (!is_email_char(*c++))
807
+ return false;
808
+ while (is_email_char(*c))
809
+ c++;
810
+ if (*c++ != '@')
811
+ return false;
812
+ if (!is_email_char(*c++))
813
+ return false;
814
+ while (is_email_char(*c))
815
+ c++;
816
+ return *c == '\0';
817
+ }
818
+
819
+ // generic constraint check.
820
+ bool
821
+ jm_check_constraint(const json_t * val, jm_constraint_op_t op, const jm_constant_t *cst,
822
+ jm_path_t *path, jm_report_t *rep)
823
+ {
824
+ bool res = false;
825
+
826
+ if (!val || json_is_null(val) || json_is_boolean(val))
827
+ {
828
+ if (rep)
829
+ jm_report_add_entry(rep, "unexpected null or bool type for any constraint", path);
830
+ return false;
831
+ }
832
+
833
+ if (cst->tag == cst_is_integer)
834
+ {
835
+ int64_t icst = cst->val.i, ival = 0;
836
+ if (json_is_object(val))
837
+ ival = (int64_t) json_object_size(val);
838
+ else if (json_is_array(val))
839
+ ival = (int64_t) json_array_size(val);
840
+ else if (json_is_string(val))
841
+ ival = (int64_t) mbstowcs(NULL, json_string_value(val), 0);
842
+ else if (json_is_integer(val))
843
+ ival = json_integer_value(val);
844
+ else if (json_is_real(val)) {
845
+ // special case
846
+ double fval = json_real_value(val);
847
+ switch (op) {
848
+ case op_eq:
849
+ res = fval == icst;
850
+ break;
851
+ case op_ne:
852
+ res = fval != icst;
853
+ break;
854
+ case op_le:
855
+ res = fval <= icst;
856
+ break;
857
+ case op_lt:
858
+ res = fval < icst;
859
+ break;
860
+ case op_ge:
861
+ res = fval >= icst;
862
+ break;
863
+ case op_gt:
864
+ res = fval > icst;
865
+ break;
866
+ }
867
+ if (!res && rep)
868
+ jm_report_add_entry(rep, "invalid float/int constraint", path);
869
+ return res;
870
+ }
871
+ // else cannot happen
872
+
873
+ // ival / icst comparison
874
+ switch (op) {
875
+ case op_eq:
876
+ res = ival == icst;
877
+ break;
878
+ case op_ne:
879
+ res = ival != icst;
880
+ break;
881
+ case op_le:
882
+ res = ival <= icst;
883
+ break;
884
+ case op_lt:
885
+ res = ival < icst;
886
+ break;
887
+ case op_ge:
888
+ res = ival >= icst;
889
+ break;
890
+ case op_gt:
891
+ res = ival > icst;
892
+ break;
893
+ }
894
+ if (!res && rep)
895
+ jm_report_add_entry(rep, "invalid int constraint", path);
896
+ return res;
897
+ }
898
+ else if (cst->tag == cst_is_float)
899
+ {
900
+ double fcst = cst->val.f;
901
+ if (!json_is_number(val))
902
+ {
903
+ if (rep)
904
+ jm_report_add_entry(rep, "invalid type for float constraint", path);
905
+ return false;
906
+ }
907
+
908
+ double fval = json_number_value(val);
909
+ switch (op) {
910
+ case op_eq:
911
+ res = fval == fcst;
912
+ break;
913
+ case op_ne:
914
+ res = fval != fcst;
915
+ break;
916
+ case op_le:
917
+ res = fval <= fcst;
918
+ break;
919
+ case op_lt:
920
+ res = fval < fcst;
921
+ break;
922
+ case op_ge:
923
+ res = fval >= fcst;
924
+ break;
925
+ case op_gt:
926
+ res = fval > fcst;
927
+ break;
928
+ }
929
+ if (!res && rep)
930
+ jm_report_add_entry(rep, "invalid float constraint", path);
931
+ return res;
932
+ }
933
+ else if (cst->tag == cst_is_string)
934
+ {
935
+ const char *scst = cst->val.s;
936
+ if (!json_is_string(val))
937
+ {
938
+ if (rep)
939
+ jm_report_add_entry(rep, "invalid type for string constraint", path);
940
+ return false;
941
+ }
942
+
943
+ const char *sval = json_string_value(val);
944
+ switch (op) {
945
+ case op_eq:
946
+ res = strcmp(sval, scst) == 0;
947
+ break;
948
+ case op_ne:
949
+ res = strcmp(sval, scst) != 0;
950
+ break;
951
+ case op_le:
952
+ res = strcmp(sval, scst) <= 0;
953
+ break;
954
+ case op_lt:
955
+ res = strcmp(sval, scst) < 0;
956
+ break;
957
+ case op_ge:
958
+ res = strcmp(sval, scst) >= 0;
959
+ break;
960
+ case op_gt:
961
+ res = strcmp(sval, scst) > 0;
962
+ break;
963
+ }
964
+ if (!res && rep)
965
+ jm_report_add_entry(rep, "invalid string constraint", path);
966
+ return res;
967
+ }
968
+
969
+ // else cannot happen
970
+ if (rep)
971
+ jm_report_add_entry(rep, "invalid constant type in constraint", path);
972
+ return false;
973
+ }
974
+
975
+
976
+ // MUST BE CONSISTENT WITH "jansson_private.h"
977
+ typedef struct {
978
+ json_t json;
979
+ char *value;
980
+ size_t length;
981
+ } json_string_t;
982
+
983
+ // call a check function with a string instead of a json objet.
984
+ bool
985
+ jm_check_fun_string(jm_check_fun_t fun, const char *val, jm_path_t *path, jm_report_t *rep)
986
+ {
987
+ // create a temporary non-malloced json head for the string
988
+ const json_string_t holder = (json_string_t) {
989
+ .json = { JSON_STRING, 1 },
990
+ .value = (char *) val,
991
+ .length = strlen(val)
992
+ };
993
+ return fun((const json_t *) &holder, path, rep);
994
+ }
995
+
996
+ bool
997
+ jm_generic_entry(
998
+ const char *(*model_init)(void),
999
+ jm_check_fun_t (*model_fun)(const char *),
1000
+ // void (*model_free)(void),
1001
+ const json_t *val, const char *name, bool *error, char **reasons)
1002
+ {
1003
+ model_init(); // lazy
1004
+ jm_check_fun_t checker = model_fun(name);
1005
+
1006
+ bool not_found = checker == NULL;
1007
+ if (error)
1008
+ *error = not_found;
1009
+
1010
+ if (not_found)
1011
+ {
1012
+ const char *format = "JSON Model check function not found for <%s>\n";
1013
+
1014
+ if (!error)
1015
+ {
1016
+ fprintf(stderr, format, name);
1017
+ exit(1);
1018
+ }
1019
+
1020
+ if (reasons)
1021
+ {
1022
+ size_t size = strlen(format) + strlen(name);
1023
+ char *message = malloc(size);
1024
+ sprintf(message, format, name);
1025
+ *reasons = message;
1026
+ }
1027
+
1028
+ return false;
1029
+ }
1030
+
1031
+ jm_path_t root = (jm_path_t) { "$", 0, NULL, NULL };
1032
+ jm_report_t report = (jm_report_t) { NULL };
1033
+
1034
+ bool valid = checker(val, reasons ? &root : NULL, reasons ? &report : NULL);
1035
+
1036
+ // generate explanations if a report is required and the value failed to validate
1037
+ if (reasons && report.entry && !valid)
1038
+ {
1039
+ size_t size = 1;
1040
+
1041
+ for (jm_report_entry_t *entry = report.entry; entry != NULL; entry = entry->prev)
1042
+ size += strlen(entry->message) + strlen(entry->path) + 3;
1043
+
1044
+ char *message = malloc(size);
1045
+ *reasons = message;
1046
+
1047
+ for (jm_report_entry_t *entry = report.entry; entry != NULL; entry = entry->prev)
1048
+ message += sprintf("%s: %s\n", entry->path, entry->message);
1049
+ }
1050
+
1051
+ jm_report_free_entries(&report);
1052
+
1053
+ return valid;
1054
+ }