Ticket #9027: svmp.patch
File svmp.patch, 7.0 KB (added by , 15 years ago) |
---|
-
extract_agos.cpp
24 24 #include <stdio.h> 25 25 #include <string.h> 26 26 27 #include "util.h" 28 27 29 typedef unsigned int ULONG; 28 30 typedef unsigned char UBYTE; 29 31 … … 142 144 * - call free() on ptr to free memory 143 145 * - size of loaded file is available in global var 'filelen' 144 146 */ 145 void *loadfile(c har *name) {147 void *loadfile(const char *name) { 146 148 void *mem = NULL; 147 149 FILE *fd; 148 150 … … 166 168 * the file named by [filename] 167 169 * - returns zero if failed, or non-zero if successful 168 170 */ 169 int savefile(c har *name, void *mem, size_t length) {171 int savefile(const char *name, void *mem, size_t length) { 170 172 unsigned int bytesWritten; 171 173 172 174 FILE *fd = fopen(name, "wb"); … … 184 186 return 1; 185 187 } 186 188 187 char filename[1024];188 189 189 int main(int argc, char *argv[]) { 190 int i; 190 int first_arg = 1; 191 int last_arg = argc; 191 192 192 if (argc < 2) { 193 printf("\nUsage: %s <file 1> ... <file n>\n", argv[0]); 194 exit(2); 195 } 193 Filename inpath, outpath; 196 194 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/"); 200 207 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 201 216 if (x) { 202 217 ULONG decrlen = simon_decr_length(x, (ULONG) filelen); 203 218 UBYTE *out = (UBYTE *) malloc(decrlen); 204 219 205 220 if (out) { 206 221 if (simon_decr(x, out, filelen)) { 207 strcat(filename, ".out"); 208 savefile(filename, out, decrlen); 222 savefile(outpath.getFullPath(), out, decrlen); 209 223 } 210 224 else { 211 printf("%s: decrunch error\n", filename);225 notice("%s: decrunch error\n", filename); 212 226 } 213 227 214 228 free((void *) x); 215 229 } 216 230 } 231 else { 232 notice("Could not load file %s\n", filename); 233 } 217 234 } 218 235 219 236 return 0; -
util.cpp
146 146 return sz; 147 147 } 148 148 149 void getPath(const char *fullpath, char *path) { 150 const char *p; 149 Filename::Filename(const char *path) { 150 strcpy(_path, path); 151 } 151 152 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, '\\'); 153 void Filename::setFullPath(const char *path) { 154 strcpy(_path, path); 155 } 159 156 160 if (!p) { 161 p = fullpath - 1; 162 } 157 Filename *Filename::setFullName(const char *newname) { 158 char p[1024]; 159 if (getPath(p)) { 160 strcat(p, newname); 161 strcpy(_path, p); 162 return this; 163 163 } 164 return NULL; 165 } 164 166 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'; 167 void Filename::addExtension(const char *ext) { 168 strcat(_path, ext); 169 } 170 171 bool Filename::empty() const { 172 return *_path == 0; 173 } 174 175 const char *Filename::getFullPath() const { 176 return _path; 177 } 178 179 const 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; 173 184 } 185 return NULL; 174 186 } 175 187 176 void getFilename(const char *fullpath, char *filename) { 177 const char *p; 188 const 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 } 178 195 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, '\\'); 196 const 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 } 186 208 187 if (!p) { 188 p = fullpath - 1; 209 void 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]); 189 213 } 214 else { 215 printf(msg, argv[0]); 216 } 217 exit(2); 190 218 } 219 } 191 220 192 strcpy(filename, p + 1); 221 bool 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; 193 241 } -
util.h
158 158 return (b[0] << 8) + b[1]; 159 159 } 160 160 FORCEINLINE uint32 READ_BE_UINT32(const void *ptr) { 161 const byte *b = (const byte *)ptr;161 const byte *b = (const byte *)ptr; 162 162 return (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + (b[3]); 163 163 } 164 164 FORCEINLINE void WRITE_BE_UINT16(void *ptr, uint16 value) { … … 204 204 void warning(const char *s, ...); 205 205 void debug(int level, const char *s, ...); 206 206 void notice(const char *s, ...); 207 void getPath(const char *fullpath, char *path);208 void getFilename(const char *fullpath, char *filename);209 207 208 struct 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 225 void parseHelpArguments(const char * const argv[], int argc, const char *msg = NULL); 226 bool parseOutputArguments(Filename *outputname, const char * const argv[], int argc, int start_arg); 227 228 210 229 #endif