2 Copyright (c) 2003-2006 Hewlett-Packard Development Company, L.P.
3 Permission is hereby granted, free of charge, to any person
4 obtaining a copy of this software and associated documentation
5 files (the "Software"), to deal in the Software without
6 restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 OTHER DEALINGS IN THE SOFTWARE.
26 #include "uwx_bstream.h"
29 /* uwx_init_bstream: initialize a byte stream for reading */
31 void uwx_init_bstream(
32 struct uwx_bstream *bstream,
40 bstream->source = source;
41 bstream->bufp = (unsigned char *) &bstream->buf;
43 bstream->copyin = env->copyin;
44 bstream->cb_token = env->cb_token;
45 bstream->request = request;
49 bstream->bufp = (unsigned char *) (intptr_t) source;
52 bstream->cb_token = 0;
55 bstream->ntotal = len;
60 /* uwx_get_byte: read the next byte from the byte stream */
62 int uwx_get_byte(struct uwx_bstream *bstream)
68 if (bstream->peekc >= 0) {
73 if (bstream->ntotal <= 0)
75 if (bstream->nbuf <= 0) {
76 if (bstream->source & 0x7 || bstream->ntotal < sizeof(uint64_t))
77 len = sizeof(uint32_t);
79 len = sizeof(uint64_t);
80 n = (*bstream->copyin)(bstream->request, (char *)&bstream->buf,
81 bstream->source, len, bstream->cb_token);
84 bstream->bufp = (unsigned char *) &bstream->buf;
96 /* uwx_unget_byte: push a byte back onto the byte stream */
98 int uwx_unget_byte(struct uwx_bstream *bstream, int b)
105 /* uwx_get_uleb128: read a ULEB128 value from the byte stream */
107 int uwx_get_uleb128(struct uwx_bstream *bstream, uint64_t *valp)
113 b = uwx_get_byte(bstream);
114 val = (uint64_t)(b & 0x7f) << 56;
115 for (i = 0; i < 8; i++) {
118 b = uwx_get_byte(bstream);
119 val |= (uint64_t)(b & 0x7f) << 56;
123 b = uwx_get_byte(bstream);
124 val |= (uint64_t)(b & 0x7f) << 63;
133 int uwx_get_uleb128_alt(struct uwx_bstream *bstream, uint64_t *valp)
138 b = uwx_get_byte(bstream);
141 b = uwx_get_byte(bstream);
142 val |= (uint64_t)(b & 0x7f) << 7;
144 b = uwx_get_byte(bstream);
145 val |= (uint64_t)(b & 0x7f) << 14;
147 b = uwx_get_byte(bstream);
148 val |= (uint64_t)(b & 0x7f) << 21;
150 b = uwx_get_byte(bstream);
151 val |= (uint64_t)(b & 0x7f) << 28;
153 b = uwx_get_byte(bstream);
154 val |= (uint64_t)(b & 0x7f) << 35;
156 b = uwx_get_byte(bstream);
157 val |= (uint64_t)(b & 0x7f) << 42;
159 b = uwx_get_byte(bstream);
160 val |= (uint64_t)(b & 0x7f) << 49;
162 b = uwx_get_byte(bstream);
163 val |= (uint64_t)(b & 0x7f) << 56;
165 b = uwx_get_byte(bstream);
166 val |= (uint64_t)(b & 0x7f) << 63;