1N/A/* $Id: MD5.xs,v 1.42 2003/12/06 22:35:16 gisle Exp $ */
1N/A
1N/A/*
1N/A * This library is free software; you can redistribute it and/or
1N/A * modify it under the same terms as Perl itself.
1N/A *
1N/A * Copyright 1998-2000 Gisle Aas.
1N/A * Copyright 1995-1996 Neil Winton.
1N/A * Copyright 1991-1992 RSA Data Security, Inc.
1N/A *
1N/A * This code is derived from Neil Winton's MD5-1.7 Perl module, which in
1N/A * turn is derived from the reference implementation in RFC 1321 which
1N/A * comes with this message:
1N/A *
1N/A * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
1N/A * rights reserved.
1N/A *
1N/A * License to copy and use this software is granted provided that it
1N/A * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
1N/A * Algorithm" in all material mentioning or referencing this software
1N/A * or this function.
1N/A *
1N/A * License is also granted to make and use derivative works provided
1N/A * that such works are identified as "derived from the RSA Data
1N/A * Security, Inc. MD5 Message-Digest Algorithm" in all material
1N/A * mentioning or referencing the derived work.
1N/A *
1N/A * RSA Data Security, Inc. makes no representations concerning either
1N/A * the merchantability of this software or the suitability of this
1N/A * software for any particular purpose. It is provided "as is"
1N/A * without express or implied warranty of any kind.
1N/A *
1N/A * These notices must be retained in any copies of any part of this
1N/A * documentation and/or software.
1N/A */
1N/A
1N/A#ifdef __cplusplus
1N/Aextern "C" {
1N/A#endif
1N/A#define PERL_NO_GET_CONTEXT /* we want efficiency */
1N/A#include "EXTERN.h"
1N/A#include "perl.h"
1N/A#include "XSUB.h"
1N/A#ifdef __cplusplus
1N/A}
1N/A#endif
1N/A
1N/A#ifndef PERL_VERSION
1N/A# include <patchlevel.h>
1N/A# if !(defined(PERL_VERSION) || (SUBVERSION > 0 && defined(PATCHLEVEL)))
1N/A# include <could_not_find_Perl_patchlevel.h>
1N/A# endif
1N/A# define PERL_REVISION 5
1N/A# define PERL_VERSION PATCHLEVEL
1N/A# define PERL_SUBVERSION SUBVERSION
1N/A#endif
1N/A
1N/A#if PERL_VERSION <= 4 && !defined(PL_dowarn)
1N/A #define PL_dowarn dowarn
1N/A#endif
1N/A
1N/A#ifdef G_WARN_ON
1N/A #define DOWARN (PL_dowarn & G_WARN_ON)
1N/A#else
1N/A #define DOWARN PL_dowarn
1N/A#endif
1N/A
1N/A#ifdef SvPVbyte
1N/A #if PERL_REVISION == 5 && PERL_VERSION < 7
1N/A /* SvPVbyte does not work in perl-5.6.1, borrowed version for 5.7.3 */
1N/A #undef SvPVbyte
1N/A #define SvPVbyte(sv, lp) \
1N/A ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK) \
1N/A ? ((lp = SvCUR(sv)), SvPVX(sv)) : my_sv_2pvbyte(aTHX_ sv, &lp))
1N/A
1N/A static char *
1N/A my_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
1N/A {
1N/A sv_utf8_downgrade(sv,0);
1N/A return SvPV(sv,*lp);
1N/A }
1N/A #endif
1N/A#else
1N/A #define SvPVbyte SvPV
1N/A#endif
1N/A
1N/A#ifndef dTHX
1N/A #define pTHX_
1N/A #define aTHX_
1N/A#endif
1N/A
1N/A/* Perl does not guarantee that U32 is exactly 32 bits. Some system
1N/A * has no integral type with exactly 32 bits. For instance, A Cray has
1N/A * short, int and long all at 64 bits so we need to apply this macro
1N/A * to reduce U32 values to 32 bits at appropriate places. If U32
1N/A * really does have 32 bits then this is a no-op.
1N/A */
1N/A#if BYTEORDER > 0x4321 || defined(TRUNCATE_U32)
1N/A #define TO32(x) ((x) & 0xFFFFffff)
1N/A #define TRUNC32(x) ((x) &= 0xFFFFffff)
1N/A#else
1N/A #define TO32(x) (x)
1N/A #define TRUNC32(x) /*nothing*/
1N/A#endif
1N/A
1N/A/* The MD5 algorithm is defined in terms of little endian 32-bit
1N/A * values. The following macros (and functions) allow us to convert
1N/A * between native integers and such values.
1N/A */
1N/A#undef BYTESWAP
1N/A#ifndef U32_ALIGNMENT_REQUIRED
1N/A #if BYTEORDER == 0x1234 /* 32-bit little endian */
1N/A #define BYTESWAP(x) (x) /* no-op */
1N/A
1N/A #elif BYTEORDER == 0x4321 /* 32-bit big endian */
1N/A #define BYTESWAP(x) ((((x)&0xFF)<<24) \
1N/A |(((x)>>24)&0xFF) \
1N/A |(((x)&0x0000FF00)<<8) \
1N/A |(((x)&0x00FF0000)>>8) )
1N/A #endif
1N/A#endif
1N/A
1N/A#ifndef BYTESWAP
1N/Astatic void u2s(U32 u, U8* s)
1N/A{
1N/A *s++ = (U8)(u & 0xFF);
1N/A *s++ = (U8)((u >> 8) & 0xFF);
1N/A *s++ = (U8)((u >> 16) & 0xFF);
1N/A *s = (U8)((u >> 24) & 0xFF);
1N/A}
1N/A
1N/A#define s2u(s,u) ((u) = (U32)(*s) | \
1N/A ((U32)(*(s+1)) << 8) | \
1N/A ((U32)(*(s+2)) << 16) | \
1N/A ((U32)(*(s+3)) << 24))
1N/A#endif
1N/A
1N/A#define MD5_CTX_SIGNATURE 200003165
1N/A
1N/A/* This stucture keeps the current state of algorithm.
1N/A */
1N/Atypedef struct {
1N/A U32 signature; /* safer cast in get_md5_ctx() */
1N/A U32 A, B, C, D; /* current digest */
1N/A U32 bytes_low; /* counts bytes in message */
1N/A U32 bytes_high; /* turn it into a 64-bit counter */
1N/A U8 buffer[128]; /* collect complete 64 byte blocks */
1N/A} MD5_CTX;
1N/A
1N/A
1N/A/* Padding is added at the end of the message in order to fill a
1N/A * complete 64 byte block (- 8 bytes for the message length). The
1N/A * padding is also the reason the buffer in MD5_CTX have to be
1N/A * 128 bytes.
1N/A */
1N/Astatic unsigned char PADDING[64] = {
1N/A 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1N/A 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1N/A 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1N/A};
1N/A
1N/A/* Constants for MD5Transform routine.
1N/A */
1N/A#define S11 7
1N/A#define S12 12
1N/A#define S13 17
1N/A#define S14 22
1N/A#define S21 5
1N/A#define S22 9
1N/A#define S23 14
1N/A#define S24 20
1N/A#define S31 4
1N/A#define S32 11
1N/A#define S33 16
1N/A#define S34 23
1N/A#define S41 6
1N/A#define S42 10
1N/A#define S43 15
1N/A#define S44 21
1N/A
1N/A/* F, G, H and I are basic MD5 functions.
1N/A */
1N/A#define F(x, y, z) ((((x) & ((y) ^ (z))) ^ (z)))
1N/A#define G(x, y, z) F(z, x, y)
1N/A#define H(x, y, z) ((x) ^ (y) ^ (z))
1N/A#define I(x, y, z) ((y) ^ ((x) | (~z)))
1N/A
1N/A/* ROTATE_LEFT rotates x left n bits.
1N/A */
1N/A#define ROTATE_LEFT(x, n) (((x) << (n) | ((x) >> (32-(n)))))
1N/A
1N/A/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
1N/A * Rotation is separate from addition to prevent recomputation.
1N/A */
1N/A#define FF(a, b, c, d, s, ac) \
1N/A (a) += F ((b), (c), (d)) + (NEXTx) + (U32)(ac); \
1N/A TRUNC32((a)); \
1N/A (a) = ROTATE_LEFT ((a), (s)); \
1N/A (a) += (b); \
1N/A TRUNC32((a));
1N/A
1N/A#define GG(a, b, c, d, x, s, ac) \
1N/A (a) += G ((b), (c), (d)) + X[x] + (U32)(ac); \
1N/A TRUNC32((a)); \
1N/A (a) = ROTATE_LEFT ((a), (s)); \
1N/A (a) += (b); \
1N/A TRUNC32((a));
1N/A
1N/A#define HH(a, b, c, d, x, s, ac) \
1N/A (a) += H ((b), (c), (d)) + X[x] + (U32)(ac); \
1N/A TRUNC32((a)); \
1N/A (a) = ROTATE_LEFT ((a), (s)); \
1N/A (a) += (b); \
1N/A TRUNC32((a));
1N/A
1N/A#define II(a, b, c, d, x, s, ac) \
1N/A (a) += I ((b), (c), (d)) + X[x] + (U32)(ac); \
1N/A TRUNC32((a)); \
1N/A (a) = ROTATE_LEFT ((a), (s)); \
1N/A (a) += (b); \
1N/A TRUNC32((a));
1N/A
1N/A
1N/Astatic void
1N/AMD5Init(MD5_CTX *ctx)
1N/A{
1N/A /* Start state */
1N/A ctx->A = 0x67452301;
1N/A ctx->B = 0xefcdab89;
1N/A ctx->C = 0x98badcfe;
1N/A ctx->D = 0x10325476;
1N/A
1N/A /* message length */
1N/A ctx->bytes_low = ctx->bytes_high = 0;
1N/A}
1N/A
1N/A
1N/Astatic void
1N/AMD5Transform(MD5_CTX* ctx, const U8* buf, STRLEN blocks)
1N/A{
1N/A#ifdef MD5_DEBUG
1N/A static int tcount = 0;
1N/A#endif
1N/A
1N/A U32 A = ctx->A;
1N/A U32 B = ctx->B;
1N/A U32 C = ctx->C;
1N/A U32 D = ctx->D;
1N/A
1N/A#ifndef U32_ALIGNMENT_REQUIRED
1N/A const U32 *x = (U32*)buf; /* really just type casting */
1N/A#endif
1N/A
1N/A do {
1N/A U32 a = A;
1N/A U32 b = B;
1N/A U32 c = C;
1N/A U32 d = D;
1N/A
1N/A#if BYTEORDER == 0x1234 && !defined(U32_ALIGNMENT_REQUIRED)
1N/A const U32 *X = x;
1N/A #define NEXTx (*x++)
1N/A#else
1N/A U32 X[16]; /* converted values, used in round 2-4 */
1N/A U32 *uptr = X;
1N/A U32 tmp;
1N/A #ifdef BYTESWAP
1N/A #define NEXTx (tmp=*x++, *uptr++ = BYTESWAP(tmp))
1N/A #else
1N/A #define NEXTx (s2u(buf,tmp), buf += 4, *uptr++ = tmp)
1N/A #endif
1N/A#endif
1N/A
1N/A#ifdef MD5_DEBUG
1N/A if (buf == ctx->buffer)
1N/A fprintf(stderr,"%5d: Transform ctx->buffer", ++tcount);
1N/A else
1N/A fprintf(stderr,"%5d: Transform %p (%d)", ++tcount, buf, blocks);
1N/A
1N/A {
1N/A int i;
1N/A fprintf(stderr,"[");
1N/A for (i = 0; i < 16; i++) {
1N/A fprintf(stderr,"%x,", x[i]);
1N/A }
1N/A fprintf(stderr,"]\n");
1N/A }
1N/A#endif
1N/A
1N/A /* Round 1 */
1N/A FF (a, b, c, d, S11, 0xd76aa478); /* 1 */
1N/A FF (d, a, b, c, S12, 0xe8c7b756); /* 2 */
1N/A FF (c, d, a, b, S13, 0x242070db); /* 3 */
1N/A FF (b, c, d, a, S14, 0xc1bdceee); /* 4 */
1N/A FF (a, b, c, d, S11, 0xf57c0faf); /* 5 */
1N/A FF (d, a, b, c, S12, 0x4787c62a); /* 6 */
1N/A FF (c, d, a, b, S13, 0xa8304613); /* 7 */
1N/A FF (b, c, d, a, S14, 0xfd469501); /* 8 */
1N/A FF (a, b, c, d, S11, 0x698098d8); /* 9 */
1N/A FF (d, a, b, c, S12, 0x8b44f7af); /* 10 */
1N/A FF (c, d, a, b, S13, 0xffff5bb1); /* 11 */
1N/A FF (b, c, d, a, S14, 0x895cd7be); /* 12 */
1N/A FF (a, b, c, d, S11, 0x6b901122); /* 13 */
1N/A FF (d, a, b, c, S12, 0xfd987193); /* 14 */
1N/A FF (c, d, a, b, S13, 0xa679438e); /* 15 */
1N/A FF (b, c, d, a, S14, 0x49b40821); /* 16 */
1N/A
1N/A /* Round 2 */
1N/A GG (a, b, c, d, 1, S21, 0xf61e2562); /* 17 */
1N/A GG (d, a, b, c, 6, S22, 0xc040b340); /* 18 */
1N/A GG (c, d, a, b, 11, S23, 0x265e5a51); /* 19 */
1N/A GG (b, c, d, a, 0, S24, 0xe9b6c7aa); /* 20 */
1N/A GG (a, b, c, d, 5, S21, 0xd62f105d); /* 21 */
1N/A GG (d, a, b, c, 10, S22, 0x2441453); /* 22 */
1N/A GG (c, d, a, b, 15, S23, 0xd8a1e681); /* 23 */
1N/A GG (b, c, d, a, 4, S24, 0xe7d3fbc8); /* 24 */
1N/A GG (a, b, c, d, 9, S21, 0x21e1cde6); /* 25 */
1N/A GG (d, a, b, c, 14, S22, 0xc33707d6); /* 26 */
1N/A GG (c, d, a, b, 3, S23, 0xf4d50d87); /* 27 */
1N/A GG (b, c, d, a, 8, S24, 0x455a14ed); /* 28 */
1N/A GG (a, b, c, d, 13, S21, 0xa9e3e905); /* 29 */
1N/A GG (d, a, b, c, 2, S22, 0xfcefa3f8); /* 30 */
1N/A GG (c, d, a, b, 7, S23, 0x676f02d9); /* 31 */
1N/A GG (b, c, d, a, 12, S24, 0x8d2a4c8a); /* 32 */
1N/A
1N/A /* Round 3 */
1N/A HH (a, b, c, d, 5, S31, 0xfffa3942); /* 33 */
1N/A HH (d, a, b, c, 8, S32, 0x8771f681); /* 34 */
1N/A HH (c, d, a, b, 11, S33, 0x6d9d6122); /* 35 */
1N/A HH (b, c, d, a, 14, S34, 0xfde5380c); /* 36 */
1N/A HH (a, b, c, d, 1, S31, 0xa4beea44); /* 37 */
1N/A HH (d, a, b, c, 4, S32, 0x4bdecfa9); /* 38 */
1N/A HH (c, d, a, b, 7, S33, 0xf6bb4b60); /* 39 */
1N/A HH (b, c, d, a, 10, S34, 0xbebfbc70); /* 40 */
1N/A HH (a, b, c, d, 13, S31, 0x289b7ec6); /* 41 */
1N/A HH (d, a, b, c, 0, S32, 0xeaa127fa); /* 42 */
1N/A HH (c, d, a, b, 3, S33, 0xd4ef3085); /* 43 */
1N/A HH (b, c, d, a, 6, S34, 0x4881d05); /* 44 */
1N/A HH (a, b, c, d, 9, S31, 0xd9d4d039); /* 45 */
1N/A HH (d, a, b, c, 12, S32, 0xe6db99e5); /* 46 */
1N/A HH (c, d, a, b, 15, S33, 0x1fa27cf8); /* 47 */
1N/A HH (b, c, d, a, 2, S34, 0xc4ac5665); /* 48 */
1N/A
1N/A /* Round 4 */
1N/A II (a, b, c, d, 0, S41, 0xf4292244); /* 49 */
1N/A II (d, a, b, c, 7, S42, 0x432aff97); /* 50 */
1N/A II (c, d, a, b, 14, S43, 0xab9423a7); /* 51 */
1N/A II (b, c, d, a, 5, S44, 0xfc93a039); /* 52 */
1N/A II (a, b, c, d, 12, S41, 0x655b59c3); /* 53 */
1N/A II (d, a, b, c, 3, S42, 0x8f0ccc92); /* 54 */
1N/A II (c, d, a, b, 10, S43, 0xffeff47d); /* 55 */
1N/A II (b, c, d, a, 1, S44, 0x85845dd1); /* 56 */
1N/A II (a, b, c, d, 8, S41, 0x6fa87e4f); /* 57 */
1N/A II (d, a, b, c, 15, S42, 0xfe2ce6e0); /* 58 */
1N/A II (c, d, a, b, 6, S43, 0xa3014314); /* 59 */
1N/A II (b, c, d, a, 13, S44, 0x4e0811a1); /* 60 */
1N/A II (a, b, c, d, 4, S41, 0xf7537e82); /* 61 */
1N/A II (d, a, b, c, 11, S42, 0xbd3af235); /* 62 */
1N/A II (c, d, a, b, 2, S43, 0x2ad7d2bb); /* 63 */
1N/A II (b, c, d, a, 9, S44, 0xeb86d391); /* 64 */
1N/A
1N/A A += a; TRUNC32(A);
1N/A B += b; TRUNC32(B);
1N/A C += c; TRUNC32(C);
1N/A D += d; TRUNC32(D);
1N/A
1N/A } while (--blocks);
1N/A ctx->A = A;
1N/A ctx->B = B;
1N/A ctx->C = C;
1N/A ctx->D = D;
1N/A}
1N/A
1N/A
1N/A#ifdef MD5_DEBUG
1N/Astatic char*
1N/Actx_dump(MD5_CTX* ctx)
1N/A{
1N/A static char buf[1024];
1N/A sprintf(buf, "{A=%x,B=%x,C=%x,D=%x,%d,%d(%d)}",
1N/A ctx->A, ctx->B, ctx->C, ctx->D,
1N/A ctx->bytes_low, ctx->bytes_high, (ctx->bytes_low&0x3F));
1N/A return buf;
1N/A}
1N/A#endif
1N/A
1N/A
1N/Astatic void
1N/AMD5Update(MD5_CTX* ctx, const U8* buf, STRLEN len)
1N/A{
1N/A STRLEN blocks;
1N/A STRLEN fill = ctx->bytes_low & 0x3F;
1N/A
1N/A#ifdef MD5_DEBUG
1N/A static int ucount = 0;
1N/A fprintf(stderr,"%5i: Update(%s, %p, %d)\n", ++ucount, ctx_dump(ctx),
1N/A buf, len);
1N/A#endif
1N/A
1N/A ctx->bytes_low += len;
1N/A if (ctx->bytes_low < len) /* wrap around */
1N/A ctx->bytes_high++;
1N/A
1N/A if (fill) {
1N/A STRLEN missing = 64 - fill;
1N/A if (len < missing) {
1N/A Copy(buf, ctx->buffer + fill, len, U8);
1N/A return;
1N/A }
1N/A Copy(buf, ctx->buffer + fill, missing, U8);
1N/A MD5Transform(ctx, ctx->buffer, 1);
1N/A buf += missing;
1N/A len -= missing;
1N/A }
1N/A
1N/A blocks = len >> 6;
1N/A if (blocks)
1N/A MD5Transform(ctx, buf, blocks);
1N/A if ( (len &= 0x3F)) {
1N/A Copy(buf + (blocks << 6), ctx->buffer, len, U8);
1N/A }
1N/A}
1N/A
1N/A
1N/Astatic void
1N/AMD5Final(U8* digest, MD5_CTX *ctx)
1N/A{
1N/A STRLEN fill = ctx->bytes_low & 0x3F;
1N/A STRLEN padlen = (fill < 56 ? 56 : 120) - fill;
1N/A U32 bits_low, bits_high;
1N/A#ifdef MD5_DEBUG
1N/A fprintf(stderr," Final: %s\n", ctx_dump(ctx));
1N/A#endif
1N/A Copy(PADDING, ctx->buffer + fill, padlen, U8);
1N/A fill += padlen;
1N/A
1N/A bits_low = ctx->bytes_low << 3;
1N/A bits_high = (ctx->bytes_high << 3) | (ctx->bytes_low >> 29);
1N/A#ifdef BYTESWAP
1N/A *(U32*)(ctx->buffer + fill) = BYTESWAP(bits_low); fill += 4;
1N/A *(U32*)(ctx->buffer + fill) = BYTESWAP(bits_high); fill += 4;
1N/A#else
1N/A u2s(bits_low, ctx->buffer + fill); fill += 4;
1N/A u2s(bits_high, ctx->buffer + fill); fill += 4;
1N/A#endif
1N/A
1N/A MD5Transform(ctx, ctx->buffer, fill >> 6);
1N/A#ifdef MD5_DEBUG
1N/A fprintf(stderr," Result: %s\n", ctx_dump(ctx));
1N/A#endif
1N/A
1N/A#ifdef BYTESWAP
1N/A *(U32*)digest = BYTESWAP(ctx->A); digest += 4;
1N/A *(U32*)digest = BYTESWAP(ctx->B); digest += 4;
1N/A *(U32*)digest = BYTESWAP(ctx->C); digest += 4;
1N/A *(U32*)digest = BYTESWAP(ctx->D);
1N/A#else
1N/A u2s(ctx->A, digest);
1N/A u2s(ctx->B, digest+4);
1N/A u2s(ctx->C, digest+8);
1N/A u2s(ctx->D, digest+12);
1N/A#endif
1N/A}
1N/A
1N/A#ifndef INT2PTR
1N/A#define INT2PTR(any,d) (any)(d)
1N/A#endif
1N/A
1N/Astatic MD5_CTX* get_md5_ctx(pTHX_ SV* sv)
1N/A{
1N/A if (SvROK(sv)) {
1N/A sv = SvRV(sv);
1N/A if (SvIOK(sv)) {
1N/A MD5_CTX* ctx = INT2PTR(MD5_CTX*, SvIV(sv));
1N/A if (ctx && ctx->signature == MD5_CTX_SIGNATURE) {
1N/A return ctx;
1N/A }
1N/A }
1N/A }
1N/A croak("Not a reference to a Digest::MD5 object");
1N/A return (MD5_CTX*)0; /* some compilers insist on a return value */
1N/A}
1N/A
1N/A
1N/Astatic char* hex_16(const unsigned char* from, char* to)
1N/A{
1N/A static char *hexdigits = "0123456789abcdef";
1N/A const unsigned char *end = from + 16;
1N/A char *d = to;
1N/A
1N/A while (from < end) {
1N/A *d++ = hexdigits[(*from >> 4)];
1N/A *d++ = hexdigits[(*from & 0x0F)];
1N/A from++;
1N/A }
1N/A *d = '\0';
1N/A return to;
1N/A}
1N/A
1N/Astatic char* base64_16(const unsigned char* from, char* to)
1N/A{
1N/A static char* base64 =
1N/A "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1N/A const unsigned char *end = from + 16;
1N/A unsigned char c1, c2, c3;
1N/A char *d = to;
1N/A
1N/A while (1) {
1N/A c1 = *from++;
1N/A *d++ = base64[c1>>2];
1N/A if (from == end) {
1N/A *d++ = base64[(c1 & 0x3) << 4];
1N/A break;
1N/A }
1N/A c2 = *from++;
1N/A c3 = *from++;
1N/A *d++ = base64[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)];
1N/A *d++ = base64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
1N/A *d++ = base64[c3 & 0x3F];
1N/A }
1N/A *d = '\0';
1N/A return to;
1N/A}
1N/A
1N/A/* Formats */
1N/A#define F_BIN 0
1N/A#define F_HEX 1
1N/A#define F_B64 2
1N/A
1N/Astatic SV* make_mortal_sv(pTHX_ const unsigned char *src, int type)
1N/A{
1N/A STRLEN len;
1N/A char result[33];
1N/A char *ret;
1N/A
1N/A switch (type) {
1N/A case F_BIN:
1N/A ret = (char*)src;
1N/A len = 16;
1N/A break;
1N/A case F_HEX:
1N/A ret = hex_16(src, result);
1N/A len = 32;
1N/A break;
1N/A case F_B64:
1N/A ret = base64_16(src, result);
1N/A len = 22;
1N/A break;
1N/A default:
1N/A croak("Bad convertion type (%d)", type);
1N/A break;
1N/A }
1N/A return sv_2mortal(newSVpv(ret,len));
1N/A}
1N/A
1N/A
1N/A/********************************************************************/
1N/A
1N/Atypedef PerlIO* InputStream;
1N/A
1N/AMODULE = Digest::MD5 PACKAGE = Digest::MD5
1N/A
1N/APROTOTYPES: DISABLE
1N/A
1N/Avoid
1N/Anew(xclass)
1N/A SV* xclass
1N/A PREINIT:
1N/A MD5_CTX* context;
1N/A PPCODE:
1N/A if (!SvROK(xclass)) {
1N/A STRLEN my_na;
1N/A char *sclass = SvPV(xclass, my_na);
1N/A New(55, context, 1, MD5_CTX);
1N/A context->signature = MD5_CTX_SIGNATURE;
1N/A ST(0) = sv_newmortal();
1N/A sv_setref_pv(ST(0), sclass, (void*)context);
1N/A SvREADONLY_on(SvRV(ST(0)));
1N/A } else {
1N/A context = get_md5_ctx(aTHX_ xclass);
1N/A }
1N/A MD5Init(context);
1N/A XSRETURN(1);
1N/A
1N/Avoid
1N/Aclone(self)
1N/A SV* self
1N/A PREINIT:
1N/A MD5_CTX* cont = get_md5_ctx(aTHX_ self);
1N/A char *myname = sv_reftype(SvRV(self),TRUE);
1N/A MD5_CTX* context;
1N/A PPCODE:
1N/A STRLEN my_na;
1N/A New(55, context, 1, MD5_CTX);
1N/A ST(0) = sv_newmortal();
1N/A sv_setref_pv(ST(0), myname , (void*)context);
1N/A SvREADONLY_on(SvRV(ST(0)));
1N/A memcpy(context,cont,sizeof(MD5_CTX));
1N/A XSRETURN(1);
1N/A
1N/Avoid
1N/ADESTROY(context)
1N/A MD5_CTX* context
1N/A CODE:
1N/A Safefree(context);
1N/A
1N/Avoid
1N/Aadd(self, ...)
1N/A SV* self
1N/A PREINIT:
1N/A MD5_CTX* context = get_md5_ctx(aTHX_ self);
1N/A int i;
1N/A unsigned char *data;
1N/A STRLEN len;
1N/A PPCODE:
1N/A for (i = 1; i < items; i++) {
1N/A data = (unsigned char *)(SvPVbyte(ST(i), len));
1N/A MD5Update(context, data, len);
1N/A }
1N/A XSRETURN(1); /* self */
1N/A
1N/Avoid
1N/Aaddfile(self, fh)
1N/A SV* self
1N/A InputStream fh
1N/A PREINIT:
1N/A MD5_CTX* context = get_md5_ctx(aTHX_ self);
1N/A STRLEN fill = context->bytes_low & 0x3F;
1N/A unsigned char buffer[4096];
1N/A int n;
1N/A CODE:
1N/A if (fh) {
1N/A if (fill) {
1N/A /* The MD5Update() function is faster if it can work with
1N/A * complete blocks. This will fill up any buffered block
1N/A * first.
1N/A */
1N/A STRLEN missing = 64 - fill;
1N/A if ( (n = PerlIO_read(fh, buffer, missing)) > 0)
1N/A MD5Update(context, buffer, n);
1N/A else
1N/A XSRETURN(1); /* self */
1N/A }
1N/A
1N/A /* Process blocks until EOF or error */
1N/A while ( (n = PerlIO_read(fh, buffer, sizeof(buffer))) > 0) {
1N/A MD5Update(context, buffer, n);
1N/A }
1N/A
1N/A if (PerlIO_error(fh)) {
1N/A croak("Reading from filehandle failed");
1N/A }
1N/A }
1N/A else {
1N/A croak("No filehandle passed");
1N/A }
1N/A XSRETURN(1); /* self */
1N/A
1N/Avoid
1N/Adigest(context)
1N/A MD5_CTX* context
1N/A ALIAS:
1N/A Digest::MD5::digest = F_BIN
1N/A Digest::MD5::hexdigest = F_HEX
1N/A Digest::MD5::b64digest = F_B64
1N/A PREINIT:
1N/A unsigned char digeststr[16];
1N/A PPCODE:
1N/A MD5Final(digeststr, context);
1N/A MD5Init(context); /* In case it is reused */
1N/A ST(0) = make_mortal_sv(aTHX_ digeststr, ix);
1N/A XSRETURN(1);
1N/A
1N/Avoid
1N/Amd5(...)
1N/A ALIAS:
1N/A Digest::MD5::md5 = F_BIN
1N/A Digest::MD5::md5_hex = F_HEX
1N/A Digest::MD5::md5_base64 = F_B64
1N/A PREINIT:
1N/A MD5_CTX ctx;
1N/A int i;
1N/A unsigned char *data;
1N/A STRLEN len;
1N/A unsigned char digeststr[16];
1N/A PPCODE:
1N/A MD5Init(&ctx);
1N/A
1N/A if (DOWARN) {
1N/A char *msg = 0;
1N/A if (items == 1) {
1N/A if (SvROK(ST(0))) {
1N/A SV* sv = SvRV(ST(0));
1N/A if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD5"))
1N/A msg = "probably called as method";
1N/A else
1N/A msg = "called with reference argument";
1N/A }
1N/A }
1N/A else if (items > 1) {
1N/A data = (unsigned char *)SvPVbyte(ST(0), len);
1N/A if (len == 11 && memEQ("Digest::MD5", data, 11)) {
1N/A msg = "probably called as class method";
1N/A }
1N/A }
1N/A if (msg) {
1N/A char *f = (ix == F_BIN) ? "md5" :
1N/A (ix == F_HEX) ? "md5_hex" : "md5_base64";
1N/A warn("&Digest::MD5::%s function %s", f, msg);
1N/A }
1N/A }
1N/A
1N/A for (i = 0; i < items; i++) {
1N/A data = (unsigned char *)(SvPVbyte(ST(i), len));
1N/A MD5Update(&ctx, data, len);
1N/A }
1N/A MD5Final(digeststr, &ctx);
1N/A ST(0) = make_mortal_sv(aTHX_ digeststr, ix);
1N/A XSRETURN(1);