File: ripole.c

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (378 lines) | stat: -rw-r--r-- 9,195 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378

/* Microsoft OLE2 stream parser.*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "logger.h"
#include "pldstr.h"
#include "ole.h"
#undef WITHMAIN

#ifdef WIN32
#include "../os_specific/win_mem_alloc.h" /* MALLOC */
#else
#include "../os_specific/sci_mem_alloc.h" /* MALLOC */
#endif


struct ripOLE_object {
	int debug;
	int verbose;
	int save_unknown_streams;

	char *inputfile;
	char *outputdir;
};


static char defaultdir[]=".";
static char version[]="0.1.4 - 27-November-2004 (C) PLDaniels http://www.pldaniels.com/ripole";
static char help[]="ripOLE -i <OLE2 file> [ -d <directory> ] [--save-unknown-streams] [--version|-V] [--verbose|-v] [--debug] [--help|-h]";

/*-----------------------------------------------------------------\
 Function Name	: set_defaults
 Returns Type	: int
 	----Parameter List
	1. struct ripOLE_object *role , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int set_defaults( struct ripOLE_object *role )
{
	role->outputdir = defaultdir;
	role->debug = 0;
	role->verbose = 0;
	role->save_unknown_streams = 0;
	role->inputfile = NULL;

	return 0;
}

/*-----------------------------------------------------------------\
 Function Name	: parse_parameters
 Returns Type	: int
 	----Parameter List
	1. struct ripOLE_object *role, 
	2.  int argc, 
	3.  char **argv , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int parse_parameters( struct ripOLE_object *role, int argc, char **argv )
{
	int i;
	int result = 0;

	role->outputdir = defaultdir;

	for (i = 1; i < argc; i++)
	{
		if (argv[i][0] == '-')
		{
			switch (argv[i][1])
			{
				case 'i':
					i++;
					role->inputfile = strdup(argv[i]);
					break;

				case 'd':
					i++;
					role->outputdir = strdup(argv[i]);
					break;

				case 'v':
					role->verbose = 1;
					break;

				case 'V':
					fprintf (stdout, "%s\n", version);
					exit (1);
					break;
				case 'h':
					fprintf (stdout, "%s\n", help);
					exit (1);
					break;

					/*  if we get ANOTHER - symbol, then we have an extended flag */

				case '-':
						if (strncmp (&(argv[i][2]), "verbose", 7) == 0)
						{
							role->verbose=1;
							
						} else if (strncmp (&(argv[i][2]), "save-unknown-streams", 20) == 0) {
							role->save_unknown_streams = 1;
						
						} else if (strncmp (&(argv[i][2]), "debug", 5) == 0) {
							role->debug=1;
							
						} else if (strncmp (&(argv[i][2]), "version", 7) == 0) {
							fprintf (stdout, "%s\n", version);
							exit (1);
							
						} else if (strncmp(&(argv[i][2]),"help",4)==0) {
							fprintf(stdout,"%s\n",help);
							exit(1);

						} else {
							fprintf(stdout,"Cannot interpret option \"%s\"\n%s\n", argv[i], help);
							exit (1);
							break;
						}
					break;

					/*  else, just dump out the help message */

				default:
					fprintf(stdout,"Cannot interpret option \"%s\"\n%s\n", argv[i], help);
					exit (1);
					break;

			}			/* Switch argv[i][1] */

		}			/*  if argv[i][0] == -*/

	}				/*  for */

	return result;
}

