-
Notifications
You must be signed in to change notification settings - Fork 32
/
ProtocolMessage.php
344 lines (314 loc) · 10.4 KB
/
ProtocolMessage.php
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
<?php
/**
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* ProtocolMessage defines the base class for all protocol buffers.
*/
namespace google\net;
/**
* The parent class of all protocol buffers.
* Subclasses are automatically generated by php protocol buffer compiler.
* Encoding methods can raise ProtocolBufferEncodeError if a value for an
* integer or long field is too large, or if any required field is not set.
* Decoding methods can raise ProtocolBufferDecodeError if they couldn't
* decode correctly, or the decoded message doesn't have all required fields.
*/
abstract class ProtocolMessage {
/**
* Serializes the message and return it as a string.
*
* @throws ProtocolBufferEncodeError If the protocol buffer is not
* initialized.
*
* @return string The serialized protocol buffer.
*/
public function serializeToString() {
$uninitialized = $this->checkInitialized();
if ($uninitialized !== null) {
throw new ProtocolBufferEncodeError(
"Not initialized: " . $uninitialized);
}
return $this->serializePartialToString();
}
/**
* Serializes a protocol buffer that might not have all of the required fields
* set.
*
* @return string The serialized protocol buffer.
*/
public function serializePartialToString() {
$enc = new Encoder();
$this->outputPartial($enc);
$res = $enc->toString();
# Continue to be a little paranoid about the correctness of the protocol
# buffer implementation.
if (mt_rand(0, 99) === 0 && $this->byteSizePartial() !== strlen($res)) {
throw new ProtocolBufferEncodeError(
"Internal bug: Encoded size doesn't match predicted");
}
return $res;
}
/**
* Fills the message with a protocol buffer parsed from the given input
* string.
*
* @param string $s The string containing a serialized protocol buffer.
*
* @throws ProtocolBufferDecodeError If the result message is not correctly
* initialized.
*/
public function parseFromString($s) {
// Reads data from the string 's'.
// Raises a ProtocolBufferDecodeError if, after successfully reading
// in the contents of 's', this protocol message is still not initialized.
$this->clear();
$this->mergeFromString($s);
}
/**
* Fills the message with a protocol buffer parsed from the given input
* string. Will not fail if the resulting protocol buffer is not fully
* initialized.
*
* @param string $s The string containing a serialized protocol buffer.
*/
public function parsePartialFromString($s) {
// Reads data from the string 's'.
// Does not enforce required fields are set.
$this->clear();
$this->mergePartialFromString($s);
}
/**
* Like parseFromString, fills the message with a protocol buffer parsed from
* the given input string.
*
* @param string $s The string containing a serialized protocol buffer.
*
* @throws ProtocolBufferDecodeError If the result message is not correctly
* initialized.
*/
public function mergeFromString($s) {
// Adds in data from the string 's'.
// Raises a ProtocolBufferDecodeError if, after successfully merging
// in the contents of 's', this protocol message is still not initialized.
$this->mergePartialFromString($s);
$uninitialized = $this->checkInitialized();
if ($uninitialized !== null) {
throw new ProtocolBufferDecodeError(
"Not initialized: " . $uninitialized);
}
}
/**
* Like parsePartialFromString, fills the message with a protocol buffer
* parsed from the given input string. Will not fail if the resulting protocol
* buffer is not fully initialized.
*
* @param string $s The string containing a serialized protocol buffer.
*/
public function mergePartialFromString($s) {
// Merges in data from the string 's'.
// Does not enforce required fields are set.
$d = new Decoder($s, 0, strlen($s));
$this->tryMerge($d);
}
/**
* Copies data from another protocol buffer into this protocol buffer.
*
* @param mixed $pb The protocol buffer to copy from.
*/
public function copyFrom($pb) {
// copy data from another protocol buffer
if ($pb === $this) {
return;
}
$this->clear();
$this->mergeFrom($pb);
}
/**
* Checks if this protocol message has all of the requried fields initialized.
*
* @return bool true if all fields are initialized, false otherwise.
*/
public function isInitialized() {
$uninitializedFields = $this->checkInitialized();
return $uninitializedFields === null;
}
public abstract function checkInitialized();
public abstract function tryMerge($decoder);
public abstract function outputPartial($encoder);
public abstract function byteSizePartial();
public abstract function clear();
public abstract function mergeFrom($proto);
public abstract function equals($proto);
public abstract function shortDebugString();
/**
* Format protocol buffer as a text string for debugging.
*/
protected static function debugFormatString($value) {
$res = "";
if (!empty($value)) {
foreach (str_split($value) as $c) {
$res .= ProtocolMessage::escapeByte($c);
}
}
return '"' . $res . '"';
}
protected static function debugFormatDouble($value) {
$val = "" . $value;
if (strpos($val, "E") !== false) {
return str_replace("E", "e", $val);
}
if (strpos($val, ".") === false) {
$val .= ".0";
}
return $val;
}
protected static function lengthVarUint64($value) {
if ($value < 0) {
throw new ProtocolBufferEncodeError("Negative value");
}
if ($value <= 0x7f) {
return 1;
} elseif ($value <= 0x3fff) {
return 2;
} elseif ($value <= 0x1fffff) {
return 3;
} elseif ($value <= 0xfffffff) {
return 4;
} elseif (bccomp($value, "34359738367") <= 0) {
# 0x7ffffffff
return 5;
} elseif (bccomp($value, "4398046511103") <= 0) {
# 0x3ffffffffff
return 6;
} elseif (bccomp($value, "562949953421311") <= 0) {
# 0x1ffffffffffff
return 7;
} elseif (bccomp($value, "72057594037927935") <= 0) {
# 0xffffffffffffff
return 8;
} elseif (bccomp($value, "9223372036854775807") <= 0) {
# 0x7fffffffffffffff
return 9;
} elseif (bccomp($value, "18446744073709551615") > 0) {
# MAXUINT64
throw new ProtocolBufferEncodeError("Value out of range: " . $value);
} else {
return 10;
}
}
protected static function lengthVarInt64($value) {
if ($value < 0) {
if (bccomp($value, bcsub(0, bcpow(2,63))) < 0) {
throw new ProtocolBufferEncodeError(
"Value out of sint64 range: " . $value);
}
$value = bcadd($value, bcpow(2, 64));
} elseif ($value > 2147483648 && bccomp($value, bcpow(2, 64)) >= 0) {
throw new ProtocolBufferEncodeError(
"Value out of sint64 range: " . $value);
}
return ProtocolMessage::lengthVarUint64($value);
}
protected static function lengthVarInt32($value) {
return ProtocolMessage::lengthVarInt64($value);
}
protected static function lengthString($len) {
return ProtocolMessage::lengthVarInt32($len) + $len;
}
protected static function debugFormatBool($b) {
if ($b === true) {
return "true";
} else if ($b === false) {
return "false";
} else {
return "???";
}
}
protected static function debugFormatFloat($value) {
return sprintf("%ff", $value);
}
protected static function debugFormatFixed32($value) {
if ($value < 0) $value = bcadd($value, bcpow(2, 32));
return ProtocolMessage::debugFormatFixed64($value);
}
protected static function debugFormatFixed64($value) {
if ($value < 0) $value = bcadd($value, bcpow(2, 64));
$res = "";
do {
$low = bcmod($value, 65536);
$value = bcdiv($value, 65536);
if ($value == 0) {
if ($low != 0) {
$res = sprintf("%x", $low) . $res;
}
} else {
$res = sprintf("%04x", $low) . $res;
}
} while ($value != 0);
if ("z" . $res === "z") {
$res = "0";
}
return sprintf("0x%s", $res);
}
protected static function debugFormatInt32($value) {
if ($value <= -2000000000 or $value >= 2000000000)
return ProtocolMessage::debugFormatFixed32($value);
return sprintf("%d", $value);
}
protected static function debugFormatInt64($value) {
if (bccomp($value, "-20000000000000") <= 0
or bccomp($value, "20000000000000")>= 0)
return ProtocolMessage::debugFormatFixed64($value);
return strval($value);
}
protected function checkProtoArray($arr) {
if (sizeof($arr) == 0) {
return;
}
// Quick approximate check that indexes of array are sane.
$keys = array_keys($arr);
if (end($keys) + 1 != sizeof($arr)) {
throw new ProtocolBufferEncodeError("Proto array should not have holes");
}
}
/**
* Checks if two integers are equal.
*
* @param int $a The first integer to compare.
* @param int $b The second integer to compare.
*
* @return bool True if the intergers are equal, false otherwise.
*/
protected static function integerEquals($a, $b) {
return ($a === $b) || (strval($a) === strval($b));
}
private static function escapeByte($c) {
# Copied from python implementation:
# For now we only escape the bare minimum to insure interoperabilty
# and redability. In the future we may want to mimick the c++ behavior
# more closely, but this will make the code a lot more messy.
if ($c == "\n") return "\\n"; # optional escape
if ($c == "\r") return "\\r"; # optional escape
if ($c == "'") return "\\'"; # optional escape
if ($c == "\"") return "\\\""; # necessary escape
if ($c == "\\") return "\\\\"; # necessary escape
if ($c < "\x20" || $c >= "\x7F") {
$o = unpack("C*", $c);
return sprintf("\\%03o", $o[1]);
}
return $c;
}
}