/* Copyright (c) 2006 Sam Ruby Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include /* The scenario is as follows: you have some data that is intended to be * interpreted as utf-8. You may have invested a lot of time in cleaning * it, a minimal amount of time cleaning it, or none at all; in any case * this data is intended to be placed into an XML document which is * rather unforgiving of encoding errors. * * At a minimum, XML markup characters needs to be escaped. * * In the normal case, this code does nothing more than a quick scan of * the input, and returns it back. If, however, it finds something amiss * it will allocate another block of memory and attempt to correct a few of * the most common errors. If this occurs, it is the callers responsibility * to free the block that was allocated. */ unsigned char *clean_utf8_for_xml(unsigned char *in, int escape_for_attr) { int expand = 0; unsigned char *c; unsigned char *out; /* scan the bytes, using the checks specified in * http://www.w3.org/International/questions/qa-forms-utf-8.html * if there are problems, accumulate a count of the number of additional * bytes that will be required in the output buffer */ for (c=in; *c; c++) { if (*c < 0x20) { if (*c != 0x09 && *c != 0x0A && *c != 0x0D) expand += 2; } else if (*c <= 0x7E) { if (*c == '&') { expand += 4; } else if (*c == '<') { expand += 3; } else if (escape_for_attr) { if (*c == '>') { expand += 3; } else if (*c == '"') { expand += 5; } else if (*c == '\'') { expand += 5; } } } else if (*c == 0x7F) { expand += 2; } else { if (*c < 0xC2) { /* noop */; } else if (*c <= 0xDF) { if (c[1] >= 0x80 && c[1] <= 0xBF) { c+=1; continue; } } else if (*c == 0xE0) { if (c[1] >= 0xA0 && c[1] <= 0xBF && c[2] >= 0x80 && c[2] <= 0xBF) { c+=2; continue; } } else if (*c == 0xED) { if (c[1] >= 0x80 && c[1] <= 0x9F && c[2] >= 0x80 && c[2] <= 0xBF) { c+=2; continue; } } else if (*c <= 0xEE) { if (c[1] >= 0x80 && c[1] <= 0xBF && c[2] >= 0x80 && c[2] <= 0xBF) { c+=2; continue; } } else if (*c == 0xEF) { if (c[1] >= 0x80 && c[1] < 0xBF && c[2] >= 0x80 && c[2] <= 0xBF) { c+=2; continue; } if (c[1] >= 0x80 && c[1] == 0xBF && c[2] >= 0x80 && c[2] <= 0xBD) { c+=2; continue; } } else if (*c == 0xF0) { if (c[1] >= 0x90 && c[1] <= 0xBF && c[2] >= 0x80 && c[2] <= 0xBF) { c+=2; continue; } } else if (*c <= 0xF3) { if (c[1] >= 0x80 && c[1] <= 0xBF && c[2] >= 0x80 && c[2] <= 0xBF && c[3] >= 0x80 && c[3] <= 0xBF) { c+=3; continue; } } else if (*c <= 0xF4) { if (c[1] >= 0x80 && c[1] <= 0x8F && c[2] >= 0x80 && c[2] <= 0xBF && c[3] >= 0x80 && c[3] <= 0xBF) { c+=3; continue; } } expand++; if (*c <= 0x9F) expand++; } } /* if all is well, return now */ if (expand == 0) return in; /* attempt to allocate a new buffer. If that fails, bail */ out = malloc(strlen((char*)in)+expand+1); if (!out) return in; /* copy/convert the characters from the input to the output */ for (c=out; *in; in++) { if (*in < 0x20) { if (*in == 0x09 || *in == 0x0A || *in == 0x0D) { *c++ = *in; } else { c = (unsigned char *)strcpy((char*)c, "\xEF\xBF\xBD") + 3; } } else if (*in <= 0x7E) { if (*in == '&') { c = (unsigned char *)strcpy((char*)c, "&") + 5; } else if (*in == '<') { c = (unsigned char *)strcpy((char*)c, "<") + 4; } else if (escape_for_attr) { if (*in == '>') { c = (unsigned char *)strcpy((char*)c, ">") + 4; } else if (*in == '"') { c = (unsigned char *)strcpy((char*)c, "'") + 6; } else if (*in == '\'') { c = (unsigned char *)strcpy((char*)c, """) + 6; } } else { *c++ = *in; } } else if (*in == 0x7F) { c = (unsigned char *)strcpy((char*)c, "\xEF\xBF\xBD") + 3; } else { if (*in < 0xC2) { /* noop */; } else if (*in <= 0xDF) { if (in[1] >= 0x80 && in[1] <= 0xBF) { *c++ = *in++; *c++ = *in; continue; } } else if (*in == 0xE0) { if (in[1] >= 0xA0 && in[1] <= 0xBF && in[2] >= 0x80 && in[2] <= 0xBF) { *c++ = *in++; *c++ = *in++; *c++ = *in; continue; } } else if (*in == 0xED) { if (in[1] >= 0x80 && in[1] <= 0x9F && in[2] >= 0x80 && in[2] <= 0xBF) { *c++ = *in++; *c++ = *in++; *c++ = *in; continue; } } else if (*in <= 0xEE) { if (in[1] >= 0x80 && in[1] <= 0xBF && in[2] >= 0x80 && in[2] <= 0xBF) { *c++ = *in++; *c++ = *in++; *c++ = *in; continue; } } else if (*in == 0xEF) { if (in[1] >= 0x80 && in[1] <= 0xBF && in[2] >= 0x80 && in[2] <= 0xBF) { *c++ = *in++; *c++ = *in++; *c++ = (*in <= 0xBD ? *in : 0xBD); continue; } } else if (*in == 0xF0) { if (in[1] >= 0x90 && in[1] <= 0xBF && in[2] >= 0x80 && in[2] <= 0xBF) { *c++ = *in++; *c++ = *in++; *c++ = *in; continue; } } else if (*in <= 0xF3) { if (in[1] >= 0x80 && in[1] <= 0xBF && in[2] >= 0x80 && in[2] <= 0xBF && in[3] >= 0x80 && in[3] <= 0xBF) { *c++ = *in++; *c++ = *in++; *c++ = *in++; *c++ = *in; continue; } } else if (*in <= 0xF4) { if (in[1] >= 0x80 && in[1] <= 0x8F && in[2] >= 0x80 && in[2] <= 0xBF && in[3] >= 0x80 && in[3] <= 0xBF) { *c++ = *in++; *c++ = *in++; *c++ = *in++; *c++ = *in; continue; } } if (*in <= 0x9F) { if (*in == 0x80) { /* € */ c = (unsigned char *)strcpy((char*)c, "\xE2\x82\xAC") + 3; } else if (*in == 0x82) { /* ‚ */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\x9A") + 3; } else if (*in == 0x83) { /* ƒ */ c = (unsigned char *)strcpy((char*)c, "\xC6\x92") + 2; } else if (*in == 0x84) { /* „ */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\x9E") + 3; } else if (*in == 0x85) { /* … */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\xA6") + 3; } else if (*in == 0x86) { /* † */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\xA0") + 3; } else if (*in == 0x87) { /* ‡ */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\xA1") + 3; } else if (*in == 0x88) { /* ˆ */ c = (unsigned char *)strcpy((char*)c, "\xCB\x86") + 2; } else if (*in == 0x89) { /* ‰ */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\xB0") + 3; } else if (*in == 0x8A) { /* Š */ c = (unsigned char *)strcpy((char*)c, "\xC5\xA0") + 2; } else if (*in == 0x8B) { /* ‹ */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\xB9") + 3; } else if (*in == 0x8C) { /* Œ */ c = (unsigned char *)strcpy((char*)c, "\xC5\x92") + 2; } else if (*in == 0x8E) { /* Ž */ c = (unsigned char *)strcpy((char*)c, "\xC5\xBD") + 2; } else if (*in == 0x91) { /* ‘ */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\x98") + 3; } else if (*in == 0x92) { /* ’ */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\x99") + 3; } else if (*in == 0x93) { /* “ */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\x9C") + 3; } else if (*in == 0x94) { /* ” */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\x9D") + 3; } else if (*in == 0x95) { /* • */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\xA2") + 3; } else if (*in == 0x96) { /* – */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\x93") + 3; } else if (*in == 0x97) { /* — */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\x94") + 3; } else if (*in == 0x98) { /* ˜ */ c = (unsigned char *)strcpy((char*)c, "\xCB\x9C") + 2; } else if (*in == 0x99) { /* ™ */ c = (unsigned char *)strcpy((char*)c, "\xE2\x84\xA2") + 3; } else if (*in == 0x9A) { /* š */ c = (unsigned char *)strcpy((char*)c, "\xC5\xA1") + 2; } else if (*in == 0x9B) { /* › */ c = (unsigned char *)strcpy((char*)c, "\xE2\x80\xBA") + 3; } else if (*in == 0x9C) { /* œ */ c = (unsigned char *)strcpy((char*)c, "\xC5\x93") + 2; } else if (*in == 0x9E) { /* ž */ c = (unsigned char *)strcpy((char*)c, "\xC5\xBE") + 2; } else if (*in == 0x9F) { /* Ÿ */ c = (unsigned char *)strcpy((char*)c, "\xC5\xB8") + 2; } else { *c++ = 0xC2; *c++ = *in; } } else if (*in < 0xC0) { *c++ = 0xC2; *c++ = *in; } else { *c++ = 0xC3; *c++ = *in - 64; } } } *c++ = 0; return out; } int test(char *in, char *expect) { int status = 0; char *result = (char *)clean_utf8_for_xml((unsigned char *)in, 0); if (strcmp(expect, result)) { status = 1; printf("Expected \"%s\" but got \"%s\"\n", expect, result); } if (in != result) free(result); return status; } int main() { int rc = 0; /* simple ascii */ rc += test("abc", "abc"); /* control characters and white space */ rc += test("\x09", "\x09"); rc += test("\x0A", "\x0A"); rc += test("\x0B", "\xEF\xBF\xBD"); rc += test("\x0C", "\xEF\xBF\xBD"); rc += test("\x0D", "\x0D"); /* valid utf-8 */ rc += test("\xE2\x80\x99", "\xE2\x80\x99"); /* right single quote */ rc += test("\xC2\xA9", "\xC2\xA9"); /* copy */ /* xml predefined characters */ rc += test("&", "&"); rc += test("<", "<"); rc += test(">", ">"); /* common in email, and no need to escape */ /* character which are illegal in XML */ rc += test("\x01", "\xEF\xBF\xBD"); /* control character */ rc += test("\x0C", "\xEF\xBF\xBD"); /* form feed */ rc += test("\xEF\xBF\xBF", "\xEF\xBF\xBD"); /* U+FFFF */ /* iso-8859-1 */ rc += test("\xE7", "\xC3\xA7"); /* small c cedilla */ rc += test("\xA9", "\xC2\xA9"); /* copyright symbol */ /* win-1252 */ rc += test("\x92", "\xE2\x80\x99"); /* smart quote */ rc += test("\x80", "\xE2\x82\xAC"); /* euro */ if (rc) { printf ("Tests failed!\n"); } else { printf ("Tests passed\n"); } return rc; }