summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
authorVincent Torri <vincent.torri@gmail.com>2012-09-17 16:35:38 +0000
committerVincent Torri <vincent.torri@gmail.com>2012-09-17 16:35:38 +0000
commit8abaff3bdf53ecc7e605b82b12e385b8c0a02327 (patch)
tree4c828ae7083f724fd0214cf3624b20a7cf9448d2 /src/bin
parent12732ab7ebcacad66352d3d48306e9945a4a584f (diff)
merge: add eet
SVN revision: 76768
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/Makefile.am2
-rw-r--r--src/bin/eet/Makefile.am17
-rw-r--r--src/bin/eet/eet_main.c507
3 files changed, 526 insertions, 0 deletions
diff --git a/src/bin/Makefile.am b/src/bin/Makefile.am
index d89d02e..7088928 100644
--- a/src/bin/Makefile.am
+++ b/src/bin/Makefile.am
@@ -7,3 +7,5 @@ if HAVE_WINDOWS
7SUBDIRS += evil 7SUBDIRS += evil
8 8
9endif 9endif
10
11SUBDIRS += eet
diff --git a/src/bin/eet/Makefile.am b/src/bin/eet/Makefile.am
new file mode 100644
index 0000000..af5fa84
--- /dev/null
+++ b/src/bin/eet/Makefile.am
@@ -0,0 +1,17 @@
1
2MAINTAINERCLEANFILES = Makefile.in
3
4AM_CPPFLAGS = \
5-I$(top_srcdir)/src/lib/eet \
6-I$(top_builddir)/src/lib/eet \
7-DPACKAGE_BIN_DIR=\"$(bindir)\" \
8-DPACKAGE_LIB_DIR=\"$(libdir)\" \
9-DPACKAGE_DATA_DIR=\"$(datadir)/$(PACKAGE)\" \
10@EET_CFLAGS@
11
12bin_PROGRAMS = @EET_PRG@
13EXTRA_PROGRAMS = eet
14
15eet_SOURCES = eet_main.c
16eet_LDADD = $(top_builddir)/src/lib/eet/libeet.la @EET_LIBS@
17eet_LDFLAGS = @lt_enable_auto_import@
diff --git a/src/bin/eet/eet_main.c b/src/bin/eet/eet_main.c
new file mode 100644
index 0000000..8a93d11
--- /dev/null
+++ b/src/bin/eet/eet_main.c
@@ -0,0 +1,507 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif /* ifdef HAVE_CONFIG_H */
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
9#ifdef HAVE_UNISTD_H
10# include <unistd.h>
11#endif /* ifdef HAVE_UNISTD_H */
12
13#ifdef HAVE_EVIL
14# include <Evil.h>
15#endif /* ifdef HAVE_EVIL */
16
17#include <Eet.h>
18
19static int _eet_main_log_dom = -1;
20
21#ifdef EET_DEFAULT_LOG_COLOR
22#undef EET_DEFAULT_LOG_COLOR
23#endif /* ifdef EET_DEFAULT_LOG_COLOR */
24#define EET_DEFAULT_LOG_COLOR EINA_COLOR_CYAN
25#ifdef ERR
26#undef ERR
27#endif /* ifdef ERR */
28#define ERR(...) EINA_LOG_DOM_ERR(_eet_main_log_dom, __VA_ARGS__)
29#ifdef DBG
30#undef DBG
31#endif /* ifdef DBG */
32#define DBG(...) EINA_LOG_DOM_DBG(_eet_main_log_dom, __VA_ARGS__)
33#ifdef INF
34#undef INF
35#endif /* ifdef INF */
36#define INF(...) EINA_LOG_DOM_INFO(_eet_main_log_dom, __VA_ARGS__)
37#ifdef WRN
38#undef WRN
39#endif /* ifdef WRN */
40#define WRN(...) EINA_LOG_DOM_WARN(_eet_main_log_dom, __VA_ARGS__)
41#ifdef CRIT
42#undef CRIT
43#endif /* ifdef CRIT */
44#define CRIT(...) EINA_LOG_DOM_CRIT(_eet_main_log_dom, __VA_ARGS__)
45
46static void
47do_eet_list(const char *file)
48{
49 int i, num;
50 char **list;
51 Eet_File *ef;
52
53 ef = eet_open(file, EET_FILE_MODE_READ);
54 if (!ef)
55 {
56 ERR("cannot open for reading: %s", file);
57 exit(-1);
58 }
59
60 list = eet_list(ef, "*", &num);
61 if (list)
62 {
63 for (i = 0; i < num; i++)
64 printf("%s\n", list[i]);
65 free(list);
66 }
67
68 eet_close(ef);
69} /* do_eet_list */
70
71static void
72do_eet_stats(const char *file)
73{
74 int i, num;
75 int count[2] = { 0, 0 };
76 int size[2] = { 0, 0 };
77 char **list;
78 Eet_File *ef;
79 Eet_Dictionary *ed;
80
81 ef = eet_open(file, EET_FILE_MODE_READ);
82 if (!ef)
83 {
84 ERR("cannot open for reading: %s", file);
85 exit(-1);
86 }
87
88 printf("*** sections stats ***\n");
89 list = eet_list(ef, "*", &num);
90 if (list)
91 {
92 for (i = 0; i < num; i++)
93 {
94 const void *ro = NULL;
95 void *rw = NULL;
96 int tsize;
97
98 ro = eet_read_direct(ef, list[i], &tsize);
99 if (!ro) rw = eet_read(ef, list[i], &tsize);
100 printf(rw ? "%s of size %i is compressed.\n" : "%s of size %i is not compressed.\n", list[i], tsize);
101 count[rw ? 0 : 1]++;
102 size[rw ? 0 : 1] += tsize;
103 free(rw);
104 }
105 free(list);
106 }
107
108 printf("*** dictionary ***\n");
109 ed = eet_dictionary_get(ef);
110 if (ed)
111 {
112 printf("%i strings inside the dictionary.\n", eet_dictionary_count(ed));
113 }
114 else
115 {
116 printf("no dictionary in this file.\n");
117 }
118 printf("*** global ***\n");
119 printf("%i sections\n", num);
120 printf("- %i of them are compressed (%02.2f%%) expanding in %i bytes.\n",
121 count[0], (float) count[0] * 100 / (float) num, size[0]);
122 printf("- %i of them are directly mappable in memory (%02.2f%%) representing %i bytes.\n",
123 count[1], (float) count[1] * 100 / (float) num, size[1]);
124
125 eet_close(ef);
126}
127
128static void
129do_eet_extract(const char *file,
130 const char *key,
131 const char *out,
132 const char *crypto_key)
133{
134 Eet_File *ef;
135 void *data;
136 int size = 0;
137 FILE *f = stdout;
138
139 ef = eet_open(file, EET_FILE_MODE_READ);
140 if (!ef)
141 {
142 ERR("cannot open for reading: %s", file);
143 exit(-1);
144 }
145
146 data = eet_read_cipher(ef, key, &size, crypto_key);
147 if (!data)
148 {
149 ERR("cannot read key %s", key);
150 exit(-1);
151 }
152
153 if (out)
154 {
155 f = fopen(out, "wb");
156 if (!f)
157 {
158 ERR("cannot open %s", out);
159 exit(-1);
160 }
161 }
162
163 if (fwrite(data, size, 1, f) != 1)
164 {
165 ERR("cannot write to %s", out ? out : "standard output");
166 exit(-1);
167 }
168
169 if (out) fclose(f);
170 free(data);
171 eet_close(ef);
172} /* do_eet_extract */
173
174static void
175do_eet_decode_dump(void *data,
176 const char *str)
177{
178 fputs(str, (FILE *)data);
179} /* do_eet_decode_dump */
180
181static void
182do_eet_decode(const char *file,
183 const char *key,
184 const char *out,
185 const char *crypto_key)
186{
187 Eet_File *ef;
188 FILE *f = stdout;
189
190 ef = eet_open(file, EET_FILE_MODE_READ);
191 if (!ef)
192 {
193 ERR("cannot open for reading: %s", file);
194 exit(-1);
195 }
196
197 if (out)
198 {
199 f = fopen(out, "wb");
200 if (!f)
201 {
202 ERR("cannot open %s", out);
203 exit(-1);
204 }
205 }
206
207 if (!eet_data_dump_cipher(ef, key, crypto_key, do_eet_decode_dump, f))
208 {
209 ERR("cannot write to %s", out ? out : "standard output");
210 exit(-1);
211 }
212
213 if (out) fclose(f);
214 eet_close(ef);
215} /* do_eet_decode */
216
217static void
218do_eet_insert(const char *file,
219 const char *key,
220 const char *out,
221 int compress,
222 const char *crypto_key)
223{
224 Eet_File *ef;
225 void *data;
226 int size = 0;
227 FILE *f;
228
229 ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
230 if (!ef)
231 ef = eet_open(file, EET_FILE_MODE_WRITE);
232
233 if (!ef)
234 {
235 ERR("cannot open for read+write: %s", file);
236 exit(-1);
237 }
238
239 f = fopen(out, "rb");
240 if (!f)
241 {
242 ERR("cannot open %s", out);
243 exit(-1);
244 }
245
246 fseek(f, 0, SEEK_END);
247 size = ftell(f);
248 rewind(f);
249 data = malloc(size);
250 if (!data)
251 {
252 ERR("cannot allocate %i bytes", size);
253 exit(-1);
254 }
255
256 if (fread(data, size, 1, f) != 1)
257 {
258 ERR("cannot read file %s", out);
259 exit(-1);
260 }
261
262 fclose(f);
263 eet_write_cipher(ef, key, data, size, compress, crypto_key);
264 free(data);
265 eet_close(ef);
266} /* do_eet_insert */
267
268static void
269do_eet_encode(const char *file,
270 const char *key,
271 const char *out,
272 int compress,
273 const char *crypto_key)
274{
275 Eet_File *ef;
276 char *text;
277 int textlen = 0;
278 int size = 0;
279 FILE *f;
280
281 ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
282 if (!ef)
283 ef = eet_open(file, EET_FILE_MODE_WRITE);
284
285 if (!ef)
286 {
287 ERR("cannot open for read+write: %s", file);
288 exit(-1);
289 }
290
291 f = fopen(out, "rb");
292 if (!f)
293 {
294 ERR("cannot open %s", out);
295 exit(-1);
296 }
297
298 fseek(f, 0, SEEK_END);
299 textlen = ftell(f);
300 rewind(f);
301 text = malloc(textlen);
302 if (!text)
303 {
304 ERR("cannot allocate %i bytes", size);
305 exit(-1);
306 }
307
308 if (fread(text, textlen, 1, f) != 1)
309 {
310 ERR("cannot read file %s", out);
311 exit(-1);
312 }
313
314 fclose(f);
315 if (!eet_data_undump_cipher(ef, key, crypto_key, text, textlen, compress))
316 {
317 ERR("cannot parse %s", out);
318 exit(-1);
319 }
320
321 free(text);
322 eet_close(ef);
323} /* do_eet_encode */
324
325static void
326do_eet_remove(const char *file,
327 const char *key)
328{
329 Eet_File *ef;
330
331 ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
332 if (!ef)
333 {
334 ERR("cannot open for read+write: %s", file);
335 exit(-1);
336 }
337
338 eet_delete(ef, key);
339 eet_close(ef);
340} /* do_eet_remove */
341
342static void
343do_eet_check(const char *file)
344{
345 Eet_File *ef;
346 const void *der;
347 int der_length;
348 int sign_length;
349
350 ef = eet_open(file, EET_FILE_MODE_READ);
351 if (!ef)
352 {
353 ERR("checking signature of `%s` failed", file);
354 exit(-1);
355 }
356
357 der = eet_identity_x509(ef, &der_length);
358
359 fprintf(stdout, "Certificate length %i.\n", der_length);
360 eet_identity_certificate_print(der, der_length, stdout);
361
362 eet_identity_signature(ef, &sign_length);
363 fprintf(stdout, "Signature length %i.\n", sign_length);
364
365 eet_close(ef);
366} /* do_eet_check */
367
368static void
369do_eet_sign(const char *file,
370 const char *private_key,
371 const char *public_key)
372{
373 Eet_File *ef;
374 Eet_Key *key;
375
376 ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
377 if (!ef)
378 {
379 ERR("cannot open for read+write: %s.", file);
380 exit(-1);
381 }
382
383 key = eet_identity_open(public_key, private_key, NULL);
384 if (!key)
385 {
386 ERR("cannot open key '%s:%s'.", public_key, private_key);
387 exit(-1);
388 }
389
390 fprintf(stdout, "Using the following key to sign `%s`.\n", file);
391 eet_identity_print(key, stdout);
392
393 eet_identity_set(ef, key);
394
395 eet_close(ef);
396} /* do_eet_sign */
397
398int
399main(int argc,
400 char **argv)
401{
402 if (!eet_init())
403 return -1;
404
405 _eet_main_log_dom = eina_log_domain_register("eet_main", EINA_COLOR_CYAN);
406 if(_eet_main_log_dom < -1)
407 {
408 EINA_LOG_ERR("Impossible to create a log domain for eet_main.");
409 eet_shutdown();
410 return -1;
411 }
412
413 if (argc < 2)
414 {
415help:
416 printf(
417 "Usage:\n"
418 " eet -l FILE.EET list all keys in FILE.EET\n"
419 " eet -x FILE.EET KEY [OUT-FILE] [CRYPTO_KEY] extract data stored in KEY in FILE.EET and write to OUT-FILE or standard output\n"
420 " eet -d FILE.EET KEY [OUT-FILE] [CRYPTO_KEY] extract and decode data stored in KEY in FILE.EET and write to OUT-FILE or standard output\n"
421 " eet -i FILE.EET KEY IN-FILE COMPRESS [CRYPTO_KEY] insert data to KEY in FILE.EET from IN-FILE and if COMPRESS is 1, compress it\n"
422 " eet -e FILE.EET KEY IN-FILE COMPRESS [CRYPTO_KEY] insert and encode to KEY in FILE.EET from IN-FILE and if COMPRESS is 1, compress it\n"
423 " eet -r FILE.EET KEY remove KEY in FILE.EET\n"
424 " eet -c FILE.EET report and check the signature information of an eet file\n"
425 " eet -s FILE.EET PRIVATE_KEY PUBLIC_KEY sign FILE.EET with PRIVATE_KEY and attach PUBLIC_KEY as it's certificate\n"
426 " eet -t FILE.EET give some statistic about a file\n"
427 );
428 eet_shutdown();
429 return -1;
430 }
431
432 if ((!strncmp(argv[1], "-h", 2)))
433 goto help;
434 else if ((!strcmp(argv[1], "-l")) && (argc > 2))
435 do_eet_list(argv[2]);
436 else if ((!strcmp(argv[1], "-x")) && (argc > 3))
437 {
438 switch (argc)
439 {
440 case 4:
441 {
442 do_eet_extract(argv[2], argv[3], NULL, NULL);
443 break;
444 }
445 case 5:
446 {
447 do_eet_extract(argv[2], argv[3], argv[4], NULL);
448 break;
449 }
450 default:
451 {
452 do_eet_extract(argv[2], argv[3], argv[4], argv[5]);
453 break;
454 }
455 }
456 }
457 else if ((!strcmp(argv[1], "-d")) && (argc > 3))
458 {
459 switch (argc)
460 {
461 case 4:
462 {
463 do_eet_decode(argv[2], argv[3], NULL, NULL);
464 break;
465 }
466 case 5:
467 {
468 do_eet_decode(argv[2], argv[3], argv[4], NULL);
469 break;
470 }
471 default:
472 {
473 do_eet_decode(argv[2], argv[3], argv[4], argv[5]);
474 break;
475 }
476 }
477 }
478 else if ((!strcmp(argv[1], "-i")) && (argc > 5))
479 {
480 if (argc > 6)
481 do_eet_insert(argv[2], argv[3], argv[4], atoi(argv[5]), argv[6]);
482 else
483 do_eet_insert(argv[2], argv[3], argv[4], atoi(argv[5]), NULL);
484 }
485 else if ((!strcmp(argv[1], "-e")) && (argc > 5))
486 {
487 if (argc > 6)
488 do_eet_encode(argv[2], argv[3], argv[4], atoi(argv[5]), argv[6]);
489 else
490 do_eet_encode(argv[2], argv[3], argv[4], atoi(argv[5]), NULL);
491 }
492 else if ((!strcmp(argv[1], "-r")) && (argc > 3))
493 do_eet_remove(argv[2], argv[3]);
494 else if ((!strcmp(argv[1], "-c")) && (argc > 2))
495 do_eet_check(argv[2]);
496 else if ((!strcmp(argv[1], "-s")) && (argc > 4))
497 do_eet_sign(argv[2], argv[3], argv[4]);
498 else if ((!strcmp(argv[1], "-t")) && (argc > 2))
499 do_eet_stats(argv[2]);
500 else
501 goto help;
502
503 eina_log_domain_unregister(_eet_main_log_dom);
504 eet_shutdown();
505 return 0;
506} /* main */
507