-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathAvroIOBinaryDecoder.php
More file actions
386 lines (342 loc) · 11.9 KB
/
Copy pathAvroIOBinaryDecoder.php
File metadata and controls
386 lines (342 loc) · 11.9 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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.
*/
namespace Apache\Avro\Datum;
// @todo Implement JSON encoding, as is required by the Avro spec.
use Apache\Avro\Avro;
use Apache\Avro\AvroException;
use Apache\Avro\AvroGMP;
use Apache\Avro\AvroIO;
use Apache\Avro\Schema\AvroArraySchema;
use Apache\Avro\Schema\AvroFixedSchema;
use Apache\Avro\Schema\AvroMapSchema;
use Apache\Avro\Schema\AvroRecordSchema;
use Apache\Avro\Schema\AvroSchema;
use Apache\Avro\Schema\AvroUnionSchema;
/**
* Decodes and reads Avro data from an AvroIO object encoded using
* Avro binary encoding.
*/
class AvroIOBinaryDecoder
{
/**
* Reads with a declared length above this many bytes are validated against
* the number of bytes actually remaining before allocating, to guard
* against an out-of-memory attack from a malicious or truncated input.
* Smaller reads skip the check to avoid per-value overhead.
*/
private const MAX_UNCHECKED_READ = 1048576; // 1 MiB
/**
* @param AvroIO $io object from which to read.
*/
public function __construct(
private AvroIO $io
) {
Avro::checkPlatform();
}
/**
* @return null
*/
public function readNull()
{
return null;
}
public function readBoolean(): bool
{
return 1 === ord($this->nextByte());
}
/**
* @param int $len count of bytes to read
*/
public function read(int $len): string
{
if ($len < 0) {
// AvroStringIO::read() accepts a negative length and moves the
// pointer backwards; reject it before delegating.
throw new AvroException("Cannot read a negative number of bytes: {$len}");
}
if ($len > self::MAX_UNCHECKED_READ) {
$remaining = $this->bytesRemaining();
if ($len > $remaining) {
throw new AvroException("Cannot read {$len} bytes, only {$remaining} remaining.");
}
}
return $this->io->read($len);
}
/**
* Number of bytes still available to read, determined by seeking to the end
* and restoring the position (which both the string and file IO
* implementations support). Used to reject a declared length or collection
* block count that exceeds the data actually available before allocating.
*/
public function bytesRemaining(): int
{
$current = $this->io->tell();
$this->io->seek(0, AvroIO::SEEK_END);
$end = $this->io->tell();
$this->io->seek($current, AvroIO::SEEK_SET);
// Clamp to 0: AvroStringIO::seek() allows seeking past EOF, which would
// otherwise yield a confusing negative "remaining" count.
return max(0, $end - $current);
}
public function readInt(): int
{
return (int) $this->readLong();
}
public function readLong(): string|int
{
$byte = ord($this->nextByte());
$bytes = [$byte];
while (0 != ($byte & 0x80)) {
// A 64-bit value uses at most 10 bytes; reject an overlong varint
// rather than reading an unbounded continuation chain and silently
// corrupting the value. Bounds both the native and GMP decode paths.
if (count($bytes) >= 10) {
throw new AvroException('Varint is too long');
}
$byte = ord($this->nextByte());
$bytes[] = $byte;
}
if (Avro::usesGmp()) {
return AvroGMP::decodeLongFromArray($bytes);
}
return self::decodeLongFromArray($bytes);
}
/**
* @param int[] $bytes array of byte ascii values
* @return int decoded value
* @internal Requires 64-bit platform
*/
public static function decodeLongFromArray(array $bytes): int
{
$b = array_shift($bytes);
$n = $b & 0x7F;
$shift = 7;
while (0 != ($b & 0x80)) {
$b = array_shift($bytes);
$n |= (($b & 0x7F) << $shift);
$shift += 7;
}
return ($n >> 1) ^ (($n >> 63) << 63) ^ -($n & 1);
}
public function readFloat(): float
{
return self::intBitsToFloat($this->read(4));
}
/**
* Performs decoding of the binary string to a float value.
*
* XXX: This is <b>not</b> endian-aware! See comments in
* {@link AvroIOBinaryEncoder::floatToIntBits()} for details.
*/
public static function intBitsToFloat(string $bits): float
{
$float = unpack('g', $bits);
return (float) $float[1];
}
public function readDouble(): float
{
return self::longBitsToDouble($this->read(8));
}
/**
* Performs decoding of the binary string to a double value.
*
* XXX: This is <b>not</b> endian-aware! See comments in
* {@link AvroIOBinaryEncoder::floatToIntBits()} for details.
*/
public static function longBitsToDouble(string $bits): float
{
$double = unpack('e', $bits);
return (float) $double[1];
}
/**
* A string is encoded as a long followed by that many bytes
* of UTF-8 encoded character data.
*/
public function readString(): string
{
return $this->readBytes();
}
public function readBytes(): string
{
return $this->read($this->readLong());
}
public function skipNull(): void
{
}
public function skipBoolean(): void
{
$this->skip(1);
}
/**
* @param int $len count of bytes to skip
* @uses AvroIO::seek()
*/
public function skip(int $len): void
{
$this->seek($len, AvroIO::SEEK_CUR);
}
public function skipInt(): void
{
$this->skipLong();
}
public function skipLong(): void
{
$b = ord($this->nextByte());
while (0 != ($b & 0x80)) {
$b = ord($this->nextByte());
}
}
public function skipFloat(): void
{
$this->skip(4);
}
public function skipDouble(): void
{
$this->skip(8);
}
public function skipString(): void
{
$this->skipBytes();
}
public function skipBytes(): void
{
$this->skip($this->readLong());
}
public function skipFixed(AvroFixedSchema $writersSchema, AvroIOBinaryDecoder $decoder): void
{
$decoder->skip($writersSchema->size());
}
public function skipEnum(AvroSchema $writersSchema, AvroIOBinaryDecoder $decoder): void
{
$decoder->skipInt();
}
public function skipUnion(AvroUnionSchema $writersSchema, AvroIOBinaryDecoder $decoder): void
{
$index = $decoder->readLong();
AvroIODatumReader::skipData($writersSchema->schemaByIndex($index), $decoder);
}
public function skipRecord(AvroRecordSchema $writersSchema, AvroIOBinaryDecoder $decoder): void
{
foreach ($writersSchema->fields() as $f) {
AvroIODatumReader::skipData($f->type(), $decoder);
}
}
public function skipArray(AvroArraySchema $writersSchema, AvroIOBinaryDecoder $decoder): void
{
$minBytes = AvroIODatumReader::collectionElementMinBytes($writersSchema->items());
$skipped = 0;
$blockCount = $decoder->readLong();
while (0 != $blockCount) {
$blockSize = null;
if ($blockCount < 0) {
if (PHP_INT_MIN == $blockCount) {
throw new AvroException('Invalid array block count');
}
$blockCount = -$blockCount;
$blockSize = $decoder->readLong();
if ($blockSize < 0) {
throw new AvroException('Invalid negative array block size');
}
}
// Bound the (normalized) count on both the sized and unsized paths so
// a negative block count cannot bypass the skip limit.
AvroIODatumReader::checkSkipCollectionCount($skipped, $blockCount, $minBytes);
$skipped += $blockCount;
if (null !== $blockSize) {
// seek() can move past EOF, so a truncated/oversized block would
// otherwise be "skipped" silently, hiding truncation. Reject a
// block size larger than the bytes actually remaining.
if ($blockSize > $decoder->bytesRemaining()) {
throw new AvroException('Array block size exceeds the remaining input');
}
if ($minBytes > 0 && $blockCount > intdiv($blockSize, $minBytes)) {
throw new AvroException('Array block size too small for the declared element count');
}
$decoder->skip($blockSize);
} else {
for ($i = 0; $i < $blockCount; $i++) {
AvroIODatumReader::skipData($writersSchema->items(), $decoder);
}
}
$blockCount = $decoder->readLong();
}
}
public function skipMap(AvroMapSchema $writersSchema, AvroIOBinaryDecoder $decoder): void
{
// Map entries always carry a >= 1 byte key, so the minimum is positive.
$minBytes = 1 + AvroIODatumReader::collectionElementMinBytes($writersSchema->values());
$skipped = 0;
$blockCount = $decoder->readLong();
while (0 != $blockCount) {
$blockSize = null;
if ($blockCount < 0) {
if (PHP_INT_MIN == $blockCount) {
throw new AvroException('Invalid map block count');
}
$blockCount = -$blockCount;
$blockSize = $decoder->readLong();
if ($blockSize < 0) {
throw new AvroException('Invalid negative map block size');
}
}
AvroIODatumReader::checkSkipCollectionCount($skipped, $blockCount, $minBytes);
$skipped += $blockCount;
if (null !== $blockSize) {
// seek() can move past EOF; reject a block size larger than the
// bytes remaining so a truncated block isn't silently skipped.
if ($blockSize > $decoder->bytesRemaining()) {
throw new AvroException('Map block size exceeds the remaining input');
}
if ($minBytes > 0 && $blockCount > intdiv($blockSize, $minBytes)) {
throw new AvroException('Map block size too small for the declared element count');
}
$decoder->skip($blockSize);
} else {
for ($i = 0; $i < $blockCount; $i++) {
$decoder->skipString();
AvroIODatumReader::skipData($writersSchema->values(), $decoder);
}
}
$blockCount = $decoder->readLong();
}
}
/**
* @throws AvroException if the next byte cannot be read.
* @return string the next byte from $this->io.
*/
private function nextByte(): string
{
return $this->read(1);
}
/**
* @uses AvroIO::seek()
*/
private function seek(int $offset, int $whence): bool
{
return $this->io->seek($offset, $whence);
}
/**
* @return int position of pointer in AvroIO instance
* @uses AvroIO::tell()
*/
private function tell(): int
{
return $this->io->tell();
}
}