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>
20 #include "loclass/cipherutils.h"
22 double CursorScaleFactor
;
23 int PlotGridX
, PlotGridY
, PlotGridXdefault
= 64, PlotGridYdefault
= 64;
25 int flushAfterWrite
= 0; //buzzy
26 extern pthread_mutex_t print_lock
;
28 static char *logfilename
= "proxmark3.log";
30 void PrintAndLog(char *fmt
, ...)
34 va_list argptr
, argptr2
;
35 static FILE *logfile
= NULL
;
38 // lock this section to avoid interlacing prints from different threats
39 pthread_mutex_lock(&print_lock
);
41 if (logging
&& !logfile
) {
42 logfile
=fopen(logfilename
, "a");
44 fprintf(stderr
, "Can't open logfile, logging disabled!\n");
49 int need_hack
= (rl_readline_state
& RL_STATE_READCMD
) > 0;
52 saved_point
= rl_point
;
53 saved_line
= rl_copy_text(0, rl_end
);
55 rl_replace_line("", 0);
59 va_start(argptr
, fmt
);
60 va_copy(argptr2
, argptr
);
62 printf(" "); // cleaning prompt
68 rl_replace_line(saved_line
, 0);
69 rl_point
= saved_point
;
74 if (logging
&& logfile
) {
75 vfprintf(logfile
, fmt
, argptr2
);
76 fprintf(logfile
,"\n");
81 if (flushAfterWrite
== 1) //buzzy
86 pthread_mutex_unlock(&print_lock
);
89 void SetLogFilename(char *fn
)
94 int manchester_decode( int * data
, const size_t len
, uint8_t * dataout
){
97 int i
, clock
, high
, low
, startindex
;
100 uint8_t bitStream
[len
];
102 memset(bitStream
, 0x00, len
);
104 /* Detect high and lows */
105 for (i
= 0; i
< len
; i
++) {
108 else if (data
[i
] < low
)
113 clock
= GetT55x7Clock( data
, len
, high
);
114 startindex
= DetectFirstTransition(data
, len
, high
);
116 //PrintAndLog(" Clock : %d", clock);
117 //PrintAndLog(" startindex : %d", startindex);
120 bitlength
= ManchesterConvertFrom255(data
, len
, bitStream
, high
, low
, clock
, startindex
);
122 bitlength
= ManchesterConvertFrom1(data
, len
, bitStream
, clock
, startindex
);
124 //if ( bitlength > 0 )
125 // PrintPaddedManchester(bitStream, bitlength, clock);
127 memcpy(dataout
, bitStream
, bitlength
);
133 int GetT55x7Clock( const int * data
, const size_t len
, int peak
){
135 int i
,lastpeak
,clock
;
139 /* Detect peak if we don't have one */
141 for (i
= 0; i
< len
; ++i
) {
142 if (data
[i
] > peak
) {
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
;
158 //defaults clock to precise values.
173 //PrintAndLog(" Found Clock : %d - trying to adjust", clock);
175 // When detected clock is 31 or 33 then then return
176 int clockmod
= clock
%8;
179 else if ( clockmod
== 1 )
185 int DetectFirstTransition(const int * data
, const size_t len
, int threshold
){
188 /* now look for the first threshold */
189 for (; i
< len
; ++i
) {
190 if (data
[i
] == threshold
) {
197 int ManchesterConvertFrom255(const int * data
, const size_t len
, uint8_t * dataout
, int high
, int low
, int clock
, int startIndex
){
199 int i
, j
, z
, hithigh
, hitlow
, bitIndex
, startType
;
204 int damplimit
= (int)((high
/ 2) * 0.3);
205 int dampHi
= (high
/2)+damplimit
;
206 int dampLow
= (high
/2)-damplimit
;
209 // i = clock frame of data
210 for (; i
< (int)(len
/ clock
); i
++)
215 z
= startIndex
+ (i
*clock
);
218 /* Find out if we hit both high and low peaks */
219 for (j
= 0; j
< clock
; j
++)
221 if (data
[z
+j
] == high
){
223 if ( startType
== -1)
227 if (data
[z
+j
] == low
){
229 if ( startType
== -1)
233 if (hithigh
&& hitlow
)
237 // No high value found, are we in a dampening field?
239 //PrintAndLog(" # Entering damp test at index : %d (%d)", z+j, j);
240 for (j
= 0; j
< clock
; j
++)
243 (data
[z
+j
] <= dampHi
&& data
[z
+j
] >= dampLow
)
250 /* Manchester Switching..
255 dataout
[bitIndex
++] = 1;
256 else if (startType
== 1)
257 dataout
[bitIndex
++] = 0;
259 dataout
[bitIndex
++] = 2;
261 if ( isDamp
> clock
/2 ) {
271 int ManchesterConvertFrom1(const int * data
, const size_t len
, uint8_t * dataout
, int clock
, int startIndex
){
273 PrintAndLog(" Path B");
275 int i
,j
, bitindex
, lc
, tolerance
, warnings
;
277 int upperlimit
= len
*2/clock
+8;
281 uint8_t decodedArr
[len
];
283 /* Detect duration between 2 successive transitions */
284 for (bitindex
= 1; i
< len
; i
++) {
286 if (data
[i
-1] != data
[i
]) {
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.");
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];
307 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
309 PrintAndLog("Error: too many detection errors, aborting.");
317 * We have a decodedArr of "01" ("1") or "10" ("0")
318 * parse it into final decoded dataout
320 for (i
= 0; i
< bitindex
; i
+= 2) {
322 if ((decodedArr
[i
] == 0) && (decodedArr
[i
+1] == 1)) {
324 } else if ((decodedArr
[i
] == 1) && (decodedArr
[i
+1] == 0)) {
329 PrintAndLog("Unsynchronized, resync...");
330 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
333 PrintAndLog("Error: too many decode errors, aborting.");
339 PrintAndLog("%s", sprint_hex(dataout
, j
));
343 void ManchesterDiffDecodedString(const uint8_t* bitstream
, size_t len
, uint8_t invert
){
345 * We have a bitstream of "01" ("1") or "10" ("0")
346 * parse it into final decoded bitstream
349 uint8_t decodedArr
[(len
/2)+1];
355 for (i
= 0; i
< len
; i
+= 2) {
357 uint8_t first
= bitstream
[i
];
358 uint8_t second
= bitstream
[i
+1];
360 if ( first
== second
) {
364 PrintAndLog("Error: too many decode errors, aborting.");
368 else if ( lastbit
!= first
) {
369 decodedArr
[j
++] = 0 ^ invert
;
372 decodedArr
[j
++] = 1 ^ invert
;
377 PrintAndLog("%s", sprint_hex(decodedArr
, j
));
380 void PrintPaddedManchester( uint8_t* bitStream
, size_t len
, size_t blocksize
){
382 PrintAndLog(" Manchester decoded : %d bits", len
);
384 uint8_t mod
= len
% blocksize
;
385 uint8_t div
= len
/ blocksize
;
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
) );
394 PrintAndLog(" %s", sprint_bin(bitStream
+i
, mod
) );