1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
react-native-pushy/android/jni/DownloadTask.c

57 lines
2.3 KiB
C
Raw Normal View History

2016-03-31 22:54:29 +08:00
//
// Created by DengYun on 3/31/16.
//
#include "cn_reactnative_modules_update_DownloadTask.h"
2021-04-05 11:32:01 +08:00
#include "hpatch.h"
2021-04-04 15:50:40 +08:00
#define _check(v,errInfo) do{ if (!(v)) { _isError=hpatch_TRUE; _errInfo=errInfo; goto _clear; } }while(0)
2021-04-04 12:17:57 +08:00
JNIEXPORT jbyteArray JNICALL Java_cn_reactnative_modules_update_DownloadTask_hdiffPatch
(JNIEnv *env, jobject self, jbyteArray origin, jbyteArray patch){
2021-04-04 15:50:40 +08:00
hpatch_BOOL _isError=hpatch_FALSE;
const char* _errInfo="";
jbyte* originPtr = (*env)->GetByteArrayElements(env, origin, NULL);
size_t originLength = (*env)->GetArrayLength(env, origin);
jbyte* patchPtr = (*env)->GetByteArrayElements(env, patch, NULL);
size_t patchLength = (*env)->GetArrayLength(env, patch);
jbyteArray ret = NULL;
jbyte* outPtr = NULL;
size_t newsize = 0;
hpatch_singleCompressedDiffInfo patInfo;
_check(((originLength==0)||originPtr) && patchPtr && (patchLength>0),"Corrupt patch");
2021-04-06 00:13:53 +08:00
_check(kHPatch_ok==hpatch_getInfo_by_mem(&patInfo,(const uint8_t*)patchPtr,patchLength),"Error info in hpatch");
2021-04-04 15:50:40 +08:00
_check(originLength==patInfo.oldDataSize,"Error oldDataSize in hpatch");
newsize=(size_t)patInfo.newDataSize;
if (sizeof(size_t)!=sizeof(hpatch_StreamPos_t))
_check(newsize==patInfo.newDataSize,"Error newDataSize in hpatch");
ret = (*env)->NewByteArray(env,newsize);
_check(ret,"Error JNIEnv::NewByteArray()");
if (newsize>0) {
outPtr = (*env)->GetByteArrayElements(env, ret, NULL);
_check(outPtr,"Corrupt JNIEnv::GetByteArrayElements");
}
2021-04-06 00:13:53 +08:00
_check(kHPatch_ok==hpatch_by_mem((const uint8_t*)originPtr,originLength,(uint8_t*)outPtr,newsize,
2021-04-05 13:40:38 +08:00
(const uint8_t*)patchPtr,patchLength,&patInfo),"hpacth");
2021-04-04 15:50:40 +08:00
_clear:
if (outPtr) (*env)->ReleaseByteArrayElements(env, ret, outPtr, (_isError?JNI_ABORT:0));
if (originPtr) (*env)->ReleaseByteArrayElements(env, origin, originPtr, JNI_ABORT);
if (patchPtr) (*env)->ReleaseByteArrayElements(env, patch, patchPtr, JNI_ABORT);
if (_isError){
jclass newExcCls = NULL;
if (ret){
(*env)->DeleteLocalRef(env, ret);
ret = NULL;
}
newExcCls = (*env)->FindClass(env, "java/lang/Error");
2021-04-05 11:32:01 +08:00
if (newExcCls != NULL) // Unable to find the new exception class, give up.
2021-04-04 15:50:40 +08:00
(*env)->ThrowNew(env, newExcCls, _errInfo);
}
return ret;
2021-04-05 11:32:01 +08:00
}