seama.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2008, Alpha Networks, Inc.
  4. * Created by David Hsieh <david_hsieh@alphanetworks.com>
  5. * All right reserved.
  6. *
  7. * (SEA)ttle i(MA)ge is the image which used in project seattle.
  8. *
  9. * Redistribution and use in source and binary forms, with or
  10. * without modification, are permitted provided that the following
  11. * conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials
  20. * provided with the distribution.
  21. *
  22. * 3. The name of the author may not be used to endorse or promote
  23. * products derived from this software without specific prior
  24. * written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
  27. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  29. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
  30. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  32. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  34. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  36. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  37. * THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdio.h>
  41. #include <stdint.h>
  42. #include <stdlib.h>
  43. #include <stdarg.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <unistd.h>
  47. #include <string.h>
  48. #include <arpa/inet.h>
  49. #include "md5.h"
  50. #include "seama.h"
  51. #define PROGNAME "seama"
  52. #define VERSION "0.20"
  53. #define MAX_SEAMA_META_SIZE 1024
  54. #define MAX_META 128
  55. #define MAX_IMAGE 128
  56. extern int optind;
  57. extern char * optarg;
  58. static int o_verbose = 0; /* verbose mode. */
  59. static char * o_dump = NULL; /* Seama file to dump. */
  60. static char * o_seal = NULL; /* Seal the input images when file name exist. */
  61. static char * o_extract = NULL; /* Extract the seama file. */
  62. static char * o_images[MAX_IMAGE];/* The image files to pack or seal */
  63. static int o_isize = 0; /* number of images */
  64. static char * o_meta[MAX_META]; /* meta data array */
  65. static int o_msize = 0; /* size of meta array */
  66. static void verbose(const char * format, ...)
  67. {
  68. va_list marker;
  69. if (o_verbose)
  70. {
  71. va_start(marker, format);
  72. vfprintf(stdout, format, marker);
  73. va_end(marker);
  74. }
  75. }
  76. static void cleanup_exit(int exit_code)
  77. {
  78. verbose("%s: exit with code %d\n", PROGNAME, exit_code);
  79. exit(exit_code);
  80. }
  81. static void show_usage(int exit_code)
  82. {
  83. printf( PROGNAME " version " VERSION "\n"
  84. "usage: " PROGNAME " [OPTIONS]\n"
  85. " -h show this help message.\n"
  86. " -v verbose mode.\n"
  87. " -m {META data} META data.\n"
  88. " -d {file} dump the info of the seama file.\n"
  89. " -i {input file} image file name.\n"
  90. " -s {file} Seal the images to the seama file.\n"
  91. " -x {seama file} Extract the seama file.\n"
  92. "\n"
  93. " SEAMA can pack the input file (with -i) into a seama file.\n"
  94. " ex: seama -i target.file\n"
  95. " SEAMA can also seal multiple seama files into a single seama file.\n"
  96. " ex: seama -s final.file -i taget1.seama -i target2.seama\n"
  97. " To extract the raw image from SEAMA, you need to specify the meta.\n"
  98. " The first image match the specified meta will be extract to\n"
  99. " the output file which was specified with '-x'.\n"
  100. " ex: seama -x output -i seama.image -m file=sealpac\n"
  101. );
  102. cleanup_exit(exit_code);
  103. }
  104. static int parse_args(int argc, char * argv[])
  105. {
  106. int opt;
  107. while ((opt = getopt(argc, argv, "hvd:s:i:m:x:")) > 0)
  108. {
  109. switch (opt)
  110. {
  111. default: show_usage(-1); break;
  112. case 'h': show_usage(0); break;
  113. case 'v': o_verbose++; break;
  114. case 'd': o_dump = optarg; break;
  115. case 's': o_seal = optarg; break;
  116. case 'x': o_extract = optarg; break;
  117. case 'i':
  118. if (o_isize < MAX_IMAGE) o_images[o_isize++] = optarg;
  119. else printf("Exceed the maximum acceptable image files.!\n");
  120. break;
  121. case 'm':
  122. if (o_msize < MAX_META) o_meta[o_msize++] = optarg;
  123. else printf("Exceed the maximum acceptable META data.!\n");
  124. break;
  125. }
  126. }
  127. return 0;
  128. }
  129. /*******************************************************************/
  130. static size_t calculate_digest(FILE * fh, size_t size, uint8_t * digest)
  131. {
  132. MD5_CTX ctx;
  133. size_t bytes_left, bytes_read, i;
  134. uint8_t buf[MAX_SEAMA_META_SIZE];
  135. bytes_left = size ? size : sizeof(buf);
  136. bytes_read = 0;
  137. MD5_Init(&ctx);
  138. while (!feof(fh) && !ferror(fh) && bytes_left > 0)
  139. {
  140. i = bytes_left < sizeof(buf) ? bytes_left : sizeof(buf);
  141. i = fread(buf, sizeof(char), i, fh);
  142. if (i > 0)
  143. {
  144. MD5_Update(&ctx, buf, i);
  145. bytes_read += i;
  146. }
  147. if (size) bytes_left -= i;
  148. }
  149. MD5_Final(digest, &ctx);
  150. return bytes_read;
  151. }
  152. #define READ_BUFF_SIZE 8*1024
  153. static size_t copy_file(FILE * to, FILE * from)
  154. {
  155. size_t i, fsize = 0;
  156. uint8_t buf[READ_BUFF_SIZE];
  157. while (!feof(from) && !ferror(from))
  158. {
  159. i = fread(buf, sizeof(uint8_t), READ_BUFF_SIZE, from);
  160. if (i > 0)
  161. {
  162. fsize += i;
  163. fwrite(buf, sizeof(uint8_t), i, to);
  164. }
  165. }
  166. return fsize;
  167. }
  168. static int verify_seama(const char * fname, int msg)
  169. {
  170. FILE * fh = NULL;
  171. struct stat st;
  172. seamahdr_t shdr;
  173. uint8_t checksum[16];
  174. uint8_t digest[16];
  175. uint8_t buf[MAX_SEAMA_META_SIZE];
  176. size_t msize, isize, i;
  177. int ret = -1;
  178. #define ERRBREAK(fmt, args...) { if (msg) printf(fmt, ##args); break; }
  179. do
  180. {
  181. if (stat(fname, &st) < 0) ERRBREAK("Unable to get the info of '%s'\n",fname);
  182. if ((fh = fopen(fname, "r+"))==NULL) ERRBREAK("Unable to open '%s' for reading!\n",fname);
  183. /* Dump SEAMA header */
  184. if (msg) printf("FILE - %s (%d bytes)\n", fname, (int)st.st_size);
  185. /* SEAMA */
  186. while (!feof(fh) && !ferror(fh))
  187. {
  188. /* read header */
  189. if (fread(&shdr, sizeof(shdr), 1, fh) != 1) break;
  190. /* Check the magic number */
  191. if (shdr.magic != htonl(SEAMA_MAGIC)) ERRBREAK("Invalid SEAMA magic. Probably no more SEAMA!\n");
  192. /* Get the size */
  193. isize = ntohl(shdr.size);
  194. msize = ntohs(shdr.metasize);
  195. /* The checksum exist only if size is greater than zero. */
  196. if (isize > 0)
  197. {
  198. if (fread(checksum, sizeof(checksum), 1, fh) != 1)
  199. ERRBREAK("Error reading checksum !\n");
  200. }
  201. /* Check the META size. */
  202. if (msize > sizeof(buf)) ERRBREAK("META data in SEAMA header is too large!\n");
  203. /* Read META data. */
  204. if (fread(buf, sizeof(char), msize, fh) != msize)
  205. ERRBREAK("Unable to read SEAMA META data!\n");
  206. /* dump header */
  207. if (msg)
  208. {
  209. printf("SEAMA ==========================================\n");
  210. printf(" magic : %08x\n", ntohl(shdr.magic));
  211. printf(" meta size : %d bytes\n", msize);
  212. for (i=0; i<msize; i+=(strlen((const char *)&buf[i])+1))
  213. printf(" meta data : %s\n", &buf[i]);
  214. printf(" image size : %d bytes\n", isize);
  215. }
  216. /* verify checksum */
  217. if (isize > 0)
  218. {
  219. if (msg)
  220. {
  221. printf(" checksum : ");
  222. for (i=0; i<16; i++) printf("%02X", checksum[i]);
  223. printf("\n");
  224. }
  225. /* Calculate the checksum */
  226. calculate_digest(fh, isize, digest);
  227. if (msg)
  228. {
  229. printf(" digest : ");
  230. for (i=0; i<16; i++) printf("%02X", digest[i]);
  231. printf("\n");
  232. }
  233. if (memcmp(checksum, digest, 16)!=0) ERRBREAK("!!ERROR!! checksum error !!\n");
  234. ret = 0;
  235. }
  236. }
  237. if (msg) printf("================================================\n");
  238. } while (0);
  239. if (fh) fclose(fh);
  240. return ret;
  241. }
  242. static size_t write_seama_header(FILE * fh, char * meta[], size_t msize, size_t size)
  243. {
  244. seamahdr_t shdr;
  245. size_t i;
  246. uint16_t metasize = 0;
  247. /* Calculate the META size */
  248. for (i=0; i<msize; i++) metasize += (strlen(meta[i]) + 1);
  249. //+++ let meta data end on 4 alignment by siyou. 2010/3/1 03:58pm
  250. metasize = ((metasize+3)/4)*4;
  251. verbose("SEAMA META : %d bytes\n", metasize);
  252. /* Fill up the header, all the data endian should be network byte order. */
  253. shdr.magic = htonl(SEAMA_MAGIC);
  254. shdr.reserved = 0;
  255. shdr.metasize = htons(metasize);
  256. shdr.size = htonl(size);
  257. /* Write the header */
  258. return fwrite(&shdr, sizeof(seamahdr_t), 1, fh);
  259. }
  260. static size_t write_checksum(FILE * fh, uint8_t * checksum)
  261. {
  262. return fwrite(checksum, sizeof(uint8_t), 16, fh);
  263. }
  264. static size_t write_meta_data(FILE * fh, char * meta[], size_t size)
  265. {
  266. size_t i,j;
  267. size_t ret = 0;
  268. for (i=0; i<size; i++)
  269. {
  270. verbose("SEAMA META data : %s\n", meta[i]);
  271. j = fwrite(meta[i], sizeof(char), strlen(meta[i])+1, fh);
  272. if (j != strlen(meta[i])+1) return 0;
  273. ret += j;
  274. }
  275. //+++ let meta data end on 4 alignment by siyou. 2010/3/1 03:58pm
  276. j = ((ret+3)/4)*4;
  277. for ( ; ret < j; ret++)
  278. fwrite("", sizeof(char), 1, fh);
  279. return ret;
  280. }
  281. /*******************************************************************/
  282. static void dump_seama(const char * fname)
  283. {
  284. verify_seama(fname, 1);
  285. }
  286. static void seal_files(const char * file)
  287. {
  288. FILE * fh;
  289. FILE * ifh;
  290. size_t i;
  291. /* Each image should be seama. */
  292. for (i = 0; i < o_isize; i++)
  293. {
  294. if (verify_seama(o_images[i], 0) < 0)
  295. {
  296. printf("'%s' is not a seama file !\n",o_images[i]);
  297. return;
  298. }
  299. }
  300. /* Open file for write */
  301. fh = fopen(file, "w+");
  302. if (fh)
  303. {
  304. /* Write the header. */
  305. write_seama_header(fh, o_meta, o_msize, 0);
  306. write_meta_data(fh, o_meta, o_msize);
  307. /* Write image files */
  308. for (i=0; i<o_isize; i++)
  309. {
  310. ifh = fopen(o_images[i], "r+");
  311. if (ifh)
  312. {
  313. copy_file(fh, ifh);
  314. fclose(ifh);
  315. }
  316. }
  317. fclose(fh);
  318. }
  319. }
  320. static void pack_files(void)
  321. {
  322. FILE * fh;
  323. FILE * ifh;
  324. size_t i, fsize;
  325. char filename[512];
  326. uint8_t digest[16];
  327. for (i=0; i<o_isize; i++)
  328. {
  329. /* Open the input file. */
  330. ifh = fopen(o_images[i], "r+");
  331. if (ifh)
  332. {
  333. fsize = calculate_digest(ifh, 0, digest);
  334. verbose("file size (%s) : %d\n", o_images[i], fsize);
  335. rewind(ifh);
  336. /* Open the output file. */
  337. sprintf(filename, "%s.seama", o_images[i]);
  338. fh = fopen(filename, "w+");
  339. if (fh)
  340. {
  341. write_seama_header(fh, o_meta, o_msize, fsize);
  342. write_checksum(fh, digest);
  343. write_meta_data(fh, o_meta, o_msize);
  344. copy_file(fh, ifh);
  345. fclose(fh);
  346. }
  347. fclose(ifh);
  348. }
  349. else
  350. {
  351. printf("Unable to open image file '%s'\n",o_images[i]);
  352. }
  353. }
  354. }
  355. /**************************************************************************/
  356. static int match_meta(const char * meta, size_t size)
  357. {
  358. size_t i, j;
  359. int match;
  360. for (i = 0; i < o_msize; i++)
  361. {
  362. for (match = 0, j = 0; j < size; j += (strlen(&meta[j])+1))
  363. if (strcmp(&meta[j], o_meta[i])==0) { match++; break; }
  364. if (!match) return 0;
  365. }
  366. return 1;
  367. }
  368. static void extract_file(const char * output)
  369. {
  370. FILE * ifh = NULL;
  371. FILE * ofh = NULL;
  372. size_t msize, isize, i, m;
  373. seamahdr_t shdr;
  374. uint8_t buf[MAX_SEAMA_META_SIZE];
  375. int done = 0;
  376. /* We need meta for searching the target image. */
  377. if (o_msize == 0)
  378. {
  379. printf("SEAMA: need meta for searching image.\n");
  380. return;
  381. }
  382. /* Walk through each input file */
  383. for (i = 0; i < o_isize; i++)
  384. {
  385. /* verify the input file */
  386. if (verify_seama(o_images[i], 0) < 0)
  387. {
  388. printf("SEAMA: '%s' is not a seama file !\n", o_images[i]);
  389. continue;
  390. }
  391. /* open the input file */
  392. ifh = fopen(o_images[i], "r");
  393. if (!ifh) continue;
  394. /* read file */
  395. while (!feof(ifh) && !ferror(ifh))
  396. {
  397. /* read header */
  398. fread(&shdr, sizeof(shdr), 1, ifh);
  399. if (shdr.magic != htonl(SEAMA_MAGIC)) break;
  400. /* Get the size */
  401. isize = ntohl(shdr.size);
  402. msize = ntohs(shdr.metasize);
  403. if (isize == 0)
  404. {
  405. while (msize > 0)
  406. {
  407. m = fread(buf, sizeof(char), (msize < MAX_SEAMA_META_SIZE) ? msize : MAX_SEAMA_META_SIZE, ifh);
  408. if (m <= 0) break;
  409. msize -= m;
  410. }
  411. continue;
  412. }
  413. /* read checksum */
  414. fread(buf, sizeof(char), 16, ifh);
  415. if (msize > 0)
  416. {
  417. /* read META */
  418. fread(buf, sizeof(char), msize, ifh);
  419. if (match_meta((const char *)buf, msize))
  420. {
  421. printf("SEAMA: found image @ '%s', image size: %d\n", o_images[i], isize);
  422. /* open output file */
  423. ofh = fopen(output, "w");
  424. if (!ofh) printf("SEAMA: unable to open '%s' for writting.\n",output);
  425. else
  426. {
  427. while (isize > 0)
  428. {
  429. m = fread(buf, sizeof(char), (isize < MAX_SEAMA_META_SIZE) ? isize : MAX_SEAMA_META_SIZE, ifh);
  430. if (m <= 0) break;
  431. fwrite(buf, sizeof(char), m, ofh);
  432. isize -= m;
  433. }
  434. fclose(ofh);
  435. }
  436. done++;
  437. break;
  438. }
  439. }
  440. while (isize > 0)
  441. {
  442. m = fread(buf, sizeof(char), (isize < MAX_SEAMA_META_SIZE) ? isize : MAX_SEAMA_META_SIZE, ifh);
  443. if (m <= 0) break;
  444. isize -= m;
  445. }
  446. }
  447. /* close the file. */
  448. fclose(ifh);
  449. if (done) break;
  450. }
  451. return;
  452. }
  453. /*******************************************************************/
  454. #ifdef RGBIN_BOX
  455. int seama_main(int argc, char * argv[], char * env[])
  456. #else
  457. int main(int argc, char * argv[], char * env[])
  458. #endif
  459. {
  460. verbose("SEAMA version " VERSION "\n");
  461. /* parse the arguments */
  462. if (parse_args(argc, argv) < 0) show_usage(9);
  463. /* Do the works */
  464. if (o_dump) dump_seama(o_dump);
  465. else if (o_seal) seal_files(o_seal);
  466. else if (o_extract) extract_file(o_extract);
  467. else pack_files();
  468. cleanup_exit(0);
  469. return 0;
  470. }