X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/549daaf7784e74a8da886e0c32d947d4128ffa1c..c83d6dc6fa3f266ace020cb90eb5df0e3e81e022:/common/lfdemod.c diff --git a/common/lfdemod.c b/common/lfdemod.c index 750dbf1b..395b44b1 100644 --- a/common/lfdemod.c +++ b/common/lfdemod.c @@ -887,6 +887,55 @@ uint8_t detectFSKClk(uint8_t *BitStream, size_t size, uint8_t fcHigh, uint8_t fc //--------------------Modulation Demods &/or Decoding Section----------------------------------- //********************************************************************************************** +void getNextLow(uint8_t samples[], size_t size, int low, int *i) { + while ((samples[*i] > low) && (*i < size)) + *i+=1; +} +void getNextHigh(uint8_t samples[], size_t size, int high, int *i) { + while ((samples[*i] < high) && (*i < size)) + *i+=1; +} + +// load wave counters +bool loadWaveCounters(uint8_t samples[], size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low) { + int i=0, start, waveStart; + size_t testsize = (size < 512) ? size : 512; + + if ( getHiLo(samples, testsize, high, low, 80, 80) == -1 ) { + if (g_debugMode==2) prnt("DEBUG STT: just noise detected - quitting"); + return false; //just noise + } + + // get to first full low to prime loop and skip incomplete first pulse + getNextHigh(samples, size, *high, &i); + getNextLow(samples, size, *low, &i); + *skip = i; + + // populate tmpbuff buffer with pulse lengths + while (i < size) { + // measure from low to low + getNextLow(samples, size, *low, &i); + start = i; + + //find first high point for this wave + getNextHigh(samples, size, *high, &i); + waveStart = i; + + getNextLow(samples, size, *low, &i); + + if (*waveCnt >= (size/32)) + break; + + highToLowWaveLen[*waveCnt] = i - waveStart; //first high to first low + lowToLowWaveLen[*waveCnt] = i - start; + *waveCnt += 1; + if (i-start < *minClk && i < size) { + *minClk = i - start; + } + } + return true; +} + // look for Sequence Terminator - should be pulses of clk*(1 or 2), clk*2, clk*(1.5 or 2), by idx we mean graph position index... bool findST(int *stStopLoc, int *stStartIdx, int lowToLowWaveLen[], int highToLowWaveLen[], int clk, int tol, int buffSize, int *i) { for (; *i < buffSize - 4; *i+=1) { @@ -912,51 +961,17 @@ bool DetectST_ext(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststa uint8_t fndClk[] = {8,16,32,40,50,64,128}; int clk = 0; int tol = 0; - int i, j, skip, start, end, low, high, minClk, waveStart; + int i=0, j, skip, start, end, low, high, minClk=255; //probably should malloc... || test if memory is available ... handle device side? memory danger!!! [marshmellow] int tmpbuff[bufsize / 32]; // low to low wave count //guess rf/32 clock, if click is smaller we will only have room for a fraction of the samples captured int waveLen[bufsize / 32]; // high to low wave count //if clock is larger then we waste memory in array size that is not needed... - size_t testsize = (bufsize < 512) ? bufsize : 512; + //size_t testsize = (bufsize < 512) ? bufsize : 512; int phaseoff = 0; high = low = 128; memset(tmpbuff, 0, sizeof(tmpbuff)); memset(waveLen, 0, sizeof(waveLen)); - if ( getHiLo(buffer, testsize, &high, &low, 80, 80) == -1 ) { - if (g_debugMode==2) prnt("DEBUG STT: just noise detected - quitting"); - return false; //just noise - } - i = 0; - j = 0; - minClk = 255; - // get to first full low to prime loop and skip incomplete first pulse - while ((buffer[i] < high) && (i < bufsize)) - ++i; - while ((buffer[i] > low) && (i < bufsize)) - ++i; - skip = i; - - // populate tmpbuff buffer with pulse lengths - while (i < bufsize) { - // measure from low to low - while ((buffer[i] > low) && (i < bufsize)) - ++i; - start= i; - while ((buffer[i] < high) && (i < bufsize)) - ++i; - //first high point for this wave - waveStart = i; - while ((buffer[i] > low) && (i < bufsize)) - ++i; - if (j >= (bufsize/32)) { - break; - } - waveLen[j] = i - waveStart; //first high to first low - tmpbuff[j++] = i - start; - if (i-start < minClk && i < bufsize) { - minClk = i - start; - } - } + if (!loadWaveCounters(buffer, bufsize, tmpbuff, waveLen, &j, &skip, &minClk, &high, &low)) return false; // set clock - might be able to get this externally and remove this work... if (!clk) { for (uint8_t clkCnt = 0; clkCnt<7; clkCnt++) { @@ -974,7 +989,6 @@ bool DetectST_ext(uint8_t buffer[], size_t *size, int *foundclock, size_t *ststa } else tol = clk/8; *foundclock = clk; - i=0; if (!findST(&start, &skip, tmpbuff, waveLen, clk, tol, j, &i)) { // first ST not found - ERROR if (g_debugMode==2) prnt("DEBUG STT: first STT not found - quitting"); @@ -1074,17 +1088,18 @@ bool DetectST(uint8_t buffer[], size_t *size, int *foundclock) { } //by marshmellow -//take 11 10 01 11 00 and make 01100 ... miller decoding +//take 11 10 01 11 00 and make 01100 ... miller decoding //check for phase errors - should never have half a 1 or 0 by itself and should never exceed 1111 or 0000 in a row //decodes miller encoded binary //NOTE askrawdemod will NOT demod miller encoded ask unless the clock is manually set to 1/2 what it is detected as! -/*int millerRawDecode(uint8_t *BitStream, size_t *size, int invert) { +int millerRawDecode(uint8_t *BitStream, size_t *size, int invert) { if (*size < 16) return -1; - uint16_t MaxBits = 512, errCnt = 0, bestErr = 1000, bestRun = 0; - size_t i, ii, bitCnt=0; - uint8_t alignCnt = 0, curBit = BitStream[0]; + uint16_t MaxBits = 512, errCnt = 0; + size_t i, bitCnt=0; + uint8_t alignCnt = 0, curBit = BitStream[0], alignedIdx = 0; + uint8_t halfClkErr = 0; //find alignment, needs 4 1s or 0s to properly align - for (i=1; i < *size; i++) { + for (i=1; i < *size-1; i++) { alignCnt = (BitStream[i] == curBit) ? alignCnt+1 : 0; curBit = BitStream[i]; if (alignCnt == 4) break; @@ -1094,8 +1109,21 @@ bool DetectST(uint8_t buffer[], size_t *size, int *foundclock) { if (g_debugMode) prnt("ERROR MillerDecode: alignment not found so either your bitstream is not miller or your data does not have a 101 in it"); return -1; } + alignedIdx = (i-1) % 2; + for (i=alignedIdx; i < *size-3; i+=2) { + halfClkErr = (uint8_t)((halfClkErr << 1 | BitStream[i]) & 0xFF); + if ( (halfClkErr & 0x7) == 5 || (halfClkErr & 0x7) == 2 || (i > 2 && (halfClkErr & 0x7) == 0) || (halfClkErr & 0x1F) == 0x1F) { + errCnt++; + BitStream[bitCnt++] = 7; + continue; + } + BitStream[bitCnt++] = BitStream[i] ^ BitStream[i+1] ^ invert; -}*/ + if (bitCnt > MaxBits) break; + } + *size = bitCnt; + return errCnt; +} //by marshmellow //take 01 or 10 = 1 and 11 or 00 = 0 @@ -1876,7 +1904,6 @@ int VikingDemod_AM(uint8_t *dest, size_t *size) { return (int) startIdx; } - // by iceman // find Visa2000 preamble in already demoded data int Visa2kDemod_AM(uint8_t *dest, size_t *size) {