Skip to content

Commit

Permalink
clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Aug 10, 2019
1 parent ae0be99 commit a83fc5e
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 107 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BasedOnStyle: LLVM
PointerAlignment: Left
98 changes: 53 additions & 45 deletions api.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <windows.h>
#include "hook.h"
#include <stdio.h>
#include <string.h>
#include "hook.h"
#include <windows.h>

#ifdef DEBUG
# define dbg_print(...) printf("[aoc-language-ini] " __VA_ARGS__)
#define dbg_print(...) printf("[aoc-language-ini] " __VA_ARGS__)
#else
# define dbg_print(...)
#define dbg_print(...)
#endif

typedef struct string_entry {
Expand All @@ -29,7 +29,8 @@ static size_t string_table_id;

static char* read_file(char* filename) {
FILE* file = fopen(filename, "rb");
if (file == NULL) return NULL;
if (file == NULL)
return NULL;
fseek(file, 0, SEEK_END);
int size = ftell(file);
fseek(file, 0, SEEK_SET);
Expand All @@ -47,30 +48,30 @@ static char* read_file(char* filename) {
return content;
}

static void expand_string_table (string_table_t* string_table) {
static void expand_string_table(string_table_t* string_table) {
string_table->capacity += 8192;
string_table->entries = realloc(string_table->entries,
string_table->capacity * sizeof(string_entry_t));
string_table->entries = realloc(
string_table->entries, string_table->capacity * sizeof(string_entry_t));
}

static char* unescape (char* input) {
static char* unescape(char* input) {
int l = strlen(input);
int j = 0;
char* buf = calloc(1, l + 1);
for (int i = 0; i < l; i++) {
if (input[i] == '\\' && i + 1 < l) {
switch (input[i + 1]) {
case 'n':
i++;
buf[j++] = '\n';
break;
case 'r':
i++;
buf[j++] = '\r';
break;
default:
buf[j++] = input[i];
break;
case 'n':
i++;
buf[j++] = '\n';
break;
case 'r':
i++;
buf[j++] = '\r';
break;
default:
buf[j++] = input[i];
break;
}
} else {
buf[j++] = input[i];
Expand All @@ -80,23 +81,22 @@ static char* unescape (char* input) {
return buf;
}

HANDLE aoc_ini_load_strings (char* filename) {
HANDLE aoc_ini_load_strings(char* filename) {
dbg_print("load strings: %s\n", filename);

string_table_t string_table = {
.id = string_table_id,
.filename = strdup(filename),
.size = 0,
.capacity = 8192
};
string_table_t string_table = {.id = string_table_id,
.filename = strdup(filename),
.size = 0,
.capacity = 8192};

string_table_id++;

int id;
char cur_string[4096];

char* content = read_file(filename);
if (!content) return INVALID_HANDLE_VALUE;
if (!content)
return INVALID_HANDLE_VALUE;

string_table.entries = calloc(string_table.capacity, sizeof(string_entry_t));

Expand All @@ -122,23 +122,26 @@ HANDLE aoc_ini_load_strings (char* filename) {
if (string_table.size < string_table.capacity) {
// just to be safe, zero out the rest
memset(&string_table.entries[string_table.size], 0,
(string_table.capacity - string_table.size) * sizeof(string_entry_t));
(string_table.capacity - string_table.size) *
sizeof(string_entry_t));
}

if (string_tables == NULL) {
string_tables = calloc(1, sizeof(string_table_t));
num_string_tables = 1;
} else {
num_string_tables += 1;
string_tables = realloc(string_tables, num_string_tables * sizeof(string_table_t));
string_tables =
realloc(string_tables, num_string_tables * sizeof(string_table_t));
}

memcpy(&string_tables[num_string_tables - 1], &string_table, sizeof(string_table));
memcpy(&string_tables[num_string_tables - 1], &string_table,
sizeof(string_table));

return (HANDLE) string_table.id;
return (HANDLE)string_table.id;
}

static void free_string_table_entries (string_table_t* table) {
static void free_string_table_entries(string_table_t* table) {
dbg_print("free entries: %s\n", table->filename);
for (int i = 0; i < table->size; i++) {
free(table->entries[i].value);
Expand All @@ -149,7 +152,7 @@ static void free_string_table_entries (string_table_t* table) {
table->capacity = 0;
}

static void aoc_ini_free_all () {
static void aoc_ini_free_all() {
for (int i = 0; i < num_string_tables; i++) {
free_string_table_entries(&string_tables[i]);
free(string_tables[i].filename);
Expand All @@ -159,21 +162,24 @@ static void aoc_ini_free_all () {
return;
}

void aoc_ini_free_strings (HANDLE table_handle) {
int table_id = (size_t) table_handle;
void aoc_ini_free_strings(HANDLE table_handle) {
int table_id = (size_t)table_handle;
for (int i = 0; i < num_string_tables; i++) {
if (string_tables[i].id == table_id) {
free_string_table_entries(&string_tables[i]);

size_t num_remaining = num_string_tables - (i + 1);
if (num_remaining == 0) {
num_string_tables -= 1;
string_tables = realloc(string_tables, num_string_tables * sizeof(string_table_t));
string_tables =
realloc(string_tables, num_string_tables * sizeof(string_table_t));
} else {
string_table_t* rest = calloc(num_remaining, sizeof(string_table_t));
memcpy(rest, &string_tables[i + 1], num_remaining * sizeof(string_table_t));
memcpy(rest, &string_tables[i + 1],
num_remaining * sizeof(string_table_t));
num_string_tables -= 1;
string_tables = realloc(string_tables, num_string_tables * sizeof(string_table_t));
string_tables =
realloc(string_tables, num_string_tables * sizeof(string_table_t));
memcpy(&string_tables[i], rest, num_remaining * sizeof(string_table_t));
free(rest);
}
Expand All @@ -183,7 +189,7 @@ void aoc_ini_free_strings (HANDLE table_handle) {
}
}

static string_entry_t* find_string_in_table (string_table_t* table, int id) {
static string_entry_t* find_string_in_table(string_table_t* table, int id) {
for (int i = 0; i < table->size; i++) {
if (table->entries[i].id == id) {
return &table->entries[i];
Expand All @@ -192,18 +198,20 @@ static string_entry_t* find_string_in_table (string_table_t* table, int id) {
return NULL;
}

static string_entry_t* find_string (int id) {
static string_entry_t* find_string(int id) {
string_entry_t* entry = NULL;
for (int i = num_string_tables - 1; i >= 0; i--) {
entry = find_string_in_table(&string_tables[i], id);
if (entry) return entry;
if (entry)
return entry;
}
return NULL;
}

typedef void* __stdcall (*fn_load_string)(HINSTANCE, unsigned int, char*, int);
static const fn_load_string aoc_load_string = (fn_load_string) 0x58E820;
static char* __stdcall load_string_hook (HINSTANCE dll, unsigned int string_id, char* buf_out, int buf_size) {
static const fn_load_string aoc_load_string = (fn_load_string)0x58E820;
static char* __stdcall load_string_hook(HINSTANCE dll, unsigned int string_id,
char* buf_out, int buf_size) {
string_entry_t* entry = find_string(string_id);
if (entry != NULL) {
strncpy(buf_out, entry->value, buf_size);
Expand All @@ -218,7 +226,7 @@ void aoc_ini_init() {
dbg_print("init()\n");

int i = 0;
hooks[i++] = install_callhook((void*) 0x43CF2B, load_string_hook);
hooks[i++] = install_callhook((void*)0x43CF2B, load_string_hook);
hooks[i] = NULL;
}

Expand Down
8 changes: 4 additions & 4 deletions api.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once
#include <windows.h>

HANDLE aoc_ini_load_strings (char* filename);
void aoc_ini_free_strings (HANDLE table_handle);
HANDLE aoc_ini_load_strings(char* filename);
void aoc_ini_free_strings(HANDLE table_handle);

void aoc_ini_init ();
void aoc_ini_deinit ();
void aoc_ini_init();
void aoc_ini_deinit();
39 changes: 20 additions & 19 deletions getstrings.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#include <stdio.h>
#include <windows.h>

/**
* getstrings: tool to dump AoC language strings from dll files
Expand All @@ -11,7 +11,7 @@
* getstrings -rc /path/to/language.dll
*/

char* escape (char* str, char quoted) {
char* escape(char* str, char quoted) {
int i = 0;
int j = 0;
char escaped[8195];
Expand All @@ -20,24 +20,24 @@ char* escape (char* str, char quoted) {
}
while (str[i] != '\0' && j < 8193) {
switch (str[i]) {
case '\r':
escaped[j++] = '\\';
escaped[j++] = 'r';
break;
case '\n':
case '\r':
escaped[j++] = '\\';
escaped[j++] = 'r';
break;
case '\n':
escaped[j++] = '\\';
escaped[j++] = 'n';
break;
case '"':
if (quoted) {
escaped[j++] = '\\';
escaped[j++] = 'n';
break;
case '"':
if (quoted) {
escaped[j++] = '\\';
escaped[j++] = str[i];
break;
}
// fall through
default:
escaped[j++] = str[i];
break;
}
// fall through
default:
escaped[j++] = str[i];
break;
}
i++;
}
Expand All @@ -48,14 +48,15 @@ char* escape (char* str, char quoted) {
return strdup(escaped);
}

int main (int argc, char** argv) {
int main(int argc, char** argv) {
if (argc < 2) {
fprintf(stderr, "usage: getstrings ~/path/to/language.dll\n");
return 1;
}

char* lib_name = argv[1];
char as_string_table = argc > 2 && (strcmp(argv[2], "--rc") == 0 || strcmp(argv[2], "-rc") == 0);
char as_string_table =
argc > 2 && (strcmp(argv[2], "--rc") == 0 || strcmp(argv[2], "-rc") == 0);
if (argc > 2 && !as_string_table) {
if (strcmp(argv[1], "--rc") == 0 || strcmp(argv[1], "-rc") == 0) {
as_string_table = 1;
Expand Down
Loading

0 comments on commit a83fc5e

Please sign in to comment.