149d3bc9Richard Lowe/*
207dc194Richard Lowe  Copyright (C) 2000,2004 Silicon Graphics, Inc.  All Rights Reserved.
34d9fdb4Robert Mustacchi  Portions Copyright 2011-2018 David Anderson. All Rights Reserved.
449d3bc9Richard Lowe
549d3bc9Richard Lowe  This program is free software; you can redistribute it and/or modify it
64d9fdb4Robert Mustacchi  under the terms of version 2.1 of the GNU Lesser General Public License
749d3bc9Richard Lowe  as published by the Free Software Foundation.
849d3bc9Richard Lowe
949d3bc9Richard Lowe  This program is distributed in the hope that it would be useful, but
1049d3bc9Richard Lowe  WITHOUT ANY WARRANTY; without even the implied warranty of
114d9fdb4Robert Mustacchi  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1249d3bc9Richard Lowe
1349d3bc9Richard Lowe  Further, this software is distributed without any warranty that it is
144d9fdb4Robert Mustacchi  free of the rightful claim of any third person regarding infringement
154d9fdb4Robert Mustacchi  or the like.  Any license provided herein, whether implied or
1649d3bc9Richard Lowe  otherwise, applies only to this software file.  Patent licenses, if
174d9fdb4Robert Mustacchi  any, provided herein do not apply to combinations of this program with
184d9fdb4Robert Mustacchi  other software, or any other product whatsoever.
1949d3bc9Richard Lowe
204d9fdb4Robert Mustacchi  You should have received a copy of the GNU Lesser General Public
214d9fdb4Robert Mustacchi  License along with this program; if not, write the Free Software
2207dc194Richard Lowe  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
2349d3bc9Richard Lowe  USA.
2449d3bc9Richard Lowe
2549d3bc9Richard Lowe*/
2649d3bc9Richard Lowe
2749d3bc9Richard Lowe
2849d3bc9Richard Lowe#include "config.h"
2949d3bc9Richard Lowe#include <stdio.h>
304d9fdb4Robert Mustacchi#include "dwarf_incl.h"
314d9fdb4Robert Mustacchi#include "dwarf_error.h"
324d9fdb4Robert Mustacchi#include "dwarf_util.h"
334d9fdb4Robert Mustacchi
344d9fdb4Robert Mustacchi/*  Note that with 'make check')
354d9fdb4Robert Mustacchi    many of the test items
364d9fdb4Robert Mustacchi    only make sense if Dwarf_Unsigned (and Dwarf_Signed)
374d9fdb4Robert Mustacchi    are 64 bits.  The encode/decode logic should
384d9fdb4Robert Mustacchi    be fine whether those types are 64 or 32 bits.
394d9fdb4Robert Mustacchi    See runtests.sh */
404d9fdb4Robert Mustacchi
414d9fdb4Robert Mustacchi/*  10 bytes of leb, 7 bits each part of the number, gives
424d9fdb4Robert Mustacchi    room for a 64bit number.
434d9fdb4Robert Mustacchi    While any number of leading zeroes would be legal, so
444d9fdb4Robert Mustacchi    no max is really truly required here, why would a
454d9fdb4Robert Mustacchi    compiler generate leading zeros?  That would
464d9fdb4Robert Mustacchi    be strange.
474d9fdb4Robert Mustacchi*/
484d9fdb4Robert Mustacchi#define BYTESLEBMAX 10
494d9fdb4Robert Mustacchi#define BITSPERBYTE 8
5049d3bc9Richard Lowe
5149d3bc9Richard Lowe
524d9fdb4Robert Mustacchi/* Decode ULEB with checking */
534d9fdb4Robert Mustacchiint
544d9fdb4Robert Mustacchi_dwarf_decode_u_leb128_chk(Dwarf_Small * leb128,
554d9fdb4Robert Mustacchi    Dwarf_Unsigned * leb128_length,
564d9fdb4Robert Mustacchi    Dwarf_Unsigned *outval,
574d9fdb4Robert Mustacchi    Dwarf_Byte_Ptr endptr)
5849d3bc9Richard Lowe{
594d9fdb4Robert Mustacchi    Dwarf_Unsigned byte     = 0;
604d9fdb4Robert Mustacchi    Dwarf_Unsigned word_number = 0;
614d9fdb4Robert Mustacchi    Dwarf_Unsigned number  = 0;
624d9fdb4Robert Mustacchi    unsigned shift      = 0;
634d9fdb4Robert Mustacchi    /*  The byte_length value will be a small non-negative integer. */
644d9fdb4Robert Mustacchi    unsigned byte_length   = 0;
654d9fdb4Robert Mustacchi
664d9fdb4Robert Mustacchi    if (leb128 >=endptr) {
674d9fdb4Robert Mustacchi        return DW_DLV_ERROR;
684d9fdb4Robert Mustacchi    }
694d9fdb4Robert Mustacchi    /*  The following unrolls-the-loop for the first two bytes and
704d9fdb4Robert Mustacchi        unpacks into 32 bits to make this as fast as possible.
714d9fdb4Robert Mustacchi        word_number is assumed big enough that the shift has a defined
724d9fdb4Robert Mustacchi        result. */
7349d3bc9Richard Lowe    if ((*leb128 & 0x80) == 0) {
744d9fdb4Robert Mustacchi        if (leb128_length) {
7507dc194Richard Lowe            *leb128_length = 1;
764d9fdb4Robert Mustacchi        }
774d9fdb4Robert Mustacchi        *outval = *leb128;
784d9fdb4Robert Mustacchi        return DW_DLV_OK;
794d9fdb4Robert Mustacchi    } else {
804d9fdb4Robert Mustacchi        if ((leb128+1) >=endptr) {
814d9fdb4Robert Mustacchi            return DW_DLV_ERROR;
824d9fdb4Robert Mustacchi        }
834d9fdb4Robert Mustacchi        if ((*(leb128 + 1) & 0x80) == 0) {
844d9fdb4Robert Mustacchi            if (leb128_length) {
854d9fdb4Robert Mustacchi                *leb128_length = 2;
864d9fdb4Robert Mustacchi            }
874d9fdb4Robert Mustacchi            word_number = *leb128 & 0x7f;
884d9fdb4Robert Mustacchi            word_number |= (*(leb128 + 1) & 0x7f) << 7;
894d9fdb4Robert Mustacchi            *outval = word_number;
904d9fdb4Robert Mustacchi            return DW_DLV_OK;
914d9fdb4Robert Mustacchi        }
924d9fdb4Robert Mustacchi        /* Gets messy to hand-inline more byte checking. */
9349d3bc9Richard Lowe    }
9449d3bc9Richard Lowe
954d9fdb4Robert Mustacchi    /*  The rest handles long numbers Because the 'number' may be larger
964d9fdb4Robert Mustacchi        than the default int/unsigned, we must cast the 'byte' before
974d9fdb4Robert Mustacchi        the shift for the shift to have a defined result. */
9849d3bc9Richard Lowe    number = 0;
9949d3bc9Richard Lowe    shift = 0;
10049d3bc9Richard Lowe    byte_length = 1;
1014d9fdb4Robert Mustacchi    byte = *leb128;
10249d3bc9Richard Lowe    for (;;) {
1034d9fdb4Robert Mustacchi        if (shift >= (sizeof(number)*BITSPERBYTE)) {
1044d9fdb4Robert Mustacchi            return DW_DLV_ERROR;
1054d9fdb4Robert Mustacchi        }
1064d9fdb4Robert Mustacchi        number |= (byte & 0x7f) << shift;
10707dc194Richard Lowe        if ((byte & 0x80) == 0) {
1084d9fdb4Robert Mustacchi            if (leb128_length) {
10907dc194Richard Lowe                *leb128_length = byte_length;
1104d9fdb4Robert Mustacchi            }
1114d9fdb4Robert Mustacchi            *outval = number;
1124d9fdb4Robert Mustacchi            return DW_DLV_OK;
11307dc194Richard Lowe        }
11407dc194Richard Lowe        shift += 7;
11507dc194Richard Lowe        byte_length++;
1164d9fdb4Robert Mustacchi        if (byte_length > BYTESLEBMAX) {
1174d9fdb4Robert Mustacchi            /*  Erroneous input.  */
1184d9fdb4Robert Mustacchi            if( leb128_length) {
1194d9fdb4Robert Mustacchi                *leb128_length = BYTESLEBMAX;
1204d9fdb4Robert Mustacchi            }
1214d9fdb4Robert Mustacchi            break;
1224d9fdb4Robert Mustacchi        }
12307dc194Richard Lowe        ++leb128;
1244d9fdb4Robert Mustacchi        if ((leb128) >=endptr) {
1254d9fdb4Robert Mustacchi            return DW_DLV_ERROR;
1264d9fdb4Robert Mustacchi        }
12707dc194Richard Lowe        byte = *leb128;
12849d3bc9Richard Lowe    }
1294d9fdb4Robert Mustacchi    return DW_DLV_ERROR;
13049d3bc9Richard Lowe}
13149d3bc9Richard Lowe
1324d9fdb4Robert Mustacchi
13349d3bc9Richard Lowe#define BITSINBYTE 8
13449d3bc9Richard Lowe
1354d9fdb4Robert Mustacchiint
1364d9fdb4Robert Mustacchi_dwarf_decode_s_leb128_chk(Dwarf_Small * leb128, Dwarf_Unsigned * leb128_length,
1374d9fdb4Robert Mustacchi    Dwarf_Signed *outval,Dwarf_Byte_Ptr endptr)
13849d3bc9Richard Lowe{
1394d9fdb4Robert Mustacchi    Dwarf_Unsigned byte   = 0;
1404d9fdb4Robert Mustacchi    Dwarf_Signed number  = 0;
1414d9fdb4Robert Mustacchi    Dwarf_Bool sign      = 0;
1424d9fdb4Robert Mustacchi    Dwarf_Unsigned shift     = 0;
1434d9fdb4Robert Mustacchi    /*  The byte_length value will be a small non-negative integer. */
1444d9fdb4Robert Mustacchi    unsigned byte_length = 1;
1454d9fdb4Robert Mustacchi
1464d9fdb4Robert Mustacchi    /*  byte_length being the number of bytes of data absorbed so far in
1474d9fdb4Robert Mustacchi        turning the leb into a Dwarf_Signed. */
1484d9fdb4Robert Mustacchi    if (!outval) {
1494d9fdb4Robert Mustacchi        return DW_DLV_ERROR;
1504d9fdb4Robert Mustacchi    }
1514d9fdb4Robert Mustacchi    if (leb128 >= endptr) {
1524d9fdb4Robert Mustacchi        return DW_DLV_ERROR;
1534d9fdb4Robert Mustacchi    }
1544d9fdb4Robert Mustacchi    byte   = *leb128;
15549d3bc9Richard Lowe    for (;;) {
15607dc194Richard Lowe        sign = byte & 0x40;
1574d9fdb4Robert Mustacchi        if (shift >= (sizeof(number)*BITSPERBYTE)) {
1584d9fdb4Robert Mustacchi            return DW_DLV_ERROR;
1594d9fdb4Robert Mustacchi        }
1604d9fdb4Robert Mustacchi        number |= ((Dwarf_Unsigned) ((byte & 0x7f))) << shift;
16107dc194Richard Lowe        shift += 7;
16207dc194Richard Lowe
16307dc194Richard Lowe        if ((byte & 0x80) == 0) {
16407dc194Richard Lowe            break;
16507dc194Richard Lowe        }
16607dc194Richard Lowe        ++leb128;
1674d9fdb4Robert Mustacchi        if (leb128 >= endptr) {
1684d9fdb4Robert Mustacchi            return DW_DLV_ERROR;
1694d9fdb4Robert Mustacchi        }
17007dc194Richard Lowe        byte = *leb128;
17107dc194Richard Lowe        byte_length++;
1724d9fdb4Robert Mustacchi        if (byte_length > BYTESLEBMAX) {
1734d9fdb4Robert Mustacchi            /*  Erroneous input. */
1744d9fdb4Robert Mustacchi            if (leb128_length) {
1754d9fdb4Robert Mustacchi                *leb128_length = BYTESLEBMAX;
1764d9fdb4Robert Mustacchi            }
1774d9fdb4Robert Mustacchi            return DW_DLV_ERROR;
1784d9fdb4Robert Mustacchi        }
17949d3bc9Richard Lowe    }
18049d3bc9Richard Lowe
1814d9fdb4Robert Mustacchi    if (sign) {
1824d9fdb4Robert Mustacchi        /* The following avoids undefined behavior. */
1834d9fdb4Robert Mustacchi        unsigned shiftlim = sizeof(Dwarf_Signed) * BITSINBYTE -1;
1844d9fdb4Robert Mustacchi        if (shift < shiftlim) {
1854d9fdb4Robert Mustacchi            number |= -(Dwarf_Signed)(((Dwarf_Unsigned)1) << shift);
1864d9fdb4Robert Mustacchi        } else if (shift == shiftlim) {
1874d9fdb4Robert Mustacchi            number |= (((Dwarf_Unsigned)1) << shift);
1884d9fdb4Robert Mustacchi        }
18949d3bc9Richard Lowe    }
19049d3bc9Richard Lowe
1914d9fdb4Robert Mustacchi    if (leb128_length) {
19207dc194Richard Lowe        *leb128_length = byte_length;
1934d9fdb4Robert Mustacchi    }
1944d9fdb4Robert Mustacchi    *outval = number;
1954d9fdb4Robert Mustacchi    return DW_DLV_OK;
19649d3bc9Richard Lowe}
197