Re: htole32() and friends

From: Alan Barrett <apb@dont-contact.us>
Date: Tue, 29 Jul 2003 13:55:56 +0200

On Mon, 28 Jul 2003, Alan Barrett wrote:
> If you like, I'll work up a patch that does as I suggest.

OK, here it is. Also see bugzilla bug 724.

This code bootstraps, configures and builds fine on NetBSD-current.
I tried testing it on Linux and FreeBSD, but automake (invoked from
bootstrap.sh) takes so long that I gave up waiting for it after about an
hour.

--apb (Alan Barrett)

Index: configure.in
===================================================================
RCS file: /cvsroot/squid/squid3/configure.in,v
retrieving revision 1.41
diff -u -r1.41 configure.in
--- configure.in 29 Jul 2003 02:12:34 -0000 1.41
+++ configure.in 29 Jul 2003 11:53:25 -0000
@@ -1474,6 +1474,8 @@
         string.h \
         strings.h \
         sys/bitypes.h \
+ sys/bswap.h \
+ sys/endian.h \
         sys/file.h \
         sys/ioctl.h \
         sys/param.h \
@@ -1925,6 +1927,8 @@
         bcopy \
         bswap_16 \
         bswap_32 \
+ bswap16 \
+ bswap32 \
         crypt \
         fchmod \
         getdtablesize \
@@ -1933,6 +1937,8 @@
         getrlimit \
         getrusage \
         getspnam \
+ htobe16 \
+ htole16 \
         lrand48 \
         mallinfo \
         mallocblksize \
Index: helpers/ntlm_auth/SMB/ntlm_auth.c
===================================================================
RCS file: /cvsroot/squid/squid3/helpers/ntlm_auth/SMB/ntlm_auth.c,v
retrieving revision 1.1
diff -u -r1.1 ntlm_auth.c
--- helpers/ntlm_auth/SMB/ntlm_auth.c 14 Oct 2002 00:18:17 -0000 1.1
+++ helpers/ntlm_auth/SMB/ntlm_auth.c 29 Jul 2003 11:53:26 -0000
@@ -18,6 +18,7 @@
 #include "config.h"
 #include "ntlmauth.h"
 #include "ntlm.h"
+#include "squid_endian.h"
 #include "util.h"
 #include "smbval/smblib-common.h"
 #include "smbval/rfcnb-error.h"
@@ -308,7 +309,7 @@
             SEND("NA Broken authentication packet");
             return;
         }
