source: releases/0.12.0/src/common.c @ 289

Last change on this file since 289 was 179, checked in by tim, 15 years ago

fixed a bug in time validation that improves recovery rates

  • Property svn:keywords set to Id
File size: 8.7 KB
Line 
1/*
2 * This file stores code common to the command line tools.
3 * XXX: This should be converted to a proper library.
4 *
5 * Copyright (C) 2005-2008 Timothy D. Morgan
6 * Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 3 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
20 *
21 * $Id: common.c 179 2010-03-13 18:00:15Z tim $
22 */
23
24#include <iconv.h>
25iconv_t conv_desc;
26
27const char* key_special_chars = ",\"\\/";
28const char* subfield_special_chars = ",\"\\|";
29const char* common_special_chars = ",\"\\";
30
31#define REGLOOKUP_VERSION "0.12.1-unreleased"
32
33#define REGLOOKUP_EXIT_OK       0
34#define REGLOOKUP_EXIT_OSERR   71
35#define REGLOOKUP_EXIT_USAGE   64
36#define REGLOOKUP_EXIT_DATAERR 65
37#define REGLOOKUP_EXIT_NOINPUT 66
38
39
40void bailOut(int code, char* message)
41{
42  fprintf(stderr, message);
43  exit(code);
44}
45
46void printMsgs(REGFI_FILE* f)
47{
48  char* msgs = regfi_get_messages(f);
49  if(msgs != NULL)
50  {
51    fprintf(stderr, "%s", msgs);
52    free(msgs);
53  }
54}
55
56void clearMsgs(REGFI_FILE* f)
57{
58  char* msgs = regfi_get_messages(f);
59  if(msgs != NULL)
60    free(msgs);
61}
62
63
64/* Returns a newly malloc()ed string which contains original buffer,
65 * except for non-printable or special characters are quoted in hex
66 * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted
67 * character.  A null terminator is added, since only ascii, not binary,
68 * is returned.
69 */
70static char* quote_buffer(const unsigned char* str, 
71                          unsigned int len, const char* special)
72{
73  unsigned int i, added_len;
74  unsigned int num_written = 0;
75
76  unsigned int buf_len = sizeof(char)*(len+1);
77  char* ret_val = NULL; 
78  char* tmp_buf;
79
80  if(buf_len > 0) 
81    ret_val = malloc(buf_len);
82  if(ret_val == NULL)
83    return NULL;
84
85  for(i=0; i<len; i++)
86  {
87    if(buf_len <= (num_written+5))
88    {
89      /* Expand the buffer by the memory consumption rate seen so far
90       * times the amount of input left to process.  The expansion is bounded
91       * below by a minimum safety increase, and above by the maximum possible
92       * output string length.  This should minimize both the number of
93       * reallocs() and the amount of wasted memory.
94       */
95      added_len = (len-i)*num_written/(i+1);
96      if((buf_len+added_len) > (len*4+1))
97        buf_len = len*4+1;
98      else
99      {
100        if (added_len < 5)
101          buf_len += 5;
102        else
103          buf_len += added_len;
104      }
105
106      tmp_buf = realloc(ret_val, buf_len);
107      if(tmp_buf == NULL)
108      {
109        free(ret_val);
110        return NULL;
111      }
112      ret_val = tmp_buf;
113    }
114   
115    if(str[i] < 32 || str[i] > 126 || strchr(special, str[i]) != NULL)
116    {
117      num_written += snprintf(ret_val + num_written, buf_len - num_written,
118                              "\\x%.2X", str[i]);
119    }
120    else
121      ret_val[num_written++] = str[i];
122  }
123  ret_val[num_written] = '\0';
124
125  return ret_val;
126}
127
128
129/* Returns a newly malloc()ed string which contains original string,
130 * except for non-printable or special characters are quoted in hex
131 * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted
132 * character.
133 */
134static char* quote_string(const char* str, const char* special)
135{
136  unsigned int len;
137
138  if(str == NULL)
139    return NULL;
140
141  len = strlen(str);
142  return quote_buffer((const unsigned char*)str, len, special);
143}
144
145
146/*
147 * Convert a data value to a string for display.  Returns NULL on error,
148 * and the string to display if there is no error, or a non-fatal
149 * error.  On any error (fatal or non-fatal) occurs, (*error_msg) will
150 * be set to a newly allocated string, containing an error message.  If
151 * a memory allocation failure occurs while generating the error
152 * message, both the return value and (*error_msg) will be NULL.  It
153 * is the responsibility of the caller to free both a non-NULL return
154 * value, and a non-NULL (*error_msg).
155 */
156static char* data_to_ascii(REGFI_DATA* data, char** error_msg)
157{
158  char* ret_val;
159  char* cur_quoted;
160  char* tmp_ptr;
161  char* delim;
162  uint32_t ret_val_left, i, tmp_len;
163
164  if(data == NULL || data->size == 0)
165  {
166    *error_msg = (char*)malloc(37);
167    if(*error_msg == NULL)
168      return NULL;
169    strcpy(*error_msg, "Data pointer was NULL or size was 0.");
170    return NULL;
171  }
172  *error_msg = NULL;
173
174
175  if(data->interpreted_size == 0)
176  {
177    *error_msg = (char*)malloc(51);
178    if(*error_msg == NULL)
179      return NULL;
180    strcpy(*error_msg, "Data could not be interpreted, quoting raw buffer.");
181    return quote_buffer(data->raw, data->size, subfield_special_chars);
182  }
183
184  switch (data->type) 
185  {
186  case REG_SZ:
187    ret_val = quote_string((char*)data->interpreted.string, common_special_chars);
188    if(ret_val == NULL && (*error_msg = (char*)malloc(49)) != NULL)
189        strcpy(*error_msg, "Buffer could not be quoted due to unknown error.");
190
191    return ret_val;
192    break;
193
194   
195  case REG_EXPAND_SZ:
196    ret_val = quote_string((char*)data->interpreted.expand_string, 
197                           common_special_chars);
198    if(ret_val == NULL && (*error_msg = (char*)malloc(49)) != NULL)
199        strcpy(*error_msg, "Buffer could not be quoted due to unknown error.");
200
201    return ret_val;
202    break;
203
204  case REG_LINK:
205    ret_val = quote_string((char*)data->interpreted.link, common_special_chars);
206    if(ret_val == NULL && (*error_msg = (char*)malloc(49)) != NULL)
207        strcpy(*error_msg, "Buffer could not be quoted due to unknown error.");
208
209    return ret_val;
210    break;
211
212  case REG_DWORD:
213    ret_val = malloc(sizeof(char)*(8+2+1));
214    if(ret_val == NULL)
215      return NULL;
216
217    sprintf(ret_val, "0x%.8X", data->interpreted.dword);
218    return ret_val;
219    break;
220
221  case REG_DWORD_BE:
222    ret_val = malloc(sizeof(char)*(8+2+1));
223    if(ret_val == NULL)
224      return NULL;
225
226    sprintf(ret_val, "0x%.8X", data->interpreted.dword_be);
227    return ret_val;
228    break;
229
230  case REG_QWORD:
231    ret_val = malloc(sizeof(char)*(16+2+1));
232    if(ret_val == NULL)
233      return NULL;
234
235    sprintf(ret_val, "0x%.16llX", 
236            (long long unsigned int)data->interpreted.qword);
237    return ret_val;
238    break;
239
240  case REG_MULTI_SZ:
241    ret_val_left = data->interpreted_size*4+1;
242    ret_val = malloc(ret_val_left);
243    if(ret_val == NULL)
244      return NULL;
245
246    tmp_ptr = ret_val;
247    tmp_ptr[0] = '\0';
248    delim = "";
249    for(i=0; data->interpreted.multiple_string[i] != NULL; i++)
250    {
251      cur_quoted = quote_string((char*)data->interpreted.multiple_string[i],
252                                subfield_special_chars);
253      if(cur_quoted != NULL && cur_quoted[0] != '\0')
254      {
255        tmp_len = snprintf(tmp_ptr, ret_val_left, "%s%s",delim, cur_quoted);
256        tmp_ptr += tmp_len;
257        ret_val_left -= tmp_len;
258        free(cur_quoted);
259      }
260      delim = "|";
261    }
262
263    return ret_val;
264    break;
265
266   
267  case REG_NONE:
268    return quote_buffer(data->interpreted.none, data->interpreted_size,
269                        common_special_chars);
270
271    break;
272
273  case REG_RESOURCE_LIST:
274    return quote_buffer(data->interpreted.resource_list, data->interpreted_size,
275                        common_special_chars);
276
277    break;
278
279  case REG_FULL_RESOURCE_DESCRIPTOR:
280    return quote_buffer(data->interpreted.full_resource_descriptor, 
281                        data->interpreted_size, common_special_chars);
282
283    break;
284
285  case REG_RESOURCE_REQUIREMENTS_LIST:
286    return quote_buffer(data->interpreted.resource_requirements_list,
287                        data->interpreted_size, common_special_chars);
288
289    break;
290
291  case REG_BINARY:
292    return quote_buffer(data->interpreted.binary, data->interpreted_size,
293                        common_special_chars);
294
295    break;
296
297  default:
298    /* This shouldn't happen, since the regfi routines won't interpret
299     * unknown types, but just as a safety measure against library changes...
300     */
301    *error_msg = (char*)malloc(65);
302    if(*error_msg == NULL)
303      return NULL;
304    sprintf(*error_msg,
305            "Unrecognized registry data type (0x%.8X); quoting as binary.",
306            data->type);
307    return quote_buffer(data->raw, data->size, common_special_chars);
308  }
309   
310  return NULL;
311}
312
313
314static char* get_quoted_keyname(const REGFI_NK_REC* nk)
315{
316  char* ret_val;
317
318  if(nk->keyname == NULL)
319    ret_val = quote_buffer(nk->keyname_raw, nk->name_length, key_special_chars);
320  else
321    ret_val = quote_string(nk->keyname, key_special_chars);
322
323  return ret_val;
324}
325
326
327static char* get_quoted_valuename(const REGFI_VK_REC* vk)
328{
329  char* ret_val;
330
331  if(vk->valuename == NULL)
332    ret_val = quote_buffer(vk->valuename_raw, vk->name_length, 
333                           key_special_chars);
334  else
335    ret_val = quote_string(vk->valuename, key_special_chars);
336
337  return ret_val;
338}
Note: See TracBrowser for help on using the repository browser.