mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Ludovic Poitou
24.47.2010 ebf96a30d0122d35e271bad15f1f31a0a9100842
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2010 Sun Microsystems, Inc.
 */
 
package com.sun.opends.sdk.ldap;
 
 
 
import static com.sun.opends.sdk.ldap.LDAPConstants.ELEMENT_READ_STATE_NEED_ADDITIONAL_LENGTH_BYTES;
import static com.sun.opends.sdk.ldap.LDAPConstants.ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE;
import static com.sun.opends.sdk.ldap.LDAPConstants.ELEMENT_READ_STATE_NEED_TYPE;
import static com.sun.opends.sdk.ldap.LDAPConstants.ELEMENT_READ_STATE_NEED_VALUE_BYTES;
import static com.sun.opends.sdk.messages.Messages.*;
 
import java.io.IOException;
import java.util.logging.Level;
 
import org.opends.sdk.ByteString;
import org.opends.sdk.ByteStringBuilder;
import org.opends.sdk.DecodeException;
import org.opends.sdk.LocalizableMessage;
import org.opends.sdk.asn1.ASN1Reader;
import org.opends.sdk.asn1.AbstractASN1Reader;
 
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.memory.ByteBuffersBuffer;
import org.glassfish.grizzly.memory.CompositeBuffer;
import com.sun.opends.sdk.util.StaticUtils;
 
 
 
/**
 * Grizzly ASN1 reader implementation.
 */
final class ASN1BufferReader extends AbstractASN1Reader implements ASN1Reader
{
  private final class ChildSequenceLimiter implements SequenceLimiter
  {
    private SequenceLimiter parent;
 
    private ChildSequenceLimiter child;
 
    private int readLimit;
 
    private int bytesRead;
 
 
 
    public void checkLimit(final int readSize) throws IOException
    {
      if ((readLimit > 0) && (bytesRead + readSize > readLimit))
      {
        final LocalizableMessage message = ERR_ASN1_TRUNCATED_LENGTH_BYTE.get();
        throw DecodeException.fatalError(message);
      }
 
      parent.checkLimit(readSize);
 
      bytesRead += readSize;
    }
 
 
 
    public SequenceLimiter endSequence() throws IOException
    {
      parent.checkLimit(remaining());
 
      if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINE) && remaining() > 0)
      {
        StaticUtils.DEBUG_LOG
            .fine(String.format(
                "Ignoring %d unused trailing bytes in ASN.1 SEQUENCE",
                remaining()));
      }
 
      for (int i = 0; i < remaining(); i++)
      {
        buffer.get();
      }
 
