2 * The UniCode Library: Reading and writing of UTF-8 on Fastbuf Streams
4 * (c) 2001 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/fastbuf.h"
12 #include "charset/unicode.h"
13 #include "charset/unistream.h"
16 bget_utf8_slow(struct fastbuf *b)
21 if (c < 0x80) /* Includes EOF */
23 if (c < 0xc0) /* Incorrect combination */
24 return UNI_REPLACEMENT;
25 if (c >= 0xf0) /* Too large, skip it */
27 while ((c = bgetc(b)) >= 0x80 && c < 0xc0)
31 if (c >= 0xe0) /* 3 bytes */
34 if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
36 code = (code << 6) | (c & 0x3f);
37 if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
39 code = (code << 6) | (c & 0x3f);
44 if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
46 code = (code << 6) | (c & 0x3f);
53 return UNI_REPLACEMENT;
57 bput_utf8_slow(struct fastbuf *b, uns u)
65 bputc(b, 0xc0 | (u >> 6));
68 bputc(b, 0xe0 | (u >> 12));
69 bputc(b, 0x80 | ((u >> 6) & 0x3f));
71 bputc(b, 0x80 | (u & 0x3f));