/*
* UCW Library: Reading and writing of UTF-8 on Fastbuf Streams
*
- * (c) 2001--2004 Martin Mares <mj@ucw.cz>
+ * (c) 2001--2015 Martin Mares <mj@ucw.cz>
* (c) 2004 Robert Spalek <robert@ucw.cz>
*
* This software may be freely distributed and used according to the terms
/*** UTF-8 ***/
int
-bget_utf8_slow(struct fastbuf *b, uns repl)
+bget_utf8_slow(struct fastbuf *b, uint repl)
{
int c = bgetc(b);
int code;
if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
goto wrong;
code = (code << 6) | (c & 0x3f);
+ if (code < 0x800)
+ goto wrong2;
}
else /* 2 bytes */
{
if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
goto wrong;
code = (code << 6) | (c & 0x3f);
+ if (code < 0x80)
+ goto wrong2;
}
return code;
- wrong:
+wrong:
if (c >= 0)
bungetc(b);
+wrong2:
return repl;
}
int
-bget_utf8_32_slow(struct fastbuf *b, uns repl)
+bget_utf8_32_slow(struct fastbuf *b, uint repl)
{
int c = bgetc(b);
int code;
int nr;
+ int limit;
if (c < 0x80) /* Includes EOF */
return c;
{
code = c & 0x1f;
nr = 1;
+ limit = 0x80;
}
else if (c < 0xf0)
{
code = c & 0x0f;
nr = 2;
+ limit = 0x800;
}
else if (c < 0xf8)
{
code = c & 0x07;
nr = 3;
+ limit = 1 << 16;
}
else if (c < 0xfc)
{
code = c & 0x03;
nr = 4;
+ limit = 1 << 21;
}
else if (c < 0xfe)
{
code = c & 0x01;
nr = 5;
+ limit = 1 << 26;
}
- else /* Too large, skip it */
- {
- while ((c = bgetc(b)) >= 0x80 && c < 0xc0)
- ;
- goto wrong;
- }
+ else /* Too large */
+ goto wrong2;
while (nr-- > 0)
{
if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
goto wrong;
code = (code << 6) | (c & 0x3f);
}
+ if (code < limit)
+ goto wrong2;
return code;
- wrong:
+wrong:
if (c >= 0)
bungetc(b);
+wrong2:
return repl;
}
void
-bput_utf8_slow(struct fastbuf *b, uns u)
+bput_utf8_slow(struct fastbuf *b, uint u)
{
ASSERT(u < 65536);
if (u < 0x80)
}
void
-bput_utf8_32_slow(struct fastbuf *b, uns u)
+bput_utf8_32_slow(struct fastbuf *b, uint u)
{
ASSERT(u < (1U<<31));
if (u < 0x80)
/*** UTF-16 ***/
int
-bget_utf16_be_slow(struct fastbuf *b, uns repl)
+bget_utf16_be_slow(struct fastbuf *b, uint repl)
{
if (bpeekc(b) < 0)
return -1;
- uns u = bgetw_be(b), x, y;
+ uint u = bgetw_be(b), x, y;
if ((int)u < 0)
return repl;
if ((x = u - 0xd800) >= 0x800)
}
int
-bget_utf16_le_slow(struct fastbuf *b, uns repl)
+bget_utf16_le_slow(struct fastbuf *b, uint repl)
{
if (bpeekc(b) < 0)
return -1;
- uns u = bgetw_le(b), x, y;
+ uint u = bgetw_le(b), x, y;
if ((int)u < 0)
return repl;
if ((x = u - 0xd800) >= 0x800)
}
void
-bput_utf16_be_slow(struct fastbuf *b, uns u)
+bput_utf16_be_slow(struct fastbuf *b, uint u)
{
if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
{
}
void
-bput_utf16_le_slow(struct fastbuf *b, uns u)
+bput_utf16_le_slow(struct fastbuf *b, uint u)
{
if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
{
#undef F
};
- uns func = ~0U;
+ uint func = ~0U;
if (argc > 1)
- for (uns i = 0; i < ARRAY_SIZE(names); i++)
+ for (uint i = 0; i < ARRAY_SIZE(names); i++)
if (!strcasecmp(names[i], argv[1]))
func = i;
if (!~func)
struct fastbuf *b = fbgrow_create(8);
if (func < FUNC_BPUT_UTF8)
{
- uns u;
+ uint u;
while (scanf("%x", &u) == 1)
bputc(b, u);
fbgrow_rewind(b);
}
else
{
- uns u, i = 0;
+ uint u, i = 0;
while (scanf("%x", &u) == 1)
{
switch (func)