/*-----------------------------------------------------------------\
 Function Name	: set_parameters
 Returns Type	: int
 	----Parameter List
	1. struct ripOLE_object *role, 
	2.  struct OLE_object *ole , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int set_parameters( struct ripOLE_object *role, struct OLE_object *ole )
{
	if(role->debug == 1)
	{
		OLE_set_debug(ole, OLE_DEBUG_NORMAL);
	} 

	if (role->verbose == 1)
	{
		OLE_set_verbose(ole, OLE_VERBOSE_NORMAL);
	}

	if (role->save_unknown_streams == 1)
	{
		OLE_set_save_unknown_streams(ole, 1);
	}

	return 0;
}


/*-----------------------------------------------------------------\
 Function Name	: ripOLE_report_filename_decoded
 Returns Type	: int
 	----Parameter List
	1. char *filename, 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int ripOLE_report_filename_decoded(char *filename)
{
	LOGGER_log("Decoding filename=%s", filename);

	return 0;
}
	 


/*-----------------------------------------------------------------\
 Function Name	: ROLE_init
 Returns Type	: int
 	----Parameter List
	1. struct ripOLE_object *role, 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int ROLE_init(struct ripOLE_object *role)
{
	role->debug = 0;
	role->verbose = 0;
	role->save_unknown_streams = 0;

	role->inputfile = NULL;
	role->outputdir = NULL;

	return 0;
}


/*-----------------------------------------------------------------\
 Function Name	: ROLE_done
 Returns Type	: int
 	----Parameter List
	1. struct ripOLE_object *role, 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/
int ROLE_done(struct ripOLE_object *role)
{
	if (role->inputfile != NULL) FREE(role->inputfile);
	if (role->outputdir != NULL) FREE(role->outputdir);

	return 0;
}

#ifdef WITHMAIN
/*-----------------------------------------------------------------\
 Function Name	: main
 Returns Type	: int
 	----Parameter List
	1. int argc, 
	2.  char **argv , 
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments:
 
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/

int main( int argc, char **argv )
{
	struct ripOLE_object role;
	struct OLE_object *ole = NULL;
	int result = 0;

	if (argc == 1) { fprintf (stdout, "%s\n", help); exit(1); }

	ole = MALLOC(sizeof(struct OLE_object));
	if (ole == NULL)
	{
		LOGGER_log("ripOLE: Cannot allocate memory for OLE object");
		return 1;
	}

	LOGGER_set_output_mode(_LOGGER_STDOUT);

	OLE_init(ole);
	ROLE_init(&role);

	set_defaults( &role );
	parse_parameters(&role, argc, argv);
	set_parameters(&role, ole);

	OLE_set_filename_report_fn(ole, ripOLE_report_filename_decoded );

	result = OLE_decode_file( ole, role.inputfile, role.outputdir );
	OLE_decode_file_done(ole);

	if ((result != 0)&&(role.verbose)) LOGGER_log("ripOLE: decoding of %s resulted in error %d\n", role.inputfile, result );

	if (ole != NULL) FREE(ole);
	ROLE_done(&role);
	
	return result;
}
#endif
/*-----------------------------------------------------------------\
 Function Name	: ripole
 Returns Type	: int
 	----Parameter List
	1. char *inputfile
	2. char *outputdir
 	------------------
 Exit Codes	: 
 Side Effects	: 
--------------------------------------------------------------------
 Comments: This function has been added to the original distribution
           for Scilab. It uses a particular value (2)
           for ole->save_unknown_streams to ask only for excel stream
           if it exist the excel stream is saved under the fullpath 
           given by outfile. A slight modification has been done in 
           OLE_decode_file to handle this case see comments tagged Scilab.
--------------------------------------------------------------------
 Changes:
 
\------------------------------------------------------------------*/

int ripole(char *inputfile, char *outputdir, int debug, int verbose)
{
  /*struct OLE_object *ole = NULL;*/
  struct OLE_object ole;
	int result = 0;

	/*ole = MALLOC(sizeof(struct OLE_object));
	if (ole == NULL)
	{
		LOGGER_log("ripOLE: Cannot allocate memory for OLE object");
		return 1;
	}*/

	LOGGER_set_output_mode(_LOGGER_STDOUT);

	OLE_init(&ole);
	if (debug ==1 ) OLE_set_debug(&ole, OLE_DEBUG_NORMAL);
	if (verbose == 1) OLE_set_verbose(&ole, OLE_VERBOSE_NORMAL);

	OLE_set_save_unknown_streams(&ole, 2); /* get only excel streams */

	OLE_set_filename_report_fn(&ole, ripOLE_report_filename_decoded );

	result = OLE_decode_file( &ole, inputfile, outputdir);
	OLE_decode_file_done(&ole);


	if ((result != 0)&&(verbose==1)) 
	  LOGGER_log("ripOLE: decoding of %s resulted in error %d\n", inputfile, result );

	/*if (ole != NULL) FREE(ole);*/
	return result;
}