shithub: jbig2

Download patch

ref: 950df8201674a19af5fa5ff61ba7b006b0691edb
parent: e21659fda78e53fdcbed87290a51a178d26788f6
author: giles <giles@ded80894-8fb9-0310-811b-c03f3676ab4d>
date: Wed Jul 17 19:59:29 EDT 2002

Use the recommended 'generalized' digest api from openssl, still computing
SHA-1. Treat the hash bytes as unsigned; update the ubc hash values to
properly printed ones. N.B.: the printing hash still doesn't match the one
generated by invoking 'openssl dgst -sha1 -hex' on the pbm output with the
header stripped. I don't understand why.


git-svn-id: http://svn.ghostscript.com/jbig2dec/trunk@144 ded80894-8fb9-0310-811b-c03f3676ab4d

--- a/jbig2dec.c
+++ b/jbig2dec.c
@@ -8,7 +8,7 @@
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    $Id: jbig2dec.c,v 1.34 2002/07/16 21:19:09 giles Exp $
+    $Id: jbig2dec.c,v 1.35 2002/07/17 23:59:29 giles Exp $
 */
 
 #ifdef HAVE_CONFIG_H
@@ -36,7 +36,7 @@
 #endif
 
 #ifdef HAVE_OPENSSL_SHA1
-# include <openssl/sha.h>
+# include <openssl/evp.h>
 #endif
 
 typedef enum {
@@ -47,7 +47,7 @@
 	jbig2dec_mode mode;
 	int verbose, hash;
 #ifdef HAVE_OPENSSL_SHA1
-        SHA_CTX *hash_ctx;
+        EVP_MD_CTX *hash_ctx;
 #endif
 	char *output_file;
 } jbig2dec_params_t;
@@ -60,13 +60,15 @@
 hash_init(jbig2dec_params_t *params)
 {
 #ifdef HAVE_OPENSSL_SHA1
-    params->hash_ctx = malloc(sizeof(SHA_CTX));
+    params->hash_ctx = malloc(sizeof(EVP_MD_CTX));
     if (params->hash == NULL) {
         fprintf(stderr, "unable to allocate hash state\n");
         params->hash = 0;
         return;
     } else {
-        SHA1_Init(params->hash_ctx);
+        // FIXME: is this call ever necessary?
+        //OpenSSL_add_all_digests();
+        EVP_DigestInit(params->hash_ctx, EVP_sha1());
     }
 #endif /* HAVE_OPENSSL_SHA1 */
 }
@@ -74,9 +76,9 @@
 static void
 hash_image(jbig2dec_params_t *params, Jbig2Image *image)
 {
-    int N = image->stride * image->height;
+    unsigned int N = image->stride * image->height;
 #ifdef HAVE_OPENSSL_SHA1
-    SHA1_Update(params->hash_ctx, image->data, N);
+    EVP_DigestUpdate(params->hash_ctx, image->data, N);
 #endif
 }
 
@@ -84,12 +86,12 @@
 hash_print(jbig2dec_params_t *params, FILE *out)
 {
 #ifdef HAVE_OPENSSL_SHA1
-    char md[SHA_DIGEST_LENGTH];
-    char digest[2*SHA_DIGEST_LENGTH + 1];
-    int i;
+    unsigned char md[EVP_MAX_MD_SIZE];
+    char digest[2*EVP_MAX_MD_SIZE + 1];
+    int i, len;
     
-    SHA1_Final(md, params->hash_ctx);
-    for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
+    EVP_DigestFinal(params->hash_ctx, md, &len);
+    for (i = 0; i < len; i++) {
         snprintf(&(digest[2*i]), 3, "%02x", md[i]);
     }
     fprintf(out, "%s", digest);
--- a/test_jbig2dec.py
+++ b/test_jbig2dec.py
@@ -10,79 +10,79 @@
   # (filename, sha-1(file), sha-1(decoded document)
   known_hashes = ( ('../ubc/042_1.jb2',
 			"673e1ee5c55ab241b171e476ba1168a42733ddaa",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_2.jb2', 
                         "9aa2804e2d220952035c16fb3c907547884067c5",
-                        "ffffffffff5fffff2271ffff19ff0e52712046ff"),
+                        "ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_3.jb2',
 			"9663a5f35727f13e61a0a2f0a64207b1f79e7d67",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_4.jb2',
 			"014df658c8b99b600c2ceac3f1d53c7cc2b4917c",
-                        "ffffffffff5fffff2271ffff19ff0e52712046ff"),
+                        "ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_5.jb2',
 			"264720a6ccbbf72aa6a2cfb6343f43b8e6f2da4b",
-                        "ffffffffff5fffff2271ffff19ff0e52712046ff"),
+                        "ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_6.jb2',
 			"96f7dc9df4a1b305f9ac082dd136f85ef5b108fe",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_7.jb2',
 			"5526371ba9dc2b8743f20ae3e05a7e60b3dcba76",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_8.jb2',
 			"4bf0c87dfaf40d67c36f2a083579eeda26d54641",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_9.jb2',
 			"53e630e7fe2fe6e1d6164758e15fc93382e07f55",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_10.jb2',
 			"5ca1364367e25cb8f642e9dc677a94d5cfed0c8b",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_11.jb2',
 			"bc194caf022bc5345fc41259e05cea3c08245216",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_12.jb2',
 			"f354df8eb4849bc707f088739e322d1fe3a14ef3",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_13.jb2',
 			"7d428bd542f58591b254d9827f554b0552c950a7",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_14.jb2',
 			"c40fe3a02acb6359baf9b40fc9c49bc0800be589",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_15.jb2',
 			"a9e39fc1ecb178aec9f05039514d75ea3246246c",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_16.jb2',
 			"4008bbca43670f3c90eaee26516293ba95baaf3d",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_17.jb2',
 			"0ff95637b64c57d659a41c582da03e25321551fb",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_18.jb2',
 			"87381d044f00c4329200e44decbe91bebfa31595",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_19.jb2',
 			"387d95a140b456d4742622c788cf5b51cebbf438",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_20.jb2',
 			"85c19e9ec42b8ddd6b860a1bebea1c67610e7a59",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_21.jb2',
 			"ab535c7d7a61a7b9dc53d546e7419ca78ac7f447",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_22.jb2',
 			"a9e2b365be63716dbde74b0661c3c6efd2a6844d",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_23.jb2',
 			"8ffa40a05e93e10982b38a2233a8da58c1b5c343",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_24.jb2',
 			"2553fe65111c58f6412de51d8cdc71651e778ccf",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/042_25.jb2',
 			"52de4a3b86252d896a8d783ba71dd0699333dd69",
-			"ffffffffff5fffff2271ffff19ff0e52712046ff"),
+			"ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"),
                    ('../ubc/amb_1.jb2', # we don't have a correct hash for this yet
 			"d6d6d1c981dc37a09108c1e3ed990aa5b345fa6a",
                         "ff32ffff0776ff66ff254129ff28ffffffff6bff"),
@@ -98,8 +98,10 @@
     self.file_hash = file_hash
     self.decode_hash = decode_hash
     unittest.TestCase.__init__(self)
+
   def shortDescription(self):
     return "Checking '%s' for correct decoded document hash" % self.file
+
   def runTest(self):
     '''jbig2dec should return proper document hashes for known files'''
     # invoke jbig2dec on our file