source: trunk/oscam-conf-mk.c@ 8464

Last change on this file since 8464 was 8464, checked in by gf, 9 years ago

cfg: Remove camd35 vars from struct s_config when the module is disabled.

File size: 21.4 KB
Line 
1#include "globals.h"
2#include "oscam-conf-mk.h"
3#include "oscam-net.h"
4#include "oscam-string.h"
5
6/*
7 * Creates a string ready to write as a token into config or WebIf for CAIDs. You must free the returned value through free_mk_t().
8 */
9char *mk_t_caidtab(CAIDTAB *ctab) {
10 int32_t i = 0, needed = 1, pos = 0;
11 while(ctab->caid[i]) {
12 if(ctab->mask[i]) needed += 10;
13 else needed += 5;
14 if(ctab->cmap[i]) needed += 5;
15 ++i;
16 }
17 char *value;
18 if (needed == 1 || !cs_malloc(&value, needed)) return "";
19 char *saveptr = value;
20 i = 0;
21 while(ctab->caid[i]) {
22 if (ctab->caid[i] < 0x0100) { //for "ignore provider for" option, caid-shortcut, just first 2 bytes:
23 if(i == 0) {
24 snprintf(value + pos, needed-(value-saveptr), "%02X", ctab->caid[i]);
25 pos += 2;
26 } else {
27 snprintf(value + pos, needed-(value-saveptr), ",%02X", ctab->caid[i]);
28 pos += 3;
29 }
30 } else {
31 if(i == 0) {
32 snprintf(value + pos, needed-(value-saveptr), "%04X", ctab->caid[i]);
33 pos += 4;
34 } else {
35 snprintf(value + pos, needed-(value-saveptr), ",%04X", ctab->caid[i]);
36 pos += 5;
37 }
38 }
39
40 if((ctab->mask[i]) && (ctab->mask[i] != 0xFFFF)) {
41 snprintf(value + pos, needed-(value-saveptr), "&%04X", ctab->mask[i]);
42 pos += 5;
43 }
44 if(ctab->cmap[i]) {
45 snprintf(value + pos, needed-(value-saveptr), ":%04X", ctab->cmap[i]);
46 pos += 5;
47 }
48 ++i;
49 }
50 value[pos] = '\0';
51 return value;
52}
53
54/*
55 * Creates a string ready to write as a token into config or WebIf for TunTabs. You must free the returned value through free_mk_t().
56 */
57char *mk_t_tuntab(TUNTAB *ttab) {
58 int32_t i, needed = 1, pos = 0;
59 for (i=0; i<ttab->n; i++) {
60 if(ttab->bt_srvid[i]) needed += 10;
61 else needed += 5;
62 if(ttab->bt_caidto[i]) needed += 5;
63 }
64 char *value;
65 if (needed == 1 || !cs_malloc(&value, needed)) return "";
66 char *saveptr = value;
67 for (i=0; i<ttab->n; i++) {
68 if(i == 0) {
69 snprintf(value + pos, needed-(value-saveptr), "%04X", ttab->bt_caidfrom[i]);
70 pos += 4;
71 } else {
72 snprintf(value + pos, needed-(value-saveptr), ",%04X", ttab->bt_caidfrom[i]);
73 pos += 5;
74 }
75 if(ttab->bt_srvid[i]) {
76 snprintf(value + pos, needed-(value-saveptr), ".%04X", ttab->bt_srvid[i]);
77 pos += 5;
78 }
79 if(ttab->bt_caidto[i]) {
80 snprintf(value + pos, needed-(value-saveptr), ":%04X", ttab->bt_caidto[i]);
81 pos += 5;
82 }
83 }
84 value[pos] = '\0';
85 return value;
86}
87
88/*
89 * Creates a string ready to write as a token into config or WebIf for groups. You must free the returned value through free_mk_t().
90 */
91char *mk_t_group(uint64_t grp) {
92 int32_t i = 0, needed = 1, pos = 0, dot = 0;
93
94 for(i = 0; i < 64; i++) {
95 if (grp&((uint64_t)1<<i)) {
96 needed += 2;
97 if(i > 9) needed += 1;
98 }
99 }
100 char *value;
101 if (needed == 1 || !cs_malloc(&value, needed)) return "";
102 char * saveptr = value;
103 for(i = 0; i < 64; i++) {
104 if (grp&((uint64_t)1<<i)) {
105 if (dot == 0) {
106 snprintf(value + pos, needed-(value-saveptr), "%d", i+1);
107 if (i > 8)pos += 2;
108 else pos += 1;
109 dot = 1;
110 } else {
111 snprintf(value + pos, needed-(value-saveptr), ",%d", i+1);
112 if (i > 8)pos += 3;
113 else pos += 2;
114 }
115 }
116 }
117 value[pos] = '\0';
118 return value;
119}
120
121/*
122 * Creates a string ready to write as a token into config or WebIf for FTabs (CHID, Ident). You must free the returned value through free_mk_t().
123 */
124char *mk_t_ftab(FTAB *ftab) {
125 int32_t i = 0, j = 0, needed = 1, pos = 0;
126
127 if (ftab->nfilts != 0) {
128 needed = ftab->nfilts * 5;
129 for (i = 0; i < ftab->nfilts; ++i)
130 needed += ftab->filts[i].nprids * 7;
131 }
132
133 char *value;
134 if (needed == 1 || !cs_malloc(&value, needed)) return "";
135 char *saveptr = value;
136 char *dot="";
137 for (i = 0; i < ftab->nfilts; ++i) {
138 snprintf(value + pos, needed-(value-saveptr), "%s%04X", dot, ftab->filts[i].caid);
139 pos += 4;
140 if (i > 0) pos += 1;
141 dot=":";
142 for (j = 0; j < ftab->filts[i].nprids; ++j) {
143 snprintf(value + pos, needed-(value-saveptr), "%s%06X", dot, ftab->filts[i].prids[j]);
144 pos += 7;
145 dot=",";
146 }
147 dot=";";
148 }
149
150 value[pos] = '\0';
151 return value;
152}
153
154/*
155 * Creates a string ready to write as a token into config or WebIf for the camd35 tcp ports. You must free the returned value through free_mk_t().
156 */
157char *mk_t_camd35tcp_port(void) {
158#if defined(MODULE_CAMD35) || defined(MODULE_CAMD35_TCP)
159 int32_t i, j, pos = 0, needed = 1;
160
161 /* Precheck to determine how long the resulting string will maximally be (might be a little bit smaller but that shouldn't hurt) */
162 for(i = 0; i < cfg.c35_tcp_ptab.nports; ++i) {
163 /* Port is maximally 5 chars long, plus the @caid, plus the ";" between ports */
164 needed += 11;
165 if (cfg.c35_tcp_ptab.ports[i].ncd && cfg.c35_tcp_ptab.ports[i].ncd->ncd_ftab.filts[0].nprids > 1) {
166 needed += cfg.c35_tcp_ptab.ports[i].ncd->ncd_ftab.filts[0].nprids * 7;
167 }
168 }
169 char *value;
170 if (needed == 1 || !cs_malloc(&value, needed)) return "";
171 char *saveptr = value;
172 char *dot1 = "", *dot2;
173 for(i = 0; i < cfg.c35_tcp_ptab.nports; ++i) {
174
175 if (cfg.c35_tcp_ptab.ports[i].ncd && cfg.c35_tcp_ptab.ports[i].ncd->ncd_ftab.filts[0].caid) {
176 pos += snprintf(value + pos, needed-(value-saveptr), "%s%d@%04X", dot1,
177 cfg.c35_tcp_ptab.ports[i].s_port,
178 cfg.c35_tcp_ptab.ports[i].ncd->ncd_ftab.filts[0].caid);
179
180 if (cfg.c35_tcp_ptab.ports[i].ncd->ncd_ftab.filts[0].nprids > 1) {
181 dot2 = ":";
182 for (j = 0; j < cfg.c35_tcp_ptab.ports[i].ncd->ncd_ftab.filts[0].nprids; ++j) {
183 pos += snprintf(value + pos, needed-(value-saveptr), "%s%X", dot2, cfg.c35_tcp_ptab.ports[i].ncd->ncd_ftab.filts[0].prids[j]);
184 dot2 = ",";
185 }
186 }
187 dot1=";";
188 } else {
189 pos += snprintf(value + pos, needed-(value-saveptr), "%d", cfg.c35_tcp_ptab.ports[i].s_port);
190 }
191 }
192 return value;
193#else
194 return NULL;
195#endif
196}
197
198#ifdef MODULE_CCCAM
199/*
200 * Creates a string ready to write as a token into config or WebIf for the cccam tcp ports. You must free the returned value through free_mk_t().
201 */
202char *mk_t_cccam_port(void) {
203 int32_t i, pos = 0, needed = CS_MAXPORTS*6+8;
204
205 char *value;
206 if (!cs_malloc(&value, needed)) return "";
207 char *dot = "";
208 for(i = 0; i < CS_MAXPORTS; i++) {
209 if (!cfg.cc_port[i]) break;
210
211 pos += snprintf(value + pos, needed-pos, "%s%d", dot, cfg.cc_port[i]);
212 dot=",";
213 }
214 return value;
215}
216#endif
217
218
219/*
220 * Creates a string ready to write as a token into config or WebIf for AESKeys. You must free the returned value through free_mk_t().
221 */
222char *mk_t_aeskeys(struct s_reader *rdr) {
223 AES_ENTRY *current = rdr->aes_list;
224 int32_t i, pos = 0, needed = 1, prevKeyid = 0, prevCaid = 0;
225 uint32_t prevIdent = 0;
226
227 /* Precheck for the approximate size that we will need; it's a bit overestimated but we correct that at the end of the function */
228 while(current) {
229 /* The caid, ident, "@" and the trailing ";" need to be output when they are changing */
230 if(prevCaid != current->caid || prevIdent != current->ident) needed += 12 + (current->keyid * 2);
231 /* "0" keys are not saved so we need to check for gaps */
232 else if(prevKeyid != current->keyid + 1) needed += (current->keyid - prevKeyid - 1) * 2;
233 /* The 32 byte key plus either the (heading) ":" or "," */
234 needed += 33;
235 prevCaid = current->caid;
236 prevIdent = current->ident;
237 prevKeyid = current->keyid;
238 current = current->next;
239 }
240
241 /* Set everything back and now create the string */
242 current = rdr->aes_list;
243 prevCaid = 0;
244 prevIdent = 0;
245 prevKeyid = 0;
246 char tmp[needed];
247 char dot;
248 if(needed == 1) tmp[0] = '\0';
249 char tmpkey[33];
250 while(current) {
251 /* A change in the ident or caid means that we need to output caid and ident */
252 if(prevCaid != current->caid || prevIdent != current->ident) {
253 if(pos > 0) {
254 tmp[pos] = ';';
255 ++pos;
256 }
257 pos += snprintf(tmp+pos, sizeof(tmp)-pos, "%04X@%06X", current->caid, current->ident);
258 prevKeyid = -1;
259 dot = ':';
260 } else dot = ',';
261 /* "0" keys are not saved so we need to check for gaps and output them! */
262 for (i = prevKeyid + 1; i < current->keyid; ++i) {
263 pos += snprintf(tmp+pos, sizeof(tmp)-pos, "%c0", dot);
264 dot = ',';
265 }
266 tmp[pos] = dot;
267 ++pos;
268 for (i = 0; i < 16; ++i) snprintf(tmpkey + (i*2), sizeof(tmpkey) - (i*2), "%02X", current->plainkey[i]);
269 /* A key consisting of only FFs has a special meaning (just return what the card outputted) and can be specified more compact */
270 if(strcmp(tmpkey, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") == 0) pos += snprintf(tmp+pos, sizeof(tmp)-pos, "FF");
271 else pos += snprintf(tmp+pos, sizeof(tmp)-pos, "%s", tmpkey);
272 prevCaid = current->caid;
273 prevIdent = current->ident;
274 prevKeyid = current->keyid;
275 current = current->next;
276 }
277
278 /* copy to result array of correct size */
279 char *value;
280 if (pos == 0 || !cs_malloc(&value, pos + 1)) return "";
281 memcpy(value, tmp, pos + 1);
282 return(value);
283}
284
285/*
286 * Creates a string ready to write as a token into config or WebIf for the Newcamd Port. You must free the returned value through free_mk_t().
287 */
288char *mk_t_newcamd_port(void) {
289 int32_t i, j, k, pos = 0, needed = 1;
290
291 /* Precheck to determine how long the resulting string will maximally be (might be a little bit smaller but that shouldn't hurt) */
292 for(i = 0; i < cfg.ncd_ptab.nports; ++i) {
293 /* Port is maximally 5 chars long, plus the @caid, plus the ";" between ports */
294 needed += 11;
295 if (cfg.ncd_ptab.ports[i].ncd) {
296 if(cfg.ncd_ptab.ports[i].ncd->ncd_key_is_set) needed += 30;
297 if (cfg.ncd_ptab.ports[i].ncd->ncd_ftab.filts[0].nprids > 0) {
298 needed += cfg.ncd_ptab.ports[i].ncd->ncd_ftab.filts[0].nprids * 7;
299 }
300 }
301 }
302 char *value;
303 if (needed == 1 || !cs_malloc(&value, needed)) return "";
304 char *dot1 = "", *dot2;
305
306 for(i = 0; i < cfg.ncd_ptab.nports; ++i) {
307 pos += snprintf(value + pos, needed-pos, "%s%d", dot1, cfg.ncd_ptab.ports[i].s_port);
308
309 // separate DES Key for this port
310 if (cfg.ncd_ptab.ports[i].ncd) {
311 if(cfg.ncd_ptab.ports[i].ncd->ncd_key_is_set) {
312 pos += snprintf(value + pos, needed-pos, "{");
313 for (k = 0; k < (int32_t)sizeof(cfg.ncd_ptab.ports[i].ncd->ncd_key[k]); k++)
314 pos += snprintf(value + pos, needed-pos, "%02X", cfg.ncd_ptab.ports[i].ncd->ncd_key[k]);
315 pos += snprintf(value + pos, needed-pos, "}");
316 }
317
318 pos += snprintf(value + pos, needed-pos, "@%04X", cfg.ncd_ptab.ports[i].ncd->ncd_ftab.filts[0].caid);
319
320 if (cfg.ncd_ptab.ports[i].ncd->ncd_ftab.filts[0].nprids > 0) {
321 dot2 = ":";
322 for (j = 0; j < cfg.ncd_ptab.ports[i].ncd->ncd_ftab.filts[0].nprids; ++j) {
323 pos += snprintf(value + pos, needed-pos, "%s%06X", dot2, (int)cfg.ncd_ptab.ports[i].ncd->ncd_ftab.filts[0].prids[j]);
324 dot2 = ",";
325 }
326 }
327 }
328 dot1=";";
329 }
330 return value;
331}
332
333/*
334 * Creates a string ready to write as a token into config or WebIf for au readers. You must free the returned value through free_mk_t().
335 */
336char *mk_t_aureader(struct s_auth *account) {
337 int32_t pos = 0;
338 char *dot = "";
339
340 char *value;
341 if (ll_count(account->aureader_list) == 0 || !cs_malloc(&value, 256)) return "";
342 value[0] = '\0';
343
344 struct s_reader *rdr;
345 LL_ITER itr = ll_iter_create(account->aureader_list);
346 while ((rdr = ll_iter_next(&itr))) {
347 pos += snprintf(value + pos, 256-pos, "%s%s", dot, rdr->label);
348 dot = ",";
349 }
350
351 return value;
352}
353
354/*
355 * Creates a string ready to write as a token into config or WebIf for blocknano and savenano. You must free the returned value through free_mk_t().
356 */
357char *mk_t_nano(uint16_t nano) {
358 int32_t i, pos = 0, needed = 0;
359
360 for (i=0; i<16; i++)
361 if ((1 << i) & nano)
362 needed++;
363
364 char *value;
365 if (nano == 0xFFFF) {
366 if (!cs_malloc(&value, 4)) return "";
367 snprintf(value, 4, "all");
368 } else {
369 if (needed == 0 || !cs_malloc(&value, needed * 3 + 1)) return "";
370 value[0] = '\0';
371 for (i=0; i<16; i++) {
372 if ((1 << i) & nano)
373 pos += snprintf(value + pos, (needed*3)+1-pos, "%s%02x", pos ? "," : "", (i+0x80));
374 }
375 }
376 return value;
377}
378
379/*
380 * Creates a string ready to write as a token into config or WebIf for the sidtab. You must free the returned value through free_mk_t().
381 */
382char *mk_t_service(SIDTABS *sidtabs) {
383 int32_t i, pos;
384 char *dot;
385 char *value;
386 struct s_sidtab *sidtab = cfg.sidtab;
387 if (!sidtab || (!sidtabs->ok && !sidtabs->no) || !cs_malloc(&value, 1024)) return "";
388 value[0] = '\0';
389
390 for (i=pos=0,dot=""; sidtab; sidtab=sidtab->next,i++) {
391 if (sidtabs->ok&((SIDTABBITS)1<<i)) {
392 pos += snprintf(value + pos, 1024 - pos, "%s%s", dot, sidtab->label);
393 dot = ",";
394 }
395 if (sidtabs->no&((SIDTABBITS)1<<i)) {
396 pos += snprintf(value + pos, 1024 - pos, "%s!%s", dot, sidtab->label);
397 dot = ",";
398 }
399 }
400 return value;
401}
402
403/*
404 * Creates a string ready to write as a token into config or WebIf for the logfile parameter. You must free the returned value through free_mk_t().
405 */
406char *mk_t_logfile(void) {
407 int32_t pos = 0, needed = 1;
408 char *value, *dot = "";
409
410 if(cfg.logtostdout == 1) needed += 7;
411 if(cfg.logtosyslog == 1) needed += 7;
412 if(cfg.logfile) needed += strlen(cfg.logfile);
413 if (needed == 1 || !cs_malloc(&value, needed)) return "";
414
415 if(cfg.logtostdout == 1) {
416 pos += snprintf(value + pos, needed - pos, "stdout");
417 dot = ";";
418 }
419 if(cfg.logtosyslog == 1) {
420 pos += snprintf(value + pos, needed - pos, "%ssyslog", dot);
421 dot = ";";
422 }
423 if(cfg.logfile) {
424 pos += snprintf(value + pos, needed - pos, "%s%s", dot, cfg.logfile);
425 }
426 return value;
427}
428
429/*
430 * Creates a string ready to write as a token into config or WebIf for the ecm whitelist. You must free the returned value through free_mk_t().
431 */
432char *mk_t_ecmwhitelist(struct s_ecmWhitelist *whitelist) {
433 int32_t needed = 1, pos = 0;
434 struct s_ecmWhitelist *cip;
435 struct s_ecmWhitelistIdent *cip2;
436 struct s_ecmWhitelistLen *cip3;
437 char *value, *dot = "", *dot2 = "";
438 for (cip = whitelist; cip; cip = cip->next) {
439 needed += 7;
440 for (cip2 = cip->idents; cip2; cip2 = cip2->next) {
441 needed +=7;
442 for (cip3 = cip2->lengths; cip3; cip3 = cip3->next) needed +=3;
443 }
444 }
445
446 char tmp[needed];
447
448 for (cip = whitelist; cip; cip = cip->next) {
449 for (cip2 = cip->idents; cip2; cip2 = cip2->next) {
450 if(cip2->lengths != NULL) {
451 if(cip->caid != 0) {
452 if(cip2->ident == 0)
453 pos += snprintf(tmp + pos, needed - pos, "%s%04X:", dot, cip->caid);
454 else
455 pos += snprintf(tmp + pos, needed - pos, "%s%04X@%06X:", dot, cip->caid, cip2->ident);
456 } else pos += snprintf(tmp + pos, needed - pos, "%s", dot);
457 }
458 dot2="";
459 for (cip3 = cip2->lengths; cip3; cip3 = cip3->next) {
460 pos += snprintf(tmp + pos, needed - pos, "%s%02X", dot2, cip3->len);
461 dot2=",";
462 }
463 dot=";";
464 }
465 }
466 if (pos == 0 || !cs_malloc(&value, pos + 1)) return "";
467 memcpy(value, tmp, pos + 1);
468 return value;
469}
470
471/*
472 * Creates a string ready to write as a token into config or WebIf for the ECM Headerwhitelist. You must free the returned value through free_mk_t().
473 */
474char *mk_t_ecmheaderwhitelist(struct s_ecmHeaderwhitelist *headerlist){
475 int32_t needed = 1, pos = 0;
476 struct s_ecmHeaderwhitelist *cip;
477 for (cip = headerlist; cip; cip = cip->next) needed += 51;
478 char *value, *dot = "";
479 char tmp[needed];
480 int16_t i;
481 int16_t ccache = 0;
482 uint32_t pcache = 0;
483 tmp[0] = '\0';
484 for (cip = headerlist; cip; cip = cip->next){
485 dot = "";
486 if (ccache == cip->caid && pcache == cip->provid){
487 if (pos)
488 pos -= 1;
489 if (strlen(tmp))
490 pos += snprintf(tmp + pos, needed - pos, ",");
491 } else {
492 if (cip->header != NULL && cip->caid != 0 && cip->provid == 0) {
493 pos += snprintf(tmp + pos, needed - pos, "%s%04X:", dot, cip->caid);
494 ccache = cip->caid;
495 pcache = 0;
496 }
497
498 if (cip->header != NULL && cip->caid != 0 && cip->provid != 0) {
499 pos += snprintf(tmp + pos, needed - pos, "%s%04X@%06X:", dot, cip->caid, cip->provid);
500 ccache = cip->caid;
501 pcache = cip->provid;
502 }
503 }
504 if(cip->header != NULL) {
505 for (i=0; i < cip->len/2; i++) {
506 pos += snprintf(tmp + pos, needed - pos, "%s%02X", dot, cip->header[i]);
507 if (i == cip->len/2-1) pos += snprintf(tmp + pos, needed - pos, ",");
508 ccache = cip->caid;
509 pcache = cip->provid;
510 }
511 }
512 if (pos)
513 pos -= 1;
514 pos += snprintf(tmp + pos, needed - pos, ";");
515 }
516 if (pos == 0 || !cs_malloc(&value, pos + 1)) return "";
517 memcpy(value, tmp, pos-1);
518 return value;
519}
520
521/*
522 * Creates a string ready to write as a token into config or WebIf for an iprange. You must free the returned value through free_mk_t().
523 */
524char *mk_t_iprange(struct s_ip *range) {
525 struct s_ip *cip;
526 char *value, *dot = "";
527 int32_t needed = 1, pos = 0;
528 for (cip = range; cip; cip = cip->next) needed += 32;
529
530 char tmp[needed];
531
532 for (cip = range; cip; cip = cip->next) {
533 pos += snprintf(tmp + pos, needed - pos, "%s%s", dot, cs_inet_ntoa(cip->ip[0]));
534 if (!IP_EQUAL(cip->ip[0], cip->ip[1])) pos += snprintf(tmp + pos, needed - pos, "-%s", cs_inet_ntoa(cip->ip[1]));
535 dot=",";
536 }
537 if (pos == 0 || !cs_malloc(&value, pos + 1)) return "";
538 memcpy(value, tmp, pos + 1);
539 return value;
540}
541
542/*
543 * Creates a string ready to write as a token into config or WebIf for the class attribute. You must free the returned value through free_mk_t().
544 */
545char *mk_t_cltab(CLASSTAB *clstab) {
546 char *value, *dot = "";
547 int32_t i, needed = 1, pos = 0;
548 for(i = 0; i < clstab->an; ++i) needed += 3;
549 for(i = 0; i < clstab->bn; ++i) needed += 4;
550
551 char tmp[needed];
552
553 for(i = 0; i < clstab->an; ++i) {
554 pos += snprintf(tmp + pos, needed - pos, "%s%02x", dot, (int32_t)clstab->aclass[i]);
555 dot=",";
556 }
557 for(i = 0; i < clstab->bn; ++i) {
558 pos += snprintf(tmp + pos, needed - pos, "%s!%02x", dot, (int32_t)clstab->bclass[i]);
559 dot=",";
560 }
561
562 if (pos == 0 || !cs_malloc(&value, pos + 1)) return "";
563 memcpy(value, tmp, pos + 1);
564 return value;
565}
566
567/*
568 * Creates a string ready to write as a token into config or WebIf. You must free the returned value through free_mk_t().
569 */
570char *mk_t_caidvaluetab(CAIDVALUETAB *tab)
571{
572 if (!tab->n) return "";
573 int32_t i, size = 2 + tab->n * (4 + 1 + 5 + 1); //caid + ":" + time + ","
574 char *buf;
575 if (!cs_malloc(&buf, size))
576 return "";
577 char *ptr = buf;
578
579 for (i = 0; i < tab->n; i++) {
580 if (tab->caid[i] < 0x0100) //Do not format 0D as 000D, its a shortcut for 0Dxx:
581 ptr += snprintf(ptr, size-(ptr-buf), "%s%02X:%d", i?",":"", tab->caid[i], tab->value[i]);
582 else
583 ptr += snprintf(ptr, size-(ptr-buf), "%s%04X:%d", i?",":"", tab->caid[i], tab->value[i]);
584 }
585 *ptr = 0;
586 return buf;
587}
588
589#ifdef CS_CACHEEX
590char *mk_t_cacheex_valuetab(CECSPVALUETAB *tab){
591 if (!tab->n) return "";
592 int32_t i, size = 2 + tab->n * (4 + 1 + 4 + 1 + 6 + 1 + 4 + 1 + 5 + 1 + 5 + 1); //caid&mask@provid$servid:awtime:dwtime","
593 char *buf;
594 if (!cs_malloc(&buf, size))
595 return "";
596 char *ptr = buf;
597
598 for (i = 0; i < tab->n; i++) {
599 if (i) ptr += snprintf(ptr, size-(ptr-buf), ",");
600 if (tab->caid[i]>=0) {
601 if (tab->caid[i] == 0) {
602 if (tab->awtime[i]>0)
603 ptr += snprintf(ptr, size-(ptr-buf), "%d", tab->caid[i]);
604 } else if(tab->caid[i] < 256) //Do not format 0D as 000D, its a shortcut for 0Dxx:
605 ptr += snprintf(ptr, size-(ptr-buf), "%02X", tab->caid[i]);
606 else
607 ptr += snprintf(ptr, size-(ptr-buf), "%04X", tab->caid[i]);
608 }
609 if (tab->cmask[i]>=0)
610 ptr += snprintf(ptr, size-(ptr-buf), "&%04X", tab->cmask[i]);
611 if (tab->prid[i]>=0)
612 ptr += snprintf(ptr, size-(ptr-buf), "@%06X", tab->prid[i]);
613 if (tab->srvid[i]>=0)
614 ptr += snprintf(ptr, size-(ptr-buf), "$%04X", tab->srvid[i]);
615 if (tab->awtime[i]>0)
616 ptr += snprintf(ptr, size-(ptr-buf), ":%d", tab->awtime[i]);
617 if (!tab->dwtime[i]>0)
618 ptr += snprintf(ptr, size-(ptr-buf), ":0");
619 if (tab->dwtime[i]>0) {
620 if ((tab->caid[i] <= 0) && (tab->prid[i] == -1) &&(tab->srvid[i] == -1) && (tab->srvid[i] == -1) && (tab->awtime[i] <= 0))
621 ptr += snprintf(ptr, size-(ptr-buf), "%d", tab->dwtime[i]);
622 else
623 ptr += snprintf(ptr, size-(ptr-buf), ":%d", tab->dwtime[i]);
624 }
625 }
626 *ptr = 0;
627 return buf;
628}
629
630char *mk_t_cacheex_hitvaluetab(CECSPVALUETAB *tab){
631 if (!tab->n) return "";
632 int32_t i, size = 2 + tab->n * (4 + 1 + 4 + 1 + 6 + 1 + 4 + 1); //caid&mask@provid$servid","
633 char *buf;
634 if (!cs_malloc(&buf, size))
635 return "";
636 char *ptr = buf;
637
638 for (i = 0; i < tab->n; i++) {
639 if (i) ptr += snprintf(ptr, size-(ptr-buf), ",");
640 if (tab->caid[i]>0) {
641 if(tab->caid[i] < 256) //Do not format 0D as 000D, its a shortcut for 0Dxx:
642 ptr += snprintf(ptr, size-(ptr-buf), "%02X", tab->caid[i]);
643 else
644 ptr += snprintf(ptr, size-(ptr-buf), "%04X", tab->caid[i]);
645 if (tab->cmask[i]>=0)
646 ptr += snprintf(ptr, size-(ptr-buf), "&%04X", tab->cmask[i]);
647 if (tab->prid[i]>=0)
648 ptr += snprintf(ptr, size-(ptr-buf), "@%06X", tab->prid[i]);
649 if (tab->srvid[i]>=0)
650 ptr += snprintf(ptr, size-(ptr-buf), "$%04X", tab->srvid[i]);
651 }
652 }
653 *ptr = 0;
654 return buf;
655}
656#endif
657
658/*
659 * returns string of comma separated values
660 */
661char *mk_t_emmbylen(struct s_reader *rdr) {
662
663 char *value, *dot = "";
664 int32_t pos = 0, needed = 0;
665 int8_t i;
666
667 needed = (CS_MAXEMMBLOCKBYLEN * 4) +1;
668
669 if (!cs_malloc(&value, needed)) return "";
670
671 for( i = 0; i < CS_MAXEMMBLOCKBYLEN; i++ ) {
672 if(rdr->blockemmbylen[i] != 0) {
673 pos += snprintf(value + pos, needed, "%s%d", dot, rdr->blockemmbylen[i]);
674 dot = ",";
675 }
676 }
677 return value;
678}
679
680/*
681 * makes string from binary structure
682 */
683char *mk_t_allowedprotocols(struct s_auth *account) {
684
685 if (!account->allowedprotocols)
686 return "";
687
688 int16_t i, tmp = 1, pos = 0, needed = 255, tagcnt;
689 char *tag[] = {"camd33", "camd35", "cs378x", "newcamd", "cccam", "gbox", "radegast", "dvbapi", "constcw", "serial"};
690 char *value, *dot = "";
691
692 if (!cs_malloc(&value, needed))
693 return "";
694
695 tagcnt = sizeof(tag)/sizeof(char *);
696 for (i = 0; i < tagcnt; i++) {
697 if ((account->allowedprotocols & tmp) == tmp) {
698 pos += snprintf(value + pos, needed, "%s%s", dot, tag[i]);
699 dot = ",";
700 }
701 tmp = tmp << 1;
702 }
703 return value;
704}
705
706/*
707 * mk_t-functions give back a constant empty string when allocation fails or when the result is an empty string.
708 * This function thus checks the stringlength and only frees if necessary.
709 */
710void free_mk_t(char *value) {
711 if(strlen(value) > 0) free(value);
712}
Note: See TracBrowser for help on using the repository browser.