Diff
Not logged in

Differences From:

File src/blob.c part of check-in [bf16ab9b7b] - Completed implementation of utility functions to encrypt and decrypt blobs. by drh on 2009-03-26 23:26:31. [view]

To:

File src/blob.c part of check-in [9a23c348b1] - Infrastructure in place on the client side to encrypt sync traffic. This is mostly untested so far because we do not yet have a server that understands encrypted traffic. by drh on 2009-03-27 14:32:33. [view]

@@ -930,15 +930,18 @@
 ** than the input.
 **
 ** pOut should be initialized prior to invoking this routine.  pOut might
 ** already contain other content.  The encryption is appended to pOut.
+** The encryption cannot be done in place; pOut cannot be the same blob
+** as pIn.
 */
 void blob_encrypt(Blob *pIn, const char *zPassword, Blob *pOut){
   char *aNonce;
   char *aIn;
   int nIn;
   char *aOut;
 
+  assert( pIn!=pOut );
   aIn = pIn->aData;
   aIn += pIn->iCursor;
   nIn = pIn->nUsed - pIn->iCursor;
   if( nIn<=0 ) return;
@@ -962,9 +965,11 @@
 ** extending to the end of the blob is decrypted.  Any content of pIn
 ** prior to the current cursor position is ignored.
 **
 ** pOut should be initialized prior to invoking this routine.  pOut might
-** already contain other content.  The encryption is appended to pOut.
+** already contain other content.  The decryption is appended to pOut
+** starting at its current cursor position.  Decryption can be done
+** in place; it is acceptable for pOut and pIn to be the same blob.
 */
 void blob_decrypt(Blob *pIn, const char *zPassword, Blob *pOut){
   char *aNonce;
   char *aIn;
@@ -974,12 +979,19 @@
   aIn = pIn->aData;
   aNonce = aIn + pIn->iCursor;
   aIn = aNonce + N_NONCE;
   nIn = pIn->nUsed - pIn->iCursor - N_NONCE;
-  blob_resize(pOut, pOut->iCursor + nIn);
+  if( pOut!=pIn ){
+    blob_resize(pOut, pOut->iCursor + nIn);
+  }
   aOut = pOut->aData;
   aOut += pOut->iCursor;
   rc4_coder(zPassword, aNonce, N_NONCE, aIn, nIn, aOut);
+  if( pOut==pIn ){
+    pOut->nUsed = pOut->iCursor + nIn;
+    aOut[nIn] = 0;
+    pOut->iCursor = pOut->nUsed;
+  }
 }
 
 
 /*