made a multi threaded version of ht2crack2search since the file lookups should benefi...
[RRG-proxmark3.git] / armsrc / dbprint.c
blob903adf872a90716d93543a8b001a2b4d0b0a0b35
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Jonathan Westhues, Mar 2006
3 // Copyright (C) Gerhard de Koning Gans, Sep 2007
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program 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.
15 // See LICENSE.txt for the text of the license.
16 //-----------------------------------------------------------------------------
18 #include "dbprint.h"
20 #include "string.h"
21 #include "cmd.h"
22 #include "printf.h"
24 #define DEBUG 1
25 #define DEBUG_MAX_MSG_SIZE 200
26 //=============================================================================
27 // Debug print functions, to go out over USB, to the usual PC-side client.
28 //=============================================================================
30 void DbpStringEx(uint32_t flags, const char *src, size_t srclen) {
31 #if DEBUG
32 struct {
33 uint16_t flag;
34 uint8_t buf[DEBUG_MAX_MSG_SIZE];
35 } PACKED data;
36 data.flag = flags;
37 uint16_t len = MIN(srclen, sizeof(data.buf));
38 memcpy(data.buf, src, len);
39 reply_ng(CMD_DEBUG_PRINT_STRING, PM3_SUCCESS, (uint8_t *)&data, sizeof(data.flag) + len);
40 #endif
43 void DbpString(const char *str) {
44 #if DEBUG
45 DbpStringEx(FLAG_LOG, str, strlen(str));
46 #endif
49 void DbprintfEx(uint32_t flags, const char *fmt, ...) {
50 #if DEBUG
51 // should probably limit size here; oh well, let's just use a big buffer
52 char s[DEBUG_MAX_MSG_SIZE] = {0x00};
53 va_list ap;
54 va_start(ap, fmt);
55 kvsprintf(fmt, s, 10, ap);
56 va_end(ap);
58 DbpStringEx(flags, s, strlen(s));
59 #endif
62 void Dbprintf(const char *fmt, ...) {
63 #if DEBUG
64 // should probably limit size here; oh well, let's just use a big buffer
65 char output_string[DEBUG_MAX_MSG_SIZE] = {0x00};
66 va_list ap;
68 va_start(ap, fmt);
69 kvsprintf(fmt, output_string, 10, ap);
70 va_end(ap);
72 DbpString(output_string);
73 #endif
76 // prints HEX & ASCII
77 void Dbhexdump(int len, const uint8_t *d, bool bAsci) {
78 #if DEBUG
79 char ascii[17];
81 while (len > 0) {
83 int l = (len > 16) ? 16 : len;
85 memcpy(ascii, d, l);
86 ascii[l] = 0;
88 // filter safe ascii
89 for (int i = 0; i < l; i++) {
90 if (ascii[i] < 32 || ascii[i] > 126) {
91 ascii[i] = '.';
95 if (bAsci)
96 Dbprintf("%-8s %*D", ascii, l, d, " ");
97 else
98 Dbprintf("%*D", l, d, " ");
100 len -= 16;
101 d += 16;
103 #endif
105 void print_result(const char *name, const uint8_t *d, size_t
107 n) {
109 const uint8_t *p = d;
110 uint16_t tmp = n & 0xFFF0;
112 for (; p - d < tmp; p += 16) {
113 Dbprintf("[%s: %02d/%02d] %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
114 name,
115 p - d,
117 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]
121 if (n % 16 != 0) {
122 char s[46] = {0};
123 char *sp = s;
124 for (; p - d < n; p++) {
125 sprintf(sp, "%02x ", p[0]);
126 sp += 3;
128 Dbprintf("[%s: %02d/%02d] %s", name, p - d, n, s);
132 // Prints message and hexdump
133 void print_dbg(const char *msg, const uint8_t *d, uint16_t n) {
134 if (g_dbglevel == DBG_DEBUG) {
135 print_result(msg, d, n);
139 /* useful when debugging new protocol implementations like FeliCa
140 void PrintToSendBuffer(void) {
141 DbpString("Printing ToSendBuffer:");
142 Dbhexdump(ToSendMax, ToSend, 0);