- Timestamp:
- 07/17/06 21:56:59 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/reglookup.c
r59 r61 3 3 * Gerald Carter''s regfio interface. 4 4 * 5 * Copyright (C) 2005 Timothy D. Morgan5 * Copyright (C) 2005-2006 Timothy D. Morgan 6 6 * Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com 7 7 * … … 28 28 #include <strings.h> 29 29 #include <time.h> 30 #include <iconv.h> 30 31 #include "../include/regfio.h" 31 32 #include "../include/void_stack.h" … … 43 44 /* Other globals */ 44 45 const char* special_chars = ",\"\\"; 46 iconv_t conv_desc; 47 45 48 46 49 void bailOut(int code, char* message) … … 54 57 * except for non-printable or special characters are quoted in hex 55 58 * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted 56 * character. A null terminator is added, as only ascii, not binary,59 * character. A null terminator is added, since only ascii, not binary, 57 60 * is returned. 58 61 */ … … 60 63 unsigned int len, const char* special) 61 64 { 62 unsigned int i; 63 unsigned int num_written=0; 64 unsigned int out_len = sizeof(char)*len+1; 65 char* ret_val = malloc(out_len); 65 unsigned int i, added_len; 66 unsigned int num_written = 0; 67 68 unsigned int buf_len = sizeof(char)*(len+1); 69 char* ret_val = malloc(buf_len); 70 char* tmp_buf; 66 71 67 72 if(ret_val == NULL) … … 70 75 for(i=0; i<len; i++) 71 76 { 77 if(buf_len <= (num_written+5)) 78 { 79 /* Expand the buffer by the memory consumption rate seen so far 80 * times the amount of input left to process. The expansion is bounded 81 * below by a minimum safety increase, and above by the maximum possible 82 * output string length. 83 */ 84 added_len = (len-i)*num_written/(i+1); 85 if((buf_len+added_len) > (len*4+1)) 86 buf_len = len*4+1; 87 else 88 { 89 if (added_len < 5) 90 buf_len += 5; 91 else 92 buf_len += added_len; 93 } 94 95 tmp_buf = realloc(ret_val, buf_len); 96 if(tmp_buf == NULL) 97 { 98 free(ret_val); 99 return NULL; 100 } 101 ret_val = tmp_buf; 102 } 103 72 104 if(str[i] < 32 || str[i] > 126 || strchr(special, str[i]) != NULL) 73 105 { 74 out_len += 3; 75 /* XXX: may not be the most efficient way of getting enough memory. */ 76 ret_val = realloc(ret_val, out_len); 77 if(ret_val == NULL) 78 break; 79 num_written += snprintf(ret_val+num_written, (out_len)-num_written, 106 num_written += snprintf(ret_val + num_written, buf_len - num_written, 80 107 "\\x%.2X", str[i]); 81 108 } … … 107 134 108 135 /* 109 * Convert from UniCode to Ascii ... Does not take into account other lang 110 * Restrict by ascii_max if > 0 136 * Convert from Unicode to ASCII. 111 137 */ 112 static int uni_to_ascii(unsigned char *uni, unsigned char *ascii, 113 int ascii_max, int uni_max) 114 { 115 int i = 0; 116 117 while (i < ascii_max && (uni[i*2] || uni[i*2+1])) 118 { 119 if (uni_max > 0 && (i*2) >= uni_max) break; 120 ascii[i] = uni[i*2]; 121 i++; 122 } 123 ascii[i] = '\0'; 124 125 return i; 138 static int uni_to_ascii(unsigned char* uni, char* ascii, 139 unsigned int uni_max, unsigned int ascii_max) 140 { 141 char* inbuf = (char*)uni; 142 char* outbuf = ascii; 143 int ret; 144 145 /* Set up conversion descriptor. */ 146 conv_desc = iconv_open("US-ASCII", "UTF-16LE"); 147 148 ret = iconv(conv_desc, &inbuf, &uni_max, &outbuf, &ascii_max); 149 if(ret == -1) 150 { 151 iconv_close(conv_desc); 152 return -1; 153 } 154 155 iconv_close(conv_desc); 156 return strlen(ascii); 126 157 } 127 158 … … 130 161 * Convert a data value to a string for display 131 162 */ 132 static unsignedchar* data_to_ascii(unsigned char *datap, int len, int type)133 { 134 unsigned char *asciip;163 static char* data_to_ascii(unsigned char *datap, int len, int type) 164 { 165 char* asciip; 135 166 unsigned int i; 136 167 unsigned short num_nulls; 137 unsignedchar* ascii;168 char* ascii; 138 169 unsigned char* cur_str; 139 unsignedchar* cur_ascii;170 char* cur_ascii; 140 171 char* cur_quoted; 141 172 unsigned int cur_str_len; … … 145 176 switch (type) 146 177 { 178 /* REG_LINK is a symbolic link, stored as a unicode string. */ 179 case REG_LINK: 147 180 case REG_SZ: 148 181 ascii_max = sizeof(char)*len; … … 151 184 return NULL; 152 185 153 /* XXX: This has to be fixed. It has to be UNICODE */ 154 uni_to_ascii(datap, ascii, len, ascii_max); 155 cur_quoted = quote_string((char*)ascii, special_chars); 186 if(uni_to_ascii(datap, ascii, len, ascii_max) < 0) 187 { 188 free(ascii); 189 return NULL; 190 } 191 cur_quoted = quote_string(ascii, special_chars); 156 192 free(ascii); 157 return (unsigned char*)cur_quoted;193 return cur_quoted; 158 194 break; 159 195 196 /* XXX: This could be Unicode or ASCII. Are we processing it 197 * correctly in both cases? 198 */ 160 199 case REG_EXPAND_SZ: 161 200 ascii_max = sizeof(char)*len; … … 164 203 return NULL; 165 204 166 uni_to_ascii(datap, ascii, len, ascii_max); 167 cur_quoted = quote_string((char*)ascii, special_chars); 205 if(uni_to_ascii(datap, ascii, len, ascii_max) < 0) 206 { 207 free(ascii); 208 return NULL; 209 } 210 cur_quoted = quote_string(ascii, special_chars); 168 211 free(ascii); 169 return (unsigned char*)cur_quoted;212 return cur_quoted; 170 213 break; 171 214 … … 176 219 return NULL; 177 220 178 snprintf( (char*)ascii, ascii_max, "0x%.2X%.2X%.2X%.2X",221 snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X", 179 222 datap[0], datap[1], datap[2], datap[3]); 180 223 return ascii; … … 187 230 return NULL; 188 231 189 snprintf( (char*)ascii, ascii_max, "0x%.2X%.2X%.2X%.2X",232 snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X", 190 233 datap[3], datap[2], datap[1], datap[0]); 191 234 return ascii; … … 226 269 if(num_nulls == 2) 227 270 { 228 uni_to_ascii(cur_str, cur_ascii, cur_str_max, 0); 229 cur_quoted = quote_string((char*)cur_ascii, ",|\"\\"); 230 alen = snprintf((char*)asciip, str_rem, "%s", cur_quoted); 271 if(uni_to_ascii(cur_str, cur_ascii, cur_str_max, cur_str_max) < 0) 272 { 273 free(ascii); 274 free(cur_ascii); 275 free(cur_str); 276 return NULL; 277 } 278 279 cur_quoted = quote_string(cur_ascii, ",|\"\\"); 280 alen = snprintf(asciip, str_rem, "%s", cur_quoted); 231 281 asciip += alen; 232 282 str_rem -= alen; … … 237 287 else 238 288 { 239 alen = snprintf((char*)asciip, str_rem, "%c", '|'); 240 asciip += alen; 241 str_rem -= alen; 289 if(str_rem > 0) 290 { 291 asciip[0] = '|'; 292 asciip[1] = '\0'; 293 asciip++; 294 str_rem--; 295 } 242 296 memset(cur_str, 0, cur_str_max); 243 297 cur_str_len = 0; … … 255 309 256 310 /* XXX: Dont know what to do with these yet, just print as binary... */ 311 case REG_NONE: 257 312 case REG_RESOURCE_LIST: 258 313 case REG_FULL_RESOURCE_DESCRIPTOR: … … 260 315 261 316 case REG_BINARY: 262 return (unsigned char*)quote_buffer(datap, len, special_chars);317 return quote_buffer(datap, len, special_chars); 263 318 break; 264 319 … … 369 424 uint32 size; 370 425 uint8 tmp_buf[4]; 371 unsignedchar* quoted_value;426 char* quoted_value; 372 427 char* quoted_prefix; 373 428 char* quoted_name; … … 397 452 size = 16384; 398 453 } 454 399 455 quoted_value = data_to_ascii(vk->data, vk->data_size, vk->type); 400 456 } … … 443 499 time_t tmp_time[1]; 444 500 struct tm* tmp_time_s = NULL; 445 501 char* quoted_path = NULL; 502 503 /* XXX: it would be faster to always pass a quoted string to this 504 * function, instead of re-quoting it every call. */ 505 /* XXX: check return of quote_string */ 506 quoted_path = quote_string(full_path, special_chars); 446 507 *tmp_time = nt_time_to_unix(&k->mtime); 447 508 tmp_time_s = gmtime(tmp_time); … … 463 524 dacl = empty_str; 464 525 465 printf("%s,KEY,,%s,%s,%s,%s,%s\n", full_path, mtime,526 printf("%s,KEY,,%s,%s,%s,%s,%s\n", quoted_path, mtime, 466 527 owner, group, sacl, dacl); 467 528 … … 476 537 } 477 538 else 478 printf("%s,KEY,,%s\n", full_path, mtime); 539 printf("%s,KEY,,%s\n", quoted_path, mtime); 540 541 free(quoted_path); 479 542 } 480 543 … … 673 736 static void usage(void) 674 737 { 675 fprintf(stderr, "Usage: re adreg[-v] [-s]"738 fprintf(stderr, "Usage: reglookup [-v] [-s]" 676 739 " [-p <PATH_FILTER>] [-t <TYPE_FILTER>]" 677 740 " <REGISTRY_FILE>\n"); 678 741 /* XXX: replace version string with Subversion property? */ 679 fprintf(stderr, "Version: 0. 2.2\n");742 fprintf(stderr, "Version: 0.3.0\n"); 680 743 fprintf(stderr, "Options:\n"); 681 744 fprintf(stderr, "\t-v\t sets verbose mode.\n"); … … 728 791 bailOut(1, "ERROR: '-t' option requires parameter.\n"); 729 792 } 730 if((type_filter = regfio_type_str2val(argv[argi])) ==0)793 if((type_filter = regfio_type_str2val(argv[argi])) < 0) 731 794 { 732 795 fprintf(stderr, "ERROR: Invalid type specified: %s.\n", argv[argi]); 733 796 bailOut(1, ""); 734 797 } 735 736 798 type_filter_enabled = true; 737 799 }
Note: See TracChangeset
for help on using the changeset viewer.