FreeSWITCH API Documentation  1.7.0
Functions | Variables
switch_utf8.c File Reference
#include "switch_utf8.h"
+ Include dependency graph for switch_utf8.c:

Go to the source code of this file.

Functions

int switch_u8_seqlen (char *s)
 
int switch_u8_toucs (uint32_t *dest, int sz, char *src, int srcsz)
 
int switch_u8_toutf8 (char *dest, int sz, uint32_t *src, int srcsz)
 
int switch_u8_wc_toutf8 (char *dest, uint32_t ch)
 
int switch_u8_offset (char *str, int charnum)
 
int switch_u8_charnum (char *s, int offset)
 
int switch_u8_strlen (char *s)
 
uint32_t switch_u8_nextchar (char *s, int *i)
 
void switch_u8_inc (char *s, int *i)
 
void switch_u8_dec (char *s, int *i)
 
int octal_digit (char c)
 
int hex_digit (char c)
 
int switch_u8_read_escape_sequence (char *str, uint32_t *dest)
 
int switch_u8_unescape (char *buf, int sz, char *src)
 
int switch_u8_escape_wchar (char *buf, int sz, uint32_t ch)
 
int switch_u8_escape (char *buf, int sz, char *src, int escape_quotes)
 
char * switch_u8_strchr (char *s, uint32_t ch, int *charn)
 
char * switch_u8_memchr (char *s, uint32_t ch, size_t sz, int *charn)
 
int switch_u8_is_locale_utf8 (char *locale)
 
uint32_t switch_u8_get_char (char *s, int *i)
 

Variables

static const uint32_t offsetsFromUTF8 [6]
 
static const char trailingBytesForUTF8 [256]
 

Function Documentation

int hex_digit ( char  c)

Definition at line 264 of file switch_utf8.c.

Referenced by switch_u8_read_escape_sequence().

265 {
266  return ((c >= '0' && c <= '9') ||
267  (c >= 'A' && c <= 'F') ||
268  (c >= 'a' && c <= 'f'));
269 }
int octal_digit ( char  c)

Definition at line 259 of file switch_utf8.c.

Referenced by switch_u8_read_escape_sequence().

260 {
261  return (c >= '0' && c <= '7');
262 }
int switch_u8_charnum ( char *  s,
int  offset 
)

Definition at line 207 of file switch_utf8.c.

References isutf.

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 }
#define isutf(c)
Definition: switch_utf8.h:50
void switch_u8_dec ( char *  s,
int *  i 
)

Definition at line 253 of file switch_utf8.c.

References isutf.

254 {
255  (void)(isutf(s[--(*i)]) || isutf(s[--(*i)]) ||
256  isutf(s[--(*i)]) || --(*i));
257 }
#define isutf(c)
Definition: switch_utf8.h:50
int switch_u8_escape ( char *  buf,
int  sz,
char *  src,
int  escape_quotes 
)

Definition at line 385 of file switch_utf8.c.

References switch_u8_escape_wchar(), and switch_u8_nextchar().

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 }
switch_byte_t switch_byte_t * buf
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
int switch_u8_escape_wchar ( char *  buf,
int  sz,
uint32_t  ch 
)

Definition at line 357 of file switch_utf8.c.

Referenced by switch_u8_escape().

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 }
switch_byte_t switch_byte_t * buf
uint32_t switch_u8_get_char ( char *  s,
int *  i 
)

Definition at line 469 of file switch_utf8.c.

References isutf, and offsetsFromUTF8.

Referenced by switch_img_txt_handle_render().

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 }
static const uint32_t offsetsFromUTF8[6]
Definition: switch_utf8.c:48
#define isutf(c)
Definition: switch_utf8.h:50
void switch_u8_inc ( char *  s,
int *  i 
)

Definition at line 247 of file switch_utf8.c.

References isutf.

248 {
249  (void)(isutf(s[++(*i)]) || isutf(s[++(*i)]) ||
250  isutf(s[++(*i)]) || ++(*i));
251 }
#define isutf(c)
Definition: switch_utf8.h:50
int switch_u8_is_locale_utf8 ( char *  locale)

