new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_crypto / md5_sha1 / md5_sha1_op.c
old mode 100755 (executable)
new mode 100644 (file)
index c2a168a..dd367c7
@@ -1,5 +1,3 @@
-/* @(#) $Id$ */
-
 /* Copyright (C) 2009 Trend Micro Inc.
  * All right reserved.
  *
@@ -9,75 +7,82 @@
  * Foundation
  */
 
-
 #include <stdio.h>
 #include <string.h>
-#include "md5_sha1_op.h"
 
+#include "md5_sha1_op.h"
 #include "../md5/md5.h"
 #include "../sha1/sha.h"
+#include "headers/defs.h"
 
 
-int OS_MD5_SHA1_File(char * fname, char *md5output, char *sha1output)
+int OS_MD5_SHA1_File(const char *fname, const char *prefilter_cmd, os_md5 md5output, os_sha1 sha1output, int mode)
 {
-    int n;
+    size_t n;
     FILE *fp;
-    unsigned char buf[2048 +2];
+    unsigned char buf[2048 + 2];
     unsigned char sha1_digest[SHA_DIGEST_LENGTH];
     unsigned char md5_digest[16];
 
     SHA_CTX sha1_ctx;
     MD5_CTX md5_ctx;
 
-    
-    /* Clearing the memory. */
+    /* Clear the memory */
     md5output[0] = '\0';
     sha1output[0] = '\0';
-    buf[2048 +1] = '\0';
-
-    fp = fopen(fname,"r");
-    if(!fp)
-        return(-1);
-
+    buf[2048 + 1] = '\0';
+
+    /* Use prefilter_cmd if set */
+    if (prefilter_cmd == NULL) {
+        fp = fopen(fname, mode == OS_BINARY ? "rb" : "r");
+        if (!fp) {
+            return (-1);
+        }
+    } else {
+        char cmd[OS_MAXSTR];
+        size_t target_length = strlen(prefilter_cmd) + 1 + strlen(fname);
+        int res = snprintf(cmd, sizeof(cmd), "%s %s", prefilter_cmd, fname);
+        if (res < 0 || (unsigned int)res != target_length) {
+            return (-1);
+        }
+        fp = popen(cmd, "r");
+        if (!fp) {
+            return (-1);
+        }
+    }
 
-    /* Initializing both hashes */
+    /* Initialize both hashes */
     MD5Init(&md5_ctx);
     SHA1_Init(&sha1_ctx);
 
-
-    /* Updating for each one. */
-    while((n = fread(buf, 1, 2048, fp)) > 0)
-    {
+    /* Update for each one */
+    while ((n = fread(buf, 1, 2048, fp)) > 0) {
         buf[n] = '\0';
-        SHA1_Update(&sha1_ctx, buf, (unsigned long)n);
-        MD5Update(&md5_ctx, buf, n);
+        SHA1_Update(&sha1_ctx, buf, n);
+        MD5Update(&md5_ctx, buf, (unsigned)n);
     }
 
     SHA1_Final(&(sha1_digest[0]), &sha1_ctx);
     MD5Final(md5_digest, &md5_ctx);
 
-
-    /* Setting output for md5. */
-    for(n = 0;n < 16; n++)
-    {
+    /* Set output for MD5 */
+    for (n = 0; n < 16; n++) {
         snprintf(md5output, 3, "%02x", md5_digest[n]);
-        md5output+=2;
+        md5output += 2;
     }
 
-    /* Setting output for sha1. */
-    for (n = 0; n<SHA_DIGEST_LENGTH; n++)
-    {
+    /* Set output for SHA-1 */
+    for (n = 0; n < SHA_DIGEST_LENGTH; n++) {
         snprintf(sha1output, 3, "%02x", sha1_digest[n]);
-        sha1output+=2;
+        sha1output += 2;
     }
 
+    /* Close it */
+    if (prefilter_cmd == NULL) {
+        fclose(fp);
+    } else {
+        pclose(fp);
+    }
 
-    /* Closing it */
-    fclose(fp);
-
-    return(0);
+    return (0);
 }
-
-
-/* EOF */