X-Git-Url: http://ftp.carnet.hr/carnet-debian/scm?p=ossec-hids.git;a=blobdiff_plain;f=src%2Fos_crypto%2Fmd5_sha1%2Fmd5_sha1_op.c;fp=src%2Fos_crypto%2Fmd5_sha1%2Fmd5_sha1_op.c;h=dd367c7a0e0fff9041a49052febb62a3718d0376;hp=b3471f4b3f5ff16fe453a83e42d244768db92508;hb=3f728675941dc69d4e544d3a880a56240a6e394a;hpb=927951d1c1ad45ba9e7325f07d996154a91c911b diff --git a/src/os_crypto/md5_sha1/md5_sha1_op.c b/src/os_crypto/md5_sha1/md5_sha1_op.c old mode 100755 new mode 100644 index b3471f4..dd367c7 --- a/src/os_crypto/md5_sha1/md5_sha1_op.c +++ b/src/os_crypto/md5_sha1/md5_sha1_op.c @@ -1,6 +1,3 @@ -/* @(#) $Id: ./src/os_crypto/md5_sha1/md5_sha1_op.c, 2011/09/08 dcid Exp $ - */ - /* Copyright (C) 2009 Trend Micro Inc. * All right reserved. * @@ -10,91 +7,82 @@ * Foundation */ - #include #include -#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 *prefilter_cmd, 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]; - char cmd[OS_MAXSTR]; - SHA_CTX sha1_ctx; MD5_CTX md5_ctx; - - /* Clearing the memory. */ + /* Clear the memory */ md5output[0] = '\0'; sha1output[0] = '\0'; - buf[2048 +1] = '\0'; + buf[2048 + 1] = '\0'; /* Use prefilter_cmd if set */ if (prefilter_cmd == NULL) { - fp = fopen(fname,"r"); - if(!fp) - return(-1); + fp = fopen(fname, mode == OS_BINARY ? "rb" : "r"); + if (!fp) { + return (-1); + } } else { - strncpy(cmd, prefilter_cmd, sizeof(cmd) - 1); - strcat(cmd, " "); - strncat(cmd, fname, sizeof(cmd) - strlen(cmd) - 1); - fp = popen(cmd, "r"); - if(!fp) - return(-1); + 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