home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / libdwarf / pro_util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  1.8 KB  |  90 lines

  1. /*
  2.     pro_util.c
  3.     $Revision: 1.5 $    $Date: 1993/07/19 22:40:18 $    
  4.     $Source: /cmplrs.src/v4.00/libdwarf/RCS/pro_util.c,v $
  5.  
  6.     utility functions 
  7. */
  8.  
  9. #include <string.h>
  10. #include "pro_incl.h"
  11.  
  12. #define MORE_BYTES      0x80
  13. #define DATA_MASK       0x7f
  14. #define DIGIT_WIDTH     7
  15. #define SIGN_BIT        0x40
  16.  
  17.  
  18. /*-------------------------------------------------------------
  19.     Encode val as a leb128. This encodes it as an unsigned 
  20.     number.
  21. ---------------------------------------------------------------*/
  22. char *
  23. _dwarf_pro_encode_leb128(Dwarf_Unsigned val, int *nbytes)
  24. {
  25.     char *a, *b ;
  26.  
  27.     *nbytes = 0;
  28.     a = (char *)  _dwarf_p_get_alloc(NULL,sizeof(Dwarf_Unsigned)*2);
  29.             /* assuming a doesnt go beyond this size */
  30.     b = a;
  31.       do
  32.         {
  33.             *a = val & DATA_MASK;
  34.             val >>= DIGIT_WIDTH;
  35.             if (val != 0)
  36.               *a |= MORE_BYTES;
  37.         a++;
  38.         (*nbytes)++;
  39.         }
  40.       while (val);
  41.     return b;
  42. }
  43.  
  44.  
  45. char *
  46. _dwarf_pro_encode_signed_leb128 (
  47.     Dwarf_Signed    value,
  48.     int            *nbytes
  49. )
  50. {
  51.   char    *ret_string, *str;
  52.   Dwarf_Signed sign = - (value < 0);
  53.   int more = 1;
  54.  
  55.   if (nbytes != NULL) *nbytes = 0;
  56.   ret_string = str = (char *)_dwarf_p_get_alloc(NULL, sizeof(Dwarf_Signed) * 2);
  57.   if (ret_string == NULL) return(NULL);
  58.  
  59.   do
  60.     {
  61.     char byte = value & DATA_MASK;
  62.     value >>= DIGIT_WIDTH;
  63.  
  64.     /*
  65.      * Remaining chunks would just contain the sign bit, and this chunk
  66.      * has already captured at least one sign bit.
  67.      */
  68.     if (value == sign && (byte & SIGN_BIT) == (sign & SIGN_BIT))
  69.       more = 0;
  70.     else
  71.       byte |= MORE_BYTES;
  72.     *str = byte;
  73.     str++;
  74.     if (nbytes != NULL) (*nbytes)++;
  75.     }
  76.   while (more);
  77.   return(ret_string);
  78. }
  79.  
  80.  
  81. /* Essentially a stub for now. */
  82. void
  83. _dwarf_p_dealloc (
  84.     Dwarf_P_Debug    dbg,
  85.     Dwarf_Small        *ptr
  86. )
  87. {
  88.     dwarf_p_dealloc(ptr,DW_DLA_STRING);
  89. }
  90.