X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/f397b5cc874ac7d658173199d92c30f3f717a300..a501c82b196b614295a6e3bf7481da84affb0d8e:/client/util.c diff --git a/client/util.c b/client/util.c index 8e7ef01b..56c4998d 100644 --- a/client/util.c +++ b/client/util.c @@ -8,13 +8,12 @@ // utilities //----------------------------------------------------------------------------- -#include -#include -#include -#include #include "util.h" -#ifdef __linux__ +#ifndef _WIN32 +#include +#include + int ukbhit(void) { int cnt = 0; @@ -46,6 +45,58 @@ int ukbhit(void) { } #endif +// log files functions +void AddLogLine(char *file, char *extData, char *c) { + FILE *fLog = NULL; + char filename[FILE_PATH_SIZE] = {0x00}; + int len = 0; + + len = strlen(file); + if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; + memcpy(filename, file, len); + + fLog = fopen(filename, "a"); + if (!fLog) { + printf("Could not append log file %s", filename); + return; + } + + fprintf(fLog, "%s", extData); + fprintf(fLog, "%s\n", c); + fclose(fLog); +} + +void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){ + AddLogLine(fileName, extData, sprint_hex(data, len)); +} + +void AddLogUint64(char *fileName, char *extData, const uint64_t data) { + char buf[100] = {0}; + sprintf(buf, "%x%x", (unsigned int)((data & 0xFFFFFFFF00000000) >> 32), (unsigned int)(data & 0xFFFFFFFF)); + AddLogLine(fileName, extData, buf); +} + +void AddLogCurrentDT(char *fileName) { + char buff[20]; + struct tm *curTime; + + time_t now = time(0); + curTime = gmtime(&now); + + strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", curTime); + AddLogLine(fileName, "\nanticollision: ", buff); +} + +void FillFileNameByUID(char *fileName, uint8_t * uid, char *ext, int byteCount) { + char * fnameptr = fileName; + memset(fileName, 0x00, 200); + + for (int j = 0; j < byteCount; j++, fnameptr += 2) + sprintf(fnameptr, "%02x", uid[j]); + sprintf(fnameptr, "%s", ext); +} + +// printing and converting functions void print_hex(const uint8_t * data, const size_t len) { @@ -58,16 +109,31 @@ void print_hex(const uint8_t * data, const size_t len) } char * sprint_hex(const uint8_t * data, const size_t len) { + + int maxLen = ( len > 1024/3) ? 1024/3 : len; static char buf[1024]; char * tmp = buf; size_t i; - for (i=0; i < len && i < 1024/3; i++, tmp += 3) + for (i=0; i < maxLen; ++i, tmp += 3) sprintf(tmp, "%02x ", data[i]); return buf; } +char * sprint_bin(const uint8_t * data, const size_t len) { + + int maxLen = ( len > 1024) ? 1024 : len; + static char buf[1024]; + char * tmp = buf; + size_t i; + + for (i=0; i < maxLen; ++i, ++tmp) + sprintf(tmp, "%u", data[i]); + + return buf; +} + void num_to_bytes(uint64_t n, size_t len, uint8_t* dest) { while (len--) { @@ -87,6 +153,32 @@ uint64_t bytes_to_num(uint8_t* src, size_t len) return num; } +//assumes little endian +char * printBits(size_t const size, void const * const ptr) +{ + unsigned char *b = (unsigned char*) ptr; + unsigned char byte; + static char buf[1024]; + char * tmp = buf; + int i, j; + + for (i=size-1;i>=0;i--) + { + for (j=7;j>=0;j--) + { + byte = b[i] & (1<>= j; + sprintf(tmp, "%u", byte); + tmp++; + } + } + return buf; +} + +// ------------------------------------------------------------------------- +// string parameters lib +// ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- // line - param line // bg, en - symbol numbers in param line of beginning an ending parameter @@ -140,7 +232,7 @@ uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base) int bg, en; if (!param_getptr(line, &bg, &en, paramnum)) - return strtol(&line[bg], NULL, base) & 0xff; + return strtoul(&line[bg], NULL, base) & 0xff; else return deflt; } @@ -150,7 +242,7 @@ uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base) int bg, en; if (!param_getptr(line, &bg, &en, paramnum)) - return strtol(&line[bg], NULL, base); + return strtoul(&line[bg], NULL, base); else return deflt; } @@ -160,7 +252,7 @@ uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base) int bg, en; if (!param_getptr(line, &bg, &en, paramnum)) - return strtol(&line[bg], NULL, base); + return strtoull(&line[bg], NULL, base); else return deflt; @@ -188,3 +280,15 @@ int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt) return 0; } + +int param_getstr(const char *line, int paramnum, char * str) +{ + int bg, en; + + if (param_getptr(line, &bg, &en, paramnum)) return 0; + + memcpy(str, line + bg, en - bg + 1); + str[en - bg + 1] = 0; + + return en - bg + 1; +}