Skip to content

Commit

Permalink
etc
Browse files Browse the repository at this point in the history
  • Loading branch information
babenek committed Nov 10, 2024
1 parent 08aa445 commit 77b90fd
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
56 changes: 56 additions & 0 deletions base64magic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import base64
import re
import string
import struct
import unittest


def gen_encoded(size, text=''):
size -= 1
if 0 <= size:
for x in string.digits + string.ascii_letters:
yield from gen_encoded(size, text + x)
else:
yield text


def check_asc_or_desc(line_data_value: str) -> bool:
"""ValuePatternCheck as example"""
count_asc = 1
count_desc = 1
for i in range(len(line_data_value) - 1):
if line_data_value[i] in string.ascii_letters + string.digits \
and ord(line_data_value[i + 1]) - ord(line_data_value[i]) == 1:
count_asc += 1
if 4 == count_asc:
return True
else:
count_asc = 1
if line_data_value[i] in string.ascii_letters + string.digits \
and ord(line_data_value[i]) - ord(line_data_value[i + 1]) == 1:
count_desc += 1
if 4 == count_desc:
return True
else:
count_desc = 1
continue
return False


def main():
pattern_similar = re.compile(r"(\w)\1{3,}")
counter = 0
for x in gen_encoded(4):
v="pA5s"+x
if pattern_similar.findall(v) or check_asc_or_desc(v):
continue
outer = sum(ord(i) for i in v)
data = base64.b64decode(v)
inner1, inner2, inner3 = struct.unpack("<HHH", data)
if inner2 ^ outer == inner1 ^ inner3:
counter += 1
print(counter, v)


if """__main__""" == __name__:
main()
30 changes: 30 additions & 0 deletions crc32file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import binascii

import multiprocessing


def run(inpt):
for i in range(inpt[1] << 28, inpt[1] << 28 + 0x10000000):
aug = f"{i:08x}".encode()
data = aug + inpt[0]
crc32 = binascii.crc32(data)
if 0 == crc32:
print(data)
break
if 0 == i & 0xFFFFFF:
print(f"{i:08x}", aug)
else:
print("nope")


def main():
with open(__file__, 'rb') as f:
text = f.read()
inpt = [(text, x) for x in range(16)]
with multiprocessing.Pool(16) as p:
for _ in p.map(run, inpt):
pass


if __name__ == '__main__':
main()
30 changes: 30 additions & 0 deletions gnutar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import io
import tarfile


def main(filename):
data = bytearray()
with open(filename, 'rb') as f:
data = f.read()

with tarfile.TarFile(fileobj=io.BytesIO(data)) as tf:
for tfi in tf.getmembers():
# skip directory
if not tfi.isreg():
continue
if tfi.size > 1 << 30:
print("Quota reached")
continue
with tf.extractfile(tfi) as f:
f_seek = f.seek(io.SEEK_END)
f_tell = f.tell()
if f_tell > 1 << 30:
print("Quota reached")
continue
f.seek(io.SEEK_SET)
d = f.read()
print(len(d))


if __name__ == """__main__""":
main("gnu-sparse-big.tar")
71 changes: 71 additions & 0 deletions perf_enumerate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
MIT License
Copyright (c) 2024 Roman Babenko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import statistics
import sys
import time
from typing import List

LEN = 100
SIZE = 10000


def perf_enum_list(args):
x = args[0]
for i in range(SIZE):
for n, i in x:
assert str(n) == i


def perf_plain_list(args):
x = args[0]
for i in range(SIZE):
for n, i in enumerate(x):
assert str(n) == i


def perf_test(func, data):
stat: List[float] = []
for n in range(LEN):
start_time = time.time()
func(data)
stat.append(time.time() - start_time)
print(f"{func} Average = {statistics.mean(stat)} Deviation = {statistics.stdev(stat)}")


def main() -> int:
start_time = time.time()
l = [str(x) for x in range(10)]
n = enumerate(l)
perf_test(perf_enum_list, (n,))
perf_test(perf_plain_list, (l,))
print(f"Total time: {time.time() - start_time} SIZE={SIZE}")
return 0


# <function perf_enum_list at 0x7076afd33d90> Average = 0.00018164873123168945 Deviation = 1.3166524916387962e-05
# <function perf_plain_list at 0x7076af15bf40> Average = 0.008094820976257324 Deviation = 0.0003013285439674138
# Total time: 0.8281574249267578 SIZE=10000

if __name__ == "__main__":
sys.exit(main())

0 comments on commit 77b90fd

Please sign in to comment.