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