Skip to content

Commit 397a1b2

Browse files
committed
extract functions
1 parent 47e00b9 commit 397a1b2

File tree

4 files changed

+160
-129
lines changed

4 files changed

+160
-129
lines changed

src/custom/custom.cpp

Lines changed: 2 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "regs.h"
99
#include "custom.h"
1010
#include "custom_hooks.h"
11+
#include "utils.h"
1112

1213
#include "asm.h"
1314

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

502-
// thanks to paxdiablo http://stackoverflow.com/users/14860/paxdiablo for the
503-
// Function to print a hexadecimal dump of memory contents.
504-
void hexDump(void *addr, int len)
505-
{
506-
int i;
507-
unsigned char buff[17];
508-
unsigned char *pc = (unsigned char *)addr;
509-
(void)buff;
510-
printf("hexDump %p:\n", addr);
511-
512-
if (len == 0) {
513-
printf(" ZERO LENGTH\n");
514-
return;
515-
}
516-
if (len < 0) {
517-
printf(" NEGATIVE LENGTH: %i\n", len);
518-
return;
519-
}
520-
521-
// Process every byte in the data.
522-
for (i = 0; i < len; i++) {
523-
// Multiple of 16 means new line (with line offset).
524-
if ((i % 16) == 0) {
525-
// Just don't print ASCII for the zeroth line.
526-
if (i != 0)
527-
printf(" %s\n", buff);
528-
529-
// Output the offset.
530-
printf(" %04x ", i);
531-
}
532-
533-
// Now the hex code for the specific character.
534-
printf(" %02x", pc[i]);
535-
536-
// And store a printable ASCII character for later.
537-
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
538-
buff[i % 16] = '.';
539-
else
540-
buff[i % 16] = pc[i];
541-
buff[(i % 16) + 1] = '\0';
542-
}
543-
544-
// Pad out last line if not exactly 16 characters.
545-
while ((i % 16) != 0) {
546-
printf(" ");
547-
i++;
548-
}
549-
550-
// And print the final ASCII bit.
551-
printf(" %s\n", buff);
552-
}
553-
554-
// Function to count the number of equal bytes between two memory regions.
555-
size_t countEqual(const db *addr1, const db *addr2, int len)
556-
{
557-
size_t bytes = 0;
558-
while (len && *(addr1++) == *(addr2++)) {
559-
++bytes;
560-
}
561-
return bytes;
562-
}
563-
564-
// Function to compare and print a hexadecimal dump of two memory regions.
565-
void cmpHexDump(void *addr1, void *addr2, int len)
566-
{
567-
int i, j;
568-
unsigned char buff1[17];
569-
unsigned char buff2[17];
570-
unsigned char *pc1 = (unsigned char *)addr1;
571-
unsigned char *pc2 = (unsigned char *)addr2;
572-
573-
printf("cmpHexDump %p %p:\n", pc1, pc2);
574-
575-
if (len == 0) {
576-
printf(" ZERO LENGTH\n");
577-
return;
578-
}
579-
if (len < 0) {
580-
printf(" NEGATIVE LENGTH: %i\n", len);
581-
return;
582-
}
583-
584-
// Process every byte in the data.
585-
for (i = 0; i < len; i++) {
586-
// Multiple of 16 means new line (with line offset).
587-
size_t size = i + 16 <= len ? 16 : len - i;
588-
589-
// Compare memory contents and print differences.
590-
if (memcmp(&pc1[i], &pc2[i], size) != 0) {
591-
// Output the offset.
592-
printf(" %04x ", i);
593-
594-
// Print the first memory region.
595-
for (j = 0; j < size; j++) {
596-
// Now the hex code for the specific character.
597-
printf(" %02x", pc1[i + j]);
598-
599-
// And store a printable ASCII character for later.
600-
if ((pc1[i + j] < 0x20) || (pc1[i + j] > 0x7e))
601-
buff1[j] = '.';
602-
else
603-
buff1[j] = pc1[i + j];
604-
}
605-
buff1[j] = '\0';
606-
printf(" %s\n", buff1);
607-
608-
// Print the second memory region.
609-
printf(" ");
610-
for (j = 0; j < size; j++) {
611-
// Now the hex code for the specific character.
612-
printf(" %02x", pc2[i + j]);
613-
614-
// And store a printable ASCII character for later.
615-
if ((pc2[i + j] < 0x20) || (pc2[i + j] > 0x7e))
616-
buff2[j] = '.';
617-
else
618-
buff2[j] = pc2[i + j];
619-
}
620-
buff2[j + 1] = '\0';
621-
622-
// And print the final ASCII bit.
623-
printf(" %s\n", buff2);
624-
}
625-
626-
i += size;
627-
}
628-
}
629503

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

