pro_encode.c 1.47 KB
/*
    pro_encode.c

    $Revision: 1.1.1.1 $				$Date: 2002/05/02 03:29:20 $
*/

#include "xlateTypes.h"

/* 
    This routine encodes the given unsigned number in leb128 mode,
    and stores the bytes at address buffer.  It returns the number 
    of bytes in the encoding.  It is the user's responsibility to
    make sure there is enough storage at buffer.  The details of
    leb128 encoding are in the Dwarf document.
*/
Elf32_Sword
_xlate_leb128_unsigned_encode (Elf32_Word number, char *buffer)
{
    char	*bufPtr;
    char	byte;

    bufPtr = buffer;

    do {
	byte = (number & 0x7f);
	number >>= 7;

	if (number != 0)
	    byte |= 0x80;

	*bufPtr = byte;
	bufPtr++;
    } while (number != 0);

    return bufPtr - buffer;
}


/* 
    This routine encodes the given signed number in leb128 mode,
    and stores the bytes at address buffer.  It returns the number 
    of bytes in the encoding.  It is the user's responsibility to
    make sure there is enough storage at buffer.  The details of
    leb128 encoding are in the Dwarf document.
*/
Elf32_Sword
_xlate_leb128_signed_encode (Elf32_Sword number, char *buffer)
{
    Elf32_Sword		sign;
    char		*bufPtr;
    char		more;
    char		byte;

    sign = (number < 0) ? -1 : 0;
    bufPtr = buffer;
    more = 1;

    do {
	byte = (number & 0x7f);
	number >>= 7;

	if (number == sign && (byte & 0x40) == ((char)sign & 0x40))
	    more = 0;
	else
	    byte |= 0x80;

	*bufPtr = byte;
	bufPtr++;
    } while (more);

    return bufPtr - buffer;
}