ripole.c

Go to the documentation of this file.
00001 
00002 /* Microsoft OLE2 stream parser.*/
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   Function Name : set_defaults
00030   Returns Type  : int
00031   ----Parameter List
00032   1. struct ripOLE_object *role , 
00033   ------------------
00034   Exit Codes    : 
00035   Side Effects  : 
00036   --------------------------------------------------------------------
00037 Comments:
00038 
00039 --------------------------------------------------------------------
00040 Changes:
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   Function Name : parse_parameters
00056   Returns Type  : int
00057   ----Parameter List
00058   1. struct ripOLE_object *role, 
00059   2.  int argc, 
00060   3.  char **argv , 
00061   ------------------
00062   Exit Codes    : 
00063   Side Effects  : 
00064   --------------------------------------------------------------------
00065 Comments:
00066 
00067 --------------------------------------------------------------------
00068 Changes:
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                                         /* if we get ANOTHER - symbol, then we have an extended flag*/
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                                         /* else, just dump out the help message*/
00136 
00137                                 default:
00138                                         fprintf(stdout,"Cannot interpret option \"%s\"\n%s\n", argv[i], help);
00139                                         exit (1);
00140                                         break;
00141 
00142                         }                       /* Switch argv[i][1]*/
00143 
00144                 }                       /* if argv[i][0] == -*/
00145 
00146         }                               /* for*/
00147 
00148         return result;
00149 }
00150 
00151 /*-----------------------------------------------------------------\
00152   Function Name : set_parameters
00153   Returns Type  : int
00154   ----Parameter List
00155   1. struct ripOLE_object *role, 
00156   2.  struct OLE_object *ole , 
00157   ------------------
00158   Exit Codes    : 
00159   Side Effects  : 
00160   --------------------------------------------------------------------
00161 Comments:
00162 
00163 --------------------------------------------------------------------
00164 Changes:
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   Function Name : ripOLE_report_filename_decoded
00190   Returns Type  : int
00191   ----Parameter List
00192   1. char *filename, 
00193   ------------------
00194   Exit Codes    : 
00195   Side Effects  : 
00196   --------------------------------------------------------------------
00197 Comments:
00198 
00199 --------------------------------------------------------------------
00200 Changes:
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   Function Name : ROLE_init
00214   Returns Type  : int
00215   ----Parameter List
00216   1. struct ripOLE_object *role, 
00217   ------------------
00218   Exit Codes    : 
00219   Side Effects  : 
00220   --------------------------------------------------------------------
00221 Comments:
00222 
00223 --------------------------------------------------------------------
00224 Changes:
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   Function Name : ROLE_done
00242   Returns Type  : int
00243   ----Parameter List
00244   1. struct ripOLE_object *role, 
00245   ------------------
00246   Exit Codes    : 
00247   Side Effects  : 
00248   --------------------------------------------------------------------
00249 Comments:
00250 
00251 --------------------------------------------------------------------
00252 Changes:
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   Function Name : ROLE_validate
00266   Returns Type  : int
00267   ----Parameter List
00268   1. struct ripOLE_object *role , 
00269   ------------------
00270   Exit Codes    : 
00271   Side Effects  : 
00272   --------------------------------------------------------------------
00273 Comments:
00274 
00275 --------------------------------------------------------------------
00276 Changes:
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   Function Name : main
00293   Returns Type  : int
00294   ----Parameter List
00295   1. int argc, 
00296   2.  char **argv , 
00297   ------------------
00298   Exit Codes    : 
00299   Side Effects  : 
00300   --------------------------------------------------------------------
00301 Comments:
00302 
00303 --------------------------------------------------------------------
00304 Changes:
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   /*struct OLE_object *ole = NULL;*/
00372   struct OLE_object ole;
00373        int result = 0;
00374 
00375        /*ole = MALLOC(sizeof(struct OLE_object));
00376        if (ole == NULL)
00377        {
00378                LOGGER_log("ripOLE: Cannot allocate memory for OLE object");
00379                return 1;
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        /*ole->save_unknown_streams==2 added for Scilab to extract onlyexcel streams
00388          see  OLE_decode_stream in ole.c file */
00389        OLE_set_save_unknown_streams(&ole, 2); /* get only excel streams */
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        /*if (ole != NULL) FREE(ole);*/
00401        return result;
00402 }

Generated on Sun Mar 4 15:03:49 2007 for Scilab [trunk] by  doxygen 1.5.1