From: iceman1001 <iceman@iuse.se>
Date: Wed, 22 Feb 2017 23:03:10 +0000 (+0100)
Subject: chg: @piwi's code cleanup and some more.
X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/commitdiff_plain/9c624f67b3c3041d171abf75d954ba9fa0ae57a4?ds=sidebyside;hp=b4762e271229268b0bddd879fbe62230e9a2f0e8

chg: @piwi's code cleanup and some more.
ref: https://github.com/Proxmark/proxmark3/commit/43534cbad22da2db2e1b59f9e08f0cadfe0d8d54
---

diff --git a/armsrc/appmain.c b/armsrc/appmain.c
index 3ba9dd68..eea3857b 100644
--- a/armsrc/appmain.c
+++ b/armsrc/appmain.c
@@ -1229,6 +1229,26 @@ void UsbPacketReceived(uint8_t *packet, int len)
         case CMD_EMV_DUMP_CARD:
             EMVdumpcard();
 			break;
+/*			
+       case CMD_EMV_READ_RECORD:
+            EMVReadRecord(c->arg[0], c->arg[1], NULL);
+            break; 
+        case CMD_EMV_TRANSACTION:
+            EMVTransaction(); 
+            break;
+        case CMD_EMV_CLONE:
+            EMVClone(c->arg[0], c->arg[1]);
+            break;
+       case CMD_EMV_SIM:
+            EMVSim();
+            break;
+        case CMD_EMV_TEST:
+            EMVTest();
+            break;
+        case CMD_EMV_FUZZ_RATS:
+            EMVFuzz_RATS(c->arg[0],c->d.asBytes);
+            break;
+			*/
 #endif
 #ifdef WITH_ICLASS
 		// Makes use of ISO14443a FPGA Firmware
@@ -1303,7 +1323,7 @@ void UsbPacketReceived(uint8_t *packet, int len)
 			// arg0 = startindex
 			// arg1 = length bytes to transfer
 			// arg2 = RFU
