]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/language.c
Final sync-up with cups.org trunk repository
[thirdparty/cups.git] / cups / language.c
index 2e91ad9d4a16535bdccd7122befb570442f56b60..accdac76832c56b38e06a6d8f4ec04cf047a6506 100644 (file)
@@ -3,7 +3,7 @@
  *
  *   I18N/language support for CUPS.
  *
- *   Copyright 2007-2011 by Apple Inc.
+ *   Copyright 2007-2012 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
  * Contents:
  *
  *   _cupsAppleLanguage()   - Get the Apple language identifier associated with
- *                            a locale ID.
+ *                           a locale ID.
  *   _cupsEncodingName()    - Return the character encoding name string for the
- *                            given encoding enumeration.
- *   cupsLangDefault()      - Return the default language.
- *   cupsLangEncoding()     - Return the character encoding (us-ascii, etc.) for
- *                            the given language.
- *   cupsLangFlush()        - Flush all language data out of the cache.
- *   cupsLangFree()         - Free language data.
- *   cupsLangGet()          - Get a language.
- *   _cupsLangString()      - Get a message string.
+ *                           given encoding enumeration.
+ *   cupsLangDefault()     - Return the default language.
+ *   cupsLangEncoding()     - Return the character encoding (us-ascii, etc.)
+ *                           for the given language.
+ *   cupsLangFlush()       - Flush all language data out of the cache.
+ *   cupsLangFree()        - Free language data.
+ *   cupsLangGet()         - Get a language.
+ *   _cupsLangString()     - Get a message string.
  *   _cupsMessageFree()     - Free a messages array.
  *   _cupsMessageLoad()     - Load a .po file into a messages array.
  *   _cupsMessageLookup()   - Lookup a message string.
+ *   _cupsMessageNew()     - Make a new message catalog array.
  *   appleLangDefault()     - Get the default locale string.
  *   appleMessageLoad()     - Load a message catalog from a localizable bundle.
  *   cups_cache_lookup()    - Lookup a language in the cache...
  *   cups_message_compare() - Compare two messages.
  *   cups_message_free()    - Free a message.
  *   cups_message_load()    - Load the message catalog for a language.
- *   cups_unquote()         - Unquote characters in strings...
+ *   cups_unquote()        - Unquote characters in strings...
  */
 
 /*
@@ -445,7 +446,7 @@ cupsLangGet(const char *language)   /* I - Language or locale */
   * Set the character set to UTF-8...
   */
 
-  strcpy(charset, "UTF8");
+  strlcpy(charset, "UTF8", sizeof(charset));
 
  /*
   * Apple's setlocale doesn't give us the user's localization
@@ -581,7 +582,7 @@ cupsLangGet(const char *language)   /* I - Language or locale */
   */
 
   if (!charset[0])
-    strcpy(charset, "UTF8");
+    strlcpy(charset, "UTF8", sizeof(charset));
 
  /*
   * Parse the language string passed in to a locale string. "C" is the
@@ -596,7 +597,7 @@ cupsLangGet(const char *language)   /* I - Language or locale */
 
   if (language == NULL || !language[0] ||
       !strcmp(language, "POSIX"))
