Windows: Partial fix for data race in composite_copy_transfer_data
[platform/upstream/libusb.git] / libusb / strerror.c
index 7fba0e4..9445fa9 100644 (file)
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
-#include "config.h"
 
-#include <locale.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "libusb.h"
 #include "libusbi.h"
 
-#if defined(_MSC_VER)
-#define strncasecmp _strnicmp
-#endif
-
-static size_t usbi_locale = 0;
+#include <ctype.h>
+#include <string.h>
 
-/** \ingroup misc
+/** \ingroup libusb_misc
  * How to add a new \ref libusb_strerror() translation:
  * <ol>
  * <li> Download the latest \c strerror.c from:<br>
- *      https://raw.github.com/libusb/libusb/master/libusb/sterror.c </li>
+ *      https://raw.github.com/libusb/libusb/master/libusb/strerror.c </li>
  * <li> Open the file in an UTF-8 capable editor </li>
  * <li> Add the 2 letter <a href="http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639-1</a>
  *      code for your locale at the end of \c usbi_locale_supported[]<br>
@@ -50,15 +41,15 @@ static size_t usbi_locale = 0;
  *         "Success",
  *         ...
  *         "Other error",
- *     }
+ *     },
  * };\endcode </li>
  * <li> Translate each of the English messages from the section you copied into your language </li>
  * <li> Save the file (in UTF-8 format) and send it to \c libusb-devel\@lists.sourceforge.net </li>
  * </ol>
  */
 
-static const char* usbi_locale_supported[] = { "en", "nl", "fr", "ru" };
-static const char* usbi_localized_errors[ARRAYSIZE(usbi_locale_supported)][LIBUSB_ERROR_COUNT] = {
+static const char * const usbi_locale_supported[] = { "en", "nl", "fr", "ru", "de", "hu" };
+static const char * const usbi_localized_errors[ARRAYSIZE(usbi_locale_supported)][LIBUSB_ERROR_COUNT] = {
        { /* English (en) */
                "Success",
                "Input/Output Error",
@@ -119,10 +110,42 @@ static const char* usbi_localized_errors[ARRAYSIZE(usbi_locale_supported)][LIBUS
                "Память исчерпана",
                "Операция не поддерживается данной платформой",
                "Неизвестная ошибка"
-       }
+       }, { /* German (de) */
+               "Erfolgreich",
+               "Eingabe-/Ausgabefehler",
+               "Ungültiger Parameter",
+               "Keine Berechtigung (Zugriffsrechte fehlen)",
+               "Kein passendes Gerät gefunden (es könnte entfernt worden sein)",
+               "Entität nicht gefunden",
+               "Die Ressource ist belegt",
+               "Die Wartezeit für die Operation ist abgelaufen",
+               "Mehr Daten empfangen als erwartet",
+               "Datenübergabe unterbrochen (broken pipe)",
+               "Unterbrechung während des Betriebssystemaufrufs",
+               "Nicht genügend Hauptspeicher verfügbar",
+               "Die Operation wird nicht unterstützt oder ist auf dieser Platform nicht implementiert",
+               "Allgemeiner Fehler",
+       }, { /* Hungarian (hu) */
+               "Sikeres",
+               "Be-/kimeneti hiba",
+               "Érvénytelen paraméter",
+               "Hozzáférés megtagadva",
+               "Az eszköz nem található (eltávolították?)",
+               "Nem található",
+               "Az erőforrás foglalt",
+               "Időtúllépés",
+               "Túlcsordulás",
+               "Törött adatcsatorna",
+               "Rendszerhívás megszakítva",
+               "Nincs elég memória",
+               "A művelet nem támogatott ezen a rendszeren",
+               "Általános hiba",
+       },
 };
 
-/** \ingroup misc
+static const char * const (*usbi_error_strings)[LIBUSB_ERROR_COUNT] = &usbi_localized_errors[0];
+
+/** \ingroup libusb_misc
  * Set the language, and only the language, not the encoding! used for
  * translatable libusb messages.
  *
@@ -131,7 +154,7 @@ static const char* usbi_localized_errors[ARRAYSIZE(usbi_locale_supported)][LIBUS
  * used, and only 2 letter ISO 639-1 codes are accepted for it, such as "de".
  * The optional region, country_region or codeset parts are ignored. This
  * means that functions which return translatable strings will NOT honor the
- * specified encoding. 
+ * specified encoding.
  * All strings returned are encoded as UTF-8 strings.
  *
  * If libusb_setlocale() is not called, all messages will be in English.
@@ -156,24 +179,25 @@ int API_EXPORTED libusb_setlocale(const char *locale)
 {
        size_t i;
 
-       if ( (locale == NULL) || (strlen(locale) < 2)
-         || ((strlen(locale) > 2) && (locale[2] != '-') && (locale[2] != '_') && (locale[2] != '.')) )
+       if (!locale || strlen(locale) < 2
+           || (locale[2] != '\0' && locale[2] != '-' && locale[2] != '_' && locale[2] != '.'))
                return LIBUSB_ERROR_INVALID_PARAM;
 
-       for (i=0; i<ARRAYSIZE(usbi_locale_supported); i++) {
-               if (strncasecmp(usbi_locale_supported[i], locale, 2) == 0)
+       for (i = 0; i < ARRAYSIZE(usbi_locale_supported); i++) {
+               if (usbi_locale_supported[i][0] == tolower((unsigned char)locale[0])
+                   && usbi_locale_supported[i][1] == tolower((unsigned char)locale[1]))
                        break;
        }
-       if (i >= ARRAYSIZE(usbi_locale_supported)) {
+
+       if (i == ARRAYSIZE(usbi_locale_supported))
                return LIBUSB_ERROR_NOT_FOUND;
-       }
 
-       usbi_locale = i;
+       usbi_error_strings = &usbi_localized_errors[i];
 
        return LIBUSB_SUCCESS;
 }
 
-/** \ingroup misc
+/** \ingroup libusb_misc
  * Returns a constant string with a short description of the given error code,
  * this description is intended for displaying to the end user and will be in
  * the language set by libusb_setlocale().
@@ -186,14 +210,14 @@ int API_EXPORTED libusb_setlocale(const char *locale)
  * \param errcode the error code whose description is desired
  * \returns a short description of the error code in UTF-8 encoding
  */
-DEFAULT_VISIBILITY const char* LIBUSB_CALL libusb_strerror(enum libusb_error errcode)
+DEFAULT_VISIBILITY const char * LIBUSB_CALL libusb_strerror(int errcode)
 {
        int errcode_index = -errcode;
 
-       if ((errcode_index < 0) || (errcode_index >= LIBUSB_ERROR_COUNT)) {
+       if (errcode_index < 0 || errcode_index >= LIBUSB_ERROR_COUNT) {
                /* "Other Error", which should always be our last message, is returned */
                errcode_index = LIBUSB_ERROR_COUNT - 1;
        }
 
-       return usbi_localized_errors[usbi_locale][errcode_index];
+       return (*usbi_error_strings)[errcode_index];
 }