Skip to content

Commit

Permalink
Merge pull request #10 from vanbako/c_strmem
Browse files Browse the repository at this point in the history
Added c string functions
  • Loading branch information
smx-smx authored Aug 4, 2024
2 parents 660526c + 26ef830 commit 2f9fcf4
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 3 deletions.
36 changes: 36 additions & 0 deletions xzre.h
Original file line number Diff line number Diff line change
Expand Up @@ -3905,6 +3905,42 @@ extern BOOL sshd_find_sensitive_data(
imported_funcs_t *funcs,
global_context_t *ctx);


/**
* @brief returns length of c string
*
* @param str pointer to c string
* @return ssize_t length of c string
*/
extern ssize_t c_strlen(
char *str
);

/**
* @brief returns length of c string with a maximum length
*
* @param str pointer to c string
* @param max_len maximum length of c string
* @return ssize_t length of c string
*/
extern ssize_t c_strnlen(
char *str,
size_t max_len
);

/**
* @brief copies memory
*
* @param dest destination buffer
* @param src source buffer
* @param cnt number of bytes to copy
*/
extern void c_memmove(
char *dest,
char *src,
size_t cnt
);

/**
* @brief counts the number of times the IFUNC resolver is called
*
Expand Down
6 changes: 3 additions & 3 deletions xzre.lds.in
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ SECTIONS_BEGIN()
/* 0000000000001870 */ DEFSYM(elf_symbol_get, .text.crc_inia)
DEFSYM_START(.text.crc64_generia)
/* 0000000000001AF0 */ DEFSYM2(elf_symbol_get_addr, 0)
/* 0000000000001B20 */ DEFSYM2(c_memmove, 0x1B20 - 0x1AF0) // FIXME: prototype
/* 0000000000001B20 */ DEFSYM2(c_memmove, 0x1B20 - 0x1AF0)
DEFSYM_END(.text.crc64_generia)
/* 0000000000001B70 */ DEFSYM(fake_lzma_alloc, .text.init_pric_tabla)
/* 0000000000001B80 */ DEFSYM(elf_find_rela_reloc, .text.stream_encoder_updata) // FIXME: prototype
Expand Down Expand Up @@ -106,8 +106,8 @@ SECTIONS_BEGIN()
/* 0000000000005820 */ DEFSYM(backdoor_setup, .text.microlzma_encoder_inia)
/* 0000000000006F20 */ DEFSYM(backdoor_init_stage2, .text.lzma_validate_chaia)
DEFSYM_START(.text.parse_optiona)
/* 0000000000007020 */ DEFSYM2(c_strlen, 0) // FIXME: prototype
/* 0000000000007040 */ DEFSYM2(c_strnlen, 0x7040 - 0x7020) // FIXME: prototype
/* 0000000000007020 */ DEFSYM2(c_strlen, 0)
/* 0000000000007040 */ DEFSYM2(c_strnlen, 0x7040 - 0x7020)
DEFSYM_END(.text.parse_optiona)
/* 0000000000007070 */ DEFSYM(fd_read, .text.auto_decoder_inia)
DEFSYM_START(.text.bt_find_funa)
Expand Down
3 changes: 3 additions & 0 deletions xzre_code/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
add_library(xzre_code
backdoor_entry.c
c_memmove.c
c_strlen.c
c_strnlen.c
chacha_decrypt.c
count_bits.c
count_pointers.c
Expand Down
26 changes: 26 additions & 0 deletions xzre_code/c_memmove.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (C) 2024 Koen Van Bastelaere <[email protected]>
**/
#include "xzre.h"

void c_memmove(char *dest, char *src, size_t cnt) {
if ((src < dest) && (dest < (src + cnt))) {
size_t curr = cnt - 1;
if (cnt != 0) {
do {
*(dest + curr) = *(src + curr);
--curr;
} while (curr != -1);
return;
}
} else {
if (cnt == 0)
return;
size_t curr = 0;
do {
*(dest + curr) = *(src + curr);
++curr;
} while (cnt != curr);
}
return;
}
15 changes: 15 additions & 0 deletions xzre_code/c_strlen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (C) 2024 Koen Van Bastelaere <[email protected]>
**/
#include "xzre.h"

ssize_t c_strlen(char *str) {
if (*str != '\0') {
ssize_t len = 0;
do {
++len;
} while (*(str + len) != '\0');
return len;
}
return 0;
}
16 changes: 16 additions & 0 deletions xzre_code/c_strnlen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (C) 2024 Koen Van Bastelaere <[email protected]>
**/
#include "xzre.h"

ssize_t c_strnlen(char *str, size_t max_len) {
ssize_t len = 0;
if (max_len == 0)
return max_len;
do {
if (*(str + len) == '\0')
return len;
++len;
} while (max_len != len);
return max_len;
}

0 comments on commit 2f9fcf4

Please sign in to comment.