Преглед изворни кода

Modified mlib.c, to malloc enough space for the digest, instead of
modifying the static buffer that was previously passed in.

Adam Heath пре 25 година
родитељ
комит
f60b25e022
3 измењених фајлова са 27 додато и 16 уклоњено
  1. 6 0
      ChangeLog
  2. 14 9
      lib/mlib.c
  3. 7 7
      utils/md5sum.c

+ 6 - 0
ChangeLog

@@ -1,3 +1,9 @@
+Wed Apr 25 20:20:07 CDT 2001 Adam Heath <doogie@debian.org>
+
+  * utils/md5sum.c, lib/mlib.c: Modified mlib.c, to malloc enough space
+    for the digest, instead of modifying the static buffer that was
+    previously passed in.
+
 Thu Apr 26 00:32:35 CEST 2001 Wichert Akkerman <wakkerma@debian.org>
 
   * po/da.po: Updated

+ 14 - 9
lib/mlib.c

@@ -136,18 +136,20 @@ int waitsubproc(pid_t pid, const char *description, int flags) {
   return checksubprocerr(status,description,flags);
 }
 
+struct buffer_write_md5ctx {
+  struct MD5Context ctx;
+  unsigned char **hash;
+};
 ssize_t buffer_write(buffer_data_t data, void *buf, ssize_t length, const char *desc) {
   ssize_t ret= length;
   if(data->type & BUFFER_WRITE_SETUP) {
     switch(data->type ^ BUFFER_WRITE_SETUP) {
       case BUFFER_WRITE_MD5:
 	{
-	  unsigned char *hash= data->data.ptr;
-	  void ** array= malloc(sizeof(void *) * 2);
-	  data->data.ptr= array;
-	  array[0]= hash;
-	  array[1]= malloc(sizeof(struct MD5Context));
-	  MD5Init((struct MD5Context *)array[1]);
+	  struct buffer_write_md5ctx *ctx = malloc(sizeof(struct buffer_write_md5ctx));
+	  ctx->hash = data->data.ptr;
+	  data->data.ptr = ctx;
+	  MD5Init(&ctx->ctx);
 	}
 	break;
     }
@@ -159,12 +161,15 @@ ssize_t buffer_write(buffer_data_t data, void *buf, ssize_t length, const char *
 	{
 	  int i;
 	  unsigned char digest[16], *p = digest;
-	  unsigned char *hash= (unsigned char *)((void **)data->data.ptr)[0];
-	  MD5Final(digest, (struct MD5Context *)((void **)data->data.ptr)[1]);
+	  struct buffer_write_md5ctx *ctx = (struct buffer_write_md5ctx *)data->data.ptr;
+	  unsigned char *hash = *ctx->hash = malloc(33);
+	  MD5Final(digest, &ctx->ctx);
 	  for (i = 0; i < 16; ++i) {
 	    sprintf(hash, "%02x", *p++);
 	    hash += 2;
 	  }
+	  *hash = 0;
+	  free(ctx);
 	}
 	break;
     }
@@ -192,7 +197,7 @@ ssize_t buffer_write(buffer_data_t data, void *buf, ssize_t length, const char *
 	ohshite(_("error in buffer_write(stream): %s"), desc);
       break;
     case BUFFER_WRITE_MD5:
-      MD5Update((struct MD5Context *)((void **)data->data.ptr)[1], buf, length);
+      MD5Update(&(((struct buffer_write_md5ctx *)data->data.ptr)->ctx), buf, length);
       break;
     default:
       fprintf(stderr, _("unknown data type `%i' in buffer_write\n"), data->type);

+ 7 - 7
utils/md5sum.c

@@ -63,7 +63,7 @@ const char thisname[]= MD5SUM;
 
 void usage(void);
 void print_digest(unsigned char *p);
-int mdfile(int fd, unsigned char *digest);
+int mdfile(int fd, unsigned char **digest);
 int do_check(FILE *chkf);
 int hex_digit(int c);
 int get_md5_line(FILE *fp, unsigned char *digest, char *file);
@@ -79,7 +79,7 @@ main(int argc, char **argv)
 	int opt, rc = 0;
 	int check = 0;
 	FILE *fp = NULL;
-	unsigned char digest[16];
+	unsigned char *digest = NULL;
 
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
@@ -109,7 +109,7 @@ main(int argc, char **argv)
 		exit(do_check(fp));
 	}
 	if (argc == 0) {
-		if (mdfile(fileno(stdin), digest)) {
+		if (mdfile(fileno(stdin), &digest)) {
 			fprintf(stderr, _("%s: read error on stdin\n"), progname);
 			exit(2);
 		}
@@ -126,7 +126,7 @@ main(int argc, char **argv)
 			rc = 2;
 			continue;
 		}
-		if (mdfile(fileno(fp), digest)) {
+		if (mdfile(fileno(fp), &digest)) {
 			fprintf(stderr, _("%s: error reading %s\n"), progname, *argv);
 			rc = 2;
 		} else {
@@ -152,7 +152,7 @@ that is printed on stdout by this program when it generates digests.\n"), stderr
 }
 
 int
-mdfile(int fd, unsigned char *digest)
+mdfile(int fd, unsigned char **digest)
 {
 	ssize_t ret = fd_md5(fd, digest, -1, _("mdfile"));
 	if ( ret >= 0 )
@@ -215,7 +215,7 @@ int
 do_check(FILE *chkf)
 {
 	int rc, ex = 0, failed = 0, checked = 0;
-	unsigned char chk_digest[16], file_digest[16];
+	unsigned char chk_digest[16], *file_digest = NULL;
 	char filename[256];
 	FILE *fp;
 	size_t flen = 14;
@@ -237,7 +237,7 @@ do_check(FILE *chkf)
 			ex = 2;
 			continue;
 		}
-		if (mdfile(fileno(fp), file_digest)) {
+		if (mdfile(fileno(fp), &file_digest)) {
 			fprintf(stderr, _("%s: error reading %s\n"), progname, filename);
 			ex = 2;
 			fclose(fp);