modules/libjar/zipwriter/src/nsZipHeader.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     4  */
     6 #include "StreamFunctions.h"
     7 #include "nsZipHeader.h"
     8 #include "nsMemory.h"
     9 #include "prtime.h"
    11 #define ZIP_FILE_HEADER_SIGNATURE 0x04034b50
    12 #define ZIP_FILE_HEADER_SIZE 30
    13 #define ZIP_CDS_HEADER_SIGNATURE 0x02014b50
    14 #define ZIP_CDS_HEADER_SIZE 46
    16 #define FLAGS_IS_UTF8 0x800
    18 #define ZIP_EXTENDED_TIMESTAMP_FIELD 0x5455
    19 #define ZIP_EXTENDED_TIMESTAMP_MODTIME 0x01
    21 /**
    22  * nsZipHeader represents an entry from a zip file.
    23  */
    24 NS_IMPL_ISUPPORTS(nsZipHeader, nsIZipEntry)
    26 /* readonly attribute unsigned short compression; */
    27 NS_IMETHODIMP nsZipHeader::GetCompression(uint16_t *aCompression)
    28 {
    29     NS_ASSERTION(mInited, "Not initalised");
    31     *aCompression = mMethod;
    32     return NS_OK;
    33 }
    35 /* readonly attribute unsigned long size; */
    36 NS_IMETHODIMP nsZipHeader::GetSize(uint32_t *aSize)
    37 {
    38     NS_ASSERTION(mInited, "Not initalised");
    40     *aSize = mCSize;
    41     return NS_OK;
    42 }
    44 /* readonly attribute unsigned long realSize; */
    45 NS_IMETHODIMP nsZipHeader::GetRealSize(uint32_t *aRealSize)
    46 {
    47     NS_ASSERTION(mInited, "Not initalised");
    49     *aRealSize = mUSize;
    50     return NS_OK;
    51 }
    53 /* readonly attribute unsigned long CRC32; */
    54 NS_IMETHODIMP nsZipHeader::GetCRC32(uint32_t *aCRC32)
    55 {
    56     NS_ASSERTION(mInited, "Not initalised");
    58     *aCRC32 = mCRC;
    59     return NS_OK;
    60 }
    62 /* readonly attribute boolean isDirectory; */
    63 NS_IMETHODIMP nsZipHeader::GetIsDirectory(bool *aIsDirectory)
    64 {
    65     NS_ASSERTION(mInited, "Not initalised");
    67     if (mName.Last() == '/')
    68         *aIsDirectory = true;
    69     else
    70         *aIsDirectory = false;
    71     return NS_OK;
    72 }
    74 /* readonly attribute PRTime lastModifiedTime; */
    75 NS_IMETHODIMP nsZipHeader::GetLastModifiedTime(PRTime *aLastModifiedTime)
    76 {
    77     NS_ASSERTION(mInited, "Not initalised");
    79     // Try to read timestamp from extra field
    80     uint16_t blocksize;
    81     const uint8_t *tsField = GetExtraField(ZIP_EXTENDED_TIMESTAMP_FIELD, false, &blocksize);
    82     if (tsField && blocksize >= 5) {
    83         uint32_t pos = 4;
    84         uint8_t flags;
    85         flags = READ8(tsField, &pos);
    86         if (flags & ZIP_EXTENDED_TIMESTAMP_MODTIME) {
    87             *aLastModifiedTime = (PRTime)(READ32(tsField, &pos))
    88                                  * PR_USEC_PER_SEC;
    89             return NS_OK;
    90         }
    91     }
    93     // Use DOS date/time fields
    94     // Note that on DST shift we can't handle correctly the hour that is valid
    95     // in both DST zones
    96     PRExplodedTime time;
    98     time.tm_usec = 0;
   100     time.tm_hour = (mTime >> 11) & 0x1F;
   101     time.tm_min = (mTime >> 5) & 0x3F;
   102     time.tm_sec = (mTime & 0x1F) * 2;
   104     time.tm_year = (mDate >> 9) + 1980;
   105     time.tm_month = ((mDate >> 5) & 0x0F) - 1;
   106     time.tm_mday = mDate & 0x1F;
   108     time.tm_params.tp_gmt_offset = 0;
   109     time.tm_params.tp_dst_offset = 0;
   111     PR_NormalizeTime(&time, PR_GMTParameters);
   112     time.tm_params.tp_gmt_offset = PR_LocalTimeParameters(&time).tp_gmt_offset;
   113     PR_NormalizeTime(&time, PR_GMTParameters);
   114     time.tm_params.tp_dst_offset = PR_LocalTimeParameters(&time).tp_dst_offset;
   116     *aLastModifiedTime = PR_ImplodeTime(&time);
   118     return NS_OK;
   119 }
   121 /* readonly attribute boolean isSynthetic; */
   122 NS_IMETHODIMP nsZipHeader::GetIsSynthetic(bool *aIsSynthetic)
   123 {
   124     NS_ASSERTION(mInited, "Not initalised");
   126     *aIsSynthetic = false;
   127     return NS_OK;
   128 }
   130 /* readonly attribute unsigned long permissions; */
   131 NS_IMETHODIMP nsZipHeader::GetPermissions(uint32_t *aPermissions)
   132 {
   133     NS_ASSERTION(mInited, "Not initalised");
   135     // Always give user read access at least, this matches nsIZipReader's behaviour
   136     *aPermissions = ((mEAttr >> 16) & 0xfff) | 0x100;
   137     return NS_OK;
   138 }
   140 void nsZipHeader::Init(const nsACString & aPath, PRTime aDate, uint32_t aAttr,
   141                        uint32_t aOffset)
   142 {
   143     NS_ASSERTION(!mInited, "Already initalised");
   145     PRExplodedTime time;
   146     PR_ExplodeTime(aDate, PR_LocalTimeParameters, &time);
   148     mTime = time.tm_sec / 2 + (time.tm_min << 5) + (time.tm_hour << 11);
   149     mDate = time.tm_mday + ((time.tm_month + 1) << 5) +
   150             ((time.tm_year - 1980) << 9);
   152     // Store modification timestamp as extra field
   153     // First fill CDS extra field
   154     mFieldLength = 9;
   155     mExtraField = new uint8_t[mFieldLength];
   156     if (!mExtraField) {
   157         mFieldLength = 0;
   158     } else {
   159         uint32_t pos = 0;
   160         WRITE16(mExtraField.get(), &pos, ZIP_EXTENDED_TIMESTAMP_FIELD);
   161         WRITE16(mExtraField.get(), &pos, 5);
   162         WRITE8(mExtraField.get(), &pos, ZIP_EXTENDED_TIMESTAMP_MODTIME);
   163         WRITE32(mExtraField.get(), &pos, aDate / PR_USEC_PER_SEC);
   165         // Fill local extra field
   166         mLocalExtraField = new uint8_t[mFieldLength];
   167         if (mLocalExtraField) {
   168             mLocalFieldLength = mFieldLength;
   169             memcpy(mLocalExtraField.get(), mExtraField.get(), mLocalFieldLength);
   170         }
   171     }
   173     mEAttr = aAttr;
   174     mOffset = aOffset;
   175     mName = aPath;
   176     mComment = NS_LITERAL_CSTRING("");
   177     // Claim a UTF-8 path in case it needs it.
   178     mFlags |= FLAGS_IS_UTF8;
   179     mInited = true;
   180 }
   182 uint32_t nsZipHeader::GetFileHeaderLength()
   183 {
   184     return ZIP_FILE_HEADER_SIZE + mName.Length() + mLocalFieldLength;
   185 }
   187 nsresult nsZipHeader::WriteFileHeader(nsIOutputStream *aStream)
   188 {
   189     NS_ASSERTION(mInited, "Not initalised");
   191     uint8_t buf[ZIP_FILE_HEADER_SIZE];
   192     uint32_t pos = 0;
   193     WRITE32(buf, &pos, ZIP_FILE_HEADER_SIGNATURE);
   194     WRITE16(buf, &pos, mVersionNeeded);
   195     WRITE16(buf, &pos, mFlags);
   196     WRITE16(buf, &pos, mMethod);
   197     WRITE16(buf, &pos, mTime);
   198     WRITE16(buf, &pos, mDate);
   199     WRITE32(buf, &pos, mCRC);
   200     WRITE32(buf, &pos, mCSize);
   201     WRITE32(buf, &pos, mUSize);
   202     WRITE16(buf, &pos, mName.Length());
   203     WRITE16(buf, &pos, mLocalFieldLength);
   205     nsresult rv = ZW_WriteData(aStream, (const char *)buf, pos);
   206     NS_ENSURE_SUCCESS(rv, rv);
   208     rv = ZW_WriteData(aStream, mName.get(), mName.Length());
   209     NS_ENSURE_SUCCESS(rv, rv);
   211     if (mLocalFieldLength)
   212     {
   213       rv = ZW_WriteData(aStream, (const char *)mLocalExtraField.get(), mLocalFieldLength);
   214       NS_ENSURE_SUCCESS(rv, rv);
   215     }
   217     return NS_OK;
   218 }
   220 uint32_t nsZipHeader::GetCDSHeaderLength()
   221 {
   222     return ZIP_CDS_HEADER_SIZE + mName.Length() + mComment.Length() +
   223            mFieldLength;
   224 }
   226 nsresult nsZipHeader::WriteCDSHeader(nsIOutputStream *aStream)
   227 {
   228     NS_ASSERTION(mInited, "Not initalised");
   230     uint8_t buf[ZIP_CDS_HEADER_SIZE];
   231     uint32_t pos = 0;
   232     WRITE32(buf, &pos, ZIP_CDS_HEADER_SIGNATURE);
   233     WRITE16(buf, &pos, mVersionMade);
   234     WRITE16(buf, &pos, mVersionNeeded);
   235     WRITE16(buf, &pos, mFlags);
   236     WRITE16(buf, &pos, mMethod);
   237     WRITE16(buf, &pos, mTime);
   238     WRITE16(buf, &pos, mDate);
   239     WRITE32(buf, &pos, mCRC);
   240     WRITE32(buf, &pos, mCSize);
   241     WRITE32(buf, &pos, mUSize);
   242     WRITE16(buf, &pos, mName.Length());
   243     WRITE16(buf, &pos, mFieldLength);
   244     WRITE16(buf, &pos, mComment.Length());
   245     WRITE16(buf, &pos, mDisk);
   246     WRITE16(buf, &pos, mIAttr);
   247     WRITE32(buf, &pos, mEAttr);
   248     WRITE32(buf, &pos, mOffset);
   250     nsresult rv = ZW_WriteData(aStream, (const char *)buf, pos);
   251     NS_ENSURE_SUCCESS(rv, rv);
   253     rv = ZW_WriteData(aStream, mName.get(), mName.Length());
   254     NS_ENSURE_SUCCESS(rv, rv);
   255     if (mExtraField) {
   256         rv = ZW_WriteData(aStream, (const char *)mExtraField.get(), mFieldLength);
   257         NS_ENSURE_SUCCESS(rv, rv);
   258     }
   259     return ZW_WriteData(aStream, mComment.get(), mComment.Length());
   260 }
   262 nsresult nsZipHeader::ReadCDSHeader(nsIInputStream *stream)
   263 {
   264     NS_ASSERTION(!mInited, "Already initalised");
   266     uint8_t buf[ZIP_CDS_HEADER_SIZE];
   268     nsresult rv = ZW_ReadData(stream, (char *)buf, ZIP_CDS_HEADER_SIZE);
   269     NS_ENSURE_SUCCESS(rv, rv);
   271     uint32_t pos = 0;
   272     uint32_t signature = READ32(buf, &pos);
   273     if (signature != ZIP_CDS_HEADER_SIGNATURE)
   274         return NS_ERROR_FILE_CORRUPTED;
   276     mVersionMade = READ16(buf, &pos);
   277     mVersionNeeded = READ16(buf, &pos);
   278     mFlags = READ16(buf, &pos);
   279     mMethod = READ16(buf, &pos);
   280     mTime = READ16(buf, &pos);
   281     mDate = READ16(buf, &pos);
   282     mCRC = READ32(buf, &pos);
   283     mCSize = READ32(buf, &pos);
   284     mUSize = READ32(buf, &pos);
   285     uint16_t namelength = READ16(buf, &pos);
   286     mFieldLength = READ16(buf, &pos);
   287     uint16_t commentlength = READ16(buf, &pos);
   288     mDisk = READ16(buf, &pos);
   289     mIAttr = READ16(buf, &pos);
   290     mEAttr = READ32(buf, &pos);
   291     mOffset = READ32(buf, &pos);
   293     if (namelength > 0) {
   294         nsAutoArrayPtr<char> field(new char[namelength]);
   295         NS_ENSURE_TRUE(field, NS_ERROR_OUT_OF_MEMORY);
   296         rv = ZW_ReadData(stream, field.get(), namelength);
   297         NS_ENSURE_SUCCESS(rv, rv);
   298         mName.Assign(field, namelength);
   299     }
   300     else
   301         mName = NS_LITERAL_CSTRING("");
   303     if (mFieldLength > 0) {
   304         mExtraField = new uint8_t[mFieldLength];
   305         NS_ENSURE_TRUE(mExtraField, NS_ERROR_OUT_OF_MEMORY);
   306         rv = ZW_ReadData(stream, (char *)mExtraField.get(), mFieldLength);
   307         NS_ENSURE_SUCCESS(rv, rv);
   308     }
   310     if (commentlength > 0) {
   311         nsAutoArrayPtr<char> field(new char[commentlength]);
   312         NS_ENSURE_TRUE(field, NS_ERROR_OUT_OF_MEMORY);
   313         rv = ZW_ReadData(stream, field.get(), commentlength);
   314         NS_ENSURE_SUCCESS(rv, rv);
   315         mComment.Assign(field, commentlength);
   316     }
   317     else
   318         mComment = NS_LITERAL_CSTRING("");
   320     mInited = true;
   321     return NS_OK;
   322 }
   324 const uint8_t * nsZipHeader::GetExtraField(uint16_t aTag, bool aLocal, uint16_t *aBlockSize)
   325 {
   326     const uint8_t *buf = aLocal ? mLocalExtraField : mExtraField;
   327     uint32_t buflen = aLocal ? mLocalFieldLength : mFieldLength;
   328     uint32_t pos = 0;
   329     uint16_t tag, blocksize;
   331     while (buf && (pos + 4) <= buflen) {
   332       tag = READ16(buf, &pos);
   333       blocksize = READ16(buf, &pos);
   335       if (aTag == tag && (pos + blocksize) <= buflen) {
   336         *aBlockSize = blocksize;
   337         return buf + pos - 4;
   338       }
   340       pos += blocksize;
   341     }
   343     return nullptr;
   344 }
   346 /*
   347  * Pad extra field to align data starting position to specified size.
   348  */
   349 nsresult nsZipHeader::PadExtraField(uint32_t aOffset, uint16_t aAlignSize)
   350 {
   351     uint32_t pad_size;
   352     uint32_t pa_offset;
   353     uint32_t pa_end;
   355     // Check for range and power of 2.
   356     if (aAlignSize < 2 || aAlignSize > 32768 ||
   357         (aAlignSize & (aAlignSize - 1)) != 0) {
   358       return NS_ERROR_INVALID_ARG;
   359     }
   361     // Point to current starting data position.
   362     aOffset += ZIP_FILE_HEADER_SIZE + mName.Length() + mLocalFieldLength;
   364     // Calculate aligned offset.
   365     pa_offset = aOffset & ~(aAlignSize - 1);
   366     pa_end = pa_offset + aAlignSize;
   367     pad_size = pa_end - aOffset;
   368     if (pad_size == 0) {
   369       return NS_OK;
   370     }
   372     // Leave enough room(at least 4 bytes) for valid values in extra field.
   373     while (pad_size < 4) {
   374       pad_size += aAlignSize;
   375     }
   376     // Extra field length is 2 bytes.
   377     if (mLocalFieldLength + pad_size > 65535) {
   378       return NS_ERROR_FAILURE;
   379     }
   381     nsAutoArrayPtr<uint8_t> field = mLocalExtraField;
   382     uint32_t pos = mLocalFieldLength;
   384     mLocalExtraField = new uint8_t[mLocalFieldLength + pad_size];
   385     memcpy(mLocalExtraField.get(), field, mLocalFieldLength);
   386     // Use 0xFFFF as tag ID to avoid conflict with other IDs.
   387     // For more information, please read "Extensible data fields" section in:
   388     // http://www.pkware.com/documents/casestudies/APPNOTE.TXT
   389     WRITE16(mLocalExtraField.get(), &pos, 0xFFFF);
   390     WRITE16(mLocalExtraField.get(), &pos, pad_size - 4);
   391     memset(mLocalExtraField.get() + pos, 0, pad_size - 4);
   392     mLocalFieldLength += pad_size;
   394     return NS_OK;
   395 }

mercurial