+void printBitStream(uint8_t bits[], uint32_t bitLen){
+
+ uint32_t i = 0;
+ if (bitLen < 16) {
+ PrintAndLog("Too few bits found: %d",bitLen);
+ return;
+ }
+ if (bitLen > 512)
+ bitLen = 512;
+
+ if ( ( bitLen % 16 ) > 0) {
+ bitLen = ((bitLen / 16) * 16);
+ PrintAndLog("ICE: equally divided with 16 = %d",bitLen);
+ }
+
+ for (i = 0; i <= ( bitLen - 16); i += 16) {
+ PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
+ bits[i],
+ bits[i+1],
+ bits[i+2],
+ bits[i+3],
+ bits[i+4],
+ bits[i+5],
+ bits[i+6],
+ bits[i+7],
+ bits[i+8],
+ bits[i+9],
+ bits[i+10],
+ bits[i+11],
+ bits[i+12],
+ bits[i+13],
+ bits[i+14],
+ bits[i+15]);
+ }
+ return;
+}
+
+void printEM410x(uint64_t id) {
+
+ if ( id <= 0 ) return;
+
+ uint64_t id2lo = 0;
+ uint32_t i,j;
+ i = j = 0;
+
+ for (j = 5; j > 0; j--){
+ for (i = 0; i < 8; i++){
+ id2lo = ( id2lo << 1LL)|((id & ( 1 << ( i +( ( j-1 ) * 8 )))) >> ( i + (( j-1) *8 )));
+ }
+ }
+ //output em id
+ PrintAndLog("EM TAG ID : %010llx", id);
+ PrintAndLog("Unique TAG ID: %010llx", id2lo);
+ PrintAndLog("DEZ 8 : %08lld", id & 0xFFFFFF);
+ PrintAndLog("DEZ 10 : %010lld", id & 0xFFFFFF);
+ PrintAndLog("DEZ 5.5 : %05lld.%05lld", (id>>16LL) & 0xFFFF, (id & 0xFFFF));
+ PrintAndLog("DEZ 3.5A : %03lld.%05lld", (id>>32ll), (id & 0xFFFF));
+ PrintAndLog("DEZ 14/IK2 : %014lld", id);
+ PrintAndLog("DEZ 15/IK3 : %015lld", id2lo);
+ PrintAndLog("Other : %05lld_%03lld_%08lld", (id & 0xFFFF), (( id >> 16LL) & 0xFF), (id & 0xFFFFFF));
+}
+
+int CmdEm410xDecode(const char *Cmd)
+{
+ uint64_t id = 0;
+ uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00};
+ uint32_t len = GetFromGraphBuf(bits);
+ id = Em410xDecode(bits, len);
+ printEM410x(id);
+ if ( id > 0 )
+ return 1;
+ return 0;
+}
+
+//by marshmellow
+//takes 2 arguments - clock and invert both as integers
+//attempts to demodulate ask while decoding manchester
+//prints binary found and saves in graphbuffer for further commands
+int Cmdaskmandemod(const char *Cmd)
+{
+ int invert = 0;
+ int clk = 0;
+
+ sscanf(Cmd, "%i %i", &clk, &invert);
+
+ if (invert != 0 && invert != 1) {
+ PrintAndLog("Invalid argument: %s", Cmd);
+ return 0;
+ }
+
+ uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00};
+ uint32_t len = GetFromGraphBuf(bits);
+
+ int errCnt = askmandemod(bits, &len, &clk, &invert);
+
+ if (errCnt < 0) return 0;
+ if (len < 16) return 0;
+
+ PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk,invert,len);
+
+ if (errCnt > 0){
+ PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+ }
+
+ PrintAndLog("ASK/Manchester decoded bitstream:");
+
+ printBitStream(bits, len);
+
+ uint64_t tagid = Em410xDecode(bits, len);
+
+ if (tagid > 0){
+ SetGraphBuf(bits, len);
+ printEM410x(tagid);
+ return 1;
+ }
+ return 0;
+}
+
+//by marshmellow
+//manchester decode
+//stricktly take 10 and 01 and convert to 0 and 1
+int Cmdmandecoderaw(const char *Cmd)
+{
+ int i = 0;
+ int errCnt = 0;
+ int bitnum = 0;
+ uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00};
+ int high = 0, low = 0;
+
+ for (; i < GraphTraceLen; ++i){
+ if (GraphBuffer[i] > high) high = GraphBuffer[i];
+ else if (GraphBuffer[i] < low) low = GraphBuffer[i];
+ bits[i] = GraphBuffer[i];
+ }
+
+ if (high > 1 || low < 0 ){
+ PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode");
+ return 0;
+ }
+
+ bitnum = i;
+ errCnt = manrawdecode(bits, &bitnum);
+
+ if (errCnt>=20){
+ PrintAndLog("Too many errors: %d",errCnt);
+ return 0;
+ }
+
+ PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt);
+ printBitStream(bits,bitnum);
+
+ if (errCnt==0){
+ //put back in graphbuffer
+ SetGraphBuf(bits, bitnum);
+
+ uint64_t id = Em410xDecode(bits,i);
+ printEM410x(id);
+ }
+ return 1;
+}
+
+//by marshmellow
+//biphase decode
+//take 01 or 10 = 0 and 11 or 00 = 1
+//takes 1 argument "offset" default = 0 if 1 it will shift the decode by one bit
+// since it is not like manchester and doesn't have an incorrect bit pattern we
+// cannot determine if our decode is correct or if it should be shifted by one bit
+// the argument offset allows us to manually shift if the output is incorrect
+// (better would be to demod and decode at the same time so we can distinguish large
+// width waves vs small width waves to help the decode positioning) or askbiphdemod
+int CmdBiphaseDecodeRaw(const char *Cmd)
+{
+ int i = 0;
+ int errCnt = 0;
+ int bitnum = 0;
+ int offset = 0;
+ int high = 0, low = 0;
+ sscanf(Cmd, "%i", &offset);
+
+ uint8_t bits[MAX_GRAPH_TRACE_LEN]={0};
+
+ //get graphbuffer & high and low
+ for (; i<GraphTraceLen; ++i){
+ if (GraphBuffer[i] > high) high = GraphBuffer[i];
+ else if (GraphBuffer[i] < low) low = GraphBuffer[i];
+ bits[i] = GraphBuffer[i];
+ }
+ if (high > 1 || low < 0){
+ PrintAndLog("Error: please raw demod the wave first then decode");
+ return 0;
+ }
+ bitnum = i;
+ errCnt = BiphaseRawDecode(bits, &bitnum, offset);
+ if (errCnt >= 20){
+ PrintAndLog("Too many errors attempting to decode: %d", errCnt);
+ return 0;
+ }
+ PrintAndLog("Biphase Decoded using offset: %d - # errors:%d - data:", offset, errCnt);
+ printBitStream(bits, bitnum);
+ PrintAndLog("\nif bitstream does not look right try offset=1");
+ return 1;
+}
+
+
+//by marshmellow
+//takes 2 arguments - clock and invert both as integers
+//attempts to demodulate ask only
+//prints binary found and saves in graphbuffer for further commands
+int Cmdaskrawdemod(const char *Cmd)
+{
+ int invert = 0;
+ int clk = 0;
+
+ sscanf(Cmd, "%i %i", &clk, &invert);
+
+ if (invert != 0 && invert != 1 ) {
+ PrintAndLog("Invalid argument: %s", Cmd);
+ return 0;
+ }
+
+ if ( clock < 0 ) {
+ PrintAndLog("Wrong clock argument");
+ return 0;
+ }
+
+ uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00};
+ int len = GetFromGraphBuf(bits);
+ int errCnt = 0;
+
+ errCnt = askrawdemod(bits, &len, &clk, &invert);
+
+ //throw away static - allow 1 and -1 (in case of threshold command first)
+ if (errCnt == -1) {
+ PrintAndLog("no data found");
+ return 0;
+ }
+
+ if (len < 16) return 0;
+
+ PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d",clk,invert,len);
+
+ //move BitStream back to GraphBuffer
+ SetGraphBuf(bits, len);
+
+ if (errCnt > 0){
+ PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+ }
+
+ PrintAndLog("ASK demoded bitstream:");
+
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printBitStream(bits,len);
+ return 1;
+}
+