use replace 0 with NULL for pointers, set some functions static
[blender.git] / source / blender / blenlib / intern / string_utf8.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2011 Blender Foundation.
21  * All rights reserved.
22  *
23  * Contributor(s): Campbell Barton.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  */
28
29  /** \file blender/blenlib/intern/string_utf8.c
30  *  \ingroup bli
31  */
32
33 #include <string.h>
34
35 #include "BLI_string.h"
36
37 /* from libswish3, originally called u8_isvalid(),
38  * modified to return the index of the bad character (byte index not utf).
39  * http://svn.swish-e.org/libswish3/trunk/src/libswish3/utf8.c r3044 - campbell */
40
41 /* based on the valid_utf8 routine from the PCRE library by Philip Hazel
42
43    length is in bytes, since without knowing whether the string is valid
44    it's hard to know how many characters there are! */
45
46 static const char trailingBytesForUTF8[256] = {
47         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,
48         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,
49         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,
50         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,
51         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,
52         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,
53         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,
54         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
55 };
56
57 int BLI_utf8_invalid_byte(const char *str, int length)
58 {
59         const unsigned char *p, *pend = (unsigned char*)str + length;
60         unsigned char c;
61         int ab;
62
63         for (p = (unsigned char*)str; p < pend; p++) {
64                 c = *p;
65                 if (c < 128)
66                         continue;
67                 if ((c & 0xc0) != 0xc0)
68                         goto utf8_error;
69                 ab = trailingBytesForUTF8[c];
70                 if (length < ab)
71                         goto utf8_error;
72                 length -= ab;
73
74                 p++;
75                 /* Check top bits in the second byte */
76                 if ((*p & 0xc0) != 0x80)
77                         goto utf8_error;
78
79                 /* Check for overlong sequences for each different length */
80                 switch (ab) {
81                         /* Check for xx00 000x */
82                 case 1:
83                         if ((c & 0x3e) == 0) goto utf8_error;
84                         continue;   /* We know there aren't any more bytes to check */
85
86                         /* Check for 1110 0000, xx0x xxxx */
87                 case 2:
88                         if (c == 0xe0 && (*p & 0x20) == 0) goto utf8_error;
89                         break;
90
91                         /* Check for 1111 0000, xx00 xxxx */
92                 case 3:
93                         if (c == 0xf0 && (*p & 0x30) == 0) goto utf8_error;
94                         break;
95
96                         /* Check for 1111 1000, xx00 0xxx */
97                 case 4:
98                         if (c == 0xf8 && (*p & 0x38) == 0) goto utf8_error;
99                         break;
100
101                         /* Check for leading 0xfe or 0xff,
102                            and then for 1111 1100, xx00 00xx */
103                 case 5:
104                         if (c == 0xfe || c == 0xff ||
105                                 (c == 0xfc && (*p & 0x3c) == 0)) goto utf8_error;
106                         break;
107                 }
108
109                 /* Check for valid bytes after the 2nd, if any; all must start 10 */
110                 while (--ab > 0) {
111                         if ((*(p+1) & 0xc0) != 0x80) goto utf8_error;
112                         p++; /* do this after so we get usable offset - campbell */
113                 }
114         }
115
116         return -1;
117
118 utf8_error:
119
120         return (int)((char *)p - (char *)str) - 1;
121 }
122
123 int BLI_utf8_invalid_strip(char *str, int length)
124 {
125         int bad_char, tot= 0;
126
127         while((bad_char= BLI_utf8_invalid_byte(str, length)) != -1) {
128                 str += bad_char;
129                 length -= bad_char;
130
131                 if(length == 0) {
132                         /* last character bad, strip it */
133                         *str= '\0';
134                         tot++;
135                         break;
136                 }
137                 else {
138                         /* strip, keep looking */
139                         memmove(str, str + 1, length);
140                         tot++;
141                 }
142         }
143
144         return tot;
145 }
146
147
148 /* compatible with BLI_strncpy, but esnure no partial utf8 chars */
149
150 /* array copied from glib's gutf8.c,
151  * note: this looks to be at odd's with 'trailingBytesForUTF8',
152  * need to find out what gives here! - campbell */
153 static const size_t utf8_skip_data[256] = {
154     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,
155     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,
156     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,
157     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,
158     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,
159     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,
160     2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
161     3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
162 };
163
164 char *BLI_strncpy_utf8(char *dst, const char *src, size_t maxncpy)
165 {
166         char *dst_r= dst;
167         size_t utf8_size;
168
169         /* note: currently we dont attempt to deal with invalid utf8 chars */
170
171         while(*src != '\0' && (utf8_size= utf8_skip_data[*src]) < maxncpy) {
172                 maxncpy -= utf8_size;
173                 switch(utf8_size) {
174                         case 6: *dst ++ = *src ++;
175                         case 5: *dst ++ = *src ++;
176                         case 4: *dst ++ = *src ++;
177                         case 3: *dst ++ = *src ++;
178                         case 2: *dst ++ = *src ++;
179                         case 1: *dst ++ = *src ++;
180                 }
181         }
182         *dst= '\0';
183         return dst_r;
184 }
185