1115013Smarcel/* 2160157SmarcelCopyright (c) 2003-2006 Hewlett-Packard Development Company, L.P. 3121642SmarcelPermission is hereby granted, free of charge, to any person 4121642Smarcelobtaining a copy of this software and associated documentation 5121642Smarcelfiles (the "Software"), to deal in the Software without 6121642Smarcelrestriction, including without limitation the rights to use, 7121642Smarcelcopy, modify, merge, publish, distribute, sublicense, and/or sell 8121642Smarcelcopies of the Software, and to permit persons to whom the 9121642SmarcelSoftware is furnished to do so, subject to the following 10121642Smarcelconditions: 11115013Smarcel 12121642SmarcelThe above copyright notice and this permission notice shall be 13121642Smarcelincluded in all copies or substantial portions of the Software. 14121642Smarcel 15121642SmarcelTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16121642SmarcelEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17121642SmarcelOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18121642SmarcelNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19121642SmarcelHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20121642SmarcelWHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21121642SmarcelFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22121642SmarcelOTHER DEALINGS IN THE SOFTWARE. 23121642Smarcel*/ 24121642Smarcel 25115013Smarcel#include "uwx_env.h" 26115013Smarcel#include "uwx_bstream.h" 27115013Smarcel 28115013Smarcel 29115013Smarcel/* uwx_init_bstream: initialize a byte stream for reading */ 30115013Smarcel 31115013Smarcelvoid uwx_init_bstream( 32115013Smarcel struct uwx_bstream *bstream, 33115013Smarcel struct uwx_env *env, 34115013Smarcel uint64_t source, 35115013Smarcel unsigned int len, 36115013Smarcel int request) 37115013Smarcel{ 38115013Smarcel bstream->buf = 0; 39115013Smarcel if (env->remote) { 40115013Smarcel bstream->source = source; 41115013Smarcel bstream->bufp = (unsigned char *) &bstream->buf; 42115013Smarcel bstream->nbuf = 0; 43115013Smarcel bstream->copyin = env->copyin; 44115013Smarcel bstream->cb_token = env->cb_token; 45115013Smarcel bstream->request = request; 46115013Smarcel } 47115013Smarcel else { 48115013Smarcel bstream->source = 0; 49160157Smarcel bstream->bufp = (unsigned char *) (intptr_t) source; 50115013Smarcel bstream->nbuf = len; 51115013Smarcel bstream->copyin = 0; 52115013Smarcel bstream->cb_token = 0; 53115013Smarcel bstream->request = 0; 54115013Smarcel } 55115013Smarcel bstream->ntotal = len; 56115013Smarcel bstream->peekc = -1; 57115013Smarcel} 58115013Smarcel 59115013Smarcel 60115013Smarcel/* uwx_get_byte: read the next byte from the byte stream */ 61115013Smarcel 62115013Smarcelint uwx_get_byte(struct uwx_bstream *bstream) 63115013Smarcel{ 64115013Smarcel int len; 65115013Smarcel int n; 66115013Smarcel int b; 67115013Smarcel 68115013Smarcel if (bstream->peekc >= 0) { 69115013Smarcel b = bstream->peekc; 70115013Smarcel bstream->peekc = -1; 71115013Smarcel return b; 72115013Smarcel } 73115013Smarcel if (bstream->ntotal <= 0) 74115013Smarcel return -1; 75115013Smarcel if (bstream->nbuf <= 0) { 76121642Smarcel if (bstream->source & 0x7 || bstream->ntotal < sizeof(uint64_t)) 77115013Smarcel len = sizeof(uint32_t); 78115013Smarcel else 79115013Smarcel len = sizeof(uint64_t); 80115013Smarcel n = (*bstream->copyin)(bstream->request, (char *)&bstream->buf, 81115013Smarcel bstream->source, len, bstream->cb_token); 82115013Smarcel if (n != len) 83115013Smarcel return -1; 84115013Smarcel bstream->bufp = (unsigned char *) &bstream->buf; 85115013Smarcel bstream->nbuf = n; 86115013Smarcel bstream->source += n; 87115013Smarcel } 88115013Smarcel 89115013Smarcel b = *bstream->bufp++; 90115013Smarcel bstream->nbuf--; 91115013Smarcel bstream->ntotal--; 92115013Smarcel return b; 93115013Smarcel} 94115013Smarcel 95115013Smarcel 96115013Smarcel/* uwx_unget_byte: push a byte back onto the byte stream */ 97115013Smarcel 98115013Smarcelint uwx_unget_byte(struct uwx_bstream *bstream, int b) 99115013Smarcel{ 100115013Smarcel bstream->peekc = b; 101115013Smarcel return 0; 102115013Smarcel} 103115013Smarcel 104115013Smarcel 105115013Smarcel/* uwx_get_uleb128: read a ULEB128 value from the byte stream */ 106115013Smarcel 107115013Smarcelint uwx_get_uleb128(struct uwx_bstream *bstream, uint64_t *valp) 108115013Smarcel{ 109115013Smarcel uint64_t val; 110115013Smarcel int i; 111115013Smarcel int b; 112115013Smarcel 113115013Smarcel b = uwx_get_byte(bstream); 114115013Smarcel val = (uint64_t)(b & 0x7f) << 56; 115115013Smarcel for (i = 0; i < 8; i++) { 116115013Smarcel val = val >> 7; 117115013Smarcel if (b & 0x80) { 118115013Smarcel b = uwx_get_byte(bstream); 119115013Smarcel val |= (uint64_t)(b & 0x7f) << 56; 120115013Smarcel } 121115013Smarcel } 122115013Smarcel if (b & 0x80) { 123115013Smarcel b = uwx_get_byte(bstream); 124115013Smarcel val |= (uint64_t)(b & 0x7f) << 63; 125115013Smarcel } 126115013Smarcel if (b & 0x80) 127115013Smarcel return -1; 128115013Smarcel *valp = val; 129115013Smarcel return 0; 130115013Smarcel} 131115013Smarcel 132160163Smarcel#if 0 133115013Smarcelint uwx_get_uleb128_alt(struct uwx_bstream *bstream, uint64_t *valp) 134115013Smarcel{ 135115013Smarcel uint64_t val; 136115013Smarcel int b; 137115013Smarcel 138115013Smarcel b = uwx_get_byte(bstream); 139115013Smarcel val = b & 0x7f; 140115013Smarcel if (b & 0x80) { 141115013Smarcel b = uwx_get_byte(bstream); 142115013Smarcel val |= (uint64_t)(b & 0x7f) << 7; 143115013Smarcel if (b & 0x80) { 144115013Smarcel b = uwx_get_byte(bstream); 145115013Smarcel val |= (uint64_t)(b & 0x7f) << 14; 146115013Smarcel if (b & 0x80) { 147115013Smarcel b = uwx_get_byte(bstream); 148115013Smarcel val |= (uint64_t)(b & 0x7f) << 21; 149115013Smarcel if (b & 0x80) { 150115013Smarcel b = uwx_get_byte(bstream); 151115013Smarcel val |= (uint64_t)(b & 0x7f) << 28; 152115013Smarcel if (b & 0x80) { 153115013Smarcel b = uwx_get_byte(bstream); 154115013Smarcel val |= (uint64_t)(b & 0x7f) << 35; 155115013Smarcel if (b & 0x80) { 156115013Smarcel b = uwx_get_byte(bstream); 157115013Smarcel val |= (uint64_t)(b & 0x7f) << 42; 158115013Smarcel if (b & 0x80) { 159115013Smarcel b = uwx_get_byte(bstream); 160115013Smarcel val |= (uint64_t)(b & 0x7f) << 49; 161115013Smarcel if (b & 0x80) { 162115013Smarcel b = uwx_get_byte(bstream); 163115013Smarcel val |= (uint64_t)(b & 0x7f) << 56; 164115013Smarcel if (b & 0x80) { 165115013Smarcel b = uwx_get_byte(bstream); 166115013Smarcel val |= (uint64_t)(b & 0x7f) << 63; 167115013Smarcel if (b & 0x80) 168115013Smarcel return -1; 169115013Smarcel } 170115013Smarcel } 171115013Smarcel } 172115013Smarcel } 173115013Smarcel } 174115013Smarcel } 175115013Smarcel } 176115013Smarcel } 177115013Smarcel } 178115013Smarcel *valp = val; 179115013Smarcel return 0; 180115013Smarcel} 181160163Smarcel#endif 182