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.

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

mercurial