Use explicitly sized types in zipalign/ziptime

getLongLE would return a 64-bit number with the upper 32-bits set when
decoding a 32-bit number with the top bit set. Per the zip file format,
it was only expected to return a 32-bit number. Use explicitly sized
types so that we use the proper sizes and don't do any implicit
extensions.

Change-Id: I5a4304dc99ce5f8f17284d4ca3094ae115207a1e
diff --git a/tools/ziptime/ZipEntry.cpp b/tools/ziptime/ZipEntry.cpp
index bdbdd32..51ce09f 100644
--- a/tools/ziptime/ZipEntry.cpp
+++ b/tools/ziptime/ZipEntry.cpp
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <inttypes.h>
 
 using namespace android;
 
@@ -55,7 +56,7 @@
     /* using the info in the CDE, go load up the LFH */
     posn = ftell(fp);
     if (fseek(fp, mCDE.mLocalHeaderRelOffset, SEEK_SET) != 0) {
-        LOG("local header seek failed (%ld)\n",
+        LOG("local header seek failed (%" PRIu32 ")\n",
             mCDE.mLocalHeaderRelOffset);
         return -1;
     }
@@ -86,7 +87,7 @@
 status_t ZipEntry::LocalFileHeader::rewrite(FILE* fp)
 {
     status_t result = 0;
-    unsigned char buf[kLFHLen];
+    uint8_t buf[kLFHLen];
 
     if (fread(buf, 1, kLFHLen, fp) != kLFHLen)
         return -1;
@@ -124,8 +125,8 @@
 status_t ZipEntry::CentralDirEntry::rewrite(FILE* fp)
 {
     status_t result = 0;
-    unsigned char buf[kCDELen];
-    unsigned short fileNameLength, extraFieldLength, fileCommentLength;
+    uint8_t buf[kCDELen];
+    uint16_t fileNameLength, extraFieldLength, fileCommentLength;
 
     if (fread(buf, 1, kCDELen, fp) != kCDELen)
         return -1;
diff --git a/tools/ziptime/ZipEntry.h b/tools/ziptime/ZipEntry.h
index beea20c..26bf596 100644
--- a/tools/ziptime/ZipEntry.h
+++ b/tools/ziptime/ZipEntry.h
@@ -23,6 +23,7 @@
 #define __LIBS_ZIPENTRY_H
 
 #include <stdlib.h>
+#include <stdint.h>
 #include <stdio.h>
 
 typedef int status_t;
@@ -49,15 +50,15 @@
      * Some basic functions for raw data manipulation.  "LE" means
      * Little Endian.
      */
-    static inline unsigned short getShortLE(const unsigned char* buf) {
+    static inline uint16_t getShortLE(const uint8_t* buf) {
         return buf[0] | (buf[1] << 8);
     }
-    static inline unsigned long getLongLE(const unsigned char* buf) {
+    static inline uint32_t getLongLE(const uint8_t* buf) {
         return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
     }
-    static inline void putShortLE(unsigned char* buf, short val) {
-        buf[0] = (unsigned char) val;
-        buf[1] = (unsigned char) (val >> 8);
+    static inline void putShortLE(uint8_t* buf, uint16_t val) {
+        buf[0] = (uint8_t) val;
+        buf[1] = (uint8_t) (val >> 8);
     }
 
 protected:
@@ -99,7 +100,7 @@
 
         status_t rewrite(FILE* fp);
 
-        unsigned long   mLocalHeaderRelOffset;
+        uint32_t mLocalHeaderRelOffset;
 
         enum {
             kSignature      = 0x02014b50,
diff --git a/tools/ziptime/ZipFile.cpp b/tools/ziptime/ZipFile.cpp
index c4c898e..1d111af 100644
--- a/tools/ziptime/ZipFile.cpp
+++ b/tools/ziptime/ZipFile.cpp
@@ -24,6 +24,7 @@
 #include <sys/stat.h>
 #include <errno.h>
 #include <assert.h>
+#include <inttypes.h>
 
 using namespace android;
 
@@ -72,7 +73,7 @@
 status_t ZipFile::rewriteCentralDir(void)
 {
     status_t result = 0;
-    unsigned char* buf = NULL;
+    uint8_t* buf = NULL;
     off_t fileLength, seekStart;
     long readAmount;
     int i;
@@ -88,7 +89,7 @@
         goto bail;
     }
 
-    buf = new unsigned char[EndOfCentralDir::kMaxEOCDSearch];
+    buf = new uint8_t[EndOfCentralDir::kMaxEOCDSearch];
     if (buf == NULL) {
         LOG("Failure allocating %d bytes for EOCD search",
              EndOfCentralDir::kMaxEOCDSearch);
@@ -152,7 +153,7 @@
      * we're hoping to preserve.
      */
     if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
-        LOG("Failure seeking to central dir offset %ld\n",
+        LOG("Failure seeking to central dir offset %" PRIu32 "\n",
              mEOCD.mCentralDirOffset);
         result = -1;
         goto bail;
@@ -179,7 +180,7 @@
     /*
      * If all went well, we should now be back at the EOCD.
      */
-    unsigned char checkBuf[4];
+    uint8_t checkBuf[4];
     if (fread(checkBuf, 1, 4, mZipFp) != 4) {
         LOG("EOCD check read failed\n");
         result = -1;
@@ -208,9 +209,9 @@
  * "buf" should be positioned at the EOCD signature, and should contain
  * the entire EOCD area including the comment.
  */
-status_t ZipFile::EndOfCentralDir::readBuf(const unsigned char* buf, int len)
+status_t ZipFile::EndOfCentralDir::readBuf(const uint8_t* buf, int len)
 {
-    unsigned short diskNumber, diskWithCentralDir, numEntries;
+    uint16_t diskNumber, diskWithCentralDir, numEntries;
 
     if (len < kEOCDLen) {
         /* looks like ZIP file got truncated */
diff --git a/tools/ziptime/ZipFile.h b/tools/ziptime/ZipFile.h
index 50ca923..b049e05 100644
--- a/tools/ziptime/ZipFile.h
+++ b/tools/ziptime/ZipFile.h
@@ -51,10 +51,10 @@
     public:
         EndOfCentralDir(void) : mTotalNumEntries(0), mCentralDirOffset(0) {}
 
-        status_t readBuf(const unsigned char* buf, int len);
+        status_t readBuf(const uint8_t* buf, int len);
 
-        unsigned short  mTotalNumEntries;
-        unsigned long   mCentralDirOffset;      // offset from first disk
+        uint16_t mTotalNumEntries;
+        uint32_t mCentralDirOffset;      // offset from first disk
 
         enum {
             kSignature      = 0x06054b50,