Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
suwacrab committed Oct 18, 2024
0 parents commit 1381208
Show file tree
Hide file tree
Showing 19 changed files with 681 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build/*.o
build/*.d
bin/*.exe

45 changes: 45 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# compiler
CXX := clang++
CC := clang

CBASEFLAGS := -MD -O2
CBASEFLAGS += -Wall -Wshadow -Werror
CBASEFLAGS += -Iinclude

CFLAGS := $(CBASEFLAGS)

CXXFLAGS := -std=c++17
CXXFLAGS += $(CBASEFLAGS)
CXXFLAGS += -Wnull-dereference

LDFLAGS := -static

# output
OBJ_DIR := build
SRC_DIR := source
OUTPUT := bin/snmy.exe

SRCS_CPP := $(shell find $(SRC_DIR) -name *.cpp)
SRCS_C := $(shell find $(SRC_DIR) -name *.c)

OBJS := $(subst $(SRC_DIR),$(OBJ_DIR),$(SRCS_CPP:.cpp=.o))
OBJS += $(subst $(SRC_DIR),$(OBJ_DIR),$(SRCS_C:.c=.o))
DEPS := $(OBJS:.o=.d)

-include $(DEPS)

all: $(OUTPUT)

# building
$(OUTPUT): $(OBJS)
$(CXX) $^ -o $@ $(LDFLAGS)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
$(C) $(CFLAGS) -c $< -o $@

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@

clean:
rm -rf build/*.o build/*.d $(OUTPUT)

Empty file added bin/.gitkeep
Empty file.
47 changes: 47 additions & 0 deletions build.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local printf = function(str,...) print(str:format(...)) end

local execa = function(...)
local cmdstr = table.concat({...}," ")
printf("\t> %s",cmdstr)
local stat = os.execute(cmdstr)
if not stat then
error("command error...")
end
end

local function argsearch(name)
for i,v in next,arg do
if v == name then
return i
end
end
return nil
end

local function build()
if argsearch("clean") then
execa("make clean")
end
if argsearch("rebuild") then
execa("rm -f bin/aya.exe")
end
if argsearch("build") then
execa("make all -j4")
end
if argsearch("test") then

local f_src = "dejiko.bmp"
local f_cmp = "dejiko.bin"
local f_tst = "dejiko2.bmp"
--[[
local f_src = "words.txt"
local f_cmp = "words.bin"
local f_tst = "words2.txt"
]]
execa("bin\\snmy -a compress -v -i",f_src,"-o",f_cmp)
execa("bin\\snmy -a decompress -i",f_cmp,"-o",f_tst)
end
end

build()

Empty file added build/.gitkeep
Empty file.
24 changes: 24 additions & 0 deletions include/argparse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef ARGPARSE_H
#define ARGPARSE_H

#include <vector>
#include <string>

class CArgParser {
public:
static const int ARG_INVALID = -1;
private:
std::vector<std::string> m_arglist;
public:
CArgParser();
CArgParser(int argc, const char* argv[]);

auto arg_find(std::string name,int len = 0) -> int;
auto arg_isValid(std::string name, int len = 0) -> bool;
auto arg_get(std::string name, int len = 0) -> std::vector<std::string>;
auto size() const -> int { return m_arglist.size(); }
auto has_arguments() const -> bool { return size() > 0; }
};

#endif

34 changes: 34 additions & 0 deletions include/blob.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef BLOB_H
#define BLOB_H

#include <vector>
#include <string>
#include <cstdint>

class Blob {
private:
std::vector<uint8_t> m_data;
public:
void write_blob(Blob& orig);
void write_raw(const void* source, size_t len);
void write_u8(uint32_t n);
void write_u16(uint32_t n);
void write_u32(uint32_t n);

void write_str(const std::string& str, bool no_terminator=false);
auto hash() const -> uint32_t;

void reset();
bool send_file(std::string filename, bool strict=true);

template<typename T=void> auto data() -> T* {
return reinterpret_cast<T*>(m_data.data());
}
constexpr auto size() const -> size_t { return m_data.size(); }

Blob();
Blob(Blob& orig);
};

#endif

32 changes: 32 additions & 0 deletions include/snmy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef SNMY_H
#define SNMY_H

#include <cstdint>
#include <string>
#include <vector>
#include <blob.h>

namespace snmy {
constexpr size_t DATALINE_MAX = 16384;

class CCompressInfo;

auto compress_file(const std::string& filename, const snmy::CCompressInfo* info = NULL) -> Blob;
auto compress(const std::vector<uint8_t>& src_buffer, const snmy::CCompressInfo* info) -> Blob;

auto decompress_file(const std::string& filename) -> Blob;
auto decompress(const std::vector<uint8_t>& src_buffer) -> Blob;
};

class snmy::CCompressInfo {
public:
CCompressInfo()
: verbose(false)
{}

void verbose_set(bool flag) { verbose = flag; }
int verbose;
};

#endif

14 changes: 14 additions & 0 deletions note/doc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-----------------------------------------------------------------------------@/
* each line begins with a 16-bit header.
- high 2 bits: the compression mode
- low 14 bits: length of whatever data the line'll use,, minus 1.
* after the header, the data varies depending on the mode.
* 3 compression modes in total
- 0: no compression
data is simply raw bytes immediately after the header
- 1: compressed (RLE)
1 byte for the run.
- 2: compressed (LZ)
2 bytes for the offset to go backwards.(minus one)
- 3: end code
compression should end here.
43 changes: 43 additions & 0 deletions source/argparse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <argparse.h>

CArgParser::CArgParser() {
m_arglist.clear();
}
CArgParser::CArgParser(int argc, const char* argv[]) {
// the arguments to parse actually start at 1
// argv[0] is the program's path
m_arglist.clear();

if(argc == 1) return;
for(int i=1; i<argc; i++) {
auto str = std::string(argv[i]);
m_arglist.push_back(str);
}
}

auto CArgParser::arg_find(std::string name, int len) -> int {
for(int i=0; i<size(); i++) {
if(m_arglist[i] == name) {
if(i+len >= size()) return CArgParser::ARG_INVALID;
return i;
}
}
return CArgParser::ARG_INVALID;
}
auto CArgParser::arg_isValid(std::string name, int len) -> bool {
return arg_find(name,len) != CArgParser::ARG_INVALID;
}
auto CArgParser::arg_get(std::string name, int len) -> std::vector<std::string> {
auto idx = arg_find(name,len);
if(idx == CArgParser::ARG_INVALID) {
return {};
}

std::vector<std::string> data;
int num_args = 1 + len;
for(int i=0; i<num_args; i++) {
data.push_back(m_arglist[idx+i]);
}
return data;
}

78 changes: 78 additions & 0 deletions source/blob.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <blob.h>
#include <cstdio>

Blob::Blob() {
reset();
}
Blob::Blob(Blob& orig) {
reset();
// write from orig to self
write_blob(orig);
}

void Blob::reset() {
m_data.clear();
}

void Blob::write_blob(Blob& source) {
write_raw(source.data(),source.size());
}
void Blob::write_raw(const void* source, size_t len) {
if(len == 0) return;
if(source == nullptr) {
std::printf("Blob::write_raw(): error: attempt to write to %p from null source",
source
);
std::exit(-1);
}

auto src_bytes = static_cast<const uint8_t*>(source);
for(size_t i=0; i<len; i++) {
m_data.push_back(*src_bytes++);
}
}
void Blob::write_u8(uint32_t n) {
m_data.push_back(n & 0xFF);
}
void Blob::write_u16(uint32_t n) {
write_u8(n);
write_u8(n>>8);
}
void Blob::write_u32(uint32_t n) {
write_u16(n);
write_u16(n>>16);
}

void Blob::write_str(const std::string& str, bool no_terminator) {
for(int i=0; i<str.size(); i++) {
char chr = str.at(i);
write_u8(chr);
}
if(!no_terminator) {
write_u8(0);
}
}
auto Blob::hash() const -> uint32_t {
uint64_t fullhash = 0x811C9DC4;
for(int i=0; i<size(); i++) {
fullhash = ((fullhash ^ m_data[i]) * 0x1000193) & 0xFFFFFFFF;
}
return static_cast<uint32_t>(fullhash);
}

bool Blob::send_file(std::string filename, bool strict) {
auto file = std::fopen(filename.c_str(),"wb");
if(!file) {
if(strict) {
std::printf("Blob::send_file(): error: unable to send to file %s\n",
filename.c_str()
);
std::exit(-1);
}
return false;
}
std::fwrite(data(),sizeof(char),size(),file);
std::fclose(file);
return true;
}

Loading

0 comments on commit 1381208

Please sign in to comment.