1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
17 #include <readline/readline.h>
19 #include "loclass/cipherutils.h"
23 //#include <liquid/liquid.h>
24 #define M_PI 3.14159265358979323846264338327
26 double CursorScaleFactor
;
27 int PlotGridX
, PlotGridY
, PlotGridXdefault
= 64, PlotGridYdefault
= 64;
29 int flushAfterWrite
= 0; //buzzy
30 extern pthread_mutex_t print_lock
;
32 static char *logfilename
= "proxmark3.log";
34 void PrintAndLog(char *fmt
, ...)
38 va_list argptr
, argptr2
;
39 static FILE *logfile
= NULL
;
42 // lock this section to avoid interlacing prints from different threats
43 pthread_mutex_lock(&print_lock
);
45 if (logging
&& !logfile
) {
46 logfile
=fopen(logfilename
, "a");
48 fprintf(stderr
, "Can't open logfile, logging disabled!\n");
53 int need_hack
= (rl_readline_state
& RL_STATE_READCMD
) > 0;
56 saved_point
= rl_point
;
57 saved_line
= rl_copy_text(0, rl_end
);
59 rl_replace_line("", 0);
63 va_start(argptr
, fmt
);
64 va_copy(argptr2
, argptr
);
66 printf(" "); // cleaning prompt
72 rl_replace_line(saved_line
, 0);
73 rl_point
= saved_point
;
78 if (logging
&& logfile
) {
79 vfprintf(logfile
, fmt
, argptr2
);
80 fprintf(logfile
,"\n");
82 fclose(logfile
); // ICEMAN, this logfile is never closed?!?
86 if (flushAfterWrite
== 1) //buzzy
91 pthread_mutex_unlock(&print_lock
);
94 void SetLogFilename(char *fn
)
99 int manchester_decode( int * data
, const size_t len
, uint8_t * dataout
, size_t dataoutlen
){
102 int i
, clock
, high
, low
, startindex
;
103 low
= startindex
= 0;
105 uint8_t * bitStream
= (uint8_t* ) malloc(sizeof(uint8_t) * dataoutlen
);
106 memset(bitStream
, 0x00, dataoutlen
);
108 /* Detect high and lows */
109 for (i
= 0; i
< len
; i
++) {
112 else if (data
[i
] < low
)
117 clock
= GetT55x7Clock( data
, len
, high
);
118 startindex
= DetectFirstTransition(data
, len
, high
);
120 //PrintAndLog(" Clock : %d", clock);
123 bitlength
= ManchesterConvertFrom255(data
, len
, bitStream
, dataoutlen
, high
, low
, clock
, startindex
);
125 bitlength
= ManchesterConvertFrom1(data
, len
, bitStream
, dataoutlen
, clock
, startindex
);
127 memcpy(dataout
, bitStream
, bitlength
);
132 int GetT55x7Clock( const int * data
, const size_t len
, int peak
){
134 int i
,lastpeak
,clock
;
138 /* Detect peak if we don't have one */
140 for (i
= 0; i
< len
; ++i
) {
141 if (data
[i
] > peak
) {
147 for (i
= 1; i
< len
; ++i
) {
148 /* if this is the beginning of a peak */
149 if ( data
[i
-1] != data
[i
] && data
[i
] == peak
) {
150 /* find lowest difference between peaks */
151 if (lastpeak
&& i
- lastpeak
< clock
)
152 clock
= i
- lastpeak
;
157 // When detected clock is 31 or 33 then then return
158 int clockmod
= clock
%8;
159 if ( clockmod
== 0) return clock
;
161 if ( clockmod
== 7 ) clock
+= 1;
162 else if ( clockmod
== 1 ) clock
-= 1;
167 int DetectFirstTransition(const int * data
, const size_t len
, int threshold
){
170 /* now look for the first threshold */
171 for (; i
< len
; ++i
) {
172 if (data
[i
] == threshold
) {
179 int ManchesterConvertFrom255(const int * data
, const size_t len
, uint8_t * dataout
, int dataoutlen
, int high
, int low
, int clock
, int startIndex
){
181 int i
, j
, z
, hithigh
, hitlow
, bitIndex
, startType
;
186 int damplimit
= (int)((high
/ 2) * 0.3);
187 int dampHi
= (high
/2)+damplimit
;
188 int dampLow
= (high
/2)-damplimit
;
191 // i = clock frame of data
192 for (; i
< (int)(len
/clock
); i
++)
197 z
= startIndex
+ (i
*clock
);
200 /* Find out if we hit both high and low peaks */
201 for (j
= 0; j
< clock
; j
++)
203 if (data
[z
+j
] == high
){
205 if ( startType
== -1)
209 if (data
[z
+j
] == low
){
211 if ( startType
== -1)
215 if (hithigh
&& hitlow
)
219 // No high value found, are we in a dampening field?
221 //PrintAndLog(" # Entering damp test at index : %d (%d)", z+j, j);
222 for (j
= 0; j
< clock
; j
++) {
224 (data
[z
+j
] <= dampHi
&& data
[z
+j
] >= dampLow
)
231 /* Manchester Switching..
236 dataout
[bitIndex
++] = 1;
237 else if (startType
== 1)
238 dataout
[bitIndex
++] = 0;
240 dataout
[bitIndex
++] = 2;
242 if ( isDamp
> clock
/2 ) {
248 if ( bitIndex
>= dataoutlen
-1 )
254 int ManchesterConvertFrom1(const int * data
, const size_t len
, uint8_t * dataout
,int dataoutlen
, int clock
, int startIndex
){
256 PrintAndLog(" Path B");
258 int i
,j
, bitindex
, lc
, tolerance
, warnings
;
260 int upperlimit
= len
*2/clock
+8;
264 uint8_t decodedArr
[len
];
266 /* Detect duration between 2 successive transitions */
267 for (bitindex
= 1; i
< len
; i
++) {
269 if (data
[i
-1] != data
[i
]) {
273 // Error check: if bitindex becomes too large, we do not
274 // have a Manchester encoded bitstream or the clock is really wrong!
275 if (bitindex
> upperlimit
) {
276 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
279 // Then switch depending on lc length:
280 // Tolerance is 1/4 of clock rate (arbitrary)
281 if (abs((lc
-clock
)/2) < tolerance
) {
282 // Short pulse : either "1" or "0"
283 decodedArr
[bitindex
++] = data
[i
-1];
284 } else if (abs(lc
-clock
) < tolerance
) {
285 // Long pulse: either "11" or "00"
286 decodedArr
[bitindex
++] = data
[i
-1];
287 decodedArr
[bitindex
++] = data
[i
-1];
290 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
292 PrintAndLog("Error: too many detection errors, aborting.");
300 * We have a decodedArr of "01" ("1") or "10" ("0")
301 * parse it into final decoded dataout
303 for (i
= 0; i
< bitindex
; i
+= 2) {
305 if ((decodedArr
[i
] == 0) && (decodedArr
[i
+1] == 1)) {
307 } else if ((decodedArr
[i
] == 1) && (decodedArr
[i
+1] == 0)) {
312 PrintAndLog("Unsynchronized, resync...");
313 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
316 PrintAndLog("Error: too many decode errors, aborting.");
322 PrintAndLog("%s", sprint_hex(dataout
, j
));
326 void ManchesterDiffDecodedString(const uint8_t* bitstream
, size_t len
, uint8_t invert
){
328 * We have a bitstream of "01" ("1") or "10" ("0")
329 * parse it into final decoded bitstream
332 uint8_t decodedArr
[(len
/2)+1];
338 for (i
= 0; i
< len
; i
+= 2) {
340 uint8_t first
= bitstream
[i
];
341 uint8_t second
= bitstream
[i
+1];
343 if ( first
== second
) {
347 PrintAndLog("Error: too many decode errors, aborting.");
351 else if ( lastbit
!= first
) {
352 decodedArr
[j
++] = 0 ^ invert
;
355 decodedArr
[j
++] = 1 ^ invert
;
360 PrintAndLog("%s", sprint_hex(decodedArr
, j
));
363 void PrintPaddedManchester( uint8_t* bitStream
, size_t len
, size_t blocksize
){
365 PrintAndLog(" Manchester decoded : %d bits", len
);
367 uint8_t mod
= len
% blocksize
;
368 uint8_t div
= len
/ blocksize
;
371 // Now output the bitstream to the scrollback by line of 16 bits
372 for (i
= 0; i
< div
*blocksize
; i
+=blocksize
) {
373 PrintAndLog(" %s", sprint_bin(bitStream
+i
,blocksize
) );
377 PrintAndLog(" %s", sprint_bin(bitStream
+i
, mod
) );
383 void iceFsk2(int * data
, const size_t len
){
386 int * output
= (int* ) malloc(sizeof(int) * len
);
387 memset(output
, 0x00, len
);
389 // for (i=0; i<len-5; ++i){
390 // for ( j=1; j <=5; ++j) {
391 // output[i] += data[i*j];
397 for (i
=0; i
<len
; ++i
){
401 tmp
= (100 * (data
[i
]-rest
)) / rest
;
402 output
[i
] = (tmp
> 60)? 100:0;
406 for (j
=0; j
<len
; ++j
)
412 void iceFsk3(int * data
, const size_t len
){
416 int * output
= (int* ) malloc(sizeof(int) * len
);
417 memset(output
, 0x00, len
);
418 float fc
= 0.1125f
; // center frequency
419 size_t adjustedLen
= len
;
421 // create very simple low-pass filter to remove images (2nd-order Butterworth)
422 float complex iir_buf
[3] = {0,0,0};
423 float b
[3] = {0.003621681514929, 0.007243363029857, 0.003621681514929};
424 float a
[3] = {1.000000000000000, -1.822694925196308, 0.837181651256023};
426 float sample
= 0; // input sample read from file
427 float complex x_prime
= 1.0f
; // save sample for estimating frequency
430 for (i
=0; i
<adjustedLen
; ++i
) {
432 sample
= data
[i
]+128;
434 // remove DC offset and mix to complex baseband
435 x
= (sample
- 127.5f
) * cexpf( _Complex_I
* 2 * M_PI
* fc
* i
);
437 // apply low-pass filter, removing spectral image (IIR using direct-form II)
438 iir_buf
[2] = iir_buf
[1];
439 iir_buf
[1] = iir_buf
[0];
440 iir_buf
[0] = x
- a
[1]*iir_buf
[1] - a
[2]*iir_buf
[2];
441 x
= b
[0]*iir_buf
[0] +
445 // compute instantaneous frequency by looking at phase difference
446 // between adjacent samples
447 float freq
= cargf(x
*conjf(x_prime
));
448 x_prime
= x
; // retain this sample for next iteration
450 output
[i
] =(freq
> 0)? 10 : -10;
454 for (j
=0; j
<adjustedLen
; ++j
)
461 for (j
=0; j
<adjustedLen
; ++j
){
462 if ( data
[j
] == 10) break;
466 for (;j
<adjustedLen
; ++j
){
467 if ( data
[j
] == -10 ) break;
471 int fieldlen
= stopOne
-startOne
;
473 fieldlen
= (fieldlen
== 39 || fieldlen
== 41)? 40 : fieldlen
;
474 fieldlen
= (fieldlen
== 59 || fieldlen
== 51)? 50 : fieldlen
;
475 if ( fieldlen
!= 40 && fieldlen
!= 50){
476 printf("Detected field Length: %d \n", fieldlen
);
477 printf("Can only handle 40 or 50. Aborting...\n");
481 // FSK sequence start == 000111
483 for (i
=0; i
<adjustedLen
; ++i
){
485 for ( j
= 0; j
< 6*fieldlen
; ++j
){
494 printf("000111 position: %d \n", startPos
);
496 startPos
+= 6*fieldlen
+5;
501 for (i
=startPos
; i
< adjustedLen
; i
+= 40){
502 bit
= data
[i
]>0 ? 1:0;
508 for (i
=startPos
; i
< adjustedLen
; i
+= 50){
509 bit
= data
[i
]>0 ? 1:0;
510 printf("%d", bit
); }
516 float complex cexpf (float complex Z
)
519 double rho
= exp (__real__ Z
);
520 __real__ Res
= rho
* cosf(__imag__ Z
);
521 __imag__ Res
= rho
* sinf(__imag__ Z
);