~ubuntu-branches/ubuntu/wily/sflphone/wily-proposed

« back to all changes in this revision

Viewing changes to daemon/src/utf8_utils.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 1999 Tom Tromey
 
3
 *  Copyright (C) 2000 Red Hat, Inc.
 
4
 *  Copyright (C) 2014 Savoir-Faire Linux Inc.
 
5
 *
 
6
 *  Author: Pascal Potvin <pascal.potvin@extenway.com>
 
7
 *
 
8
 *  This program is free software; you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU General Public License as published by
 
10
 *  the Free Software Foundation; either version 3 of the License, or
 
11
 *  (at your option) any later version.
 
12
 *
 
13
 *  This program is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 *  GNU General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU General Public License
 
19
 *  along with this program; if not, write to the Free Software
 
20
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 
21
 *
 
22
 *  Additional permission under GNU GPL version 3 section 7:
 
23
 *
 
24
 *  If you modify this program, or any covered work, by linking or
 
25
 *  combining it with the OpenSSL project's OpenSSL library (or a
 
26
 *  modified version of that library), containing parts covered by the
 
27
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 
28
 *  grants you additional permission to convey the resulting work.
 
29
 *  Corresponding Source for a non-source form of such a combination
 
30
 *  shall include the source code for the parts of OpenSSL used as well
 
31
 *  as that of the covered work.
 
32
 */
 
33
 
 
34
 
 
35
#include <cstring>
 
36
#include <cassert>
 
37
#include "utf8_utils.h"
 
38
 
 
39
/*
 
40
 * The LIKELY and UNLIKELY macros let the programmer give hints to
 
41
 * the compiler about the expected result of an expression. Some compilers
 
42
 * can use this information for optimizations.
 
43
 */
 
44
#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
 
45
#define LIKELY(expr) (__builtin_expect (expr, 1))
 
46
#define UNLIKELY(expr) (__builtin_expect (expr, 0))
 
47
#else
 
48
#define LIKELY(expr) (expr)
 
49
#define UNLIKELY(expr) (expr)
 
50
#endif
 
51
 
 
52
 
 
53
/*
 
54
 * Check whether a Unicode (5.2) char is in a valid range.
 
55
 *
 
56
 * The first check comes from the Unicode guarantee to never encode
 
57
 * a point above 0x0010ffff, since UTF-16 couldn't represent it.
 
58
 *
 
59
 * The second check covers surrogate pairs (category Cs).
 
60
 *
 
61
 * @param Char the character
 
62
 */
 
63
#define UNICODE_VALID(Char)                   \
 
64
    ((Char) < 0x110000 &&                     \
 
65
     (((Char) & 0xFFFFF800) != 0xD800))
 
66
 
 
67
#define CONTINUATION_CHAR                           \
 
68
  if ((*(unsigned char *)p & 0xc0) != 0x80) /* 10xxxxxx */ \
 
69
    goto error;                                     \
 
70
  val <<= 6;                                        \
 
71
  val |= (*(unsigned char *)p) & 0x3f;
 
72
 
 
73
static const char *
 
74
fast_validate(const char *str)
 
