1
0
mirror of https://github.com/fumiama/android-base16384.git synced 2026-06-09 04:30:16 +08:00
This commit is contained in:
fumiama
2020-12-16 23:16:28 +08:00
parent c39bed681c
commit 25168a6556
42 changed files with 1363 additions and 21 deletions

View File

@@ -0,0 +1,48 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("base16384")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
base16384
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
base16384.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
base16384
# Links the target library to the log library
# included in the NDK.
${log-lib} )

View File

@@ -0,0 +1,28 @@
#include <jni.h>
#include <cstring>
#include <cstdio>
#include "base16384.h"
FILE *fp, *fpo;
#define execute(function){\
const char *inputFileDir = env->GetStringUTFChars(sf, JNI_FALSE);\
const char *outputFileDir = env->GetStringUTFChars(df, JNI_FALSE);\
fp = fpo = nullptr;\
fp = fopen(inputFileDir, "rb");\
fpo = fopen(outputFileDir, "wb");\
if(fp != nullptr && fpo != nullptr){\
function(fp, fpo);\
env->ReleaseStringUTFChars(sf, inputFileDir);\
env->ReleaseStringUTFChars(df, outputFileDir);\
fclose(fp);\
fclose(fpo);\
return 0;\
}else return 1;\
}
extern "C" JNIEXPORT int JNICALL
Java_top_fumiama_base16384_MainActivity_encode(JNIEnv* env, jobject, jstring sf, jstring df) execute(encode)
extern "C" JNIEXPORT int JNICALL
Java_top_fumiama_base16384_MainActivity_decode(JNIEnv* env, jobject, jstring sf, jstring df) execute(decode)

View File

@@ -0,0 +1,78 @@
//
// Created by rumia on 2020/12/16.
//
#ifndef BASE16384_BASE16384_H
#define BASE16384_BASE16384_H
//base16384.h
//MIT fumiama 20200416
#include "bitio.h"
#define HEAD 0x4E
BIT outbuf;
unsigned char buf[BUFSIZ];
void push(int istrue, FILE *fpo, int offset){
if(!~pushbit(&outbuf, istrue)){
if(offset) for(int i = 0; i < BITBUFSIZE; i += 2) outbuf.b[i] += offset;
fwrite(outbuf.b, sizeof(char), BITBUFSIZE, fpo);
outbuf.p = 0;
pushbit(&outbuf, istrue);
}
}
#define setoffset(x, offset){\
char flag = 0;\
ch1 = ch2 = x;\
while(!feof(fp) && (ch1 = fgetc(fp)) == x && (ch2 = fgetc(fp)) == x) {cnt8 -= offset; flag++;}\
if(ch2 != x) {ungetc(ch2, fp); ch2 = x;}\
if(ch1 != x) {ungetc(ch1, fp); ch1 = x;}\
while(feof(fp) && buf[cnt - 1] == x && buf[cnt - 2] == x) {cnt8 -= offset; cnt -= 2; flag++;}\
if(flag) cnt8 -= 8;\
}
void encode(FILE *fp, FILE *fpo){
int cnt;
memset(&outbuf, 0, sizeof(BIT));
fputc(0xFE, fpo);
fputc(0xFF, fpo);
while((cnt = fread(buf, sizeof(char), BUFSIZ, fp))){
for(int i = 0; i < cnt * 8; i++){
if(!(outbuf.p % 16)){
push(0, fpo, HEAD);
push(0, fpo, HEAD);
}
push(buf[i / 8] & (128u >> (i % 8)), fpo, HEAD);
}
}
if(outbuf.p){
outbuf.b[outbuf.p / 8] &= ~(255u >> outbuf.p % 8);
for(int i = 0; i < outbuf.p / 8 + 1; i += 2) outbuf.b[i] += HEAD;
fwrite(outbuf.b, sizeof(char), (outbuf.p % 8)?outbuf.p / 8 + 1:outbuf.p / 8 , fpo);
if(((outbuf.p % 8)?outbuf.p / 8 + 1:outbuf.p / 8) % 2){
fputc(0, fpo);
fputc('>', fpo);
fputc('>', fpo);
}
for(int i = 0; i < outbuf.p % 8; i++) fputc('=', fpo);
}
}
void decode(FILE *fp, FILE *fpo){
int cnt, cnt8;
char ch1, ch2;
memset(&outbuf, 0, sizeof(BIT));
while((cnt = fread(buf, sizeof(char), BUFSIZ, fp))){
cnt8 = cnt * 8;
setoffset('=', 14)
setoffset('>', 16)
for(int i = ((buf[0] == 0xFE)?16:0); i < cnt8; i++){
if(!(i % 16)){
buf[i / 8] -= HEAD;
i += 2;
}
push(buf[i / 8] & (128u >> (i % 8)), fpo, 0);
}
}
if(outbuf.p) fwrite(outbuf.b, sizeof(char), (outbuf.p % 8)?outbuf.p / 8 + 1:outbuf.p / 8, fpo);
}
#endif //BASE16384_BASE16384_H

53
app/src/main/cpp/bitio.h Normal file
View File

@@ -0,0 +1,53 @@
//
// Created by rumia on 2020/12/16.
//
#ifndef BASE16384_BITIO_H
#define BASE16384_BITIO_H
//bitio.h
//made by fumiama
//20200413
#ifndef BITBUFSIZE
#define BITBUFSIZE 1024
#endif
struct BIT{
char b[BITBUFSIZE];
int p;
};
typedef struct BIT BIT;
int pushbit(BIT *buffer, const int isture){
if(buffer->p >= BITBUFSIZE * 8) return EOF;
else if(isture) buffer->b[buffer->p / 8] |= 128u >> buffer->p % 8;
else buffer->b[buffer->p / 8] &= ~(128u >> buffer->p % 8);
buffer->p++;
return isture;
}
int fpushbit(BIT *buffer, FILE *fp){
memset(buffer, 0, sizeof(BIT));
if((buffer->p = fread(buffer->b, sizeof(char), BITBUFSIZE, fp)) && feof(fp))
buffer->p = (buffer->p - 2) * 8 + buffer->b[buffer->p - 1] + 1;
else buffer->p *= 8;
return buffer->p;
}
int changebit(BIT *buffer, const int isture, const int position){
if(position >= buffer->p) return EOF;
else if(isture) buffer->b[position / 8] |= 128u >> position % 8;
else buffer->b[position / 8] &= ~(128u >> position % 8);
buffer->p++;
return isture;
}
int readbit(const BIT *buffer, const int position){
if(position >= buffer->p) return EOF;
else return buffer->b[position / 8] & (128u >> position % 8);
}
int popbit(BIT *buffer){
if(buffer->p >= BITBUFSIZE * 8 || buffer->p < 0) return EOF;
buffer->p--;
return buffer->b[(buffer->p+1) / 8] & (128u >> (buffer->p+1) % 8);
}
#endif //BASE16384_BITIO_H