svn update: 48539 (latest:48959)
[framework/uifw/eet.git] / src / bin / eet_main.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #ifndef _MSC_VER
13 # include <unistd.h>
14 #endif
15
16 #ifdef HAVE_EVIL
17 # include <Evil.h>
18 #endif
19
20 #include <Eet.h>
21
22
23 static int _eet_main_log_dom = -1;
24
25 #ifdef EET_DEFAULT_LOG_COLOR
26 #undef EET_DEFAULT_LOG_COLOR
27 #endif
28 #define EET_DEFAULT_LOG_COLOR EINA_COLOR_CYAN
29 #ifdef ERR
30 #undef ERR
31 #endif
32 #define ERR(...) EINA_LOG_DOM_ERR(_eet_main_log_dom, __VA_ARGS__)
33 #ifdef DBG
34 #undef DBG
35 #endif
36 #define DBG(...) EINA_LOG_DOM_DBG(_eet_main_log_dom, __VA_ARGS__)
37 #ifdef INF
38 #undef INF
39 #endif
40 #define INF(...) EINA_LOG_DOM_INFO(_eet_main_log_dom, __VA_ARGS__)
41 #ifdef WRN
42 #undef WRN
43 #endif
44 #define WRN(...) EINA_LOG_DOM_WARN(_eet_main_log_dom, __VA_ARGS__)
45 #ifdef CRIT
46 #undef CRIT
47 #endif
48 #define CRIT(...) EINA_LOG_DOM_CRIT(_eet_main_log_dom, __VA_ARGS__)
49
50 static void
51 do_eet_list(const char *file)
52 {
53    int i, num;
54    char **list;
55    Eet_File *ef;
56
57    ef = eet_open(file, EET_FILE_MODE_READ);
58    if (!ef)
59      {
60         ERR("cannot open for reading: %s\n", file);
61         exit(-1);
62      }
63    list = eet_list(ef, "*", &num);
64    if (list)
65      {
66         for (i = 0; i < num; i++)
67           printf("%s\n",list[i]);
68         free(list);
69      }
70    eet_close(ef);
71 }
72
73 static void
74 do_eet_extract(const char *file, const char *key, const char *out, const char *crypto_key)
75 {
76    Eet_File *ef;
77    void *data;
78    int size = 0;
79    FILE *f;
80
81    ef = eet_open(file, EET_FILE_MODE_READ);
82    if (!ef)
83      {
84         ERR("cannot open for reading: %s\n", file);
85         exit(-1);
86      }
87    data = eet_read_cipher(ef, key, &size, crypto_key);
88    if (!data)
89      {
90         ERR("cannot read key %s\n", key);
91         exit(-1);
92      }
93    f = fopen(out, "wb");
94    if (!f)
95      {
96         ERR("cannot open %s\n", out);
97         exit(-1);
98      }
99    if (fwrite(data, size, 1, f) != 1)
100      {
101         ERR("cannot write to %s\n", out);
102         exit(-1);
103      }
104    fclose(f);
105    free(data);
106    eet_close(ef);
107 }
108
109 static void
110 do_eet_decode_dump(void *data, const char *str)
111 {
112    fputs(str, (FILE *)data);
113 }
114
115 static void
116 do_eet_decode(const char *file, const char *key, const char *out, const char *crypto_key)
117 {
118    Eet_File *ef;
119    FILE *f;
120
121    ef = eet_open(file, EET_FILE_MODE_READ);
122    if (!ef)
123      {
124         ERR("cannot open for reading: %s\n", file);
125         exit(-1);
126      }
127    f = fopen(out, "wb");
128    if (!f)
129      {
130         ERR("cannot open %s\n", out);
131         exit(-1);
132      }
133    if (!eet_data_dump_cipher(ef, key, crypto_key, do_eet_decode_dump, f))
134      {
135         ERR("cannot write to %s\n", out);
136         exit(-1);
137      }
138    fclose(f);
139    eet_close(ef);
140 }
141
142 static void
143 do_eet_insert(const char *file, const char *key, const char *out, int compress, const char *crypto_key)
144 {
145    Eet_File *ef;
146    void *data;
147    int size = 0;
148    FILE *f;
149
150    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
151    if (!ef)
152      ef = eet_open(file, EET_FILE_MODE_WRITE);
153    if (!ef)
154      {
155         ERR("cannot open for read+write: %s\n", file);
156         exit(-1);
157      }
158    f = fopen(out, "rb");
159    if (!f)
160      {
161         ERR("cannot open %s\n", out);
162         exit(-1);
163      }
164    fseek(f, 0, SEEK_END);
165    size = ftell(f);
166    rewind(f);
167    data = malloc(size);
168    if (!data)
169      {
170         ERR("cannot allocate %i bytes\n", size);
171         exit(-1);
172      }
173    if (fread(data, size, 1, f) != 1)
174      {
175         ERR("cannot read file %s\n", out);
176         exit(-1);
177      }
178    fclose(f);
179    eet_write_cipher(ef, key, data, size, compress, crypto_key);
180    free(data);
181    eet_close(ef);
182 }
183
184 static void
185 do_eet_encode(const char *file, const char *key, const char *out, int compress, const char *crypto_key)
186 {
187    Eet_File *ef;
188    char *text;
189    int textlen = 0;
190    int size = 0;
191    FILE *f;
192
193    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
194    if (!ef)
195      ef = eet_open(file, EET_FILE_MODE_WRITE);
196    if (!ef)
197      {
198         ERR("cannot open for read+write: %s\n", file);
199         exit(-1);
200      }
201    f = fopen(out, "rb");
202    if (!f)
203      {
204         ERR("cannot open %s\n", out);
205         exit(-1);
206      }
207    fseek(f, 0, SEEK_END);
208    textlen = ftell(f);
209    rewind(f);
210    text = malloc(textlen);
211    if (!text)
212      {
213         ERR("cannot allocate %i bytes\n", size);
214         exit(-1);
215      }
216    if (fread(text, textlen, 1, f) != 1)
217      {
218         ERR("cannot read file %s\n", out);
219         exit(-1);
220      }
221    fclose(f);
222    if (!eet_data_undump_cipher(ef, key, crypto_key, text, textlen, compress))
223      {
224         ERR("cannot parse %s\n", out);
225         exit(-1);
226      }
227    free(text);
228    eet_close(ef);
229 }
230
231 static void
232 do_eet_remove(const char *file, const char *key)
233 {
234    Eet_File *ef;
235
236    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
237    if (!ef)
238      {
239         ERR("cannot open for read+write: %s\n", file);
240         exit(-1);
241      }
242    eet_delete(ef, key);
243    eet_close(ef);
244 }
245
246 static void
247 do_eet_check(const char *file)
248 {
249    Eet_File *ef;
250    const void *der;
251    int der_length;
252    int sign_length;
253
254    ef = eet_open(file, EET_FILE_MODE_READ);
255    if (!ef)
256      {
257         ERR("checking signature of `%s` failed\n", file);
258         exit(-1);
259      }
260
261    der = eet_identity_x509(ef, &der_length);
262
263    fprintf(stdout, "Certificate length %i.\n", der_length);
264    eet_identity_certificate_print(der, der_length, stdout);
265
266    eet_identity_signature(ef, &sign_length);
267    fprintf(stdout, "Signature length %i.\n", sign_length);
268
269    eet_close(ef);
270 }
271
272 static void
273 do_eet_sign(const char *file, const char *private_key, const char *public_key)
274 {
275    Eet_File *ef;
276    Eet_Key *key;
277
278    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
279    if (!ef)
280      {
281        ERR("cannot open for read+write: %s.\n", file);
282         exit(-1);
283      }
284
285    key = eet_identity_open(public_key, private_key, NULL);
286    if (!key)
287      {
288         ERR("cannot open key '%s:%s'.\n", public_key, private_key);
289         exit(-1);
290      }
291
292    fprintf(stdout, "Using the following key to sign `%s`.\n", file);
293    eet_identity_print(key, stdout);
294
295    eet_identity_set(ef, key);
296
297    eet_close(ef);
298 }
299
300 int
301 main(int argc, char **argv)
302 {
303    if (!eet_init())
304      return -1;
305
306    _eet_main_log_dom = eina_log_domain_register("Eet_Main",EINA_COLOR_CYAN);
307    if(_eet_main_log_dom < -1)
308      {
309        EINA_LOG_ERR("Impossible to create a log domain for eet_main.\n");
310        eet_shutdown();
311        return(-1);
312      }
313    if (argc < 2)
314      {
315         help:
316         printf("Usage:\n"
317                "  eet -l FILE.EET                                    list all keys in FILE.EET\n"
318                "  eet -x FILE.EET KEY OUT-FILE [CRYPTO_KEY]          extract data stored in KEY in FILE.EET and write to OUT-FILE\n"
319                "  eet -d FILE.EET KEY OUT-FILE [CRYPTO_KEY]          extract and decode data stored in KEY in FILE.EET and write to OUT-FILE\n"
320                "  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"
321                "  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"
322                "  eet -r FILE.EET KEY                                remove KEY in FILE.EET\n"
323                "  eet -c FILE.EET                                    report and check the signature information of an eet file\n"
324                "  eet -s FILE.EET PRIVATE_KEY PUBLIC_KEY             sign FILE.EET with PRIVATE_KEY and attach PUBLIC_KEY as it's certificate\n"
325                );
326         eet_shutdown();
327         return -1;
328      }
329    if ((!strncmp(argv[1], "-h", 2)))
330      {
331         goto help;
332      }
333    else if ((!strcmp(argv[1], "-l")) && (argc > 2))
334      {
335         do_eet_list(argv[2]);
336      }
337    else if ((!strcmp(argv[1], "-x")) && (argc > 4))
338      {
339         if (argc > 5)
340           do_eet_extract(argv[2], argv[3], argv[4], argv[5]);
341         else
342           do_eet_extract(argv[2], argv[3], argv[4], NULL);
343      }
344    else if ((!strcmp(argv[1], "-d")) && (argc > 4))
345      {
346         if (argc > 5)
347          do_eet_decode(argv[2], argv[3], argv[4], argv[5]);
348         else
349          do_eet_decode(argv[2], argv[3], argv[4], NULL);
350      }
351    else if ((!strcmp(argv[1], "-i")) && (argc > 5))
352      {
353         if (argc > 6)
354           do_eet_insert(argv[2], argv[3], argv[4], atoi(argv[5]), argv[6]);
355         else
356           do_eet_insert(argv[2], argv[3], argv[4], atoi(argv[5]), NULL);
357      }
358    else if ((!strcmp(argv[1], "-e")) && (argc > 5))
359      {
360         if (argc > 6)
361           do_eet_encode(argv[2], argv[3], argv[4], atoi(argv[5]), argv[6]);
362         else
363           do_eet_encode(argv[2], argv[3], argv[4], atoi(argv[5]), NULL);
364      }
365    else if ((!strcmp(argv[1], "-r")) && (argc > 3))
366      {
367         do_eet_remove(argv[2], argv[3]);
368      }
369    else if ((!strcmp(argv[1], "-c")) && (argc > 2))
370      {
371         do_eet_check(argv[2]);
372      }
373    else if ((!strcmp(argv[1], "-s")) && (argc > 4))
374      {
375         do_eet_sign(argv[2], argv[3], argv[4]);
376      }
377    else
378      {
379         goto help;
380      }
381    eina_log_domain_unregister(_eet_main_log_dom);
382    eet_shutdown();
383    return 0;
384 }