75
{
 
76
    char32_t val = 0;
 
77
    char32_t min = 0;
 
78
    const char *p;
 
79
 
 
80
    for (p = str; *p; p++) {
 
81
        if (*(unsigned char *)p < 128)
 
82
            /* done */;
 
83
        else {
 
84
            const char *last;
 
85
 
 
86
            last = p;
 
87
 
 
88
            if ((*(unsigned char *)p & 0xe0) == 0xc0) { /* 110xxxxx */
 
89
                if (UNLIKELY((*(unsigned char *)p & 0x1e) == 0))
 
90
                    goto error;
 
91
 
 
92
                p++;
 
93
 
 
94
                if (UNLIKELY((*(unsigned char *)p & 0xc0) != 0x80))  /* 10xxxxxx */
 
95
                    goto error;
 
96
            } else {
 
97
                if ((*(unsigned char *)p & 0xf0) == 0xe0) { /* 1110xxxx */
 
98
                    min = (1 << 11);
 
99
                    val = *(unsigned char *)p & 0x0f;
 
100
                    goto TWO_REMAINING;
 
101
                } else if ((*(unsigned char *)p & 0xf8) == 0xf0) { /* 11110xxx */
 
102
                    min = (1 << 16);
 
103
                    val = *(unsigned char *)p & 0x07;
 
104
                } else
 
105
                    goto error;
 
106
 
 
107
                p++;
 
108
                CONTINUATION_CHAR;
 
109
TWO_REMAINING:
 
110
                p++;
 
111
                CONTINUATION_CHAR;
 
112
                p++;
 
113
                CONTINUATION_CHAR;
 
114
 
 
115
                if (UNLIKELY(val < min))
 
116
                    goto error;
 
117
 
 
118
                if (UNLIKELY(!UNICODE_VALID(val)))
 
119
                    goto error;
 
120
            }
 
121
 
 
122
            continue;
 
123
 
 
124
error:
 
125
            return last;
 
126
        }
 
127
    }
 
128
 
 
129
    return p;
 
130
}
 
131
 
 
132
static const char *
 
133
fast_validate_len(const char *str, ssize_t max_len)
 
134
{
 
135
    char32_t val = 0;
 
136
    char32_t min = 0;
 
137
    const char *p;
 
138
 
 
139
    assert(max_len >= 0);
 
140
 
 
141
    for (p = str; ((p - str) < max_len) && *p; p++) {
 
142
        if (*(unsigned char *)p < 128)
 
143
            /* done */;
 
144
        else {
 
145
            const char *last;
 
146
 
 
147
            last = p;
 
148
 
 
149
            if ((*(unsigned char *)p & 0xe0) == 0xc0) { /* 110xxxxx */
 
150
                if (UNLIKELY(max_len - (p - str) < 2))
 
151
                    goto error;
 
152
 
 
153
                if (UNLIKELY((*(unsigned char *)p & 0x1e) == 0))
 
154
                    goto error;
 
155
 
 
156
                p++;
 
157
 
 
158
                if (UNLIKELY((*(unsigned char *)p & 0xc0) != 0x80))  /* 10xxxxxx */
 
159
                    goto error;
 
160
            } else {
 
161
                if ((*(unsigned char *)p & 0xf0) == 0xe0) { /* 1110xxxx */
 
162
                    if (UNLIKELY(max_len - (p - str) < 3))
 
163
                        goto error;
 
164
 
 
165
                    min = (1 << 11);
 
166
                    val = *(unsigned char *)p & 0x0f;
 
167
                    goto TWO_REMAINING;
 
168
                } else if ((*(unsigned char *)p & 0xf8) == 0xf0) { /* 11110xxx */
 
169
                    if (UNLIKELY(max_len - (p - str) < 4))
 
170
                        goto error;
 
171
 
 
172
                    min = (1 << 16);
 
173
                    val = *(unsigned char *)p & 0x07;
 
174
                } else
 
175
                    goto error;
 
176
 
 
177
                p++;
 
178
                CONTINUATION_CHAR;
 
179
TWO_REMAINING:
 
180
                p++;
 
181
                CONTINUATION_CHAR;
 
182
                p++;
 
183
                CONTINUATION_CHAR;
 
184
 
 
185
                if (UNLIKELY(val < min))
 
186
                    goto error;
 
187
 
 
188
                if (UNLIKELY(!UNICODE_VALID(val)))
 
189
                    goto error;
 
190
            }
 
191
 
 
192
            continue;
 
193
 
 
194
error:
 
195
            return last;
 
196
        }
 
197
    }
 
198
 
 
199
    return p;
 
200
}
 
