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"
24 #define M_PI 3.14159265358979323846264338327
26 double CursorScaleFactor
;
27 int PlotGridX
, PlotGridY
, PlotGridXdefault
= 64, PlotGridYdefault
= 64;
29 int flushAfterWrite
= 0;
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
;
40 static int logging
= 1;
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");
85 if (flushAfterWrite
== 1) {
89 pthread_mutex_unlock(&print_lock
);
92 void SetLogFilename(char *fn
) {
96 void iceFsk3(int * data
, const size_t len
){
100 int * output
= (int* ) malloc(sizeof(int) * len
);
101 memset(output
, 0x00, len
);
102 float fc
= 0.1125f
; // center frequency
103 size_t adjustedLen
= len
;
105 // create very simple low-pass filter to remove images (2nd-order Butterworth)
106 float complex iir_buf
[3] = {0,0,0};
107 float b
[3] = {0.003621681514929, 0.007243363029857, 0.003621681514929};
108 float a
[3] = {1.000000000000000, -1.822694925196308, 0.837181651256023};
110 float sample
= 0; // input sample read from file
111 float complex x_prime
= 1.0f
; // save sample for estimating frequency
114 for (i
=0; i
<adjustedLen
; ++i
) {
116 sample
= data
[i
]+128;
118 // remove DC offset and mix to complex baseband
119 x
= (sample
- 127.5f
) * cexpf( _Complex_I
* 2 * M_PI
* fc
* i
);
121 // apply low-pass filter, removing spectral image (IIR using direct-form II)
122 iir_buf
[2] = iir_buf
[1];
123 iir_buf
[1] = iir_buf
[0];
124 iir_buf
[0] = x
- a
[1]*iir_buf
[1] - a
[2]*iir_buf
[2];
125 x
= b
[0]*iir_buf
[0] +
129 // compute instantaneous frequency by looking at phase difference
130 // between adjacent samples
131 float freq
= cargf(x
*conjf(x_prime
));
132 x_prime
= x
; // retain this sample for next iteration
134 output
[i
] =(freq
> 0)? 10 : -10;
138 for (j
=0; j
<adjustedLen
; ++j
)
145 for (j
=0; j
<adjustedLen
; ++j
){
146 if ( data
[j
] == 10) break;
150 for (;j
<adjustedLen
; ++j
){
151 if ( data
[j
] == -10 ) break;
155 int fieldlen
= stopOne
-startOne
;
157 fieldlen
= (fieldlen
== 39 || fieldlen
== 41)? 40 : fieldlen
;
158 fieldlen
= (fieldlen
== 59 || fieldlen
== 51)? 50 : fieldlen
;
159 if ( fieldlen
!= 40 && fieldlen
!= 50){
160 printf("Detected field Length: %d \n", fieldlen
);
161 printf("Can only handle 40 or 50. Aborting...\n");
166 // FSK sequence start == 000111
168 for (i
=0; i
<adjustedLen
; ++i
){
170 for ( j
= 0; j
< 6*fieldlen
; ++j
){
179 printf("000111 position: %d \n", startPos
);
181 startPos
+= 6*fieldlen
+5;
186 for (i
=startPos
; i
< adjustedLen
; i
+= 40){
187 bit
= data
[i
]>0 ? 1:0;
193 for (i
=startPos
; i
< adjustedLen
; i
+= 50){
194 bit
= data
[i
]>0 ? 1:0;
195 printf("%d", bit
); }
201 float complex cexpf (float complex Z
)
204 double rho
= exp (__real__ Z
);
205 __real__ Res
= rho
* cosf(__imag__ Z
);
206 __imag__ Res
= rho
* sinf(__imag__ Z
);