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.shared.asn1.ber.tlv;
021
022
023import org.apache.directory.shared.i18n.I18n;
024
025
026/**
027 * Parse and decode an Integer value.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public final class IntegerDecoder
032{
033    /** A mask used to get only the necessary bytes */
034    private static final int[] MASK = new int[]
035        { 0x000000FF, 0x0000FFFF, 0x00FFFFFF, 0xFFFFFFFF };
036
037
038    /**
039     * Parse a byte buffer and send back an integer, controlling that this number
040     * is in a specified interval.
041     *
042     * @param value The byte buffer to parse
043     * @param min Lowest value allowed, included
044     * @param max Highest value allowed, included
045     * @return An integer
046     * @throws IntegerDecoderException Thrown if the byte stream does not contains an integer
047     */
048    public static int parse( Value value, int min, int max ) throws IntegerDecoderException
049    {
050
051        int result = 0;
052
053        byte[] bytes = value.getData();
054
055        if ( ( bytes == null ) || ( bytes.length == 0 ) )
056        {
057            throw new IntegerDecoderException( I18n.err( I18n.ERR_00036_0_BYTES_LONG_INTEGER ) );
058        }
059
060        if ( bytes.length > 4 )
061        {
062            throw new IntegerDecoderException( I18n.err( I18n.ERR_00037_ABOVE_4_BYTES_INTEGER ) );
063        }
064
065        for ( int i = 0; ( i < bytes.length ) && ( i < 5 ); i++ )
066        {
067            result = ( result << 8 ) | ( bytes[i] & 0x00FF );
068        }
069
070        if ( ( bytes[0] & 0x80 ) == 0x80 )
071        {
072            result = -( ( ( ~result ) + 1 ) & MASK[bytes.length - 1] );
073        }
074
075        if ( ( result >= min ) && ( result <= max ) )
076        {
077            return result;
078        }
079        else
080        {
081            throw new IntegerDecoderException( I18n.err( I18n.ERR_00038_VALUE_NOT_IN_RANGE, min, max ) );
082        }
083    }
084
085
086    /**
087     * Parse a byte buffer and send back an integer
088     *
089     * @param value The byte buffer to parse
090     * @return An integer
091     * @throws IntegerDecoderException Thrown if the byte stream does not contains an integer
092     */
093    public static int parse( Value value ) throws IntegerDecoderException
094    {
095        return parse( value, Integer.MIN_VALUE, Integer.MAX_VALUE );
096    }
097}