clib/dvb_ts_lib/ts_bits.c

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 121 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« clib/dvb_ts_lib/tables/parse_si_tot.c ↑ Summary clib/dvb_ts_lib/ts_cut.c »
1 /*
2 * ts_bits.c
3 *
4 * Created on: 2 Apr 2011
5 * Author: sdprice1
6 */
7
8 // VERSION = 1.00
9
10 /*=============================================================================================*/
11 // USES
12 /*=============================================================================================*/
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <ctype.h>
20 #include <fcntl.h>
21 #include <inttypes.h>
22
23 #include "ts_bits.h"
24
25 /*=============================================================================================*/
26 // CONSTANTS
27 /*=============================================================================================*/
28
29 /*=============================================================================================*/
30 // MACROS
31 /*=============================================================================================*/
32
33 /*=============================================================================================*/
34 // FUNCTIONS
35 /*=============================================================================================*/
36
37 /* ----------------------------------------------------------------------- */
38 0 void bits_free(struct TS_bits **bits)
39 {
40 0 struct TS_bits *bp = *bits ;
41
42 0 ▶ if (bp)
Branch: 1 branch
BranchTrueFalseTotal
if (bp) 0 0 0
43 {
44 0 free(bp) ;
45 }
46 0 *bits = NULL ;
47 0 }
48
49 /* ----------------------------------------------------------------------- */
50 0 struct TS_bits *bits_new(uint8_t *src, unsigned src_len)
51 {
52 struct TS_bits *bp ;
53
54 // create struct
55 0 bp = (struct TS_bits *)malloc(sizeof(struct TS_bits)) ;
56 0 memset(bp, 0, sizeof(*bp));
57
58 // init
59 0 bp->buff_ptr = src ;
60 0 bp->buff_len = src_len ;
61 0 bp->start_bit = 0 ;
62
63 0 return bp ;
64 }
65
66
67
68
69 /* ----------------------------------------------------------------------- */
70 0 unsigned bits_get(struct TS_bits *bits, unsigned len)
71 {
72 0 unsigned int result = 0;
73 0 unsigned start_len = len + bits->start_bit ;
74 unsigned mask ;
75 int left_shift ;
76 0 unsigned byte = 0 ;
77
78 0 ▶ if (len==0)
Branch: 1 branch
BranchTrueFalseTotal
if (len==0) 0 0 0
79 0 return 0 ;
80
81 0 ▶if (len > 32)
Branch: 1 branch
BranchTrueFalseTotal
if (len > 32) 0 0 0
82 {
83 0 fprintf(stderr, "BUGGER! Request for > 32 bits!\n") ;
84 0 exit(1) ;
85 }
86
87 0 ▶if (bits->buff_len <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (bits->buff_len <= 0) 0 0 0
88 {
89 0 fprintf(stderr, "BUGGER! Gone past the end of the buffer!\n") ;
90 0 exit(1) ;
91 }
92
93 0 ▶ if (len == 32)
Branch: 1 branch
BranchTrueFalseTotal
if (len == 32) 0 0 0
94 {
95 0 mask = 0xffffffff ;
96 }
97 else
98 {
99 0 mask = (1 << len) -1 ;
100 }
101
102 // We want to shift the "start" bit to the MS bit of the final length
103 //
104 // 0 s 7 - NOTE: start bit is numbered from MS = 0 to LS = 7
105 // [ 0 ]
106 //
107 //
108 0 left_shift = (len-1) - (7-bits->start_bit) ;
109
110 0 ▶ if (left_shift >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (left_shift >= 0) 0 0 0
111 {
112
113 0 ▶ while (left_shift >= 0)
Branch: 1 branch
BranchTrueFalseTotal
while (left_shift >= 0) 0 0 0
114 {
115 0 result |= bits->buff_ptr[byte++] << left_shift ;
116 0 left_shift -= 8 ;
117 }
118 }
119 0 ▶ if ((left_shift < 0) && (left_shift > -8))
Branch: 2 branches
BranchTrueFalseTotal
if ((left_shift < 0) && (left_shift > -8)) 0 0 0
if ((left_shift < 0) && (left_shift > -8)) 0 0 0
120 {
121 0 result |= bits->buff_ptr[byte] >> -left_shift ;
122 }
123
124 0 result &= mask ;
125
126 // update buffer
127 0 bits->start_bit = start_len % 8 ;
128 0 bits->buff_len -= start_len / 8 ;
129 0 bits->buff_ptr += start_len / 8 ;
130
131 0 return result;
132 }
133
134 /* ----------------------------------------------------------------------- */
135 0 void bits_skip(struct TS_bits *bits, unsigned len)
136 {
137 unsigned int result ;
138
139 0 ▶ while (len > 32)
Branch: 1 branch
BranchTrueFalseTotal
while (len > 32) 0 0 0
140 {
141 // chop into 32 bit chunkc
142 0 result= bits_get(bits, 32) ;
143 0 len -= 32 ;
144 }
145 0 result = bits_get(bits, len) ;
146 0 }
147
148 /* ----------------------------------------------------------------------- */
149 // Calculate the result of adding the offset to the current buffer length and return
150 // that result (clamping value to 0 min)
151 0 int bits_len_calc(struct TS_bits *bits, int offset)
152 {
153 0 int len = bits->buff_len + offset ;
154 0 ▶ if (len < 0) len = 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 0) len = 0 ; 0 0 0
155 0 return len ;
156 }
157
158
159 /* ----------------------------------------------------------------------- */
160 // Read the 16 bit MJD & 24 bit START then convert to time_t format
161 0 struct tm bits_get_mjd_time(struct TS_bits *bits)
162 {
163 struct tm tm;
164 int y2,m2,k;
165 int mjd, start ;
166
167 0 mjd = bits_get(bits, 16) ;
168 0 start = bits_get(bits, 24) ;
169
170 0 memset(&tm,0,sizeof(tm));
171
172 /* taken as-is from EN-300-486 */
173 0 y2 = (int)((mjd - 15078.2) / 365.25);
174 0 m2 = (int)((mjd - 14956.1 - (int)(y2 * 365.25)) / 30.6001);
175 0 ▶ k = (m2 == 14 || m2 == 15) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
k = (m2 == 14 || m2 == 15) ? 1 : 0; 0 0 0
k = (m2 == 14 || m2 == 15) ? 1 : 0; 0 0 0
176 0 tm.tm_mday = mjd - 14956 - (int)(y2 * 365.25) - (int)(m2 * 30.6001);
177 0 tm.tm_year = y2 + k + 1900;
178 0 tm.tm_mon = m2 - 1 - k * 12;
179
180 /* time is bcd ... */
181 0 tm.tm_hour = ((start >> 20) & 0xf) * 10;
182 0 tm.tm_hour += ((start >> 16) & 0xf);
183 0 tm.tm_min = ((start >> 12) & 0xf) * 10;
184 0 tm.tm_min += ((start >> 8) & 0xf);
185 0 tm.tm_sec = ((start >> 4) & 0xf) * 10;
186 0 tm.tm_sec += ((start) & 0xf);
187
188 0 return tm ;
189 }
190
191
192 #if 0
193 fprintf(stderr,"mjd %d, time 0x%06x => %04d-%02d-%02d %02d:%02d:%02d",
194 mjd, start,
195 tm.tm_year, tm.tm_mon, tm.tm_mday,
196 tm.tm_hour, tm.tm_min, tm.tm_sec);
197
198 {
199 char buf[16];
200
201 strftime(buf,sizeof(buf),"%H:%M:%S",&tm);
202 fprintf(stderr," => %s",buf);
203
204 gmtime_r(&t,&tm);
205 strftime(buf,sizeof(buf),"%H:%M:%S GMT",&tm);
206 fprintf(stderr," => %s",buf);
207
208 localtime_r(&t,&tm);
209 strftime(buf,sizeof(buf),"%H:%M:%S %z",&tm);
210 fprintf(stderr," => %s\n",buf);
211 }
212 #endif
213
214 /* ----------------------------------------------------------------------- */
215 0 void bits_dump_indent(unsigned level)
216 {
217 unsigned i ;
218
219 0 ▶ for (i=0; i < level; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i < level; i++) 0 0 0
220 {
221 0 printf(" ") ;
222 }
223 0 }
224
225 /* ----------------------------------------------------------------------- */
226 0 void bits_dump(char *name, unsigned *buff, unsigned length, unsigned level)
227 {
228 unsigned byte ;
229
230 0 bits_dump_indent(level) ; printf("%s :\n", name) ;
231 0 bits_dump_indent(level+1) ;
232 0 ▶ for (byte=0; byte < length; ++byte)
Branch: 1 branch
BranchTrueFalseTotal
for (byte=0; byte < length; ++byte) 0 0 0
233 {
234 0 ▶ if (byte % 32 == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (byte % 32 == 0) 0 0 0
235 {
236 0 printf("%04x: ", byte) ;
237 }
238 0 printf("%02x ", buff[byte]) ;
239 0 ▶ if (byte % 8 == 7)
Branch: 1 branch
BranchTrueFalseTotal
if (byte % 8 == 7) 0 0 0
240 {
241 0 printf(" - ") ;
242 }
243 0 ▶ if (byte % 32 == 31)
Branch: 1 branch
BranchTrueFalseTotal
if (byte % 32 == 31) 0 0 0
244 {
245 0 printf("\n") ;
246 0 bits_dump_indent(level+1) ;
247 }
248 }
249 0 printf("\n") ;
250 0 }
251
« clib/dvb_ts_lib/tables/parse_si_tot.c ↑ Summary clib/dvb_ts_lib/ts_cut.c »