1 ////////////////////////////////////////////////////////////////////////////
2 // **** WAVPACK **** //
3 // Hybrid Lossless Wavefile Compressor //
4 // Copyright (c) 1998 - 2004 Conifer Software. //
5 // All Rights Reserved. //
6 // Distributed under the BSD Software License (see license.txt) //
7 ////////////////////////////////////////////////////////////////////////////
11 // This module provides utilities to support the BitStream structure which is
12 // used to read and write all WavPack audio data streams. It also contains a
13 // wrapper for the stream I/O functions and a set of functions dealing with
14 // endian-ness, both for enhancing portability. Finally, a debug wrapper for
15 // the malloc() system is provided.
22 ////////////////////////// Bitstream functions ////////////////////////////////
24 // Open the specified BitStream and associate with the specified buffer.
26 static void bs_read (Bitstream
*bs
);
28 void bs_open_read (Bitstream
*bs
, uchar
*buffer_start
, uchar
*buffer_end
, read_stream file
, uint32_t file_bytes
)
31 bs
->buf
= buffer_start
;
35 bs
->ptr
= bs
->end
- 1;
36 bs
->file_bytes
= file_bytes
;
40 bs
->ptr
= bs
->buf
- 1;
45 // This function is only called from the getbit() and getbits() macros when
46 // the BitStream has been exhausted and more data is required. Sinve these
47 // bistreams no longer access files, this function simple sets an error and
50 static void bs_read (Bitstream
*bs
)
52 if (bs
->file
&& bs
->file_bytes
) {
53 uint32_t bytes_read
, bytes_to_read
= bs
->end
- bs
->buf
;
55 if (bytes_to_read
> bs
->file_bytes
)
56 bytes_to_read
= bs
->file_bytes
;
58 bytes_read
= bs
->file (bs
->buf
, bytes_to_read
);
61 bs
->end
= bs
->buf
+ bytes_read
;
62 bs
->file_bytes
-= bytes_read
;
65 memset (bs
->buf
, -1, bs
->end
- bs
->buf
);
73 memset (bs
->buf
, -1, bs
->end
- bs
->buf
);
78 // Open the specified BitStream using the specified buffer pointers. It is
79 // assumed that enough buffer space has been allocated for all data that will
80 // be written, otherwise an error will be generated.
82 static void bs_write (Bitstream
*bs
);
84 void bs_open_write (Bitstream
*bs
, uchar
*buffer_start
, uchar
*buffer_end
)
86 bs
->error
= bs
->sr
= bs
->bc
= 0;
87 bs
->ptr
= bs
->buf
= buffer_start
;
92 // This function is only called from the putbit() and putbits() macros when
93 // the buffer is full, which is now flagged as an error.
95 static void bs_write (Bitstream
*bs
)
101 // This function forces a flushing write of the specified BitStream, and
102 // returns the total number of bytes written into the buffer.
104 uint32_t bs_close_write (Bitstream
*bs
)
106 uint32_t bytes_written
;
109 return (uint32_t) -1;
111 while (bs
->bc
|| ((bs
->ptr
- bs
->buf
) & 1)) putbit_1 (bs
);
112 bytes_written
= bs
->ptr
- bs
->buf
;
114 return bytes_written
;
117 /////////////////////// Endian Correction Routines ////////////////////////////
119 void little_endian_to_native (void *data
, char *format
)
121 uchar
*cp
= (uchar
*) data
;
126 *(long *)cp
= letoh32(*(long *)cp
);
131 *(short *)cp
= letoh16(*(short *)cp
);
136 if (*format
>= '0' && *format
<= '9')
146 void native_to_little_endian (void *data
, char *format
)
148 uchar
*cp
= (uchar
*) data
;
153 *(long *)cp
= htole32(*(long *)cp
);
158 *(short *)cp
= htole16(*(short *)cp
);
163 if (*format
>= '0' && *format
<= '9')