Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
cmake_minimum_required(VERSION 3.14)
project(cb2util C)

set(CMAKE_C_STANDARD 99)

# Include directories
include_directories(
.
include
libcheats/include
libbig_int/include
)

# Source files
set(SOURCES
arcfour.c
cb2_crypto.c
cb2util.c
cbc.c
cheats.c
cheats_common.c
compress.c
fileio.c
getopt.c
pcb.c
shs.c
libcheats/src/cheatlist.c
libcheats/src/libcheats.c
libcheats/src/mystring.c
libcheats/src/parser.c
libbig_int/src/basic_funcs.c
libbig_int/src/bitset_funcs.c
libbig_int/src/low_level_funcs/add.c
libbig_int/src/low_level_funcs/and.c
libbig_int/src/low_level_funcs/andnot.c
libbig_int/src/low_level_funcs/cmp.c
libbig_int/src/low_level_funcs/div.c
libbig_int/src/low_level_funcs/mul.c
libbig_int/src/low_level_funcs/or.c
libbig_int/src/low_level_funcs/sqr.c
libbig_int/src/low_level_funcs/sub.c
libbig_int/src/low_level_funcs/xor.c
libbig_int/src/memory_manager.c
libbig_int/src/modular_arithmetic.c
libbig_int/src/number_theory.c
libbig_int/src/service_funcs.c
libbig_int/src/str_funcs.c
)

add_definitions(-DCB2UTIL_VERSION="1.9" -DHAVE_STDINT_H)

find_package(ZLIB QUIET)

if(NOT ZLIB_FOUND)
include(FetchContent)
FetchContent_Declare(
zlib
URL https://github.com/madler/zlib/archive/refs/tags/v1.3.1.tar.gz
)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(ZLIB_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(zlib)
set(ZLIB_INCLUDE_DIRS ${zlib_SOURCE_DIR} ${zlib_BINARY_DIR})
include_directories(${ZLIB_INCLUDE_DIRS})
endif()

add_executable(cb2util ${SOURCES})

if(TARGET zlibstatic)
target_link_libraries(cb2util PRIVATE zlibstatic)
elseif(TARGET zlib)
target_link_libraries(cb2util PRIVATE zlib)
else()
target_link_libraries(cb2util PRIVATE ZLIB::ZLIB)
endif()
45 changes: 20 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
all:

# Define V=1 to have a more verbose compile.
#
# Define BUILD_MINGW=1 to cross-compile cb2util for Win32 using MinGW.

# Generate version info
CB2UTIL-VERSION-FILE: FORCE
@./CB2UTIL-VERSION-GEN
-include CB2UTIL-VERSION-FILE
export CB2UTIL_VERSION
# Detect OS
ifeq ($(OS),Windows_NT)
IS_WIN = 1
endif

prefix = $(HOME)
BIGINT = libbig_int
LIBCHEATS = libcheats

CC = gcc
CURL = curl
INSTALL = install
RM = rm -f
SHA1SUM = sha1sum
STRIP = strip
TAR = tar
ZIP = zip
CC ?= gcc
CURL ?= curl
INSTALL ?= install
RM ?= rm -f
SHA1SUM ?= sha1sum
STRIP ?= strip
TAR ?= tar
ZIP ?= zip

CFLAGS = -Wall -Werror -Wno-date-time -O2
CFLAGS += -I$(BIGINT)/include -I$(LIBCHEATS)/include -Iinclude
CFLAGS += -DHAVE_STDINT_H
LDFLAGS =
LIBS =

ifeq ($(BUILD_MINGW),1)
ifeq ($(IS_WIN),1)
PROG = cb2util.exe
LIBS += -lz
else ifeq ($(BUILD_MINGW),1)
CC = i586-mingw32msvc-gcc
STRIP = i586-mingw32msvc-strip
CFLAGS += -I$(ZLIB_PATH)/include -L$(ZLIB_PATH)/lib
Expand Down Expand Up @@ -72,6 +72,7 @@ OBJS += cheats.o
OBJS += cheats_common.o
OBJS += compress.o
OBJS += fileio.o
OBJS += getopt.o
OBJS += pcb.o
OBJS += shs.o

Expand Down Expand Up @@ -119,26 +120,20 @@ $(PROG): $(OBJS)
$(QUIET_LINK)$(CC) $(CFLAGS) -o $@ $(LDFLAGS) $(filter %.o,$^) $(LIBS)
$(QUIET_STRIP)$(STRIP) $@

cb2util.o: CB2UTIL-VERSION-FILE include/elf.h
cb2util.o: CFLAGS += -DCB2UTIL_VERSION='"$(CB2UTIL_VERSION)"'

include/elf.h:
mkdir -p include
$(CURL) -fSs -o $@ https://gist.githubusercontent.com/mlafeldt/3885346/raw/elf.h
cb2util.o: CFLAGS += -DCB2UTIL_VERSION='"1.9"'

clean:
$(RM) $(PROG) $(OBJS)
$(RM) CB2UTIL-VERSION-FILE
$(RM) -r release include
$(QUIET_SUBDIR0)test $(QUIET_SUBDIR1) clean
$(RM) -r release

test: all
$(QUIET_SUBDIR0)test $(QUIET_SUBDIR1) all

prove: all
$(QUIET_SUBDIR0)test $(QUIET_SUBDIR1) prove

PACKAGE = cb2util-$(CB2UTIL_VERSION)
PACKAGE = cb2util-1.9
release: clean test
$(RM) -r release/
$(INSTALL) -d -m 755 release/$(PACKAGE)
Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,32 @@ The features are:

## Installation

### Linux & macOS (GCC/Clang)

To build cb2util from source simply run:

$ git clone --recursive https://github.com/mlafeldt/cb2util
$ cd cb2util/
$ make
$ make install

Zig is also supported, which is particularly useful for cross-compiling:
### Windows (CMake / MinGW / MSVC)

Using CMake (recommended on Windows):

> cmake -B build
> cmake --build build

The compiled `cb2util.exe` will be located in the `build/` directory (or `build/Debug/` / `build/Release/`).

Using MinGW / GCC on Windows (PowerShell, Command Prompt, or MSYS2):

> make

### Cross-compiling / Zig

Zig is also supported for native builds and cross-compiling without external dependencies:

$ make include/elf.h
$ zig build
$ zig build -Dtarget=aarch64-macos
$ zig build -Dtarget=x86_64-linux-gnu
Expand Down
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub fn build(b: *std.Build) void {
"cheats_common.c",
"compress.c",
"fileio.c",
"getopt.c",
"pcb.c",
"shs.c",
}, .flags = &cflags });
Expand Down
4 changes: 4 additions & 0 deletions cb2util.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include <string.h>
#include <getopt.h>

