- errChk = preambleSearch(BitStream, preamble, sizeof(preamble), size, startIdx);
- if (errChk == 0 ) return -4;
- if (*size < 64) return -3;
- if (*size > 64) FmtLen = 22;
- *startIdx += 1; //get rid of 0 from preamble
- 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 - quit if failed
- if (parityTest(parityBits, 5, 0) == 0) return -5;
- //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]);
- }
+
+ // preamble 0111111111
+ // include 0 in front to help get start pos
+ uint8_t preamble[] = {0,1,1,1,1,1,1,1,1,1};
+ if (!preambleSearch(BitStream, preamble, sizeof(preamble), size, startIdx))
+ return 0;
+ if (*size < 64) return 0;
+
+ fmtlen = (*size == 110) ? 22 : 10;
+
+ //skip last 4bit parity row for simplicity
+ *size = removeParity(BitStream, *startIdx + sizeof(preamble), 5, 0, fmtlen * 5);
+
+ switch (*size) {
+ case 40: {
+ // std em410x format
+ *hi = 0;
+ *lo = ((uint64_t)(bytebits_to_byte(BitStream, 8)) << 32) | (bytebits_to_byte(BitStream + 8, 32));
+ break;
+ }
+ case 88: {
+ // long em format
+ *hi = (bytebits_to_byte(BitStream, 24));
+ *lo = ((uint64_t)(bytebits_to_byte(BitStream + 24, 32)) << 32) | (bytebits_to_byte(BitStream + 24 + 32, 32));
+ break;
+ }
+ default: return 0;
+