Add lz decompression code
Also add byte search code
This commit is contained in:
7
include/search.h
Normal file
7
include/search.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef SEARCH_H
|
||||||
|
#define SEARCH_H
|
||||||
|
|
||||||
|
int BM_search(char *txt, int n, char *pat, int m);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
99
source/fs.c
99
source/fs.c
@@ -28,6 +28,7 @@
|
|||||||
|
|
||||||
#include "fs.h"
|
#include "fs.h"
|
||||||
#include "unicode.h"
|
#include "unicode.h"
|
||||||
|
#include "search.h"
|
||||||
|
|
||||||
#include <archive.h>
|
#include <archive.h>
|
||||||
#include <archive_entry.h>
|
#include <archive_entry.h>
|
||||||
@@ -196,6 +197,104 @@ Result buf_to_file(u32 size, FS_Path path, FS_Archive archive, char *buf)
|
|||||||
return 0;
|
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)
|
void remake_file(FS_Path path, FS_Archive archive, u32 size)
|
||||||
{
|
{
|
||||||
Handle handle;
|
Handle handle;
|
||||||
|
|||||||
54
source/search.c
Normal file
54
source/search.c
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
/* C implementation of Boyer-Moore Pattern Searching taken from https://www.geeksforgeeks.org/boyer-moore-algorithm-for-pattern-searching/ */
|
||||||
|
|
||||||
|
# include <limits.h>
|
||||||
|
# include <string.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
|
||||||
|
# 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user