[BACK]Return to mdXhl.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / md

Annotation of src/lib/libc/md/mdXhl.c, Revision 1.7

1.7     ! christos    1: /*     $NetBSD: mdXhl.c,v 1.6 2003/07/26 19:24:47 salo Exp $   */
1.1       thorpej     2:
1.2       thorpej     3: /*
1.1       thorpej     4:  * ----------------------------------------------------------------------------
                      5:  * "THE BEER-WARE LICENSE" (Revision 42):
                      6:  * <phk@login.dkuug.dk> wrote this file.  As long as you retain this notice you
                      7:  * can do whatever you want with this stuff. If we meet some day, and you think
                      8:  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
                      9:  * ----------------------------------------------------------------------------
                     10:  *
                     11:  * from FreeBSD Id: mdXhl.c,v 1.8 1996/10/25 06:48:12 bde Exp
1.2       thorpej    12:  */
                     13:
                     14: /*
1.7     ! christos   15:  * Modified April 29, 1997 by Jason R. Thorpe <thorpej@NetBSD.org>
1.1       thorpej    16:  */
                     17:
1.7     ! christos   18: #define        CONCAT(x,y)     __CONCAT(x,y)
        !            19: #define        MDNAME(x)       CONCAT(MDALGORITHM,x)
        !            20:
        !            21: #if !defined(_KERNEL) && defined(__weak_alias)
        !            22: #define        WA(a,b) __weak_alias(a,b)
        !            23: WA(MDNAME(End),CONCAT(_,MDNAME(End)))
        !            24: WA(MDNAME(File),CONCAT(_,MDNAME(File)))
        !            25: WA(MDNAME(Data),CONCAT(_,MDNAME(Data)))
        !            26: #undef WA
        !            27: #endif
        !            28:
        !            29: #include "namespace.h"
        !            30:
1.1       thorpej    31: #include <sys/types.h>
1.4       lukem      32:
1.7     ! christos   33: #include MDINCLUDE
1.4       lukem      34: #include <assert.h>
1.1       thorpej    35: #include <fcntl.h>
                     36: #include <errno.h>
                     37: #include <stdio.h>
                     38: #include <stdlib.h>
1.4       lukem      39: #include <unistd.h>
1.1       thorpej    40:
1.7     ! christos   41: #if HAVE_NBTOOL_CONFIG_H
        !            42: #include "nbtool_config.h"
        !            43: #endif
        !            44:
        !            45:
1.1       thorpej    46:
                     47: char *
1.2       thorpej    48: MDNAME(End)(ctx, buf)
                     49:        MDNAME(_CTX) *ctx;
                     50:        char *buf;
1.1       thorpej    51: {
1.2       thorpej    52:        int i;
                     53:        unsigned char digest[16];
                     54:        static const char hex[]="0123456789abcdef";
                     55:
1.4       lukem      56:        _DIAGASSERT(ctx != 0);
                     57:
1.2       thorpej    58:        if (buf == NULL)
                     59:                buf = malloc(33);
                     60:        if (buf == NULL)
                     61:                return (NULL);
                     62:
                     63:        MDNAME(Final)(digest, ctx);
                     64:
                     65:        for (i = 0; i < 16; i++) {
1.3       christos   66:                buf[i+i] = hex[(u_int32_t)digest[i] >> 4];
1.2       thorpej    67:                buf[i+i+1] = hex[digest[i] & 0x0f];
                     68:        }
                     69:
                     70:        buf[i+i] = '\0';
                     71:        return (buf);
1.1       thorpej    72: }
                     73:
                     74: char *
1.2       thorpej    75: MDNAME(File)(filename, buf)
                     76:        const char *filename;
                     77:        char *buf;
1.1       thorpej    78: {
1.2       thorpej    79:        unsigned char buffer[BUFSIZ];
                     80:        MDNAME(_CTX) ctx;
                     81:        int f, i, j;
                     82:
1.4       lukem      83:        _DIAGASSERT(filename != 0);
                     84:        /* buf may be NULL */
                     85:
1.2       thorpej    86:        MDNAME(Init)(&ctx);
                     87:        f = open(filename, O_RDONLY, 0666);
                     88:        if (f < 0)
                     89:                return NULL;
                     90:
                     91:        while ((i = read(f, buffer, sizeof(buffer))) > 0)
1.3       christos   92:                MDNAME(Update)(&ctx, buffer, (unsigned int)i);
1.2       thorpej    93:
                     94:        j = errno;
                     95:        close(f);
                     96:        errno = j;
                     97:
                     98:        if (i < 0)
                     99:                return NULL;
                    100:
                    101:        return (MDNAME(End)(&ctx, buf));
1.1       thorpej   102: }
                    103:
                    104: char *
1.2       thorpej   105: MDNAME(Data)(data, len, buf)
                    106:        const unsigned char *data;
                    107:        unsigned int len;
                    108:        char *buf;
1.1       thorpej   109: {
1.2       thorpej   110:        MDNAME(_CTX) ctx;
1.4       lukem     111:
                    112:        _DIAGASSERT(data != 0);
1.1       thorpej   113:
1.2       thorpej   114:        MDNAME(Init)(&ctx);
                    115:        MDNAME(Update)(&ctx, data, len);
                    116:        return (MDNAME(End)(&ctx, buf));
1.1       thorpej   117: }

CVSweb <webmaster@jp.NetBSD.org>