From ab63172abb157380107d35ff3871202da3046781 Mon Sep 17 00:00:00 2001
From: Nikias Bassen
Date: Thu, 2 Feb 2012 23:22:01 +0100
Subject: main: add check for local .shsh files in get_shsh_blobs()

However this is only done for iOS < 5 or in custom firmware mode.
---
 src/idevicerestore.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

(limited to 'src')

diff --git a/src/idevicerestore.c b/src/idevicerestore.c
index bfe19c7..f1d441a 100644
--- a/src/idevicerestore.c
+++ b/src/idevicerestore.c
@@ -25,6 +25,7 @@
 #include <unistd.h>
 #include <getopt.h>
 #include <plist/plist.h>
+#include <zlib.h>
 
 #include "dfu.h"
 #include "tss.h"
@@ -815,6 +816,43 @@ int get_shsh_blobs(struct idevicerestore_client_t* client, uint64_t ecid, unsign
 	plist_t response = NULL;
 	*tss = NULL;
 
+	if ((client->build[0] <= 8) || (client->flags & FLAG_CUSTOM)) {
+		error("checking for local shsh\n");
+
+		/* first check for local copy */
+		char zfn[512];
+		if (client->version) {
+			sprintf(zfn, "shsh/%lld-%s-%s.shsh", (long long int)client->ecid, client->device->product, client->version);
+			struct stat fst;
+			if (stat(zfn, &fst) == 0) {
+				gzFile zf = gzopen(zfn, "rb");
+				if (zf) {
+					unsigned char bin[65536];
+					int blen = gzread(zf, bin, sizeof(bin));
+					if (blen > 0) {
+						if (memcmp(bin, "bplist00", 8) == 0) {
+							plist_from_bin(bin, blen, tss);
+						} else {
+							plist_from_xml(bin, blen, tss);
+						}
+					}
+					gzclose(zf);
+				}
+			} else {
+				error("no local file %s\n", zfn);
+			}
+		} else {
+			error("No version found?!\n");
+		}
+	}
+
+	if (*tss) {
+		info("Using cached SHSH\n");
+		return 0;
+	} else {
+		info("Trying to fetch new SHSH blob\n");
+	}
+
 	request = tss_create_request(build_identity, ecid, nonce, nonce_size);
 	if (request == NULL) {
 		error("ERROR: Unable to create TSS request\n");
-- 
cgit v1.1-32-gdbae