mirror of
https://github.com/nillerusr/source-engine.git
synced 2026-08-09 18:29:35 +00:00
touch: add new commands
This commit is contained in:
Executable
BIN
Binary file not shown.
Executable
+331
@@ -0,0 +1,331 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_strm_mem.h"
|
||||
#include "mz_strm_bzip.h"
|
||||
#include "mz_strm_crypt.h"
|
||||
#include "mz_strm_aes.h"
|
||||
#include "mz_strm_zlib.h"
|
||||
#include "mz_zip.h"
|
||||
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password)
|
||||
{
|
||||
char buf[UINT16_MAX];
|
||||
int16_t read = 0;
|
||||
int16_t written = 0;
|
||||
void *out_stream = NULL;
|
||||
void *in_stream = NULL;
|
||||
void *crypt_out_stream = NULL;
|
||||
char filename[120];
|
||||
|
||||
mz_stream_os_create(&in_stream);
|
||||
|
||||
if (mz_stream_os_open(in_stream, "LICENSE", MZ_OPEN_MODE_READ) == MZ_OK)
|
||||
{
|
||||
read = mz_stream_os_read(in_stream, buf, UINT16_MAX);
|
||||
mz_stream_os_close(in_stream);
|
||||
}
|
||||
|
||||
mz_stream_os_delete(&in_stream);
|
||||
mz_stream_os_create(&out_stream);
|
||||
|
||||
snprintf(filename, sizeof(filename), "LICENSE.encrypt.%s", method);
|
||||
if (mz_stream_os_open(out_stream, filename, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE) == MZ_OK)
|
||||
{
|
||||
crypt_create(&crypt_out_stream);
|
||||
|
||||
mz_stream_set_base(crypt_out_stream, out_stream);
|
||||
|
||||
if (mz_stream_open(crypt_out_stream, password, MZ_OPEN_MODE_WRITE) == MZ_OK)
|
||||
{
|
||||
written = mz_stream_write(crypt_out_stream, buf, read);
|
||||
mz_stream_close(crypt_out_stream);
|
||||
}
|
||||
|
||||
mz_stream_delete(&crypt_out_stream);
|
||||
|
||||
mz_stream_os_close(out_stream);
|
||||
|
||||
printf("%s encrypted %d\n", filename, written);
|
||||
}
|
||||
|
||||
mz_stream_os_delete(&out_stream);
|
||||
mz_stream_os_create(&in_stream);
|
||||
|
||||
if (mz_stream_os_open(in_stream, filename, MZ_OPEN_MODE_READ) == MZ_OK)
|
||||
{
|
||||
crypt_create(&crypt_out_stream);
|
||||
|
||||
mz_stream_set_base(crypt_out_stream, in_stream);
|
||||
|
||||
if (mz_stream_open(crypt_out_stream, password, MZ_OPEN_MODE_READ) == MZ_OK)
|
||||
{
|
||||
read = mz_stream_read(crypt_out_stream, buf, read);
|
||||
mz_stream_close(crypt_out_stream);
|
||||
}
|
||||
|
||||
mz_stream_delete(&crypt_out_stream);
|
||||
|
||||
mz_stream_os_close(in_stream);
|
||||
|
||||
printf("%s decrypted %d\n", filename, read);
|
||||
}
|
||||
|
||||
mz_stream_os_delete(&in_stream);
|
||||
mz_stream_os_create(&out_stream);
|
||||
|
||||
snprintf(filename, sizeof(filename), "LICENSE.decrypt.%s", method);
|
||||
if (mz_stream_os_open(out_stream, filename, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE) == MZ_OK)
|
||||
{
|
||||
mz_stream_os_write(out_stream, buf, read);
|
||||
mz_stream_os_close(out_stream);
|
||||
}
|
||||
|
||||
mz_stream_os_delete(&out_stream);
|
||||
}
|
||||
|
||||
void test_compress(char *method, mz_stream_create_cb create_compress)
|
||||
{
|
||||
char buf[UINT16_MAX];
|
||||
int16_t read = 0;
|
||||
uint64_t total_in = 0;
|
||||
uint64_t total_out = 0;
|
||||
void *crc_in_stream = NULL;
|
||||
void *in_stream = NULL;
|
||||
void *out_stream = NULL;
|
||||
void *deflate_stream = NULL;
|
||||
void *inflate_stream = NULL;
|
||||
uint32_t crc32 = 0;
|
||||
char filename[120];
|
||||
|
||||
printf("Testing compress %s\n", method);
|
||||
|
||||
mz_stream_os_create(&in_stream);
|
||||
|
||||
if (mz_stream_os_open(in_stream, "LICENSE", MZ_OPEN_MODE_READ) == MZ_OK)
|
||||
{
|
||||
|
||||
mz_stream_crc32_create(&crc_in_stream);
|
||||
mz_stream_set_base(crc_in_stream, in_stream);
|
||||
mz_stream_crc32_open(crc_in_stream, NULL, MZ_OPEN_MODE_READ);
|
||||
read = mz_stream_read(crc_in_stream, buf, UINT16_MAX);
|
||||
crc32 = mz_stream_crc32_get_value(crc_in_stream);
|
||||
mz_stream_close(crc_in_stream);
|
||||
mz_stream_crc32_delete(&crc_in_stream);
|
||||
|
||||
mz_stream_os_close(in_stream);
|
||||
}
|
||||
|
||||
mz_stream_os_delete(&in_stream);
|
||||
|
||||
if (read < 0)
|
||||
{
|
||||
printf("Failed to read LICENSE\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("LICENSE crc 0x%08x\n", crc32);
|
||||
|
||||
mz_stream_os_create(&out_stream);
|
||||
|
||||
snprintf(filename, sizeof(filename), "LICENSE.deflate.%s", method);
|
||||
if (mz_stream_os_open(out_stream, filename, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE) == MZ_OK)
|
||||
{
|
||||
create_compress(&deflate_stream);
|
||||
mz_stream_set_base(deflate_stream, out_stream);
|
||||
|
||||
mz_stream_open(deflate_stream, NULL, MZ_OPEN_MODE_WRITE);
|
||||
mz_stream_write(deflate_stream, buf, read);
|
||||
mz_stream_close(deflate_stream);
|
||||
|
||||
mz_stream_get_prop_int64(deflate_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
|
||||
mz_stream_get_prop_int64(deflate_stream, MZ_STREAM_PROP_TOTAL_OUT, &total_out);
|
||||
|
||||
mz_stream_delete(&deflate_stream);
|
||||
|
||||
printf("%s compressed from %u to %u\n", filename, (uint32_t)total_in, (uint32_t)total_out);
|
||||
|
||||
mz_stream_os_close(out_stream);
|
||||
}
|
||||
|
||||
mz_stream_os_delete(&out_stream);
|
||||
mz_stream_os_create(&in_stream);
|
||||
|
||||
if (mz_stream_os_open(in_stream, filename, MZ_OPEN_MODE_READ) == MZ_OK)
|
||||
{
|
||||
create_compress(&inflate_stream);
|
||||
mz_stream_set_base(inflate_stream, in_stream);
|
||||
|
||||
mz_stream_open(inflate_stream, NULL, MZ_OPEN_MODE_READ);
|
||||
read = mz_stream_read(inflate_stream, buf, UINT16_MAX);
|
||||
mz_stream_close(inflate_stream);
|
||||
|
||||
mz_stream_get_prop_int64(inflate_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
|
||||
mz_stream_get_prop_int64(inflate_stream, MZ_STREAM_PROP_TOTAL_OUT, &total_out);
|
||||
|
||||
mz_stream_delete(&inflate_stream);
|
||||
|
||||
mz_stream_os_close(in_stream);
|
||||
|
||||
printf("%s uncompressed from %u to %u\n", filename, (uint32_t)total_in, (uint32_t)total_out);
|
||||
}
|
||||
|
||||
mz_stream_os_delete(&in_stream);
|
||||
mz_stream_os_create(&out_stream);
|
||||
|
||||
snprintf(filename, sizeof(filename), "LICENSE.inflate.%s", method);
|
||||
if (mz_stream_os_open(out_stream, filename, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE) == MZ_OK)
|
||||
{
|
||||
mz_stream_crc32_create(&crc_in_stream);
|
||||
mz_stream_crc32_open(crc_in_stream, NULL, MZ_OPEN_MODE_WRITE);
|
||||
|
||||
mz_stream_set_base(crc_in_stream, in_stream);
|
||||
if (mz_stream_write(crc_in_stream, buf, read) != read)
|
||||
printf("Failed to write %s\n", filename);
|
||||
|
||||
crc32 = mz_stream_crc32_get_value(crc_in_stream);
|
||||
|
||||
mz_stream_close(crc_in_stream);
|
||||
mz_stream_delete(&crc_in_stream);
|
||||
|
||||
mz_stream_os_close(out_stream);
|
||||
|
||||
printf("%s crc 0x%08x\n", filename, crc32);
|
||||
}
|
||||
|
||||
mz_stream_os_delete(&out_stream);
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void test_aes()
|
||||
{
|
||||
test_encrypt("aes", mz_stream_aes_create, "hello");
|
||||
}
|
||||
|
||||
void test_crypt()
|
||||
{
|
||||
test_encrypt("crypt", mz_stream_crypt_create, "hello");
|
||||
}
|
||||
|
||||
void test_zlib()
|
||||
{
|
||||
test_compress("zlib", mz_stream_zlib_create);
|
||||
}
|
||||
|
||||
void test_bzip()
|
||||
{
|
||||
test_compress("bzip", mz_stream_bzip_create);
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void test_zip_mem()
|
||||
{
|
||||
mz_zip_file file_info = { 0 };
|
||||
void *read_mem_stream = NULL;
|
||||
void *write_mem_stream = NULL;
|
||||
void *os_stream = NULL;
|
||||
void *zip_handle = NULL;
|
||||
int32_t written = 0;
|
||||
int32_t read = 0;
|
||||
int32_t text_size = 0;
|
||||
int32_t buffer_size = 0;
|
||||
int32_t err = MZ_OK;
|
||||
char *buffer_ptr = NULL;
|
||||
char *password = "1234";
|
||||
char *text_name = "test";
|
||||
char *text_ptr = "test string";
|
||||
char temp[120];
|
||||
|
||||
|
||||
text_size = (int32_t)strlen(text_ptr);
|
||||
|
||||
// Write zip to memory stream
|
||||
mz_stream_mem_create(&write_mem_stream);
|
||||
mz_stream_mem_set_grow_size(write_mem_stream, 128 * 1024);
|
||||
mz_stream_open(write_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
|
||||
|
||||
zip_handle = mz_zip_open(write_mem_stream, MZ_OPEN_MODE_READWRITE);
|
||||
|
||||
if (zip_handle != NULL)
|
||||
{
|
||||
file_info.version_madeby = MZ_VERSION_MADEBY;
|
||||
file_info.compression_method = MZ_COMPRESS_METHOD_DEFLATE;
|
||||
file_info.filename = text_name;
|
||||
file_info.uncompressed_size = text_size;
|
||||
file_info.aes_version = MZ_AES_VERSION;
|
||||
|
||||
err = mz_zip_entry_write_open(zip_handle, &file_info, MZ_COMPRESS_LEVEL_DEFAULT, password);
|
||||
if (err == MZ_OK)
|
||||
{
|
||||
written = mz_zip_entry_write(zip_handle, text_ptr, text_size);
|
||||
if (written < MZ_OK)
|
||||
err = written;
|
||||
mz_zip_entry_close(zip_handle);
|
||||
}
|
||||
|
||||
mz_zip_close(zip_handle);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = MZ_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
mz_stream_mem_get_buffer(write_mem_stream, (void **)&buffer_ptr);
|
||||
mz_stream_mem_seek(write_mem_stream, 0, MZ_SEEK_END);
|
||||
buffer_size = (int32_t)mz_stream_mem_tell(write_mem_stream);
|
||||
|
||||
if (err == MZ_OK)
|
||||
{
|
||||
// Create a zip file on disk for inspection
|
||||
mz_stream_os_create(&os_stream);
|
||||
mz_stream_os_open(os_stream, "mytest.zip", MZ_OPEN_MODE_WRITE | MZ_OPEN_MODE_CREATE);
|
||||
mz_stream_os_write(os_stream, buffer_ptr, buffer_size);
|
||||
mz_stream_os_close(os_stream);
|
||||
mz_stream_os_delete(&os_stream);
|
||||
}
|
||||
|
||||
if (err == MZ_OK)
|
||||
{
|
||||
// Read from a memory stream
|
||||
mz_stream_mem_create(&read_mem_stream);
|
||||
mz_stream_mem_set_buffer(read_mem_stream, buffer_ptr, buffer_size);
|
||||
mz_stream_open(read_mem_stream, NULL, MZ_OPEN_MODE_READ);
|
||||
|
||||
zip_handle = mz_zip_open(read_mem_stream, MZ_OPEN_MODE_READ);
|
||||
|
||||
if (zip_handle != NULL)
|
||||
{
|
||||
err = mz_zip_goto_first_entry(zip_handle);
|
||||
if (err == MZ_OK)
|
||||
err = mz_zip_entry_read_open(zip_handle, 0, password);
|
||||
if (err == MZ_OK)
|
||||
read = mz_zip_entry_read(zip_handle, temp, sizeof(temp));
|
||||
|
||||
mz_zip_entry_close(zip_handle);
|
||||
mz_zip_close(zip_handle);
|
||||
}
|
||||
|
||||
mz_stream_mem_close(&read_mem_stream);
|
||||
mz_stream_mem_delete(&read_mem_stream);
|
||||
read_mem_stream = NULL;
|
||||
}
|
||||
|
||||
mz_stream_mem_close(write_mem_stream);
|
||||
mz_stream_mem_delete(&write_mem_stream);
|
||||
write_mem_stream = NULL;
|
||||
}
|
||||
|
||||
|
||||
/***************************************************************************/
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
#ifndef _MZ_TEST_H
|
||||
#define _MZ_TEST_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void test_aes();
|
||||
void test_crypt();
|
||||
void test_inflate();
|
||||
void test_deflate();
|
||||
void test_bzip();
|
||||
void test_zip_mem();
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Executable
+202
@@ -0,0 +1,202 @@
|
||||
import os
|
||||
import sys
|
||||
import stat
|
||||
import argparse
|
||||
import glob
|
||||
import shutil
|
||||
import fnmatch
|
||||
import time
|
||||
import struct
|
||||
import numpy
|
||||
import random
|
||||
import pprint
|
||||
import hashlib
|
||||
from sys import platform
|
||||
|
||||
parser = argparse.ArgumentParser(description='Test script')
|
||||
parser.add_argument('--exe_dir', help='Built exes directory', default=None, action='store', required=False)
|
||||
args, unknown = parser.parse_known_args()
|
||||
|
||||
def hash_file_sha1(path):
|
||||
sha1 = hashlib.sha1()
|
||||
with open(path, 'rb') as f:
|
||||
while True:
|
||||
data = f.read(8192)
|
||||
if not data:
|
||||
break
|
||||
sha1.update(data)
|
||||
return sha1.hexdigest()
|
||||
|
||||
def find_files(directory, pattern, recursive = True):
|
||||
if directory == '':
|
||||
directory = os.getcwd()
|
||||
if recursive:
|
||||
items = os.walk(directory)
|
||||
else:
|
||||
items = [next(os.walk(directory))]
|
||||
for root, dirs, files in items:
|
||||
for basename in files:
|
||||
if fnmatch.fnmatch(basename, pattern):
|
||||
filename = os.path.join(root, basename)
|
||||
yield filename
|
||||
|
||||
def erase_file(path):
|
||||
#print('Deleting {0}'.format(path))
|
||||
os.chmod(path, stat.S_IWUSR)
|
||||
os.remove(path)
|
||||
|
||||
def erase_files(search_pattern):
|
||||
search_dir, match = os.path.split(search_pattern)
|
||||
for path in find_files(search_dir, match):
|
||||
erase_file(path)
|
||||
|
||||
def erase_dir(path):
|
||||
if not os.path.exists(path):
|
||||
return
|
||||
print('Erasing dir {0}'.format(path))
|
||||
# shutil.rmtree doesn't work well and sometimes can't delete
|
||||
for root, dirs, files in os.walk(path, topdown=False):
|
||||
for name in files:
|
||||
filename = os.path.join(root, name)
|
||||
success = False
|
||||
while not success:
|
||||
try:
|
||||
erase_file(filename)
|
||||
success = True
|
||||
except:
|
||||
time.sleep(4)
|
||||
pass
|
||||
for name in dirs:
|
||||
os.rmdir(os.path.join(root, name))
|
||||
os.rmdir(path)
|
||||
|
||||
def get_exec(name):
|
||||
global args
|
||||
if platform == 'win32':
|
||||
name += '.exe'
|
||||
if args.exe_dir is not None:
|
||||
return os.path.join(args.exe_dir, name)
|
||||
return name
|
||||
|
||||
def get_files_info(path):
|
||||
if os.path.isfile(path):
|
||||
return {
|
||||
path: {
|
||||
'hash': hash_file_sha1(path),
|
||||
'size': os.path.getsize(path)
|
||||
}
|
||||
}
|
||||
info = {}
|
||||
for root, dirs, files in os.walk(path):
|
||||
path = root.split(os.sep)
|
||||
for path in files:
|
||||
info.update(get_files_info(path))
|
||||
return info
|
||||
|
||||
def create_random_file(path, size):
|
||||
if os.path.exists(path):
|
||||
return
|
||||
with open(path, 'wb') as fout:
|
||||
i = 0
|
||||
while size > 0:
|
||||
if i == 0 or i % 400000 == 0:
|
||||
data = numpy.random.rand(100000)
|
||||
floatlist = numpy.squeeze(numpy.asarray(data))
|
||||
buf = struct.pack('%sf' % len(floatlist), *floatlist)
|
||||
fout.write(buf)
|
||||
size -= len(buf)
|
||||
i += 1
|
||||
|
||||
def zip(zip_file, zip_args, files):
|
||||
cmd = '{0} {1} {2} {3}'.format(get_exec('minizip'), zip_args, zip_file, ' '.join(files))
|
||||
print cmd
|
||||
err = os.system(cmd)
|
||||
if (err != 0):
|
||||
print('Zip returned error code {0}'.format(err))
|
||||
sys.exit(err)
|
||||
|
||||
def list(zip_file):
|
||||
cmd = '{0} -l {1}'.format(get_exec('minizip'), zip_file)
|
||||
print cmd
|
||||
err = os.system(cmd)
|
||||
if (err != 0):
|
||||
print('List returned error code {0}'.format(err))
|
||||
sys.exit(err)
|
||||
|
||||
def unzip(zip_file, dest_dir, unzip_args = ''):
|
||||
cmd = '{0} -x {1} -d {2} {3}'.format(get_exec('minizip'), unzip_args, dest_dir, zip_file)
|
||||
print cmd
|
||||
err = os.system(cmd)
|
||||
if (err != 0):
|
||||
print('Unzip returned error code {0}'.format(err))
|
||||
sys.exit(err)
|
||||
|
||||
def zip_list_unzip(zip_file, dest_dir, zip_args, unzip_args, files):
|
||||
# Single test run
|
||||
original_infos = {}
|
||||
for (i, path) in enumerate(files):
|
||||
original_infos.update(get_files_info(path))
|
||||
print original_infos
|
||||
|
||||
erase_files(zip_file[0:-2] + "*")
|
||||
erase_dir(dest_dir)
|
||||
|
||||
zip(zip_file, zip_args, files)
|
||||
list(zip_file)
|
||||
unzip(zip_file, dest_dir, unzip_args)
|
||||
|
||||
new_infos = {}
|
||||
for (i, path) in enumerate(files):
|
||||
new_infos.update(get_files_info(path))
|
||||
|
||||
if (' '.join(original_infos) != ' '.join(new_infos)):
|
||||
print('Infos do not match')
|
||||
print('Original: ')
|
||||
pprint(original_infos)
|
||||
print('New: ')
|
||||
print(new_infos)
|
||||
|
||||
def file_tests(method, zip_arg = '', unzip_arg = ''):
|
||||
print('Testing {0} on Single File'.format(method))
|
||||
zip_list_unzip('test.zip', 'out', zip_arg, unzip_arg, ['test.c'])
|
||||
print('Testing {0} on Two Files'.format(method))
|
||||
zip_list_unzip('test.zip', 'out', zip_arg, unzip_arg, ['test.c', 'test.h'])
|
||||
print('Testing {0} Directory'.format(method))
|
||||
zip_list_unzip('test.zip', 'out', zip_arg, unzip_arg, ['repo'])
|
||||
print('Testing {0} 1MB file'.format(method))
|
||||
create_random_file('1MB.dat', 1 * 1024 * 1024)
|
||||
zip_list_unzip('test.zip', 'out', zip_arg, unzip_arg, ['1MB.dat'])
|
||||
print('Testing {0} 50MB file'.format(method))
|
||||
create_random_file('50MB.dat', 50 * 1024 * 1024)
|
||||
zip_list_unzip('test.zip', 'out', zip_arg, unzip_arg, ['50MB.dat'])
|
||||
|
||||
def compression_method_tests(method = '', zip_arg = '', unzip_arg = ''):
|
||||
method = method + ' ' if method != '' else ''
|
||||
file_tests(method + 'Deflate', zip_arg, unzip_arg)
|
||||
file_tests(method + 'Raw', '-0 ' + zip_arg, unzip_arg)
|
||||
file_tests(method + 'BZIP2', '-b ' + zip_arg, unzip_arg)
|
||||
file_tests(method + 'LZMA', '-m ' + zip_arg, unzip_arg)
|
||||
|
||||
def encryption_tests():
|
||||
compression_method_tests('Crypt', '-p 1234567890', '-p 1234567890')
|
||||
compression_method_tests('AES', '-s -p 1234567890', '-p 1234567890')
|
||||
|
||||
def empty_zip_test():
|
||||
unzip('empty.zip', 'out')
|
||||
|
||||
def sfx_zip_test():
|
||||
org_exe = get_exec('minizip')
|
||||
sfx_exe = org_exe.replace('.exe', '') + '_sfx.exe'
|
||||
shutil.copyfile(org_exe, sfx_exe)
|
||||
zip_list_unzip(sfx_exe, 'out', '-a', '', [ 'test.c', 'test.h' ])
|
||||
|
||||
if not os.path.exists('repo'):
|
||||
os.system('git clone https://github.com/nmoinvaz/minizip repo')
|
||||
|
||||
# Run tests
|
||||
empty_zip_test()
|
||||
sfx_zip_test()
|
||||
compression_method_tests()
|
||||
encryption_tests()
|
||||
compression_method_tests('Disk Span', '-k 1024', '')
|
||||
compression_method_tests('Buffered', '-u', '-u')
|
||||
Reference in New Issue
Block a user