Definition at line 447 of file switch_utf8.c.

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 }
char* switch_u8_memchr ( char *  s,
uint32_t  ch,
size_t  sz,
int *  charn 
)

Definition at line 422 of file switch_utf8.c.

References isutf, and offsetsFromUTF8.

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 }
static const uint32_t offsetsFromUTF8[6]
Definition: switch_utf8.c:48
#define isutf(c)
Definition: switch_utf8.h:50
uint32_t switch_u8_nextchar ( char *  s,
int *  i 
)

Definition at line 232 of file switch_utf8.c.

References isutf, and offsetsFromUTF8.

Referenced by switch_u8_escape(), switch_u8_strchr(), and switch_u8_strlen().

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 }
static const uint32_t offsetsFromUTF8[6]
Definition: switch_utf8.c:48
#define isutf(c)
Definition: switch_utf8.h:50
int switch_u8_offset ( char *  str,
int  charnum 
)

Definition at line 194 of file switch_utf8.c.

References isutf.

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 }
#define isutf(c)
Definition: switch_utf8.h:50
int switch_u8_read_escape_sequence ( char *  str,
uint32_t *  dest 
)

Definition at line 273 of file switch_utf8.c.

References hex_digit(), and octal_digit().

Referenced by switch_u8_unescape().

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 }
int hex_digit(char c)
Definition: switch_utf8.c:264
int octal_digit(char c)
Definition: switch_utf8.c:259
int switch_u8_seqlen ( char *  s)

Definition at line 65 of file switch_utf8.c.

References trailingBytesForUTF8.

66 {
67  return trailingBytesForUTF8[(unsigned int)(unsigned char)s[0]] + 1;
68 }
static const char trailingBytesForUTF8[256]
Definition: switch_utf8.c:53
char* switch_u8_strchr ( char *  s,
uint32_t  ch,
int *  charn 
)

Definition at line 405 of file switch_utf8.c.

References switch_u8_nextchar().

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 }
uint32_t switch_u8_nextchar(char *s, int *i)
Definition: switch_utf8.c:232
int switch_u8_strlen ( char *  s)

Definition at line 220 of file switch_utf8.c.

References switch_u8_nextchar().

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 }
uint32_t switch_u8_nextchar(char *s, int *i)
Definition: switch_utf8.c:232
int switch_u8_toucs ( uint32_t *  dest,
int  sz,
char *  src,
int  srcsz 
)

Definition at line 80 of file switch_utf8.c.

References offsetsFromUTF8, and trailingBytesForUTF8.

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 }
static const uint32_t offsetsFromUTF8[6]
Definition: switch_utf8.c:48
static const char trailingBytesForUTF8[256]
Definition: switch_utf8.c:53
int switch_u8_toutf8 ( char *  dest,
int  sz,
uint32_t *  src,
int  srcsz 
)

Definition at line 125 of file switch_utf8.c.

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 }
int switch_u8_unescape ( char *  buf,
int  sz,
char *  src 
)

Definition at line 330 of file switch_utf8.c.

References switch_u8_read_escape_sequence(), and switch_u8_wc_toutf8().

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 }
switch_byte_t switch_byte_t * buf
int switch_u8_read_escape_sequence(char *str, uint32_t *dest)
Definition: switch_utf8.c:273
int switch_u8_wc_toutf8(char *dest, uint32_t ch)
Definition: switch_utf8.c:166
int switch_u8_wc_toutf8 ( char *  dest,
uint32_t  ch 
)

Definition at line 166 of file switch_utf8.c.

Referenced by switch_u8_unescape().

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 }

Variable Documentation

const uint32_t offsetsFromUTF8[6]
static
Initial value:
= {
0x00000000UL, 0x00003080UL, 0x000E2080UL,
0x03C82080UL, 0xFA082080UL, 0x82082080UL
}

Definition at line 48 of file switch_utf8.c.

Referenced by switch_u8_get_char(), switch_u8_memchr(), switch_u8_nextchar(), and switch_u8_toucs().

const char trailingBytesForUTF8[256]
static
Initial value:
= {
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,
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,
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,
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,
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,
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,
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,
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
}

Definition at line 53 of file switch_utf8.c.

Referenced by switch_u8_seqlen(), and switch_u8_toucs().