- // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
- // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
- // to stop output at the final bitidx2 value, not bitidx
- for (i = 0; i < bitidx; i += 2) {
- if ((bitStream[i] == 0) && (bitStream[i+1] == 1)) {
- bitStream[bit2idx++] = 1 ^ invert;
- }
- else if ((bitStream[i] == 1) && (bitStream[i+1] == 0)) {
- bitStream[bit2idx++] = 0 ^ invert;
- }
- else {
- // We cannot end up in this state, this means we are unsynchronized,
- // move up 1 bit:
- i++;
- warnings++;
- PrintAndLog("Unsynchronized, resync...");
- if (warnings > 10) {
- PrintAndLog("Error: too many decode errors, aborting.");
- return 0;
- }
- }
- }
-
- // PrintAndLog(" Manchester decoded bitstream : %d bits", (bit2idx-16));
- // uint8_t mod = (bit2idx-16) % blocksize;
- // uint8_t div = (bit2idx-16) / blocksize;
-
- // // Now output the bitstream to the scrollback by line of 16 bits
- // for (i = 0; i < div*blocksize; i+=blocksize) {
- // PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) );
- // }
- // if ( mod > 0 ){
- // PrintAndLog(" %s", sprint_bin(bitStream+i, mod) );
- // }
-
- if ( bit2idx > 0 )
- memcpy(dataout, bitStream, bit2idx);
-
- free(bitStream);
- return bit2idx;