📄 stafzipcentraldirextension.cpp
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2004 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/#include "STAF.h"#include "STAFString.h"#include "STAFTrace.h"#include <sys/stat.h>#include <vector>#include <map>#include "STAFZip.h"#include "STAFZipUtil.h"#include "STAFZipFileAttribute.h"#include "STAFZipCentralDirExtension.h"// constructorSTAFZipCentralDirExtension::STAFZipCentralDirExtension(){ signature = 0x01024653; // STAF offset = 0; size = 4;}// read in central dir entension dataSTAFRC_t STAFZipCentralDirExtension::readInData(FILE *zf, uLong centralDirOffset, uLong endOfLastLocalFileHeaderOffset, STAFString *result){ // search for central dir extension section uLong cdeSize = centralDirOffset - endOfLastLocalFileHeaderOffset; if (cdeSize == 0) { return kSTAFOk; } unsigned char* buf; buf = (unsigned char*)malloc(cdeSize); if (buf == NULL) { *result = STAFString("STAFZipCentralDirExtension::readInData: ") + "Error allocating memory for reading in central dir extension [" + (cdeSize) + "].\n"; return kZIPNotEnoughMemory; } if (fseek(zf, endOfLastLocalFileHeaderOffset, SEEK_SET) != 0) { *result = STAFString("STAFZipCentralDirExtension::readInData: ") + "Error in fseek to the end of last local file header offset [" + endOfLastLocalFileHeaderOffset + "]\n"; return kZIPGeneralZipError; } // read in a block of data from the file if (fread(buf, (uInt)cdeSize, 1, zf) != 1) { *result = STAFString("STAFZipCentralDirExtension::readInData: ") + "Error reading central dir extension data size [" + cdeSize + "].\n"; return kZIPGeneralZipError; } // search for central dir extension signature for (int i = (int)cdeSize - 3; (i--) > 0;) { if (((*(buf + i)) == 0x53) && ((*(buf + i + 1)) == 0x46) && ((*(buf + i + 2)) == 0x02) && ((*(buf + i + 3))== 0x01)) { offset = endOfLastLocalFileHeaderOffset + i; size = centralDirOffset - offset; break; } } if (offset == 0) { return kSTAFOk; } STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDirExtension::readInData_CP1") + " offset [" + offset + " size [" + size + "]"); STAFZipUtil util = STAFZipUtil(); uLong sig; STAFRC_t rc; if (fseek(zf, offset, SEEK_SET) != 0) { *result = STAFString("STAFZipCentralDirExtension::readInData: ") + "Error in fseek to the beginning of central dir extension [" + offset + "]\n"; return kZIPGeneralZipError; } // get signature rc = util.getLong(zf, &sig); if (rc == kSTAFOk) { if (sig != signature) { *result = STAFString("STAFZipCentralDirExtension::readInData: ") + "Wrong signature [" + sig + "].\n"; return kZIPGeneralZipError; } } else { *result = STAFString("STAFZipCentralDirExtension::readInData: ") + "Error getting signature [" + sig + "].\n"; return rc; } long ul = (long)size; ul -= 4; // while there is still data in central dir extension block while (ul > 0) { STAFZipFileAttribute *fa = new STAFZipFileAttribute(); // get filename length if (rc == kSTAFOk) { uLong iL; rc = util.getShort(zf, &iL); fa->filename_length = (unsigned short)iL; } STAFTrace::trace(kSTAFTraceServiceResult, STAFString("STAFZipCentralDirExtension::readInData_CP4" ) + " fa->filename_length [" + fa->filename_length + " rc [" + rc + "]"); if (rc == kSTAFOk && fa->filename_length <= 0) { *result = STAFString("STAFZipCentralDirExtension::readInData: ") + "filename_length less than zero [" + fa->filename_length + "].\n"; rc = kZIPGeneralZipError; } // get filename if (rc == kSTAFOk) { if ((fa->filename = (char*)calloc(fa->filename_length + 1, 1)) != NULL) { if (fread(fa->filename, (uInt)fa->filename_length, 1, zf) != 1 ) { *result = STAFString( "STAFZipCentralDirExtension::readInData: ") + "Error getting file name.\n"; rc = kZIPGeneralZipError; } else { STAFTrace::trace(kSTAFTraceServiceResult, STAFString( "STAFZipCentralDirExtension::readInData_CP7") + " fa->filename [" + fa->filename + "]"); } } else { *result = STAFString("STAFZipCentralDirExtension::readInData: ") + "Error allocating memory for file name length [" + (fa->filename_length + 1) + "].\n"; rc = kZIPNotEnoughMemory; } } // get mode if (rc == kSTAFOk) { uLong iL; rc = util.getLong(zf, &iL); fa->mode = (mode_t)iL; STAFTrace::trace(kSTAFTraceServiceResult, STAFString( "STAFZipCentralDirExtension::readInData_CP9") + " fa->mode [" + fa->mode + " rc" + rc + "]"); } // get owner if (rc == kSTAFOk) { uLong iL; rc = util.getLong(zf, &iL); fa->owner = (uid_t)iL; STAFTrace::trace(kSTAFTraceServiceResult, STAFString( "STAFZipCentralDirExtension::readInData_CP10") + " fa->owner [" + fa->owner + " rc" + rc + "]"); } // get group if (rc == kSTAFOk) { uLong iL; rc = util.getLong(zf, &iL); fa->group = (gid_t)iL; STAFTrace::trace(kSTAFTraceServiceResult, STAFString( "STAFZipCentralDirExtension::readInData_CP11") + " fa->group [" + fa->group + " rc" + rc + "]"); } if (rc != kSTAFOk) { delete fa; if ((*result).length() == 0) { *result = STAFString("STAFZipCentralDirExtension::readInData: ") + "Error getting data.\n"; } break; } // reduce the size counter by one record ul -= 2 + fa->filename_length + sizeof(uLong) + sizeof(uLong) + sizeof(uLong); STAFTrace::trace(kSTAFTraceServiceResult, STAFString( "STAFZipCentralDirExtension::readInData_CP13") + " ul [" + ul + "]"); falist.push_back(fa); falistSorted[STAFString(fa->filename)] = fa; } return rc;}// flush the central dir extension to zip archive
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -