]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/transcode.c
Return kDNSServiceErr_ServiceNotRunning when Bonjour for Windows not installed.
[thirdparty/cups.git] / cups / transcode.c
index 0e33744ad254814eedf2fd0af371dd69e007043e..4267813192d76adac64d831bd629084368da9c6d 100644 (file)
@@ -1,26 +1,10 @@
 /*
- * "$Id: transcode.c 9306 2010-09-16 21:43:57Z mike $"
+ * Transcoding support for CUPS.
  *
- *   Transcoding support for CUPS.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products.
  *
- *   Copyright 2007-2010 by Apple Inc.
- *   Copyright 1997-2007 by Easy Software Products.
- *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   _cupsCharmapFlush() - Flush all character set maps out of cache.
- *   cupsCharsetToUTF8() - Convert legacy character set to UTF-8.
- *   cupsUTF8ToCharset() - Convert UTF-8 to legacy character set.
- *   cupsUTF8ToUTF32()   - Convert UTF-8 to UTF-32.
- *   cupsUTF32ToUTF8()   - Convert UTF-32 to UTF-8.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
@@ -28,6 +12,7 @@
  */
 
 #include "cups-private.h"
+#include "debug-internal.h"
 #include <limits.h>
 #include <time.h>
 #ifdef HAVE_ICONV_H
