MD5 removal of int64 code - Mailing list pgsql-hackers

From Bruce Momjian
Subject MD5 removal of int64 code
Date
Msg-id 200108250136.f7P1avi22282@candle.pha.pa.us
Whole thread Raw
List pgsql-hackers
I have applied the following patch to remove MD5 usage of int64 types.
I split it into two int32 values and did the job that way.  I also
changed the code to use standard c.h data types, and changes 0xFF to
0xff.


--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
Index: src/backend/libpq/md5.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/libpq/md5.c,v
retrieving revision 1.4
diff -c -r1.4 md5.c
*** src/backend/libpq/md5.c    2001/08/17 02:59:19    1.4
--- src/backend/libpq/md5.c    2001/08/25 01:00:23
***************
*** 24,33 ****
   *    PRIVATE FUNCTIONS
   */

- typedef unsigned char unsigned8;
- typedef unsigned int  unsigned32;
- typedef unsigned long unsigned64;
-
  #ifdef FRONTEND
  #undef palloc
  #define palloc malloc
--- 24,29 ----
***************
*** 39,51 ****
   *    The returned array is allocated using malloc.  the caller should free it
   *     when it is no longer needed.
   */
! static unsigned8 *
! createPaddedCopyWithLength(unsigned8 *b, unsigned32 *l)
  {
!     unsigned8  *ret;
!     unsigned32 q;
!     unsigned32 len, newLen448;
!     unsigned64 len64;

      len = ((b == NULL) ? 0 : *l);
      newLen448 = len + 64 - (len % 64) - 8;
--- 35,47 ----
   *    The returned array is allocated using malloc.  the caller should free it
   *     when it is no longer needed.
   */
! static uint8 *
! createPaddedCopyWithLength(uint8 *b, uint32 *l)
  {
!     uint8  *ret;
!     uint32 q;
!     uint32 len, newLen448;
!     uint32 len_high, len_low;    /* 64-bit value split into 32-bit sections */

      len = ((b == NULL) ? 0 : *l);
      newLen448 = len + 64 - (len % 64) - 8;
***************
*** 53,63 ****
          newLen448 += 64;

      *l = newLen448 + 8;
!     if ((ret = (unsigned8 *) malloc(sizeof(unsigned8) * *l)) == NULL)
          return NULL;

      if (b != NULL)
!         memcpy(ret, b, sizeof(unsigned8) * len);

      /* pad */
      ret[len] = 0x80;
--- 49,59 ----
          newLen448 += 64;

      *l = newLen448 + 8;
!     if ((ret = (uint8 *) malloc(sizeof(uint8) * *l)) == NULL)
          return NULL;

      if (b != NULL)
!         memcpy(ret, b, sizeof(uint8) * len);

      /* pad */
      ret[len] = 0x80;
***************
*** 65,88 ****
          ret[q] = 0x00;

      /* append length as a 64 bit bitcount */
!     len64 = len;
!     len64 <<= 3;
      q = newLen448;
!     ret[q++] = (len64 & 0xFF);
!     len64 >>= 8;
!     ret[q++] = (len64 & 0xFF);
!     len64 >>= 8;
!     ret[q++] = (len64 & 0xFF);
!     len64 >>= 8;
!     ret[q++] = (len64 & 0xFF);
!     len64 >>= 8;
!     ret[q++] = (len64 & 0xFF);
!     len64 >>= 8;
!     ret[q++] = (len64 & 0xFF);
!     len64 >>= 8;
!     ret[q++] = (len64 & 0xFF);
!     len64 >>= 8;
!     ret[q] = (len64 & 0xFF);

      return ret;
  }
--- 61,86 ----
          ret[q] = 0x00;

      /* append length as a 64 bit bitcount */
!     len_low = len;
!     /* split into two 32-bit values */
!     /* we only look at the bottom 32-bits */
!     len_high = len >> 29;
!     len_low <<= 3;
      q = newLen448;
!     ret[q++] = (len_low & 0xff);
!     len_low >>= 8;
!     ret[q++] = (len_low & 0xff);
!     len_low >>= 8;
!     ret[q++] = (len_low & 0xff);
!     len_low >>= 8;
!     ret[q++] = (len_low & 0xff);
!     ret[q++] = (len_high & 0xff);
!     len_high >>= 8;
!     ret[q++] = (len_high & 0xff);
!     len_high >>= 8;
!     ret[q++] = (len_high & 0xff);
!     len_high >>= 8;
!     ret[q] = (len_high & 0xff);

      return ret;
  }
***************
*** 94,102 ****
  #define ROT_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))

  static void
! doTheRounds(unsigned32 X[16], unsigned32 state[4])
  {
!     unsigned32 a, b, c, d;

      a = state[0];
      b = state[1];
--- 92,100 ----
  #define ROT_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))

  static void
! doTheRounds(uint32 X[16], uint32 state[4])
  {
!     uint32 a, b, c, d;

      a = state[0];
      b = state[1];
***************
*** 182,194 ****
  }

  static int
! calculateDigestFromBuffer(unsigned8 *b, unsigned32 len, unsigned8 sum[16])
  {
!     register unsigned32 i, j, k, newI;
!     unsigned32 l;
!     unsigned8 *input;
!     register unsigned32 *wbp;
!     unsigned32 workBuff[16], state[4];

      l = len;

--- 180,192 ----
  }

  static int
! calculateDigestFromBuffer(uint8 *b, uint32 len, uint8 sum[16])
  {
!     register uint32 i, j, k, newI;
!     uint32 l;
!     uint8 *input;
!     register uint32 *wbp;
!     uint32 workBuff[16], state[4];

      l = len;

***************
*** 223,241 ****
      j = 0;
      for (i = 0; i < 4; i++) {
          k = state[i];
!         sum[j++] = (k & 0xFF);
          k >>= 8;
!         sum[j++] = (k & 0xFF);
          k >>= 8;
!         sum[j++] = (k & 0xFF);
          k >>= 8;
!         sum[j++] = (k & 0xFF);
      }
      return 1;
  }

  static void
! bytesToHex(unsigned8 b[16], char *s)
  {
      static char *hex = "0123456789abcdef";
      int         q, w;
--- 221,239 ----
      j = 0;
      for (i = 0; i < 4; i++) {
          k = state[i];
!         sum[j++] = (k & 0xff);
          k >>= 8;
!         sum[j++] = (k & 0xff);
          k >>= 8;
!         sum[j++] = (k & 0xff);
          k >>= 8;
!         sum[j++] = (k & 0xff);
      }
      return 1;
  }

  static void
! bytesToHex(uint8 b[16], char *s)
  {
      static char *hex = "0123456789abcdef";
      int         q, w;
***************
*** 280,288 ****
  bool
  md5_hash(const void *buff, size_t len, char *hexsum)
  {
!     unsigned8 sum[16];

!     if (!calculateDigestFromBuffer((unsigned8 *) buff, len, sum))
          return false;

      bytesToHex(sum, hexsum);
--- 278,286 ----
  bool
  md5_hash(const void *buff, size_t len, char *hexsum)
  {
!     uint8 sum[16];

!     if (!calculateDigestFromBuffer((uint8 *) buff, len, sum))
          return false;

      bytesToHex(sum, hexsum);

pgsql-hackers by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: Permissions for large-object comments
Next
From: "Dominic J. Eidson"
Date:
Subject: Re: [PATCHES] Patch to include PAM support...