}
}
- /*
-
- shift bitbuf n bits left, read n bits
-
- <---- n bits shift |
-
- 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- bitbuf | |
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- <-----n----->
-
-
-
- read from file
- <------------->
- 7 6 5 4 3 2 1 0
- +-+-+-+-+-+-+-+-+
- subbitbuf| 1 1 1|
- +-+-+-+-+-+-+-+-+
- <-----n----->
- <--->
- bitcount (size of subbitbuf data)
-
- */
-
- const char *
+ static const char *
-bitstring(unsigned int bitbuf, int n, char *ptr)
+bitstring(unsigned int bitbuf, int n, char *ptr, size_t sz)
{
static char str[256];
+ size_t size = sz;
- if (ptr == NULL)
- ptr = str;
+ if (ptr == NULL) {
+ ptr = str;
+ size = sizeof(str);
+ }
- if (n+1 > sizeof(str))
+ if (n+1 > size)
return "bit size is too big";
- str[n] = '\0';
+ ptr[n] = '\0';
while (n > 0) {
- str[--n] = (bitbuf & 1) ? '1' : '0';
+ ptr[--n] = (bitbuf & 1) ? '1' : '0';
bitbuf >>= 1;
}
- return str;
+ return ptr;
}
+ /*
+ fill bitbuf for reading.
+
+ o shift bitbuf n bits left.
+ o read 8 bits from file and put it to subbitbuf.
+ o get n bits from subbitbuf and fill into bitbuf.
+
+ 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ bitbuf | a b c d e|
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+
+ already filled at last time.
+ <--------->
+ 7 6 5 4 3 2 1 0
+ +-+-+-+-+-+-+-+-+
+ subbitbuf |a b c d e 1 1 1|
+ +-+-+-+-+-+-+-+-+
+ <----->
+ bitcount (size of subbitbuf data)
+ */
void
fillbuf(struct lzh_istream *rp, int n)
{