ossp-pkg/xds/xml-decode-string.c 1.1 -> 1.2
--- xml-decode-string.c 2001/08/08 11:21:27 1.1
+++ xml-decode-string.c 2001/08/08 15:10:02 1.2
@@ -25,9 +25,71 @@
SUCH DAMAGE.
*/
+#include <stdio.h>
#include <string.h>
#include "xds.h"
+#define INVALID 0x80000000
+
+#define get(c) c = *strptr++; \
+ if (chars) (*chars)++; \
+ if ((c) == 0) return (unsigned int)EOF
+
+static unsigned int sgetu8(unsigned char *strptr, int *chars)
+ {
+ unsigned int c;
+ int i, iterations;
+ unsigned char ch;
+
+ if (chars)
+ *chars = 0;
+
+ if (strptr == NULL)
+ return (unsigned int)EOF;
+
+ get(c);
+
+ if ((c & 0xFE) == 0xFC)
+ {
+ c &= 0x01;
+ iterations = 5;
+ }
+ else if ((c & 0xFC) == 0xF8)
+ {
+ c &= 0x03;
+ iterations = 4;
+ }
+ else if ((c & 0xF8) == 0xF0)
+ {
+ c &= 0x07;
+ iterations = 3;
+ }
+ else if ((c & 0xF0) == 0xE0)
+ {
+ c &= 0x0F;
+ iterations = 2;
+ }
+ else if ((c & 0xE0) == 0xC0)
+ {
+ c &= 0x1F;
+ iterations = 1;
+ }
+ else if ((c & 0x80) == 0x80)
+ return INVALID;
+ else return c;
+
+ for (i = 0; i < iterations; i++)
+ {
+ get(ch);
+ if ((ch & 0xC0) != 0x80)
+ return INVALID;
+ c <<= 6;
+ c |= ch & 0x3F;
+ }
+
+ return c;
+ }
+
static const char TAG_OPEN[] = "<string>";
static const char TAG_CLOSE[] = "</string>";
static const size_t TAG_OPEN_LEN = sizeof(TAG_OPEN)-1;
@@ -41,6 +103,8 @@
char* src;
size_t src_len;
char* dst;
+ int utf8_len;
+ unsigned int rc;
/* Setup the engine. We need at least space for our tags; how long
the actual content is going to be will be seen soon. */
@@ -100,6 +164,16 @@
return XDS_ERR_TYPE_MISMATCH;
}
}
+ else if (*((xds_uint8_t*)src) >= 0x80)
+ {
+ rc = sgetu8((xds_uint8_t*)src, &utf8_len);
+ if (rc == (unsigned int)EOF)
+ return XDS_ERR_UNDERFLOW;
+ else if (rc == INVALID || rc > 255)
+ return XDS_ERR_TYPE_MISMATCH;
+ *dst++ = (xds_uint8_t)rc;
+ src += utf8_len; src_len -= utf8_len;
+ }
else
{
*dst++ = *src++;
|
|