1 /*
2   general_utilities.c
3   -------------------
4   A bunch of useful functions that I keep using all the time.
5 
6   Copyright (C) 2000  Eu-Jin Goh
7 
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU General Public License
10   as published by the Free Software Foundation; either version 2
11   of the License, or (at your option) any later version.
12 
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17 
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
21   USA.
22 */
23 
24 /* necessary header files and defines */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29 
30 #include "general_utilities.h"
31 
32 /*
33   Just calls a malloc except that it checks that the memory has actually
34   been allocated. Exits from program if failure to allocate memory.
35   Used to allocate memory for char *.
36 */
37 void *
utl_GetMem(const int size)38 utl_GetMem(const int size)
39 {
40 
41     void *temp = malloc(size);
42 
43     if (!temp)
44     {
45 	utl_HandleError("utl_GetMem");
46     }
47 
48     return temp;
49 }
50 
51 /*
52    allocates memory and copies the src up to size amt
53 */
54 void *
utl_memcpy(const void * src,const int size_to_copy)55 utl_memcpy(const void *src, const int size_to_copy)
56 {
57     void *dest;
58 
59     dest = utl_GetMem(size_to_copy);
60     memcpy(dest, src, size_to_copy);
61 
62     return dest;
63 }
64 
65 /*
66   uses perror to print out the error and then exits
67 */
68 void
utl_HandleError(char * error_msg)69 utl_HandleError(char *error_msg)
70 {
71     perror(error_msg);
72     exit(UTL_FAILURE);
73 }
74 
75 int
utl_GenRandomInt(const int low,const int high)76 utl_GenRandomInt(const int low, const int high)
77 {
78     if(high < low)
79     {
80 	utl_HandleError("utl_GenerateRandomInt");
81     }
82 
83     if(high == low)
84     {
85 	return low;
86     }
87     return ((rand() % (high + 1 - low)) + low);
88 }
89 
90 /*
91    hex encodes and decodes strings
92    Returns length of output buffer.
93    encode returns an even length null terminated string.
94    decode expects an even length null terminated string.
95    memory will be allocated for out if outLen is 0
96 */
utl_HexEncode(unsigned char * in,int in_len,char ** out,int out_len)97 int utl_HexEncode(unsigned char *in, int in_len, char **out, int out_len)
98 {
99     char map[17] = "0123456789ABCDEF";
100     long i;
101 
102     if ((out_len != 0) && (2*in_len+1 > out_len))
103     {
104 	fprintf(stderr, "utl_encode: Input stream too long.\n");
105 	return UTL_FAILURE;
106     }
107 
108     if (out_len == 0)
109     {
110 	*out = utl_GetMem(2*in_len + 1);
111     }
112 
113     for (i=0; i<in_len; i++)
114     {
115 	(*out)[i*2]   = map[(in[i]>>4)&0x0f];
116 	(*out)[i*2+1] = map[(in[i]   )&0x0f];
117     }
118     (*out)[i*2]='\0';
119     return i*2;
120 }
121 
122 /* rewrite this so that it allocates memory as well */
utl_HexDecode(char * in,int in_len,unsigned char ** out,int out_len)123 int utl_HexDecode(char *in, int in_len, unsigned char **out, int out_len)
124 {
125     int v, i;
126     char *from = in;
127 
128     /* checks if the number is odd */
129     if (in_len&1)
130     {
131 	fprintf(stderr, "utl_decode: Cannot decode odd length data.\n");
132 	return UTL_FAILURE;
133     }
134 
135 
136     if(out_len == 0)
137     {
138 	*out = utl_GetMem(in_len/2);
139     }
140     else if (out_len < in_len/2)
141     {
142 	fprintf(stderr, "utl_decode: Out buffer too short.\n");
143 	return UTL_FAILURE;
144     }
145 
146     for (i=0; i<in_len/2; i++) (*out)[i]=0;
147 
148     for (i=0; (i<in_len) && (*from != '\0') && (!isspace(*from)); i++, from++)
149     {
150 	if ((*from >= '0') && (*from <= '9'))
151 	    v= *from-'0';
152 	else if ((*from >= 'A') && (*from <= 'F'))
153 	    v= *from-'A'+10;
154 	else if ((*from >= 'a') && (*from <= 'f'))
155 	    v= *from-'a'+10;
156 	else
157 	{
158 	    fprintf(stderr, "utl_decode: Cannot decode arguments.\n");
159 	    if(out_len == 0)
160 	    {
161 		free(*out);
162 	    }
163 	    return -1;
164 	}
165 	(*out)[i/2] |= v << (long)((!(i&1))*4);
166     }
167 
168     return in_len/2;
169 }
170 
171 /*
172   Prints a char as hex. Converts the char to a unsigned int to avoid
173   sign extensions. This is used because printf does not support
174   printing 0x if the value is 0.
175 */
utl_PrintCharAsHex(char x)176 void utl_PrintCharAsHex(char x)
177 {
178     unsigned int byte=0;
179 
180     memcpy(&byte,&x,1);
181 
182     if(byte)
183     {
184 	printf(" %#.2x",byte);
185     }
186     else
187     {
188 	printf(" 0x00");
189     }
190 }
191