From 6e52073fac7be205251b222f63cf2f61d1519364 Mon Sep 17 00:00:00 2001 From: Arch Librarian Date: Mon, 20 Sep 2004 16:54:27 +0000 Subject: A few functions migrated around Author: jgg Date: 1999-08-02 03:07:47 GMT A few functions migrated around --- apt-pkg/contrib/md5.cc | 79 +++++++++++++---------------------------------- apt-pkg/contrib/md5.h | 4 ++- apt-pkg/contrib/mmap.cc | 4 +-- apt-pkg/contrib/mmap.h | 6 ++-- apt-pkg/contrib/strutl.cc | 39 ++++++++++++++++++++++- apt-pkg/contrib/strutl.h | 4 ++- 6 files changed, 70 insertions(+), 66 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/md5.cc b/apt-pkg/contrib/md5.cc index 671d61d06..0d0a59696 100644 --- a/apt-pkg/contrib/md5.cc +++ b/apt-pkg/contrib/md5.cc @@ -1,25 +1,23 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: md5.cc,v 1.2 1998/11/01 05:27:36 jgg Exp $ +// $Id: md5.cc,v 1.3 1999/08/02 03:07:47 jgg Exp $ /* ###################################################################### MD5Sum - MD5 Message Digest Algorithm. - This code implements the MD5 message-digest algorithm. - The algorithm is due to Ron Rivest. This code was - written by Colin Plumb in 1993, no copyright is claimed. - This code is in the public domain; do with it what you wish. - - Equivalent code is available from RSA Data Security, Inc. - This code has been tested against that, and is equivalent, - except that you don't need to include two pages of legalese - with every copy. - - To compute the message digest of a chunk of bytes, declare an - MD5Context structure, pass it to MD5Init, call MD5Update as - needed on buffers full of bytes, and then call MD5Final, which - will fill a supplied 16-byte array with the digest. + This code implements the MD5 message-digest algorithm. The algorithm is + due to Ron Rivest. This code was written by Colin Plumb in 1993, no + copyright is claimed. This code is in the public domain; do with it what + you wish. + Equivalent code is available from RSA Data Security, Inc. This code has + been tested against that, and is equivalent, except that you don't need to + include two pages of legalese with every copy. + + To compute the message digest of a chunk of bytes, instantiate the class, + and repeatedly call one of the Add() members. When finished the Result + method will return the Hash and finalize the value. + Changed so as no longer to depend on Colin Plumb's `usual.h' header definitions; now uses stuff from dpkg's config.h. - Ian Jackson . @@ -33,9 +31,6 @@ arrays to UINT32's and go from there. This allows us to advoid using config.h in a public header or internally newing memory. - Some of the terms may be quite bogus, I don't really know the details of - MD5, just converted the code ;> - JGG - ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ @@ -44,6 +39,8 @@ #endif #include +#include + #include #include #include @@ -72,7 +69,7 @@ static void byteSwap(UINT32 *buf, unsigned words) // MD5Transform - Alters an existing MD5 hash /*{{{*/ // --------------------------------------------------------------------- /* The core of the MD5 algorithm, this alters an existing MD5 hash to - reflect the addition of 16 longwords of new data. MD5Update blocks + reflect the addition of 16 longwords of new data. Add blocks the data and converts bytes into longwords for this routine. */ // The four core functions - F1 is optimized somewhat @@ -169,22 +166,6 @@ static void MD5Transform(UINT32 buf[4], UINT32 const in[16]) buf[3] += d; } /*}}}*/ -// hex_digit - Convert a hex character into an integer /*{{{*/ -// --------------------------------------------------------------------- -/* The original version of this could only handle lower case. These routines - do output to lowercase hex but can handle upper okay as well.*/ -static int hex_digit(int c) -{ - - if (c >= '0' && c <= '9') - return c - '0'; - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - return 0; -} - /*}}}*/ // MD5SumValue::MD5SumValue - Constructs the summation from a string /*{{{*/ // --------------------------------------------------------------------- /* The string form of a MD5 is a 32 character hex number */ @@ -206,26 +187,8 @@ MD5SumValue::MD5SumValue() // --------------------------------------------------------------------- /* Converts the hex string into a set of chars */ bool MD5SumValue::Set(string Str) -{ - // Check for correct length - if (Str.length() != 32) - return false; - - // Check for only hex digits - const char *I = Str.begin(); - for (;I != Str.end(); I++) - if (isxdigit(*I) == 0) - return false; - - // Convert each digit. We store it in the same order as the string - int J = 0; - for (I = Str.begin(); I != Str.end();J++, I += 2) - { - Sum[J] = hex_digit(I[0]) << 4; - Sum[J] += hex_digit(I[1]); - } - - return true; +{ + return Hex2Num(Str.begin(),Str.end(),Sum,sizeof(Sum)); } /*}}}*/ // MD5SumValue::Value - Convert the number into a string /*{{{*/ @@ -252,7 +215,7 @@ string MD5SumValue::Value() const /*}}}*/ // MD5SumValue::operator == - Comparitor /*{{{*/ // --------------------------------------------------------------------- -/* Call memcmp on the buffer */ +/* Call memcmp on the buffer */ bool MD5SumValue::operator ==(const MD5SumValue &rhs) const { return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0; @@ -276,7 +239,7 @@ MD5Summation::MD5Summation() Done = false; } /*}}}*/ -// MD5Summation::Add - 'Add' a data set to the sum /*{{{*/ +// MD5Summation::Add - 'Add' a data set to the hash /*{{{*/ // --------------------------------------------------------------------- /* */ bool MD5Summation::Add(const unsigned char *data,unsigned long len) @@ -293,7 +256,7 @@ bool MD5Summation::Add(const unsigned char *data,unsigned long len) if ((bytes[0] = t + len) < t) bytes[1]++; - // Space available in ctx->in (at least 1) + // Space available (at least 1) t = 64 - (t & 0x3f); if (t > len) { diff --git a/apt-pkg/contrib/md5.h b/apt-pkg/contrib/md5.h index 9cfa6ae72..19b5bac6c 100644 --- a/apt-pkg/contrib/md5.h +++ b/apt-pkg/contrib/md5.h @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: md5.h,v 1.3 1999/01/18 06:20:08 jgg Exp $ +// $Id: md5.h,v 1.4 1999/08/02 03:07:47 jgg Exp $ /* ###################################################################### MD5SumValue - Storage for a MD5Sum @@ -11,6 +11,8 @@ A MD5Sum is used to generate a (hopefully) unique 16 byte number for a block of data. This can be used to gaurd against corruption of a file. + MD5 should not be used for tamper protection, use SHA or something more + secure. There are two classes because computing a MD5 is not a continual operation unless 64 byte blocks are used. Also the summation requires an diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 850e1046a..ef6b96d6a 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: mmap.cc,v 1.16 1999/07/18 05:58:40 jgg Exp $ +// $Id: mmap.cc,v 1.17 1999/08/02 03:07:47 jgg Exp $ /* ###################################################################### MMap Class - Provides 'real' mmap or a faked mmap using read(). @@ -269,7 +269,7 @@ unsigned long DynamicMMap::WriteString(const char *String, return 0; } - if (Len == 0) + if (Len == (unsigned long)-1) Len = strlen(String); iSize += Len + 1; memcpy((char *)Base + Result,String,Len); diff --git a/apt-pkg/contrib/mmap.h b/apt-pkg/contrib/mmap.h index d086e51ab..6285620c9 100644 --- a/apt-pkg/contrib/mmap.h +++ b/apt-pkg/contrib/mmap.h @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: mmap.h,v 1.10 1999/07/15 03:15:49 jgg Exp $ +// $Id: mmap.h,v 1.11 1999/08/02 03:07:47 jgg Exp $ /* ###################################################################### MMap Class - Provides 'real' mmap or a faked mmap using read(). @@ -91,9 +91,9 @@ class DynamicMMap : public MMap // Allocation unsigned long RawAllocate(unsigned long Size,unsigned long Aln = 0); unsigned long Allocate(unsigned long ItemSize); - unsigned long WriteString(const char *String,unsigned long Len = 0); + unsigned long WriteString(const char *String,unsigned long Len = (unsigned long)-1); inline unsigned long WriteString(string S) {return WriteString(S.begin(),S.size());}; - void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;}; + void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;}; DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace = 2*1024*1024); DynamicMMap(unsigned long Flags,unsigned long WorkSpace = 2*1024*1024); diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index fb5f6683c..17a23709b 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: strutl.cc,v 1.27 1999/07/26 17:46:08 jgg Exp $ +// $Id: strutl.cc,v 1.28 1999/08/02 03:07:48 jgg Exp $ /* ###################################################################### String Util - Some usefull string functions. @@ -693,6 +693,43 @@ bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base) return true; } /*}}}*/ +// HexDigit - Convert a hex character into an integer /*{{{*/ +// --------------------------------------------------------------------- +/* Helper for Hex2Num */ +static int HexDigit(int c) +{ + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + return 0; +} + /*}}}*/ +// Hex2Num - Convert a long hex number into a buffer /*{{{*/ +// --------------------------------------------------------------------- +/* The length of the buffer must be exactly 1/2 the length of the string. */ +bool Hex2Num(const char *Start,const char *End,unsigned char *Num, + unsigned int Length) +{ + if (End - Start != Length*2) + return false; + + // Convert each digit. We store it in the same order as the string + int J = 0; + for (const char *I = Start; I < End;J++, I += 2) + { + if (isxdigit(*I) == 0 || isxdigit(I[1]) == 0) + return false; + + Num[J] = HexDigit(I[0]) << 4; + Num[J] += HexDigit(I[1]); + } + + return true; +} + /*}}}*/ // URI::CopyFrom - Copy from an object /*{{{*/ // --------------------------------------------------------------------- diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index 6ba1f9ebe..14293ae05 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: strutl.h,v 1.14 1999/07/26 17:46:08 jgg Exp $ +// $Id: strutl.h,v 1.15 1999/08/02 03:07:48 jgg Exp $ /* ###################################################################### String Util - These are some usefull string functions @@ -42,6 +42,8 @@ string LookupTag(string Message,const char *Tag,const char *Default = 0); int StringToBool(string Text,int Default = -1); bool ReadMessages(int Fd, vector &List); bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0); +bool Hex2Num(const char *Start,const char *End,unsigned char *Num, + unsigned int Length); int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd); inline int stringcmp(const char *A,const char *AEnd,const char *B) {return stringcmp(A,AEnd,B,B+strlen(B));}; -- cgit v1.2.3-70-g09d2