- switch WSWAP(fast_header->type) {
+ switch le32toh(fast_header->type) {
         case NTLM_NEGOTIATE:
             SEND("NA Invalid negotiation request received");
             return;
Index: helpers/ntlm_auth/fakeauth/fakeauth_auth.c
===================================================================
RCS file: /cvsroot/squid/squid3/helpers/ntlm_auth/fakeauth/fakeauth_auth.c,v
retrieving revision 1.1
diff -u -r1.1 fakeauth_auth.c
--- helpers/ntlm_auth/fakeauth/fakeauth_auth.c 14 Oct 2002 00:18:17 -0000 1.1
+++ helpers/ntlm_auth/fakeauth/fakeauth_auth.c 29 Jul 2003 11:53:26 -0000
@@ -14,6 +14,7 @@
 
 #include "config.h"
 #include "ntlmauth.h"
+#include "squid_endian.h"
 
 #include "ntlm.h"
 #include "util.h"
@@ -78,9 +79,9 @@
 
     memset(chal, 0, sizeof(*chal));
     memcpy(chal->hdr.signature, "NTLMSSP", 8);
- chal->flags = WSWAP(0x00018206);
- chal->hdr.type = WSWAP(NTLM_CHALLENGE);
- chal->unknown[6] = SSWAP(0x003a);
+ chal->flags = htole32(0x00018206);
+ chal->hdr.type = htole32(NTLM_CHALLENGE);
+ chal->unknown[6] = htole16(0x003a);
 
     d = (char *) chal + 48;
     i = 0;
@@ -89,8 +90,8 @@
         while (authenticate_ntlm_domain[i++]);
 
 
- chal->target.offset = WSWAP(48);
- chal->target.maxlen = SSWAP(i);
+ chal->target.offset = htole32(48);
+ chal->target.maxlen = htole16(i);
     chal->target.len = chal->target.maxlen;
 
 #ifdef NTLM_STATIC_CHALLENGE
@@ -125,10 +126,10 @@
     if (type == NTLM_ANY)
         return 0;
 
- if (WSWAP(hdr->type) != type) {
+ if (le32toh(hdr->type) != type) {
 /* don't report this error - it's ok as we do a if() around this function */
 // fprintf(stderr, "ntlmCheckHeader: type is %d, wanted %d\n",
- // WSWAP(hdr->type), type);
+ // le32toh(hdr->type), type);
         return (-1);
     }
     return (0);
@@ -145,8 +146,8 @@
     char *d, *sc;
     int l, o;
 
- l = SSWAP(str->len);
- o = WSWAP(str->offset);
+ l = le16toh(str->len);
+ o = le32toh(str->offset);
 
     /* Sanity checks. XXX values arbitrarialy chosen */
     if (l <= 0 || l >= 32 || o >= 256) {
@@ -159,7 +160,7 @@
         d = buf;
 
         for (l >>= 1; l; s++, l--) {
- c = SSWAP(*s);
+ c = le16toh(*s);
             if (c > 254 || c == '\0' || !isprint(c)) {
                 fprintf(stderr, "ntlmGetString: bad uni: %04x\n", c);
                 return (NULL);
@@ -258,7 +259,7 @@
             ntlmMakeChallenge(&chal);
             len =
                 sizeof(chal) - sizeof(chal.pad) +
- SSWAP(chal.target.maxlen);
+ le16toh(chal.target.maxlen);
             data = (char *) base64_encode_bin((char *) &chal, len);
             printf("TT %s\n", data);
         } else if (strncasecmp(buf, "KK ", 3) == 0) {
@@ -287,7 +288,7 @@
                 ntlmMakeChallenge(&chal);
                 len =
                     sizeof(chal) - sizeof(chal.pad) +
- SSWAP(chal.target.maxlen);
+ le16toh(chal.target.maxlen);
                 data = (char *) base64_encode_bin((char *) &chal, len);
                 printf("CH %s\n", data);
             } else if (!ntlmCheckHeader
Index: helpers/ntlm_auth/fakeauth/ntlm.h
===================================================================
RCS file: /cvsroot/squid/squid3/helpers/ntlm_auth/fakeauth/ntlm.h,v
retrieving revision 1.1
diff -u -r1.1 ntlm.h
--- helpers/ntlm_auth/fakeauth/ntlm.h 14 Oct 2002 00:18:17 -0000 1.1
+++ helpers/ntlm_auth/fakeauth/ntlm.h 29 Jul 2003 11:53:27 -0000
@@ -41,13 +41,7 @@
 #include <sys/types.h>
 
 /* All of this cruft is little endian */
-#ifdef WORDS_BIGENDIAN
-#define SSWAP(x) (bswap16((x)))
-#define WSWAP(x) (bswap32((x)))
-#else
-#define SSWAP(x) (x)
-#define WSWAP(x) (x)
-#endif
+#include "squid_endian.h"
 
 /* NTLM request types that we know about */
 #define NTLM_NEGOTIATE 1
Index: helpers/ntlm_auth/winbind/wb_ntlm_auth.c
===================================================================
RCS file: /cvsroot/squid/squid3/helpers/ntlm_auth/winbind/wb_ntlm_auth.c,v
retrieving revision 1.2
diff -u -r1.2 wb_ntlm_auth.c
--- helpers/ntlm_auth/winbind/wb_ntlm_auth.c 12 Feb 2003 03:14:35 -0000 1.2
+++ helpers/ntlm_auth/winbind/wb_ntlm_auth.c 29 Jul 2003 11:53:27 -0000
@@ -26,6 +26,7 @@
 
 #include "wbntlm.h"
 #include "util.h"
+#include "squid_endian.h"
 /* stdio.h is included in wbntlm.h */
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -316,7 +317,7 @@
         return;
     }
     /* Understand what we got */
- switch WSWAP(fast_header->type) {
+ switch le32toh(fast_header->type) {
     case NTLM_NEGOTIATE:
         authfail("-", "-", "Received neg-request while expecting auth packet");
         return;
Index: include/ntlmauth.h
===================================================================
RCS file: /cvsroot/squid/squid3/include/ntlmauth.h,v
retrieving revision 1.2
diff -u -r1.2 ntlmauth.h
--- include/ntlmauth.h 13 May 2003 02:12:48 -0000 1.2
+++ include/ntlmauth.h 29 Jul 2003 11:53:27 -0000
@@ -57,24 +57,7 @@
 #include "config.h"
 
 /* All of this cruft is little endian */
-#ifdef WORDS_BIGENDIAN
-#define SSWAP(x) (bswap16((x)))
-#define WSWAP(x) (bswap32((x)))
-#else
-#define SSWAP(x) (x)
-#define WSWAP(x) (x)
-#endif
-
-#ifdef HAVE_BYTESWAP_H
-#include <byteswap.h>
-#define bswap16(x) bswap_16(x)
-#define bswap32(x) bswap_32(x)
-#else /* HAVE_BISTWAP_H */
-#define bswap16(x) (((((u_int16_t)x) >> 8) & 0xff) | ((((u_int16_t)x) & 0xff) << 8))
-#define bswap32(x) \
- (((((u_int32_t)x) & 0xff000000) >> 24) | ((((u_int32_t)x) & 0x00ff0000) >> 8) | \
- ((((u_int32_t)x) & 0x0000ff00) << 8) | ((((u_int32_t)x) & 0x000000ff) << 24))
-#endif /* HAVE_BITSWAP_H */
+#include "squid_endian.h"
 
 /* Used internally. Microsoft seems to think this is right, I believe them.
  * Right. */
Index: include/squid_endian.h
===================================================================
RCS file: include/squid_endian.h
diff -N include/squid_endian.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ include/squid_endian.h 29 Jul 2003 11:53:27 -0000
@@ -0,0 +1,117 @@
+/* $Id$ */
+
+#ifndef SQUID_ENDIAN_H
+#define SQUID_ENDIAN_H
+
+/*
+ * Macros to deal with byte swapping. These macros provide
+ * the following interface:
+ *
+ * // Byte-swap
+ * u_int16_t bswap16(u_int16_t);
+ * u_int32_t bswap32(u_int32_t);
+ *
+ * // Convert from host byte order to big-endian, and vice versa.
+ * u_int16_t htobe16(u_int16_t); // equivalent to htons()
+ * u_int32_t htobe32(u_int32_t); // equivalent to htonl()
+ * u_int16_t be16toh(u_int16_t); // equivalent to ntohs()
+ * u_int32_t be32toh(u_int32_t); // equivalent to ntohs()
+ *
+ * // Convert from host byte order to little-endian, and vice versa.
+ * u_int16_t htole16(u_int16_t);
+ * u_int32_t htole32(u_int32_t);
+ * u_int16_t le16toh(u_int16_t);
+ * u_int32_t le32toh(u_int32_t);
+ */
+
+#include "config.h"
+#include "squid_types.h"
+
+/*
+ * Some systems define bswap_16() and bswap_32() in <byteswap.h>
+ *
+ * Some systems define bswap16() and bswap32() in <sys/bswap.h>.
+ *
+ * Some systems define htobe16()/be16toh() and friends in <sys/endian.h>.
+ */
+#include <sys/types.h>
+#if HAVE_BYTESWAP_H
+# include <byteswap.h>
+#endif /* HAVE_BYTESWAP_H */
+#if HAVE_SYS_BSWAP_H
+# include <sys/bswap.h>
+#endif /* HAVE_MACHINE_BSWAP_H */
+#if HAVE_SYS_ENDIAN_H
+# include <sys/endian.h>
+#endif /* HAVE_SYS_ENDIAN_H */
+
+/*
+ * Define bswap16() and bswap32() in terms of bswap_16() and bswap_32(),
+ * or the hard way.
+ */
+#if ! HAVE_BSWAP16 && ! defined(bswap16)
+# if defined(bswap_16)
+# define bswap16(x) bswap_16(x)
+# else
+# define bswap16(x) \
+ ((((u_int16_t)x) >> 8) & 0xff) | ((((u_int16_t)x) & 0xff) << 8))
+# endif
+#endif /* ! HAVE_BSWAP16 && ! defined(bswap16) */
+#if ! HAVE_BSWAP32 && ! defined(bswap32)
+# if defined(bswap_32)
+# define bswap32(x) bswap_32(x)
+# else
+# define bswap32(x) \
+ (((((u_int32_t)x) & 0xff000000) >> 24) | \
+ ((((u_int32_t)x) & 0x00ff0000) >> 8) | \
+ ((((u_int32_t)x) & 0x0000ff00) << 8) | \
+ ((((u_int32_t)x) & 0x000000ff) << 24))
+# endif
+#endif /* ! HAVE_BSWAP32 && ! defined(bswap32) */
+
+/*
+ * Define htobe*()/be*toh() in terms of hton*()/ntoh*().
+ *
+ * XXX: If htobe16() is missing, we assume that the other *be*() functions
+ * are also missing.
+ */
+#if ! HAVE_HTOBE16 && ! defined(htobe16)
+# ifdef WORDS_BIGENDIAN
+# define htobe16(x) (x)
+# define htobe32(x) (x)
+# define be16toh(x) (x)
+# define be32toh(x) (x)
+# else /* ! WORDS_BIGENDIAN */
+# define htobe16(x) htons(x)
+# define htobe32(x) htonl(x)
+# define be16toh(x) ntohs(x)
+# define be32toh(x) ntohl(x)
+# endif /* ! WORDS_BIGENDIAN */
+#endif /* ! HAVE_HTOBE16 && ! defined(htobe16) */
+
+/*
+ * Define htole*()/le*toh() in terms of bswap*().
+ *
+ * XXX: If htole16() is missing, we assume that the other *le*() functions
+ * are also missing.
+ */
+#if ! HAVE_HTOLE16 && ! defined(htole16)
+# ifdef WORDS_BIGENDIAN
+# define htole16(x) bswap16(x)
+# define htole32(x) bswap32(x)
+# define le16toh(x) bswap16(x)
+# define le32toh(x) bswap32(x)
+# else /* ! WORDS_BIGENDIAN */
+ /*
+ * XXX: What about unusual byte orders like 3412 or 2143 ?
+ * Nothing else in squid seems to care about them,
+ * so we don't worry about them here either.
+ */
+# define htole16(x) (x)
+# define htole32(x) (x)
+# define le16toh(x) (x)
+# define le32toh(x) (x)
+# endif /* ! WORDS_BIGENDIAN */
+#endif /* ! HAVE_HTOLE16 && ! defined(htole16) */
+
+#endif /* SQUID_ENDIAN_H */
Index: lib/ntlmauth.c
===================================================================
RCS file: /cvsroot/squid/squid3/lib/ntlmauth.c,v
retrieving revision 1.1
diff -u -r1.1 ntlmauth.c
--- lib/ntlmauth.c 14 Oct 2002 00:18:18 -0000 1.1
+++ lib/ntlmauth.c 29 Jul 2003 11:53:29 -0000
@@ -31,6 +31,7 @@
 #endif
 
 #include "ntlmauth.h"
+#include "squid_endian.h"
 #include "util.h" /* for base64-related stuff */
 
 #if UNUSED_CODE
@@ -80,8 +81,8 @@
 
     lstring_zero(rv);
 
- l = SSWAP(str->len);
- o = WSWAP(str->offset);
+ l = le16toh(str->len);
+ o = le32toh(str->offset);
     /* debug("fetch_string(plength=%d,l=%d,o=%d)\n",length,l,o); */
 
     if (l < 0 || l > MAX_FIELD_LENGTH || o + l > length || o == 0) {
@@ -109,9 +110,9 @@
     int l = (*payload_length);
     memcpy(payload + l, toadd, toadd_length);
 
- hdr->len = SSWAP(toadd_length);
- hdr->maxlen = SSWAP(toadd_length);
- hdr->offset = WSWAP(l + base_offset); /* 48 is the base offset of the payload */
+ hdr->len = htole16(toadd_length);
+ hdr->maxlen = htole16(toadd_length);
+ hdr->offset = le32toh(l + base_offset); /* 48 is the base offset of the payload */
     (*payload_length) += toadd_length;
 }
 
@@ -130,10 +131,10 @@
     const char *encoded;
     memset(&ch, 0, sizeof(ntlm_challenge)); /* reset */
     memcpy(ch.signature, "NTLMSSP", 8); /* set the signature */
- ch.type = WSWAP(NTLM_CHALLENGE); /* this is a challenge */
+ ch.type = htole32(NTLM_CHALLENGE); /* this is a challenge */
     ntlm_add_to_payload(ch.payload, &pl, &ch.target, domain, strlen(domain),
         NTLM_CHALLENGE_HEADER_OFFSET);
- ch.flags = WSWAP(
+ ch.flags = htole32(
         REQUEST_NON_NT_SESSION_KEY |
         CHALLENGE_TARGET_IS_DOMAIN |
         NEGOTIATE_ALWAYS_SIGN |
Received on Tue Jul 29 2003 - 05:56:59 MDT

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:20:18 MST