+//by marshmellow
+//takes 1s and 0s and searches for EM410x format - output EM ID
+uint8_t Em410xDecode(uint8_t *BitStream, size_t *size, size_t *startIdx, uint32_t *hi, uint64_t *lo)
+{
+ //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
+ 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;
+ uint8_t FmtLen = 10;
+ *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;
+ if (*size<64) return 0;
+ if (*size>64) FmtLen = 22;
+ idx = *startIdx + 9;
+ for (i=0; i<FmtLen; i++){ //loop through 10 or 22 sets of 5 bits (50-10p = 40 bits or 88 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;
+ }
+ //set uint64 with ID from BitStream
+ for (uint8_t ii=0; ii<4; ii++){
+ *hi = (*hi << 1) | (*lo >> 63);
+ *lo = (*lo << 1) | (BitStream[(i*5)+ii+idx]);
+ }
+ }
+ if (errChk != 0) return 1;
+ //skip last 5 bit parity test for simplicity.
+ // *size = 64 | 128;
+ }
+ return 0;
+}
+