Selaa lähdekoodia

fixes gh-2596 added support for creation of md5sum from file.

added md5_filesum in order to support md5sum creation from a file.

Signed-off-by: Philip Schwartz <philip.schwartz@lexisnexis.com>
Philip Schwartz 13 vuotta sitten
vanhempi
commit
7ad0e9c0a8
2 muutettua tiedostoa jossa 26 lisäystä ja 0 poistoa
  1. 23 0
      system/jlib/jmd5.cpp
  2. 3 0
      system/jlib/jmd5.hpp

+ 23 - 0
system/jlib/jmd5.cpp

@@ -78,6 +78,8 @@
 #endif
 
 #include "jmd5.hpp"
+#include "jfile.hpp"
+#include "jlog.hpp"
 #include <string.h>
 
 #undef BYTE_ORDER   /* 1 = big-endian, -1 = little-endian, 0 = unknown */
@@ -438,3 +440,24 @@ void md5_string(StringBuffer& inpstring, StringBuffer& outstring)
     md5_string(inpstring, inpstring.length(), outstring);
 }
 
+void md5_filesum(const char* filename, StringBuffer& outstring)
+{
+    MemoryBuffer mb;
+    Owned<IFile> file = createIFile(filename);
+    Owned<IFileIO> io = file->openShared(IFOread, IFSHread);
+    if (!io)
+        throw MakeStringException(1, "File %s could not be opened", file->queryFilename());
+
+    offset_t size = io->size();
+    size32_t sizeToRead = (size32_t)size;
+    if (sizeToRead != size)
+        throw MakeStringException(1, "File %s is larger than 4Gb", file->queryFilename());
+
+    mb.ensureCapacity(sizeToRead+1);
+    byte * contents = static_cast<byte *>(mb.reserve(sizeToRead));
+    size32_t sizeRead = io->read(0, sizeToRead, contents);
+    if (sizeRead != sizeToRead)
+        throw MakeStringException(1, "File %s only read %u of %u bytes", file->queryFilename(), sizeRead, sizeToRead);
+
+    md5_string(mb.toByteArray(), sizeRead, outstring);
+}

+ 3 - 0
system/jlib/jmd5.hpp

@@ -109,6 +109,9 @@ jlib_decl void md5_finish(md5_state_t *pms, md5_byte_t digest[16]);
 jlib_decl void md5_string (StringBuffer& inpstring, StringBuffer& outstring);
 jlib_decl void md5_string2(const char* inpstring, StringBuffer& outstring);
 
+/* Takes in a filename and returns the md5 sum of the file */
+jlib_decl void md5_filesum(const char* filename, StringBuffer& outstring);
+
 #ifdef __cplusplus
 }  /* end extern "C" */
 #endif