From 679fdd4351e44a067a2b3d80f8d5f4447a58d5a4 Mon Sep 17 00:00:00 2001 From: mintsuki Date: Tue, 30 May 2023 12:23:31 +0200 Subject: [PATCH] misc: Use C instead of asm for mem*() family of functions --- common/lib/mem.asm_aarch64 | 66 --------------------------------- common/lib/mem.asm_x86_64 | 55 --------------------------- common/lib/mem.s2.asm_ia32 | 76 -------------------------------------- common/lib/memory.s2.c | 53 ++++++++++++++++++++++++++ decompressor/mem.asm | 75 ------------------------------------- decompressor/memory.c | 53 ++++++++++++++++++++++++++ 6 files changed, 106 insertions(+), 272 deletions(-) delete mode 100644 common/lib/mem.asm_aarch64 delete mode 100644 common/lib/mem.asm_x86_64 create mode 100644 common/lib/memory.s2.c delete mode 100644 decompressor/mem.asm create mode 100644 decompressor/memory.c diff --git a/common/lib/mem.asm_aarch64 b/common/lib/mem.asm_aarch64 deleted file mode 100644 index fa632984..00000000 --- a/common/lib/mem.asm_aarch64 +++ /dev/null @@ -1,66 +0,0 @@ -.section .text - -.global memcpy -memcpy: - mov x3, x0 -0: - cbz x2, 1f - ldrb w4, [x1], #1 - strb w4, [x0], #1 - sub x2, x2, #1 - b 0b -1: - mov x0, x3 - ret - -.global memset -memset: - mov x3, x0 -0: - cbz x2, 1f - strb w1, [x0], #1 - sub x2, x2, #1 - b 0b -1: - mov x0, x3 - ret - -.global memmove -memmove: - mov x3, x0 - mov x5, x2 - - cmp x0, x1 - b.gt 1f -0: - cbz x2, 2f - ldrb w4, [x1], #1 - strb w4, [x0], #1 - sub x2, x2, #1 - b 0b -1: - sub x5, x5, #1 - cbz x2, 2f - ldrb w4, [x1, x5] - strb w4, [x0, x5] - sub x2, x2, #1 - b 1b -2: - mov x0, x3 - ret - -.global memcmp -memcmp: - mov x3, xzr -0: - cbz x2, 1f - ldrb w3, [x0], #1 - ldrb w4, [x1], #1 - sub w3, w3, w4 - cbnz w3, 1f - sub x2, x2, #1 - b 0b -1: - sxtw x0, w3 - mov x0, x3 - ret diff --git a/common/lib/mem.asm_x86_64 b/common/lib/mem.asm_x86_64 deleted file mode 100644 index 4463c197..00000000 --- a/common/lib/mem.asm_x86_64 +++ /dev/null @@ -1,55 +0,0 @@ -section .text - -global memcpy -memcpy: - mov rcx, rdx - mov rax, rdi - rep movsb - ret - -global memset -memset: - push rdi - mov rax, rsi - mov rcx, rdx - rep stosb - pop rax - ret - -global memmove -memmove: - mov rcx, rdx - mov rax, rdi - - cmp rdi, rsi - ja .copy_backwards - - rep movsb - jmp .done - - .copy_backwards: - lea rdi, [rdi+rcx-1] - lea rsi, [rsi+rcx-1] - std - rep movsb - cld - - .done: - ret - -global memcmp -memcmp: - mov rcx, rdx - repe cmpsb - je .equal - - mov al, byte [rdi-1] - sub al, byte [rsi-1] - movsx rax, al - jmp .done - - .equal: - xor eax, eax - - .done: - ret diff --git a/common/lib/mem.s2.asm_ia32 b/common/lib/mem.s2.asm_ia32 index c4452247..3c960dff 100644 --- a/common/lib/mem.s2.asm_ia32 +++ b/common/lib/mem.s2.asm_ia32 @@ -1,79 +1,3 @@ -section .text - -global memcpy -memcpy: - push esi - push edi - mov eax, dword [esp+12] - mov edi, eax - mov esi, dword [esp+16] - mov ecx, dword [esp+20] - rep movsb - pop edi - pop esi - ret - -global memset -memset: - push edi - mov edx, dword [esp+8] - mov edi, edx - mov eax, dword [esp+12] - mov ecx, dword [esp+16] - rep stosb - mov eax, edx - pop edi - ret - -global memmove -memmove: - push esi - push edi - mov eax, dword [esp+12] - mov edi, eax - mov esi, dword [esp+16] - mov ecx, dword [esp+20] - - cmp edi, esi - ja .copy_backwards - - rep movsb - jmp .done - - .copy_backwards: - lea edi, [edi+ecx-1] - lea esi, [esi+ecx-1] - std - rep movsb - cld - - .done: - pop edi - pop esi - ret - -global memcmp -memcmp: - push esi - push edi - mov edi, dword [esp+12] - mov esi, dword [esp+16] - mov ecx, dword [esp+20] - repe cmpsb - je .equal - mov al, byte [edi-1] - sub al, byte [esi-1] - movsx eax, al - jmp .done - - .equal: - xor eax, eax - - .done: - pop edi - pop esi - ret - global memcpy32to64 memcpy32to64: push ebp diff --git a/common/lib/memory.s2.c b/common/lib/memory.s2.c new file mode 100644 index 00000000..6d530115 --- /dev/null +++ b/common/lib/memory.s2.c @@ -0,0 +1,53 @@ +#include +#include + +void *memcpy(void *dest, const void *src, size_t n) { + uint8_t *pdest = (uint8_t *)dest; + const uint8_t *psrc = (const uint8_t *)src; + + for (size_t i = 0; i < n; i++) { + pdest[i] = psrc[i]; + } + + return dest; +} + +void *memset(void *s, int c, size_t n) { + uint8_t *p = (uint8_t *)s; + + for (size_t i = 0; i < n; i++) { + p[i] = (uint8_t)c; + } + + return s; +} + +void *memmove(void *dest, const void *src, size_t n) { + uint8_t *pdest = (uint8_t *)dest; + const uint8_t *psrc = (const uint8_t *)src; + + if (src > dest) { + for (size_t i = 0; i < n; i++) { + pdest[i] = psrc[i]; + } + } else if (src < dest) { + for (size_t i = n; i > 0; i--) { + pdest[i-1] = psrc[i-1]; + } + } + + return dest; +} + +int memcmp(const void *s1, const void *s2, size_t n) { + const uint8_t *p1 = (const uint8_t *)s1; + const uint8_t *p2 = (const uint8_t *)s2; + + for (size_t i = 0; i < n; i++) { + if (p1[i] != p2[i]) { + return p1[i] < p2[i] ? -1 : 1; + } + } + + return 0; +} diff --git a/decompressor/mem.asm b/decompressor/mem.asm deleted file mode 100644 index d3a7d9e7..00000000 --- a/decompressor/mem.asm +++ /dev/null @@ -1,75 +0,0 @@ -section .text - -global memcpy -memcpy: - push esi - push edi - mov eax, dword [esp+12] - mov edi, eax - mov esi, dword [esp+16] - mov ecx, dword [esp+20] - rep movsb - pop edi - pop esi - ret - -global memset -memset: - push edi - mov edx, dword [esp+8] - mov edi, edx - mov eax, dword [esp+12] - mov ecx, dword [esp+16] - rep stosb - mov eax, edx - pop edi - ret - -global memmove -memmove: - push esi - push edi - mov eax, dword [esp+12] - mov edi, eax - mov esi, dword [esp+16] - mov ecx, dword [esp+20] - - cmp edi, esi - ja .copy_backwards - - rep movsb - jmp .done - - .copy_backwards: - lea edi, [edi+ecx-1] - lea esi, [esi+ecx-1] - std - rep movsb - cld - - .done: - pop edi - pop esi - ret - -global memcmp -memcmp: - push esi - push edi - mov edi, dword [esp+12] - mov esi, dword [esp+16] - mov ecx, dword [esp+20] - repe cmpsb - je .equal - mov al, byte [edi-1] - sub al, byte [esi-1] - movsx eax, al - jmp .done - - .equal: - xor eax, eax - - .done: - pop edi - pop esi - ret diff --git a/decompressor/memory.c b/decompressor/memory.c new file mode 100644 index 00000000..6d530115 --- /dev/null +++ b/decompressor/memory.c @@ -0,0 +1,53 @@ +#include +#include + +void *memcpy(void *dest, const void *src, size_t n) { + uint8_t *pdest = (uint8_t *)dest; + const uint8_t *psrc = (const uint8_t *)src; + + for (size_t i = 0; i < n; i++) { + pdest[i] = psrc[i]; + } + + return dest; +} + +void *memset(void *s, int c, size_t n) { + uint8_t *p = (uint8_t *)s; + + for (size_t i = 0; i < n; i++) { + p[i] = (uint8_t)c; + } + + return s; +} + +void *memmove(void *dest, const void *src, size_t n) { + uint8_t *pdest = (uint8_t *)dest; + const uint8_t *psrc = (const uint8_t *)src; + + if (src > dest) { + for (size_t i = 0; i < n; i++) { + pdest[i] = psrc[i]; + } + } else if (src < dest) { + for (size_t i = n; i > 0; i--) { + pdest[i-1] = psrc[i-1]; + } + } + + return dest; +} + +int memcmp(const void *s1, const void *s2, size_t n) { + const uint8_t *p1 = (const uint8_t *)s1; + const uint8_t *p2 = (const uint8_t *)s2; + + for (size_t i = 0; i < n; i++) { + if (p1[i] != p2[i]) { + return p1[i] < p2[i] ? -1 : 1; + } + } + + return 0; +}