From c6e0a2ebebb186849764b5a227662c515cfd7c6b Mon Sep 17 00:00:00 2001
From: iceman1001 <iceman@iuse.se>
Date: Sun, 14 Feb 2016 13:29:20 +0100
Subject: [PATCH] FIX:  Faster data load.

---
 client/cmdhflegic.c | 59 ++++++++++++++++++++++++++++++---------------
 1 file changed, 39 insertions(+), 20 deletions(-)

diff --git a/client/cmdhflegic.c b/client/cmdhflegic.c
index 1863c955..aef6ef0c 100644
--- a/client/cmdhflegic.c
+++ b/client/cmdhflegic.c
@@ -235,28 +235,53 @@ int CmdLegicLoad(const char *Cmd) {
 	
     char line[80]; 
 	int offset = 0; 
-	uint8_t data[8] = {0x00};
-	
+	uint8_t data[USB_CMD_DATA_SIZE] = {0x00};
+	int index = 0;
+	int totalbytes = 0;
     while ( fgets(line, sizeof(line), f) ) {
         int res = sscanf(line, "%x %x %x %x %x %x %x %x", 
-            (unsigned int *)&data[0], (unsigned int *)&data[1], (unsigned int *)&data[2], (unsigned int *)&data[3],
-            (unsigned int *)&data[4], (unsigned int *)&data[5], (unsigned int *)&data[6], (unsigned int *)&data[7]);
+            (unsigned int *)&data[index],
+			(unsigned int *)&data[index + 1],
+			(unsigned int *)&data[index + 2],
+			(unsigned int *)&data[index + 3],
+            (unsigned int *)&data[index + 4],
+			(unsigned int *)&data[index + 5],
+			(unsigned int *)&data[index + 6],
+			(unsigned int *)&data[index + 7]);
 			
         if(res != 8) {
           PrintAndLog("Error: could not read samples");
           fclose(f);
           return -1;
         }
-		
-        UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
-		memcpy(c.d.asBytes, data, 8);
-		clearCommandBuffer();
-        SendCommand(&c);
-        WaitForResponse(CMD_ACK, NULL);
-        offset += 8;
+		index += res;
+			
+		if ( index == USB_CMD_DATA_SIZE ){
+//			PrintAndLog("sent %d | %d | %d", index, offset, totalbytes);
+			UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
+			memcpy(c.d.asBytes, data, sizeof(data));
+			clearCommandBuffer();
+			SendCommand(&c);
+			WaitForResponse(CMD_ACK, NULL);
+
+			offset += index;
+			totalbytes += index;
+			index = 0;
+		}
     }
     fclose(f);
-    PrintAndLog("loaded %u samples", offset);
+	
+	// left over bytes?
+	if ( index != 0 ) {
+		UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
+		memcpy(c.d.asBytes, data, 8);
+		clearCommandBuffer();
+		SendCommand(&c);
+		WaitForResponse(CMD_ACK, NULL);
+		totalbytes += index;		
+	}
+	
+    PrintAndLog("loaded %u samples", totalbytes);
     return 0;
 }
 
@@ -295,14 +320,8 @@ int CmdLegicSave(const char *Cmd) {
 
 	for (int j = 0; j < requested; j += 8) {
 		fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
-			got[j+0],
-			got[j+1],
-			got[j+2],
-			got[j+3],
-			got[j+4],
-			got[j+5],
-			got[j+6],
-			got[j+7]
+			got[j+0], got[j+1], got[j+2], got[j+3],
+			got[j+4], got[j+5],	got[j+6], got[j+7]
 		);
 		delivered += 8;
 		if (delivered >= requested) break;
-- 
2.39.5