PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cdrkit-1.1.11/genisoimage/jte.c

#
C | 1044 lines | 786 code | 138 blank | 120 comment | 93 complexity | 6cb1371d631d78d06c095d61859c07a9 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * jte.c
  3. *
  4. * Copyright (c) 2004-2006 Steve McIntyre <steve@einval.com>
  5. *
  6. * Implementation of the Jigdo Template Engine - make jigdo files
  7. * directly when making ISO images
  8. *
  9. * GNU GPL v2
  10. */
  11. #include <mconfig.h>
  12. #include "genisoimage.h"
  13. #include <timedefs.h>
  14. #include <fctldefs.h>
  15. #include <zlib.h>
  16. #include <bzlib.h>
  17. #include <regex.h>
  18. #ifdef SORTING
  19. #include "match.h"
  20. #endif /* SORTING */
  21. #include <errno.h>
  22. #include <schily.h>
  23. #ifdef DVD_VIDEO
  24. #include "dvd_reader.h"
  25. #include "dvd_file.h"
  26. #include "ifo_read.h"
  27. #include "endianconv.h"
  28. #include "checksum.h"
  29. #endif
  30. #ifdef APPLE_HYB
  31. #include <ctype.h>
  32. #endif
  33. #ifdef VMS
  34. #include "vms.h"
  35. #endif
  36. #include "md5.h"
  37. /* Different types used in building our state list below */
  38. #define JTET_FILE_MATCH 1
  39. #define JTET_NOMATCH 2
  40. #define JTE_VER_MAJOR 0x0001
  41. #define JTE_VER_MINOR 0x0013
  42. #define JTE_NAME "JTE"
  43. #define JTE_COMMENT "JTE at http://www.einval.com/~steve/software/JTE/ ; jigdo at http://atterer.org/jigdo/"
  44. #define JIGDO_TEMPLATE_VERSION "1.1"
  45. /*
  46. Simple list to hold the results of -jigdo-exclude and
  47. -jigdo-force-match command line options. Seems easiest to do this
  48. using regexps.
  49. */
  50. struct path_match
  51. {
  52. regex_t match_pattern;
  53. char *match_rule;
  54. struct path_match *next;
  55. };
  56. /* List of mappings e.g. Debian=/mirror/debian */
  57. struct path_mapping
  58. {
  59. char *from;
  60. char *to;
  61. struct path_mapping *next;
  62. };
  63. FILE *jtjigdo = NULL; /* File handle used throughout for the jigdo file */
  64. FILE *jttemplate = NULL; /* File handle used throughout for the template file */
  65. char *jjigdo_out = NULL; /* Output name for jigdo .jigdo file; NULL means don't do it */
  66. char *jtemplate_out = NULL; /* Output name for jigdo template file; NULL means don't do it */
  67. char *jmd5_list = NULL; /* Name of file to use for MD5 checking */
  68. int jte_min_size = MIN_JIGDO_FILE_SIZE;
  69. jtc_t jte_template_compression = JTE_TEMP_GZIP;
  70. struct path_match *exclude_list = NULL;
  71. struct path_match *include_list = NULL;
  72. struct path_mapping *map_list = NULL;
  73. unsigned long long template_size = 0;
  74. unsigned long long image_size = 0;
  75. int checksum_algo_iso = (CHECK_MD5_USED | \
  76. CHECK_SHA1_USED | \
  77. CHECK_SHA256_USED | \
  78. CHECK_SHA512_USED);
  79. int checksum_algo_tmpl = CHECK_MD5_USED;
  80. static checksum_context_t *iso_context = NULL;
  81. static checksum_context_t *template_context = NULL;
  82. /* List of files that we've seen, ready to write into the template and
  83. jigdo files */
  84. typedef struct _file_entry
  85. {
  86. unsigned char md5[16];
  87. off_t file_length;
  88. unsigned long long rsyncsum;
  89. char *filename;
  90. } file_entry_t;
  91. typedef struct _unmatched_entry
  92. {
  93. off_t uncompressed_length;
  94. } unmatched_entry_t;
  95. typedef struct _entry
  96. {
  97. int entry_type; /* JTET_TYPE as above */
  98. struct _entry *next;
  99. union
  100. {
  101. file_entry_t file;
  102. unmatched_entry_t chunk;
  103. } data;
  104. } entry_t;
  105. typedef struct _jigdo_file_entry
  106. {
  107. unsigned char type;
  108. unsigned char fileLen[6];
  109. unsigned char fileRsync[8];
  110. unsigned char fileMD5[16];
  111. } jigdo_file_entry_t;
  112. typedef struct _jigdo_chunk_entry
  113. {
  114. unsigned char type;
  115. unsigned char skipLen[6];
  116. } jigdo_chunk_entry_t;
  117. typedef struct _jigdo_image_entry
  118. {
  119. unsigned char type;
  120. unsigned char imageLen[6];
  121. unsigned char imageMD5[16];
  122. unsigned char blockLen[4];
  123. } jigdo_image_entry_t;
  124. typedef struct _md5_list_entry
  125. {
  126. struct _md5_list_entry *next;
  127. unsigned char MD5[16];
  128. unsigned long long size;
  129. char *filename;
  130. } md5_list_entry_t;
  131. entry_t *entry_list = NULL;
  132. entry_t *entry_last = NULL;
  133. FILE *t_file = NULL;
  134. FILE *j_file = NULL;
  135. int num_matches = 0;
  136. int num_chunks = 0;
  137. md5_list_entry_t *md5_list = NULL;
  138. md5_list_entry_t *md5_last = NULL;
  139. /* Grab the file component from a full path */
  140. static char *file_base_name(char *path)
  141. {
  142. char *endptr = path;
  143. char *ptr = path;
  144. while (*ptr != '\0')
  145. {
  146. if ('/' == *ptr)
  147. endptr = ++ptr;
  148. else
  149. ++ptr;
  150. }
  151. return endptr;
  152. }
  153. /* Build the list of exclusion regexps */
  154. extern int jte_add_exclude(char *pattern)
  155. {
  156. struct path_match *new = NULL;
  157. new = malloc(sizeof *new);
  158. if (!new)
  159. return ENOMEM;
  160. regcomp(&new->match_pattern, pattern, REG_NEWLINE);
  161. new->match_rule = pattern;
  162. /* Order on the exclude list doesn't matter! */
  163. new->next = exclude_list;
  164. exclude_list = new;
  165. return 0;
  166. }
  167. /* Check if the file should be excluded because of a filename match. 1
  168. means exclude, 0 means not */
  169. static int check_exclude_by_name(char *filename, char **matched)
  170. {
  171. struct path_match *ptr = exclude_list;
  172. regmatch_t pmatch[1];
  173. int i = 0;
  174. while (ptr)
  175. {
  176. if (!regexec(&ptr->match_pattern, filename, 1, pmatch, 0))
  177. {
  178. *matched = ptr->match_rule;
  179. return 1;
  180. }
  181. ptr = ptr->next;
  182. }
  183. /* Not matched, so return 0 */
  184. return 0;
  185. }
  186. /* Build the list of required inclusion regexps */
  187. extern int jte_add_include(char *pattern)
  188. {
  189. struct path_match *new = NULL;
  190. new = malloc(sizeof *new);
  191. if (!new)
  192. return ENOMEM;
  193. regcomp(&new->match_pattern, pattern, REG_NEWLINE);
  194. new->match_rule = pattern;
  195. /* Order on the include list doesn't matter! */
  196. new->next = include_list;
  197. include_list = new;
  198. return 0;
  199. }
  200. /* Check if a file has to be MD5-matched to be valid. If we get called
  201. here, we've failed to match any of the MD5 entries we were
  202. given. If the path to the filename matches one of the paths in our
  203. list, clearly it must have been corrupted. Abort with an error. */
  204. static void check_md5_file_match(char *filename)
  205. {
  206. struct path_match *ptr = include_list;
  207. regmatch_t pmatch[1];
  208. int i = 0;
  209. while (ptr)
  210. {
  211. if (!regexec(&ptr->match_pattern, filename, 1, pmatch, 0))
  212. {
  213. #ifdef USE_LIBSCHILY
  214. comerr("File %s should have matched an MD5 entry, but didn't! (Rule '%s')\n", filename, ptr->match_rule);
  215. #else
  216. fprintf(stderr, "File %s should have matched an MD5 entry, but didn't! (Rule '%s')\n", filename, ptr->match_rule);
  217. exit(1);
  218. #endif
  219. }
  220. ptr = ptr->next;
  221. }
  222. }
  223. /* Should we list a file separately in the jigdo output, or should we
  224. just dump it into the template file as binary data? Three things
  225. cases to look for here:
  226. 1. Small files are better simply folded in, as they take less space that way.
  227. 2. Files in /doc (for example) may change in the archive all the
  228. time and it's better to not have to fetch snapshot copies if we
  229. can avoid it.
  230. 3. Files living in specified paths *must* match an entry in the
  231. md5-list, or they must have been corrupted. If we find a corrupt
  232. file, bail out with an error.
  233. */
  234. extern int list_file_in_jigdo(char *filename, off_t size, char **realname, unsigned char md5[16])
  235. {
  236. char *matched_rule;
  237. md5_list_entry_t *entry = md5_list;
  238. int md5sum_done = 0;
  239. if (!jtemplate_out)
  240. return 0;
  241. memset(md5, 0, sizeof(md5));
  242. /* Cheaper to check file size first */
  243. if (size < jte_min_size)
  244. {
  245. if (verbose > 1)
  246. fprintf(stderr, "Jigdo-ignoring file %s; it's too small\n", filename);
  247. return 0;
  248. }
  249. /* Now check the excluded list by name */
  250. if (check_exclude_by_name(filename, &matched_rule))
  251. {
  252. if (verbose > 1)
  253. fprintf(stderr, "Jigdo-ignoring file %s; it's covered in the exclude list by \"%s\"\n", filename, matched_rule);
  254. return 0;
  255. }
  256. /* Check to see if the file is in our md5 list. Check three things:
  257. 1. the size
  258. 2. the filename
  259. 3. (only if the first 2 match) the md5sum
  260. If we get a match for all three, include the file and return
  261. the full path to the file that we have gleaned from the mirror.
  262. */
  263. while (entry)
  264. {
  265. if (size == entry->size)
  266. {
  267. if (!strcmp(file_base_name(filename), file_base_name(entry->filename)))
  268. {
  269. if (!md5sum_done)
  270. {
  271. calculate_md5sum(filename, size, md5);
  272. md5sum_done = 1;
  273. }
  274. if (!memcmp(md5, entry->MD5, sizeof(entry->MD5)))
  275. {
  276. *realname = entry->filename;
  277. return 1;
  278. }
  279. }
  280. }
  281. entry = entry->next;
  282. }
  283. /* We haven't found an entry in our MD5 list to match this
  284. * file. If we should have done, complain and bail out. */
  285. check_md5_file_match(filename);
  286. return 0;
  287. }
  288. /* Add a mapping of pathnames (e.g. Debian=/mirror/debian). We should
  289. be passed TO=FROM here */
  290. extern int jte_add_mapping(char *arg)
  291. {
  292. int error = 0;
  293. struct path_mapping *new = NULL;
  294. struct path_mapping *entry = NULL;
  295. char *p = arg;
  296. char *from = NULL;
  297. char *to = NULL;
  298. /* Find the "=" in the string passed. Set it to NULL and we can
  299. use the string in-place */
  300. while (*p)
  301. {
  302. if ('=' == *p)
  303. {
  304. *p = 0;
  305. p++;
  306. to = arg;
  307. from = p;
  308. }
  309. p++;
  310. }
  311. if (!from || !strlen(from) || !to || !strlen(to))
  312. return EINVAL;
  313. new = malloc(sizeof(*new));
  314. if (!new)
  315. return ENOMEM;
  316. new->from = from;
  317. new->to = to;
  318. new->next = NULL;
  319. if (verbose > 0)
  320. fprintf(stderr, "Adding mapping from %s to %s for the jigdo file\n", from, to);
  321. if (!map_list)
  322. map_list = new;
  323. else
  324. {
  325. /* Order is important; add to the end of the list */
  326. entry = map_list;
  327. while (NULL != entry->next)
  328. entry = entry->next;
  329. entry->next = new;
  330. }
  331. return 0;
  332. }
  333. /* Check if the filename should be remapped; if so map it, otherwise
  334. return the original name. */
  335. static char *remap_filename(char *filename)
  336. {
  337. char *new_name = filename;
  338. struct path_mapping *entry = map_list;
  339. while (entry)
  340. {
  341. if (!strncmp(filename, entry->from, strlen(entry->from)))
  342. {
  343. new_name = calloc(1, 2 + strlen(filename) + strlen(entry->to) - strlen(entry->from));
  344. if (!new_name)
  345. {
  346. fprintf(stderr, "Failed to malloc new filename; abort!\n");
  347. exit(1);
  348. }
  349. sprintf(new_name, "%s:%s", entry->to, &filename[strlen(entry->from)]);
  350. return new_name;
  351. }
  352. entry = entry->next;
  353. }
  354. /* No mapping in effect */
  355. return strdup(filename);
  356. }
  357. /* Write data to the template file and update the MD5 sum */
  358. static size_t template_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
  359. {
  360. checksum_update(template_context, ptr, size * nmemb);
  361. template_size += (unsigned long long)size * nmemb;
  362. return fwrite(ptr, size, nmemb, stream);
  363. }
  364. /* Create a new template file and initialise it */
  365. static void write_template_header()
  366. {
  367. char buf[2048];
  368. int i = 0;
  369. char *p = buf;
  370. memset(buf, 0, sizeof(buf));
  371. template_context = checksum_init_context(checksum_algo_tmpl, "template");
  372. if (!template_context)
  373. {
  374. #ifdef USE_LIBSCHILY
  375. comerr("cannot allocate template checksum contexts\n");
  376. #else
  377. fprintf(stderr, "cannot allocate template checksum contexts\n");
  378. exit(1);
  379. #endif
  380. }
  381. i += sprintf(p, "JigsawDownload template %s %s/%d.%d \r\n",
  382. JIGDO_TEMPLATE_VERSION, JTE_NAME, JTE_VER_MAJOR, JTE_VER_MINOR);
  383. p = &buf[i];
  384. i += sprintf(p, "%s \r\n", JTE_COMMENT);
  385. p = &buf[i];
  386. i += sprintf(p, "\r\n");
  387. template_fwrite(buf, i, 1, t_file);
  388. }
  389. /* Read the MD5 list and build a list in memory for us to use later */
  390. static void add_md5_entry(unsigned char *md5, unsigned long long size, char *filename)
  391. {
  392. int error = 0;
  393. md5_list_entry_t *new = NULL;
  394. new = calloc(1, sizeof(md5_list_entry_t));
  395. memcpy(new->MD5, md5, sizeof(new->MD5));
  396. new->size = size;
  397. new->filename = strdup(filename);
  398. /* Add to the end of the list */
  399. if (NULL == md5_last)
  400. {
  401. md5_last = new;
  402. md5_list = new;
  403. }
  404. else
  405. {
  406. md5_last->next = new;
  407. md5_last = new;
  408. }
  409. }
  410. /* Parse a 12-digit decimal number */
  411. static unsigned long long parse_number(unsigned char in[12])
  412. {
  413. unsigned long long size = 0;
  414. int i = 0;
  415. for (i = 0; i < 12; i++)
  416. {
  417. size *= 10;
  418. if (isdigit(in[i]))
  419. size += (in[i] - '0');
  420. }
  421. return size;
  422. }
  423. /* Read the MD5 list and build a list in memory for us to use later
  424. MD5 list format:
  425. <---MD5---> <--Size--> <--Filename-->
  426. 32 12 remaining
  427. */
  428. static void parse_md5_list(void)
  429. {
  430. FILE *md5_file = NULL;
  431. unsigned char buf[1024];
  432. unsigned char md5[16];
  433. char *filename = NULL;
  434. unsigned char *numbuf = NULL;
  435. int num_files = 0;
  436. unsigned long long size = 0;
  437. md5_file = fopen(jmd5_list, "rb");
  438. if (!md5_file)
  439. {
  440. #ifdef USE_LIBSCHILY
  441. comerr("cannot read from MD5 list file '%s'\n", jmd5_list);
  442. #else
  443. fprintf(stderr, "cannot read from MD5 list file '%s'\n", jmd5_list);
  444. exit(1);
  445. #endif
  446. }
  447. memset(buf, 0, sizeof(buf));
  448. while (fgets((char *)buf, sizeof(buf), md5_file))
  449. {
  450. numbuf = &buf[34];
  451. filename = (char *)&buf[48];
  452. /* Lose the trailing \n from the fgets() call */
  453. if (buf[strlen((char *)buf)-1] == '\n')
  454. buf[strlen((char *)buf)-1] = 0;
  455. if (mk_MD5Parse(buf, md5))
  456. {
  457. #ifdef USE_LIBSCHILY
  458. comerr("cannot parse MD5 file '%s'\n", jmd5_list);
  459. #else
  460. fprintf(stderr, "cannot parse MD5 file '%s'\n", jmd5_list);
  461. exit(1);
  462. #endif
  463. }
  464. size = parse_number(numbuf);
  465. add_md5_entry(md5, size, filename);
  466. memset(buf, 0, sizeof(buf));
  467. num_files++;
  468. }
  469. if (verbose > 0)
  470. fprintf(stderr, "parse_md5_list: added MD5 checksums for %d files\n", num_files);
  471. fclose(md5_file);
  472. }
  473. /* Initialise state and start the jigdo template file */
  474. void write_jt_header(FILE *template_file, FILE *jigdo_file)
  475. {
  476. t_file = template_file;
  477. j_file = jigdo_file;
  478. /* Start checksum work for the image */
  479. iso_context = checksum_init_context(checksum_algo_iso, "iso");
  480. if (!iso_context)
  481. {
  482. #ifdef USE_LIBSCHILY
  483. comerr("cannot allocate iso checksum contexts\n");
  484. #else
  485. fprintf(stderr, "cannot allocate iso checksum contexts\n");
  486. exit(1);
  487. #endif
  488. }
  489. /* Start the template file */
  490. write_template_header();
  491. /* Load up the MD5 list if we've been given one */
  492. if (jmd5_list)
  493. parse_md5_list();
  494. }
  495. /* Compress and flush out a buffer full of template data */
  496. static void flush_gzip_chunk(void *buffer, off_t size)
  497. {
  498. z_stream c_stream; /* compression stream */
  499. unsigned char comp_size_out[6];
  500. unsigned char uncomp_size_out[6];
  501. off_t compressed_size_out = 0;
  502. int err = 0;
  503. unsigned char *comp_buf = NULL;
  504. c_stream.zalloc = NULL;
  505. c_stream.zfree = NULL;
  506. c_stream.opaque = NULL;
  507. err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
  508. comp_buf = malloc(2 * size); /* Worst case */
  509. c_stream.next_out = comp_buf;
  510. c_stream.avail_out = 2 * size;
  511. c_stream.next_in = buffer;
  512. c_stream.avail_in = size;
  513. err = deflate(&c_stream, Z_NO_FLUSH);
  514. err = deflate(&c_stream, Z_FINISH);
  515. compressed_size_out = c_stream.total_out + 16;
  516. err = deflateEnd(&c_stream);
  517. template_fwrite("DATA", 4, 1, t_file);
  518. write_le48(compressed_size_out, &comp_size_out[0]);
  519. template_fwrite(comp_size_out, sizeof(comp_size_out), 1, t_file);
  520. write_le48(size, &uncomp_size_out[0]);
  521. template_fwrite(uncomp_size_out, sizeof(uncomp_size_out), 1, t_file);
  522. template_fwrite(comp_buf, c_stream.total_out, 1, t_file);
  523. free(comp_buf);
  524. }
  525. /* Compress and flush out a buffer full of template data */
  526. static void flush_bz2_chunk(void *buffer, off_t size)
  527. {
  528. bz_stream c_stream; /* compression stream */
  529. unsigned char comp_size_out[6];
  530. unsigned char uncomp_size_out[6];
  531. off_t compressed_size_out = 0;
  532. int err = 0;
  533. unsigned char *comp_buf = NULL;
  534. c_stream.bzalloc = NULL;
  535. c_stream.bzfree = NULL;
  536. c_stream.opaque = NULL;
  537. err = BZ2_bzCompressInit(&c_stream, 9, 0, 0);
  538. comp_buf = malloc(2 * size); /* Worst case */
  539. c_stream.next_out = (char *)comp_buf;
  540. c_stream.avail_out = 2 * size;
  541. c_stream.next_in = buffer;
  542. c_stream.avail_in = size;
  543. err = BZ2_bzCompress(&c_stream, BZ_FINISH);
  544. compressed_size_out = c_stream.total_out_lo32 + 16;
  545. err = BZ2_bzCompressEnd(&c_stream);
  546. template_fwrite("BZIP", 4, 1, t_file);
  547. write_le48(compressed_size_out, &comp_size_out[0]);
  548. template_fwrite(comp_size_out, sizeof(comp_size_out), 1, t_file);
  549. write_le48(size, &uncomp_size_out[0]);
  550. template_fwrite(uncomp_size_out, sizeof(uncomp_size_out), 1, t_file);
  551. template_fwrite(comp_buf, c_stream.total_out_lo32, 1, t_file);
  552. free(comp_buf);
  553. }
  554. static void flush_compressed_chunk(void *buffer, off_t size)
  555. {
  556. if (jte_template_compression == JTE_TEMP_BZIP2)
  557. flush_bz2_chunk(buffer, size);
  558. else
  559. flush_gzip_chunk(buffer, size);
  560. }
  561. /* Append to an existing data buffer, and compress/flush it if
  562. necessary */
  563. static void write_compressed_chunk(unsigned char *buffer, size_t size)
  564. {
  565. static unsigned char *uncomp_buf = NULL;
  566. static size_t uncomp_size = 0;
  567. static size_t uncomp_buf_used = 0;
  568. if (!uncomp_buf)
  569. {
  570. if (jte_template_compression == JTE_TEMP_BZIP2)
  571. uncomp_size = 900 * 1024;
  572. else
  573. uncomp_size = 1024 * 1024;
  574. uncomp_buf = malloc(uncomp_size);
  575. if (!uncomp_buf)
  576. {
  577. #ifdef USE_LIBSCHILY
  578. comerr("failed to allocate %d bytes for template compression buffer\n", (int)uncomp_size);
  579. #else
  580. fprintf(stderr, "failed to allocate %d bytes for template compression buffer\n", uncomp_size);
  581. exit(1);
  582. #endif
  583. }
  584. }
  585. if ((uncomp_buf_used + size) > uncomp_size)
  586. {
  587. flush_compressed_chunk(uncomp_buf, uncomp_buf_used);
  588. uncomp_buf_used = 0;
  589. }
  590. if (!size) /* Signal a flush before we start writing the DESC entry */
  591. {
  592. flush_compressed_chunk(uncomp_buf, uncomp_buf_used);
  593. return;
  594. }
  595. if (!uncomp_buf_used)
  596. memset(uncomp_buf, 0, uncomp_size);
  597. while (size > uncomp_size)
  598. {
  599. flush_compressed_chunk(buffer, uncomp_size);
  600. buffer += uncomp_size;
  601. size -= uncomp_size;
  602. }
  603. memcpy(&uncomp_buf[uncomp_buf_used], buffer, size);
  604. uncomp_buf_used += size;
  605. }
  606. /* Loop through the list of DESC entries that we've built up and
  607. append them to the template file */
  608. static void write_template_desc_entries(off_t image_len)
  609. {
  610. entry_t *entry = entry_list;
  611. off_t desc_len = 0;
  612. unsigned char out_len[6];
  613. jigdo_image_entry_t jimage;
  614. desc_len = 16 /* DESC + length twice */
  615. + (sizeof(jigdo_file_entry_t) * num_matches)
  616. + (sizeof(jigdo_chunk_entry_t) * num_chunks)
  617. + sizeof(jigdo_image_entry_t);
  618. write_le48(desc_len, &out_len[0]);
  619. write_compressed_chunk(NULL, 0);
  620. template_fwrite("DESC", 4, 1, t_file);
  621. template_fwrite(out_len, sizeof(out_len), 1, t_file);
  622. while (entry)
  623. {
  624. switch (entry->entry_type)
  625. {
  626. case JTET_FILE_MATCH:
  627. {
  628. jigdo_file_entry_t jfile;
  629. jfile.type = 6; /* Matched file */
  630. write_le48(entry->data.file.file_length, &jfile.fileLen[0]);
  631. write_le64(entry->data.file.rsyncsum, &jfile.fileRsync[0]);
  632. memcpy(jfile.fileMD5, entry->data.file.md5, sizeof(jfile.fileMD5));
  633. template_fwrite(&jfile, sizeof(jfile), 1, t_file);
  634. break;
  635. }
  636. case JTET_NOMATCH:
  637. {
  638. jigdo_chunk_entry_t jchunk;
  639. jchunk.type = 2; /* Raw data, compressed */
  640. write_le48(entry->data.chunk.uncompressed_length, &jchunk.skipLen[0]);
  641. template_fwrite(&jchunk, sizeof(jchunk), 1, t_file);
  642. break;
  643. }
  644. }
  645. entry = entry->next;
  646. }
  647. jimage.type = 5;
  648. write_le48(image_len, &jimage.imageLen[0]);
  649. checksum_copy(iso_context, CHECK_MD5, &jimage.imageMD5[0]);
  650. write_le32(MIN_JIGDO_FILE_SIZE, &jimage.blockLen[0]);
  651. template_fwrite(&jimage, sizeof(jimage), 1, t_file);
  652. template_fwrite(out_len, sizeof(out_len), 1, t_file);
  653. }
  654. /* Dump a buffer in jigdo-style "base64" */
  655. static char *base64_dump(unsigned char *buf, size_t buf_size)
  656. {
  657. const char *b64_enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  658. int value = 0;
  659. unsigned int i;
  660. int bits = 0;
  661. static char output_buffer[2048];
  662. char *p = output_buffer;
  663. memset(output_buffer, 0, sizeof(output_buffer));
  664. if (buf_size >= (sizeof(output_buffer) * 6/8))
  665. {
  666. fprintf(stderr, "base64_dump: Buffer too small!\n");
  667. exit(1);
  668. }
  669. for (i = 0; i < buf_size ; i++)
  670. {
  671. value = (value << 8) | buf[i];
  672. bits += 2;
  673. p += sprintf(p, "%c", b64_enc[(value >> bits) & 63U]);
  674. if (bits >= 6) {
  675. bits -= 6;
  676. p += sprintf(p, "%c", b64_enc[(value >> bits) & 63U]);
  677. }
  678. }
  679. if (bits > 0)
  680. {
  681. value <<= 6 - bits;
  682. p += sprintf(p, "%c", b64_enc[value & 63U]);
  683. }
  684. return output_buffer;
  685. }
  686. /* Write the .jigdo file to match the .template we've just finished. */
  687. static void write_jigdo_file(void)
  688. {
  689. unsigned char template_md5sum[16];
  690. entry_t *entry = entry_list;
  691. struct path_mapping *map = map_list;
  692. int i = 0;
  693. struct checksum_info *info = NULL;
  694. checksum_final(template_context);
  695. checksum_copy(template_context, CHECK_MD5, &template_md5sum[0]);
  696. fprintf(j_file, "# JigsawDownload\n");
  697. fprintf(j_file, "# See <http://atterer.org/jigdo/> for details about jigdo\n");
  698. fprintf(j_file, "# See <http://www.einval.com/~steve/software/CD/JTE/> for details about JTE\n\n");
  699. fprintf(j_file, "[Jigdo]\n");
  700. fprintf(j_file, "Version=%s\n", JIGDO_TEMPLATE_VERSION);
  701. fprintf(j_file, "Generator=%s/%d.%d\n\n", JTE_NAME, JTE_VER_MAJOR, JTE_VER_MINOR);
  702. fprintf(j_file, "[Image]\n");
  703. fprintf(j_file, "Filename=%s\n", file_base_name(outfile));
  704. fprintf(j_file, "Template=http://localhost/%s\n", jtemplate_out);
  705. fprintf(j_file, "Template-MD5Sum=%s \n",
  706. base64_dump(&template_md5sum[0], sizeof(template_md5sum)));
  707. for (i = 0; i < NUM_CHECKSUMS; i++)
  708. {
  709. if (checksum_algo_tmpl & (1 << i))
  710. {
  711. info = checksum_information(i);
  712. fprintf(j_file, "# Template Hex %sSum %s\n", info->name, checksum_hex(template_context, i));
  713. }
  714. }
  715. fprintf(j_file, "# Template size %lld bytes\n", template_size);
  716. for (i = 0; i < NUM_CHECKSUMS; i++)
  717. {
  718. if (checksum_algo_iso & (1 << i))
  719. {
  720. info = checksum_information(i);
  721. fprintf(j_file, "# Image Hex %sSum %s\n", info->name, checksum_hex(iso_context, i));
  722. }
  723. }
  724. fprintf(j_file, "# Image size %lld bytes\n\n", image_size);
  725. fprintf(j_file, "[Parts]\n");
  726. while (entry)
  727. {
  728. if (JTET_FILE_MATCH == entry->entry_type)
  729. {
  730. char *new_name = remap_filename(entry->data.file.filename);
  731. fprintf(j_file, "%s=%s\n",
  732. base64_dump(&entry->data.file.md5[0], sizeof(entry->data.file.md5)),
  733. new_name);
  734. free(new_name);
  735. }
  736. entry = entry->next;
  737. }
  738. fprintf(j_file, "\n[Servers]\n");
  739. fflush(j_file);
  740. }
  741. /* Finish and flush state; for now:
  742. 1. Dump the DESC blocks and the footer information in the jigdo template file
  743. 2. Write the jigdo .jigdo file containing file pointers
  744. */
  745. void write_jt_footer(void)
  746. {
  747. /* Finish calculating the image's checksum */
  748. checksum_final(iso_context);
  749. /* And calculate the image size */
  750. image_size = (unsigned long long)SECTOR_SIZE * last_extent_written;
  751. write_template_desc_entries(image_size);
  752. write_jigdo_file();
  753. }
  754. /* Add a raw data entry to the list of extents; no file to match */
  755. static void add_unmatched_entry(int uncompressed_length)
  756. {
  757. entry_t *new_entry = NULL;
  758. /* Can we extend a previous non-match entry? */
  759. if (entry_last && (JTET_NOMATCH == entry_last->entry_type))
  760. {
  761. entry_last->data.chunk.uncompressed_length += uncompressed_length;
  762. return;
  763. }
  764. new_entry = calloc(1, sizeof(entry_t));
  765. new_entry->entry_type = JTET_NOMATCH;
  766. new_entry->next = NULL;
  767. new_entry->data.chunk.uncompressed_length = uncompressed_length;
  768. /* Add to the end of the list */
  769. if (NULL == entry_last)
  770. {
  771. entry_last = new_entry;
  772. entry_list = new_entry;
  773. }
  774. else
  775. {
  776. entry_last->next = new_entry;
  777. entry_last = new_entry;
  778. }
  779. num_chunks++;
  780. }
  781. /* Add a file match entry to the list of extents */
  782. static void add_file_entry(char *filename, off_t size, unsigned char *md5,
  783. unsigned long long rsyncsum)
  784. {
  785. entry_t *new_entry = NULL;
  786. new_entry = calloc(1, sizeof(entry_t));
  787. new_entry->entry_type = JTET_FILE_MATCH;
  788. new_entry->next = NULL;
  789. memcpy(new_entry->data.file.md5, md5, sizeof(new_entry->data.file.md5));
  790. new_entry->data.file.file_length = size;
  791. new_entry->data.file.rsyncsum = rsyncsum;
  792. new_entry->data.file.filename = strdup(filename);
  793. /* Add to the end of the list */
  794. if (NULL == entry_last)
  795. {
  796. entry_last = new_entry;
  797. entry_list = new_entry;
  798. }
  799. else
  800. {
  801. entry_last->next = new_entry;
  802. entry_last = new_entry;
  803. }
  804. num_matches++;
  805. }
  806. /* Cope with an unmatched block in the .iso file:
  807. 1. Write a compressed data chunk in the jigdo template file
  808. 2. Add an entry in our list of unmatched chunks for later */
  809. void jtwrite(buffer, size, count, submode, islast)
  810. void *buffer;
  811. int size;
  812. int count;
  813. int submode;
  814. BOOL islast;
  815. {
  816. #ifdef JTWRITE_DEBUG
  817. if (count != 1 || (size % 2048) != 0)
  818. error("Count: %d, size: %d\n", count, size);
  819. #endif
  820. if (!jtemplate_out)
  821. return;
  822. /* Update the global image checksum */
  823. checksum_update(iso_context, buffer, size * count);
  824. // mk_MD5Update(&iso_context, buffer, size*count);
  825. /* Write a compressed version of the data to the template file,
  826. and add a reference on the state list so we can write that
  827. later. */
  828. write_compressed_chunk(buffer, size*count);
  829. add_unmatched_entry(size*count);
  830. }
  831. /* Cope with a file entry in the .iso file:
  832. 1. Read the file for the image's md5 checksum
  833. 2. Add an entry in our list of files to be written into the .jigdo later
  834. */
  835. void write_jt_match_record(char *filename, char *mirror_name, int sector_size, off_t size, unsigned char md5[16])
  836. {
  837. unsigned long long tmp_size = 0;
  838. char buf[32768];
  839. off_t remain = size;
  840. FILE *infile = NULL;
  841. int use = 0;
  842. unsigned long long rsync64_sum = 0;
  843. int first_block = 1;
  844. memset(buf, 0, sizeof(buf));
  845. if ((infile = fopen(filename, "rb")) == NULL) {
  846. #ifdef USE_LIBSCHILY
  847. comerr("cannot open '%s'\n", filename);
  848. #else
  849. #ifndef HAVE_STRERROR
  850. fprintf(stderr, "cannot open '%s': (%d)\n",
  851. filename, errno);
  852. #else
  853. fprintf(stderr, "cannot open '%s': %s\n",
  854. filename, strerror(errno));
  855. #endif
  856. exit(1);
  857. #endif
  858. }
  859. while (remain > 0)
  860. {
  861. use = remain;
  862. if (remain > sizeof(buf))
  863. use = sizeof(buf);
  864. if (fread(buf, 1, use, infile) == 0)
  865. {
  866. #ifdef USE_LIBSCHILY
  867. comerr("cannot read from '%s'\n", filename);
  868. #else
  869. fprintf(stderr, "cannot read from '%s'\n", filename);
  870. exit(1);
  871. #endif
  872. }
  873. if (first_block)
  874. rsync64_sum = rsync64((unsigned char *)buf, MIN_JIGDO_FILE_SIZE);
  875. checksum_update(iso_context, (unsigned char *)buf, use);
  876. // mk_MD5Update(&iso_context, buf, use);
  877. remain -= use;
  878. first_block = 0;
  879. }
  880. fclose(infile);
  881. /* Update the image checksum with any necessary padding data */
  882. if (size % sector_size)
  883. {
  884. int pad_size = sector_size - (size % sector_size);
  885. memset(buf, 0, pad_size);
  886. checksum_update(iso_context, (unsigned char *)buf, pad_size);
  887. // mk_MD5Update(&iso_context, buf, pad_size);
  888. }
  889. add_file_entry(mirror_name, size, &md5[0], rsync64_sum);
  890. if (size % sector_size)
  891. {
  892. int pad_size = sector_size - (size % sector_size);
  893. write_compressed_chunk((unsigned char *)buf, pad_size);
  894. add_unmatched_entry(pad_size);
  895. }
  896. }