1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * test-dump-msole.c: Export a msole file to a directory tree
4  *
5  * Copyright (C) 2002-2006	Jody Goldberg (jody@gnome.org)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2.1 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA
20  */
21 
22 #include <gsf/gsf-utils.h>
23 
24 #include <gsf/gsf-input-stdio.h>
25 #include <gsf/gsf-infile.h>
26 #include <gsf/gsf-infile-msole.h>
27 
28 #include <gsf/gsf-output-stdio.h>
29 #include <gsf/gsf-outfile.h>
30 #include <gsf/gsf-outfile-stdio.h>
31 
32 #include <stdio.h>
33 
34 static void
clone_(GsfInput * input,GsfOutput * output)35 clone_ (GsfInput *input, GsfOutput *output)
36 {
37 	guint8 const *data;
38 	size_t len;
39 	int i;
40 
41 	if (gsf_input_size (input) > 0) {
42 		while ((len = gsf_input_remaining (input)) > 0) {
43 			/* copy in odd sized chunks to exercise system */
44 			if (len > 314)
45 				len = 314;
46 			if (NULL == (data = gsf_input_read (input, len, NULL))) {
47 				g_warning ("error reading ?");
48 				return;
49 			}
50 			if (!gsf_output_write (output, len, data)) {
51 				g_warning ("error writing ?");
52 				return;
53 			}
54 		}
55 	}
56 
57 	/* See test-cp-msole.c for explanation how to distinct directories
58 	 * from regular files.
59 	 */
60 	if (GSF_IS_INFILE (input) &&
61 	    gsf_infile_num_children (GSF_INFILE (input)) > 0) {
62 		GsfInfile *in = GSF_INFILE (input);
63 		GsfOutfile *out = GSF_OUTFILE (output);
64 		GsfInput *src;
65 		GsfOutput *dst;
66 		gboolean is_dir;
67 
68 		for (i = 0 ; i < gsf_infile_num_children (in) ; i++) {
69 			src = gsf_infile_child_by_index (in, i);
70 			is_dir = GSF_IS_INFILE (src) &&
71 				gsf_infile_num_children (GSF_INFILE (src)) >= 0;
72 			dst = gsf_outfile_new_child  (out,
73 				gsf_infile_name_by_index  (in, i),
74 				is_dir);
75 			clone_ (src, dst);
76 		}
77 	}
78 
79 	gsf_output_close (output);
80 	g_object_unref (G_OBJECT (output));
81 	g_object_unref (G_OBJECT (input));
82 }
83 
84 static int
test(char * argv[])85 test (char *argv[])
86 {
87 	GsfInput   *input;
88 	GsfInfile  *infile;
89 	GsfOutfile *outfile;
90 	GError    *err = NULL;
91 
92 	fprintf (stderr, "%s\n", argv [1]);
93 	input = gsf_input_stdio_new (argv[1], &err);
94 	if (input == NULL) {
95 		g_return_val_if_fail (err != NULL, 1);
96 
97 		g_warning ("'%s' error: %s", argv[1], err->message);
98 		g_error_free (err);
99 		return 1;
100 	}
101 
102 	infile = gsf_infile_msole_new (input, &err);
103 	g_object_unref (G_OBJECT (input));
104 
105 	if (infile == NULL) {
106 		g_return_val_if_fail (err != NULL, 1);
107 
108 		g_warning ("'%s' Not an OLE file: %s", argv[1], err->message);
109 		g_error_free (err);
110 		return 1;
111 	}
112 
113 	outfile = gsf_outfile_stdio_new (argv[2], &err);
114 	if (outfile == NULL) {
115 		g_return_val_if_fail (err != NULL, 1);
116 
117 		g_warning ("'%s' error: %s", argv[1], err->message);
118 		g_error_free (err);
119 		return 1;
120 	}
121 	clone_ (GSF_INPUT (infile), GSF_OUTPUT (outfile));
122 
123 	return 0;
124 }
125 
126 int
main(int argc,char * argv[])127 main (int argc, char *argv[])
128 {
129 	int res;
130 
131 	if (argc != 3) {
132 		fprintf (stderr, "%s : infile outdir\n", argv [0]);
133 		return 1;
134 	}
135 
136 	gsf_init ();
137 	res = test (argv);
138 	gsf_shutdown ();
139 
140 	return res;
141 }
142