diff options
| author | Nicolas Pennequin <nicolas.pennequin@free.fr> | 2008-07-11 16:51:25 +0000 |
|---|---|---|
| committer | Nicolas Pennequin <nicolas.pennequin@free.fr> | 2008-07-11 16:51:25 +0000 |
| commit | ca5bb76d2b8f65aa97e50b633f828c1deb241526 (patch) | |
| tree | 453a1b2de3a0dc0d0b2f7080d10d033bf8fbcdf1 /utils/zenutils/source/shared | |
| parent | 141774be48940d56e3ad4dbf451d245b61d4f8b2 (diff) | |
| download | rockbox-ca5bb76d2b8f65aa97e50b633f828c1deb241526.zip rockbox-ca5bb76d2b8f65aa97e50b633f828c1deb241526.tar.gz rockbox-ca5bb76d2b8f65aa97e50b633f828c1deb241526.tar.bz2 rockbox-ca5bb76d2b8f65aa97e50b633f828c1deb241526.tar.xz | |
Delete the svn:executable property and set svn:eol-style to native for all those text files.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18012 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/zenutils/source/shared')
16 files changed, 1852 insertions, 1852 deletions
diff --git a/utils/zenutils/source/shared/CMakeLists.txt b/utils/zenutils/source/shared/CMakeLists.txt index 2e42dbb..751257f 100755..100644 --- a/utils/zenutils/source/shared/CMakeLists.txt +++ b/utils/zenutils/source/shared/CMakeLists.txt @@ -1,16 +1,16 @@ -PROJECT(shared)
-
-# source files for shared
-SET(shared_srcs
- cenc.cpp
- crypt.cpp
- file.cpp
- firmware.cpp
- pe.cpp
- updater.cpp
- utils.cpp
-)
-
-ADD_LIBRARY(shared ${shared_srcs})
-TARGET_LINK_LIBRARIES(shared pelib)
-TARGET_LINK_LIBRARIES(shared zlib)
+PROJECT(shared) + +# source files for shared +SET(shared_srcs + cenc.cpp + crypt.cpp + file.cpp + firmware.cpp + pe.cpp + updater.cpp + utils.cpp +) + +ADD_LIBRARY(shared ${shared_srcs}) +TARGET_LINK_LIBRARIES(shared pelib) +TARGET_LINK_LIBRARIES(shared zlib) diff --git a/utils/zenutils/source/shared/cenc.cpp b/utils/zenutils/source/shared/cenc.cpp index 932bee4..929a59b 100755..100644 --- a/utils/zenutils/source/shared/cenc.cpp +++ b/utils/zenutils/source/shared/cenc.cpp @@ -1,333 +1,333 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include "cenc.h"
-#include <firmware.h>
-#include <stdexcept>
-
-
-namespace {
-const byte CODE_MASK = 0xC0;
-const byte ARGS_MASK = 0x3F;
-
-const byte REPEAT_CODE = 0x00;
-const byte BLOCK_CODE = 0x40;
-const byte LONG_RUN_CODE = 0x80;
-const byte SHORT_RUN_CODE = 0xC0;
-
-const byte BLOCK_ARGS = 0x1F;
-const byte BLOCK_MODE = 0x20;
-
-
-void decode_run(byte* dst, word len, byte val,
- int& dstidx)
-{
- memset(dst + dstidx, val, len);
- dstidx += len;
-}
-
-void decode_pattern(byte* src, byte* dst,
- word len, int& srcidx, int& dstidx,
- bool bdecode, int npasses)
-{
- for (int i = 0; i < npasses; i++)
- {
- if (bdecode)
- {
- for (int j = 0; j < len; j++)
- {
- word c, d;
- c = src[srcidx + j];
- d = (c >> 5) & 7;
- c = (c << 3) & 0xF8;
- src[srcidx + j] = static_cast<byte>(c | d);
- }
- bdecode = false;
- }
- memcpy(dst + dstidx, src + srcidx, len);
- dstidx += len;
- }
- srcidx += len;
-}
-}; //namespace
-
-int zen::cenc_decode(byte* src, int srclen, byte* dst, int dstlen)
-{
- if (!src || !srclen || !dst || !dstlen)
- {
- throw std::invalid_argument("Invalid argument(s).");
- }
-
- int i = 0, j = 0;
- do
- {
- word c, d, e;
- c = src[i++];
- switch (c & CODE_MASK)
- {
- case REPEAT_CODE: // 2 bytes
- d = src[i++];
- d = d + 2;
-
- e = (c & ARGS_MASK) + 2;
-
- decode_pattern(src, dst, e, i, j, false, d);
- break;
-
- case BLOCK_CODE: // 1/2/3 bytes
- d = c & BLOCK_ARGS;
- if (!(c & BLOCK_MODE))
- {
- e = src[i++];
- e = (d << 8) + (e + 0x21);
-
- d = static_cast<word>(i ^ j);
- }
- else
- {
- e = d + 1;
-
- d = static_cast<word>(i ^ j);
- }
- if (d & 1)
- {
- i++;
- }
-
- decode_pattern(src, dst, e, i, j, true, 1);
- break;
-
- case LONG_RUN_CODE: // 3 bytes
- d = src[i++];
- e = ((c & ARGS_MASK) << 8) + (d + 0x42);
-
- d = src[i++];
- d = ((d & 7) << 5) | ((d >> 3) & 0x1F);
-
- decode_run(dst, e, static_cast<byte>(d), j);
- break;
-
- case SHORT_RUN_CODE: // 2 bytes
- d = src[i++];
- d = ((d & 3) << 6) | ((d >> 2) & 0x3F);
-
- e = (c & ARGS_MASK) + 2;
-
- decode_run(dst, e, static_cast<byte>(d), j);
- break;
- };
- } while (i < srclen && j < dstlen);
-
- return j;
-}
-
-namespace {
-int encode_run(byte* dst, int& dstidx, byte val, int len, int dstlen)
-{
- if (len < 2)
- throw std::invalid_argument("Length is too small.");
-
- int ret = 0;
- if (len <= 0x41)
- {
- if ((dstidx + 2) > dstlen)
- throw std::runtime_error("Not enough space to store run.");
-
- dst[dstidx++] = SHORT_RUN_CODE | (((len - 2) & ARGS_MASK));
- dst[dstidx++] = ((val >> 6) & 3) | ((val & 0x3F) << 2);
-
- ret = 2;
- }
- else if (len <= 0x4041)
- {
- if ((dstidx + 3) > dstlen)
- throw std::runtime_error("Not enough space to store run.");
-
- byte b1 = (len - 0x42) >> 8;
- byte b2 = (len - 0x42) & 0xFF;
-
- dst[dstidx++] = LONG_RUN_CODE | ((b1 & ARGS_MASK));
- dst[dstidx++] = b2;
- dst[dstidx++] = ((val >> 5) & 7) | ((val & 0x1F) << 3);
-
- ret = 3;
- }
- else
- {
- int long_count = len / 0x4041;
- int short_len = len % 0x4041;
- bool toosmall = short_len == 1;
-
- int run_len = 0x4041;
- for (int i = 0; i < long_count; i++)
- {
- if (toosmall && (i == (long_count-1)))
- {
- run_len--;
- toosmall = false;
- }
- int tmp = encode_run(dst, dstidx, val, run_len, dstlen);
- if (!tmp) return 0;
- ret += tmp;
- len -= run_len;
- }
-
- if (len)
- {
- int short_count = len / 0x41;
- int short_rest = short_count ? (len % 0x41) : 0;
- toosmall = short_rest == 1;
-
- run_len = 0x41;
- for (int i = 0; i < short_count; i++)
- {
- if (toosmall && (i == (short_count-1)))
- {
- run_len--;
- toosmall = false;
- }
- int tmp = encode_run(dst, dstidx, val, run_len, dstlen);
- if (!tmp) return 0;
- ret += tmp;
- len -= run_len;
- }
- int tmp = encode_run(dst, dstidx, val, len, dstlen);
- if (!tmp) return 0;
- ret += tmp;
- len -= len;
- }
- }
-
- return ret;
-}
-
-int encode_block(byte* dst, int& dstidx, byte* src, int& srcidx, int len,
- int dstlen)
-{
- if (len < 1)
- throw std::invalid_argument("Length is too small.");
-
- int startidx = dstidx;
- if (len < 0x21)
- {
- if ((dstidx + 2 + len) > dstlen)
- throw std::runtime_error("Not enough space to store block.");
-
- dst[dstidx++] = BLOCK_CODE | BLOCK_MODE | ((len - 1) & BLOCK_ARGS);
- if ((dstidx ^ srcidx) & 1)
- dst[dstidx++] = 0;
-
- for (int i = 0; i < len; i++)
- {
- byte c = src[srcidx++];
- byte d = (c & 7) << 5;
- c = (c & 0xF8) >> 3;
- dst[dstidx++] = c | d;
- }
- }
- else if (len < 0x2021)
- {
- if ((dstidx + 3 + len) > dstlen)
- throw std::runtime_error("Not enough space to store block.");
-
- dst[dstidx++] = BLOCK_CODE | (((len - 0x21) >> 8) & BLOCK_ARGS);
- dst[dstidx++] = (len - 0x21) & 0xFF;
- if ((dstidx ^ srcidx) & 1)
- dst[dstidx++] = 0;
-
- for (int i = 0; i < len; i++)
- {
- byte c = src[srcidx++];
- byte d = (c & 7) << 5;
- c = (c & 0xF8) >> 3;
- dst[dstidx++] = c | d;
- }
- }
- else
- {
- int longblocks = len / 0x2020;
- int rest = len % 0x2020;
- for (int i = 0; i < longblocks; i++)
- {
- int tmp = encode_block(dst, dstidx, src, srcidx, 0x2020, dstlen);
- if (!tmp) return 0;
- }
- if (rest)
- {
- int shortblocks = rest / 0x20;
- for (int i = 0; i < shortblocks; i++)
- {
- int tmp = encode_block(dst, dstidx, src, srcidx, 0x20, dstlen);
- if (!tmp) return 0;
- }
- rest = rest % 0x20;
- int tmp = encode_block(dst, dstidx, src, srcidx, rest, dstlen);
- if (!tmp) return 0;
- }
- }
-
- return (dstidx - startidx);
-}
-}; //namespace
-
-int zen::cenc_encode(byte* src, int srclen, byte* dst, int dstlen)
-{
- if (!src || !srclen || !dst || !dstlen)
- {
- throw std::invalid_argument("Invalid argument(s).");
- }
-
- int i = 0, j = 0, k = 0;
- word c, d, e;
- int runlen = 0;
- while (i < srclen && j < dstlen)
- {
- k = i;
- c = src[i++];
- runlen = 1;
- while (i < srclen && src[i] == c)
- {
- runlen++;
- i++;
- }
- if (runlen >= 2)
- {
- if (!encode_run(dst, j, c, runlen, dstlen))
- return 0;
- }
- else
- {
- runlen = 0;
- i = k;
- while (i < (srclen - 1) && (src[i] != src[i + 1]))
- {
- runlen++;
- i++;
- }
- if (i == (srclen - 1))
- {
- runlen++;
- i++;
- }
- if (!encode_block(dst, j, src, k, runlen, dstlen))
- return 0;
- }
- }
-
- return j;
-}
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "cenc.h" +#include <firmware.h> +#include <stdexcept> + + +namespace { +const byte CODE_MASK = 0xC0; +const byte ARGS_MASK = 0x3F; + +const byte REPEAT_CODE = 0x00; +const byte BLOCK_CODE = 0x40; +const byte LONG_RUN_CODE = 0x80; +const byte SHORT_RUN_CODE = 0xC0; + +const byte BLOCK_ARGS = 0x1F; +const byte BLOCK_MODE = 0x20; + + +void decode_run(byte* dst, word len, byte val, + int& dstidx) +{ + memset(dst + dstidx, val, len); + dstidx += len; +} + +void decode_pattern(byte* src, byte* dst, + word len, int& srcidx, int& dstidx, + bool bdecode, int npasses) +{ + for (int i = 0; i < npasses; i++) + { + if (bdecode) + { + for (int j = 0; j < len; j++) + { + word c, d; + c = src[srcidx + j]; + d = (c >> 5) & 7; + c = (c << 3) & 0xF8; + src[srcidx + j] = static_cast<byte>(c | d); + } + bdecode = false; + } + memcpy(dst + dstidx, src + srcidx, len); + dstidx += len; + } + srcidx += len; +} +}; //namespace + +int zen::cenc_decode(byte* src, int srclen, byte* dst, int dstlen) +{ + if (!src || !srclen || !dst || !dstlen) + { + throw std::invalid_argument("Invalid argument(s)."); + } + + int i = 0, j = 0; + do + { + word c, d, e; + c = src[i++]; + switch (c & CODE_MASK) + { + case REPEAT_CODE: // 2 bytes + d = src[i++]; + d = d + 2; + + e = (c & ARGS_MASK) + 2; + + decode_pattern(src, dst, e, i, j, false, d); + break; + + case BLOCK_CODE: // 1/2/3 bytes + d = c & BLOCK_ARGS; + if (!(c & BLOCK_MODE)) + { + e = src[i++]; + e = (d << 8) + (e + 0x21); + + d = static_cast<word>(i ^ j); + } + else + { + e = d + 1; + + d = static_cast<word>(i ^ j); + } + if (d & 1) + { + i++; + } + + decode_pattern(src, dst, e, i, j, true, 1); + break; + + case LONG_RUN_CODE: // 3 bytes + d = src[i++]; + e = ((c & ARGS_MASK) << 8) + (d + 0x42); + + d = src[i++]; + d = ((d & 7) << 5) | ((d >> 3) & 0x1F); + + decode_run(dst, e, static_cast<byte>(d), j); + break; + + case SHORT_RUN_CODE: // 2 bytes + d = src[i++]; + d = ((d & 3) << 6) | ((d >> 2) & 0x3F); + + e = (c & ARGS_MASK) + 2; + + decode_run(dst, e, static_cast<byte>(d), j); + break; + }; + } while (i < srclen && j < dstlen); + + return j; +} + +namespace { +int encode_run(byte* dst, int& dstidx, byte val, int len, int dstlen) +{ + if (len < 2) + throw std::invalid_argument("Length is too small."); + + int ret = 0; + if (len <= 0x41) + { + if ((dstidx + 2) > dstlen) + throw std::runtime_error("Not enough space to store run."); + + dst[dstidx++] = SHORT_RUN_CODE | (((len - 2) & ARGS_MASK)); + dst[dstidx++] = ((val >> 6) & 3) | ((val & 0x3F) << 2); + + ret = 2; + } + else if (len <= 0x4041) + { + if ((dstidx + 3) > dstlen) + throw std::runtime_error("Not enough space to store run."); + + byte b1 = (len - 0x42) >> 8; + byte b2 = (len - 0x42) & 0xFF; + + dst[dstidx++] = LONG_RUN_CODE | ((b1 & ARGS_MASK)); + dst[dstidx++] = b2; + dst[dstidx++] = ((val >> 5) & 7) | ((val & 0x1F) << 3); + + ret = 3; + } + else + { + int long_count = len / 0x4041; + int short_len = len % 0x4041; + bool toosmall = short_len == 1; + + int run_len = 0x4041; + for (int i = 0; i < long_count; i++) + { + if (toosmall && (i == (long_count-1))) + { + run_len--; + toosmall = false; + } + int tmp = encode_run(dst, dstidx, val, run_len, dstlen); + if (!tmp) return 0; + ret += tmp; + len -= run_len; + } + + if (len) + { + int short_count = len / 0x41; + int short_rest = short_count ? (len % 0x41) : 0; + toosmall = short_rest == 1; + + run_len = 0x41; + for (int i = 0; i < short_count; i++) + { + if (toosmall && (i == (short_count-1))) + { + run_len--; + toosmall = false; + } + int tmp = encode_run(dst, dstidx, val, run_len, dstlen); + if (!tmp) return 0; + ret += tmp; + len -= run_len; + } + int tmp = encode_run(dst, dstidx, val, len, dstlen); + if (!tmp) return 0; + ret += tmp; + len -= len; + } + } + + return ret; +} + +int encode_block(byte* dst, int& dstidx, byte* src, int& srcidx, int len, + int dstlen) +{ + if (len < 1) + throw std::invalid_argument("Length is too small."); + + int startidx = dstidx; + if (len < 0x21) + { + if ((dstidx + 2 + len) > dstlen) + throw std::runtime_error("Not enough space to store block."); + + dst[dstidx++] = BLOCK_CODE | BLOCK_MODE | ((len - 1) & BLOCK_ARGS); + if ((dstidx ^ srcidx) & 1) + dst[dstidx++] = 0; + + for (int i = 0; i < len; i++) + { + byte c = src[srcidx++]; + byte d = (c & 7) << 5; + c = (c & 0xF8) >> 3; + dst[dstidx++] = c | d; + } + } + else if (len < 0x2021) + { + if ((dstidx + 3 + len) > dstlen) + throw std::runtime_error("Not enough space to store block."); + + dst[dstidx++] = BLOCK_CODE | (((len - 0x21) >> 8) & BLOCK_ARGS); + dst[dstidx++] = (len - 0x21) & 0xFF; + if ((dstidx ^ srcidx) & 1) + dst[dstidx++] = 0; + + for (int i = 0; i < len; i++) + { + byte c = src[srcidx++]; + byte d = (c & 7) << 5; + c = (c & 0xF8) >> 3; + dst[dstidx++] = c | d; + } + } + else + { + int longblocks = len / 0x2020; + int rest = len % 0x2020; + for (int i = 0; i < longblocks; i++) + { + int tmp = encode_block(dst, dstidx, src, srcidx, 0x2020, dstlen); + if (!tmp) return 0; + } + if (rest) + { + int shortblocks = rest / 0x20; + for (int i = 0; i < shortblocks; i++) + { + int tmp = encode_block(dst, dstidx, src, srcidx, 0x20, dstlen); + if (!tmp) return 0; + } + rest = rest % 0x20; + int tmp = encode_block(dst, dstidx, src, srcidx, rest, dstlen); + if (!tmp) return 0; + } + } + + return (dstidx - startidx); +} +}; //namespace + +int zen::cenc_encode(byte* src, int srclen, byte* dst, int dstlen) +{ + if (!src || !srclen || !dst || !dstlen) + { + throw std::invalid_argument("Invalid argument(s)."); + } + + int i = 0, j = 0, k = 0; + word c, d, e; + int runlen = 0; + while (i < srclen && j < dstlen) + { + k = i; + c = src[i++]; + runlen = 1; + while (i < srclen && src[i] == c) + { + runlen++; + i++; + } + if (runlen >= 2) + { + if (!encode_run(dst, j, c, runlen, dstlen)) + return 0; + } + else + { + runlen = 0; + i = k; + while (i < (srclen - 1) && (src[i] != src[i + 1])) + { + runlen++; + i++; + } + if (i == (srclen - 1)) + { + runlen++; + i++; + } + if (!encode_block(dst, j, src, k, runlen, dstlen)) + return 0; + } + } + + return j; +} diff --git a/utils/zenutils/source/shared/cenc.h b/utils/zenutils/source/shared/cenc.h index 12a7c92..e96794d 100755..100644 --- a/utils/zenutils/source/shared/cenc.h +++ b/utils/zenutils/source/shared/cenc.h @@ -1,29 +1,29 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#ifndef ZEN_CENC_H_INCLUDED
-#define ZEN_CENC_H_INCLUDED
-
-#include <utils.h>
-
-namespace zen {
- int cenc_decode(byte* src, int srclen, byte* dst, int dstlen);
- int cenc_encode(byte* src, int srclen, byte* dst, int dstlen);
-}; //namespace zen
-
-#endif //CENC_H_INCLUDED
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef ZEN_CENC_H_INCLUDED +#define ZEN_CENC_H_INCLUDED + +#include <utils.h> + +namespace zen { + int cenc_decode(byte* src, int srclen, byte* dst, int dstlen); + int cenc_encode(byte* src, int srclen, byte* dst, int dstlen); +}; //namespace zen + +#endif //CENC_H_INCLUDED diff --git a/utils/zenutils/source/shared/crypt.cpp b/utils/zenutils/source/shared/crypt.cpp index 9c2d338..3f15ac6 100755..100644 --- a/utils/zenutils/source/shared/crypt.cpp +++ b/utils/zenutils/source/shared/crypt.cpp @@ -1,91 +1,91 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include "crypt.h"
-#include <stdexcept>
-#include <beecrypt/hmacsha1.h>
-#include <beecrypt/blockmode.h>
-#include <beecrypt/blowfish.h>
-
-
-bool zen::hmac_sha1_calc(const byte* key, size_t keylen, const byte* data,
- size_t datalen, byte* sig, size_t* siglen)
-{
- hmacsha1Param param;
- if (hmacsha1Setup(¶m, key, keylen * 8))
- return false;
- if (hmacsha1Update(¶m, data, datalen))
- return false;
- if (hmacsha1Digest(¶m, sig))
- return false;
- return true;
-}
-
-bool zen::bf_cbc_encrypt(const byte* key, size_t keylen, byte* data,
- size_t datalen, const byte* iv)
-{
- if (datalen % blowfish.blocksize)
- throw std::invalid_argument(
- "The length must be aligned on a 8 byte boundary.");
-
- blowfishParam param;
- if (blowfishSetup(¶m, key, keylen * 8, ENCRYPT))
- return false;
- if (blowfishSetIV(¶m, iv))
- return false;
-
- byte* plain = new byte[datalen];
- memcpy(plain, data, datalen);
-
- unsigned int nblocks = datalen / blowfish.blocksize;
- if (blockEncryptCBC(&blowfish, ¶m, (uint32_t*)data, (uint32_t*)plain,
- nblocks))
- {
- delete [] plain;
- return false;
- }
-
- return true;
-}
-
-bool zen::bf_cbc_decrypt(const byte* key, size_t keylen, byte* data,
- size_t datalen, const byte* iv)
-{
- if (datalen % blowfish.blocksize)
- throw std::invalid_argument(
- "The length must be aligned on a 8 byte boundary.");
-
- blowfishParam param;
- if (blowfishSetup(¶m, key, keylen * 8, ENCRYPT))
- return false;
- if (blowfishSetIV(¶m, iv))
- return false;
-
- byte* cipher = new byte[datalen];
- memcpy(cipher, data, datalen);
-
- unsigned int nblocks = datalen / blowfish.blocksize;
- if (blockDecryptCBC(&blowfish, ¶m, (uint32_t*)data, (uint32_t*)cipher,
- nblocks))
- {
- delete [] cipher;
- return false;
- }
-
- return true;
-}
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "crypt.h" +#include <stdexcept> +#include <beecrypt/hmacsha1.h> +#include <beecrypt/blockmode.h> +#include <beecrypt/blowfish.h> + + +bool zen::hmac_sha1_calc(const byte* key, size_t keylen, const byte* data, + size_t datalen, byte* sig, size_t* siglen) +{ + hmacsha1Param param; + if (hmacsha1Setup(¶m, key, keylen * 8)) + return false; + if (hmacsha1Update(¶m, data, datalen)) + return false; + if (hmacsha1Digest(¶m, sig)) + return false; + return true; +} + +bool zen::bf_cbc_encrypt(const byte* key, size_t keylen, byte* data, + size_t datalen, const byte* iv) +{ + if (datalen % blowfish.blocksize) + throw std::invalid_argument( + "The length must be aligned on a 8 byte boundary."); + + blowfishParam param; + if (blowfishSetup(¶m, key, keylen * 8, ENCRYPT)) + return false; + if (blowfishSetIV(¶m, iv)) + return false; + + byte* plain = new byte[datalen]; + memcpy(plain, data, datalen); + + unsigned int nblocks = datalen / blowfish.blocksize; + if (blockEncryptCBC(&blowfish, ¶m, (uint32_t*)data, (uint32_t*)plain, + nblocks)) + { + delete [] plain; + return false; + } + + return true; +} + +bool zen::bf_cbc_decrypt(const byte* key, size_t keylen, byte* data, + size_t datalen, const byte* iv) +{ + if (datalen % blowfish.blocksize) + throw std::invalid_argument( + "The length must be aligned on a 8 byte boundary."); + + blowfishParam param; + if (blowfishSetup(¶m, key, keylen * 8, ENCRYPT)) + return false; + if (blowfishSetIV(¶m, iv)) + return false; + + byte* cipher = new byte[datalen]; + memcpy(cipher, data, datalen); + + unsigned int nblocks = datalen / blowfish.blocksize; + if (blockDecryptCBC(&blowfish, ¶m, (uint32_t*)data, (uint32_t*)cipher, + nblocks)) + { + delete [] cipher; + return false; + } + + return true; +} diff --git a/utils/zenutils/source/shared/crypt.h b/utils/zenutils/source/shared/crypt.h index a057055..a357fef 100755..100644 --- a/utils/zenutils/source/shared/crypt.h +++ b/utils/zenutils/source/shared/crypt.h @@ -1,30 +1,30 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#ifndef ZEN_CRYPT_H_INCLUDED
-#define ZEN_CRYPT_H_INCLUDED
-
-#include <utils.h>
-
-namespace zen {
- bool hmac_sha1_calc(const byte* key, size_t keylen, const byte* data, size_t datalen, byte* sig, size_t* siglen);
- bool bf_cbc_encrypt(const byte* key, size_t keylen, byte* data, size_t datalen, const byte* iv);
- bool bf_cbc_decrypt(const byte* key, size_t keylen, byte* data, size_t datalen, const byte* iv);
-}; //namespace zen
-
-#endif //ZEN_CRYPT_H_INCLUDED
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef ZEN_CRYPT_H_INCLUDED +#define ZEN_CRYPT_H_INCLUDED + +#include <utils.h> + +namespace zen { + bool hmac_sha1_calc(const byte* key, size_t keylen, const byte* data, size_t datalen, byte* sig, size_t* siglen); + bool bf_cbc_encrypt(const byte* key, size_t keylen, byte* data, size_t datalen, const byte* iv); + bool bf_cbc_decrypt(const byte* key, size_t keylen, byte* data, size_t datalen, const byte* iv); +}; //namespace zen + +#endif //ZEN_CRYPT_H_INCLUDED diff --git a/utils/zenutils/source/shared/file.cpp b/utils/zenutils/source/shared/file.cpp index 2c31498..b1b1093 100755..100644 --- a/utils/zenutils/source/shared/file.cpp +++ b/utils/zenutils/source/shared/file.cpp @@ -1,106 +1,106 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include "file.h"
-#include <fstream>
-
-
-bool shared::read_file(const std::string& filename, bytes& buffer,
- std::streampos offset, std::streamsize count)
-{
- std::ifstream ifs;
- ifs.open(filename.c_str(), std::ios::binary);
- if (!ifs)
- {
- return false;
- }
-
- std::ifstream::pos_type startpos = offset;
- ifs.seekg(offset, std::ios::beg);
- if (count == -1)
- ifs.seekg(0, std::ios::end);
- else
- ifs.seekg(count, std::ios::cur);
- std::ifstream::pos_type endpos = ifs.tellg();
-
- buffer.resize(endpos-startpos);
- ifs.seekg(offset, std::ios::beg);
-
- ifs.read((char*)&buffer[0], endpos-startpos);
-
- ifs.close();
- return ifs.good();
-}
-
-
-bool shared::write_file(const std::string& filename, bytes& buffer,
- bool truncate, std::streampos offset,
- std::streamsize count)
-{
- std::ios::openmode mode = std::ios::in|std::ios::out|std::ios::binary;
- if (truncate)
- mode |= std::ios::trunc;
-
- std::fstream ofs;
- ofs.open(filename.c_str(), mode);
- if (!ofs)
- {
- return false;
- }
-
- if (count == -1)
- count = buffer.size();
- else if (count > buffer.size())
- return false;
-
- ofs.seekg(offset, std::ios::beg);
-
- ofs.write((char*)&buffer[0], count);
-
- ofs.close();
- return ofs.good();
-}
-
-bool shared::file_exists(const std::string& filename)
-{
- std::ifstream ifs;
- ifs.open(filename.c_str(), std::ios::in);
- if (ifs.is_open())
- {
- ifs.close();
- return true;
- }
- return false;
-}
-
-bool shared::copy_file(const std::string& srcname, const std::string& dstname)
-{
- bytes buffer;
- if (!read_file(srcname, buffer))
- return false;
- return write_file(dstname, buffer, true);
-}
-
-bool shared::backup_file(const std::string& filename, bool force)
-{
- std::string backupname = filename + ".bak";
- if (!force)
- if (file_exists(backupname))
- return true;
- return copy_file(filename, backupname);
-}
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "file.h" +#include <fstream> + + +bool shared::read_file(const std::string& filename, bytes& buffer, + std::streampos offset, std::streamsize count) +{ + std::ifstream ifs; + ifs.open(filename.c_str(), std::ios::binary); + if (!ifs) + { + return false; + } + + std::ifstream::pos_type startpos = offset; + ifs.seekg(offset, std::ios::beg); + if (count == -1) + ifs.seekg(0, std::ios::end); + else + ifs.seekg(count, std::ios::cur); + std::ifstream::pos_type endpos = ifs.tellg(); + + buffer.resize(endpos-startpos); + ifs.seekg(offset, std::ios::beg); + + ifs.read((char*)&buffer[0], endpos-startpos); + + ifs.close(); + return ifs.good(); +} + + +bool shared::write_file(const std::string& filename, bytes& buffer, + bool truncate, std::streampos offset, + std::streamsize count) +{ + std::ios::openmode mode = std::ios::in|std::ios::out|std::ios::binary; + if (truncate) + mode |= std::ios::trunc; + + std::fstream ofs; + ofs.open(filename.c_str(), mode); + if (!ofs) + { + return false; + } + + if (count == -1) + count = buffer.size(); + else if (count > buffer.size()) + return false; + + ofs.seekg(offset, std::ios::beg); + + ofs.write((char*)&buffer[0], count); + + ofs.close(); + return ofs.good(); +} + +bool shared::file_exists(const std::string& filename) +{ + std::ifstream ifs; + ifs.open(filename.c_str(), std::ios::in); + if (ifs.is_open()) + { + ifs.close(); + return true; + } + return false; +} + +bool shared::copy_file(const std::string& srcname, const std::string& dstname) +{ + bytes buffer; + if (!read_file(srcname, buffer)) + return false; + return write_file(dstname, buffer, true); +} + +bool shared::backup_file(const std::string& filename, bool force) +{ + std::string backupname = filename + ".bak"; + if (!force) + if (file_exists(backupname)) + return true; + return copy_file(filename, backupname); +} diff --git a/utils/zenutils/source/shared/file.h b/utils/zenutils/source/shared/file.h index 8fa533c..770f39a 100755..100644 --- a/utils/zenutils/source/shared/file.h +++ b/utils/zenutils/source/shared/file.h @@ -1,36 +1,36 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#ifndef SHARED_FILE_H_INCLUDED
-#define SHARED_FILE_H_INCLUDED
-
-#include <string>
-#include <iostream>
-#include "utils.h"
-
-namespace shared {
- bool read_file(const std::string& filename, bytes& buffer,
- std::streampos offset = 0, std::streamsize count = -1);
- bool write_file(const std::string& filename, bytes& buffer, bool truncate,
- std::streampos offset = 0, std::streamsize count = -1);
- bool file_exists(const std::string& filename);
- bool copy_file(const std::string& srcname, const std::string& dstname);
- bool backup_file(const std::string& filename, bool force = false);
-}; //namespace shared
-
-#endif //SHARED_FILE_H_INCLUDED
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef SHARED_FILE_H_INCLUDED +#define SHARED_FILE_H_INCLUDED + +#include <string> +#include <iostream> +#include "utils.h" + +namespace shared { + bool read_file(const std::string& filename, bytes& buffer, + std::streampos offset = 0, std::streamsize count = -1); + bool write_file(const std::string& filename, bytes& buffer, bool truncate, + std::streampos offset = 0, std::streamsize count = -1); + bool file_exists(const std::string& filename); + bool copy_file(const std::string& srcname, const std::string& dstname); + bool backup_file(const std::string& filename, bool force = false); +}; //namespace shared + +#endif //SHARED_FILE_H_INCLUDED diff --git a/utils/zenutils/source/shared/firmware.cpp b/utils/zenutils/source/shared/firmware.cpp index 7767b55..811b814 100755..100644 --- a/utils/zenutils/source/shared/firmware.cpp +++ b/utils/zenutils/source/shared/firmware.cpp @@ -1,387 +1,387 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include "firmware.h"
-#include <iostream>
-#include <stdexcept>
-
-
-zen::firmware_entry::firmware_entry(bool big_endian)
- : _big_endian(big_endian)
-{
-}
-
-zen::firmware_entry::firmware_entry(const firmware_entry& copy)
-{
- assign(copy);
-}
-
-zen::firmware_entry& zen::firmware_entry::operator=(const firmware_entry& right)
-{
- assign(right);
- return *this;
-}
-
-
-bool zen::firmware_entry::read(std::istream& is)
-{
- // Read the header.
- is.read((char*)&_header, sizeof(firmware_header_t));
- if (!is.good())
- return false;
-
- // If the firmware is big-endian, swap the header values to little-endian.
- if (_big_endian)
- {
- _header.tag = shared::swap(_header.tag);
- if (_header.tag != 'NULL')
- {
- _header.size = shared::swap(_header.size);
- }
- }
-
- // Resize the bytes buffer to the size specified in the header.
- _bytes.resize(_header.size);
-
- // Read the entry contents.
- is.read(reinterpret_cast<char*>(&_bytes[0]),
- _header.size);
-
- return is.good();
-}
-
-bool zen::firmware_entry::write(std::ostream& os) const
-{
- // Form a header using the current size of the bytes buffer.
- firmware_header_t header = {
- _header.tag,
- static_cast<dword>(_bytes.size())
- };
-
- // If the firmware is big-endian, swap the header values back into big-endian.
- if (_big_endian)
- {
- if (header.tag != 'NULL')
- {
- header.size = shared::swap(header.size);
- }
- header.tag = shared::swap(header.tag);
- }
-
- // Write the header.
- os.write((const char*)&header, sizeof(firmware_header_t));
- if (!os.good())
- return false;
-
- // Write the entry contents.
- os.write(reinterpret_cast<const char*>(&_bytes[0]),
- static_cast<std::streamsize>(_bytes.size()));
-
- return os.good();
-}
-
-
-bool zen::firmware_entry::is_big_endian() const
-{
- return _big_endian;
-}
-
-const zen::firmware_header_t& zen::firmware_entry::get_header() const
-{
- return _header;
-}
-zen::firmware_header_t& zen::firmware_entry::get_header()
-{
- return _header;
-}
-
-const shared::bytes& zen::firmware_entry::get_bytes() const
-{
- return _bytes;
-}
-shared::bytes& zen::firmware_entry::get_bytes()
-{
- return _bytes;
-}
-
-
-std::string zen::firmware_entry::get_name() const
-{
- char name[5];
- *(dword*)name = shared::swap(_header.tag);
- name[4] = '\0';
-
- // Determine if all characters in the tag are printable.
- bool isprintable = true;
- for (int i = 0; i < 4; i++)
- {
- if (!isprint((byte)name[i]))
- {
- isprintable = false;
- break;
- }
- }
-
- // If they are, simply return the tag as a string.
- if (isprintable)
- {
- return std::string(name);
- }
-
- // Otherwise, encode the tag into a hexadecimal string.
- char buffer[11];
- sprintf(buffer, "0x%08x", _header.tag);
- return std::string(buffer);
-}
-
-std::string zen::firmware_entry::get_content_name() const
-{
- std::string name = get_name();
- if (name == "DATA")
- {
- name = "";
- int nameoff = is_big_endian() ? 1 : 0;
- for (int i = 0; i < 16; i++)
- {
- char c = get_bytes()[i * 2 + nameoff];
- if (!c)
- break;
- name += c;
- }
- }
- else if (name == "EXT0")
- {
- name = "";
- int nameoff = is_big_endian() ? 1 : 0;
- for (int i = 0; i < 12; i++)
- {
- char c = get_bytes()[i * 2 + nameoff];
- if (!c)
- break;
- name += c;
- }
- }
- return name;
-}
-
-size_t zen::firmware_entry::get_content_offset() const
-{
- std::string name = get_name();
- if (name == "DATA")
- {
- return 32;
- }
- else if (name == "EXT0")
- {
- return 24;
- }
- return 0;
-}
-
-size_t zen::firmware_entry::calc_size() const
-{
- return _bytes.size() + sizeof(firmware_header_t);
-}
-
-
-void zen::firmware_entry::assign(const firmware_entry& copy)
-{
- _big_endian = copy._big_endian;
- _header.tag = copy._header.tag;
- _header.size = copy._header.size;
- _bytes.assign(copy._bytes.begin(), copy._bytes.end());
-}
-
-
-
-zen::firmware_archive::firmware_archive(bool big_endian)
- : _big_endian(big_endian)
-{
-}
-
-zen::firmware_archive::firmware_archive(const firmware_archive& copy)
-{
- assign(copy);
-}
-
-zen::firmware_archive& zen::firmware_archive::operator=(const firmware_archive& right)
-{
- assign(right);
- return *this;
-}
-
-
-bool zen::firmware_archive::read(std::istream& is)
-{
- // Read the root entry's header.
- firmware_header_t root;
- is.read((char*)&root, sizeof(firmware_header_t));
- if (!is.good())
- return false;
-
- if ((root.tag != 'CIFF') && (root.tag != 'FFIC'))
- {
- throw std::runtime_error("Invalid firmware archive format!");
- }
-
- _big_endian = root.tag == 'FFIC' ? true : false;
- if (_big_endian)
- {
- root.tag = shared::swap(root.tag);
- root.size = shared::swap(root.size);
- }
-
- // Save the current stream position.
- std::istream::pos_type endpos = is.tellg();
- std::istream::pos_type curpos = endpos;
- endpos += std::istream::pos_type(root.size);
-
- // Read untill the end of the root entry contents.
- while (curpos < endpos)
- {
- firmware_entry entry(_big_endian);
- if (!entry.read(is))
- return false;
-
- _children.push_back(entry);
- curpos = is.tellg();
- }
-
- curpos = is.tellg();
- is.seekg(0, std::ios::end);
- endpos = is.tellg();
- is.seekg(curpos);
-
- // Read untill the end of the file.
- while (((size_t)curpos + sizeof(firmware_header_t)) < endpos)
- {
- firmware_entry entry(_big_endian);
- if (!entry.read(is))
- return false;
-
- _neighbours.push_back(entry);
- curpos = is.tellg();
- }
-
- return true;
-}
-
-bool zen::firmware_archive::write(std::ostream& os) const
-{
- // Read the root entry's header.
- firmware_header_t root = {'CIFF', 0};
-
- // Calculate the total size of all the children entries.
- for (firmware_entries::const_iterator i = _children.begin();
- i != _children.end(); ++i)
- {
- root.size += i->calc_size();
- }
-
- // If the firmware is big-endian, swap the header values back into big-endian.
- if (_big_endian)
- {
- root.tag = shared::swap(root.tag);
- root.size = shared::swap(root.size);
- }
-
- // Write the header.
- os.write((const char*)&root, sizeof(firmware_header_t));
- if (!os.good())
- return false;
-
- // Write all the child entries.
- for (firmware_entries::const_iterator i = _children.begin();
- i != _children.end(); ++i)
- {
- if (!i->write(os))
- return false;
- }
-
- // Write all the neighbour entries.
- for (firmware_entries::const_iterator i = _neighbours.begin();
- i != _neighbours.end(); ++i)
- {
- if (!i->write(os))
- return false;
- }
-
- return true;
-}
-
-
-bool zen::firmware_archive::is_big_endian() const
-{
- return _big_endian;
-}
-
-const zen::firmware_entries& zen::firmware_archive::get_children() const
-{
- return _children;
-}
-zen::firmware_entries& zen::firmware_archive::get_children()
-{
- return _children;
-}
-
-const zen::firmware_entries& zen::firmware_archive::get_neighbours() const
-{
- return _neighbours;
-}
-zen::firmware_entries& zen::firmware_archive::get_neighbours()
-{
- return _neighbours;
-}
-
-bool zen::firmware_archive::is_signed() const
-{
- for (firmware_entries::const_iterator i = _neighbours.begin();
- i != _neighbours.end(); i++)
- {
- if (i->get_name() == "NULL")
- return true;
- }
- return false;
-}
-
-size_t zen::firmware_archive::calc_size() const
-{
- size_t size = sizeof(firmware_header_t);
-
- for (firmware_entries::const_iterator i = _children.begin();
- i != _children.end(); i++)
- {
- size += i->calc_size();
- }
-
- for (firmware_entries::const_iterator i = _neighbours.begin();
- i != _neighbours.end(); i++)
- {
- size += i->calc_size();
- }
-
- return size;
-}
-
-
-void zen::firmware_archive::assign(const firmware_archive& copy)
-{
- _big_endian = copy._big_endian;
- _children.assign(copy._children.begin(), copy._children.end());
- _neighbours.assign(copy._neighbours.begin(), copy._neighbours.end());
-}
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "firmware.h" +#include <iostream> +#include <stdexcept> + + +zen::firmware_entry::firmware_entry(bool big_endian) + : _big_endian(big_endian) +{ +} + +zen::firmware_entry::firmware_entry(const firmware_entry& copy) +{ + assign(copy); +} + +zen::firmware_entry& zen::firmware_entry::operator=(const firmware_entry& right) +{ + assign(right); + return *this; +} + + +bool zen::firmware_entry::read(std::istream& is) +{ + // Read the header. + is.read((char*)&_header, sizeof(firmware_header_t)); + if (!is.good()) + return false; + + // If the firmware is big-endian, swap the header values to little-endian. + if (_big_endian) + { + _header.tag = shared::swap(_header.tag); + if (_header.tag != 'NULL') + { + _header.size = shared::swap(_header.size); + } + } + + // Resize the bytes buffer to the size specified in the header. + _bytes.resize(_header.size); + + // Read the entry contents. + is.read(reinterpret_cast<char*>(&_bytes[0]), + _header.size); + + return is.good(); +} + +bool zen::firmware_entry::write(std::ostream& os) const +{ + // Form a header using the current size of the bytes buffer. + firmware_header_t header = { + _header.tag, + static_cast<dword>(_bytes.size()) + }; + + // If the firmware is big-endian, swap the header values back into big-endian. + if (_big_endian) + { + if (header.tag != 'NULL') + { + header.size = shared::swap(header.size); + } + header.tag = shared::swap(header.tag); + } + + // Write the header. + os.write((const char*)&header, sizeof(firmware_header_t)); + if (!os.good()) + return false; + + // Write the entry contents. + os.write(reinterpret_cast<const char*>(&_bytes[0]), + static_cast<std::streamsize>(_bytes.size())); + + return os.good(); +} + + +bool zen::firmware_entry::is_big_endian() const +{ + return _big_endian; +} + +const zen::firmware_header_t& zen::firmware_entry::get_header() const +{ + return _header; +} +zen::firmware_header_t& zen::firmware_entry::get_header() +{ + return _header; +} + +const shared::bytes& zen::firmware_entry::get_bytes() const +{ + return _bytes; +} +shared::bytes& zen::firmware_entry::get_bytes() +{ + return _bytes; +} + + +std::string zen::firmware_entry::get_name() const +{ + char name[5]; + *(dword*)name = shared::swap(_header.tag); + name[4] = '\0'; + + // Determine if all characters in the tag are printable. + bool isprintable = true; + for (int i = 0; i < 4; i++) + { + if (!isprint((byte)name[i])) + { + isprintable = false; + break; + } + } + + // If they are, simply return the tag as a string. + if (isprintable) + { + return std::string(name); + } + + // Otherwise, encode the tag into a hexadecimal string. + char buffer[11]; + sprintf(buffer, "0x%08x", _header.tag); + return std::string(buffer); +} + +std::string zen::firmware_entry::get_content_name() const +{ + std::string name = get_name(); + if (name == "DATA") + { + name = ""; + int nameoff = is_big_endian() ? 1 : 0; + for (int i = 0; i < 16; i++) + { + char c = get_bytes()[i * 2 + nameoff]; + if (!c) + break; + name += c; + } + } + else if (name == "EXT0") + { + name = ""; + int nameoff = is_big_endian() ? 1 : 0; + for (int i = 0; i < 12; i++) + { + char c = get_bytes()[i * 2 + nameoff]; + if (!c) + break; + name += c; + } + } + return name; +} + +size_t zen::firmware_entry::get_content_offset() const +{ + std::string name = get_name(); + if (name == "DATA") + { + return 32; + } + else if (name == "EXT0") + { + return 24; + } + return 0; +} + +size_t zen::firmware_entry::calc_size() const +{ + return _bytes.size() + sizeof(firmware_header_t); +} + + +void zen::firmware_entry::assign(const firmware_entry& copy) +{ + _big_endian = copy._big_endian; + _header.tag = copy._header.tag; + _header.size = copy._header.size; + _bytes.assign(copy._bytes.begin(), copy._bytes.end()); +} + + + +zen::firmware_archive::firmware_archive(bool big_endian) + : _big_endian(big_endian) +{ +} + +zen::firmware_archive::firmware_archive(const firmware_archive& copy) +{ + assign(copy); +} + +zen::firmware_archive& zen::firmware_archive::operator=(const firmware_archive& right) +{ + assign(right); + return *this; +} + + +bool zen::firmware_archive::read(std::istream& is) +{ + // Read the root entry's header. + firmware_header_t root; + is.read((char*)&root, sizeof(firmware_header_t)); + if (!is.good()) + return false; + + if ((root.tag != 'CIFF') && (root.tag != 'FFIC')) + { + throw std::runtime_error("Invalid firmware archive format!"); + } + + _big_endian = root.tag == 'FFIC' ? true : false; + if (_big_endian) + { + root.tag = shared::swap(root.tag); + root.size = shared::swap(root.size); + } + + // Save the current stream position. + std::istream::pos_type endpos = is.tellg(); + std::istream::pos_type curpos = endpos; + endpos += std::istream::pos_type(root.size); + + // Read untill the end of the root entry contents. + while (curpos < endpos) + { + firmware_entry entry(_big_endian); + if (!entry.read(is)) + return false; + + _children.push_back(entry); + curpos = is.tellg(); + } + + curpos = is.tellg(); + is.seekg(0, std::ios::end); + endpos = is.tellg(); + is.seekg(curpos); + + // Read untill the end of the file. + while (((size_t)curpos + sizeof(firmware_header_t)) < endpos) + { + firmware_entry entry(_big_endian); + if (!entry.read(is)) + return false; + + _neighbours.push_back(entry); + curpos = is.tellg(); + } + + return true; +} + +bool zen::firmware_archive::write(std::ostream& os) const +{ + // Read the root entry's header. + firmware_header_t root = {'CIFF', 0}; + + // Calculate the total size of all the children entries. + for (firmware_entries::const_iterator i = _children.begin(); + i != _children.end(); ++i) + { + root.size += i->calc_size(); + } + + // If the firmware is big-endian, swap the header values back into big-endian. + if (_big_endian) + { + root.tag = shared::swap(root.tag); + root.size = shared::swap(root.size); + } + + // Write the header. + os.write((const char*)&root, sizeof(firmware_header_t)); + if (!os.good()) + return false; + + // Write all the child entries. + for (firmware_entries::const_iterator i = _children.begin(); + i != _children.end(); ++i) + { + if (!i->write(os)) + return false; + } + + // Write all the neighbour entries. + for (firmware_entries::const_iterator i = _neighbours.begin(); + i != _neighbours.end(); ++i) + { + if (!i->write(os)) + return false; + } + + return true; +} + + +bool zen::firmware_archive::is_big_endian() const +{ + return _big_endian; +} + +const zen::firmware_entries& zen::firmware_archive::get_children() const +{ + return _children; +} +zen::firmware_entries& zen::firmware_archive::get_children() +{ + return _children; +} + +const zen::firmware_entries& zen::firmware_archive::get_neighbours() const +{ + return _neighbours; +} +zen::firmware_entries& zen::firmware_archive::get_neighbours() +{ + return _neighbours; +} + +bool zen::firmware_archive::is_signed() const +{ + for (firmware_entries::const_iterator i = _neighbours.begin(); + i != _neighbours.end(); i++) + { + if (i->get_name() == "NULL") + return true; + } + return false; +} + +size_t zen::firmware_archive::calc_size() const +{ + size_t size = sizeof(firmware_header_t); + + for (firmware_entries::const_iterator i = _children.begin(); + i != _children.end(); i++) + { + size += i->calc_size(); + } + + for (firmware_entries::const_iterator i = _neighbours.begin(); + i != _neighbours.end(); i++) + { + size += i->calc_size(); + } + + return size; +} + + +void zen::firmware_archive::assign(const firmware_archive& copy) +{ + _big_endian = copy._big_endian; + _children.assign(copy._children.begin(), copy._children.end()); + _neighbours.assign(copy._neighbours.begin(), copy._neighbours.end()); +} diff --git a/utils/zenutils/source/shared/firmware.h b/utils/zenutils/source/shared/firmware.h index 3542186..3cd233c 100755..100644 --- a/utils/zenutils/source/shared/firmware.h +++ b/utils/zenutils/source/shared/firmware.h @@ -1,92 +1,92 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#ifndef ZEN_FIRMWARE_H_INCLUDED
-#define ZEN_FIRMWARE_H_INCLUDED
-
-#include <list>
-#include <utils.h>
-
-namespace zen {
- struct firmware_header_t
- {
- dword tag;
- dword size;
- }; //struct firmware_header_t
-
- class firmware_entry
- {
- public:
- firmware_entry(bool big_endian);
- firmware_entry(const firmware_entry& copy);
- firmware_entry& operator=(const firmware_entry& right);
-
- bool read(std::istream& is);
- bool write(std::ostream& os) const;
-
- bool is_big_endian() const;
- const firmware_header_t& get_header() const;
- firmware_header_t& get_header();
- const shared::bytes& get_bytes() const;
- shared::bytes& get_bytes();
-
- std::string get_name() const;
- std::string get_content_name() const;
- size_t get_content_offset() const;
- size_t calc_size() const;
-
- protected:
- void assign(const firmware_entry& copy);
-
- private:
- bool _big_endian;
- firmware_header_t _header;
- shared::bytes _bytes;
- }; //class firmware_entry
-
- typedef std::list<firmware_entry> firmware_entries;
-
- class firmware_archive
- {
- public:
- firmware_archive(bool big_endian);
- firmware_archive(const firmware_archive& copy);
- firmware_archive& operator=(const firmware_archive& right);
-
- bool read(std::istream& is);
- bool write(std::ostream& os) const;
-
- bool is_big_endian() const;
- const firmware_entries& get_children() const;
- firmware_entries& get_children();
- const firmware_entries& get_neighbours() const;
- firmware_entries& get_neighbours();
- bool is_signed() const;
- size_t calc_size() const;
-
- protected:
- void assign(const firmware_archive& copy);
-
- private:
- firmware_entries _children;
- firmware_entries _neighbours;
- bool _big_endian;
- }; //class firmware_archive
-}; //namespace zen
-
-#endif //ZEN_FIRMWARE_ARCHIVE_H_INCLUDED
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef ZEN_FIRMWARE_H_INCLUDED +#define ZEN_FIRMWARE_H_INCLUDED + +#include <list> +#include <utils.h> + +namespace zen { + struct firmware_header_t + { + dword tag; + dword size; + }; //struct firmware_header_t + + class firmware_entry + { + public: + firmware_entry(bool big_endian); + firmware_entry(const firmware_entry& copy); + firmware_entry& operator=(const firmware_entry& right); + + bool read(std::istream& is); + bool write(std::ostream& os) const; + + bool is_big_endian() const; + const firmware_header_t& get_header() const; + firmware_header_t& get_header(); + const shared::bytes& get_bytes() const; + shared::bytes& get_bytes(); + + std::string get_name() const; + std::string get_content_name() const; + size_t get_content_offset() const; + size_t calc_size() const; + + protected: + void assign(const firmware_entry& copy); + + private: + bool _big_endian; + firmware_header_t _header; + shared::bytes _bytes; + }; //class firmware_entry + + typedef std::list<firmware_entry> firmware_entries; + + class firmware_archive + { + public: + firmware_archive(bool big_endian); + firmware_archive(const firmware_archive& copy); + firmware_archive& operator=(const firmware_archive& right); + + bool read(std::istream& is); + bool write(std::ostream& os) const; + + bool is_big_endian() const; + const firmware_entries& get_children() const; + firmware_entries& get_children(); + const firmware_entries& get_neighbours() const; + firmware_entries& get_neighbours(); + bool is_signed() const; + size_t calc_size() const; + + protected: + void assign(const firmware_archive& copy); + + private: + firmware_entries _children; + firmware_entries _neighbours; + bool _big_endian; + }; //class firmware_archive +}; //namespace zen + +#endif //ZEN_FIRMWARE_ARCHIVE_H_INCLUDED diff --git a/utils/zenutils/source/shared/pe.cpp b/utils/zenutils/source/shared/pe.cpp index c86ec6c..1007007 100755..100644 --- a/utils/zenutils/source/shared/pe.cpp +++ b/utils/zenutils/source/shared/pe.cpp @@ -1,128 +1,128 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include "pe.h"
-
-
-shared::pe_file::pe_file(PeLib::PeFile* pef) : _pef(pef)
-{
-}
-shared::pe_file::~pe_file()
-{
- if (_pef != NULL)
- delete _pef;
-}
-
-bool shared::pe_file::is_valid() const
-{
- if (_pef->getBits() == 32)
- {
- PeLib::PeHeader32& pef32 = static_cast<PeLib::PeFile32*>(_pef)->peHeader();
- if (!pef32.isValid())
- return false;
- return true;
- }
- else if (_pef->getBits() == 64)
- {
- PeLib::PeHeader64& pef64 = static_cast<PeLib::PeFile64*>(_pef)->peHeader();
- if (!pef64.isValid())
- return false;
- return true;
- }
- return false;
-}
-
-bool shared::pe_file::read(const std::string& filename)
-{
- if (_pef != NULL)
- {
- delete _pef;
- _pef = NULL;
- }
-
- _pef = PeLib::openPeFile(filename);
- if (!_pef)
- {
- return false;
- }
- if (_pef->readMzHeader())
- {
- delete _pef;
- return false;
- }
- if (!_pef->mzHeader().isValid())
- {
- delete _pef;
- return false;
- }
- if (_pef->readPeHeader())
- {
- delete _pef;
- return false;
- }
- if (!is_valid())
- {
- delete _pef;
- return false;
- }
- return true;
-}
-
-bool shared::pe_file::find_section(const std::string& name, section_info& info) const
-{
- if (_pef->getBits() == 32)
- return find_section(static_cast<PeLib::PeFile32*>(_pef),
- name, info);
- else if (_pef->getBits() == 64)
- return find_section(static_cast<PeLib::PeFile64*>(_pef),
- name, info);
- return false;
-}
-
-bool shared::pe_file::add_section(const std::string& name,
- const bytes& buffer, section_info& info)
-{
- if (_pef->getBits() == 32)
- {
- return add_section(static_cast<PeLib::PeFile32*>(_pef),
- name, buffer, info);
- }
- else if (_pef->getBits() == 64)
- {
- return add_section(static_cast<PeLib::PeFile64*>(_pef),
- name, buffer, info);
- }
- return false;
-}
-
-dword shared::pe_file::get_image_base() const
-{
- if (_pef->getBits() == 32)
- return static_cast<PeLib::PeFile32*>(_pef)->peHeader().getImageBase();
- else
- return static_cast<PeLib::PeFile64*>(_pef)->peHeader().getImageBase();
- return 0;
-}
-dword shared::pe_file::pa_to_va(dword pa) const
-{
- if (_pef->getBits() == 32)
- return static_cast<PeLib::PeFile32*>(_pef)->peHeader().offsetToVa(pa);
- else
- return static_cast<PeLib::PeFile64*>(_pef)->peHeader().offsetToVa(pa);
- return 0;
-}
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "pe.h" + + +shared::pe_file::pe_file(PeLib::PeFile* pef) : _pef(pef) +{ +} +shared::pe_file::~pe_file() +{ + if (_pef != NULL) + delete _pef; +} + +bool shared::pe_file::is_valid() const +{ + if (_pef->getBits() == 32) + { + PeLib::PeHeader32& pef32 = static_cast<PeLib::PeFile32*>(_pef)->peHeader(); + if (!pef32.isValid()) + return false; + return true; + } + else if (_pef->getBits() == 64) + { + PeLib::PeHeader64& pef64 = static_cast<PeLib::PeFile64*>(_pef)->peHeader(); + if (!pef64.isValid()) + return false; + return true; + } + return false; +} + +bool shared::pe_file::read(const std::string& filename) +{ + if (_pef != NULL) + { + delete _pef; + _pef = NULL; + } + + _pef = PeLib::openPeFile(filename); + if (!_pef) + { + return false; + } + if (_pef->readMzHeader()) + { + delete _pef; + return false; + } + if (!_pef->mzHeader().isValid()) + { + delete _pef; + return false; + } + if (_pef->readPeHeader()) + { + delete _pef; + return false; + } + if (!is_valid()) + { + delete _pef; + return false; + } + return true; +} + +bool shared::pe_file::find_section(const std::string& name, section_info& info) const +{ + if (_pef->getBits() == 32) + return find_section(static_cast<PeLib::PeFile32*>(_pef), + name, info); + else if (_pef->getBits() == 64) + return find_section(static_cast<PeLib::PeFile64*>(_pef), + name, info); + return false; +} + +bool shared::pe_file::add_section(const std::string& name, + const bytes& buffer, section_info& info) +{ + if (_pef->getBits() == 32) + { + return add_section(static_cast<PeLib::PeFile32*>(_pef), + name, buffer, info); + } + else if (_pef->getBits() == 64) + { + return add_section(static_cast<PeLib::PeFile64*>(_pef), + name, buffer, info); + } + return false; +} + +dword shared::pe_file::get_image_base() const +{ + if (_pef->getBits() == 32) + return static_cast<PeLib::PeFile32*>(_pef)->peHeader().getImageBase(); + else + return static_cast<PeLib::PeFile64*>(_pef)->peHeader().getImageBase(); + return 0; +} +dword shared::pe_file::pa_to_va(dword pa) const +{ + if (_pef->getBits() == 32) + return static_cast<PeLib::PeFile32*>(_pef)->peHeader().offsetToVa(pa); + else + return static_cast<PeLib::PeFile64*>(_pef)->peHeader().offsetToVa(pa); + return 0; +} diff --git a/utils/zenutils/source/shared/pe.h b/utils/zenutils/source/shared/pe.h index 92a272d..f2f3aa4 100755..100644 --- a/utils/zenutils/source/shared/pe.h +++ b/utils/zenutils/source/shared/pe.h @@ -1,142 +1,142 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#ifndef SHARED_PE_H_INCLUDED
-#define SHARED_PE_H_INCLUDED
-
-#include <string>
-#include <pelib/PeLib.h>
-#include <utils.h>
-
-namespace shared {
- struct section_info
- {
- word index;
- dword virtual_address;
- dword virtual_size;
- dword raw_address;
- dword raw_size;
- dword characteristics;
- }; //struct section_info
-
- class pe_file
- {
- public:
- pe_file(PeLib::PeFile* pef = NULL);
- ~pe_file();
-
- bool is_valid() const;
- bool read(const std::string& filename);
- bool find_section(const std::string& name, section_info& info) const;
- bool add_section(const std::string& name, const bytes& buffer, section_info& info);
- dword get_image_base() const;
- dword pa_to_va(PeLib::dword pa) const;
-
- protected:
- template <int _Bits>
- static bool find_section(const PeLib::PeFileT<_Bits>* pef,
- const std::string& name, section_info& info);
- template <int _Bits>
- static bool add_section(PeLib::PeFileT<_Bits>* pef,
- const std::string& name, const bytes& buffer,
- section_info& info);
-
- private:
- PeLib::PeFile* _pef;
- }; //class pe_file
-
-
- template <int _Bits>
- bool pe_file::find_section(const PeLib::PeFileT<_Bits>* pef,
- const std::string& name, section_info& info)
- {
- for (PeLib::word i = 0; i < pef->peHeader().getNumberOfSections(); i++)
- {
- if (pef->peHeader().getSectionName(i) == name)
- {
- info.index = i;
- info.virtual_address = pef->peHeader().getVirtualAddress(i);
- info.virtual_size = pef->peHeader().getVirtualSize(i);
- info.raw_address = pef->peHeader().getPointerToRawData(i);
- info.raw_size = pef->peHeader().getSizeOfRawData(i);
- info.characteristics = pef->peHeader().getCharacteristics(i);
- return true;
- }
- }
- return false;
- }
-
- template <int _Bits>
- bool pe_file::add_section(PeLib::PeFileT<_Bits>* pef,
- const std::string& name, const bytes& buffer,
- section_info& info)
- {
- using namespace PeLib;
-
- // Check if the last section has the same name as the one being added.
- PeLib::word secnum = pef->peHeader().getNumberOfSections();
- if (pef->peHeader().getSectionName(secnum-1) == name)
- {
- // If it is, we change the attributes of the existing section.
- secnum = secnum - 1;
- pef->peHeader().setSizeOfRawData(secnum,
- alignOffset(buffer.size(),
- pef->peHeader().getFileAlignment()));
- pef->peHeader().setVirtualSize(secnum,
- alignOffset(buffer.size(),
- pef->peHeader().getSectionAlignment()));
- PeLib::dword chars = pef->peHeader().getCharacteristics(secnum-1);
- pef->peHeader().setCharacteristics(secnum,
- chars | PELIB_IMAGE_SCN_MEM_WRITE | PELIB_IMAGE_SCN_MEM_READ);
- }
- else
- {
- // Otherwise we add a new section.
- if (pef->peHeader().addSection(name, buffer.size()) != NO_ERROR)
- {
- return false;
- }
- pef->peHeader().makeValid(pef->mzHeader().getAddressOfPeHeader());
- pef->peHeader().write(pef->getFileName(), pef->mzHeader().getAddressOfPeHeader());
- }
-
- // Save the section headers to the file.
- if (pef->peHeader().writeSections(pef->getFileName()) != NO_ERROR)
- {
- return false;
- }
-
- // Save the section data to the file.
- if (pef->peHeader().writeSectionData(pef->getFileName(), secnum, buffer) != NO_ERROR)
- {
- return false;
- }
-
- // Fill out the section information.
- info.index = secnum;
- info.virtual_address = pef->peHeader().getVirtualAddress(secnum);
- info.virtual_size = pef->peHeader().getVirtualSize(secnum);
- info.raw_address = pef->peHeader().getPointerToRawData(secnum);
- info.raw_size = pef->peHeader().getSizeOfRawData(secnum);
- info.characteristics = pef->peHeader().getCharacteristics(secnum);
-
- return true;
- }
-}; //namespace shared
-
-#endif //SHARED_PE_H_INCLUDED
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef SHARED_PE_H_INCLUDED +#define SHARED_PE_H_INCLUDED + +#include <string> +#include <pelib/PeLib.h> +#include <utils.h> + +namespace shared { + struct section_info + { + word index; + dword virtual_address; + dword virtual_size; + dword raw_address; + dword raw_size; + dword characteristics; + }; //struct section_info + + class pe_file + { + public: + pe_file(PeLib::PeFile* pef = NULL); + ~pe_file(); + + bool is_valid() const; + bool read(const std::string& filename); + bool find_section(const std::string& name, section_info& info) const; + bool add_section(const std::string& name, const bytes& buffer, section_info& info); + dword get_image_base() const; + dword pa_to_va(PeLib::dword pa) const; + + protected: + template <int _Bits> + static bool find_section(const PeLib::PeFileT<_Bits>* pef, + const std::string& name, section_info& info); + template <int _Bits> + static bool add_section(PeLib::PeFileT<_Bits>* pef, + const std::string& name, const bytes& buffer, + section_info& info); + + private: + PeLib::PeFile* _pef; + }; //class pe_file + + + template <int _Bits> + bool pe_file::find_section(const PeLib::PeFileT<_Bits>* pef, + const std::string& name, section_info& info) + { + for (PeLib::word i = 0; i < pef->peHeader().getNumberOfSections(); i++) + { + if (pef->peHeader().getSectionName(i) == name) + { + info.index = i; + info.virtual_address = pef->peHeader().getVirtualAddress(i); + info.virtual_size = pef->peHeader().getVirtualSize(i); + info.raw_address = pef->peHeader().getPointerToRawData(i); + info.raw_size = pef->peHeader().getSizeOfRawData(i); + info.characteristics = pef->peHeader().getCharacteristics(i); + return true; + } + } + return false; + } + + template <int _Bits> + bool pe_file::add_section(PeLib::PeFileT<_Bits>* pef, + const std::string& name, const bytes& buffer, + section_info& info) + { + using namespace PeLib; + + // Check if the last section has the same name as the one being added. + PeLib::word secnum = pef->peHeader().getNumberOfSections(); + if (pef->peHeader().getSectionName(secnum-1) == name) + { + // If it is, we change the attributes of the existing section. + secnum = secnum - 1; + pef->peHeader().setSizeOfRawData(secnum, + alignOffset(buffer.size(), + pef->peHeader().getFileAlignment())); + pef->peHeader().setVirtualSize(secnum, + alignOffset(buffer.size(), + pef->peHeader().getSectionAlignment())); + PeLib::dword chars = pef->peHeader().getCharacteristics(secnum-1); + pef->peHeader().setCharacteristics(secnum, + chars | PELIB_IMAGE_SCN_MEM_WRITE | PELIB_IMAGE_SCN_MEM_READ); + } + else + { + // Otherwise we add a new section. + if (pef->peHeader().addSection(name, buffer.size()) != NO_ERROR) + { + return false; + } + pef->peHeader().makeValid(pef->mzHeader().getAddressOfPeHeader()); + pef->peHeader().write(pef->getFileName(), pef->mzHeader().getAddressOfPeHeader()); + } + + // Save the section headers to the file. + if (pef->peHeader().writeSections(pef->getFileName()) != NO_ERROR) + { + return false; + } + + // Save the section data to the file. + if (pef->peHeader().writeSectionData(pef->getFileName(), secnum, buffer) != NO_ERROR) + { + return false; + } + + // Fill out the section information. + info.index = secnum; + info.virtual_address = pef->peHeader().getVirtualAddress(secnum); + info.virtual_size = pef->peHeader().getVirtualSize(secnum); + info.raw_address = pef->peHeader().getPointerToRawData(secnum); + info.raw_size = pef->peHeader().getSizeOfRawData(secnum); + info.characteristics = pef->peHeader().getCharacteristics(secnum); + + return true; + } +}; //namespace shared + +#endif //SHARED_PE_H_INCLUDED diff --git a/utils/zenutils/source/shared/shared.cpp b/utils/zenutils/source/shared/shared.cpp index e69de29..e69de29 100755..100644 --- a/utils/zenutils/source/shared/shared.cpp +++ b/utils/zenutils/source/shared/shared.cpp diff --git a/utils/zenutils/source/shared/updater.cpp b/utils/zenutils/source/shared/updater.cpp index 77d3f28..25d8452 100755..100644 --- a/utils/zenutils/source/shared/updater.cpp +++ b/utils/zenutils/source/shared/updater.cpp @@ -1,151 +1,151 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include "updater.h"
-#include <file.h>
-#include <pe.h>
-#include <utils.h>
-
-
-const char* zen::find_firmware_key(const byte* buffer, size_t len)
-{
- char szkey1[] = "34d1";
- size_t cchkey1 = strlen(szkey1);
- char szkey2[] = "TbnCboEbn";
- size_t cchkey2 = strlen(szkey2);
- for (int i = 0; i < static_cast<int>(len); i++)
- {
- if (len >= cchkey1)
- {
- if (!strncmp((char*)&buffer[i], szkey1, cchkey1))
- {
- return (const char*)&buffer[i];
- }
- }
- if (len >= cchkey2)
- {
- if (!strncmp((char*)&buffer[i], szkey2, cchkey2))
- {
- return (const char*)&buffer[i];
- }
- }
- }
- return NULL;
-}
-
-dword zen::find_firmware_offset(byte* buffer, size_t len)
-{
- for (dword i = 0; i < static_cast<dword>(len); i += 0x10)
- {
- dword size = *(dword*)&buffer[i];
- if (size < (i + len) && size > (len >> 1))
- {
- if (buffer[i + sizeof(dword)] != 0
- && buffer[i + sizeof(dword) + 1] != 0
- && buffer[i + sizeof(dword) + 2] != 0
- && buffer[i + sizeof(dword) + 3] != 0)
- {
- return i;
- }
- }
- }
- return 0;
-}
-
-bool zen::find_firmware_archive(const std::string& filename, dword& va, dword& pa)
-{
- shared::pe_file pef;
- if (!pef.read(filename))
- {
- return false;
- }
- shared::section_info data_section;
- if (!pef.find_section(".data", data_section))
- {
- return false;
- }
- shared::bytes buffer;
- if (!shared::read_file(filename, buffer, data_section.raw_address,
- data_section.raw_size))
- {
- return false;
- }
- dword offset = find_firmware_offset(&buffer[0], buffer.size());
- if (!offset)
- {
- return false;
- }
- va = data_section.virtual_address + offset;
- pa = data_section.raw_address + offset;
-
- return true;
-}
-
-
-bool zen::crypt_firmware(const char* key, byte* buffer, size_t len)
-{
- // Determine if the key length is dword aligned.
- int keylen = strlen(key);
- int keylen_rem = keylen % sizeof(dword);
-
- // Determine how many times the key must be repeated to be dword aligned.
- int keycycle = keylen_rem ? (sizeof(dword) / keylen_rem) : 1;
- int keyscount = (keylen * keycycle) / sizeof(dword);
-
- // Allocate a buffer to hold the key as an array of dwords.
- dword* keys = new dword[keyscount];
-
- // Copy the key into the key array, whilst mutating it.
- for (int i = 0; i < keyscount; i++)
- {
- dword val;
- int keyoffset = (i * sizeof(dword)) % keylen;
- if ((keyoffset+sizeof(dword)) < keylen)
- {
- val = *(dword*)&key[keyoffset];
- }
- else
- {
- val = key[keyoffset]
- | (key[(keyoffset + 1) % keylen] << 8)
- | (key[(keyoffset + 2) % keylen] << 16)
- | (key[(keyoffset + 3) % keylen] << 24);
- }
- keys[i] = (val - 0x01010101) | 0x80808080;
- }
-
- // Determine the number of dwords in the buffer.
- int len_div = len / sizeof(dword);
-
- // Decrypt all dwords of the buffer.
- for (int i = 0; i < len_div; i++)
- {
- ((dword*)buffer)[i] ^= keys[i % keyscount];
- }
-
- // Determine the remaining number of bytes in the buffer.
- int len_rem = len % sizeof(dword);
-
- // Decrypt the remaining number of bytes in the buffer.
- for (int i = len_div * sizeof(dword); i < len; i++)
- {
- buffer[i] ^= ((key[i % keylen] - 0x01) | 0x80);
- }
-
- return true;
-}
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "updater.h" +#include <file.h> +#include <pe.h> +#include <utils.h> + + +const char* zen::find_firmware_key(const byte* buffer, size_t len) +{ + char szkey1[] = "34d1"; + size_t cchkey1 = strlen(szkey1); + char szkey2[] = "TbnCboEbn"; + size_t cchkey2 = strlen(szkey2); + for (int i = 0; i < static_cast<int>(len); i++) + { + if (len >= cchkey1) + { + if (!strncmp((char*)&buffer[i], szkey1, cchkey1)) + { + return (const char*)&buffer[i]; + } + } + if (len >= cchkey2) + { + if (!strncmp((char*)&buffer[i], szkey2, cchkey2)) + { + return (const char*)&buffer[i]; + } + } + } + return NULL; +} + +dword zen::find_firmware_offset(byte* buffer, size_t len) +{ + for (dword i = 0; i < static_cast<dword>(len); i += 0x10) + { + dword size = *(dword*)&buffer[i]; + if (size < (i + len) && size > (len >> 1)) + { + if (buffer[i + sizeof(dword)] != 0 + && buffer[i + sizeof(dword) + 1] != 0 + && buffer[i + sizeof(dword) + 2] != 0 + && buffer[i + sizeof(dword) + 3] != 0) + { + return i; + } + } + } + return 0; +} + +bool zen::find_firmware_archive(const std::string& filename, dword& va, dword& pa) +{ + shared::pe_file pef; + if (!pef.read(filename)) + { + return false; + } + shared::section_info data_section; + if (!pef.find_section(".data", data_section)) + { + return false; + } + shared::bytes buffer; + if (!shared::read_file(filename, buffer, data_section.raw_address, + data_section.raw_size)) + { + return false; + } + dword offset = find_firmware_offset(&buffer[0], buffer.size()); + if (!offset) + { + return false; + } + va = data_section.virtual_address + offset; + pa = data_section.raw_address + offset; + + return true; +} + + +bool zen::crypt_firmware(const char* key, byte* buffer, size_t len) +{ + // Determine if the key length is dword aligned. + int keylen = strlen(key); + int keylen_rem = keylen % sizeof(dword); + + // Determine how many times the key must be repeated to be dword aligned. + int keycycle = keylen_rem ? (sizeof(dword) / keylen_rem) : 1; + int keyscount = (keylen * keycycle) / sizeof(dword); + + // Allocate a buffer to hold the key as an array of dwords. + dword* keys = new dword[keyscount]; + + // Copy the key into the key array, whilst mutating it. + for (int i = 0; i < keyscount; i++) + { + dword val; + int keyoffset = (i * sizeof(dword)) % keylen; + if ((keyoffset+sizeof(dword)) < keylen) + { + val = *(dword*)&key[keyoffset]; + } + else + { + val = key[keyoffset] + | (key[(keyoffset + 1) % keylen] << 8) + | (key[(keyoffset + 2) % keylen] << 16) + | (key[(keyoffset + 3) % keylen] << 24); + } + keys[i] = (val - 0x01010101) | 0x80808080; + } + + // Determine the number of dwords in the buffer. + int len_div = len / sizeof(dword); + + // Decrypt all dwords of the buffer. + for (int i = 0; i < len_div; i++) + { + ((dword*)buffer)[i] ^= keys[i % keyscount]; + } + + // Determine the remaining number of bytes in the buffer. + int len_rem = len % sizeof(dword); + + // Decrypt the remaining number of bytes in the buffer. + for (int i = len_div * sizeof(dword); i < len; i++) + { + buffer[i] ^= ((key[i % keylen] - 0x01) | 0x80); + } + + return true; +} diff --git a/utils/zenutils/source/shared/updater.h b/utils/zenutils/source/shared/updater.h index 57f9979..884fe95 100755..100644 --- a/utils/zenutils/source/shared/updater.h +++ b/utils/zenutils/source/shared/updater.h @@ -1,32 +1,32 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#ifndef ZEN_UPDATER_H_INCLUDED
-#define ZEN_UPDATER_H_INCLUDED
-
-#include <pelib/PeLib.h>
-#include <utils.h>
-
-namespace zen {
- const char* find_firmware_key(const byte* buffer, size_t len);
- dword find_firmware_offset(byte* buffer, size_t len);
- bool find_firmware_archive(const std::string& filename, dword& va, dword& pa);
- bool crypt_firmware(const char* key, byte* buffer, size_t len);
-}; //namespace zen
-
-#endif //ZEN_UPDATER_H_INCLUDED
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef ZEN_UPDATER_H_INCLUDED +#define ZEN_UPDATER_H_INCLUDED + +#include <pelib/PeLib.h> +#include <utils.h> + +namespace zen { + const char* find_firmware_key(const byte* buffer, size_t len); + dword find_firmware_offset(byte* buffer, size_t len); + bool find_firmware_archive(const std::string& filename, dword& va, dword& pa); + bool crypt_firmware(const char* key, byte* buffer, size_t len); +}; //namespace zen + +#endif //ZEN_UPDATER_H_INCLUDED diff --git a/utils/zenutils/source/shared/utils.cpp b/utils/zenutils/source/shared/utils.cpp index 8f45de5..fbb223b 100755..100644 --- a/utils/zenutils/source/shared/utils.cpp +++ b/utils/zenutils/source/shared/utils.cpp @@ -1,211 +1,211 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include "utils.h"
-#include <fstream>
-#include <zlib/zlib.h>
-
-
-std::string shared::replace_extension(const std::string& filename, const std::string& extension)
-{
- std::string newname;
- const char* name = filename.c_str();
- const char* ext = strrchr(name, '.');
- if (ext)
- {
- // If an extension was found, replace it.
- newname.assign(name, ext-name);
- newname += extension;
- }
- else
- {
- // If an extension was not found, append it.
- newname = name;
- newname += extension;
- }
- return newname;
-}
-
-std::string shared::remove_extension(const std::string& filename)
-{
- std::string newname;
- const char* name = filename.c_str();
- const char* ext = strrchr(name, '.');
- if (ext)
- {
- newname.assign(name, ext-name);
- }
- else
- {
- newname = name;
- }
- return newname;
-}
-
-std::string shared::double_quote(const std::string& str)
-{
- std::string out;
- for (int i = 0, j = str.length(); i < j; i++)
- {
- if (str[i] == '\\')
- out += "\\\\";
- else
- out += str[i];
- }
- return out;
-}
-
-bool shared::inflate_to_file(const bytes& buffer, const char* filename)
-{
- // Open output file.
- std::ofstream ofs;
- ofs.open(filename, std::ios::binary);
- if (!ofs)
- {
- return false;
- }
-
- // Initialize zlib.
- z_stream d_stream; // decompression stream
-
- d_stream.zalloc = Z_NULL;
- d_stream.zfree = Z_NULL;
- d_stream.opaque = Z_NULL;
-
- d_stream.next_in = const_cast<bytes::value_type*>(&buffer[0]);
- d_stream.avail_in = static_cast<uInt>(buffer.size());
-
- int ret = inflateInit(&d_stream);
- if (ret != Z_OK)
- return false;
-
- // Allocate buffer to hold the inflated data.
- const size_t BUFSIZE = 1048576;
- Bytef* infbuf = new Bytef[BUFSIZE];
- if (!infbuf)
- return false;
-
- // Decompress untill the end of the input buffer.
- uLong totalout = 0;
- bool bLoop = true;
- while (bLoop)
- {
- d_stream.next_out = infbuf;
- d_stream.avail_out = BUFSIZE;
-
- ret = inflate(&d_stream, Z_NO_FLUSH);
- if (ret == Z_STREAM_END)
- {
- bLoop = false;
- }
- else if (ret != Z_OK)
- {
- inflateEnd(&d_stream);
- delete [] infbuf;
- return false;
- }
-
- // Write the inflated data to the output file.
- if (!ofs.write((const char*)infbuf, d_stream.total_out-totalout))
- {
- inflateEnd(&d_stream);
- delete [] infbuf;
- return false;
- }
- totalout = d_stream.total_out;
- }
-
- // Cleanup and return.
- inflateEnd(&d_stream);
- delete [] infbuf;
-
- return true;
-}
-
-bool shared::deflate_to_file(const bytes& buffer, const char* filename)
-{
- // Open output file.
- std::ofstream ofs;
- ofs.open(filename, std::ios::binary);
- if (!ofs)
- {
- return false;
- }
-
- // Initialize zlib.
- z_stream c_stream; // compression stream.
-
- c_stream.zalloc = Z_NULL;
- c_stream.zfree = Z_NULL;
- c_stream.opaque = Z_NULL;
-
- int ret = deflateInit(&c_stream, Z_BEST_COMPRESSION);
- if (ret != Z_OK)
- return false;
-
- // Allocate buffer to hold the deflated data.
- const size_t BUFSIZE = 1048576;
- Bytef* defbuf = new Bytef[BUFSIZE];
- if (!defbuf)
- return false;
-
- c_stream.avail_in = static_cast<uInt>(buffer.size());
- c_stream.next_in = const_cast<bytes::value_type*>(&buffer[0]);
-
- // Compress until end of the buffer.
- uLong totalout = 0;
- bool bLoop = true;
- while (bLoop)
- {
- c_stream.avail_out = BUFSIZE;
- c_stream.next_out = defbuf;
-
- ret = deflate(&c_stream, Z_NO_FLUSH); // no bad return value
- if (ret == Z_STREAM_END)
- {
- bLoop = false;
- }
- else if (ret == Z_BUF_ERROR && !c_stream.avail_in)
- {
- ret = deflate(&c_stream, Z_FINISH); // no bad return value
- bLoop = false;
- }
- else if (ret != Z_OK)
- {
- deflateEnd(&c_stream);
- delete [] defbuf;
- return false;
- }
-
- // Write the inflated data to the output file.
- if (!ofs.write((const char*)defbuf, c_stream.total_out-totalout))
- {
- deflateEnd(&c_stream);
- delete [] defbuf;
- return false;
- }
-
- totalout = c_stream.total_out;
- }
-
- // Clean up and return.
- deflateEnd(&c_stream);
- delete [] defbuf;
-
- return true;
-}
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "utils.h" +#include <fstream> +#include <zlib/zlib.h> + + +std::string shared::replace_extension(const std::string& filename, const std::string& extension) +{ + std::string newname; + const char* name = filename.c_str(); + const char* ext = strrchr(name, '.'); + if (ext) + { + // If an extension was found, replace it. + newname.assign(name, ext-name); + newname += extension; + } + else + { + // If an extension was not found, append it. + newname = name; + newname += extension; + } + return newname; +} + +std::string shared::remove_extension(const std::string& filename) +{ + std::string newname; + const char* name = filename.c_str(); + const char* ext = strrchr(name, '.'); + if (ext) + { + newname.assign(name, ext-name); + } + else + { + newname = name; + } + return newname; +} + +std::string shared::double_quote(const std::string& str) +{ + std::string out; + for (int i = 0, j = str.length(); i < j; i++) + { + if (str[i] == '\\') + out += "\\\\"; + else + out += str[i]; + } + return out; +} + +bool shared::inflate_to_file(const bytes& buffer, const char* filename) +{ + // Open output file. + std::ofstream ofs; + ofs.open(filename, std::ios::binary); + if (!ofs) + { + return false; + } + + // Initialize zlib. + z_stream d_stream; // decompression stream + + d_stream.zalloc = Z_NULL; + d_stream.zfree = Z_NULL; + d_stream.opaque = Z_NULL; + + d_stream.next_in = const_cast<bytes::value_type*>(&buffer[0]); + d_stream.avail_in = static_cast<uInt>(buffer.size()); + + int ret = inflateInit(&d_stream); + if (ret != Z_OK) + return false; + + // Allocate buffer to hold the inflated data. + const size_t BUFSIZE = 1048576; + Bytef* infbuf = new Bytef[BUFSIZE]; + if (!infbuf) + return false; + + // Decompress untill the end of the input buffer. + uLong totalout = 0; + bool bLoop = true; + while (bLoop) + { + d_stream.next_out = infbuf; + d_stream.avail_out = BUFSIZE; + + ret = inflate(&d_stream, Z_NO_FLUSH); + if (ret == Z_STREAM_END) + { + bLoop = false; + } + else if (ret != Z_OK) + { + inflateEnd(&d_stream); + delete [] infbuf; + return false; + } + + // Write the inflated data to the output file. + if (!ofs.write((const char*)infbuf, d_stream.total_out-totalout)) + { + inflateEnd(&d_stream); + delete [] infbuf; + return false; + } + totalout = d_stream.total_out; + } + + // Cleanup and return. + inflateEnd(&d_stream); + delete [] infbuf; + + return true; +} + +bool shared::deflate_to_file(const bytes& buffer, const char* filename) +{ + // Open output file. + std::ofstream ofs; + ofs.open(filename, std::ios::binary); + if (!ofs) + { + return false; + } + + // Initialize zlib. + z_stream c_stream; // compression stream. + + c_stream.zalloc = Z_NULL; + c_stream.zfree = Z_NULL; + c_stream.opaque = Z_NULL; + + int ret = deflateInit(&c_stream, Z_BEST_COMPRESSION); + if (ret != Z_OK) + return false; + + // Allocate buffer to hold the deflated data. + const size_t BUFSIZE = 1048576; + Bytef* defbuf = new Bytef[BUFSIZE]; + if (!defbuf) + return false; + + c_stream.avail_in = static_cast<uInt>(buffer.size()); + c_stream.next_in = const_cast<bytes::value_type*>(&buffer[0]); + + // Compress until end of the buffer. + uLong totalout = 0; + bool bLoop = true; + while (bLoop) + { + c_stream.avail_out = BUFSIZE; + c_stream.next_out = defbuf; + + ret = deflate(&c_stream, Z_NO_FLUSH); // no bad return value + if (ret == Z_STREAM_END) + { + bLoop = false; + } + else if (ret == Z_BUF_ERROR && !c_stream.avail_in) + { + ret = deflate(&c_stream, Z_FINISH); // no bad return value + bLoop = false; + } + else if (ret != Z_OK) + { + deflateEnd(&c_stream); + delete [] defbuf; + return false; + } + + // Write the inflated data to the output file. + if (!ofs.write((const char*)defbuf, c_stream.total_out-totalout)) + { + deflateEnd(&c_stream); + delete [] defbuf; + return false; + } + + totalout = c_stream.total_out; + } + + // Clean up and return. + deflateEnd(&c_stream); + delete [] defbuf; + + return true; +} diff --git a/utils/zenutils/source/shared/utils.h b/utils/zenutils/source/shared/utils.h index ca9e291..694cb9c 100755..100644 --- a/utils/zenutils/source/shared/utils.h +++ b/utils/zenutils/source/shared/utils.h @@ -1,68 +1,68 @@ -/* zenutils - Utilities for working with creative firmwares.
- * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#ifndef SHARED_UTILS_H_INCLUDED
-#define SHARED_UTILS_H_INCLUDED
-
-#include <vector>
-#include <pelib/PeLib.h>
-
-#ifndef byte
-typedef PeLib::byte byte;
-#endif
-#ifndef word
-typedef PeLib::word word;
-#endif
-#ifndef dword
-typedef PeLib::dword dword;
-#endif
-
-namespace shared {
- typedef std::vector<byte> bytes;
-
- inline dword swap(dword val)
- {
- return ((val & 0xFF) << 24)
- | ((val & 0xFF00) << 8)
- | ((val & 0xFF0000) >> 8)
- | ((val & 0xFF000000) >> 24);
- }
-
- template <typename _Type>
- inline void reverse(_Type* start, _Type* end)
- {
- while (start < end)
- {
- *start ^= *end;
- *end ^= *start;
- *start ^= *end;
- start++;
- end--;
- }
- }
-
- std::string replace_extension(const std::string& filename, const std::string& extension);
- std::string remove_extension(const std::string& filename);
- std::string get_path(const std::string& filename);
- std::string double_quote(const std::string& str);
-
- bool inflate_to_file(const bytes& buffer, const char* filename);
- bool deflate_to_file(const bytes& buffer, const char* filename);
-}; //namespace shared
-
-#endif //SHARED_UTILS_H_INCLUDED
+/* zenutils - Utilities for working with creative firmwares. + * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef SHARED_UTILS_H_INCLUDED +#define SHARED_UTILS_H_INCLUDED + +#include <vector> +#include <pelib/PeLib.h> + +#ifndef byte +typedef PeLib::byte byte; +#endif +#ifndef word +typedef PeLib::word word; +#endif +#ifndef dword +typedef PeLib::dword dword; +#endif + +namespace shared { + typedef std::vector<byte> bytes; + + inline dword swap(dword val) + { + return ((val & 0xFF) << 24) + | ((val & 0xFF00) << 8) + | ((val & 0xFF0000) >> 8) + | ((val & 0xFF000000) >> 24); + } + + template <typename _Type> + inline void reverse(_Type* start, _Type* end) + { + while (start < end) + { + *start ^= *end; + *end ^= *start; + *start ^= *end; + start++; + end--; + } + } + + std::string replace_extension(const std::string& filename, const std::string& extension); + std::string remove_extension(const std::string& filename); + std::string get_path(const std::string& filename); + std::string double_quote(const std::string& str); + + bool inflate_to_file(const bytes& buffer, const char* filename); + bool deflate_to_file(const bytes& buffer, const char* filename); +}; //namespace shared + +#endif //SHARED_UTILS_H_INCLUDED |