From 34204aac424f8c29317d863d64f1c3bb4d241a50 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 3 Nov 2005 09:00:23 +0000 Subject: 2005-11-03 Paul Jakma * zebra.h: BSD BYTE_ORDER define isn't available everywhere, define if needs be. * checksum.h: new file. checksum.c exports in_cksum, provide a header for it. * checksum.c: (in_cksum) callers shouldn't have to know it uses a u_short internally, change to void *. * Makefile.am: Add checksum.h * command.h: remove bogus trailling slash. * md5.c: (general) Update it for the twentieth century. ANSI declarations are widely supported now.. Don't include system headers, only include zebra.h. Use POSIX types (the alternative is to define u_int64_t in a portable way - rest of Quagga needs same cleanup). Make endian-conditional code be compiler conditional rather than preprocessor conditional, so that breakage gets noticed quicker. * md5.h: POSIX types. Get rid of the odd __P() non-ANSI capable compiler compatibility hack. --- lib/ChangeLog | 21 +++++++ lib/Makefile.am | 3 +- lib/checksum.c | 6 +- lib/checksum.h | 1 + lib/command.h | 2 +- lib/md5.c | 178 +++++++++++++++++++++++++------------------------------- lib/md5.h | 22 +++---- lib/zebra.h | 17 ++++++ 8 files changed, 136 insertions(+), 114 deletions(-) create mode 100644 lib/checksum.h diff --git a/lib/ChangeLog b/lib/ChangeLog index 4030470d..3ad9458c 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,24 @@ +2005-11-03 Paul Jakma + + * zebra.h: BSD BYTE_ORDER define isn't available everywhere, + define if needs be. + * checksum.h: new file. checksum.c exports in_cksum, provide + a header for it. + * checksum.c: (in_cksum) callers shouldn't have to know it uses + a u_short internally, change to void *. + * Makefile.am: Add checksum.h + * command.h: remove bogus trailling slash. + * md5.c: (general) Update it for the twentieth century. ANSI + declarations are widely supported now.. Don't include system + headers, only include zebra.h. Use POSIX types (the + alternative is to define u_int64_t in a portable way - rest + of Quagga needs same cleanup). + Make endian-conditional code be compiler conditional rather + than preprocessor conditional, so that breakage gets noticed + quicker. + * md5.h: POSIX types. Get rid of the odd __P() non-ANSI capable + compiler compatibility hack. + 2005-10-26 Paul Jakma * (general) Cleanup a some calls to XFREE,strdup, etc. to use diff --git a/lib/Makefile.am b/lib/Makefile.am index e26040f6..5162de54 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -21,7 +21,8 @@ libzebra_la_DEPENDENCIES = @LIB_REGEX@ libzebra_la_LIBADD = @LIB_REGEX@ pkginclude_HEADERS = \ - buffer.h command.h filter.h getopt.h hash.h if.h linklist.h log.h \ + buffer.h checksum.h command.h filter.h getopt.h hash.h \ + if.h linklist.h log.h \ memory.h network.h prefix.h routemap.h distribute.h sockunion.h \ str.h stream.h table.h thread.h vector.h version.h vty.h zebra.h \ plist.h zclient.h sockopt.h smux.h md5.h if_rmap.h keychain.h \ diff --git a/lib/checksum.c b/lib/checksum.c index 8c0ea521..201da59f 100644 --- a/lib/checksum.c +++ b/lib/checksum.c @@ -7,10 +7,12 @@ */ #include +#include "checksum.h" -int /* return checksum in low-order 16 bits */ -in_cksum(u_short *ptr, int nbytes) +int /* return checksum in low-order 16 bits */ +in_cksum(void *parg, int nbytes) { + u_short *ptr = parg; register long sum; /* assumes long == 32 bits */ u_short oddbyte; register u_short answer; /* assumes u_short == 16 bits */ diff --git a/lib/checksum.h b/lib/checksum.h new file mode 100644 index 00000000..ccc4da03 --- /dev/null +++ b/lib/checksum.h @@ -0,0 +1 @@ +extern int in_cksum(void *, int); diff --git a/lib/command.h b/lib/command.h index 02d96a42..fbe6a0a1 100644 --- a/lib/command.h +++ b/lib/command.h @@ -179,7 +179,7 @@ struct desc }; #define DEFUN_CMD_FUNC_DECL(funcname) \ - static int funcname (struct cmd_element *, struct vty *, int, const char *[]); \ + static int funcname (struct cmd_element *, struct vty *, int, const char *[]); #define DEFUN_CMD_FUNC_TEXT(funcname) \ static int funcname \ diff --git a/lib/md5.c b/lib/md5.c index 4e974941..e994085c 100644 --- a/lib/md5.c +++ b/lib/md5.c @@ -1,6 +1,6 @@ /* $USAGI: md5.c,v 1.2 2000/11/02 11:59:24 yoshfuji Exp $ */ /* $KAME: md5.c,v 1.2 2000/05/27 07:07:48 jinmei Exp $ */ -/* $Id: md5.c,v 1.3 2005/09/28 15:47:44 vincent Exp $ */ +/* $Id: md5.c,v 1.4 2005/11/03 09:00:23 paul Exp $ */ /* * Copyright (C) 2004 6WIND @@ -39,11 +39,7 @@ * SUCH DAMAGE. */ -#include -#include -#include -#include -#include +#include #include "md5.h" #define SHIFT(X, s) (((X) << (s)) | ((X) >> (32 - (s)))) @@ -103,7 +99,7 @@ #define MD5_D0 0x10325476 /* Integer part of 4294967296 times abs(sin(i)), where i is in radians. */ -static const u_int32_t T[65] = { +static const uint32_t T[65] = { 0, 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, @@ -126,7 +122,7 @@ static const u_int32_t T[65] = { 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391, }; -static const u_int8_t md5_paddat[MD5_BUFLEN] = { +static const uint8_t md5_paddat[MD5_BUFLEN] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -137,10 +133,9 @@ static const u_int8_t md5_paddat[MD5_BUFLEN] = { 0, 0, 0, 0, 0, 0, 0, 0, }; -static void md5_calc __P((u_int8_t *, md5_ctxt *)); +static void md5_calc (const uint8_t *, md5_ctxt *); -void md5_init(ctxt) - md5_ctxt *ctxt; +void md5_init(md5_ctxt *ctxt) { ctxt->md5_n = 0; ctxt->md5_i = 0; @@ -148,132 +143,117 @@ void md5_init(ctxt) ctxt->md5_stb = MD5_B0; ctxt->md5_stc = MD5_C0; ctxt->md5_std = MD5_D0; - bzero(ctxt->md5_buf, sizeof(ctxt->md5_buf)); + memset (ctxt->md5_buf, 0, sizeof(ctxt->md5_buf)); } -void md5_loop(ctxt, input, len) - md5_ctxt *ctxt; - u_int8_t *input; - u_int len; /* number of bytes */ +void md5_loop(md5_ctxt *ctxt, const uint8_t *input, uint len) { - u_int gap, i; + uint gap, i; ctxt->md5_n += len * 8; /* byte to bit */ gap = MD5_BUFLEN - ctxt->md5_i; if (len >= gap) { - bcopy((void *)input, (void *)(ctxt->md5_buf + ctxt->md5_i), - gap); + memcpy (ctxt->md5_buf + ctxt->md5_i, input, gap); md5_calc(ctxt->md5_buf, ctxt); for (i = gap; i + MD5_BUFLEN <= len; i += MD5_BUFLEN) { - md5_calc((u_int8_t *)(input + i), ctxt); + md5_calc((input + i), ctxt); } ctxt->md5_i = len - i; - bcopy((void *)(input + i), (void *)ctxt->md5_buf, ctxt->md5_i); + memcpy (ctxt->md5_buf, (input + i), ctxt->md5_i); } else { - bcopy((void *)input, (void *)(ctxt->md5_buf + ctxt->md5_i), - len); + memcpy (ctxt->md5_buf + ctxt->md5_i, input, len); ctxt->md5_i += len; } } -void md5_pad(ctxt) - md5_ctxt *ctxt; +void md5_pad(md5_ctxt *ctxt) { - u_int gap; + uint gap; /* Don't count up padding. Keep md5_n. */ gap = MD5_BUFLEN - ctxt->md5_i; if (gap > 8) { - bcopy((void *)md5_paddat, - (void *)(ctxt->md5_buf + ctxt->md5_i), - gap - sizeof(ctxt->md5_n)); + memcpy (ctxt->md5_buf + ctxt->md5_i, md5_paddat, + gap - sizeof(ctxt->md5_n)); } else { /* including gap == 8 */ - bcopy((void *)md5_paddat, (void *)(ctxt->md5_buf + ctxt->md5_i), - gap); - md5_calc(ctxt->md5_buf, ctxt); - bcopy((void *)(md5_paddat + gap), - (void *)ctxt->md5_buf, - MD5_BUFLEN - sizeof(ctxt->md5_n)); + memcpy (ctxt->md5_buf + ctxt->md5_i, md5_paddat, gap); + md5_calc (ctxt->md5_buf, ctxt); + memcpy (ctxt->md5_buf, md5_paddat + gap, + MD5_BUFLEN - sizeof(ctxt->md5_n)); } /* 8 byte word */ -#if BYTE_ORDER == LITTLE_ENDIAN - bcopy(&ctxt->md5_n8[0], &ctxt->md5_buf[56], 8); -#endif -#if BYTE_ORDER == BIG_ENDIAN - ctxt->md5_buf[56] = ctxt->md5_n8[7]; - ctxt->md5_buf[57] = ctxt->md5_n8[6]; - ctxt->md5_buf[58] = ctxt->md5_n8[5]; - ctxt->md5_buf[59] = ctxt->md5_n8[4]; - ctxt->md5_buf[60] = ctxt->md5_n8[3]; - ctxt->md5_buf[61] = ctxt->md5_n8[2]; - ctxt->md5_buf[62] = ctxt->md5_n8[1]; - ctxt->md5_buf[63] = ctxt->md5_n8[0]; -#endif - + if (BYTE_ORDER == LITTLE_ENDIAN) + memcpy (&ctxt->md5_buf[56], &ctxt->md5_n8[0], 8); + else + { + ctxt->md5_buf[56] = ctxt->md5_n8[7]; + ctxt->md5_buf[57] = ctxt->md5_n8[6]; + ctxt->md5_buf[58] = ctxt->md5_n8[5]; + ctxt->md5_buf[59] = ctxt->md5_n8[4]; + ctxt->md5_buf[60] = ctxt->md5_n8[3]; + ctxt->md5_buf[61] = ctxt->md5_n8[2]; + ctxt->md5_buf[62] = ctxt->md5_n8[1]; + ctxt->md5_buf[63] = ctxt->md5_n8[0]; + } md5_calc(ctxt->md5_buf, ctxt); } -void md5_result(digest, ctxt) - u_int8_t *digest; - md5_ctxt *ctxt; +void md5_result(uint8_t *digest, md5_ctxt *ctxt) { /* 4 byte words */ -#if BYTE_ORDER == LITTLE_ENDIAN - bcopy(&ctxt->md5_st8[0], digest, 16); -#endif -#if BYTE_ORDER == BIG_ENDIAN - digest[ 0] = ctxt->md5_st8[ 3]; digest[ 1] = ctxt->md5_st8[ 2]; - digest[ 2] = ctxt->md5_st8[ 1]; digest[ 3] = ctxt->md5_st8[ 0]; - digest[ 4] = ctxt->md5_st8[ 7]; digest[ 5] = ctxt->md5_st8[ 6]; - digest[ 6] = ctxt->md5_st8[ 5]; digest[ 7] = ctxt->md5_st8[ 4]; - digest[ 8] = ctxt->md5_st8[11]; digest[ 9] = ctxt->md5_st8[10]; - digest[10] = ctxt->md5_st8[ 9]; digest[11] = ctxt->md5_st8[ 8]; - digest[12] = ctxt->md5_st8[15]; digest[13] = ctxt->md5_st8[14]; - digest[14] = ctxt->md5_st8[13]; digest[15] = ctxt->md5_st8[12]; -#endif + if (BYTE_ORDER == LITTLE_ENDIAN) + memcpy (digest, &ctxt->md5_st8[0], 16); + else if (BYTE_ORDER == BIG_ENDIAN) + { + digest[ 0] = ctxt->md5_st8[ 3]; digest[ 1] = ctxt->md5_st8[ 2]; + digest[ 2] = ctxt->md5_st8[ 1]; digest[ 3] = ctxt->md5_st8[ 0]; + digest[ 4] = ctxt->md5_st8[ 7]; digest[ 5] = ctxt->md5_st8[ 6]; + digest[ 6] = ctxt->md5_st8[ 5]; digest[ 7] = ctxt->md5_st8[ 4]; + digest[ 8] = ctxt->md5_st8[11]; digest[ 9] = ctxt->md5_st8[10]; + digest[10] = ctxt->md5_st8[ 9]; digest[11] = ctxt->md5_st8[ 8]; + digest[12] = ctxt->md5_st8[15]; digest[13] = ctxt->md5_st8[14]; + digest[14] = ctxt->md5_st8[13]; digest[15] = ctxt->md5_st8[12]; + } } -#if BYTE_ORDER == BIG_ENDIAN -u_int32_t X[16]; -#endif - -static void md5_calc(b64, ctxt) - u_int8_t *b64; - md5_ctxt *ctxt; +static void md5_calc(const uint8_t *b64, md5_ctxt * ctxt) { - u_int32_t A = ctxt->md5_sta; - u_int32_t B = ctxt->md5_stb; - u_int32_t C = ctxt->md5_stc; - u_int32_t D = ctxt->md5_std; -#if BYTE_ORDER == LITTLE_ENDIAN - u_int32_t *X = (u_int32_t *)b64; -#endif -#if BYTE_ORDER == BIG_ENDIAN - /* 4 byte words */ - /* what a brute force but fast! */ - u_int8_t *y = (u_int8_t *)X; - y[ 0] = b64[ 3]; y[ 1] = b64[ 2]; y[ 2] = b64[ 1]; y[ 3] = b64[ 0]; - y[ 4] = b64[ 7]; y[ 5] = b64[ 6]; y[ 6] = b64[ 5]; y[ 7] = b64[ 4]; - y[ 8] = b64[11]; y[ 9] = b64[10]; y[10] = b64[ 9]; y[11] = b64[ 8]; - y[12] = b64[15]; y[13] = b64[14]; y[14] = b64[13]; y[15] = b64[12]; - y[16] = b64[19]; y[17] = b64[18]; y[18] = b64[17]; y[19] = b64[16]; - y[20] = b64[23]; y[21] = b64[22]; y[22] = b64[21]; y[23] = b64[20]; - y[24] = b64[27]; y[25] = b64[26]; y[26] = b64[25]; y[27] = b64[24]; - y[28] = b64[31]; y[29] = b64[30]; y[30] = b64[29]; y[31] = b64[28]; - y[32] = b64[35]; y[33] = b64[34]; y[34] = b64[33]; y[35] = b64[32]; - y[36] = b64[39]; y[37] = b64[38]; y[38] = b64[37]; y[39] = b64[36]; - y[40] = b64[43]; y[41] = b64[42]; y[42] = b64[41]; y[43] = b64[40]; - y[44] = b64[47]; y[45] = b64[46]; y[46] = b64[45]; y[47] = b64[44]; - y[48] = b64[51]; y[49] = b64[50]; y[50] = b64[49]; y[51] = b64[48]; - y[52] = b64[55]; y[53] = b64[54]; y[54] = b64[53]; y[55] = b64[52]; - y[56] = b64[59]; y[57] = b64[58]; y[58] = b64[57]; y[59] = b64[56]; - y[60] = b64[63]; y[61] = b64[62]; y[62] = b64[61]; y[63] = b64[60]; + uint32_t A = ctxt->md5_sta; + uint32_t B = ctxt->md5_stb; + uint32_t C = ctxt->md5_stc; + uint32_t D = ctxt->md5_std; +#if (BYTE_ORDER == LITTLE_ENDIAN) + const uint32_t *X = (const uint32_t *)b64; +#elif (BYTE_ORDER == BIG_ENDIAN) + uint32_t X[16]; #endif + if (BYTE_ORDER == BIG_ENDIAN) + { + /* 4 byte words */ + /* what a brute force but fast! */ + uint8_t *y = (uint8_t *)X; + y[ 0] = b64[ 3]; y[ 1] = b64[ 2]; y[ 2] = b64[ 1]; y[ 3] = b64[ 0]; + y[ 4] = b64[ 7]; y[ 5] = b64[ 6]; y[ 6] = b64[ 5]; y[ 7] = b64[ 4]; + y[ 8] = b64[11]; y[ 9] = b64[10]; y[10] = b64[ 9]; y[11] = b64[ 8]; + y[12] = b64[15]; y[13] = b64[14]; y[14] = b64[13]; y[15] = b64[12]; + y[16] = b64[19]; y[17] = b64[18]; y[18] = b64[17]; y[19] = b64[16]; + y[20] = b64[23]; y[21] = b64[22]; y[22] = b64[21]; y[23] = b64[20]; + y[24] = b64[27]; y[25] = b64[26]; y[26] = b64[25]; y[27] = b64[24]; + y[28] = b64[31]; y[29] = b64[30]; y[30] = b64[29]; y[31] = b64[28]; + y[32] = b64[35]; y[33] = b64[34]; y[34] = b64[33]; y[35] = b64[32]; + y[36] = b64[39]; y[37] = b64[38]; y[38] = b64[37]; y[39] = b64[36]; + y[40] = b64[43]; y[41] = b64[42]; y[42] = b64[41]; y[43] = b64[40]; + y[44] = b64[47]; y[45] = b64[46]; y[46] = b64[45]; y[47] = b64[44]; + y[48] = b64[51]; y[49] = b64[50]; y[50] = b64[49]; y[51] = b64[48]; + y[52] = b64[55]; y[53] = b64[54]; y[54] = b64[53]; y[55] = b64[52]; + y[56] = b64[59]; y[57] = b64[58]; y[58] = b64[57]; y[59] = b64[56]; + y[60] = b64[63]; y[61] = b64[62]; y[62] = b64[61]; y[63] = b64[60]; + } ROUND1(A, B, C, D, 0, Sa, 1); ROUND1(D, A, B, C, 1, Sb, 2); ROUND1(C, D, A, B, 2, Sc, 3); ROUND1(B, C, D, A, 3, Sd, 4); diff --git a/lib/md5.h b/lib/md5.h index d899fbd3..9e627dd8 100644 --- a/lib/md5.h +++ b/lib/md5.h @@ -1,6 +1,6 @@ /* $USAGI: md5.h,v 1.2 2000/11/02 11:59:25 yoshfuji Exp $ */ /* $KAME: md5.h,v 1.4 2000/03/27 04:36:22 sumikawa Exp $ */ -/* $Id: md5.h,v 1.1 2005/09/28 15:47:44 vincent Exp $ */ +/* $Id: md5.h,v 1.2 2005/11/03 09:00:23 paul Exp $ */ /* * Copyright (C) 2004 6WIND @@ -46,8 +46,8 @@ typedef struct { union { - u_int32_t md5_state32[4]; - u_int8_t md5_state8[16]; + uint32_t md5_state32[4]; + uint8_t md5_state8[16]; } md5_st; #define md5_sta md5_st.md5_state32[0] @@ -57,20 +57,20 @@ typedef struct { #define md5_st8 md5_st.md5_state8 union { - u_int64_t md5_count64; - u_int8_t md5_count8[8]; + uint64_t md5_count64; + uint8_t md5_count8[8]; } md5_count; #define md5_n md5_count.md5_count64 #define md5_n8 md5_count.md5_count8 - u_int md5_i; - u_int8_t md5_buf[MD5_BUFLEN]; + uint md5_i; + uint8_t md5_buf[MD5_BUFLEN]; } md5_ctxt; -extern void md5_init __P((md5_ctxt *)); -extern void md5_loop __P((md5_ctxt *, u_int8_t *, u_int)); -extern void md5_pad __P((md5_ctxt *)); -extern void md5_result __P((u_int8_t *, md5_ctxt *)); +extern void md5_init (md5_ctxt *); +extern void md5_loop (md5_ctxt *, const uint8_t *, u_int); +extern void md5_pad (md5_ctxt *); +extern void md5_result (uint8_t *, md5_ctxt *); /* compatibility */ #define MD5_CTX md5_ctxt diff --git a/lib/zebra.h b/lib/zebra.h index 88f16252..1739d771 100644 --- a/lib/zebra.h +++ b/lib/zebra.h @@ -336,6 +336,23 @@ struct in_pktinfo #define HAVE_IP_HDRINCL_BSD_ORDER #endif +/* Define BYTE_ORDER, if not defined. Useful for compiler conditional + * code, rather than preprocessor conditional. + * Not all the world has this BSD define. + */ +#ifndef BYTE_ORDER +#define BIG_ENDIAN 4321 /* least-significant byte first (vax, pc) */ +#define LITTLE_ENDIAN 1234 /* most-significant byte first (IBM, net) */ +#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */ + +#if defined(WORDS_BIGENDIAN) +#define BYTE_ORDER BIG_ENDIAN +#else /* !WORDS_BIGENDIAN */ +#define BYTE_ORDER LITTLE_ENDIAN +#endif /* WORDS_BIGENDIAN */ + +#endif /* ndef BYTE_ORDER */ + /* MAX / MIN are not commonly defined, but useful */ #ifndef MAX #define MAX(a, b) ((a) > (b) ? (a) : (b)) -- cgit v1.2.1