]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - common/lfdemod.c
CHG: minor fixes to some tnp luascripts
[proxmark3-svn] / common / lfdemod.c
index 5b0bc29d28dfb61fd8daf75e07ceb64603c930be..3305c8b0c53e84adb4cdd992c28c3a51fac531b2 100644 (file)
@@ -97,6 +97,7 @@ uint8_t Em410xDecode(uint8_t *BitStream, size_t *size, size_t *startIdx, uint32_
   for (uint8_t extraBitChk=0; extraBitChk<5; extraBitChk++){
     errChk = preambleSearch(BitStream+extraBitChk+*startIdx, preamble, sizeof(preamble), size, startIdx);
     if (errChk == 0) return 0;
   for (uint8_t extraBitChk=0; extraBitChk<5; extraBitChk++){
     errChk = preambleSearch(BitStream+extraBitChk+*startIdx, preamble, sizeof(preamble), size, startIdx);
     if (errChk == 0) return 0;
+       if (*size<64) return 0;
     if (*size>64) FmtLen = 22;
     if (*size<64) return 0;
     idx = *startIdx + 9;
     if (*size>64) FmtLen = 22;
     if (*size<64) return 0;
     idx = *startIdx + 9;
@@ -375,34 +376,39 @@ int cleanAskRawDemod(uint8_t *BinStream, size_t *size, int clk, int invert, int
                        smplCnt++;
                } else if (BinStream[i] <= low && !waveHigh){
                        smplCnt++;
                        smplCnt++;
                } else if (BinStream[i] <= low && !waveHigh){
                        smplCnt++;
-               } else { //not high or low or a transition
-                       if (smplCnt > clk-(clk/4)) { //full clock
-                               if (smplCnt > clk + (clk/4)) { //too many samples
-                                       errCnt++;
-                                       BinStream[bitCnt++]=77;
-                               } else if (waveHigh) {
-                                       BinStream[bitCnt++] = invert;
-                                       BinStream[bitCnt++] = invert;
-                               } else if (!waveHigh) {
-                                       BinStream[bitCnt++] = invert ^ 1;
-                                       BinStream[bitCnt++] = invert ^ 1;
-                               }
-                               waveHigh ^= 1;  
-                               smplCnt = 0;
-                       } else if (smplCnt > (clk/2) - (clk/5)) {
-                               if (waveHigh) {
-                                       BinStream[bitCnt++] = invert;
-                               } else if (!waveHigh) {
-                                       BinStream[bitCnt++] = invert ^ 1;
+               } else { //transition
+                       if ((BinStream[i] >= high && !waveHigh) || (BinStream[i] <= low && waveHigh)){
+                               if (smplCnt > clk-(clk/4)-1) { //full clock
+                                       if (smplCnt > clk + (clk/4)+1) { //too many samples
+                                               errCnt++;
+                                               BinStream[bitCnt++]=77;
+                                       } else if (waveHigh) {
+                                               BinStream[bitCnt++] = invert;
+                                               BinStream[bitCnt++] = invert;
+                                       } else if (!waveHigh) {
+                                               BinStream[bitCnt++] = invert ^ 1;
+                                               BinStream[bitCnt++] = invert ^ 1;
+                                       }
+                                       waveHigh ^= 1;  
+                                       smplCnt = 0;
+                               } else if (smplCnt > (clk/2) - (clk/4)-1) {
+                                       if (waveHigh) {
+                                               BinStream[bitCnt++] = invert;
+                                       } else if (!waveHigh) {
+                                               BinStream[bitCnt++] = invert ^ 1;
+                                       }
+                                       waveHigh ^= 1;  
+                                       smplCnt = 0;
+                               } else if (!bitCnt) {
+                                       //first bit
+                                       waveHigh = (BinStream[i] >= high);
+                                       smplCnt = 1;
+                               } else {
+                                       smplCnt++;
+                                       //transition bit oops
                                }
                                }
-                               waveHigh ^= 1;  
-                               smplCnt = 0;
-                       } else if (!bitCnt) {
-                               //first bit
-                               waveHigh = (BinStream[i] >= high);
-                               smplCnt = 1;
-                       } else {
-                               //transition bit? ignore
+                       } else { //haven't hit new high or new low yet
+                               smplCnt++;
                        }
                }
        }
                        }
                }
        }
