1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * test-msole2.c: test program to list content and dump raw stream data
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-input-stdio.h>
23 #include <gsf/gsf-utils.h>
24 #include <gsf/gsf-infile.h>
25 #include <gsf/gsf-infile-msole.h>
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 
32 static gboolean dump_as_hex = FALSE;
33 
34 static void
ls_R(GsfInput * input)35 ls_R (GsfInput *input)
36 {
37 	char const *name = gsf_input_name (input);
38 	gboolean is_dir = GSF_IS_INFILE (input) &&
39 		(gsf_infile_num_children (GSF_INFILE (input)) >= 0);
40 	/* Please see the comment on is_dir in test-cp-msole.c. */
41 
42 	g_print ("%c '%s'\t\t%" GSF_OFF_T_FORMAT "\n",
43 		(is_dir ? 'd' : ' '),
44 		(name != NULL) ? name : "",
45 		gsf_input_size (input));
46 
47 	if (is_dir) {
48 		GsfInfile *infile = GSF_INFILE (input);
49 		int i;
50 
51 		g_print ("{");
52 		for (i = 0 ; i < gsf_infile_num_children (infile) ; i++)
53 			ls_R (gsf_infile_child_by_index (infile, i));
54 		g_print ("}");
55 	}
56 
57 	g_object_unref (G_OBJECT (input));
58 }
59 
60 static int
test(int argc,char * argv[])61 test (int argc, char *argv[])
62 {
63 	GsfInput  *input;
64 	GsfInfile *infile;
65 	GError    *err = NULL;
66 
67 	g_printerr ("%s\n", argv [1]);
68 	input = gsf_input_stdio_new (argv[1], &err);
69 	if (input == NULL) {
70 
71 		g_return_val_if_fail (err != NULL, 1);
72 
73 		g_warning ("'%s' error: %s", argv[1], err->message);
74 		g_error_free (err);
75 		return 1;
76 	}
77 
78 	input = gsf_input_uncompress (input);
79 	infile = gsf_infile_msole_new (input, &err);
80 	g_object_unref (G_OBJECT (input));
81 
82 	if (infile == NULL) {
83 		g_return_val_if_fail (err != NULL, 1);
84 
85 		g_warning ("'%s' Not an OLE file: %s", argv[1], err->message);
86 		g_error_free (err);
87 		return 1;
88 	}
89 
90 	if (argc > 2) {
91 		int i;
92 		GsfInput *child, *ptr = GSF_INPUT (infile);
93 
94 		for (i = 2 ; i < argc && ptr != NULL; i++, ptr = child) {
95 			g_printerr ("--> '%s'\n", argv [i]);
96 			if (GSF_IS_INFILE (ptr) &&
97 			    gsf_infile_num_children (GSF_INFILE (ptr)) >= 0) {
98 				child = gsf_infile_child_by_name (GSF_INFILE (ptr), argv [i]);
99 				if (child == NULL) {
100 					g_warning ("No child named '%s'", argv [i]);
101 					child = NULL;
102 				}
103 			} else {
104 				g_warning ("stream is not a directory '%s'", argv [i]);
105 				child = NULL;
106 			}
107 			g_object_unref (G_OBJECT (ptr));
108 		}
109 		if (ptr != NULL) {
110 			/* See the comment on is_dir in test-cp-msole.c. */
111 			if (GSF_IS_INFILE (ptr) &&
112 			    gsf_infile_num_children (GSF_INFILE (ptr)) >= 0)
113 				ls_R (ptr); /* unrefs infile */
114 			else {
115 				gsf_input_dump (GSF_INPUT (ptr), dump_as_hex);
116 				g_object_unref (G_OBJECT (ptr));
117 			}
118 		}
119 	} else
120 		ls_R (GSF_INPUT (infile)); /* unrefs infile */
121 
122 	return 0;
123 }
124 
125 int
main(int argc,char * argv[])126 main (int argc, char *argv[])
127 {
128 	int res;
129 	char *progname = argv[0];
130 
131 	if (argc >= 2 && argv[1] != NULL && 0 == strcmp (argv[1], "--hex")) {
132 		dump_as_hex = TRUE;
133 		argv++;
134 		argc--;
135 	}
136 
137 	if (argc < 2) {
138 		g_printerr ("Usage: %s [--hex] file stream stream ...\n\n", progname);
139 		g_printerr ("For example, \"%s X A B C\" dumps the streem C "
140 			"from directory A/B from file X.\n", progname);
141 		return 1;
142 	}
143 
144 	gsf_init ();
145 	res = test (argc, argv);
146 	gsf_shutdown ();
147 
148 	return res;
149 }
150