LCOV - app.info - modules/libjar/zipwriter/src/nsZipHeader.cpp

LCOV - code coverage report
| Current view: | directory - modules/libjar/zipwriter/src - nsZipHeader.cpp (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 189 | 160 | 84.7 %
| Date: | 2012-06-02 | Functions: | 17 | 16 | 94.1 %

           1                 : /* ***** BEGIN LICENSE BLOCK *****
           2                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
           3                 :  *
           4                 :  * The contents of this file are subject to the Mozilla Public License Version
           5                 :  * 1.1 (the "License"); you may not use this file except in compliance with
           6                 :  * the License. You may obtain a copy of the License at
           7                 :  * http://www.mozilla.org/MPL/
           8                 :  *
           9                 :  * Software distributed under the License is distributed on an "AS IS" basis,
          10                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
          11                 :  * for the specific language governing rights and limitations under the
          12                 :  * License.
          13                 :  *
          14                 :  * The Original Code is Zip Writer Component.
          15                 :  *
          16                 :  * The Initial Developer of the Original Code is
          17                 :  * Dave Townsend <dtownsend@oxymoronical.com>.
          18                 :  *
          19                 :  * Portions created by the Initial Developer are Copyright (C) 2007
          20                 :  * the Initial Developer. All Rights Reserved.
          21                 :  *
          22                 :  * Contributor(s):
          23                 :  *
          24                 :  * Alternatively, the contents of this file may be used under the terms of
          25                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
          26                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
          27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
          28                 :  * of those above. If you wish to allow use of your version of this file only
          29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
          30                 :  * use your version of this file under the terms of the MPL, indicate your
          31                 :  * decision by deleting the provisions above and replace them with the notice
          32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
          33                 :  * the provisions above, a recipient may use your version of this file under
          34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
          35                 :  *
          36                 :  * ***** END LICENSE BLOCK *****
          37                 :  */
          38                 :
          39                 : #include "StreamFunctions.h"
          40                 : #include "nsZipHeader.h"
          41                 : #include "nsMemory.h"
          42                 :
          43                 : #define ZIP_FILE_HEADER_SIGNATURE 0x04034b50
          44                 : #define ZIP_FILE_HEADER_SIZE 30
          45                 : #define ZIP_CDS_HEADER_SIGNATURE 0x02014b50
          46                 : #define ZIP_CDS_HEADER_SIZE 46
          47                 :
          48                 : #define FLAGS_IS_UTF8 0x800
          49                 :
          50                 : #define ZIP_EXTENDED_TIMESTAMP_FIELD 0x5455
          51                 : #define ZIP_EXTENDED_TIMESTAMP_MODTIME 0x01
          52                 :
          53                 : /**
          54                 :  * nsZipHeader represents an entry from a zip file.
          55                 :  */
          56           40323 : NS_IMPL_ISUPPORTS1(nsZipHeader, nsIZipEntry)
          57                 :
          58                 : /* readonly attribute unsigned short compression; */
          59               7 : NS_IMETHODIMP nsZipHeader::GetCompression(PRUint16 *aCompression)
          60                 : {
          61               7 :     NS_ASSERTION(mInited, "Not initalised");
          62                 :
          63               7 :     *aCompression = mMethod;
          64               7 :     return NS_OK;
          65                 : }
          66                 :
          67                 : /* readonly attribute unsigned long size; */
          68               2 : NS_IMETHODIMP nsZipHeader::GetSize(PRUint32 *aSize)
          69                 : {
          70               2 :     NS_ASSERTION(mInited, "Not initalised");
          71                 :
          72               2 :     *aSize = mCSize;
          73               2 :     return NS_OK;
          74                 : }
          75                 :
          76                 : /* readonly attribute unsigned long realSize; */
          77              11 : NS_IMETHODIMP nsZipHeader::GetRealSize(PRUint32 *aRealSize)
          78                 : {
          79              11 :     NS_ASSERTION(mInited, "Not initalised");
          80                 :
          81              11 :     *aRealSize = mUSize;
          82              11 :     return NS_OK;
          83                 : }
          84                 :
          85                 : /* readonly attribute unsigned long CRC32; */
          86               9 : NS_IMETHODIMP nsZipHeader::GetCRC32(PRUint32 *aCRC32)
          87                 : {
          88               9 :     NS_ASSERTION(mInited, "Not initalised");
          89                 :
          90               9 :     *aCRC32 = mCRC;
          91               9 :     return NS_OK;
          92                 : }
          93                 :
          94                 : /* readonly attribute boolean isDirectory; */
          95               8 : NS_IMETHODIMP nsZipHeader::GetIsDirectory(bool *aIsDirectory)
          96                 : {
          97               8 :     NS_ASSERTION(mInited, "Not initalised");
          98                 :
          99               8 :     if (mName.Last() == '/')
         100               2 :         *aIsDirectory = true;
         101                 :     else
         102               6 :         *aIsDirectory = false;
         103               8 :     return NS_OK;
         104                 : }
         105                 :
         106                 : /* readonly attribute PRTime lastModifiedTime; */
         107               7 : NS_IMETHODIMP nsZipHeader::GetLastModifiedTime(PRTime *aLastModifiedTime)
         108                 : {
         109               7 :     NS_ASSERTION(mInited, "Not initalised");
         110                 :
         111                 :     // Try to read timestamp from extra field
         112                 :     PRUint16 blocksize;
         113               7 :     const PRUint8 *tsField = GetExtraField(ZIP_EXTENDED_TIMESTAMP_FIELD, false, &blocksize);
         114               7 :     if (tsField && blocksize >= 5) {
         115               7 :         PRUint32 pos = 4;
         116                 :         PRUint8 flags;
         117               7 :         flags = READ8(tsField, &pos);
         118               7 :         if (flags & ZIP_EXTENDED_TIMESTAMP_MODTIME) {
         119               7 :             *aLastModifiedTime = (PRTime)(READ32(tsField, &pos))
         120               7 :                                  * PR_USEC_PER_SEC;
         121               7 :             return NS_OK;
         122                 :         }
         123                 :     }
         124                 :
         125                 :     // Use DOS date/time fields
         126                 :     // Note that on DST shift we can't handle correctly the hour that is valid
         127                 :     // in both DST zones
         128                 :     PRExplodedTime time;
         129                 :
         130               0 :     time.tm_usec = 0;
         131                 :
         132               0 :     time.tm_hour = (mTime >> 11) & 0x1F;
         133               0 :     time.tm_min = (mTime >> 5) & 0x3F;
         134               0 :     time.tm_sec = (mTime & 0x1F) * 2;
         135                 :
         136               0 :     time.tm_year = (mDate >> 9) + 1980;
         137               0 :     time.tm_month = ((mDate >> 5) & 0x0F) - 1;
         138               0 :     time.tm_mday = mDate & 0x1F;
         139                 :
         140               0 :     time.tm_params.tp_gmt_offset = 0;
         141               0 :     time.tm_params.tp_dst_offset = 0;
         142                 :
         143               0 :     PR_NormalizeTime(&time, PR_GMTParameters);
         144               0 :     time.tm_params.tp_gmt_offset = PR_LocalTimeParameters(&time).tp_gmt_offset;
         145               0 :     PR_NormalizeTime(&time, PR_GMTParameters);
         146               0 :     time.tm_params.tp_dst_offset = PR_LocalTimeParameters(&time).tp_dst_offset;
         147                 :
         148               0 :     *aLastModifiedTime = PR_ImplodeTime(&time);
         149                 :
         150               0 :     return NS_OK;
         151                 : }
         152                 :
         153                 : /* readonly attribute boolean isSynthetic; */
         154               0 : NS_IMETHODIMP nsZipHeader::GetIsSynthetic(bool *aIsSynthetic)
         155                 : {
         156               0 :     NS_ASSERTION(mInited, "Not initalised");
         157                 :
         158               0 :     *aIsSynthetic = false;
         159               0 :     return NS_OK;
         160                 : }
         161                 :
         162            5736 : void nsZipHeader::Init(const nsACString & aPath, PRTime aDate, PRUint32 aAttr,
         163                 :                        PRUint32 aOffset)
         164                 : {
         165            5736 :     NS_ASSERTION(!mInited, "Already initalised");
         166                 :
         167                 :     PRExplodedTime time;
         168            5736 :     PR_ExplodeTime(aDate, PR_LocalTimeParameters, &time);
         169                 :
         170            5736 :     mTime = time.tm_sec / 2 + (time.tm_min << 5) + (time.tm_hour << 11);
         171                 :     mDate = time.tm_mday + ((time.tm_month + 1) << 5) +
         172            5736 :             ((time.tm_year - 1980) << 9);
         173                 :
         174                 :     // Store modification timestamp as extra field
         175                 :     // First fill CDS extra field
         176            5736 :     mFieldLength = 9;
         177           11472 :     mExtraField = new PRUint8[mFieldLength];
         178            5736 :     if (!mExtraField) {
         179               0 :         mFieldLength = 0;
         180                 :     } else {
         181            5736 :         PRUint32 pos = 0;
         182            5736 :         WRITE16(mExtraField.get(), &pos, ZIP_EXTENDED_TIMESTAMP_FIELD);
         183            5736 :         WRITE16(mExtraField.get(), &pos, 5);
         184            5736 :         WRITE8(mExtraField.get(), &pos, ZIP_EXTENDED_TIMESTAMP_MODTIME);
         185            5736 :         WRITE32(mExtraField.get(), &pos, aDate / PR_USEC_PER_SEC);
         186                 :
         187                 :         // Fill local extra field
         188           11472 :         mLocalExtraField = new PRUint8[mFieldLength];
         189            5736 :         if (mLocalExtraField) {
         190            5736 :             mLocalFieldLength = mFieldLength;
         191            5736 :             memcpy(mLocalExtraField.get(), mExtraField.get(), mLocalFieldLength);
         192                 :         }
         193                 :     }
         194                 :
         195            5736 :     mEAttr = aAttr;
         196            5736 :     mOffset = aOffset;
         197            5736 :     mName = aPath;
         198            5736 :     mComment = NS_LITERAL_CSTRING("");
         199                 :     // Claim a UTF-8 path in case it needs it.
         200            5736 :     mFlags |= FLAGS_IS_UTF8;
         201            5736 :     mInited = true;
         202            5736 : }
         203                 :
         204           11467 : PRUint32 nsZipHeader::GetFileHeaderLength()
         205                 : {
         206           11467 :     return ZIP_FILE_HEADER_SIZE + mName.Length() + mLocalFieldLength;
         207                 : }
         208                 :
         209           11465 : nsresult nsZipHeader::WriteFileHeader(nsIOutputStream *aStream)
         210                 : {
         211           11465 :     NS_ASSERTION(mInited, "Not initalised");
         212                 :
         213                 :     PRUint8 buf[ZIP_FILE_HEADER_SIZE];
         214           11465 :     PRUint32 pos = 0;
         215           11465 :     WRITE32(buf, &pos, ZIP_FILE_HEADER_SIGNATURE);
         216           11465 :     WRITE16(buf, &pos, mVersionNeeded);
         217           11465 :     WRITE16(buf, &pos, mFlags);
         218           11465 :     WRITE16(buf, &pos, mMethod);
         219           11465 :     WRITE16(buf, &pos, mTime);
         220           11465 :     WRITE16(buf, &pos, mDate);
         221           11465 :     WRITE32(buf, &pos, mCRC);
         222           11465 :     WRITE32(buf, &pos, mCSize);
         223           11465 :     WRITE32(buf, &pos, mUSize);
         224           11465 :     WRITE16(buf, &pos, mName.Length());
         225           11465 :     WRITE16(buf, &pos, mLocalFieldLength);
         226                 :
         227           11465 :     nsresult rv = ZW_WriteData(aStream, (const char *)buf, pos);
         228           11465 :     NS_ENSURE_SUCCESS(rv, rv);
         229                 :
         230           11465 :     rv = ZW_WriteData(aStream, mName.get(), mName.Length());
         231           11465 :     NS_ENSURE_SUCCESS(rv, rv);
         232                 :
         233           11465 :     if (mLocalFieldLength)
         234                 :     {
         235           11465 :       rv = ZW_WriteData(aStream, (const char *)mLocalExtraField.get(), mLocalFieldLength);
         236           11465 :       NS_ENSURE_SUCCESS(rv, rv);
         237                 :     }
         238                 :
         239           11465 :     return NS_OK;
         240                 : }
         241                 :
         242            5736 : PRUint32 nsZipHeader::GetCDSHeaderLength()
         243                 : {
         244            5736 :     return ZIP_CDS_HEADER_SIZE + mName.Length() + mComment.Length() +
         245            5736 :            mFieldLength;
         246                 : }
         247                 :
         248            5736 : nsresult nsZipHeader::WriteCDSHeader(nsIOutputStream *aStream)
         249                 : {
         250            5736 :     NS_ASSERTION(mInited, "Not initalised");
         251                 :
         252                 :     PRUint8 buf[ZIP_CDS_HEADER_SIZE];
         253            5736 :     PRUint32 pos = 0;
         254            5736 :     WRITE32(buf, &pos, ZIP_CDS_HEADER_SIGNATURE);
         255            5736 :     WRITE16(buf, &pos, mVersionMade);
         256            5736 :     WRITE16(buf, &pos, mVersionNeeded);
         257            5736 :     WRITE16(buf, &pos, mFlags);
         258            5736 :     WRITE16(buf, &pos, mMethod);
         259            5736 :     WRITE16(buf, &pos, mTime);
         260            5736 :     WRITE16(buf, &pos, mDate);
         261            5736 :     WRITE32(buf, &pos, mCRC);
         262            5736 :     WRITE32(buf, &pos, mCSize);
         263            5736 :     WRITE32(buf, &pos, mUSize);
         264            5736 :     WRITE16(buf, &pos, mName.Length());
         265            5736 :     WRITE16(buf, &pos, mFieldLength);
         266            5736 :     WRITE16(buf, &pos, mComment.Length());
         267            5736 :     WRITE16(buf, &pos, mDisk);
         268            5736 :     WRITE16(buf, &pos, mIAttr);
         269            5736 :     WRITE32(buf, &pos, mEAttr);
         270            5736 :     WRITE32(buf, &pos, mOffset);
         271                 :
         272            5736 :     nsresult rv = ZW_WriteData(aStream, (const char *)buf, pos);
         273            5736 :     NS_ENSURE_SUCCESS(rv, rv);
         274                 :
         275            5736 :     rv = ZW_WriteData(aStream, mName.get(), mName.Length());
         276            5736 :     NS_ENSURE_SUCCESS(rv, rv);
         277            5736 :     if (mExtraField) {
         278            5736 :         rv = ZW_WriteData(aStream, (const char *)mExtraField.get(), mFieldLength);
         279            5736 :         NS_ENSURE_SUCCESS(rv, rv);
         280                 :     }
         281            5736 :     return ZW_WriteData(aStream, mComment.get(), mComment.Length());
         282                 : }
         283                 :
         284               9 : nsresult nsZipHeader::ReadCDSHeader(nsIInputStream *stream)
         285                 : {
         286               9 :     NS_ASSERTION(!mInited, "Already initalised");
         287                 :
         288                 :     PRUint8 buf[ZIP_CDS_HEADER_SIZE];
         289                 :
         290               9 :     nsresult rv = ZW_ReadData(stream, (char *)buf, ZIP_CDS_HEADER_SIZE);
         291               9 :     NS_ENSURE_SUCCESS(rv, rv);
         292                 :
         293               9 :     PRUint32 pos = 0;
         294               9 :     PRUint32 signature = READ32(buf, &pos);
         295               9 :     if (signature != ZIP_CDS_HEADER_SIGNATURE)
         296               0 :         return NS_ERROR_FILE_CORRUPTED;
         297                 :
         298               9 :     mVersionMade = READ16(buf, &pos);
         299               9 :     mVersionNeeded = READ16(buf, &pos);
         300               9 :     mFlags = READ16(buf, &pos);
         301               9 :     mMethod = READ16(buf, &pos);
         302               9 :     mTime = READ16(buf, &pos);
         303               9 :     mDate = READ16(buf, &pos);
         304               9 :     mCRC = READ32(buf, &pos);
         305               9 :     mCSize = READ32(buf, &pos);
         306               9 :     mUSize = READ32(buf, &pos);
         307               9 :     PRUint16 namelength = READ16(buf, &pos);
         308               9 :     mFieldLength = READ16(buf, &pos);
         309               9 :     PRUint16 commentlength = READ16(buf, &pos);
         310               9 :     mDisk = READ16(buf, &pos);
         311               9 :     mIAttr = READ16(buf, &pos);
         312               9 :     mEAttr = READ32(buf, &pos);
         313               9 :     mOffset = READ32(buf, &pos);
         314                 :
         315               9 :     if (namelength > 0) {
         316              27 :         nsAutoArrayPtr<char> field(new char[namelength]);
         317               9 :         NS_ENSURE_TRUE(field, NS_ERROR_OUT_OF_MEMORY);
         318               9 :         rv = ZW_ReadData(stream, field.get(), namelength);
         319               9 :         NS_ENSURE_SUCCESS(rv, rv);
         320              18 :         mName.Assign(field, namelength);
         321                 :     }
         322                 :     else
         323               0 :         mName = NS_LITERAL_CSTRING("");
         324                 :
         325               9 :     if (mFieldLength > 0) {
         326              18 :         mExtraField = new PRUint8[mFieldLength];
         327               9 :         NS_ENSURE_TRUE(mExtraField, NS_ERROR_OUT_OF_MEMORY);
         328               9 :         rv = ZW_ReadData(stream, (char *)mExtraField.get(), mFieldLength);
         329               9 :         NS_ENSURE_SUCCESS(rv, rv);
         330                 :     }
         331                 :
         332               9 :     if (commentlength > 0) {
         333               0 :         nsAutoArrayPtr<char> field(new char[commentlength]);
         334               0 :         NS_ENSURE_TRUE(field, NS_ERROR_OUT_OF_MEMORY);
         335               0 :         rv = ZW_ReadData(stream, field.get(), commentlength);
         336               0 :         NS_ENSURE_SUCCESS(rv, rv);
         337               0 :         mComment.Assign(field, commentlength);
         338                 :     }
         339                 :     else
         340               9 :         mComment = NS_LITERAL_CSTRING("");
         341                 :
         342               9 :     mInited = true;
         343               9 :     return NS_OK;
         344                 : }
         345                 :
         346               7 : const PRUint8 * nsZipHeader::GetExtraField(PRUint16 aTag, bool aLocal, PRUint16 *aBlockSize)
         347                 : {
         348               7 :     const PRUint8 *buf = aLocal ? mLocalExtraField : mExtraField;
         349               7 :     PRUint32 buflen = aLocal ? mLocalFieldLength : mFieldLength;
         350               7 :     PRUint32 pos = 0;
         351                 :     PRUint16 tag, blocksize;
         352                 :
         353              14 :     while (buf && (pos + 4) <= buflen) {
         354               7 :       tag = READ16(buf, &pos);
         355               7 :       blocksize = READ16(buf, &pos);
         356                 :
         357               7 :       if (aTag == tag && (pos + blocksize) <= buflen) {
         358               7 :         *aBlockSize = blocksize;
         359               7 :         return buf + pos - 4;
         360                 :       }
         361                 :
         362               0 :       pos += blocksize;
         363                 :     }
         364                 :
         365               0 :     return NULL;
         366                 : }

Generated by: LCOV version 1.7