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 org.apache.directory.api.i18n.I18n;
024
025
026/**
027 * Parse and decode a Long value.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public final class LongDecoder
032{
033    /** A mask used to get only the necessary bytes */
034    private static final long[] MASK = new long[]
035        { 0x00000000000000FFL, 0x000000000000FFFFL, 0x0000000000FFFFFFL, 0x00000000FFFFFFFFL, 0x000000FFFFFFFFFFL,
036            0x0000FFFFFFFFFFFFL, 0x00FFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL };
037
038
039    private LongDecoder()
040    {
041    }
042
043
044    /**
045     * Parse a byte buffer and send back an long, controlling that this number
046     * is in a specified interval.
047     *
048     * @param value The byte buffer to parse
049     * @param min Lowest value allowed, included
050     * @param max Highest value allowed, included
051     * @return An integer
052     * @throws LongDecoderException Thrown if the byte stream does not contains an integer
053     */
054    public static long parse( BerValue value, long min, long max ) throws LongDecoderException
055    {
056        long result = parseLong( value );
057
058        if ( ( result >= min ) && ( result <= max ) )
059        {
060            return result;
061        }
062        else
063        {
064            throw new LongDecoderException( I18n.err( I18n.ERR_00038_VALUE_NOT_IN_RANGE, min, max ) );
065        }
066    }
067
068
069    /**
070     * Parse a byte buffer and send back an integer
071     *
072     * @param value The byte buffer to parse
073     * @return An integer
074     * @throws LongDecoderException Thrown if the byte stream does not contains an integer
075     */
076    public static long parse( BerValue value ) throws LongDecoderException
077    {
078        return parseLong( value );
079    }
080    
081    
082    /**
083     * Helper method used to parse the long. We don't check any minimal or maximal
084     * bound.
085     * 
086     * @param value The value to parse to a long
087     * @return The decoded long
088     * @throws LongDecoderException If we failed to decode a long
089     */
090    public static long parseLong( BerValue value ) throws LongDecoderException
091    {
092        long result = 0;
093
094        byte[] bytes = value.getData();
095
096        if ( ( bytes == null ) || ( bytes.length == 0 ) )
097        {
098            throw new LongDecoderException( I18n.err( I18n.ERR_00039_0_BYTES_LONG_LONG ) );
099        }
100
101        if ( bytes.length > 8 )
102        {
103            throw new LongDecoderException( I18n.err( I18n.ERR_00039_0_BYTES_LONG_LONG ) );
104        }
105
106        for ( int i = 0; ( i < bytes.length ) && ( i < 9 ); i++ )
107        {
108            result = ( result << 8 ) | ( bytes[i] & 0x00FF );
109        }
110
111        if ( ( bytes[0] & 0x80 ) == 0x80 )
112        {
113            result = -( ( ( ~result ) + 1 ) & MASK[bytes.length - 1] );
114        }
115        
116        return result;
117    }
118}