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.

cbuf.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <netdb.h>
  5. #include <pthread.h>
  6. #include <assert.h>
  7. #include "libfuncs/libfuncs.h"
  8. #include "cbuf.h"
  9. static void cbuf_lock(CBUF *b) {
  10. pthread_mutex_lock(b->lock);
  11. }
  12. static void cbuf_unlock(CBUF *b) {
  13. pthread_mutex_unlock(b->lock);
  14. }
  15. /* Returns how much data is filled in the buffer */
  16. int cbuf_free_data_size(CBUF *b) {
  17. int ret = b->size - (b->input - b->output);
  18. assert(ret >= 0);
  19. return ret;
  20. }
  21. void cbuf_dump(CBUF *b) {
  22. LOGf("CBUF [%10s]: size:%d pos:%d writepos:%d input:%llu output:%llu free_data:%d buffered:%lld\n",
  23. b->name,
  24. b->size,
  25. b->pos,
  26. b->writepos,
  27. b->input,
  28. b->output,
  29. cbuf_free_data_size(b),
  30. b->input - b->output
  31. );
  32. /*
  33. char *z = b->buffer;
  34. printf("cbuf(%s), dump:", b->name);
  35. int i;
  36. for (i=0;i<b->size;i++) {
  37. printf("%c", z[i]);
  38. }
  39. printf("\n\n");
  40. */
  41. }
  42. CBUF *cbuf_init(int buffer_size, char *name) {
  43. CBUF *b = calloc(1, sizeof(CBUF));
  44. if (!b)
  45. return NULL;
  46. if (!buffer_size)
  47. return 0;
  48. pthread_mutex_t *mutex = malloc(sizeof(pthread_mutex_t));
  49. if (pthread_mutex_init(mutex, NULL) != 0) {
  50. perror("cbuf_new: mutex_init");
  51. return NULL;
  52. }
  53. b->lock = mutex;
  54. b->name = strdup(name);
  55. b->size = buffer_size;
  56. b->pos = 0;
  57. b->writepos = 0;
  58. b->buffer = calloc(1, buffer_size);
  59. if (!b->buffer) {
  60. free(b);
  61. LOGf("CBUF [%10s]: Can't allocate buffer size: %d\n", name, buffer_size);
  62. return NULL;
  63. }
  64. return b;
  65. }
  66. void cbuf_free(CBUF **pb) {
  67. CBUF *b = *pb;
  68. if (!b)
  69. return;
  70. pthread_mutex_destroy(b->lock);
  71. FREE(b->lock);
  72. FREE(b->buffer);
  73. FREE(b->name);
  74. FREE(*pb);
  75. }
  76. // Returns -1 on buffer wrap around
  77. int cbuf_fill(CBUF *b, uint8_t *data, int datasize) {
  78. int ret = 0;
  79. cbuf_lock(b);
  80. // LOGf(" cbuf_fill(%s, '%s', %d)\n", b->name, data, datasize);
  81. // cbuf_dump(b);
  82. assert(datasize <= b->size);
  83. int to_copy = min(datasize, (b->size - b->pos));
  84. if (!to_copy || !data) {
  85. LOGf("CBUF [%10s]: Nothing to fill.\n", b->name);
  86. ret = -2;
  87. goto OUT;
  88. }
  89. if (cbuf_free_data_size(b)-to_copy <= 0) {
  90. // LOGf("CBUF [%10s]: Buffer will wrap by (%d bytes). Data not filled, consume more!\n", b->name, -(cbuf_free_data_size(b)-to_copy));
  91. // cbuf_dump(b);
  92. // b->debug_get = 1;
  93. ret = -1;
  94. goto OUT;
  95. }
  96. memcpy(b->buffer + b->pos, data, to_copy);
  97. int copied = to_copy;
  98. b->pos += copied; // Move current buffer position
  99. b->input += copied;
  100. assert(b->pos <= b->size);
  101. if (b->pos == b->size) { // Buffer wrap around
  102. b->pos = 0;
  103. }
  104. if (copied < datasize) { // Move the rest
  105. // Logs when wrapping
  106. // LOGf("cbuf(%10s) copied < datasize, copied:%d datasize:%d datasize-copied: %d pos:%d\n",
  107. // b->name, copied, datasize, datasize - copied, b->pos);
  108. // cbuf_dump(b);
  109. cbuf_unlock(b);
  110. ret = cbuf_fill(b, data + copied, datasize - copied);
  111. goto OUT;
  112. }
  113. OUT:
  114. cbuf_unlock(b);
  115. return ret;
  116. }
  117. /* Returns how much space is left to the end of the buffer */
  118. static int cbuf_size_to_end(CBUF *b) {
  119. int ret = b->input - b->output;
  120. if (b->writepos + ret > b->size) {
  121. ret = b->size - b->writepos;
  122. }
  123. return ret;
  124. }
  125. int cbuf_data_size(CBUF *b) {
  126. return cbuf_size_to_end(b);
  127. }
  128. void *cbuf_get(CBUF *b, int size, int *ret_datasize) {
  129. cbuf_lock(b);
  130. void *ret = NULL;
  131. int new_size = min(size, cbuf_size_to_end(b));
  132. if (b->debug_get) {
  133. LOGf("1 cbuf_get(%s, %d) new_size: %d size_to_end: %d\n",
  134. b->name, size, new_size, cbuf_size_to_end(b));
  135. cbuf_dump(b);
  136. }
  137. if (new_size <= 0) { // No data
  138. *ret_datasize = 0;
  139. ret = NULL;
  140. goto OUT;
  141. }
  142. *ret_datasize = new_size;
  143. ret = b->buffer + b->writepos;
  144. b->writepos += new_size; // Move writepos
  145. b->output += new_size;
  146. if (b->writepos > b->size) {
  147. LOGf("!!! b->writepos > b->size !!! size:%d new_size:%d\n", size, new_size);
  148. cbuf_dump(b);
  149. assert(b->writepos <= b->size);
  150. }
  151. if (b->writepos == b->size) // Buffer wraparound
  152. b->writepos = 0;
  153. OUT:
  154. if (b->debug_get) {
  155. LOGf("2 cbuf_get(%s, %d) new_size: %d size_to_end: %d ret_sz:%d\n",
  156. b->name, size, new_size, cbuf_size_to_end(b), *ret_datasize);
  157. cbuf_dump(b);
  158. b->debug_get = 0;
  159. }
  160. cbuf_unlock(b);
  161. return ret;
  162. }
  163. void *cbuf_peek(CBUF *b, int size, int *ret_datasize) {
  164. cbuf_lock(b);
  165. void *ret = NULL;
  166. int new_size = min(size, cbuf_size_to_end(b));
  167. if (new_size <= 0) { // No data
  168. *ret_datasize = 0;
  169. ret = NULL;
  170. goto OUT;
  171. }
  172. *ret_datasize = new_size;
  173. ret = b->buffer + b->writepos;
  174. OUT:
  175. cbuf_unlock(b);
  176. return ret;
  177. }
  178. void cbuf_copy(CBUF *from, CBUF *to) {
  179. // LOGf("cbuf_copy(%s, %s)\n", from->name, to->name);
  180. int data_size;
  181. void *data;
  182. do {
  183. data = cbuf_get(from, from->input - from->output, &data_size);
  184. if (from->debug_get)
  185. LOGf("copied from %s to %s size=%d\n", from->name, to->name, data_size);
  186. if (!data || data_size <= 0)
  187. break;
  188. cbuf_fill(to, data, data_size);
  189. } while (1);
  190. }
  191. void cbuf_poison(CBUF *b, char poison_byte) {
  192. memset(b->buffer, poison_byte, b->size);
  193. }
  194. /*
  195. void consume(CBUF *b, int size) {
  196. int data_size, i;
  197. char *data = cbuf_get(b, size, &data_size);
  198. if (data && data_size > 0) {
  199. printf("Consumed %d Data: \"", data_size);
  200. for (i=0;i<data_size;i++) {
  201. printf("%c", data[i]);
  202. }
  203. printf("\"\n");
  204. } else {
  205. printf("%s", "There is nothing to consume!\n");
  206. }
  207. }
  208. void cbuf_test() {
  209. CBUF *in;
  210. CBUF *out;
  211. out = cbuf_init(64, "out");
  212. cbuf_poison(out, 'O');
  213. cbuf_dump(out);
  214. in = cbuf_init(4, "in");
  215. cbuf_poison(in, '*');
  216. cbuf_fill(in, "12", 2);
  217. cbuf_fill(in, "34", 2);
  218. cbuf_fill(in, "z" , 1);
  219. cbuf_dump(in);
  220. cbuf_copy(in, out);
  221. cbuf_dump(out);
  222. consume(in, 16);
  223. cbuf_dump(in);
  224. cbuf_fill(in, "a", 1);
  225. cbuf_fill(in, "b", 1);
  226. cbuf_fill(in, "c", 1);
  227. cbuf_fill(in, "d", 1);
  228. cbuf_dump(in);
  229. cbuf_copy(in, out);
  230. cbuf_dump(out);
  231. consume(in, 4);
  232. cbuf_dump(in);
  233. cbuf_fill(in, "gfgf", 4);
  234. cbuf_dump(in);
  235. consume(in, 4);
  236. cbuf_dump(in);
  237. cbuf_fill(in, "r", 1);
  238. cbuf_fill(in, "r", 1);
  239. cbuf_fill(in, "r", 1);
  240. cbuf_fill(in, "r", 1);
  241. cbuf_dump(in);
  242. consume(out, 6);
  243. cbuf_copy(in, out);
  244. consume(out, 6);
  245. consume(out, 6);
  246. consume(out, 6);
  247. consume(out, 6);
  248. cbuf_free(in);
  249. cbuf_free(out);
  250. }
  251. */