mfcuk_utils.c 2.74 KB
Newer Older
1 2 3
/*
 Package:
    MiFare Classic Universal toolKit (MFCUK)
4

5 6
 Package version:
    0.1
7

8 9 10 11 12 13 14 15 16 17
 Filename:
    mfcuk_utils.c

 Description:
    MFCUK common utility functions implementation.

 License:
    GPL2 (see below), Copyright (C) 2009, Andrei Costin

 * @file mfcuk_utils.c
18
 * @brief
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
*/

/*
 VERSION HISTORY
--------------------------------------------------------------------------------
| Number     : 0.1
| dd/mm/yyyy : 23/11/2009
| Author     : zveriu@gmail.com, http://andreicostin.com
| Description: Moved bulk of defines and prototypes from "mfcuk_keyrecovery_darkside.c"
--------------------------------------------------------------------------------
*/

/*
 LICENSE

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 2 of the License, or
 (at your option) any later version.
38

39 40 41 42
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
43

44
 You should have received a copy of the GNU General Public License
45
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
46 47
*/

48
#include "config.h"
49

50 51 52 53 54 55
#if defined(WIN32)
#include <windows.h>
#elif defined(HAVE_UNISTD_H)
#include <unistd.h>
#else
#error "Unsupported system"
56 57
#endif

58 59 60
#include "mfcuk_utils.h"
#include <stdio.h>

61 62 63 64 65 66 67 68 69 70 71 72 73 74
/*
http://www.velocityreviews.com/forums/t451319-advice-required-on-my-ascii-to-hex-conversion-c.html
Basically, converting a hex digit into a hex nibble (4 binary digits) algorithm looks like;
        char xdigit; // hex digit to convert [0-9A-Fa-f]
        xdigit = tolower(xdigit); // make it lowercase [0-9a-f]
        xdigit -= '0'; // if it was a [0-9] digit, it's the value now
        if(xdigit > 9) // if it was a [a-f] digit, compensate for that
        xdigit = xdigit + '0' - 'a';
The below code is just an optimization of the algorithm. Maxim Yegorushkin
*/

/*inline*/
int is_hex(char c)
{
75
  return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f');
76 77 78 79 80
}

/*inline*/
unsigned char hex2bin(unsigned char h, unsigned char l)
{
81 82 83 84 85 86 87
  h |= 0x20; // to lower
  h -= 0x30;
  h -= -(h > 9) & 0x27;
  l |= 0x20;
  l -= 0x30;
  l -= -(l > 9) & 0x27;
  return h << 4 | l;
88
}
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

void sleepmillis(unsigned int millis)
{
#ifdef WIN32 // If system is Windows, use system's own function if possible to reduce overhead, even if a standard C library is available
  Sleep(millis);
#else
  usleep(millis * 1000);
#endif
}

void clear_screen()
{
#ifdef WIN32 // On Windows, use "cls" command
  system("cls");
#else // Otherwise fall back to TTY control characters
  printf("\033[1;1H\033[J");
#endif
}