From b509731104abc357bec9b79127050f50599ed111 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Fri, 27 May 2016 10:51:12 -0400 Subject: [PATCH] removed SET_CURSOR message --- 4coder_config.h | 10 +- 4coder_default_include.cpp | 2 + 4coder_string.h | 367 +++++++++--------- 4ed_dll_reader.cpp | 756 ++++++++++++++++++------------------- win32_4ed.cpp | 85 ++--- 5 files changed, 600 insertions(+), 620 deletions(-) diff --git a/4coder_config.h b/4coder_config.h index 2852eb12..81df0bc0 100644 --- a/4coder_config.h +++ b/4coder_config.h @@ -8,13 +8,13 @@ NOTES ON USE: #ifdef FCPP_NO_CRT # ifndef FCPP_NO_MALLOC -# define FCPP_NO_MALLOC +# define FCPP_NO_MALLOC # endif # ifndef FCPP_NO_ASSERT -# define FCPP_NO_ASSERT +# define FCPP_NO_ASSERT # endif # ifndef FCPP_NO_STRING -# define FCPP_NO_STRING +# define FCPP_NO_STRING # endif #endif @@ -36,10 +36,10 @@ NOTES ON USE: #ifndef FCPP_NO_MALLOC # ifndef FCPP_GET_MEMORY -# define FCPP_GET_MEMORY malloc +# define FCPP_GET_MEMORY malloc # endif # ifndef FCPP_FREE_MEMORY -# define FCPP_FREE_MEMORY free +# define FCPP_FREE_MEMORY free # endif #else # ifndef FCPP_FORBID_MALLOC diff --git a/4coder_default_include.cpp b/4coder_default_include.cpp index 8b2a8160..72efbe6c 100644 --- a/4coder_default_include.cpp +++ b/4coder_default_include.cpp @@ -7,6 +7,8 @@ #define UseInterfacesThatArePhasingOut 0 #include "4coder_helper.h" +#include + static void write_string(Application_Links *app, String string){ Buffer_Summary buffer = app->get_active_buffer(app); diff --git a/4coder_string.h b/4coder_string.h index 81346ca5..c1958cc0 100644 --- a/4coder_string.h +++ b/4coder_string.h @@ -9,7 +9,7 @@ NOTES ON USE: - this option is unset after use so that future includes of this file in the same unit do not continue to output implementations - FCPP_LINK - defines linkage of non-inline functions, defaults to static + FSTRING_LINK - defines linkage of non-inline functions, defaults to static FCPP_EXTERN changes FCPP_LINK default to extern, this option is ignored if FCPP_LINK is defined include the file "4cpp_clear_config.h" if yo want to undefine all options for some reason @@ -19,17 +19,18 @@ NOTES ON USE: */ // TOP -// TODO(allen): -// - comments -// - memcpy / memmove replacements (different file for optimization options?) -// - examples and docs -// - -#include "4coder_config.h" #ifndef FCPP_STRING_INC #define FCPP_STRING_INC +#ifndef FSTRING_LINK +# define FSTRING_LINK static +#endif + +#ifndef FSTRING_INLINE +# define FSTRING_INLINE inline +#endif + #ifndef FRED_STRING_STRUCT #define FRED_STRING_STRUCT struct String{ @@ -44,29 +45,29 @@ struct Offset_String{ }; #endif -inline bool char_not_slash(char c) { return (c != '\\' && c != '/'); } -inline bool char_is_slash(char c) { return (c == '\\' || c == '/'); } +FSTRING_INLINE bool char_not_slash(char c) { return (c != '\\' && c != '/'); } +FSTRING_INLINE bool char_is_slash(char c) { return (c == '\\' || c == '/'); } -inline char char_to_upper(char c) { return (c >= 'a' && c <= 'z') ? c + (char)('A' - 'a') : c; } -inline char char_to_lower(char c) { return (c >= 'A' && c <= 'Z') ? c - (char)('A' - 'a') : c; } -inline int char_to_int(char c) { return (c - '0'); } -inline char int_to_char(int x) { return (char)(x + '0'); } +FSTRING_INLINE char char_to_upper(char c) { return (c >= 'a' && c <= 'z') ? c + (char)('A' - 'a') : c; } +FSTRING_INLINE char char_to_lower(char c) { return (c >= 'A' && c <= 'Z') ? c - (char)('A' - 'a') : c; } +FSTRING_INLINE int char_to_int(char c) { return (c - '0'); } +FSTRING_INLINE char int_to_char(int x) { return (char)(x + '0'); } -inline bool char_is_whitespace(char c) { return (c == ' ' || c == '\n' || c == '\r' || c == '\t'); } -inline bool char_is_white_not_r(char c) { return (c == ' ' || c == '\n' || c == '\t'); } -inline bool char_is_lower(char c) { return (c >= 'a' && c <= 'z'); } -inline bool char_is_upper(char c) { return (c >= 'A' && c <= 'Z'); } -inline bool char_is_alpha(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_'); } -inline bool char_is_alpha_true(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'); } -inline bool char_is_numeric(char c) { return (c >= '0' && c <= '9'); } -inline bool char_is_alpha_numeric_true(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'); } -inline bool char_is_alpha_numeric(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_'); } -inline bool char_is_hex(char c) { return c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f'; } -inline bool char_is_basic(char c) { return c >= ' ' && c <= '~'; } +FSTRING_INLINE bool char_is_whitespace(char c) { return (c == ' ' || c == '\n' || c == '\r' || c == '\t'); } +FSTRING_INLINE bool char_is_white_not_r(char c) { return (c == ' ' || c == '\n' || c == '\t'); } +FSTRING_INLINE bool char_is_lower(char c) { return (c >= 'a' && c <= 'z'); } +FSTRING_INLINE bool char_is_upper(char c) { return (c >= 'A' && c <= 'Z'); } +FSTRING_INLINE bool char_is_alpha(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_'); } +FSTRING_INLINE bool char_is_alpha_true(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'); } +FSTRING_INLINE bool char_is_numeric(char c) { return (c >= '0' && c <= '9'); } +FSTRING_INLINE bool char_is_alpha_numeric_true(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'); } +FSTRING_INLINE bool char_is_alpha_numeric(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_'); } +FSTRING_INLINE bool char_is_hex(char c) { return c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f'; } +FSTRING_INLINE bool char_is_basic(char c) { return c >= ' ' && c <= '~'; } -inline String string_zero(); -inline String make_string(void *s, int size, int mem_size); -inline String make_string(void *s, int size); +FSTRING_INLINE String string_zero(); +FSTRING_INLINE String make_string(void *s, int size, int mem_size); +FSTRING_INLINE String make_string(void *s, int size); #define make_lit_string(str) (make_string((char*)(str), sizeof(str)-1, sizeof(str))) #define make_fixed_width_string(str) (make_string((char*)(str), 0, sizeof(str))) @@ -77,92 +78,92 @@ inline String make_string(void *s, int size); #define expand_str(s) ((s).str), ((s).size) -inline String make_string_slowly(void *s); -inline char* make_c_str(String s); +FSTRING_INLINE String make_string_slowly(void *s); +FSTRING_INLINE char* make_c_str(String s); -inline String substr(String str, int start); -inline String substr(String str, int start, int size); -inline String substr_slowly(char *s, int start); -inline String substr(char *s, int start, int size); -inline String tailstr(String s); +FSTRING_INLINE String substr(String str, int start); +FSTRING_INLINE String substr(String str, int start, int size); +FSTRING_INLINE String substr_slowly(char *s, int start); +FSTRING_INLINE String substr(char *s, int start, int size); +FSTRING_INLINE String tailstr(String s); -FCPP_LINK int str_size(char *s); +FSTRING_LINK int str_size(char *s); -FCPP_LINK bool match(char *a, char *b); -FCPP_LINK bool match(String a, char *b); -inline bool match(char *a, String b) { return match(b,a); } -FCPP_LINK bool match(String a, String b); +FSTRING_LINK bool match(char *a, char *b); +FSTRING_LINK bool match(String a, char *b); +FSTRING_INLINE bool match(char *a, String b) { return match(b,a); } +FSTRING_LINK bool match(String a, String b); -FCPP_LINK bool match_part(char *a, char *b, int *len); -FCPP_LINK bool match_part(String a, char *b, int *len); -inline bool match_part(char *a, char *b) { int x; return match_part(a,b,&x); } -inline bool match_part(String a, char *b) { int x; return match_part(a,b,&x); } -FCPP_LINK bool match_part(char *a, String b); -FCPP_LINK bool match_part(String a, String b); +FSTRING_LINK bool match_part(char *a, char *b, int *len); +FSTRING_LINK bool match_part(String a, char *b, int *len); +FSTRING_INLINE bool match_part(char *a, char *b) { int x; return match_part(a,b,&x); } +FSTRING_INLINE bool match_part(String a, char *b) { int x; return match_part(a,b,&x); } +FSTRING_LINK bool match_part(char *a, String b); +FSTRING_LINK bool match_part(String a, String b); -FCPP_LINK bool match_unsensitive(char *a, char *b); -FCPP_LINK bool match_unsensitive(String a, char *b); -inline bool match_unsensitive(char *a, String b) { return match_unsensitive(b,a); } -FCPP_LINK bool match_unsensitive(String a, String b); +FSTRING_LINK bool match_unsensitive(char *a, char *b); +FSTRING_LINK bool match_unsensitive(String a, char *b); +FSTRING_INLINE bool match_unsensitive(char *a, String b) { return match_unsensitive(b,a); } +FSTRING_LINK bool match_unsensitive(String a, String b); -FCPP_LINK bool match_part_unsensitive(char *a, char *b, int *len); -FCPP_LINK bool match_part_unsensitive(String a, char *b, int *len); -inline bool match_part_unsensitive(char *a, char *b) { int x; return match_part(a,b,&x); } -inline bool match_part_unsensitive(String a, char *b) { int x; return match_part(a,b,&x); } -FCPP_LINK bool match_part_unsensitive(char *a, String b); -FCPP_LINK bool match_part_unsensitive(String a, String b); +FSTRING_LINK bool match_part_unsensitive(char *a, char *b, int *len); +FSTRING_LINK bool match_part_unsensitive(String a, char *b, int *len); +FSTRING_INLINE bool match_part_unsensitive(char *a, char *b) { int x; return match_part(a,b,&x); } +FSTRING_INLINE bool match_part_unsensitive(String a, char *b) { int x; return match_part(a,b,&x); } +FSTRING_LINK bool match_part_unsensitive(char *a, String b); +FSTRING_LINK bool match_part_unsensitive(String a, String b); -FCPP_LINK int find(char *s, int start, char c); -FCPP_LINK int find(String s, int start, char c); -FCPP_LINK int find(char *s, int start, char *c); -FCPP_LINK int find(String s, int start, char *c); +FSTRING_LINK int find(char *s, int start, char c); +FSTRING_LINK int find(String s, int start, char c); +FSTRING_LINK int find(char *s, int start, char *c); +FSTRING_LINK int find(String s, int start, char *c); -FCPP_LINK int find_substr(char *s, int start, String seek); -FCPP_LINK int find_substr(String s, int start, String seek); -FCPP_LINK int rfind_substr(String s, int start, String seek); +FSTRING_LINK int find_substr(char *s, int start, String seek); +FSTRING_LINK int find_substr(String s, int start, String seek); +FSTRING_LINK int rfind_substr(String s, int start, String seek); -FCPP_LINK int find_substr_unsensitive(char *s, int start, String seek); -FCPP_LINK int find_substr_unsensitive(String s, int start, String seek); +FSTRING_LINK int find_substr_unsensitive(char *s, int start, String seek); +FSTRING_LINK int find_substr_unsensitive(String s, int start, String seek); -inline bool has_substr(char *s, String seek) { return (s[find_substr(s, 0, seek)] != 0); } -inline bool has_substr(String s, String seek) { return (find_substr(s, 0, seek) < s.size); } +FSTRING_INLINE bool has_substr(char *s, String seek) { return (s[find_substr(s, 0, seek)] != 0); } +FSTRING_INLINE bool has_substr(String s, String seek) { return (find_substr(s, 0, seek) < s.size); } -inline bool has_substr_unsensitive(char *s, String seek) { return (s[find_substr_unsensitive(s, 0, seek)] != 0); } -inline bool has_substr_unsensitive(String s, String seek) { return (find_substr_unsensitive(s, 0, seek) < s.size); } +FSTRING_INLINE bool has_substr_unsensitive(char *s, String seek) { return (s[find_substr_unsensitive(s, 0, seek)] != 0); } +FSTRING_INLINE bool has_substr_unsensitive(String s, String seek) { return (find_substr_unsensitive(s, 0, seek) < s.size); } -FCPP_LINK int int_to_str_size(int x); -FCPP_LINK int int_to_str(int x, char *s_out); -FCPP_LINK bool int_to_str(int x, String *s_out); -FCPP_LINK bool append_int_to_str(int x, String *s_out); +FSTRING_LINK int int_to_str_size(int x); +FSTRING_LINK int int_to_str(int x, char *s_out); +FSTRING_LINK bool int_to_str(int x, String *s_out); +FSTRING_LINK bool append_int_to_str(int x, String *s_out); -FCPP_LINK int str_to_int(char *s); -FCPP_LINK int str_to_int(String s); -FCPP_LINK int hexchar_to_int(char c); -FCPP_LINK char int_to_hexchar(int c); -FCPP_LINK unsigned int hexstr_to_int(String s); +FSTRING_LINK int str_to_int(char *s); +FSTRING_LINK int str_to_int(String s); +FSTRING_LINK int hexchar_to_int(char c); +FSTRING_LINK char int_to_hexchar(int c); +FSTRING_LINK unsigned int hexstr_to_int(String s); -FCPP_LINK bool color_to_hexstr(unsigned int color, String *s_out); -FCPP_LINK bool hexstr_to_color(String s, unsigned int *color); +FSTRING_LINK bool color_to_hexstr(unsigned int color, String *s_out); +FSTRING_LINK bool hexstr_to_color(String s, unsigned int *color); -FCPP_LINK int copy_fast_unsafe(char *dest, char *src); -FCPP_LINK void copy_fast_unsafe(char *dest, String src); -FCPP_LINK bool copy_checked(String *dest, String src); -FCPP_LINK bool copy_partial(String *dest, char *src); -FCPP_LINK bool copy_partial(String *dest, String src); +FSTRING_LINK int copy_fast_unsafe(char *dest, char *src); +FSTRING_LINK void copy_fast_unsafe(char *dest, String src); +FSTRING_LINK bool copy_checked(String *dest, String src); +FSTRING_LINK bool copy_partial(String *dest, char *src); +FSTRING_LINK bool copy_partial(String *dest, String src); -inline int copy(char *dest, char *src) { return copy_fast_unsafe(dest, src); } -inline void copy(String *dest, String src) { copy_checked(dest, src); } -inline void copy(String *dest, char *src) { copy_partial(dest, src); } +FSTRING_INLINE int copy(char *dest, char *src) { return copy_fast_unsafe(dest, src); } +FSTRING_INLINE void copy(String *dest, String src) { copy_checked(dest, src); } +FSTRING_INLINE void copy(String *dest, char *src) { copy_partial(dest, src); } -FCPP_LINK bool append_checked(String *dest, String src); -FCPP_LINK bool append_partial(String *dest, char *src); -FCPP_LINK bool append_partial(String *dest, String src); +FSTRING_LINK bool append_checked(String *dest, String src); +FSTRING_LINK bool append_partial(String *dest, char *src); +FSTRING_LINK bool append_partial(String *dest, String src); -FCPP_LINK bool append(String *dest, char c); -inline bool append(String *dest, String src) { return append_partial(dest, src); } -inline bool append(String *dest, char *src) { return append_partial(dest, src); } -inline bool terminate_with_null(String *str){ +FSTRING_LINK bool append(String *dest, char c); +FSTRING_INLINE bool append(String *dest, String src) { return append_partial(dest, src); } +FSTRING_INLINE bool append(String *dest, char *src) { return append_partial(dest, src); } +FSTRING_INLINE bool terminate_with_null(String *str){ bool result; if (str->size < str->memory_size){ str->str[str->size] = 0; @@ -175,32 +176,32 @@ inline bool terminate_with_null(String *str){ return result; } -FCPP_LINK int compare(char *a, char *b); -FCPP_LINK int compare(String a, char *b); -inline int compare(char *a, String b) { return -compare(b,a); } -FCPP_LINK int compare(String a, String b); +FSTRING_LINK int compare(char *a, char *b); +FSTRING_LINK int compare(String a, char *b); +FSTRING_INLINE int compare(char *a, String b) { return -compare(b,a); } +FSTRING_LINK int compare(String a, String b); -FCPP_LINK int reverse_seek_slash(String str); -FCPP_LINK int reverse_seek_slash(String str, int start_pos); -inline String front_of_directory(String dir) { return substr(dir, reverse_seek_slash(dir) + 1); } -inline String path_of_directory(String dir) { return substr(dir, 0, reverse_seek_slash(dir) + 1); } -inline bool get_front_of_directory(String *dest, String dir) { return append_checked(dest, front_of_directory(dir)); } -inline bool get_path_of_directory(String *dest, String dir) { return append_checked(dest, path_of_directory(dir)); } -FCPP_LINK bool set_last_folder(String *dir, char *folder_name, char slash); -FCPP_LINK bool set_last_folder(String *dir, String folder_name, char slash); -FCPP_LINK String file_extension(String str); -FCPP_LINK String file_extension_slowly(char *str); -FCPP_LINK char * file_extension_c(String str); -FCPP_LINK bool remove_last_folder(String *str); -FCPP_LINK void replace_char(String str, char replace, char with); -FCPP_LINK void replace_char(char *str, char replace, char with); +FSTRING_LINK int reverse_seek_slash(String str); +FSTRING_LINK int reverse_seek_slash(String str, int start_pos); +FSTRING_INLINE String front_of_directory(String dir) { return substr(dir, reverse_seek_slash(dir) + 1); } +FSTRING_INLINE String path_of_directory(String dir) { return substr(dir, 0, reverse_seek_slash(dir) + 1); } +FSTRING_INLINE bool get_front_of_directory(String *dest, String dir) { return append_checked(dest, front_of_directory(dir)); } +FSTRING_INLINE bool get_path_of_directory(String *dest, String dir) { return append_checked(dest, path_of_directory(dir)); } +FSTRING_LINK bool set_last_folder(String *dir, char *folder_name, char slash); +FSTRING_LINK bool set_last_folder(String *dir, String folder_name, char slash); +FSTRING_LINK String file_extension(String str); +FSTRING_LINK String file_extension_slowly(char *str); +FSTRING_LINK char * file_extension_c(String str); +FSTRING_LINK bool remove_last_folder(String *str); +FSTRING_LINK void replace_char(String str, char replace, char with); +FSTRING_LINK void replace_char(char *str, char replace, char with); -inline String string_zero(){ +FSTRING_INLINE String string_zero(){ String str={0}; return(str); } -inline String make_string(void *str, int size, int mem_size){ +FSTRING_INLINE String make_string(void *str, int size, int mem_size){ String result; result.str = (char*)str; result.size = size; @@ -208,7 +209,7 @@ inline String make_string(void *str, int size, int mem_size){ return result; } -inline String +FSTRING_INLINE String make_string(void *str, int size){ String result; result.str = (char*)str; @@ -217,7 +218,7 @@ make_string(void *str, int size){ return result; } -inline String +FSTRING_INLINE String make_string_slowly(void *str){ String result; result.str = (char*)str; @@ -226,7 +227,7 @@ make_string_slowly(void *str){ return result; } -inline char* +FSTRING_INLINE char* make_c_str(String str){ if (str.size < str.memory_size){ str.str[str.size] = 0; @@ -237,7 +238,7 @@ make_c_str(String str){ return (char*)str.str; } -inline String +FSTRING_INLINE String substr(String str, int start){ String result; result.str = str.str + start; @@ -245,7 +246,7 @@ substr(String str, int start){ return result; } -inline String +FSTRING_INLINE String substr(String str, int start, int size){ String result; result.str = str.str + start; @@ -258,7 +259,7 @@ substr(String str, int start, int size){ return result; } -inline String +FSTRING_INLINE String substr_slowly(char *str, int start){ String result; result.str = str + start; @@ -266,7 +267,7 @@ substr_slowly(char *str, int start){ return result; } -inline String +FSTRING_INLINE String substr(char *str, int start, int size){ String result; result.str = str + start; @@ -280,7 +281,7 @@ substr(char *str, int start, int size){ return result; } -inline String +FSTRING_INLINE String tailstr(String str){ String result; result.str = str.str + str.size; @@ -296,14 +297,14 @@ tailstr(String str){ #ifndef FCPP_DID_STRING_IMPLEMENTATION #define FCPP_DID_STRING_IMPLEMENTATION -FCPP_LINK int +FSTRING_LINK int str_size(char *str){ int i = 0; while (str[i]) ++i; return i; } -FCPP_LINK bool +FSTRING_LINK bool match(char *a, char *b){ for (int i = 0;; ++i){ if (a[i] != b[i]){ @@ -315,7 +316,7 @@ match(char *a, char *b){ } } -FCPP_LINK bool +FSTRING_LINK bool match(String a, char *b){ int i = 0; for (; i < a.size; ++i){ @@ -329,7 +330,7 @@ match(String a, char *b){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool match(String a, String b){ if (a.size != b.size){ return 0; @@ -342,7 +343,7 @@ match(String a, String b){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool match_part(char *a, char *b, int *len){ int i; for (i = 0; b[i] != 0; ++i){ @@ -354,7 +355,7 @@ match_part(char *a, char *b, int *len){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool match_part(String a, char *b, int *len){ int i; for (i = 0; b[i] != 0; ++i){ @@ -366,7 +367,7 @@ match_part(String a, char *b, int *len){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool match_part(char *a, String b){ for (int i = 0; i != b.size; ++i){ if (a[i] != b.str[i]){ @@ -376,7 +377,7 @@ match_part(char *a, String b){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool match_part(String a, String b){ if (a.size < b.size){ return 0; @@ -389,7 +390,7 @@ match_part(String a, String b){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool match_unsensitive(char *a, char *b){ for (int i = 0;; ++i){ if (char_to_upper(a[i]) != @@ -402,7 +403,7 @@ match_unsensitive(char *a, char *b){ } } -FCPP_LINK bool +FSTRING_LINK bool match_unsensitive(String a, char *b){ int i = 0; for (; i < a.size; ++i){ @@ -417,7 +418,7 @@ match_unsensitive(String a, char *b){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool match_unsensitive(String a, String b){ if (a.size != b.size){ return 0; @@ -431,7 +432,7 @@ match_unsensitive(String a, String b){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool match_part_unsensitive(char *a, char *b, int *len){ int i; for (i = 0; b[i] != 0; ++i){ @@ -443,7 +444,7 @@ match_part_unsensitive(char *a, char *b, int *len){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool match_part_unsensitive(String a, char *b, int *len){ int i; for (i = 0; b[i] != 0; ++i){ @@ -456,7 +457,7 @@ match_part_unsensitive(String a, char *b, int *len){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool match_part_unsensitive(char *a, String b){ for (int i = 0; i != b.size; ++i){ if (char_to_upper(a[i]) != char_to_upper(b.str[i])){ @@ -466,7 +467,7 @@ match_part_unsensitive(char *a, String b){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool match_part_unsensitive(String a, String b){ if (a.size < b.size){ return 0; @@ -479,21 +480,21 @@ match_part_unsensitive(String a, String b){ return 1; } -FCPP_LINK int +FSTRING_LINK int find(char *str, int start, char character){ int i = start; while (str[i] != character && str[i] != 0) ++i; return i; } -FCPP_LINK int +FSTRING_LINK int find(String str, int start, char character){ int i = start; while (i < str.size && str.str[i] != character) ++i; return i; } -FCPP_LINK int +FSTRING_LINK int find(char *str, int start, char *characters){ int i = start, j; while (str[i] != 0){ @@ -507,7 +508,7 @@ find(char *str, int start, char *characters){ return i; } -FCPP_LINK int +FSTRING_LINK int find(String str, int start, char *characters){ int i = start, j; while (i < str.size){ @@ -521,7 +522,7 @@ find(String str, int start, char *characters){ return i; } -FCPP_LINK int +FSTRING_LINK int find_substr(char *str, int start, String seek){ int i, j, k; bool hit; @@ -546,7 +547,7 @@ find_substr(char *str, int start, String seek){ return i; } -FCPP_LINK int +FSTRING_LINK int find_substr(String str, int start, String seek){ int stop_at, i, j, k; bool hit; @@ -572,7 +573,7 @@ find_substr(String str, int start, String seek){ return str.size; } -FCPP_LINK int +FSTRING_LINK int rfind_substr(String str, int start, String seek){ int i, j, k; bool hit; @@ -600,7 +601,7 @@ rfind_substr(String str, int start, String seek){ return -1; } -FCPP_LINK int +FSTRING_LINK int find_substr_unsensitive(char *str, int start, String seek){ int i, j, k; bool hit; @@ -628,7 +629,7 @@ find_substr_unsensitive(char *str, int start, String seek){ return i; } -FCPP_LINK int +FSTRING_LINK int find_substr_unsensitive(String str, int start, String seek){ int i, j, k; int stop_at; @@ -658,7 +659,7 @@ find_substr_unsensitive(String str, int start, String seek){ return str.size; } -FCPP_LINK int +FSTRING_LINK int int_to_str_size(int x){ int size; if (x < 0){ @@ -675,7 +676,7 @@ int_to_str_size(int x){ return size; } -FCPP_LINK int +FSTRING_LINK int int_to_str(int x, char *str){ int size, i, j; bool negative; @@ -712,7 +713,7 @@ int_to_str(int x, char *str){ return size; } -FCPP_LINK bool +FSTRING_LINK bool int_to_str(int x, String *dest){ bool result = 1; char *str = dest->str; @@ -758,7 +759,7 @@ int_to_str(int x, String *dest){ return result; } -FCPP_LINK bool +FSTRING_LINK bool append_int_to_str(int x, String *dest){ String last_part = tailstr(*dest); bool result = int_to_str(x, &last_part); @@ -768,7 +769,7 @@ append_int_to_str(int x, String *dest){ return result; } -FCPP_LINK int +FSTRING_LINK int str_to_int(char *str){ int x = 0; for (; *str; ++str){ @@ -784,7 +785,7 @@ str_to_int(char *str){ return(x); } -FCPP_LINK int +FSTRING_LINK int str_to_int(String str){ int x, i; if (str.size == 0){ @@ -800,7 +801,7 @@ str_to_int(String str){ return x; } -FCPP_LINK int +FSTRING_LINK int hexchar_to_int(char c){ int x; if (c >= '0' && c <= '9'){ @@ -815,12 +816,12 @@ hexchar_to_int(char c){ return x; } -FCPP_LINK char +FSTRING_LINK char int_to_hexchar(int x){ return (x<10)?((char)x+'0'):((char)x+'a'-10); } -FCPP_LINK unsigned int +FSTRING_LINK unsigned int hexstr_to_int(String str){ unsigned int x; int i; @@ -837,7 +838,7 @@ hexstr_to_int(String str){ return x; } -FCPP_LINK bool +FSTRING_LINK bool color_to_hexstr(unsigned int color, String *s){ bool result = 0; int i; @@ -864,7 +865,7 @@ color_to_hexstr(unsigned int color, String *s){ return(result); } -FCPP_LINK bool +FSTRING_LINK bool hexstr_to_color(String s, unsigned int *out){ bool result = 0; unsigned int color = 0; @@ -882,7 +883,7 @@ hexstr_to_color(String s, unsigned int *out){ return(result); } -FCPP_LINK int +FSTRING_LINK int copy_fast_unsafe(char *dest, char *src){ char *start = dest; while (*src != 0){ @@ -893,7 +894,7 @@ copy_fast_unsafe(char *dest, char *src){ return (int)(dest - start); } -FCPP_LINK void +FSTRING_LINK void copy_fast_unsafe(char *dest, String src){ int i = 0; while (i != src.size){ @@ -902,7 +903,7 @@ copy_fast_unsafe(char *dest, String src){ } } -FCPP_LINK bool +FSTRING_LINK bool copy_checked(String *dest, String src){ char *dest_str; int i; @@ -917,7 +918,7 @@ copy_checked(String *dest, String src){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool copy_partial(String *dest, char *src){ int i = 0; int memory_size = dest->memory_size; @@ -933,7 +934,7 @@ copy_partial(String *dest, char *src){ return 1; } -FCPP_LINK bool +FSTRING_LINK bool copy_partial(String *dest, String src){ bool result; int memory_size = dest->memory_size; @@ -955,7 +956,7 @@ copy_partial(String *dest, String src){ return result; } -FCPP_LINK bool +FSTRING_LINK bool append_checked(String *dest, String src){ String end; end = tailstr(*dest); @@ -966,7 +967,7 @@ append_checked(String *dest, String src){ return result; } -FCPP_LINK bool +FSTRING_LINK bool append_partial(String *dest, char *src){ String end = tailstr(*dest); bool result = copy_partial(&end, src); @@ -974,7 +975,7 @@ append_partial(String *dest, char *src){ return result; } -FCPP_LINK bool +FSTRING_LINK bool append_partial(String *dest, String src){ String end = tailstr(*dest); bool result = copy_partial(&end, src); @@ -982,7 +983,7 @@ append_partial(String *dest, String src){ return result; } -FCPP_LINK bool +FSTRING_LINK bool append(String *dest, char c){ bool result = 0; if (dest->size < dest->memory_size){ @@ -992,7 +993,7 @@ append(String *dest, char c){ return result; } -FCPP_LINK int +FSTRING_LINK int compare(char *a, char *b){ int i = 0; while (a[i] == b[i] && a[i] != 0){ @@ -1001,7 +1002,7 @@ compare(char *a, char *b){ return (a[i] > b[i]) - (a[i] < b[i]); } -FCPP_LINK int +FSTRING_LINK int compare(String a, char *b){ int i = 0; while (i < a.size && a.str[i] == b[i]){ @@ -1020,7 +1021,7 @@ compare(String a, char *b){ } } -FCPP_LINK int +FSTRING_LINK int compare(String a, String b){ int i = 0; while (i < a.size && i < b.size && a.str[i] == b.str[i]){ @@ -1034,7 +1035,7 @@ compare(String a, String b){ } } -FCPP_LINK int +FSTRING_LINK int reverse_seek_slash(String str, int pos){ int i = str.size - 1 - pos; while (i >= 0 && char_not_slash(str.str[i])){ @@ -1043,12 +1044,12 @@ reverse_seek_slash(String str, int pos){ return i; } -FCPP_LINK int +FSTRING_LINK int reverse_seek_slash(String str){ return(reverse_seek_slash(str, 0)); } -FCPP_LINK bool +FSTRING_LINK bool set_last_folder(String *dir, char *folder_name, char slash){ char str[2]; bool result = 0; @@ -1067,7 +1068,7 @@ set_last_folder(String *dir, char *folder_name, char slash){ return result; } -FCPP_LINK bool +FSTRING_LINK bool set_last_folder(String *dir, String folder_name, char slash){ char str[2]; bool result = 0; @@ -1086,7 +1087,7 @@ set_last_folder(String *dir, String folder_name, char slash){ return result; } -FCPP_LINK String +FSTRING_LINK String file_extension(String str){ int i; for (i = str.size - 1; i >= 0; --i){ @@ -1096,7 +1097,7 @@ file_extension(String str){ return make_string(str.str+i, str.size-i); } -FCPP_LINK String +FSTRING_LINK String file_extension_slowly(char *str){ int s, i; for (s = 0; str[s]; ++s); @@ -1107,7 +1108,7 @@ file_extension_slowly(char *str){ return make_string(str+i, s-i); } -FCPP_LINK char* +FSTRING_LINK char* file_extension_c(String str){ int i; for (i = str.size - 1; i >= 0; --i){ @@ -1117,7 +1118,7 @@ file_extension_c(String str){ return str.str+i; } -FCPP_LINK bool +FSTRING_LINK bool remove_last_folder(String *str){ bool result = 0; int end = reverse_seek_slash(*str, 1); @@ -1128,7 +1129,7 @@ remove_last_folder(String *str){ return(result); } -FCPP_LINK void +FSTRING_LINK void replace_char(String str, char replace, char with){ char *s = str.str; int i; @@ -1138,7 +1139,7 @@ replace_char(String str, char replace, char with){ } } -FCPP_LINK void +FSTRING_LINK void replace_char(char *str, char replace, char with){ for (; *str; ++str){ if (*str == replace) *str = with; @@ -1157,7 +1158,7 @@ struct Absolutes{ int count; }; -FCPP_LINK void +FSTRING_LINK void get_absolutes(String name, Absolutes *absolutes, bool implicit_first, bool implicit_last){ int count = 0; int max = ArrayCount(absolutes->a) - 1; @@ -1203,7 +1204,7 @@ get_absolutes(String name, Absolutes *absolutes, bool implicit_first, bool impli absolutes->count = count; } -FCPP_LINK bool +FSTRING_LINK bool wildcard_match(Absolutes *absolutes, char *x, int case_sensitive){ bool r = 1; String *a = absolutes->a; @@ -1261,7 +1262,7 @@ wildcard_match(Absolutes *absolutes, char *x, int case_sensitive){ return r; } -FCPP_LINK bool +FSTRING_LINK bool wildcard_match(Absolutes *absolutes, String x, int case_sensitive){ terminate_with_null(&x); return wildcard_match(absolutes, x.str, case_sensitive); diff --git a/4ed_dll_reader.cpp b/4ed_dll_reader.cpp index ac1adeb0..ea4d6384 100644 --- a/4ed_dll_reader.cpp +++ b/4ed_dll_reader.cpp @@ -1,378 +1,378 @@ -/* - * Mr. 4th Dimention - Allen Webster - * - * 20.11.2015 - * - * DLL loader declarations for 4coder - * - */ - -// TOP - -// TODO(allen): -// Check the relocation table, if it contains anything that -// is platform specific generate an error to avoid calling -// into invalid code. - -i32 -dll_compare(char *a, char *b, i32 len){ - i32 result; - char *e; - - result = 0; - e = a + len; - for (;a < e && *a == *b; ++a, ++b); - if (a < e){ - if (*a < *b) result = -1; - else result = 1; - } - - return(result); -} - -enum DLL_Error{ - dll_err_too_small_for_header = 1, - dll_err_wrong_MZ_signature, - dll_err_wrong_DOS_error, - dll_err_wrong_PE_signature, - dll_err_unrecognized_bit_signature, -}; - -b32 -dll_parse_headers(Data file, DLL_Data *dll, i32 *error){ - b32 result; - i32 pe_offset; - i32 read_pos; - - result = 1; - if (file.size <= sizeof(DOS_Header) + DOS_error_size){ - if (error) *error = dll_err_too_small_for_header; - result = 0; - goto dll_parse_end; - } - - dll->dos_header = (DOS_Header*)file.data; - - if (dll_compare(dll->dos_header->signature, "MZ", 2) != 0){ - if (error) *error = dll_err_wrong_MZ_signature; - result = 0; - goto dll_parse_end; - } - - if (file.size <= DOS_error_offset + DOS_error_size){ - if (error) *error = dll_err_too_small_for_header; - result = 0; - goto dll_parse_end; - } - - if (dll_compare((char*)(file.data + DOS_error_offset), DOS_error_message, - sizeof(DOS_error_message) - 1) != 0){ - if (error) *error = dll_err_wrong_DOS_error; - result = 0; - goto dll_parse_end; - } - - pe_offset = dll->dos_header->e_lfanew; - read_pos = pe_offset; - - if (file.size <= read_pos + PE_header_size){ - if (error) *error = dll_err_too_small_for_header; - result = 0; - goto dll_parse_end; - } - - if (dll_compare((char*)(file.data + read_pos), - PE_header, PE_header_size) != 0){ - if (error) *error = dll_err_wrong_PE_signature; - result = 0; - goto dll_parse_end; - } - - read_pos += PE_header_size; - - if (file.size <= read_pos + sizeof(COFF_Header)){ - if (error) *error = dll_err_too_small_for_header; - result = 0; - goto dll_parse_end; - } - - dll->coff_header = (COFF_Header*)(file.data + read_pos); - read_pos += sizeof(COFF_Header); - - if (file.size <= read_pos + dll->coff_header->size_of_optional_header){ - if (error) *error = dll_err_too_small_for_header; - result = 0; - goto dll_parse_end; - } - - dll->opt_header_32 = (PE_Opt_Header_32Bit*)(file.data + read_pos); - dll->opt_header_64 = (PE_Opt_Header_64Bit*)(file.data + read_pos); - read_pos += dll->coff_header->size_of_optional_header; - - if (dll->opt_header_32->signature != bitsig_32bit && - dll->opt_header_32->signature != bitsig_64bit){ - if (error) *error = dll_err_unrecognized_bit_signature; - result = 0; - goto dll_parse_end; - } - - if (dll->opt_header_32->signature == bitsig_32bit) dll->is_64bit = 0; - else dll->is_64bit = 1; - - dll->section_defs = (PE_Section_Definition*)(file.data + read_pos); - -dll_parse_end: - return(result); -} - -i32 -dll_total_loaded_size(DLL_Data *dll){ - COFF_Header *coff_header; - PE_Section_Definition *section_def; - i32 result, section_end, i; - - coff_header = dll->coff_header; - section_def = dll->section_defs; - result = 0; - - for (i = 0; i < coff_header->number_of_sections; ++i, ++section_def){ - section_end = section_def->loaded_location + section_def->loaded_size; - if (section_end > result){ - result = section_end; - } - } - - return(result); -} - -b32 -dll_perform_reloc(DLL_Loaded *loaded){ - Data img; - byte *base; - Relocation_Block_Header *header; - Relocation_Block_Entry *entry; - Data_Directory *data_directory; - u32 cursor; - u32 bytes_in_table; - u32 block_end; - u32 type; - u32 offset; - b32 result; - b32 highadj_stage; - - u64 dif64; - - result = 1; - img = loaded->img; - if (loaded->is_64bit){ - data_directory = loaded->opt_header_64->data_directory; - dif64 = ((u64)img.data - (u64)loaded->opt_header_64->image_base); - } - else{ - data_directory = loaded->opt_header_32->data_directory; - dif64 = ((u64)img.data - (u64)loaded->opt_header_32->image_base); - } - data_directory += image_dir_base_reloc_table; - base = img.data + data_directory->virtual_address; - bytes_in_table = data_directory->size; - - highadj_stage = 1; - - - for (cursor = 0; cursor < bytes_in_table;){ - header = (Relocation_Block_Header*)(base + cursor); - block_end = cursor + header->block_size; - cursor += sizeof(Relocation_Block_Header); - - for (;cursor < block_end;){ - entry = (Relocation_Block_Entry*)(base + cursor); - cursor += sizeof(Relocation_Block_Entry); - - type = (u32)(entry->entry & reloc_entry_type_mask) >> reloc_entry_type_shift; - offset = (u32)(entry->entry & reloc_entry_offset_mask) + header->page_base_offset; - - switch (type){ - case image_base_absolute: break; - - case image_base_high: - case image_base_low: - case image_base_highlow: - case image_base_highadj: - case image_base_arm_mov32a: - case image_base_arm_mov32t: - case image_base_mips_jmpaddr16: - result = 0; - goto dll_reloc_end; - - case image_base_dir64: - *(u64*)(img.data + offset) += dif64; - break; - } - } - } - -dll_reloc_end: - return(result); -} - -b32 -dll_load_sections(Data img, DLL_Loaded *loaded, - Data file, DLL_Data *dll){ - COFF_Header *coff_header; - PE_Section_Definition *section_def; - u32 header_size; - u32 size; - u32 i; - - coff_header = dll->coff_header; - section_def = dll->section_defs; - - header_size = - (u32)((byte*)(section_def + coff_header->number_of_sections) - file.data); - - memcpy(img.data, file.data, header_size); - memset(img.data + header_size, 0, img.size - header_size); - - for (i = 0; i < coff_header->number_of_sections; ++i, ++section_def){ - size = section_def->loaded_size; - if (size > section_def->disk_size) - size = section_def->disk_size; - - memcpy(img.data + section_def->loaded_location, - file.data + section_def->disk_location, - size); - - if (dll_compare(section_def->name, ".text", 5) == 0){ - loaded->text_start = section_def->loaded_location; - loaded->text_size = section_def->loaded_size; - } - } - - return(1); -} - -void -dll_load(Data img, DLL_Loaded *loaded, Data file, DLL_Data *dll){ - Data_Directory *export_dir; - - dll_load_sections(img, loaded, file, dll); - loaded->img = img; - - loaded->dos_header = (DOS_Header*)((byte*)img.data + ((byte*)dll->dos_header - file.data)); - loaded->coff_header = (COFF_Header*)((byte*)img.data + ((byte*)dll->coff_header - file.data)); - - loaded->opt_header_32 = (PE_Opt_Header_32Bit*) - ((byte*)img.data + ((byte*)dll->opt_header_32 - file.data)); - loaded->opt_header_64 = (PE_Opt_Header_64Bit*) - ((byte*)img.data + ((byte*)dll->opt_header_64 - file.data)); - - loaded->section_defs = (PE_Section_Definition*) - ((byte*)img.data + ((byte*)dll->section_defs - file.data)); - - loaded->is_64bit = dll->is_64bit; - - if (dll->is_64bit){ - export_dir = dll->opt_header_64->data_directory; - } - else{ - export_dir = dll->opt_header_32->data_directory; - } - export_dir += image_dir_entry_export; - loaded->export_start = export_dir->virtual_address; - - dll_perform_reloc(loaded); -} - -void* -dll_load_function(DLL_Loaded *dll, char *func_name, i32 size){ - Data img; - DLL_Export_Directory_Table *export_dir; - DLL_Export_Address *address_ptr; - DLL_Export_Name *name_ptr; - void *result; - u32 count, i; - u32 result_offset; - u32 ordinal; - - img = dll->img; - export_dir = (DLL_Export_Directory_Table*)(img.data + dll->export_start); - - count = export_dir->number_of_name_pointers; - name_ptr = (DLL_Export_Name*)(img.data + export_dir->name_pointer_offset); - - result = 0; - for (i = 0; i < count; ++i, ++name_ptr){ - if (dll_compare((char*)img.data + name_ptr->name_offset, - func_name, size) == 0){ - ordinal = ((u16*)(img.data + export_dir->ordinal_offset))[i]; -#if 0 - // NOTE(allen): The MS docs say to do this, but - // it appears to just be downright incorrect. - ordinal -= export_dir->ordinal_base; -#endif - address_ptr = (DLL_Export_Address*)(img.data + export_dir->address_offset); - address_ptr += ordinal; - result_offset = address_ptr->export_offset; - result = (img.data + result_offset); - break; - } - } - - return(result); -} - -#define MachineCase(x) case x: result = #x; *len = sizeof(#x) - 1; break - -char* -dll_machine_type_str(u16 machine, i32 *len){ - char *result; - i32 extra; - - if (!len) len = &extra; - result = 0; - - switch (machine){ - MachineCase(intel_i386); - MachineCase(intel_i860); - - MachineCase(mips_r3000); - MachineCase(mips_little_endian); - MachineCase(mips_r10000); - - MachineCase(old_alpha_axp); - MachineCase(alpha_axp); - - MachineCase(hitachi_sh3); - MachineCase(hitachi_sh3_dsp); - MachineCase(hitachi_sh4); - MachineCase(hitachi_sh5); - - MachineCase(arm_little_endian); - MachineCase(thumb); - - MachineCase(matsushita_am33); - MachineCase(power_pc_little_endian); - MachineCase(power_pc_with_floating); - - MachineCase(intel_ia64); - MachineCase(mips16); - MachineCase(motorola_68000_series); - - MachineCase(alpha_axp_64_bit); - - MachineCase(mips_with_fpu); - MachineCase(mips16_with_fpu); - MachineCase(eft_byte_code); - - MachineCase(amd_amd64); - MachineCase(mitsubishi_m32r_little_endian); - MachineCase(clr_pure_msil); - } - - return(result); -} - -#undef MachineCase - -// BOTTOM - +/* + * Mr. 4th Dimention - Allen Webster + * + * 20.11.2015 + * + * DLL loader declarations for 4coder + * + */ + +// TOP + +// TODO(allen): +// Check the relocation table, if it contains anything that +// is platform specific generate an error to avoid calling +// into invalid code. + +i32 +dll_compare(char *a, char *b, i32 len){ + i32 result; + char *e; + + result = 0; + e = a + len; + for (;a < e && *a == *b; ++a, ++b); + if (a < e){ + if (*a < *b) result = -1; + else result = 1; + } + + return(result); +} + +enum DLL_Error{ + dll_err_too_small_for_header = 1, + dll_err_wrong_MZ_signature, + dll_err_wrong_DOS_error, + dll_err_wrong_PE_signature, + dll_err_unrecognized_bit_signature, +}; + +b32 +dll_parse_headers(Data file, DLL_Data *dll, i32 *error){ + b32 result; + i32 pe_offset; + i32 read_pos; + + result = 1; + if (file.size <= sizeof(DOS_Header) + DOS_error_size){ + if (error) *error = dll_err_too_small_for_header; + result = 0; + goto dll_parse_end; + } + + dll->dos_header = (DOS_Header*)file.data; + + if (dll_compare(dll->dos_header->signature, "MZ", 2) != 0){ + if (error) *error = dll_err_wrong_MZ_signature; + result = 0; + goto dll_parse_end; + } + + if (file.size <= DOS_error_offset + DOS_error_size){ + if (error) *error = dll_err_too_small_for_header; + result = 0; + goto dll_parse_end; + } + + if (dll_compare((char*)(file.data + DOS_error_offset), DOS_error_message, + sizeof(DOS_error_message) - 1) != 0){ + if (error) *error = dll_err_wrong_DOS_error; + result = 0; + goto dll_parse_end; + } + + pe_offset = dll->dos_header->e_lfanew; + read_pos = pe_offset; + + if (file.size <= read_pos + PE_header_size){ + if (error) *error = dll_err_too_small_for_header; + result = 0; + goto dll_parse_end; + } + + if (dll_compare((char*)(file.data + read_pos), + PE_header, PE_header_size) != 0){ + if (error) *error = dll_err_wrong_PE_signature; + result = 0; + goto dll_parse_end; + } + + read_pos += PE_header_size; + + if (file.size <= read_pos + sizeof(COFF_Header)){ + if (error) *error = dll_err_too_small_for_header; + result = 0; + goto dll_parse_end; + } + + dll->coff_header = (COFF_Header*)(file.data + read_pos); + read_pos += sizeof(COFF_Header); + + if (file.size <= read_pos + dll->coff_header->size_of_optional_header){ + if (error) *error = dll_err_too_small_for_header; + result = 0; + goto dll_parse_end; + } + + dll->opt_header_32 = (PE_Opt_Header_32Bit*)(file.data + read_pos); + dll->opt_header_64 = (PE_Opt_Header_64Bit*)(file.data + read_pos); + read_pos += dll->coff_header->size_of_optional_header; + + if (dll->opt_header_32->signature != bitsig_32bit && + dll->opt_header_32->signature != bitsig_64bit){ + if (error) *error = dll_err_unrecognized_bit_signature; + result = 0; + goto dll_parse_end; + } + + if (dll->opt_header_32->signature == bitsig_32bit) dll->is_64bit = 0; + else dll->is_64bit = 1; + + dll->section_defs = (PE_Section_Definition*)(file.data + read_pos); + +dll_parse_end: + return(result); +} + +i32 +dll_total_loaded_size(DLL_Data *dll){ + COFF_Header *coff_header; + PE_Section_Definition *section_def; + i32 result, section_end, i; + + coff_header = dll->coff_header; + section_def = dll->section_defs; + result = 0; + + for (i = 0; i < coff_header->number_of_sections; ++i, ++section_def){ + section_end = section_def->loaded_location + section_def->loaded_size; + if (section_end > result){ + result = section_end; + } + } + + return(result); +} + +b32 +dll_perform_reloc(DLL_Loaded *loaded){ + Data img; + byte *base; + Relocation_Block_Header *header; + Relocation_Block_Entry *entry; + Data_Directory *data_directory; + u32 cursor; + u32 bytes_in_table; + u32 block_end; + u32 type; + u32 offset; + b32 result; + b32 highadj_stage; + + u64 dif64; + + result = 1; + img = loaded->img; + if (loaded->is_64bit){ + data_directory = loaded->opt_header_64->data_directory; + dif64 = ((u64)img.data - (u64)loaded->opt_header_64->image_base); + } + else{ + data_directory = loaded->opt_header_32->data_directory; + dif64 = ((u64)img.data - (u64)loaded->opt_header_32->image_base); + } + data_directory += image_dir_base_reloc_table; + base = img.data + data_directory->virtual_address; + bytes_in_table = data_directory->size; + + highadj_stage = 1; + + + for (cursor = 0; cursor < bytes_in_table;){ + header = (Relocation_Block_Header*)(base + cursor); + block_end = cursor + header->block_size; + cursor += sizeof(Relocation_Block_Header); + + for (;cursor < block_end;){ + entry = (Relocation_Block_Entry*)(base + cursor); + cursor += sizeof(Relocation_Block_Entry); + + type = (u32)(entry->entry & reloc_entry_type_mask) >> reloc_entry_type_shift; + offset = (u32)(entry->entry & reloc_entry_offset_mask) + header->page_base_offset; + + switch (type){ + case image_base_absolute: break; + + case image_base_high: + case image_base_low: + case image_base_highlow: + case image_base_highadj: + case image_base_arm_mov32a: + case image_base_arm_mov32t: + case image_base_mips_jmpaddr16: + result = 0; + goto dll_reloc_end; + + case image_base_dir64: + *(u64*)(img.data + offset) += dif64; + break; + } + } + } + +dll_reloc_end: + return(result); +} + +b32 +dll_load_sections(Data img, DLL_Loaded *loaded, + Data file, DLL_Data *dll){ + COFF_Header *coff_header; + PE_Section_Definition *section_def; + u32 header_size; + u32 size; + u32 i; + + coff_header = dll->coff_header; + section_def = dll->section_defs; + + header_size = + (u32)((byte*)(section_def + coff_header->number_of_sections) - file.data); + + memcpy(img.data, file.data, header_size); + memset(img.data + header_size, 0, img.size - header_size); + + for (i = 0; i < coff_header->number_of_sections; ++i, ++section_def){ + size = section_def->loaded_size; + if (size > section_def->disk_size) + size = section_def->disk_size; + + memcpy(img.data + section_def->loaded_location, + file.data + section_def->disk_location, + size); + + if (dll_compare(section_def->name, ".text", 5) == 0){ + loaded->text_start = section_def->loaded_location; + loaded->text_size = section_def->loaded_size; + } + } + + return(1); +} + +void +dll_load(Data img, DLL_Loaded *loaded, Data file, DLL_Data *dll){ + Data_Directory *export_dir; + + dll_load_sections(img, loaded, file, dll); + loaded->img = img; + + loaded->dos_header = (DOS_Header*)((byte*)img.data + ((byte*)dll->dos_header - file.data)); + loaded->coff_header = (COFF_Header*)((byte*)img.data + ((byte*)dll->coff_header - file.data)); + + loaded->opt_header_32 = (PE_Opt_Header_32Bit*) + ((byte*)img.data + ((byte*)dll->opt_header_32 - file.data)); + loaded->opt_header_64 = (PE_Opt_Header_64Bit*) + ((byte*)img.data + ((byte*)dll->opt_header_64 - file.data)); + + loaded->section_defs = (PE_Section_Definition*) + ((byte*)img.data + ((byte*)dll->section_defs - file.data)); + + loaded->is_64bit = dll->is_64bit; + + if (dll->is_64bit){ + export_dir = dll->opt_header_64->data_directory; + } + else{ + export_dir = dll->opt_header_32->data_directory; + } + export_dir += image_dir_entry_export; + loaded->export_start = export_dir->virtual_address; + + dll_perform_reloc(loaded); +} + +void* +dll_load_function(DLL_Loaded *dll, char *func_name, i32 size){ + Data img; + DLL_Export_Directory_Table *export_dir; + DLL_Export_Address *address_ptr; + DLL_Export_Name *name_ptr; + void *result; + u32 count, i; + u32 result_offset; + u32 ordinal; + + img = dll->img; + export_dir = (DLL_Export_Directory_Table*)(img.data + dll->export_start); + + count = export_dir->number_of_name_pointers; + name_ptr = (DLL_Export_Name*)(img.data + export_dir->name_pointer_offset); + + result = 0; + for (i = 0; i < count; ++i, ++name_ptr){ + if (dll_compare((char*)img.data + name_ptr->name_offset, + func_name, size) == 0){ + ordinal = ((u16*)(img.data + export_dir->ordinal_offset))[i]; +#if 0 + // NOTE(allen): The MS docs say to do this, but + // it appears to just be downright incorrect. + ordinal -= export_dir->ordinal_base; +#endif + address_ptr = (DLL_Export_Address*)(img.data + export_dir->address_offset); + address_ptr += ordinal; + result_offset = address_ptr->export_offset; + result = (img.data + result_offset); + break; + } + } + + return(result); +} + +#define MachineCase(x) case x: result = #x; *len = sizeof(#x) - 1; break + +char* +dll_machine_type_str(u16 machine, i32 *len){ + char *result; + i32 extra; + + if (!len) len = &extra; + result = 0; + + switch (machine){ + MachineCase(intel_i386); + MachineCase(intel_i860); + + MachineCase(mips_r3000); + MachineCase(mips_little_endian); + MachineCase(mips_r10000); + + MachineCase(old_alpha_axp); + MachineCase(alpha_axp); + + MachineCase(hitachi_sh3); + MachineCase(hitachi_sh3_dsp); + MachineCase(hitachi_sh4); + MachineCase(hitachi_sh5); + + MachineCase(arm_little_endian); + MachineCase(thumb); + + MachineCase(matsushita_am33); + MachineCase(power_pc_little_endian); + MachineCase(power_pc_with_floating); + + MachineCase(intel_ia64); + MachineCase(mips16); + MachineCase(motorola_68000_series); + + MachineCase(alpha_axp_64_bit); + + MachineCase(mips_with_fpu); + MachineCase(mips16_with_fpu); + MachineCase(eft_byte_code); + + MachineCase(amd_amd64); + MachineCase(mitsubishi_m32r_little_endian); + MachineCase(clr_pure_msil); + } + + return(result); +} + +#undef MachineCase + +// BOTTOM + diff --git a/win32_4ed.cpp b/win32_4ed.cpp index 456cbbc1..752ad09a 100644 --- a/win32_4ed.cpp +++ b/win32_4ed.cpp @@ -10,23 +10,15 @@ // TOP #include "4coder_default_bindings.cpp" -#undef exec_command -#undef exec_command_keep_stack -#undef clear_parameters #include "4ed_meta.h" -#define FCPP_FORBID_MALLOC - -#include "4cpp_types.h" #define FCPP_STRING_IMPLEMENTATION #include "4coder_string.h" #include "4ed_mem.cpp" #include "4ed_math.cpp" -#include - #include "4ed_system.h" #include "4ed_rendering.h" #include "4ed.h" @@ -36,21 +28,10 @@ #include "system_shared.h" -#if FRED_INTERNAL - -struct Sys_Bubble : public Bubble{ - i32 line_number; - char *file_name; -}; - -#endif - #define FPS 60 #define frame_useconds (1000000 / FPS) -#define WM_4coder_SET_CURSOR (WM_USER + 1) -#define WM_4coder_ANIMATE (WM_USER + 2) -#define WM_4coder_EVENT_COMPLETE (WM_USER + 3) +#define WM_4coder_ANIMATE (WM_USER + 1) struct Thread_Context{ u32 job_id; @@ -123,6 +104,13 @@ struct Win32_Coroutine{ int done; }; +#if FRED_INTERNAL +struct Sys_Bubble : public Bubble{ + i32 line_number; + char *file_name; +}; +#endif + struct Win32_Vars{ HWND window_handle; HDC window_hdc; @@ -194,7 +182,7 @@ INTERNAL_system_sentinel(){ internal void INTERNAL_system_debug_message(char *message){ - OutputDebugString(message); + OutputDebugStringA(message); } #endif @@ -636,10 +624,19 @@ Sys_Release_Lock_Sig(system_release_lock){ internal void Win32SetCursorFromUpdate(Application_Mouse_Cursor cursor){ - SendMessage( - win32vars.window_handle, - WM_4coder_SET_CURSOR, - cursor, 0); + switch (cursor){ + case APP_MOUSE_CURSOR_ARROW: + SetCursor(win32vars.cursor_arrow); break; + + case APP_MOUSE_CURSOR_IBEAM: + SetCursor(win32vars.cursor_ibeam); break; + + case APP_MOUSE_CURSOR_LEFTRIGHT: + SetCursor(win32vars.cursor_leftright); break; + + case APP_MOUSE_CURSOR_UPDOWN: + SetCursor(win32vars.cursor_updown); break; + } } internal void @@ -722,7 +719,7 @@ JobThreadProc(LPVOID lpParameter){ } full_job->job.callback(win32vars.system, thread, thread_memory, &exchange_vars.thread, full_job->job.data); - PostMessage(win32vars.window_handle, WM_4coder_EVENT_COMPLETE, 0, 0); + PostMessage(win32vars.window_handle, WM_4coder_ANIMATE, 0, 0); full_job->running_thread = 0; thread->running = 0; } @@ -1085,12 +1082,12 @@ Win32LoadAppCode(){ } #else - Data file = system_load_file("4ed_app.dll"); + File_Data file = system_load_file("4ed_app.dll"); - if (file.data){ + if (file.got_file){ i32 error; DLL_Data dll_data; - if (dll_parse_headers(file, &dll_data, &error)){ + if (dll_parse_headers(file.data, &dll_data, &error)){ Data img; img.size = dll_total_loaded_size(&dll_data); img.data = (byte*) @@ -1098,7 +1095,7 @@ Win32LoadAppCode(){ MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); - dll_load(img, &win32vars.app_dll, file, &dll_data); + dll_load(img, &win32vars.app_dll, file.data, &dll_data); DWORD extra_; VirtualProtect(img.data + win32vars.app_dll.text_start, @@ -1113,9 +1110,7 @@ Win32LoadAppCode(){ // TODO(allen): file loading error } - system_free(file.data); - - DUMP((byte*)(Tbytes(3)), Kbytes(400)); + Win32FreeMemory(file.data.data); } else{ // TODO(allen): file loading error @@ -1564,23 +1559,6 @@ Win32Callback(HWND hwnd, UINT uMsg, EndPaint(hwnd, &ps); }break; - case WM_4coder_SET_CURSOR: - { - switch (wParam){ - case APP_MOUSE_CURSOR_ARROW: - SetCursor(win32vars.cursor_arrow); break; - - case APP_MOUSE_CURSOR_IBEAM: - SetCursor(win32vars.cursor_ibeam); break; - - case APP_MOUSE_CURSOR_LEFTRIGHT: - SetCursor(win32vars.cursor_leftright); break; - - case APP_MOUSE_CURSOR_UPDOWN: - SetCursor(win32vars.cursor_updown); break; - } - }break; - case WM_CLOSE: // NOTE(allen): I expect WM_CLOSE not WM_DESTROY case WM_DESTROY: { @@ -1589,7 +1567,6 @@ Win32Callback(HWND hwnd, UINT uMsg, }break; case WM_4coder_ANIMATE: - case WM_4coder_EVENT_COMPLETE: win32vars.got_useful_event = 1; break; @@ -1713,7 +1690,7 @@ UpdateStep(){ else{ file->flags |= FEx_Save_Failed; } - PostMessage(win32vars.window_handle, WM_4coder_EVENT_COMPLETE, 0, 0); + PostMessage(win32vars.window_handle, WM_4coder_ANIMATE, 0, 0); } if (file->flags & FEx_Request){ @@ -1728,7 +1705,7 @@ UpdateStep(){ file->data = sysfile.data.data; file->size = sysfile.data.size; } - PostMessage(win32vars.window_handle, WM_4coder_EVENT_COMPLETE, 0, 0); + PostMessage(win32vars.window_handle, WM_4coder_ANIMATE, 0, 0); } } @@ -2044,7 +2021,7 @@ int main(int argc, char **argv){ } // TODO(allen): not Windows XP compatible, do we care? - SetProcessDPIAware(); + // SetProcessDPIAware(); HWND window_handle = {}; window_handle = CreateWindowA(