3 * Types for the test-vector framework
5 * (c) 2023 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 /*----- Header files ------------------------------------------------------*/
45 /*----- Preliminary utilities ---------------------------------------------*/
47 static int signed_to_buf(buf *b, long i)
53 if (i >= 0) ASSIGN64(k, u);
54 else { ASSIGN64(k, ~u); CPL64(k, k); }
55 return (buf_putk64l(b, k));
58 static int signed_from_buf(buf *b, long *i_out)
60 kludge64 k, lmax, not_lmin;
62 ASSIGN64(lmax, LONG_MAX); ASSIGN64(not_lmin, ~(unsigned long)LONG_MIN);
63 if (buf_getk64l(b, &k)) return (-1);
64 if (CMP64(k, <=, lmax)) *i_out = (long)GET64(unsigned long, k);
67 if (CMP64(k, <=, not_lmin)) *i_out = -(long)GET64(unsigned long, k) - 1;
73 static int unsigned_to_buf(buf *b, unsigned long u)
74 { kludge64 k; ASSIGN64(k, u); return (buf_putk64l(b, k)); }
76 static int unsigned_from_buf(buf *b, unsigned long *u_out)
80 ASSIGN64(ulmax, ULONG_MAX);
81 if (buf_getk64l(b, &k)) return (-1);
82 if (CMP64(k, >, ulmax)) return (-1);
83 *u_out = GET64(unsigned long, k); return (0);
86 static int hex_width(unsigned long u)
91 for (t = u >> 4, wd = 4; t >>= wd, wd *= 2, t; );
95 static int check_signed_range(long i,
96 const struct tvec_irange *ir,
97 struct tvec_state *tv)
99 if (ir && (ir->min > i || i > ir->max)) {
100 tvec_error(tv, "integer %ld out of range (must be in [%ld .. %ld])",
101 i, ir->min, ir->max);
107 static int check_unsigned_range(unsigned long u,
108 const struct tvec_urange *ur,
109 struct tvec_state *tv)
111 if (ur && (ur->min > u || u > ur->max)) {
112 tvec_error(tv, "integer %lu out of range (must be in [%lu .. %lu])",
113 u, ur->min, ur->max);
119 static int parse_signed(long *i_out, const char *p,
120 const struct tvec_irange *ir,
121 struct tvec_state *tv)
123 char *q; const char *pp;
127 olderr = errno; errno = 0;
128 pp = p; if (*pp == '-' || *pp == '+') pp++;
129 if (!ISDIGIT(*pp)) return (tvec_syntax(tv, *pp, "signed integer"));
130 i = strtol(p, &q, 0);
131 if (*q && !ISSPACE(*q)) return (tvec_syntax(tv, *q, "end-of-line"));
132 if (errno) return (tvec_error(tv, "invalid integer `%s'", p));
133 check_signed_range(i, ir, tv);
134 errno = olderr; *i_out = i;
138 static int parse_unsigned(unsigned long *u_out, const char *p,
139 const struct tvec_urange *ur,
140 struct tvec_state *tv)
146 olderr = errno; errno = 0;
147 if (!ISDIGIT(*p)) return (tvec_syntax(tv, *p, "unsigned integer"));
148 u = strtoul(p, &q, 0);
149 if (*q && !ISSPACE(*q)) return (tvec_syntax(tv, *q, "end-of-line"));
150 if (errno) return (tvec_error(tv, "invalid integer `%s'", p));
151 check_unsigned_range(u, ur, tv);
152 errno = olderr; *u_out = u;
156 static int convert_hex(char ch, int *v_out)
158 if ('0' <= ch && ch <= '9') { *v_out = ch - '0'; return (0); }
159 else if ('a' <= ch && ch <= 'f') { *v_out = ch - 'a' + 10; return (0); }
160 else if ('A' <= ch && ch <= 'F') { *v_out = ch - 'A' + 10; return (0); }
164 static int read_quoted_string(dstr *d, int quote, struct tvec_state *tv)
171 sprintf(expect, "`%c'", quote);
178 return (tvec_syntax(tv, ch, expect));
181 if (quote == '\'') goto ordinary;
184 case EOF: tvec_syntax(tv, ch, expect);
185 case '\n': tv->lno++; break;
186 case '\'': DPUTC(d, '\''); break;
187 case '\\': DPUTC(d, '\\'); break;
188 case '"': DPUTC(d, '"'); break;
189 case 'a': DPUTC(d, '\a'); break;
190 case 'b': DPUTC(d, '\b'); break;
191 case 'e': DPUTC(d, '\x1b'); break;
192 case 'f': DPUTC(d, '\f'); break;
193 case 'n': DPUTC(d, '\n'); break;
194 case 'r': DPUTC(d, '\r'); break;
195 case 't': DPUTC(d, '\t'); break;
196 case 'v': DPUTC(d, '\v'); break;
200 if (ch == '{') { f |= f_brace; ch = getc(tv->fp); }
202 if (convert_hex(ch, &esc))
203 return (tvec_syntax(tv, ch, "hex digit"));
205 ch = getc(tv->fp); if (convert_hex(ch, &i)) break;
208 return (tvec_error(tv,
209 "character code %d out of range", esc));
212 if (!(f&f_brace)) goto reinsert;
213 else if (ch != '}') return (tvec_syntax(tv, ch, "`}'"));
217 if ('0' <= ch && ch < '8') {
218 i = 1; esc = ch - '0';
221 if (i > 3 || '0' > ch || ch >= '8') break;
222 esc = 8*esc + ch - '0'; i++;
225 return (tvec_error(tv,
226 "character code %d out of range", esc));
230 return (tvec_syntax(tv, ch, "string escape"));
235 if (ch == quote) goto end;
249 enum { TVCODE_BARE, TVCODE_HEX, TVCODE_BASE64, TVCODE_BASE32 };
251 static int collect_bare(dstr *d, struct tvec_state *tv)
254 enum { WORD, SPACE, ESCAPE }; unsigned s = WORD;
261 tvec_syntax(tv, ch, "bareword");
264 if (s == ESCAPE) { tv->lno++; goto addch; }
265 if (s == WORD) pos = d->len;
266 ungetc(ch, tv->fp); if (tvec_nexttoken(tv)) { rc = -1; goto end; }
267 DPUTC(d, ' '); s = SPACE;
269 case '"': case '\'': case '!':
270 if (s == SPACE) { ungetc(ch, tv->fp); goto done; }
276 if (s != ESCAPE && isspace(ch)) {
277 if (s == WORD) pos = d->len;
278 DPUTC(d, ch); s = SPACE;
282 DPUTC(d, ch); s = WORD;
287 if (s == SPACE) d->len = pos;
293 static void set_up_encoding(const codec_class **ccl_out, unsigned *f_out,
298 *ccl_out = 0; *f_out = 0;
301 *ccl_out = &hex_class; *f_out = CDCF_IGNCASE;
304 *ccl_out = &base32_class; *f_out = CDCF_IGNCASE | CDCF_IGNEQPAD;
307 *ccl_out = &base64_class; *f_out = CDCF_IGNEQPAD;
314 static int read_compound_string(void **p_inout, size_t *sz_inout,
315 unsigned code, struct tvec_state *tv)
317 const codec_class *ccl; unsigned f;
319 dstr d = DSTR_INIT, w = DSTR_INIT;
323 set_up_encoding(&ccl, &f, code);
324 if (tvec_nexttoken(tv)) tvec_syntax(tv, fgetc(tv->fp), "string");
327 if (ch == '"' || ch == '\'')
328 { if (read_quoted_string(&d, ch, tv)) { rc = -1; goto end; } }
329 else if (ch == '!') {
331 DRESET(&w); tvec_readword(tv, &w, ";", "`!'-keyword");
332 if (STRCMP(w.buf, ==, "!bare")) code = TVCODE_BARE;
333 else if (STRCMP(w.buf, ==, "!hex")) code = TVCODE_HEX;
334 else if (STRCMP(w.buf, ==, "!base32")) code = TVCODE_BASE32;
335 else if (STRCMP(w.buf, ==, "!base64")) code = TVCODE_BASE64;
337 tvec_error(tv, "unknown string keyword `%s'", w.buf);
340 set_up_encoding(&ccl, &f, code);
344 if (tvec_readword(tv, &w, ";", "%s-encoded fragment", ccl->name))
345 { rc = -1; goto end; }
346 cdc = ccl->decoder(f);
347 err = cdc->ops->code(cdc, w.buf, w.len, &d);
348 if (!err) err = cdc->ops->code(cdc, 0, 0, &d);
350 tvec_error(tv, "invalid %s fragment `%s': %s",
351 ccl->name, w.buf, codec_strerror(err));
354 cdc->ops->destroy(cdc);
355 } else switch (code) {
358 if (collect_bare(&d, tv)) goto done;
363 } while (!tvec_nexttoken(tv));
366 if (*sz_inout <= d.len)
367 { xfree(*p_inout); *p_inout = xmalloc(d.len + 1); }
368 p = *p_inout; memcpy(p, d.buf, d.len); p[d.len] = 0; *sz_inout = d.len;
371 dstr_destroy(&d); dstr_destroy(&w);
375 /*----- Skeleton ----------------------------------------------------------*/
377 static void init_...(union tvec_regval *rv, const struct tvec_regdef *rd)
378 static void release_...(union tvec_regval *rv, const struct tvec_regdef *rd)
379 static int eq_...(const union tvec_regval *rv0, const union tvec_regval *rv1,
380 const struct tvec_regdef *rd)
381 static size_t measure_...(const union tvec_regval *rv,
382 const struct tvec_regdef *rd)
383 static int tobuf_...(buf *b, const union tvec_regval *rv,
384 const struct tvec_regdef *rd)
385 static int frombuf_...(buf *b, union tvec_regval *rv,
386 const struct tvec_regdef *rd)
387 static void parse_...(union tvec_regval *rv, const struct tvec_regdef *rd,
388 struct tvec_state *tv)
389 static void dump_...(const union tvec_regval *rv,
390 const struct tvec_regdef *rd,
391 struct tvec_state *tv, unsigned style)
393 const struct tvec_regty tvty_... = {
394 init_..., release_..., eq_..., measure_...,
395 tobuf_..., frombuf_...,
399 /*----- Signed and unsigned integer types ---------------------------------*/
401 static void init_int(union tvec_regval *rv, const struct tvec_regdef *rd)
404 static void init_uint(union tvec_regval *rv, const struct tvec_regdef *rd)
407 static void release_int(union tvec_regval *rv, const struct tvec_regdef *rd)
410 static int eq_int(const union tvec_regval *rv0, const union tvec_regval *rv1,
411 const struct tvec_regdef *rd)
412 { return (rv0->i == rv1->i); }
414 static int eq_uint(const union tvec_regval *rv0,
415 const union tvec_regval *rv1,
416 const struct tvec_regdef *rd)
417 { return (rv0->u == rv1->u); }
419 static size_t measure_int(const union tvec_regval *rv,
420 const struct tvec_regdef *rd)
423 static int tobuf_int(buf *b, const union tvec_regval *rv,
424 const struct tvec_regdef *rd)
425 { return (signed_to_buf(b, rv->i)); }
427 static int tobuf_uint(buf *b, const union tvec_regval *rv,
428 const struct tvec_regdef *rd)
429 { return (unsigned_to_buf(b, rv->u)); }
431 static int frombuf_int(buf *b, union tvec_regval *rv,
432 const struct tvec_regdef *rd)
433 { return (signed_from_buf(b, &rv->i)); }
435 static int frombuf_uint(buf *b, union tvec_regval *rv,
436 const struct tvec_regdef *rd)
437 { return (unsigned_from_buf(b, &rv->u)); }
439 static int parse_int(union tvec_regval *rv, const struct tvec_regdef *rd,
440 struct tvec_state *tv)
445 if (tvec_readword(tv, &d, ";", "signed integer")) { rc = -1; goto end; }
446 if (parse_signed(&rv->i, d.buf, rd->arg.p, tv)) { rc = -1; goto end; }
447 if (tvec_flushtoeol(tv, 0)) { rc = -1; goto end; }
454 static int parse_uint(union tvec_regval *rv, const struct tvec_regdef *rd,
455 struct tvec_state *tv)
460 if (tvec_readword(tv, &d, ";", "unsigned integer")) { rc = -1; goto end; }
461 if (parse_unsigned(&rv->u, d.buf, rd->arg.p, tv)) { rc = -1; goto end; }
462 if (tvec_flushtoeol(tv, 0)) { rc = -1; goto end; }
469 static void dump_int(const union tvec_regval *rv,
470 const struct tvec_regdef *rd,
471 struct tvec_state *tv, unsigned style)
475 tvec_write(tv, "%ld", rv->i);
476 if (!(style&TVSF_COMPACT)) {
477 if (rv->i >= 0) u = rv->i;
478 else u = -(unsigned long)rv->i;
479 tvec_write(tv, " ; = %s0x%0*lx", rv->i < 0 ? "-" : "", hex_width(u), u);
483 static void dump_uint(const union tvec_regval *rv,
484 const struct tvec_regdef *rd,
485 struct tvec_state *tv, unsigned style)
487 tvec_write(tv, "%lu", rv->u);
488 if (!(style&TVSF_COMPACT))
489 tvec_write(tv, " ; = 0x%0*lx", hex_width(rv->u), rv->u);
492 const struct tvec_regty tvty_int = {
493 init_int, release_int, eq_int, measure_int,
494 tobuf_int, frombuf_int,
498 const struct tvec_irange
499 tvrange_schar = { SCHAR_MIN, SCHAR_MAX },
500 tvrange_short = { SHRT_MIN, SHRT_MAX },
501 tvrange_int = { INT_MIN, INT_MAX },
502 tvrange_long = { LONG_MIN, LONG_MAX },
503 tvrange_sbyte = { -128, 127 },
504 tvrange_i16 = { -32768, +32767 },
505 tvrange_i32 = { -2147483648, 2147483647 };
507 const struct tvec_regty tvty_uint = {
508 init_uint, release_int, eq_uint, measure_int,
509 tobuf_uint, frombuf_uint,
510 parse_uint, dump_uint
513 const struct tvec_urange
514 tvrange_uchar = { 0, UCHAR_MAX },
515 tvrange_ushort = { 0, USHRT_MAX },
516 tvrange_uint = { 0, UINT_MAX },
517 tvrange_ulong = { 0, ULONG_MAX },
518 tvrange_size = { 0, (size_t)-1 },
519 tvrange_byte = { 0, 255 },
520 tvrange_u16 = { 0, 65535 },
521 tvrange_u32 = { 0, 4294967296 };
523 int tvec_claimeq_int(struct tvec_state *tv, long i0, long i1,
524 const char *file, unsigned lno, const char *expr)
526 tv->in[0].v.i = i0; tv->out[0].v.i = i1;
527 return (tvec_claimeq(tv, &tvty_int, 0, file, lno, expr));
530 int tvec_claimeq_uint(struct tvec_state *tv,
531 unsigned long u0, unsigned long u1,
532 const char *file, unsigned lno, const char *expr)
534 tv->in[0].v.u = u0; tv->out[0].v.u = u1;
535 return (tvec_claimeq(tv, &tvty_uint, 0, file, lno, expr));
538 /*----- Enumerations ------------------------------------------------------*/
540 static void init_enum(union tvec_regval *rv, const struct tvec_regdef *rd)
542 const struct tvec_enuminfo *ei = rd->arg.p;
545 #define CASE(tag, ty, slot) \
546 case TVMISC_##tag: rv->slot = 0; break;
553 static int eq_enum(const union tvec_regval *rv0,
554 const union tvec_regval *rv1,
555 const struct tvec_regdef *rd)
557 const struct tvec_enuminfo *ei = rd->arg.p;
560 #define CASE(tag, ty, slot) \
561 case TVMISC_##tag: return (rv0->slot == rv1->slot);
568 static int tobuf_enum(buf *b, const union tvec_regval *rv,
569 const struct tvec_regdef *rd)
571 const struct tvec_enuminfo *ei = rd->arg.p;
574 #define CASE(tag, ty, slot) \
575 case TVMISC_##tag: return (HANDLE_##tag);
576 #define HANDLE_INT signed_to_buf(b, rv->i)
577 #define HANDLE_UINT unsigned_to_buf(b, rv->u)
578 #define HANDLE_PTR -1
589 static int frombuf_enum(buf *b, union tvec_regval *rv,
590 const struct tvec_regdef *rd)
592 const struct tvec_enuminfo *ei = rd->arg.p;
595 #define CASE(tag, ty, slot) \
596 case TVMISC_##tag: return (HANDLE_##tag);
597 #define HANDLE_INT signed_from_buf(b, &rv->i)
598 #define HANDLE_UINT unsigned_from_buf(b, &rv->u)
599 #define HANDLE_PTR -1
609 static int parse_enum(union tvec_regval *rv, const struct tvec_regdef *rd,
610 struct tvec_state *tv)
612 const struct tvec_enuminfo *ei = rd->arg.p;
613 #define DECLS(tag, ty, slot) \
614 const struct tvec_##slot##assoc *slot##a;
615 TVEC_MISCSLOTS(DECLS)
620 if (tvec_readword(tv, &d, ";", "enumeration tag or literal integer"))
621 { rc = -1; goto end; }
623 #define CASE(tag_, ty, slot) \
624 case TVMISC_##tag_: \
625 for (slot##a = ei->u.slot.av; slot##a->tag; slot##a++) \
626 if (STRCMP(d.buf, ==, slot##a->tag)) \
627 { rv->slot = FETCH_##tag_; goto done; }
628 #define FETCH_INT (ia->i)
629 #define FETCH_UINT (ua->u)
630 #define FETCH_PTR ((/*unconst*/ void *)(pa->p))
639 #define CASE(tag, ty, slot) \
640 case TVMISC_##tag: HANDLE_##tag goto done;
642 if (parse_signed(&rv->i, d.buf, ei->u.i.ir, tv)) \
643 { rc = -1; goto end; }
644 #define HANDLE_UINT \
645 if (parse_unsigned(&rv->u, d.buf, ei->u.u.ur, tv)) \
646 { rc = -1; goto end; }
648 if (STRCMP(d.buf, ==, "#nil")) rv->p = 0; \
656 tvec_error(tv, "unknown `%s' value `%s'", ei->name, d.buf);
661 if (tvec_flushtoeol(tv, 0)) { rc = -1; goto end; }
668 static void dump_enum(const union tvec_regval *rv,
669 const struct tvec_regdef *rd,
670 struct tvec_state *tv, unsigned style)
672 const struct tvec_enuminfo *ei = rd->arg.p;
673 #define DECLS(tag, ty, slot) \
674 const struct tvec_##slot##assoc *slot##a;
675 TVEC_MISCSLOTS(DECLS)
683 #define CASE(tag_, ty, slot) \
684 case TVMISC_##tag_: \
685 for (slot##a = ei->u.slot.av; slot##a->tag; slot##a++) \
686 if (rv->slot == slot##a->slot) \
687 { tag = slot##a->tag; goto found; } \
697 tvec_write(tv, "%s", tag);
698 if (style&TVSF_COMPACT) return;
699 tvec_write(tv, " ; = ");
703 #define CASE(tag, ty, slot) \
704 case TVMISC_##tag: HANDLE_##tag break;
705 #define HANDLE_INT tvec_write(tv, "%ld", rv->i);
706 #define HANDLE_UINT tvec_write(tv, "%lu", rv->u);
707 #define HANDLE_PTR if (!rv->p) tvec_write(tv, "#nil"); \
708 else tvec_write(tv, "#<%s %p>", ei->name, rv->p);
718 if (!(f&f_known)) tvec_write(tv, " ;");
719 if (rv->i >= 0) u = rv->i;
720 else u = -(unsigned long)rv->i;
721 tvec_write(tv, " = %s0x%0*lx", rv->i < 0 ? "-" : "", hex_width(u), u);
724 if (!(f&f_known)) tvec_write(tv, " ;");
725 tvec_write(tv, " = 0x%0*lx", hex_width(rv->u), rv->u);
730 const struct tvec_regty tvty_enum = {
731 init_enum, release_int, eq_enum, measure_int,
732 tobuf_enum, frombuf_enum,
733 parse_enum, dump_enum
736 #define DEFCLAIM(tag, ty, slot) \
737 int tvec_claimeq_##slot##enum(struct tvec_state *tv, \
738 const struct tvec_enuminfo *ei, \
740 const char *file, unsigned lno, \
743 union tvec_misc arg; \
745 assert(ei->mv == TVMISC_##tag); \
747 tv->in[0].v.slot = GET_##tag(e0); \
748 tv->out[0].v.slot = GET_##tag(e1); \
749 return (tvec_claimeq(tv, &tvty_enum, &arg, file, lno, expr)); \
751 #define GET_INT(e) (e)
752 #define GET_UINT(e) (e)
753 #define GET_PTR(e) ((/*unconst*/ void *)(e))
754 TVEC_MISCSLOTS(DEFCLAIM)
760 /*----- Flag types --------------------------------------------------------*/
762 static int parse_flags(union tvec_regval *rv, const struct tvec_regdef *rd,
763 struct tvec_state *tv)
765 const struct tvec_flaginfo *fi = rd->arg.p;
766 const struct tvec_flag *f;
767 unsigned long m = 0, v = 0, t;
773 if (tvec_readword(tv, &d, "|;", "flag name or integer"))
774 { rc = -1; goto end; }
776 for (f = fi->fv; f->tag; f++)
777 if (STRCMP(f->tag, ==, d.buf)) {
779 { tvec_error(tv, "colliding flag setting"); rc = -1; goto end; }
781 { m |= f->m; v |= f->v; goto next; }
784 if (parse_unsigned(&t, d.buf, fi->range, tv)) { rc = -1; goto end; }
787 if (tvec_nexttoken(tv)) break;
789 if (ch != '|') { tvec_syntax(tv, ch, "`|'"); rc = -1; goto end; }
790 if (tvec_nexttoken(tv))
791 { tvec_syntax(tv, '\n', "flag name or integer"); rc = -1; goto end; }
800 static void dump_flags(const union tvec_regval *rv,
801 const struct tvec_regdef *rd,
802 struct tvec_state *tv, unsigned style)
804 const struct tvec_flaginfo *fi = rd->arg.p;
805 const struct tvec_flag *f;
806 unsigned long m = ~(unsigned long)0, v = rv->u;
809 for (f = fi->fv, sep = ""; f->tag; f++)
810 if ((m&f->m) && (v&f->m) == f->v) {
811 tvec_write(tv, "%s%s", sep, f->tag); m &= ~f->m;
812 sep = style&TVSF_COMPACT ? "|" : " | ";
815 if (v&m) tvec_write(tv, "%s0x%0*lx", sep, hex_width(v), v&m);
817 if (!(style&TVSF_COMPACT))
818 tvec_write(tv, " ; = 0x%0*lx", hex_width(rv->u), rv->u);
821 const struct tvec_regty tvty_flags = {
822 init_uint, release_int, eq_uint, measure_int,
823 tobuf_uint, frombuf_uint,
824 parse_flags, dump_flags
827 int tvec_claimeq_flags(struct tvec_state *tv,
828 const struct tvec_flaginfo *fi,
829 unsigned long f0, unsigned long f1,
830 const char *file, unsigned lno, const char *expr)
834 arg.p = fi; tv->in[0].v.u = f0; tv->out[0].v.u = f1;
835 return (tvec_claimeq(tv, &tvty_flags, &arg, file, lno, expr));
838 /*----- Text and byte strings ---------------------------------------------*/
840 void tvec_allocstring(union tvec_regval *rv, size_t sz)
842 if (rv->str.sz < sz) { xfree(rv->str.p); rv->str.p = xmalloc(sz); }
846 void tvec_allocbytes(union tvec_regval *rv, size_t sz)
848 if (rv->bytes.sz < sz) { xfree(rv->bytes.p); rv->bytes.p = xmalloc(sz); }
852 static void init_string(union tvec_regval *rv, const struct tvec_regdef *rd)
853 { rv->str.p = 0; rv->str.sz = 0; }
855 static void init_bytes(union tvec_regval *rv, const struct tvec_regdef *rd)
856 { rv->bytes.p = 0; rv->bytes.sz = 0; }
858 static void release_string(union tvec_regval *rv,
859 const struct tvec_regdef *rd)
860 { xfree(rv->str.p); }
862 static void release_bytes(union tvec_regval *rv,
863 const struct tvec_regdef *rd)
864 { xfree(rv->bytes.p); }
866 static int eq_string(const union tvec_regval *rv0,
867 const union tvec_regval *rv1,
868 const struct tvec_regdef *rd)
870 return (rv0->str.sz == rv1->str.sz &&
872 MEMCMP(rv0->str.p, ==, rv1->str.p, rv1->str.sz)));
875 static int eq_bytes(const union tvec_regval *rv0,
876 const union tvec_regval *rv1,
877 const struct tvec_regdef *rd)
879 return (rv0->bytes.sz == rv1->bytes.sz &&
881 MEMCMP(rv0->bytes.p, ==, rv1->bytes.p, rv1->bytes.sz)));
884 static size_t measure_string(const union tvec_regval *rv,
885 const struct tvec_regdef *rd)
886 { return (rv->str.sz + 4); }
888 static size_t measure_bytes(const union tvec_regval *rv,
889 const struct tvec_regdef *rd)
890 { return (rv->bytes.sz + 4); }
892 static int tobuf_string(buf *b, const union tvec_regval *rv,
893 const struct tvec_regdef *rd)
894 { return (buf_putmem32l(b, rv->str.p, rv->str.sz)); }
896 static int tobuf_bytes(buf *b, const union tvec_regval *rv,
897 const struct tvec_regdef *rd)
898 { return (buf_putmem32l(b, rv->bytes.p, rv->bytes.sz)); }
900 static int frombuf_string(buf *b, union tvec_regval *rv,
901 const struct tvec_regdef *rd)
906 p = buf_getmem32l(b, &sz); if (!p) return (-1);
907 tvec_allocstring(rv, sz); memcpy(rv->str.p, p, sz);
911 static int frombuf_bytes(buf *b, union tvec_regval *rv,
912 const struct tvec_regdef *rd)
917 p = buf_getmem32l(b, &sz); if (!p) return (-1);
918 tvec_allocbytes(rv, sz); memcpy(rv->bytes.p, p, sz);
922 static int check_string_length(size_t sz, const struct tvec_urange *ur,
923 struct tvec_state *tv)
925 if (ur && (ur->min > sz || sz > ur->max))
926 return (tvec_error(tv,
927 "invalid string length %lu; must be in [%lu..%lu]",
928 (unsigned long)sz, ur->min, ur->max));
932 static int parse_string(union tvec_regval *rv, const struct tvec_regdef *rd,
933 struct tvec_state *tv)
937 if (read_compound_string(&p, &rv->str.sz, TVCODE_BARE, tv)) return (-1);
939 if (check_string_length(rv->str.sz, rd->arg.p, tv)) return (-1);
943 static int parse_bytes(union tvec_regval *rv, const struct tvec_regdef *rd,
944 struct tvec_state *tv)
946 void *p = rv->bytes.p;
948 if (read_compound_string(&p, &rv->bytes.sz, TVCODE_HEX, tv)) return (-1);
950 if (check_string_length(rv->bytes.sz, rd->arg.p, tv)) return (-1);
954 static void dump_string(const union tvec_regval *rv,
955 const struct tvec_regdef *rd,
956 struct tvec_state *tv, unsigned style)
958 const unsigned char *p, *q, *l;
964 if (!rv->str.sz) { tvec_write(tv, "\"\""); return; }
966 p = (const unsigned char *)rv->str.p; l = p + rv->str.sz;
967 if (*p == '!' || *p == ';' || *p == '"' || *p == '\'') goto quote;
968 for (q = p; q < l; q++)
969 if (*q == '\n' && q != l - 1) f |= f_newline;
970 else if (!*q || !isgraph(*q) || *q == '\\') f |= f_nonword;
971 if (f&f_newline) { tvec_write(tv, "\n\t"); goto quote; }
972 else if (f&f_nonword) goto quote;
973 tv->output->ops->write(tv->output, (const char *)p, rv->str.sz); return;
976 tvec_write(tv, "\"");
977 for (q = p; q < l; q++)
979 case '"': case '\\': ch = *q; goto escape;
980 case '\a': ch = 'a'; goto escape;
981 case '\b': ch = 'b'; goto escape;
982 case '\x1b': ch = 'e'; goto escape;
983 case '\f': ch = 'f'; goto escape;
984 case '\r': ch = 'r'; goto escape;
985 case '\t': ch = 't'; goto escape;
986 case '\v': ch = 'v'; goto escape;
989 tv->output->ops->write(tv->output, (const char *)p, q - p);
990 tvec_write(tv, "\\%c", ch); p = q + 1;
995 tv->output->ops->write(tv->output, (const char *)p, q - p);
996 tvec_write(tv, "\\n"); p = q + 1;
997 if (!(style&TVSF_COMPACT) && q < l) tvec_write(tv, "\"\t\"");
1001 if (isprint(*q)) break;
1003 tv->output->ops->write(tv->output, (const char *)p, q - p);
1004 tvec_write(tv, "\\x{%0*x}", hex_width(UCHAR_MAX), *q); p = q + 1;
1007 if (p < q) tv->output->ops->write(tv->output, (const char *)p, q - p);
1008 tvec_write(tv, "\"");
1014 static void dump_bytes(const union tvec_regval *rv,
1015 const struct tvec_regdef *rd,
1016 struct tvec_state *tv, unsigned style)
1018 const unsigned char *p = rv->bytes.p, *l = p + rv->bytes.sz;
1019 size_t off, sz = rv->bytes.sz;
1024 tvec_write(tv, style&TVSF_COMPACT ? "\"\"" : "\"\" ; empty");
1028 if (style&TVSF_COMPACT) {
1029 while (p < l) tvec_write(tv, "%02x", *p++);
1033 if (sz > 16) tvec_write(tv, "\n\t");
1035 off = 0; wd = hex_width(sz);
1037 if (l - p < 16) n = l - p;
1040 for (i = 0; i < 16; i++) {
1041 if (i < n) tvec_write(tv, "%02x", p[i]);
1042 else tvec_write(tv, " ");
1043 if (i%4 == 3) tvec_write(tv, " ");
1045 tvec_write(tv, " ; ");
1046 if (sz > 16) tvec_write(tv, "[%0*lx] ", wd, (unsigned long)off);
1047 for (i = 0; i < n; i++)
1048 tvec_write(tv, "%c", isprint(p[i]) ? p[i] : '.');
1050 if (p < l) tvec_write(tv, "\n\t");
1054 const struct tvec_regty tvty_string = {
1055 init_string, release_string, eq_string, measure_string,
1056 tobuf_string, frombuf_string,
1057 parse_string, dump_string
1060 const struct tvec_regty tvty_bytes = {
1061 init_bytes, release_bytes, eq_bytes, measure_bytes,
1062 tobuf_bytes, frombuf_bytes,
1063 parse_bytes, dump_bytes
1066 int tvec_claimeq_string(struct tvec_state *tv,
1067 const char *p0, size_t sz0,
1068 const char *p1, size_t sz1,
1069 const char *file, unsigned lno, const char *expr)
1071 tv->in[0].v.str.p = (/*unconst*/ char *)p0; tv->in[0].v.str.sz = sz0;
1072 tv->out[0].v.str.p =(/*unconst*/ char *) p1; tv->out[0].v.str.sz = sz1;
1073 return (tvec_claimeq(tv, &tvty_string, 0, file, lno, expr));
1076 int tvec_claimeq_strz(struct tvec_state *tv,
1077 const char *p0, const char *p1,
1078 const char *file, unsigned lno, const char *expr)
1080 tv->in[0].v.str.p = (/*unconst*/ char *)p0;
1081 tv->in[0].v.str.sz = strlen(p0);
1082 tv->out[0].v.str.p = (/*unconst*/ char *)p1;
1083 tv->out[0].v.str.sz = strlen(p1);
1084 return (tvec_claimeq(tv, &tvty_string, 0, file, lno, expr));
1087 int tvec_claimeq_bytes(struct tvec_state *tv,
1088 const void *p0, size_t sz0,
1089 const void *p1, size_t sz1,
1090 const char *file, unsigned lno, const char *expr)
1092 tv->in[0].v.bytes.p = (/*unconst*/ void *)p0;
1093 tv->in[0].v.bytes.sz = sz0;
1094 tv->out[0].v.bytes.p = (/*unconst*/ void *)p1;
1095 tv->out[0].v.bytes.sz = sz1;
1096 return (tvec_claimeq(tv, &tvty_bytes, 0, file, lno, expr));
1099 /*----- Buffer type -------------------------------------------------------*/
1101 static int eq_buffer(const union tvec_regval *rv0,
1102 const union tvec_regval *rv1,
1103 const struct tvec_regdef *rd)
1104 { return (rv0->bytes.sz == rv1->bytes.sz); }
1106 static int tobuf_buffer(buf *b, const union tvec_regval *rv,
1107 const struct tvec_regdef *rd)
1108 { return (unsigned_to_buf(b, rv->bytes.sz)); }
1110 static int frombuf_buffer(buf *b, union tvec_regval *rv,
1111 const struct tvec_regdef *rd)
1115 if (unsigned_from_buf(b, &u)) return (-1);
1116 if (u > (size_t)-1) return (-1);
1117 tvec_allocbytes(rv, u); memset(rv->bytes.p, '!', u);
1121 static const char units[] = "kMGTPEZY";
1123 static int parse_buffer(union tvec_regval *rv,
1124 const struct tvec_regdef *rd,
1125 struct tvec_state *tv)
1128 char *q; const char *unit;
1136 if (tvec_readword(tv, &d, ";", "buffer length")) { rc = -1; goto end; }
1137 olderr = errno; errno = 0;
1138 u = strtoul(d.buf, &q, 0);
1139 if (errno) goto bad;
1142 tvec_skipspc(tv); pos = d.len;
1143 if (!tvec_readword(tv, &d, ";", 0)) pos++;
1147 if (u > (size_t)-1) goto rangerr;
1148 for (t = u, unit = units; *unit; unit++) {
1149 if (t > (size_t)-1/1024) f |= f_range;
1151 if (*q == *unit && (!q[1] || q[1] == 'B')) {
1152 if (f&f_range) goto rangerr;
1153 u = t; q += 2; break;
1156 if (*q && *q != ';') goto bad;
1157 if (check_string_length(u, rd->arg.p, tv)) { rc = -1; goto end; }
1159 if (tvec_flushtoeol(tv, 0)) { rc = -1; goto end; }
1160 tvec_allocbytes(rv, u); memset(rv->bytes.p, '?', u);
1163 DDESTROY(&d); return (rc);
1166 tvec_error(tv, "invalid buffer length `%s'", d.buf);
1170 tvec_error(tv, "buffer length `%s' out of range", d.buf);
1176 static void dump_buffer(const union tvec_regval *rv,
1177 const struct tvec_regdef *rd,
1178 struct tvec_state *tv, unsigned style)
1181 unsigned long u = rv->bytes.sz;
1184 tvec_write(tv, "%lu B", u);
1186 for (unit = units, u /= 1024; !(u%1024) && unit[1]; u /= 1024, unit++);
1187 tvec_write(tv, "%lu %cB", u, *unit);
1191 const struct tvec_regty tvty_buffer = {
1192 init_bytes, release_bytes, eq_buffer, measure_int,
1193 tobuf_buffer, frombuf_buffer,
1194 parse_buffer, dump_buffer
1197 /*----- That's all, folks -------------------------------------------------*/