Skip to content

Commit 4cb5259

Browse files
authored
Merge pull request #93 from sisong/dev
fix ApkNormalized error & recode
2 parents a96f8cd + a787996 commit 4cb5259

File tree

13 files changed

+125
-91
lines changed

13 files changed

+125
-91
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# [ApkDiffPatch]
2-
[![release](https://img.shields.io/badge/release-v1.7.0-blue.svg)](https://github.com/sisong/ApkDiffPatch/releases)
2+
[![release](https://img.shields.io/badge/release-v1.7.1-blue.svg)](https://github.com/sisong/ApkDiffPatch/releases)
33
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/sisong/ApkDiffPatch/blob/master/LICENSE)
44
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-blue.svg)](https://github.com/sisong/ApkDiffPatch/pulls)
55
[![+issue Welcome](https://img.shields.io/github/issues-raw/sisong/ApkDiffPatch?color=green&label=%2Bissue%20welcome)](https://github.com/sisong/ApkDiffPatch/issues)

src/ZiPatExtraDemo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ int extra_cmd_line(int argc, const char * argv[]) {
7373
srcZiPatPath =argv[1];
7474
outZiPatPath =argv[2];
7575
const char* appendData=argv[3];//NOTE: modify this for your require
76-
printf(" src ZiPat :\"%s\"\n",srcZiPatPath);
77-
printf(" out ZiPat :\"%s\"\n",outZiPatPath);
78-
printf("test append:\"%s\"\n",appendData);
76+
hpatch_printPath_utf8((std::string("src ZiPat : \"")+srcZiPatPath+"\"\n").c_str());
77+
hpatch_printPath_utf8((std::string("out ZiPat : \"")+outZiPatPath+"\"\n").c_str());
78+
hpatch_printPath_utf8((std::string("test append: \"")+appendData+"\"\n").c_str());
7979
if (!(addToExtra(srcZiPatPath,outZiPatPath,(const TByte*)appendData,strlen(appendData)))){
8080
return _kRET_ERROR;
8181
}

src/apk_normalized.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static void printUsage(){
5151
" align file data offset in zip file (compatible with AndroidSDK#zipalign),\n"
5252
" remove all data descriptor, reserve & normalized Extra field and Comment,\n"
5353
" compatible with jar sign(apk v1 sign), etc...\n"
54-
" if apk file used apk v2 sign, must re sign apk file after ApkNormalized;\n"
54+
" if apk file used apk v2 sign, must re-sign apk file after ApkNormalized;\n"
5555
" release signedApk:=AndroidSDK#apksigner(normalizedApk)\n"
5656
" -cl-compressLevel\n"
5757
" set zlib compress level [0..9], recommended 4,5,6, DEFAULT -cl-6;\n"
@@ -73,6 +73,7 @@ static void printUsage(){
7373
" WARNING: if have compressed empty file,\n"
7474
" it can't patch by old(version<v1.3.5) ZipPatch!\n"
7575
" -nce-1 DEFAULT, not compress all empty file.\n"
76+
" -q quiet mode, don't print fileName\n"
7677
" -v output Version info. \n"
7778
);
7879
}
@@ -128,6 +129,10 @@ int normalized_cmd_line(int argc, const char * argv[]){
128129
_options_check((isOutputVersion==_kNULL_VALUE)&&(op[2]=='\0'),"-v");
129130
isOutputVersion=hpatch_TRUE;
130131
} break;
132+
case 'q':{
133+
_options_check((op[2]=='\0'),"-q");
134+
g_isPrintApkNormalizedFileName=false;
135+
} break;
131136
case 'n':{
132137
if ((op[2]=='c')&&(op[3]=='e')&&(op[4]=='-')&&((op[5]=='0')||(op[5]=='1'))){
133138
_options_check(isNotCompressEmptyFile==_kNULL_VALUE,"-nce-?");
@@ -187,7 +192,8 @@ int normalized_cmd_line(int argc, const char * argv[]){
187192
_options_check(arg_values_size==2,"count");
188193
const char* srcApk=arg_values[0];
189194
const char* dstApk=arg_values[1];
190-
printf("src: \"%s\"\nout: \"%s\"\n",srcApk,dstApk);
195+
hpatch_printPath_utf8((std::string("src: \"")+srcApk+"\"\n").c_str());
196+
hpatch_printPath_utf8((std::string("out: \"")+dstApk+"\"\n").c_str());
191197
double time0=clock_s();
192198
int apkFilesRemoved=0;
193199
if (!ZipNormalized(srcApk,dstApk,(int)alignSize,(int)compressLevel,

src/diff/DiffData.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ bool getSamePairList(UnZipper* newZip,UnZipper* oldZip,
270270
if (zipFile_name(newZip,i)==zipFile_name(oldZip,oldIndex)) break;
271271
}else{
272272
printf("WARNING: crc32 equal but data not equal! file index: %d,%d\n",i,oldIndex);
273-
printf(" name:\"%s\"\n \"%s\"\n",zipFile_name(newZip,i).c_str(),
274-
zipFile_name(oldZip,oldIndex).c_str());
273+
hpatch_printPath_utf8((" name: \""+zipFile_name(newZip,i)+"\"\n").c_str());
274+
hpatch_printPath_utf8((" \""+zipFile_name(oldZip,oldIndex)+"\"\n").c_str());
275275
}
276276
}
277277
if (findSame){

src/diff/Differ.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ static bool checkZipInfo(UnZipper* oldZip,UnZipper* newZip){
308308
}
309309
if ((!newIsV2Sign)&&UnZipper_isHaveApkV2orV3SignTag_in_ApkV1SignFile(newZip)){
310310
//maybe bring apk can't install ERROR!
311-
printf(" ERROR: newZip fond \"X-Android-APK-Signed: 2(or 3...)\" in ApkV1Sign file, need re sign "
311+
printf(" ERROR: newZip fond \"X-Android-APK-Signed: 2(or 3...)\" in ApkV1Sign file, need re-sign "
312312
"newZip:=AndroidSDK#apksigner(newZip) before running ZipDiff!\n");
313313
isOk=false;
314314
}

src/normalized/normalized.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
#include <algorithm> //sort
3232
#include "../patch/Zipper.h"
3333
#include "../diff/DiffData.h"
34+
#ifdef __cplusplus
35+
extern "C" {
36+
#endif
37+
bool g_isPrintApkNormalizedFileName=true;
38+
#ifdef __cplusplus
39+
}
40+
#endif
3441

3542
#define check(value) { \
3643
if (!(value)){ printf(#value" ERROR!\n"); \
@@ -150,7 +157,7 @@ bool ZipNormalized(const char* srcApk,const char* dstApk,int ZipAlignSize,int co
150157
getAllFiles(&unzipper,files);
151158
apkFilesRemoved=removeNonEmptyDirs(&unzipper,files);
152159
fileCount=(int)files.size();
153-
std::sort(files.begin(),files.end(),TFileValue::TCmp(fileCount));
160+
std::sort(files.begin(),files.end(),TFileValue::TCmp(UnZipper_fileCount(&unzipper)));
154161
for (int i=0; i<fileCount; ++i) {
155162
int fileIndex=files[i].fileIndex;
156163
if (UnZipper_file_isApkV1Sign(&unzipper,fileIndex)){
@@ -177,13 +184,15 @@ bool ZipNormalized(const char* srcApk,const char* dstApk,int ZipAlignSize,int co
177184
for (int i=0; i<(int)fileIndexs.size(); ++i) {
178185
int fileIndex=fileIndexs[i];
179186
std::string fileName=zipFile_name(&unzipper,fileIndex);
180-
printf("\"%s\"\n",fileName.c_str());
187+
if (g_isPrintApkNormalizedFileName)
188+
hpatch_printPath_utf8(("\""+fileName+"\"\n").c_str());
181189
if (compressLevel==0){
182190
check(Zipper_file_append_set_new_isCompress(&zipper,false));
183191
} else if (isCompressedEmptyFile(&unzipper,fileIndex)){
184192
if (isNotCompressEmptyFile){
185193
check(Zipper_file_append_set_new_isCompress(&zipper,false));
186-
printf("NOTE: \"%s\" is a compressed empty file, change to uncompressed!\n",fileName.c_str());
194+
hpatch_printPath_utf8(("NOTE: \""+fileName+"\"").c_str());
195+
printf(" is a compressed empty file, change to uncompressed!\n");
187196
}else{
188197
_compressedEmptyFiles.push_back(fileName);
189198
}
@@ -206,21 +215,22 @@ bool ZipNormalized(const char* srcApk,const char* dstApk,int ZipAlignSize,int co
206215
check(Zipper_endCentralDirectory_append(&zipper,&unzipper));
207216

208217
for (int i=0;i<(int)_compressedEmptyFiles.size();++i){
209-
printf("WARNING: \"%s\" is a compressed empty file, can't patch by old(version<v1.3.5) ZipPatch!)\n",_compressedEmptyFiles[i].c_str());
218+
hpatch_printPath_utf8(("WARNING: \""+_compressedEmptyFiles[i]).c_str());
219+
printf(" is a compressed empty file, can't patch by old(version<v1.3.5) ZipPatch!)\n");
210220
}
211221
if (jarSignFileCount>0){
212222
if (isHaveApkV2Sign){
213-
printf("WARNING: src removed JarSign(ApkV1Sign) (%d file, need re sign)\n",jarSignFileCount);
223+
printf("WARNING: src removed JarSign(ApkV1Sign) (%d file, need re-sign)\n",jarSignFileCount);
214224
}else{
215-
printf("NOTE: src found JarSign(ApkV1Sign) (%d file)\n",jarSignFileCount);
225+
printf("WARNING: src JarSign(ApkV1Sign) (%d file) has been Normalized, Don't re-sign!\n",jarSignFileCount);
216226
}
217227
}
218228
for (size_t i=0;i<removedFiles.size();++i)
219-
printf("WARNING: removed file: %s\n",removedFiles[i].c_str());
229+
hpatch_printPath_utf8((std::string("WARNING: removed file: \"")+removedFiles[i]+"\"\n").c_str());
220230
if (isHaveApkV2Sign){
221231
printf(isHaveApkV3Sign?
222-
"WARNING: src removed ApkV2Sign & ApkV3Sign data (%d Byte, need re sign)\n"
223-
:"WARNING: src removed ApkV2Sign data (%d Byte, need re sign)\n",
232+
"WARNING: src removed ApkV2Sign & ApkV3Sign data (%d Byte, need re-sign)\n"
233+
:"WARNING: src removed ApkV2Sign data (%d Byte, need re-sign)\n",
224234
(int)UnZipper_ApkV2SignSize(&unzipper));
225235
}
226236
if (zipper._normalizeSoPageAlignCount>0)

src/normalized/normalized.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,11 @@
3131
//规范化zip包;
3232
bool ZipNormalized(const char* srcApk,const char* dstApk,int ZipAlignSize,int compressLevel,
3333
bool isNotCompressEmptyFile=true,bool isPageAlignSoFile=true,int* out_apkFilesRemoved=0);
34-
34+
#ifdef __cplusplus
35+
extern "C" {
36+
#endif
37+
extern bool g_isPrintApkNormalizedFileName; //default true
38+
#ifdef __cplusplus
39+
}
40+
#endif
3541
#endif //ZipNormalized_normalized_h

src/patch/NewStream.cpp

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -135,63 +135,66 @@ static hpatch_BOOL _NewStream_write(const hpatch_TStreamOutput* stream,
135135
check(self->_vout->endVirtual(self->_vout));
136136
#endif
137137
++self->_curFileIndex;
138-
}else{
139-
break;
140-
}
141-
}
142-
143-
if (self->_curFileIndex<self->_fileCount){//open file for write
144-
ZipFilePos_t uncompressedSize=UnZipper_file_uncompressedSize(&self->_newZipVCE,self->_curFileIndex);
145-
ZipFilePos_t compressedSize=uncompressedSize;
146-
if (UnZipper_file_isCompressed(&self->_newZipVCE,self->_curFileIndex)){
147-
check(self->_curNewReCompressSizeIndex<self->_newReCompressSizeCount);
148-
compressedSize=self->_newReCompressSizeList[self->_curNewReCompressSizeIndex];
149-
++self->_curNewReCompressSizeIndex;
150-
}
151-
_update_compressedSize(self,self->_curFileIndex,compressedSize);
152-
153-
bool isWriteOtherCompressedData=(self->_curNewOtherCompressIndex<self->_newRefOtherCompressedCount)
154-
&&((int)self->_newRefOtherCompressedList[self->_curNewOtherCompressIndex]==self->_curFileIndex);
155-
if (isWriteOtherCompressedData)
156-
++self->_curNewOtherCompressIndex;
138+
}else{//open file for write
139+
ZipFilePos_t uncompressedSize=UnZipper_file_uncompressedSize(&self->_newZipVCE,self->_curFileIndex);
140+
ZipFilePos_t compressedSize=uncompressedSize;
141+
if (UnZipper_file_isCompressed(&self->_newZipVCE,self->_curFileIndex)){
142+
check(self->_curNewReCompressSizeIndex<self->_newReCompressSizeCount);
143+
compressedSize=self->_newReCompressSizeList[self->_curNewReCompressSizeIndex];
144+
++self->_curNewReCompressSizeIndex;
145+
}
146+
_update_compressedSize(self,self->_curFileIndex,compressedSize);
147+
148+
bool isWriteOtherCompressedData=(self->_curNewOtherCompressIndex<self->_newRefOtherCompressedCount)
149+
&&((int)self->_newRefOtherCompressedList[self->_curNewOtherCompressIndex]==self->_curFileIndex);
150+
if (isWriteOtherCompressedData)
151+
++self->_curNewOtherCompressIndex;
157152

158-
bool is0FileSize=false; //in zip
159-
#if (_IS_NEED_VIRTUAL_ZIP)
160-
TVirtualZip_out_type ty=kVirtualZip_out_void;
161-
if (self->_vout){
162-
ty=self->_vout->beginVirtual(self->_vout,&self->_newZipVCE,self->_curFileIndex);
163-
switch (ty) {
164-
case kVirtualZip_out_void: { //ok, do nothing
165-
assert(self->_vout->virtualStream==0); } break;
166-
case kVirtualZip_out_emptyFile_cast: { // set fileSize==0
167-
assert(self->_vout->virtualStream==0);
168-
_update_fileSize(self,self->_curFileIndex,0,0);
169-
is0FileSize=true; } break;
170-
case kVirtualZip_out_emptyFile_uncompressed: { //set fileSize==0, need out and endVirtual
171-
assert(self->_vout->virtualStream!=0);
172-
_update_fileSize(self,self->_curFileIndex,0,0);
173-
is0FileSize=true; } break;
174-
default: { check(false); } break; //error, or unknow as error
153+
bool isVirtualFile0Size=false; // is notInZip virtualFile && uncompressedFizeSize==0 ?
154+
#if (_IS_NEED_VIRTUAL_ZIP)
155+
TVirtualZip_out_type ty=kVirtualZip_out_void;
156+
if (self->_vout){
157+
ty=self->_vout->beginVirtual(self->_vout,&self->_newZipVCE,self->_curFileIndex);
158+
switch (ty) {
159+
case kVirtualZip_out_void: { //ok, do nothing
160+
assert(self->_vout->virtualStream==0); } break;
161+
case kVirtualZip_out_emptyFile_cast: { // set fileSize==0
162+
assert(self->_vout->virtualStream==0);
163+
_update_fileSize(self,self->_curFileIndex,0,0);
164+
isVirtualFile0Size=true; } break;
165+
case kVirtualZip_out_emptyFile_uncompressed: { //set fileSize==0, need out and endVirtual
166+
assert(self->_vout->virtualStream!=0);
167+
_update_fileSize(self,self->_curFileIndex,0,0);
168+
isVirtualFile0Size=true; } break;
169+
default: { check(false); } break; //error, or unknow as error
170+
}
175171
}
176-
}
177-
#endif
178-
if (isWriteOtherCompressedData && self->_newOtherCompressIsValid){
179-
check(Zipper_file_append_beginWith(self->_out_newZip,&self->_newZipVCE,self->_curFileIndex,
180-
false,is0FileSize?0:uncompressedSize,is0FileSize?0:compressedSize,
181-
self->_newOtherCompressLevel,self->_newOtherCompressMemLevel));
182-
self->_curWriteToPosEnd+=uncompressedSize;
183-
}else{
184-
#if (_IS_NEED_VIRTUAL_ZIP)
185-
if (ty==kVirtualZip_out_emptyFile_uncompressed){
186-
check(!isWriteOtherCompressedData); //now unsupport auto decompress data
172+
#endif
173+
if (isWriteOtherCompressedData && self->_newOtherCompressIsValid){
174+
check(Zipper_file_append_beginWith(self->_out_newZip,&self->_newZipVCE,self->_curFileIndex,
175+
false,isVirtualFile0Size?0:uncompressedSize,isVirtualFile0Size?0:compressedSize,
176+
self->_newOtherCompressLevel,self->_newOtherCompressMemLevel));
177+
self->_curWriteToPosEnd+=uncompressedSize;
178+
}else{
179+
#if (_IS_NEED_VIRTUAL_ZIP)
180+
if (ty==kVirtualZip_out_emptyFile_uncompressed){
181+
check(!isWriteOtherCompressedData); //now unsupport auto decompress data
182+
}
183+
#endif
184+
check(Zipper_file_append_begin(self->_out_newZip,&self->_newZipVCE,self->_curFileIndex,
185+
isWriteOtherCompressedData,
186+
isVirtualFile0Size?0:uncompressedSize,isVirtualFile0Size?0:compressedSize));
187+
self->_curWriteToPosEnd+=isWriteOtherCompressedData?compressedSize:uncompressedSize;
188+
}
189+
190+
if ((!isVirtualFile0Size)&&(uncompressedSize==0)){ // compressed && empty file
191+
check(Zipper_file_append_part(self->_out_newZip,0,0));
192+
check(Zipper_file_append_end(self->_out_newZip));
193+
++self->_curFileIndex;
194+
}else{
195+
return hpatch_TRUE; //opened a file wait for write;
187196
}
188-
#endif
189-
check(Zipper_file_append_begin(self->_out_newZip,&self->_newZipVCE,self->_curFileIndex,
190-
isWriteOtherCompressedData,
191-
is0FileSize?0:uncompressedSize,is0FileSize?0:compressedSize));
192-
self->_curWriteToPosEnd+=isWriteOtherCompressedData?compressedSize:uncompressedSize;
193197
}
194-
return hpatch_TRUE;
195198
}
196199

197200
//file entry end

src/patch/Zipper.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -526,16 +526,19 @@ bool UnZipper_updateVirtualVCE(UnZipper* self,bool isDataNormalized,size_t zipCE
526526
return true;
527527
}
528528

529+
//NOTE: used bit pos in generalPurposeBitFlag for saving isPageAlignSoFile tag
530+
#define kPurposeBitFlag_so_pos 7 //now 7,8?,10 unused by zip
531+
#define _kPurposeBitFlag_so_BytePos (kPurposeBitFlag_so_pos/8)
532+
#define _kPurposeBitFlag_so_bitPos (kPurposeBitFlag_so_pos-(kPurposeBitFlag_so_pos/8)*8)
529533

530534
inline static unsigned char* _at_file_generalPurposeBitFlag(const UnZipper* self,int fileIndex){
531-
return fileHeaderBuf(self,fileIndex)+8;
535+
return fileHeaderBuf(self,fileIndex)+8+_kPurposeBitFlag_so_BytePos;
532536
}
533-
//NOTE: used bit pos 7 in generalPurposeBitFlag for saving isPageAlignSoFile tag
534537
inline static bool _file_getIsPageAlignSoFile(const UnZipper* self,int fileIndex){
535-
return 0!=((*_at_file_generalPurposeBitFlag(self,fileIndex))&(1<<7));
538+
return 0!=((*_at_file_generalPurposeBitFlag(self,fileIndex))&(1<<_kPurposeBitFlag_so_bitPos));
536539
}
537540
inline static void _file_setIsPageAlignSoFile(const UnZipper* self,int fileIndex){
538-
(*_at_file_generalPurposeBitFlag(self,fileIndex))|=(1<<7);
541+
(*_at_file_generalPurposeBitFlag(self,fileIndex))|=(1<<_kPurposeBitFlag_so_bitPos);
539542
}
540543

541544
inline static const unsigned char* _at_file_compressType(const UnZipper* self,int fileIndex){
@@ -860,13 +863,13 @@ bool Zipper_close(Zipper* self){
860863

861864
self->_stream=0;
862865
self->_fileEntryCount=0;
863-
if (self->_buf) { free(self->_buf); self->_buf=0; }
864866
if (self->_fileStream.m_file) { check(hpatch_TFileStreamOutput_close(&self->_fileStream)); }
865867
if (self->_append_stream.compressHandle!=0){
866868
struct _zlib_TCompress* compressHandle=self->_append_stream.compressHandle;
867869
self->_append_stream.compressHandle=0;
868870
check(_zlib_compress_close_by(compressPlugin,compressHandle));
869871
}
872+
if (self->_buf) { free(self->_buf); self->_buf=0; }
870873
return true;
871874
}
872875

@@ -998,7 +1001,6 @@ inline static size_t _getAlignSkipLen(size_t curPos,size_t align){
9981001
return align-1-(curPos+align-1)%align;
9991002
}
10001003
inline static bool _writeAlignSkip(Zipper* self,size_t alignSkipLen){
1001-
assert(alignSkipLen<self->_ZipAlignSize);
10021004
const size_t bufSize =16;
10031005
const TByte _alignSkipBuf[bufSize]={0};
10041006
while (alignSkipLen>0) {
@@ -1289,12 +1291,15 @@ bool Zipper_file_append_end(Zipper* self){
12891291
if (append_state->self==0) { assert(false); return false; }
12901292

12911293
bool result=true;
1292-
if (append_state->compressHandle!=0){
1294+
const bool isCompressedFile=(append_state->compressHandle!=0);
1295+
if (isCompressedFile){
12931296
if ((append_state->inputPos==0)&&(append_state->outputPos==0)){
12941297
Byte emptyBuf=0; //compress empty file
12951298
check(append_state->write(append_state,0,&emptyBuf,&emptyBuf));
12961299
}
1297-
check_clear(_zlib_compress_close_by(compressPlugin,append_state->compressHandle));
1300+
struct _zlib_TCompress* compressHandle=append_state->compressHandle;
1301+
append_state->compressHandle=0;
1302+
check_clear(_zlib_compress_close_by(compressPlugin,compressHandle));
12981303
}
12991304

13001305
check_clear(append_state->inputPos==append_state->streamSize);
@@ -1307,7 +1312,7 @@ bool Zipper_file_append_end(Zipper* self){
13071312
check_clear(_dispose_filishedThreadWork(self,false));
13081313
}
13091314
#endif
1310-
if (append_state->compressHandle!=0){
1315+
if (isCompressedFile){
13111316
assert(append_state->outputPos==(uint32_t)append_state->outputPos);
13121317
uint32_t compressedSize=(uint32_t)append_state->outputPos;
13131318
check_clear(_zipper_file_update_compressedSize(self,append_state->curFileIndex,compressedSize));

0 commit comments

Comments
 (0)