001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.api.asn1.ber.tlv;
021
022
023import java.nio.BufferOverflowException;
024import java.nio.ByteBuffer;
025
026import org.apache.directory.api.asn1.EncoderException;
027import org.apache.directory.api.asn1.util.Asn1StringUtils;
028import org.apache.directory.api.asn1.util.BitString;
029import org.apache.directory.api.asn1.util.Oid;
030import org.apache.directory.api.i18n.I18n;
031
032
033/**
034 * This class stores the data decoded from a TLV.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public class BerValue
039{
040    /** The data buffer. */
041    private byte[] data;
042
043    /** The current position of the last byte in the data buffer */
044    private int currentPos;
045
046    /** The encoded byte for a TRUE value */
047    public static final byte TRUE_VALUE = ( byte ) 0xFF;
048
049    /** The encoded byte for a FALSE value */
050    public static final byte FALSE_VALUE = ( byte ) 0x00;
051
052    /** Pre-encoded PDUs for a TRUE TLV */
053    private static final byte[] ENCODED_TRUE = new byte[]
054        { 0x01, 0x01, TRUE_VALUE };
055
056    /** Pre-encoded PDUs for a FALSE TLV */
057    private static final byte[] ENCODED_FALSE = new byte[]
058        { 0x01, 0x01, FALSE_VALUE };
059
060    /** Integer limits for encoding : 0x7F */
061    private static final int ONE_BYTE_MAX = ( 1 << 7 ) - 1;
062
063    /** Integer limits for encoding : -0x7F */
064    private static final int ONE_BYTE_MIN = -( 1 << 7 );
065
066    /** Integer limits for encoding : 0x7FFF */
067    private static final int TWO_BYTE_MAX = ( 1 << 15 ) - 1;
068
069    /** Integer limits for encoding : -0x7FFF */
070    private static final int TWO_BYTE_MIN = -( 1 << 15 );
071
072    /** Integer limits for encoding : 0x7FFFFF */
073    private static final int THREE_BYTE_MAX = ( 1 << 23 ) - 1;
074
075    /** Integer limits for encoding : -0x7FFFFF */
076    private static final int THREE_BYTE_MIN = -( 1 << 23 );
077
078    /** Integer limits for encoding : 0x7FFFFFFF */
079    private static final long FOUR_BYTE_MAX = ( 1L << 31 ) - 1L;
080
081    /** Integer limits for encoding : -0x7FFFFFFF */
082    private static final long FOUR_BYTE_MIN = -( 1L << 31 );
083
084    /** Integer limits for encoding : 0x7FFFFFFFFF */
085    private static final long FIVE_BYTE_MAX = ( 1L << 39 ) - 1L;
086
087    /** Integer limits for encoding : -0x7FFFFFFFFF */
088    private static final long FIVE_BYTE_MIN = -( 1L << 39 );
089
090    /** Integer limits for encoding : 0x7FFFFFFFFFFF */
091    private static final long SIX_BYTE_MAX = ( 1L << 47 ) - 1L;
092
093    /** Integer limits for encoding : -0x7FFFFFFFFFFF */
094    private static final long SIX_BYTE_MIN = -( 1L << 47 );
095
096    /** Integer limits for encoding : 0x7FFFFFFFFFFF */
097    private static final long SEVEN_BYTE_MAX = ( 1L << 55 ) - 1L;
098
099    /** Integer limits for encoding : -0x7FFFFFFFFFFF */
100    private static final long SEVEN_BYTE_MIN = -( 1L << 55 );
101
102
103    /**
104     * Creates a new Value from a byte[]
105     *
106     * @param value the associated value
107     */
108    public BerValue( byte[] value )
109    {
110        // Do a copy of the byte array
111        data = new byte[value.length];
112        System.arraycopy( value, 0, data, 0, value.length );
113        currentPos = 0;
114    }
115
116
117    /**
118     * The default constructor.
119     */
120    public BerValue()
121    {
122        data = null;
123        currentPos = 0;
124    }
125
126
127    /**
128     * Initialize the Value
129     *
130     * @param size The data size to allocate.
131     */
132    public void init( int size )
133    {
134        data = new byte[size];
135        currentPos = 0;
136    }
137
138
139    /**
140     * Reset the Value so that it can be reused
141     */
142    public void reset()
143    {
144        data = null;
145        currentPos = 0;
146    }
147
148
149    /**
150     * Get the Values'data
151     *
152     * @return Returns the data.
153     */
154    public byte[] getData()
155    {
156        return data;
157    }
158
159
160    /**
161     * Set a block of bytes in the Value
162     *
163     * @param data The data to set.
164     */
165    public void setData( ByteBuffer data )
166    {
167        int length = data.remaining();
168        data.get( this.data, 0, length );
169        currentPos = length;
170    }
171
172
173    /**
174     * Append some bytes to the data buffer.
175     *
176     * @param buffer The data to append.
177     */
178    public void addData( ByteBuffer buffer )
179    {
180        int length = buffer.remaining();
181        buffer.get( data, currentPos, length );
182        currentPos += length;
183    }
184
185
186    /**
187     * Set a block of bytes in the Value
188     *
189     * @param data The data to set.
190     */
191    public void setData( byte[] data )
192    {
193        System.arraycopy( data, 0, this.data, 0, data.length );
194        currentPos = data.length;
195    }
196
197
198    /**
199     * Append some bytes to the data buffer.
200     *
201     * @param array The data to append.
202     */
203    public void addData( byte[] array )
204    {
205        System.arraycopy( array, 0, this.data, currentPos, array.length );
206        currentPos = array.length;
207    }
208
209
210    /**
211     * @return The number of bytes actually stored
212     */
213    public int getCurrentLength()
214    {
215        return currentPos;
216    }
217
218
219    /**
220     * Utility function that return the number of bytes necessary to store an
221     * integer value. Note that this value must be in [Integer.MIN_VALUE,
222     * Integer.MAX_VALUE].
223     *
224     * @param value The value to store in a byte array
225     * @return The number of bytes necessary to store the value.
226     */
227    public static int getNbBytes( int value )
228    {
229        if ( ( value >= ONE_BYTE_MIN ) && ( value <= ONE_BYTE_MAX ) )
230        {
231            return 1;
232        }
233        else if ( ( value >= TWO_BYTE_MIN ) && ( value <= TWO_BYTE_MAX ) )
234        {
235            return 2;
236        }
237        else if ( ( value >= THREE_BYTE_MIN ) && ( value <= THREE_BYTE_MAX ) )
238        {
239            return 3;
240        }
241        else
242        {
243            return 4;
244        }
245    }
246
247
248    /**
249     * Utility function that return the number of bytes necessary to store a
250     * long value. Note that this value must be in [Long.MIN_VALUE,
251     * Long.MAX_VALUE].
252     *
253     * @param value The value to store in a byte array
254     * @return The number of bytes necessary to store the value.
255     */
256    public static int getNbBytes( long value )
257    {
258        if ( ( value >= ONE_BYTE_MIN ) && ( value <= ONE_BYTE_MAX ) )
259        {
260            return 1;
261        }
262        else if ( ( value >= TWO_BYTE_MIN ) && ( value <= TWO_BYTE_MAX ) )
263        {
264            return 2;
265        }
266        else if ( ( value >= THREE_BYTE_MIN ) && ( value <= THREE_BYTE_MAX ) )
267        {
268            return 3;
269        }
270        else if ( ( value >= FOUR_BYTE_MIN ) && ( value <= FOUR_BYTE_MAX ) )
271        {
272            return 4;
273        }
274        else if ( ( value >= FIVE_BYTE_MIN ) && ( value <= FIVE_BYTE_MAX ) )
275        {
276            return 5;
277        }
278        else if ( ( value >= SIX_BYTE_MIN ) && ( value <= SIX_BYTE_MAX ) )
279        {
280            return 6;
281        }
282        else if ( ( value >= SEVEN_BYTE_MIN ) && ( value <= SEVEN_BYTE_MAX ) )
283        {
284            return 7;
285        }
286        else
287        {
288            return 8;
289        }
290    }
291
292
293    /**
294     * Utility function that return a byte array representing the Value We must
295     * respect the ASN.1 BER encoding scheme :
296     * <pre>
297     * 1) positive integer
298     * - [0 - 0x7F] : 0xVV
299     * - [0x80 - 0xFF] : 0x00 0xVV
300     * - [0x0100 - 0x7FFF] : 0xVV 0xVV
301     * - [0x8000 - 0xFFFF] : 0x00 0xVV 0xVV
302     * - [0x010000 - 0x7FFFFF] : 0xVV 0xVV 0xVV
303     * - [0x800000 - 0xFFFFFF] : 0x00 0xVV 0xVV 0xVV
304     * - [0x01000000 - 0x7FFFFFFF] : 0xVV 0xVV 0xVV 0xVV
305     * 2) Negative number - (~value) + 1
306     * </pre>
307     *
308     * @param value The value to store in a byte array
309     * @return The byte array representing the value.
310     */
311    public static byte[] getBytes( int value )
312    {
313        byte[] bytes = null;
314
315        if ( value >= 0 )
316        {
317            if ( ( value >= 0 ) && ( value <= ONE_BYTE_MAX ) )
318            {
319                bytes = new byte[1];
320                bytes[0] = ( byte ) value;
321            }
322            else if ( ( value > ONE_BYTE_MAX ) && ( value <= TWO_BYTE_MAX ) )
323            {
324                bytes = new byte[2];
325                bytes[1] = ( byte ) value;
326                bytes[0] = ( byte ) ( value >> 8 );
327            }
328            else if ( ( value > TWO_BYTE_MAX ) && ( value <= THREE_BYTE_MAX ) )
329            {
330                bytes = new byte[3];
331                bytes[2] = ( byte ) value;
332                bytes[1] = ( byte ) ( value >> 8 );
333                bytes[0] = ( byte ) ( value >> 16 );
334            }
335            else
336            {
337                bytes = new byte[4];
338                bytes[3] = ( byte ) value;
339                bytes[2] = ( byte ) ( value >> 8 );
340                bytes[1] = ( byte ) ( value >> 16 );
341                bytes[0] = ( byte ) ( value >> 24 );
342            }
343        }
344        else
345        {
346            // On special case : 0x80000000
347            if ( value == 0x80000000 )
348            {
349                bytes = new byte[4];
350                bytes[3] = ( byte ) value;
351                bytes[2] = ( byte ) ( value >> 8 );
352                bytes[1] = ( byte ) ( value >> 16 );
353                bytes[0] = ( byte ) ( value >> 24 );
354            }
355            else
356            {
357                // We have to compute the complement, and add 1
358                //value = ( ~value ) + 1;
359
360                if ( value >= 0xFFFFFF80 )
361                {
362                    bytes = new byte[1];
363                    bytes[0] = ( byte ) value;
364                }
365                else if ( value >= 0xFFFF8000 )
366                {
367                    bytes = new byte[2];
368                    bytes[1] = ( byte ) ( value );
369                    bytes[0] = ( byte ) ( value >> 8 );
370                }
371                else if ( value >= 0xFF800000 )
372                {
373                    bytes = new byte[3];
374                    bytes[2] = ( byte ) value;
375                    bytes[1] = ( byte ) ( value >> 8 );
376                    bytes[0] = ( byte ) ( value >> 16 );
377                }
378                else
379                {
380                    bytes = new byte[4];
381                    bytes[3] = ( byte ) value;
382                    bytes[2] = ( byte ) ( value >> 8 );
383                    bytes[1] = ( byte ) ( value >> 16 );
384                    bytes[0] = ( byte ) ( value >> 24 );
385                }
386            }
387        }
388
389        return bytes;
390    }
391
392
393    /**
394     * Utility function that return a byte array representing the Value.
395     * We must respect the ASN.1 BER encoding scheme : <br>
396     * <pre>
397     * 1) positive integer
398     * - [0 - 0x7F] : 0xVV
399     * - [0x80 - 0xFF] : 0x00 0xVV
400     * - [0x0100 - 0x7FFF] : 0xVV 0xVV
401     * - [0x8000 - 0xFFFF] : 0x00 0xVV 0xVV
402     * - [0x010000 - 0x7FFFFF] : 0xVV 0xVV 0xVV
403     * - [0x800000 - 0xFFFFFF] : 0x00 0xVV 0xVV 0xVV
404     * - [0x01000000 - 0x7FFFFFFF] : 0xVV 0xVV 0xVV 0xVV
405     * 2) Negative number - (~value) + 1
406     * <pre>
407     * They are encoded following the table (the <br>
408     * encode bytes are those enclosed by squared braquets) :<br>
409     * <br>
410     * <pre>
411     *   -1                      -> FF FF FF FF FF FF FF [FF]
412     *   -127                    -> FF FF FF FF FF FF FF [81]
413     *   -128                    -> FF FF FF FF FF FF FF [80]
414     *   -129                    -> FF FF FF FF FF FF [FF 7F]
415     *   -255                    -> FF FF FF FF FF FF [FF 01]
416     *   -256                    -> FF FF FF FF FF FF [FF 00]
417     *   -257                    -> FF FF FF FF FF FF [FE FF]
418     *   -32767                  -> FF FF FF FF FF FF [80 01]
419     *   -32768                  -> FF FF FF FF FF FF [80 00]
420     *   -32769                  -> FF FF FF FF FF [FF 7F FF]
421     *   -65535                  -> FF FF FF FF FF [FF 00 01]
422     *   -65536                  -> FF FF FF FF FF [FF 00 00]
423     *   -65537                  -> FF FF FF FF FF [FE FF FF]
424     *   -8388607                -> FF FF FF FF FF [80 00 01]
425     *   -8388608                -> FF FF FF FF FF [80 00 00]
426     *   -8388609                -> FF FF FF FF [FF 7F FF FF]
427     *   -16777215               -> FF FF FF FF [FF 00 00 01]
428     *   -16777216               -> FF FF FF FF [FF 00 00 00]
429     *   -16777217               -> FF FF FF FF [FE FF FF FF]
430     *   -2147483647             -> FF FF FF FF [80 00 00 01]
431     *   -2147483648             -> FF FF FF FF [80 00 00 00]
432     *   -2147483649             -> FF FF FF [FF 7F FF FF FF]
433     *   -4294967295             -> FF FF FF [FF 00 00 00 01]
434     *   -4294967296             -> FF FF FF [FF 00 00 00 00]
435     *   -4294967297             -> FF FF FF [FE FF FF FF FF]
436     *   -549755813887           -> FF FF FF [80 00 00 00 01]
437     *   -549755813888           -> FF FF FF [80 00 00 00 00]
438     *   -549755813889           -> FF FF [FF 7F FF FF FF FF]
439     *   -1099511627775          -> FF FF [FF 00 00 00 00 01]
440     *   -1099511627776          -> FF FF [FF 00 00 00 00 00]
441     *   -1099511627777          -> FF FF [FE FF FF FF FF FF]
442     *   -140737488355327        -> FF FF [80 00 00 00 00 01]
443     *   -140737488355328        -> FF FF [80 00 00 00 00 00]
444     *   -140737488355329        -> FF [FF 7F FF FF FF FF FF]
445     *   -281474976710655        -> FF [FF 00 00 00 00 00 01]
446     *   -281474976710656        -> FF [FF 00 00 00 00 00 00]
447     *   -281474976710657        -> FF [FE FF FF FF FF FF FF]
448     *   -36028797018963967      -> FF [80 00 00 00 00 00 01]
449     *   -36028797018963968      -> FF [80 00 00 00 00 00 00]
450     *   -36028797018963969      -> [FF 7F FF FF FF FF FF FF]
451     *   -72057594037927936      -> [FF 00 00 00 00 00 00 00]
452     *   -72057594037927937      -> [FE FF FF FF FF FF FF FF]
453     *   -9223372036854775807    -> [80 00 00 00 00 00 00 01]
454     *   -9223372036854775808    -> [80 00 00 00 00 00 00 00]
455     * </pre>
456     * @param value The value to store in a byte array
457     * @return The byte array representing the value.
458     */
459    public static byte[] getBytes( long value )
460    {
461        byte[] bytes = null;
462
463        if ( value >= 0 )
464        {
465            if ( ( value >= 0 ) && ( value <= ONE_BYTE_MAX ) )
466            {
467                bytes = new byte[1];
468                bytes[0] = ( byte ) value;
469            }
470            else if ( ( value > ONE_BYTE_MAX ) && ( value <= TWO_BYTE_MAX ) )
471            {
472                bytes = new byte[2];
473                bytes[1] = ( byte ) value;
474                bytes[0] = ( byte ) ( value >> 8 );
475            }
476            else if ( ( value > TWO_BYTE_MAX ) && ( value <= THREE_BYTE_MAX ) )
477            {
478                bytes = new byte[3];
479                bytes[2] = ( byte ) value;
480                bytes[1] = ( byte ) ( value >> 8 );
481                bytes[0] = ( byte ) ( value >> 16 );
482            }
483            else if ( ( value > THREE_BYTE_MAX ) && ( value <= FOUR_BYTE_MAX ) )
484            {
485                bytes = new byte[4];
486                bytes[3] = ( byte ) value;
487                bytes[2] = ( byte ) ( value >> 8 );
488                bytes[1] = ( byte ) ( value >> 16 );
489                bytes[0] = ( byte ) ( value >> 24 );
490            }
491            else if ( ( value > FOUR_BYTE_MAX ) && ( value <= FIVE_BYTE_MAX ) )
492            {
493                bytes = new byte[5];
494                bytes[4] = ( byte ) value;
495                bytes[3] = ( byte ) ( value >> 8 );
496                bytes[2] = ( byte ) ( value >> 16 );
497                bytes[1] = ( byte ) ( value >> 24 );
498                bytes[0] = ( byte ) ( value >> 32 );
499            }
500            else if ( ( value > FIVE_BYTE_MAX ) && ( value <= SIX_BYTE_MAX ) )
501            {
502                bytes = new byte[6];
503                bytes[5] = ( byte ) value;
504                bytes[4] = ( byte ) ( value >> 8 );
505                bytes[3] = ( byte ) ( value >> 16 );
506                bytes[2] = ( byte ) ( value >> 24 );
507                bytes[1] = ( byte ) ( value >> 32 );
508                bytes[0] = ( byte ) ( value >> 40 );
509            }
510            else if ( ( value > SIX_BYTE_MAX ) && ( value <= SEVEN_BYTE_MAX ) )
511            {
512                bytes = new byte[7];
513                bytes[6] = ( byte ) value;
514                bytes[5] = ( byte ) ( value >> 8 );
515                bytes[4] = ( byte ) ( value >> 16 );
516                bytes[3] = ( byte ) ( value >> 24 );
517                bytes[2] = ( byte ) ( value >> 32 );
518                bytes[1] = ( byte ) ( value >> 40 );
519                bytes[0] = ( byte ) ( value >> 48 );
520            }
521            else
522            {
523                bytes = new byte[8];
524                bytes[7] = ( byte ) value;
525                bytes[6] = ( byte ) ( value >> 8 );
526                bytes[5] = ( byte ) ( value >> 16 );
527                bytes[4] = ( byte ) ( value >> 24 );
528                bytes[3] = ( byte ) ( value >> 32 );
529                bytes[2] = ( byte ) ( value >> 40 );
530                bytes[1] = ( byte ) ( value >> 48 );
531                bytes[0] = ( byte ) ( value >> 56 );
532            }
533        }
534        else
535        {
536            // On special case : 0x80000000
537            if ( value == 0x8000000000000000L )
538            {
539                bytes = new byte[8];
540                bytes[7] = ( byte ) 0x00;
541                bytes[6] = ( byte ) 0x00;
542                bytes[5] = ( byte ) 0x00;
543                bytes[4] = ( byte ) 0x00;
544                bytes[3] = ( byte ) 0x00;
545                bytes[2] = ( byte ) 0x00;
546                bytes[1] = ( byte ) 0x00;
547                bytes[0] = ( byte ) 0x80;
548            }
549            else
550            {
551                // We have to compute the complement, and add 1
552                // value = ( ~value ) + 1;
553
554                if ( value >= 0xFFFFFFFFFFFFFF80L )
555                {
556                    bytes = new byte[1];
557                    bytes[0] = ( byte ) value;
558                }
559                else if ( value >= 0xFFFFFFFFFFFF8000L )
560                {
561                    bytes = new byte[2];
562                    bytes[1] = ( byte ) ( value );
563                    bytes[0] = ( byte ) ( value >> 8 );
564                }
565                else if ( value >= 0xFFFFFFFFFF800000L )
566                {
567                    bytes = new byte[3];
568                    bytes[2] = ( byte ) value;
569                    bytes[1] = ( byte ) ( value >> 8 );
570                    bytes[0] = ( byte ) ( value >> 16 );
571                }
572                else if ( value >= 0xFFFFFFFF80000000L )
573                {
574                    bytes = new byte[4];
575                    bytes[3] = ( byte ) value;
576                    bytes[2] = ( byte ) ( value >> 8 );
577                    bytes[1] = ( byte ) ( value >> 16 );
578                    bytes[0] = ( byte ) ( value >> 24 );
579                }
580                else if ( value >= 0xFFFFFF8000000000L )
581                {
582                    bytes = new byte[5];
583                    bytes[4] = ( byte ) value;
584                    bytes[3] = ( byte ) ( value >> 8 );
585                    bytes[2] = ( byte ) ( value >> 16 );
586                    bytes[1] = ( byte ) ( value >> 24 );
587                    bytes[0] = ( byte ) ( value >> 32 );
588                }
589                else if ( value >= 0xFFFF800000000000L )
590                {
591                    bytes = new byte[6];
592                    bytes[5] = ( byte ) value;
593                    bytes[4] = ( byte ) ( value >> 8 );
594                    bytes[3] = ( byte ) ( value >> 16 );
595                    bytes[2] = ( byte ) ( value >> 24 );
596                    bytes[1] = ( byte ) ( value >> 32 );
597                    bytes[0] = ( byte ) ( value >> 40 );
598                }
599                else if ( value >= 0xFF80000000000000L )
600                {
601                    bytes = new byte[7];
602                    bytes[6] = ( byte ) value;
603                    bytes[5] = ( byte ) ( value >> 8 );
604                    bytes[4] = ( byte ) ( value >> 16 );
605                    bytes[3] = ( byte ) ( value >> 24 );
606                    bytes[2] = ( byte ) ( value >> 32 );
607                    bytes[1] = ( byte ) ( value >> 40 );
608                    bytes[0] = ( byte ) ( value >> 48 );
609                }
610                else
611                {
612                    bytes = new byte[8];
613                    bytes[7] = ( byte ) value;
614                    bytes[6] = ( byte ) ( value >> 8 );
615                    bytes[5] = ( byte ) ( value >> 16 );
616                    bytes[4] = ( byte ) ( value >> 24 );
617                    bytes[3] = ( byte ) ( value >> 32 );
618                    bytes[2] = ( byte ) ( value >> 40 );
619                    bytes[1] = ( byte ) ( value >> 48 );
620                    bytes[0] = ( byte ) ( value >> 56 );
621                }
622            }
623        }
624
625        return bytes;
626    }
627
628
629    /**
630     * Encode a String value
631     *
632     * @param buffer The PDU in which the value will be put
633     * @param string The String to be encoded. It is supposed to be UTF-8
634     * @throws EncoderException if the PDU in which the value should be encoded is
635     * two small
636     */
637    public static void encode( ByteBuffer buffer, String string ) throws EncoderException
638    {
639        if ( buffer == null )
640        {
641            throw new EncoderException( I18n.err( I18n.ERR_00003_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
642        }
643
644        try
645        {
646            buffer.put( UniversalTag.OCTET_STRING.getValue() );
647
648            byte[] value = Asn1StringUtils.getBytesUtf8( string );
649
650            buffer.put( TLV.getBytes( value.length ) );
651
652            if ( value.length != 0 )
653            {
654                buffer.put( value );
655            }
656        }
657        catch ( BufferOverflowException boe )
658        {
659            throw new EncoderException( I18n.err( I18n.ERR_00004_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
660        }
661    }
662
663
664    /**
665     * Encode a BIT STRING value
666     *
667     * @param buffer The PDU in which the value will be put
668     * @param bitString The BitString to be encoded.
669     * @throws EncoderException if the PDU in which the value should be encoded is
670     * two small
671     */
672    public static void encode( ByteBuffer buffer, BitString bitString ) throws EncoderException
673    {
674        if ( buffer == null )
675        {
676            throw new EncoderException( I18n.err( I18n.ERR_00003_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
677        }
678
679        try
680        {
681            buffer.put( UniversalTag.BIT_STRING.getValue() );
682
683            // The BitString length. We add one byte for the unused number
684            // of bits
685            byte[] bytes = bitString.getData();
686            int length = bytes.length;
687
688            buffer.put( TLV.getBytes( length ) );
689            buffer.put( bytes );
690        }
691        catch ( BufferOverflowException boe )
692        {
693            throw new EncoderException( I18n.err( I18n.ERR_00004_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
694        }
695    }
696
697
698    /**
699     * Encode an OctetString value
700     *
701     * @param buffer The PDU in which the value will be put
702     * @param bytes The bytes to be encoded
703     * @throws EncoderException if the PDU in which the value should be encoded is
704     * two small
705     */
706    public static void encode( ByteBuffer buffer, byte[] bytes ) throws EncoderException
707    {
708        if ( buffer == null )
709        {
710            throw new EncoderException( I18n.err( I18n.ERR_00003_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
711        }
712
713        try
714        {
715            buffer.put( UniversalTag.OCTET_STRING.getValue() );
716
717            if ( ( bytes == null ) || ( bytes.length == 0 ) )
718            {
719                buffer.put( ( byte ) 0 );
720            }
721            else
722            {
723                buffer.put( TLV.getBytes( bytes.length ) );
724                buffer.put( bytes );
725            }
726        }
727        catch ( BufferOverflowException boe )
728        {
729            throw new EncoderException( I18n.err( I18n.ERR_00004_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
730        }
731    }
732
733
734    /**
735     * Encode an OID value
736     *
737     * @param buffer The PDU in which the value will be put
738     * @param oid The OID to be encoded
739     * @throws EncoderException if the PDU in which the value should be encoded is
740     * two small
741     */
742    public static void encode( ByteBuffer buffer, Oid oid ) throws EncoderException
743    {
744        if ( buffer == null )
745        {
746            throw new EncoderException( I18n.err( I18n.ERR_00003_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
747        }
748
749        try
750        {
751            buffer.put( UniversalTag.OCTET_STRING.getValue() );
752            buffer.put( TLV.getBytes( oid.getEncodedLength() ) );
753
754            if ( oid.getEncodedLength() != 0 )
755            {
756                oid.writeBytesTo( buffer );
757            }
758        }
759        catch ( BufferOverflowException boe )
760        {
761            throw new EncoderException( I18n.err( I18n.ERR_00004_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
762        }
763    }
764
765
766    /**
767     * Encode an integer value
768     *
769     * @param buffer The PDU in which the value will be put
770     * @param value The integer to be encoded
771     * @throws EncoderException if the PDU in which the value should be encoded is
772     * two small
773     */
774    public static void encode( ByteBuffer buffer, int value ) throws EncoderException
775    {
776        if ( buffer == null )
777        {
778            throw new EncoderException( I18n.err( I18n.ERR_00003_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
779        }
780
781        try
782        {
783            buffer.put( UniversalTag.INTEGER.getValue() );
784            buffer.put( ( byte ) getNbBytes( value ) );
785            buffer.put( getBytes( value ) );
786        }
787        catch ( BufferOverflowException boe )
788        {
789            throw new EncoderException( I18n.err( I18n.ERR_00004_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
790        }
791    }
792
793
794    /**
795     * Encode a long value
796     *
797     * @param buffer The PDU in which the value will be put
798     * @param value The long to be encoded
799     * @throws EncoderException if the PDU in which the value should be encoded is
800     * two small
801     */
802    public static void encode( ByteBuffer buffer, long value ) throws EncoderException
803    {
804        if ( buffer == null )
805        {
806            throw new EncoderException( I18n.err( I18n.ERR_00003_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
807        }
808
809        try
810        {
811            buffer.put( UniversalTag.INTEGER.getValue() );
812            buffer.put( ( byte ) getNbBytes( value ) );
813            buffer.put( getBytes( value ) );
814        }
815        catch ( BufferOverflowException boe )
816        {
817            throw new EncoderException( I18n.err( I18n.ERR_00004_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
818        }
819    }
820
821
822    /**
823     * Encode an integer value
824     *
825     * @param buffer The PDU in which the value will be put
826     * @param tag The tag if it's not an UNIVERSAL one
827     * @param value The integer to be encoded
828     * @throws EncoderException if the PDU in which the value should be encoded is
829     * two small
830     */
831    public static void encode( ByteBuffer buffer, byte tag, int value ) throws EncoderException
832    {
833        if ( buffer == null )
834        {
835            throw new EncoderException( I18n.err( I18n.ERR_00003_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
836        }
837
838        try
839        {
840            buffer.put( tag );
841            buffer.put( ( byte ) getNbBytes( value ) );
842            buffer.put( getBytes( value ) );
843        }
844        catch ( BufferOverflowException boe )
845        {
846            throw new EncoderException( I18n.err( I18n.ERR_00004_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
847        }
848    }
849
850
851    /**
852     * Encode an enumerated value
853     *
854     * @param buffer The PDU in which the value will be put
855     * @param value The integer to be encoded
856     * @throws EncoderException if the PDU in which the value should be encoded is
857     * two small
858     */
859    public static void encodeEnumerated( ByteBuffer buffer, int value ) throws EncoderException
860    {
861        if ( buffer == null )
862        {
863            throw new EncoderException( I18n.err( I18n.ERR_00003_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
864        }
865
866        try
867        {
868            buffer.put( UniversalTag.ENUMERATED.getValue() );
869            buffer.put( TLV.getBytes( getNbBytes( value ) ) );
870            buffer.put( getBytes( value ) );
871        }
872        catch ( BufferOverflowException boe )
873        {
874            throw new EncoderException( I18n.err( I18n.ERR_00004_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
875        }
876    }
877
878
879    /**
880     * Encode a boolean value
881     *
882     * @param buffer The PDU in which the value will be put
883     * @param bool The boolean to be encoded
884     * @throws EncoderException if the PDU in which the value should be encoded is
885     * two small
886     */
887    public static void encode( ByteBuffer buffer, boolean bool ) throws EncoderException
888    {
889        if ( buffer == null )
890        {
891            throw new EncoderException( I18n.err( I18n.ERR_00003_CANNOT_PUT_PDU_IN_NULL_BUFFER ) );
892        }
893
894        try
895        {
896            if ( bool )
897            {
898                buffer.put( ENCODED_TRUE );
899            }
900            else
901            {
902                buffer.put( ENCODED_FALSE );
903            }
904        }
905        catch ( BufferOverflowException boe )
906        {
907            throw new EncoderException( I18n.err( I18n.ERR_00004_PDU_BUFFER_SIZE_TOO_SMALL ), boe );
908        }
909    }
910
911
912    /**
913     * Return a string representing the Value
914     *
915     * @return A string representing the value
916     */
917    @Override
918    public String toString()
919    {
920        StringBuilder sb = new StringBuilder();
921        sb.append( "DATA" );
922
923        if ( data != null )
924        {
925            sb.append( '[' );
926            sb.append( Asn1StringUtils.dumpBytes( data ) );
927            sb.append( ']' );
928        }
929        else
930        {
931            return "[]";
932        }
933
934        return sb.toString();
935    }
936}