summaryrefslogtreecommitdiff
path: root/apps/codecs/libwavpack/bits.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libwavpack/bits.c')
-rw-r--r--apps/codecs/libwavpack/bits.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/apps/codecs/libwavpack/bits.c b/apps/codecs/libwavpack/bits.c
new file mode 100644
index 0000000000..25a222780f
--- /dev/null
+++ b/apps/codecs/libwavpack/bits.c
@@ -0,0 +1,140 @@
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////////////////////////////////////////////////////////////////////////////
8
9// bits.c
10
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.
16
17#include "wavpack.h"
18
19#include <string.h>
20#include <ctype.h>
21
22////////////////////////// Bitstream functions ////////////////////////////////
23
24// Open the specified BitStream and associate with the specified buffer.
25
26static void bs_read (Bitstream *bs);
27
28void bs_open_read (Bitstream *bs, uchar *buffer_start, uchar *buffer_end, read_stream file, ulong file_bytes)
29{
30 CLEAR (*bs);
31 bs->buf = buffer_start;
32 bs->end = buffer_end;
33
34 if (file) {
35 bs->ptr = bs->end - 1;
36 bs->file_bytes = file_bytes;
37 bs->file = file;
38 }
39 else
40 bs->ptr = bs->buf - 1;
41
42 bs->wrap = bs_read;
43}
44
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
48// resets the buffer.
49
50static void bs_read (Bitstream *bs)
51{
52 if (bs->file && bs->file_bytes) {
53 ulong bytes_read, bytes_to_read = bs->end - bs->buf;
54
55 if (bytes_to_read > bs->file_bytes)
56 bytes_to_read = bs->file_bytes;
57
58 bytes_read = bs->file (bs->buf, bytes_to_read);
59
60 if (bytes_read) {
61 bs->end = bs->buf + bytes_read;
62 bs->file_bytes -= bytes_read;
63 }
64 else {
65 memset (bs->buf, -1, bs->end - bs->buf);
66 bs->error = 1;
67 }
68 }
69 else
70 bs->error = 1;
71
72 if (bs->error)
73 memset (bs->buf, -1, bs->end - bs->buf);
74
75 bs->ptr = bs->buf;
76}
77
78/////////////////////// Endian Correction Routines ////////////////////////////
79
80void little_endian_to_native (void *data, char *format)
81{
82 uchar *cp = (uchar *) data;
83 long temp;
84
85 while (*format) {
86 switch (*format) {
87 case 'L':
88 temp = cp [0] + ((long) cp [1] << 8) + ((long) cp [2] << 16) + ((long) cp [3] << 24);
89 * (long *) cp = temp;
90 cp += 4;
91 break;
92
93 case 'S':
94 temp = cp [0] + (cp [1] << 8);
95 * (short *) cp = (short) temp;
96 cp += 2;
97 break;
98
99 default:
100 if (isdigit (*format))
101 cp += *format - '0';
102
103 break;
104 }
105
106 format++;
107 }
108}
109
110void native_to_little_endian (void *data, char *format)
111{
112 uchar *cp = (uchar *) data;
113 long temp;
114
115 while (*format) {
116 switch (*format) {
117 case 'L':
118 temp = * (long *) cp;
119 *cp++ = (uchar) temp;
120 *cp++ = (uchar) (temp >> 8);
121 *cp++ = (uchar) (temp >> 16);
122 *cp++ = (uchar) (temp >> 24);
123 break;
124
125 case 'S':
126 temp = * (short *) cp;
127 *cp++ = (uchar) temp;
128 *cp++ = (uchar) (temp >> 8);
129 break;
130
131 default:
132 if (isdigit (*format))
133 cp += *format - '0';
134
135 break;
136 }
137
138 format++;
139 }
140}