Skip to content

Commit

Permalink
zip -> gzip
Browse files Browse the repository at this point in the history
  • Loading branch information
peteraba committed Jan 23, 2021
1 parent 2bb36d9 commit f9d59cf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,23 @@ print(base64_decode('TnVuYyBzdXNjaXBpdCBsaWJlcm8gdmVsaXQsIHV0IHZlbmVuYXRpcyBkdWk
### GZipped Base64 encode and decode

```rust
fn zip_base64_decode(s: *const c_char) -> *mut c_char {}
fn zip_base64_encode(s: *const c_char) -> *mut c_char {}
fn gzip_base64_decode(s: *const c_char) -> *mut c_char {}
fn gzip_base64_encode(s: *const c_char) -> *mut c_char {}
```

Correct usage in python:

```python3
lib.zip_base64_encode.argtypes = (c_void_p, )
lib.zip_base64_encode.restype = c_void_p
lib.gzip_base64_encode.argtypes = (c_void_p, )
lib.gzip_base64_encode.restype = c_void_p

lib.zip_base64_decode.argtypes = (c_void_p, )
lib.zip_base64_decode.restype = c_void_p
lib.gzip_base64_decode.argtypes = (c_void_p, )
lib.gzip_base64_decode.restype = c_void_p

def zip_base64_encode(text):
def gzip_base64_encode(text):
res = ""

ptr1 = lib.zip_base64_encode(text.encode('utf-8'))
ptr1 = lib.gzip_base64_encode(text.encode('utf-8'))

try:
res = ctypes.cast(ptr1, ctypes.c_char_p).value.decode('utf-8')
Expand All @@ -108,10 +108,10 @@ def zip_base64_encode(text):

return res

def zip_base64_decode(text):
def gzip_base64_decode(text):
res = ""

ptr1 = lib.zip_base64_decode(text.encode('utf-8'))
ptr1 = lib.gzip_base64_decode(text.encode('utf-8'))

try:
res = ctypes.cast(ptr1, ctypes.c_char_p).value.decode('utf-8')
Expand All @@ -120,9 +120,9 @@ def zip_base64_decode(text):

return res

print(zip_base64_encode('abc' * 10000))
print(zip_base64_encode('Nunc suscipit libero velit, ut venenatis dui aliquet eu.'))
print(zip_base64_decode('H4sIABd1DGAAAw3GyQ2AQAwDwFZcAKIUeliWPCxF4YhN/fCbzTXR7smLQnKP58QbSS2wflXUEBuHiZG8HUJ4/QBu/ZCdOAAAAA=='))
print(gzip_base64_encode('abc' * 10000))
print(gzip_base64_encode('Nunc suscipit libero velit, ut venenatis dui aliquet eu.'))
print(gzip_base64_decode('H4sIABd1DGAAAw3GyQ2AQAwDwFZcAKIUeliWPCxF4YhN/fCbzTXR7smLQnKP58QbSS2wflXUEBuHiZG8HUJ4/QBu/ZCdOAAAAA=='))
```

### Hashing via SHA-512
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub extern "C" fn base64_decode(s: *const c_char) -> *mut c_char {
}

#[no_mangle]
pub extern "C" fn zip_base64_encode(s: *const c_char) -> *mut c_char {
pub extern "C" fn gzip_base64_encode(s: *const c_char) -> *mut c_char {
let r_str = c_str_ptr_to_rust(s);
let r_bytes = r_str.as_bytes();

Expand All @@ -71,7 +71,7 @@ pub extern "C" fn zip_base64_encode(s: *const c_char) -> *mut c_char {


#[no_mangle]
pub extern "C" fn zip_base64_decode(s: *const c_char) -> *mut c_char {
pub extern "C" fn gzip_base64_decode(s: *const c_char) -> *mut c_char {
let r_str = c_str_ptr_to_rust(s);

let buf = base64::decode_config(r_str, base64::STANDARD).unwrap();
Expand Down
20 changes: 10 additions & 10 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def compare(a, b):
lib.base64_decode.argtypes = (c_void_p, )
lib.base64_decode.restype = c_void_p

lib.zip_base64_encode.argtypes = (c_void_p, )
lib.zip_base64_encode.restype = c_void_p
lib.gzip_base64_encode.argtypes = (c_void_p, )
lib.gzip_base64_encode.restype = c_void_p

lib.zip_base64_decode.argtypes = (c_void_p, )
lib.zip_base64_decode.restype = c_void_p
lib.gzip_base64_decode.argtypes = (c_void_p, )
lib.gzip_base64_decode.restype = c_void_p

lib.sha512_hash.argtypes = (c_void_p, )
lib.sha512_hash.restype = c_void_p
Expand Down Expand Up @@ -77,10 +77,10 @@ def base64_decode(text):

return res

def zip_base64_encode(text):
def gzip_base64_encode(text):
res = ""

ptr1 = lib.zip_base64_encode(text.encode('utf-8'))
ptr1 = lib.gzip_base64_encode(text.encode('utf-8'))

try:
res = ctypes.cast(ptr1, ctypes.c_char_p).value.decode('utf-8')
Expand All @@ -89,10 +89,10 @@ def zip_base64_encode(text):

return res

def zip_base64_decode(text):
def gzip_base64_decode(text):
res = ""

ptr1 = lib.zip_base64_decode(text.encode('utf-8'))
ptr1 = lib.gzip_base64_decode(text.encode('utf-8'))

try:
res = ctypes.cast(ptr1, ctypes.c_char_p).value.decode('utf-8')
Expand Down Expand Up @@ -224,9 +224,9 @@ def post_json(url, body):
# print(post_json("https://reqbin.com/echo/post/json", '{}'))

text = 'abc' * 10000
compressed = zip_base64_encode(text)
compressed = gzip_base64_encode(text)
print(len(text) > len(compressed))
print(compare(text, zip_base64_decode(compressed)))
print(compare(text, gzip_base64_decode(compressed)))

# data = "H4sIAAAAAAAAAM1YS2+jOhTe31+Buk5iIH1MKwaJJDRFeQpopGbnG5zGEq+LIUqWs+x6pFncv5M/dm1IeEwxSUd3NM0qnPOdY3+fz7ENiuFvA7xCAxhDYee5Pvl6tYnj8AEAstogD5KOD7ed12Db2SRgZmlA7ojAoegr9S+B/hScJZgm3t8oUq0l6Mr3oijeAVGWpC+A/hVlBVRRlUiDkISNj1SZhrRFqS0VAYWzEjOB2M8sZWthKVufEHSqntRLkjB0MYoMfx28d1cgNtyVJ16LjeEuhHuazlEl8V6U5C4lUTLyI7cw7gcOZa+A018+eBUkfrxPQdfXCig91lMAF3LIuU6hh9SJNnzRTGE+s2xNWJp2p8iT+ptTaI4TIUIaWBDshS46iysYRxnHp+cj5aiBcx4ZBiSGbgqUpO4XBZQMZ8bE8V7tPQ+0uW7ZdEj22BwBHQfHOPChe2Q1QDHErjp4nmoLwzx8GwrPdl8T5PYtFZOH5isGLpCsWCQurIDUl72ySkg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