Changeset 3526
- Timestamp:
- 05/26/08 12:46:16 (6 months ago)
- Files:
-
- 1 modified
-
trunk/src/tools/opensc-explorer.c (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/tools/opensc-explorer.c
r3510 r3526 352 352 static int do_cat(int argc, char **argv) 353 353 { 354 int r, err = 0;354 int r, err = 1; 355 355 sc_path_t path; 356 sc_file_t *file ;356 sc_file_t *file = NULL; 357 357 int not_current = 1; 358 358 … … 370 370 if (r) { 371 371 check_ret(r, SC_AC_OP_SELECT, "unable to select file", current_file); 372 return -1;372 goto err; 373 373 } 374 374 } 375 375 if (file->type != SC_FILE_TYPE_WORKING_EF) { 376 376 printf("only working EFs may be read\n"); 377 sc_file_free(file); 378 return -1; 377 goto err; 379 378 } 380 379 if (file->ef_structure == SC_FILE_EF_TRANSPARENT) … … 382 381 else 383 382 read_and_print_record_file(file); 383 384 err = 0; 385 386 err: 384 387 if (not_current) { 385 sc_file_free(file); 388 if (file != NULL) { 389 sc_file_free(file); 390 } 386 391 r = sc_select_file(card, ¤t_path, NULL); 387 392 if (r) { … … 390 395 } 391 396 } 392 return -err; 397 398 return -err; 393 399 usage: 394 400 puts("Usage: cat [file_id]"); … … 833 839 { 834 840 u8 buf[256]; 835 int r, err = 0;841 int r, err = 1; 836 842 size_t count = 0; 837 843 unsigned int idx = 0; 838 844 sc_path_t path; 839 sc_file_t *file ;845 sc_file_t *file = NULL; 840 846 char fbuf[256], *filename; 841 847 FILE *outf = NULL; … … 860 866 if (outf == NULL) { 861 867 perror(filename); 862 return -1;868 goto err; 863 869 } 864 870 r = sc_select_file(card, &path, &file); 865 871 if (r) { 866 fclose(outf);867 872 check_ret(r, SC_AC_OP_SELECT, "unable to select file", current_file); 868 return -1;873 goto err; 869 874 } 870 875 if (file->type != SC_FILE_TYPE_WORKING_EF) { 871 fclose(outf);872 876 printf("only working EFs may be read\n"); 873 sc_file_free(file); 874 return -1; 877 goto err; 875 878 } 876 879 count = file->size; … … 881 884 if (r < 0) { 882 885 check_ret(r, SC_AC_OP_READ, "read failed", file); 883 err = 1;884 886 goto err; 885 887 } 886 888 if ((r != c) && !(card->caps & SC_CARD_CAP_NO_FCI)) { 887 889 printf("expecting %d, got only %d bytes.\n", c, r); 888 err = 1;889 890 goto err; 890 891 } … … 897 898 printf("Total of %d bytes read from %s and saved to %s.\n", 898 899 idx, argv[0], filename); 900 901 err = 0; 899 902 err: 900 sc_file_free(file); 903 if (file) 904 sc_file_free(file); 905 if (outf) 906 fclose(outf); 901 907 r = sc_select_file(card, ¤t_path, NULL); 902 908 if (r) { … … 904 910 die(1); 905 911 } 906 if (outf)907 fclose(outf);908 912 return -err; 909 913 usage: … … 994 998 printf("Total of %d bytes written to %04X at %i offset.\n", 995 999 r, file->id, offs); 1000 996 1001 err = 0; 1002 997 1003 err: 998 1004 sc_file_free(file); … … 1063 1069 i, rec, offs); 1064 1070 err = 0; 1071 1065 1072 err: 1066 1073 sc_file_free(file); … … 1081 1088 { 1082 1089 u8 buf[256]; 1083 int r, err = 0;1090 int r, err = 1; 1084 1091 size_t count = 0; 1085 1092 unsigned int idx = 0; 1086 1093 sc_path_t path; 1087 sc_file_t *file ;1094 sc_file_t *file = NULL; 1088 1095 const char *filename; 1089 1096 FILE *outf = NULL; … … 1102 1109 if (outf == NULL) { 1103 1110 perror(filename); 1104 return -1;1111 goto err; 1105 1112 } 1106 1113 r = sc_select_file(card, &path, &file); 1107 1114 if (r) { 1108 1115 check_ret(r, SC_AC_OP_SELECT, "unable to select file", current_file); 1109 fclose(outf); 1110 return -1; 1116 goto err; 1111 1117 } 1112 1118 count = file->size; … … 1117 1123 if (r < 0) { 1118 1124 perror("fread"); 1119 err = 1;1120 1125 goto err; 1121 1126 } … … 1125 1130 if (r < 0) { 1126 1131 check_ret(r, SC_AC_OP_READ, "update failed", file); 1127 err = 1;1128 1132 goto err; 1129 1133 } 1130 1134 if (r != c) { 1131 1135 printf("expecting %d, wrote only %d bytes.\n", c, r); 1132 err = 1;1133 1136 goto err; 1134 1137 } … … 1137 1140 } 1138 1141 printf("Total of %d bytes written.\n", idx); 1142 1143 err = 0; 1144 1139 1145 err: 1140 sc_file_free(file); 1146 1147 if (file) 1148 sc_file_free(file); 1149 if (outf) 1150 fclose(outf); 1141 1151 r = sc_select_file(card, ¤t_path, NULL); 1142 1152 if (r) { … … 1144 1154 die(1); 1145 1155 } 1146 if (outf)1147 fclose(outf);1148 1156 return -err; 1149 1157 usage: … … 1513 1521 static int do_asn1(int argc, char **argv) 1514 1522 { 1515 int r ;1523 int r, err = 1; 1516 1524 sc_path_t path; 1517 sc_file_t *file ;1525 sc_file_t *file = NULL; 1518 1526 int not_current = 1; 1519 1527 size_t len; 1520 char *buf;1528 unsigned char *buf = NULL; 1521 1529 1522 1530 if (argc > 1) { … … 1525 1533 } 1526 1534 1527 / / select file1535 /* select file */ 1528 1536 if (argc) { 1529 1537 if (arg_to_path(argv[0], &path, 1) != 0) { … … 1534 1542 if (r) { 1535 1543 check_ret(r, SC_AC_OP_SELECT, "unable to select file", current_file); 1536 return -1;1544 goto err; 1537 1545 } 1538 1546 } else { … … 1543 1551 if (file->type != SC_FILE_TYPE_WORKING_EF) { 1544 1552 printf("only working EFs may be read\n"); 1545 sc_file_free(file); 1546 return -1; 1547 } 1548 1549 // read 1553 goto err; 1554 } 1555 1556 /* read */ 1550 1557 if (file->ef_structure != SC_FILE_EF_TRANSPARENT) { 1551 1558 printf("only transparent file type is supported at the moment\n"); 1552 sc_file_free(file); 1553 return -1; 1559 goto err; 1554 1560 } 1555 1561 len = file->size; 1556 1562 buf = calloc(1, len); 1557 if (!buf) die(1); 1563 if (!buf) { 1564 goto err; 1565 } 1558 1566 r = sc_read_binary(card, 0, buf, len, 0); 1559 1567 if (r < 0) { 1560 1568 check_ret(r, SC_AC_OP_READ, "read failed", file); 1569 goto err; 1570 } 1571 if ((size_t)r != len) { 1572 printf("expecting %d, got only %d bytes.\n", len, r); 1573 goto err; 1574 } 1575 1576 /* asn1 dump */ 1577 sc_asn1_print_tags(buf, len); 1578 1579 err = 0; 1580 err: 1581 if (buf) 1561 1582 free(buf); 1562 return -1;1563 }1564 if (r != len) {1565 printf("expecting %d, got only %d bytes.\n", len, r);1566 free(buf);1567 return -1;1568 }1569 1570 // asn1 dump1571 sc_asn1_print_tags(buf, len);1572 1573 1583 if (not_current) { 1574 sc_file_free(file); 1584 if (file) 1585 sc_file_free(file); 1575 1586 r = sc_select_file(card, ¤t_path, NULL); 1576 1587 if (r) { … … 1579 1590 } 1580 1591 } 1581 1582 return 0; 1592 return -err; 1583 1593 } 1584 1594
