pbwire 0.1.17 → 0.1.18

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.
@@ -0,0 +1,735 @@
1
+ /* Wire format primitives: varints, tags, little-endian fixed values, output
2
+ * buffer. */
3
+
4
+ #include <math.h>
5
+ #include <stdio.h>
6
+
7
+ #include "internal.h"
8
+
9
+ const uint8_t pbw_empty_input[1] = {0};
10
+
11
+ size_t
12
+ pbw_varint_write(uint8_t *out, uint64_t value)
13
+ {
14
+ size_t len = 0;
15
+ while (value >= 0x80) {
16
+ out[len++] = (uint8_t)((value & 0x7f) | 0x80);
17
+ value >>= 7;
18
+ }
19
+ out[len++] = (uint8_t)value;
20
+ return len;
21
+ }
22
+
23
+ /* Returns 1 on success, 0 on truncation or overflow. Rejects varints longer
24
+ * than 10 bytes, which no valid encoder produces. Bits above bit 63 of the last
25
+ * byte are dropped, matching the reference parser. */
26
+ int
27
+ pbw_varint_read(const uint8_t *p, const uint8_t *end, uint64_t *value,
28
+ size_t *used)
29
+ {
30
+ uint64_t result;
31
+ unsigned i;
32
+ size_t avail;
33
+
34
+ if (p == NULL || end == NULL || value == NULL || used == NULL ||
35
+ p >= end) {
36
+ return 0;
37
+ }
38
+ /* One-byte varints (small tags and lengths) are the common case. */
39
+ result = p[0];
40
+ if (result < 0x80) {
41
+ *value = result;
42
+ *used = 1;
43
+ return 1;
44
+ }
45
+ avail = pbw_remaining(end, p);
46
+ if (avail < 10) {
47
+ result = 0;
48
+ for (i = 0; i < 10 && (size_t)i < avail; i++) {
49
+ uint8_t byte = p[i];
50
+
51
+ result |= (uint64_t)(byte & 0x7f) << (7 * i);
52
+ if ((byte & 0x80) == 0) {
53
+ *value = result;
54
+ *used = i + 1;
55
+ return 1;
56
+ }
57
+ }
58
+ return 0;
59
+ }
60
+ /* Ten readable bytes: no per-byte bounds check, and the shifts are
61
+ * constants, which is what makes long (negative int32/int64) varints cheap. */
62
+ result = p[0] & 0x7f;
63
+ result |= (uint64_t)(p[1] & 0x7f) << 7;
64
+ if (p[1] < 0x80) {
65
+ *value = result;
66
+ *used = 2;
67
+ return 1;
68
+ }
69
+ result |= (uint64_t)(p[2] & 0x7f) << 14;
70
+ if (p[2] < 0x80) {
71
+ *value = result;
72
+ *used = 3;
73
+ return 1;
74
+ }
75
+ result |= (uint64_t)(p[3] & 0x7f) << 21;
76
+ if (p[3] < 0x80) {
77
+ *value = result;
78
+ *used = 4;
79
+ return 1;
80
+ }
81
+ result |= (uint64_t)(p[4] & 0x7f) << 28;
82
+ if (p[4] < 0x80) {
83
+ *value = result;
84
+ *used = 5;
85
+ return 1;
86
+ }
87
+ result |= (uint64_t)(p[5] & 0x7f) << 35;
88
+ if (p[5] < 0x80) {
89
+ *value = result;
90
+ *used = 6;
91
+ return 1;
92
+ }
93
+ result |= (uint64_t)(p[6] & 0x7f) << 42;
94
+ if (p[6] < 0x80) {
95
+ *value = result;
96
+ *used = 7;
97
+ return 1;
98
+ }
99
+ result |= (uint64_t)(p[7] & 0x7f) << 49;
100
+ if (p[7] < 0x80) {
101
+ *value = result;
102
+ *used = 8;
103
+ return 1;
104
+ }
105
+ result |= (uint64_t)(p[8] & 0x7f) << 56;
106
+ if (p[8] < 0x80) {
107
+ *value = result;
108
+ *used = 9;
109
+ return 1;
110
+ }
111
+ result |= (uint64_t)(p[9] & 0x7f) << 63;
112
+ if (p[9] < 0x80) {
113
+ *value = result;
114
+ *used = 10;
115
+ return 1;
116
+ }
117
+ return 0;
118
+ }
119
+
120
+ int
121
+ pbw_tag_read(const uint8_t *p, const uint8_t *end, uint32_t *tag, size_t *used)
122
+ {
123
+ uint32_t result;
124
+ unsigned i;
125
+ size_t avail;
126
+
127
+ if (p == NULL || end == NULL || tag == NULL || used == NULL ||
128
+ p >= end) {
129
+ return 0;
130
+ }
131
+ result = p[0];
132
+ if (result < 0x80) {
133
+ *tag = result;
134
+ *used = 1;
135
+ return 1;
136
+ }
137
+ result &= 0x7f;
138
+ avail = pbw_remaining(end, p);
139
+ /* A tag is a varint32: the fifth byte only contributes its four low bits,
140
+ * anything above is dropped exactly like the reference parser (a 5-byte tag
141
+ * may overflow without being rejected). A 6-byte tag is invalid. */
142
+ for (i = 1; i < 5 && (size_t)i < avail; i++) {
143
+ uint32_t byte = p[i];
144
+
145
+ result |= (byte & 0x7f) << (7 * i);
146
+ if (byte < 0x80) {
147
+ *tag = result;
148
+ *used = i + 1;
149
+ return 1;
150
+ }
151
+ }
152
+ return 0;
153
+ }
154
+
155
+ /* A length prefix is a strict varint32 in the reference parser: at most five
156
+ * bytes, and the fifth may only carry the four low bits. Unlike a tag, an
157
+ * out-of-range length is rejected instead of truncated. */
158
+ int
159
+ pbw_len_read(const uint8_t *p, const uint8_t *end, size_t *len, size_t *used)
160
+ {
161
+ uint32_t result;
162
+ unsigned i;
163
+ size_t avail;
164
+
165
+ if (p == NULL || end == NULL || len == NULL || used == NULL ||
166
+ p >= end) {
167
+ return 0;
168
+ }
169
+ result = p[0];
170
+ if (result < 0x80) {
171
+ *len = result;
172
+ *used = 1;
173
+ return 1;
174
+ }
175
+ result &= 0x7f;
176
+ avail = pbw_remaining(end, p);
177
+ for (i = 1; i < 5 && (size_t)i < avail; i++) {
178
+ uint32_t byte = p[i];
179
+
180
+ if (i == 4 && (byte & 0x7f) > 0x0f) {
181
+ return 0;
182
+ }
183
+ result |= (byte & 0x7f) << (7 * i);
184
+ if (byte < 0x80) {
185
+ *len = (size_t)result;
186
+ *used = i + 1;
187
+ return 1;
188
+ }
189
+ }
190
+ return 0;
191
+ }
192
+
193
+ /* A word made only of bytes below 0x80 has no high bit set, on either
194
+ * endianness, so one test covers eight bytes of an ASCII run: a long value
195
+ * costs one load and one branch per 16 bytes instead of one per byte. The
196
+ * rules for the non-ASCII sequences live in pbw_utf8_seq (internal.h). */
197
+ #define PBW_UTF8_HIGH 0x8080808080808080ull
198
+
199
+ int
200
+ pbw_utf8_valid(const uint8_t *data, size_t len)
201
+ {
202
+ size_t i = 0;
203
+
204
+ if (len == 0) {
205
+ return 1; /* an empty buffer may carry a NULL data pointer */
206
+ }
207
+ while (i < len) {
208
+ int n;
209
+
210
+ if (data[i] >= 0x80) {
211
+ n = pbw_utf8_seq(data + i, len - i);
212
+ if (n == 0) {
213
+ return 0;
214
+ }
215
+ i += (size_t)n;
216
+ continue;
217
+ }
218
+ /* ASCII run: two words per test, then one, then the last few bytes. */
219
+ while (i + 16 <= len) {
220
+ uint64_t a, b;
221
+
222
+ memcpy(&a, data + i, sizeof a);
223
+ memcpy(&b, data + i + 8, sizeof b);
224
+ if ((a | b) & PBW_UTF8_HIGH) {
225
+ break;
226
+ }
227
+ i += 16;
228
+ }
229
+ while (i + 8 <= len) {
230
+ uint64_t a;
231
+
232
+ memcpy(&a, data + i, sizeof a);
233
+ if (a & PBW_UTF8_HIGH) {
234
+ break;
235
+ }
236
+ i += 8;
237
+ }
238
+ while (i < len && data[i] < 0x80) {
239
+ i++;
240
+ }
241
+ }
242
+ return 1;
243
+ }
244
+
245
+ void
246
+ pbw_buf_init(pbw_buf *buf)
247
+ {
248
+ buf->data = NULL;
249
+ buf->len = 0;
250
+ buf->cap = 0;
251
+ buf->oom = 0;
252
+ buf->fixed = 0;
253
+ }
254
+
255
+ void
256
+ pbw_buf_init_fixed(pbw_buf *buf, void *data, size_t cap)
257
+ {
258
+ buf->data = (uint8_t *)data;
259
+ buf->len = 0;
260
+ buf->cap = cap;
261
+ buf->oom = 0;
262
+ buf->fixed = 1;
263
+ }
264
+
265
+ void
266
+ pbw_buf_free(pbw_buf *buf)
267
+ {
268
+ free(buf->data);
269
+ pbw_buf_init(buf);
270
+ }
271
+
272
+ int
273
+ pbw_buf_reserve(pbw_buf *buf, size_t extra)
274
+ {
275
+ size_t need;
276
+ size_t cap;
277
+ uint8_t *data;
278
+
279
+ if (buf == NULL || buf->oom) {
280
+ return 0;
281
+ }
282
+ if (!pbw_cap_holds(buf->cap, buf->len)) {
283
+ buf->oom = 1;
284
+ return 0;
285
+ }
286
+ need = buf->len + extra;
287
+ if (need < buf->len) {
288
+ /* Size overflow: there is no buffer that big. */
289
+ buf->oom = 1;
290
+ return 0;
291
+ }
292
+ if (need <= buf->cap) {
293
+ return 1;
294
+ }
295
+ if (buf->fixed) {
296
+ /* Caller memory: report the overflow as "does not fit". */
297
+ buf->oom = 1;
298
+ return 0;
299
+ }
300
+ cap = buf->cap ? buf->cap : 64;
301
+ while (cap < need) {
302
+ if (cap > SIZE_MAX / 2) {
303
+ cap = need;
304
+ break;
305
+ }
306
+ cap *= 2;
307
+ }
308
+ data = (uint8_t *)realloc(buf->data, cap);
309
+ if (data == NULL) {
310
+ buf->oom = 1;
311
+ return 0;
312
+ }
313
+ buf->data = data;
314
+ buf->cap = cap;
315
+ return 1;
316
+ }
317
+
318
+ uint32_t
319
+ pbw_read_le32(const uint8_t *in)
320
+ {
321
+ return (uint32_t)in[0] | ((uint32_t)in[1] << 8) | ((uint32_t)in[2] << 16) |
322
+ ((uint32_t)in[3] << 24);
323
+ }
324
+
325
+ /* Exact (a, b) = a + b with the rounding error in *e (Knuth TwoSum: no
326
+ * branches, no FMA, exact with or without denormals as long as nothing
327
+ * overflows to infinity). The compiler must not fuse these into an FMA:
328
+ * there is no multiply here, so separate statements stay separate. */
329
+ static void
330
+ pbw_twosum(double a, double b, double *s, double *e)
331
+ {
332
+ double v = a + b;
333
+ double w = v - a;
334
+
335
+ *s = v;
336
+ *e = (a - (v - w)) + (b - w);
337
+ }
338
+
339
+ /* '.'-only strtod: the JSON and text grammars use '.' and never touch the
340
+ * host locale, so neither a foreign LC_NUMERIC nor a concurrent setlocale
341
+ * can change the result. Grammar: [space]* [+-]? (digits [. digits]? |
342
+ * . digits) ([eE] [+-]? digits)?. End points at the first unconsumed byte
343
+ * (the text start when nothing converts), like strtod.
344
+ *
345
+ * Precision: the first 25 significant digits accumulate into an exact-ish
346
+ * (hi, lo) pair (error ~2^-100) and the decimal exponent scales it with
347
+ * exact x10 steps, so short inputs round exactly once, like strtod.
348
+ * Digits past the 25th shift the value by under 1e-25 relative (far below
349
+ * half an ulp) and only cost position bookkeeping. Saturation (inf/zero)
350
+ * is decided by integer decimal-exponent math, so DBL_MAX/DBL_MIN parse
351
+ * exactly and gigantic exponents cannot spin the scaler. */
352
+ double
353
+ pbw_strtod_c(const char *text, char **end)
354
+ {
355
+ const char *p = text;
356
+ int neg = 0;
357
+ /* Exact-ish value pair: hi + lo, error ~2^-100 relative. */
358
+ double hi = 0.0;
359
+ double lo = 0.0;
360
+ long intpart = 0; /* significant digits before the '.' (all counted) */
361
+ long lzfrac = 0; /* zeros after the '.' before the first digit */
362
+ long acc = 0; /* significant digits accumulated (max 25) */
363
+ int seendot = 0;
364
+ int seendig = 0;
365
+ long expval = 0;
366
+ int expneg = 0;
367
+ int seenexpdig = 0;
368
+ /* Decimal exponent of the leading digit, then the scale the pair
369
+ * still needs: both plain longs, so no float is involved. */
370
+ long edec;
371
+ long scale;
372
+ long k;
373
+ double v;
374
+
375
+ while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') {
376
+ p++;
377
+ }
378
+ if (*p == '+' || *p == '-') {
379
+ neg = *p == '-';
380
+ p++;
381
+ }
382
+ /* strtod-compatible spellings the callers rely on: inf/nan in any case
383
+ * and C99 hex ("0x10" for integer fields). Hex scales by powers of two,
384
+ * which are exact. */
385
+ if ((p[0] | 32) == 'i' && (p[1] | 32) == 'n' && (p[2] | 32) == 'f') {
386
+ p += 3;
387
+ if ((p[0] | 32) == 'i' && (p[1] | 32) == 'n' &&
388
+ (p[2] | 32) == 'i' && (p[3] | 32) == 't' &&
389
+ (p[4] | 32) == 'y') {
390
+ p += 5;
391
+ }
392
+ if (end != NULL) {
393
+ *end = (char *)p;
394
+ }
395
+ v = HUGE_VAL;
396
+ return neg ? -v : v;
397
+ }
398
+ if ((p[0] | 32) == 'n' && (p[1] | 32) == 'a' && (p[2] | 32) == 'n') {
399
+ /* No (n-char-seq): strtod's sequence handling differs per libc,
400
+ * and no caller passes it. */
401
+ if (end != NULL) {
402
+ *end = (char *)(p + 3);
403
+ }
404
+ v = (double)NAN;
405
+ return neg ? -v : v;
406
+ }
407
+ if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
408
+ const char *q = p + 2;
409
+ uint64_t hmant = 0;
410
+ long hfrac = 0; /* hex digits after the '.' (each is 4 bits) */
411
+ long hdrop = 0; /* integer digits past the uint64 window */
412
+ long hscale;
413
+ long hexp = 0;
414
+ int hexpneg = 0;
415
+ int hdig = 0;
416
+ int hdot = 0;
417
+
418
+ for (;; q++) {
419
+ int d;
420
+
421
+ if (*q >= '0' && *q <= '9') {
422
+ d = *q - '0';
423
+ } else if ((*q | 32) >= 'a' && (*q | 32) <= 'f') {
424
+ d = (*q | 32) - 'a' + 10;
425
+ } else if (*q == '.' && !hdot) {
426
+ hdot = 1;
427
+ continue;
428
+ } else {
429
+ break;
430
+ }
431
+ hdig = 1;
432
+ if ((hmant >> 60) != 0) {
433
+ /* Window full: a dropped integer digit still shifts the
434
+ * value; a dropped fraction digit only holds position. */
435
+ if (!hdot && hdrop < 999999L) {
436
+ hdrop++;
437
+ }
438
+ if (hdot && hfrac < 999999L) {
439
+ hfrac++;
440
+ }
441
+ continue;
442
+ }
443
+ if (hdot && hfrac < 999999L) {
444
+ hfrac++;
445
+ }
446
+ hmant = hmant * 16u + (uint64_t)d;
447
+ }
448
+ if (hdig) {
449
+ int seenp = 0;
450
+
451
+ p = q;
452
+ if (*p == 'p' || *p == 'P') {
453
+ q = p + 1;
454
+ if (*q == '+' || *q == '-') {
455
+ hexpneg = *q == '-';
456
+ q++;
457
+ }
458
+ while (*q >= '0' && *q <= '9') {
459
+ seenp = 1;
460
+ if (hexp < 999999L) {
461
+ hexp = hexp * 10L + (*q - '0');
462
+ }
463
+ q++;
464
+ }
465
+ if (seenp) {
466
+ p = q;
467
+ }
468
+ }
469
+ if (end != NULL) {
470
+ *end = (char *)p;
471
+ }
472
+ if (hmant == 0) {
473
+ return neg ? -0.0 : 0.0;
474
+ }
475
+ hscale = (hexpneg ? -hexp : hexp) - 4L * hfrac + 4L * hdrop;
476
+ if (hscale > 999999L) {
477
+ hscale = 999999L;
478
+ }
479
+ if (hscale < -999999L) {
480
+ hscale = -999999L;
481
+ }
482
+ v = (double)hmant;
483
+ if (hscale < 0) {
484
+ for (k = hscale; k < 0; k++) {
485
+ v /= 2.0;
486
+ if (v == 0.0) {
487
+ break;
488
+ }
489
+ }
490
+ } else {
491
+ for (k = 0; k < hscale; k++) {
492
+ v *= 2.0;
493
+ if (isinf(v)) {
494
+ break;
495
+ }
496
+ }
497
+ }
498
+ return neg ? -v : v;
499
+ }
500
+ /* else: a bare "0x" parses as decimal 0, like strtod. */
501
+ }
502
+ for (;; p++) {
503
+ if (*p >= '0' && *p <= '9') {
504
+ int d = *p - '0';
505
+ double s1, s2, t1, t0, l10, u1, u0, w1, w0;
506
+
507
+ seendig = 1;
508
+ if (acc == 0 && d == 0) {
509
+ /* Leading zeros are insignificant: only their position
510
+ * counts (after the dot), never their value. */
511
+ if (seendot) {
512
+ lzfrac++;
513
+ }
514
+ continue;
515
+ }
516
+ if (!seendot) {
517
+ intpart++;
518
+ }
519
+ if (acc >= 25) {
520
+ /* Position already booked in Edec below; the value is
521
+ * settled far past half an ulp. Still consumed for end. */
522
+ continue;
523
+ }
524
+ acc++;
525
+ /* Pair digit step: (hi, lo) = (hi, lo) * 10 + d. The digit
526
+ * joins the large term through an exact TwoSum first; the
527
+ * tiny residuals fold at their own scale afterwards. Adding
528
+ * them to the digit in one plain sum would round them away. */
529
+ s1 = hi * 8.0;
530
+ s2 = hi * 2.0;
531
+ pbw_twosum(s1, s2, &t1, &t0);
532
+ l10 = lo * 10.0;
533
+ pbw_twosum(t1, l10, &u1, &u0);
534
+ pbw_twosum(u1, (double)d, &w1, &w0);
535
+ hi = w1;
536
+ lo = w0 + u0 + t0;
537
+ } else if (*p == '.' && !seendot) {
538
+ seendot = 1;
539
+ } else {
540
+ break;
541
+ }
542
+ }
543
+ if (!seendig) {
544
+ if (end != NULL) {
545
+ *end = (char *)text;
546
+ }
547
+ return 0.0;
548
+ }
549
+ if (*p == 'e' || *p == 'E') {
550
+ const char *q = p + 1;
551
+ int qneg = 0;
552
+
553
+ if (*q == '+' || *q == '-') {
554
+ qneg = *q == '-';
555
+ q++;
556
+ }
557
+ while (*q >= '0' && *q <= '9') {
558
+ seenexpdig = 1;
559
+ /* Saturate: beyond +-999999 the scale below over/underflows
560
+ * anyway, but every digit is still consumed for the end. */
561
+ if (expval < 999999L) {
562
+ expval = expval * 10L + (*q - '0');
563
+ }
564
+ q++;
565
+ }
566
+ if (seenexpdig) {
567
+ p = q;
568
+ expneg = qneg;
569
+ }
570
+ /* else: a bare 'e' (as in "1e") ends the number, like strtod. */
571
+ }
572
+ if (end != NULL) {
573
+ *end = (char *)p;
574
+ }
575
+ if (acc == 0) {
576
+ return neg ? -0.0 : 0.0;
577
+ }
578
+ /* Decimal exponent of the leading digit: exact integer math, so the
579
+ * saturation verdicts below never depend on a rounded float. A value
580
+ * at or past 1e309 overflows; below 1e-324 it is zero. */
581
+ if (intpart > 0) {
582
+ edec = (expneg ? -expval : expval) + intpart - 1;
583
+ } else {
584
+ edec = (expneg ? -expval : expval) - lzfrac - 1;
585
+ }
586
+ if (edec > 308) {
587
+ return neg ? -HUGE_VAL : HUGE_VAL;
588
+ }
589
+ if (edec < -324) {
590
+ return neg ? -0.0 : 0.0;
591
+ }
592
+ /* The pair holds `acc` significant digits, so shifting it by
593
+ * Edec - acc + 1 lands on the true value. In range that shift is at
594
+ * most ~350 steps, whatever the input looked like. */
595
+ scale = edec - acc + 1;
596
+ if (scale == 0) {
597
+ v = hi + lo;
598
+ return neg ? -v : v;
599
+ }
600
+ if (scale < 0) {
601
+ for (k = scale; k < 0; k++) {
602
+ /* Compensated /10: q1 carries the quotient, the TwoSum chain
603
+ * folds the exact remainder back in. */
604
+ double q1 = hi / 10.0;
605
+ double p1 = q1 * 8.0;
606
+ double p2 = q1 * 2.0;
607
+ double s1, s0, d1, d0, e1, e0, l10, r, r1, rr, n1, n0;
608
+
609
+ pbw_twosum(p1, p2, &s1, &s0);
610
+ pbw_twosum(hi, -s1, &d1, &d0);
611
+ pbw_twosum(d1, -s0, &e1, &e0);
612
+ l10 = lo / 10.0;
613
+ /* Every remainder piece scales: e1 is the large one, the
614
+ * rest fold at their own tiny scale. Forgetting the /10 on
615
+ * (e0 + d0) poisoned the correction by 10x. */
616
+ r = (e0 + d0) / 10.0 + l10;
617
+ r1 = e1 / 10.0;
618
+ rr = r1 + r;
619
+ pbw_twosum(q1, rr, &n1, &n0);
620
+ hi = n1;
621
+ lo = n0;
622
+ if (hi == 0.0 && lo == 0.0) {
623
+ break;
624
+ }
625
+ }
626
+ } else {
627
+ for (k = 0; k < scale; k++) {
628
+ /* Exact x10: powers of two never round (an overflow verdict
629
+ * stays correct: the true value overflows too). */
630
+ double s1 = hi * 8.0;
631
+ double s2 = hi * 2.0;
632
+ double t1, t0, l10, u1, u0, v0, n1, n0;
633
+
634
+ pbw_twosum(s1, s2, &t1, &t0);
635
+ l10 = lo * 10.0;
636
+ pbw_twosum(t1, l10, &u1, &u0);
637
+ v0 = u0 + t0;
638
+ pbw_twosum(u1, v0, &n1, &n0);
639
+ hi = n1;
640
+ lo = n0;
641
+ /* An overflowed step poisons the pair through inf - inf =
642
+ * NaN, so catch either and report infinity (inputs here are
643
+ * finite, and the true value overflows with them). */
644
+ if (!isfinite(hi)) {
645
+ hi = HUGE_VAL;
646
+ lo = 0.0;
647
+ break;
648
+ }
649
+ }
650
+ }
651
+ v = hi + lo;
652
+ return neg ? -v : v;
653
+ }
654
+
655
+ /* '.'-only %g: snprintf follows LC_NUMERIC, so a foreign locale may emit its
656
+ * own separator. It is the one punctuation mark between the leading digits
657
+ * and the exponent (or end): '.' in the C locale, the host separator
658
+ * otherwise. inf/nan have no leading digits and pass through untouched. */
659
+ int
660
+ pbw_format_g_c(char *buf, size_t cap, int precision, double value)
661
+ {
662
+ int n = snprintf(buf, cap, "%.*g", precision, value);
663
+ char *p;
664
+ char *digits;
665
+ size_t seplen;
666
+
667
+ if (n <= 0 || (size_t)n >= cap) {
668
+ return n;
669
+ }
670
+ p = buf;
671
+ if (*p == '-' || *p == '+') {
672
+ p++;
673
+ }
674
+ digits = p;
675
+ while (*p >= '0' && *p <= '9') {
676
+ p++;
677
+ }
678
+ if (p == digits || *p == '.' || *p == 'e' || *p == 'E' || *p == '\0') {
679
+ return n;
680
+ }
681
+ /* A foreign separator: ASCII is one byte, otherwise a UTF-8 sequence
682
+ * whose lead byte gives the width. Anything odder (a non-UTF-8
683
+ * multibyte) is consumed up to the next digit or exponent mark. */
684
+ if ((*p & 0x80) == 0) {
685
+ seplen = 1;
686
+ } else if ((*p & 0xe0) == 0xc0) {
687
+ seplen = 2;
688
+ } else if ((*p & 0xf0) == 0xe0) {
689
+ seplen = 3;
690
+ } else if ((*p & 0xf8) == 0xf0) {
691
+ seplen = 4;
692
+ } else {
693
+ seplen = 1;
694
+ while (p[seplen] != '\0' && !(p[seplen] >= '0' && p[seplen] <= '9') &&
695
+ p[seplen] != 'e' && p[seplen] != 'E') {
696
+ seplen++;
697
+ }
698
+ }
699
+ /* snprintf counted every byte it wrote (`n` includes the separator) and
700
+ * the separator is always followed by a fraction digit, so the memmove
701
+ * length below cannot wrap. */
702
+ *p = '.';
703
+ memmove(p + 1, p + seplen,
704
+ (size_t)n - (size_t)(p - buf) - seplen + 1);
705
+ return (int)((size_t)n - (seplen - 1));
706
+ }
707
+
708
+ void
709
+ pbw_free(void *ptr)
710
+ {
711
+ free(ptr);
712
+ }
713
+
714
+ const char *
715
+ pbw_strerror(pbw_status status)
716
+ {
717
+ switch (status) {
718
+ case PBW_OK:
719
+ return "ok";
720
+ case PBW_ERR_INVALID:
721
+ return "invalid serialized data";
722
+ case PBW_ERR_NOMEM:
723
+ return "out of memory";
724
+ case PBW_ERR_NOT_FOUND:
725
+ return "field not found";
726
+ case PBW_ERR_TYPE:
727
+ return "type mismatch";
728
+ case PBW_ERR_LIMIT:
729
+ return "limit exceeded";
730
+ case PBW_ERR_REQUIRED:
731
+ return "required field not set";
732
+ default:
733
+ return "unknown error";
734
+ }
735
+ }