@@ -98,8 +83,7 @@ cupsCharsetToUTF8(
   * Check for valid arguments...
   */
 
-  DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)",
-               dest, src, maxout, encoding));
+  DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)", (void *)dest, src, maxout, encoding));
 
   if (!dest || !src || maxout < 1)
   {
@@ -117,7 +101,7 @@ cupsCharsetToUTF8(
   if (encoding == CUPS_UTF8 || encoding <= CUPS_US_ASCII ||
       encoding >= CUPS_ENCODING_VBCS_END)
   {
-    strlcpy((char *)dest, src, maxout);
+    strlcpy((char *)dest, src, (size_t)maxout);
     return ((int)strlen((char *)dest));
   }
 
@@ -141,11 +125,11 @@ cupsCharsetToUTF8(
 
       if (ch & 128)
       {
-       *destptr++ = 0xc0 | (ch >> 6);
-       *destptr++ = 0x80 | (ch & 0x3f);
+       *destptr++ = (cups_utf8_t)(0xc0 | (ch >> 6));
+       *destptr++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
       }
       else
-       *destptr++ = ch;
+       *destptr++ = (cups_utf8_t)ch;
     }
 
     *destptr = '\0';
@@ -162,25 +146,30 @@ cupsCharsetToUTF8(
 
   if (map_encoding != encoding)
   {
+    char       toset[1024];            /* Destination character set */
+
     _cupsCharmapFlush();
 
+    snprintf(toset, sizeof(toset), "%s//IGNORE", _cupsEncodingName(encoding));
+
+    map_encoding  = encoding;
     map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8");
-    map_to_utf8   = iconv_open("UTF-8", _cupsEncodingName(encoding));
-    map_encoding     = encoding;
+    map_to_utf8   = iconv_open("UTF-8", toset);
   }
 
   if (map_to_utf8 != (iconv_t)-1)
   {
+    char *altdestptr = (char *)dest;   /* Silence bogus GCC type-punned */
+
     srclen       = strlen(src);
-    outBytesLeft = maxout - 1;
+    outBytesLeft = (size_t)maxout - 1;
 
-    iconv(map_to_utf8, (char **)&src, &srclen, (char **)&destptr,
-         &outBytesLeft);
-    *destptr = '\0';
+    iconv(map_to_utf8, (char **)&src, &srclen, &altdestptr, &outBytesLeft);
+    *altdestptr = '\0';
 
     _cupsMutexUnlock(&map_mutex);
 
-    return ((int)(destptr - dest));
+    return ((int)(altdestptr - (char *)dest));
   }
 
   _cupsMutexUnlock(&map_mutex);
@@ -230,10 +219,10 @@ cupsUTF8ToCharset(
   * Handle identity conversions...
   */
 
-  if (encoding == CUPS_UTF8 || encoding <= CUPS_US_ASCII ||
+  if (encoding == CUPS_UTF8 ||
       encoding >= CUPS_ENCODING_VBCS_END)
   {
-    strlcpy(dest, (char *)src, maxout);
+    strlcpy(dest, (char *)src, (size_t)maxout);
     return ((int)strlen(dest));
   }
 
@@ -243,12 +232,13 @@ cupsUTF8ToCharset(
 
   destptr = dest;
 
-  if (encoding == CUPS_ISO8859_1)
+  if (encoding == CUPS_ISO8859_1 || encoding <= CUPS_US_ASCII)
   {
-    int                ch;                     /* Character from string */
+    int                ch,                     /* Character from string */
+               maxch;                  /* Maximum character for charset */
     char       *destend;               /* End of ISO-8859-1 buffer */
 
-
+    maxch   = encoding == CUPS_ISO8859_1 ? 256 : 128;
     destend = dest + maxout - 1;
 
     while (*src && destptr < destend)
@@ -259,8 +249,8 @@ cupsUTF8ToCharset(
       {
        ch = ((ch & 0x1f) << 6) | (*src++ & 0x3f);
 
-       if (ch < 256)
-          *destptr++ = ch;
+       if (ch < maxch)
+          *destptr++ = (char)ch;
        else
           *destptr++ = '?';
       }
@@ -268,7 +258,7 @@ cupsUTF8ToCharset(
                (ch & 0xf8) == 0xf0)
         *destptr++ = '?';
       else if (!(ch & 0x80))
-       *destptr++ = ch;
+       *destptr++ = (char)ch;
     }
 
     *destptr = '\0';
@@ -285,19 +275,25 @@ cupsUTF8ToCharset(
 
   if (map_encoding != encoding)
   {
+    char       toset[1024];            /* Destination character set */
+
     _cupsCharmapFlush();
 
-    map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8");
-    map_to_utf8   = iconv_open("UTF-8", _cupsEncodingName(encoding));
+    snprintf(toset, sizeof(toset), "%s//IGNORE", _cupsEncodingName(encoding));
+
     map_encoding  = encoding;
+    map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8");
+    map_to_utf8   = iconv_open("UTF-8", toset);
   }
 
   if (map_from_utf8 != (iconv_t)-1)
   {
+    char *altsrc = (char *)src;                /* Silence bogus GCC type-punned */
+
     srclen       = strlen((char *)src);
-    outBytesLeft = maxout - 1;
+    outBytesLeft = (size_t)maxout - 1;
 
-    iconv(map_from_utf8, (char **)&src, &srclen, &destptr, &outBytesLeft);
+    iconv(map_from_utf8, &altsrc, &srclen, &destptr, &outBytesLeft);
     *destptr = '\0';
 
     _cupsMutexUnlock(&map_mutex);
@@ -350,8 +346,7 @@ cupsUTF8ToUTF32(
   * Check for valid arguments and clear output...
   */
 
-  DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest,
-                src, maxout));
+  DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", (void *)dest, src, maxout));
 
   if (dest)
     *dest = 0;
@@ -400,7 +395,7 @@ cupsUTF8ToUTF32(
        return (-1);
       }
 
-      ch32 = ((ch & 0x1f) << 6) | (next & 0x3f);
+      ch32 = (cups_utf32_t)((ch & 0x1f) << 6) | (cups_utf32_t)(next & 0x3f);
 
      /*
       * Check for non-shortest form (invalid UTF-8)...
@@ -432,7 +427,7 @@ cupsUTF8ToUTF32(
        return (-1);
       }
 
-      ch32 = ((ch & 0x0f) << 6) | (next & 0x3f);
+      ch32 = (cups_utf32_t)((ch & 0x0f) << 6) | (cups_utf32_t)(next & 0x3f);
 
       next = *src++;
       if ((next & 0xc0) != 0x80)
@@ -442,7 +437,7 @@ cupsUTF8ToUTF32(
        return (-1);
       }
 
-      ch32 = (ch32 << 6) | (next & 0x3f);
+      ch32 = (ch32 << 6) | (cups_utf32_t)(next & 0x3f);
 
      /*
       * Check for non-shortest form (invalid UTF-8)...
@@ -474,7 +469,7 @@ cupsUTF8ToUTF32(
        return (-1);
       }
 
-      ch32 = ((ch & 0x07) << 6) | (next & 0x3f);
+      ch32 = (cups_utf32_t)((ch & 0x07) << 6) | (cups_utf32_t)(next & 0x3f);
 
       next = *src++;
       if ((next & 0xc0) != 0x80)
@@ -484,7 +479,7 @@ cupsUTF8ToUTF32(
        return (-1);
       }
 
-      ch32 = (ch32 << 6) | (next & 0x3f);
+      ch32 = (ch32 << 6) | (cups_utf32_t)(next & 0x3f);
 
       next = *src++;
       if ((next & 0xc0) != 0x80)
@@ -494,7 +489,7 @@ cupsUTF8ToUTF32(
        return (-1);
       }
 
-      ch32 = (ch32 << 6) | (next & 0x3f);
+      ch32 = (ch32 << 6) | (cups_utf32_t)(next & 0x3f);
 
      /*
       * Check for non-shortest form (invalid UTF-8)...
@@ -571,8 +566,7 @@ cupsUTF32ToUTF8(
   * Check for valid arguments and clear output...
   */
 
-  DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest, src,
-                maxout));
+  DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", (void *)dest, (void *)src, maxout));
 
   if (dest)
     *dest = '\0';
@@ -709,8 +703,3 @@ cupsUTF32ToUTF8(
 
   return ((int)(dest - start));
 }
-
-
-/*
- * End of "$Id: transcode.c 9306 2010-09-16 21:43:57Z mike $"
- */