@@ -888,16 +894,18 @@ int PyramiddemodFSK(uint8_t *dest, size_t *size)
 
 uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, int high, int low)
 {
 
 uint8_t DetectCleanAskWave(uint8_t dest[], size_t size, int high, int low)
 {
-       uint8_t allPeaks=1;
+       uint16_t allPeaks=1;
        uint16_t cntPeaks=0;
        uint16_t cntPeaks=0;
-       for (size_t i=30; i<255; i++){
+       size_t loopEnd = 572;
+       if (loopEnd > size) loopEnd = size;
+       for (size_t i=60; i<loopEnd; i++){
                if (dest[i]>low && dest[i]<high) 
                        allPeaks=0;
                else
                        cntPeaks++;
        }
                if (dest[i]>low && dest[i]<high) 
                        allPeaks=0;
                else
                        cntPeaks++;
        }
-       if (allPeaks==0){
-               if (cntPeaks>210) return 1;
+       if (allPeaks == 0){
+               if (cntPeaks > 300) return 1;
        }
        return allPeaks;
 }
        }
        return allPeaks;
 }
@@ -939,10 +947,12 @@ int DetectStrongAskClock(uint8_t dest[], size_t size)
                        }
                }
        }
                        }
                }
        }
+       uint8_t tol;
        for (idx=8; idx>0; idx--){
        for (idx=8; idx>0; idx--){
-               if (clk[idx] >= highCnt && clk[idx] <= highCnt+2)
+               tol = clk[idx]/8;
+               if (clk[idx] >= highCnt - tol && clk[idx] <= highCnt + tol)
                        return clk[idx];
                        return clk[idx];
-               if (clk[idx] >= highCnt2 && clk[idx] <= highCnt2+2)
+               if (clk[idx] >= highCnt2 - tol && clk[idx] <= highCnt2 + tol)
                        return clk[idx];
        }
        return -1;
                        return clk[idx];
        }
        return -1;
@@ -1742,22 +1752,22 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
   //find first phase shift
   for (i=0; i<loopCnt; i++){
     if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
   //find first phase shift
   for (i=0; i<loopCnt; i++){
     if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
-      waveEnd = i+1;
-      //PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
-      waveLenCnt = waveEnd-waveStart;
+        waveEnd = i+1;
+        //PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
+        waveLenCnt = waveEnd-waveStart;
       if (waveLenCnt > fc && waveStart > fc){ //not first peak and is a large wave 
         lastAvgWaveVal = avgWaveVal/(waveLenCnt);
       if (waveLenCnt > fc && waveStart > fc){ //not first peak and is a large wave 
         lastAvgWaveVal = avgWaveVal/(waveLenCnt);
-        firstFullWave = waveStart;
-        fullWaveLen=waveLenCnt;
-        //if average wave value is > graph 0 then it is an up wave or a 1
+          firstFullWave = waveStart;
+          fullWaveLen=waveLenCnt;
+          //if average wave value is > graph 0 then it is an up wave or a 1
         if (lastAvgWaveVal > 123) curPhase^=1;  //fudge graph 0 a little 123 vs 128
         if (lastAvgWaveVal > 123) curPhase^=1;  //fudge graph 0 a little 123 vs 128
-        break;
-      } 
+          break;
+        
       waveStart = i+1;
       avgWaveVal = 0;
       waveStart = i+1;
       avgWaveVal = 0;
-    }
+      }
     avgWaveVal+=dest[i+2];
     avgWaveVal+=dest[i+2];
-  }
+    }
   //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);  
   lastClkBit = firstFullWave; //set start of wave as clock align
   //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
   //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);  
   lastClkBit = firstFullWave; //set start of wave as clock align
   //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
@@ -1779,7 +1789,7 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
         waveEnd = i+1;
         waveLenCnt = waveEnd-waveStart;
         lastAvgWaveVal = avgWaveVal/waveLenCnt;
         waveEnd = i+1;
         waveLenCnt = waveEnd-waveStart;
         lastAvgWaveVal = avgWaveVal/waveLenCnt;
-        if (waveLenCnt > fc){  
+        if (waveLenCnt > fc){ 
           //PrintAndLog("DEBUG: avgWaveVal: %d, waveSum: %d",lastAvgWaveVal,avgWaveVal);
           //if this wave is a phase shift
           //PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+*clock-tol,i+1,fc);
           //PrintAndLog("DEBUG: avgWaveVal: %d, waveSum: %d",lastAvgWaveVal,avgWaveVal);
           //if this wave is a phase shift
           //PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+*clock-tol,i+1,fc);
Impressum, Datenschutz