Home | History | Annotate | Download | only in ziptime
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 //
     18 // Zip archive entries.
     19 //
     20 // The ZipEntry class is tightly meshed with the ZipFile class.
     21 //
     22 #ifndef __LIBS_ZIPENTRY_H
     23 #define __LIBS_ZIPENTRY_H
     24 
     25 #include <stdlib.h>
     26 #include <stdint.h>
     27 #include <stdio.h>
     28 
     29 typedef int status_t;
     30 
     31 namespace android {
     32 
     33 class ZipFile;
     34 
     35 /*
     36  * ZipEntry objects represent a single entry in a Zip archive.
     37  *
     38  * File information is stored in two places: next to the file data (the Local
     39  * File Header, and possibly a Data Descriptor), and at the end of the file
     40  * (the Central Directory Entry).  The two must be kept in sync.
     41  */
     42 class ZipEntry {
     43 public:
     44     friend class ZipFile;
     45 
     46     ZipEntry(void) {}
     47     ~ZipEntry(void) {}
     48 
     49     /*
     50      * Some basic functions for raw data manipulation.  "LE" means
     51      * Little Endian.
     52      */
     53     static inline uint16_t getShortLE(const uint8_t* buf) {
     54         return buf[0] | (buf[1] << 8);
     55     }
     56     static inline uint32_t getLongLE(const uint8_t* buf) {
     57         return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
     58     }
     59     static inline void putShortLE(uint8_t* buf, uint16_t val) {
     60         buf[0] = (uint8_t) val;
     61         buf[1] = (uint8_t) (val >> 8);
     62     }
     63 
     64 protected:
     65     /*
     66      * Initialize the structure from the file, which is pointing at
     67      * our Central Directory entry. And rewrite it.
     68      */
     69     status_t initAndRewriteFromCDE(FILE* fp);
     70 
     71 private:
     72     /* these are private and not defined */
     73     ZipEntry(const ZipEntry& src);
     74     ZipEntry& operator=(const ZipEntry& src);
     75 
     76     /*
     77      * Every entry in the Zip archive starts off with one of these.
     78      */
     79     class LocalFileHeader {
     80     public:
     81         LocalFileHeader(void) {}
     82 
     83         status_t rewrite(FILE* fp);
     84 
     85         enum {
     86             kSignature      = 0x04034b50,
     87             kLFHLen         = 30,       // LocalFileHdr len, excl. var fields
     88         };
     89     };
     90 
     91     /*
     92      * Every entry in the Zip archive has one of these in the "central
     93      * directory" at the end of the file.
     94      */
     95     class CentralDirEntry {
     96     public:
     97         CentralDirEntry(void) :
     98             mLocalHeaderRelOffset(0)
     99         {}
    100 
    101         status_t rewrite(FILE* fp);
    102 
    103         uint32_t mLocalHeaderRelOffset;
    104 
    105         enum {
    106             kSignature      = 0x02014b50,
    107             kCDELen         = 46,       // CentralDirEnt len, excl. var fields
    108         };
    109     };
    110 
    111     LocalFileHeader     mLFH;
    112     CentralDirEntry     mCDE;
    113 };
    114 
    115 }; // namespace android
    116 
    117 #endif // __LIBS_ZIPENTRY_H
    118