Guest User

Untitled

a guest
Apr 26th, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. int read_packet(void *opaque, uint8_t *buf, int buf_size) {
  2.  
  3.     BufferContext *pBufferCtx = (BufferContext*) opaque;
  4.  
  5.     void* inputBufferAddress = pBufferCtx->pBufferAddress;
  6.     int inputBufferSize = pBufferCtx->iBufferSize;
  7.     int inputBytesRead = pBufferCtx->iBytesRead;    //bytes already read
  8.  
  9.     int outputBytesRead = 0;                        //bytes read this f'n call
  10.  
  11.     //bufferPointer pBufferCtx = (bufferPointer)h->priv_data; //pretending priv_data is a buffer
  12.  
  13.     REFERENCE_VAR(buf); //what does this do? Left over from original read_buffer f'n
  14.  
  15.     //the rest of this is a mess. Sorry :(
  16.     if (buf_size >= inputBufferSize) {
  17.         if (inputBytesRead > 0) {
  18.             memcpy(buf, inputBufferAddress + inputBytesRead,inputBufferSize - inputBytesRead);
  19.             outputBytesRead = inputBufferSize - inputBytesRead;
  20.         } else { // inputBytesRead == 0
  21.             memcpy(buf, inputBufferAddress, inputBufferSize);
  22.             outputBytesRead = inputBufferSize;
  23.         }
  24.  
  25.     } else { // buf_min_size < inputBufferSize
  26.         if (inputBytesRead > 0) {
  27.             if (buf_size >= inputBufferSize - inputBytesRead) {
  28.                 memcpy(buf, inputBufferAddress + inputBytesRead, inputBufferSize - inputBytesRead);
  29.                 outputBytesRead = inputBufferSize - inputBytesRead;//
  30.             }
  31.  
  32.             else { // buf_min_size < inputBufferSize - inputBytesRead
  33.                 memcpy(buf, inputBufferAddress + inputBytesRead, buf_size );
  34.                 outputBytesRead = buf_size;
  35.             }
  36.         } else {
  37.  
  38.             memcpy(buf, inputBufferAddress, inputBufferSize);
  39.             outputBytesRead = buf_size;
  40.  
  41.         }
  42.  
  43.     }
  44.     pBufferCtx->iBytesRead += outputBytesRead;
  45.     if(pBufferCtx->iBytesRead > pBufferCtx->iBufferSize)
  46.         exit(-1);
  47.     return outputBytesRead;
  48. }
Add Comment
Please, Sign In to add comment