]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/ui.c
Fixed: the dampening field detection is enhanced. If half a frame (clock rate)...
[proxmark3-svn] / client / ui.c
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 "ui.h"
20 #include "loclass/cipherutils.h"
21
22 double CursorScaleFactor;
23 int PlotGridX, PlotGridY, PlotGridXdefault= 64, PlotGridYdefault= 64;
24 int offline;
25 int flushAfterWrite = 0; //buzzy
26 extern pthread_mutex_t print_lock;
27
28 static char *logfilename = "proxmark3.log";
29
30 void PrintAndLog(char *fmt, ...)
31 {
32 char *saved_line;
33 int saved_point;
34 va_list argptr, argptr2;
35 static FILE *logfile = NULL;
36 static int logging=1;
37
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 }
48
49 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
50
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
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");
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
74 if (logging && logfile) {
75 vfprintf(logfile, fmt, argptr2);
76 fprintf(logfile,"\n");
77 fflush(logfile);
78 }
79 va_end(argptr2);
80
81 if (flushAfterWrite == 1) //buzzy
82 {
83 fflush(NULL);
84 }
85 //release lock
86 pthread_mutex_unlock(&print_lock);
87 }
88
89 void SetLogFilename(char *fn)
90 {
91 logfilename = fn;
92 }
93
94 int manchester_decode( int * data, const size_t len, uint8_t * dataout){
95
96 int bitlength = 0;
97 int i, clock, high, low, startindex;
98 low = startindex = 0;
99 high = 1;
100 uint8_t bitStream[len];
101
102 memset(bitStream, 0x00, len);
103
104 /* Detect high and lows */
105 for (i = 0; i < len; i++) {
106 if (data[i] > high)
107 high = data[i];
108 else if (data[i] < low)
109 low = data[i];
110 }
111
112 /* get clock */
113 clock = GetT55x7Clock( data, len, high );
114 startindex = DetectFirstTransition(data, len, high);
115
116 //PrintAndLog(" Clock : %d", clock);
117 //PrintAndLog(" startindex : %d", startindex);
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
124 //if ( bitlength > 0 )
125 // PrintPaddedManchester(bitStream, bitlength, clock);
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) {
149 /* if this is the beginning of a peak */
150 if ( data[i-1] != data[i] && data[i] == peak) {
151 /* find lowest difference between peaks */
152 if (lastpeak && i - lastpeak < clock)
153 clock = i - lastpeak;
154 lastpeak = i;
155 }
156 }
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 }
172
173 //PrintAndLog(" Found Clock : %d - trying to adjust", clock);
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;
183 }
184
185 int DetectFirstTransition(const int * data, const size_t len, int threshold){
186
187 int i =0;
188 /* now look for the first threshold */
189 for (; i < len; ++i) {
190 if (data[i] == threshold) {
191 break;
192 }
193 }
194 return i;
195 }
196
197 int ManchesterConvertFrom255(const int * data, const size_t len, uint8_t * dataout, int high, int low, int clock, int startIndex){
198
199 int i, j, z, hithigh, hitlow, bitIndex, startType;
200 i = 0;
201 bitIndex = 0;
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;
208
209 // i = clock frame of data
210 for (; i < (int)(len / clock); i++)
211 {
212 hithigh = 0;
213 hitlow = 0;
214 startType = -1;
215 z = startIndex + (i*clock);
216 isDamp = 0;
217
218 /* Find out if we hit both high and low peaks */
219 for (j = 0; j < clock; j++)
220 {
221 if (data[z+j] == high){
222 hithigh = 1;
223 if ( startType == -1)
224 startType = 1;
225 }
226
227 if (data[z+j] == low ){
228 hitlow = 1;
229 if ( startType == -1)
230 startType = 0;
231 }
232
233 if (hithigh && hitlow)
234 break;
235 }
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);
240 for (j = 0; j < clock; j++)
241 {
242 if (
243 (data[z+j] <= dampHi && data[z+j] >= dampLow)
244 ){
245 isDamp++;
246 }
247 }
248 }
249
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
261 if ( isDamp > clock/2 ) {
262 firstST++;
263 }
264
265 if ( firstST == 4)
266 break;
267 }
268 return bitIndex;
269 }
270
271 int ManchesterConvertFrom1(const int * data, const size_t len, uint8_t * dataout, int clock, int startIndex){
272
273 PrintAndLog(" Path B");
274
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
283 /* Detect duration between 2 successive transitions */
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;
311 }
312 }
313 }
314 }
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 {
327 i++;
328 warnings++;
329 PrintAndLog("Unsynchronized, resync...");
330 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
331
332 if (warnings > 10) {
333 PrintAndLog("Error: too many decode errors, aborting.");
334 return 0;
335 }
336 }
337 }
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];
350
351 j = warnings = 0;
352
353 uint8_t lastbit = 0;
354
355 for (i = 0; i < len; i += 2) {
356
357 uint8_t first = bitstream[i];
358 uint8_t second = bitstream[i+1];
359
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
380 void PrintPaddedManchester( uint8_t* bitStream, size_t len, size_t blocksize){
381
382 PrintAndLog(" Manchester decoded : %d bits", len);
383
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) {
390 PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) );
391 }
392
393 if ( mod > 0 )
394 PrintAndLog(" %s", sprint_bin(bitStream+i, mod) );
395 }
Impressum, Datenschutz