Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 23rd, 2012  |  syntax: None  |  size: 1.77 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  int read_encrypted_file(fz_stream *stm, unsigned char *buf, int len){
  2.         int n=0;
  3.         rimage_aesd *aesd = (rimage_aesd *)stm->state;//get aesd from stm
  4.        
  5.         int64_t toPos;//position to read to from decryptedBuffer
  6.         int64_t fromPos;//position to read from in decrypted buffer
  7.         int whence = 0;
  8.         int whence1 = 0;
  9.         int lastSeekPos = stm->pos;
  10.         int64_t blockStartPoint = 0;   
  11.         int64_t block_num = findBlocNum(stm->pos);//depending on the fileHandler position
  12.        
  13.         blockStartPoint = block_num*BLOCK_SIZE;
  14.        
  15.         aesd->blockNum = block_num;
  16.         fromPos = stm->pos - blockStartPoint;
  17.         toPos = fromPos + len;
  18.         if(lastSeekPos > aesd->contentSize){
  19.                 //return -1;
  20.         }
  21.         whence = stm->pos - blockStartPoint;
  22.         //lseek(aesd->filehandle, 1, -whence);//seek to block start to read data
  23.         lseek(aesd->filehandle, 0, blockStartPoint);
  24.  
  25.         read(aesd->filehandle, aesd->encryptedBuffer, BLOCK_SIZE);
  26.         rimage_decrypt_block(aesd);
  27.         //to handle scenario where some data is in this block and remaining in next block
  28.         if(toPos > BLOCK_SIZE){
  29.                 n += copy_decrypted_data(buf,aesd,fromPos,BLOCK_SIZE,n);
  30.                 aesd->blockNum++;
  31.                 read(aesd->filehandle, aesd->encryptedBuffer, BLOCK_SIZE);
  32.                 rimage_decrypt_block(aesd);
  33.                 toPos = len - n;
  34.                 fromPos = 0;
  35.  
  36.                 if( (aesd->blockNum * BLOCK_SIZE) + toPos > aesd->contentSize){
  37.                         toPos = toPos - ((aesd->blockNum * BLOCK_SIZE) + toPos - aesd->contentSize);
  38.                 }
  39.         }else{
  40.                 if(lastSeekPos+len > aesd->contentSize){
  41.                         toPos = toPos - (lastSeekPos+len - aesd->contentSize);
  42.                        
  43.                 }
  44.         }
  45.        
  46.         n += copy_decrypted_data(buf,aesd,fromPos,toPos,n);
  47.        
  48.         whence = lastSeekPos + n;
  49.         lseek(aesd->filehandle, 0, whence);//set file handle position correctly
  50.         fz_assert_lock_held(stm->ctx, FZ_LOCK_FILE);
  51.         if (n < 0)
  52.                 fz_throw(stm->ctx, "read error: %s", strerror(errno));
  53.         return n;
  54.  }