- //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
- // otherwise could be a void with no arguments
- //set defaults
- uint64_t lo=0;
- uint32_t i = 0;
- if (BitStream[10]>1){ //allow only 1s and 0s
- // PrintAndLog("no data found");
- return 0;
- }
- uint8_t parityTest=0;
- // 111111111 bit pattern represent start of frame
- uint8_t frame_marker_mask[] = {1,1,1,1,1,1,1,1,1};
- uint32_t idx = 0;
- uint32_t ii=0;
- uint8_t resetCnt = 0;
- while( (idx + 64) < *size) {
- restart:
- // search for a start of frame marker
- if ( memcmp(BitStream+idx, frame_marker_mask, sizeof(frame_marker_mask)) == 0)
- { // frame marker found
- *startIdx=idx;
- idx+=9;
- for (i=0; i<10;i++){
- for(ii=0; ii<5; ++ii){
- parityTest ^= BitStream[(i*5)+ii+idx];
- }
- if (!parityTest){ //even parity
- parityTest=0;
- for (ii=0; ii<4;++ii){
- lo=(lo<<1LL)|(BitStream[(i*5)+ii+idx]);
- }
- //PrintAndLog("DEBUG: EM parity passed parity val: %d, i:%d, ii:%d,idx:%d, Buffer: %d%d%d%d%d,lo: %d",parityTest,i,ii,idx,BitStream[idx+ii+(i*5)-5],BitStream[idx+ii+(i*5)-4],BitStream[idx+ii+(i*5)-3],BitStream[idx+ii+(i*5)-2],BitStream[idx+ii+(i*5)-1],lo);
- }else {//parity failed
- //PrintAndLog("DEBUG: EM parity failed parity val: %d, i:%d, ii:%d,idx:%d, Buffer: %d%d%d%d%d",parityTest,i,ii,idx,BitStream[idx+ii+(i*5)-5],BitStream[idx+ii+(i*5)-4],BitStream[idx+ii+(i*5)-3],BitStream[idx+ii+(i*5)-2],BitStream[idx+ii+(i*5)-1]);
- parityTest=0;
- idx-=8;
- if (resetCnt>5)return 0; //try 5 times
- resetCnt++;
- goto restart;//continue;
- }
- }
- //skip last 5 bit parity test for simplicity.
- *size = 64;
- return lo;
- }else{
- idx++;
- }
- }
- return 0;
+ //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
+ // otherwise could be a void with no arguments
+ //set defaults
+ uint64_t lo=0;
+ uint32_t i = 0;
+ if (BitStream[1]>1){ //allow only 1s and 0s
+ // PrintAndLog("no data found");
+ return 0;
+ }
+ // 111111111 bit pattern represent start of frame
+ uint8_t preamble[] = {1,1,1,1,1,1,1,1,1};
+ uint32_t idx = 0;
+ uint32_t parityBits = 0;
+ uint8_t errChk = 0;
+ *startIdx = 0;
+ for (uint8_t extraBitChk=0; extraBitChk<5; extraBitChk++){
+ errChk = preambleSearch(BitStream+extraBitChk+*startIdx, preamble, sizeof(preamble), size, startIdx);
+ if (errChk == 0) return 0;
+ idx = *startIdx + 9;
+ for (i=0; i<10;i++){ //loop through 10 sets of 5 bits (50-10p = 40 bits)
+ parityBits = bytebits_to_byte(BitStream+(i*5)+idx,5);
+ //check even parity
+ if (parityTest(parityBits, 5, 0) == 0){
+ //parity failed try next bit (in the case of 1111111111) but last 9 = preamble
+ startIdx++;
+ errChk = 0;
+ break;
+ }
+ for (uint8_t ii=0; ii<4; ii++){
+ lo = (lo << 1LL) | (BitStream[(i*5)+ii+idx]);
+ }
+ }
+ if (errChk != 0) return lo;
+ //skip last 5 bit parity test for simplicity.
+ // *size = 64;
+ }
+ return 0;