You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mfcuk_utils.c 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Package:
  3. MiFare Classic Universal toolKit (MFCUK)
  4. Package version:
  5. 0.1
  6. Filename:
  7. mfcuk_utils.c
  8. Description:
  9. MFCUK common utility functions implementation.
  10. License:
  11. GPL2 (see below), Copyright (C) 2009, Andrei Costin
  12. * @file mfcuk_utils.c
  13. * @brief
  14. */
  15. /*
  16. VERSION HISTORY
  17. --------------------------------------------------------------------------------
  18. | Number : 0.1
  19. | dd/mm/yyyy : 23/11/2009
  20. | Author : zveriu@gmail.com, http://andreicostin.com
  21. | Description: Moved bulk of defines and prototypes from "mfcuk_keyrecovery_darkside.c"
  22. --------------------------------------------------------------------------------
  23. */
  24. /*
  25. LICENSE
  26. This program is free software: you can redistribute it and/or modify
  27. it under the terms of the GNU General Public License as published by
  28. the Free Software Foundation, either version 2 of the License, or
  29. (at your option) any later version.
  30. This program is distributed in the hope that it will be useful,
  31. but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. GNU General Public License for more details.
  34. You should have received a copy of the GNU General Public License
  35. along with this program. If not, see <http://www.gnu.org/licenses/>.
  36. */
  37. #include "mfcuk_utils.h"
  38. #ifdef __STDC__
  39. struct timeval global_timeout;
  40. #endif
  41. /*
  42. http://www.velocityreviews.com/forums/t451319-advice-required-on-my-ascii-to-hex-conversion-c.html
  43. Basically, converting a hex digit into a hex nibble (4 binary digits) algorithm looks like;
  44. char xdigit; // hex digit to convert [0-9A-Fa-f]
  45. xdigit = tolower(xdigit); // make it lowercase [0-9a-f]
  46. xdigit -= '0'; // if it was a [0-9] digit, it's the value now
  47. if(xdigit > 9) // if it was a [a-f] digit, compensate for that
  48. xdigit = xdigit + '0' - 'a';
  49. The below code is just an optimization of the algorithm. Maxim Yegorushkin
  50. */
  51. /*inline*/
  52. int is_hex(char c)
  53. {
  54. return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f');
  55. }
  56. /*inline*/
  57. unsigned char hex2bin(unsigned char h, unsigned char l)
  58. {
  59. h |= 0x20; // to lower
  60. h -= 0x30;
  61. h -= -(h > 9) & 0x27;
  62. l |= 0x20;
  63. l -= 0x30;
  64. l -= -(l > 9) & 0x27;
  65. return h << 4 | l;
  66. }