NCBI C++ ToolKit
ct_nlmzip_bits.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ct_nlmzip_bits.cpp 94680 2021-08-30 13:02:50Z vakatov $ */
2 /*****************************************************************************
3 
4  Name: bits.c ur/compr/bits.c
5 
6  Description: Utility functions for compress/uncompress data
7 
8  Author: Grisha Starchenko
9  InforMax, Inc.
10  Gaithersburg, USA.
11 
12  ***************************************************************************
13 
14  PUBLIC DOMAIN NOTICE
15  National Center for Biotechnology Information
16 
17  This software/database is a "United States Government Work" under the
18  terms of the United States Copyright Act. It was written as part of
19  the author's official duties as a United States Government employee
20  and thus cannot be copyrighted. This software/database is freely
21  available to the public for use. The National Library of Medicine and
22  the U.S. Government have not placed any restriction on its use or
23  reproduction.
24 
25  Although all reasonable efforts have been taken to ensure the accuracy
26  and reliability of the software and data, the NLM and the U.S.
27  Government do not and cannot warrant the performance or results that
28  may be obtained by using this software or data. The NLM and the U.S.
29  Government disclaim all warranties, express or implied, including
30  warranties of performance, merchantability or fitness for any
31  particular purpose.
32 
33  Please cite the author in any work or product based on this material.
34 
35  ***************************************************************************
36 
37  Entry Points:
38 
39  void
40  Nlmzip_bi_init (void)
41 
42  void
43  Nlmzip_send_bits (value,length)
44  int value; [ value to send (I) ]
45  int length; [ number of bits (I) ]
46 
47  Uint4
48  Nlmzip_bi_reverse (code,len)
49  Uint4 code; [ the value to invert (I) ]
50  int len; [ its bit length (I) ]
51 
52  void
53  Nlmzip_bi_windup (void)
54 
55  void
56  Nlmzip_copy_block (buf,len,header)
57  char *buf; [ the input data (I) ]
58  Uint4 len; [ its length (I) ]
59  int header; [ true if block header must be written (I) ]
60 
61  Modification History:
62  05 Aug 1995 - grisha - original written
63 
64  Bugs and restriction on use:
65 
66  Notes:
67 
68 *****************************************************************************/
69 
70 #include <ncbi_pch.hpp>
71 #include "ct_nlmzip_i.h"
72 
74 
75 
76 /****************************************************************************/
77 /* DEFINES */
78 /****************************************************************************/
79 
80 /* Number of bits used within bi_buf. (bi_buf might be implemented on
81  more than 16 bits on some systems.)
82 */
83 #define Buf_size (8 * 2 * sizeof(char))
84 
85 /****************************************************************************/
86 /* LOCAL VARIABLES */
87 /****************************************************************************/
88 
89 /* Output buffer. bits are inserted starting at the bottom
90  (least significant bits).
91 */
92 static unsigned short bi_buf;
93 
94 /* Number of valid bits in bi_buf. All bits above the last valid
95  bit are always zero.
96 */
97 static int bi_valid;
98 
99 /****************************************************************************/
100 /* GLOBAL FUNCTIONS */
101 /****************************************************************************/
102 
103 /****************************************************************************/
104 /*.doc Nlmzip_bi_init (external) */
105 /*+
106  Initialize the bit string routines.
107 -*/
108 /****************************************************************************/
109 void
110 Nlmzip_bi_init (void) /*FCN*/
111 {
112  bi_buf = 0;
113  bi_valid = 0;
114 } /* Nlmzip_bi_init() */
115 
116 /****************************************************************************/
117 /*.doc Nlmzip_send_bits (external) */
118 /*+
119  Send a value on a given number of bits.
120  IN assertion: length <= 16 and value fits in length bits.
121 -*/
122 /****************************************************************************/
123 void
125  int value, /* value to send (I) */
126  int length /* number of bits (I) */
127 ){
128  /* If not enough room in bi_buf, use (valid) bits from bi_buf and
129  (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
130  unused bits in value.
131  */
132  if ( bi_valid > (int)Buf_size - length ) {
133  bi_buf |= (value << bi_valid);
135  bi_buf = (ush)value >> (Buf_size - bi_valid);
136  bi_valid += length - Buf_size;
137  } else {
138  bi_buf |= value << bi_valid;
139  bi_valid += length;
140  }
141 } /* Nlmzip_send_bits() */
142 
143 /****************************************************************************/
144 /*.doc Nlmzip_bi_reverse (external) */
145 /*+
146  Reverse the first len bits of a code, using straightforward code (a faster
147  Nlmzip_method would use a table)
148  IN assertion: 1 <= len <= 15
149 -*/
150 /****************************************************************************/
151 Uint4
153  Uint4 code, /* the value to invert (I) */
154  int len /* its bit length (I) */
155 ){
156  Uint4 res = 0;
157 
158  do {
159  res |= code & 1;
160  code >>= 1;
161  res <<= 1;
162  } while ( --len > 0 );
163 
164  return res >> 1;
165 }
166 
167 /****************************************************************************/
168 /*.doc Nlmzip_bi_windup (external) */
169 /*+
170  Write out any remaining bits in an incomplete byte.
171 -*/
172 /****************************************************************************/
173 void
174 Nlmzip_bi_windup (void) /*FCN*/
175 {
176  if ( bi_valid > 8 ) {
178  } else if ( bi_valid > 0 ) {
179  Nlmzip_WriteByte ((unsigned char)bi_buf);
180  }
181  bi_buf = 0;
182  bi_valid = 0;
183 }
184 
185 /****************************************************************************/
186 /*.doc Nlmzip_copy_block (external) */
187 /*+
188  Copy a stored block to the zip file, storing first the length and
189  its one's complement if requested.
190 -*/
191 /****************************************************************************/
192 void
194  char *buf, /* the input data (I) */
195  Uint4 len, /* its length (I) */
196  int header /* true if block header must be written (I) */
197 ){
198  Nlmzip_bi_windup(); /* align on byte boundary */
199 
200  if ( header ) { /* write header information */
203  }
204  while ( len-- ) { /* write data */
205  Nlmzip_WriteByte (*buf++);
206  }
207 }
208 
209 
#define Buf_size
void Nlmzip_bi_init(void)
static unsigned short bi_buf
void Nlmzip_send_bits(int value, int length)
Uint4 Nlmzip_bi_reverse(Uint4 code, int len)
void Nlmzip_copy_block(char *buf, Uint4 len, int header)
void Nlmzip_bi_windup(void)
static int bi_valid
void Nlmzip_WriteByte(unsigned char theChar)
void Nlmzip_WriteShort(unsigned short usData)
#define BEGIN_CTRANSITION_SCOPE
Definition: ncbilcl.hpp:49
#define END_CTRANSITION_SCOPE
Definition: ncbilcl.hpp:50
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
char * buf
int len
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Definition: inftrees.h:24
unsigned short ush
Definition: zutil.h:41
Modified on Fri May 24 14:58:16 2024 by modify_doxy.py rev. 669887