[Debburn-changes] r827 - cdrkit/trunk/genisoimage
Steve McIntyre
93sam at alioth.debian.org
Mon May 11 00:27:09 UTC 2009
Author: 93sam
Date: 2009-05-11 00:27:09 +0000 (Mon, 11 May 2009)
New Revision: 827
Modified:
cdrkit/trunk/genisoimage/checksum.c
cdrkit/trunk/genisoimage/checksum.h
cdrkit/trunk/genisoimage/jte.c
Log:
Slight refactor: move the hexdump code into checksum.c
Modified: cdrkit/trunk/genisoimage/checksum.c
===================================================================
--- cdrkit/trunk/genisoimage/checksum.c 2009-05-10 22:19:55 UTC (rev 826)
+++ cdrkit/trunk/genisoimage/checksum.c 2009-05-11 00:27:09 UTC (rev 827)
@@ -128,6 +128,8 @@
void *context;
unsigned char *digest;
int enabled;
+ int finalised;
+ char *hexdump;
};
struct _checksum_context
@@ -141,6 +143,17 @@
return (struct checksum_info *)&algorithms[which];
}
+/* Dump a buffer in hex */
+static void hex_dump_to_buffer(char *output_buffer, unsigned char *buf, size_t buf_size)
+{
+ unsigned int i;
+ char *p = output_buffer;
+
+ memset(output_buffer, 0, 1 + (2*buf_size));
+ for (i = 0; i < buf_size ; i++)
+ p += sprintf(p, "%2.2x", buf[i]);
+}
+
checksum_context_t *checksum_init_context(int checksums, const char *owner)
{
int i = 0;
@@ -165,8 +178,12 @@
context->algo[i].digest = malloc(algorithms[i].digest_size);
if (!context->algo[i].digest)
return NULL;
+ context->algo[i].hexdump = malloc(1 + (2*algorithms[i].digest_size));
+ if (!context->algo[i].hexdump)
+ return NULL;
algorithms[i].init(context->algo[i].context);
context->algo[i].enabled = 1;
+ context->algo[i].finalised = 0;
}
else
context->algo[i].enabled = 0;
@@ -184,6 +201,7 @@
{
free(c->algo[i].context);
free(c->algo[i].digest);
+ free(c->algo[i].hexdump);
}
free(c->owner);
free(c);
@@ -210,7 +228,11 @@
for (i = 0; i < NUM_CHECKSUMS; i++)
{
if (c->algo[i].enabled)
+ {
algorithms[i].final(c->algo[i].digest, c->algo[i].context);
+ hex_dump_to_buffer(c->algo[i].hexdump, c->algo[i].digest, algorithms[i].digest_size);
+ c->algo[i].finalised = 1;
+ }
}
}
@@ -221,12 +243,29 @@
struct _checksum_context *c = context;
if (c->algo[which].enabled)
- memcpy(digest, c->algo[which].digest, algorithms[which].digest_size);
+ {
+ if (c->algo[which].finalised)
+ memcpy(digest, c->algo[which].digest, algorithms[which].digest_size);
+ else
+ memset(digest, 0, algorithms[which].digest_size);
+ }
else
fprintf(stderr, "Asked for %s checksum, not enabled!\n",
algorithms[which].name);
}
+const char *checksum_hex(checksum_context_t *context,
+ enum checksum_types which)
+{
+ struct _checksum_context *c = context;
+
+ if (c->algo[which].enabled && c->algo[which].finalised)
+ return c->algo[which].hexdump;
+
+ /* else */
+ return NULL;
+}
+
#ifdef CHECKSUM_SELF_TEST
#include <sys/types.h>
#include <sys/stat.h>
Modified: cdrkit/trunk/genisoimage/checksum.h
===================================================================
--- cdrkit/trunk/genisoimage/checksum.h 2009-05-10 22:19:55 UTC (rev 826)
+++ cdrkit/trunk/genisoimage/checksum.h 2009-05-11 00:27:09 UTC (rev 827)
@@ -56,7 +56,16 @@
/* Extract a particular algorithm's checksum once checksum_final() has
* been called. Use the details in checksum_information() above first
- * to see how big the digest will be. */
+ * to see how big the digest will be. Only valid once checksum_final()
+ * has been called, otherwise the digest returned will be all
+ * zeroes. */
void checksum_copy(checksum_context_t *context,
enum checksum_types which,
unsigned char *digest);
+
+/* Helper function: return a pointer to a string containing the ASCII
+ * hexadecimal dump of a checksum. Only valid once checksum_final()
+ * has been called, otherwise will return NULL */
+const char * checksum_hex(checksum_context_t *context,
+ enum checksum_types which);
+
Modified: cdrkit/trunk/genisoimage/jte.c
===================================================================
--- cdrkit/trunk/genisoimage/jte.c 2009-05-10 22:19:55 UTC (rev 826)
+++ cdrkit/trunk/genisoimage/jte.c 2009-05-11 00:27:09 UTC (rev 827)
@@ -83,11 +83,6 @@
static checksum_context_t *iso_context = NULL;
static checksum_context_t *template_context = NULL;
-unsigned char image_md5[16]; /* MD5SUM of the entire image */
-unsigned char image_sha1[20]; /* SHA1SUM of the entire image */
-unsigned char image_sha256[32]; /* SHA1SUM of the entire image */
-unsigned char image_sha512[64]; /* SHA1SUM of the entire image */
-
#define CHECK_USED_ISO (CHECK_MD5_USED | CHECK_SHA1_USED | CHECK_SHA256_USED | CHECK_SHA512_USED)
#define CHECK_USED_TPL (CHECK_MD5_USED)
@@ -172,26 +167,6 @@
return endptr;
}
-/* Dump a buffer in hex */
-static char *hex_dump(unsigned char *buf, size_t buf_size)
-{
- unsigned int i;
- static char output_buffer[2048];
- char *p = output_buffer;
-
- memset(output_buffer, 0, sizeof(output_buffer));
- if (buf_size >= (sizeof(output_buffer) / 2))
- {
- fprintf(stderr, "hex_dump: Buffer too small!\n");
- exit(1);
- }
-
- for (i = 0; i < buf_size ; i++)
- p += sprintf(p, "%2.2x", buf[i]);
-
- return output_buffer;
-}
-
/* Build the list of exclusion regexps */
extern int jte_add_exclude(char *pattern)
{
@@ -731,7 +706,7 @@
/* Loop through the list of DESC entries that we've built up and
append them to the template file */
-static void write_template_desc_entries(off_t image_len, char *image_md5)
+static void write_template_desc_entries(off_t image_len)
{
entry_t *entry = entry_list;
off_t desc_len = 0;
@@ -776,7 +751,7 @@
jimage.type = 5;
write_le48(image_len, &jimage.imageLen[0]);
- memcpy(jimage.imageMD5, image_md5, sizeof(jimage.imageMD5));
+ memcpy(jimage.imageMD5, checksum_hex(iso_context, CHECK_MD5), sizeof(jimage.imageMD5));
write_le32(MIN_JIGDO_FILE_SIZE, &jimage.blockLen[0]);
template_fwrite(&jimage, sizeof(jimage), 1, t_file);
template_fwrite(out_len, sizeof(out_len), 1, t_file);
@@ -823,6 +798,8 @@
unsigned char template_md5sum[16];
entry_t *entry = entry_list;
struct path_mapping *map = map_list;
+ int i = 0;
+ struct checksum_info *info = NULL;
checksum_final(template_context);
checksum_copy(template_context, CHECK_MD5, &template_md5sum[0]);
@@ -841,17 +818,18 @@
fprintf(j_file, "Template-MD5Sum=%s \n",
base64_dump(&template_md5sum[0], sizeof(template_md5sum)));
- fprintf(j_file, "# Template Hex MD5sum %s\n",
- hex_dump(&template_md5sum[0], sizeof(template_md5sum)));
+ fprintf(j_file, "# Template Hex MD5sum %s\n", checksum_hex(template_context, CHECK_MD5));
fprintf(j_file, "# Template size %lld bytes\n", template_size);
- fprintf(j_file, "# Image Hex MD5Sum %s\n",
- hex_dump(&image_md5[0], sizeof(image_md5)));
- fprintf(j_file, "# Image Hex SHA1Sum %s\n",
- hex_dump(&image_sha1[0], sizeof(image_sha1)));
- fprintf(j_file, "# Image Hex SHA256Sum %s\n",
- hex_dump(&image_sha256[0], sizeof(image_sha256)));
- fprintf(j_file, "# Image Hex SHA512Sum %s\n",
- hex_dump(&image_sha512[0], sizeof(image_sha512)));
+
+ for (i = 0; i < NUM_CHECKSUMS; i++)
+ {
+ if (CHECK_USED_ISO & (1 << i))
+ {
+ info = checksum_information(i);
+ fprintf(j_file, "# Image Hex %sSum %s\n", info->name, checksum_hex(iso_context, i));
+ }
+ }
+
fprintf(j_file, "# Image size %lld bytes\n\n", image_size);
fprintf(j_file, "[Parts]\n");
@@ -881,15 +859,11 @@
{
/* Finish calculating the image's checksum */
checksum_final(iso_context);
- checksum_copy(iso_context, CHECK_MD5, &image_md5[0]);
- checksum_copy(iso_context, CHECK_SHA1, &image_sha1[0]);
- checksum_copy(iso_context, CHECK_SHA256, &image_sha256[0]);
- checksum_copy(iso_context, CHECK_SHA512, &image_sha512[0]);
/* And calculate the image size */
image_size = (unsigned long long)SECTOR_SIZE * last_extent_written;
- write_template_desc_entries(image_size, (char *)image_md5);
+ write_template_desc_entries(image_size);
write_jigdo_file();
}
More information about the Debburn-changes
mailing list