FreeSWITCH API Documentation  1.7.0
switch_utf8.c
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Seven Du <dujinfang@gmail.com>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  * Anthony Minessale II <anthm@freeswitch.org>
26  *
27  *
28  * switch_utf8.c UTf8
29  *
30  */
31 
32 #include "switch_utf8.h"
33 
34 /*
35  Basic UTF-8 manipulation routines
36  by Jeff Bezanson
37  placed in the public domain Fall 2005
38 
39  This code is designed to provide the utilities you need to manipulate
40  UTF-8 as an internal string encoding. These functions do not perform the
41  error checking normally needed when handling UTF-8 data, so if you happen
42  to be from the Unicode Consortium you will want to flay me alive.
43  I do this because error checking can be performed at the boundaries (I/O),
44  with these routines reserved for higher performance on data known to be
45  valid.
46 */
47 
48 static const uint32_t offsetsFromUTF8[6] = {
49  0x00000000UL, 0x00003080UL, 0x000E2080UL,
50  0x03C82080UL, 0xFA082080UL, 0x82082080UL
51 };
52 
53 static const char trailingBytesForUTF8[256] = {
54  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
55  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
56  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
57  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
58  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
59  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
60  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
61  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
62 };
63 
64 /* returns length of next utf-8 sequence */
66 {
67  return trailingBytesForUTF8[(unsigned int)(unsigned char)s[0]] + 1;
68 }
69 
70 /* conversions without error checking
71  only works for valid UTF-8, i.e. no 5- or 6-byte sequences
72  srcsz = source size in bytes, or -1 if 0-terminated
73  sz = dest size in # of wide characters
74 
75  returns # characters converted
76  dest will always be L'\0'-terminated, even if there isn't enough room
77  for all the characters.
78  if sz = srcsz+1 (i.e. 4*srcsz+4 bytes), there will always be enough space.
79  */
80 SWITCH_DECLARE(int) switch_u8_toucs(uint32_t *dest, int sz, char *src, int srcsz)
81 {
82  uint32_t ch;
83  char *src_end = src + srcsz;
84  int nb;
85  int i=0;
86 
87  while (i < sz-1) {
88  nb = trailingBytesForUTF8[(unsigned char)*src];
89  if (srcsz == -1) {
90  if (*src == 0)
91  goto done_toucs;
92  }
93  else {
94  if (src + nb >= src_end)
95  goto done_toucs;
96  }
97  ch = 0;
98  switch (nb) {
99  /* these fall through deliberately */
100  case 3: ch += (unsigned char)*src++; ch <<= 6;
101  case 2: ch += (unsigned char)*src++; ch <<= 6;
102  case 1: ch += (unsigned char)*src++; ch <<= 6;
103  case 0: ch += (unsigned char)*src++;
104  }
105  ch -= offsetsFromUTF8[nb];
106  dest[i++] = ch;
107  }
108 done_toucs:
109  dest[i] = 0;
110  return i;
111 }
112 
113 /* srcsz = number of source characters, or -1 if 0-terminated
114  sz = size of dest buffer in bytes
115 
116  returns # characters converted
117  dest will only be '\0'-terminated if there is enough space. this is
118  for consistency; imagine there are 2 bytes of space left, but the next
119  character requires 3 bytes. in this case we could NUL-terminate, but in
120  general we can't when there's insufficient space. therefore this function
121  only NUL-terminates if all the characters fit, and there's space for
122  the NUL as well.
123  the destination string will never be bigger than the source string.
124  */
125 SWITCH_DECLARE(int) switch_u8_toutf8(char *dest, int sz, uint32_t *src, int srcsz)
126 {
127  uint32_t ch;
128  int i = 0;
129  char *dest_end = dest + sz;
130 
131  while (srcsz<0 ? src[i]!=0 : i < srcsz) {
132  ch = src[i];
133  if (ch < 0x80) {
134  if (dest >= dest_end)
135  return i;
136  *dest++ = (char)ch;
137  }
138  else if (ch < 0x800) {
139  if (dest >= dest_end-1)
140  return i;
141  *dest++ = (ch>>6) | 0xC0;
142  *dest++ = (ch & 0x3F) | 0x80;
143  }
144  else if (ch < 0x10000) {
145  if (dest >= dest_end-2)
146  return i;
147  *dest++ = (ch>>12) | 0xE0;
148  *dest++ = ((ch>>6) & 0x3F) | 0x80;
149  *dest++ = (ch & 0x3F) | 0x80;
150  }
151  else if (ch < 0x110000) {
152  if (dest >= dest_end-3)
153  return i;
154  *dest++ = (ch>>18) | 0xF0;
155  *dest++ = ((ch>>12) & 0x3F) | 0x80;
156  *dest++ = ((ch>>6) & 0x3F) | 0x80;
157  *dest++ = (ch & 0x3F) | 0x80;
158  }
159  i++;
160  }
161  if (dest < dest_end)
162  *dest = '\0';
163  return i;
164 }
165 
166 SWITCH_DECLARE(int) switch_u8_wc_toutf8(char *dest, uint32_t ch)
167 {
168  if (ch < 0x80) {
169  dest[0] = (char)ch;
170  return 1;
171  }
172  if (ch < 0x800) {
173  dest[0] = (ch>>6) | 0xC0;
174  dest[1] = (ch & 0x3F) | 0x80;
175  return 2;
176  }
177  if (ch < 0x10000) {
178  dest[0] = (ch>>12) | 0xE0;
179  dest[1] = ((ch>>6) & 0x3F) | 0x80;
180  dest[2] = (ch & 0x3F) | 0x80;
181  return 3;
182  }
183  if (ch < 0x110000) {
184  dest[0] = (ch>>18) | 0xF0;
185  dest[1] = ((ch>>12) & 0x3F) | 0x80;
186  dest[2] = ((ch>>6) & 0x3F) | 0x80;
187  dest[3] = (ch & 0x3F) | 0x80;
188  return 4;
189  }
190  return 0;
191 }
192 
193 /* charnum => byte offset */
194 SWITCH_DECLARE(int) switch_u8_offset(char *str, int charnum)
195 {
196  int offs=0;
197 
198  while (charnum > 0 && str[offs]) {
199  (void)(isutf(str[++offs]) || isutf(str[++offs]) ||
200  isutf(str[++offs]) || ++offs);
201  charnum--;
202  }
203  return offs;
204 }
205 
206 /* byte offset => charnum */
207 SWITCH_DECLARE(int) switch_u8_charnum(char *s, int offset)
208 {
209  int charnum = 0, offs=0;
210 
211  while (offs < offset && s[offs]) {
212  (void)(isutf(s[++offs]) || isutf(s[++offs]) ||
213  isutf(s[++offs]) || ++offs);
214  charnum++;
215  }
216  return charnum;
217 }
218 
219 /* number of characters */
221 {
222  int count = 0;
223  int i = 0;
224 
225  while (switch_u8_nextchar(s, &i) != 0)
226  count++;
227 
228  return count;
229 }
230 
231 /* reads the next utf-8 sequence out of a string, updating an index */
232 SWITCH_DECLARE(uint32_t) switch_u8_nextchar(char *s, int *i)
233 {
234  uint32_t ch = 0;
235  int sz = 0;
236 
237  do {
238  ch <<= 6;
239  ch += (unsigned char)s[(*i)++];
240  sz++;
241  } while (s[*i] && !isutf(s[*i]));
242  ch -= offsetsFromUTF8[sz-1];
243 
244  return ch;
245 }
246 
247 SWITCH_DECLARE(void) switch_u8_inc(char *s, int *i)
248 {
249  (void)(isutf(s[++(*i)]) || isutf(s[++(*i)]) ||
250  isutf(s[++(*i)]) || ++(*i));
251 }
252 
253 SWITCH_DECLARE(void) switch_u8_dec(char *s, int *i)
254 {
255  (void)(isutf(s[--(*i)]) || isutf(s[--(*i)]) ||
256  isutf(s[--(*i)]) || --(*i));
257 }
258 
260 {
261  return (c >= '0' && c <= '7');
262 }
263 
265 {
266  return ((c >= '0' && c <= '9') ||
267  (c >= 'A' && c <= 'F') ||
268  (c >= 'a' && c <= 'f'));
269 }
270 
271 /* assumes that src points to the character after a backslash
272  returns number of input characters processed */
273 SWITCH_DECLARE(int) switch_u8_read_escape_sequence(char *str, uint32_t *dest)
274 {
275  uint32_t ch;
276  char digs[9]="\0\0\0\0\0\0\0\0";
277  int dno=0, i=1;
278 
279  ch = (uint32_t)str[0]; /* take literal character */
280  if (str[0] == 'n')
281  ch = L'\n';
282  else if (str[0] == 't')
283  ch = L'\t';
284  else if (str[0] == 'r')
285  ch = L'\r';
286  else if (str[0] == 'b')
287  ch = L'\b';
288  else if (str[0] == 'f')
289  ch = L'\f';
290  else if (str[0] == 'v')
291  ch = L'\v';
292  else if (str[0] == 'a')
293  ch = L'\a';
294  else if (octal_digit(str[0])) {
295  i = 0;
296  do {
297  digs[dno++] = str[i++];
298  } while (octal_digit(str[i]) && dno < 3);
299  ch = strtol(digs, NULL, 8);
300  }
301  else if (str[0] == 'x') {
302  while (hex_digit(str[i]) && dno < 2) {
303  digs[dno++] = str[i++];
304  }
305  if (dno > 0)
306  ch = strtol(digs, NULL, 16);
307  }
308  else if (str[0] == 'u') {
309  while (hex_digit(str[i]) && dno < 4) {
310  digs[dno++] = str[i++];
311  }
312  if (dno > 0)
313  ch = strtol(digs, NULL, 16);
314  }
315  else if (str[0] == 'U') {
316  while (hex_digit(str[i]) && dno < 8) {
317  digs[dno++] = str[i++];
318  }
319  if (dno > 0)
320  ch = strtol(digs, NULL, 16);
321  }
322  *dest = ch;
323 
324  return i;
325 }
326 
327 /* convert a string with literal \uxxxx or \Uxxxxxxxx characters to UTF-8
328 example: u8_unescape(mybuf, 256, "hello\\u220e")
329 note the double backslash is needed if called on a C string literal */
330 SWITCH_DECLARE(int) switch_u8_unescape(char *buf, int sz, char *src)
331 {
332  int c=0, amt;
333  uint32_t ch;
334  char temp[4];
335 
336  while (*src && c < sz) {
337  if (*src == '\\') {
338  src++;
339  amt = switch_u8_read_escape_sequence(src, &ch);
340  }
341  else {
342  ch = (uint32_t)*src;
343  amt = 1;
344  }
345  src += amt;
346  amt = switch_u8_wc_toutf8(temp, ch);
347  if (amt > sz-c)
348  break;
349  memcpy(&buf[c], temp, amt);
350  c += amt;
351  }
352  if (c < sz)
353  buf[c] = '\0';
354  return c;
355 }
356 
357 SWITCH_DECLARE(int) switch_u8_escape_wchar(char *buf, int sz, uint32_t ch)
358 {
359  if (ch == L'\n')
360  return snprintf(buf, sz, "\\n");
361  else if (ch == L'\t')
362  return snprintf(buf, sz, "\\t");
363  else if (ch == L'\r')
364  return snprintf(buf, sz, "\\r");
365  else if (ch == L'\b')
366  return snprintf(buf, sz, "\\b");
367  else if (ch == L'\f')
368  return snprintf(buf, sz, "\\f");
369  else if (ch == L'\v')
370  return snprintf(buf, sz, "\\v");
371  else if (ch == L'\a')
372  return snprintf(buf, sz, "\\a");
373  else if (ch == L'\\')
374  return snprintf(buf, sz, "\\\\");
375  else if (ch < 32 || ch == 0x7f)
376  return snprintf(buf, sz, "\\x%hhX", (unsigned char)ch);
377  else if (ch > 0xFFFF)
378  return snprintf(buf, sz, "\\U%.8X", (uint32_t)ch);
379  else if (ch >= 0x80 && ch <= 0xFFFF)
380  return snprintf(buf, sz, "\\u%.4hX", (unsigned short)ch);
381 
382  return snprintf(buf, sz, "%c", (char)ch);
383 }
384 
385 SWITCH_DECLARE(int) switch_u8_escape(char *buf, int sz, char *src, int escape_quotes)
386 {
387  int c=0, i=0, amt;
388 
389  while (src[i] && c < sz) {
390  if (escape_quotes && src[i] == '"') {
391  amt = snprintf(buf, sz - c, "\\\"");
392  i++;
393  }
394  else {
395  amt = switch_u8_escape_wchar(buf, sz - c, switch_u8_nextchar(src, &i));
396  }
397  c += amt;
398  buf += amt;
399  }
400  if (c < sz)
401  *buf = '\0';
402  return c;
403 }
404 
405 SWITCH_DECLARE(char *) switch_u8_strchr(char *s, uint32_t ch, int *charn)
406 {
407  int i = 0, lasti=0;
408  uint32_t c;
409 
410  *charn = 0;
411  while (s[i]) {
412  c = switch_u8_nextchar(s, &i);
413  if (c == ch) {
414  return &s[lasti];
415  }
416  lasti = i;
417  (*charn)++;
418  }
419  return NULL;
420 }
421 
422 SWITCH_DECLARE(char *) switch_u8_memchr(char *s, uint32_t ch, size_t sz, int *charn)
423 {
424  int i = 0, lasti=0;
425  uint32_t c;
426  int csz;
427 
428  *charn = 0;
429  while (i < sz) {
430  c = csz = 0;
431  do {
432  c <<= 6;
433  c += (unsigned char)s[i++];
434  csz++;
435  } while (i < sz && !isutf(s[i]));
436  c -= offsetsFromUTF8[csz-1];
437 
438  if (c == ch) {
439  return &s[lasti];
440  }
441  lasti = i;
442  (*charn)++;
443  }
444  return NULL;
445 }
446 
448 {
449  /* this code based on libutf8 */
450  const char* cp = locale;
451 
452  for (; *cp != '\0' && *cp != '@' && *cp != '+' && *cp != ','; cp++) {
453  if (*cp == '.') {
454  const char* encoding = ++cp;
455  for (; *cp != '\0' && *cp != '@' && *cp != '+' && *cp != ','; cp++)
456  ;
457  if ((cp-encoding == 5 && !strncmp(encoding, "UTF-8", 5))
458  || (cp-encoding == 4 && !strncmp(encoding, "utf8", 4)))
459  return 1; /* it's UTF-8 */
460  break;
461  }
462  }
463  return 0;
464 }
465 
466 
467 
468 /* reads the next utf-8 sequence out of a string, updating an index */
469 SWITCH_DECLARE(uint32_t) switch_u8_get_char(char *s, int *i)
470 {
471  uint32_t ch = 0;
472  int sz = 0;
473 
474  do {
475  ch <<= 6;
476  ch += (unsigned char)s[(*i)++];
477  sz++;
478  } while (s[*i] && !isutf(s[*i]));
479 
480  ch -= offsetsFromUTF8[sz-1];
481 
482  return ch;
483 }
int switch_u8_offset(char *str, int charnum)
Definition: switch_utf8.c:194
int hex_digit(char c)
Definition: switch_utf8.c:264
uint32_t switch_u8_get_char(char *s, int *i)
Definition: switch_utf8.c:469
void switch_u8_dec(char *s, int *i)
Definition: switch_utf8.c:253
switch_byte_t switch_byte_t * buf
int switch_u8_escape(char *buf, int sz, char *src, int escape_quotes)
Definition: switch_utf8.c:385
void switch_u8_inc(char *s, int *i)
Definition: switch_utf8.c:247
int switch_u8_seqlen(char *s)
Definition: switch_utf8.c:65
int switch_u8_charnum(char *s, int offset)
Definition: switch_utf8.c:207
int switch_u8_read_escape_sequence(char *str, uint32_t *dest)
Definition: switch_utf8.c:273
int switch_u8_escape_wchar(char *buf, int sz, uint32_t ch)
Definition: switch_utf8.c:357
uint32_t switch_u8_nextchar(char *s, int *i)
Definition: switch_utf8.c:232
static const uint32_t offsetsFromUTF8[6]
Definition: switch_utf8.c:48
char * switch_u8_memchr(char *s, uint32_t ch, size_t sz, int *charn)
Definition: switch_utf8.c:422
int octal_digit(char c)
Definition: switch_utf8.c:259
int switch_u8_is_locale_utf8(char *locale)
Definition: switch_utf8.c:447
#define SWITCH_DECLARE(type)
int switch_u8_wc_toutf8(char *dest, uint32_t ch)
Definition: switch_utf8.c:166
int switch_u8_toutf8(char *dest, int sz, uint32_t *src, int srcsz)
Definition: switch_utf8.c:125
int switch_u8_toucs(uint32_t *dest, int sz, char *src, int srcsz)
Definition: switch_utf8.c:80
char * switch_u8_strchr(char *s, uint32_t ch, int *charn)
Definition: switch_utf8.c:405
int switch_u8_strlen(char *s)
Definition: switch_utf8.c:220
int switch_u8_unescape(char *buf, int sz, char *src)
Definition: switch_utf8.c:330
#define isutf(c)
Definition: switch_utf8.h:50
static const char trailingBytesForUTF8[256]
Definition: switch_utf8.c:53