Skip to content

Commit

Permalink
update libbase16384
Browse files Browse the repository at this point in the history
  • Loading branch information
synodriver committed Apr 4, 2024
1 parent 038dee3 commit 7bfa276
Show file tree
Hide file tree
Showing 10 changed files with 3,196 additions and 699 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ lib/
*.lib
*.a
*.so
*.egg-info
*.egg-info
*.whl
2 changes: 1 addition & 1 deletion base16384
Submodule base16384 updated 9 files
+2 −1 CMakeLists.txt
+44 −29 README.md
+106 −46 base1432.c
+99 −46 base1464.c
+20 −4 base16384.1
+73 −52 base16384.c
+144 −34 base16384.h
+90 −0 binary.h
+198 −59 file.c
87 changes: 67 additions & 20 deletions pybase16384/backends/cffi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
encode_len = lib.base16384_encode_len
decode_len = lib.base16384_decode_len

ENCBUFSZ = lib.get_encsize()
DECBUFSZ = lib.get_decsize()
FLAG_NOHEADER = lib.BASE16384_FLAG_NOHEADER_()
FLAG_SUM_CHECK_ON_REMAIN = lib.BASE16384_FLAG_SUM_CHECK_ON_REMAIN_()
SIMPLE_SUM_INIT_VALUE = lib.BASE16384_SIMPLE_SUM_INIT_VALUE_()

# -----------------low level api------------------------------
def _encode(data: bytes) -> bytes:
Expand All @@ -24,9 +29,7 @@ def _encode(data: bytes) -> bytes:


def _encode_into(data: bytes, out: bytearray) -> int:
return lib.base16384_encode(
ffi.from_buffer(data), len(data), ffi.from_buffer(out)
)
return lib.base16384_encode(ffi.from_buffer(data), len(data), ffi.from_buffer(out))


def _decode(data: bytes) -> bytes:
Expand All @@ -40,9 +43,7 @@ def _decode(data: bytes) -> bytes:


def _decode_into(data: bytes, out: bytearray) -> int:
return lib.base16384_decode(
ffi.from_buffer(data), len(data), ffi.from_buffer(out)
)
return lib.base16384_decode(ffi.from_buffer(data), len(data), ffi.from_buffer(out))


