summaryrefslogtreecommitdiff
path: root/src/tools/qtlibspatcher/binpatch.cpp
blob: 41f82ea37f1fea8b5f4fb3f506685f319cbc58fc (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#include <cstdio>
#include <cstring>
#include <locale>

#include <qglobal.h>

#include "binpatch.h"

#ifdef Q_OS_WIN
#	define strcasecmp _stricmp
#	define strncasecmp _strnicmp
#endif

// returns positive value if it finds a null termination inside the buffer
long BinPatch::getBufferStringLength(char *data, char *end)
{
    long size = 0;
    while (data < end) {
        if (*data == '\0')
            return size;
        ++data;
        ++size;
    }

    return -1;
}

// returns true if data ends with one of the tokens. (Sep. with ;)
bool BinPatch::endsWithTokens(const char *data)
{
    if(strlen(endTokens) > 0) {
        char endstmp[1024];
        ulong tlen = ulong(strlen(data));

        if(strlen(endTokens) >= sizeof(endstmp))
            return false;

        strcpy(endstmp, endTokens);

        char *token = strtok(endstmp, ";");

        while(token != NULL) {
            // check if it ends with the token
            if ((tlen >= strlen(token))
                && (strcasecmp((data+tlen)-strlen(token), token) == 0))
                return true;
            token = strtok(NULL, ";");
        }
    } else {
        return true; //true if no tokens
    }

    return false; //no matching tokens
}

bool BinPatch::patchHelper(char *inbuffer, const char *oldstr, const char *newstr, size_t len, long *rw)
{
    char nc1 = *oldstr;
    char nc2 = 0;
    char *inend = inbuffer + len;
    size_t oldlen = strlen(oldstr);
    size_t newlen = strlen(newstr);
    char *instart = inbuffer;
    *rw = 0;
    bool write = true;

    isupper(nc1) ? nc2 = tolower(nc1) : nc2 = toupper(nc1);

    while(inbuffer < inend) {
        if ((*inbuffer == nc1) || (*inbuffer == nc2)) {
            if (inbuffer > (inend-oldlen) || inbuffer > (inend-newlen)) {
                *rw = (long)(inend-inbuffer); //rewind, not enough to make a compare
                break;
            }

            if (strncasecmp(inbuffer, oldstr, oldlen) == 0) {
                if (useLength && (instart == inbuffer)) {
                    *rw = (long)(len+1); //we don't have access to the length byte, rewind all + 1!
                    write = false;
                    break;
                }

                long oldsize = -1;
                if (useLength) { //VC60
                    oldsize = (unsigned char)(*(inbuffer-1));

                    // vc60 pdb files sometimes uses 0A, then it should be null terminated
                    if (oldsize < (long)oldlen) {
                        if (oldsize != 0x0A) { //strange case... skip
                            inbuffer+=oldlen;
                            continue;
                        }

                        oldsize = getBufferStringLength(inbuffer, inend);

                        if (oldsize < 0) {
                            *rw = (long)(inend-inbuffer); //rewind, entire string not in buffer
                            break;
                        }
                    }

                    if (inbuffer > (inend-oldsize)) {
                        *rw = (long)(inend-inbuffer); //rewind, entire string not in buffer
                        break;
                    }
                } else { //VC7x
                    oldsize = getBufferStringLength(inbuffer, inend);
                    if (oldsize < 0) {
                        *rw = (long)(inend-inbuffer); //rewind, entire string not in buffer
                        break;
                    }
                }

                char oldPath[1024];
                if (oldsize > (long)sizeof(oldPath)) {
                    //at least don't crash
                    inbuffer+=oldsize;
                    continue;
                }
                memset(oldPath, '\0', sizeof(oldPath));
                strncpy(oldPath, newstr, newlen);

                if (insertReplace)
                    strncat(oldPath, inbuffer+oldlen, oldsize-oldlen);

                // just replace if it ends with a token in endTokens
                if (endsWithTokens(oldPath)) {
                    if (oldsize < (long)strlen(oldPath))
                        oldsize = (long)strlen(oldPath);

                    memcpy(inbuffer, oldPath, oldsize);
                }

                inbuffer+=oldsize;
                continue;
            }
        }
        ++inbuffer;
    }

    return write;
}

bool BinPatch::patch(const char *oldstr, const char *newstr)
{
    size_t oldlen = strlen(oldstr);
    size_t newlen = strlen(newstr);

    if ((!fileName || strlen(fileName) < 1)
        || (!oldstr || oldlen < 1)
        || (!newstr || newlen < 1))
        return false;

    FILE *input;

    if (!(input = fopen(fileName, "r+b")))
    {
        fprintf(stderr, "Warning: Could not open file %s\n", fileName);
        return false;
    }

    char data[60000];
    long rw = 0;
    long offset = 0;

    size_t len;
    len = fread(data, sizeof(char), sizeof(data), input);

    do {
        if (patchHelper(data, oldstr, newstr, len, &rw)) {
            fseek(input, offset, SEEK_SET); //overwrite
            fwrite(data, sizeof(char), len, input);
        }

        offset += (long)((-rw) + len);
        if (fseek(input, offset, SEEK_SET) != 0)
            break;
        len = fread(data, sizeof(char), sizeof(data), input);
    } while(!(feof(input) && (len <= oldlen || len <= newlen)));

    fclose(input);

    return true;
}