MWMessagePack.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * MessagePack serializer
  4. *
  5. * MessagePack is a space-efficient binary data interchange format. This
  6. * class provides a pack() method that encodes native PHP values as MessagePack
  7. * binary strings. The implementation is derived from msgpack-php.
  8. *
  9. * Copyright (c) 2013 Ori Livneh <ori@wikimedia.org>
  10. * Copyright (c) 2011 OnlineCity <https://github.com/onlinecity/msgpack-php>.
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this software and associated documentation files (the "Software"), to
  14. * deal in the Software without restriction, including without limitation the
  15. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  16. * sell copies of the Software, and to permit persons to whom the Software is
  17. * furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  27. * IN THE SOFTWARE.
  28. *
  29. * @see <http://msgpack.org/>
  30. * @see <http://wiki.msgpack.org/display/MSGPACK/Format+specification>
  31. *
  32. * @since 1.23
  33. * @file
  34. */
  35. class MWMessagePack {
  36. /** @var bool|null Whether current system is bigendian. **/
  37. public static $bigendian = null;
  38. /**
  39. * Encode a value using MessagePack
  40. *
  41. * This method supports null, boolean, integer, float, string and array
  42. * (both indexed and associative) types. Object serialization is not
  43. * supported.
  44. *
  45. * @param mixed $value
  46. * @return string
  47. * @throws InvalidArgumentException if $value is an unsupported type or too long a string
  48. */
  49. public static function pack( $value ) {
  50. if ( self::$bigendian === null ) {
  51. self::$bigendian = pack( 'S', 1 ) === pack( 'n', 1 );
  52. }
  53. switch ( gettype( $value ) ) {
  54. case 'NULL':
  55. return "\xC0";
  56. case 'boolean':
  57. return $value ? "\xC3" : "\xC2";
  58. case 'double':
  59. case 'float':
  60. return self::$bigendian
  61. ? "\xCB" . pack( 'd', $value )
  62. : "\xCB" . strrev( pack( 'd', $value ) );
  63. case 'string':
  64. $length = strlen( $value );
  65. if ( $length < 32 ) {
  66. return pack( 'Ca*', 0xA0 | $length, $value );
  67. } elseif ( $length <= 0xFFFF ) {
  68. return pack( 'Cna*', 0xDA, $length, $value );
  69. } elseif ( $length <= 0xFFFFFFFF ) {
  70. return pack( 'CNa*', 0xDB, $length, $value );
  71. }
  72. throw new InvalidArgumentException( __METHOD__
  73. . ": string too long (length: $length; max: 4294967295)" );
  74. case 'integer':
  75. if ( $value >= 0 ) {
  76. if ( $value <= 0x7F ) {
  77. // positive fixnum
  78. return chr( $value );
  79. }
  80. if ( $value <= 0xFF ) {
  81. // uint8
  82. return pack( 'CC', 0xCC, $value );
  83. }
  84. if ( $value <= 0xFFFF ) {
  85. // uint16
  86. return pack( 'Cn', 0xCD, $value );
  87. }
  88. if ( $value <= 0xFFFFFFFF ) {
  89. // uint32
  90. return pack( 'CN', 0xCE, $value );
  91. }
  92. if ( $value <= 0xFFFFFFFFFFFFFFFF ) {
  93. // uint64
  94. $hi = ( $value & 0xFFFFFFFF00000000 ) >> 32;
  95. $lo = $value & 0xFFFFFFFF;
  96. return self::$bigendian
  97. ? pack( 'CNN', 0xCF, $lo, $hi )
  98. : pack( 'CNN', 0xCF, $hi, $lo );
  99. }
  100. } else {
  101. if ( $value >= -32 ) {
  102. // negative fixnum
  103. return pack( 'c', $value );
  104. }
  105. if ( $value >= -0x80 ) {
  106. // int8
  107. return pack( 'Cc', 0xD0, $value );
  108. }
  109. if ( $value >= -0x8000 ) {
  110. // int16
  111. $p = pack( 's', $value );
  112. return self::$bigendian
  113. ? pack( 'Ca2', 0xD1, $p )
  114. : pack( 'Ca2', 0xD1, strrev( $p ) );
  115. }
  116. if ( $value >= -0x80000000 ) {
  117. // int32
  118. $p = pack( 'l', $value );
  119. return self::$bigendian
  120. ? pack( 'Ca4', 0xD2, $p )
  121. : pack( 'Ca4', 0xD2, strrev( $p ) );
  122. }
  123. if ( $value >= -0x8000000000000000 ) {
  124. // int64
  125. // pack() does not support 64-bit ints either so pack into two 32-bits
  126. $p1 = pack( 'l', $value & 0xFFFFFFFF );
  127. $p2 = pack( 'l', ( $value >> 32 ) & 0xFFFFFFFF );
  128. return self::$bigendian
  129. ? pack( 'Ca4a4', 0xD3, $p1, $p2 )
  130. : pack( 'Ca4a4', 0xD3, strrev( $p2 ), strrev( $p1 ) );
  131. }
  132. }
  133. throw new InvalidArgumentException( __METHOD__ . ": invalid integer '$value'" );
  134. case 'array':
  135. $buffer = '';
  136. $length = count( $value );
  137. if ( $length > 0xFFFFFFFF ) {
  138. throw new InvalidArgumentException( __METHOD__
  139. . ": array too long (length: $length, max: 4294967295)" );
  140. }
  141. $index = 0;
  142. foreach ( $value as $k => $v ) {
  143. if ( $index !== $k || $index === $length ) {
  144. break;
  145. } else {
  146. $index++;
  147. }
  148. }
  149. $associative = $index !== $length;
  150. if ( $associative ) {
  151. if ( $length < 16 ) {
  152. $buffer .= pack( 'C', 0x80 | $length );
  153. } elseif ( $length <= 0xFFFF ) {
  154. $buffer .= pack( 'Cn', 0xDE, $length );
  155. } else {
  156. $buffer .= pack( 'CN', 0xDF, $length );
  157. }
  158. foreach ( $value as $k => $v ) {
  159. $buffer .= self::pack( $k );
  160. $buffer .= self::pack( $v );
  161. }
  162. } else {
  163. if ( $length < 16 ) {
  164. $buffer .= pack( 'C', 0x90 | $length );
  165. } elseif ( $length <= 0xFFFF ) {
  166. $buffer .= pack( 'Cn', 0xDC, $length );
  167. } else {
  168. $buffer .= pack( 'CN', 0xDD, $length );
  169. }
  170. foreach ( $value as $v ) {
  171. $buffer .= self::pack( $v );
  172. }
  173. }
  174. return $buffer;
  175. default:
  176. throw new InvalidArgumentException( __METHOD__ . ': unsupported type ' . gettype( $value ) );
  177. }
  178. }
  179. }