aboutsummaryrefslogtreecommitdiff
path: root/reloc.c
blob: 1193a1eb0ea964531c2f0060fa7f33c38f3e80f4 (plain) (blame)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "reloc.h"

RelocMatch *reloc_match(char *haystack, const char *needle) {
    char *data = haystack;
    char *pattern = strdup(needle);
    size_t pattern_size = strlen(pattern);
    RelocMatch *match = NULL;

    // Search the needle in the data
    if (!memcmp(data, pattern, pattern_size)) {
        if (!(match = (RelocMatch *)calloc(1, sizeof(RelocMatch)))) {
            reloc_error = RELOC_ENOMEM;
            free(pattern);
            return NULL;
        }
        size_t data_end = strlen(data);
        match->begin = data;
        match->end = match->begin + pattern_size;
        match->length = match->end - match->begin;
        match->post = match->end;
        match->post_length = strlen(match->post);
        match->total_length = data_end;
    }

    reloc_error = RELOC_ESUCCESS;
    free(pattern);
    return match;
}


RelocData *reloc_read(const char *filename) {
    char *data = NULL;
    FILE *fp = NULL;
    size_t size = 0;
    RelocData *result;

    // Open file for reading in binary mode
    if (!(fp = fopen(filename, "rb"))) {
        reloc_error = RELOC_EREAD;
        return NULL;
    }

    // Determine file size
    fseek(fp, 0, SEEK_END);
    size = (size_t)ftell(fp);
    rewind(fp);

    if (!(data = (char *)calloc(size + 1, sizeof(char)))) {
        reloc_error = RELOC_ENOMEM;
        return NULL;
    }

    // Read data into array
    fread(data, sizeof(char), size, fp);
    if (!(result = (RelocData *)calloc(1, sizeof(RelocData)))) {
        reloc_error = RELOC_ENOMEM;
        return NULL;
    }
    fclose(fp);

    result->size = size;
    result->data = data;
    result->path = strdup(filename);
    reloc_error = RELOC_ESUCCESS;
    return result;
}


size_t reloc_write(RelocData *finfo, const char *filename) {
    size_t bytes = 0;
    FILE *fp;

    // Open file for writing in binary mode
    if (!(fp = fopen(filename, "w+b"))) {
        reloc_error = RELOC_EWRITE;
        return 0;
    }

    // Write data
    bytes = fwrite(finfo->data, sizeof(char), finfo->size, fp);
    fclose(fp);

    reloc_error = RELOC_ESUCCESS;
    return bytes;
}


void reloc_deinit_data(RelocData *finfo) {
    if (finfo) {
        if (finfo->path) {
            free(finfo->path);
        }
        if (finfo->data) {
            free(finfo->data);
        }
        free(finfo);
    }
}


void reloc_replace(RelocMatch *match, const char *rstr) {
    size_t rstr_length = strlen(rstr);
    char *data = match->begin;
    size_t i = 0;

    // Overwrite data with replacement string
    while (i < rstr_length) {
        *data = *rstr;
        data++;
        rstr++;
        i++;
    }

    i = 0;
    // Append remaining bytes not part of the original match to the replaced string
    char *post = match->post;
    while (i < match->post_length) {
        *data = *post;
        data++;
        post++;
        i++;
    }

    // Destroy bytes between the end of the original data and the end of the replaced string
    memset(data, NULLBYTE, post - data);
}