diff --git a/include/search.h b/include/search.h new file mode 100644 index 0000000..b2307b7 --- /dev/null +++ b/include/search.h @@ -0,0 +1,7 @@ +#ifndef SEARCH_H +#define SEARCH_H + +int BM_search(char *txt, int n, char *pat, int m); + +#endif + diff --git a/source/fs.c b/source/fs.c index 2b954f6..5d76cd9 100644 --- a/source/fs.c +++ b/source/fs.c @@ -25,29 +25,30 @@ */ #include - + #include "fs.h" #include "unicode.h" - +#include "search.h" + #include #include FS_Archive ArchiveSD; FS_Archive ArchiveHomeExt; FS_Archive ArchiveThemeExt; - + Result open_archives(void) { romfsInit(); u8 regionCode; u32 archive1; u32 archive2; - + Result res = 0; - + FS_Path home; FS_Path theme; - + CFGU_SecureInfoGetRegion(®ionCode); switch(regionCode) { @@ -67,7 +68,7 @@ Result open_archives(void) archive1 = 0x00; archive2 = 0x00; } - + if(R_FAILED(res = FSUSER_OpenArchive(&ArchiveSD, ARCHIVE_SDMC, fsMakePath(PATH_EMPTY, "")))) return res; FSUSER_CreateDirectory(ArchiveSD, fsMakePath(PATH_ASCII, "/Themes"), FS_ATTRIBUTE_DIRECTORY); @@ -75,43 +76,43 @@ Result open_archives(void) FSUSER_CreateDirectory(ArchiveSD, fsMakePath(PATH_ASCII, "/3ds"), FS_ATTRIBUTE_DIRECTORY); FSUSER_CreateDirectory(ArchiveSD, fsMakePath(PATH_ASCII, "/3ds/" APP_TITLE), FS_ATTRIBUTE_DIRECTORY); FSUSER_CreateDirectory(ArchiveSD, fsMakePath(PATH_ASCII, "/3ds/" APP_TITLE "/cache"), FS_ATTRIBUTE_DIRECTORY); - + u32 homeMenuPath[3] = {MEDIATYPE_SD, archive2, 0}; home.type = PATH_BINARY; home.size = 0xC; home.data = homeMenuPath; if(R_FAILED(res = FSUSER_OpenArchive(&ArchiveHomeExt, ARCHIVE_EXTDATA, home))) return res; - + u32 themePath[3] = {MEDIATYPE_SD, archive1, 0}; theme.type = PATH_BINARY; theme.size = 0xC; theme.data = themePath; if(R_FAILED(res = FSUSER_OpenArchive(&ArchiveThemeExt, ARCHIVE_EXTDATA, theme))) return res; - + Handle test_handle; if(R_FAILED(res = FSUSER_OpenFile(&test_handle, ArchiveThemeExt, fsMakePath(PATH_ASCII, "/ThemeManage.bin"), FS_OPEN_READ, 0))) return res; FSFILE_Close(test_handle); - + return 0; } Result close_archives(void) { Result res; - + if(R_FAILED(res = FSUSER_CloseArchive(ArchiveSD))) return res; if(R_FAILED(res = FSUSER_CloseArchive(ArchiveHomeExt))) return res; if(R_FAILED(res = FSUSER_CloseArchive(ArchiveThemeExt))) return res; return 0; } - + u32 file_to_buf(FS_Path path, FS_Archive archive, char** buf) { Handle file; Result res = 0; if (R_FAILED(res = FSUSER_OpenFile(&file, archive, path, FS_OPEN_READ, 0))) return 0; - + u64 size; FSFILE_GetSize(file, &size); if(size != 0) @@ -195,7 +196,105 @@ Result buf_to_file(u32 size, FS_Path path, FS_Archive archive, char *buf) if (R_FAILED(res = FSFILE_Close(handle))) return res; return 0; } - + +u32 decompress_lz_file(FS_Path file_name, char **buf) +{ + Handle handle; + Result res = 0; + if (R_FAILED(res = FSUSER_OpenFile(&handle, ArchiveSD, file_name, FS_OPEN_READ, 0))) { + DEBUG("%lu\n", res); + return 0; + } + u64 size; + FSFILE_GetSize(handle, &size); + + char *temp_buf = NULL; + + if(size != 0) + { + temp_buf = calloc(1, size); + FSFILE_Read(handle, NULL, 0, temp_buf, size); + } + FSFILE_Close(handle); + + if (temp_buf[0] != 0x11) { + return 0; + } + + u32 output_size = temp_buf[1] | ((temp_buf[2] << 8) & 0xFF00) | ((temp_buf[3] << 16) & 0xFF0000); + printf("%ld\n", output_size); + + + *buf = calloc(1, output_size); + + u32 pos = 4; + u32 cur_written = 0; + u8 counter = 0; + u8 mask = 0; + + while (cur_written < output_size) + { + if (counter == 0) // read mask + { + mask = temp_buf[pos++]; + counter++; + continue; + } + + if ((mask >> (8 - counter)) & 0x01) // compressed block + { + int len = 0; + int disp = 0; + switch (temp_buf[pos] >> 4) + { + case 0: + len = temp_buf[pos++] << 4; + len |= temp_buf[pos] >> 4; + len += 0x11; + break; + + case 1: + len = (temp_buf[pos++] & 0x0F) << 12; + len |= temp_buf[pos++] << 4; + len |= temp_buf[pos] >> 4; + len += 0x111; + break; + + default: + len = (temp_buf[pos] >> 4) + 1; + } + + disp = (temp_buf[pos++] & 0x0F) << 8; + disp |= temp_buf[pos++]; + + for (int i = 0; i < len; ++i) + { + *(*buf + cur_written + i) = *(*buf + cur_written - disp - 1 + i); + } + + cur_written += len; + } + else // byte literal + { + *(*buf + cur_written) = temp_buf[pos++]; + cur_written++; + } + + if (++counter > 8) counter = 0; + + } + + + free(temp_buf); + + return cur_written; +} + +u32 compress_lz_file(FS_Path path, char *buf, u32 len) +{ + +} + void remake_file(FS_Path path, FS_Archive archive, u32 size) { Handle handle; diff --git a/source/main.c b/source/main.c index aaaf17b..a89e66b 100644 --- a/source/main.c +++ b/source/main.c @@ -317,7 +317,7 @@ static void toggle_shuffle(Entry_List_s * list) current_entry->no_bgm_shuffle = true; } } - else + else { current_entry->in_shuffle = true; list->shuffle_count++; @@ -408,7 +408,7 @@ int main(void) } if(qr_mode) take_picture(); - else if(preview_mode) + else if(preview_mode) { draw_preview(preview, preview_offset); } diff --git a/source/search.c b/source/search.c new file mode 100644 index 0000000..0492a37 --- /dev/null +++ b/source/search.c @@ -0,0 +1,54 @@ +/* C implementation of Boyer-Moore Pattern Searching taken from https://www.geeksforgeeks.org/boyer-moore-algorithm-for-pattern-searching/ */ + +# include +# include +# include + +# define NO_OF_CHARS 256 + +int max (int a, int b) { return (a > b)? a: b; } + + +void badCharHeuristic( char *str, int size, + int badchar[NO_OF_CHARS]) +{ + int i; + + for (i = 0; i < NO_OF_CHARS; i++) + badchar[i] = -1; + + for (i = 0; i < size; i++) + badchar[(int) str[i]] = i; +} + + +int BM_search(char *txt, int n, char *pat, int m) +{ + + int badchar[NO_OF_CHARS]; + + badCharHeuristic(pat, m, badchar); + + int s = 0; + + while(s <= (n - m)) + { + int j = m-1; + + while(j >= 0 && pat[j] == txt[s+j]) + j--; + + if (j < 0) + { + return s; + + s += (s+m < n)? m-badchar[txt[s+m]] : 1; + + } + + else + s += max(1, j - badchar[txt[s+j]]); + } + + return -1; +}