]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/ui.c
FIX: textual changes
[proxmark3-svn] / client / ui.c
... / ...
CommitLineData
1//-----------------------------------------------------------------------------
2// Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
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
12#include <stdarg.h>
13#include <stdlib.h>
14#include <stdio.h>
15#include <stdbool.h>
16#include <time.h>
17#include <readline/readline.h>
18#include <pthread.h>
19#include "loclass/cipherutils.h"
20#include "ui.h"
21#include "cmdmain.h"
22#include "cmddata.h"
23#include "graph.h"
24//#include <liquid/liquid.h>
25#define M_PI 3.14159265358979323846264338327
26
27double CursorScaleFactor;
28int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64;
29int offline;
30int flushAfterWrite = 0;
31extern pthread_mutex_t print_lock;
32
33static char *logfilename = "proxmark3.log";
34
35void PrintAndLog(char *fmt, ...)
36{
37 char *saved_line;
38 int saved_point;
39 va_list argptr, argptr2;
40 static FILE *logfile = NULL;
41 static int logging = 1;
42
43 // lock this section to avoid interlacing prints from different threats
44 pthread_mutex_lock(&print_lock);
45
46 if (logging && !logfile) {
47 logfile = fopen(logfilename, "a");
48 if (!logfile) {
49 fprintf(stderr, "Can't open logfile, logging disabled!\n");
50 logging=0;
51 }
52 }
53
54 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
55
56 if (need_hack) {
57 saved_point = rl_point;
58 saved_line = rl_copy_text(0, rl_end);
59 rl_save_prompt();
60 rl_replace_line("", 0);
61 rl_redisplay();
62 }
63
64 va_start(argptr, fmt);
65 va_copy(argptr2, argptr);
66 vprintf(fmt, argptr);
67 printf(" "); // cleaning prompt
68 va_end(argptr);
69 printf("\n");
70
71 if (need_hack) {
72 rl_restore_prompt();
73 rl_replace_line(saved_line, 0);
74 rl_point = saved_point;
75 rl_redisplay();
76 free(saved_line);
77 }
78
79 if (logging && logfile) {
80 vfprintf(logfile, fmt, argptr2);
81 fprintf(logfile,"\n");
82 fflush(logfile);
83 }
84 va_end(argptr2);
85
86 if (flushAfterWrite == 1) {
87 fflush(NULL);
88 }
89 //release lock
90 pthread_mutex_unlock(&print_lock);
91}
92
93void SetLogFilename(char *fn)
94{
95 logfilename = fn;
96}
97
98int manchester_decode( int * data, const size_t len, uint8_t * dataout, size_t dataoutlen){
99
100 int bitlength = 0;
101 int clock, high, low, startindex;
102 low = startindex = 0;
103 high = 1;
104 uint8_t * bitStream = (uint8_t* ) malloc(sizeof(uint8_t) * dataoutlen);
105 memset(bitStream, 0x00, dataoutlen);
106
107 /* Detect high and lows */
108 DetectHighLowInGraph(&high, &low, TRUE);
109
110 /* get clock */
111 clock = GetAskClock("",false, false);
112
113 startindex = DetectFirstTransition(data, len, high);
114
115 if (high != 1)
116 // decode "raw"
117 bitlength = ManchesterConvertFrom255(data, len, bitStream, dataoutlen, high, low, clock, startindex);
118 else
119 // decode manchester
120 bitlength = ManchesterConvertFrom1(data, len, bitStream, dataoutlen, clock, startindex);
121
122 memcpy(dataout, bitStream, bitlength);
123 free(bitStream);
124 return bitlength;
125}
126
127 int DetectFirstTransition(const int * data, const size_t len, int threshold){
128
129 int i = 0;
130 /* now look for the first threshold */
131 for (; i < len; ++i) {
132 if (data[i] == threshold) {
133 break;
134 }
135 }
136 return i;
137 }
138
139 int ManchesterConvertFrom255(const int * data, const size_t len, uint8_t * dataout, int dataoutlen, int high, int low, int clock, int startIndex){
140
141 int i, j, z, hithigh, hitlow, bitIndex, startType;
142 i = 0;
143 bitIndex = 0;
144
145 int isDamp = 0;
146 int damplimit = (int)((high / 2) * 0.3);
147 int dampHi = (high/2)+damplimit;
148 int dampLow = (high/2)-damplimit;
149 int firstST = 0;
150
151 // i = clock frame of data
152 for (; i < (int)(len/clock); i++)
153 {
154 hithigh = 0;
155 hitlow = 0;
156 startType = -1;
157 z = startIndex + (i*clock);
158 isDamp = 0;
159
160 /* Find out if we hit both high and low peaks */
161 for (j = 0; j < clock; j++)
162 {
163 if (data[z+j] == high){
164 hithigh = 1;
165 if ( startType == -1)
166 startType = 1;
167 }
168
169 if (data[z+j] == low ){
170 hitlow = 1;
171 if ( startType == -1)
172 startType = 0;
173 }
174
175 if (hithigh && hitlow)
176 break;
177 }
178
179 // No high value found, are we in a dampening field?
180 if ( !hithigh ) {
181 //PrintAndLog(" # Entering damp test at index : %d (%d)", z+j, j);
182 for (j = 0; j < clock; j++) {
183 if (
184 (data[z+j] <= dampHi && data[z+j] >= dampLow)
185 ){
186 isDamp++;
187 }
188 }
189 }
190
191 /* Manchester Switching..
192 0: High -> Low
193 1: Low -> High
194 */
195 if (startType == 0)
196 dataout[bitIndex++] = 1;
197 else if (startType == 1)
198 dataout[bitIndex++] = 0;
199 else
200 dataout[bitIndex++] = 2;
201
202 if ( isDamp > clock/2 ) {
203 firstST++;
204 }
205
206 if ( firstST == 4)
207 break;
208 if ( bitIndex >= dataoutlen-1 )
209 break;
210 }
211 return bitIndex;
212 }
213
214 int ManchesterConvertFrom1(const int * data, const size_t len, uint8_t * dataout,int dataoutlen, int clock, int startIndex){
215
216 int i,j, bitindex, lc, tolerance, warnings;
217 warnings = 0;
218 int upperlimit = len*2/clock+8;
219 i = startIndex;
220 j = 0;
221 tolerance = clock/4;
222 uint8_t decodedArr[len];
223
224 /* Detect duration between 2 successive transitions */
225 for (bitindex = 1; i < len; i++) {
226
227 if (data[i-1] != data[i]) {
228 lc = i - startIndex;
229 startIndex = i;
230
231 // Error check: if bitindex becomes too large, we do not
232 // have a Manchester encoded bitstream or the clock is really wrong!
233 if (bitindex > upperlimit ) {
234 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
235 return 0;
236 }
237 // Then switch depending on lc length:
238 // Tolerance is 1/4 of clock rate (arbitrary)
239 if (abs((lc-clock)/2) < tolerance) {
240 // Short pulse : either "1" or "0"
241 decodedArr[bitindex++] = data[i-1];
242 } else if (abs(lc-clock) < tolerance) {
243 // Long pulse: either "11" or "00"
244 decodedArr[bitindex++] = data[i-1];
245 decodedArr[bitindex++] = data[i-1];
246 } else {
247 ++warnings;
248 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
249 if (warnings > 10) {
250 PrintAndLog("Error: too many detection errors, aborting.");
251 return 0;
252 }
253 }
254 }
255 }
256
257 /*
258 * We have a decodedArr of "01" ("1") or "10" ("0")
259 * parse it into final decoded dataout
260 */
261 for (i = 0; i < bitindex; i += 2) {
262
263 if ((decodedArr[i] == 0) && (decodedArr[i+1] == 1)) {
264 dataout[j++] = 1;
265 } else if ((decodedArr[i] == 1) && (decodedArr[i+1] == 0)) {
266 dataout[j++] = 0;
267 } else {
268 i++;
269 warnings++;
270 PrintAndLog("Unsynchronized, resync...");
271 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
272
273 if (warnings > 10) {
274 PrintAndLog("Error: too many decode errors, aborting.");
275 return 0;
276 }
277 }
278 }
279
280 PrintAndLog("%s", sprint_hex(dataout, j));
281 return j;
282 }
283
284 void ManchesterDiffDecodedString(const uint8_t* bitstream, size_t len, uint8_t invert){
285 /*
286 * We have a bitstream of "01" ("1") or "10" ("0")
287 * parse it into final decoded bitstream
288 */
289 int i, j, warnings;
290 uint8_t decodedArr[(len/2)+1];
291
292 j = warnings = 0;
293
294 uint8_t lastbit = 0;
295
296 for (i = 0; i < len; i += 2) {
297
298 uint8_t first = bitstream[i];
299 uint8_t second = bitstream[i+1];
300
301 if ( first == second ) {
302 ++i;
303 ++warnings;
304 if (warnings > 10) {
305 PrintAndLog("Error: too many decode errors, aborting.");
306 return;
307 }
308 }
309 else if ( lastbit != first ) {
310 decodedArr[j++] = 0 ^ invert;
311 }
312 else {
313 decodedArr[j++] = 1 ^ invert;
314 }
315 lastbit = second;
316 }
317
318 PrintAndLog("%s", sprint_hex(decodedArr, j));
319}
320
321void PrintPaddedManchester( uint8_t* bitStream, size_t len, size_t blocksize){
322
323 PrintAndLog(" Manchester decoded : %d bits", len);
324
325 uint8_t mod = len % blocksize;
326 uint8_t div = len / blocksize;
327 int i;
328
329 // Now output the bitstream to the scrollback by line of 16 bits
330 for (i = 0; i < div*blocksize; i+=blocksize) {
331 PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) );
332 }
333
334 if ( mod > 0 )
335 PrintAndLog(" %s", sprint_bin(bitStream+i, mod) );
336}
337
338/* Sliding DFT
339 Smooths out
340*/
341void iceFsk2(int * data, const size_t len){
342
343 int i, j;
344 int * output = (int* ) malloc(sizeof(int) * len);
345 memset(output, 0x00, len);
346
347 // for (i=0; i<len-5; ++i){
348 // for ( j=1; j <=5; ++j) {
349 // output[i] += data[i*j];
350 // }
351 // output[i] /= 5;
352 // }
353 int rest = 127;
354 int tmp =0;
355 for (i=0; i<len; ++i){
356 if ( data[i] < 127)
357 output[i] = 0;
358 else {
359 tmp = (100 * (data[i]-rest)) / rest;
360 output[i] = (tmp > 60)? 100:0;
361 }
362 }
363
364 for (j=0; j<len; ++j)
365 data[j] = output[j];
366
367 free(output);
368}
369
370void iceFsk3(int * data, const size_t len){
371
372 int i,j;
373
374 int * output = (int* ) malloc(sizeof(int) * len);
375 memset(output, 0x00, len);
376 float fc = 0.1125f; // center frequency
377 size_t adjustedLen = len;
378
379 // create very simple low-pass filter to remove images (2nd-order Butterworth)
380 float complex iir_buf[3] = {0,0,0};
381 float b[3] = {0.003621681514929, 0.007243363029857, 0.003621681514929};
382 float a[3] = {1.000000000000000, -1.822694925196308, 0.837181651256023};
383
384 float sample = 0; // input sample read from file
385 float complex x_prime = 1.0f; // save sample for estimating frequency
386 float complex x;
387
388 for (i=0; i<adjustedLen; ++i) {
389
390 sample = data[i]+128;
391
392 // remove DC offset and mix to complex baseband
393 x = (sample - 127.5f) * cexpf( _Complex_I * 2 * M_PI * fc * i );
394
395 // apply low-pass filter, removing spectral image (IIR using direct-form II)
396 iir_buf[2] = iir_buf[1];
397 iir_buf[1] = iir_buf[0];
398 iir_buf[0] = x - a[1]*iir_buf[1] - a[2]*iir_buf[2];
399 x = b[0]*iir_buf[0] +
400 b[1]*iir_buf[1] +
401 b[2]*iir_buf[2];
402
403 // compute instantaneous frequency by looking at phase difference
404 // between adjacent samples
405 float freq = cargf(x*conjf(x_prime));
406 x_prime = x; // retain this sample for next iteration
407
408 output[i] =(freq > 0)? 10 : -10;
409 }
410
411 // show data
412 for (j=0; j<adjustedLen; ++j)
413 data[j] = output[j];
414
415 CmdLtrim("30");
416 adjustedLen -= 30;
417
418 // zero crossings.
419 for (j=0; j<adjustedLen; ++j){
420 if ( data[j] == 10) break;
421 }
422 int startOne =j;
423
424 for (;j<adjustedLen; ++j){
425 if ( data[j] == -10 ) break;
426 }
427 int stopOne = j-1;
428
429 int fieldlen = stopOne-startOne;
430
431 fieldlen = (fieldlen == 39 || fieldlen == 41)? 40 : fieldlen;
432 fieldlen = (fieldlen == 59 || fieldlen == 51)? 50 : fieldlen;
433 if ( fieldlen != 40 && fieldlen != 50){
434 printf("Detected field Length: %d \n", fieldlen);
435 printf("Can only handle 40 or 50. Aborting...\n");
436 return;
437 }
438
439 // FSK sequence start == 000111
440 int startPos = 0;
441 for (i =0; i<adjustedLen; ++i){
442 int dec = 0;
443 for ( j = 0; j < 6*fieldlen; ++j){
444 dec += data[i + j];
445 }
446 if (dec == 0) {
447 startPos = i;
448 break;
449 }
450 }
451
452 printf("000111 position: %d \n", startPos);
453
454 startPos += 6*fieldlen+5;
455
456 int bit =0;
457 printf("BINARY\n");
458 printf("R/40 : ");
459 for (i =startPos ; i < adjustedLen; i += 40){
460 bit = data[i]>0 ? 1:0;
461 printf("%d", bit );
462 }
463 printf("\n");
464
465 printf("R/50 : ");
466 for (i =startPos ; i < adjustedLen; i += 50){
467 bit = data[i]>0 ? 1:0;
468 printf("%d", bit ); }
469 printf("\n");
470
471 free(output);
472}
473
474float complex cexpf (float complex Z)
475{
476 float complex Res;
477 double rho = exp (__real__ Z);
478 __real__ Res = rho * cosf(__imag__ Z);
479 __imag__ Res = rho * sinf(__imag__ Z);
480 return Res;
481}
Impressum, Datenschutz