Cheetah
Transform.class.php
Go to the documentation of this file.
1 <?php
49 final class Transform
50 {
53  const BIG_ENDIAN_ORDER = 2;
54 
58  private function __construct() {}
59 
65  public static function isLittleEndian()
66  {
67  return self::fromInt32("\x01\x00\x00\x00") == 1;
68  }
69 
75  public static function isBigEndian()
76  {
77  return self::fromInt32("\x00\x00\x00\x01") == 1;
78  }
79 
86  public static function toInt64LE($value)
87  {
88  return pack("V*", $value & 0xffffffff, $value / (0xffffffff+1));
89  }
90 
100  public static function fromInt64LE($value)
101  {
102  list(, $lolo, $lohi, $hilo, $hihi) = unpack("v*", $value);
103  return ($hihi * (0xffff+1) + $hilo) * (0xffffffff+1) +
104  ($lohi * (0xffff+1) + $lolo);
105  }
106 
113  public static function toInt64BE($value)
114  {
115  return pack("N*", $value / (0xffffffff+1), $value & 0xffffffff);
116  }
117 
127  public static function fromInt64BE($value)
128  {
129  list(, $hihi, $hilo, $lohi, $lolo) = unpack("n*", $value);
130  return ($hihi * (0xffff+1) + $hilo) * (0xffffffff+1) +
131  ($lohi * (0xffff+1) + $lolo);
132  }
133 
140  public static function toInt32($value)
141  {
142  return pack("l*", $value);
143  }
144 
151  public static function fromInt32($value)
152  {
153  list(, $int) = unpack("l*", $value);
154  return $int;
155  }
156 
163  public static function toInt32LE($value)
164  {
165  if (self::isBigEndian())
166  return strrev(self::toInt32($value));
167  else
168  return self::toInt32($value);
169  }
170 
177  public static function fromInt32LE($value)
178  {
179  if (self::isBigEndian())
180  return self::fromInt32(strrev($value));
181  else
182  return self::fromInt32($value);
183  }
184 
191  public static function toInt32BE($value)
192  {
193  if (self::isBigEndian())
194  return self::toInt32($value);
195  else
196  return strrev(self::toInt32($value));
197  }
198 
205  public static function fromInt32BE($value)
206  {
207  if (self::isBigEndian())
208  return self::fromInt32($value);
209  else
210  return self::fromInt32(strrev($value));
211  }
212 
219  public static function toUInt32LE($value)
220  {
221  return pack("V*", $value);
222  }
223 
230  public static function fromUInt32LE($value)
231  {
232  if (PHP_INT_SIZE < 8) {
233  list(, $lo, $hi) = unpack("v*", $value);
234  return $hi * (0xffff+1) + $lo; // eq $hi << 16 | $lo
235  } else {
236  list(, $int) = unpack("V*", $value);
237  return $int;
238  }
239  }
240 
247  public static function toUInt32BE($value)
248  {
249  return pack("N*", $value);
250  }
251 
258  public static function fromUInt32BE($value)
259  {
260  if (PHP_INT_SIZE < 8) {
261  list(, $hi, $lo) = unpack("n*", $value);
262  return $hi * (0xffff+1) + $lo; // eq $hi << 16 | $lo
263  } else {
264  list(, $int) = unpack("N*", $value);
265  return $int;
266  }
267  }
268 
275  public static function toInt16($value)
276  {
277  return pack("s*", $value);
278  }
279 
286  public static function fromInt16($value)
287  {
288  list(, $int) = unpack("s*", $value);
289  return $int;
290  }
291 
298  public static function toInt16LE($value)
299  {
300  if (self::isBigEndian())
301  return strrev(self::toInt16($value));
302  else
303  return self::toInt16($value);
304  }
305 
312  public static function fromInt16LE($value)
313  {
314  if (self::isBigEndian())
315  return self::fromInt16(strrev($value));
316  else
317  return self::fromInt16($value);
318  }
319 
326  public static function toInt16BE($value)
327  {
328  if (self::isBigEndian())
329  return self::toInt16($value);
330  else
331  return strrev(self::toInt16($value));
332  }
333 
340  public static function fromInt16BE($value)
341  {
342  if (self::isBigEndian())
343  return self::fromInt16($value);
344  else
345  return self::fromInt16(strrev($value));
346  }
347 
355  private static function fromUInt16($value, $order = self::MACHINE_ENDIAN_ORDER)
356  {
357  list(, $int) = unpack
358  (($order == self::BIG_ENDIAN_ORDER ? "n" :
359  ($order == self::LITTLE_ENDIAN_ORDER ? "v" : "S")) . "*", $value);
360  return $int;
361  }
362 
369  public static function toUInt16LE($value)
370  {
371  return pack("v*", $value);
372  }
373 
380  public static function fromUInt16LE($value)
381  {
382  return self::fromUInt16($value, self::LITTLE_ENDIAN_ORDER);
383  }
384 
391  public static function toUInt16BE($value)
392  {
393  return pack("n*", $value);
394  }
395 
402  public static function fromUInt16BE($value)
403  {
404  return self::fromUInt16($value, self::BIG_ENDIAN_ORDER);
405  }
406 
413  public static function toInt8($value)
414  {
415  return pack("c*", $value);
416  }
417 
424  public static function fromInt8($value)
425  {
426  list(, $int) = unpack("c*", $value);
427  return $int;
428  }
429 
436  public static function toUInt8($value)
437  {
438  return pack("C*", $value);
439  }
440 
447  public static function fromUInt8($value)
448  {
449  list(, $int) = unpack("C*", $value);
450  return $int;
451  }
452 
459  public static function toFloat($value)
460  {
461  return pack("f*", $value);
462  }
463 
470  public static function fromFloat($value)
471  {
472  list(, $float) = unpack("f*", $value);
473  return $float;
474  }
475 
482  public static function toFloatLE($value)
483  {
484  if (self::isBigEndian())
485  return strrev(self::toFloat($value));
486  else
487  return self::toFloat($value);
488  }
489 
496  public static function fromFloatLE($value)
497  {
498  if (self::isBigEndian())
499  return self::fromFloat(strrev($value));
500  else
501  return self::fromFloat($value);
502  }
503 
510  public static function toFloatBE($value)
511  {
512  if (self::isBigEndian())
513  return self::toFloat($value);
514  else
515  return strrev(self::toFloat($value));
516  }
517 
524  public static function fromFloatBE($value)
525  {
526  if (self::isBigEndian())
527  return self::fromFloat($value);
528  else
529  return self::fromFloat(strrev($value));
530  }
531 
542  public static function toString8($value, $length = false, $padding = "\0")
543  {
544  if ($length === false)
545  $length = strlen($value);
546  if ($length < ($tmp = strlen($value)))
547  $length = $tmp + $length;
548  return str_pad($value, $length, $padding);
549  }
550 
557  public static function fromString8($value)
558  {
559  return rtrim($value, "\0");
560  }
561 
578  public static function toString16
579  ($value, $order = false, $length = false, $padding = "\0")
580  {
581  if ($length === false)
582  $length = (int)(strlen($value) / 2);
583  if ($length < ($tmp = strlen($value) / 2))
584  $length = $tmp + $length;
585  if ($order == self::BIG_ENDIAN_ORDER &&
586  !(ord($value[0]) == 0xfe && ord($value[1]) == 0xff)) {
587  $value = 0xfeff . $value;
588  $length++;
589  }
590  if ($order == self::LITTLE_ENDIAN_ORDER &&
591  !(ord($value[0]) == 0xff && ord($value[1]) == 0xfe)) {
592  $value = 0xfffe . $value;
593  $length++;
594  }
595  return str_pad($value, $length * 2, $padding);
596  }
597 
610  public static function fromString16
611  ($value, &$order = false, $trimOrder = false)
612  {
613  if (strlen($value) < 2)
614  return "";
615 
616  if (ord($value[0]) == 0xfe && ord($value[1]) == 0xff) {
617  $order = self::BIG_ENDIAN_ORDER;
618  if ($trimOrder)
619  $value = substr($value, 2);
620  }
621  if (ord($value[0]) == 0xff && ord($value[1]) == 0xfe) {
622  $order = self::LITTLE_ENDIAN_ORDER;
623  if ($trimOrder)
624  $value = substr($value, 2);
625  }
626 
627  return substr($value, -2) == "\0\0" ? substr($value, 0, -2) : $value;
628  }
629 
636  public static function toHHex($value)
637  {
638  return pack("H*", $value);
639  }
640 
647  public static function fromHHex($value)
648  {
649  list($hex) = unpack("H*0", $value);
650  return $hex;
651  }
652 
659  public static function toLHex($value)
660  {
661  return pack("h*", $value);
662  }
663 
670  public static function fromLHex($value)
671  {
672  list($hex) = unpack("h*0", $value);
673  return $hex;
674  }
675 
683  public static function toGUID($value)
684  {
685  $string = ""; $C = preg_split("/-/", $value);
686  return pack
687  ("V1v2N2", hexdec($C[0]), hexdec($C[1]), hexdec($C[2]),
688  hexdec($C[3] . substr($C[4], 0, 4)), hexdec(substr($C[4], 4)));
689  }
690 
698  public static function fromGUID($value)
699  {
700  $C = @unpack("V1V/v2v/N2N", $value);
701  list($hex) = @unpack("H*0", pack
702  ("NnnNN", $C["V"], $C["v1"], $C["v2"], $C["N1"], $C["N2"]));
703 
704  /* Fixes a bug in PHP versions earlier than Jan 25 2006 */
705  if (implode("", unpack("H*", pack("H*", "a"))) == "a00")
706  $hex = substr($hex, 0, -1);
707 
708  return preg_replace
709  ("/^(.{8})(.{4})(.{4})(.{4})/", "\\1-\\2-\\3-\\4-", $hex);
710  }
711 }
Transform\toUInt16BE
static toUInt16BE($value)
Definition: Transform.class.php:391
Transform\fromInt16
static fromInt16($value)
Definition: Transform.class.php:286
Transform\fromInt32LE
static fromInt32LE($value)
Definition: Transform.class.php:177
Transform\toUInt32BE
static toUInt32BE($value)
Definition: Transform.class.php:247
Transform\toString8
static toString8($value, $length=false, $padding="\0")
Definition: Transform.class.php:542
Transform\fromLHex
static fromLHex($value)
Definition: Transform.class.php:670
Transform\fromInt32
static fromInt32($value)
Definition: Transform.class.php:151
Transform\toString16
static toString16($value, $order=false, $length=false, $padding="\0")
Definition: Transform.class.php:579
Transform\fromUInt32BE
static fromUInt32BE($value)
Definition: Transform.class.php:258
Transform\fromInt8
static fromInt8($value)
Definition: Transform.class.php:424
Transform\toUInt32LE
static toUInt32LE($value)
Definition: Transform.class.php:219
Transform\fromFloatLE
static fromFloatLE($value)
Definition: Transform.class.php:496
Transform\fromInt16LE
static fromInt16LE($value)
Definition: Transform.class.php:312
php
Transform\toFloat
static toFloat($value)
Definition: Transform.class.php:459
Transform\fromHHex
static fromHHex($value)
Definition: Transform.class.php:647
Transform\toInt16
static toInt16($value)
Definition: Transform.class.php:275
Transform\fromInt64BE
static fromInt64BE($value)
Definition: Transform.class.php:127
Transform\toInt32
static toInt32($value)
Definition: Transform.class.php:140
Transform\fromUInt16LE
static fromUInt16LE($value)
Definition: Transform.class.php:380
Transform\toInt16LE
static toInt16LE($value)
Definition: Transform.class.php:298
Transform\isLittleEndian
static isLittleEndian()
Definition: Transform.class.php:65
Transform\toFloatBE
static toFloatBE($value)
Definition: Transform.class.php:510
Transform\toFloatLE
static toFloatLE($value)
Definition: Transform.class.php:482
Transform\fromString16
static fromString16($value, &$order=false, $trimOrder=false)
Definition: Transform.class.php:611
Transform\toHHex
static toHHex($value)
Definition: Transform.class.php:636
Transform\toUInt16LE
static toUInt16LE($value)
Definition: Transform.class.php:369
Transform\fromInt64LE
static fromInt64LE($value)
Definition: Transform.class.php:100
Transform\fromInt32BE
static fromInt32BE($value)
Definition: Transform.class.php:205
Transform\fromUInt8
static fromUInt8($value)
Definition: Transform.class.php:447
Transform\fromUInt32LE
static fromUInt32LE($value)
Definition: Transform.class.php:230
Transform\toInt64LE
static toInt64LE($value)
Definition: Transform.class.php:86
Transform\fromFloatBE
static fromFloatBE($value)
Definition: Transform.class.php:524
Transform\fromGUID
static fromGUID($value)
Definition: Transform.class.php:698
Transform\fromString8
static fromString8($value)
Definition: Transform.class.php:557
Transform\fromUInt16BE
static fromUInt16BE($value)
Definition: Transform.class.php:402
Transform\MACHINE_ENDIAN_ORDER
const MACHINE_ENDIAN_ORDER
Definition: Transform.class.php:51
Transform\fromFloat
static fromFloat($value)
Definition: Transform.class.php:470
Transform\fromInt16BE
static fromInt16BE($value)
Definition: Transform.class.php:340
Transform\toLHex
static toLHex($value)
Definition: Transform.class.php:659
Transform\toInt8
static toInt8($value)
Definition: Transform.class.php:413
Transform\toGUID
static toGUID($value)
Definition: Transform.class.php:683
Transform
Definition: Transform.class.php:50
Transform\toUInt8
static toUInt8($value)
Definition: Transform.class.php:436
Transform\toInt16BE
static toInt16BE($value)
Definition: Transform.class.php:326
Transform\BIG_ENDIAN_ORDER
const BIG_ENDIAN_ORDER
Definition: Transform.class.php:53
Transform\toInt64BE
static toInt64BE($value)
Definition: Transform.class.php:113
Transform\toInt32LE
static toInt32LE($value)
Definition: Transform.class.php:163
Transform\LITTLE_ENDIAN_ORDER
const LITTLE_ENDIAN_ORDER
Definition: Transform.class.php:52
Transform\isBigEndian
static isBigEndian()
Definition: Transform.class.php:75
Transform\toInt32BE
static toInt32BE($value)
Definition: Transform.class.php:191