- simplify cak7 code (thanks to lpm11 and for all who tested)
[oscam.git] / oscam-simples.c
blob69dfcb30914b16dd44507488fa9233a9ccbf07df
1 #include "globals.h"
2 #include "oscam-string.h"
4 static void cl_set_last_providptr(struct s_client *cl, uint32_t provid, uint16_t caid)
6 int32_t i;
7 struct s_provid *this = cfg.provid;
8 struct s_provid *zero_match = NULL;
10 cl->last_providptr = NULL;
12 if(!caid)
14 return;
17 for(; this; this = this->next)
19 if(this->caid == caid)
21 if(this->nprovid == 0 )
23 zero_match = this;
26 for(i = 0; i < this->nprovid; i++)
28 if(this->provid[i] == 0 )
30 zero_match = this;
33 if(this->provid[i] == provid)
35 cl->last_providptr = this;
36 return;
42 if(zero_match != NULL)
44 cl->last_providptr = zero_match;
48 /* Gets the servicename. */
49 static char *__get_servicename(struct s_client *cl, uint16_t srvid, uint32_t provid, uint16_t caid, char *buf, uint32_t buflen, bool return_unknown)
51 int32_t i, j;
52 struct s_srvid *this, *provid_zero_match = NULL, *provid_any_match = NULL;
53 buf[0] = '\0';
55 if(!srvid || (srvid >> 12) >= 16) // cfg.srvid[16]
57 return (buf);
60 if(cl && cl->last_srvidptr && cl->last_srvidptr->srvid == srvid)
62 for(i = 0; i < cl->last_srvidptr->ncaid; i++)
64 if(cl->last_srvidptr->caid[i].caid == caid && cl->last_srvidptr_search_provid == provid && cl->last_srvidptr->name)
66 if(cl->last_providptr == NULL)
68 cl_set_last_providptr(cl, provid, caid);
70 cs_strncpy(buf, cl->last_srvidptr->name, buflen);
71 return (buf);
76 for(this = cfg.srvid[srvid >> 12]; this; this = this->next)
78 if(this->srvid == srvid)
80 for(i = 0; i < this->ncaid; i++)
82 if(this->caid[i].caid == caid && this->name)
84 provid_any_match = this;
85 if(this->caid[i].nprovid == 0)
87 provid_zero_match = this;
89 if(0 == provid)
91 if(cl)
93 cl_set_last_providptr(cl, provid, caid);
94 cl->last_srvidptr = this;
95 cl->last_srvidptr_search_provid = provid;
97 cs_strncpy(buf, this->name, buflen);
98 return (buf);
102 for(j = 0; j < this->caid[i].nprovid; j++)
104 if(this->caid[i].provid[j] == 0)
105 { provid_zero_match = this; }
107 if(this->caid[i].provid[j] == provid)
109 if(cl)
111 cl_set_last_providptr(cl, provid, caid);
112 cl->last_srvidptr = this;
113 cl->last_srvidptr_search_provid = provid;
115 cs_strncpy(buf, this->name, buflen);
116 return (buf);
124 if(!buf[0])
126 if(provid != 0 && provid_zero_match != NULL)
128 if(cl)
130 cl_set_last_providptr(cl, provid, caid);
131 cl->last_srvidptr = provid_zero_match;
132 cl->last_srvidptr_search_provid = provid;
134 cs_strncpy(buf, provid_zero_match->name, buflen);
135 return (buf);
137 else if(provid == 0 && provid_any_match != NULL)
139 if(cl)
141 cl_set_last_providptr(cl, provid, caid);
142 cl->last_srvidptr = provid_any_match;
143 cl->last_srvidptr_search_provid = provid;
145 cs_strncpy(buf, provid_any_match->name, buflen);
146 return (buf);
149 if(return_unknown)
151 snprintf(buf, buflen, "%04X@%06X:%04X unknown", caid, provid, srvid);
154 if(cl)
156 cl->last_providptr = NULL;
157 cl->last_srvidptr = NULL;
158 cl->last_srvidptr_search_provid = provid;
161 return (buf);
164 char *get_servicename(struct s_client *cl, uint16_t srvid, uint32_t provid, uint16_t caid, char *buf, uint32_t buflen)
166 return __get_servicename(cl, srvid, provid, caid, buf, buflen, true);
169 char *get_servicename_or_null(struct s_client *cl, uint16_t srvid, uint32_t provid, uint16_t caid, char *buf, uint32_t buflen)
171 return __get_servicename(cl, srvid, provid, caid, buf, buflen, false);
174 char *get_picon_servicename_or_null(struct s_client *cl, uint16_t srvid, uint32_t provid, uint16_t caid, char *buf, uint32_t buflen)
176 uint32_t i, j;
177 __get_servicename(cl, srvid, provid, caid, buf, buflen, false);
178 char *tmp_buf;
180 if(buf[0])
182 if(!cs_malloc(&tmp_buf, buflen))
184 buf[0] = '\0';
185 return (buf);
187 j = 0;
189 for(i = 0; i < buflen && buf[i] && j + 4 < buflen; i++)
191 if(isalnum((int)buf[i]))
193 tmp_buf[j] = (char)tolower((int)buf[i]);
194 j++;
196 else if(buf[i] == '*')
198 tmp_buf[j] = 's';
199 tmp_buf[j + 1] = 't';
200 tmp_buf[j + 2] = 'a';
201 tmp_buf[j + 3] = 'r';
202 j += 4;
204 else if(buf[i] == '+')
206 tmp_buf[j] = 'p';
207 tmp_buf[j + 1] = 'l';
208 tmp_buf[j + 2] = 'u';
209 tmp_buf[j + 3] = 's';
210 j += 4;
212 else if(buf[i] == '&')
214 tmp_buf[j] = 'a';
215 tmp_buf[j + 1] = 'n';
216 tmp_buf[j + 2] = 'd';
217 j += 3;
221 tmp_buf[buflen - 1] = '\0';
222 cs_strncpy(buf, tmp_buf, buflen);
223 NULLFREE(tmp_buf);
226 return (buf);
229 int32_t picon_servicename_remve_hd(char *buf, uint32_t UNUSED(buflen))
231 int32_t l = cs_strlen(buf);
233 if(l < 3)
235 return 0;
238 if(buf[l - 2] == 'h' && buf[l - 1] == 'd')
240 buf[l - 2] = '\0';
241 buf[l - 1] = '\0';
242 return 1;
245 return 0;
248 /* Gets the tier name. Make sure that buf is at least 83 bytes long. */
249 char *get_tiername(uint16_t tierid, uint16_t caid, char *buf)
251 uint8_t found = 0;
252 int32_t i;
253 struct s_tierid *this = cfg.tierid;
255 for(buf[0] = 0; this && !found; this = this->next)
257 if(this->tierid == tierid)
259 for(i = 0; i < this->ncaid && !found; i++)
261 if(this->caid[i] == caid)
263 cs_strncpy(buf, this->name, 32); found = 1;
269 if(!tierid)
271 buf[0] = '\0';
274 return (buf);
277 /* Gets the tier name. Make sure that buf is at least 83 bytes long. */
278 char *get_tiername_defaultid(uint16_t tierid, uint16_t caid, char *buf)
280 uint8_t found = 0;
281 int32_t i;
282 struct s_tierid *this = cfg.tierid;
284 for(buf[0] = 0; this && !found; this = this->next)
286 if(this->tierid == tierid)
288 for(i = 0; i < this->ncaid && !found; i++)
290 if(this->caid[i] == caid)
292 cs_strncpy(buf, this->name, 32); found = 1;
298 if(!tierid)
300 snprintf(buf, 82, "%04X", tierid);
303 return (buf);
306 /* Gets the provider name. */
307 char *get_provider(uint32_t provid, uint16_t caid, char *buf, uint32_t buflen)
309 uint8_t found = 0;
310 int32_t i;
311 struct s_provid *this = cfg.provid;
313 if(!caid)
315 buf[0] = '\0';
316 return (buf);
319 for(buf[0] = 0; this && !found; this = this->next)
321 if(this->caid == caid)
323 for(i = 0; i < this->nprovid && !found; i++)
325 if(this->provid[i] == provid)
327 snprintf(buf, buflen, "%s%s%s%s%s",
328 this->prov,
329 *this->sat && this->sat[0] ? " / " : "",
330 this->sat, this->lang[0] ? " / " : "",
331 this->lang);
332 found = 1;
338 if(!buf[0])
340 snprintf(buf, buflen, "%04X@%06X unknown", caid, provid);
342 return (buf);
345 char *__get_providername(uint32_t provid, uint16_t caid, char *buf, uint32_t buflen, bool return_unknown)
347 uint8_t found = 0;
348 int32_t i;
349 struct s_provid *this = cfg.provid;
350 struct s_provid *zero_match = NULL;
352 if(!caid)
354 buf[0] = '\0';
355 return (buf);
358 for(buf[0] = 0; this && !found; this = this->next)
360 if(this->caid == caid)
362 if(this->nprovid == 0 )
364 zero_match = this;
367 for(i = 0; i < this->nprovid && !found; i++)
369 if(this->provid[i] == 0 )
371 zero_match = this;
374 if(this->provid[i] == provid)
376 cs_strncpy(buf, this->prov, buflen);
377 found = 1;
383 if(!found && zero_match != NULL)
385 cs_strncpy(buf, zero_match->prov, buflen);
388 if(!buf[0] && return_unknown)
390 snprintf(buf, buflen, "%04X@%06X unknown", caid, provid);
393 return (buf);
396 char *get_providername(uint32_t provid, uint16_t caid, char *buf, uint32_t buflen)
398 return __get_providername(provid, caid, buf, buflen, true);
401 char *get_providername_or_null(uint32_t provid, uint16_t caid, char *buf, uint32_t buflen)
403 return __get_providername(provid, caid, buf, buflen, false);
406 const char *get_cl_lastprovidername(struct s_client *cl)
408 if(!cl->last_srvidptr || !cl->last_srvidptr->prov || cl->last_srvidptr->prov[0] == '\0' || !strcmp(cl->last_srvidptr->prov, " "))
410 if(!cl->last_providptr)
412 return "";
414 else
416 return cl->last_providptr->prov;
419 return cl->last_srvidptr->prov;
422 // Add provider description. If provider was already present, do nothing.
423 void add_provider(uint16_t caid, uint32_t provid, const char *name, const char *sat, const char *lang)
425 int32_t i;
426 struct s_provid **ptr;
428 for(ptr = &cfg.provid; *ptr; ptr = &(*ptr)->next)
430 if((*ptr)->caid == caid)
432 for(i = 0; i < (*ptr)->nprovid; i++)
434 if((*ptr)->provid[i] == provid)
436 return;
442 struct s_provid *prov;
443 if(!cs_malloc(&prov, sizeof(struct s_provid)))
445 return;
448 if(!cs_malloc(&prov->provid, sizeof(uint32_t)))
450 NULLFREE(prov);
451 return;
454 prov->nprovid = 1;
455 prov->provid[0] = provid;
456 prov->caid = caid;
457 cs_strncpy(prov->prov, name, sizeof(prov->prov));
458 cs_strncpy(prov->sat, sat, sizeof(prov->sat));
459 cs_strncpy(prov->lang, lang, sizeof(prov->lang));
460 *ptr = prov;
463 // Get a cardsystem name based on caid
464 // used in webif/CCcam share and in dvbapi/ecminfo
465 const char *get_cardsystem_desc_by_caid(uint16_t caid)
467 if(caid_is_seca(caid)) { return "seca"; }
468 if(caid_is_viaccess(caid)) { return "viaccess"; }
469 if(caid_is_irdeto(caid)) { return "irdeto"; }
470 if(caid_is_videoguard(caid)) { return "videoguard"; }
471 if(caid_is_powervu(caid)) { return "powervu"; }
472 if(caid_is_director(caid)) { return "director"; }
473 if(caid_is_conax(caid)) { return "conax"; }
474 if(caid_is_cryptoworks(caid)) { return "cryptoworks"; }
475 if(caid_is_betacrypt(caid)) { return "betacrypt"; }
476 if(caid_is_nagra(caid)) { return "nagra"; }
477 if(caid >= 0x4B00 && caid <= 0x4BFF) { return "tongfang"; }
478 if(caid >= 0x5501 && caid <= 0x551A) { return "griffin"; }
479 if(caid_is_dre(caid)) { return "drecrypt"; }
480 if(caid_is_bulcrypt(caid)) { return "bulcrypt"; }
481 if(caid_is_biss(caid)) { return "biss"; }
482 if(caid == 0x4ABF) { return "dgcrypt"; }
483 return "unknown";