util_md5.c revision 10a4cdd68ef1ca0e54af296fe1d08ac00150c90b
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd/* ====================================================================
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * The Apache Software License, Version 1.1
a1d62218cdb0efd0f02da1b54fd3eda91a681d98nd * Copyright (c) 2000 The Apache Software Foundation. All rights
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * reserved.
031b91a62d25106ae69d4693475c79618dd5e884fielding * Redistribution and use in source and binary forms, with or without
031b91a62d25106ae69d4693475c79618dd5e884fielding * modification, are permitted provided that the following conditions
031b91a62d25106ae69d4693475c79618dd5e884fielding * 1. Redistributions of source code must retain the above copyright
031b91a62d25106ae69d4693475c79618dd5e884fielding * notice, this list of conditions and the following disclaimer.
acc36ab93565d2880447d535da6ca6e5feac7a70nd * 2. Redistributions in binary form must reproduce the above copyright
acc36ab93565d2880447d535da6ca6e5feac7a70nd * notice, this list of conditions and the following disclaimer in
acc36ab93565d2880447d535da6ca6e5feac7a70nd * the documentation and/or other materials provided with the
acc36ab93565d2880447d535da6ca6e5feac7a70nd * distribution.
acc36ab93565d2880447d535da6ca6e5feac7a70nd * 3. The end-user documentation included with the redistribution,
acc36ab93565d2880447d535da6ca6e5feac7a70nd * if any, must include the following acknowledgment:
acc36ab93565d2880447d535da6ca6e5feac7a70nd * "This product includes software developed by the
acc36ab93565d2880447d535da6ca6e5feac7a70nd * Apache Software Foundation (http://www.apache.org/)."
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Alternately, this acknowledgment may appear in the software itself,
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * if and wherever such third-party acknowledgments normally appear.
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * 4. The names "Apache" and "Apache Software Foundation" must
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * not be used to endorse or promote products derived from this
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * software without prior written permission. For written
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * permission, please contact apache@apache.org.
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * 5. Products derived from this software may not be called "Apache",
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * nor may "Apache" appear in their name, without prior written
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak * permission of the Apache Software Foundation.
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * SUCH DAMAGE.
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * ====================================================================
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * This software consists of voluntary contributions made by many
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * individuals on behalf of the Apache Software Foundation. For more
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * information on the Apache Software Foundation, please see
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Portions of this software are based upon public domain software
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * originally written at the National Center for Supercomputing Applications,
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * University of Illinois, Urbana-Champaign.
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd/************************************************************************
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * NCSA HTTPd Server
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Software Development Group
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak * National Center for Supercomputing Applications
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * University of Illinois at Urbana-Champaign
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * 605 E. Springfield, Champaign, IL 61820
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak * httpd@ncsa.uiuc.edu
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Copyright (C) 1995, Board of Trustees of the University of Illinois
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd ************************************************************************
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak * md5.c: NCSA HTTPd code which uses the md5c.c RSA Code
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Original Code Copyright (C) 1994, Jeff Hostetler, Spyglass, Inc.
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Portions of Content-MD5 code Copyright (C) 1993, 1994 by Carnegie Mellon
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak * University (see Copyright below).
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Portions of Content-MD5 code Copyright (C) 1991 Bell Communications
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Research, Inc. (Bellcore) (see Copyright below).
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Portions extracted from mpack, John G. Myers - jgm+@cmu.edu
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Content-MD5 Code contributed by Martin Hamilton (martin@net.lut.ac.uk)
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd/* md5.c --Module Interface to MD5. */
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd/* Jeff Hostetler, Spyglass, Inc., 1994. */
d78d735dbf7c5ce5ae545eecd8ee2c052224db77ndAPI_EXPORT(char *) ap_md5_binary(apr_pool_t *p, const unsigned char *buf, int length)
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Take the MD5 hash of the string argument.
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd *r = '\0';
d78d735dbf7c5ce5ae545eecd8ee2c052224db77ndAPI_EXPORT(char *) ap_md5(apr_pool_t *p, const unsigned char *string)
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak return ap_md5_binary(p, string, (int) strlen((char *)string));
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd/* these portions extracted from mpack, John G. Myers - jgm+@cmu.edu */
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd/* (C) Copyright 1993,1994 by Carnegie Mellon University
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * All Rights Reserved.
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Permission to use, copy, modify, distribute, and sell this software
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * and its documentation for any purpose is hereby granted without
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * fee, provided that the above copyright notice appear in all copies
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * and that both that copyright notice and this permission notice
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * appear in supporting documentation, and that the name of Carnegie
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Mellon University not be used in advertising or publicity
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * pertaining to distribution of the software without specific,
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * written prior permission. Carnegie Mellon University makes no
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * representations about the suitability of this software for any
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * purpose. It is provided "as is" without express or implied
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * warranty.
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * SOFTWARE.
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak * Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * Permission to use, copy, modify, and distribute this material
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * for any purpose and without fee is hereby granted, provided
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak * that the above copyright notice and this permission notice
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * appear in all copies, and that the name of Bellcore not be
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * used in advertising or publicity pertaining to this
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * material without the specific, prior written permission
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * of an authorized representative of Bellcore. BELLCORE
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
d78d735dbf7c5ce5ae545eecd8ee2c052224db77ndstatic char basis_64[] =
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
d78d735dbf7c5ce5ae545eecd8ee2c052224db77ndAPI_EXPORT(char *) ap_md5contextTo64(apr_pool_t *a, apr_md5_ctx_t *context)
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak encodedDigest = (char *) apr_pcalloc(a, 25 * sizeof(char));
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd digest[sizeof(digest) - 1] = digest[sizeof(digest) - 2] = 0;
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd *p++ = basis_64[((digest[i] & 0x3) << 4) | ((int) (digest[i + 1] & 0xF0) >> 4)];
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd *p++ = basis_64[((digest[i + 1] & 0xF) << 2) | ((int) (digest[i + 2] & 0xC0) >> 6)];
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd *p-- = '\0';
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd *p-- = '=';
d78d735dbf7c5ce5ae545eecd8ee2c052224db77nd *p-- = '=';
d78d735dbf7c5ce5ae545eecd8ee2c052224db77ndAPI_EXPORT(char *) ap_md5digest(apr_pool_t *p, apr_file_t *infile,