System: Fastboot: warning fixit, misc bugs and cleanup.

Change-Id: Ib8517a852977962de3f05c879ba7dce89bcef644
Signed-off-by: Sasha Levitskiy <sanek@google.com>
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c
index 4d3e0af..eeab2e9 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.c
@@ -92,8 +92,8 @@
 
 struct fastboot_buffer {
     enum fb_buffer_type type;
+    size_t sz;
     void *data;
-    unsigned int sz;
 };
 
 static struct {
@@ -152,7 +152,7 @@
     return strdup(path);
 }
 
-static int64_t file_size(int fd)
+static ssize_t file_size(int fd)
 {
     struct stat st;
     int ret;
@@ -162,15 +162,12 @@
     return ret ? -1 : st.st_size;
 }
 
-static void *load_fd(int fd, unsigned *_sz)
+static void *load_fd(int fd, size_t *_sz)
 {
-    char *data;
-    int sz;
+    char *data = NULL;
+    ssize_t sz = file_size(fd);
     int errno_tmp;
 
-    data = 0;
-
-    sz = file_size(fd);
     if (sz < 0) {
         goto oops;
     }
@@ -192,7 +189,7 @@
     return 0;
 }
 
-static void *load_file(const char *fn, unsigned *_sz)
+static void *load_file(const char *fn, size_t *_sz)
 {
     int fd;
 
@@ -381,34 +378,34 @@
     return bdata;
 }
 
-void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
+void *unzip_file(zipfile_t zip, const char *name, size_t *sz)
 {
     void *data;
-    zipentry_t entry;
-    unsigned datasz;
+    zipentry_t entry = lookup_zipentry(zip, name);
+    size_t zentrysz;
+    size_t datasz;
 
-    entry = lookup_zipentry(zip, name);
     if (entry == NULL) {
         fprintf(stderr, "archive does not contain '%s'\n", name);
-        return 0;
+        return NULL;
     }
 
-    *sz = get_zipentry_size(entry);
+    zentrysz = get_zipentry_size(entry);
 
-    datasz = *sz * 1.001;
+    datasz = zentrysz * 1.001;
     data = malloc(datasz);
 
-    if(data == 0) {
-        fprintf(stderr, "failed to allocate %d bytes\n", *sz);
-        return 0;
+    if(data == NULL) {
+        fprintf(stderr, "failed to allocate %zu bytes\n", datasz);
+        return NULL;
     }
 
     if (decompress_zipentry(entry, data, datasz)) {
         fprintf(stderr, "failed to unzip '%s' from archive\n", name);
         free(data);
-        return 0;
+        return NULL;
     }
-
+    *sz = zentrysz;
     return data;
 }
 
@@ -416,24 +413,25 @@
 {
     int fd;
     char *data;
-    unsigned sz;
+    size_t sz;
 
     fd = fileno(tmpfile());
     if (fd < 0) {
-        return -1;
+        return fd;
     }
 
     data = unzip_file(zip, name, &sz);
-    if (data == 0) {
+    if (data == NULL) {
         return -1;
     }
 
-    if (write(fd, data, sz) != sz) {
+    if (write(fd, data, sz) != (ssize_t)sz) {
         fd = -1;
+    } else {
+        lseek(fd, 0, SEEK_SET);
     }
 
     free(data);
-    lseek(fd, 0, SEEK_SET);
     return fd;
 }
 
@@ -514,11 +512,10 @@
     return 0;
 }
 
-static void setup_requirements(char *data, unsigned sz)
+static void setup_requirements(char *data, size_t sz)
 {
-    char *s;
+    char *s = data;
 
-    s = data;
     while (sz-- > 0) {
         if(*s == '\n') {
             *s++ = 0;
@@ -628,14 +625,15 @@
 static int load_buf_fd(usb_handle *usb, int fd,
         struct fastboot_buffer *buf)
 {
-    int64_t sz64;
-    void *data;
+    ssize_t f_size = file_size(fd);
+    void *data = NULL;
     int64_t limit;
+    int64_t sz64;
 
-
-    sz64 = file_size(fd);
-    if (sz64 < 0) {
-        return -1;
+    if (f_size < 0) {
+        return f_size;
+    } else {
+        sz64 = f_size;
     }
 
     lseek(fd, 0, SEEK_SET);
@@ -648,12 +646,12 @@
         buf->type = FB_BUFFER_SPARSE;
         buf->data = s;
     } else {
-        unsigned int sz;
+        size_t sz;
         data = load_fd(fd, &sz);
-        if (data == 0) return -1;
+        if (data == NULL) return -1;
         buf->type = FB_BUFFER;
-        buf->data = data;
         buf->sz = sz;
+        buf->data = data;
     }
 
     return 0;
@@ -704,10 +702,11 @@
 
 void do_update_signature(zipfile_t zip, char *fn)
 {
-    void *data;
-    unsigned sz;
-    data = unzip_file(zip, fn, &sz);
-    if (data == 0) return;
+    size_t sz;
+    void *data = unzip_file(zip, fn, &sz);
+    if (data == NULL) {
+        die("can't unzip '%s'", fn);
+    }
     fb_queue_download("signature", data, sz);
     fb_queue_command("signature", "installing signature");
 }
@@ -717,12 +716,12 @@
     void *zdata;
     unsigned zsize;
     void *data;
-    unsigned sz;
+    size_t sz = 0;
     zipfile_t zip;
     int fd;
     int rc;
     struct fastboot_buffer buf;
-    int i;
+    size_t i;
 
     queue_info_dump();
 
@@ -735,11 +734,11 @@
     if(zip == 0) die("failed to access zipdata in '%s'");
 
     data = unzip_file(zip, "android-info.txt", &sz);
-    if (data == 0) {
+    if (data == NULL) {
         char *tmp;
             /* fallback for older zipfiles */
         data = unzip_file(zip, "android-product.txt", &sz);
-        if ((data == 0) || (sz < 1)) {
+        if ((data == NULL) || (sz < 1)) {
             die("update package has no android-info.txt or android-product.txt");
         }
         tmp = malloc(sz + 128);
@@ -775,7 +774,7 @@
 void do_send_signature(char *fn)
 {
     void *data;
-    unsigned sz;
+    size_t sz;
     char *xtn;
 
     xtn = strrchr(fn, '.');
@@ -794,9 +793,9 @@
 {
     char *fname;
     void *data;
-    unsigned sz;
+    size_t sz;
     struct fastboot_buffer buf;
-    int i;
+    size_t i;
 
     queue_info_dump();
 
@@ -978,7 +977,7 @@
     int wants_reboot_bootloader = 0;
     int erase_first = 1;
     void *data;
-    unsigned sz;
+    size_t sz;
     int status;
     int c;
     int r;