Deleted Added
sdiff udiff text old ( 121642 ) new ( 160157 )
full compact
1/*
2Copyright (c) 2003-2006 Hewlett-Packard Development Company, L.P.
3Permission is hereby granted, free of charge, to any person
4obtaining a copy of this software and associated documentation
5files (the "Software"), to deal in the Software without
6restriction, including without limitation the rights to use,
7copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the
9Software is furnished to do so, subject to the following
10conditions:
11
12The above copyright notice and this permission notice shall be
13included in all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22OTHER DEALINGS IN THE SOFTWARE.
23*/
24
25#include "uwx_env.h"
26#include "uwx_bstream.h"
27
28
29/* uwx_init_bstream: initialize a byte stream for reading */
30
31void uwx_init_bstream(
32 struct uwx_bstream *bstream,
33 struct uwx_env *env,
34 uint64_t source,
35 unsigned int len,
36 int request)
37{
38 bstream->buf = 0;
39 if (env->remote) {
40 bstream->source = source;
41 bstream->bufp = (unsigned char *) &bstream->buf;
42 bstream->nbuf = 0;
43 bstream->copyin = env->copyin;
44 bstream->cb_token = env->cb_token;
45 bstream->request = request;
46 }
47 else {
48 bstream->source = 0;
49 bstream->bufp = (unsigned char *) (intptr_t) source;
50 bstream->nbuf = len;
51 bstream->copyin = 0;
52 bstream->cb_token = 0;
53 bstream->request = 0;
54 }
55 bstream->ntotal = len;
56 bstream->peekc = -1;
57}
58
59
60/* uwx_get_byte: read the next byte from the byte stream */
61
62int uwx_get_byte(struct uwx_bstream *bstream)
63{
64 int len;
65 int n;
66 int b;
67 uint32_t *wp;
68 uint64_t *dp;
69
70 if (bstream->peekc >= 0) {
71 b = bstream->peekc;
72 bstream->peekc = -1;
73 return b;
74 }
75 if (bstream->ntotal <= 0)
76 return -1;
77 if (bstream->nbuf <= 0) {
78 if (bstream->source & 0x7 || bstream->ntotal < sizeof(uint64_t))
79 len = sizeof(uint32_t);
80 else
81 len = sizeof(uint64_t);
82 n = (*bstream->copyin)(bstream->request, (char *)&bstream->buf,
83 bstream->source, len, bstream->cb_token);
84 if (n != len)
85 return -1;
86 bstream->bufp = (unsigned char *) &bstream->buf;
87 bstream->nbuf = n;
88 bstream->source += n;
89 }
90
91 b = *bstream->bufp++;
92 bstream->nbuf--;
93 bstream->ntotal--;
94 return b;
95}
96
97
98/* uwx_unget_byte: push a byte back onto the byte stream */
99
100int uwx_unget_byte(struct uwx_bstream *bstream, int b)
101{
102 bstream->peekc = b;
103 return 0;
104}
105
106
107/* uwx_get_uleb128: read a ULEB128 value from the byte stream */
108
109int uwx_get_uleb128(struct uwx_bstream *bstream, uint64_t *valp)
110{
111 uint64_t val;
112 int i;
113 int b;
114
115 b = uwx_get_byte(bstream);
116 val = (uint64_t)(b & 0x7f) << 56;
117 for (i = 0; i < 8; i++) {
118 val = val >> 7;
119 if (b & 0x80) {
120 b = uwx_get_byte(bstream);
121 val |= (uint64_t)(b & 0x7f) << 56;
122 }
123 }
124 if (b & 0x80) {
125 b = uwx_get_byte(bstream);
126 val |= (uint64_t)(b & 0x7f) << 63;
127 }
128 if (b & 0x80)
129 return -1;
130 *valp = val;
131 return 0;
132}
133
134int uwx_get_uleb128_alt(struct uwx_bstream *bstream, uint64_t *valp)
135{
136 uint64_t val;
137 int b;
138
139 b = uwx_get_byte(bstream);
140 val = b & 0x7f;
141 if (b & 0x80) {
142 b = uwx_get_byte(bstream);
143 val |= (uint64_t)(b & 0x7f) << 7;
144 if (b & 0x80) {
145 b = uwx_get_byte(bstream);
146 val |= (uint64_t)(b & 0x7f) << 14;
147 if (b & 0x80) {
148 b = uwx_get_byte(bstream);
149 val |= (uint64_t)(b & 0x7f) << 21;
150 if (b & 0x80) {
151 b = uwx_get_byte(bstream);
152 val |= (uint64_t)(b & 0x7f) << 28;
153 if (b & 0x80) {
154 b = uwx_get_byte(bstream);
155 val |= (uint64_t)(b & 0x7f) << 35;
156 if (b & 0x80) {
157 b = uwx_get_byte(bstream);
158 val |= (uint64_t)(b & 0x7f) << 42;
159 if (b & 0x80) {
160 b = uwx_get_byte(bstream);
161 val |= (uint64_t)(b & 0x7f) << 49;
162 if (b & 0x80) {
163 b = uwx_get_byte(bstream);
164 val |= (uint64_t)(b & 0x7f) << 56;
165 if (b & 0x80) {
166 b = uwx_get_byte(bstream);
167 val |= (uint64_t)(b & 0x7f) << 63;
168 if (b & 0x80)
169 return -1;
170 }
171 }
172 }
173 }
174 }
175 }
176 }
177 }
178 }
179 *valp = val;
180 return 0;
181}