FreeCalypso > hg > fc-sim-tools
comparison simtool/savebin.c @ 10:ddd767f6e15b
fc-simtool ported over
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sun, 14 Mar 2021 07:11:25 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 9:c9ef9e91dd8e | 10:ddd767f6e15b |
|---|---|
| 1 /* | |
| 2 * This module implements commands for saving SIM file content | |
| 3 * in UNIX host files. | |
| 4 */ | |
| 5 | |
| 6 #include <sys/types.h> | |
| 7 #include <ctype.h> | |
| 8 #include <stdio.h> | |
| 9 #include <stdlib.h> | |
| 10 #include "simresp.h" | |
| 11 #include "curfile.h" | |
| 12 #include "file_id.h" | |
| 13 | |
| 14 static | |
| 15 savebin_transparent(outf) | |
| 16 FILE *outf; | |
| 17 { | |
| 18 unsigned off, cc; | |
| 19 int rc; | |
| 20 | |
| 21 for (off = 0; off < curfile_total_size; off += cc) { | |
| 22 cc = curfile_total_size - off; | |
| 23 if (cc > 256) | |
| 24 cc = 256; | |
| 25 rc = readbin_op(off, cc); | |
| 26 if (rc < 0) | |
| 27 return(rc); | |
| 28 fwrite(sim_resp_data, 1, cc, outf); | |
| 29 } | |
| 30 return(0); | |
| 31 } | |
| 32 | |
| 33 static | |
| 34 savebin_records(outf) | |
| 35 FILE *outf; | |
| 36 { | |
| 37 unsigned recno; | |
| 38 int rc; | |
| 39 | |
| 40 for (recno = 1; recno <= curfile_record_count; recno++) { | |
| 41 rc = readrec_op(recno, 0x04, curfile_record_len); | |
| 42 if (rc < 0) | |
| 43 return(rc); | |
| 44 fwrite(sim_resp_data, curfile_record_len, 1, outf); | |
| 45 } | |
| 46 return(0); | |
| 47 } | |
| 48 | |
| 49 cmd_savebin(argc, argv) | |
| 50 char **argv; | |
| 51 { | |
| 52 int file_id, rc; | |
| 53 FILE *of; | |
| 54 | |
| 55 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) && | |
| 56 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4]) | |
| 57 file_id = strtoul(argv[1], 0, 16); | |
| 58 else | |
| 59 file_id = find_symbolic_file_name(argv[1]); | |
| 60 if (file_id < 0) { | |
| 61 fprintf(stderr, | |
| 62 "error: file ID argument is not a hex value or a recognized symbolic name\n"); | |
| 63 return(-1); | |
| 64 } | |
| 65 rc = select_op(file_id); | |
| 66 if (rc < 0) | |
| 67 return(rc); | |
| 68 rc = parse_ef_select_response(); | |
| 69 if (rc < 0) | |
| 70 return(rc); | |
| 71 of = fopen(argv[2], "w"); | |
| 72 if (!of) { | |
| 73 perror(argv[2]); | |
| 74 return(-1); | |
| 75 } | |
| 76 switch (curfile_structure) { | |
| 77 case 0x00: | |
| 78 /* transparent */ | |
| 79 rc = savebin_transparent(of); | |
| 80 break; | |
| 81 case 0x01: | |
| 82 case 0x03: | |
| 83 /* record-based */ | |
| 84 rc = savebin_records(of); | |
| 85 break; | |
| 86 } | |
| 87 fclose(of); | |
| 88 return(rc); | |
| 89 } | |
| 90 | |
| 91 cmd_save_sms_bin(argc, argv) | |
| 92 char **argv; | |
| 93 { | |
| 94 int rc; | |
| 95 FILE *of; | |
| 96 | |
| 97 rc = select_op(DF_TELECOM); | |
| 98 if (rc < 0) | |
| 99 return(rc); | |
| 100 rc = select_op(EF_SMS); | |
| 101 if (rc < 0) | |
| 102 return(rc); | |
| 103 rc = parse_ef_select_response(); | |
| 104 if (rc < 0) | |
| 105 return(rc); | |
| 106 if (curfile_structure != 0x01 || curfile_record_len != 176) { | |
| 107 fprintf(stderr, | |
| 108 "error: EF_SMS is not linear fixed with 176-byte records\n"); | |
| 109 return(-1); | |
| 110 } | |
| 111 of = fopen(argv[1], "w"); | |
| 112 if (!of) { | |
| 113 perror(argv[1]); | |
| 114 return(-1); | |
| 115 } | |
| 116 rc = savebin_records(of); | |
| 117 fclose(of); | |
| 118 return(rc); | |
| 119 } |
