]> git.zerfleddert.de Git - proxmark3-svn/blame - client/ui.c
Fixed: the dampening field detection is enhanced. If half a frame (clock rate)...
[proxmark3-svn] / client / ui.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
212ef3a0 2// Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
a553f267 3// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4//
5// This code is licensed to you under the terms of the GNU GPL, version 2 or,
6// at your option, any later version. See the LICENSE.txt file for the text of
7// the license.
8//-----------------------------------------------------------------------------
9// UI utilities
10//-----------------------------------------------------------------------------
11
7fe9b0b7 12#include <stdarg.h>
51969283 13#include <stdlib.h>
7fe9b0b7 14#include <stdio.h>
f6c18637 15#include <stdbool.h>
7fe9b0b7 16#include <time.h>
51969283 17#include <readline/readline.h>
9492e0b0 18#include <pthread.h>
7fe9b0b7 19#include "ui.h"
f6c18637 20#include "loclass/cipherutils.h"
7fe9b0b7 21
22double CursorScaleFactor;
7ddb9900 23int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64;
7fe9b0b7 24int offline;
ed77aabe 25int flushAfterWrite = 0; //buzzy
9492e0b0 26extern pthread_mutex_t print_lock;
27
7fe9b0b7 28static char *logfilename = "proxmark3.log";
29
30void PrintAndLog(char *fmt, ...)
31{
51969283
M
32 char *saved_line;
33 int saved_point;
9492e0b0 34 va_list argptr, argptr2;
35 static FILE *logfile = NULL;
36 static int logging=1;
7fe9b0b7 37
9492e0b0 38 // lock this section to avoid interlacing prints from different threats
39 pthread_mutex_lock(&print_lock);
40
41 if (logging && !logfile) {
42 logfile=fopen(logfilename, "a");
43 if (!logfile) {
44 fprintf(stderr, "Can't open logfile, logging disabled!\n");
45 logging=0;
46 }
47 }
51969283
M
48
49 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
7fe9b0b7 50
51969283
M
51 if (need_hack) {
52 saved_point = rl_point;
53 saved_line = rl_copy_text(0, rl_end);
54 rl_save_prompt();
55 rl_replace_line("", 0);
56 rl_redisplay();
57 }
58
9492e0b0 59 va_start(argptr, fmt);
60 va_copy(argptr2, argptr);
61 vprintf(fmt, argptr);
62 printf(" "); // cleaning prompt
63 va_end(argptr);
64 printf("\n");
51969283
M
65
66 if (need_hack) {
67 rl_restore_prompt();
68 rl_replace_line(saved_line, 0);
69 rl_point = saved_point;
70 rl_redisplay();
71 free(saved_line);
72 }
73
9492e0b0 74 if (logging && logfile) {
75 vfprintf(logfile, fmt, argptr2);
76 fprintf(logfile,"\n");
77 fflush(logfile);
78 }
79 va_end(argptr2);
80
ed77aabe 81 if (flushAfterWrite == 1) //buzzy
82 {
83 fflush(NULL);
84 }
9492e0b0 85 //release lock
86 pthread_mutex_unlock(&print_lock);
7fe9b0b7 87}
88
89void SetLogFilename(char *fn)
90{
91 logfilename = fn;
92}
f38a1528 93
f6c18637 94int manchester_decode( int * data, const size_t len, uint8_t * dataout){
f38a1528 95
b44e5233 96 int bitlength = 0;
97 int i, clock, high, low, startindex;
98 low = startindex = 0;
f38a1528 99 high = 1;
b44e5233 100 uint8_t bitStream[len];
f6c18637 101
102 memset(bitStream, 0x00, len);
b44e5233 103
f38a1528 104 /* Detect high and lows */
b44e5233 105 for (i = 0; i < len; i++) {
f38a1528 106 if (data[i] > high)
107 high = data[i];
108 else if (data[i] < low)
109 low = data[i];
110 }
111
112 /* get clock */
b44e5233 113 clock = GetT55x7Clock( data, len, high );
f6c18637 114 startindex = DetectFirstTransition(data, len, high);
b44e5233 115
77376577 116 //PrintAndLog(" Clock : %d", clock);
117 //PrintAndLog(" startindex : %d", startindex);
b44e5233 118
119 if (high != 1)
120 bitlength = ManchesterConvertFrom255(data, len, bitStream, high, low, clock, startindex);
121 else
122 bitlength= ManchesterConvertFrom1(data, len, bitStream, clock, startindex);
123
77376577 124 //if ( bitlength > 0 )
125 // PrintPaddedManchester(bitStream, bitlength, clock);
b44e5233 126
127 memcpy(dataout, bitStream, bitlength);
128
129 free(bitStream);
130 return bitlength;
131}
132
133 int GetT55x7Clock( const int * data, const size_t len, int peak ){
134
135 int i,lastpeak,clock;
136 clock = 0xFFFF;
137 lastpeak = 0;
138
139 /* Detect peak if we don't have one */
140 if (!peak) {
141 for (i = 0; i < len; ++i) {
142 if (data[i] > peak) {
143 peak = data[i];
144 }
145 }
146 }
147
148 for (i = 1; i < len; ++i) {
f38a1528 149 /* if this is the beginning of a peak */
b44e5233 150 if ( data[i-1] != data[i] && data[i] == peak) {
f38a1528 151 /* find lowest difference between peaks */
152 if (lastpeak && i - lastpeak < clock)
153 clock = i - lastpeak;
154 lastpeak = i;
155 }
156 }
b44e5233 157 //return clock;
158 //defaults clock to precise values.
159 switch(clock){
160 case 8:
161 case 16:
162 case 32:
163 case 40:
164 case 50:
165 case 64:
166 case 100:
167 case 128:
168 return clock;
169 break;
170 default: break;
171 }
f6c18637 172
77376577 173 //PrintAndLog(" Found Clock : %d - trying to adjust", clock);
f6c18637 174
175 // When detected clock is 31 or 33 then then return
176 int clockmod = clock%8;
177 if ( clockmod == 7 )
178 clock += 1;
179 else if ( clockmod == 1 )
180 clock -= 1;
181
182 return clock;
b44e5233 183 }
184
f6c18637 185 int DetectFirstTransition(const int * data, const size_t len, int threshold){
b44e5233 186
f6c18637 187 int i =0;
188 /* now look for the first threshold */
189 for (; i < len; ++i) {
190 if (data[i] == threshold) {
f38a1528 191 break;
192 }
f6c18637 193 }
194 return i;
b44e5233 195 }
196
197 int ManchesterConvertFrom255(const int * data, const size_t len, uint8_t * dataout, int high, int low, int clock, int startIndex){
198
f6c18637 199 int i, j, z, hithigh, hitlow, bitIndex, startType;
200 i = 0;
b44e5233 201 bitIndex = 0;
f6c18637 202
203 int isDamp = 0;
204 int damplimit = (int)((high / 2) * 0.3);
205 int dampHi = (high/2)+damplimit;
206 int dampLow = (high/2)-damplimit;
207 int firstST = 0;
b44e5233 208
f6c18637 209 // i = clock frame of data
b44e5233 210 for (; i < (int)(len / clock); i++)
f38a1528 211 {
f38a1528 212 hithigh = 0;
213 hitlow = 0;
f6c18637 214 startType = -1;
215 z = startIndex + (i*clock);
216 isDamp = 0;
77376577 217
f38a1528 218 /* Find out if we hit both high and low peaks */
219 for (j = 0; j < clock; j++)
f6c18637 220 {
221 if (data[z+j] == high){
f38a1528 222 hithigh = 1;
f6c18637 223 if ( startType == -1)
224 startType = 1;
225 }
226
227 if (data[z+j] == low ){
f38a1528 228 hitlow = 1;
f6c18637 229 if ( startType == -1)
230 startType = 0;
231 }
232
f38a1528 233 if (hithigh && hitlow)
234 break;
b44e5233 235 }
f6c18637 236
237 // No high value found, are we in a dampening field?
238 if ( !hithigh ) {
239 //PrintAndLog(" # Entering damp test at index : %d (%d)", z+j, j);
77376577 240 for (j = 0; j < clock; j++)
f6c18637 241 {
242 if (
243 (data[z+j] <= dampHi && data[z+j] >= dampLow)
244 ){
77376577 245 isDamp++;
f6c18637 246 }
f6c18637 247 }
248 }
f38a1528 249
f6c18637 250 /* Manchester Switching..
251 0: High -> Low
252 1: Low -> High
253 */
254 if (startType == 0)
255 dataout[bitIndex++] = 1;
256 else if (startType == 1)
257 dataout[bitIndex++] = 0;
258 else
259 dataout[bitIndex++] = 2;
260
77376577 261 if ( isDamp > clock/2 ) {
f6c18637 262 firstST++;
263 }
264
265 if ( firstST == 4)
266 break;
f38a1528 267 }
b44e5233 268 return bitIndex;
269 }
270
271 int ManchesterConvertFrom1(const int * data, const size_t len, uint8_t * dataout, int clock, int startIndex){
272
f6c18637 273 PrintAndLog(" Path B");
274
b44e5233 275 int i,j, bitindex, lc, tolerance, warnings;
276 warnings = 0;
277 int upperlimit = len*2/clock+8;
278 i = startIndex;
279 j = 0;
280 tolerance = clock/4;
281 uint8_t decodedArr[len];
282
f6c18637 283 /* Detect duration between 2 successive transitions */
b44e5233 284 for (bitindex = 1; i < len; i++) {
285
286 if (data[i-1] != data[i]) {
287 lc = i - startIndex;
288 startIndex = i;
289
290 // Error check: if bitindex becomes too large, we do not
291 // have a Manchester encoded bitstream or the clock is really wrong!
292 if (bitindex > upperlimit ) {
293 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
294 return 0;
295 }
296 // Then switch depending on lc length:
297 // Tolerance is 1/4 of clock rate (arbitrary)
298 if (abs((lc-clock)/2) < tolerance) {
299 // Short pulse : either "1" or "0"
300 decodedArr[bitindex++] = data[i-1];
301 } else if (abs(lc-clock) < tolerance) {
302 // Long pulse: either "11" or "00"
303 decodedArr[bitindex++] = data[i-1];
304 decodedArr[bitindex++] = data[i-1];
305 } else {
306 ++warnings;
307 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
308 if (warnings > 10) {
309 PrintAndLog("Error: too many detection errors, aborting.");
310 return 0;
f38a1528 311 }
312 }
313 }
314 }
b44e5233 315
316 /*
317 * We have a decodedArr of "01" ("1") or "10" ("0")
318 * parse it into final decoded dataout
319 */
320 for (i = 0; i < bitindex; i += 2) {
321
322 if ((decodedArr[i] == 0) && (decodedArr[i+1] == 1)) {
323 dataout[j++] = 1;
324 } else if ((decodedArr[i] == 1) && (decodedArr[i+1] == 0)) {
325 dataout[j++] = 0;
326 } else {
f38a1528 327 i++;
328 warnings++;
329 PrintAndLog("Unsynchronized, resync...");
b44e5233 330 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
331
332 if (warnings > 10) {
f38a1528 333 PrintAndLog("Error: too many decode errors, aborting.");
334 return 0;
335 }
336 }
337 }
b44e5233 338
339 PrintAndLog("%s", sprint_hex(dataout, j));
340 return j;
341 }
342
343 void ManchesterDiffDecodedString(const uint8_t* bitstream, size_t len, uint8_t invert){
344 /*
345 * We have a bitstream of "01" ("1") or "10" ("0")
346 * parse it into final decoded bitstream
347 */
348 int i, j, warnings;
349 uint8_t decodedArr[(len/2)+1];
f38a1528 350
b44e5233 351 j = warnings = 0;
f38a1528 352
b44e5233 353 uint8_t lastbit = 0;
f38a1528 354
b44e5233 355 for (i = 0; i < len; i += 2) {
356
357 uint8_t first = bitstream[i];
358 uint8_t second = bitstream[i+1];
f38a1528 359
b44e5233 360 if ( first == second ) {
361 ++i;
362 ++warnings;
363 if (warnings > 10) {
364 PrintAndLog("Error: too many decode errors, aborting.");
365 return;
366 }
367 }
368 else if ( lastbit != first ) {
369 decodedArr[j++] = 0 ^ invert;
370 }
371 else {
372 decodedArr[j++] = 1 ^ invert;
373 }
374 lastbit = second;
375 }
376
377 PrintAndLog("%s", sprint_hex(decodedArr, j));
378}
379
f38a1528 380void PrintPaddedManchester( uint8_t* bitStream, size_t len, size_t blocksize){
381
f6c18637 382 PrintAndLog(" Manchester decoded : %d bits", len);
f38a1528 383
f6c18637 384 uint8_t mod = len % blocksize;
385 uint8_t div = len / blocksize;
386 int i;
387
388 // Now output the bitstream to the scrollback by line of 16 bits
389 for (i = 0; i < div*blocksize; i+=blocksize) {
f38a1528 390 PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) );
f6c18637 391 }
392
393 if ( mod > 0 )
394 PrintAndLog(" %s", sprint_bin(bitStream+i, mod) );
395}
Impressum, Datenschutz