| 1 |
|
| #ifndef FRX_ERR_H |
| 2 |
|
| #define FRX_ERR_H |
| 3 |
|
| |
| 4 |
|
| /* The refusal shape. |
| 5 |
|
| * |
| 6 |
|
| * The core never croaks (rule 4). Every refusal fills an frx_err - a code, |
| 7 |
|
| * the byte offset it was found at, and a static string saying what - and |
| 8 |
|
| * the two boundaries render it: the Perl surface croaks with the message, |
| 9 |
|
| * the ABI returns NULL and puts the message in a mortal SV. One formatter, |
| 10 |
|
| * so the two cannot drift. |
| 11 |
|
| * |
| 12 |
|
| * The message is |
| 13 |
|
| * |
| 14 |
|
| * File::Raw::XML: <what> at byte offset <N> near "<ctx>" |
| 15 |
|
| * |
| 16 |
|
| * or, when the offset is at or past the end, `... at end of input`. ctx is |
| 17 |
|
| * up to sixteen bytes from the offset with anything outside printable |
| 18 |
|
| * ASCII - and the quote and backslash - rendered \xNN, so a NUL in the |
| 19 |
|
| * input cannot truncate the message and a binary byte never reaches a |
| 20 |
|
| * %s. No snprintf: the number is rendered by hand, because the core is |
| 21 |
|
| * perl-free and the family's memory about my_snprintf and a shadowed |
| 22 |
|
| * `len` says to keep formatting out of the hot path anyway. |
| 23 |
|
| * |
| 24 |
|
| * Needs frx_abi.h, for frx_code and frx_err. */ |
| 25 |
|
| |
| 26 |
|
| /* frx_code and frx_err are frx_abi.h's: every core function that |
| 27 |
|
| * refuses fills one, and the v2 table hands them to a consumer |
| 28 |
|
| * directly rather than through a wrapper per entry. */ |
| 29 |
|
| |
| 30 |
|
| #define FRX_ERR_CONTEXT 16 |
| 31 |
|
| #define FRX_ERR_PREFIX "File::Raw::XML: " |
| 32 |
|
| |
| 33 |
|
| static void |
| 34 |
81368 |
| frx_err_set(frx_err *e, frx_code code, size_t offset, const char *what) |
| 35 |
|
| { |
| 36 |
81368 |
| e->code = code; |
| 37 |
81368 |
| e->offset = offset; |
| 38 |
81368 |
| e->what = what; |
| 39 |
81368 |
| e->enc = NULL; |
| 40 |
81368 |
| } |
| 41 |
|
| |
| 42 |
|
| /* Append n bytes of s to out at *pos, bounded by cap - 1; *pos advances by |
| 43 |
|
| * n regardless, so the caller learns the length that would have been |
| 44 |
|
| * written, as snprintf reports it. */ |
| 45 |
|
| static void |
| 46 |
361905 |
| frx_err_put(char *out, size_t cap, size_t *pos, const char *s, size_t n) |
| 47 |
|
| { |
| 48 |
|
| size_t i; |
| 49 |
2140361 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
1778456 |
361905 |
2140361 |
|
| 50 |
1778456 |
▶ | if (*pos + 1 < cap) out[*pos] = s[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*pos + 1 < cap) out[*pos] = s[i]; |
1778456 |
0 |
1778456 |
|
| 51 |
1778456 |
| (*pos)++; |
| 52 |
|
| } |
| 53 |
361905 |
| } |
| 54 |
|
| |
| 55 |
|
| static void |
| 56 |
18323 |
| frx_err_put_num(char *out, size_t cap, size_t *pos, size_t v) |
| 57 |
|
| { |
| 58 |
|
| char tmp[32]; |
| 59 |
18323 |
| size_t n = 0; |
| 60 |
|
| do { |
| 61 |
54372 |
| tmp[n++] = (char)('0' + (v % 10)); |
| 62 |
54372 |
| v /= 10; |
| 63 |
54372 |
▶ | } while (v && n < sizeof tmp); |
Branch: 2 branches
| Branch | True | False | Total |
| } while (v && n < sizeof tmp); |
36049 |
18323 |
54372 |
| } while (v && n < sizeof tmp); |
36049 |
0 |
36049 |
|
| 64 |
72695 |
▶ | while (n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (n) { |
54372 |
18323 |
72695 |
|
| 65 |
54372 |
| n--; |
| 66 |
54372 |
| frx_err_put(out, cap, pos, &tmp[n], 1); |
| 67 |
|
| } |
| 68 |
18323 |
| } |
| 69 |
|
| |
| 70 |
|
| /* The message over a window of the input: in holds the bytes from stream |
| 71 |
|
| * offset in_base, which is 0 for a whole document and, for a streaming |
| 72 |
|
| * reader that has dropped what it consumed, the offset of the first byte |
| 73 |
|
| * it still holds. An offset outside the window is reported without |
| 74 |
|
| * context; one at or past the end of a whole document is the end. */ |
| 75 |
|
| static size_t |
| 76 |
21143 |
| frx_err_format_at(const frx_err *e, const char *in, size_t len, size_t in_base, |
| 77 |
|
| char *out, size_t cap) |
| 78 |
|
| { |
| 79 |
|
| static const char hex[] = "0123456789abcdef"; |
| 80 |
21143 |
| size_t pos = 0; |
| 81 |
21143 |
▶ | const char *what = e->what ? e->what : "parse error"; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *what = e->what ? e->what : "parse error"; |
21143 |
0 |
21143 |
|
| 82 |
|
| |
| 83 |
21143 |
| frx_err_put(out, cap, &pos, FRX_ERR_PREFIX, sizeof FRX_ERR_PREFIX - 1); |
| 84 |
21143 |
| frx_err_put(out, cap, &pos, what, strlen(what)); |
| 85 |
|
| |
| 86 |
21143 |
▶ | if (in_base && (!in || e->offset < in_base || e->offset - in_base >= len)) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (in_base && (!in || e->offset < in_base || e->offset - in_base >= len)) { |
0 |
21143 |
21143 |
| if (in_base && (!in || e->offset < in_base || e->offset - in_base >= len)) { |
0 |
0 |
0 |
| if (in_base && (!in || e->offset < in_base || e->offset - in_base >= len)) { |
0 |
0 |
0 |
| if (in_base && (!in || e->offset < in_base || e->offset - in_base >= len)) { |
0 |
0 |
0 |
|
| 87 |
0 |
| frx_err_put(out, cap, &pos, " at byte offset ", 16); |
| 88 |
0 |
| frx_err_put_num(out, cap, &pos, e->offset); |
| 89 |
21143 |
▶ | } else if (!in || e->offset >= len) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (!in || e->offset >= len) { |
21143 |
0 |
21143 |
| } else if (!in || e->offset >= len) { |
3084 |
18059 |
21143 |
|
| 90 |
3084 |
| frx_err_put(out, cap, &pos, " at end of input", 16); |
| 91 |
|
| } else { |
| 92 |
18059 |
| size_t rel = e->offset - in_base; |
| 93 |
18059 |
| size_t i, n = len - rel; |
| 94 |
18059 |
▶ | if (n > FRX_ERR_CONTEXT) n = FRX_ERR_CONTEXT; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > FRX_ERR_CONTEXT) n = FRX_ERR_CONTEXT; |
9398 |
8661 |
18059 |
|
| 95 |
18059 |
| frx_err_put(out, cap, &pos, " at byte offset ", 16); |
| 96 |
18059 |
| frx_err_put_num(out, cap, &pos, e->offset); |
| 97 |
18059 |
▶ | if (e->enc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->enc) { |
52 |
18007 |
18059 |
|
| 98 |
|
| /* the offset is in the caller's bytes, which were transcoded */ |
| 99 |
52 |
| frx_err_put(out, cap, &pos, " of the ", 8); |
| 100 |
52 |
| frx_err_put(out, cap, &pos, e->enc, strlen(e->enc)); |
| 101 |
52 |
| frx_err_put(out, cap, &pos, " input", 6); |
| 102 |
|
| } |
| 103 |
18059 |
| frx_err_put(out, cap, &pos, " near \"", 7); |
| 104 |
225097 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
207038 |
18059 |
225097 |
|
| 105 |
207038 |
| unsigned char c = (unsigned char)in[rel + i]; |
| 106 |
207038 |
▶ | if (c >= 0x20 && c < 0x7f && c != '"' && c != '\\') { |
Branch: 4 branches
| Branch | True | False | Total |
| if (c >= 0x20 && c < 0x7f && c != '"' && c != '\\') { |
203119 |
3919 |
207038 |
| if (c >= 0x20 && c < 0x7f && c != '"' && c != '\\') { |
200849 |
2270 |
203119 |
| if (c >= 0x20 && c < 0x7f && c != '"' && c != '\\') { |
191957 |
8892 |
200849 |
| if (c >= 0x20 && c < 0x7f && c != '"' && c != '\\') { |
191948 |
9 |
191957 |
|
| 107 |
191948 |
| frx_err_put(out, cap, &pos, (const char *)&c, 1); |
| 108 |
|
| } else { |
| 109 |
|
| char esc[4]; |
| 110 |
15090 |
| esc[0] = '\\'; esc[1] = 'x'; |
| 111 |
15090 |
| esc[2] = hex[c >> 4]; esc[3] = hex[c & 15]; |
| 112 |
15090 |
| frx_err_put(out, cap, &pos, esc, 4); |
| 113 |
|
| } |
| 114 |
|
| } |
| 115 |
18059 |
| frx_err_put(out, cap, &pos, "\"", 1); |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
21143 |
▶ | if (cap) out[pos < cap ? pos : cap - 1] = '\0'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cap) out[pos < cap ? pos : cap - 1] = '\0'; |
21143 |
0 |
21143 |
| if (cap) out[pos < cap ? pos : cap - 1] = '\0'; |
0 |
21143 |
21143 |
|
| 119 |
21143 |
▶ | return pos < cap ? pos : cap - 1; |
Branch: 1 branch
| Branch | True | False | Total |
| return pos < cap ? pos : cap - 1; |
0 |
21143 |
21143 |
|
| 120 |
|
| } |
| 121 |
|
| |
| 122 |
|
| static size_t |
| 123 |
21133 |
| frx_err_format(const frx_err *e, const char *in, size_t len, char *out, size_t cap) |
| 124 |
|
| { |
| 125 |
21133 |
| return frx_err_format_at(e, in, len, 0, out, cap); |
| 126 |
|
| } |
| 127 |
|
| |
| 128 |
|
| #endif /* FRX_ERR_H */ |