diff options
author | Cameron Katri <me@cameronkatri.com> | 2021-05-09 14:20:58 -0400 |
---|---|---|
committer | Cameron Katri <me@cameronkatri.com> | 2021-05-09 14:20:58 -0400 |
commit | 5fd83771641d15c418f747bd343ba6738d3875f7 (patch) | |
tree | 5abf0f78f680d9837dbd93d4d4c3933bb7509599 /text_cmds/sort/commoncrypto.c | |
download | apple_cmds-5fd83771641d15c418f747bd343ba6738d3875f7.tar.gz apple_cmds-5fd83771641d15c418f747bd343ba6738d3875f7.tar.zst apple_cmds-5fd83771641d15c418f747bd343ba6738d3875f7.zip |
Import macOS userland
adv_cmds-176
basic_cmds-55
bootstrap_cmds-116.100.1
developer_cmds-66
diskdev_cmds-667.40.1
doc_cmds-53.60.1
file_cmds-321.40.3
mail_cmds-35
misc_cmds-34
network_cmds-606.40.1
patch_cmds-17
remote_cmds-63
shell_cmds-216.60.1
system_cmds-880.60.2
text_cmds-106
Diffstat (limited to 'text_cmds/sort/commoncrypto.c')
-rw-r--r-- | text_cmds/sort/commoncrypto.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/text_cmds/sort/commoncrypto.c b/text_cmds/sort/commoncrypto.c new file mode 100644 index 0000000..c7b3b8c --- /dev/null +++ b/text_cmds/sort/commoncrypto.c @@ -0,0 +1,98 @@ +/* mdXhl.c + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp + * ---------------------------------------------------------------------------- + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD: head/lib/libmd/mdXhl.c 294037 2016-01-14 21:08:23Z jtl $"); + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> + +#include "commoncrypto.h" + +#define LENGTH CC_MD5_DIGEST_LENGTH + +char *MD5FileChunk(const char *, char *, off_t, off_t); + +char * +MD5End(CC_MD5_CTX *ctx, char *buf) +{ + int i; + unsigned char digest[LENGTH]; + static const char hex[]="0123456789abcdef"; + + if (!buf) + buf = malloc(2*LENGTH + 1); + if (!buf) + return 0; + CC_MD5_Final(digest, ctx); + for (i = 0; i < LENGTH; i++) { + buf[i+i] = hex[digest[i] >> 4]; + buf[i+i+1] = hex[digest[i] & 0x0f]; + } + buf[i+i] = '\0'; + return buf; +} + +char * +MD5File(const char *filename, char *buf) +{ + return (MD5FileChunk(filename, buf, 0, 0)); +} + +char * +MD5FileChunk(const char *filename, char *buf, off_t ofs, off_t len) +{ + unsigned char buffer[16*1024]; + CC_MD5_CTX ctx; + int fd, readrv, e; + off_t remain; + + if (len < 0) { + errno = EINVAL; + return NULL; + } + + CC_MD5_Init(&ctx); + fd = open(filename, O_RDONLY); + if (fd < 0) + return NULL; + if (ofs != 0) { + errno = 0; + if (lseek(fd, ofs, SEEK_SET) != ofs || + (ofs == -1 && errno != 0)) { + readrv = -1; + goto error; + } + } + remain = len; + readrv = 0; + while (len == 0 || remain > 0) { + if (len == 0 || remain > (off_t)sizeof(buffer)) + readrv = read(fd, buffer, sizeof(buffer)); + else + readrv = read(fd, buffer, remain); + if (readrv <= 0) + break; + CC_MD5_Update(&ctx, buffer, readrv); + remain -= readrv; + } +error: + e = errno; + close(fd); + errno = e; + if (readrv < 0) + return NULL; + return (MD5End(&ctx, buf)); +} |