tsdecrypt reads and decrypts CSA encrypted incoming mpeg transport stream over UDP/RTP using code words obtained from OSCAM or similar CAM server. tsdecrypt communicates with CAM server using cs378x (camd35 over tcp) protocol or newcamd protocol. https://georgi.unixsol.org/programs/tsdecrypt/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logic.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* logic -- synthetize logic functions with 4 inputs
  2. *
  3. * Copyright (C) 2003-2004 fatih89r
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /* Can we use negated inputs? */
  20. #define noNEGATEDTOO
  21. #include <stdio.h>
  22. /*
  23. * abcd
  24. */
  25. #define BINARY(b15,b14,b13,b12,b11,b10,b9,b8,b7,b6,b5,b4,b3,b2,b1,b0) \
  26. ((b15)<<15)|((b14)<<14)|((b13)<<13)|((b12)<<12)| \
  27. ((b11)<<11)|((b10)<<10)|((b9) << 9)|((b8) << 8)| \
  28. ((b7) << 7)|((b6) << 6)|((b5) << 5)|((b4) << 4)| \
  29. ((b3) << 3)|((b2) << 2)|((b1) << 1)|((b0) << 0)
  30. struct fun{
  31. int level;
  32. int op_type;
  33. int op1;
  34. int op2;
  35. };
  36. struct fun db[65536];
  37. int n_fun;
  38. #define LEVEL_ALOT 1000000
  39. #define OP_FALSE 0
  40. #define OP_TRUE 1
  41. #define OP_SRC 2
  42. #define OP_AND 3
  43. #define OP_OR 4
  44. #define OP_XOR 5
  45. #define SRC_A 10
  46. #define SRC_B 20
  47. #define SRC_C 30
  48. #define SRC_D 40
  49. #define SRC_AN 11
  50. #define SRC_BN 21
  51. #define SRC_CN 31
  52. #define SRC_DN 41
  53. void dump_element_prefix(int);
  54. void dump_element_infix(int);
  55. int main(void){
  56. int i,j;
  57. int l,p1,p2;
  58. int candidate;
  59. int max_p2_lev;
  60. for(i=0;i<65536;i++){
  61. db[i].level=LEVEL_ALOT;
  62. }
  63. n_fun=0;
  64. db[0].level=0;
  65. db[0].op_type=OP_FALSE;
  66. n_fun++;
  67. db[65535].level=0;
  68. db[65535].op_type=OP_TRUE;
  69. n_fun++;
  70. db[BINARY(0,0,0,0, 0,0,0,0, 1,1,1,1, 1,1,1,1)].level=0;
  71. db[BINARY(0,0,0,0, 0,0,0,0, 1,1,1,1, 1,1,1,1)].op_type=OP_SRC;
  72. db[BINARY(0,0,0,0, 0,0,0,0, 1,1,1,1, 1,1,1,1)].op1=SRC_A;
  73. n_fun++;
  74. db[BINARY(0,0,0,0, 1,1,1,1, 0,0,0,0, 1,1,1,1)].level=0;
  75. db[BINARY(0,0,0,0, 1,1,1,1, 0,0,0,0, 1,1,1,1)].op_type=OP_SRC;
  76. db[BINARY(0,0,0,0, 1,1,1,1, 0,0,0,0, 1,1,1,1)].op1=SRC_B;
  77. n_fun++;
  78. db[BINARY(0,0,1,1, 0,0,1,1, 0,0,1,1, 0,0,1,1)].level=0;
  79. db[BINARY(0,0,1,1, 0,0,1,1, 0,0,1,1, 0,0,1,1)].op_type=OP_SRC;
  80. db[BINARY(0,0,1,1, 0,0,1,1, 0,0,1,1, 0,0,1,1)].op1=SRC_C;
  81. n_fun++;
  82. db[BINARY(0,1,0,1, 0,1,0,1, 0,1,0,1, 0,1,0,1)].level=0;
  83. db[BINARY(0,1,0,1, 0,1,0,1, 0,1,0,1, 0,1,0,1)].op_type=OP_SRC;
  84. db[BINARY(0,1,0,1, 0,1,0,1, 0,1,0,1, 0,1,0,1)].op1=SRC_D;
  85. n_fun++;
  86. #ifdef NEGATEDTOO
  87. db[BINARY(1,1,1,1, 1,1,1,1, 0,0,0,0, 0,0,0,0)].level=0;
  88. db[BINARY(1,1,1,1, 1,1,1,1, 0,0,0,0, 0,0,0,0)].op_type=OP_SRC;
  89. db[BINARY(1,1,1,1, 1,1,1,1, 0,0,0,0, 0,0,0,0)].op1=SRC_AN;
  90. n_fun++;
  91. db[BINARY(1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0,0)].level=0;
  92. db[BINARY(1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0,0)].op_type=OP_SRC;
  93. db[BINARY(1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0,0)].op1=SRC_BN;
  94. n_fun++;
  95. db[BINARY(1,1,0,0, 1,1,0,0, 1,1,0,0, 1,1,0,0)].level=0;
  96. db[BINARY(1,1,0,0, 1,1,0,0, 1,1,0,0, 1,1,0,0)].op_type=OP_SRC;
  97. db[BINARY(1,1,0,0, 1,1,0,0, 1,1,0,0, 1,1,0,0)].op1=SRC_CN;
  98. n_fun++;
  99. db[BINARY(1,0,1,0, 1,0,1,0, 1,0,1,0, 1,0,1,0)].level=0;
  100. db[BINARY(1,0,1,0, 1,0,1,0, 1,0,1,0, 1,0,1,0)].op_type=OP_SRC;
  101. db[BINARY(1,0,1,0, 1,0,1,0, 1,0,1,0, 1,0,1,0)].op1=SRC_DN;
  102. n_fun++;
  103. #endif
  104. for(l=0;l<100;l++){
  105. printf("calculating level %i\n",l);
  106. for(p1=1;p1<65536;p1++){
  107. if(db[p1].level==LEVEL_ALOT) continue;
  108. max_p2_lev=l-db[p1].level-1;
  109. for(p2=p1+1;p2<65536;p2++){
  110. if(db[p2].level>max_p2_lev) continue;
  111. candidate=p1&p2;
  112. if(db[candidate].level==LEVEL_ALOT){
  113. //found new
  114. db[candidate].level=db[p1].level+db[p2].level+1;
  115. db[candidate].op_type=OP_AND;
  116. db[candidate].op1=p1;
  117. db[candidate].op2=p2;
  118. n_fun++;
  119. }
  120. candidate=p1|p2;
  121. if(db[candidate].level==LEVEL_ALOT){
  122. //found new
  123. db[candidate].level=db[p1].level+db[p2].level+1;
  124. db[candidate].op_type=OP_OR;
  125. db[candidate].op1=p1;
  126. db[candidate].op2=p2;
  127. n_fun++;
  128. }
  129. candidate=p1^p2;
  130. if(db[candidate].level==LEVEL_ALOT){
  131. //found new
  132. db[candidate].level=db[p1].level+db[p2].level+1;
  133. db[candidate].op_type=OP_XOR;
  134. db[candidate].op1=p1;
  135. db[candidate].op2=p2;
  136. n_fun++;
  137. }
  138. }
  139. }
  140. printf("num fun=%i\n\n",n_fun);
  141. fflush(stdout);
  142. if(n_fun>=65536) break;
  143. }
  144. for(i=0;i<65536;i++){
  145. if(db[i].level==LEVEL_ALOT) continue;
  146. printf("PREFIX ");
  147. for(j=15;j>=0;j--){
  148. printf("%i",i&(1<<j)?1:0);
  149. if(j%4==0) printf(" ");
  150. if(j%8==0) printf(" ");
  151. }
  152. printf(" : lev %2i: ",db[i].level);
  153. dump_element_prefix(i);
  154. printf("\n");
  155. printf("INFIX ");
  156. for(j=15;j>=0;j--){
  157. printf("%i",i&(1<<j)?1:0);
  158. if(j%4==0) printf(" ");
  159. if(j%8==0) printf(" ");
  160. }
  161. printf(" : lev %2i: ",db[i].level);
  162. dump_element_infix(i);
  163. printf("\n");
  164. }
  165. return 0;
  166. }
  167. void dump_element_prefix(int e){
  168. if(db[e].level==LEVEL_ALOT){
  169. printf("PANIC!\n");
  170. return;
  171. };
  172. switch(db[e].op_type){
  173. case OP_FALSE:
  174. printf("0");
  175. break;
  176. case OP_TRUE:
  177. printf("1");
  178. break;
  179. case OP_SRC:
  180. switch(db[e].op1){
  181. case SRC_A:
  182. printf("a");
  183. break;
  184. case SRC_B:
  185. printf("b");
  186. break;
  187. case SRC_C:
  188. printf("c");
  189. break;
  190. case SRC_D:
  191. printf("d");
  192. break;
  193. case SRC_AN:
  194. printf("an");
  195. break;
  196. case SRC_BN:
  197. printf("bn");
  198. break;
  199. case SRC_CN:
  200. printf("cn");
  201. break;
  202. case SRC_DN:
  203. printf("dn");
  204. break;
  205. }
  206. break;
  207. case OP_AND:
  208. printf("FFAND(");
  209. dump_element_prefix(db[e].op1);
  210. printf(",");
  211. dump_element_prefix(db[e].op2);
  212. printf(")");
  213. break;
  214. case OP_OR:
  215. printf("FFOR(");
  216. dump_element_prefix(db[e].op1);
  217. printf(",");
  218. dump_element_prefix(db[e].op2);
  219. printf(")");
  220. break;
  221. case OP_XOR:
  222. printf("FFXOR(");
  223. dump_element_prefix(db[e].op1);
  224. printf(",");
  225. dump_element_prefix(db[e].op2);
  226. printf(")");
  227. break;
  228. }
  229. }
  230. void dump_element_infix(int e){
  231. if(db[e].level==LEVEL_ALOT){
  232. printf("PANIC!\n");
  233. return;
  234. };
  235. switch(db[e].op_type){
  236. case OP_FALSE:
  237. printf("0");
  238. break;
  239. case OP_TRUE:
  240. printf("1");
  241. break;
  242. case OP_SRC:
  243. switch(db[e].op1){
  244. case SRC_A:
  245. printf("a");
  246. break;
  247. case SRC_B:
  248. printf("b");
  249. break;
  250. case SRC_C:
  251. printf("c");
  252. break;
  253. case SRC_D:
  254. printf("d");
  255. break;
  256. case SRC_AN:
  257. printf("an");
  258. break;
  259. case SRC_BN:
  260. printf("bn");
  261. break;
  262. case SRC_CN:
  263. printf("cn");
  264. break;
  265. case SRC_DN:
  266. printf("dn");
  267. break;
  268. }
  269. break;
  270. case OP_AND:
  271. printf("( ");
  272. dump_element_infix(db[e].op1);
  273. printf("&");
  274. dump_element_infix(db[e].op2);
  275. printf(" )");
  276. break;
  277. case OP_OR:
  278. printf("( ");
  279. dump_element_infix(db[e].op1);
  280. printf("|");
  281. dump_element_infix(db[e].op2);
  282. printf(" )");
  283. break;
  284. case OP_XOR:
  285. printf("( ");
  286. dump_element_infix(db[e].op1);
  287. printf("^");
  288. dump_element_infix(db[e].op2);
  289. printf(" )");
  290. break;
  291. }
  292. }