-			//Dbprintf("transfer to client parameters: %llu | %llu | %llu", c->arg[0], c->arg[1], c->arg[2]);
+			//Dbprintf("transfer to client parameters: %" PRIu64 " | %" PRIu64 " | %" PRIu64, c->arg[0], c->arg[1], c->arg[2]);
 			
 			for(size_t i = 0; i < c->arg[1]; i += USB_CMD_DATA_SIZE) {
 				len = MIN( (c->arg[1] - i), USB_CMD_DATA_SIZE);
diff --git a/armsrc/apps.h b/armsrc/apps.h
index efc8ae07..41f8543c 100644
--- a/armsrc/apps.h
+++ b/armsrc/apps.h
@@ -179,7 +179,7 @@ size_t enciphered_data_length (const desfiretag_t tag, const size_t nbytes, int
 void cmac_generate_subkeys (desfirekey_t key);
 void cmac (const desfirekey_t  key, uint8_t *ivect, const uint8_t *data, size_t len, uint8_t *cmac);
 
-/// iso15693.h
+// iso15693.h
 void RecordRawAdcSamplesIso15693(void);
 void AcquireRawAdcSamplesIso15693(void);
 void ReaderIso15693(uint32_t parameter);	// Simulate an ISO15693 reader - greg
@@ -188,7 +188,7 @@ void BruteforceIso15693Afi(uint32_t speed); // find an AFI of a tag - atrox
 void DirectTag15693Command(uint32_t datalen,uint32_t speed, uint32_t recv, uint8_t data[]); // send arbitrary commands from CLI - atrox 
 void SetDebugIso15693(uint32_t flag);
 
-/// iclass.h
+// iclass.h
 void RAMFUNC SnoopIClass(void);
 void SimulateIClass(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain);
 void ReaderIClass(uint8_t arg0);
@@ -226,6 +226,18 @@ void EMVgetUDOL(void);
 void EMVloadvalue(uint32_t tag, uint8_t* datain);
 void EMVdumpcard(void);
 
+/*
+//void EMVSelect(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *data);
+void EMVFuzz_RATS(uint8_t len, uint8_t* RATS);
+void EMVReadRecord(uint8_t arg0, uint8_t arg1,emvcard* inputcard);
+void EMVSelectPPSE();
+void EMVSelectAID(uint8_t *AID, uint8_t AIDlen, emvcard* inputcard);
+void EMVTransaction(); //perform an EMV transaction
+void EMVClone(uint8_t maxsfi, uint8_t maxrecord); //clone an EMV card.
+void EMVSim();
+void EMVTest(); //test function for emv stuff.
+*/
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/client/cmdcrc.c b/client/cmdcrc.c
index 4c1c74f5..249fdebc 100644
--- a/client/cmdcrc.c
+++ b/client/cmdcrc.c
@@ -39,13 +39,14 @@ int split(char *str, char *arr[MAX_ARGS]){
     int wordCnt = 0;
 
     while(1){
-        while(isspace(str[beginIndex])){
+        while(isspace(str[beginIndex])) {
             ++beginIndex;
         }
-        if(str[beginIndex] == '\0')
+        if(str[beginIndex] == '\0') {
             break;
+		}
         endIndex = beginIndex;
-        while (str[endIndex] && !isspace(str[endIndex])){
+        while (str[endIndex] && !isspace(str[endIndex])) {
             ++endIndex;
         }
         int len = endIndex - beginIndex;
@@ -151,7 +152,7 @@ int GetModels(char *Models[], int *count, uint8_t *width){
 		 * searched.
 		 */
 		/* scan against preset models */
-		if(~uflags & C_FORCE) {
+		if (~uflags & C_FORCE) {
 			pass = 0;
 			Cnt = 0;
 			do {
@@ -161,34 +162,36 @@ int GetModels(char *Models[], int *count, uint8_t *width){
 					mbynum(&pset, --psets);
 					
 					/* skip if different width, or refin or refout don't match */
-				if(plen(pset.spoly) != width[0] || (model.flags ^ pset.flags) & (P_REFIN | P_REFOUT))
+					if( plen(pset.spoly) != width[0] || (model.flags ^ pset.flags) & (P_REFIN | P_REFOUT))
 						continue;
 					/* skip if the preset doesn't match specified parameters */
-					if(rflags & R_HAVEP && pcmp(&model.spoly, &pset.spoly))
+					if (rflags & R_HAVEP && pcmp(&model.spoly, &pset.spoly))
 						continue;
-					if(rflags & R_HAVEI && psncmp(&model.init, &pset.init))
+					if (rflags & R_HAVEI && psncmp(&model.init, &pset.init))
 						continue;
-					if(rflags & R_HAVEX && psncmp(&model.xorout, &pset.xorout))
+					if (rflags & R_HAVEX && psncmp(&model.xorout, &pset.xorout))
 						continue;
 			
-				//for additional args (not used yet, maybe future?)
+					//for additional args (not used yet, maybe future?)
 					apoly = pclone(pset.xorout);
-					if(pset.flags & P_REFOUT)
+					
+					if (pset.flags & P_REFOUT)
 						prev(&apoly);
-				
-					for(qptr = apolys; qptr < pptr; ++qptr) {
+					
+					
+					for (qptr = apolys; qptr < pptr; ++qptr) {
 						crc = pcrc(*qptr, pset.spoly, pset.init, apoly, 0);
-						if(ptst(crc)) {
+						if (ptst(crc)) {
 							pfree(&crc);
 							break;
-						} else
-							pfree(&crc);
+						}
+						pfree(&crc);
 					}
 					pfree(&apoly);
-					if(qptr == pptr) {
+					
+					if (qptr == pptr) {
 
 						/* the selected model solved all arguments */
-
 						mcanon(&pset);
 						
 						size_t size = (pset.name && *pset.name) ? strlen(pset.name) : 6;
@@ -198,7 +201,7 @@ int GetModels(char *Models[], int *count, uint8_t *width){
 							PrintAndLog("out of memory?");
 							return 0;
 						}
-					width[Cnt] = width[0];
+						width[Cnt] = width[0];
 						memcpy(tmp, pset.name, size);
 						Models[Cnt++] = tmp;
 						*count = Cnt;
@@ -208,44 +211,50 @@ int GetModels(char *Models[], int *count, uint8_t *width){
 				mfree(&pset);
 
 				/* toggle refIn/refOut and reflect arguments */
-				if(~rflags & R_HAVERI) {
+				if (~rflags & R_HAVERI) {
 					model.flags ^= P_REFIN | P_REFOUT;
-					for(qptr = apolys; qptr < pptr; ++qptr)
+					for (qptr = apolys; qptr < pptr; ++qptr) {
 						prevch(qptr, ibperhx);
+					}
 				}
-			} while(~rflags & R_HAVERI && ++pass < 2);
+			} while (~rflags & R_HAVERI && ++pass < 2);
 		}
 		//got everything now free the memory...
 
-		if(uflags & C_RESULT) {
-			for(qptr = apolys; qptr < pptr; ++qptr)
+		if (uflags & C_RESULT) {
+			for (qptr = apolys; qptr < pptr; ++qptr) {
 				pfree(qptr);
+			}
 		}
-		if(!(model.flags & P_REFIN) != !(model.flags & P_REFOUT))
+		if (!(model.flags & P_REFIN) != !(model.flags & P_REFOUT))
 			return uerr("cannot search for crossed-endian models");
 
 		pass = 0;
 		do {
 			mptr = candmods = reveng(&model, qpoly, rflags, args, apolys);
-			if(mptr && plen(mptr->spoly))
+			if (mptr && plen(mptr->spoly)) {
 				uflags |= C_RESULT;
-			while(mptr && plen(mptr->spoly)) {
+			}
+			while (mptr && plen(mptr->spoly)) {
 				mfree(mptr++);
 			}
 			free(candmods);
-			if(~rflags & R_HAVERI) {
+			if (~rflags & R_HAVERI) {
 				model.flags ^= P_REFIN | P_REFOUT;
-				for(qptr = apolys; qptr < pptr; ++qptr)
+				for (qptr = apolys; qptr < pptr; ++qptr) {
 					prevch(qptr, ibperhx);
+				}
 			}
-		} while(~rflags & R_HAVERI && ++pass < 2);
-		for(qptr = apolys; qptr < pptr; ++qptr)
+		} while (~rflags & R_HAVERI && ++pass < 2);
+		
+		for (qptr = apolys; qptr < pptr; ++qptr) {
 			pfree(qptr);
+		}
 		free(apolys);
-		if(~uflags & C_RESULT)
+		if (~uflags & C_RESULT)
 			return uerr("no models found");
+		
 		mfree(&model);
-
 	}
 	return 1;
 }
@@ -281,11 +290,11 @@ int RunModel(char *inModel, char *inHexStr, bool reverse, char endian, char *res
 
 	SETBMP();
 	//set model
-	if(!(c = mbynam(&model, inModel))) {
+	if (!(c = mbynam(&model, inModel))) {
 		PrintAndLog("error: preset model '%s' not found.  Use reveng -D to list presets.", inModel);
 		return 0;
 	}
-	if(c < 0)
+	if (c < 0)
 		return uerr("no preset models available");
 
 	rflags |= R_HAVEP | R_HAVEI | R_HAVERI | R_HAVERO | R_HAVEX;
@@ -340,7 +349,7 @@ int RunModel(char *inModel, char *inHexStr, bool reverse, char endian, char *res
 		 * Consequently Init is the mirror image of the
 		 * one resulting from -V, and so we have:
 		 */
-		if(~model.flags & P_REFOUT) {
+		if (~model.flags & P_REFOUT) {
 			prev(&model.init);
 			prev(&model.xorout);
 		}
@@ -355,17 +364,17 @@ int RunModel(char *inModel, char *inHexStr, bool reverse, char endian, char *res
 	/* in the Williams model, xorout is applied after the refout stage.
 	 * as refout is part of ptostr(), we reverse xorout here.
 	 */
-	if(model.flags & P_REFOUT)
+	if (model.flags & P_REFOUT)
 		prev(&model.xorout);
 
 	apoly = strtop(inHexStr, model.flags, ibperhx);
 
-	if(reverse)
+	if (reverse)
 		prev(&apoly);
 
 	crc = pcrc(apoly, model.spoly, model.init, model.xorout, model.flags);
 
-	if(reverse)
+	if (reverse)
 		prev(&crc);
 
 	string = ptostr(crc, model.flags, obperhx);
diff --git a/client/cmddata.c b/client/cmddata.c
index fe22aa4b..6a5753c7 100644
--- a/client/cmddata.c
+++ b/client/cmddata.c
@@ -376,21 +376,21 @@ void printEM410x(uint32_t hi, uint64_t id)
 		}
 		if (hi){
 			//output 88 bit em id
-			PrintAndLog("\nEM TAG ID      : %06X%016llX", hi, id);
+			PrintAndLog("\nEM TAG ID      : %06X%016" PRIX64, hi, id);
 		} else{
 			//output 40 bit em id
-			PrintAndLog("\nEM TAG ID      : %010llX", id);
-			PrintAndLog("Unique TAG ID  : %010llX",  id2lo);
+			PrintAndLog("\nEM TAG ID      : %010" PRIX64, id);
+			PrintAndLog("Unique TAG ID  : %010" PRIX64, id2lo);
 			PrintAndLog("\nPossible de-scramble patterns");
 			PrintAndLog("HoneyWell IdentKey {");
-			PrintAndLog("DEZ 8          : %08lld",id & 0xFFFFFF);
-			PrintAndLog("DEZ 10         : %010lld",id & 0xFFFFFFFF);
-			PrintAndLog("DEZ 5.5        : %05lld.%05lld",(id>>16LL) & 0xFFFF,(id & 0xFFFF));
-			PrintAndLog("DEZ 3.5A       : %03lld.%05lld",(id>>32ll),(id & 0xFFFF));
-			PrintAndLog("DEZ 3.5B       : %03lld.%05lld",(id & 0xFF000000) >> 24,(id & 0xFFFF));
-			PrintAndLog("DEZ 3.5C       : %03lld.%05lld",(id & 0xFF0000) >> 16,(id & 0xFFFF));
-			PrintAndLog("DEZ 14/IK2     : %014lld",id);
-			PrintAndLog("DEZ 15/IK3     : %015lld",id2lo);
+			PrintAndLog("DEZ 8          : %08" PRIu64, id & 0xFFFFFF);
+			PrintAndLog("DEZ 10         : %010" PRIu64, id & 0xFFFFFFFF);
+			PrintAndLog("DEZ 5.5        : %05" PRIu64 ".%05" PRIu64, (id>>16LL) & 0xFFFF, (id & 0xFFFF));
+			PrintAndLog("DEZ 3.5A       : %03" PRIu64 ".%05" PRIu64, (id>>32ll), (id & 0xFFFF));
+			PrintAndLog("DEZ 3.5B       : %03" PRIu64 ".%05" PRIu64, (id & 0xFF000000) >> 24, (id & 0xFFFF));
+			PrintAndLog("DEZ 3.5C       : %03" PRIu64 ".%05" PRIu64, (id & 0xFF0000) >> 16, (id & 0xFFFF));
+			PrintAndLog("DEZ 14/IK2     : %014" PRIu64, id);
+			PrintAndLog("DEZ 15/IK3     : %015" PRIu64, id2lo);
 			PrintAndLog("DEZ 20/ZK      : %02lld%02lld%02lld%02lld%02lld%02lld%02lld%02lld%02lld%02lld",
 			    (id2lo & 0xf000000000) >> 36,
 			    (id2lo & 0x0f00000000) >> 32,
@@ -404,8 +404,8 @@ void printEM410x(uint32_t hi, uint64_t id)
 			    (id2lo & 0x000000000f)
 			);
 			uint64_t paxton = (((id>>32) << 24) | (id & 0xffffff))  + 0x143e00;
-			PrintAndLog("}\nOther          : %05lld_%03lld_%08lld",(id&0xFFFF),((id>>16LL) & 0xFF),(id & 0xFFFFFF));  
-			PrintAndLog("Pattern Paxton : %lld [0x%llX]", paxton, paxton);
+			PrintAndLog("}\nOther          : %05" PRIu64 "_%03" PRIu64 "_%08" PRIu64, (id&0xFFFF), ((id>>16LL) & 0xFF), (id & 0xFFFFFF));  
+			PrintAndLog("Pattern Paxton : %" PRIu64 " [0x%" PRIX64 "]", paxton, paxton);
 
 			uint32_t p1id = (id & 0xFFFFFF);
 			uint8_t arr[32] = {0x00};
@@ -1735,8 +1735,8 @@ int CmdFDXBdemodBI(const char *Cmd){
 	if (g_debugMode) PrintAndLog("Raw ID Hex: %s", sprint_hex(raw,8));
 
 	uint16_t calcCrc = crc16_ccitt_kermit(raw, 8);
-	PrintAndLog("Animal ID:     %04u-%012llu", countryCode, NationalCode);
-	PrintAndLog("National Code: %012llu", NationalCode);
+	PrintAndLog("Animal ID:     %04u-%012" PRIu64, countryCode, NationalCode);
+	PrintAndLog("National Code: %012" PRIu64, NationalCode);
 	PrintAndLog("CountryCode:   %04u", countryCode);
 
 	PrintAndLog("Reserved/RFU:      %u", reservedCode);
diff --git a/client/cmdhfemv.c b/client/cmdhfemv.c
index 664c00c4..bf92d53f 100644
--- a/client/cmdhfemv.c
+++ b/client/cmdhfemv.c
@@ -214,7 +214,7 @@ int CmdHfEmvSim(const char *Cmd) {
 	if(errors) return usage_hf_emv_sim();	
 	
 	UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}};
-	sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]);
+	sscanf(Cmd, " %" SCNi64 " %" SCNi64 " %" SCNi64 , &c.arg[0], &c.arg[1], &c.arg[2]);
 	clearCommandBuffer();
 	SendCommand(&c);
 	return 0;
diff --git a/client/cmdhfepa.c b/client/cmdhfepa.c
index 179de05d..2f7ee7bc 100644
--- a/client/cmdhfepa.c
+++ b/client/cmdhfepa.c
@@ -36,7 +36,7 @@ int CmdHFEPACollectPACENonces(const char *Cmd)
 	m = m > 0 ? m : 1;
 	n = n > 0 ? n : 1;
 
-	PrintAndLog("Collecting %u %"hhu"-byte nonces", n, m);
+	PrintAndLog("Collecting %u %u byte nonces", n, m);
 	PrintAndLog("Start: %u", time(NULL));
 	// repeat n times
 	for (unsigned int i = 0; i < n; i++) {
diff --git a/client/cmdhficlass.c b/client/cmdhficlass.c
index 90856e91..3891d431 100644
--- a/client/cmdhficlass.c
+++ b/client/cmdhficlass.c
@@ -1388,25 +1388,6 @@ int CmdHFiClassReadTagFile(const char *Cmd) {
 	return 0;
 }
 
-/*
-uint64_t xorcheck(uint64_t sdiv,uint64_t hdiv) {
-	uint64_t new_div = 0x00;
-	new_div ^= sdiv;
-	new_div ^= hdiv;
-	return new_div;
-}
-
-uint64_t hexarray_to_uint64(uint8_t *key) {
-	char temp[17];
-	uint64_t uint_key;
-	for (int i = 0;i < 8;i++)
-		sprintf(&temp[(i *2)],"%02X",key[i]);
-	temp[16] = '\0';
-	if (sscanf(temp,"%016"llX,&uint_key) < 1)
-		return 0;
-	return uint_key;
-}
-*/
 void HFiClassCalcDivKey(uint8_t	*CSN, uint8_t	*KEY, uint8_t *div_key, bool elite){
 	uint8_t keytable[128] = {0};
 	uint8_t key_index[8] = {0};
diff --git a/client/cmdhflegic.c b/client/cmdhflegic.c
index 62c43f04..4414efa1 100644
--- a/client/cmdhflegic.c
+++ b/client/cmdhflegic.c
@@ -527,7 +527,7 @@ int CmdLegicRdmem(const char *Cmd) {
 // int phase, int frame, int reqresp
 int CmdLegicRfSim(const char *Cmd) {
 	UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}};
-	sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]);
+	sscanf(Cmd, " %" SCNi64 " %" SCNi64 " %" SCNi64 , &c.arg[0], &c.arg[1], &c.arg[2]);
 	clearCommandBuffer();
 	SendCommand(&c);
 	return 0;
diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c
index e6568f59..20bb5929 100644
--- a/client/cmdhfmf.c
+++ b/client/cmdhfmf.c
@@ -226,7 +226,7 @@ start:
 			c.arg[0] = false;
 			goto start;
 		} else {
-			PrintAndLog("Found valid key: %012"llx" \n", r_key);
+			PrintAndLog("Found valid key: %012" PRIx64 " \n", r_key);
 			goto END;
 		}
 	}
@@ -246,10 +246,10 @@ start:
 		uint64_t key64 = 0;
 		int res = mfCheckKeys(blockNo, keytype - 0x60 , false, 1, keyblock, &key64);
 		if ( res > 0 ) {
-			PrintAndLog("Candidate Key found (%012"llx") - Test authentication failed. [%d] Restarting darkside attack", r_key, res);	
+			PrintAndLog("Candidate Key found (%012" PRIx64 ") - Test authentication failed. [%d] Restarting darkside attack", r_key, res);	
 			goto start;
 		}
-		PrintAndLog("Found valid key: %012"llx" \n", r_key);
+		PrintAndLog("Found valid key: %012" PRIx64 " \n", r_key);
 	}
 END:
 	t1 = clock() - t1;
@@ -313,9 +313,7 @@ int CmdHF14AMfRdBl(const char *Cmd) {
 	uint8_t blockNo = 0;
 	uint8_t keyType = 0;
 	uint8_t key[6] = {0, 0, 0, 0, 0, 0};
-	
-	char cmdp	= 0x00;
-
+	char cmdp = 0x00;
 
 	if (strlen(Cmd)<3) {
 		PrintAndLog("Usage:  hf mf rdbl    <block number> <key A/B> <key (12 hex symbols)>");
@@ -1207,7 +1205,7 @@ int CmdHF14AMfChk(const char *Cmd) {
 					}
 					memset(keyBlock + 6 * keycnt, 0, 6);
 					num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);
-					PrintAndLog("check key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));
+					PrintAndLog("check key[%2d] %012" PRIx64, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));
 					keycnt++;
 					memset(buf, 0, sizeof(buf));
 				}
@@ -1400,7 +1398,7 @@ void readerAttack(nonces_t data, bool setEmulatorMem, bool verbose) {
 		uint8_t sector = data.sector;
 		uint8_t keytype = data.keytype;
 
-		PrintAndLog("Reader is trying authenticate with: Key %s, sector %02d: [%012"llx"]"
+		PrintAndLog("Reader is trying authenticate with: Key %s, sector %02d: [%012" PRIx64 "]"
 			, keytype ? "B" : "A"
 			, sector
 			, key
@@ -1707,7 +1705,7 @@ int CmdHF14AMfKeyBrute(const char *Cmd) {
 	time(&start);
 	
 	if (mfKeyBrute( blockNo, keytype, key, &foundkey))
-		PrintAndLog("Found valid key: %012"llx" \n", foundkey);
+		PrintAndLog("Found valid key: %012" PRIx64 " \n", foundkey);
 	else
 		PrintAndLog("Key not found");
 	
@@ -1725,7 +1723,7 @@ void printKeyTable( uint8_t sectorscnt, sector *e_sector ){
 	PrintAndLog("|sec|key A           |res|key B           |res|");
 	PrintAndLog("|---|----------------|---|----------------|---|");
 	for (uint8_t i = 0; i < sectorscnt; ++i) {
-		PrintAndLog("|%03d|  %012"llx"  | %d |  %012"llx"  | %d |", i,
+		PrintAndLog("|%03d|  %012" PRIx64 "  | %d |  %012" PRIx64 "  | %d |", i,
 			e_sector[i].Key[0], e_sector[i].foundKey[0], 
 			e_sector[i].Key[1], e_sector[i].foundKey[1]
 		);
@@ -1774,7 +1772,6 @@ int CmdHF14AMfESet(const char *Cmd)
 {
 	uint8_t memBlock[16];
 	uint8_t blockNo = 0;
-
 	memset(memBlock, 0x00, sizeof(memBlock));
 
 	if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {
@@ -2056,7 +2053,7 @@ int CmdHF14AMfEKeyPrn(const char *Cmd)
 		}
 		keyA = bytes_to_num(data, 6);
 		keyB = bytes_to_num(data + 10, 6);
-		PrintAndLog("|%03d|  %012"llx"  |  %012"llx"  |", i, keyA, keyB);
+		PrintAndLog("|%03d|  %012" PRIx64 "  |  %012" PRIx64 "  |", i, keyA, keyB);
 	}
 	PrintAndLog("|---|----------------|----------------|");
 	
diff --git a/client/cmdhfmfhard.c b/client/cmdhfmfhard.c
index 3fed7c95..2e504675 100644
--- a/client/cmdhfmfhard.c
+++ b/client/cmdhfmfhard.c
@@ -723,8 +723,8 @@ static void simulate_acquire_nonces()
 	cuid = (rand() & 0xff) << 24 | (rand() & 0xff) << 16 | (rand() & 0xff) << 8 | (rand() & 0xff);
 	known_target_key = ((uint64_t)rand() & 0xfff) << 36 | ((uint64_t)rand() & 0xfff) << 24 | ((uint64_t)rand() & 0xfff) << 12 | ((uint64_t)rand() & 0xfff);
 	
-	printf("Simulating nonce acquisition for target key %012"llx", cuid %08x ...\n", known_target_key, cuid);
-	fprintf(fstats, "%012"llx";%08x;", known_target_key, cuid);
+	printf("Simulating nonce acquisition for target key %012" PRIx64 ", cuid %08x ...\n", known_target_key, cuid);
+	fprintf(fstats, "%012" PRIx64 ";%08x;", known_target_key, cuid);
 	
 	do {
 		uint32_t nt_enc = 0;
@@ -1247,7 +1247,7 @@ static bool TestIfKeyExists(uint64_t key)
 
 	uint32_t state_odd = pcs->odd & 0x00ffffff;
 	uint32_t state_even = pcs->even & 0x00ffffff;
-	//printf("Tests: searching for key %llx after first byte 0x%02x (state_odd = 0x%06x, state_even = 0x%06x) ...\n", key, best_first_bytes[0], state_odd, state_even);
+	//printf("Tests: searching for key %" PRIx64 " after first byte 0x%02x (state_odd = 0x%06x, state_even = 0x%06x) ...\n", key, best_first_bytes[0], state_odd, state_even);
 	printf("Validating key search space\n");
 	uint64_t count = 0;
 	for (statelist_t *p = candidates; p != NULL; p = p->next) {
@@ -1271,7 +1271,7 @@ static bool TestIfKeyExists(uint64_t key)
 		count += (p_odd - p->states[ODD_STATE]) * (p_even - p->states[EVEN_STATE]);
 		if (found_odd && found_even) {
 			if (known_target_key != -1) {
-				PrintAndLog("Key Found after testing %llu (2^%1.1f) out of %lld (2^%1.1f) keys.", 
+				PrintAndLog("Key Found after testing %" PRIu64 " (2^%1.1f) out of %lld (2^%1.1f) keys.", 
 					count,
 					log(count)/log(2), 
 					maximum_states,
diff --git a/client/cmdlfem4x.c b/client/cmdlfem4x.c
index 7cc62987..e6db8026 100644
--- a/client/cmdlfem4x.c
+++ b/client/cmdlfem4x.c
@@ -147,7 +147,8 @@ int CmdEM410xWatch(const char *Cmd)
 		}
 		
 		CmdLFRead("s");
-		getSamples("8201",true); //capture enough to get 2 complete preambles (4096*2+9)	
+		//getSamples("8201",true); //capture enough to get 2 complete preambles (4096*2+9)	
+		getSamples("6144",true);
 	} while (!CmdEM410xRead(""));
 
 	return 0;
@@ -159,7 +160,7 @@ int CmdEM410xWatchnSpoof(const char *Cmd)
 {
 	// loops if the captured ID was in XL-format.
 	CmdEM410xWatch(Cmd);
-	PrintAndLog("# Replaying captured ID: %llu", g_em410xid);
+	PrintAndLog("# Replaying captured ID: %" PRIu64 , g_em410xid);
 	CmdLFaskSim("");
 	return 0;
 }
@@ -593,8 +594,7 @@ int CmdEM4x50Dump(const char *Cmd){
 }
 
 #define EM_PREAMBLE_LEN 6
-// download samples from device
-// and copy them to Graphbuffer
+// download samples from device and copy to Graphbuffer
 bool downloadSamplesEM(){
 	
 	// 8 bit preamble + 32 bit word response (max clock (128) * 40bits = 5120 samples)
@@ -607,7 +607,8 @@ bool downloadSamplesEM(){
 	setGraphBuf(got, sizeof(got));
 	return TRUE;
 }
-//search for given preamble in given BitStream and return success=1 or fail=0 and startIndex
+
+// em_demod 
 bool doPreambleSearch(size_t *startIdx){
 	
 	// sanity check
@@ -615,26 +616,14 @@ bool doPreambleSearch(size_t *startIdx){
 		if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305 demodbuffer too small");
 		return FALSE;
 	}
-	
-	// skip first two 0 bits as they might have been missed in the demod 
-	uint8_t preamble[EM_PREAMBLE_LEN] = {0,0,1,0,1,0};
-	
-	// set size to 15 to only test first 4 positions for the preamble
-	size_t size = (15 > DemodBufferLen) ? DemodBufferLen : 15;
+
+	// set size to 20 to only test first 14 positions for the preamble
+	size_t size = (20 > DemodBufferLen) ? DemodBufferLen : 20;
 	*startIdx = 0; 
-	uint8_t found = 0;
-	
-	// em only sends preamble once, so look for it once in the first x bits
-	for (int idx = 0; idx < size - EM_PREAMBLE_LEN; idx++){
-		if (memcmp(DemodBuffer+idx, preamble, EM_PREAMBLE_LEN) == 0){
-			//first index found
-			*startIdx = idx;
-			found = 1;
-			break;
-		}
-	}
+	// skip first two 0 bits as they might have been missed in the demod
+	uint8_t preamble[EM_PREAMBLE_LEN] = {0,0,1,0,1,0};
 	
-	if ( !found) {
+	if ( !preambleSearchEx(DemodBuffer, preamble, EM_PREAMBLE_LEN, &size, startIdx, TRUE)) {
 		if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305 preamble not found :: %d", *startIdx);
 		return FALSE;
 	} 
@@ -714,12 +703,10 @@ bool setDemodBufferEM(uint32_t *word, size_t idx){
 		return FALSE;
 	}
 	
-	size_t size = removeParity(DemodBuffer, idx + EM_PREAMBLE_LEN, 9, 0, 44);
-	if (!size) {
-		if (g_debugMode) PrintAndLog("DEBUG: Error - EM Parity not detected");
+	if (!removeParity(DemodBuffer, idx + EM_PREAMBLE_LEN, 9, 0, 44)) {
+		if (g_debugMode) PrintAndLog("DEBUG: Error - EM, failed removing parity");
 		return FALSE;
 	}
-	// set & copy to output
 	setDemodBuf(DemodBuffer, 40, 0);
 	*word = bytebits_to_byteLSBF(DemodBuffer, 32);
 	return TRUE;
@@ -910,8 +897,7 @@ int CmdEM4x05Write(const char *Cmd) {
 	if (!downloadSamplesEM())
 		return -1;
 
-	//todo: check response for 00001010 then write data for write confirmation!
-	
+
 	//attempt demod:
 	//need 0 bits demoded (after preamble) to verify write cmd
 	uint32_t dummy = 0;
diff --git a/client/cmdlffdx.c b/client/cmdlffdx.c
index 292ebad6..17f50ab1 100644
--- a/client/cmdlffdx.c
+++ b/client/cmdlffdx.c
@@ -175,8 +175,8 @@ int CmdFdxDemod(const char *Cmd) {
 	uint16_t calcCrc = crc16_ccitt_kermit(raw, 8);
 	
 	PrintAndLog("\nFDX-B / ISO 11784/5 Animal Tag ID Found:  Raw : %s", sprint_hex(raw, 8));
-	PrintAndLog("Animal ID          %04u-%012llu", countryCode, NationalCode);
-	PrintAndLog("National Code      %012llu (0x%llX)", NationalCode, NationalCode);
+	PrintAndLog("Animal ID          %04u-%012" PRIu64, countryCode, NationalCode);
+	PrintAndLog("National Code      %012" PRIu64 " (0x%" PRIx64 ")", NationalCode, NationalCode);
 	PrintAndLog("Country Code       %04u", countryCode);
 	PrintAndLog("Reserved/RFU       %u (0x04%X)", reservedCode,  reservedCode);
 	PrintAndLog("");
diff --git a/client/cmdlfhid.c b/client/cmdlfhid.c
index 3eb96422..6ed6c4fd 100644
--- a/client/cmdlfhid.c
+++ b/client/cmdlfhid.c
@@ -341,7 +341,7 @@ int CmdHIDWiegand(const char *Cmd) {
 		uint8_t shifts = 64-fmtlen[i];		
 		wiegand >>= shifts;
 		
-		PrintAndLog(" %u | %03u | %03u  | %llu  | %llX  |  %llX",
+		PrintAndLog(" %u | %03u | %03u  | %" PRIu64 "  | %" PRIX64 "  |  %" PRIX64 ,
 			fmtlen[i],
 			oem,
 			fc,
diff --git a/client/cmdlfti.c b/client/cmdlfti.c
index 2e5cfbba..65ffc12f 100644
--- a/client/cmdlfti.c
+++ b/client/cmdlfti.c
@@ -280,7 +280,7 @@ int CmdTIWrite(const char *Cmd)
 {
 	int res = 0;
 	UsbCommand c = {CMD_WRITE_TI_TYPE};
-	res = sscanf(Cmd, "%012"llx" %012"llx" %012"llx"", &c.arg[0], &c.arg[1], &c.arg[2]);
+	res = sscanf(Cmd, "%012" SCNx64 " %012" SCNx64 " %012" SCNx64, &c.arg[0], &c.arg[1], &c.arg[2]);
 
 	if (res == 2) c.arg[2]=0;
 	if (res < 2) {
diff --git a/client/cmdmain.c b/client/cmdmain.c
index dc44a55c..84d6b1b9 100644
--- a/client/cmdmain.c
+++ b/client/cmdmain.c
@@ -209,7 +209,7 @@ void UsbCommandReceived(UsbCommand *UC)
 			// arg1 = length bytes to transfer
 			// arg2 = bigbuff tracelength (?)
 			memcpy( sample_buf + (UC->arg[0]), UC->d.asBytes, UC->arg[1]);
-			//printf("DBG:: Download from device. chunk %llu | size %llu | tracelen:%llu \n", UC->arg[0], UC->arg[1], UC->arg[2]);
+			//printf("DBG:: Download from device. chunk %" PRIu64 " | size %" PRIu64 " | tracelen:%" PRIu64 " \n", UC->arg[0], UC->arg[1], UC->arg[2]);
 			break;
 		}
 		default: {
diff --git a/client/flash.c b/client/flash.c
index c26dd307..37ee8fb0 100644
--- a/client/flash.c
+++ b/client/flash.c
@@ -302,7 +302,7 @@ static int get_proxmark_state(uint32_t *state)
 			*state = resp.arg[0];
 			break;
 		default:
-			fprintf(stderr, "Error: Couldn't get proxmark state, bad response type: 0x%04"llx"\n", resp.cmd);
+			fprintf(stderr, "Error: Couldn't get proxmark state, bad response type: 0x%04" PRIx64 "\n", resp.cmd);
 			return -1;
 			break;
 	}
@@ -360,7 +360,7 @@ static int wait_for_ack(UsbCommand *ack)
 {
 	ReceiveCommand(ack);
 	if (ack->cmd != CMD_ACK) {
-		printf("Error: Unexpected reply 0x%04"llx" %s (expected ACK)\n", ack->cmd, (ack->cmd==CMD_NACK)?"NACK":"");
+		printf("Error: Unexpected reply 0x%04" PRIx64 " %s (expected ACK)\n", ack->cmd, (ack->cmd==CMD_NACK)?"NACK":"");
 		return -1;
 	}
 	return 0;
diff --git a/client/flasher.c b/client/flasher.c
index 4e431658..fe3de3fe 100644
--- a/client/flasher.c
+++ b/client/flasher.c
@@ -25,10 +25,10 @@ static char* serial_port_name;
 void cmd_debug(UsbCommand* UC) {
 	//  Debug
 	printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
-	printf("  cmd[len=%zd]: %016"llx"\n",sizeof(UC->cmd),UC->cmd);
-	printf(" arg0[len=%zd]: %016"llx"\n",sizeof(UC->arg[0]),UC->arg[0]);
-	printf(" arg1[len=%zd]: %016"llx"\n",sizeof(UC->arg[1]),UC->arg[1]);
-	printf(" arg2[len=%zd]: %016"llx"\n",sizeof(UC->arg[2]),UC->arg[2]);
+	printf("  cmd[len=%zd]: %016" PRIx64"\n",sizeof(UC->cmd),UC->cmd);
+	printf(" arg0[len=%zd]: %016" PRIx64"\n",sizeof(UC->arg[0]),UC->arg[0]);
+	printf(" arg1[len=%zd]: %016" PRIx64"\n",sizeof(UC->arg[1]),UC->arg[1]);
+	printf(" arg2[len=%zd]: %016" PRIx64"\n",sizeof(UC->arg[2]),UC->arg[2]);
 	printf(" data[len=%zd]: ",sizeof(UC->d.asBytes));
 
 	for (size_t i=0; i<16; i++)
diff --git a/client/fpga_compress.c b/client/fpga_compress.c
index c67f6769..c1302694 100644
--- a/client/fpga_compress.c
+++ b/client/fpga_compress.c
@@ -14,6 +14,7 @@
 #include <string.h>
 #include <stdint.h>
 #include <stdbool.h>
+#include <inttypes.h>
 #include "zlib.h"
 
 #define MAX(a,b) ((a)>(b)?(a):(b))
@@ -140,7 +141,7 @@ int zlib_compress(FILE *infile[], uint8_t num_infiles, FILE *outfile)
 		ret = deflate(&compressed_fpga_stream, Z_FINISH);
 	}
 	
-	fprintf(stderr, "compressed %u input bytes to %lu output bytes\n", i, compressed_fpga_stream.total_out);
+	fprintf(stderr, "compressed %u input bytes to %" PRIu32 " output bytes\n", i, compressed_fpga_stream.total_out);
 
 	if (ret != Z_STREAM_END) {
 		fprintf(stderr, "Error in deflate(): %d %s\n", ret, compressed_fpga_stream.msg);
diff --git a/client/mifarehost.c b/client/mifarehost.c
index f9e05807..d1bfa69a 100644
--- a/client/mifarehost.c
+++ b/client/mifarehost.c
@@ -170,7 +170,7 @@ int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo
 		free(statelists[1].head.slhead);
 		num_to_bytes(key64, 6, resultKey);
 
-		PrintAndLog("UID: %08x target block:%3u key type: %c  -- Found key [%012"llx"]",
+		PrintAndLog("UID: %08x target block:%3u key type: %c  -- Found key [%012" PRIx64 "]",
 			uid,
 			(uint16_t)resp.arg[2] & 0xff,
 			(resp.arg[2] >> 8) ? 'B' : 'A',
@@ -627,7 +627,7 @@ int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {
 			lfsr_rollback_word(revstate, nr_enc, 1);
 			lfsr_rollback_word(revstate, cuid ^ nt, 0);
 			crypto1_get_lfsr(revstate, &key);
-			PrintAndLog("Found Key: [%012"llx"]", key);
+			PrintAndLog("Found Key: [%012" PRIx64 "]", key);
 			
 			//if ( tryMfk64(cuid, nt, nr_enc, ar_enc, at_enc, &key) )
 			AddLogUint64(logHexFileName, "Found Key: ", key); 
diff --git a/client/nonce2key/crypto1_bs.c b/client/nonce2key/crypto1_bs.c
index 7fd1b715..62d586df 100644
--- a/client/nonce2key/crypto1_bs.c
+++ b/client/nonce2key/crypto1_bs.c
@@ -25,9 +25,6 @@ THE SOFTWARE.
 #include "crypto1_bs.h"
 #include <inttypes.h>
 #define __STDC_FORMAT_MACROS
-#define llx PRIx64
-#define lli PRIi64
-#define lu PRIu32
 
 // The following functions use this global or thread-local state
 // It is sized to fit exactly KEYSTREAM_SIZE more states next to the initial state
@@ -117,7 +114,7 @@ void crypto1_bs_print_states(bitslice_t bitsliced_states[]){
     state_t values[MAX_BITSLICES]  = {{0x00}};
     crypto1_bs_convert_states(bitsliced_states, values);
     for(slice_idx = 0; slice_idx < MAX_BITSLICES; slice_idx++){
-        printf("State %03zu: %012"llx"\n", slice_idx, values[slice_idx].value);
+        printf("State %03zu: %012" PRIx64 "\n", slice_idx, values[slice_idx].value);
     }
 }
 
diff --git a/client/nonce2key/nonce2key.c b/client/nonce2key/nonce2key.c
index 64d35873..3c3d3e19 100644
--- a/client/nonce2key/nonce2key.c
+++ b/client/nonce2key/nonce2key.c
@@ -19,7 +19,7 @@ int nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint64_t par_info, uint64_
 	// Reset the last three significant bits of the reader nonce
 	nr &= 0xffffff1f;
   
-	PrintAndLog("uid(%08x) nt(%08x) par(%016"llx") ks(%016"llx") nr(%08x)", uid, nt, par_info, ks_info, nr);
+	PrintAndLog("uid(%08x) nt(%08x) par(%016" PRIx64") ks(%016" PRIx64") nr(%08x)", uid, nt, par_info, ks_info, nr);
 
 	for ( pos = 0; pos < 8; pos++ ) {
 		ks3x[7-pos] = (ks_info >> (pos*8)) & 0x0f;
@@ -121,11 +121,11 @@ int nonce2key_ex(uint8_t blockno, uint8_t keytype, uint32_t uid, uint32_t nt, ui
 		free(last_keylist);
 		last_keylist = state_s;
 		PrintAndLog("parity is all zero, testing special attack. First call, this attack needs at least two calls. Hold on...");		
-		PrintAndLog("uid(%08x) nt(%08x) ks(%016"llx") nr(%08x)", uid, nt, ks_info, nr);
+		PrintAndLog("uid(%08x) nt(%08x) ks(%016" PRIx64") nr(%08x)", uid, nt, ks_info, nr);
 		return 1;
 	}
 
-	PrintAndLog("uid(%08x) nt(%08x) ks(%016"llx") nr(%08x)", uid, nt, ks_info, nr);
+	PrintAndLog("uid(%08x) nt(%08x) ks(%016" PRIx64") nr(%08x)", uid, nt, ks_info, nr);
 		
 	//Create the intersection:
 	int64_t *p1, *p2, *p3;
@@ -134,7 +134,12 @@ int nonce2key_ex(uint8_t blockno, uint8_t keytype, uint32_t uid, uint32_t nt, ui
 		
 	while ( *p1 != -1 && *p2 != -1 ) {
 		if (compar_intA(p1, p2) == 0) {
-			PrintAndLog("p1:%"llx" p2:%"llx" p3:%"llx" key:%012"llx,(uint64_t)(p1-last_keylist),(uint64_t)(p2-state_s),(uint64_t)(p3-last_keylist),*p1);
+			PrintAndLog("p1:%" PRIx64" p2:%" PRIx64" p3:%" PRIx64" key:%012" PRIx64
+				, (uint64_t)(p1-last_keylist)
+				, (uint64_t)(p2-state_s)
+				, (uint64_t)(p3-last_keylist)
+				, *p1
+			);
 			*p3++ = *p1++;
 			p2++;
 		}
@@ -316,7 +321,7 @@ int tryMfk64(uint32_t uid, uint32_t nt, uint32_t nr_enc, uint32_t ar_enc, uint32
 	lfsr_rollback_word(revstate, uid ^ nt, 0);
 	crypto1_get_lfsr(revstate, &key);
 
-	PrintAndLog("Found Key: [%012"llx"]", key);
+	PrintAndLog("Found Key: [%012" PRIx64 "]", key);
 	t1 = clock() - t1;
 	if ( t1 > 0 ) PrintAndLog("Time in mfkey64: %.0f ticks", (float)t1);
 
diff --git a/client/proxmark3.h b/client/proxmark3.h
index 9b10af1a..56121269 100644
--- a/client/proxmark3.h
+++ b/client/proxmark3.h
@@ -8,7 +8,6 @@
 //-----------------------------------------------------------------------------
 // Main binary
 //-----------------------------------------------------------------------------
-
 #ifndef PROXMARK3_H__
 #define PROXMARK3_H__
 
@@ -28,14 +27,11 @@
 #include <inttypes.h>
 #include "usb_cmd.h"
 
+/*
 #define lu PRIu32
 #define lx  PRIx32
-#define llx PRIx64
-#define lli PRIi64
-#define llu PRIu64
-#define hhu PRIu8
+*/
 #define PROXPROMPT "pm3 --> "
 
 void SendCommand(UsbCommand *c);
-
-#endif
+#endif
\ No newline at end of file
diff --git a/client/reveng/poly.c b/client/reveng/poly.c
index ed311831..35e38af3 100644
--- a/client/reveng/poly.c
+++ b/client/reveng/poly.c
@@ -799,15 +799,15 @@ prev(poly_t *poly) {
 	unsigned long fulllength = poly->length + ofs;
 	bmp_t accu;
 
-	if(ofs)
+	if(ofs) {
 		/* removable optimisation */
 		if(poly->length < (unsigned long) BMP_BIT) {
 			*poly->bitmap = rev(*poly->bitmap >> ofs, (int) poly->length) << ofs;
 			return;
 		}
-
-		/* claim remaining bits of last word (as we use public function pshift()) */
-		poly->length = fulllength;
+	}
+	/* claim remaining bits of last word (as we use public function pshift()) */
+	poly->length = fulllength;
 
 	/* reverse and swap words in the array, leaving it right-justified */
 	while(leftidx < rightidx) {
diff --git a/client/scripting.c b/client/scripting.c
index 752edba6..f35707fe 100644
--- a/client/scripting.c
+++ b/client/scripting.c
@@ -145,7 +145,7 @@ static int l_nonce2key(lua_State *L){
     uint8_t dest_key[8];
     num_to_bytes(key,sizeof(dest_key),dest_key);
 
-    //printf("Pushing to lua stack: %012"llx"\n",key);
+    //printf("Pushing to lua stack: %012" PRIx64 "\n",key);
     lua_pushlstring(L,(const char *) dest_key,sizeof(dest_key));
 
     return 2; //Two return values
@@ -531,7 +531,7 @@ static int l_hardnested(lua_State *L){
     uint8_t dest_key[6];
     num_to_bytes(foundkey, sizeof(dest_key), dest_key);
 
-    //printf("Pushing to lua stack: %012"llx"\n",key);
+    //printf("Pushing to lua stack: %012" PRIx64 "\n",key);
     lua_pushlstring(L, (const char *) dest_key, sizeof(dest_key));
     return 2; //Two return values
 }
diff --git a/client/util.c b/client/util.c
index fdbd2648..4adc4e70 100644
--- a/client/util.c
+++ b/client/util.c
@@ -75,7 +75,7 @@ void AddLogUint64(char *fileName, char *extData, const uint64_t data) {
 	char buf[20] = {0};
 	memset(buf, 0x00, sizeof(buf));
 	//sprintf(buf, "%X%X", (unsigned int)((data & 0xFFFFFFFF00000000) >> 32), (unsigned int)(data & 0xFFFFFFFF));
-	sprintf(buf, "%012"llx"", data);
+	sprintf(buf, "%012" PRIx64 "", data);
 	AddLogLine(fileName, extData, buf);
 }
 
diff --git a/common/Makefile.common b/common/Makefile.common
index a13e32dd..aa93433a 100644
--- a/common/Makefile.common
+++ b/common/Makefile.common
@@ -59,15 +59,12 @@ DETECTED_OS=Windows
 
 endif
 
-#
 # uncomment these two; to enable EMV
-#
 #APP_CFLAGS += -DWITH_EMV
 #COMMON_FLAGS += -DWITH_EMV
-#
+
 # uncomment these two; to fix 256 vs 512kb PM3 devices
 # flashing bootrom -b is needed
-#
 APP_CFLAGS += -DHAS_512_FLASH
 COMMON_FLAGS += -DHAS_512_FLASH
 
diff --git a/tools/mfkey/mfkey32.c b/tools/mfkey/mfkey32.c
index 43e5a094..3872e81d 100755
--- a/tools/mfkey/mfkey32.c
+++ b/tools/mfkey/mfkey32.c
@@ -5,8 +5,6 @@
 #include <stdlib.h>
 #include <time.h>
 
-#define llx PRIx64
-#define lli PRIi64
 int main (int argc, char *argv[]) {
 	struct Crypto1State *s,*t;
 	uint64_t key;     // recovered key
@@ -64,7 +62,7 @@ int main (int argc, char *argv[]) {
 		crypto1_word(t, uid ^ nt, 0);
 		crypto1_word(t, nr1_enc, 1);
 		if (ar1_enc == (crypto1_word(t, 0, 0) ^ p64)) {
-			printf("\nFound Key: [%012"llx"]\n\n",key);
+			printf("\nFound Key: [%012" PRIx64 "]\n\n",key);
 			break;}
 	}
 	free(s);
diff --git a/tools/mfkey/mfkey32v2.c b/tools/mfkey/mfkey32v2.c
index 92ac371f..d220566e 100644
--- a/tools/mfkey/mfkey32v2.c
+++ b/tools/mfkey/mfkey32v2.c
@@ -4,9 +4,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
-
-#define llx PRIx64
-#define lli PRIi64
+ 
 int main (int argc, char *argv[]) {
 	struct Crypto1State *s,*t;
 	uint64_t key;     // recovered key
@@ -71,7 +69,7 @@ int main (int argc, char *argv[]) {
 		crypto1_word(t, uid ^ nt1, 0);
 		crypto1_word(t, nr1_enc, 1);
 		if (ar1_enc == (crypto1_word(t, 0, 0) ^ p64b)) {
-			printf("\nFound Key: [%012"llx"]\n\n",key);
+			printf("\nFound Key: [%012" PRIx64 "]\n\n",key);
 			break;}
 	}
 	free(s);
diff --git a/tools/mfkey/mfkey64.c b/tools/mfkey/mfkey64.c
index 616c6f3d..6a7e78c0 100755
--- a/tools/mfkey/mfkey64.c
+++ b/tools/mfkey/mfkey64.c
@@ -5,9 +5,6 @@
 #include <inttypes.h>
 #include "crapto1.h"
 
-#define llx PRIx64
-#define lli PRIi64
-
 int main (int argc, char *argv[]) {
 	struct Crypto1State *revstate;
 	uint64_t key;     // recovered key
@@ -98,7 +95,7 @@ int main (int argc, char *argv[]) {
 	lfsr_rollback_word(revstate, nr_enc, 1);
 	lfsr_rollback_word(revstate, uid ^ nt, 0);
 	crypto1_get_lfsr(revstate, &key);
-	printf("\nFound Key: [%012"llx"]\n\n", key);
+	printf("\nFound Key: [%012" PRIx64 "]\n\n", key);
 	crypto1_destroy(revstate);
   
 	t1 = clock() - t1;
diff --git a/tools/nonce2key/nonce2key.c b/tools/nonce2key/nonce2key.c
index b08c5cd2..250691c1 100644
--- a/tools/nonce2key/nonce2key.c
+++ b/tools/nonce2key/nonce2key.c
@@ -1,7 +1,6 @@
 #include "crapto1.h"
 #define __STDC_FORMAT_MACROS
 #include <inttypes.h>
-#define llx PRIx64
 #include <stdio.h>
 #include <time.h>
 typedef unsigned char byte_t;
@@ -19,15 +18,15 @@ int main(const int argc, const char* argv[]) {
 		printf("\nsyntax: %s <uid> <nt> <par> <ks>\n\n",argv[0]);
 		return 1;
 	}
-	sscanf(argv[1],"%08x",&uid);
-	sscanf(argv[2],"%08x",&nt);
-	sscanf(argv[3],"%016"llx,&par_info);
-	sscanf(argv[4],"%016"llx,&ks_info);
+	sscanf(argv[1],"%08x", &uid);
+	sscanf(argv[2],"%08x", &nt);
+	sscanf(argv[3],"%016" SCNx64 ,&par_info);
+	sscanf(argv[4],"%016" SCNx64 ,&ks_info);
   
 	// Reset the last three significant bits of the reader nonce
 	nr &= 0xffffff1f;
 
-	printf("\nuid(%08x) nt(%08x) par(%016"llx") ks(%016"llx")\n\n",uid,nt,par_info,ks_info);
+	printf("\nuid(%08x) nt(%08x) par(%016" PRIx64 ") ks(%016" PRIx64 ")\n\n", uid, nt, par_info, ks_info);
 
 	for ( pos = 0; pos < 8; pos++ ) {
 		ks3x[7-pos] = (ks_info >> (pos*8)) & 0x0f;
@@ -56,7 +55,7 @@ int main(const int argc, const char* argv[]) {
 	state = lfsr_common_prefix(nr,rr,ks3x,par);
 	lfsr_rollback_word(state,uid^nt,0);
 	crypto1_get_lfsr(state,&key_recovered);
-	printf("\nkey recovered: %012"llx"\n\n",key_recovered);
+	printf("\nkey recovered: %012" PRIx64 "\n\n", key_recovered);
 	crypto1_destroy(state);
   
 	t1 = clock() - t1;
diff --git a/zlib/trees.c b/zlib/trees.c
index dbfa49c9..49cce330 100644
--- a/zlib/trees.c
+++ b/zlib/trees.c
@@ -951,7 +951,7 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
         opt_lenb = (s->opt_len+3+7)>>3;
         static_lenb = (s->static_len+3+7)>>3;
 
-        Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
+        Tracev((stderr, "\nopt %" PRIu32 "(%" PRIu32 ") stat %" PRIu32 "(%" PRIu32 ") stored %" PRIu32 " lit %u ",
                 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
                 s->last_lit));