summaryrefslogtreecommitdiff
path: root/include/utf8/utils/bad.php
blob: 27042940c881de2fb538f423b1147116e29d1f9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php

/**
* @version $Id: bad.php,v 1.2 2006/02/26 13:20:44 harryf Exp $
* Tools for locating / replacing bad bytes in UTF-8 strings
* The Original Code is Mozilla Communicator client code.
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
* Ported to PHP by Henri Sivonen (http://hsivonen.iki.fi)
* Slight modifications to fit with phputf8 library by Harry Fuecks (hfuecks gmail com)
* @see http://lxr.mozilla.org/seamonkey/source/intl/uconv/src/nsUTF8ToUnicode.cpp
* @see http://lxr.mozilla.org/seamonkey/source/intl/uconv/src/nsUnicodeToUTF8.cpp
* @see http://hsivonen.iki.fi/php-utf8/
* @package utf8
* @subpackage bad
* @see utf8_is_valid
*/

/**
* Locates the first bad byte in a UTF-8 string returning it's
* byte index in the string
* PCRE Pattern to locate bad bytes in a UTF-8 string
* Comes from W3 FAQ: Multilingual Forms
* Note: modified to include full ASCII range including control chars
* @see http://www.w3.org/International/questions/qa-forms-utf-8
* @param string
* @return mixed integer byte index or FALSE if no bad found
* @package utf8
* @subpackage bad
*/
function utf8_bad_find($str)
{
	$UTF8_BAD =
		'([\x00-\x7F]'.                          # ASCII (including control chars)
		'|[\xC2-\xDF][\x80-\xBF]'.               # Non-overlong 2-byte
		'|\xE0[\xA0-\xBF][\x80-\xBF]'.           # Excluding overlongs
		'|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'.    # Straight 3-byte
		'|\xED[\x80-\x9F][\x80-\xBF]'.           # Excluding surrogates
		'|\xF0[\x90-\xBF][\x80-\xBF]{2}'.        # Planes 1-3
		'|[\xF1-\xF3][\x80-\xBF]{3}'.            # Planes 4-15
		'|\xF4[\x80-\x8F][\x80-\xBF]{2}'.        # Plane 16
		'|(.{1}))';                              # Invalid byte
	$pos = 0;
	$badList = array();

	while (preg_match('/'.$UTF8_BAD.'/S', $str, $matches))
	{
		$bytes = strlen($matches[0]);

		if (isset($matches[2]))
			return $pos;

		$pos += $bytes;
		$str = substr($str,$bytes);
	}

	return false;
}

/**
* Locates all bad bytes in a UTF-8 string and returns a list of their
* byte index in the string
* PCRE Pattern to locate bad bytes in a UTF-8 string
* Comes from W3 FAQ: Multilingual Forms
* Note: modified to include full ASCII range including control chars
* @see http://www.w3.org/International/questions/qa-forms-utf-8
* @param string
* @return mixed array of integers or FALSE if no bad found
* @package utf8
* @subpackage bad
*/
function utf8_bad_findall($str)
{
	$UTF8_BAD =
		'([\x00-\x7F]'.                          # ASCII (including control chars)
		'|[\xC2-\xDF][\x80-\xBF]'.               # Non-overlong 2-byte
		'|\xE0[\xA0-\xBF][\x80-\xBF]'.           # Excluding overlongs
		'|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'.    # Straight 3-byte
		'|\xED[\x80-\x9F][\x80-\xBF]'.           # Excluding surrogates
		'|\xF0[\x90-\xBF][\x80-\xBF]{2}'.        # Planes 1-3
		'|[\xF1-\xF3][\x80-\xBF]{3}'.            # Planes 4-15
		'|\xF4[\x80-\x8F][\x80-\xBF]{2}'.        # Plane 16
		'|(.{1}))';                              # Invalid byte
	$pos = 0;
	$badList = array();

	while (preg_match('/'.$UTF8_BAD.'/S', $str, $matches))
	{
		$bytes = strlen($matches[0]);

		if (isset($matches[2]))
			$badList[] = $pos;

		$pos += $bytes;
		$str = substr($str,$bytes);
	}

	if (count($badList) > 0)
		return $badList;

	return false;
}

/**
* Strips out any bad bytes from a UTF-8 string and returns the rest
* PCRE Pattern to locate bad bytes in a UTF-8 string
* Comes from W3 FAQ: Multilingual Forms
* Note: modified to include full ASCII range including control chars
* @see http://www.w3.org/International/questions/qa-forms-utf-8
* @param string
* @return string
* @package utf8
* @subpackage bad
*/
function utf8_bad_strip($original)
{
	return utf8_bad_replace($original, '');
}

