Decode it

From OpenFSG
Jump to: navigation, search


To decode (binary) firmware one can (compile and) use decode_it.c:

/*
 * decode_it.c: decode files encoded by encode_it as shipped in
 * http://www.openfsg.com/download/source-fcsnap-3.1.15.tar.bz2
 *
 * copyright (C) 2006 Paul Bolle <pebolle@tiscali.nl>
 **/

/*
 * 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 2 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 **/

#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>

char *err_a = "ERROR: Argument missing!";
char *err_r = "ERROR: File not found!";
char *err_w = "ERROR: File not writeable!";

FILE *fp;
unsigned int i;
unsigned char *buf, *ptr, csum;
size_t size, sread;

unsigned char *
read_file (size_t *size, char * filename) {
    struct stat stat_buf;
    unsigned char *tmp, *end;

    if (stat (filename, &stat_buf) != 0)
        return NULL;
    *size = (size_t) stat_buf.st_size - 2;

    fp = fopen(filename, "r");
    if (fp == NULL) {
       return NULL;
    }

    for (i=0; i<2; i++) {
        sread = fread (&csum, sizeof(char), 1, fp);
        if (sread != 1) {
            return NULL;
        }
    }

    tmp = (unsigned char *) malloc (*size+1);
    if (tmp == NULL) {
        return tmp;
    }

    sread = fread (tmp, sizeof(char), *size, fp);
    if (sread != *size) {
        free (tmp);
        return NULL;
    }

    end = tmp + *size + 1;
    *end = 0;

    fclose(fp);

    return tmp;
}

int
main (int argc, char *argv[]) {

   if (argc != 2) {
        printf ("%s\n", err_a);
        return 1;
    }

    buf = read_file(&size, argv[1]);
    if (buf == NULL) {
        printf ("%s\n", err_r);
        return 1;
    }

    ptr = buf;

    /* decode */
    for (i=0; i<size; i++) {
        *ptr = *ptr ^ csum;
        csum = csum ^ *ptr;
        ptr++;
    }

    fp = fopen(argv[1], "w");
    if (fp == NULL) {
        printf ("%s\n", err_w);
        return 1;
    }

    fwrite (buf, size, 1, fp);

    fclose(fp);

    return 0;
}
It is the reverse operation of encode_it.
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox