term_cmd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /**
  2. * Copyright (C) 2011 Anders Sundman <anders@4zm.org>
  3. *
  4. * This file is part of mfterm.
  5. *
  6. * mfterm is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. * mfterm is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. * You should have received a copy of the GNU General Public License
  15. * along with mfterm. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Parts of code used in this file are from the GNU readline library file
  18. * fileman.c (GPLv3). Copyright (C) 1987-2009 Free Software Foundation, Inc
  19. */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <strings.h>
  23. #include <stdlib.h>
  24. #include "mfterm.h"
  25. #include "tag.h"
  26. #include "term_cmd.h"
  27. #include "mifare_ctrl.h"
  28. #include "dictionary.h"
  29. #include "spec_syntax.h"
  30. #include "util.h"
  31. #include "mac.h"
  32. command_t commands[] = {
  33. { "help", com_help, 0, 0, "Display this text" },
  34. { "?", com_help, 0, 0, "Synonym for 'help'" },
  35. { "quit", com_quit, 0, 1, "Exit the program" },
  36. { "exit", com_quit, 0, 0, "Synonym for 'quit'" },
  37. { "load", com_load_tag, 1, 1, "Load tag data from a file" },
  38. { "save", com_save_tag, 1, 1, "Save tag data to a file" },
  39. { "clear", com_clear_tag, 0, 1, "Clear the current tag data" },
  40. { "read", com_read_tag, 0, 1, "A|B : Read tag data from a physical tag" },
  41. { "read unlocked", com_read_tag_unlocked, 0, 1, "On pirate cards, read card without keys" },
  42. { "write", com_write_tag, 0, 1, "A|B : Write tag data to a physical tag" },
  43. { "write unlocked", com_write_tag_unlocked, 0, 1, "On pirate cards, write 1k tag with block 0" },
  44. { "print", com_print, 0, 1, "1k|4k : Print tag data" },
  45. { "print keys", com_print_keys, 0, 1, "1k|4k : Print tag's keys" },
  46. { "print ac", com_print_ac, 0, 1, "Print access conditions" },
  47. { "set", com_set, 0, 1, "#block #offset = xx xx xx : Set tag data" },
  48. { "keys load", com_keys_load, 1, 1, "Load keys from a file" },
  49. { "keys save", com_keys_save, 1, 1, "Save keys to a file" },
  50. { "keys clear", com_keys_clear, 0, 1, "Clear the keys" },
  51. { "keys set", com_keys_set, 0, 1, "A|B #S key : Set a key value" },
  52. { "keys import", com_keys_import, 0, 1, "Import keys from the current tag" },
  53. { "keys test", com_keys_test, 0, 1, "Try to authenticate with the keys" },
  54. { "keys", com_keys_print, 0, 1, "1k|4k : Print the keys" },
  55. { "dict load", com_dict_load, 1, 1, "Load a dictionary key file" },
  56. { "dict clear", com_dict_clear, 0, 1, "Clear the key dictionary" },
  57. { "dict attack", com_dict_attack, 0, 1, "Find keys of a physical tag"},
  58. { "dict", com_dict_print, 0, 1, "Print the key dictionary" },
  59. { "spec load", com_spec_load, 1, 1, "Load a specification file" },
  60. { "spec clear", com_spec_clear, 0, 1, "Unload the specification" },
  61. { "spec", com_spec_print, 0, 1, "Print the specification" },
  62. { "mac key", com_mac_key_get_set, 0, 1, "<k0..k7> : Get or set MAC key" },
  63. { "mac compute", com_mac_block_compute, 0, 1, "#block : Compute block MAC" },
  64. { "mac update", com_mac_block_update, 0, 1, "#block : Compute block MAC" },
  65. { "mac validate", com_mac_validate, 0, 1, "1k|4k : Validates block MAC of the whole tag" },
  66. { (char *)NULL, (cmd_func_t)NULL, 0, 0, (char *)NULL }
  67. };
  68. // Parse a Mifare size type argument (1k|4k)
  69. mf_size_t parse_size(const char* str);
  70. // Parse a Mifare size type argument (1k|4k). Return the default
  71. // argument value if the string is NULL.
  72. mf_size_t parse_size_default(const char* str, mf_size_t default_size);
  73. // Parse a Mifare key type argument (A|B)
  74. mf_key_type_t parse_key_type(const char* str);
  75. // Parse a Mifare key type argument (A|B). Return the default
  76. // argument value if the string is NULL.
  77. mf_key_type_t parse_key_type_default(const char* str,
  78. mf_key_type_t default_type);
  79. // Compute the MAC using the current_mac_key. If update is nonzero,
  80. // the mac of the current tag is updated. If not, the MAC is simply
  81. // printed.
  82. int com_mac_block_compute_impl(char* arg, int update);
  83. /* Look up NAME as the name of a command, and return a pointer to that
  84. command. Return a NULL pointer if NAME isn't a command name. */
  85. command_t* find_command(const char *name) {
  86. command_t* cmd = NULL;
  87. size_t cmd_len = 0;
  88. for (size_t i = 0; commands[i].name; ++i) {
  89. size_t l = strlen(commands[i].name);
  90. if (l > cmd_len && strncmp(name, commands[i].name, l) == 0) {
  91. cmd = &commands[i];
  92. cmd_len = l;
  93. }
  94. }
  95. return cmd;
  96. }
  97. /**
  98. * Helper function to print the specified command alligned with the longest
  99. * command name.
  100. */
  101. void print_help_(size_t cmd) {
  102. // Find longest command (and cache the result)
  103. static size_t cmd_len_max = 0;
  104. if (cmd_len_max == 0) {
  105. for (int i = 0; commands[i].name; ++i) {
  106. size_t cmd_len = strlen(commands[i].name);
  107. cmd_len_max = cmd_len > cmd_len_max ? cmd_len : cmd_len_max;
  108. }
  109. }
  110. // Format: 4x' ' | cmd | ' '-pad-to-longest-cmd | 4x' ' | doc
  111. printf (" %s", commands[cmd].name);
  112. for (int j = (int)(cmd_len_max - strlen(commands[cmd].name)); j >= 0; --j)
  113. printf(" ");
  114. printf (" %s.\n", commands[cmd].doc);
  115. }
  116. int com_help(char* arg) {
  117. // Help request for specific command?
  118. if (arg && *arg != '\0') {
  119. for (size_t i = 0; commands[i].name; ++i) {
  120. if (strcmp(arg, commands[i].name) == 0) {
  121. print_help_(i);
  122. return 0;
  123. }
  124. }
  125. printf ("No commands match '%s'\n", arg);
  126. }
  127. // Help for all commands (with doc flag)
  128. for (size_t i = 0; commands[i].name; i++) {
  129. if (commands[i].document)
  130. print_help_(i);
  131. }
  132. return 0;
  133. }
  134. int com_quit(char *arg) {
  135. stop_input_loop();
  136. return 0;
  137. }
  138. int com_load_tag(char *arg) {
  139. int res = load_tag(arg);
  140. if (res == 0)
  141. printf("Successfully loaded tag from: %s\n", arg);
  142. return 0;
  143. }
  144. int com_save_tag(char* arg) {
  145. int res = save_tag(arg);
  146. if (res == 0)
  147. printf("Successfully wrote tag to: %s\n", arg);
  148. return 0;
  149. }
  150. int com_clear_tag(char* arg) {
  151. clear_tag(&current_tag);
  152. return 0;
  153. }
  154. int com_read_tag(char* arg) {
  155. // Add option to choose key
  156. char* ab = strtok(arg, " ");
  157. if (ab && strtok(NULL, " ") != (char*)NULL) {
  158. printf("Too many arguments\n");
  159. return -1;
  160. }
  161. if (!ab)
  162. printf("No key argument (A|B) given. Defaulting to A\n");
  163. // Parse key selection
  164. mf_key_type_t key_type = parse_key_type_default(ab, MF_KEY_A);
  165. if (key_type == MF_INVALID_KEY_TYPE) {
  166. printf("Invalid argument (A|B): %s\n", ab);
  167. return -1;
  168. }
  169. // Issue the read request
  170. mf_read_tag(&current_tag, key_type);
  171. return 0;
  172. }
  173. int com_read_tag_unlocked(char* arg) {
  174. char* ab = strtok(arg, " ");
  175. if (ab) {
  176. printf("This command doesn't take any arguments\n");
  177. return -1;
  178. }
  179. // Issue the read request
  180. mf_read_tag(&current_tag, MF_KEY_UNLOCKED);
  181. return 0;
  182. }
  183. int com_write_tag(char* arg) {
  184. // Add option to choose key
  185. char* ab = strtok(arg, " ");
  186. if (!ab) {
  187. printf("Too few arguments: (A|B)\n");
  188. return -1;
  189. }
  190. if (strtok(NULL, " ") != (char*)NULL) {
  191. printf("Too many arguments\n");
  192. return -1;
  193. }
  194. // Parse key selection
  195. mf_key_type_t key_type = parse_key_type(ab);
  196. if (key_type == MF_INVALID_KEY_TYPE) {
  197. printf("Invalid argument (A|B): %s\n", ab);
  198. return -1;
  199. }
  200. // Issue the read request
  201. mf_write_tag(&current_tag, key_type);
  202. return 0;
  203. }
  204. int com_write_tag_unlocked(char* arg) {
  205. char* ab = strtok(arg, " ");
  206. if (ab) {
  207. printf("This command doesn't take any arguments\n");
  208. return -1;
  209. }
  210. // Issue the write request
  211. mf_write_tag(&current_tag, MF_KEY_UNLOCKED);
  212. return 0;
  213. }
  214. int com_print(char* arg) {
  215. char* a = strtok(arg, " ");
  216. if (a && strtok(NULL, " ") != (char*)NULL) {
  217. printf("Too many arguments\n");
  218. return -1;
  219. }
  220. mf_size_t size = parse_size_default(a, MF_1K);
  221. if (size == MF_INVALID_SIZE) {
  222. printf("Unknown argument: %s\n", a);
  223. return -1;
  224. }
  225. print_tag(size);
  226. return 0;
  227. }
  228. int com_set(char* arg) {
  229. char* block_str = strtok(arg, " ");
  230. char* offset_str = strtok(NULL, " ");
  231. char* byte_str = strtok(NULL, " ");
  232. if (!block_str || !offset_str || !byte_str) {
  233. printf("Too few arguments: #block #offset xx xx xx .. xx\n");
  234. return -1;
  235. }
  236. unsigned int block = (unsigned int) strtoul(block_str, &block_str, 16);
  237. if (*block_str != '\0') {
  238. printf("Invalid block character (non hex): %s\n", block_str);
  239. return -1;
  240. }
  241. if (block < 0 || block > 0xff) {
  242. printf("Invalid block [0,ff]: %x\n", block);
  243. return -1;
  244. }
  245. unsigned int offset = (unsigned int) strtoul(offset_str, &offset_str, 16);
  246. if (*offset_str != '\0') {
  247. printf("Invalid offset character (non hex): %s\n", offset_str);
  248. return -1;
  249. }
  250. if (offset < 0 || offset > 0x0f) {
  251. printf("Invalid offset [0,f]: %x\n", offset);
  252. return -1;
  253. }
  254. // Consume the byte tokens
  255. do {
  256. long int byte = strtol(byte_str, &byte_str, 16);
  257. if (*byte_str != '\0') {
  258. printf("Invalid byte character (non hex): %s\n", byte_str);
  259. return -1;
  260. }
  261. if (byte < 0 || byte > 0xff) {
  262. printf("Invalid byte value [0,ff]: %lx\n", byte);
  263. return -1;
  264. }
  265. if (offset > 0x0f) {
  266. printf("Too many bytes specified.\n");
  267. return -1;
  268. }
  269. // Write the data
  270. current_tag.amb[block].mbd.abtData[offset++] = (uint8_t)byte;
  271. } while((byte_str = strtok(NULL, " ")) != (char*)NULL);
  272. return 0;
  273. }
  274. int com_print_keys(char* arg) {
  275. char* a = strtok(arg, " ");
  276. if (a && strtok(NULL, " ") != (char*)NULL) {
  277. printf("Too many arguments\n");
  278. return -1;
  279. }
  280. mf_size_t size = parse_size_default(a, MF_1K);
  281. if (size == MF_INVALID_SIZE) {
  282. printf("Unknown argument: %s\n", a);
  283. return -1;
  284. }
  285. print_keys(&current_tag, size);
  286. return 0;
  287. }
  288. int com_print_ac(char* arg) {
  289. if (strtok(arg, " ") != (char*)NULL) {
  290. printf("Too many arguments\n");
  291. return -1;
  292. }
  293. print_ac(&current_tag);
  294. return 0;
  295. }
  296. int com_keys_load(char* arg) {
  297. int res = load_auth(arg);
  298. if (res == 0)
  299. printf("Successfully loaded keys from: %s\n", arg);
  300. return 0;
  301. }
  302. int com_keys_save(char* arg) {
  303. int res = save_auth(arg);
  304. if (res == 0)
  305. printf("Successfully wrote keys to: %s\n", arg);
  306. return 0;
  307. }
  308. int com_keys_clear(char* arg) {
  309. clear_tag(&current_auth);
  310. return 0;
  311. }
  312. int com_keys_set(char* arg) {
  313. // Arg format: A|B #S key
  314. char* ab = strtok(arg, " ");
  315. char* sector_str = strtok(NULL, " ");
  316. char* key_str = strtok(NULL, " ");
  317. if (strtok(NULL, " ") != (char*)NULL) {
  318. printf("Too many arguments\n");
  319. return -1;
  320. }
  321. if (!ab || !sector_str || !key_str) {
  322. printf("Too few arguments: (A|B) #sector key\n");
  323. return -1;
  324. }
  325. // Read sector
  326. long int sector = strtol(sector_str, &sector_str, 16);
  327. // Sanity check sector range
  328. if (*sector_str != '\0') {
  329. printf("Invalid sector character (non hex): %s\n", sector_str);
  330. return -1;
  331. }
  332. if (sector < 0 || sector > 0x1b) {
  333. printf("Invalid sector [0,1b]: %lx\n", sector);
  334. return -1;
  335. }
  336. // Sanity check key length
  337. if (strncmp(key_str, "0x", 2) == 0)
  338. key_str += 2;
  339. if (strlen(key_str) != 12) {
  340. printf("Invalid key (6 byte hex): %s\n", key_str);
  341. return -1;
  342. }
  343. // Compute the block that houses the key for the desired sector
  344. size_t block = sector_to_trailer((size_t)sector);
  345. // Parse key selection and point to appropriate key
  346. uint8_t* key;
  347. mf_key_type_t key_type = parse_key_type(ab);
  348. if (key_type == MF_KEY_A)
  349. key = current_auth.amb[block].mbt.abtKeyA;
  350. else if (key_type == MF_KEY_B)
  351. key = current_auth.amb[block].mbt.abtKeyB;
  352. else {
  353. printf("Invalid argument (A|B): %s\n", ab);
  354. return -1;
  355. }
  356. // Parse the key
  357. if (read_key(key, key_str) == NULL) {
  358. printf("Invalid key character (non hex)\n");
  359. return -1;
  360. }
  361. return 0;
  362. }
  363. int com_keys_import(char* arg) {
  364. import_auth();
  365. return 0;
  366. }
  367. int com_keys_test(char* arg) {
  368. // Arg format: 1k|4k A|B
  369. char* s = strtok(arg, " ");
  370. char* ab = strtok(NULL, " ");
  371. if (s && ab && strtok(NULL, " ") != NULL) {
  372. printf("Too many arguments\n");
  373. return -1;
  374. }
  375. if (!s || !ab) {
  376. printf("Too few arguments: (1k|4k) (A|B)\n");
  377. return -1;
  378. }
  379. // Parse arguments
  380. mf_size_t size = parse_size(s);
  381. if (size == MF_INVALID_SIZE) {
  382. printf("Unknown size argument (1k|4k): %s\n", s);
  383. return -1;
  384. }
  385. mf_key_type_t key_type = parse_key_type(ab);
  386. if (key_type == MF_INVALID_KEY_TYPE) {
  387. printf("Unknown key type argument (A|B): %s\n", ab);
  388. return -1;
  389. }
  390. // Run the auth test
  391. mf_test_auth(&current_auth, size, key_type);
  392. return 0;
  393. }
  394. int com_keys_print(char* arg) {
  395. char* a = strtok(arg, " ");
  396. if (a && strtok(NULL, " ") != (char*)NULL) {
  397. printf("Too many arguments\n");
  398. return -1;
  399. }
  400. mf_size_t size = parse_size_default(a, MF_1K);
  401. if (size == MF_INVALID_SIZE) {
  402. printf("Unknown argument: %s\n", a);
  403. return -1;
  404. }
  405. print_keys(&current_auth, size);
  406. return 0;
  407. }
  408. int com_dict_load(char* arg) {
  409. FILE* dict_file = fopen(arg, "r");
  410. if (dict_file == NULL) {
  411. printf("Could not open file: %s\n", arg);
  412. return 1;
  413. }
  414. dictionary_import(dict_file);
  415. fclose(dict_file);
  416. return 0;
  417. }
  418. int com_dict_clear(char* arg) {
  419. dictionary_clear();
  420. return 0;
  421. }
  422. int com_dict_attack(char* arg) {
  423. // Not much point if we don't have any keys
  424. if (!dictionary_get()) {
  425. printf("Dictionary is empty!\n");
  426. return -1;
  427. }
  428. mf_dictionary_attack(&current_auth);
  429. return 0;
  430. }
  431. int com_dict_print(char* arg) {
  432. key_list_t* kl = dictionary_get();
  433. int count = 0;
  434. while(kl) {
  435. printf("%s\n", sprint_key(kl->key));
  436. kl = kl->next;
  437. ++count;
  438. }
  439. printf("Dictionary contains: %d keys\n", count);
  440. return 0;
  441. }
  442. int com_spec_print(char* arg) {
  443. print_instance_tree();
  444. return 0;
  445. }
  446. int com_spec_load(char* arg) {
  447. // Start by clearing the current hierarcy
  448. clear_instance_tree();
  449. tt_clear();
  450. // Open the file
  451. FILE* spec_file = fopen(arg, "r");
  452. if (spec_file == NULL) {
  453. printf("Could not open file: %s\n", arg);
  454. return 1;
  455. }
  456. // Parse the specification
  457. spec_import(spec_file);
  458. fclose(spec_file);
  459. return 0;
  460. }
  461. int com_spec_clear(char* arg) {
  462. clear_instance_tree();
  463. tt_clear();
  464. return 0;
  465. }
  466. int com_mac_key_get_set(char* arg) {
  467. char* key_str = strtok(arg, " ");
  468. if (key_str == 0) {
  469. printf("Current MAC key: \n");
  470. print_hex_array_sep(current_mac_key, 8, " ");
  471. printf("\n");
  472. return 0;
  473. }
  474. uint8_t key[8];
  475. int key_ptr = 0;
  476. // Consume the key tokens
  477. do {
  478. long int byte = strtol(key_str, &key_str, 16);
  479. if (*key_str != '\0') {
  480. printf("Invalid key character (non hex): %s\n", key_str);
  481. return -1;
  482. }
  483. if (byte < 0 || byte > 0xff) {
  484. printf("Invalid byte value [0,ff]: %lx\n", byte);
  485. return -1;
  486. }
  487. if (key_ptr > sizeof(key)) {
  488. printf("Too many bytes specified in key (should be 8).\n");
  489. return -1;
  490. }
  491. // Accept the byte and add it to the key
  492. key[key_ptr++] = (uint8_t)byte;
  493. } while((key_str = strtok(NULL, " ")) != (char*)NULL);
  494. if (key_ptr != sizeof(key)) {
  495. printf("Too few bytes specified in key (should be 8).\n");
  496. return -1;
  497. }
  498. // Everything ok, so update the global
  499. memcpy(current_mac_key, key, 8);
  500. return 0;
  501. }
  502. int com_mac_block_compute(char* arg) {
  503. return com_mac_block_compute_impl(arg, 0);
  504. }
  505. int com_mac_block_update(char* arg) {
  506. return com_mac_block_compute_impl(arg, 1);
  507. }
  508. int com_mac_block_compute_impl(char* arg, int update) {
  509. char* block_str = strtok(arg, " ");
  510. if (!block_str) {
  511. printf("Too few arguments: #block\n");
  512. return -1;
  513. }
  514. unsigned int block = (unsigned int) strtoul(block_str, &block_str, 16);
  515. if (*block_str != '\0') {
  516. printf("Invalid block character (non hex): %s\n", block_str);
  517. return -1;
  518. }
  519. if (block < 0 || block > 0xff) {
  520. printf("Invalid block [0,ff]: %x\n", block);
  521. return -1;
  522. }
  523. // Use the key
  524. unsigned char* mac = compute_block_mac(block, current_mac_key, update);
  525. // MAC is null on error, else 8 bytes
  526. if (mac == 0)
  527. return -1;
  528. // Only need 16 MSBs.
  529. printf("Block %2.2x, MAC : ", block);
  530. print_hex_array_sep(mac, 2, " ");
  531. printf("\n");
  532. return 0;
  533. }
  534. int com_mac_validate(char* arg) {
  535. char* a = strtok(arg, " ");
  536. if (a && strtok(NULL, " ") != (char*)NULL) {
  537. printf("Too many arguments\n");
  538. return -1;
  539. }
  540. mf_size_t size = parse_size_default(a, MF_1K);
  541. if (size == MF_INVALID_SIZE) {
  542. printf("Unknown argument: %s\n", a);
  543. return -1;
  544. }
  545. for (unsigned int i = 1; i < block_count(size); ++i) {
  546. if (is_trailer_block(i))
  547. continue;
  548. unsigned char* mac = compute_block_mac(i, current_mac_key, 0);
  549. printf("Block: %2x ", i);
  550. printf("Tag: ");
  551. print_hex_array_sep(&current_tag.amb[i].mbd.abtData[14], 2, " ");
  552. printf(" Computed: ");
  553. print_hex_array_sep(mac, 2, " ");
  554. printf(" Result: ");
  555. if (memcmp(mac, &current_tag.amb[i].mbd.abtData[14], 2) == 0)
  556. printf("VALID");
  557. else
  558. printf("IN-VALID");
  559. printf("\n");
  560. }
  561. return 0;
  562. }
  563. mf_size_t parse_size(const char* str) {
  564. if (str == NULL)
  565. return MF_INVALID_SIZE;
  566. if (strcasecmp(str, "1k") == 0)
  567. return MF_1K;
  568. if (strcasecmp(str, "4k") == 0)
  569. return MF_4K;
  570. return MF_INVALID_SIZE;
  571. }
  572. mf_size_t parse_size_default(const char* str, mf_size_t default_size) {
  573. if (str == NULL)
  574. return default_size;
  575. return parse_size(str);
  576. }
  577. mf_key_type_t parse_key_type(const char* str) {
  578. if (str == NULL)
  579. return MF_INVALID_KEY_TYPE;
  580. if (strcasecmp(str, "a") == 0)
  581. return MF_KEY_A;
  582. if (strcasecmp(str, "b") == 0)
  583. return MF_KEY_B;
  584. return MF_INVALID_KEY_TYPE;
  585. }
  586. mf_key_type_t parse_key_type_default(const char* str,
  587. mf_key_type_t default_type) {
  588. if (str == NULL)
  589. return default_type;
  590. return parse_key_type(str);
  591. }
  592. // Any command starting with '.' - path spec
  593. int exec_path_command(const char *line) {
  594. instance_t* inst = parse_spec_path(line);
  595. if (inst)
  596. print_tag_data_range(inst->offset_bytes, inst->offset_bits,
  597. inst->size_bytes, inst->size_bits);
  598. else
  599. printf("Invalid Path\n");
  600. return 0;
  601. }