Guest User

Untitled

a guest
Dec 11th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. static GstFlowReturn
  2. gst_destool_chain (GstPad * pad, GstBuffer * inbuf)
  3. {
  4.  
  5. GstDesTool *filter;
  6. GstBuffer *outbuf;
  7. outbuf=NULL;
  8. filter = GST_DESTOOL (GST_OBJECT_PARENT (pad));
  9.  
  10. nFrame++;
  11.  
  12. if( ( filter->ptss[filter->ptsUsed] < inbuf->timestamp/1000 ) )
  13. {
  14. filter->ptsUsed++;
  15. gchar* key = g_strdup (g_value_get_string ( g_value_array_get_nth( filter->keys, filter->ptsUsed ) ));
  16. printf(" --- TimeStamp: %llu --- key changed: %02x %02x %02x %02x %02x %02x %02x %02x\n", inbuf->timestamp/1000, key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7]);
  17. }
  18.  
  19. gchar* key = g_strdup (g_value_get_string ( g_value_array_get_nth( filter->keys, filter->ptsUsed ) ));
  20. gchar* iv = g_strdup (g_value_get_string ( g_value_array_get_nth( filter->ivs, filter->ptsUsed ) ));
  21.  
  22.  
  23. if (filter->silent == FALSE)
  24. {
  25. //Valore dello START_CODE
  26. guint8 startCode[3] = { 0x00, 0x00, 0x01 };
  27. guint8 *start = NULL;
  28. guint8 *temp = NULL;
  29. guint offset = 0;
  30.  
  31. temp = inbuf->data;
  32.  
  33. if(inbuf->size < 3){
  34. printf(" --- frame piu' corto di 3 byte --- STRANO!!!");
  35. outbuf = gst_buffer_try_new_and_alloc (inbuf->size);
  36. GST_BUFFER_TIMESTAMP(outbuf)= GST_BUFFER_TIMESTAMP (inbuf);
  37. GST_BUFFER_DURATION(outbuf)= GST_BUFFER_DURATION (inbuf);
  38. gst_buffer_set_caps (outbuf, GST_PAD_CAPS (filter->srcpad));
  39. memcpy( outbuf->data, inbuf->data, inbuf->size );
  40. return gst_pad_push (filter->srcpad, outbuf);
  41. }
  42.  
  43. guint ind = 0;
  44.  
  45. ind = 0;
  46. guint sizeBuffer = inbuf->size;
  47. do{
  48. temp = (guint8*)memmem( inbuf->data + offset, sizeBuffer, startCode, 3 );
  49. if(temp != NULL){
  50.  
  51. #ifdef PRINT
  52. printf(" --- --- --- ciclo %u --- --- ---\n",++ind);
  53. printf(" --- --- startcode trovato: %02x %02x %02x %02x\n",temp[0],temp[1],temp[2],temp[3]);
  54. #endif
  55. offset = temp - inbuf->data + 3;
  56. sizeBuffer = inbuf->size - offset;
  57. start = temp;
  58. }
  59. }
  60. while (temp != NULL && offset < inbuf->size);
  61. #ifdef PRINT
  62. printf(" --- --- --- startcode prima di cifrare: %02x %02x %02x %02x\n",start[0],start[1],start[2],start[3]);
  63. #endif
  64.  
  65. if ( start != NULL )
  66. {
  67.  
  68. offset = start - inbuf->data;
  69.  
  70. //CIFRATURA
  71. if(filter->mode==TRUE)
  72. {
  73. if ( inbuf->size <= ( offset + 8 ) ){
  74. printf("-- Frame troppo corto, NON CIFRO --\n");
  75. outbuf = gst_buffer_try_new_and_alloc (inbuf->size);
  76. GST_BUFFER_TIMESTAMP(outbuf)= GST_BUFFER_TIMESTAMP (inbuf);
  77. GST_BUFFER_DURATION(outbuf)= GST_BUFFER_DURATION (inbuf);
  78. gst_buffer_set_caps (outbuf, GST_PAD_CAPS (filter->srcpad));
  79. memcpy( outbuf->data, inbuf->data, inbuf->size );
  80. return gst_pad_push (filter->srcpad, outbuf);
  81. }
  82. offset = offset + 8;
  83. guint padSize = (8 - (inbuf->size - offset) % 8);
  84. guint newSize = (inbuf->size) + padSize;
  85. guint pad;
  86.  
  87. //Alloco buffer di output
  88. outbuf = gst_buffer_try_new_and_alloc (newSize);
  89. if(outbuf == NULL)
  90. printf("potrebbe essere qui il problema!!!\n");
  91. GST_BUFFER_TIMESTAMP(outbuf)= GST_BUFFER_TIMESTAMP (inbuf);
  92. GST_BUFFER_DURATION(outbuf)= GST_BUFFER_DURATION (inbuf);
  93. gst_buffer_set_caps (outbuf, GST_PAD_CAPS (filter->srcpad));
  94.  
  95. memcpy( outbuf->data, inbuf->data, inbuf->size );
  96.  
  97. // effettuo il padding
  98. for( pad = 0; pad < padSize; pad++)
  99. {
  100. outbuf->data[ inbuf->size + pad ] = padSize;
  101. }
  102. // prepariamo la chiave
  103. des_check_key = 1;
  104. DES_key_schedule schedule;
  105. int ret = DES_set_key ( (des_cblock*)key, &schedule );
  106. if(ret != 0 )
  107. printf(" --- (%d) c'e' qualche problema sulle chiavi!!!\n", ret );
  108. #ifdef PRINT
  109. else
  110. printf( " --- (%d) OK chiavi!!!\n", ret );
  111. #endif
  112. /*
  113. des_cblock *desKey = (des_cblock *) filter->key;
  114. char strKey[sizeof(des_cblock)];
  115. memcpy(strKey, desKey, sizeof(des_cblock));
  116. printf( " --- chiave: " );
  117. int ind;
  118. for(ind = 0; ind < sizeof(des_cblock); ind++){
  119. printf(" %x",strKey[ind]);
  120. }
  121. printf( "\n" );
  122. char *keyB64 = base64((unsigned char *) strKey, sizeof(des_cblock));
  123. printf(" --- key base64: %s (%d)\n", keyB64, (int)strlen(keyB64));
  124.  
  125. des_cblock *desIv = (des_cblock *) filter->iv;
  126. char strIv[sizeof(des_cblock)];
  127. memcpy(strIv, desIv, sizeof(des_cblock));
  128. printf( " --- chiave: " );
  129. for(ind = 0; ind < sizeof(des_cblock); ind++){
  130. printf(" %x",strIv[ind]);
  131. }
  132. printf( "\n" );
  133. char *ivB64 = base64((unsigned char *) strIv, sizeof(des_cblock));
  134. printf(" --- iv base64: %s (%d)\n", ivB64, (int)strlen(ivB64));
  135.  
  136. */
  137.  
  138. DES_cbc_encrypt( outbuf->data + offset, outbuf->data + offset, newSize - offset, &schedule, (des_cblock*)iv, DES_ENCRYPT );
  139. }
  140.  
  141. }
  142. else
  143. {
  144. printf ("begin of frame is not expected start code, dropping\n");
  145. }
  146.  
  147. }
  148.  
  149. /* just push out the incoming buffer without touching it */
  150. //gst_buffer_unref (inbuf);
  151. return gst_pad_push (filter->srcpad, outbuf);
  152. }
Add Comment
Please, Sign In to add comment