201
 
 
202
/**
 
203
 * utf8_validate_c_str:
 
204
 * @str: a pointer to character data
 
205
 * @max_len: max bytes to validate, or -1 to go until NULL
 
206
 * @end: return location for end of valid data
 
207
 *
 
208
 * Validates UTF-8 encoded text. @str is the text to validate;
 
209
 * if @str is nul-terminated, then @max_len can be -1, otherwise
 
210
 * @max_len should be the number of bytes to validate.
 
211
 * If @end is non-%NULL, then the end of the valid range
 
212
 * will be stored there (i.e. the start of the first invalid
 
213
 * character if some bytes were invalid, or the end of the text
 
214
 * being validated otherwise).
 
215
 *
 
216
 * Note that utf8_validate() returns %false if @max_len is
 
217
 * positive and any of the @max_len bytes are nul.
 
218
 *
 
219
 * Returns true if all of @str was valid. Dbus requires valid UTF-8 as input;
 
220
 * sip packets should also be encoded in utf8; so data read from a file or the
 
221
 * network should be checked with utf8_validate() before doing anything else
 
222
 * with it.
 
223
 *
 
224
 * Returns: true if the text was valid UTF-8
 
225
 */
 
226
bool
 
227
utf8_validate_c_str(const char *str, ssize_t max_len, const char **end)
 
228
{
 
229
    const char *p;
 
230
 
 
231
    if (max_len < 0)
 
232
        p = fast_validate(str);
 
233
    else
 
234
        p = fast_validate_len(str, max_len);
 
235
 
 
236
    if (end)
 
237
        *end = p;
 
238
 
 
239
    if ((max_len >= 0 && p != str + max_len) ||
 
240
            (max_len < 0 && *p != '\0'))
 
241
        return false;
 
242
    else
 
243
        return true;
 
244
}
 
245
 
 
246
bool
 
247
utf8_validate(const std::string & str)
 
248
{
 
249
    const char *p;
 
250
 
 
251
    p = fast_validate(str.c_str());
 
252
 
 
253
    return (*p == '\0');
 
254
}
 
255
 
 
256
std::string
 
257
utf8_make_valid(const std::string & name)
 
258
{
 
259
    ssize_t remaining_bytes = name.size();
 
260
    ssize_t valid_bytes;
 
261
    const char *remainder = name.c_str();
 
262
    const char *invalid;
 
263
    char *str = NULL;
 
264
    char *pos;
 
265
 
 
266
    while (remaining_bytes != 0) {
 
267
        if (utf8_validate_c_str(remainder, remaining_bytes, &invalid))
 
268
            break;
 
269
 
 
270
        valid_bytes = invalid - remainder;
 
271
 
 
272
        if (str == NULL)
 
273
            // If every byte is replaced by U+FFFD, max(strlen(string)) == 3 * name.size()
 
274
            str = new char[3 * remaining_bytes];
 
275
 
 
276
        pos = str;
 
277
 
 
278
        strncpy(pos, remainder, valid_bytes);
 
279
        pos += valid_bytes;
 
280
 
 
281
        /* append U+FFFD REPLACEMENT CHARACTER */
 
282
        pos[0] = '\357';
 
283
        pos[1] = '\277';
 
284
        pos[2] = '\275';
 
285
 
 
286
        pos += 3;
 
287
 
 
288
        remaining_bytes -= valid_bytes + 1;
 
289
        remainder = invalid + 1;
 
290
    }
 
291
 
 
292
    if (str == NULL)
 
293
        return std::string(name);
 
294
 
 
295
    strncpy(pos, remainder, remaining_bytes);
 
296
    pos += remaining_bytes;
 
297
 
 
298
    std::string answer(str, pos - str);
 
299
    assert(utf8_validate_c_str(answer.c_str(), -1, NULL));
 
300
 
 
301
    delete[] str;
 
302
 
 
303
    return answer;
 
304
}