Ticket #9027: svmp.patch

File svmp.patch, 7.0 KB (added by SF/remere, 15 years ago)

The .patch file

  • extract_agos.cpp

     
    2424#include <stdio.h>
    2525#include <string.h>
    2626
     27#include "util.h"
     28
    2729typedef unsigned int ULONG;
    2830typedef unsigned char UBYTE;
    2931
     
    142144 * - call free() on ptr to free memory
    143145 * - size of loaded file is available in global var 'filelen'
    144146 */
    145 void *loadfile(char *name) {
     147void *loadfile(const char *name) {
    146148        void *mem = NULL;
    147149        FILE *fd;
    148150
     
    166168 *   the file named by [filename]
    167169 * - returns zero if failed, or non-zero if successful
    168170 */
    169 int savefile(char *name, void *mem, size_t length) {
     171int savefile(const char *name, void *mem, size_t length) {
    170172        unsigned int bytesWritten;
    171173
    172174        FILE *fd = fopen(name, "wb");
     
    184186        return 1;
    185187}
    186188
    187 char filename[1024];
    188 
    189189int main(int argc, char *argv[]) {
    190         int i;
     190        int first_arg = 1;
     191        int last_arg = argc;
    191192
    192         if (argc < 2) {
    193                 printf("\nUsage: %s <file 1> ... <file n>\n", argv[0]);
    194                 exit(2);
    195         }
     193        Filename inpath, outpath;
    196194
    197         for (i = 1; i < argc; i++) {
    198                 UBYTE *x = (UBYTE *) loadfile(argv[i]);
    199                 strcpy(filename, argv[i]);
     195        // Check if we should display some heplful text
     196        parseHelpArguments(argv, argc);
     197       
     198        // Continuing with finding out output directory
     199        // also make sure we skip those arguments
     200        if ( parseOutputArguments(&outpath, argv, argc, first_arg))
     201                first_arg += 2;
     202        else if (parseOutputArguments(&outpath, argv, argc, last_arg - 2))
     203                last_arg -= 2;
     204        else
     205                // Standard output dir
     206                outpath.setFullPath("out/");
    200207
     208        // Loop through all input files
     209        for (int parsed_args = first_arg; parsed_args <= last_arg; ++parsed_args) {
     210                const char *filename = argv[parsed_args];
     211                UBYTE *x = (UBYTE *) loadfile(filename);
     212
     213                inpath.setFullPath(filename);
     214                outpath.setFullName(inpath.getFullName());
     215
    201216                if (x) {
    202217                        ULONG decrlen = simon_decr_length(x, (ULONG) filelen);
    203218                        UBYTE *out = (UBYTE *) malloc(decrlen);
    204219
    205220                        if (out) {
    206221                                if (simon_decr(x, out, filelen)) {
    207                                         strcat(filename, ".out");
    208                                         savefile(filename, out, decrlen);
     222                                        savefile(outpath.getFullPath(), out, decrlen);
    209223                                }
    210224                                else {
    211                                         printf("%s: decrunch error\n", filename);
     225                                        notice("%s: decrunch error\n", filename);
    212226                                }
    213227
    214228                                free((void *) x);
    215229                        }
    216230                }
     231                else {
     232                        notice("Could not load file %s\n", filename);
     233                }
    217234        }
    218235
    219236        return 0;
  • util.cpp

     
    146146        return sz;
    147147}
    148148
    149 void getPath(const char *fullpath, char *path) {
    150         const char *p;
     149Filename::Filename(const char *path) {
     150        strcpy(_path, path);
     151}
    151152
    152         /* Find the last occurence of '/' or '\'
    153          * Everything before this point is the path
    154          * Everything after this point is the filename
    155          */
    156         p = strrchr(fullpath, '/');
    157         if (!p) {
    158                 p = strrchr(fullpath, '\\');
     153void Filename::setFullPath(const char *path) {
     154        strcpy(_path, path);
     155}
    159156
    160                 if (!p) {
    161                         p = fullpath - 1;
    162                 }
     157Filename *Filename::setFullName(const char *newname) {
     158        char p[1024];
     159        if (getPath(p)) {
     160                strcat(p, newname);
     161                strcpy(_path, p);
     162                return this;
    163163        }
     164        return NULL;
     165}
    164166
    165         /* The path is everything before p, unless the file is in the current
    166          * directory, in which case the path is '.'
    167          */
    168         if (p < fullpath) {
    169                 strcpy(path, ".");
    170         } else {
    171                 strncpy(path, fullpath, p - fullpath);
    172                 path[strlen(fullpath) - strlen(p)] = '\0';
     167void Filename::addExtension(const char *ext) {
     168        strcat(_path, ext);
     169}
     170
     171bool Filename::empty() const {
     172        return *_path == 0;
     173}
     174
     175const char *Filename::getFullPath() const {
     176        return _path;
     177}
     178
     179const char *Filename::getFullName(char *out) const {
     180        const char *slash;
     181        if ((slash = strrchr(_path, '/')) || (slash = strrchr(_path, '\\'))) {
     182                strcpy(out, slash + 1);
     183                return out;
    173184        }
     185        return NULL;
    174186}
    175187
    176 void getFilename(const char *fullpath, char *filename) {
    177         const char *p;
     188const char *Filename::getFullName() const {
     189        const char *slash;
     190        if ((slash = strrchr(_path, '/')) || (slash = strrchr(_path, '\\'))) {
     191                return slash + 1;
     192        }
     193        return NULL;
     194}
    178195
    179         /* Find the last occurence of '/' or '\'
    180          * Everything before this point is the path
    181          * Everything after this point is the filename
    182          */
    183         p = strrchr(fullpath, '/');
    184         if (!p) {
    185                 p = strrchr(fullpath, '\\');
     196const char *Filename::getPath(char *out) const {
     197        const char *slash;
     198        if ((slash = strrchr(_path, '/')) || (slash = strrchr(_path, '\\'))) {
     199                int end = strlen(_path) - strlen(slash) + 1;
     200                strncpy(out, _path, end);
     201                out[end] = 0;
     202                return out;
     203        }
     204        // If there was no '/', this was a relative path
     205        strcpy(out, _path);
     206        return out;
     207}
    186208
    187                 if (!p) {
    188                         p = fullpath - 1;
     209void parseHelpArguments(const char * const argv[], int argc, const char *msg) {
     210        if (argc < 2 || strcmp(argv[1], "--help") == 0 || stricmp(argv[1], "-h") == 0) {
     211                if (!msg) {
     212                        printf("\nUsage: %s [-o <output dir> = out/] <file 1> ... <file n>\n", argv[0]);
    189213                }
     214                else {
     215                        printf(msg, argv[0]);
     216                }
     217                exit(2);
    190218        }
     219}
    191220
    192         strcpy(filename, p + 1);
     221bool parseOutputArguments(Filename *outputname, const char * const argv[], int argc, int start_arg) {
     222        char lastchr;
     223
     224        if (start_arg >= 0 && (strcmp(argv[start_arg], "-o") == 0 || strcmp(argv[start_arg], "--output") == 0)) {
     225                /* It's a -o argument, can we check next arg? */
     226
     227                if (start_arg + 1 < argc) {
     228                        outputname->setFullPath(argv[start_arg + 1]);
     229
     230                        /* Ensure last character is a /, this way we force directory output */
     231                        lastchr = outputname->getFullPath()[strlen(outputname->getFullPath()) - 1];
     232                        if (lastchr != '/' && lastchr != '\\') {
     233                                strcat(outputname->_path, "/");
     234                        }
     235                        return true;
     236                } else {
     237                        error("Expected directory path after '-o' or '--output'.");
     238                }
     239        }
     240        return false;
    193241}
  • util.h

     
    158158        return (b[0] << 8) + b[1];
    159159}
    160160FORCEINLINE uint32 READ_BE_UINT32(const void *ptr) {
    161         const byte *b = (const byte*)ptr;
     161        const byte *b = (const byte *)ptr;
    162162        return (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + (b[3]);
    163163}
    164164FORCEINLINE void WRITE_BE_UINT16(void *ptr, uint16 value) {
     
    204204void warning(const char *s, ...);
    205205void debug(int level, const char *s, ...);
    206206void notice(const char *s, ...);
    207 void getPath(const char *fullpath, char *path);
    208 void getFilename(const char *fullpath, char *filename);
    209207
     208struct Filename {
     209        char _path[1024];
     210
     211        Filename(const char *path = "");
     212
     213        void setFullPath(const char *path);
     214        Filename *setFullName(const char *name);
     215        void addExtension(const char *ext);
     216
     217        bool empty() const;
     218
     219        const char *getFullPath() const;
     220        const char *getFullName() const;
     221        const char *getFullName(char *out) const;
     222        const char *getPath(char *out) const;
     223};
     224
     225void parseHelpArguments(const char * const argv[], int argc, const char *msg = NULL);
     226bool parseOutputArguments(Filename *outputname, const char * const argv[], int argc, int start_arg);
     227
     228
    210229#endif