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
68    if (bstream->peekc >= 0) {
69	b = bstream->peekc;
70	bstream->peekc = -1;
71	return b;
72    }
73    if (bstream->ntotal <= 0)
74	return -1;
75    if (bstream->nbuf <= 0) {
76	if (bstream->source & 0x7 || bstream->ntotal < sizeof(uint64_t))
77	    len = sizeof(uint32_t);
78	else
79	    len = sizeof(uint64_t);
80	n = (*bstream->copyin)(bstream->request, (char *)&bstream->buf,
81		bstream->source, len, bstream->cb_token);
82	if (n != len)
83	    return -1;
84	bstream->bufp = (unsigned char *) &bstream->buf;
85	bstream->nbuf = n;
86	bstream->source += n;
87    }
88
89    b = *bstream->bufp++;
90    bstream->nbuf--;
91    bstream->ntotal--;
92    return b;
93}
94
95
96/* uwx_unget_byte: push a byte back onto the byte stream */
97
98int uwx_unget_byte(struct uwx_bstream *bstream, int b)
99{
100    bstream->peekc = b;
101    return 0;
102}
103
104
105/* uwx_get_uleb128: read a ULEB128 value from the byte stream */
106
107int uwx_get_uleb128(struct uwx_bstream *bstream, uint64_t *valp)
108{
109    uint64_t val;
110    int i;
111    int b;
112
113    b = uwx_get_byte(bstream);
114    val = (uint64_t)(b & 0x7f) << 56;
115    for (i = 0; i < 8; i++) {
116	val = val >> 7;
117	if (b & 0x80) {
118	    b = uwx_get_byte(bstream);
119	    val |= (uint64_t)(b & 0x7f) << 56;
120	}
121    }
122    if (b & 0x80) {
123	b = uwx_get_byte(bstream);
124	val |= (uint64_t)(b & 0x7f) << 63;
125    }
126    if (b & 0x80)
127	return -1;
128    *valp = val;
129    return 0;
130}
131
132#if 0
133int uwx_get_uleb128_alt(struct uwx_bstream *bstream, uint64_t *valp)
134{
135    uint64_t val;
136    int b;
137
138    b = uwx_get_byte(bstream);
139    val = b & 0x7f;
140    if (b & 0x80) {
141	b = uwx_get_byte(bstream);
142	val |= (uint64_t)(b & 0x7f) << 7;
143	if (b & 0x80) {
144	    b = uwx_get_byte(bstream);
145	    val |= (uint64_t)(b & 0x7f) << 14;
146	    if (b & 0x80) {
147		b = uwx_get_byte(bstream);
148		val |= (uint64_t)(b & 0x7f) << 21;
149		if (b & 0x80) {
150		    b = uwx_get_byte(bstream);
151		    val |= (uint64_t)(b & 0x7f) << 28;
152		    if (b & 0x80) {
153			b = uwx_get_byte(bstream);
154			val |= (uint64_t)(b & 0x7f) << 35;
155			if (b & 0x80) {
156			    b = uwx_get_byte(bstream);
157			    val |= (uint64_t)(b & 0x7f) << 42;
158			    if (b & 0x80) {
159				b = uwx_get_byte(bstream);
160				val |= (uint64_t)(b & 0x7f) << 49;
161				if (b & 0x80) {
162				    b = uwx_get_byte(bstream);
163				    val |= (uint64_t)(b & 0x7f) << 56;
164				    if (b & 0x80) {
165					b = uwx_get_byte(bstream);
166					val |= (uint64_t)(b & 0x7f) << 63;
167					if (b & 0x80)
168					    return -1;
169				    }
170				}
171			    }
172			}
173		    }
174		}
175	    }
176	}
177    }
178    *valp = val;
179    return 0;
180}
181#endif
182