-
Notifications
You must be signed in to change notification settings - Fork 4
/
misc.c
288 lines (254 loc) · 5.98 KB
/
misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Softcam plugin to VDR (C++)
*
* This code is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* Or, point your browser to http://www.gnu.org/copyleft/gpl.html
*/
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <vdr/tools.h>
#include <vdr/thread.h>
#include "misc.h"
// -----------------------------------------------------------------------------
const char *HexStr(char *str, const unsigned char *mem, int len)
{
for(int i=0 ; i<len ; i++) sprintf(&str[i*2],"%02X",mem[i]);
return str;
}
static int HexDigit(int asc)
{
if(asc>='A') return toupper(asc)-'A'+10;
return asc-'0';
}
int GetChar(const char * &line, int *store, int count)
{
line=skipspace(line);
const char *sline=line;
*store=0;
while(count) {
if(line[0]==0 || !isalnum(line[0])) break;
*store<<=8; *store+=line[0];
line++; count--;
}
if(!count && (!*line || isspace(*line) || *line==';')) return sline-line;
*store=0;
line=sline;
return 0;
}
static int GetHexBase(const char * &line, unsigned char *store, int count, bool fixedLen, bool asc)
{
line=skipspace(line);
const char *sline=line;
unsigned char *sstore=store;
int scount=count;
while(count) {
if(line[0]==0 || line[1]==0 || !isxdigit(line[0]) || !isxdigit(line[1])) break;
if(asc) {
*store++=line[0]; *store++=line[1];
}
else {
int d1=HexDigit(line[0]);
int d0=HexDigit(line[1]);
*store++=(d1<<4) + d0;
}
line+=2; count--;
}
if(asc) *store=0; // make sure strings are NULL terminated
if((!count || (!fixedLen && count!=scount)) &&
(!*line || isspace(*line) || *line==';' || *line==']' || *line=='/') ) return scount-count;
memset(sstore,0,scount);
line=sline;
return 0;
}
int GetHex(const char * &line, unsigned char *store, int count, bool fixedLen)
{
return GetHexBase(line,store,count,fixedLen,false);
}
int GetHexAsc(const char * &line, unsigned char *store, int count)
{
return GetHexBase(line,store,count/2,true,true)*2;
}
unsigned long long Bin2LongLong(unsigned char *mem, int len)
{
unsigned long long k=0;
for(int i=0 ; i<len ; i++) { k<<=8; k+=mem[i]; }
return k;
}
bool CheckNull(const unsigned char *data, int len)
{
while(--len>=0) if(data[len]) return false;
return true;
}
bool CheckFF(const unsigned char *data, int len)
{
while(--len>=0) if(data[len]!=0xFF) return false;
return true;
}
void SetSctLen(unsigned char *data, int len)
{
data[1]=(len>>8) | 0x70;
data[2]=len & 0xFF;
}
unsigned char XorSum(const unsigned char *mem, int len)
{
unsigned char cs=0;
while(len>0) { cs ^= *mem++; len--; }
return cs;
}
char *bprintf(const char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
char *str=0;
if(vasprintf(&str,fmt,ap)<0);
va_end(ap);
return str;
}
// crc stuff taken from linux-2.6.0/lib/crc32.c
/*
* There are multiple 16-bit CRC polynomials in common use, but this is
* *the* standard CRC-32 polynomial, first popularized by Ethernet.
* x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0
*/
#define CRCPOLY_LE 0xedb88320
unsigned int crc32_le(unsigned int crc, unsigned char const *p, int len)
{
crc^=0xffffffff; // zlib mod
while(len--) {
crc^=*p++;
for(int i=0; i<8; i++)
crc=(crc&1) ? (crc>>1)^CRCPOLY_LE : (crc>>1);
}
crc^=0xffffffff; // zlib mod
return crc;
}
// -- cLineBuff -----------------------------------------------------------------
cLineBuff::cLineBuff(int blocksize)
{
blockSize=blocksize;
work=0;
blen=wlen=0;
}
cLineBuff::~cLineBuff()
{
free(work);
}
void cLineBuff::Flush(void)
{
blen=0;
}
char *cLineBuff::Grab(void)
{
char *w=work;
work=0; wlen=0;
Flush();
return w;
}
bool cLineBuff::Check(int num)
{
if(wlen-blen<num) {
if(num<blockSize) num=blockSize;
char *w=(char *)realloc(work,wlen+num);
if(w) {
wlen+=num;
work=w;
}
}
return work!=0;
}
void cLineBuff::Printf(const char *fmt, ...)
{
int q=120;
while(Check(q)) {
int s=wlen-blen;
va_list ap;
va_start(ap,fmt);
q=vsnprintf(work+blen,s,fmt,ap);
va_end(ap);
if(q<s) {
if(q>0) blen+=q;
break;
}
// truncated
q+=100;
}
}
void cLineBuff::Strcat(const char *str)
{
if(str) Printf("%s",str);
}
void cLineBuff::Back(int n)
{
if(n>blen) blen=0; else blen-=n;
if(work) work[blen]=0;
}
// -- cSimpleListBase --------------------------------------------------------------
cSimpleListBase::cSimpleListBase(void)
{
first=last=0; count=0;
}
cSimpleListBase::~cSimpleListBase()
{
Clear();
}
void cSimpleListBase::Add(cSimpleItem *Item, cSimpleItem *After)
{
if(After) {
Item->next=After->next;
After->next=Item;
}
else {
Item->next=0;
if(last) last->next=Item;
else first=Item;
}
if(!Item->next) last=Item;
count++;
}
void cSimpleListBase::Ins(cSimpleItem *Item)
{
Item->next=first;
first=Item;
if(!Item->next) last=Item;
count++;
}
void cSimpleListBase::Del(cSimpleItem *Item, bool Del)
{
if(first==Item) {
first=Item->next;
if(!first) last=0;
count--;
}
else {
cSimpleItem *item=first;
while(item) {
if(item->next==Item) {
item->next=Item->next;
if(!item->next) last=item;
count--;
break;
}
item=item->next;
}
}
if(Del) delete Item;
}
void cSimpleListBase::Clear(void)
{
while(first) Del(first);
first=last=0; count=0;
}