      return parent;
    }
 
 
 
    public int remaining()
    {
      return readLimit - bytesRead;
    }
 
 
 
    public ChildSequenceLimiter startSequence(final int readLimit)
    {
      if (child == null)
      {
        child = new ChildSequenceLimiter();
        child.parent = this;
      }
 
      child.readLimit = readLimit;
      child.bytesRead = 0;
 
      return child;
    }
  }
 
 
 
  private final class RootSequenceLimiter implements SequenceLimiter
  {
    private ChildSequenceLimiter child;
 
 
 
    public void checkLimit(final int readSize) throws IOException
    {
      if (buffer.remaining() < readSize)
      {
        final LocalizableMessage message = ERR_ASN1_TRUNCATED_LENGTH_BYTE.get();
        throw DecodeException.fatalError(message);
      }
    }
 
 
 
    public ChildSequenceLimiter endSequence() throws DecodeException
    {
      final LocalizableMessage message = ERR_ASN1_SEQUENCE_READ_NOT_STARTED
          .get();
      throw new IllegalStateException(message.toString());
    }
 
 
 
    public int remaining()
    {
      return buffer.remaining();
    }
 
 
 
    public ChildSequenceLimiter startSequence(final int readLimit)
    {
      if (child == null)
      {
        child = new ChildSequenceLimiter();
        child.parent = this;
      }
 
      child.readLimit = readLimit;
      child.bytesRead = 0;
 
      return child;
    }
  }
 
 
 
  private interface SequenceLimiter
  {
    public void checkLimit(int readSize) throws IOException;
 
 
 
    public SequenceLimiter endSequence() throws IOException;
 
 
 
    public int remaining();
 
 
 
    public SequenceLimiter startSequence(int readLimit);
  }
 
 
 
  private static final int MAX_STRING_BUFFER_SIZE = 1024;
 
  private int state = ELEMENT_READ_STATE_NEED_TYPE;
 
  private byte peekType = 0;
 
  private int peekLength = -1;
 
  private int lengthBytesNeeded = 0;
 
  private final int maxElementSize;
 
  private final CompositeBuffer buffer;
 
  private SequenceLimiter readLimiter;
 
  private final byte[] stringBuffer;
 
 
 
  /**
   * Creates a new ASN1 reader whose source is the provided input stream and
   * having a user defined maximum BER element size.
   *
   * @param maxElementSize
   *          The maximum BER element size, or <code>0</code> to indicate that
   *          there is no limit.
   */
  ASN1BufferReader(final int maxElementSize)
  {
    this.readLimiter = new RootSequenceLimiter();
    this.stringBuffer = new byte[MAX_STRING_BUFFER_SIZE];
    this.maxElementSize = maxElementSize;
    this.buffer = ByteBuffersBuffer.create();
  }
 
 
 
  /**
   * Closes this ASN.1 reader and the underlying stream.
   *
   * @throws IOException
   *           if an I/O error occurs
   */
  public void close() throws IOException
  {
    buffer.dispose();
  }
 
 
 
  /**
   * Determines if a complete ASN.1 element is ready to be read from the stream
   * reader.
   *
   * @return <code>true</code> if another complete element is available or
   *         <code>false</code> otherwise.
   * @throws IOException
   *           If an error occurs while trying to decode an ASN1 element.
   */
  public boolean elementAvailable() throws IOException
  {
    return !((state == ELEMENT_READ_STATE_NEED_TYPE) && !needTypeState(true))
        && !((state == ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE)
            && !needFirstLengthByteState(true))
        && !((state == ELEMENT_READ_STATE_NEED_ADDITIONAL_LENGTH_BYTES)
            && !needAdditionalLengthBytesState(true))
        && peekLength <= readLimiter.remaining();
 
  }
 
 
 
  /**
   * Determines if the input stream contains at least one ASN.1 element to be
   * read.
   *
   * @return <code>true</code> if another element is available or
   *         <code>false</code> otherwise.
   * @throws IOException
   *           If an error occurs while trying to decode an ASN1 element.
   */
  public boolean hasNextElement() throws IOException
  {
    return (state != ELEMENT_READ_STATE_NEED_TYPE) || needTypeState(true);
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public int peekLength() throws IOException
  {
    peekType();
 
    switch (state)
    {
    case ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE:
      needFirstLengthByteState(false);
      break;
 
    case ELEMENT_READ_STATE_NEED_ADDITIONAL_LENGTH_BYTES:
      needAdditionalLengthBytesState(false);
    }
 
    return peekLength;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public byte peekType() throws IOException
  {
    if (state == ELEMENT_READ_STATE_NEED_TYPE)
    {
      needTypeState(false);
    }
 
    return peekType;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public boolean readBoolean() throws IOException
  {
    // Read the header if haven't done so already
    peekLength();
 
    if (peekLength != 1)
    {
      final LocalizableMessage message = ERR_ASN1_BOOLEAN_INVALID_LENGTH
          .get(peekLength);
      throw DecodeException.fatalError(message);
    }
 
    readLimiter.checkLimit(peekLength);
    final byte readByte = buffer.get();
 
    if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST))
    {
      StaticUtils.DEBUG_LOG.finest(String.format(
          "READ ASN.1 BOOLEAN(type=0x%x, length=%d, value=%s)", peekType,
          peekLength, String.valueOf(readByte != 0x00)));
    }
 
    state = ELEMENT_READ_STATE_NEED_TYPE;
    return readByte != 0x00;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public void readEndSequence() throws IOException, IllegalStateException
  {
    readLimiter = readLimiter.endSequence();
 
    if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST))
    {
      StaticUtils.DEBUG_LOG.finest(String.format("READ ASN.1 END SEQUENCE"));
    }
 
    // Reset the state
    state = ELEMENT_READ_STATE_NEED_TYPE;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public void readEndSet() throws IOException
  {
    // From an implementation point of view, a set is equivalent to a
    // sequence.
    readEndSequence();
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public int readEnumerated() throws IOException
  {
    // Read the header if haven't done so already
    peekLength();
 
    if ((peekLength < 1) || (peekLength > 4))
    {
      final LocalizableMessage message = ERR_ASN1_INTEGER_INVALID_LENGTH
          .get(peekLength);
      throw DecodeException.fatalError(message);
    }
 
    // From an implementation point of view, an enumerated value is
    // equivalent to an integer.
    return (int) readInteger();
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public long readInteger() throws IOException
  {
    // Read the header if haven't done so already
    peekLength();
 
    if ((peekLength < 1) || (peekLength > 8))
    {
      final LocalizableMessage message = ERR_ASN1_INTEGER_INVALID_LENGTH
          .get(peekLength);
      throw DecodeException.fatalError(message);
    }
 
    readLimiter.checkLimit(peekLength);
    if (peekLength > 4)
    {
      long longValue = 0;
      for (int i = 0; i < peekLength; i++)
      {
        final int readByte = buffer.get();
        if ((i == 0) && (((byte) readByte) < 0))
        {
          longValue = 0xFFFFFFFFFFFFFFFFL;
        }
        longValue = (longValue << 8) | (readByte & 0xFF);
      }
 
      state = ELEMENT_READ_STATE_NEED_TYPE;
      return longValue;
    }
    else
    {
      int intValue = 0;
      for (int i = 0; i < peekLength; i++)
      {
        final int readByte = buffer.get();
        if ((i == 0) && (((byte) readByte) < 0))
        {
          intValue = 0xFFFFFFFF;
        }
        intValue = (intValue << 8) | (readByte & 0xFF);
      }
 
      if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST))
      {
        StaticUtils.DEBUG_LOG.finest(String.format(
            "READ ASN.1 INTEGER(type=0x%x, length=%d, value=%d)", peekType,
            peekLength, intValue));
      }
 
      state = ELEMENT_READ_STATE_NEED_TYPE;
      return intValue;
    }
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public void readNull() throws IOException
  {
    // Read the header if haven't done so already
    peekLength();
 
    // Make sure that the decoded length is exactly zero byte.
    if (peekLength != 0)
    {
      final LocalizableMessage message = ERR_ASN1_NULL_INVALID_LENGTH
          .get(peekLength);
      throw DecodeException.fatalError(message);
    }
 
    if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST))
    {
      StaticUtils.DEBUG_LOG.finest(String.format(
          "READ ASN.1 NULL(type=0x%x, length=%d)", peekType, peekLength));
    }
 
    state = ELEMENT_READ_STATE_NEED_TYPE;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public ByteString readOctetString() throws IOException
  {
    // Read the header if haven't done so already
    peekLength();
 
    if (peekLength == 0)
    {
      state = ELEMENT_READ_STATE_NEED_TYPE;
      return ByteString.empty();
    }
 
    readLimiter.checkLimit(peekLength);
    // Copy the value and construct the element to return.
    final byte[] value = new byte[peekLength];
    buffer.get(value);
 
    if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST))
    {
      StaticUtils.DEBUG_LOG
          .finest(String.format("READ ASN.1 OCTETSTRING(type=0x%x, length=%d)",
              peekType, peekLength));
    }
 
    state = ELEMENT_READ_STATE_NEED_TYPE;
    return ByteString.wrap(value);
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public ByteStringBuilder readOctetString(final ByteStringBuilder builder)
      throws IOException
  {
    // Read the header if haven't done so already
    peekLength();
 
    if (peekLength == 0)
    {
      state = ELEMENT_READ_STATE_NEED_TYPE;
      return builder;
    }
 
    readLimiter.checkLimit(peekLength);
    // Copy the value and construct the element to return.
    // TODO: Is there a more efficient way to do this?
    for (int i = 0; i < peekLength; i++)
    {
      builder.append(buffer.get());
    }
 
    if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST))
    {
      StaticUtils.DEBUG_LOG
          .finest(String.format("READ ASN.1 OCTETSTRING(type=0x%x, length=%d)",
              peekType, peekLength));
    }
 
    state = ELEMENT_READ_STATE_NEED_TYPE;
    return builder;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public String readOctetStringAsString() throws IOException
  {
    // Read the header if haven't done so already
    peekLength();
 
    if (peekLength == 0)
    {
      state = ELEMENT_READ_STATE_NEED_TYPE;
      return "";
    }
 
    byte[] readBuffer;
    if (peekLength <= stringBuffer.length)
    {
      readBuffer = stringBuffer;
    }
    else
    {
      readBuffer = new byte[peekLength];
    }
 
    readLimiter.checkLimit(peekLength);
    buffer.get(readBuffer, 0, peekLength);
 
    state = ELEMENT_READ_STATE_NEED_TYPE;
 
    String str;
    try
    {
      str = new String(readBuffer, 0, peekLength, "UTF-8");
    }
    catch (final Exception e)
    {
      if (StaticUtils.DEBUG_LOG.isLoggable(Level.WARNING))
      {
        StaticUtils.DEBUG_LOG
            .warning("Unable to decode ASN.1 OCTETSTRING bytes as UTF-8 string: "
                + e.toString());
      }
 
      str = new String(stringBuffer, 0, peekLength);
    }
 
    if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST))
    {
      StaticUtils.DEBUG_LOG.finest(String.format(
          "READ ASN.1 OCTETSTRING(type=0x%x, length=%d, value=%s)", peekType,
          peekLength, str));
    }
 
    return str;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public void readStartSequence() throws IOException
  {
    // Read the header if haven't done so already
    peekLength();
 
    readLimiter = readLimiter.startSequence(peekLength);
 
    if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST))
    {
      StaticUtils.DEBUG_LOG.finest(String.format(
          "READ ASN.1 START SEQUENCE(type=0x%x, length=%d)", peekType,
          peekLength));
    }
 
    // Reset the state
    state = ELEMENT_READ_STATE_NEED_TYPE;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public void readStartSet() throws IOException
  {
    // From an implementation point of view, a set is equivalent to a
    // sequence.
    readStartSequence();
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public ASN1Reader skipElement() throws IOException
  {
    // Read the header if haven't done so already
    peekLength();
 
    readLimiter.checkLimit(peekLength);
    for (int i = 0; i < peekLength; i++)
    {
      buffer.get();
    }
    state = ELEMENT_READ_STATE_NEED_TYPE;
    return this;
  }
 
 
 
  void appendBytesRead(final Buffer buffer)
  {
    this.buffer.append(buffer);
  }
 
 
 
  void disposeBytesRead()
  {
    this.buffer.shrink();
  }
 
 
 
  /**
   * Internal helper method reading the additional ASN.1 length bytes and
   * transition to the next state if successful.
   *
   * @param ensureRead
   *          <code>true</code> to check for availability first.
   * @return <code>true</code> if the length bytes was successfully read.
   * @throws IOException
   *           If an error occurs while reading from the stream.
   */
  private boolean needAdditionalLengthBytesState(final boolean ensureRead)
      throws IOException
  {
    if (ensureRead && (readLimiter.remaining() < lengthBytesNeeded))
    {
      return false;
    }
 
    byte readByte;
    readLimiter.checkLimit(lengthBytesNeeded);
    while (lengthBytesNeeded > 0)
    {
      readByte = buffer.get();
      peekLength = (peekLength << 8) | (readByte & 0xFF);
      lengthBytesNeeded--;
    }
 
    // Make sure that the element is not larger than the maximum allowed
    // message size.
    if ((maxElementSize > 0) && (peekLength > maxElementSize))
    {
      final LocalizableMessage m = ERR_LDAP_CLIENT_DECODE_MAX_REQUEST_SIZE_EXCEEDED
          .get(peekLength, maxElementSize);
      throw DecodeException.fatalError(m);
    }
    state = ELEMENT_READ_STATE_NEED_VALUE_BYTES;
    return true;
  }
 
 
 
  /**
   * Internal helper method reading the first length bytes and transition to the
   * next state if successful.
   *
   * @param ensureRead
   *          <code>true</code> to check for availability first.
   * @return <code>true</code> if the length bytes was successfully read
   * @throws IOException
   *           If an error occurs while trying to decode an ASN1 element.
   */
  private boolean needFirstLengthByteState(final boolean ensureRead)
      throws IOException
  {
    if (ensureRead && (readLimiter.remaining() <= 0))
    {
      return false;
    }
 
    readLimiter.checkLimit(1);
    byte readByte = buffer.get();
    peekLength = (readByte & 0x7F);
    if (peekLength != readByte)
    {
      lengthBytesNeeded = peekLength;
      if (lengthBytesNeeded > 4)
      {
        final LocalizableMessage message = ERR_ASN1_INVALID_NUM_LENGTH_BYTES
            .get(lengthBytesNeeded);
        throw DecodeException.fatalError(message);
      }
      peekLength = 0x00;
 
      if (ensureRead && (readLimiter.remaining() < lengthBytesNeeded))
      {
        state = ELEMENT_READ_STATE_NEED_ADDITIONAL_LENGTH_BYTES;
        return false;
      }
 
      readLimiter.checkLimit(lengthBytesNeeded);
      while (lengthBytesNeeded > 0)
      {
        readByte = buffer.get();
        peekLength = (peekLength << 8) | (readByte & 0xFF);
        lengthBytesNeeded--;
      }
    }
 
    // Make sure that the element is not larger than the maximum allowed
    // message size.
    if ((maxElementSize > 0) && (peekLength > maxElementSize))
    {
      final LocalizableMessage m = ERR_LDAP_CLIENT_DECODE_MAX_REQUEST_SIZE_EXCEEDED
          .get(peekLength, maxElementSize);
      throw DecodeException.fatalError(m);
    }
    state = ELEMENT_READ_STATE_NEED_VALUE_BYTES;
    return true;
  }
 
 
 
  /**
   * Internal helper method reading the ASN.1 type byte and transition to the
   * next state if successful.
   *
   * @param ensureRead
   *          <code>true</code> to check for availability first.
   * @return <code>true</code> if the type byte was successfully read
   * @throws IOException
   *           If an error occurs while trying to decode an ASN1 element.
   */
  private boolean needTypeState(final boolean ensureRead) throws IOException
  {
    // Read just the type.
    if (ensureRead && (readLimiter.remaining() <= 0))
    {
      return false;
    }
 
    readLimiter.checkLimit(1);
    peekType = buffer.get();
    state = ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE;
    return true;
  }
}