diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2015-06-28 15:34:21 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2015-06-28 15:34:21 +0000 |
commit | 620ecbee2ed1cb478e0289722d86dd72717f1cb8 (patch) | |
tree | 7a427b24d86f6789706acee21a52dca15b88806b /lib/util | |
parent | 8c2dab4922b514045cbae8e71ba93aaf8c0fff48 (diff) | |
download | netpbm-mirror-620ecbee2ed1cb478e0289722d86dd72717f1cb8.tar.gz netpbm-mirror-620ecbee2ed1cb478e0289722d86dd72717f1cb8.tar.xz netpbm-mirror-620ecbee2ed1cb478e0289722d86dd72717f1cb8.zip |
Release 10.71.00
git-svn-id: http://svn.code.sf.net/p/netpbm/code/advanced@2588 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/util')
-rw-r--r-- | lib/util/Makefile | 1 | ||||
-rw-r--r-- | lib/util/bitio.c | 207 | ||||
-rw-r--r-- | lib/util/bitio.h | 89 | ||||
-rw-r--r-- | lib/util/nstring.c | 62 |
4 files changed, 333 insertions, 26 deletions
diff --git a/lib/util/Makefile b/lib/util/Makefile index 28dfddfe..c8522a04 100644 --- a/lib/util/Makefile +++ b/lib/util/Makefile @@ -12,6 +12,7 @@ include $(BUILDDIR)/config.mk # nstring is required for asprintf(), etc. Also some systems don't have # snprintf(), e.g. Solaris 2.5.1. 2002.03.29. UTILOBJECTS = \ + bitio.o \ filename.o \ io.o \ mallocvar.o \ diff --git a/lib/util/bitio.c b/lib/util/bitio.c new file mode 100644 index 00000000..ca1b55f9 --- /dev/null +++ b/lib/util/bitio.c @@ -0,0 +1,207 @@ +/*\ + * $Id: bitio.c,v 1.5 1992/11/24 19:36:46 dws Exp dws $ + * + * bitio.c - bitstream I/O + * + * Works for (sizeof(unsigned long)-1)*8 bits. + * + * Copyright (C) 1992 by David W. Sanderson. + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose and without fee is hereby granted, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. This software is provided "as is" + * without express or implied warranty. + * + * $Log: bitio.c,v $ + * Revision 1.5 1992/11/24 19:36:46 dws + * Added copyright. + * + * Revision 1.4 1992/11/17 03:37:50 dws + * updated comment + * + * Revision 1.3 1992/11/10 23:15:16 dws + * Removed superfluous code. + * + * Revision 1.2 1992/11/10 23:11:22 dws + * Generalized to handle more than one bitstream at once. + * + * Revision 1.1 1992/11/10 18:33:21 dws + * Initial revision + * +\*/ + +#include <string.h> + +#include "bitio.h" + +struct bitstream +{ + FILE * + f; /* bytestream */ + unsigned long + bitbuf; /* bit buffer */ + int + nbitbuf; /* number of bits in 'bitbuf' */ + char + mode; +}; + +#define Mask(n) ((1<<(n))-1) + +#define BitPut(b,ul,n) ((b)->bitbuf = (((b)->bitbuf<<(n)) \ + |((ul)&Mask(n))), \ + (b)->nbitbuf += (n)) + +#define BitGet(b,n) (((b)->bitbuf>>((b)->nbitbuf-=(n))) & Mask(n)) + +/* + * pm_bitinit() - allocate and return a struct bitstream * for the + * given FILE*. + * + * mode must be one of "r" or "w", according to whether you will be + * reading from or writing to the struct bitstream *. + * + * Returns 0 on error. + */ + +struct bitstream * +pm_bitinit(FILE * const f, const char * const mode) { + + struct bitstream * ans; + + if (!f || !mode) + ans = NULL; + else if (strcmp(mode, "r") != 0 && strcmp(mode, "w") != 0) + ans = NULL; + else { + ans = (struct bitstream *)calloc(1, sizeof(struct bitstream)); + if (ans != NULL) { + ans->f = f; + ans->mode = *mode; + } + } + return ans; +} + +/* + * pm_bitfini() - deallocate the given struct bitstream *. + * + * You must call this after you are done with the struct bitstream *. + * + * It may flush some bits left in the buffer. + * + * Returns the number of bytes written, -1 on error. + */ + +int +pm_bitfini(b) + struct bitstream *b; +{ + int nbyte = 0; + + if(!b) + return -1; + + /* flush the output */ + if(b->mode == 'w') + { + /* flush the bits */ + if (b->nbitbuf < 0 || b->nbitbuf >= 8) + { + /* pm_bitwrite() didn't work */ + return -1; + } + + /* + * If we get to here, nbitbuf is 0..7 + */ + if(b->nbitbuf) + { + char c; + + BitPut(b, 0, (long)8-(b->nbitbuf)); + c = (char) BitGet(b, (long)8); + if(putc(c, b->f) == EOF) + { + return -1; + } + nbyte++; + } + } + + free(b); + return nbyte; +} + +/* + * pm_bitread() - read the next nbits into *val from the given file. + * + * The last pm_bitread() must be followed by a call to pm_bitfini(). + * + * Returns the number of bytes read, -1 on error. + */ + +int +pm_bitread(b, nbits, val) + struct bitstream *b; + unsigned long nbits; + unsigned long *val; +{ + int nbyte = 0; + int c; + + if(!b) + return -1; + + while (b->nbitbuf < nbits) + { + if((c = getc(b->f)) == EOF) + { + return -1; + } + nbyte++; + + BitPut(b, c, (long) 8); + } + + *val = BitGet(b, nbits); + return nbyte; +} + +/* + * pm_bitwrite() - write the low nbits of val to the given file. + * + * The last pm_bitwrite() must be followed by a call to pm_bitfini(). + * + * Returns the number of bytes written, -1 on error. + */ + +int +pm_bitwrite(b, nbits, val) + struct bitstream *b; + unsigned long nbits; + unsigned long val; +{ + int nbyte = 0; + char c; + + if(!b) + return -1; + + BitPut(b, val, nbits); + + while (b->nbitbuf >= 8) + { + c = (char) BitGet(b, (long)8); + + if(putc(c, b->f) == EOF) + { + return -1; + } + nbyte++; + } + + return nbyte; +} diff --git a/lib/util/bitio.h b/lib/util/bitio.h new file mode 100644 index 00000000..dfc5a153 --- /dev/null +++ b/lib/util/bitio.h @@ -0,0 +1,89 @@ +/*\ + * $Id: bitio.h,v 1.4 1992/11/24 19:37:02 dws Exp dws $ + * + * bitio.h - bitstream I/O + * + * Works for (sizeof(unsigned long)-1)*8 bits. + * + * Copyright (C) 1992 by David W. Sanderson. + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose and without fee is hereby granted, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. This software is provided "as is" + * without express or implied warranty. + * + * $Log: bitio.h,v $ + * Revision 1.4 1992/11/24 19:37:02 dws + * Added copyright + * + * Revision 1.3 1992/11/17 03:37:59 dws + * updated comment + * + * Revision 1.2 1992/11/10 23:10:22 dws + * Generalized to handle more than one bitstream at a time. + * + * Revision 1.1 1992/11/10 18:33:51 dws + * Initial revision + * +\*/ + +#ifndef _BITIO_H_ +#define _BITIO_H_ + +#include <netpbm/pm.h> + +#ifdef __cplusplus +extern "C" { +#endif +#if 0 +} /* to fake out automatic code indenters */ +#endif + +typedef struct bitstream *BITSTREAM; + +struct bitstream * +pm_bitinit(FILE * const f, const char * const mode); + +/* + * pm_bitfini() - deallocate the given BITSTREAM. + * + * You must call this after you are done with the BITSTREAM. + * + * It may flush some bits left in the buffer. + * + * Returns the number of bytes written, -1 on error. + */ + +int +pm_bitfini(BITSTREAM b); + +/* + * pm_bitread() - read the next nbits into *val from the given file. + * + * Returns the number of bytes read, -1 on error. + */ + +int +pm_bitread(BITSTREAM b, + unsigned long nbits, + unsigned long * val); + +/* + * pm_bitwrite() - write the low nbits of val to the given file. + * + * The last pm_bitwrite() must be followed by a call to pm_bitflush(). + * + * Returns the number of bytes written, -1 on error. + */ + +int +pm_bitwrite(BITSTREAM b, + unsigned long nbits, + unsigned long val); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/lib/util/nstring.c b/lib/util/nstring.c index 039c2b3b..711cfca9 100644 --- a/lib/util/nstring.c +++ b/lib/util/nstring.c @@ -897,49 +897,59 @@ pm_stripeq(const char * const comparand, Return 1 (true) if the strings are identical; 0 (false) otherwise. -----------------------------------------------------------------------------*/ - char *p, *q, *px, *qx; - char equal; + const char * p; + const char * q; + const char * px; + const char * qx; + bool equal; /* Make p and q point to the first non-blank character in each string. - If there are no non-blank characters, make them point to the terminating - NULL. - */ + If there are no non-blank characters, make them point to the terminating + NUL. + */ - p = (char *) comparand; - while (ISSPACE(*p)) p++; - q = (char *) comparator; - while (ISSPACE(*q)) q++; + p = &comparand[0]; + while (ISSPACE(*p)) + p++; + q = &comparator[0]; + while (ISSPACE(*q)) + q++; /* Make px and qx point to the last non-blank character in each string. If there are no nonblank characters (which implies the string is - null), make them point to the terminating NULL. + null), make them point to the terminating NUL. */ - if (*p == '\0') px = p; + if (*p == '\0') + px = p; else { px = p + strlen(p) - 1; - while (ISSPACE(*px)) px--; + while (ISSPACE(*px)) + --px; } - if (*q == '\0') qx = q; + if (*q == '\0') + qx = q; else { qx = q + strlen(q) - 1; - while (ISSPACE(*qx)) qx--; + while (ISSPACE(*qx)) + --qx; } - equal = 1; /* initial assumption */ - - /* If the stripped strings aren't the same length, - we know they aren't equal - */ - if (px - p != qx - q) equal = 0; - - else - while (p <= px) { - if (*p != *q) equal = 0; - p++; q++; + if (px - p != qx - q) { + /* The stripped strings aren't the same length, so we know they aren't + equal. + */ + equal = false; + } else { + /* Move p and q through the nonblank characters, comparing. */ + for (equal = true; p <= px; ++p, ++q) { + assert(q <= qx); /* Because stripped strings are same length */ + if (*p != *q) + equal = false; + } } - return equal; + return equal ? 1 : 0; } |