Functions for bitio. Taken as is from The Data Compression Book
authorvestbo <vestbo@f7af4fe6-9843-0410-8265-dc069ae4e863>
Thu, 31 Jan 2002 17:46:09 +0000 (17:46 +0000)
committervestbo <vestbo@f7af4fe6-9843-0410-8265-dc069ae4e863>
Thu, 31 Jan 2002 17:46:09 +0000 (17:46 +0000)
HLT/comp/bitio.c [new file with mode: 0644]
HLT/comp/bitio.h [new file with mode: 0644]
HLT/comp/errhand.c [new file with mode: 0644]
HLT/comp/errhand.h [new file with mode: 0644]

diff --git a/HLT/comp/bitio.c b/HLT/comp/bitio.c
new file mode 100644 (file)
index 0000000..c46cb85
--- /dev/null
@@ -0,0 +1,153 @@
+/************************** Start of BITIO.C *************************
+ *
+ * This utility file contains all of the routines needed to impement
+ * bit oriented routines under either ANSI or K&R C.  It needs to be
+ * linked with every program used in the entire book.
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include "bitio.h"
+#include "errhand.h"
+
+#define PACIFIER_COUNT 2047
+
+BIT_FILE *OpenOutputBitFile( char *name )
+{
+  BIT_FILE *bit_file;
+
+    bit_file = (BIT_FILE *) calloc( 1, sizeof( BIT_FILE ) );
+    if ( bit_file == NULL )
+        return( bit_file );
+    bit_file->file = fopen( name, "wb" );
+    bit_file->rack = 0;
+    bit_file->mask = 0x80;
+    bit_file->pacifier_counter = 0;
+    return( bit_file );
+}
+
+BIT_FILE *OpenInputBitFile( char *name )
+{
+    BIT_FILE *bit_file;
+
+    bit_file = (BIT_FILE *) calloc( 1, sizeof( BIT_FILE ) );
+    if ( bit_file == NULL )
+       return( bit_file );
+    bit_file->file = fopen( name, "rb" );
+    bit_file->rack = 0;
+    bit_file->mask = 0x80;
+    bit_file->pacifier_counter = 0;
+    return( bit_file );
+}
+
+void CloseOutputBitFile(BIT_FILE *bit_file )
+{
+    if ( bit_file->mask != 0x80 )
+        if ( putc( bit_file->rack, bit_file->file ) != bit_file->rack )
+            fatal_error( "Fatal error in CloseBitFile!\n" );
+    fclose( bit_file->file );
+    free( (char *) bit_file );
+}
+
+void CloseInputBitFile(BIT_FILE *bit_file )
+{
+    fclose( bit_file->file );
+    free( (char *) bit_file );
+}
+
+void OutputBit( BIT_FILE *bit_file, int bit )
+{
+    if ( bit )
+        bit_file->rack |= bit_file->mask;
+    bit_file->mask >>= 1;
+    if ( bit_file->mask == 0 ) {
+       if ( putc( bit_file->rack, bit_file->file ) != bit_file->rack )
+           fatal_error( "Fatal error in OutputBit!\n" );
+       else
+        if ( ( bit_file->pacifier_counter++ & PACIFIER_COUNT ) == 0 )
+               putc( '.', stdout );
+       bit_file->rack = 0;
+       bit_file->mask = 0x80;
+    }
+}
+
+void OutputBits(BIT_FILE *bit_file,unsigned long code, int count )
+{
+    unsigned long mask;
+
+    mask = 1L << ( count - 1 );
+    while ( mask != 0) {
+        if ( mask & code )
+            bit_file->rack |= bit_file->mask;
+        bit_file->mask >>= 1;
+        if ( bit_file->mask == 0 ) {
+           if ( putc( bit_file->rack, bit_file->file ) != bit_file->rack )
+               fatal_error( "Fatal error in OutputBit!\n" );
+        else if ( ( bit_file->pacifier_counter++ & PACIFIER_COUNT ) == 0 )
+               putc( '.', stdout );
+           bit_file->rack = 0;
+            bit_file->mask = 0x80;
+        }
+        mask >>= 1;
+    }
+}
+
+int InputBit( BIT_FILE *bit_file )
+{
+    int value;
+
+    if ( bit_file->mask == 0x80 ) {
+        bit_file->rack = getc( bit_file->file );
+        if ( bit_file->rack == EOF )
+            fatal_error( "Fatal error in InputBit!\n" );
+    if ( ( bit_file->pacifier_counter++ & PACIFIER_COUNT ) == 0 )
+           putc( '.', stdout );
+    }
+    value = bit_file->rack & bit_file->mask;
+    bit_file->mask >>= 1;
+    if ( bit_file->mask == 0 )
+       bit_file->mask = 0x80;
+    return( value ? 1 : 0 );
+}
+
+unsigned long InputBits( BIT_FILE *bit_file, int bit_count )
+{
+    unsigned long mask;
+    unsigned long return_value;
+
+    mask = 1L << ( bit_count - 1 );
+    return_value = 0;
+    while ( mask != 0) {
+       if ( bit_file->mask == 0x80 ) {
+           bit_file->rack = getc( bit_file->file );
+           if ( bit_file->rack == EOF )
+               fatal_error( "Fatal error in InputBit!\n" );
+        if ( ( bit_file->pacifier_counter++ & PACIFIER_COUNT ) == 0 )
+               putc( '.', stdout );
+       }
+       if ( bit_file->rack & bit_file->mask )
+            return_value |= mask;
+        mask >>= 1;
+        bit_file->mask >>= 1;
+        if ( bit_file->mask == 0 )
+            bit_file->mask = 0x80;
+    }
+    return( return_value );
+}
+
+void FilePrintBinary( FILE *file, unsigned int code, int bits )
+{
+    unsigned int mask;
+
+    mask = 1 << ( bits - 1 );
+    while ( mask != 0 ) {
+        if ( code & mask )
+            fputc( '1', file );
+        else
+            fputc( '0', file );
+        mask >>= 1;
+    }
+}
+
+/*************************** End of BITIO.C **************************/
+
diff --git a/HLT/comp/bitio.h b/HLT/comp/bitio.h
new file mode 100644 (file)
index 0000000..0a6c6a0
--- /dev/null
@@ -0,0 +1,45 @@
+/************************** Start of BITIO.H *************************/
+
+#ifndef _BITIO_H
+#define _BITIO_H
+
+#include <stdio.h>
+
+typedef struct bit_file {
+    FILE *file;
+    unsigned char mask;
+    int rack;
+    int pacifier_counter;
+} BIT_FILE;
+
+//#ifdef __STDC__
+
+BIT_FILE     *OpenInputBitFile( char *name );
+BIT_FILE     *OpenOutputBitFile( char *name );
+void          OutputBit( BIT_FILE *bit_file, int bit );
+void          OutputBits( BIT_FILE *bit_file,
+                          unsigned long code, int count );
+int           InputBit( BIT_FILE *bit_file );
+unsigned long InputBits( BIT_FILE *bit_file, int bit_count );
+void          CloseInputBitFile( BIT_FILE *bit_file );
+void          CloseOutputBitFile( BIT_FILE *bit_file );
+void          FilePrintBinary( FILE *file, unsigned int code, int bits );
+/*
+#else   
+
+BIT_FILE     *OpenInputBitFile();
+BIT_FILE     *OpenOutputBitFile();
+void          OutputBit();
+void          OutputBits();
+int           InputBit();
+unsigned long InputBits();
+void          CloseInputBitFile();
+void          CloseOutputBitFile();
+void          FilePrintBinary();
+
+#endif  
+*/
+#endif  /* _BITIO_H */
+
+/*************************** End of BITIO.H **************************/
+
diff --git a/HLT/comp/errhand.c b/HLT/comp/errhand.c
new file mode 100644 (file)
index 0000000..5be6730
--- /dev/null
@@ -0,0 +1,38 @@
+/************************* Start of ERRHAND.C ************************
+ *
+ * This is a general purpose error handler used with every program in
+ * the book.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include "errhand.h"
+/*
+  #ifdef __STDC__
+  
+  
+  #else
+  #ifdef __UNIX__
+  void fatal_error( fmt, va_alist )
+  char *fmt;
+  va_dcl
+  #else
+  void fatal_error( fmt )
+  char *fmt;
+  #endif
+  #endif
+*/
+void fatal_error( char *fmt, ... )
+{
+    va_list argptr;
+
+    va_start( argptr, fmt );
+    printf( "Fatal error: " );
+    vprintf( fmt, argptr );
+    va_end( argptr );
+    exit( -1 );
+}
+
+/************************** End of ERRHAND.C *************************/
+
diff --git a/HLT/comp/errhand.h b/HLT/comp/errhand.h
new file mode 100644 (file)
index 0000000..2d58bb1
--- /dev/null
@@ -0,0 +1,18 @@
+/************************* Start of ERRHAND.H ************************/
+
+#ifndef _ERRHAND_H
+#define _ERRHAND_H
+
+//#ifdef __STDC__
+
+void fatal_error( char *fmt, ... );
+/*
+#else
+
+void fatal_error();
+
+#endif
+*/
+#endif  /* _ERRHAND_H */
+
+/************************** End of ERRHAND.H *************************/