-    strcpy(langname, "C");
+    strlcpy(langname, "C", sizeof(langname));
   else
   {
    /*
@@ -645,7 +646,7 @@ cupsLangGet(const char *language)   /* I - Language or locale */
 
     if (strlen(langname) != 2)
     {
-      strcpy(langname, "C");
+      strlcpy(langname, "C", sizeof(langname));
       country[0] = '\0';
       charset[0] = '\0';
     }
@@ -700,7 +701,7 @@ cupsLangGet(const char *language)   /* I - Language or locale */
   if (country[0])
     snprintf(real, sizeof(real), "%s_%s", langname, country);
   else
-    strcpy(real, langname);
+    strlcpy(real, langname, sizeof(real));
 
   _cupsMutexLock(&lang_mutex);
 
@@ -787,7 +788,7 @@ _cupsLangString(cups_lang_t *lang,  /* I - Language */
   * Range check input...
   */
 
-  if (!lang || !message)
+  if (!lang || !message || !*message)
     return (message);
 
   _cupsMutexLock(&lang_mutex);
@@ -846,6 +847,7 @@ _cupsMessageLoad(const char *filename,      /* I - Message catalog to load */
                        *ptr,           /* Pointer into buffer */
                        *temp;          /* New string */
   int                  length;         /* Length of combined strings */
+  size_t               ptrlen;         /* Length of string */
 
 
   DEBUG_printf(("4_cupsMessageLoad(filename=\"%s\")", filename));
@@ -854,10 +856,7 @@ _cupsMessageLoad(const char *filename,     /* I - Message catalog to load */
   * Create an array to hold the messages...
   */
 
-  if ((a = cupsArrayNew3((cups_array_func_t)cups_message_compare, NULL,
-                         (cups_ahash_func_t)NULL, 0,
-                        (cups_acopy_func_t)NULL,
-                        (cups_afree_func_t)cups_message_free)) == NULL)
+  if ((a = _cupsMessageNew(NULL)) == NULL)
   {
     DEBUG_puts("5_cupsMessageLoad: Unable to allocate array!");
     return (NULL);
@@ -938,7 +937,23 @@ _cupsMessageLoad(const char *filename,     /* I - Message catalog to load */
       */
 
       if (m)
-        cupsArrayAdd(a, m);
+      {
+        if (m->str && m->str[0])
+        {
+          cupsArrayAdd(a, m);
+        }
+        else
+        {
+         /*
+          * Translation is empty, don't add it... (STR #4033)
+          */
+
+          free(m->id);
+          if (m->str)
+            free(m->str);
+          free(m);
+        }
+      }
 
      /*
       * Create a new message with the given msgid string...
@@ -964,10 +979,16 @@ _cupsMessageLoad(const char *filename,    /* I - Message catalog to load */
       */
 
       length = (int)strlen(m->str ? m->str : m->id);
+      ptrlen = strlen(ptr);
 
       if ((temp = realloc(m->str ? m->str : m->id,
-                          length + strlen(ptr) + 1)) == NULL)
+                          length + ptrlen + 1)) == NULL)
       {
+        if (m->str)
+         free(m->str);
+       free(m->id);
+        free(m);
+
        cupsFileClose(fp);
        return (a);
       }
@@ -976,25 +997,25 @@ _cupsMessageLoad(const char *filename,    /* I - Message catalog to load */
       {
        /*
         * Copy the new portion to the end of the msgstr string - safe
-       * to use strcpy because the buffer is allocated to the correct
+       * to use memcpy because the buffer is allocated to the correct
        * size...
        */
 
         m->str = temp;
 
-       strcpy(m->str + length, ptr);
+       memcpy(m->str + length, ptr, ptrlen + 1);
       }
       else
       {
        /*
         * Copy the new portion to the end of the msgid string - safe
-       * to use strcpy because the buffer is allocated to the correct
+       * to use memcpy because the buffer is allocated to the correct
        * size...
        */
 
         m->id = temp;
 
-       strcpy(m->id + length, ptr);
+       memcpy(m->id + length, ptr, ptrlen + 1);
       }
     }
     else if (!strncmp(s, "msgstr", 6) && m)
@@ -1005,6 +1026,9 @@ _cupsMessageLoad(const char *filename,    /* I - Message catalog to load */
 
       if ((m->str = strdup(ptr)) == NULL)
       {
+       free(m->id);
+        free(m);
+
         cupsFileClose(fp);
        return (a);
       }
@@ -1016,7 +1040,23 @@ _cupsMessageLoad(const char *filename,   /* I - Message catalog to load */
   */
 
   if (m)
-    cupsArrayAdd(a, m);
+  {
+    if (m->str && m->str[0])
+    {
+      cupsArrayAdd(a, m);
+    }
+    else
+    {
+     /*
+      * Translation is empty, don't add it... (STR #4033)
+      */
+
+      free(m->id);
+      if (m->str)
+       free(m->str);
+      free(m);
+    }
+  }
 
  /*
   * Close the message catalog file and return the new array...
@@ -1100,6 +1140,20 @@ _cupsMessageLookup(cups_array_t *a,      /* I - Message array */
 }
 
 
+/*
+ * '_cupsMessageNew()' - Make a new message catalog array.
+ */
+
+cups_array_t *                         /* O - Array */
+_cupsMessageNew(void *context)         /* I - User data */
+{
+  return (cupsArrayNew3((cups_array_func_t)cups_message_compare, context,
+                        (cups_ahash_func_t)NULL, 0,
+                       (cups_acopy_func_t)NULL,
+                       (cups_afree_func_t)cups_message_free));
+}
+
+
 #ifdef __APPLE__
 /*
  * 'appleLangDefault()' - Get the default locale string.
@@ -1130,24 +1184,40 @@ appleLangDefault(void)
   {
     if (getenv("SOFTWARE") != NULL && (lang = getenv("LANG")) != NULL)
     {
+      DEBUG_printf(("3appleLangDefault: Using LANG=%s", lang));
       strlcpy(cg->language, lang, sizeof(cg->language));
       return (cg->language);
     }
     else if ((bundle = CFBundleGetMainBundle()) != NULL &&
              (bundleList = CFBundleCopyBundleLocalizations(bundle)) != NULL)
     {
+      DEBUG_puts("3appleLangDefault: Getting localizationList from bundle.");
+
       localizationList =
          CFBundleCopyPreferredLocalizationsFromArray(bundleList);
 
       CFRelease(bundleList);
     }
     else
+    {
+      DEBUG_puts("3appleLangDefault: Getting localizationList from preferences.");
+
       localizationList =
          CFPreferencesCopyAppValue(CFSTR("AppleLanguages"),
                                    kCFPreferencesCurrentApplication);
+    }
 
     if (localizationList)
     {
+
+#ifdef DEBUG
+      if (CFGetTypeID(localizationList) == CFArrayGetTypeID())
+        DEBUG_printf(("3appleLangDefault: Got localizationList, %d entries.",
+                      (int)CFArrayGetCount(localizationList)));
+      else
+        DEBUG_puts("3appleLangDefault: Got localizationList but not an array.");
+#endif /* DEBUG */
+
       if (CFGetTypeID(localizationList) == CFArrayGetTypeID() &&
          CFArrayGetCount(localizationList) > 0)
       {
@@ -1165,7 +1235,7 @@ appleLangDefault(void)
                               kCFStringEncodingASCII);
            CFRelease(localeName);
 
-           DEBUG_printf(("9appleLangDefault: cg->language=\"%s\"",
+           DEBUG_printf(("3appleLangDefault: cg->language=\"%s\"",
                          cg->language));
 
           /*
@@ -1179,7 +1249,7 @@ appleLangDefault(void)
            {
              if (!strcmp(cg->language, apple_language_locale[i].language))
              {
-               DEBUG_printf(("9appleLangDefault: mapping \"%s\" to \"%s\"...",
+               DEBUG_printf(("3appleLangDefault: mapping \"%s\" to \"%s\"...",
                              cg->language, apple_language_locale[i].locale));
                strlcpy(cg->language, apple_language_locale[i].locale,
                        sizeof(cg->language));
@@ -1197,6 +1267,8 @@ appleLangDefault(void)
            if (!strchr(cg->language, '.'))
              strlcat(cg->language, ".UTF-8", sizeof(cg->language));
          }
+         else
+           DEBUG_puts("3appleLangDefault: Unable to get localeName.");
        }
       }
 
@@ -1208,7 +1280,10 @@ appleLangDefault(void)
     */
 
     if (!cg->language[0])
+    {
+      DEBUG_puts("3appleLangDefault: Defaulting to en_US.");
       strlcpy(cg->language, "en_US.UTF-8", sizeof(cg->language));
+    }
   }
 
  /*
@@ -1334,10 +1409,7 @@ appleMessageLoad(const char *locale)     /* I - Locale ID */
   * plist as the user data.
   */
 
-  return (cupsArrayNew3((cups_array_func_t)cups_message_compare, (void *)plist,
-                        (cups_ahash_func_t)NULL, 0,
-                       (cups_acopy_func_t)NULL,
-                       (cups_afree_func_t)cups_message_free));
+  return (_cupsMessageNew((void *)plist));
 }
 #  endif /* CUPS_BUNDLEDIR */
 #endif /* __APPLE__ */