From: chaoskagami Date: Tue, 24 May 2016 02:00:01 +0000 (-0400) Subject: Switch corbenik to use same memfind as loader X-Git-Tag: stable-1~44 X-Git-Url: https://chaos.moe/g/?a=commitdiff_plain;h=f84d61ec3fe9071977f583f218958338ea7bab30;p=corbenik%2Fcorbenik.git Switch corbenik to use same memfind as loader --- diff --git a/Makefile b/Makefile index f22430f..cd09351 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,7 @@ host/langemu.conf: clean: make -C modules clean make -C external clean - rm host/langemu.conf + rm -f host/langemu.conf rm -rf $(dir_out) $(dir_build) .PHONY: $(dir_out)/arm9loaderhax.bin diff --git a/source/main.c b/source/main.c index d0a35aa..c66ec4e 100644 --- a/source/main.c +++ b/source/main.c @@ -22,7 +22,7 @@ main() // Autoboot. Non-standard code path. if (config.options[OPTION_AUTOBOOT] && !(HID_PAD & BUTTON_R)) { if (config.options[OPTION_SILENCE]) - shut_up(); + shut_up(); // This does exactly what it sounds like. load_firms(); doing_autoboot = 1; boot_cfw(); // Just boot shit. diff --git a/source/std/draw.c b/source/std/draw.c index 0695573..0cdacac 100644 --- a/source/std/draw.c +++ b/source/std/draw.c @@ -241,6 +241,9 @@ putc(void* buf, const int c) void puts(void* buf, const char* string) { + if ((buf == stdout || buf == stderr) && kill_output) + return; + char* ref = (char*)string; while (*ref != '\0') { @@ -331,6 +334,9 @@ void fflush(void* channel) { if (channel == TOP_SCREEN) { + if (kill_output) + return; + for (int y = 0; y < TEXT_TOP_HEIGHT; y++) { for (int x = 0; x < TEXT_TOP_WIDTH; x++) { char c = text_buffer_top[y * TEXT_TOP_WIDTH + x]; @@ -345,6 +351,9 @@ fflush(void* channel) } } } else if (channel == BOTTOM_SCREEN) { + if (kill_output) + return; + for (int y = 0; y < TEXT_BOTTOM_HEIGHT; y++) { for (int x = 0; x < TEXT_BOTTOM_WIDTH; x++) { char c = text_buffer_bottom[y * TEXT_BOTTOM_WIDTH + x]; @@ -367,6 +376,9 @@ fflush(void* channel) void vfprintf(void* channel, const char* format, va_list ap) { + if ((channel == stdout || channel == stderr) && kill_output) + return; + char* ref = (char*)format; unsigned char* color; @@ -479,6 +491,12 @@ vfprintf(void* channel, const char* format, va_list ap) void fprintf(void* channel, const char* format, ...) { + // The output suppression is in all the functions to minimize overhead. + // Function calls and format conversions take time and we don't just want + // to stop at putc + if ((channel == stdout || channel == stderr) && kill_output) + return; + va_list ap; va_start(ap, format); diff --git a/source/std/memory.c b/source/std/memory.c index 27c7cdb..f829b36 100644 --- a/source/std/memory.c +++ b/source/std/memory.c @@ -167,91 +167,30 @@ atoi(const char* str) #define NOT_FOUND patlen #define max(a, b) ((a < b) ? b : a) -void -make_delta1(int* delta1, uint8_t* pat, int32_t patlen) +// Quick Search algorithm, adapted from +// http://igm.univ-mlv.fr/~lecroq/string/node19.html#SECTION00190 +uint8_t* +memfind(uint8_t* startPos, uint32_t size, const void* pattern, uint32_t patternSize) { - int i; - for (i = 0; i < ALPHABET_LEN; i++) { - delta1[i] = NOT_FOUND; - } - for (i = 0; i < patlen - 1; i++) { - delta1[pat[i]] = patlen - 1 - i; - } -} + const uint8_t* patternc = (const uint8_t*)pattern; -int -is_prefix(uint8_t* word, int wordlen, int pos) -{ - int i; - int suffixlen = wordlen - pos; - // could also use the strncmp() library function here - for (i = 0; i < suffixlen; i++) { - if (word[i] != word[pos + i]) { - return 0; - } - } - return 1; -} + // Preprocessing + uint32_t table[ALPHABET_LEN]; -int -suffix_length(uint8_t* word, int wordlen, int pos) -{ - int i; - // increment suffix length i to the first mismatch or beginning - // of the word - for (i = 0; (word[pos - i] == word[wordlen - 1 - i]) && (i < pos); i++) - ; - return i; -} + for (uint32_t i = 0; i < ALPHABET_LEN; ++i) + table[i] = patternSize + 1; + for (uint32_t i = 0; i < patternSize; ++i) + table[patternc[i]] = patternSize - i; -void -make_delta2(int* delta2, uint8_t* pat, int32_t patlen) -{ - int p; - int last_prefix_index = patlen - 1; + // Searching + uint32_t j = 0; - // first loop - for (p = patlen - 1; p >= 0; p--) { - if (is_prefix(pat, patlen, p + 1)) { - last_prefix_index = p + 1; - } - delta2[p] = last_prefix_index + (patlen - 1 - p); + while (j <= size - patternSize) { + if (memcmp(patternc, startPos + j, patternSize) == 0) + return startPos + j; + j += table[startPos[j + patternSize]]; } - // second loop - for (p = 0; p < patlen - 1; p++) { - int slen = suffix_length(pat, patlen, p); - if (pat[p - slen] != pat[patlen - 1 - slen]) { - delta2[patlen - 1 - slen] = patlen - 1 - p + slen; - } - } -} - -uint8_t* -memfind(uint8_t* string, uint32_t stringlen, uint8_t* pat, uint32_t patlen) -{ - uint32_t i; - int delta1[ALPHABET_LEN]; - int delta2[ALPHABET_LEN]; // Max search length is 256. - make_delta1(delta1, pat, patlen); - make_delta2(delta2, pat, patlen); - - // The empty pattern must be considered specially - if (patlen == 0) - return string; - - i = patlen - 1; - while (i < stringlen) { - int j = patlen - 1; - while (j >= 0 && (string[i] == pat[j])) { - --i; - --j; - } - if (j < 0) { - return (string + i + 1); - } - - i += max(delta1[string[i]], delta2[j]); - } return NULL; } + diff --git a/source/std/memory.h b/source/std/memory.h index 859115f..44e59ae 100644 --- a/source/std/memory.h +++ b/source/std/memory.h @@ -12,8 +12,8 @@ int memcmp(const void* buf1, const void* buf2, const size_t size); void strncpy(void* dest, const void* src, const size_t size); int strncmp(const void* buf1, const void* buf2, const size_t size); int atoi(const char* str); -uint8_t* memfind(uint8_t* string, uint32_t stringlen, uint8_t* pat, - uint32_t patlen); +uint8_t* memfind(uint8_t* startPos, uint32_t size, + const void* pattern, uint32_t patternSize); int isprint(char c); #endif