647-
// Function to log register values and other debug information.
521+
// Function to log dosbox register values
648522
void log_regs_dbx_direct(size_t counter_,
649523
const char *file,
650524
int line,

src/custom/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
libcustom_sources = files([
22
'custom.cpp',
33
'segfault.cpp',
4-
'shadowstack.cpp'
4+
'shadowstack.cpp',
5+
'utils.cpp'
56
])
67

78
libcustom = static_library('custom', libcustom_sources,

src/custom/utils.cpp

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
//
2+
// Created by xor on 2.10.24..
3+
//
4+
5+
#include <stdio.h>
6+
#include <string.h>
7+
#include <unistd.h>
8+
#include <sstream>
9+
#include "utils.h"
10+
11+
namespace m2c {
12+
// thanks to paxdiablo http://stackoverflow.com/users/14860/paxdiablo for the
13+
// Function to print a hexadecimal dump of memory contents.
14+
void hexDump(void *addr, int len)
15+
{
16+
int i;
17+
unsigned char buff[17];
18+
unsigned char *pc = (unsigned char *)addr;
19+
(void)buff;
20+
printf("hexDump %p:\n", addr);
21+
22+
if (len == 0) {
23+
printf(" ZERO LENGTH\n");
24+
return;
25+
}
26+
if (len < 0) {
27+
printf(" NEGATIVE LENGTH: %i\n", len);
28+
return;
29+
}
30+
31+
// Process every byte in the data.
32+
for (i = 0; i < len; i++) {
33+
// Multiple of 16 means new line (with line offset).
34+
if ((i % 16) == 0) {
35+
// Just don't print ASCII for the zeroth line.
36+
if (i != 0)
37+
printf(" %s\n", buff);
38+
39+
// Output the offset.
40+
printf(" %04x ", i);
41+
}
42+
43+
// Now the hex code for the specific character.
44+
printf(" %02x", pc[i]);
45+
46+
// And store a printable ASCII character for later.
47+
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
48+
buff[i % 16] = '.';
49+
else
50+
buff[i % 16] = pc[i];
51+
buff[(i % 16) + 1] = '\0';
52+
}
53+
54+
// Pad out last line if not exactly 16 characters.
55+
while ((i % 16) != 0) {
56+
printf(" ");
57+
i++;
58+
}
59+
60+
// And print the final ASCII bit.
61+
printf(" %s\n", buff);
62+
}
63+
64+
// Function to compare and print a hexadecimal dump of two memory regions.
65+
void cmpHexDump(void *addr1, void *addr2, int len)
66+
{
67+
int i, j;
68+
unsigned char buff1[17];
69+
unsigned char buff2[17];
70+
unsigned char *pc1 = (unsigned char *)addr1;
71+
unsigned char *pc2 = (unsigned char *)addr2;
72+
73+
printf("cmpHexDump %p %p:\n", pc1, pc2);
74+
75+
if (len == 0) {
76+
printf(" ZERO LENGTH\n");
77+
return;
78+
}
79+
if (len < 0) {
80+
printf(" NEGATIVE LENGTH: %i\n", len);
81+
return;
82+
}
83+
84+
// Process every byte in the data.
85+
for (i = 0; i < len; i++) {
86+
// Multiple of 16 means new line (with line offset).
87+
size_t size = i + 16 <= len ? 16 : len - i;
88+
89+
// Compare memory contents and print differences.
90+
if (memcmp(&pc1[i], &pc2[i], size) != 0) {
91+
// Output the offset.
92+
printf(" %04x ", i);
93+
94+
// Print the first memory region.
95+
for (j = 0; j < size; j++) {
96+
// Now the hex code for the specific character.
97+
printf(" %02x", pc1[i + j]);
98+
99+
// And store a printable ASCII character for later.
100+
if ((pc1[i + j] < 0x20) || (pc1[i + j] > 0x7e))
101+
buff1[j] = '.';
102+
else
103+
buff1[j] = pc1[i + j];
104+
}
105+
buff1[j] = '\0';
106+
printf(" %s\n", buff1);
107+
108+
// Print the second memory region.
109+
printf(" ");
110+
for (j = 0; j < size; j++) {
111+
// Now the hex code for the specific character.
112+
printf(" %02x", pc2[i + j]);
113+
114+
// And store a printable ASCII character for later.
115+
if ((pc2[i + j] < 0x20) || (pc2[i + j] > 0x7e))
116+
buff2[j] = '.';
117+
else
118+
buff2[j] = pc2[i + j];
119+
}
120+
buff2[j + 1] = '\0';
121+
122+
// And print the final ASCII bit.
123+
printf(" %s\n", buff2);
124+
}
125+
126+
i += size;
127+
}
128+
}
129+
130+
} // namespace m2c
131+
132+
// Function to count the number of equal bytes between two memory regions.
133+
size_t countEqual(const db *addr1, const db *addr2, int len)
134+
{
135+
size_t bytes = 0;
136+
while (len && *(addr1++) == *(addr2++)) {
137+
++bytes;
138+
}
139+
return bytes;
140+
}

src/custom/utils.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Created by xor on 2.10.24..
3+
//
4+
5+
#ifndef LIBDOSBOX_UTILS_H
6+
#define LIBDOSBOX_UTILS_H
7+
8+
#include "asm.h"
9+
10+
//void hexDump(void *addr, int len);
11+
size_t countEqual(const db *addr1, const db *addr2, int len);
12+
namespace m2c {
13+
void cmpHexDump(void *addr1, void *addr2, int len);
14+
}
15+
16+
#endif // LIBDOSBOX_UTILS_H

0 commit comments

Comments
 (0)