Commit e141093c authored by Niels Möller's avatar Niels Möller
Browse files

New function parse_utf8().

Rev: src/parse.c:1.14
Rev: src/parse.h:1.13
parent 06a8d730
......@@ -112,6 +112,67 @@ int parse_uint8(struct simple_buffer *buffer, int *result)
return 1;
}
int parse_utf8(struct simple_buffer *buffer, UINT32 *result)
{
UINT32 first;
int length;
int i;
if (!LEFT)
return -1;
first = HERE[0];
if (first < 0x80)
{
*result = first;
return 1;
}
switch(first & 0xF0)
{
default:
return 0;
case 0xC0:
case 0xD0:
length = 2;
*result = first & 0x1F;
break;
case 0xE0:
length = 3;
*result = first & 0x0F;
break;
case 0xF0:
switch(first & 0x0E)
{
case 0: case 2: case 4: case 6:
length = 4;
*result = first & 0x07;
break;
case 8: case 0xA;
length = 5;
*result = first & 0x03;
break;
case 0xC:
length = 6;
*result = first & 0x01;
break;
default:
fatal("Internal error!\n");
}
break;
}
for(i = 1; i<length; i++)
{
UINT32 c = HERE[i];
if ( (c & 0xC0) != 0x80)
return 0;
*result = (*result << 6) | (c & 0x3f);
}
ADVANCE(length);
return 1;
}
int parse_boolean(struct simple_buffer *buffer, int *result)
{
if (!LEFT)
......
......@@ -64,6 +64,9 @@ int parse_sub_buffer(struct simple_buffer *buffer,
int parse_uint8(struct simple_buffer *buffer, int *result);
/* Returns 1 on success, 0 on error, and -1 at end of buffer */
int parse_utf8(struct simple_buffer *buffer, UINT32 *result);
int parse_boolean(struct simple_buffer *buffer, int *result);
int parse_bignum(struct simple_buffer *buffer, mpz_t result);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment