Android增量更新和签名校验
sorunme
8年前
<h3>1. 概述</h3> <p>NDK图片压缩有很多人反应是蒙的,包括在文章评论的一些哥们,也包括私下聊天的一些哥们。那么内涵段子后面的所有分享都离不开NDK,比如gif图片加载,视频压缩,视频直播推流等等。当然我们也可以去网上下载别人写好的,但是对于我们来说没任何意义。经过后来一系列的考虑,内涵段子项目暂时告一个段落。希望后面有时间出一些C和C++的语法基础,然后我们再来实现后面的功能,这期是现阶段最后一次分享NDK了, <strong>这次我会一步一步来实现</strong> 。</p> <p>每次演示我记得我都下载的是内涵段子6.0.1的版本,但是每到周末就发现更新到了最新的版本,在我的印象里我从来都没有去更新过,那么到底是怎么回事?有些应用弹出更新我一般是拒绝的,因为我需要下载几十M的apk要等太久,没耐心。而增量更新就解决了这个问题,我们更新版本不需要全部下载最新版的apk,而只需要下载一个几M甚至更小的差分包,就能完成更新了。网上这方面的资料也比较多大家可以多看看别人写的文章。</p> <h3>2. 下载生成AS的.so库</h3> <p>1.下载整合bsdiff和bzip2</p> <p>百度搜索 bsdiff 目前最新版本是4.3,点击 here 就可以下载了。文件只有5KB,里面有两个文件很重要bsdiff.c和bspatch.c,一个用来生成差分包一个用来合并差分包。</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/f774697b814647afe9eca6e9f894497d.png"></p> <p>bsdiff解压后.png</p> <p>打开.c源码瞧瞧,里面还需要我们下载bzip2,百度搜索 bzip2 ,点击 1.0.6 source tarball 就可以下载了。这个文件就有点大了,我们把里面的.h和.c文件拷贝出来整合到bsdiff里面。</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/128dddbbe454668d14b9fc5ebb576e06.png"></p> <p>bsdiff和bzip2整合.png</p> <p>2.生成AS的.so库</p> <p>把整合好的所有文件都copy到项目的jni目录下面就可以开始写代码了:</p> <pre> <code class="language-java">#if 0 __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59:06 cperciva Exp $"); #endif #include "bzlib.c" #include "crctable.c" #include "compress.c" #include "decompress.c" #include "randtable.c" #include "blocksort.c" #include "huffman.c" #include <stdlib.h> #include <stdio.h> #include <string.h> #include <err.h> #include <unistd.h> #include <fcntl.h> #include "com_darren_util_PatchUtils.h" static off_t offtin(u_char *buf) { off_t y; y=buf[7]&0x7F; y=y*256;y+=buf[6]; y=y*256;y+=buf[5]; y=y*256;y+=buf[4]; y=y*256;y+=buf[3]; y=y*256;y+=buf[2]; y=y*256;y+=buf[1]; y=y*256;y+=buf[0]; if(buf[7]&0x80) y=-y; return y; } int combine(int argc,char * argv[]) { FILE * f, * cpf, * dpf, * epf; BZFILE * cpfbz2, * dpfbz2, * epfbz2; int cbz2err, dbz2err, ebz2err; int fd; ssize_t oldsize,newsize; ssize_t bzctrllen,bzdatalen; u_char header[32],buf[8]; u_char *old, *new; off_t oldpos,newpos; off_t ctrl[3]; off_t lenread; off_t i; if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]); /* Open patch file */ if ((f = fopen(argv[3], "r")) == NULL) err(1, "fopen(%s)", argv[3]); /* File format: 0 8 "BSDIFF40" 8 8 X 16 8 Y 24 8 sizeof(newfile) 32 X bzip2(control block) 32+X Y bzip2(diff block) 32+X+Y ??? bzip2(extra block) with control block a set of triples (x,y,z) meaning "add x bytes from oldfile to x bytes from the diff block; copy y bytes from the extra block; seek forwards in oldfile by z bytes". */ /* Read header */ if (fread(header, 1, 32, f) < 32) { if (feof(f)) errx(1, "Corrupt patch\n"); err(1, "fread(%s)", argv[3]); } /* Check for appropriate magic */ if (memcmp(header, "BSDIFF40", 8) != 0) errx(1, "Corrupt patch\n"); /* Read lengths from header */ bzctrllen=offtin(header+8); bzdatalen=offtin(header+16); newsize=offtin(header+24); if((bzctrllen<0) || (bzdatalen<0) || (newsize<0)) errx(1,"Corrupt patch\n"); /* Close patch file and re-open it via libbzip2 at the right places */ if (fclose(f)) err(1, "fclose(%s)", argv[3]); if ((cpf = fopen(argv[3], "r")) == NULL) err(1, "fopen(%s)", argv[3]); if (fseeko(cpf, 32, SEEK_SET)) err(1, "fseeko(%s, %lld)", argv[3], (long long)32); if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL) errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err); if ((dpf = fopen(argv[3], "r")) == NULL) err(1, "fopen(%s)", argv[3]); if (fseeko(dpf, 32 + bzctrllen, SEEK_SET)) err(1, "fseeko(%s, %lld)", argv[3], (long long)(32 + bzctrllen)); if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL) errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err); if ((epf = fopen(argv[3], "r")) == NULL) err(1, "fopen(%s)", argv[3]); if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET)) err(1, "fseeko(%s, %lld)", argv[3], (long long)(32 + bzctrllen + bzdatalen)); if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL) errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err); if(((fd=open(argv[1],O_RDONLY,0))<0) || ((oldsize=lseek(fd,0,SEEK_END))==-1) || ((old=malloc(oldsize+1))==NULL) || (lseek(fd,0,SEEK_SET)!=0) || (read(fd,old,oldsize)!=oldsize) || (close(fd)==-1)) err(1,"%s",argv[1]); if((new=malloc(newsize+1))==NULL) err(1,NULL); oldpos=0;newpos=0; while(newpos<newsize) { /* Read control data */ for(i=0;i<=2;i++) { lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8); if ((lenread < 8) || ((cbz2err != BZ_OK) && (cbz2err != BZ_STREAM_END))) errx(1, "Corrupt patch\n"); ctrl[i]=offtin(buf); }; /* Sanity-check */ if(newpos+ctrl[0]>newsize) errx(1,"Corrupt patch\n"); /* Read diff string */ lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]); if ((lenread < ctrl[0]) || ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END))) errx(1, "Corrupt patch\n"); /* Add old data to diff string */ for(i=0;i<ctrl[0];i++) if((oldpos+i>=0) && (oldpos+i<oldsize)) new[newpos+i]+=old[oldpos+i]; /* Adjust pointers */ newpos+=ctrl[0]; oldpos+=ctrl[0]; /* Sanity-check */ if(newpos+ctrl[1]>newsize) errx(1,"Corrupt patch\n"); /* Read extra string */ lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]); if ((lenread < ctrl[1]) || ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END))) errx(1, "Corrupt patch\n"); /* Adjust pointers */ newpos+=ctrl[1]; oldpos+=ctrl[2]; }; /* Clean up the bzip2 reads */ BZ2_bzReadClose(&cbz2err, cpfbz2); BZ2_bzReadClose(&dbz2err, dpfbz2); BZ2_bzReadClose(&ebz2err, epfbz2); if (fclose(cpf) || fclose(dpf) || fclose(epf)) err(1, "fclose(%s)", argv[3]); /* Write the new file */ if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) || (write(fd,new,newsize)!=newsize) || (close(fd)==-1)) err(1,"%s",argv[2]); free(new); free(old); return 0; } // 合并差分包 JNIEXPORT void JNICALL Java_com_darren_util_PatchUtils_combine (JNIEnv *env, jclass jclz, jstring old_apk_path, jstring new_apk_path, jstring patch_path){ // 固定参数 4 int argc = 4; // 转换 jstring -> char* char* old_apk_cstr = (char*)(*env)->GetStringUTFChars(env,old_apk_path,NULL); char* new_apk_cstr = (char*)(*env)->GetStringUTFChars(env,new_apk_path,NULL); char* patch_path_cstr = (char*)(*env)->GetStringUTFChars(env,patch_path,NULL); // 赋值 char * argv[4]; argv[0] = "combine"; argv[1] = old_apk_cstr; argv[2] = new_apk_cstr; argv[3] = patch_path_cstr; // 调用合并方法 patch(argc,argv); // 释放资源 (*env)->ReleaseStringUTFChars(env,old_apk_path,old_apk_cstr); (*env)->ReleaseStringUTFChars(env,new_apk_path,new_apk_cstr); (*env)->ReleaseStringUTFChars(env,patch_path,patch_path_cstr); }</code></pre> <p>3.下载差分包合并校验</p> <p>假设目前我们手机上面是安装的内涵段子1.0的版本,服务器上面已经有了2.0的版本,那么现在我们只需要下载差分包就好,然后调用已经写好的.so的方法生成2.0的版本,校验签名即可。</p> <pre> <code class="language-java">/** * 获取当前apk的签名 * @param context * @return */ public static String getSignature(Context context){ // 通过Context获取当前包名 String currentApkPackageName = context.getApplicationInfo().packageName; Log.e(TAG,"TAG -> "+currentApkPackageName); // 通过PackageManager获取所有应用的PackageInfo信息 PackageManager packageManager = context.getPackageManager(); List<PackageInfo> packageInfos = packageManager.getInstalledPackages(PackageManager.GET_SIGNATURES); for (PackageInfo packageInfo : packageInfos) { if(packageInfo.packageName.equals(currentApkPackageName)){ // 获取签名 return packageInfo.signatures[0].toCharsString(); } } return null; }</code></pre> <p>获取某个路径下的apk签名,这个倒是真的把我难住了。某些哥们就立马来灵感了,百度啊,可是早在几年前那个时候网上很少找得到答案。况且网上找的解决方案也大多存在版本兼容问题,不过还好系统应用里面不是有签名校验吗?我们可以把系统应用的源码拿出来看看,看它是怎么校验签名的又或者说是怎么获取PackageInfo的。</p> <pre> <code class="language-java">/* * Utility method to get application information for a given packageURI */ public static ApplicationInfo getApplicationInfo(Uri packageURI) { final String archiveFilePath = packageURI.getPath(); PackageParser packageParser = new PackageParser(archiveFilePath); File sourceFile = new File(archiveFilePath); DisplayMetrics metrics = new DisplayMetrics(); metrics.setToDefaults(); PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0); if (pkg == null) { return null; } return pkg.applicationInfo; } /* * Utility method to get package information for a given packageURI */ public static PackageParser.Package getPackageInfo(Uri packageURI) { final String archiveFilePath = packageURI.getPath(); PackageParser packageParser = new PackageParser(archiveFilePath); File sourceFile = new File(archiveFilePath); DisplayMetrics metrics = new DisplayMetrics(); metrics.setToDefaults(); return packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0); }</code></pre> <p>但是目前还有一个问题PackageParser你根本找不到这个类,这个类被隐藏了。能不能不要这么操蛋,我不校验签名了还不行吗?不校验那出了问题锅得你背,你可以把这个流程去掉试试。所以硬着头皮用反射吧:</p> <pre> <code class="language-java">/** * 获取该目录下的apk签名 * @param path 当前apk路径 * @return */ public static String getSignature(String path) throws Exception{ // 1.反射实例化PackageParser对象 Object packageParser = getPackageParser(path); // 2.反射获取parsePackage方法 Object packageObject = getPackageInfo(path,packageParser); // 3.调用collectCertificates方法 Method collectCertificatesMethod = packageParser.getClass(). getDeclaredMethod("collectCertificates",packageObject.getClass(),int.class); collectCertificatesMethod.invoke(packageParser,packageObject,0); // 4.获取mSignatures属性 Field signaturesField = packageObject.getClass().getDeclaredField("mSignatures"); signaturesField.setAccessible(true); Signature[] mSignatures = (Signature[]) signaturesField.get(packageObject); return mSignatures[0].toCharsString(); } /** * 创建PackageParser.Package类,兼容5.0 * @param path * @return * @throws Exception */ private static Object getPackageInfo(String path, Object packageParser) throws Exception { if(Build.VERSION.SDK_INT >=21){ Class<?>[] paramClass = new Class[2]; paramClass[0] = File.class; paramClass[1] = int.class; Method parsePackageMethod = packageParser.getClass().getDeclaredMethod("parsePackage",paramClass); // 3.反射执行parsePackage方法 Object[] paramObject = new Object[2]; paramObject[0] = new File(path); paramObject[1] = 0; parsePackageMethod.setAccessible(true); return parsePackageMethod.invoke(packageParser,paramObject); }else{ Class<?>[] paramClass = new Class[4]; paramClass[0] = File.class; paramClass[1] = String.class; paramClass[2] = DisplayMetrics.class; paramClass[3] = int.class; Method parsePackageMethod = packageParser.getClass().getDeclaredMethod("parsePackage",paramClass); // 3.反射执行parsePackage方法 Object[] paramObject = new Object[4]; paramObject[0] = new File(path); paramObject[1] = path; DisplayMetrics metrics = new DisplayMetrics(); metrics.setToDefaults(); paramObject[2] = metrics; paramObject[3] = 0; parsePackageMethod.setAccessible(true); return parsePackageMethod.invoke(packageParser,paramObject); } } /** * 创建PackageParser类 * @param path * @return * @throws Exception */ private static Object getPackageParser(String path) throws Exception{ Class<?> packageParserClazz = Class.forName("android.content.pm.PackageParser"); // 版本兼容 if(Build.VERSION.SDK_INT >=21 ){ Constructor<?> packageParserConstructor = packageParserClazz.getDeclaredConstructor(); return packageParserConstructor.newInstance(); }else{ Constructor<?> packageParserConstructor = packageParserClazz.getDeclaredConstructor(String.class); return packageParserConstructor.newInstance(path); } }</code></pre> <h3>3. 生成服务端.dll动态库</h3> <p>好不容测试没问题了,高高兴兴的打算去提交申请单。可是需要后台协助的时候说我根本不会生成dll动态库,好说歹说都不行,直接对你说你行你来啊,所以也只能自己来了。</p> <pre> <code class="language-java">#include <stdlib.h> #include "bzlib.h" #include <stdio.h> #include <string.h> //#include <err.h> //#include <unistd.h> #include <io.h> #include <fcntl.h> //#include <sys/wait.h> #include "com_hc_diff_BSDiff.h" #include <windows.h> #include <process.h> #include <sys/types.h> typedef unsigned char u_char; typedef long pid_t; template<class T> void err(int i, const char* str, T arg) { char lastErrorTxt[1024]; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),0,lastErrorTxt,1024,NULL); printf("%s",lastErrorTxt); printf(str, arg); exit(i); } void err(int i, const char* str) { char lastErrorTxt[1024]; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),0,lastErrorTxt,1024,NULL); printf("%s",lastErrorTxt); if (str!=NULL) { printf("%s",str); } exit(i); } template<class T> void errx(int i, const char* str, T arg) { printf(str, arg); exit(i); } void errx(int i, const char* str) { printf("%s",str); exit(i); } #define MIN(x,y) (((x)<(y)) ? (x) : (y)) static void split(off_t *I,off_t *V,off_t start,off_t len,off_t h) { off_t i,j,k,x,tmp,jj,kk; if(len<16) { for(k=start;k<start+len;k+=j) { j=1;x=V[I[k]+h]; for(i=1;k+i<start+len;i++) { if(V[I[k+i]+h]<x) { x=V[I[k+i]+h]; j=0; }; if(V[I[k+i]+h]==x) { tmp=I[k+j];I[k+j]=I[k+i];I[k+i]=tmp; j++; }; }; for(i=0;i<j;i++) V[I[k+i]]=k+j-1; if(j==1) I[k]=-1; }; return; }; x=V[I[start+len/2]+h]; jj=0;kk=0; for(i=start;i<start+len;i++) { if(V[I[i]+h]<x) jj++; if(V[I[i]+h]==x) kk++; }; jj+=start;kk+=jj; i=start;j=0;k=0; while(i<jj) { if(V[I[i]+h]<x) { i++; } else if(V[I[i]+h]==x) { tmp=I[i];I[i]=I[jj+j];I[jj+j]=tmp; j++; } else { tmp=I[i];I[i]=I[kk+k];I[kk+k]=tmp; k++; }; }; while(jj+j<kk) { if(V[I[jj+j]+h]==x) { j++; } else { tmp=I[jj+j];I[jj+j]=I[kk+k];I[kk+k]=tmp; k++; }; }; if(jj>start) split(I,V,start,jj-start,h); for(i=0;i<kk-jj;i++) V[I[jj+i]]=kk-1; if(jj==kk-1) I[jj]=-1; if(start+len>kk) split(I,V,kk,start+len-kk,h); } static void qsufsort(off_t *I,off_t *V,u_char *old,off_t oldsize) { off_t buckets[256]; off_t i,h,len; for(i=0;i<256;i++) buckets[i]=0; for(i=0;i<oldsize;i++) buckets[old[i]]++; for(i=1;i<256;i++) buckets[i]+=buckets[i-1]; for(i=255;i>0;i--) buckets[i]=buckets[i-1]; buckets[0]=0; for(i=0;i<oldsize;i++) I[++buckets[old[i]]]=i; I[0]=oldsize; for(i=0;i<oldsize;i++) V[i]=buckets[old[i]]; V[oldsize]=0; for(i=1;i<256;i++) if(buckets[i]==buckets[i-1]+1) I[buckets[i]]=-1; I[0]=-1; for(h=1;I[0]!=-(oldsize+1);h+=h) { len=0; for(i=0;i<oldsize+1;) { if(I[i]<0) { len-=I[i]; i-=I[i]; } else { if(len) I[i-len]=-len; len=V[I[i]]+1-i; split(I,V,i,len,h); i+=len; len=0; }; }; if(len) I[i-len]=-len; }; for(i=0;i<oldsize+1;i++) I[V[i]]=i; } static off_t matchlen(u_char *old,off_t oldsize,u_char *_new,off_t newsize) { off_t i; for(i=0;(i<oldsize)&&(i<newsize);i++) if(old[i]!=_new[i]) break; return i; } static off_t search(off_t *I,u_char *old,off_t oldsize, u_char *_new,off_t newsize,off_t st,off_t en,off_t *pos) { off_t x,y; if(en-st<2) { x=matchlen(old+I[st],oldsize-I[st],_new,newsize); y=matchlen(old+I[en],oldsize-I[en],_new,newsize); if(x>y) { *pos=I[st]; return x; } else { *pos=I[en]; return y; } }; x=st+(en-st)/2; if(memcmp(old+I[x],_new,MIN(oldsize-I[x],newsize))<0) { return search(I,old,oldsize,_new,newsize,x,en,pos); } else { return search(I,old,oldsize,_new,newsize,st,x,pos); }; } static void offtout(off_t x,u_char *buf) { off_t y; if(x<0) y=-x; else y=x; buf[0]=y%256;y-=buf[0]; y=y/256;buf[1]=y%256;y-=buf[1]; y=y/256;buf[2]=y%256;y-=buf[2]; y=y/256;buf[3]=y%256;y-=buf[3]; y=y/256;buf[4]=y%256;y-=buf[4]; y=y/256;buf[5]=y%256;y-=buf[5]; y=y/256;buf[6]=y%256;y-=buf[6]; y=y/256;buf[7]=y%256; if(x<0) buf[7]|=0x80; } int diff(int argc,char *argv[]) { int fd; u_char *old,*_new; off_t oldsize,newsize; off_t *I,*V; off_t scan,pos,len; off_t lastscan,lastpos,lastoffset; off_t oldscore,scsc; off_t s,Sf,lenf,Sb,lenb; off_t overlap,Ss,lens; off_t i; off_t dblen,eblen; u_char *db,*eb; u_char buf[8]; u_char header[32]; FILE * pf; BZFILE * pfbz2; int bz2err; if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]); /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure that we never try to malloc(0) and get a NULL pointer */ //org: //if(((fd=open(argv[1],O_RDONLY,0))<0) || // ((oldsize=lseek(fd,0,SEEK_END))==-1) || // ((old=malloc(oldsize+1))==NULL) || // (lseek(fd,0,SEEK_SET)!=0) || // (read(fd,old,oldsize)!=oldsize) || // (close(fd)==-1)) err(1,"%s",argv[1]); //new: //Read in chunks, don't rely on read always returns full data! if(((fd=_open(argv[1],O_RDONLY|O_BINARY|O_NOINHERIT,0))<0) || ((oldsize=lseek(fd,0,SEEK_END))==-1) || ((old=(u_char*)malloc(oldsize+1))==NULL) || (_lseek(fd,0,SEEK_SET)!=0)) err(1,"%s",argv[1]); int r=oldsize; while (r>0 && (i=read(fd,old+oldsize-r,r))>0) r-=i; if (r>0 || close(fd)==-1) err(1,"%s",argv[1]); if(((I=(off_t*)malloc((oldsize+1)*sizeof(off_t)))==NULL) || ((V=(off_t*)malloc((oldsize+1)*sizeof(off_t)))==NULL)) err(1,NULL); qsufsort(I,V,old,oldsize); free(V); /* Allocate newsize+1 bytes instead of newsize bytes to ensure that we never try to malloc(0) and get a NULL pointer */ //org: //if(((fd=open(argv[2],O_RDONLY,0))<0) || // ((newsize=lseek(fd,0,SEEK_END))==-1) || // ((_new=malloc(newsize+1))==NULL) || // (lseek(fd,0,SEEK_SET)!=0) || // (read(fd,_new,newsize)!=newsize) || // (close(fd)==-1)) err(1,"%s",argv[2]); //new: //Read in chunks, don't rely on read always returns full data! if(((fd=open(argv[2],O_RDONLY|O_BINARY|O_NOINHERIT,0))<0) || ((newsize=lseek(fd,0,SEEK_END))==-1) || ((_new=(u_char*)malloc(newsize+1))==NULL) || (lseek(fd,0,SEEK_SET)!=0)) err(1,"%s",argv[2]); r=newsize; while (r>0 && (i=read(fd,_new+newsize-r,r))>0) r-=i; if (r>0 || close(fd)==-1) err(1,"%s",argv[1]); if(((db=(u_char*)malloc(newsize+1))==NULL) || ((eb=(u_char*)malloc(newsize+1))==NULL)) err(1,NULL); dblen=0; eblen=0; /* Create the patch file */ //org: //if ((pf = fopen(argv[3], "w")) == NULL) //new: //if((fd=open(argv[3],O_CREAT|O_TRUNC|O_WRONLY|O_BINARY|O_NOINHERIT,0666))<0) if ((pf = fopen(argv[3], "wb")) == NULL) err(1,"%s",argv[3]); /* Header is 0 8 "BSDIFF40" 8 8 length of bzip2ed ctrl block 16 8 length of bzip2ed diff block 24 8 length of new file */ /* File is 0 32 Header 32 ?? Bzip2ed ctrl block ?? ?? Bzip2ed diff block ?? ?? Bzip2ed extra block */ memcpy(header,"BSDIFF40",8); offtout(0, header + 8); offtout(0, header + 16); offtout(newsize, header + 24); if (fwrite(header, 32, 1, pf) != 1) err(1, "fwrite(%s)", argv[3]); /* Compute the differences, writing ctrl as we go */ if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err); scan=0;len=0; lastscan=0;lastpos=0;lastoffset=0; while(scan<newsize) { oldscore=0; for(scsc=scan+=len;scan<newsize;scan++) { len=search(I,old,oldsize,_new+scan,newsize-scan, 0,oldsize,&pos); for(;scsc<scan+len;scsc++) if((scsc+lastoffset<oldsize) && (old[scsc+lastoffset] == _new[scsc])) oldscore++; if(((len==oldscore) && (len!=0)) || (len>oldscore+8)) break; if((scan+lastoffset<oldsize) && (old[scan+lastoffset] == _new[scan])) oldscore--; }; if((len!=oldscore) || (scan==newsize)) { s=0;Sf=0;lenf=0; for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) { if(old[lastpos+i]==_new[lastscan+i]) s++; i++; if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; }; }; lenb=0; if(scan<newsize) { s=0;Sb=0; for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) { if(old[pos-i]==_new[scan-i]) s++; if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; }; }; }; if(lastscan+lenf>scan-lenb) { overlap=(lastscan+lenf)-(scan-lenb); s=0;Ss=0;lens=0; for(i=0;i<overlap;i++) { if(_new[lastscan+lenf-overlap+i]== old[lastpos+lenf-overlap+i]) s++; if(_new[scan-lenb+i]== old[pos-lenb+i]) s--; if(s>Ss) { Ss=s; lens=i+1; }; }; lenf+=lens-overlap; lenb-=lens; }; for(i=0;i<lenf;i++) db[dblen+i]=_new[lastscan+i]-old[lastpos+i]; for(i=0;i<(scan-lenb)-(lastscan+lenf);i++) eb[eblen+i]=_new[lastscan+lenf+i]; dblen+=lenf; eblen+=(scan-lenb)-(lastscan+lenf); offtout(lenf,buf); BZ2_bzWrite(&bz2err, pfbz2, buf, 8); if (bz2err != BZ_OK) errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); offtout((scan-lenb)-(lastscan+lenf),buf); BZ2_bzWrite(&bz2err, pfbz2, buf, 8); if (bz2err != BZ_OK) errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); offtout((pos-lenb)-(lastpos+lenf),buf); BZ2_bzWrite(&bz2err, pfbz2, buf, 8); if (bz2err != BZ_OK) errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); lastscan=scan-lenb; lastpos=pos-lenb; lastoffset=pos-scan; }; }; BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL); if (bz2err != BZ_OK) errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err); /* Compute size of compressed ctrl data */ if ((len = ftell(pf)) == -1) err(1, "ftello"); offtout(len-32, header + 8); /* Write compressed diff data */ if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err); BZ2_bzWrite(&bz2err, pfbz2, db, dblen); if (bz2err != BZ_OK) errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL); if (bz2err != BZ_OK) errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err); /* Compute size of compressed diff data */ if ((newsize = ftell(pf)) == -1) err(1, "ftello"); offtout(newsize - len, header + 16); /* Write compressed extra data */ if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err); BZ2_bzWrite(&bz2err, pfbz2, eb, eblen); if (bz2err != BZ_OK) errx(1, "BZ2_bzWrite, bz2err = %d", bz2err); BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL); if (bz2err != BZ_OK) errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err); /* Seek to the beginning, write the header, and close the file */ if (fseek(pf, 0, SEEK_SET)) err(1, "fseeko"); if (fwrite(header, 32, 1, pf) != 1) err(1, "fwrite(%s)", argv[3]); if (fclose(pf)) err(1, "fclose"); /* Free the memory we used */ free(db); free(eb); free(I); free(old); free(_new); return 0; } JNIEXPORT void JNICALL Java_com_hc_diff_BSDiff_diff (JNIEnv *env, jclass jclz, jstring new_apk_path, jstring old_apk_path, jstring patch_path){ // 固定为4 int argc = 4; char *argv[4]; // jstring -> char* char* new_apk_cstr = (char*)env->GetStringUTFChars(new_apk_path, NULL); char* old_apk_cstr = (char*)env->GetStringUTFChars(old_apk_path, NULL); char* patch_path_cstr = (char*)env->GetStringUTFChars(patch_path, NULL); // 封装参数 argv[0] = "diff"; argv[1] = new_apk_cstr; argv[2] = old_apk_cstr; argv[3] = patch_path_cstr; // 调用生成差分包方法 diff(argc,argv); // 释放资源 env->ReleaseStringUTFChars(new_apk_path,new_apk_cstr); env->ReleaseStringUTFChars(old_apk_path, old_apk_cstr); env->ReleaseStringUTFChars(patch_path, patch_path_cstr); }</code></pre> <p>NDK图片压缩加密上传和增量更新的实现流程差不多,我们都是用的第三方提供好的一些C库文件,然后在他的基础上写了一些调用代码。以后我们自己去编译libgif,webrtc,ffmpeg,fomd也是类似, 解决问题的方式很重要而不是过多的关注代码,这是内涵段子最后一篇技术分享的文章了。</p> <p> </p> <p> </p> <p>来自:http://www.jianshu.com/p/4132834b7fe7</p> <p> </p>