2 * UCW Library: Reading and writing of UTF-8 on Fastbuf Streams
4 * (c) 2001--2004 Martin Mares <mj@ucw.cz>
5 * (c) 2004 Robert Spalek <robert@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include "lib/fastbuf.h"
13 #include "lib/unicode.h"
14 #include "lib/ff-utf8.h"
17 bget_utf8_slow(struct fastbuf *b)
22 if (c < 0x80) /* Includes EOF */
24 if (c < 0xc0) /* Incorrect combination */
25 return UNI_REPLACEMENT;
26 if (c >= 0xf0) /* Too large, skip it */
28 while ((c = bgetc(b)) >= 0x80 && c < 0xc0)
32 if (c >= 0xe0) /* 3 bytes */
35 if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
37 code = (code << 6) | (c & 0x3f);
38 if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
40 code = (code << 6) | (c & 0x3f);
45 if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
47 code = (code << 6) | (c & 0x3f);
54 return UNI_REPLACEMENT;
58 bget_utf8_32_slow(struct fastbuf *b)
64 if (c < 0x80) /* Includes EOF */
66 if (c < 0xc0) /* Incorrect combination */
67 return UNI_REPLACEMENT;
93 else /* Too large, skip it */
95 while ((c = bgetc(b)) >= 0x80 && c < 0xc0)
101 if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
103 code = (code << 6) | (c & 0x3f);
110 return UNI_REPLACEMENT;
114 bput_utf8_slow(struct fastbuf *b, uns u)
122 bputc(b, 0xc0 | (u >> 6));
125 bputc(b, 0xe0 | (u >> 12));
126 bputc(b, 0x80 | ((u >> 6) & 0x3f));
128 bputc(b, 0x80 | (u & 0x3f));
133 bput_utf8_32_slow(struct fastbuf *b, uns u)
135 ASSERT(u < (1U<<31));
141 bputc(b, 0xc0 | (u >> 6));
145 bputc(b, 0xe0 | (u >> 12));
149 bputc(b, 0xf0 | (u >> 18));
153 bputc(b, 0xf8 | (u >> 24));
156 bputc(b, 0xfc | (u >> 30));
157 bputc(b, 0x80 | ((u >> 24) & 0x3f));
159 bputc(b, 0x80 | ((u >> 18) & 0x3f));
161 bputc(b, 0x80 | ((u >> 12) & 0x3f));
163 bputc(b, 0x80 | ((u >> 6) & 0x3f));
165 bputc(b, 0x80 | (u & 0x3f));