Merge pull request #3707 from lateralusX/jlorenss/win-api-family-support-libmonoutils
[mono.git] / mono / utils / mono-sha1.c
index c4189915dd68a9bf62b6639bfcefd9685aa0f81f..372ef2bcee9fe958c32d98ac2812d7eefc90d62d 100644 (file)
@@ -72,22 +72,30 @@ A million repetitions of "a"
 
 #include <stdio.h>
 #include <string.h>
+#include <glib.h>
 #include "mono-digest.h"
 
-#ifndef  i386   /* For ALPHA  (SAK) */
-#define LITTLE_ENDIAN 
-typedef          long int int64;
-typedef unsigned long int uint64;
-typedef          int int32;
-typedef unsigned int uint32;
-#else  /*i386*/
-#define LITTLE_ENDIAN 
-typedef          long long int int64;
-typedef unsigned long long int uint64;
-typedef          long int int32;
-typedef unsigned long int uint32;
-#endif /*i386*/
+#if HAVE_COMMONCRYPTO_COMMONDIGEST_H
 
+void
+mono_sha1_init (MonoSHA1Context* context)
+{
+       CC_SHA1_Init (context);
+}
+
+void
+mono_sha1_update (MonoSHA1Context* context, const guchar* data, guint32 len)
+{
+       CC_SHA1_Update (context, data, len);
+}
+
+void
+mono_sha1_final (MonoSHA1Context* context, unsigned char digest[20])
+{
+       CC_SHA1_Final (digest, context);
+}
+
+#else
 
 /* #include <process.h> */     /* prototype for exit() - JHB */
 /* Using return() instead of exit() - SWR */
@@ -98,7 +106,7 @@ static void SHA1Transform(guint32 state[5], const guchar buffer[64]);
 
 /* blk0() and blk() perform the initial expand. */
 /* I got the idea of expanding during the round function from SSLeay */
-#ifdef LITTLE_ENDIAN
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
     |(rol(block->l[i],8)&0x00FF00FF))
 #else
@@ -132,10 +140,10 @@ static void SHAPrintContext(MonoSHA1Context *context, char *msg){
 
 static void SHA1Transform(guint32 state[5], const guchar buffer[64])
 {
-uint32 a, b, c, d, e;
+guint32 a, b, c, d, e;
 typedef union {
     unsigned char c[64];
-    uint32 l[16];
+    guint32 l[16];
 } CHAR64LONG16;
 CHAR64LONG16* block;
 #ifdef SHA1HANDSOFF
@@ -202,7 +210,7 @@ void mono_sha1_init(MonoSHA1Context* context)
 void mono_sha1_update(MonoSHA1Context* context, const guchar* data, guint32 len)       /*
 JHB */
 {
-uint32 i, j;   /* JHB */
+guint32 i, j;  /* JHB */
 
 #ifdef VERBOSE
     SHAPrintContext(context, "before");
@@ -230,7 +238,7 @@ uint32 i, j;        /* JHB */
 
 void mono_sha1_final( MonoSHA1Context* context, unsigned char digest[20])
 {
-uint32 i;      /* JHB */
+guint32 i;     /* JHB */
 unsigned char finalcount[8];
 
     for (i = 0; i < 8; i++) {
@@ -257,9 +265,11 @@ unsigned char finalcount[8];
     SHA1Transform(context->state, context->buffer);
 #endif
 }
+
+#endif
  
 void
-mono_sha1_get_digest (const gchar *buffer, gint buffer_size, guchar digest [20])
+mono_sha1_get_digest (const guchar *buffer, gint buffer_size, guchar digest [20])
 {      
        MonoSHA1Context ctx;
 
@@ -269,6 +279,16 @@ mono_sha1_get_digest (const gchar *buffer, gint buffer_size, guchar digest [20])
        
 }
 
+/**
+ * mono_sha1_get_digest_from_file: get the sha1 hash of a file
+ * @filename: file name
+ * @digest: 20 bytes buffer receiving the hash code.
+ * 
+ * Get the sha1 hash of a file. The result is put in 
+ * the 20 bytes buffer @digest .
+ * 
+ * If an IO error happens the value in @digest is not updated.
+ **/
 void
 mono_sha1_get_digest_from_file (const gchar *filename, guchar digest [20])
 {      
@@ -289,6 +309,8 @@ mono_sha1_get_digest_from_file (const gchar *filename, guchar digest [20])
        if (ferror(fp)) {
                fclose(fp);
                return;
+       } else {
+               fclose(fp);
        }
 
        mono_sha1_final (&ctx, digest);