Changeset 8
- Timestamp:
- 05/28/05 23:02:58 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/reglookup.c
r7 r8 2 2 * $Id$ 3 3 * 4 * A utility to edita Windows NT/2K etc registry file.4 * A utility to read a Windows NT/2K etc registry file. 5 5 * 6 6 * This code was taken from Richard Sharpe''s editreg utility, in the … … 332 332 #define REG_TYPE_DWORD 4 333 333 #define REG_TYPE_MULTISZ 7 334 /* Not a real type in the registry */ 335 #define REG_TYPE_KEY 255 334 336 335 337 typedef struct _val_str { … … 403 405 404 406 char* prefix_filter = ""; 405 char* type_filter = "";407 int type_filter = 0; 406 408 bool type_filter_enabled = false; 407 409 … … 424 426 425 427 426 /* Returns a newly malloc()ed string which contains original string,428 /* Returns a newly malloc()ed string which contains original buffer, 427 429 * except for non-printable or special characters are quoted in hex 428 430 * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted 429 * character. 430 */ 431 static 432 char* quote_string(const char* str, char* special) 431 * character. A null terminator is added, as only ascii, not binary, 432 * is returned. 433 */ 434 static 435 char* quote_buffer(const unsigned char* str, char* special, unsigned int len) 433 436 { 434 437 unsigned int i; 435 438 unsigned int num_written=0; 436 unsigned int len = strlen(str);437 439 unsigned int out_len = sizeof(char)*len+1; 438 440 char* ret_val = malloc(out_len); 441 439 442 if(ret_val == NULL) 440 443 return NULL; … … 445 448 { 446 449 out_len += 3; 450 /* XXX: may not be the most efficient way of getting enough memory. */ 447 451 ret_val = realloc(ret_val, out_len); 448 452 if(ret_val == NULL) 449 453 break; 450 num_written += snprintf(ret_val+num_written, out_len-num_written,454 num_written += snprintf(ret_val+num_written, (out_len)-num_written, 451 455 "\\x%.2X", str[i]); 452 456 } … … 458 462 return ret_val; 459 463 } 464 465 466 /* Returns a newly malloc()ed string which contains original string, 467 * except for non-printable or special characters are quoted in hex 468 * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted 469 * character. 470 */ 471 static 472 char* quote_string(const char* str, char* special) 473 { 474 unsigned int len = strlen(str); 475 char* ret_val = quote_buffer((const unsigned char*)str, special, len); 476 477 return ret_val; 478 } 479 460 480 461 481 … … 533 553 if (str_is_prefix(prefix_filter, new_path)) 534 554 { 535 if (!type_filter_enabled || ( strcmp(type_filter, "KEY") == 0))555 if (!type_filter_enabled || (type_filter == REG_TYPE_KEY)) 536 556 printf("%s%s:KEY\n", path, key_tree->name); 537 557 … … 797 817 /* 798 818 * add the new key at the new slot 799 * FIXME: Sort the list someday819 * XXX: Sort the list someday 800 820 */ 801 821 … … 876 896 { REG_TYPE_DWORD, "DWORD" }, 877 897 { REG_TYPE_MULTISZ, "MULTI_SZ" }, 878 /* { REG_TYPE_KEY, "KEY" },*/898 { REG_TYPE_KEY, "KEY" }, 879 899 { 0, NULL }, 880 900 }; 881 901 902 882 903 static 883 904 const char *val_to_str(unsigned int val, const VAL_STR *val_array) 884 905 { 885 int i = 0; 886 887 if (!val_array) return NULL; 888 889 while (val_array[i].val && val_array[i].str) { 890 891 if (val_array[i].val == val) return val_array[i].str; 892 i++; 893 894 } 906 int i; 907 908 if (!val_array) 909 return NULL; 910 911 for(i=0; val_array[i].val && val_array[i].str; i++) 912 if (val_array[i].val == val) 913 return val_array[i].str; 895 914 896 915 return NULL; 897 898 } 916 } 917 918 919 /* Returns 0 on error */ 920 static 921 int str_to_val(const char* str, const VAL_STR *val_array) 922 { 923 int i; 924 925 if (!val_array) 926 return 0; 927 928 for(i=0; val_array[i].val && val_array[i].str; i++) 929 if (strcmp(val_array[i].str, str) == 0) 930 return val_array[i].val; 931 932 return 0; 933 } 934 899 935 900 936 /* … … 947 983 return NULL; 948 984 949 /* FIXME. This has to be fixed. It has to be UNICODE */985 /* XXX: This has to be fixed. It has to be UNICODE */ 950 986 uni_to_ascii(datap, ascii, len, ascii_max); 951 987 return ascii; … … 963 999 964 1000 case REG_TYPE_BIN: 965 ascii_max = sizeof(char)*len*3; 966 ascii = malloc(ascii_max+4); 967 if(ascii == NULL) 968 return NULL; 969 970 asciip = ascii; 971 for (i=0; (i<len)&&(i+1)*3<ascii_max; i++) { 972 int str_rem = ascii_max - ((int)asciip - (int)ascii); 973 asciip += snprintf((char*)asciip, str_rem, "%02x", 974 *(unsigned char *)(datap+i)); 975 if (i < len && str_rem > 0) 976 *asciip = ' '; asciip++; 977 } 978 *asciip = '\0'; 1001 ascii = (unsigned char*)quote_buffer(datap, "\\", len); 979 1002 return ascii; 980 1003 break; … … 1025 1048 { 1026 1049 uni_to_ascii(cur_str, cur_ascii, cur_str_max, 0); 1050 /* XXX: Should backslashes be quoted as well? */ 1027 1051 cur_quoted = quote_string((char*)cur_ascii, "|"); 1028 1052 alen = snprintf((char*)asciip, str_rem, "%s", cur_quoted); … … 1259 1283 tmp = (ACE *)malloc(sizeof(ACE)); 1260 1284 1261 if (!tmp) return NULL; 1285 if (!tmp) 1286 return NULL; 1262 1287 1263 1288 tmp->type = CVAL(&ace->type); … … 1292 1317 tmp->aces[i] = dup_ace(ace); 1293 1318 ace = (REG_ACE *)((char *)ace + SVAL(&ace->length)); 1294 /* XXX: FIXME, should handle malloc errors*/1319 /* XXX: should handle NULLs returned from dup_ace() */ 1295 1320 } 1296 1321 … … 1493 1518 { /* The data is pointed to by the offset */ 1494 1519 char *dat_ptr = LOCN(regf->base, dat_off); 1495 /* XXX: replace with memcpy */ 1496 bcopy(dat_ptr, dtmp, dat_len); 1520 memcpy(dtmp, dat_ptr, dat_len); 1497 1521 } 1498 1522 else { /* The data is in the offset or type */ 1499 1523 /* 1500 * FIXME.1524 * XXX: 1501 1525 * Some registry files seem to have wierd fields. If top bit is set, 1502 1526 * but len is 0, the type seems to be the value ... … … 1504 1528 */ 1505 1529 dat_len = dat_len & 0x7FFFFFFF; 1506 /* XXX: replace with memcpy */ 1507 bcopy(&dat_off, dtmp, dat_len); 1530 memcpy(dtmp, &dat_off, dat_len); 1508 1531 } 1509 1532 … … 1564 1587 1565 1588 error: 1566 /* XXX: FIXME,free the partially allocated structure */1589 /* XXX: free the partially allocated structure */ 1567 1590 return NULL; 1568 1591 } … … 1710 1733 1711 1734 /* 1735 * XXX: 1712 1736 * I am keeping class name as an ascii string for the moment. 1713 1737 * That means it needs to be converted on output. 1714 1738 * It will also piss off people who need Unicode/UTF-8 strings. Sorry. 1715 * XXX: FIXME1716 1739 */ 1717 1740 tmp->class_name = strdup((char*)cls_name); … … 2099 2122 unsigned char* data_asc; 2100 2123 char* new_path; 2101 const char* str_type = val_to_str(val_type,reg_type_names);2124 const char* str_type; 2102 2125 2103 2126 if(!val_name) 2104 2127 val_name = ""; 2105 if(!str_type)2106 str_type = "";2107 2128 if(!path) 2108 2129 path = ""; … … 2117 2138 if (str_is_prefix(prefix_filter, new_path)) 2118 2139 { 2119 if (!type_filter_enabled || ( strcmp(type_filter, str_type) == 0))2140 if (!type_filter_enabled || (type_filter == val_type)) 2120 2141 { 2121 2142 if(!val_name) 2122 2143 val_name = "<No Name>"; 2144 2145 str_type = val_to_str(val_type,reg_type_names); 2146 if(!str_type) 2147 str_type = ""; 2123 2148 2124 2149 data_asc = data_to_ascii((unsigned char *)data_blk, data_len, val_type); … … 2138 2163 fprintf(stderr, "Usage: readreg [-f<PREFIX_FILTER>] [-t<TYPE_FILTER>] " 2139 2164 "[-v] [-p] [-k] [-s] <REGISTRY_FILE>\n"); 2165 /* XXX: replace version string with Subversion tag? */ 2140 2166 fprintf(stderr, "Version: 0.1\n"); 2141 2167 fprintf(stderr, "\n\t-v\t sets verbose mode."); … … 2176 2202 2177 2203 case 't': 2178 /* XXX: this should be converted to the integer form of types up front, 2179 * and then used to filter with a simple comparison later. 2180 */ 2181 type_filter = strdup(optarg); 2204 type_filter = str_to_val(optarg, reg_type_names); 2182 2205 type_filter_enabled = true; 2183 2206 regf_opt++;
Note: See TracChangeset
for help on using the changeset viewer.