typo: RE_IPV4_BLOCK -> RE_IP_BLOCK
[lhc/web/wiklou.git] / includes / IP.php
1 <?php
2 /**
3 * Functions and constants to play with IP addresses and ranges
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Ashar Voultoiz <hashar at free dot fr>, Aaron Schulz
22 */
23
24 // Some regex definition to "play" with IP address and IP address blocks
25
26 // An IPv4 address is made of 4 bytes from x00 to xFF which is d0 to d255
27 define( 'RE_IP_BYTE', '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])' );
28 define( 'RE_IP_ADD' , RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE );
29 // An IPv4 block is an IP address and a prefix (d1 to d32)
30 define( 'RE_IP_PREFIX', '(3[0-2]|[12]?\d)' );
31 define( 'RE_IP_BLOCK', RE_IP_ADD . '\/' . RE_IP_PREFIX );
32
33 // An IPv6 address is made up of 8 words (each x0000 to xFFFF).
34 // However, the "::" abbreviation can be used on consecutive x0000 words.
35 define( 'RE_IPV6_WORD', '([0-9A-Fa-f]{1,4})' );
36 define( 'RE_IPV6_PREFIX', '(12[0-8]|1[01][0-9]|[1-9]?\d)');
37 define( 'RE_IPV6_ADD',
38 '(' . // starts with "::" (includes the address "::")
39 '(::|:(:' . RE_IPV6_WORD . '){1,7})' .
40 '|' . // ends with "::" (not including the address "::")
41 RE_IPV6_WORD . '(:' . RE_IPV6_WORD . '){0,6}::' .
42 '|' . // has no "::"
43 RE_IPV6_WORD . '(:' . RE_IPV6_WORD . '){7}' .
44 '|' . // contains one "::" in the middle ("^" check always fails if no "::" found)
45 RE_IPV6_WORD . '(:(?P<abbr>(?(abbr)|:))?' . RE_IPV6_WORD . '){1,6}(?(abbr)|^)' .
46 ')'
47 );
48 // An IPv6 block is an IP address and a prefix (d1 to d128)
49 define( 'RE_IPV6_BLOCK', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX );
50 // For IPv6 canonicalization (NOT for strict validation; these are quite lax!)
51 define( 'RE_IPV6_GAP', ':(?:0+:)*(?::(?:0+:)*)?' );
52 define( 'RE_IPV6_V4_PREFIX', '0*' . RE_IPV6_GAP . '(?:ffff:)?' );
53
54 // This might be useful for regexps used elsewhere, matches any IPv6 or IPv6 address or network
55 define( 'IP_ADDRESS_STRING',
56 '(?:' .
57 RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)' . // IPv4
58 '|' .
59 RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)' . // IPv6
60 ')'
61 );
62
63 /**
64 * A collection of public static functions to play with IP address
65 * and IP blocks.
66 */
67 class IP {
68 /**
69 * Determine if a string is as valid IP address or network (CIDR prefix).
70 * SIIT IPv4-translated addresses are rejected.
71 * Note: canonicalize() tries to convert translated addresses to IPv4.
72 * @param string $ip possible IP address
73 * @return bool
74 */
75 public static function isIPAddress( $ip ) {
76 return (bool)preg_match( '/^' . IP_ADDRESS_STRING . '$/', $ip );
77 }
78
79 /**
80 * Given a string, determine if it as valid IP in IPv6 only.
81 * Note: Unlike isValid(), this looks for networks too.
82 * @param string $ip possible IP address
83 * @return bool
84 */
85 public static function isIPv6( $ip ) {
86 return (bool)preg_match( '/^' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)$/', $ip );
87 }
88
89 /**
90 * Given a string, determine if it as valid IP in IPv4 only.
91 * Note: Unlike isValid(), this looks for networks too.
92 * @param string $ip possible IP address
93 * @return bool
94 */
95 public static function isIPv4( $ip ) {
96 return (bool)preg_match( '/^' . RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)$/', $ip );
97 }
98
99 /**
100 * Validate an IP address. Ranges are NOT considered valid.
101 * SIIT IPv4-translated addresses are rejected.
102 * Note: canonicalize() tries to convert translated addresses to IPv4.
103 * @param string $ip
104 * @return boolean True if it is valid.
105 */
106 public static function isValid( $ip ) {
107 return ( preg_match( '/^' . RE_IP_ADD . '$/', $ip )
108 || preg_match( '/^' . RE_IPV6_ADD . '$/', $ip ) );
109 }
110
111 /**
112 * Validate an IP Block (valid address WITH a valid prefix).
113 * SIIT IPv4-translated addresses are rejected.
114 * Note: canonicalize() tries to convert translated addresses to IPv4.
115 * @param string $ipblock
116 * @return boolean True if it is valid.
117 */
118 public static function isValidBlock( $ipblock ) {
119 return ( preg_match( '/^' . RE_IPV6_BLOCK . '$/', $ipblock )
120 || preg_match( '/^' . RE_IP_BLOCK . '$/', $ipblock ) );
121 }
122
123 /**
124 * Given an IP address in dotted-quad notation, returns an IPv6 octet.
125 * See http://www.answers.com/topic/ipv4-compatible-address
126 * IPs with the first 92 bits as zeros are reserved from IPv6
127 * @param string $ip quad-dotted IP address.
128 * @return string IPv4 address
129 */
130 public static function IPv4toIPv6( $ip ) {
131 if ( !self::isIPv4( $ip ) ) {
132 throw new MWException( "IPv4toIPv6() called on a non-IPv4 address." );
133 }
134 // IPv4 address with CIDR
135 if ( strpos( $ip, '/' ) !== false ) {
136 $parts = explode( '/', $ip, 2 );
137 if ( count( $parts ) != 2 ) {
138 return false;
139 }
140 list( $network, $bits ) = $parts;
141 $network = self::toUnsigned( $network );
142 if ( $network !== false && is_numeric( $bits ) && $bits >= 0 && $bits <= 32 ) {
143 $bits += 96;
144 return self::toOctet( $network ) . "/$bits";
145 } else {
146 return false;
147 }
148 }
149 return self::toOctet( self::toUnsigned( $ip ) );
150 }
151
152 /**
153 * Convert an IP into a nice standard form.
154 * IPv6 addresses in octet notation are expanded to 8 words.
155 * IPv4 addresses are just trimmed.
156 * @param string $ip IP address in quad or octet form (CIDR or not).
157 * @return string
158 */
159 public static function sanitizeIP( $ip ) {
160 $ip = trim( $ip );
161 if ( $ip === '' ) {
162 return null;
163 }
164 if ( self::isIPv4( $ip ) || !self::isIPv6( $ip ) ) {
165 return $ip; // nothing else to do for IPv4 addresses or invalid ones
166 }
167 // Remove any whitespaces, convert to upper case
168 $ip = strtoupper( $ip );
169 // Expand zero abbreviations
170 $abbrevPos = strpos( $ip, '::' );
171 if ( $abbrevPos !== false ) {
172 // We know this is valid IPv6. Find the last index of the
173 // address before any CIDR number (e.g. "a:b:c::/24").
174 $CIDRStart = strpos( $ip, "/" );
175 $addressEnd = ( $CIDRStart !== false )
176 ? $CIDRStart - 1
177 : strlen( $ip ) - 1;
178 // If the '::' is at the beginning...
179 if ( $abbrevPos == 0 ) {
180 $repeat = '0:';
181 $extra = ( $ip == '::' ) ? '0' : ''; // for the address '::'
182 $pad = 9; // 7+2 (due to '::')
183 // If the '::' is at the end...
184 } elseif ( $abbrevPos == ( $addressEnd - 1 ) ) {
185 $repeat = ':0';
186 $extra = '';
187 $pad = 9; // 7+2 (due to '::')
188 // If the '::' is in the middle...
189 } else {
190 $repeat = ':0';
191 $extra = ':';
192 $pad = 8; // 6+2 (due to '::')
193 }
194 $ip = str_replace( '::',
195 str_repeat( $repeat, $pad - substr_count( $ip, ':' ) ) . $extra,
196 $ip
197 );
198 }
199 // Remove leading zereos from each bloc as needed
200 $ip = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip );
201 return $ip;
202 }
203
204 /**
205 * Given an unsigned integer, returns an IPv6 address in octet notation
206 * @param string $ip_int IP address.
207 * @return string
208 */
209 public static function toOctet( $ip_int ) {
210 return self::hexToOctet( wfBaseConvert( $ip_int, 10, 16, 32, false ) );
211 }
212
213 /**
214 * Convert an IPv4 or IPv6 hexadecimal representation back to readable format
215 * @param string $hex number, with "v6-" prefix if it is IPv6
216 * @return string quad-dotted (IPv4) or octet notation (IPv6)
217 */
218 public static function formatHex( $hex ) {
219 if ( substr( $hex, 0, 3 ) == 'v6-' ) { // IPv6
220 return self::hexToOctet( substr( $hex, 3 ) );
221 } else { // IPv4
222 return self::hexToQuad( $hex );
223 }
224 }
225
226 /**
227 * Converts a hexadecimal number to an IPv6 address in octet notation
228 * @param string $ip_hex pure hex (no v6- prefix)
229 * @return string (of format a:b:c:d:e:f:g:h)
230 */
231 public static function hexToOctet( $ip_hex ) {
232 // Pad hex to 32 chars (128 bits)
233 $ip_hex = str_pad( strtoupper( $ip_hex ), 32, '0', STR_PAD_LEFT );
234 // Separate into 8 words
235 $ip_oct = substr( $ip_hex, 0, 4 );
236 for ( $n = 1; $n < 8; $n++ ) {
237 $ip_oct .= ':' . substr( $ip_hex, 4 * $n, 4 );
238 }
239 // NO leading zeroes
240 $ip_oct = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip_oct );
241 return $ip_oct;
242 }
243
244 /**
245 * Converts a hexadecimal number to an IPv4 address in quad-dotted notation
246 * @param string $ip_hex pure hex
247 * @return string (of format a.b.c.d)
248 */
249 public static function hexToQuad( $ip_hex ) {
250 // Pad hex to 8 chars (32 bits)
251 $ip_hex = str_pad( strtoupper( $ip_hex ), 8, '0', STR_PAD_LEFT );
252 // Separate into four quads
253 $s = '';
254 for ( $i = 0; $i < 4; $i++ ) {
255 if ( $s !== '' ) {
256 $s .= '.';
257 }
258 $s .= base_convert( substr( $ip_hex, $i * 2, 2 ), 16, 10 );
259 }
260 return $s;
261 }
262
263 /**
264 * Determine if an IP address really is an IP address, and if it is public,
265 * i.e. not RFC 1918 or similar
266 * Comes from ProxyTools.php
267 * @param string $ip
268 * @return bool
269 */
270 public static function isPublic( $ip ) {
271 if ( self::isIPv6( $ip ) ) {
272 return self::isPublic6( $ip );
273 }
274 $n = self::toUnsigned( $ip );
275 if ( !$n ) {
276 return false;
277 }
278
279 // ip2long accepts incomplete addresses, as well as some addresses
280 // followed by garbage characters. Check that it's really valid.
281 if ( $ip != long2ip( $n ) ) {
282 return false;
283 }
284
285 static $privateRanges = false;
286 if ( !$privateRanges ) {
287 $privateRanges = array(
288 array( '10.0.0.0', '10.255.255.255' ), # RFC 1918 (private)
289 array( '172.16.0.0', '172.31.255.255' ), # "
290 array( '192.168.0.0', '192.168.255.255' ), # "
291 array( '0.0.0.0', '0.255.255.255' ), # this network
292 array( '127.0.0.0', '127.255.255.255' ), # loopback
293 );
294 }
295
296 foreach ( $privateRanges as $r ) {
297 $start = self::toUnsigned( $r[0] );
298 $end = self::toUnsigned( $r[1] );
299 if ( $n >= $start && $n <= $end ) {
300 return false;
301 }
302 }
303 return true;
304 }
305
306 /**
307 * Determine if an IPv6 address really is an IP address, and if it is public,
308 * i.e. not RFC 4193 or similar
309 * @param string $ip
310 * @return bool
311 */
312 private static function isPublic6( $ip ) {
313 static $privateRanges = false;
314 if ( !$privateRanges ) {
315 $privateRanges = array(
316 array( 'fc::', 'fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' ), # RFC 4193 (local)
317 array( '0:0:0:0:0:0:0:1', '0:0:0:0:0:0:0:1' ), # loopback
318 );
319 }
320 $n = self::toHex( $ip );
321 foreach ( $privateRanges as $r ) {
322 $start = self::toHex( $r[0] );
323 $end = self::toHex( $r[1] );
324 if ( $n >= $start && $n <= $end ) {
325 return false;
326 }
327 }
328 return true;
329 }
330
331 /**
332 * Return a zero-padded upper case hexadecimal representation of an IP address.
333 *
334 * Hexadecimal addresses are used because they can easily be extended to
335 * IPv6 support. To separate the ranges, the return value from this
336 * function for an IPv6 address will be prefixed with "v6-", a non-
337 * hexadecimal string which sorts after the IPv4 addresses.
338 *
339 * @param string $ip Quad dotted/octet IP address.
340 * @return string
341 */
342 public static function toHex( $ip ) {
343 if ( self::isIPv6( $ip ) ) {
344 $n = 'v6-' . self::IPv6ToRawHex( $ip );
345 } else {
346 $n = self::toUnsigned( $ip );
347 if ( $n !== false ) {
348 $n = wfBaseConvert( $n, 10, 16, 8, false );
349 }
350 }
351 return $n;
352 }
353
354 /**
355 * Given an IPv6 address in octet notation, returns a pure hex string.
356 * @param string $ip octet ipv6 IP address.
357 * @return string pure hex (uppercase)
358 */
359 private static function IPv6ToRawHex( $ip ) {
360 $ip = self::sanitizeIP( $ip );
361 if ( !$ip ) {
362 return null;
363 }
364 $r_ip = '';
365 foreach ( explode( ':', $ip ) as $v ) {
366 $r_ip .= str_pad( $v, 4, 0, STR_PAD_LEFT );
367 }
368 return $r_ip;
369 }
370
371 /**
372 * Given an IP address in dotted-quad/octet notation, returns an unsigned integer.
373 * Like ip2long() except that it actually works and has a consistent error return value.
374 * Comes from ProxyTools.php
375 * @param string $ip Quad dotted IP address.
376 * @return mixed (string/int/false)
377 */
378 public static function toUnsigned( $ip ) {
379 if ( self::isIPv6( $ip ) ) {
380 $n = self::toUnsigned6( $ip );
381 } else {
382 $n = ip2long( $ip );
383 if ( $n < 0 ) {
384 $n += pow( 2, 32 );
385 }
386 }
387 return $n;
388 }
389
390 private static function toUnsigned6( $ip ) {
391 return wfBaseConvert( self::IPv6ToRawHex( $ip ), 16, 10 );
392 }
393
394 /**
395 * Convert a network specification in CIDR notation
396 * to an integer network and a number of bits
397 * @param string $range IP with CIDR prefix
398 * @return array(int or string, int)
399 */
400 public static function parseCIDR( $range ) {
401 if ( self::isIPv6( $range ) ) {
402 return self::parseCIDR6( $range );
403 }
404 $parts = explode( '/', $range, 2 );
405 if ( count( $parts ) != 2 ) {
406 return array( false, false );
407 }
408 list( $network, $bits ) = $parts;
409 $network = ip2long( $network );
410 if ( $network !== false && is_numeric( $bits ) && $bits >= 0 && $bits <= 32 ) {
411 if ( $bits == 0 ) {
412 $network = 0;
413 } else {
414 $network &= ~( ( 1 << ( 32 - $bits ) ) - 1);
415 }
416 # Convert to unsigned
417 if ( $network < 0 ) {
418 $network += pow( 2, 32 );
419 }
420 } else {
421 $network = false;
422 $bits = false;
423 }
424 return array( $network, $bits );
425 }
426
427 /**
428 * Given a string range in a number of formats,
429 * return the start and end of the range in hexadecimal.
430 *
431 * Formats are:
432 * 1.2.3.4/24 CIDR
433 * 1.2.3.4 - 1.2.3.5 Explicit range
434 * 1.2.3.4 Single IP
435 *
436 * 2001:0db8:85a3::7344/96 CIDR
437 * 2001:0db8:85a3::7344 - 2001:0db8:85a3::7344 Explicit range
438 * 2001:0db8:85a3::7344 Single IP
439 * @param string $range IP range
440 * @return array(string, string)
441 */
442 public static function parseRange( $range ) {
443 // CIDR notation
444 if ( strpos( $range, '/' ) !== false ) {
445 if ( self::isIPv6( $range ) ) {
446 return self::parseRange6( $range );
447 }
448 list( $network, $bits ) = self::parseCIDR( $range );
449 if ( $network === false ) {
450 $start = $end = false;
451 } else {
452 $start = sprintf( '%08X', $network );
453 $end = sprintf( '%08X', $network + pow( 2, ( 32 - $bits ) ) - 1 );
454 }
455 // Explicit range
456 } elseif ( strpos( $range, '-' ) !== false ) {
457 list( $start, $end ) = array_map( 'trim', explode( '-', $range, 2 ) );
458 if ( self::isIPv6( $start ) && self::isIPv6( $end ) ) {
459 return self::parseRange6( $range );
460 }
461 if ( self::isIPv4( $start ) && self::isIPv4( $end ) ) {
462 $start = self::toUnsigned( $start );
463 $end = self::toUnsigned( $end );
464 if ( $start > $end ) {
465 $start = $end = false;
466 } else {
467 $start = sprintf( '%08X', $start );
468 $end = sprintf( '%08X', $end );
469 }
470 } else {
471 $start = $end = false;
472 }
473 } else {
474 # Single IP
475 $start = $end = self::toHex( $range );
476 }
477 if ( $start === false || $end === false ) {
478 return array( false, false );
479 } else {
480 return array( $start, $end );
481 }
482 }
483
484 /**
485 * Convert a network specification in IPv6 CIDR notation to an
486 * integer network and a number of bits
487 * @return array(string, int)
488 */
489 private static function parseCIDR6( $range ) {
490 # Explode into <expanded IP,range>
491 $parts = explode( '/', IP::sanitizeIP( $range ), 2 );
492 if ( count( $parts ) != 2 ) {
493 return array( false, false );
494 }
495 list( $network, $bits ) = $parts;
496 $network = self::IPv6ToRawHex( $network );
497 if ( $network !== false && is_numeric( $bits ) && $bits >= 0 && $bits <= 128 ) {
498 if ( $bits == 0 ) {
499 $network = "0";
500 } else {
501 # Native 32 bit functions WONT work here!!!
502 # Convert to a padded binary number
503 $network = wfBaseConvert( $network, 16, 2, 128 );
504 # Truncate the last (128-$bits) bits and replace them with zeros
505 $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
506 # Convert back to an integer
507 $network = wfBaseConvert( $network, 2, 10 );
508 }
509 } else {
510 $network = false;
511 $bits = false;
512 }
513 return array( $network, (int)$bits );
514 }
515
516 /**
517 * Given a string range in a number of formats, return the
518 * start and end of the range in hexadecimal. For IPv6.
519 *
520 * Formats are:
521 * 2001:0db8:85a3::7344/96 CIDR
522 * 2001:0db8:85a3::7344 - 2001:0db8:85a3::7344 Explicit range
523 * 2001:0db8:85a3::7344/96 Single IP
524 * @return array(string, string)
525 */
526 private static function parseRange6( $range ) {
527 # Expand any IPv6 IP
528 $range = IP::sanitizeIP( $range );
529 // CIDR notation...
530 if ( strpos( $range, '/' ) !== false ) {
531 list( $network, $bits ) = self::parseCIDR6( $range );
532 if ( $network === false ) {
533 $start = $end = false;
534 } else {
535 $start = wfBaseConvert( $network, 10, 16, 32, false );
536 # Turn network to binary (again)
537 $end = wfBaseConvert( $network, 10, 2, 128 );
538 # Truncate the last (128-$bits) bits and replace them with ones
539 $end = str_pad( substr( $end, 0, $bits ), 128, 1, STR_PAD_RIGHT );
540 # Convert to hex
541 $end = wfBaseConvert( $end, 2, 16, 32, false );
542 # see toHex() comment
543 $start = "v6-$start";
544 $end = "v6-$end";
545 }
546 // Explicit range notation...
547 } elseif ( strpos( $range, '-' ) !== false ) {
548 list( $start, $end ) = array_map( 'trim', explode( '-', $range, 2 ) );
549 $start = self::toUnsigned6( $start );
550 $end = self::toUnsigned6( $end );
551 if ( $start > $end ) {
552 $start = $end = false;
553 } else {
554 $start = wfBaseConvert( $start, 10, 16, 32, false );
555 $end = wfBaseConvert( $end, 10, 16, 32, false );
556 }
557 # see toHex() comment
558 $start = "v6-$start";
559 $end = "v6-$end";
560 } else {
561 # Single IP
562 $start = $end = self::toHex( $range );
563 }
564 if ( $start === false || $end === false ) {
565 return array( false, false );
566 } else {
567 return array( $start, $end );
568 }
569 }
570
571 /**
572 * Determine if a given IPv4/IPv6 address is in a given CIDR network
573 * @param $addr The address to check against the given range.
574 * @param $range The range to check the given address against.
575 * @return bool Whether or not the given address is in the given range.
576 */
577 public static function isInRange( $addr, $range ) {
578 $hexIP = self::toHex( $addr );
579 list( $start, $end ) = self::parseRange( $range );
580 return ( strcmp( $hexIP, $start ) >= 0 &&
581 strcmp( $hexIP, $end ) <= 0 );
582 }
583
584 /**
585 * Convert some unusual representations of IPv4 addresses to their
586 * canonical dotted quad representation.
587 *
588 * This currently only checks a few IPV4-to-IPv6 related cases. More
589 * unusual representations may be added later.
590 *
591 * @param $addr something that might be an IP address
592 * @return valid dotted quad IPv4 address or null
593 */
594 public static function canonicalize( $addr ) {
595 if ( self::isValid( $addr ) ) {
596 return $addr;
597 }
598 // Turn mapped addresses from ::ce:ffff:1.2.3.4 to 1.2.3.4
599 if ( strpos( $addr, ':' ) !== false && strpos( $addr, '.' ) !== false ) {
600 $addr = substr( $addr, strrpos( $addr, ':' ) + 1 );
601 if ( self::isIPv4( $addr ) ) {
602 return $addr;
603 }
604 }
605 // IPv6 loopback address
606 $m = array();
607 if ( preg_match( '/^0*' . RE_IPV6_GAP . '1$/', $addr, $m ) ) {
608 return '127.0.0.1';
609 }
610 // IPv4-mapped and IPv4-compatible IPv6 addresses
611 if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . '(' . RE_IP_ADD . ')$/i', $addr, $m ) ) {
612 return $m[1];
613 }
614 if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . RE_IPV6_WORD .
615 ':' . RE_IPV6_WORD . '$/i', $addr, $m ) )
616 {
617 return long2ip( ( hexdec( $m[1] ) << 16 ) + hexdec( $m[2] ) );
618 }
619
620 return null; // give up
621 }
622 }