TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
bitstream.c
Go to the documentation of this file.
1 /*
2  * qrencode - QR Code encoder
3  *
4  * Binary sequence class.
5  * Copyright (C) 2006-2017 Kentaro Fukuchi <kentaro@fukuchi.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "bitstream.h"
28 
29 #define DEFAULT_BUFSIZE (128)
30 
32 {
33  BitStream *bstream;
34 
35  bstream = (BitStream *)malloc(sizeof(BitStream));
36  if(bstream == NULL) return NULL;
37 
38  bstream->length = 0;
39  bstream->data = (unsigned char *)malloc(DEFAULT_BUFSIZE);
40  if(bstream->data == NULL) {
41  free(bstream);
42  return NULL;
43  }
44  bstream->datasize = DEFAULT_BUFSIZE;
45 
46  return bstream;
47 }
48 
49 #ifdef WITH_TESTS
50 BitStream *BitStream_newWithBits(size_t size, unsigned char *bits)
51 {
52  BitStream *bstream;
53 
54  if(size == 0) return BitStream_new();
55 
56  bstream = (BitStream *)malloc(sizeof(BitStream));
57  if(bstream == NULL) return NULL;
58 
59  bstream->data = (unsigned char *)malloc(size);
60  if(bstream->data == NULL) {
61  free(bstream);
62  return NULL;
63  }
64 
65  bstream->length = size;
66  bstream->datasize = size;
67  memcpy(bstream->data, bits, size);
68 
69  return bstream;
70 }
71 #endif
72 
73 static int BitStream_expand(BitStream *bstream)
74 {
75  unsigned char *data;
76 
77  data = (unsigned char *)realloc(bstream->data, bstream->datasize * 2);
78  if(data == NULL) {
79  return -1;
80  }
81 
82  bstream->data = data;
83  bstream->datasize *= 2;
84 
85  return 0;
86 }
87 
88 static void BitStream_writeNum(unsigned char *dest, size_t bits, unsigned int num)
89 {
90  unsigned int mask;
91  size_t i;
92  unsigned char *p;
93 
94  p = dest;
95  mask = 1U << (bits - 1);
96  for(i = 0; i < bits; i++) {
97  if(num & mask) {
98  *p = 1;
99  } else {
100  *p = 0;
101  }
102  p++;
103  mask = mask >> 1;
104  }
105 }
106 
107 static void BitStream_writeBytes(unsigned char *dest, size_t size, unsigned char *data)
108 {
109  unsigned char mask;
110  size_t i, j;
111  unsigned char *p;
112 
113  p = dest;
114  for(i = 0; i < size; i++) {
115  mask = 0x80;
116  for(j = 0; j < 8; j++) {
117  if(data[i] & mask) {
118  *p = 1;
119  } else {
120  *p = 0;
121  }
122  p++;
123  mask = mask >> 1;
124  }
125  }
126 }
127 
129 {
130  int ret;
131 
132  if(arg == NULL) {
133  return -1;
134  }
135  if(arg->length == 0) {
136  return 0;
137  }
138 
139  while(bstream->length + arg->length > bstream->datasize) {
140  ret = BitStream_expand(bstream);
141  if(ret < 0) return ret;
142  }
143 
144  memcpy(bstream->data + bstream->length, arg->data, arg->length);
145  bstream->length += arg->length;
146 
147  return 0;
148 }
149 
150 int BitStream_appendNum(BitStream *bstream, size_t bits, unsigned int num)
151 {
152  int ret;
153 
154  if(bits == 0) return 0;
155 
156  while(bstream->datasize - bstream->length < bits) {
157  ret = BitStream_expand(bstream);
158  if(ret < 0) return ret;
159  }
160  BitStream_writeNum(bstream->data + bstream->length, bits, num);
161  bstream->length += bits;
162 
163  return 0;
164 }
165 
166 int BitStream_appendBytes(BitStream *bstream, size_t size, unsigned char *data)
167 {
168  int ret;
169 
170  if(size == 0) return 0;
171 
172  while(bstream->datasize - bstream->length < size * 8) {
173  ret = BitStream_expand(bstream);
174  if(ret < 0) return ret;
175  }
176  BitStream_writeBytes(bstream->data + bstream->length, size, data);
177  bstream->length += size * 8;
178 
179  return 0;
180 }
181 
182 unsigned char *BitStream_toByte(BitStream *bstream)
183 {
184  size_t i, j, size, bytes, oddbits;
185  unsigned char *data, v;
186  unsigned char *p;
187 
188  size = BitStream_size(bstream);
189  if(size == 0) {
190  return NULL;
191  }
192  data = (unsigned char *)malloc((size + 7) / 8);
193  if(data == NULL) {
194  return NULL;
195  }
196 
197  bytes = size / 8;
198 
199  p = bstream->data;
200  for(i = 0; i < bytes; i++) {
201  v = 0;
202  for(j = 0; j < 8; j++) {
203  v = (unsigned char)(v << 1);
204  v |= *p;
205  p++;
206  }
207  data[i] = v;
208  }
209  oddbits = size & 7;
210  if(oddbits > 0) {
211  v = 0;
212  for(j = 0; j < oddbits; j++) {
213  v = (unsigned char)(v << 1);
214  v |= *p;
215  p++;
216  }
217  data[bytes] = (unsigned char)(v << (8 - oddbits));
218  }
219 
220  return data;
221 }
222 
223 void BitStream_free(BitStream *bstream)
224 {
225  if(bstream != NULL) {
226  free(bstream->data);
227  free(bstream);
228  }
229 }
size_t datasize
Definition: bitstream.h:27
unsigned char * data
Definition: bitstream.h:28
static int BitStream_expand(BitStream *bstream)
Definition: bitstream.c:73
voidpf void uLong size
Definition: ioapi.h:136
int BitStream_append(BitStream *bstream, BitStream *arg)
Definition: bitstream.c:128
size_t length
Definition: bitstream.h:26
int BitStream_appendBytes(BitStream *bstream, size_t size, unsigned char *data)
Definition: bitstream.c:166
unsigned char * BitStream_toByte(BitStream *bstream)
Definition: bitstream.c:182
void BitStream_free(BitStream *bstream)
Definition: bitstream.c:223
static void BitStream_writeNum(unsigned char *dest, size_t bits, unsigned int num)
Definition: bitstream.c:88
#define BitStream_size(__bstream__)
Definition: bitstream.h:38
#define DEFAULT_BUFSIZE
Definition: bitstream.c:29
void free(voidpf ptr)
BitStream * BitStream_new(void)
Definition: bitstream.c:31
static void BitStream_writeBytes(unsigned char *dest, size_t size, unsigned char *data)
Definition: bitstream.c:107
voidp malloc(uInt size)
int BitStream_appendNum(BitStream *bstream, size_t bits, unsigned int num)
Definition: bitstream.c:150