summaryrefslogtreecommitdiff
path: root/includes/libs/MWMessagePack.php
blob: cd9aad8fe46830c4bcd9a8c77f075b257d657125 (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
<?php
/**
 * MessagePack serializer
 *
 * MessagePack is a space-efficient binary data interchange format. This
 * class provides a pack() method that encodes native PHP values as MessagePack
 * binary strings. The implementation is derived from msgpack-php.
 *
 * Copyright (c) 2013 Ori Livneh <ori@wikimedia.org>
 * Copyright (c) 2011 OnlineCity <https://github.com/onlinecity/msgpack-php>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * @see <http://msgpack.org/>
 * @see <http://wiki.msgpack.org/display/MSGPACK/Format+specification>
 *
 * @since 1.23
 * @file
 */
class MWMessagePack {
	/** @var boolean|null Whether current system is bigendian. **/
	public static $bigendian = null;

	/**
	 * Encode a value using MessagePack
	 *
	 * This method supports null, boolean, integer, float, string and array
	 * (both indexed and associative) types. Object serialization is not
	 * supported.
	 *
	 * @param mixed $value
	 * @return string
	 * @throws InvalidArgumentException if $value is an unsupported type or too long a string
	 */
	public static function pack( $value ) {
		if ( self::$bigendian === null ) {
			self::$bigendian = pack( 'S', 1 ) === pack( 'n', 1 );
		}

		switch ( gettype( $value ) ) {
		case 'NULL':
			return "\xC0";

		case 'boolean':
			return $value ? "\xC3" : "\xC2";

		case 'double':
		case 'float':
			return self::$bigendian
				? "\xCB" . pack( 'd', $value )
				: "\xCB" . strrev( pack( 'd', $value ) );

		case 'string':
			$length = strlen( $value );
			if ( $length < 32 ) {
				return pack( 'Ca*', 0xA0 | $length, $value );
			} elseif ( $length <= 0xFFFF ) {
				return pack( 'Cna*', 0xDA, $length, $value );
			} elseif ( $length <= 0xFFFFFFFF ) {
				return pack( 'CNa*', 0xDB, $length, $value );
			}
			throw new InvalidArgumentException( __METHOD__
				. ": string too long (length: $length; max: 4294967295)" );

		case 'integer':
			if ( $value >= 0 ) {
				if ( $value <= 0x7F ) {
					// positive fixnum
					return chr( $value );
				}
				if ( $value <= 0xFF ) {
					// uint8
					return pack( 'CC', 0xCC, $value );
				}
				if ( $value <= 0xFFFF ) {
					// uint16
					return pack( 'Cn', 0xCD, $value );
				}
				if ( $value <= 0xFFFFFFFF ) {
					// uint32
					return pack( 'CN', 0xCE, $value );
				}
				if ( $value <= 0xFFFFFFFFFFFFFFFF ) {
					// uint64
					$hi = ( $value & 0xFFFFFFFF00000000 ) >> 32;
					$lo = $value & 0xFFFFFFFF;
					return self::$bigendian
						? pack( 'CNN', 0xCF, $lo, $hi )
						: pack( 'CNN', 0xCF, $hi, $lo );
				}
			} else {
				if ( $value >= -32 ) {
					// negative fixnum
					return pack( 'c', $value );
				}
				if ( $value >= -0x80 ) {
					// int8
					return pack( 'Cc', 0xD0, $value );
				}
				if ( $value >= -0x8000 ) {
					// int16
					$p = pack( 's', $value );
					return self::$bigendian
						? pack( 'Ca2', 0xD1, $p )
						: pack( 'Ca2', 0xD1, strrev( $p ) );
				}
				if ( $value >= -0x80000000 ) {
					// int32
					$p = pack( 'l', $value );
					return self::$bigendian
						? pack( 'Ca4', 0xD2, $p )
						: pack( 'Ca4', 0xD2, strrev( $p ) );
				}
				if ( $value >= -0x8000000000000000 ) {
					// int64
					// pack() does not support 64-bit ints either so pack into two 32-bits
					$p1 = pack( 'l', $value & 0xFFFFFFFF );
					$p2 = pack( 'l', ( $value >> 32 ) & 0xFFFFFFFF );
					return self::$bigendian
						? pack( 'Ca4a4', 0xD3, $p1, $p2 )
						: pack( 'Ca4a4', 0xD3, strrev( $p2 ), strrev( $p1 ) );
				}
			}
			throw new InvalidArgumentException( __METHOD__ . ": invalid integer '$value'" );

		case 'array':
			$buffer = '';
			$length = count( $value );
			if ( $length > 0xFFFFFFFF ) {
				throw new InvalidArgumentException( __METHOD__
					. ": array too long (length: $length, max: 4294967295)" );
			}

			$index = 0;
			foreach ( $value as $k => $v ) {
				if ( $index !== $k || $index === $length ) {
					break;
				} else {
					$index++;
				}
			}
			$associative = $index !== $length;

			if ( $associative ) {
				if ( $length < 16 ) {
					$buffer .= pack( 'C', 0x80 | $length );
				} elseif ( $length <= 0xFFFF ) {
					$buffer .= pack( 'Cn', 0xDE, $length );
				} else {
					$buffer .= pack( 'CN', 0xDF, $length );
				}
				foreach ( $value as $k => $v ) {
					$buffer .= self::pack( $k );
					$buffer .= self::pack( $v );
				}
			} else {
				if ( $length < 16 ) {
					$buffer .= pack( 'C', 0x90 | $length );
				} elseif ( $length <= 0xFFFF ) {
					$buffer .= pack( 'Cn', 0xDC, $length );
				} else {
					$buffer .= pack( 'CN', 0xDD, $length );
				}
				foreach ( $value as $v ) {
					$buffer .= self::pack( $v );
				}
			}
			return $buffer;

		default:
			throw new InvalidArgumentException( __METHOD__ . ': unsupported type ' . gettype( $value ) );
		}
	}
}