File: operationfile.cpp

package info (click to toggle)
concordance 1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,700 kB
  • sloc: cpp: 7,203; sh: 4,621; ansic: 882; python: 707; perl: 171; makefile: 132; xml: 34
file content (387 lines) | stat: -rw-r--r-- 10,718 bytes parent folder | download | duplicates (4)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * vim:tw=80:ai:tabstop=4:softtabstop=4:shiftwidth=4:expandtab
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * (C) Copyright Phil Dibowitz 2010
 */

#include "operationfile.h"

#include <stdlib.h>
#include <string.h>
#include <zip.h>
#include <string>

#include "libconcord.h"
#include "lc_internal.h"
#include "binaryfile.h"
#include "web.h"
#include "remote.h"

static const char *FW_URL =
    "EasyZapper/New/ProcUpgradeFirmware/Upgrade_Receive_Complete.asp";

int find_config_binary(uint8_t *config, uint32_t config_size,
    uint8_t **binary_ptr, uint32_t *binary_size)
{
    int err;

    err = GetTag("/INFORMATION", config, config_size, *binary_ptr);
    if (err == -1)
        return LC_ERROR;

    *binary_ptr += 2;
    *binary_size = config_size - (*binary_ptr - config);

    // Limit tag searches to XML portion
    config_size -= *binary_size;

    string binary_tag_size_s;
    uint8_t *n = 0;
    err = GetTag("BINARYDATASIZE", config, config_size, n, &binary_tag_size_s);
    if (err == -1)
        return LC_ERROR;
    uint32_t binary_tag_size = (uint32_t)atoi(binary_tag_size_s.c_str());

    debug("actual data size %i", *binary_size);
    debug("reported data size %i", binary_tag_size);

    if (*binary_size != binary_tag_size) {
        debug("Config data size mismatch");
        return LC_ERROR;
    }

    string s;
    err = GetTag("CHECKSUM", config, config_size, n, &s);
    if (err != 0)
        return err;
    const uint8_t checksum = atoi(s.c_str());

    // Calculate checksum
    uint32_t u = *binary_size;
    uint8_t calc_checksum = 0x69;
    uint8_t *pc = *binary_ptr;
    while (u--)
        calc_checksum ^= *pc++;

    debug("reported checksum %i %02x", checksum, checksum);
    debug("actual checksum %i %02x", calc_checksum, calc_checksum);

    if (calc_checksum != checksum) {
        debug("Config checksum mismatch");
        return LC_ERROR;
    }

    return 0;
}

int OperationFile::ReadZipFile(char *file_name)
{
    struct zip *zip = zip_open(file_name, 0, NULL);
    if (!zip) {
        return LC_ERROR;
    }

    struct zip_stat stat;
    zip_uint64_t num_entries = zip_get_num_entries(zip, 0);
    for (zip_uint64_t i = 0; i < num_entries; i++) {
        zip_stat_index(zip, i, 0, &stat);
        struct zip_file *file = zip_fopen(zip, stat.name, 0);
        if ((strcmp(stat.name, "Data.xml") == 0) ||
            (strcmp(stat.name, "Description.xml") == 0)) {
            debug("Internal file is %s", stat.name);
            debug("Size is %lu", stat.size);
            xml_size = stat.size;
            xml = new uint8_t[xml_size];
            zip_fread(file, xml, xml_size);
            debug("xml is %p, and xmlsize is %d", xml, xml_size);
        } else {
            data_size = stat.size;
            data = new uint8_t[data_size];
            data_alloc = true;
            zip_fread(file, data, data_size);
            debug("data_size is %d", data_size);
        }
        zip_fclose(file);
    }
    zip_close(zip);
    return 0;
}

int OperationFile::ReadPlainFile(char *file_name)
{
    /* Read file */
    binaryinfile file;

    if (file.open(file_name) != 0) {
        debug("Failed to open %s", file_name);
        return LC_ERROR_OS_FILE;
    }

    debug("file opened");
    uint32_t size = file.getlength();
    uint8_t *out = new uint8_t[size];
    file.read(out, size);

    if (file.close() != 0) {
        debug("Failed to close %s\n", file_name);
        return LC_ERROR_OS_FILE;
    }

    debug("finding binary bit...");
    /* Find the config part */
    find_config_binary(out, size, &data, &data_size);

    xml = out;
    xml_size = size - data_size;

    return 0;
}

OperationFile::OperationFile()
{
    data_size = xml_size = 0;
    data = xml = NULL;
    data_alloc = false;
}

OperationFile::~OperationFile()
{
    /* 
     * In certain places, the data pointer is used to point to an area
     * inside of the xml memory.  In those cases, we don't want to delete
     * it.  Use the data_alloc variable to keep track of when we've actually
     * allocated unique memory to it, and in those cases, delete it.
     */
    if (data && data_alloc)
        delete data;
    if (xml)
        delete xml;
}

int _convert_to_binary(string hex, uint8_t *&ptr)
{
    size_t size = hex.length();
    for (size_t i = 0; i < size; i += 2) {
        char tmp[6];
        sprintf(tmp, "0x%s ", hex.substr(i, 2).c_str());
        ptr[0] = (uint8_t)strtoul(tmp, NULL, 16);
        ptr++;
    }
    return 0;
}