/**
* Replace bad bytes with an alternative character - ASCII character
* recommended is replacement char
* PCRE Pattern to locate bad bytes in a UTF-8 string
* Comes from W3 FAQ: Multilingual Forms
* Note: modified to include full ASCII range including control chars
* @see http://www.w3.org/International/questions/qa-forms-utf-8
* @param string to search
* @param string to replace bad bytes with (defaults to '?') - use ASCII
* @return string
* @package utf8
* @subpackage bad
*/
function utf8_bad_replace($original, $replace = '?') {
	$result = '';

	$strlen = strlen($original);
	for ($i = 0; $i < $strlen;) {
		$char = $original[$i++];
		$byte = ord($char);

		if ($byte < 0x80) $bytes = 0; // 1-bytes (00000000 - 01111111)
		else if ($byte < 0xC0) { // 1-bytes (10000000 - 10111111)
			$result .= $replace;
			continue;
		}
		else if ($byte < 0xE0) $bytes = 1; // 2-bytes (11000000 - 11011111)
		else if ($byte < 0xF0) $bytes = 2; // 3-bytes (11100000 - 11101111)
		else if ($byte < 0xF8) $bytes = 3; // 4-bytes (11110000 - 11110111)
		else if ($byte < 0xFC) $bytes = 4; // 5-bytes (11111000 - 11111011)
		else if ($byte < 0xFE) $bytes = 5; // 6-bytes (11111100 - 11111101)
		else { // Otherwise it's something invalid
			$result .= $replace;
			continue;
		}

		// Check our input actually has enough data
		if ($i + $bytes > $strlen) {
			$result .= $replace;
			continue;
		}

		// If we've got this far then we have a multiple-byte character
		for ($j = 0; $j < $bytes; $j++) {
			$byte = $original[$i + $j];

			$char .= $byte;
			$byte = ord($byte);

			// Every following byte must be 10000000 - 10111111
			if ($byte < 0x80 || $byte > 0xBF) {
				$result .= $replace;
				continue 2;
			}
		}

		$i += $bytes;
		$result .= $char;
	}

	return $result;
}

/**
* Return code from utf8_bad_identify() when a five octet sequence is detected.
* Note: 5 octets sequences are valid UTF-8 but are not supported by Unicode so
* do not represent a useful character
* @see utf8_bad_identify
* @package utf8
* @subpackage bad
*/
define('UTF8_BAD_5OCTET', 1);

/**
* Return code from utf8_bad_identify() when a six octet sequence is detected.
* Note: 6 octets sequences are valid UTF-8 but are not supported by Unicode so
* do not represent a useful character
* @see utf8_bad_identify
* @package utf8
* @subpackage bad
*/
define('UTF8_BAD_6OCTET', 2);

/**
* Return code from utf8_bad_identify().
* Invalid octet for use as start of multi-byte UTF-8 sequence
* @see utf8_bad_identify
* @package utf8
* @subpackage bad
*/
define('UTF8_BAD_SEQID', 3);

/**
* Return code from utf8_bad_identify().
* From Unicode 3.1, non-shortest form is illegal
* @see utf8_bad_identify
* @package utf8
* @subpackage bad
*/
define('UTF8_BAD_NONSHORT', 4);

/**
* Return code from utf8_bad_identify().
* From Unicode 3.2, surrogate characters are illegal
* @see utf8_bad_identify
* @package utf8
* @subpackage bad
*/
define('UTF8_BAD_SURROGATE', 5);

/**
* Return code from utf8_bad_identify().
* Codepoints outside the Unicode range are illegal
* @see utf8_bad_identify
* @package utf8
* @subpackage bad
*/
define('UTF8_BAD_UNIOUTRANGE', 6);

/**
* Return code from utf8_bad_identify().
* Incomplete multi-octet sequence
* Note: this is kind of a "catch-all"
* @see utf8_bad_identify
* @package utf8
* @subpackage bad
*/
define('UTF8_BAD_SEQINCOMPLETE', 7);