#ifndef CB2UTIL_VERSION
#define CB2UTIL_VERSION "1.9"
#endif

extern int cmd_cbc(int argc, char **argv);
extern int cmd_cheats(int argc, char **argv);
extern int cmd_pcb(int argc, char **argv);
Expand Down
18 changes: 13 additions & 5 deletions cbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ typedef struct {
char gametitle[72];
uint32_t dataoff; /* file offset of data section */
uint32_t zero; /* always 0 */
uint8_t data[0];
uint8_t data[];
} cbc_hdr_t;

#define CBC_FILE_ID 0x01554643 /* "CFU", 1 */
Expand All @@ -39,7 +39,7 @@ typedef struct {
/* CBC v7 file header */
typedef struct {
char gametitle[64];
uint8_t data[0];
uint8_t data[];
} cbc7_hdr_t;

static const char *cbc_usage =
Expand Down Expand Up @@ -168,21 +168,29 @@ int cmd_cbc(int argc, char **argv)
goto compile_end;
}

#if defined(__GNUC__) && __GNUC__ >= 7
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-truncation"
#pragma GCC diagnostic ignored "-Wstringop-truncation"
#endif
if (v7) {
cbc7_hdr_t hdr;
memset(&hdr, 0, sizeof(hdr));
strncpy(hdr.gametitle, GAMES_FIRST(&cheats.games)->title, 64);
snprintf(hdr.gametitle, sizeof(hdr.gametitle), "%s", GAMES_FIRST(&cheats.games)->title);
fwrite(&hdr, sizeof(hdr), 1, fp);
} else {
cbc_hdr_t hdr;
memset(&hdr, 0, sizeof(hdr));
hdr.fileid = CBC_FILE_ID;
strncpy((char*)hdr.rsasig, banner != NULL ? banner : CBC_BANNER, 256);
snprintf((char*)hdr.rsasig, sizeof(hdr.rsasig), "%s", banner != NULL ? banner : CBC_BANNER);
hdr.cbvers = 0x0800;
strncpy(hdr.gametitle, GAMES_FIRST(&cheats.games)->title, 72);
snprintf(hdr.gametitle, sizeof(hdr.gametitle), "%s", GAMES_FIRST(&cheats.games)->title);
hdr.dataoff = sizeof(hdr);
fwrite(&hdr, sizeof(hdr), 1, fp);
}
#if defined(__GNUC__) && __GNUC__ >= 7
#pragma GCC diagnostic pop
#endif

cb_crypt_data(buf, buflen);
fwrite(buf, buflen, 1, fp);
Expand Down
2 changes: 1 addition & 1 deletion cheats.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
typedef struct {
uint32_t fileid; /* must be CHEATS_FILE_ID */
uint32_t unknown; /* always 0x00010000 */
uint8_t data[0];
uint8_t data[];
} cheats_hdr_t;

#define CHEATS_FILE_ID 0x00554643 /* "CFU\0" */
Expand Down
Loading