1 /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19 #include <rdr/HexInStream.h>
20 #include <rdr/Exception.h>
21
22 #include <stdlib.h>
23 #include <ctype.h>
24
25 using namespace rdr;
26
min(int a,int b)27 static inline int min(int a, int b) {return a<b ? a : b;}
28
HexInStream(InStream & is)29 HexInStream::HexInStream(InStream& is)
30 : in_stream(is)
31 {
32 }
33
~HexInStream()34 HexInStream::~HexInStream() {
35 }
36
37
readHexAndShift(char c,int * v)38 bool HexInStream::readHexAndShift(char c, int* v) {
39 c=tolower(c);
40 if ((c >= '0') && (c <= '9'))
41 *v = (*v << 4) + (c - '0');
42 else if ((c >= 'a') && (c <= 'f'))
43 *v = (*v << 4) + (c - 'a' + 10);
44 else
45 return false;
46 return true;
47 }
48
hexStrToBin(const char * s,char ** data,size_t * length)49 bool HexInStream::hexStrToBin(const char* s, char** data, size_t* length) {
50 size_t l=strlen(s);
51 if ((l % 2) == 0) {
52 delete [] *data;
53 *data = 0; *length = 0;
54 if (l == 0)
55 return true;
56 *data = new char[l/2];
57 *length = l/2;
58 for(size_t i=0;i<l;i+=2) {
59 int byte = 0;
60 if (!readHexAndShift(s[i], &byte) ||
61 !readHexAndShift(s[i+1], &byte))
62 goto decodeError;
63 (*data)[i/2] = byte;
64 }
65 return true;
66 }
67 decodeError:
68 delete [] *data;
69 *data = 0;
70 *length = 0;
71 return false;
72 }
73
74
fillBuffer(size_t maxSize)75 bool HexInStream::fillBuffer(size_t maxSize) {
76 if (!in_stream.hasData(2))
77 return false;
78
79 size_t length = min(in_stream.avail()/2, maxSize);
80 const U8* iptr = in_stream.getptr(length*2);
81
82 U8* optr = (U8*) end;
83 for (size_t i=0; i<length; i++) {
84 int v = 0;
85 readHexAndShift(iptr[i*2], &v);
86 readHexAndShift(iptr[i*2+1], &v);
87 optr[i] = v;
88 }
89
90 in_stream.setptr(length*2);
91 end += length;
92
93 return true;
94 }
95