| 64 | */ |
| 65 | |
| 66 | int /* O - Count or -1 on error */ |
| 67 | cupsCharsetToUTF8( |
| 68 | cups_utf8_t *dest, /* O - Target string */ |
| 69 | const char *src, /* I - Source string */ |
| 70 | const int maxout, /* I - Max output */ |
| 71 | const cups_encoding_t encoding) /* I - Encoding */ |
| 72 | { |
| 73 | cups_utf8_t *destptr; /* Pointer into UTF-8 buffer */ |
| 74 | #ifdef HAVE_ICONV_H |
| 75 | size_t srclen, /* Length of source string */ |
| 76 | outBytesLeft; /* Bytes remaining in output buffer */ |
| 77 | #endif /* HAVE_ICONV_H */ |
| 78 | |
| 79 | |
| 80 | /* |
| 81 | * Check for valid arguments... |
| 82 | */ |
| 83 | |
| 84 | DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)", (void *)dest, src, maxout, encoding)); |
| 85 | |
| 86 | if (!dest || !src || maxout < 1) |
| 87 | { |
| 88 | if (dest) |
| 89 | *dest = '\0'; |
| 90 | |
| 91 | DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1"); |
| 92 | return (-1); |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Handle identity conversions... |
| 97 | */ |
| 98 | |
| 99 | if (encoding == CUPS_UTF8 || encoding <= CUPS_US_ASCII || |
| 100 | encoding >= CUPS_ENCODING_VBCS_END) |
| 101 | { |
| 102 | strlcpy((char *)dest, src, (size_t)maxout); |
| 103 | return ((int)strlen((char *)dest)); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Handle ISO-8859-1 to UTF-8 directly... |
| 108 | */ |
| 109 | |
| 110 | destptr = dest; |
| 111 | |
| 112 | if (encoding == CUPS_ISO8859_1) |
| 113 | { |
| 114 | int ch; /* Character from string */ |
| 115 | cups_utf8_t *destend; /* End of UTF-8 buffer */ |
| 116 | |
| 117 | |
| 118 | destend = dest + maxout - 2; |
| 119 | |
| 120 | while (*src && destptr < destend) |
| 121 | { |
| 122 | ch = *src++ & 255; |
| 123 | |