xref: /titanic_50/usr/src/lib/libdwarf/common/pro_encode_nm.c (revision f3e7f55e73a39377d55a030f124cc86b3b66a9cc)
1 /*
2 
3   Copyright (C) 2000,2004 Silicon Graphics, Inc.  All Rights Reserved.
4 
5   This program is free software; you can redistribute it and/or modify it
6   under the terms of version 2.1 of the GNU Lesser General Public License
7   as published by the Free Software Foundation.
8 
9   This program is distributed in the hope that it would be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13   Further, this software is distributed without any warranty that it is
14   free of the rightful claim of any third person regarding infringement
15   or the like.  Any license provided herein, whether implied or
16   otherwise, applies only to this software file.  Patent licenses, if
17   any, provided herein do not apply to combinations of this program with
18   other software, or any other product whatsoever.
19 
20   You should have received a copy of the GNU Lesser General Public
21   License along with this program; if not, write the Free Software
22   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
23   USA.
24 
25   Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
26   Mountain View, CA 94043, or:
27 
28   http://www.sgi.com
29 
30   For further information regarding this notice, see:
31 
32   http://oss.sgi.com/projects/GenInfo/NoticeExplan
33 
34 */
35 
36 
37 
38 #include "config.h"
39 #include "libdwarfdefs.h"
40 #include <string.h>
41 #include "pro_incl.h"
42 
43 #define MORE_BYTES      0x80
44 #define DATA_MASK       0x7f
45 #define DIGIT_WIDTH     7
46 #define SIGN_BIT        0x40
47 
48 
49 /*-------------------------------------------------------------
50         Encode val as a leb128. This encodes it as an unsigned
51         number.
52 ---------------------------------------------------------------*/
53 /* return DW_DLV_ERROR or DW_DLV_OK.
54 ** space to write leb number is provided by caller, with caller
55 ** passing length.
56 ** number of bytes used returned thru nbytes arg
57 */
58 int
_dwarf_pro_encode_leb128_nm(Dwarf_Unsigned val,int * nbytes,char * space,int splen)59 _dwarf_pro_encode_leb128_nm(Dwarf_Unsigned val, int *nbytes,
60                             char *space, int splen)
61 {
62     char *a;
63     char *end = space + splen;
64 
65     a = space;
66     do {
67         unsigned char uc;
68 
69         if (a >= end) {
70             return DW_DLV_ERROR;
71         }
72         uc = val & DATA_MASK;
73         val >>= DIGIT_WIDTH;
74         if (val != 0) {
75             uc |= MORE_BYTES;
76         }
77         *a = uc;
78         a++;
79     } while (val);
80     *nbytes = a - space;
81     return DW_DLV_OK;
82 }
83 
84 /* return DW_DLV_ERROR or DW_DLV_OK.
85 ** space to write leb number is provided by caller, with caller
86 ** passing length.
87 ** number of bytes used returned thru nbytes arg
88 ** encodes a signed number.
89 */
90 int
_dwarf_pro_encode_signed_leb128_nm(Dwarf_Signed value,int * nbytes,char * space,int splen)91 _dwarf_pro_encode_signed_leb128_nm(Dwarf_Signed value, int *nbytes,
92                                    char *space, int splen)
93 {
94     char *str;
95     Dwarf_Signed sign = -(value < 0);
96     int more = 1;
97     char *end = space + splen;
98 
99     str = space;
100 
101     do {
102         unsigned char byte = value & DATA_MASK;
103 
104         value >>= DIGIT_WIDTH;
105 
106         if (str >= end) {
107             return DW_DLV_ERROR;
108         }
109         /*
110          * Remaining chunks would just contain the sign bit, and this chunk
111          * has already captured at least one sign bit.
112          */
113         if (value == sign && ((byte & SIGN_BIT) == (sign & SIGN_BIT))) {
114             more = 0;
115         } else {
116             byte |= MORE_BYTES;
117         }
118         *str = byte;
119         str++;
120     } while (more);
121     *nbytes = str - space;
122     return DW_DLV_OK;
123 }
124