| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#ifndef __LIBS_ZIPENTRY_H |
| 23 |
#define __LIBS_ZIPENTRY_H |
| 24 |
|
| 25 |
#include <utils/Errors.h> |
| 26 |
|
| 27 |
#include <stdlib.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <time.h> |
| 30 |
|
| 31 |
namespace android { |
| 32 |
|
| 33 |
class ZipFile; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class ZipEntry { |
| 48 |
public: |
| 49 |
friend class ZipFile; |
| 50 |
|
| 51 |
ZipEntry(void) |
| 52 |
: mDeleted(false), mMarked(false) |
| 53 |
{} |
| 54 |
~ZipEntry(void) {} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
bool isCompressed(void) const { |
| 60 |
return mCDE.mCompressionMethod != kCompressStored; |
| 61 |
} |
| 62 |
int getCompressionMethod(void) const { return mCDE.mCompressionMethod; } |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
off_t getUncompressedLen(void) const { return mCDE.mUncompressedSize; } |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
off_t getCompressedLen(void) const { return mCDE.mCompressedSize; } |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
off_t getLFHOffset(void) const { return mCDE.mLocalHeaderRelOffset; } |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
off_t getFileOffset(void) const { |
| 85 |
return mCDE.mLocalHeaderRelOffset + |
| 86 |
LocalFileHeader::kLFHLen + |
| 87 |
mLFH.mFileNameLength + |
| 88 |
mLFH.mExtraFieldLength; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
unsigned long getCRC32(void) const { return mCDE.mCRC32; } |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
time_t getModWhen(void) const; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
const char* getFileName(void) const { return (const char*) mCDE.mFileName; } |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
bool getMarked(void) const { return mMarked; } |
| 111 |
void setMarked(bool val) { mMarked = val; } |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
static inline unsigned short getShortLE(const unsigned char* buf) { |
| 118 |
return buf[0] | (buf[1] << 8); |
| 119 |
} |
| 120 |
static inline unsigned long getLongLE(const unsigned char* buf) { |
| 121 |
return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); |
| 122 |
} |
| 123 |
static inline void putShortLE(unsigned char* buf, short val) { |
| 124 |
buf[0] = (unsigned char) val; |
| 125 |
buf[1] = (unsigned char) (val >> 8); |
| 126 |
} |
| 127 |
static inline void putLongLE(unsigned char* buf, long val) { |
| 128 |
buf[0] = (unsigned char) val; |
| 129 |
buf[1] = (unsigned char) (val >> 8); |
| 130 |
buf[2] = (unsigned char) (val >> 16); |
| 131 |
buf[3] = (unsigned char) (val >> 24); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
enum { |
| 136 |
kCompressStored = 0, |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
kCompressDeflated = 8, |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
bool getDeleted(void) const { return mDeleted; } |
| 156 |
|
| 157 |
protected: |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
status_t initFromCDE(FILE* fp); |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
void initNew(const char* fileName, const char* comment); |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
status_t initFromExternal(const ZipFile* pZipFile, const ZipEntry* pEntry); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
status_t addPadding(int padding); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
void setDataInfo(long uncompLen, long compLen, unsigned long crc32, |
| 187 |
int compressionMethod); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
void setModWhen(time_t when); |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
void setLFHOffset(off_t offset) { |
| 199 |
mCDE.mLocalHeaderRelOffset = (long) offset; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
void setDeleted(void) { mDeleted = true; } |
| 204 |
|
| 205 |
private: |
| 206 |
|
| 207 |
ZipEntry(const ZipEntry& src); |
| 208 |
ZipEntry& operator=(const ZipEntry& src); |
| 209 |
|
| 210 |
|
| 211 |
bool compareHeaders(void) const; |
| 212 |
void copyCDEtoLFH(void); |
| 213 |
|
| 214 |
bool mDeleted; |
| 215 |
bool mMarked; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
class LocalFileHeader { |
| 221 |
public: |
| 222 |
LocalFileHeader(void) : |
| 223 |
mVersionToExtract(0), |
| 224 |
mGPBitFlag(0), |
| 225 |
mCompressionMethod(0), |
| 226 |
mLastModFileTime(0), |
| 227 |
mLastModFileDate(0), |
| 228 |
mCRC32(0), |
| 229 |
mCompressedSize(0), |
| 230 |
mUncompressedSize(0), |
| 231 |
mFileNameLength(0), |
| 232 |
mExtraFieldLength(0), |
| 233 |
mFileName(NULL), |
| 234 |
mExtraField(NULL) |
| 235 |
{} |
| 236 |
virtual ~LocalFileHeader(void) { |
| 237 |
delete[] mFileName; |
| 238 |
delete[] mExtraField; |
| 239 |
} |
| 240 |
|
| 241 |
status_t read(FILE* fp); |
| 242 |
status_t write(FILE* fp); |
| 243 |
|
| 244 |
|
| 245 |
unsigned short mVersionToExtract; |
| 246 |
unsigned short mGPBitFlag; |
| 247 |
unsigned short mCompressionMethod; |
| 248 |
unsigned short mLastModFileTime; |
| 249 |
unsigned short mLastModFileDate; |
| 250 |
unsigned long mCRC32; |
| 251 |
unsigned long mCompressedSize; |
| 252 |
unsigned long mUncompressedSize; |
| 253 |
unsigned short mFileNameLength; |
| 254 |
unsigned short mExtraFieldLength; |
| 255 |
unsigned char* mFileName; |
| 256 |
unsigned char* mExtraField; |
| 257 |
|
| 258 |
enum { |
| 259 |
kSignature = 0x04034b50, |
| 260 |
kLFHLen = 30, |
| 261 |
}; |
| 262 |
|
| 263 |
void dump(void) const; |
| 264 |
}; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
class CentralDirEntry { |
| 271 |
public: |
| 272 |
CentralDirEntry(void) : |
| 273 |
mVersionMadeBy(0), |
| 274 |
mVersionToExtract(0), |
| 275 |
mGPBitFlag(0), |
| 276 |
mCompressionMethod(0), |
| 277 |
mLastModFileTime(0), |
| 278 |
mLastModFileDate(0), |
| 279 |
mCRC32(0), |
| 280 |
mCompressedSize(0), |
| 281 |
mUncompressedSize(0), |
| 282 |
mFileNameLength(0), |
| 283 |
mExtraFieldLength(0), |
| 284 |
mFileCommentLength(0), |
| 285 |
mDiskNumberStart(0), |
| 286 |
mInternalAttrs(0), |
| 287 |
mExternalAttrs(0), |
| 288 |
mLocalHeaderRelOffset(0), |
| 289 |
mFileName(NULL), |
| 290 |
mExtraField(NULL), |
| 291 |
mFileComment(NULL) |
| 292 |
{} |
| 293 |
virtual ~CentralDirEntry(void) { |
| 294 |
delete[] mFileName; |
| 295 |
delete[] mExtraField; |
| 296 |
delete[] mFileComment; |
| 297 |
} |
| 298 |
|
| 299 |
status_t read(FILE* fp); |
| 300 |
status_t write(FILE* fp); |
| 301 |
|
| 302 |
CentralDirEntry& operator=(const CentralDirEntry& src); |
| 303 |
|
| 304 |
|
| 305 |
unsigned short mVersionMadeBy; |
| 306 |
unsigned short mVersionToExtract; |
| 307 |
unsigned short mGPBitFlag; |
| 308 |
unsigned short mCompressionMethod; |
| 309 |
unsigned short mLastModFileTime; |
| 310 |
unsigned short mLastModFileDate; |
| 311 |
unsigned long mCRC32; |
| 312 |
unsigned long mCompressedSize; |
| 313 |
unsigned long mUncompressedSize; |
| 314 |
unsigned short mFileNameLength; |
| 315 |
unsigned short mExtraFieldLength; |
| 316 |
unsigned short mFileCommentLength; |
| 317 |
unsigned short mDiskNumberStart; |
| 318 |
unsigned short mInternalAttrs; |
| 319 |
unsigned long mExternalAttrs; |
| 320 |
unsigned long mLocalHeaderRelOffset; |
| 321 |
unsigned char* mFileName; |
| 322 |
unsigned char* mExtraField; |
| 323 |
unsigned char* mFileComment; |
| 324 |
|
| 325 |
void dump(void) const; |
| 326 |
|
| 327 |
enum { |
| 328 |
kSignature = 0x02014b50, |
| 329 |
kCDELen = 46, |
| 330 |
}; |
| 331 |
}; |
| 332 |
|
| 333 |
enum { |
| 334 |
|
| 335 |
kDataDescriptorLen = 16, |
| 336 |
|
| 337 |
kDefaultVersion = 20, |
| 338 |
kDefaultMadeBy = 0x0317, |
| 339 |
kUsesDataDescr = 0x0008, |
| 340 |
}; |
| 341 |
|
| 342 |
LocalFileHeader mLFH; |
| 343 |
CentralDirEntry mCDE; |
| 344 |
}; |
| 345 |
|
| 346 |
}; |
| 347 |
|
| 348 |
#endif |