+
+int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert) {
+ int startIdx = 0;
+ return pskRawDemod_ext(dest, size, clock, invert, &startIdx);
+}
+
+//**********************************************************************************************
+//-----------------Tag format detection section-------------------------------------------------
+//**********************************************************************************************
+
+// by marshmellow
+// FSK Demod then try to locate an AWID ID
+int AWIDdemodFSK(uint8_t *dest, size_t *size, int *waveStartIdx) {
+ //make sure buffer has enough data
+ if (*size < 96*50) return -1;
+
+ // FSK demodulator
+ *size = fskdemod(dest, *size, 50, 1, 10, 8, waveStartIdx); // fsk2a RF/50
+ if (*size < 96) return -3; //did we get a good demod?
+
+ uint8_t preamble[] = {0,0,0,0,0,0,0,1};
+ size_t startIdx = 0;
+ uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
+ if (errChk == 0) return -4; //preamble not found
+ if (*size != 96) return -5;
+ return (int)startIdx;
+}
+
+//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)
+{
+ //sanity checks
+ if (*size < 64) return 0;
+ if (BitStream[1]>1) return 0; //allow only 1s and 0s
+
+ // 111111111 bit pattern represent start of frame
+ // include 0 in front to help get start pos
+ uint8_t preamble[] = {0,1,1,1,1,1,1,1,1,1};
+ uint8_t errChk = 0;
+ uint8_t FmtLen = 10; // sets of 4 bits = end data
+ *startIdx = 0;
+ errChk = preambleSearch(BitStream, preamble, sizeof(preamble), size, startIdx);
+ if ( errChk == 0 || (*size != 64 && *size != 128) ) return 0;
+ if (*size == 128) FmtLen = 22; // 22 sets of 4 bits
+
+ //skip last 4bit parity row for simplicity
+ *size = removeParity(BitStream, *startIdx + sizeof(preamble), 5, 0, FmtLen * 5);
+ if (*size == 40) { // std em410x format
+ *hi = 0;
+ *lo = ((uint64_t)(bytebits_to_byte(BitStream, 8)) << 32) | (bytebits_to_byte(BitStream + 8, 32));
+ } else if (*size == 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));
+ } else {
+ if (g_debugMode) prnt("Error removing parity: %u", *size);
+ return 0;
+ }
+ return 1;
+}
+
+// Ask/Biphase Demod then try to locate an ISO 11784/85 ID
+// BitStream must contain previously askrawdemod and biphasedemoded data
+int FDXBdemodBI(uint8_t *dest, size_t *size) {
+ //make sure buffer has enough data
+ if (*size < 128) return -1;
+
+ size_t startIdx = 0;
+ uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,0,1};
+
+ uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
+ if (errChk == 0) return -2; //preamble not found
+ if (*size != 128) return -3; //wrong size for fdxb
+ //return start position
+ return (int)startIdx;
+}
+
+// by marshmellow
+// demod gProxIIDemod
+// error returns as -x
+// success returns start position in BitStream
+// BitStream must contain previously askrawdemod and biphasedemoded data
+int gProxII_Demod(uint8_t BitStream[], size_t *size) {
+ size_t startIdx=0;
+ uint8_t preamble[] = {1,1,1,1,1,0};
+
+ uint8_t errChk = preambleSearch(BitStream, preamble, sizeof(preamble), size, &startIdx);
+ if (errChk == 0) return -3; //preamble not found
+ if (*size != 96) return -2; //should have found 96 bits
+ //check first 6 spacer bits to verify format
+ if (!BitStream[startIdx+5] && !BitStream[startIdx+10] && !BitStream[startIdx+15] && !BitStream[startIdx+20] && !BitStream[startIdx+25] && !BitStream[startIdx+30]){
+ //confirmed proper separator bits found
+ //return start position
+ return (int) startIdx;
+ }
+ return -5; //spacer bits not found - not a valid gproxII
+}
+
+// loop to get raw HID waveform then FSK demodulate the TAG ID from it
+int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo, int *waveStartIdx) {
+ size_t numStart=0, size2=*size, startIdx=0;
+ // FSK demodulator fsk2a so invert and fc/10/8
+ *size = fskdemod(dest, size2, 50, 1, 10, 8, waveStartIdx);
+ if (*size < 96*2) return -2;
+ // 00011101 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
+ uint8_t preamble[] = {0,0,0,1,1,1,0,1};
+ // find bitstring in array
+ uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
+ if (errChk == 0) return -3; //preamble not found
+
+ numStart = startIdx + sizeof(preamble);
+ // final loop, go over previously decoded FSK data and manchester decode into usable tag ID
+ for (size_t idx = numStart; (idx-numStart) < *size - sizeof(preamble); idx+=2){
+ if (dest[idx] == dest[idx+1]){
+ return -4; //not manchester data
+ }
+ *hi2 = (*hi2<<1)|(*hi>>31);
+ *hi = (*hi<<1)|(*lo>>31);
+ //Then, shift in a 0 or one into low
+ if (dest[idx] && !dest[idx+1]) // 1 0
+ *lo=(*lo<<1)|1;
+ else // 0 1
+ *lo=(*lo<<1)|0;
+ }
+ return (int)startIdx;
+}
+
+int IOdemodFSK(uint8_t *dest, size_t size, int *waveStartIdx) {
+ //make sure buffer has data
+ if (size < 66*64) return -2;
+ // FSK demodulator RF/64, fsk2a so invert, and fc/10/8
+ size = fskdemod(dest, size, 64, 1, 10, 8, waveStartIdx);
+ if (size < 65) return -3; //did we get a good demod?
+ //Index map
+ //0 10 20 30 40 50 60
+ //| | | | | | |
+ //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
+ //-----------------------------------------------------------------------------
+ //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
+ //
+ //XSF(version)facility:codeone+codetwo
+ //Handle the data
+ size_t startIdx = 0;
+ uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,1};
+ uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), &size, &startIdx);
+ if (errChk == 0) return -4; //preamble not found
+
+ if (!dest[startIdx+8] && dest[startIdx+17]==1 && dest[startIdx+26]==1 && dest[startIdx+35]==1 && dest[startIdx+44]==1 && dest[startIdx+53]==1){
+ //confirmed proper separator bits found
+ //return start position
+ return (int) startIdx;
+ }
+ return -5;
+}
+
+// redesigned by marshmellow adjusted from existing decode functions
+// indala id decoding
+int indala64decode(uint8_t *bitStream, size_t *size, uint8_t *invert) {
+ //standard 64 bit indala formats including 26 bit 40134 format
+ uint8_t preamble64[] = {1,0,1,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 1};
+ uint8_t preamble64_i[] = {0,1,0,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 0};
+ size_t startidx = 0;
+ size_t found_size = *size;
+ bool found = preambleSearch(bitStream, preamble64, sizeof(preamble64), &found_size, &startidx);
+ if (!found) {
+ found = preambleSearch(bitStream, preamble64_i, sizeof(preamble64_i), &found_size, &startidx);
+ if (!found) return -1;
+ *invert ^= 1;
+ }
+ if (found_size != 64) return -2;
+ if (*invert==1)
+ for (size_t i = startidx; i < found_size + startidx; i++)
+ bitStream[i] ^= 1;
+
+ // note: don't change *size until we are sure we got it...
+ *size = found_size;
+ return (int) startidx;
+}
+
+int indala224decode(uint8_t *bitStream, size_t *size, uint8_t *invert) {
+ //large 224 bit indala formats (different preamble too...)
+ uint8_t preamble224[] = {1,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1};
+ uint8_t preamble224_i[] = {0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,0};
+ size_t startidx = 0;
+ size_t found_size = *size;
+ bool found = preambleSearch(bitStream, preamble224, sizeof(preamble224), &found_size, &startidx);
+ if (!found) {
+ found = preambleSearch(bitStream, preamble224_i, sizeof(preamble224_i), &found_size, &startidx);
+ if (!found) return -1;
+ *invert ^= 1;
+ }
+ if (found_size != 224) return -2;
+ if (*invert==1 && startidx > 0)
+ for (size_t i = startidx-1; i < found_size + startidx + 2; i++)
+ bitStream[i] ^= 1;
+
+ // 224 formats are typically PSK2 (afaik 2017 Marshmellow)
+ // note loses 1 bit at beginning of transformation...
+ // don't need to verify array is big enough as to get here there has to be a full preamble after all of our data
+ psk1TOpsk2(bitStream + (startidx-1), found_size+2);
+ startidx++;
+
+ *size = found_size;
+ return (int) startidx;
+}
+
+// loop to get raw paradox waveform then FSK demodulate the TAG ID from it
+int ParadoxdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo, int *waveStartIdx) {
+ size_t numStart=0, size2=*size, startIdx=0;
+ // FSK demodulator
+ *size = fskdemod(dest, size2,50,1,10,8,waveStartIdx); //fsk2a
+ if (*size < 96) return -2;
+
+ // 00001111 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
+ uint8_t preamble[] = {0,0,0,0,1,1,1,1};
+
+ uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
+ if (errChk == 0) return -3; //preamble not found
+
+ numStart = startIdx + sizeof(preamble);
+ // final loop, go over previously decoded FSK data and manchester decode into usable tag ID
+ for (size_t idx = numStart; (idx-numStart) < *size - sizeof(preamble); idx+=2){
+ if (dest[idx] == dest[idx+1])
+ return -4; //not manchester data
+ *hi2 = (*hi2<<1)|(*hi>>31);
+ *hi = (*hi<<1)|(*lo>>31);
+ //Then, shift in a 0 or one into low
+ if (dest[idx] && !dest[idx+1]) // 1 0
+ *lo=(*lo<<1)|1;
+ else // 0 1
+ *lo=(*lo<<1)|0;
+ }
+ return (int)startIdx;
+}
+
+// find presco preamble 0x10D in already demoded data
+int PrescoDemod(uint8_t *dest, size_t *size) {
+ //make sure buffer has data
+ if (*size < 64*2) return -2;
+
+ size_t startIdx = 0;
+ uint8_t preamble[] = {1,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0};
+ uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
+ if (errChk == 0) return -4; //preamble not found
+ //return start position
+ return (int) startIdx;
+}
+
+// by marshmellow
+// FSK Demod then try to locate a Farpointe Data (pyramid) ID
+int PyramiddemodFSK(uint8_t *dest, size_t *size, int *waveStartIdx) {
+ //make sure buffer has data
+ if (*size < 128*50) return -5;
+
+ // FSK demodulator
+ *size = fskdemod(dest, *size, 50, 1, 10, 8, waveStartIdx); // fsk2a RF/50
+ if (*size < 128) return -2; //did we get a good demod?
+
+ uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
+ size_t startIdx = 0;
+ uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
+ if (errChk == 0) return -4; //preamble not found
+ if (*size != 128) return -3;
+ return (int)startIdx;
+}
+
+// by marshmellow
+// find viking preamble 0xF200 in already demoded data
+int VikingDemod_AM(uint8_t *dest, size_t *size) {
+ //make sure buffer has data
+ if (*size < 64*2) return -2;
+
+ size_t startIdx = 0;
+ uint8_t preamble[] = {1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+ uint8_t errChk = preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx);
+ if (errChk == 0) return -4; //preamble not found
+ uint32_t checkCalc = bytebits_to_byte(dest+startIdx,8) ^ bytebits_to_byte(dest+startIdx+8,8) ^ bytebits_to_byte(dest+startIdx+16,8)
+ ^ bytebits_to_byte(dest+startIdx+24,8) ^ bytebits_to_byte(dest+startIdx+32,8) ^ bytebits_to_byte(dest+startIdx+40,8)
+ ^ bytebits_to_byte(dest+startIdx+48,8) ^ bytebits_to_byte(dest+startIdx+56,8);
+ if ( checkCalc != 0xA8 ) return -5;
+ if (*size != 64) return -6;
+ //return start position
+ return (int) startIdx;
+}
+
+// by iceman
+// find Visa2000 preamble in already demoded data
+int Visa2kDemod_AM(uint8_t *dest, size_t *size) {
+ if (*size < 96) return -1; //make sure buffer has data
+ size_t startIdx = 0;
+ uint8_t preamble[] = {0,1,0,1,0,1,1,0,0,1,0,0,1,0,0,1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,0};
+ if (preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx) == 0)
+ return -2; //preamble not found
+ if (*size != 96) return -3; //wrong demoded size
+ //return start position
+ return (int)startIdx;
+}