From 3dccb685fb1b879e8910e8732e748b22e7ff8dc7 Mon Sep 17 00:00:00 2001 From: Ken Hornstein Date: Thu, 14 Jun 2012 10:33:21 -0400 Subject: [PATCH] Make the base64 encoder endian-agnostic, and remove the function set_endian() since it's no longer needed. --- h/prototypes.h | 1 - uip/mhbuild.c | 2 -- uip/mhbuildsbr.c | 2 -- uip/mhlist.c | 2 -- uip/mhmisc.c | 17 ----------------- uip/mhn.c | 2 -- uip/mhparse.c | 50 +++++++++++++++----------------------------------- uip/mhshow.c | 2 -- uip/mhstore.c | 2 -- uip/mhtest.c | 2 -- uip/viamail.c | 2 -- 11 files changed, 15 insertions(+), 69 deletions(-) diff --git a/h/prototypes.h b/h/prototypes.h index 914036c..9c4b208 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -155,7 +155,6 @@ int mhlsbr(int, char **, FILE *(*)(char *)); int distout (char *, char *, char *); void replout (FILE *, char *, char *, struct msgs *, int, int, char *, char *, char *, int); -void set_endian(void); int sc_hardcopy(void); int sc_length(void); int sc_width(void); diff --git a/uip/mhbuild.c b/uip/mhbuild.c index a4cce1c..e8e7423 100644 --- a/uip/mhbuild.c +++ b/uip/mhbuild.c @@ -254,8 +254,6 @@ main (int argc, char **argv) compfile = cp; } - set_endian (); - /* * Check if we've specified an additional profile */ diff --git a/uip/mhbuildsbr.c b/uip/mhbuildsbr.c index 4a2c70c..00a9408 100644 --- a/uip/mhbuildsbr.c +++ b/uip/mhbuildsbr.c @@ -41,8 +41,6 @@ extern int listsw; extern int rfc934sw; extern int contentidsw; -extern int endian; /* mhmisc.c */ - /* cache policies */ extern int rcachesw; /* mhcachesbr.c */ extern int wcachesw; /* mhcachesbr.c */ diff --git a/uip/mhlist.c b/uip/mhlist.c index 72b91d1..021a4b2 100644 --- a/uip/mhlist.c +++ b/uip/mhlist.c @@ -244,8 +244,6 @@ do_cache: parts[npart] = NULL; types[ntype] = NULL; - set_endian (); - /* Check for public cache location */ if ((cache_public = context_find (nmhcache)) && *cache_public != '/') cache_public = NULL; diff --git a/uip/mhmisc.c b/uip/mhmisc.c index e8a30ac..86b8ad8 100644 --- a/uip/mhmisc.c +++ b/uip/mhmisc.c @@ -23,7 +23,6 @@ int ntype = 0; char *parts[NPARTS + 1]; char *types[NTYPES + 1]; -int endian = 0; /* little or big endian */ int userrs = 0; static char *errs = NULL; @@ -77,22 +76,6 @@ type_ok (CT ct, int sP) } -void -set_endian (void) -{ - union { - long l; - char c[sizeof(long)]; - } un; - - un.l = 1; - endian = un.c[0] ? -1 : 1; - if (debugsw) - fprintf (stderr, "%s endian architecture\n", - endian > 0 ? "big" : "little"); -} - - int make_intermediates (char *file) { diff --git a/uip/mhn.c b/uip/mhn.c index 64a7fc4..65e78d3 100644 --- a/uip/mhn.c +++ b/uip/mhn.c @@ -429,8 +429,6 @@ do_cache: parts[npart] = NULL; types[ntype] = NULL; - set_endian (); - /* * Check if we've specified an additional profile */ diff --git a/uip/mhparse.c b/uip/mhparse.c index 91cc411..25bbad7 100644 --- a/uip/mhparse.c +++ b/uip/mhparse.c @@ -22,8 +22,6 @@ extern int debugsw; -extern int endian; /* mhmisc.c */ - extern pid_t xpid; /* mhshowsbr.c */ /* cache policies */ @@ -1728,8 +1726,8 @@ openBase64 (CT ct, char **file) { int bitno, cc, digested; int fd, len, skip, own_ct_fp = 0; - unsigned long bits; - unsigned char value, *b, *b1, *b2, *b3; + uint32_t bits; + unsigned char value, b; unsigned char *cp, *ep; char buffer[BUFSIZ]; /* sbeck -- handle suffixes */ @@ -1737,16 +1735,6 @@ openBase64 (CT ct, char **file) CE ce; MD5_CTX mdContext; - /* the decoder works on the least-significant three bytes of the bits integer, - but their position in memory depend on both endian-ness and size of - long int... for little-endian architectures the size is irrelevant, for - big-endian archs it's crucial... ideally we'd adopt posix and use a64l instead - of this mess. */ - b = (unsigned char *) &bits; - b1 = &b[endian > 0 ? sizeof(bits)==8?5:1 : 2]; - b2 = &b[endian > 0 ? sizeof(bits)==8?6:2 : 1]; - b3 = &b[endian > 0 ? sizeof(bits)==8?7:3 : 0]; - ce = ct->c_cefile; if (ce->ce_fp) { fseek (ce->ce_fp, 0L, SEEK_SET); @@ -1855,17 +1843,20 @@ openBase64 (CT ct, char **file) bits |= value << bitno; test_end: if ((bitno -= 6) < 0) { - putc ((char) *b1, ce->ce_fp); + b = (bits >> 16) & 0xff; + putc ((char) b, ce->ce_fp); if (digested) - MD5Update (&mdContext, b1, 1); + MD5Update (&mdContext, &b, 1); if (skip < 2) { - putc ((char) *b2, ce->ce_fp); + b = (bits >> 8) & 0xff; + putc ((char) b, ce->ce_fp); if (digested) - MD5Update (&mdContext, b2, 1); + MD5Update (&mdContext, &b, 1); if (skip < 1) { - putc ((char) *b3, ce->ce_fp); + b = bits & 0xff; + putc ((char) b, ce->ce_fp); if (digested) - MD5Update (&mdContext, b3, 1); + MD5Update (&mdContext, &b, 1); } } @@ -2825,20 +2816,9 @@ static int readDigest (CT ct, char *cp) { int bitno, skip; - unsigned long bits; + uint32_t bits; char *bp = cp; unsigned char *dp, value, *ep; - unsigned char *b, *b1, *b2, *b3; - - /* the decoder works on the least-significant three bytes of the bits integer, - but their position in memory depend on both endian-ness and size of - long int... for little-endian architectures the size is irrelevant, for - big-endian archs it's crucial... ideally we'd adopt posix and use a64l instead - of this mess. */ - b = (unsigned char *) &bits; - b1 = &b[endian > 0 ? sizeof(bits)==8?5:1 : 2]; - b2 = &b[endian > 0 ? sizeof(bits)==8?6:2 : 1]; - b3 = &b[endian > 0 ? sizeof(bits)==8?7:3 : 0]; bitno = 18; bits = 0L; @@ -2861,11 +2841,11 @@ test_end: if ((bitno -= 6) < 0) { if (dp + (3 - skip) > ep) goto invalid_digest; - *dp++ = *b1; + *dp++ = (bits >> 16) & 0xff; if (skip < 2) { - *dp++ = *b2; + *dp++ = (bits >> 8) & 0xff; if (skip < 1) - *dp++ = *b3; + *dp++ = bits & 0xff; } bitno = 18; bits = 0L; diff --git a/uip/mhshow.c b/uip/mhshow.c index e4eba4f..993477b 100644 --- a/uip/mhshow.c +++ b/uip/mhshow.c @@ -285,8 +285,6 @@ do_cache: parts[npart] = NULL; types[ntype] = NULL; - set_endian (); - /* * Check if we've specified an additional profile */ diff --git a/uip/mhstore.c b/uip/mhstore.c index 41e62c7..e491dd9 100644 --- a/uip/mhstore.c +++ b/uip/mhstore.c @@ -231,8 +231,6 @@ do_cache: parts[npart] = NULL; types[ntype] = NULL; - set_endian (); - /* * Check if we've specified an additional profile */ diff --git a/uip/mhtest.c b/uip/mhtest.c index caaf7af..cbaefd3 100644 --- a/uip/mhtest.c +++ b/uip/mhtest.c @@ -232,8 +232,6 @@ do_cache: parts[npart] = NULL; types[ntype] = NULL; - set_endian (); - if (outfile == NULL) adios (NULL, "must specify output file"); diff --git a/uip/viamail.c b/uip/viamail.c index 6e47ea5..e9b26b7 100644 --- a/uip/viamail.c +++ b/uip/viamail.c @@ -146,8 +146,6 @@ main (int argc, char **argv) } } - set_endian (); - if (!f1) adios (NULL, "missing -viamail \"mailpath\" switch"); -- 1.7.10.4