00001
00002
00003
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <ctype.h>
00008
00009 #include "logger.h"
00010 #include "pldstr.h"
00011 #include "ole.h"
00012 #include "ripole.h"
00013
00014 #undef WITHMAIN
00015
00016 #include "MALLOC.h"
00017
00018 #ifdef _MSC_VER
00019 #define strdup _strdup
00020 #endif
00021
00022 #define ROLE_VERSION "0.2.0"
00023
00024 static char defaultdir[]=".";
00025 static char version[]="0.2.0 - 12-December-2005 (C) PLDaniels http:/*www.pldaniels.com/ripole";
00026 static char help[]="ripOLE -i <OLE2 file> [ -d <directory> ] [--save-unknown-streams] [--version|-V] [--verbose|-v] [--debug] [--help|-h]";
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 int ROLE_set_defaults( struct ripOLE_object *role )
00044 {
00045 role->outputdir = defaultdir;
00046 role->debug = 0;
00047 role->verbose = 0;
00048 role->save_unknown_streams = 0;
00049 role->inputfile = NULL;
00050
00051 return 0;
00052 }
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 int ROLE_parse_parameters( struct ripOLE_object *role, int argc, char **argv )
00072 {
00073 int i;
00074 int result = 0;
00075
00076 role->outputdir = defaultdir;
00077
00078 for (i = 1; i < argc; i++)
00079 {
00080 if (argv[i][0] == '-')
00081 {
00082 switch (argv[i][1])
00083 {
00084 case 'i':
00085 i++;
00086 role->inputfile = strdup(argv[i]);
00087 break;
00088
00089 case 'd':
00090 i++;
00091 role->outputdir = strdup(argv[i]);
00092 break;
00093
00094 case 'v':
00095 role->verbose = 1;
00096 break;
00097
00098 case 'V':
00099 fprintf (stdout, "%s\n", version);
00100 exit (1);
00101 break;
00102 case 'h':
00103 fprintf (stdout, "%s\n", help);
00104 exit (1);
00105 break;
00106
00107
00108
00109 case '-':
00110 if (strncmp (&(argv[i][2]), "verbose", 7) == 0)
00111 {
00112 role->verbose=1;
00113
00114 } else if (strncmp (&(argv[i][2]), "save-unknown-streams", 20) == 0) {
00115 role->save_unknown_streams = 1;
00116
00117 } else if (strncmp (&(argv[i][2]), "debug", 5) == 0) {
00118 role->debug=1;
00119
00120 } else if (strncmp (&(argv[i][2]), "version", 7) == 0) {
00121 fprintf (stdout, "%s\n", version);
00122 exit (1);
00123
00124 } else if (strncmp(&(argv[i][2]),"help",4)==0) {
00125 fprintf(stdout,"%s\n",help);
00126 exit(1);
00127
00128 } else {
00129 fprintf(stdout,"Cannot interpret option \"%s\"\n%s\n", argv[i], help);
00130 exit (1);
00131 break;
00132 }
00133 break;
00134
00135
00136
00137 default:
00138 fprintf(stdout,"Cannot interpret option \"%s\"\n%s\n", argv[i], help);
00139 exit (1);
00140 break;
00141
00142 }
00143
00144 }
00145
00146 }
00147
00148 return result;
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 int ROLE_set_parameters( struct ripOLE_object *role, struct OLE_object *ole )
00168 {
00169 if(role->debug == 1)
00170 {
00171 OLE_set_debug(ole, OLE_DEBUG_NORMAL);
00172 }
00173
00174 if (role->verbose == 1)
00175 {
00176 OLE_set_verbose(ole, OLE_VERBOSE_NORMAL);
00177 }
00178
00179 if (role->save_unknown_streams == 1)
00180 {
00181 OLE_set_save_unknown_streams(ole, 1);
00182 }
00183
00184 return 0;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 int ROLE_report_filename_decoded(char *filename)
00204 {
00205 LOGGER_log("Decoding filename=%s", filename);
00206
00207 return 0;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 int ROLE_init(struct ripOLE_object *role)
00228 {
00229 role->debug = 0;
00230 role->verbose = 0;
00231 role->save_unknown_streams = 0;
00232
00233 role->inputfile = NULL;
00234 role->outputdir = NULL;
00235
00236 return 0;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 int ROLE_done(struct ripOLE_object *role)
00256 {
00257 if (role->inputfile != NULL) FREE(role->inputfile);
00258 if (role->outputdir != NULL) FREE(role->outputdir);
00259
00260 return 0;
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 int ROLE_validate(struct ripOLE_object *role )
00280 {
00281 int result = 0;
00282
00283 if (role->inputfile == NULL) {
00284 fprintf(stderr,"ripOLE requires an input file to decode\n");
00285 return -1;
00286 }
00287
00288 return result;
00289 }
00290 #ifdef WITHMAIN
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 int main( int argc, char **argv )
00308 {
00309 struct ripOLE_object role;
00310 struct OLE_object *ole = NULL;
00311 int result = 0;
00312
00313 if (argc == 1) { fprintf (stdout, "%s\n", help); exit(1); }
00314
00315 ole = MALLOC(sizeof(struct OLE_object));
00316 if (ole == NULL)
00317 {
00318 LOGGER_log("ripOLE: Cannot allocate memory for OLE object");
00319 return 1;
00320 }
00321
00322
00323 LOGGER_set_output_mode(_LOGGER_STDOUT);
00324
00325 OLE_init(ole);
00326 ROLE_init(&role);
00327
00328 ROLE_set_defaults( &role );
00329 ROLE_parse_parameters(&role, argc, argv);
00330 result = ROLE_validate(&role);
00331 if (result != 0) return result;
00332
00333 ROLE_set_parameters(&role, ole);
00334
00335 OLE_set_filename_report_fn(ole, ROLE_report_filename_decoded );
00336
00337 result = OLE_decode_file( ole, role.inputfile, role.outputdir );
00338 OLE_decode_file_done(ole);
00339
00340 if ((result != 0)) {
00341 if (role.verbose) {
00342 switch (result) {
00343 case OLEER_NO_INPUT_FILE:
00344 case OLEER_BAD_INPUT_FILE:
00345 LOGGER_log("Cannot locate input file '%s'",role.inputfile);
00346 break;
00347 case OLEER_NOT_OLE_FILE:
00348 LOGGER_log("File '%s' is not OLE2 format",role.inputfile);
00349 break;
00350 case OLEER_INSANE_OLE_FILE:
00351 LOGGER_log("OLE input file '%s' is insane", role.inputfile);
00352 break;
00353 default:
00354 LOGGER_log("ripOLE: decoding of %s resulted in error %d\n", role.inputfile, result );
00355 }
00356 }
00357 return result;
00358 }
00359
00360 if (ole != NULL) FREE(ole);
00361 ROLE_done(&role);
00362
00363 return result;
00364 }
00365
00366 #endif
00367
00369 int ripole(char *inputfile, char *outputdir, int debug, int verbose)
00370 {
00371
00372 struct OLE_object ole;
00373 int result = 0;
00374
00375
00376
00377
00378
00379
00380
00381
00382 LOGGER_set_output_mode(_LOGGER_STDOUT);
00383
00384 OLE_init(&ole);
00385 if (debug ==1 ) OLE_set_debug(&ole, OLE_DEBUG_NORMAL);
00386 if (verbose == 1) OLE_set_verbose(&ole, OLE_VERBOSE_NORMAL);
00387
00388
00389 OLE_set_save_unknown_streams(&ole, 2);
00390
00391 OLE_set_filename_report_fn(&ole, ROLE_report_filename_decoded );
00392
00393 result = OLE_decode_file( &ole, inputfile, outputdir);
00394 OLE_decode_file_done(&ole);
00395
00396
00397 if ((result != 0)&&(verbose==1))
00398 LOGGER_log("ripOLE: decoding of %s resulted in error %d\n", inputfile, result );
00399
00400
00401 return result;
00402 }