forked from HACKERCHANNEL/benzin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memfile.c
113 lines (101 loc) · 2.95 KB
/
memfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/******************************************************************************
* memfile.c *
* Part of Benzin *
* Handles memory like files. *
* Copyright (C)2009 SquidMan (Alex Marshall) <[email protected]> *
* Copyright (C)2009 megazig (Stephen Simpson) <[email protected]> *
* Copyright (C)2009 Matt_P (Matthew Parlane) *
* Copyright (C)2009 comex *
* Copyright (C)2009 booto *
* All Rights Reserved, HACKERCHANNEL. *
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "memfile.h"
void ReadMemory(void* dst, size_t size, size_t count, MEMORY* src)
{
if((src->mode & 0x1) == 0) {
printf("Invalid mode for reading!\n");
return;
}
u8* data = (u8*)dst;
if(((size * count) + src->position) > src->memorysize) {
printf("Tried to over read.\n");
return;
}
memcpy(data, ((u8*)src->memory) + src->position, size * count);
src->position += size * count;
}
char ReadMemoryChar(MEMORY* src)
{
char ret;
ReadMemory(&ret, 1, 1, src);
return ret;
}
void WriteMemory(void* dst, size_t size, size_t count, MEMORY* src)
{
if((src->mode & 0x2) == 0) {
printf("Invalid mode for writing!\n");
return;
}
u8* data = (u8*)dst;
if(((size * count) + src->position) > src->memorysize) {
printf("Tried to over read.\n");
return;
}
memcpy(((u8*)src->memory) + src->position, data, size * count);
src->position += size * count;
}
void WriteMemoryChar(char inchar, MEMORY* src)
{
WriteMemory(&inchar, 1, 1, src);
}
MEMORY* OpenMemory(void* indata, size_t size, char mode)
{
MEMORY* mem = malloc(sizeof(MEMORY));
if((mode & 0x2) == 0x0)
mem->memory = indata;
else
mem->memory = calloc(size, 1);
if(mem->memory == NULL)
return NULL;
mem->memorysize = size;
mem->position = 0;
mem->mode = mode;
return mem;
}
void* CloseMemory(MEMORY* mem)
{
void* ret = mem->memory;
mem->memory = NULL;
mem->memorysize = 0;
mem->position = 0;
mem->mode = 0;
free(mem);
return ret;
}
void* GetMemory(MEMORY* mem)
{
return mem->memory;
}
void SeekMemory(MEMORY* mem, size_t location, int type)
{
switch(type) {
case SEEK_SET:
mem->position = location;
break;
case SEEK_END:
mem->position = mem->memorysize - location;
break;
}
}
size_t TellMemory(MEMORY* mem)
{
return mem->position;
}
size_t SizeMemory(MEMORY* mem)
{
return mem->memorysize;
}