1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/mtransport/third_party/nICEr/src/util/mbslen.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,146 @@ 1.4 +/* 1.5 +Copyright (c) 2007, Adobe Systems, Incorporated 1.6 +All rights reserved. 1.7 + 1.8 +Redistribution and use in source and binary forms, with or without 1.9 +modification, are permitted provided that the following conditions are 1.10 +met: 1.11 + 1.12 +* Redistributions of source code must retain the above copyright 1.13 + notice, this list of conditions and the following disclaimer. 1.14 + 1.15 +* Redistributions in binary form must reproduce the above copyright 1.16 + notice, this list of conditions and the following disclaimer in the 1.17 + documentation and/or other materials provided with the distribution. 1.18 + 1.19 +* Neither the name of Adobe Systems, Network Resonance nor the names of its 1.20 + contributors may be used to endorse or promote products derived from 1.21 + this software without specific prior written permission. 1.22 + 1.23 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.27 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.28 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.29 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.30 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.31 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.32 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.33 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 +*/ 1.35 + 1.36 + 1.37 +#ifdef LINUX 1.38 +#define _GNU_SOURCE 1 1.39 +#endif 1.40 +#include <string.h> 1.41 + 1.42 +#include <errno.h> 1.43 +#include <csi_platform.h> 1.44 + 1.45 +#include <assert.h> 1.46 +#include <locale.h> 1.47 +#include <stdlib.h> 1.48 +#include <wchar.h> 1.49 +#ifdef DARWIN 1.50 +#define HAVE_XLOCALE 1.51 +#endif 1.52 + 1.53 +#ifdef __FreeBSD__ 1.54 +#include <osreldate.h> 1.55 +# if __FreeBSD_version > 900505 1.56 +# define HAVE_XLOCALE 1.57 +# endif 1.58 +#endif 1.59 + 1.60 +#ifdef HAVE_XLOCALE 1.61 +#include <xlocale.h> 1.62 +#endif /* HAVE_XLOCALE */ 1.63 + 1.64 +#include "nr_api.h" 1.65 +#include "mbslen.h" 1.66 + 1.67 +/* get number of characters in a mult-byte character string */ 1.68 +int 1.69 +mbslen(const char *s, size_t *ncharsp) 1.70 +{ 1.71 +#ifdef HAVE_XLOCALE 1.72 + static locale_t loc = 0; 1.73 + static int initialized = 0; 1.74 +#endif /* HAVE_XLOCALE */ 1.75 +#ifdef WIN32 1.76 + char *my_locale=0; 1.77 + unsigned int i; 1.78 +#endif /* WIN32 */ 1.79 + int _status; 1.80 + size_t nbytes; 1.81 + int nchars; 1.82 + mbstate_t mbs; 1.83 + 1.84 +#ifdef HAVE_XLOCALE 1.85 + if (! initialized) { 1.86 + initialized = 1; 1.87 + loc = newlocale(LC_CTYPE_MASK, "UTF-8", LC_GLOBAL_LOCALE); 1.88 + } 1.89 + 1.90 + if (loc == 0) { 1.91 + /* unable to create the UTF-8 locale */ 1.92 + assert(loc != 0); /* should never happen */ 1.93 +#endif /* HAVE_XLOCALE */ 1.94 + 1.95 +#ifdef WIN32 1.96 + if (!setlocale(LC_CTYPE, 0)) 1.97 + ABORT(R_INTERNAL); 1.98 + 1.99 + if (!(my_locale = r_strdup(setlocale(LC_CTYPE, 0)))) 1.100 + ABORT(R_NO_MEMORY); 1.101 + 1.102 + for (i=0; i<strlen(my_locale); i++) 1.103 + my_locale[i] = toupper(my_locale[i]); 1.104 + 1.105 + if (!strstr(my_locale, "UTF-8") && !strstr(my_locale, "UTF8")) 1.106 + ABORT(R_NOT_FOUND); 1.107 +#else 1.108 + /* can't count UTF-8 characters with mbrlen if the locale isn't UTF-8 */ 1.109 + /* null-checking setlocale is required because Android */ 1.110 + char *locale = setlocale(LC_CTYPE, 0); 1.111 + /* some systems use "utf8" instead of "UTF-8" like Fedora 17 */ 1.112 + if (!locale || (!strcasestr(locale, "UTF-8") && !strcasestr(locale, "UTF8"))) 1.113 + ABORT(R_NOT_FOUND); 1.114 +#endif 1.115 + 1.116 +#ifdef HAVE_XLOCALE 1.117 + } 1.118 +#endif /* HAVE_XLOCALE */ 1.119 + 1.120 + memset(&mbs, 0, sizeof(mbs)); 1.121 + nchars = 0; 1.122 + 1.123 +#ifdef HAVE_XLOCALE 1.124 + while (*s != '\0' && (nbytes = mbrlen_l(s, strlen(s), &mbs, loc)) != 0) 1.125 +#else 1.126 + while (*s != '\0' && (nbytes = mbrlen(s, strlen(s), &mbs)) != 0) 1.127 +#endif /* HAVE_XLOCALE */ 1.128 + { 1.129 + if (nbytes == (size_t)-1) /* should never happen */ { 1.130 + ABORT(R_INTERNAL); 1.131 + } 1.132 + if (nbytes == (size_t)-2) /* encoding error */ { 1.133 + ABORT(R_BAD_DATA); 1.134 + } 1.135 + 1.136 + s += nbytes; 1.137 + ++nchars; 1.138 + } 1.139 + 1.140 + *ncharsp = nchars; 1.141 + 1.142 + _status = 0; 1.143 + abort: 1.144 +#ifdef WIN32 1.145 + RFREE(my_locale); 1.146 +#endif 1.147 + return _status; 1.148 +} 1.149 +