001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.marshall.msgpack;
018
019import static org.apache.juneau.commons.utils.IoUtils.*;
020import static org.apache.juneau.commons.utils.Shorts.*;
021import static org.apache.juneau.marshall.msgpack.DataType.*;
022
023import java.io.*;
024
025import org.apache.juneau.marshall.parser.*;
026
027/**
028 * Specialized input stream for parsing MessagePack streams.
029 *
030 * <h5 class='section'>Notes:</h5><ul>
031 *    <li class='note'>
032 *       This class is not intended for external use.
033 * </ul>
034 *
035 * <h5 class='section'>See Also:</h5><ul>
036 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/MessagePackSupport">MessagePack</a>
037
038 * </ul>
039 */
040public class MsgPackInputStream extends ParserInputStream {
041
042   // Data type quick-lookup table.
043   // @formatter:off
044   private static final DataType[] TYPES = {
045      /*0x0?*/ INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,
046      /*0x1?*/ INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,
047      /*0x2?*/ INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,
048      /*0x3?*/ INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,
049      /*0x4?*/ INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,
050      /*0x5?*/ INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,
051      /*0x6?*/ INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,
052      /*0x7?*/ INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,
053      /*0x8?*/ MAP,MAP,MAP,MAP,MAP,MAP,MAP,MAP,MAP,MAP,MAP,MAP,MAP,MAP,MAP,MAP,
054      /*0x9?*/ ARRAY,ARRAY,ARRAY,ARRAY,ARRAY,ARRAY,ARRAY,ARRAY,ARRAY,ARRAY,ARRAY,ARRAY,ARRAY,ARRAY,ARRAY,ARRAY,
055      /*0xA?*/ STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,
056      /*0xB?*/ STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,STRING,
057      /*0xC?*/ NULL, INVALID, BOOLEAN, BOOLEAN, BIN, BIN, BIN, EXT, EXT, EXT, FLOAT, DOUBLE, INT, INT, LONG, LONG,
058      /*0xD?*/ INT, INT, INT, LONG, EXT, EXT, EXT, EXT, EXT, STRING, STRING, STRING, ARRAY, ARRAY, MAP, MAP,
059      /*0xE?*/ INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,
060      /*0xF?*/ INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT,INT
061   };
062   // @formatter:on
063
064   /** Default cap (16 MiB) for wire-declared lengths, mirroring {@code BsonInputStream}. */
065   static final int DEFAULT_MAX_LENGTH = 16 * 1024 * 1024;
066
067   private long length;
068   private int lastByte;
069   private int extType;
070
071   int pos2 = 0;
072
073   /**
074    * Constructor.
075    *
076    * @param pipe The parser input.
077    * @throws IOException Thrown by underlying stream.
078    */
079   protected MsgPackInputStream(ParserPipe pipe) throws IOException {
080      super(pipe);
081   }
082
083   /**
084    * Read one byte from the stream.
085    */
086   private int readUInt1() throws IOException {
087      return read();
088   }
089
090   /**
091    * Read two bytes from the stream.
092    */
093   private int readUInt2() throws IOException {
094      return (read() << 8) | read();
095   }
096
097   /**
098    * Read four bytes from the stream.
099    */
100   private long readUInt4() throws IOException {
101      long l = read();
102      l <<= 8;
103      l |= read();
104      l <<= 8;
105      l |= read();
106      l <<= 8;
107      l |= read();
108      return l;
109   }
110
111   /**
112    * Return the extended-format type.
113    * Currently not used.
114    */
115   int getExtType() { return extType; }
116
117   /**
118    * Returns whether the last-read {@link DataType#LONG} token was an unsigned 64-bit integer (uint64, {@code 0xCF}).
119    *
120    * <p>
121    * Only meaningful immediately after a {@link DataType#LONG} tag has been read via {@link #readDataType()}.
122    */
123   boolean isUint64() {
124      return lastByte == UINT64;
125   }
126
127   /**
128    * Read a binary field from the stream.
129    */
130   byte[] readBinary() throws IOException {
131      // checkLength bounds the declared length against the configured maximum (always <= Integer.MAX_VALUE),
132      // so a 32-bit MessagePack length in [2^31, 2^32-1] is rejected here rather than truncating to a
133      // negative int and allocating the wrong size.
134      var b = new byte[checkLength(length, "binary")];
135      var bytesRead = read(b);
136      if (bytesRead != b.length)
137         throw ioex("Expected to read %s bytes but only read %s", b.length, bytesRead);
138      return b;
139   }
140
141   /**
142    * Read a boolean from the stream.
143    */
144   boolean readBoolean() {
145      return lastByte == TRUE;
146   }
147
148   /**
149    * Reads the data type flag from the stream.
150    *
151    * <p>
152    * This is the byte that indicates what kind of data follows.
153    */
154   @SuppressWarnings({
155      "java:S3776" // Cognitive complexity acceptable for MsgPack data type reading
156   })
157   DataType readDataType() throws IOException {
158      int i = read();
159      if (i == -1)
160         throw ioex("Unexpected end of file found at position %s", pos2);
161      DataType currentDataType = TYPES[i];
162      switch (currentDataType) {
163         case NULL, FLOAT: {
164            length = 4;
165            break;
166         }
167         case DOUBLE: {
168            length = 8;
169            break;
170         }
171         case BOOLEAN: {
172            lastByte = i;
173            break;
174         }
175         case INT: {
176            // positive fixnum stores 7-bit positive integer
177            // +--------+
178            // |0XXXXXXX|
179            // +--------+
180            //
181            // negative fixnum stores 5-bit negative integer
182            // +--------+
183            // |111YYYYY|
184            // +--------+
185            //
186            // * 0XXXXXXX is 8-bit unsigned integer
187            // * 111YYYYY is 8-bit signed integer
188            //
189            // uint 8 stores a 8-bit unsigned integer
190            // +--------+--------+
191            // |  0xcc  |ZZZZZZZZ|
192            // +--------+--------+
193            //
194            // uint 16 stores a 16-bit big-endian unsigned integer
195            // +--------+--------+--------+
196            // |  0xcd  |ZZZZZZZZ|ZZZZZZZZ|
197            // +--------+--------+--------+
198            //
199            // uint 32 stores a 32-bit big-endian unsigned integer
200            // +--------+--------+--------+--------+--------+
201            // |  0xce  |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
202            // +--------+--------+--------+--------+--------+
203            //
204            // uint 64 stores a 64-bit big-endian unsigned integer
205            // +--------+--------+--------+--------+--------+--------+--------+--------+--------+
206            // |  0xcf  |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
207            // +--------+--------+--------+--------+--------+--------+--------+--------+--------+
208            //
209            // int 8 stores a 8-bit signed integer
210            // +--------+--------+
211            // |  0xd0  |ZZZZZZZZ|
212            // +--------+--------+
213            //
214            // int 16 stores a 16-bit big-endian signed integer
215            // +--------+--------+--------+
216            // |  0xd1  |ZZZZZZZZ|ZZZZZZZZ|
217            // +--------+--------+--------+
218            //
219            // int 32 stores a 32-bit big-endian signed integer
220            // +--------+--------+--------+--------+--------+
221            // |  0xd2  |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
222            // +--------+--------+--------+--------+--------+
223            //
224            // int 64 stores a 64-bit big-endian signed integer
225            // +--------+--------+--------+--------+--------+--------+--------+--------+--------+
226            // |  0xd3  |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
227            // +--------+--------+--------+--------+--------+--------+--------+--------+--------+
228            lastByte = i;
229            if (i <= POSFIXINT_U)
230               length = 0;
231            else if (i >= NEGFIXINT_L)
232               length = -1;
233            else if (i == INT8 || i == UINT8)
234               length = 1;
235            else if (i == INT16 || i == UINT16)
236               length = 2;
237            else if (i == INT32)
238               length = 4;
239            else
240               length = 0;
241            break;
242         }
243         case LONG: {
244            // Remember the tag byte so isUint64() can distinguish an unsigned 64-bit integer from a
245            // signed INT64 / unsigned UINT32 on the LONG read path (used by the uint64-widening logic).
246            lastByte = i;
247            if (i == UINT32)
248               length = 4;
249            else if (i == INT64 || i == UINT64)
250               length = 8;
251            else
252               length = 0;
253            break;
254         }
255         case STRING: {
256            // fixstr stores a byte array whose length is up to 31 bytes:
257            // +--------+========+
258            // |101XXXXX|  data  |
259            // +--------+========+
260            //
261            // str 8 stores a byte array whose length is up to (2^8)-1 bytes:
262            // +--------+--------+========+
263            // |  0xd9  |YYYYYYYY|  data  |
264            // +--------+--------+========+
265            //
266            // str 16 stores a byte array whose length is up to (2^16)-1 bytes:
267            // +--------+--------+--------+========+
268            // |  0xda  |ZZZZZZZZ|ZZZZZZZZ|  data  |
269            // +--------+--------+--------+========+
270            //
271            // str 32 stores a byte array whose length is up to (2^32)-1 bytes:
272            // +--------+--------+--------+--------+--------+========+
273            // |  0xdb  |AAAAAAAA|AAAAAAAA|AAAAAAAA|AAAAAAAA|  data  |
274            // +--------+--------+--------+--------+--------+========+
275            //
276            // where
277            // * XXXXX is a 5-bit unsigned integer which represents N
278            // * YYYYYYYY is a 8-bit unsigned integer which represents N
279            // * ZZZZZZZZ_ZZZZZZZZ is a 16-bit big-endian unsigned integer which represents N
280            // * AAAAAAAA_AAAAAAAA_AAAAAAAA_AAAAAAAA is a 32-bit big-endian unsigned integer which represents N
281            // * N is the length of data
282            if (i <= FIXSTR_U)
283               length = i & 0x1F;
284            else if (i == STR8)
285               length = readUInt1();
286            else if (i == STR16)
287               length = readUInt2();
288            else
289               length = readUInt4();
290            break;
291         }
292         case ARRAY: {
293            // fixarray stores an array whose length is up to 15 elements:
294            // +--------+~~~~~~~~~~~~~~~~~+
295            // |1001XXXX|    N objects    |
296            // +--------+~~~~~~~~~~~~~~~~~+
297            //
298            // array 16 stores an array whose length is up to (2^16)-1 elements:
299            // +--------+--------+--------+~~~~~~~~~~~~~~~~~+
300            // |  0xdc  |YYYYYYYY|YYYYYYYY|    N objects    |
301            // +--------+--------+--------+~~~~~~~~~~~~~~~~~+
302            //
303            // array 32 stores an array whose length is up to (2^32)-1 elements:
304            // +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
305            // |  0xdd  |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|    N objects    |
306            // +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
307            //
308            // where
309            // * XXXX is a 4-bit unsigned integer which represents N
310            // * YYYYYYYY_YYYYYYYY is a 16-bit big-endian unsigned integer which represents N
311            // * ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ is a 32-bit big-endian unsigned integer which represents N
312            //     N is the size of a array
313            if (i <= FIXARRAY_U)
314               length = i & 0x0F;
315            else if (i == ARRAY16)
316               length = readUInt2();
317            else
318               length = readUInt4();
319            break;
320         }
321         case BIN: {
322            // bin 8 stores a byte array whose length is up to (2^8)-1 bytes:
323            // +--------+--------+========+
324            // |  0xc4  |XXXXXXXX|  data  |
325            // +--------+--------+========+
326            //
327            // bin 16 stores a byte array whose length is up to (2^16)-1 bytes:
328            // +--------+--------+--------+========+
329            // |  0xc5  |YYYYYYYY|YYYYYYYY|  data  |
330            // +--------+--------+--------+========+
331            //
332            // bin 32 stores a byte array whose length is up to (2^32)-1 bytes:
333            // +--------+--------+--------+--------+--------+========+
334            // |  0xc6  |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|  data  |
335            // +--------+--------+--------+--------+--------+========+
336            //
337            // where
338            // * XXXXXXXX is a 8-bit unsigned integer which represents N
339            // * YYYYYYYY_YYYYYYYY is a 16-bit big-endian unsigned integer which represents N
340            // * ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ is a 32-bit big-endian unsigned integer which represents N
341            // * N is the length of data
342            if (i == BIN8)
343               length = readUInt1();
344            else if (i == BIN16)
345               length = readUInt2();
346            else
347               length = readUInt4();
348            break;
349         }
350         case EXT: {
351            // fixext 1 stores an integer and a byte array whose length is 1 byte
352            // +--------+--------+--------+
353            // |  0xd4  |  type  |  data  |
354            // +--------+--------+--------+
355            //
356            // fixext 2 stores an integer and a byte array whose length is 2 bytes
357            // +--------+--------+--------+--------+
358            // |  0xd5  |  type  |       data      |
359            // +--------+--------+--------+--------+
360            //
361            // fixext 4 stores an integer and a byte array whose length is 4 bytes
362            // +--------+--------+--------+--------+--------+--------+
363            // |  0xd6  |  type  |                data               |
364            // +--------+--------+--------+--------+--------+--------+
365            //
366            // fixext 8 stores an integer and a byte array whose length is 8 bytes
367            // +--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
368            // |  0xd7  |  type  |                                  data                                 |
369            // +--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
370            //
371            // fixext 16 stores an integer and a byte array whose length is 16 bytes
372            // +--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
373            // |  0xd8  |  type  |                                  data
374            // +--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
375            // +--------+--------+--------+--------+--------+--------+--------+--------+
376            //                               data (cont.)                              |
377            // +--------+--------+--------+--------+--------+--------+--------+--------+
378            //
379            // ext 8 stores an integer and a byte array whose length is up to (2^8)-1 bytes:
380            // +--------+--------+--------+========+
381            // |  0xc7  |XXXXXXXX|  type  |  data  |
382            // +--------+--------+--------+========+
383            //
384            // ext 16 stores an integer and a byte array whose length is up to (2^16)-1 bytes:
385            // +--------+--------+--------+--------+========+
386            // |  0xc8  |YYYYYYYY|YYYYYYYY|  type  |  data  |
387            // +--------+--------+--------+--------+========+
388            //
389            // ext 32 stores an integer and a byte array whose length is up to (2^32)-1 bytes:
390            // +--------+--------+--------+--------+--------+--------+========+
391            // |  0xc9  |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|  type  |  data  |
392            // +--------+--------+--------+--------+--------+--------+========+
393            //
394            // where
395            // * XXXXXXXX is a 8-bit unsigned integer which represents N
396            // * YYYYYYYY_YYYYYYYY is a 16-bit big-endian unsigned integer which represents N
397            // * ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ is a big-endian 32-bit unsigned integer which represents N
398            // * N is a length of data
399            // * type is a signed 8-bit signed integer
400            // * type < 0 is reserved for future extension including 2-byte type information
401            if (i == FIXEXT1)
402               length = 1;
403            else if (i == FIXEXT2)
404               length = 2;
405            else if (i == FIXEXT4)
406               length = 4;
407            else if (i == FIXEXT8)
408               length = 8;
409            else if (i == FIXEXT16)
410               length = 16;
411            else if (i == EXT8)
412               length = readUInt1();
413            else if (i == EXT16)
414               length = readUInt2();
415            else if (i == EXT32)
416               length = readUInt4();
417            extType = read();
418
419            break;
420         }
421         case MAP: {
422            // fixmap stores a map whose length is up to 15 elements
423            // +--------+~~~~~~~~~~~~~~~~~+
424            // |1000XXXX|   N*2 objects   |
425            // +--------+~~~~~~~~~~~~~~~~~+
426            //
427            // map 16 stores a map whose length is up to (2^16)-1 elements
428            // +--------+--------+--------+~~~~~~~~~~~~~~~~~+
429            // |  0xde  |YYYYYYYY|YYYYYYYY|   N*2 objects   |
430            // +--------+--------+--------+~~~~~~~~~~~~~~~~~+
431            //
432            // map 32 stores a map whose length is up to (2^32)-1 elements
433            // +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
434            // |  0xdf  |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|   N*2 objects   |
435            // +--------+--------+--------+--------+--------+~~~~~~~~~~~~~~~~~+
436            //
437            // where
438            // * XXXX is a 4-bit unsigned integer which represents N
439            // * YYYYYYYY_YYYYYYYY is a 16-bit big-endian unsigned integer which represents N
440            // * ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ is a 32-bit big-endian unsigned integer which represents N
441            // * N is the size of a map
442            // * odd elements in objects are keys of a map
443            // * the next element of a key is its associated value
444            if (i <= FIXMAP_U)
445               length = i & 0x0F;
446            else if (i == MAP16)
447               length = readUInt2();
448            else
449               length = readUInt4();
450            break;
451         }
452         default:
453            throw ioex("Invalid flag 0xC1 detected in stream.");
454      }
455      return currentDataType;
456   }
457
458   /**
459    * Read a double from the stream.
460    */
461   double readDouble() throws IOException {
462      return Double.longBitsToDouble(readLong());
463   }
464
465   /**
466    * Read a float from the stream.
467    */
468   float readFloat() throws IOException {
469      return Float.intBitsToFloat(readInt());
470   }
471
472   /**
473    * Read an integer from the stream.
474    */
475   int readInt() throws IOException {
476      if (length == 0)
477         return lastByte;
478      if (length == -1)
479         // NEGFIXINT path: lastByte is the negfixint byte itself (0xE0..0xFF, representing -32..-1).
480         // Sign-extend through (byte) so the negative value survives the int widen; without this guard
481         // the fallthrough below would consume 4 extra bytes from the stream and corrupt subsequent reads.
482         return (byte) lastByte;
483      if (length == 1) {
484         // INT8 (0xD0) is signed; UINT8 (0xCC) is unsigned.  Sign-extend only for INT8 so that
485         // negative values round-trip correctly (the serializer routes negatives through INT8/INT16
486         // per the MsgPack int-format-family spec).
487         var v = read();
488         return (lastByte == INT8) ? (byte) v : v;
489      }
490      if (length == 2) {
491         // INT16 (0xD1) is signed; UINT16 (0xCD) is unsigned.  Without the sign-extension here, a
492         // negative int16 value such as 0xD1 0xFE 0x54 (= -428) was being read back as 65108.
493         var v = (read() << 8) | read();
494         return (lastByte == INT16) ? (short) v : v;
495      }
496      int i = read();
497      i <<= 8;
498      i |= read();
499      i <<= 8;
500      i |= read();
501      i <<= 8;
502      i |= read();
503      return i;
504   }
505
506   /**
507    * Returns the length value for the field.
508    *
509    * <p>
510    * For ints/floats/bins/strings, this is the number of bytes that the field takes up (minus the data-type flag).
511    * For arrays, it's the number of array entries.
512    * For maps, it's the number of map entries.
513    */
514   long readLength() {
515      return length;
516   }
517
518   /**
519    * Read 64-bit long from the stream.
520    */
521   long readLong() throws IOException {
522      if (length == 4)
523         return readUInt4();
524      long l = read();
525      l <<= 8;
526      l |= read();
527      l <<= 8;
528      l |= read();
529      l <<= 8;
530      l |= read();
531      l <<= 8;
532      l |= read();
533      l <<= 8;
534      l |= read();
535      l <<= 8;
536      l |= read();
537      l <<= 8;
538      l |= read();
539      return l;
540   }
541
542   /**
543    * Read a string from the stream.
544    */
545   String readString() throws IOException {
546      return new String(readBinary(), UTF8);
547   }
548}