int OperationFile::_ExtractFirmwareBinary()
{
    debug("extracting firmware binary");
    uint32_t o_size = FIRMWARE_MAX_SIZE;
    data = new uint8_t[o_size];
    data_alloc = true;
    uint8_t *o = data;

    uint8_t *x = xml;
    uint8_t *x_new;
    uint32_t x_size = xml_size;

    /*
     * Some remotes (e.g., Arch 7) contain multiple phases in their
     * firmware update files.  In that case, extract only the first phase.
     */
    if (GetTag("PHASE", x, x_size, x_new) == 0) {
        debug("multi-phase firmware found, extracting 1st phase");
        x_size = x_size - (x_new - x);
        x = x_new;
        uint8_t *phase_end;
        GetTag("/PHASE", x, x_size, phase_end);
        x_size = phase_end - x;
    }

    string hex;
    while (GetTag("DATA", x, x_size, x_new, &hex) == 0) {
        uint32_t hex_size = hex.length() / 2;
        if (hex_size > o_size) {
            return LC_ERROR;
        }

        _convert_to_binary(hex, o);

        x_size = x_size - (x_new - x);
        x = x_new;
        o_size -= hex_size;
    }

    data_size = o - data;
    debug("acquired firmware binary");

    return 0;
}

int OperationFile::ReadAndParseOpFile(char *file_name, int *type)
{
    debug("In RAPOF");
    int err;

    if (file_name == NULL) {
        debug("Empty file_name");
        return LC_ERROR_OS_FILE;
    }

    bool is_zip = false;
    if (!ReadZipFile(file_name)) {
        debug("Is zip");
        is_zip = true;
    }

    if (!is_zip) {
        if ((err = ReadPlainFile(file_name)))
            return LC_ERROR_READ;
    }

    /* Determine the file type */
    uint8_t *start_info_ptr, *end_info_ptr;
    bool has_binary = false;
    if (data && data_size) {
        debug("Has binary!");
        has_binary = true;
    }

    /*
     * Validate this is a remotely sane XML file
     */
    debug("determining type...");
    if (is_zip) {
        start_info_ptr = xml;
        end_info_ptr = xml + xml_size;
    } else {
        err = GetTag("INFORMATION", xml, xml_size, start_info_ptr);
        debug("err is %d", err);
        if (err == -1) {
            debug("Unable to find INFORMATION tag");
            return LC_ERROR;
        }
        err = GetTag("/INFORMATION", xml, xml_size, end_info_ptr);
        if (err == -1) {
            debug("Unable to find /INFORMATION tag");
            return LC_ERROR;
        }
    }
    debug("start/end pointers populated");

    /*
     * Search for tag only in "connectivity test" files
     */
    bool found_get_zaps_only = false;
    uint8_t *tmp_data = xml;
    uint32_t tmp_size = xml_size;
    while (1) {
        uint8_t *tag_ptr;
        string tag_s;
        err = GetTag("KEY", tmp_data, tmp_size, tag_ptr, &tag_s);
        if (err == -1) {
            debug("not a connectivity test file");
            break;
        }
        if (!stricmp(tag_s.c_str(), "GETZAPSONLY")) {
            found_get_zaps_only = true;
            break;
        }
        tmp_data = tag_ptr + tag_s.length();
        tmp_size = end_info_ptr - tmp_data;
    }

    /*
     * Search for tag only in "firmware" files
     */
    bool found_firmware = false;
    tmp_data = xml;
    tmp_size = xml_size;
    while (1) {
        uint8_t *tag_ptr;
        string tag_s;
        /*
         * Firmware files will have either a TYPE or a PATH tag with
         * either a URL or a Firmware_Main string in them.
         *
         * Unless we created them, which is what the DATA check
         * is for.
         */
        err = GetTag("TYPE", tmp_data, tmp_size, tag_ptr, &tag_s);
        if (err == -1) {
            err = GetTag("PATH", tmp_data, tmp_size, tag_ptr, &tag_s);
            if (err == -1) {
                debug("not a firmware file");
                break;
            }
        }
        if (!stricmp(tag_s.c_str(), "Firmware_Main")) {
            debug("IS a firmware file");
            found_firmware = true;
            break;
        } else if (!stricmp(tag_s.c_str(), FW_URL)) {
            debug("IS a firmware file");
            found_firmware = true;
            break;
        }
        tmp_data = tag_ptr + tag_s.length();
        tmp_size = end_info_ptr - tmp_data;
    }

    if (found_firmware)
        _ExtractFirmwareBinary();

    /*
     * Search for tag only in "IR learning files.
     */
    uint8_t *tag_ptr;
    err = GetTag("CHECKKEYS", xml, xml_size, tag_ptr);
    bool found_learn_ir = (err != -1);

    debug("zaps: %d, binary: %d, firmware: %d, ir: %d",
        found_get_zaps_only, has_binary, found_firmware,
        found_learn_ir);

    /*
     * Check tag search results for consistency, and deduce the file type
     */
    if (found_get_zaps_only && !has_binary && !found_firmware &&
        !found_learn_ir) {
        debug("returning connect file");
        *type = LC_FILE_TYPE_CONNECTIVITY;
        return 0;
    }
    if (!found_get_zaps_only && has_binary && data_size >= 16
        && !found_firmware && !found_learn_ir) {
        debug("returning conf file");
        *type = LC_FILE_TYPE_CONFIGURATION;
        return 0;
    }
    if (!found_get_zaps_only && found_firmware && !found_learn_ir) {
        debug("returning firmware file");
        *type = LC_FILE_TYPE_FIRMWARE;
        return 0;
    }
    if (!found_get_zaps_only && !found_firmware && found_learn_ir) {
        debug("returning IR file");
        *type = LC_FILE_TYPE_LEARN_IR;
        return 0;
    }
    debug("returning error");

    /*
     * Findings didn't match a single file type; indicate a problem
     */
    return LC_ERROR;
}