1 /******************************************************************************
2  *
3  * File :    pgchiputilities.cpp
4  * Project:  PGCHIP Driver
5  * Purpose:  Utility functions for POSTGIS CHIP/GDAL Driver
6  * Author:   Benjamin Simon, noumayoss@gmail.com
7  *
8  ******************************************************************************
9  * Copyright (c) 2005, Benjamin Simon, noumayoss@gmail.com
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  ******************************************************************************
29  *
30  * Revision 1.1  2005/08/29 bsimon
31  * New
32  *
33  */
34 
35 #include "pgchip.h"
36 
37 /************************************************************************/
38 /* ==================================================================== */
39 /*				Utility Hex Functions                   */
40 /* ==================================================================== */
41 /************************************************************************/
42 
pgch_deparse_hex(unsigned char str,unsigned char * result)43 void pgch_deparse_hex(unsigned char str, unsigned char *result){
44 
45 	int	input_high;
46 	int  input_low;
47 
48 	input_high = (str>>4);
49 	input_low = (str & 0x0F);
50 
51 	switch (input_high)
52 	{
53 		case 0:
54 			result[0] = '0';
55 			break;
56 		case 1:
57 			result[0] = '1';
58 			break;
59 		case 2:
60 			result[0] = '2';
61 			break;
62 		case 3:
63 			result[0] = '3';
64 			break;
65 		case 4:
66 			result[0] = '4';
67 			break;
68 		case 5:
69 			result[0] = '5';
70 			break;
71 		case 6:
72 			result[0] = '6';
73 			break;
74 		case 7:
75 			result[0] = '7';
76 			break;
77 		case 8:
78 			result[0] = '8';
79 			break;
80 		case 9:
81 			result[0] = '9';
82 			break;
83 		case 10:
84 			result[0] = 'A';
85 			break;
86 		case 11:
87 			result[0] = 'B';
88 			break;
89 		case 12:
90 			result[0] = 'C';
91 			break;
92 		case 13:
93 			result[0] = 'D';
94 			break;
95 		case 14:
96 			result[0] = 'E';
97 			break;
98 		case 15:
99 			result[0] = 'F';
100 			break;
101 	}
102 
103 	switch (input_low)
104 	{
105 		case 0:
106 			result[1] = '0';
107 			break;
108 		case 1:
109 			result[1] = '1';
110 			break;
111 		case 2:
112 			result[1] = '2';
113 			break;
114 		case 3:
115 			result[1] = '3';
116 			break;
117 		case 4:
118 			result[1] = '4';
119 			break;
120 		case 5:
121 			result[1] = '5';
122 			break;
123 		case 6:
124 			result[1] = '6';
125 			break;
126 		case 7:
127 			result[1] = '7';
128 			break;
129 		case 8:
130 			result[1] = '8';
131 			break;
132 		case 9:
133 			result[1] = '9';
134 			break;
135 		case 10:
136 			result[1] = 'A';
137 			break;
138 		case 11:
139 			result[1] = 'B';
140 			break;
141 		case 12:
142 			result[1] = 'C';
143 			break;
144 		case 13:
145 			result[1] = 'D';
146 			break;
147 		case 14:
148 			result[1] = 'E';
149 			break;
150 		case 15:
151 			result[1] = 'F';
152 			break;
153 	}
154 }
155 
156 //given a string with at least 2 chars in it, convert them to
157 // a byte value.  No error checking done!
parse_hex(char * str)158 unsigned char parse_hex(char *str){
159 
160 	//do this a little brute force to make it faster
161 
162 	unsigned char		result_high = 0;
163 	unsigned char		result_low = 0;
164 
165 	switch (str[0])
166 	{
167 		case '0' :
168 			result_high = 0;
169 			break;
170 		case '1' :
171 			result_high = 1;
172 			break;
173 		case '2' :
174 			result_high = 2;
175 			break;
176 		case '3' :
177 			result_high = 3;
178 			break;
179 		case '4' :
180 			result_high = 4;
181 			break;
182 		case '5' :
183 			result_high = 5;
184 			break;
185 		case '6' :
186 			result_high = 6;
187 			break;
188 		case '7' :
189 			result_high = 7;
190 			break;
191 		case '8' :
192 			result_high = 8;
193 			break;
194 		case '9' :
195 			result_high = 9;
196 			break;
197 		case 'A' :
198 			result_high = 10;
199 			break;
200 		case 'B' :
201 			result_high = 11;
202 			break;
203 		case 'C' :
204 			result_high = 12;
205 			break;
206 		case 'D' :
207 			result_high = 13;
208 			break;
209 		case 'E' :
210 			result_high = 14;
211 			break;
212 		case 'F' :
213 			result_high = 15;
214 			break;
215 	}
216 	switch (str[1])
217 	{
218 		case '0' :
219 			result_low = 0;
220 			break;
221 		case '1' :
222 			result_low = 1;
223 			break;
224 		case '2' :
225 			result_low = 2;
226 			break;
227 		case '3' :
228 			result_low = 3;
229 			break;
230 		case '4' :
231 			result_low = 4;
232 			break;
233 		case '5' :
234 			result_low = 5;
235 			break;
236 		case '6' :
237 			result_low = 6;
238 			break;
239 		case '7' :
240 			result_low = 7;
241 			break;
242 		case '8' :
243 			result_low = 8;
244 			break;
245 		case '9' :
246 			result_low = 9;
247 			break;
248 		case 'A' :
249 			result_low = 10;
250 			break;
251 		case 'B' :
252 			result_low = 11;
253 			break;
254 		case 'C' :
255 			result_low = 12;
256 			break;
257 		case 'D' :
258 			result_low = 13;
259 			break;
260 		case 'E' :
261 			result_low = 14;
262 			break;
263 		case 'F' :
264 			result_low = 15;
265 			break;
266 	}
267 	return (unsigned char) ((result_high<<4) + result_low);
268 }
269 
270 /* Parse an hex string */
parse_hex_string(unsigned char * strOut,char * strIn,int length)271 void parse_hex_string(unsigned char *strOut,char *strIn,int length){
272 
273     int i;
274     for(i=0;i<length;i++){
275         //printf("Before = %c\n",strIn[i]);
276         strOut[i] = parse_hex(&strIn[i]);
277         //printf("After = %c\n",strOut[i]);
278         }
279 
280 }
281 
282 /* Deparse an hex string */
deparse_hex_string(unsigned char * strOut,char * strIn,int length)283 void deparse_hex_string(unsigned char *strOut,char *strIn,int length){
284 
285     int i;
286 
287     for(i=0;i<length;i++)
288         pgch_deparse_hex(strIn[i],&strOut[i]);
289 
290 }
291