Skip to content

Commit

Permalink
extract functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xor2003 committed Oct 2, 2024
1 parent 47e00b9 commit 397a1b2
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 129 deletions.
130 changes: 2 additions & 128 deletions src/custom/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "regs.h"
#include "custom.h"
#include "custom_hooks.h"
#include "utils.h"

#include "asm.h"

Expand Down Expand Up @@ -499,133 +500,6 @@ void mycopy(db *d, db *s, size_t size, const char *name)
#endif
}

// thanks to paxdiablo http://stackoverflow.com/users/14860/paxdiablo for the
// Function to print a hexadecimal dump of memory contents.
void hexDump(void *addr, int len)
{
int i;
unsigned char buff[17];
unsigned char *pc = (unsigned char *)addr;
(void)buff;
printf("hexDump %p:\n", addr);

if (len == 0) {
printf(" ZERO LENGTH\n");
return;
}
if (len < 0) {
printf(" NEGATIVE LENGTH: %i\n", len);
return;
}

// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0)
printf(" %s\n", buff);

// Output the offset.
printf(" %04x ", i);
}

// Now the hex code for the specific character.
printf(" %02x", pc[i]);

// And store a printable ASCII character for later.
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
buff[i % 16] = '.';
else
buff[i % 16] = pc[i];
buff[(i % 16) + 1] = '\0';
}

// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf(" ");
i++;
}

// And print the final ASCII bit.
printf(" %s\n", buff);
}

// Function to count the number of equal bytes between two memory regions.
size_t countEqual(const db *addr1, const db *addr2, int len)
{
size_t bytes = 0;
while (len && *(addr1++) == *(addr2++)) {
++bytes;
}
return bytes;
}

// Function to compare and print a hexadecimal dump of two memory regions.
void cmpHexDump(void *addr1, void *addr2, int len)
{
int i, j;
unsigned char buff1[17];
unsigned char buff2[17];
unsigned char *pc1 = (unsigned char *)addr1;
unsigned char *pc2 = (unsigned char *)addr2;

printf("cmpHexDump %p %p:\n", pc1, pc2);

if (len == 0) {
printf(" ZERO LENGTH\n");
return;
}
if (len < 0) {
printf(" NEGATIVE LENGTH: %i\n", len);
return;
}

// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
size_t size = i + 16 <= len ? 16 : len - i;

// Compare memory contents and print differences.
if (memcmp(&pc1[i], &pc2[i], size) != 0) {
// Output the offset.
printf(" %04x ", i);

// Print the first memory region.
for (j = 0; j < size; j++) {
// Now the hex code for the specific character.
printf(" %02x", pc1[i + j]);

// And store a printable ASCII character for later.
if ((pc1[i + j] < 0x20) || (pc1[i + j] > 0x7e))
buff1[j] = '.';
else
buff1[j] = pc1[i + j];
}
buff1[j] = '\0';
printf(" %s\n", buff1);

// Print the second memory region.
printf(" ");
for (j = 0; j < size; j++) {
// Now the hex code for the specific character.
printf(" %02x", pc2[i + j]);

// And store a printable ASCII character for later.
if ((pc2[i + j] < 0x20) || (pc2[i + j] > 0x7e))
buff2[j] = '.';
else
buff2[j] = pc2[i + j];
}
buff2[j + 1] = '\0';

// And print the final ASCII bit.
printf(" %s\n", buff2);
}

i += size;
}
}

// Function to dump the stack and shadow memory.
void stackDump(_STATE *_state)
Expand All @@ -644,7 +518,7 @@ void stackDump(_STATE *_state)
shadow_stack.print(0);
}

// Function to log register values and other debug information.
// Function to log dosbox register values
void log_regs_dbx_direct(size_t counter_,
const char *file,
int line,
Expand Down
3 changes: 2 additions & 1 deletion src/custom/meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
libcustom_sources = files([
'custom.cpp',
'segfault.cpp',
'shadowstack.cpp'
'shadowstack.cpp',
'utils.cpp'
])

libcustom = static_library('custom', libcustom_sources,
Expand Down
140 changes: 140 additions & 0 deletions src/custom/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//
// Created by xor on 2.10.24..
//

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sstream>
#include "utils.h"

namespace m2c {
// thanks to paxdiablo http://stackoverflow.com/users/14860/paxdiablo for the
// Function to print a hexadecimal dump of memory contents.
void hexDump(void *addr, int len)
{
int i;
unsigned char buff[17];
unsigned char *pc = (unsigned char *)addr;
(void)buff;
printf("hexDump %p:\n", addr);

if (len == 0) {
printf(" ZERO LENGTH\n");
return;
}
if (len < 0) {
printf(" NEGATIVE LENGTH: %i\n", len);
return;
}

// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0)
printf(" %s\n", buff);

// Output the offset.
printf(" %04x ", i);
}

// Now the hex code for the specific character.
printf(" %02x", pc[i]);

// And store a printable ASCII character for later.
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
buff[i % 16] = '.';
else
buff[i % 16] = pc[i];
buff[(i % 16) + 1] = '\0';
}

// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf(" ");
i++;
}

// And print the final ASCII bit.
printf(" %s\n", buff);
}

// Function to compare and print a hexadecimal dump of two memory regions.
void cmpHexDump(void *addr1, void *addr2, int len)
{
int i, j;
unsigned char buff1[17];
unsigned char buff2[17];
unsigned char *pc1 = (unsigned char *)addr1;
unsigned char *pc2 = (unsigned char *)addr2;

printf("cmpHexDump %p %p:\n", pc1, pc2);

if (len == 0) {
printf(" ZERO LENGTH\n");
return;
}
if (len < 0) {
printf(" NEGATIVE LENGTH: %i\n", len);
return;
}

// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
size_t size = i + 16 <= len ? 16 : len - i;

// Compare memory contents and print differences.
if (memcmp(&pc1[i], &pc2[i], size) != 0) {
// Output the offset.
printf(" %04x ", i);

// Print the first memory region.
for (j = 0; j < size; j++) {
// Now the hex code for the specific character.
printf(" %02x", pc1[i + j]);

// And store a printable ASCII character for later.
if ((pc1[i + j] < 0x20) || (pc1[i + j] > 0x7e))
buff1[j] = '.';
else
buff1[j] = pc1[i + j];
}
buff1[j] = '\0';
printf(" %s\n", buff1);

// Print the second memory region.
printf(" ");
for (j = 0; j < size; j++) {
// Now the hex code for the specific character.
printf(" %02x", pc2[i + j]);

// And store a printable ASCII character for later.
if ((pc2[i + j] < 0x20) || (pc2[i + j] > 0x7e))
buff2[j] = '.';
else
buff2[j] = pc2[i + j];
}
buff2[j + 1] = '\0';

// And print the final ASCII bit.
printf(" %s\n", buff2);
}

i += size;
}
}

} // namespace m2c

// Function to count the number of equal bytes between two memory regions.
size_t countEqual(const db *addr1, const db *addr2, int len)
{
size_t bytes = 0;
while (len && *(addr1++) == *(addr2++)) {
++bytes;
}
return bytes;
}
16 changes: 16 additions & 0 deletions src/custom/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Created by xor on 2.10.24..
//

#ifndef LIBDOSBOX_UTILS_H
#define LIBDOSBOX_UTILS_H

#include "asm.h"

//void hexDump(void *addr, int len);
size_t countEqual(const db *addr1, const db *addr2, int len);
namespace m2c {
void cmpHexDump(void *addr1, void *addr2, int len);
}

#endif // LIBDOSBOX_UTILS_H

0 comments on commit 397a1b2

Please sign in to comment.