/**
* Reports on the type of bad byte found in a UTF-8 string. Returns a
* status code on the first bad byte found
* @author <hsivonen@iki.fi>
* @param string UTF-8 encoded string
* @return mixed integer constant describing problem or FALSE if valid UTF-8
* @see utf8_bad_explain
* @see http://hsivonen.iki.fi/php-utf8/
* @package utf8
* @subpackage bad
*/
function utf8_bad_identify($str, &$i)
{
	$mState = 0;	// Cached expected number of octets after the current octet
					// until the beginning of the next UTF8 character sequence
	$mUcs4  = 0;	// Cached Unicode character
	$mBytes = 1;	// Cached expected number of octets in the current sequence

	$len = strlen($str);

	for($i=0; $i < $len; $i++)
	{
		$in = ord($str{$i});

		if ( $mState == 0)
		{
			// When mState is zero we expect either a US-ASCII character or a multi-octet sequence.
			if (0 == (0x80 & ($in)))
			{
				// US-ASCII, pass straight through.
				$mBytes = 1;
			}
			else if (0xC0 == (0xE0 & ($in)))
			{
				// First octet of 2 octet sequence
				$mUcs4 = ($in);
				$mUcs4 = ($mUcs4 & 0x1F) << 6;
				$mState = 1;
				$mBytes = 2;
			}
			else if (0xE0 == (0xF0 & ($in)))
			{
				// First octet of 3 octet sequence
				$mUcs4 = ($in);
				$mUcs4 = ($mUcs4 & 0x0F) << 12;
				$mState = 2;
				$mBytes = 3;
			}
			else if (0xF0 == (0xF8 & ($in)))
			{
				// First octet of 4 octet sequence
				$mUcs4 = ($in);
				$mUcs4 = ($mUcs4 & 0x07) << 18;
				$mState = 3;
				$mBytes = 4;
			}
			else if (0xF8 == (0xFC & ($in)))
			{
				/* First octet of 5 octet sequence.
				*
				* This is illegal because the encoded codepoint must be either
				* (a) not the shortest form or
				* (b) outside the Unicode range of 0-0x10FFFF.
				*/
				return UTF8_BAD_5OCTET;
			}
			else if (0xFC == (0xFE & ($in)))
			{
				// First octet of 6 octet sequence, see comments for 5 octet sequence.
				return UTF8_BAD_6OCTET;
			}
			else
			{
				// Current octet is neither in the US-ASCII range nor a legal first
				// octet of a multi-octet sequence.
				return UTF8_BAD_SEQID;
			}
		}
		else
		{
			// When mState is non-zero, we expect a continuation of the multi-octet sequence
			if (0x80 == (0xC0 & ($in)))
			{
				// Legal continuation.
				$shift = ($mState - 1) * 6;
				$tmp = $in;
				$tmp = ($tmp & 0x0000003F) << $shift;
				$mUcs4 |= $tmp;

				/**
				* End of the multi-octet sequence. mUcs4 now contains the final
				* Unicode codepoint to be output
				*/
				if (0 == --$mState)
				{
					// From Unicode 3.1, non-shortest form is illegal
					if (((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
					((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
					((4 == $mBytes) && ($mUcs4 < 0x10000)) )
						return UTF8_BAD_NONSHORT;
					else if (($mUcs4 & 0xFFFFF800) == 0xD800) // From Unicode 3.2, surrogate characters are illegal
						return UTF8_BAD_SURROGATE;
					else if ($mUcs4 > 0x10FFFF) // Codepoints outside the Unicode range are illegal
						return UTF8_BAD_UNIOUTRANGE;

					// Initialize UTF8 cache
					$mState = 0;
					$mUcs4  = 0;
					$mBytes = 1;
				}

			}
			else
			{
				// ((0xC0 & (*in) != 0x80) && (mState != 0))
				// Incomplete multi-octet sequence.
				$i--;
				return UTF8_BAD_SEQINCOMPLETE;
			}
		}
	}

	// Incomplete multi-octet sequence
	if ($mState != 0)
	{
		$i--;
		return UTF8_BAD_SEQINCOMPLETE;
	}

	// No bad octets found
	$i = null;
	return false;
}

/**
* Takes a return code from utf8_bad_identify() are returns a message
* (in English) explaining what the problem is.
* @param int return code from utf8_bad_identify
* @return mixed string message or FALSE if return code unknown
* @see utf8_bad_identify
* @package utf8
* @subpackage bad
*/
function utf8_bad_explain($code)
{
	switch ($code)
	{
		case UTF8_BAD_5OCTET:
		return 'Five octet sequences are valid UTF-8 but are not supported by Unicode';
		break;

		case UTF8_BAD_6OCTET:
		return 'Six octet sequences are valid UTF-8 but are not supported by Unicode';
		break;

		case UTF8_BAD_SEQID:
		return 'Invalid octet for use as start of multi-byte UTF-8 sequence';
		break;

		case UTF8_BAD_NONSHORT:
		return 'From Unicode 3.1, non-shortest form is illegal';
		break;

		case UTF8_BAD_SURROGATE:
		return 'From Unicode 3.2, surrogate characters are illegal';
		break;

		case UTF8_BAD_UNIOUTRANGE:
		return 'Codepoints outside the Unicode range are illegal';
		break;

		case UTF8_BAD_SEQINCOMPLETE:
		return 'Incomplete multi-octet sequence';
		break;
	}

	trigger_error('Unknown error code: '.$code, E_USER_WARNING);

	return false;
}