* Check for valid arguments and clear output...
*/
+ DEBUG_printf(("cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)\n", dest,
+ src ? (const char *)src : "(null)", maxout));
+
if (dest)
*dest = 0;
if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
+ {
+ DEBUG_puts("cupsUTF8ToUTF32: Returning -1 (bad arguments)");
+
return (-1);
+ }
/*
* Convert input UTF-8 to output UTF-32 (and insert BOM)...
*/
*dest++ = ch;
+
+ DEBUG_printf(("cupsUTF8ToUTF32: %02x => %08X\n", src[-1], ch));
continue;
}
else if ((ch & 0xe0) == 0xc0)
*/
next = *src++;
- if (!next)
+ if ((next & 0xc0) != 0x80)
+ {
+ DEBUG_puts("cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
+
return (-1);
+ }
ch32 = ((ch & 0x1f) << 6) | (next & 0x3f);
*/
if (ch32 < 0x80)
+ {
+ DEBUG_puts("cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
+
return (-1);
+ }
*dest++ = ch32;
+
+ DEBUG_printf(("cupsUTF8ToUTF32: %02x %02x => %08X\n",
+ src[-2], src[-1], (unsigned)ch32));
}
else if ((ch & 0xf0) == 0xe0)
{
*/
next = *src++;
- if (!next)
+ if ((next & 0xc0) != 0x80)
+ {
+ DEBUG_puts("cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
+
return (-1);
+ }
ch32 = ((ch & 0x0f) << 6) | (next & 0x3f);
next = *src++;
- if (!next)
+ if ((next & 0xc0) != 0x80)
+ {
+ DEBUG_puts("cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
+
return (-1);
+ }
ch32 = (ch32 << 6) | (next & 0x3f);
*/
if (ch32 < 0x800)
+ {
+ DEBUG_puts("cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
+
return (-1);
+ }
*dest++ = ch32;
+
+ DEBUG_printf(("cupsUTF8ToUTF32: %02x %02x %02x => %08X\n",
+ src[-3], src[-2], src[-1], (unsigned)ch32));
}
else if ((ch & 0xf8) == 0xf0)
{
*/
next = *src++;
- if (!next)
+ if ((next & 0xc0) != 0x80)
+ {
+ DEBUG_puts("cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
+
return (-1);
+ }
ch32 = ((ch & 0x07) << 6) | (next & 0x3f);
next = *src++;
- if (!next)
+ if ((next & 0xc0) != 0x80)
+ {
+ DEBUG_puts("cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
+
return (-1);
+ }
ch32 = (ch32 << 6) | (next & 0x3f);
next = *src++;
- if (!next)
+ if ((next & 0xc0) != 0x80)
+ {
+ DEBUG_puts("cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
+
return (-1);
+ }
ch32 = (ch32 << 6) | (next & 0x3f);
*/
if (ch32 < 0x10000)
+ {
+ DEBUG_puts("cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
+
return (-1);
+ }
*dest++ = ch32;
+
+ DEBUG_printf(("cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X\n",
+ src[-4], src[-3], src[-2], src[-1], (unsigned)ch32));
}
else
{
* More than 4-octet (invalid UTF-8 sequence)...
*/
+ DEBUG_puts("cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
+
return (-1);
}
*dest = 0;
- return (i);
+ DEBUG_printf(("cupsUTF8ToUTF32: Returning %d characters\n", maxout - 1 - i));
+
+ return (maxout - 1 - i);
}
* Check for valid arguments and clear output...
*/
+ DEBUG_printf(("cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)\n", dest, src,
+ maxout));
+
if (dest)
*dest = '\0';
if (!dest || !src || maxout < 1)
+ {
+ DEBUG_puts("cupsUTF32ToUTF8: Returning -1 (bad args)");
+
return (-1);
+ }
/*
* Check for leading BOM in UTF-32 and inverted BOM...
start = dest;
swap = *src == 0xfffe0000;
+ DEBUG_printf(("cupsUTF32ToUTF8: swap=%d\n", swap));
+
if (*src == 0xfffe0000 || *src == 0xfeff)
src ++;
*/
if (ch > 0x10ffff)
+ {
+ DEBUG_puts("cupsUTF32ToUTF8: Returning -1 (character out of range)");
+
return (-1);
+ }
/*
* Convert UTF-32 character to UTF-8 character(s)...
*dest++ = (cups_utf8_t)ch;
i --;
+
+ DEBUG_printf(("cupsUTF32ToUTF8: %08x => %02x\n", (unsigned)ch, dest[-1]));
}
else if (ch < 0x800)
{
*/
if (i < 2)
+ {
+ DEBUG_puts("cupsUTF32ToUTF8: Returning -1 (too long 2)");
+
return (-1);
+ }
*dest++ = (cups_utf8_t)(0xc0 | ((ch >> 6) & 0x1f));
*dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
i -= 2;
+
+ DEBUG_printf(("cupsUTF32ToUTF8: %08x => %02x %02x\n", (unsigned)ch,
+ dest[-2], dest[-1]));
}
else if (ch < 0x10000)
{
*/
if (i < 3)
+ {
+ DEBUG_puts("cupsUTF32ToUTF8: Returning -1 (too long 3)");
+
return (-1);
+ }
*dest++ = (cups_utf8_t)(0xe0 | ((ch >> 12) & 0x0f));
*dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
*dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
i -= 3;
+
+ DEBUG_printf(("cupsUTF32ToUTF8: %08x => %02x %02x %02x\n", (unsigned)ch,
+ dest[-3], dest[-2], dest[-1]));
}
else
{
*dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
*dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
i -= 4;
+
+ DEBUG_printf(("cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x\n",
+ (unsigned)ch, dest[-4], dest[-3], dest[-2], dest[-1]));
}
}
*dest = '\0';
+ DEBUG_printf(("cupsUTF32ToUTF8: Returning %d\n", (int)(dest - start)));
+
return ((int)(dest - start));
}
*workptr; /* Pointer into string */
+ DEBUG_printf(("conv_utf8_to_vbcs(dest=%p, src=\"%s\", maxout=%d, "
+ "encoding=%d)\n", dest, src, maxout, encoding));
+
/*
* Find legacy charset map in cache...
*/
if ((vmap = (_cups_vmap_t *)get_charmap(encoding)) == NULL)
+ {
+ DEBUG_puts("conv_utf8_to_vbcs: Returning -1 (no charmap)");
+
return (-1);
+ }
/*
* Convert input UTF-8 to internal UCS-4 (and insert BOM)...
*/
if (cupsUTF8ToUTF32(work, src, CUPS_MAX_USTRING) < 0)
+ {
+ DEBUG_puts("conv_utf8_to_vbcs: Returning -1 (Unable to convert to UTF-32)");
+
return (-1);
+ }
/*
* Convert internal UCS-4 to VBCS legacy charset (and delete BOM)...
for (start = dest, workptr = work + 1; *workptr && maxout > 1; maxout --)
{
unichar = *workptr++;
- if (!unichar)
- break;
/*
* Convert ASCII verbatim (optimization)...
if (unichar < 0x80)
{
*dest++ = (cups_sbcs_t)unichar;
+
+ DEBUG_printf(("conv_utf8_to_vbcs: %08x => %02X\n", (unsigned)unichar,
+ dest[-1]));
+
continue;
}
if (legchar > 0xffffff)
{
if (maxout < 5)
+ {
+ DEBUG_puts("conv_utf8_to_vbcs: Returning -1 (out of space)");
+
return (-1);
+ }
*dest++ = (cups_sbcs_t)(legchar >> 24);
*dest++ = (cups_sbcs_t)(legchar >> 16);
*dest++ = (cups_sbcs_t)legchar;
maxout -= 3;
+
+ DEBUG_printf(("conv_utf8_to_vbcs: %08x => %02X %02X %02X %02X\n",
+ (unsigned)unichar, dest[-4], dest[-3], dest[-2], dest[-1]));
}
else if (legchar > 0xffff)
{
if (maxout < 4)
+ {
+ DEBUG_puts("conv_utf8_to_vbcs: Returning -1 (out of space)");
+
return (-1);
+ }
*dest++ = (cups_sbcs_t)(legchar >> 16);
*dest++ = (cups_sbcs_t)(legchar >> 8);
*dest++ = (cups_sbcs_t)legchar;
maxout -= 2;
+
+ DEBUG_printf(("conv_utf8_to_vbcs: %08x => %02X %02X %02X\n",
+ (unsigned)unichar, dest[-3], dest[-2], dest[-1]));
}
else if (legchar > 0xff)
{
*dest++ = (cups_sbcs_t)legchar;
maxout --;
+
+ DEBUG_printf(("conv_utf8_to_vbcs: %08x => %02X %02X\n",
+ (unsigned)unichar, dest[-2], dest[-1]));
+ }
+ else
+ {
+ *dest++ = legchar;
+
+ DEBUG_printf(("conv_utf8_to_vbcs: %08x => %02X\n",
+ (unsigned)unichar, dest[-1]));
}
}
vmap->used --;
+ DEBUG_printf(("conv_utf8_to_vbcs: Returning %d characters\n",
+ (int)(dest - start)));
+
return ((int)(dest - start));
}
* Find legacy charset map in cache...
*/
+ DEBUG_printf(("conv_vbcs_to_utf8(dest=%p, src=%p, maxout=%d, encoding=%d)\n",
+ dest, src, maxout, encoding));
+
if ((vmap = (_cups_vmap_t *)get_charmap(encoding)) == NULL)
+ {
+ DEBUG_puts("conv_vbcs_to_utf8: Returning -1 (NULL vmap)");
+
return (-1);
+ }
/*
* Convert input legacy charset to internal UCS-4 (and insert BOM)...
if (legchar < 0x80)
{
*workptr++ = (cups_utf32_t)legchar;
+
+ DEBUG_printf(("conv_vbcs_to_utf8: %02X => %08X\n", src[-1],
+ (unsigned)legchar));
continue;
}
if (vmap->lead2char[(int)leadchar] == leadchar)
{
if (!*src)
+ {
+ DEBUG_puts("conv_vbcs_to_utf8: Returning -1 (short string)");
+
return (-1);
+ }
legchar = (legchar << 8) | *src++;
*workptr++ = 0xfffd;
else
*workptr++ = (cups_utf32_t)*crow;
+
+ DEBUG_printf(("conv_vbcs_to_utf8: %02X %02X => %08X\n",
+ src[-2], src[-1], (unsigned)workptr[-1]));
continue;
}
if (vmap->lead3char[(int)leadchar] == leadchar)
{
if (!*src || !src[1])
+ {
+ DEBUG_puts("conv_vbcs_to_utf8: Returning -1 (short string 2)");
+
return (-1);
+ }
legchar = (legchar << 8) | *src++;
legchar = (legchar << 8) | *src++;
else if (vmap->lead4char[(int)leadchar] == leadchar)
{
if (!*src || !src[1] || !src[2])
+ {
+ DEBUG_puts("conv_vbcs_to_utf8: Returning -1 (short string 3)");
+
return (-1);
+ }
legchar = (legchar << 8) | *src++;
legchar = (legchar << 8) | *src++;
legchar = (legchar << 8) | *src++;
}
else
+ {
+ DEBUG_puts("conv_vbcs_to_utf8: Returning -1 (bad character)");
+
return (-1);
+ }
/*
* Find 3-byte or 4-byte legacy character...
*workptr++ = 0xfffd;
else
*workptr++ = wide2uni->unichar;
+
+ if (vmap->lead3char[(int)leadchar] == leadchar)
+ DEBUG_printf(("conv_vbcs_to_utf8: %02X %02X %02X => %08X\n",
+ src[-3], src[-2], src[-1], (unsigned)workptr[-1]));
+ else
+ DEBUG_printf(("conv_vbcs_to_utf8: %02X %02X %02X %02X => %08X\n",
+ src[-4], src[-3], src[-2], src[-1], (unsigned)workptr[-1]));
}
*workptr = 0;
vmap->used --;
+ DEBUG_printf(("conv_vbcs_to_utf8: Converting %d UTF-32 characters to UTF-8\n",
+ (int)(workptr - work)));
+
/*
* Convert internal UCS-4 to output UTF-8 (and delete BOM)...
*/
_cups_globals_t *cg = _cupsGlobals(); /* Global data */
+ DEBUG_printf(("get_charmap(encoding=%d)\n", encoding));
+
/*
* Get the data directory and charset map name...
*/
snprintf(filename, sizeof(filename), "%s/charmaps/%s.txt",
cg->cups_datadir, _cupsEncodingName(encoding));
- DEBUG_printf((" filename=\"%s\"\n", filename));
+ DEBUG_printf(("get_charmap: filename=\"%s\"\n", filename));
/*
* Read charset map input file into cache...
* See if we already have this SBCS charset map loaded...
*/
+ DEBUG_printf(("get_sbcs_charmap(encoding=%d, filename=\"%s\")\n", encoding,
+ filename));
+
for (cmap = cmap_cache; cmap; cmap = cmap->next)
{
if (cmap->encoding == encoding)
{
cmap->used ++;
- DEBUG_printf((" returning existing cmap=%p\n", cmap));
+ DEBUG_printf(("get_sbcs_charmap: Returning existing cmap=%p\n", cmap));
return ((void *)cmap);
}
*/
if ((fp = cupsFileOpen(filename, "r")) == NULL)
+ {
+ DEBUG_printf(("get_sbcs_charmap: Returning NULL (%s)\n", strerror(errno)));
+
return (NULL);
+ }
/*
* Allocate memory for SBCS charset map...
if ((cmap = (_cups_cmap_t *)calloc(1, sizeof(_cups_cmap_t))) == NULL)
{
cupsFileClose(fp);
- DEBUG_puts(" Unable to allocate memory!");
+ DEBUG_puts("get_sbcs_charmap: Returning NULL (Unable to allocate memory)");
return (NULL);
}
cmap->next = cmap_cache;
cmap_cache = cmap;
- DEBUG_printf((" returning new cmap=%p\n", cmap));
+ DEBUG_printf(("get_sbcs_charmap: Returning new cmap=%p\n", cmap));
return (cmap);
cupsFileClose(fp);
- DEBUG_puts(" Error, returning NULL!");
+ DEBUG_puts("get_sbcs_charmap: Returning NULL (Read/format error)");
return (NULL);
}
if (vmap->encoding == encoding)
{
vmap->used ++;
- DEBUG_printf((" returning existing vmap=%p\n", vmap));
+ DEBUG_printf(("get_vbcs_charmap: Returning existing vmap=%p\n", vmap));
return ((void *)vmap);
}
if ((fp = cupsFileOpen(filename, "r")) == NULL)
{
- DEBUG_printf((" Unable to open file: %s\n", strerror(errno)));
+ DEBUG_printf(("get_vbcs_charmap: Returning NULL (%s)\n", strerror(errno)));
return (NULL);
}
if ((mapcount = get_charmap_count(fp)) <= 0)
{
- DEBUG_puts(" Unable to get charmap count!");
+ DEBUG_puts("get_vbcs_charmap: Unable to get charmap count!");
cupsFileClose(fp);
return (NULL);
}
- DEBUG_printf((" mapcount=%d\n", mapcount));
+ DEBUG_printf(("get_vbcs_charmap: mapcount=%d\n", mapcount));
/*
* Allocate memory for DBCS/VBCS charset map...
if ((vmap = (_cups_vmap_t *)calloc(1, sizeof(_cups_vmap_t))) == NULL)
{
- DEBUG_puts(" Unable to allocate memory!");
+ DEBUG_puts("get_vbcs_charmap: Unable to allocate memory!");
cupsFileClose(fp);
* Save lead char of 2/3/4-byte legacy char...
*/
- if (legchar > 0xff && legchar <= 0xffff)
+ if (legchar > 0xffffff)
{
- leadchar = (cups_sbcs_t)(legchar >> 8);
- vmap->lead2char[leadchar] = leadchar;
+ leadchar = (cups_sbcs_t)(legchar >> 24);
+ vmap->lead4char[leadchar] = leadchar;
}
-
- if (legchar > 0xffff && legchar <= 0xffffff)
+ else if (legchar > 0xffff)
{
leadchar = (cups_sbcs_t)(legchar >> 16);
vmap->lead3char[leadchar] = leadchar;
}
-
- if (legchar > 0xffffff)
+ else if (legchar > 0xff)
{
- leadchar = (cups_sbcs_t)(legchar >> 24);
- vmap->lead4char[leadchar] = leadchar;
+ leadchar = (cups_sbcs_t)(legchar >> 8);
+ vmap->lead2char[leadchar] = leadchar;
}
/*
* Add it to the cache and return...
*/
- vmap->next = vmap_cache;
+ vmap->next = vmap_cache;
vmap_cache = vmap;
- DEBUG_printf((" returning new vmap=%p\n", vmap));
+ DEBUG_printf(("get_vbcs_charmap: Returning new vmap=%p\n", vmap));
return (vmap);
cupsFileClose(fp);
- DEBUG_puts(" Error, returning NULL!");
+ DEBUG_puts("get_vbcs_charmap: Returning NULL (Read/format error)");
return (NULL);
}