def is_64bits() -> bool:
Expand Down Expand Up @@ -92,9 +93,7 @@ def encode_file(input: IO, output: IO, write_head: bool = False, buf_rate: int =
input.seek(-size, 1)
continue

count = lib.base16384_encode(
ffi.from_buffer(chunk), size, output_buf
)
count = lib.base16384_encode(ffi.from_buffer(chunk), size, output_buf)
output.write(ffi.unpack(output_buf, count))
if size < 7:
break
Expand Down Expand Up @@ -146,9 +145,7 @@ def decode_file(input: IO, output: IO, buf_rate: int = 10):
else:
input.seek(-2, 1)

count = lib.base16384_decode(
ffi.from_buffer(chunk), size, output_buf
)
count = lib.base16384_decode(ffi.from_buffer(chunk), size, output_buf)
output.write(ffi.unpack(output_buf, count))


Expand Down Expand Up @@ -176,13 +173,23 @@ def err_to_str(ret) -> str:
return "base16384_err_open_input_file"
elif ret == lib.base16384_err_map_input_file:
return "base16384_err_map_input_file"
elif ret == lib.base16384_err_read_file:
return "base16384_err_read_file"
elif ret == lib.base16384_err_invalid_file_name:
return "base16384_err_invalid_file_name"
elif ret == lib.base16384_err_invalid_file_name:
return "base16384_err_invalid_file_name"
elif ret == lib.base16384_err_invalid_commandline_parameter:
return "base16384_err_invalid_commandline_parameter"
elif ret == lib.base16384_err_invalid_decoding_checksum:
return "base16384_err_invalid_decoding_checksum"


def encode_local_file(inp, out) -> None:
inp_name: bytes = ensure_bytes(inp)
out_name: bytes = ensure_bytes(out)
encbuf = ffi.new(f"char[{lib.get_encsize()}]")
decbuf = ffi.new(f"char[{lib.get_decsize()}]")
encbuf = ffi.new(f"char[{ENCBUFSZ}]")
decbuf = ffi.new(f"char[{DECBUFSZ}]")
ret = lib.base16384_encode_file(
ffi.from_buffer(inp_name), ffi.from_buffer(out_name), encbuf, decbuf
)
Expand All @@ -193,8 +200,8 @@ def encode_local_file(inp, out) -> None:
def decode_local_file(inp, out) -> None:
inp_name: bytes = ensure_bytes(inp)
out_name: bytes = ensure_bytes(out)
encbuf = ffi.new(f"char[{lib.get_encsize()}]")
decbuf = ffi.new(f"char[{lib.get_decsize()}]")
encbuf = ffi.new(f"char[{ENCBUFSZ}]")
decbuf = ffi.new(f"char[{DECBUFSZ}]")
ret = lib.base16384_decode_file(
ffi.from_buffer(inp_name), ffi.from_buffer(out_name), encbuf, decbuf
)
Expand All @@ -203,16 +210,56 @@ def decode_local_file(inp, out) -> None:


def encode_fd(inp: int, out: int) -> None:
encbuf = ffi.new(f"char[{lib.get_encsize()}]")
decbuf = ffi.new(f"char[{lib.get_decsize()}]")
encbuf = ffi.new(f"char[{ENCBUFSZ}]")
decbuf = ffi.new(f"char[{DECBUFSZ}]")
ret = lib.base16384_encode_fd(inp, out, encbuf, decbuf)
if ret != lib.base16384_err_ok:
raise ValueError(err_to_str(ret))


def decode_fd(inp: int, out: int) -> None:
encbuf = ffi.new(f"char[{lib.get_encsize()}]")
decbuf = ffi.new(f"char[{lib.get_decsize()}]")
encbuf = ffi.new(f"char[{ENCBUFSZ}]")
decbuf = ffi.new(f"char[{DECBUFSZ}]")
ret = lib.base16384_decode_fd(inp, out, encbuf, decbuf)
if ret != lib.base16384_err_ok:
raise ValueError(err_to_str(ret))

# detail
def encode_local_file_detailed(inp, out, flag: int) -> None:
inp_name: bytes = ensure_bytes(inp)
out_name: bytes = ensure_bytes(out)
encbuf = ffi.new(f"char[{ENCBUFSZ}]")
decbuf = ffi.new(f"char[{DECBUFSZ}]")
ret = lib.base16384_encode_file_detailed(
ffi.from_buffer(inp_name), ffi.from_buffer(out_name), encbuf, decbuf, flag
)
if ret != lib.base16384_err_ok:
raise ValueError(err_to_str(ret))


def decode_local_file_detailed(inp, out, flag: int) -> None:
inp_name: bytes = ensure_bytes(inp)
out_name: bytes = ensure_bytes(out)
encbuf = ffi.new(f"char[{ENCBUFSZ}]")
decbuf = ffi.new(f"char[{DECBUFSZ}]")
ret = lib.base16384_decode_file_detailed(
ffi.from_buffer(inp_name), ffi.from_buffer(out_name), encbuf, decbuf, flag
)
if ret != lib.base16384_err_ok:
raise ValueError(err_to_str(ret))


def encode_fd_detailed(inp: int, out: int, flag: int) -> None:
encbuf = ffi.new(f"char[{ENCBUFSZ}]")
decbuf = ffi.new(f"char[{DECBUFSZ}]")
ret = lib.base16384_encode_fd_detailed(inp, out, encbuf, decbuf, flag)
if ret != lib.base16384_err_ok:
raise ValueError(err_to_str(ret))


def decode_fd_detailed(inp: int, out: int, flag: int) -> None:
encbuf = ffi.new(f"char[{ENCBUFSZ}]")
decbuf = ffi.new(f"char[{DECBUFSZ}]")
ret = lib.base16384_decode_fd_detailed(inp, out, encbuf, decbuf, flag)
if ret != lib.base16384_err_ok:
raise ValueError(err_to_str(ret))
36 changes: 36 additions & 0 deletions pybase16384/backends/cffi/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

if CPUBIT == 64:
macro_base.append(("CPUBIT64", None))
macro_base.append(("IS_64BIT_PROCESSOR", None))
else:
macro_base.append(("CPUBIT32", None))

Expand All @@ -41,6 +42,10 @@
base16384_err_write_file,
base16384_err_open_input_file,
base16384_err_map_input_file,
base16384_err_read_file,
base16384_err_invalid_file_name,
base16384_err_invalid_commandline_parameter,
base16384_err_invalid_decoding_checksum
};
// base16384_err_t is the return value of base16384_en/decode_file
typedef enum base16384_err_t base16384_err_t;
Expand All @@ -67,11 +72,27 @@
// encbuf & decbuf must be no less than BASE16384_ENCBUFSZ & BASE16384_DECBUFSZ
base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* decbuf);
int base16384_encode_unsafe(const char * data, int dlen, char * buf);
int base16384_decode_unsafe(const char * data, int dlen, char * buf);
base16384_err_t base16384_encode_file_detailed(const char* input, const char* output, char* encbuf, char* decbuf, int flag);
base16384_err_t base16384_decode_file_detailed(const char* input, const char* output, char* encbuf, char* decbuf, int flag);
base16384_err_t base16384_encode_fd_detailed(int input, int output, char* encbuf, char* decbuf, int flag);
base16384_err_t base16384_decode_fd_detailed(int input, int output, char* encbuf, char* decbuf, int flag);
base16384_err_t base16384_encode_fp_detailed(FILE* input, FILE* output, char* encbuf, char* decbuf, int flag);
base16384_err_t base16384_decode_fp_detailed(FILE* input, FILE* output, char* encbuf, char* decbuf, int flag);
int32_t pybase16384_64bits();
int get_encsize();
int get_decsize();
int BASE16384_FLAG_NOHEADER_();
int BASE16384_FLAG_SUM_CHECK_ON_REMAIN_();
int BASE16384_SIMPLE_SUM_INIT_VALUE_();
"""
)

Expand All @@ -93,6 +114,21 @@
{
return BASE16384_DECBUFSZ;
}
int BASE16384_FLAG_NOHEADER_()
{
return BASE16384_FLAG_NOHEADER;
}
int BASE16384_FLAG_SUM_CHECK_ON_REMAIN_()
{
return BASE16384_FLAG_SUM_CHECK_ON_REMAIN;
}
int BASE16384_SIMPLE_SUM_INIT_VALUE_()
{
return BASE16384_SIMPLE_SUM_INIT_VALUE;
}
"""

ffibuilder.set_source(
Expand Down
Loading

0 comments on commit 7bfa276

Please sign in to comment.