Blender  V3.3
utfconv.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2012 Blender Foundation. All rights reserved. */
3 
8 #include "utfconv.h"
9 
10 size_t count_utf_8_from_16(const wchar_t *string16)
11 {
12  int i;
13  size_t count = 0;
14  wchar_t u = 0;
15  if (!string16) {
16  return 0;
17  }
18 
19  for (i = 0; (u = string16[i]); i++) {
20  if (u < 0x0080) {
21  count += 1;
22  }
23  else {
24  if (u < 0x0800) {
25  count += 2;
26  }
27  else {
28  if (u < 0xD800) {
29  count += 3;
30  }
31  else {
32  if (u < 0xDC00) {
33  i++;
34  if ((u = string16[i]) == 0) {
35  break;
36  }
37  if (u >= 0xDC00 && u < 0xE000) {
38  count += 4;
39  }
40  }
41  else {
42  if (u < 0xE000) {
43  /* Illegal. */
44  }
45  else {
46  count += 3;
47  }
48  }
49  }
50  }
51  }
52  }
53 
54  return ++count;
55 }
56 
57 size_t count_utf_16_from_8(const char *string8)
58 {
59  size_t count = 0;
60  char u;
61  char type = 0;
62  unsigned int u32 = 0;
63 
64  if (!string8) {
65  return 0;
66  }
67 
68  for (; (u = *string8); string8++) {
69  if (type == 0) {
70  if ((u & 0x01 << 7) == 0) {
71  count++;
72  u32 = 0;
73  continue;
74  } // 1 utf-8 char
75  if ((u & 0x07 << 5) == 0xC0) {
76  type = 1;
77  u32 = u & 0x1F;
78  continue;
79  } // 2 utf-8 char
80  if ((u & 0x0F << 4) == 0xE0) {
81  type = 2;
82  u32 = u & 0x0F;
83  continue;
84  } // 3 utf-8 char
85  if ((u & 0x1F << 3) == 0xF0) {
86  type = 3;
87  u32 = u & 0x07;
88  continue;
89  } // 4 utf-8 char
90  continue;
91  }
92  if ((u & 0xC0) == 0x80) {
93  u32 = (u32 << 6) | (u & 0x3F);
94  type--;
95  }
96  else {
97  u32 = 0;
98  type = 0;
99  }
100 
101  if (type == 0) {
102  if ((0 < u32 && u32 < 0xD800) || (0xE000 <= u32 && u32 < 0x10000)) {
103  count++;
104  }
105  else if (0x10000 <= u32 && u32 < 0x110000) {
106  count += 2;
107  }
108  u32 = 0;
109  }
110  }
111 
112  return ++count;
113 }
114 
115 int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
116 {
117  char *out8end = out8 + size8;
118  wchar_t u = 0;
119  int err = 0;
120  if (!size8 || !in16 || !out8) {
121  return UTF_ERROR_NULL_IN;
122  }
123  out8end--;
124 
125  for (; out8 < out8end && (u = *in16); in16++, out8++) {
126  if (u < 0x0080) {
127  *out8 = u;
128  }
129  else if (u < 0x0800) {
130  if (out8 + 1 >= out8end) {
131  break;
132  }
133  *out8++ = (0x3 << 6) | (0x1F & (u >> 6));
134  *out8 = (0x1 << 7) | (0x3F & (u));
135  }
136  else if (u < 0xD800 || u >= 0xE000) {
137  if (out8 + 2 >= out8end) {
138  break;
139  }
140  *out8++ = (0x7 << 5) | (0xF & (u >> 12));
141  *out8++ = (0x1 << 7) | (0x3F & (u >> 6));
142  *out8 = (0x1 << 7) | (0x3F & (u));
143  }
144  else if (u < 0xDC00) {
145  wchar_t u2 = *++in16;
146 
147  if (!u2) {
148  break;
149  }
150  if (u2 >= 0xDC00 && u2 < 0xE000) {
151  if (out8 + 3 >= out8end) {
152  break;
153  }
154  unsigned int uc = 0x10000 + (u2 - 0xDC00) + ((u - 0xD800) << 10);
155 
156  *out8++ = (0xF << 4) | (0x7 & (uc >> 18));
157  *out8++ = (0x1 << 7) | (0x3F & (uc >> 12));
158  *out8++ = (0x1 << 7) | (0x3F & (uc >> 6));
159  *out8 = (0x1 << 7) | (0x3F & (uc));
160  }
161  else {
162  out8--;
164  }
165  }
166  else if (u < 0xE000) {
167  out8--;
169  }
170  }
171 
172  *out8 = *out8end = 0;
173 
174  if (*in16) {
175  err |= UTF_ERROR_SMALL;
176  }
177 
178  return err;
179 }
180 
181 int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16)
182 {
183  char u;
184  char type = 0;
185  unsigned int u32 = 0;
186  wchar_t *out16end = out16 + size16;
187  int err = 0;
188  if (!size16 || !in8 || !out16) {
189  return UTF_ERROR_NULL_IN;
190  }
191  out16end--;
192 
193  for (; out16 < out16end && (u = *in8); in8++) {
194  if (type == 0) {
195  if ((u & 0x01 << 7) == 0) {
196  *out16 = u;
197  out16++;
198  u32 = 0;
199  continue;
200  } // 1 utf-8 char
201  if ((u & 0x07 << 5) == 0xC0) {
202  type = 1;
203  u32 = u & 0x1F;
204  continue;
205  } // 2 utf-8 char
206  if ((u & 0x0F << 4) == 0xE0) {
207  type = 2;
208  u32 = u & 0x0F;
209  continue;
210  } // 3 utf-8 char
211  if ((u & 0x1F << 3) == 0xF0) {
212  type = 3;
213  u32 = u & 0x07;
214  continue;
215  } // 4 utf-8 char
217  continue;
218  }
219  if ((u & 0xC0) == 0x80) {
220  u32 = (u32 << 6) | (u & 0x3F);
221  type--;
222  }
223  else {
224  u32 = 0;
225  type = 0;
227  }
228 
229  if (type == 0) {
230  if ((0 < u32 && u32 < 0xD800) || (0xE000 <= u32 && u32 < 0x10000)) {
231  *out16 = u32;
232  out16++;
233  }
234  else if (0x10000 <= u32 && u32 < 0x110000) {
235  if (out16 + 1 >= out16end) {
236  break;
237  }
238  u32 -= 0x10000;
239  *out16 = 0xD800 + (u32 >> 10);
240  out16++;
241  *out16 = 0xDC00 + (u32 & 0x3FF);
242  out16++;
243  }
244  u32 = 0;
245  }
246  }
247 
248  *out16 = *out16end = 0;
249 
250  if (*in8) {
251  err |= UTF_ERROR_SMALL;
252  }
253 
254  return err;
255 }
256 
257 /* UNUSED FUNCTIONS */
258 #if 0
259 static int is_ascii(const char *in8)
260 {
261  for (; *in8; in8++)
262  if (0x80 & *in8)
263  return 0;
264 
265  return 1;
266 }
267 
268 static void utf_8_cut_end(char *inout8, size_t maxcutpoint)
269 {
270  char *cur = inout8 + maxcutpoint;
271  char cc;
272  if (!inout8)
273  return;
274 
275  cc = *cur;
276 }
277 #endif
278 
279 char *alloc_utf_8_from_16(const wchar_t *in16, size_t add)
280 {
281  size_t bsize = count_utf_8_from_16(in16);
282  char *out8 = NULL;
283  if (!bsize) {
284  return NULL;
285  }
286  out8 = (char *)malloc(sizeof(char) * (bsize + add));
287  conv_utf_16_to_8(in16, out8, bsize);
288  return out8;
289 }
290 
291 wchar_t *alloc_utf16_from_8(const char *in8, size_t add)
292 {
293  size_t bsize = count_utf_16_from_8(in8);
294  wchar_t *out16 = NULL;
295  if (!bsize) {
296  return NULL;
297  }
298  out16 = (wchar_t *)malloc(sizeof(wchar_t) * (bsize + add));
299  conv_utf_8_to_16(in8, out16, bsize);
300  return out16;
301 }
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble u2
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
int count
bool add(void *owner, const AttributeIDRef &attribute_id, eAttrDomain domain, eCustomDataType data_type, const AttributeInit &initializer)
size_t count_utf_8_from_16(const wchar_t *string16)
Definition: utfconv.c:10
wchar_t * alloc_utf16_from_8(const char *in8, size_t add)
Definition: utfconv.c:291
int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16)
Definition: utfconv.c:181
char * alloc_utf_8_from_16(const wchar_t *in16, size_t add)
Definition: utfconv.c:279
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
Definition: utfconv.c:115
size_t count_utf_16_from_8(const char *string8)
Definition: utfconv.c:57
#define UTF_ERROR_ILLSEQ
Definition: utfconv.h:44
#define UTF_ERROR_ILLCHAR
Definition: utfconv.h:40
#define UTF_ERROR_NULL_IN
Definition: utfconv.h:38
#define UTF_ERROR_SMALL
Definition: utfconv.h:42
static FT_Error err