new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_crypto / md5 / md5_op.c
old mode 100755 (executable)
new mode 100644 (file)
index 6785697..c299aa4
@@ -1,5 +1,3 @@
-/*      $OSSEC, os_crypto/md5_op.c, v0.2, 2005/09/17, Daniel B. Cid$      */
-
 /* Copyright (C) 2009 Trend Micro Inc.
  * All right reserved.
  *
@@ -9,80 +7,68 @@
  * Foundation
  */
 
-/* v0.2 (2005/09/17): char fixes (signal)
- * v0.1 (2004/08/09)
- */
-
-/* OS_crypto/md5 Library.
- * APIs for many crypto operations.
+/* OS_crypto/md5 Library
+ * APIs for many crypto operations
  */
 
-
 #include <stdio.h>
 #include <string.h>
+
+#include "md5_op.h"
 #include "md5.h"
+#include "headers/defs.h"
+
 
-int OS_MD5_File(char * fname, char * output)
+int OS_MD5_File(const char *fname, os_md5 output, int mode)
 {
     FILE *fp;
     MD5_CTX ctx;
-    unsigned char buf[1024 +1];
+    unsigned char buf[1024 + 1];
     unsigned char digest[16];
-    int n;
+    size_t n;
 
-    memset(output,0, 33);
+    memset(output, 0, 33);
     buf[1024] = '\0';
 
-    fp = fopen(fname,"r");
-    if(!fp)
-    {
-        return(-1);
+    fp = fopen(fname, mode == OS_BINARY ? "rb" : "r");
+    if (!fp) {
+        return (-1);
     }
 
     MD5Init(&ctx);
-    while((n = fread(buf, 1, sizeof(buf) -1, fp)) > 0)
-    {
+    while ((n = fread(buf, 1, sizeof(buf) - 1, fp)) > 0) {
         buf[n] = '\0';
-        MD5Update(&ctx,buf,n);
+        MD5Update(&ctx, buf, (unsigned)n);
     }
 
     MD5Final(digest, &ctx);
 
-    for(n = 0;n < 16; n++)
-    {
+    for (n = 0; n < 16; n++) {
         snprintf(output, 3, "%02x", digest[n]);
-        output+=2;
+        output += 2;
     }
 
-    /* Closing it */
     fclose(fp);
 
-    return(0);
+    return (0);
 }
 
-/* EOF */
-int OS_MD5_Str(char * str, char * output)
+int OS_MD5_Str(const char *str, os_md5 output)
 {
     unsigned char digest[16];
 
     int n;
 
     MD5_CTX ctx;
-
     MD5Init(&ctx);
-
-    MD5Update(&ctx,(unsigned char *)str,strlen(str));
-
+    MD5Update(&ctx, (const unsigned char *)str, (unsigned)strlen(str));
     MD5Final(digest, &ctx);
 
     output[32] = '\0';
-    for(n = 0;n < 16;n++)
-    {
+    for (n = 0; n < 16; n++) {
         snprintf(output, 3, "%02x", digest[n]);
-        output+=2;
+        output += 2;
     }
 
-    return(0);
+    return (0);
 }
-
-/* EOF */