media/mtransport/third_party/nICEr/src/util/mbslen.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /*
     2 Copyright (c) 2007, Adobe Systems, Incorporated
     3 All rights reserved.
     5 Redistribution and use in source and binary forms, with or without
     6 modification, are permitted provided that the following conditions are
     7 met:
     9 * Redistributions of source code must retain the above copyright
    10   notice, this list of conditions and the following disclaimer.
    12 * Redistributions in binary form must reproduce the above copyright
    13   notice, this list of conditions and the following disclaimer in the
    14   documentation and/or other materials provided with the distribution.
    16 * Neither the name of Adobe Systems, Network Resonance nor the names of its
    17   contributors may be used to endorse or promote products derived from
    18   this software without specific prior written permission.
    20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31 */
    34 #ifdef LINUX
    35 #define _GNU_SOURCE 1
    36 #endif
    37 #include <string.h>
    39 #include <errno.h>
    40 #include <csi_platform.h>
    42 #include <assert.h>
    43 #include <locale.h>
    44 #include <stdlib.h>
    45 #include <wchar.h>
    46 #ifdef DARWIN
    47 #define HAVE_XLOCALE
    48 #endif
    50 #ifdef __FreeBSD__
    51 #include <osreldate.h>
    52 # if __FreeBSD_version > 900505
    53 #  define HAVE_XLOCALE
    54 # endif
    55 #endif
    57 #ifdef HAVE_XLOCALE
    58 #include <xlocale.h>
    59 #endif /* HAVE_XLOCALE */
    61 #include "nr_api.h"
    62 #include "mbslen.h"
    64 /* get number of characters in a mult-byte character string */
    65 int
    66 mbslen(const char *s, size_t *ncharsp)
    67 {
    68 #ifdef HAVE_XLOCALE
    69     static locale_t loc = 0;
    70     static int initialized = 0;
    71 #endif /* HAVE_XLOCALE */
    72 #ifdef WIN32
    73     char *my_locale=0;
    74     unsigned int i;
    75 #endif  /* WIN32 */
    76     int _status;
    77     size_t nbytes;
    78     int nchars;
    79     mbstate_t mbs;
    81 #ifdef HAVE_XLOCALE
    82     if (! initialized) {
    83         initialized = 1;
    84         loc = newlocale(LC_CTYPE_MASK, "UTF-8", LC_GLOBAL_LOCALE);
    85     }
    87     if (loc == 0) {
    88         /* unable to create the UTF-8 locale */
    89         assert(loc != 0);  /* should never happen */
    90 #endif /* HAVE_XLOCALE */
    92 #ifdef WIN32
    93     if (!setlocale(LC_CTYPE, 0))
    94         ABORT(R_INTERNAL);
    96     if (!(my_locale = r_strdup(setlocale(LC_CTYPE, 0))))
    97         ABORT(R_NO_MEMORY);
    99     for (i=0; i<strlen(my_locale); i++)
   100         my_locale[i] = toupper(my_locale[i]);
   102     if (!strstr(my_locale, "UTF-8") && !strstr(my_locale, "UTF8"))
   103         ABORT(R_NOT_FOUND);
   104 #else
   105     /* can't count UTF-8 characters with mbrlen if the locale isn't UTF-8 */
   106     /* null-checking setlocale is required because Android */
   107     char *locale = setlocale(LC_CTYPE, 0);
   108     /* some systems use "utf8" instead of "UTF-8" like Fedora 17 */
   109     if (!locale || (!strcasestr(locale, "UTF-8") && !strcasestr(locale, "UTF8")))
   110         ABORT(R_NOT_FOUND);
   111 #endif
   113 #ifdef HAVE_XLOCALE
   114     }
   115 #endif /* HAVE_XLOCALE */
   117     memset(&mbs, 0, sizeof(mbs));
   118     nchars = 0;
   120 #ifdef HAVE_XLOCALE
   121     while (*s != '\0' && (nbytes = mbrlen_l(s, strlen(s), &mbs, loc)) != 0)
   122 #else
   123     while (*s != '\0' && (nbytes = mbrlen(s, strlen(s), &mbs)) != 0)
   124 #endif /* HAVE_XLOCALE */
   125     {
   126         if (nbytes == (size_t)-1)   /* should never happen */ {
   127             ABORT(R_INTERNAL);
   128         }
   129         if (nbytes == (size_t)-2)   /* encoding error */ {
   130             ABORT(R_BAD_DATA);
   131         }
   133         s += nbytes;
   134         ++nchars;
   135     }
   137     *ncharsp = nchars;
   139     _status = 0;
   140   abort:
   141 #ifdef WIN32
   142     RFREE(my_locale);
   143 #endif
   144     return _status;
   145 }

mercurial