- Timestamp:
- 07/17/06 21:56:59 (18 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/regfio.h
r54 r61 6 6 * Windows NT registry I/O library 7 7 * 8 * Copyright (C) 2005 Timothy D. Morgan8 * Copyright (C) 2005-2006 Timothy D. Morgan 9 9 * Copyright (C) 2005 Gerald (Jerry) Carter 10 10 * … … 67 67 #define REG_KEY 255 68 68 69 typedef struct _val_str {70 unsigned int val;71 const char * str;72 } VAL_STR;73 69 74 70 #define REGF_BLOCKSIZE 0x1000 -
trunk/lib/regfio.c
r59 r61 6 6 * Windows NT registry I/O library 7 7 * 8 * Copyright (C) 2005 Timothy D. Morgan8 * Copyright (C) 2005-2006 Timothy D. Morgan 9 9 * Copyright (C) 2005 Gerald (Jerry) Carter 10 10 * … … 28 28 29 29 30 31 30 /* Registry types mapping */ 32 const VAL_STR reg_type_names[] = 33 { 34 { REG_SZ, "SZ" }, 35 { REG_EXPAND_SZ, "EXPAND_SZ" }, 36 { REG_BINARY, "BINARY" }, 37 { REG_DWORD, "DWORD" }, 38 { REG_DWORD_BE, "DWORD_BE" }, 39 { REG_LINK, "LINK" }, 40 { REG_MULTI_SZ, "MULTI_SZ" }, 41 { REG_RESOURCE_LIST, "RSRC_LIST" }, 42 { REG_FULL_RESOURCE_DESCRIPTOR, "RSRC_DESC" }, 43 { REG_RESOURCE_REQUIREMENTS_LIST,"RSRC_REQ_LIST"}, 44 { REG_KEY, "KEY" }, 45 { 0, NULL }, 46 }; 31 const unsigned int regfio_num_reg_types = 11; 32 static const char* regfio_type_names[] = 33 {"NONE", "SZ", "EXPAND_SZ", "BINARY", "DWORD", "DWORD_BE", "LINK" 34 "MULTI_SZ", "RSRC_LIST", "RSRC_DESC", "RSRC_REQ_LIST"}; 47 35 48 36 … … 50 38 const char* regfio_type_val2str(unsigned int val) 51 39 { 40 if(val == REG_KEY) 41 return "KEY"; 42 43 if(val >= regfio_num_reg_types) 44 return NULL; 45 46 return regfio_type_names[val]; 47 } 48 49 50 /* Returns -1 on error */ 51 int regfio_type_str2val(const char* str) 52 { 52 53 int i; 53 54 54 for(i=0; reg_type_names[i].val && reg_type_names[i].str; i++) 55 if (reg_type_names[i].val == val) 56 return reg_type_names[i].str; 57 58 return NULL; 59 } 60 61 62 /* Returns 0 on error */ 63 int regfio_type_str2val(const char* str) 64 { 65 int i; 66 67 for(i=0; reg_type_names[i].val && reg_type_names[i].str; i++) 68 if (strcmp(reg_type_names[i].str, str) == 0) 69 return reg_type_names[i].val; 70 71 return 0; 55 if(strcmp("KEY", str) == 0) 56 return REG_KEY; 57 58 for(i=0; i < regfio_num_reg_types; i++) 59 if (strcmp(regfio_type_names[i], str) == 0) 60 return i; 61 62 if(strcmp("DWORD_LE", str) == 0) 63 return REG_DWORD_LE; 64 65 return -1; 72 66 } 73 67 … … 182 176 char* perms_str; 183 177 char* sid_str; 178 char* ace_delim = ""; 184 179 char* ret_val = NULL; 185 char* ace_delim = ""; 180 char* tmp_val = NULL; 181 bool failed = false; 186 182 char field_delim = ':'; 187 183 188 for (i = 0; i < acl->num_aces ; i++)184 for (i = 0; i < acl->num_aces && !failed; i++) 189 185 { 190 186 sid_str = regfio_sid2str(&acl->ace[i].trustee); … … 193 189 flags_str = regfio_ace_flags2str(acl->ace[i].flags); 194 190 195 if(flags_str == NULL || perms_str == NULL 196 || type_str == NULL || sid_str == NULL) 197 return NULL; 198 199 /* XXX: this is slow */ 200 extra = strlen(sid_str) + strlen(type_str) 201 + strlen(perms_str) + strlen(flags_str)+5; 202 ret_val = realloc(ret_val, size+extra); 203 if(ret_val == NULL) 204 return NULL; 205 size += snprintf(ret_val+size, extra, "%s%s%c%s%c%s%c%s", 206 ace_delim,sid_str, 207 field_delim,type_str, 208 field_delim,perms_str, 209 field_delim,flags_str); 210 ace_delim = "|"; 211 free(sid_str); 212 free(perms_str); 213 free(flags_str); 191 if(flags_str != NULL && perms_str != NULL 192 && type_str != NULL && sid_str != NULL) 193 { 194 /* XXX: this is slow */ 195 extra = strlen(sid_str) + strlen(type_str) 196 + strlen(perms_str) + strlen(flags_str)+5; 197 tmp_val = realloc(ret_val, size+extra); 198 199 if(tmp_val == NULL) 200 { 201 free(ret_val); 202 failed = true; 203 } 204 else 205 { 206 ret_val = tmp_val; 207 size += snprintf(ret_val+size, extra, "%s%s%c%s%c%s%c%s", 208 ace_delim,sid_str, 209 field_delim,type_str, 210 field_delim,perms_str, 211 field_delim,flags_str); 212 ace_delim = "|"; 213 } 214 } 215 else 216 failed = true; 217 218 if(sid_str != NULL) 219 free(sid_str); 220 if(sid_str != NULL) 221 free(perms_str); 222 if(sid_str != NULL) 223 free(flags_str); 214 224 } 215 225 … … 254 264 uint32 block_size ) 255 265 { 266 const int hdr_size = 0x20; 256 267 int bytes_read, returned; 257 268 char *buffer; … … 279 290 } 280 291 281 returned = read( file->fd, hdr, 0x20 ); 282 if ( (returned == -1) || (returned < 0x20) ) { 283 /*DEBUG(0,("read_block: failed to read in HBIN header. Is the file corrupt?\n"));*/ 284 return -1; 292 bytes_read = returned = 0; 293 while (bytes_read < hdr_size) 294 { 295 returned = read(file->fd, hdr + bytes_read, hdr_size - bytes_read); 296 if(returned == -1 && errno != EINTR && errno != EAGAIN) 297 { 298 /*DEBUG(0,("read_block: read of hdr failed (%s)\n",strerror(errno)));*/ 299 return -1; 300 } 301 302 if(returned == 0) 303 return -1; 304 305 bytes_read += returned; 285 306 } 286 307 … … 310 331 while ( bytes_read < block_size ) 311 332 { 312 if((returned =313 read(file->fd, buffer+bytes_read, block_size-bytes_read)) == -1)333 returned = read(file->fd, buffer+bytes_read, block_size-bytes_read); 334 if(returned == -1 && errno != EINTR && errno != EAGAIN) 314 335 { 315 336 /*DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) ));*/ 316 return false; 317 } 337 return -1; 338 } 339 318 340 if ((returned == 0) && (bytes_read < block_size)) 319 341 { 320 342 /*DEBUG(0,("read_block: not a vald registry file ?\n" ));*/ 321 return false;343 return -1; 322 344 } 323 345 -
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.