5 * cc utf8bin.c -o ~/bin/utf8bin
7 * setenv LANG en_US.UTF-8
8 * dd if=/dev/urandom bs=32k count=1024 | utf8bin
10 * Test round-trip UTF8-B binary escaping functions.
12 #include <sys/types.h>
21 main(int ac
, char **av
)
37 * NOTE: If we use WCSBIN_SURRO the round-trip will not be 8-bit
40 * Typically use either 0 or WCSBIN_LONGCODE, both of which
41 * are 8-bit clean, will escape, and cannot error on input
42 * (or generate wchars that might error on output) given the
46 flags
= WCSBIN_LONGCODES
;
48 setlocale(LC_ALL
, "");
50 while ((flags
& WCSBIN_EOF
) == 0 &&
51 (r1
= read(0, ibuf
+ x
, sizeof(ibuf
) - x
)) >= 0) {
52 /* allow final loop for loose ends */
57 w1
= mbintowcr(warray
, ibuf
, 1024, &r2
, flags
);
59 /* round-trip, output buffer can be same size as input buffer */
61 o
= wcrtombin(obuf
, warray
, sizeof(obuf
), &w2
, flags
);
64 printf("read %4d/%-4d wc=%4d/%-4d write=%-4d\t",
67 if (bcmp(ibuf
, obuf
, o
) == 0) {
69 (flags
& WCSBIN_EOF
) ? '\n' : '\r');
72 printf("compare-fail\n");
75 if (bcmp(ibuf
, obuf
, r2
) == 0)
76 printf("len-fail, rest-ok\n");
78 printf("len-fail, compare-fail\n");
80 if (bcmp(ibuf
, obuf
, o
) == 0)
81 printf("len-fail, rest-ok\n");
83 printf("len-fail, compare-fail\n");
85 for (i
= failed
= 0; i
< r2
&& i
< o
; ++i
) {
86 if (ibuf
[i
] != obuf
[i
]) {
87 printf(" @%04x %02x %02x\n",
88 i
, (uint8_t)ibuf
[i
], (uint8_t)obuf
[i
]);
92 printf(" @%04x %02x %02x\n",
93 i
, (uint8_t)ibuf
[i
], (uint8_t)obuf
[i
]);
99 bcopy(ibuf
+ r2
, ibuf
, x
);