diff --git a/app/src/main/cpp/archive.c b/app/src/main/cpp/archive.c index f787851f9..54e82db02 100644 --- a/app/src/main/cpp/archive.c +++ b/app/src/main/cpp/archive.c @@ -195,8 +195,8 @@ static int archive_get_ctx(archive_ctx **ctxptr, int idx) { JNIEXPORT jint JNICALL Java_com_hippo_UriArchiveAccessor_openArchive(JNIEnv *env, jobject thiz, jint fd, jlong size) { - (void)env; /* UNUSED */ - (void)thiz; /* UNUSED */ + (void) env; /* UNUSED */ + (void) thiz; /* UNUSED */ archive_ctx *ctx = NULL; archiveAddr = mmap64(0, size, PROT_READ, MAP_PRIVATE, fd, 0); if (archiveAddr == MAP_FAILED) { @@ -246,51 +246,40 @@ Java_com_hippo_UriArchiveAccessor_openArchive(JNIEnv *env, jobject thiz, jint fd return r; } -typedef struct Memarea { - void *buffer; - long size; -} Memarea; - JNIEXPORT jlong JNICALL Java_com_hippo_UriArchiveAccessor_extractToAddr(JNIEnv *env, jobject thiz, jint index) { - (void)env; /* UNUSED */ - (void)thiz; /* UNUSED */ + (void) env; /* UNUSED */ + (void) thiz; /* UNUSED */ archive_ctx *ctx = NULL; int ret; ret = archive_get_ctx(&ctx, index); if (ret) return 0; - Memarea *memarea = malloc(sizeof(Memarea)); - if (!memarea) { + long long size = archive_entry_size(ctx->entry); + void *buffer = malloc(size + sizeof(long long)); + *(long long *) buffer = size; + if (!buffer) { ctx->using = 0; LOGE("Allocate buffer for decompression failed:ENOMEM"); + free(buffer); return 0; } - memarea->size = (long) archive_entry_size(ctx->entry); - memarea->buffer = malloc(memarea->size); - if (!memarea->buffer) { - ctx->using = 0; - LOGE("Allocate buffer for decompression failed:ENOMEM"); - free(memarea); - return 0; - } - ret = archive_read_data(ctx->arc, memarea->buffer, memarea->size); + ret = archive_read_data(ctx->arc, buffer + sizeof(long long), size); ctx->using = 0; - if (ret == memarea->size) - return (jlong) memarea; - if (ret != memarea->size) + if (ret == size) + return (jlong) buffer; + if (ret != size) LOGE("%s", "No enough data read, WTF?"); if (ret < 0) LOGE("%s%s", "Archive read failed:", archive_error_string(ctx->arc)); - free(memarea->buffer); - free(memarea); + free(buffer); return 0; } JNIEXPORT void JNICALL Java_com_hippo_UriArchiveAccessor_closeArchive(JNIEnv *env, jobject thiz) { - (void)env; /* UNUSED */ - (void)thiz; /* UNUSED */ + (void) env; /* UNUSED */ + (void) thiz; /* UNUSED */ for (int i = 0; i < CTX_POOL_SIZE; i++) archive_release_ctx(ctx_pool[i]); free(passwd); @@ -303,14 +292,14 @@ Java_com_hippo_UriArchiveAccessor_closeArchive(JNIEnv *env, jobject thiz) { JNIEXPORT jboolean JNICALL Java_com_hippo_UriArchiveAccessor_needPassword(JNIEnv *env, jobject thiz) { - (void)env; /* UNUSED */ - (void)thiz; /* UNUSED */ + (void) env; /* UNUSED */ + (void) thiz; /* UNUSED */ return need_encrypt; } JNIEXPORT jboolean JNICALL Java_com_hippo_UriArchiveAccessor_providePassword(JNIEnv *env, jobject thiz, jstring str) { - (void)thiz; /* UNUSED */ + (void) thiz; /* UNUSED */ struct archive_entry *entry; archive_ctx *ctx; jboolean ret = true; @@ -340,8 +329,8 @@ Java_com_hippo_UriArchiveAccessor_providePassword(JNIEnv *env, jobject thiz, jst JNIEXPORT jstring JNICALL Java_com_hippo_UriArchiveAccessor_getFilename(JNIEnv *env, jobject thiz, jint index) { - (void)env; /* UNUSED */ - (void)thiz; /* UNUSED */ + (void) env; /* UNUSED */ + (void) thiz; /* UNUSED */ archive_ctx *ctx = NULL; int ret; ret = archive_get_ctx(&ctx, index); @@ -353,9 +342,9 @@ Java_com_hippo_UriArchiveAccessor_getFilename(JNIEnv *env, jobject thiz, jint in } JNIEXPORT void JNICALL -Java_com_hippo_UriArchiveAccessor_extractToFd(JNIEnv *env, jobject thiz,jint index, jint fd) { - (void)env; /* UNUSED */ - (void)thiz; /* UNUSED */ +Java_com_hippo_UriArchiveAccessor_extractToFd(JNIEnv *env, jobject thiz, jint index, jint fd) { + (void) env; /* UNUSED */ + (void) thiz; /* UNUSED */ archive_ctx *ctx = NULL; int ret; ret = archive_get_ctx(&ctx, index); diff --git a/app/src/main/cpp/image.c b/app/src/main/cpp/image.c index 8eda55267..61b619b01 100644 --- a/app/src/main/cpp/image.c +++ b/app/src/main/cpp/image.c @@ -84,11 +84,6 @@ static void recycle(IMAGE *image) { free(image); } -typedef struct Memarea { - void *buffer; - long size; -} Memarea; - bool copy_pixels(const void *src, int src_w, int src_h, int src_x, int src_y, void *dst, int dst_w, int dst_h, int dst_x, int dst_y, int width, int height) { @@ -159,17 +154,17 @@ bool copy_pixels(const void *src, int src_w, int src_h, int src_x, int src_y, dst_blank_length = (size_t) (dst_y * dst_w + dst_x) * 4; // First line - dst_pos += (int)dst_blank_length; + dst_pos += (int) dst_blank_length; memcpy(dst + dst_pos, src + src_pos, line_stride); - dst_pos += (int)line_stride; + dst_pos += (int) line_stride; src_pos += src_stride; // Other lines dst_blank_length = (size_t) ((dst_w - width) * 4); for (line = 1; line < height; line++) { - dst_pos += (int)dst_blank_length; + dst_pos += (int) dst_blank_length; memcpy(dst + dst_pos, src + src_pos, line_stride); - dst_pos += (int)line_stride; + dst_pos += (int) line_stride; src_pos += src_stride; } @@ -216,9 +211,9 @@ IMAGE *createFromFd(int fd) { return image; } -IMAGE *createFromAddr(void *addr, long size) { +IMAGE *createFromAddr(void *addr, long long size) { AImageDecoder *decoder; - AImageDecoder_createFromBuffer((const void *) addr, size, &decoder); + AImageDecoder_createFromBuffer((const void *) addr + sizeof(long long), size, &decoder); IMAGE *image = calloc(1, sizeof(IMAGE)); SOURCE *src = calloc(1, sizeof(SOURCE)); src->decoder = decoder; @@ -278,7 +273,7 @@ Java_com_hippo_image_Image_nativeCreate(JNIEnv *env, jclass clazz, jobject bitma AndroidBitmap_getInfo(env, bitmap, &info); AndroidBitmap_lockPixels(env, bitmap, &pixels); - image = create((int)info.width, (int)info.height, pixels); + image = create((int) info.width, (int) info.height, pixels); AndroidBitmap_unlockPixels(env, bitmap); return (jlong) image; } @@ -293,7 +288,8 @@ Java_com_hippo_image_Image_nativeRender(JNIEnv *env, jclass clazz, jlong ptr, ji AndroidBitmap_getInfo(env, dst, &info); AndroidBitmap_lockPixels(env, dst, &pixels); - render((IMAGE *) ptr, src_x, src_y, pixels, (int)info.width, (int)info.height, dst_x, dst_y, width, + render((IMAGE *) ptr, src_x, src_y, pixels, (int) info.width, (int) info.height, dst_x, dst_y, + width, height); AndroidBitmap_unlockPixels(env, dst); @@ -338,9 +334,7 @@ Java_com_hippo_image_Image_nativeRecycle(JNIEnv *env, jclass clazz, jlong ptr) { JNIEXPORT jlong JNICALL Java_com_hippo_image_Image_nativeDecodeAddr(JNIEnv *env, jclass clazz, jlong addr) { - Memarea *memarea = (Memarea *) addr; - IMAGE *image = createFromAddr(memarea->buffer, memarea->size); - free(memarea); + IMAGE *image = createFromAddr((void *) addr, *(long long *) addr); return (jlong) image; }