Hex Artifact Content
Not logged in

Artifact e730b77c18f05c0c72e8c5d9825a31eae58a0fc2:

File src/deltacmd.c part of check-in [0c227fa406] - Remove a c++ism from deltacmd.c. by dan on 2007-07-28 08:04:24. Also file src/deltacmd.c part of check-in [3a25b68390] - Only request the password one time on a push or pull. by drh on 2007-07-30 16:35:16.

0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20  /*.** Copyright 
0010: 28 63 29 20 32 30 30 36 20 44 2e 20 52 69 63 68  (c) 2006 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54  ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66  his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f  ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75  u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20  te it and/or.** 
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20  modify it under 
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62   GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76  lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c  ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65  ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73  s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20  tributed in the 
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c  hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20  l be useful,.** 
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20  but WITHOUT ANY 
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75  WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69  t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a  ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52  Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55   A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20  RPOSE.  See the 
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50  GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f  ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a  r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c  ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20  d have received 
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e  a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63  U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e  .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72  g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69  ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65  te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20  ation, Inc., 59 
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53  Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73  uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31  ton, MA  02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20  307, USA..**.** 
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69  Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20  nformation:.**  
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a   drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68  *   http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a  waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75  .**.** This modu
0370: 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  le implements th
0380: 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  e interface to t
0390: 68 65 20 64 65 6c 74 61 20 67 65 6e 65 72 61 74  he delta generat
03a0: 6f 72 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20  or..*/.#include 
03b0: 22 63 6f 6e 66 69 67 2e 68 22 0a 23 69 6e 63 6c  "config.h".#incl
03c0: 75 64 65 20 22 64 65 6c 74 61 63 6d 64 2e 68 22  ude "deltacmd.h"
03d0: 0a 20 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  . ./*.** Create 
03e0: 61 20 64 65 6c 74 61 20 74 68 61 74 20 64 65 73  a delta that des
03f0: 63 72 69 62 65 73 20 74 68 65 20 63 68 61 6e 67  cribes the chang
0400: 65 20 66 72 6f 6d 20 70 4f 72 69 67 69 6e 61 6c  e from pOriginal
0410: 20 74 6f 20 70 54 61 72 67 65 74 0a 2a 2a 20 61   to pTarget.** a
0420: 6e 64 20 70 75 74 20 74 68 61 74 20 64 65 6c 74  nd put that delt
0430: 61 20 69 6e 20 70 44 65 6c 74 61 2e 20 20 54 68  a in pDelta.  Th
0440: 65 20 70 44 65 6c 74 61 20 62 6c 6f 62 20 69 73  e pDelta blob is
0450: 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a   assumed to be.*
0460: 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e  * uninitialized.
0470: 0a 2a 2f 0a 69 6e 74 20 62 6c 6f 62 5f 64 65 6c  .*/.int blob_del
0480: 74 61 5f 63 72 65 61 74 65 28 42 6c 6f 62 20 2a  ta_create(Blob *
0490: 70 4f 72 69 67 69 6e 61 6c 2c 20 42 6c 6f 62 20  pOriginal, Blob 
04a0: 2a 70 54 61 72 67 65 74 2c 20 42 6c 6f 62 20 2a  *pTarget, Blob *
04b0: 70 44 65 6c 74 61 29 7b 0a 20 20 63 6f 6e 73 74  pDelta){.  const
04c0: 20 63 68 61 72 20 2a 7a 4f 72 69 67 2c 20 2a 7a   char *zOrig, *z
04d0: 54 61 72 67 3b 0a 20 20 69 6e 74 20 6c 65 6e 4f  Targ;.  int lenO
04e0: 72 69 67 2c 20 6c 65 6e 54 61 72 67 3b 0a 20 20  rig, lenTarg;.  
04f0: 69 6e 74 20 6c 65 6e 3b 0a 20 20 63 68 61 72 20  int len;.  char 
0500: 2a 7a 52 65 73 3b 0a 20 20 62 6c 6f 62 5f 7a 65  *zRes;.  blob_ze
0510: 72 6f 28 70 44 65 6c 74 61 29 3b 0a 20 20 7a 4f  ro(pDelta);.  zO
0520: 72 69 67 20 3d 20 62 6c 6f 62 5f 62 75 66 66 65  rig = blob_buffe
0530: 72 28 70 4f 72 69 67 69 6e 61 6c 29 3b 0a 20 20  r(pOriginal);.  
0540: 6c 65 6e 4f 72 69 67 20 3d 20 62 6c 6f 62 5f 73  lenOrig = blob_s
0550: 69 7a 65 28 70 4f 72 69 67 69 6e 61 6c 29 3b 0a  ize(pOriginal);.
0560: 20 20 7a 54 61 72 67 20 3d 20 62 6c 6f 62 5f 62    zTarg = blob_b
0570: 75 66 66 65 72 28 70 54 61 72 67 65 74 29 3b 0a  uffer(pTarget);.
0580: 20 20 6c 65 6e 54 61 72 67 20 3d 20 62 6c 6f 62    lenTarg = blob
0590: 5f 73 69 7a 65 28 70 54 61 72 67 65 74 29 3b 0a  _size(pTarget);.
05a0: 20 20 62 6c 6f 62 5f 72 65 73 69 7a 65 28 70 44    blob_resize(pD
05b0: 65 6c 74 61 2c 20 6c 65 6e 54 61 72 67 2b 31 36  elta, lenTarg+16
05c0: 29 3b 0a 20 20 7a 52 65 73 20 3d 20 62 6c 6f 62  );.  zRes = blob
05d0: 5f 62 75 66 66 65 72 28 70 44 65 6c 74 61 29 3b  _buffer(pDelta);
05e0: 0a 20 20 6c 65 6e 20 3d 20 64 65 6c 74 61 5f 63  .  len = delta_c
05f0: 72 65 61 74 65 28 7a 4f 72 69 67 2c 20 6c 65 6e  reate(zOrig, len
0600: 4f 72 69 67 2c 20 7a 54 61 72 67 2c 20 6c 65 6e  Orig, zTarg, len
0610: 54 61 72 67 2c 20 7a 52 65 73 29 3b 0a 20 20 62  Targ, zRes);.  b
0620: 6c 6f 62 5f 72 65 73 69 7a 65 28 70 44 65 6c 74  lob_resize(pDelt
0630: 61 2c 20 6c 65 6e 29 3b 0a 20 20 72 65 74 75 72  a, len);.  retur
0640: 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f  n 0;.}../*.** CO
0650: 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 64 65 6c  MMAND:  test-del
0660: 74 61 2d 63 72 65 61 74 65 0a 2a 2a 0a 2a 2a 20  ta-create.**.** 
0670: 47 69 76 65 6e 20 74 77 6f 20 69 6e 70 75 74 20  Given two input 
0680: 66 69 6c 65 73 2c 20 63 72 65 61 74 65 20 61 6e  files, create an
0690: 64 20 6f 75 74 70 75 74 20 61 20 64 65 6c 74 61  d output a delta
06a0: 20 74 68 61 74 20 63 61 72 72 69 65 73 0a 2a 2a   that carries.**
06b0: 20 74 68 65 20 66 69 72 73 74 20 66 69 6c 65 20   the first file 
06c0: 69 6e 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 2e  into the second.
06d0: 0a 2a 2f 0a 76 6f 69 64 20 64 65 6c 74 61 5f 63  .*/.void delta_c
06e0: 72 65 61 74 65 5f 63 6d 64 28 76 6f 69 64 29 7b  reate_cmd(void){
06f0: 0a 20 20 42 6c 6f 62 20 6f 72 69 67 2c 20 74 61  .  Blob orig, ta
0700: 72 67 65 74 2c 20 64 65 6c 74 61 3b 0a 20 20 69  rget, delta;.  i
0710: 66 28 20 67 2e 61 72 67 63 21 3d 35 20 29 7b 0a  f( g.argc!=5 ){.
0720: 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65      fprintf(stde
0730: 72 72 2c 22 55 73 61 67 65 3a 20 25 73 20 25 73  rr,"Usage: %s %s
0740: 20 4f 52 49 47 49 4e 20 54 41 52 47 45 54 20 44   ORIGIN TARGET D
0750: 45 4c 54 41 5c 6e 22 2c 20 67 2e 61 72 67 76 5b  ELTA\n", g.argv[
0760: 30 5d 2c 20 67 2e 61 72 67 76 5b 31 5d 29 3b 0a  0], g.argv[1]);.
0770: 20 20 20 20 65 78 69 74 28 31 29 3b 0a 20 20 7d      exit(1);.  }
0780: 0a 20 20 69 66 28 20 62 6c 6f 62 5f 72 65 61 64  .  if( blob_read
0790: 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 6f 72 69 67  _from_file(&orig
07a0: 2c 20 67 2e 61 72 67 76 5b 32 5d 29 3c 30 20 29  , g.argv[2])<0 )
07b0: 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 74  {.    fprintf(st
07c0: 64 65 72 72 2c 22 63 61 6e 6e 6f 74 20 72 65 61  derr,"cannot rea
07d0: 64 20 25 73 5c 6e 22 2c 20 67 2e 61 72 67 76 5b  d %s\n", g.argv[
07e0: 32 5d 29 3b 0a 20 20 20 20 65 78 69 74 28 31 29  2]);.    exit(1)
07f0: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 62 6c 6f 62  ;.  }.  if( blob
0800: 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28  _read_from_file(
0810: 26 74 61 72 67 65 74 2c 20 67 2e 61 72 67 76 5b  &target, g.argv[
0820: 33 5d 29 3c 30 20 29 7b 0a 20 20 20 20 66 70 72  3])<0 ){.    fpr
0830: 69 6e 74 66 28 73 74 64 65 72 72 2c 22 63 61 6e  intf(stderr,"can
0840: 6e 6f 74 20 72 65 61 64 20 25 73 5c 6e 22 2c 20  not read %s\n", 
0850: 67 2e 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20  g.argv[3]);.    
0860: 65 78 69 74 28 31 29 3b 0a 20 20 7d 0a 20 20 62  exit(1);.  }.  b
0870: 6c 6f 62 5f 64 65 6c 74 61 5f 63 72 65 61 74 65  lob_delta_create
0880: 28 26 6f 72 69 67 2c 20 26 74 61 72 67 65 74 2c  (&orig, &target,
0890: 20 26 64 65 6c 74 61 29 3b 0a 20 20 69 66 28 20   &delta);.  if( 
08a0: 62 6c 6f 62 5f 77 72 69 74 65 5f 74 6f 5f 66 69  blob_write_to_fi
08b0: 6c 65 28 26 64 65 6c 74 61 2c 20 67 2e 61 72 67  le(&delta, g.arg
08c0: 76 5b 34 5d 29 3c 62 6c 6f 62 5f 73 69 7a 65 28  v[4])<blob_size(
08d0: 26 64 65 6c 74 61 29 20 29 7b 0a 20 20 20 20 66  &delta) ){.    f
08e0: 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 22 63  printf(stderr,"c
08f0: 61 6e 6e 6f 74 20 77 72 69 74 65 20 25 73 5c 6e  annot write %s\n
0900: 22 2c 20 67 2e 61 72 67 76 5b 34 5d 29 3b 0a 20  ", g.argv[4]);. 
0910: 20 20 20 65 78 69 74 28 31 29 3b 0a 20 20 7d 0a     exit(1);.  }.
0920: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 6f 72    blob_reset(&or
0930: 69 67 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65  ig);.  blob_rese
0940: 74 28 26 74 61 72 67 65 74 29 3b 0a 20 20 62 6c  t(&target);.  bl
0950: 6f 62 5f 72 65 73 65 74 28 26 64 65 6c 74 61 29  ob_reset(&delta)
0960: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 6c 79  ;.}../*.** Apply
0970: 20 74 68 65 20 64 65 6c 74 61 20 69 6e 20 70 44   the delta in pD
0980: 65 6c 74 61 20 74 6f 20 74 68 65 20 6f 72 69 67  elta to the orig
0990: 69 6e 61 6c 20 66 69 6c 65 20 70 4f 72 69 67 69  inal file pOrigi
09a0: 6e 61 6c 20 74 6f 20 67 65 6e 65 72 61 74 65 0a  nal to generate.
09b0: 2a 2a 20 74 68 65 20 74 61 72 67 65 74 20 66 69  ** the target fi
09c0: 6c 65 20 70 54 61 72 67 65 74 2e 20 20 54 68 65  le pTarget.  The
09d0: 20 70 54 61 72 67 65 74 20 62 6c 6f 62 20 69 73   pTarget blob is
09e0: 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 79 20   initialized by 
09f0: 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e  this.** routine.
0a00: 0a 2a 2a 0a 2a 2a 20 49 74 20 77 6f 72 6b 73 20  .**.** It works 
0a10: 6f 6b 20 66 6f 72 20 70 54 61 72 67 65 74 20 61  ok for pTarget a
0a20: 6e 64 20 70 4f 72 69 67 69 6e 61 6c 20 74 6f 20  nd pOriginal to 
0a30: 62 65 20 74 68 65 20 73 61 6d 65 20 62 6c 6f 62  be the same blob
0a40: 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ..**.** Return t
0a50: 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
0a60: 20 74 61 72 67 65 74 2e 20 20 52 65 74 75 72 6e   target.  Return
0a70: 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 20   -1 if there is 
0a80: 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 69 6e 74  an error..*/.int
0a90: 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 70 6c   blob_delta_appl
0aa0: 79 28 42 6c 6f 62 20 2a 70 4f 72 69 67 69 6e 61  y(Blob *pOrigina
0ab0: 6c 2c 20 42 6c 6f 62 20 2a 70 44 65 6c 74 61 2c  l, Blob *pDelta,
0ac0: 20 42 6c 6f 62 20 2a 70 54 61 72 67 65 74 29 7b   Blob *pTarget){
0ad0: 0a 20 20 69 6e 74 20 6c 65 6e 2c 20 6e 3b 0a 20  .  int len, n;. 
0ae0: 20 42 6c 6f 62 20 6f 75 74 3b 0a 0a 20 20 6e 20   Blob out;..  n 
0af0: 3d 20 64 65 6c 74 61 5f 6f 75 74 70 75 74 5f 73  = delta_output_s
0b00: 69 7a 65 28 62 6c 6f 62 5f 62 75 66 66 65 72 28  ize(blob_buffer(
0b10: 70 44 65 6c 74 61 29 2c 20 62 6c 6f 62 5f 73 69  pDelta), blob_si
0b20: 7a 65 28 70 44 65 6c 74 61 29 29 3b 0a 20 20 62  ze(pDelta));.  b
0b30: 6c 6f 62 5f 7a 65 72 6f 28 26 6f 75 74 29 3b 0a  lob_zero(&out);.
0b40: 20 20 62 6c 6f 62 5f 72 65 73 69 7a 65 28 26 6f    blob_resize(&o
0b50: 75 74 2c 20 6e 29 3b 0a 20 20 6c 65 6e 20 3d 20  ut, n);.  len = 
0b60: 64 65 6c 74 61 5f 61 70 70 6c 79 28 0a 20 20 20  delta_apply(.   
0b70: 20 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 70 4f    blob_buffer(pO
0b80: 72 69 67 69 6e 61 6c 29 2c 20 62 6c 6f 62 5f 73  riginal), blob_s
0b90: 69 7a 65 28 70 4f 72 69 67 69 6e 61 6c 29 2c 0a  ize(pOriginal),.
0ba0: 20 20 20 20 20 62 6c 6f 62 5f 62 75 66 66 65 72       blob_buffer
0bb0: 28 70 44 65 6c 74 61 29 2c 20 62 6c 6f 62 5f 73  (pDelta), blob_s
0bc0: 69 7a 65 28 70 44 65 6c 74 61 29 2c 0a 20 20 20  ize(pDelta),.   
0bd0: 20 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26 6f    blob_buffer(&o
0be0: 75 74 29 29 3b 0a 20 20 69 66 28 20 6c 65 6e 3c  ut));.  if( len<
0bf0: 30 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 72 65  0 ){.    blob_re
0c00: 73 65 74 28 26 6f 75 74 29 3b 0a 20 20 7d 65 6c  set(&out);.  }el
0c10: 73 65 20 69 66 28 20 6c 65 6e 21 3d 6e 20 29 7b  se if( len!=n ){
0c20: 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 69 7a 65  .    blob_resize
0c30: 28 26 6f 75 74 2c 20 6c 65 6e 29 3b 0a 20 20 7d  (&out, len);.  }
0c40: 0a 20 20 69 66 28 20 70 54 61 72 67 65 74 3d 3d  .  if( pTarget==
0c50: 70 4f 72 69 67 69 6e 61 6c 20 29 7b 0a 20 20 20  pOriginal ){.   
0c60: 20 62 6c 6f 62 5f 72 65 73 65 74 28 70 4f 72 69   blob_reset(pOri
0c70: 67 69 6e 61 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70  ginal);.  }.  *p
0c80: 54 61 72 67 65 74 20 3d 20 6f 75 74 3b 0a 20 20  Target = out;.  
0c90: 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 7d 0a 0a 2f  return len;.}../
0ca0: 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74  *.** COMMAND:  t
0cb0: 65 73 74 2d 64 65 6c 74 61 2d 61 70 70 6c 79 0a  est-delta-apply.
0cc0: 2a 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 69  **.** Given an i
0cd0: 6e 70 75 74 20 66 69 6c 65 73 20 61 6e 64 20 61  nput files and a
0ce0: 20 64 65 6c 74 61 2c 20 61 70 70 6c 79 20 74 68   delta, apply th
0cf0: 65 20 64 65 6c 74 61 20 74 6f 20 74 68 65 20 69  e delta to the i
0d00: 6e 70 75 74 20 66 69 6c 65 0a 2a 2a 20 61 6e 64  nput file.** and
0d10: 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75 6c   write the resul
0d20: 74 2e 0a 2a 2f 0a 76 6f 69 64 20 64 65 6c 74 61  t..*/.void delta
0d30: 5f 61 70 70 6c 79 5f 63 6d 64 28 76 6f 69 64 29  _apply_cmd(void)
0d40: 7b 0a 20 20 42 6c 6f 62 20 6f 72 69 67 2c 20 74  {.  Blob orig, t
0d50: 61 72 67 65 74 2c 20 64 65 6c 74 61 3b 0a 20 20  arget, delta;.  
0d60: 69 66 28 20 67 2e 61 72 67 63 21 3d 35 20 29 7b  if( g.argc!=5 ){
0d70: 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64  .    fprintf(std
0d80: 65 72 72 2c 22 55 73 61 67 65 3a 20 25 73 20 25  err,"Usage: %s %
0d90: 73 20 4f 52 49 47 49 4e 20 44 45 4c 54 41 20 54  s ORIGIN DELTA T
0da0: 41 52 47 45 54 5c 6e 22 2c 20 67 2e 61 72 67 76  ARGET\n", g.argv
0db0: 5b 30 5d 2c 20 67 2e 61 72 67 76 5b 31 5d 29 3b  [0], g.argv[1]);
0dc0: 0a 20 20 20 20 65 78 69 74 28 31 29 3b 0a 20 20  .    exit(1);.  
0dd0: 7d 0a 20 20 69 66 28 20 62 6c 6f 62 5f 72 65 61  }.  if( blob_rea
0de0: 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 6f 72 69  d_from_file(&ori
0df0: 67 2c 20 67 2e 61 72 67 76 5b 32 5d 29 3c 30 20  g, g.argv[2])<0 
0e00: 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73  ){.    fprintf(s
0e10: 74 64 65 72 72 2c 22 63 61 6e 6e 6f 74 20 72 65  tderr,"cannot re
0e20: 61 64 20 25 73 5c 6e 22 2c 20 67 2e 61 72 67 76  ad %s\n", g.argv
0e30: 5b 32 5d 29 3b 0a 20 20 20 20 65 78 69 74 28 31  [2]);.    exit(1
0e40: 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 62 6c 6f  );.  }.  if( blo
0e50: 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65  b_read_from_file
0e60: 28 26 64 65 6c 74 61 2c 20 67 2e 61 72 67 76 5b  (&delta, g.argv[
0e70: 33 5d 29 3c 30 20 29 7b 0a 20 20 20 20 66 70 72  3])<0 ){.    fpr
0e80: 69 6e 74 66 28 73 74 64 65 72 72 2c 22 63 61 6e  intf(stderr,"can
0e90: 6e 6f 74 20 72 65 61 64 20 25 73 5c 6e 22 2c 20  not read %s\n", 
0ea0: 67 2e 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20  g.argv[3]);.    
0eb0: 65 78 69 74 28 31 29 3b 0a 20 20 7d 0a 20 20 62  exit(1);.  }.  b
0ec0: 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 70 6c 79 28  lob_delta_apply(
0ed0: 26 6f 72 69 67 2c 20 26 64 65 6c 74 61 2c 20 26  &orig, &delta, &
0ee0: 74 61 72 67 65 74 29 3b 0a 20 20 69 66 28 20 62  target);.  if( b
0ef0: 6c 6f 62 5f 77 72 69 74 65 5f 74 6f 5f 66 69 6c  lob_write_to_fil
0f00: 65 28 26 74 61 72 67 65 74 2c 20 67 2e 61 72 67  e(&target, g.arg
0f10: 76 5b 34 5d 29 3c 62 6c 6f 62 5f 73 69 7a 65 28  v[4])<blob_size(
0f20: 26 74 61 72 67 65 74 29 20 29 7b 0a 20 20 20 20  &target) ){.    
0f30: 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 22  fprintf(stderr,"
0f40: 63 61 6e 6e 6f 74 20 77 72 69 74 65 20 25 73 5c  cannot write %s\
0f50: 6e 22 2c 20 67 2e 61 72 67 76 5b 34 5d 29 3b 0a  n", g.argv[4]);.
0f60: 20 20 20 20 65 78 69 74 28 31 29 3b 0a 20 20 7d      exit(1);.  }
0f70: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 6f  .  blob_reset(&o
0f80: 72 69 67 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73  rig);.  blob_res
0f90: 65 74 28 26 74 61 72 67 65 74 29 3b 0a 20 20 62  et(&target);.  b
0fa0: 6c 6f 62 5f 72 65 73 65 74 28 26 64 65 6c 74 61  lob_reset(&delta
0fb0: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d  );.}../*.** COMM
0fc0: 41 4e 44 3a 20 20 74 65 73 74 2d 64 65 6c 74 61  AND:  test-delta
0fd0: 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 77 6f 20  .**.** Read two 
0fe0: 66 69 6c 65 73 20 6e 61 6d 65 64 20 6f 6e 20 74  files named on t
0ff0: 68 65 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e  he command-line.
1000: 20 20 43 72 65 61 74 65 20 61 6e 64 20 61 70 70    Create and app
1010: 6c 79 20 64 65 6c 74 61 73 0a 2a 2a 20 67 6f 69  ly deltas.** goi
1020: 6e 67 20 69 6e 20 62 6f 74 68 20 64 69 72 65 63  ng in both direc
1030: 74 69 6f 6e 73 2e 20 20 56 65 72 69 66 79 20 74  tions.  Verify t
1040: 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  hat the original
1050: 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a 20 63 6f   files are.** co
1060: 72 72 65 63 74 6c 79 20 72 65 63 6f 76 65 72 65  rrectly recovere
1070: 64 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6d 64 5f 74  d..*/.void cmd_t
1080: 65 73 74 5f 64 65 6c 74 61 28 76 6f 69 64 29 7b  est_delta(void){
1090: 0a 20 20 42 6c 6f 62 20 66 31 2c 20 66 32 3b 20  .  Blob f1, f2; 
10a0: 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20      /* Original 
10b0: 66 69 6c 65 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a  file content */.
10c0: 20 20 42 6c 6f 62 20 64 31 32 2c 20 64 32 31 3b    Blob d12, d21;
10d0: 20 20 20 2f 2a 20 44 65 6c 74 61 73 20 66 72 6f     /* Deltas fro
10e0: 6d 20 66 31 2d 3e 66 32 20 61 6e 64 20 66 32 2d  m f1->f2 and f2-
10f0: 3e 66 31 20 2a 2f 0a 20 20 42 6c 6f 62 20 61 31  >f1 */.  Blob a1
1100: 2c 20 61 32 3b 20 20 20 20 20 2f 2a 20 52 65 63  , a2;     /* Rec
1110: 6f 76 65 72 65 64 20 66 69 6c 65 20 63 6f 6e 74  overed file cont
1120: 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20 67 2e 61  ent */.  if( g.a
1130: 72 67 63 21 3d 34 20 29 20 75 73 61 67 65 28 22  rgc!=4 ) usage("
1140: 46 49 4c 45 31 20 46 49 4c 45 32 22 29 3b 0a 20  FILE1 FILE2");. 
1150: 20 62 6c 6f 62 5f 72 65 61 64 5f 66 72 6f 6d 5f   blob_read_from_
1160: 66 69 6c 65 28 26 66 31 2c 20 67 2e 61 72 67 76  file(&f1, g.argv
1170: 5b 32 5d 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 61  [2]);.  blob_rea
1180: 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 66 32 2c  d_from_file(&f2,
1190: 20 67 2e 61 72 67 76 5b 33 5d 29 3b 0a 20 20 62   g.argv[3]);.  b
11a0: 6c 6f 62 5f 64 65 6c 74 61 5f 63 72 65 61 74 65  lob_delta_create
11b0: 28 26 66 31 2c 20 26 66 32 2c 20 26 64 31 32 29  (&f1, &f2, &d12)
11c0: 3b 0a 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 63  ;.  blob_delta_c
11d0: 72 65 61 74 65 28 26 66 32 2c 20 26 66 31 2c 20  reate(&f2, &f1, 
11e0: 26 64 32 31 29 3b 0a 20 20 62 6c 6f 62 5f 64 65  &d21);.  blob_de
11f0: 6c 74 61 5f 61 70 70 6c 79 28 26 66 31 2c 20 26  lta_apply(&f1, &
1200: 64 31 32 2c 20 26 61 32 29 3b 0a 20 20 62 6c 6f  d12, &a2);.  blo
1210: 62 5f 64 65 6c 74 61 5f 61 70 70 6c 79 28 26 66  b_delta_apply(&f
1220: 32 2c 20 26 64 32 31 2c 20 26 61 31 29 3b 0a 20  2, &d21, &a1);. 
1230: 20 69 66 28 20 62 6c 6f 62 5f 63 6f 6d 70 61 72   if( blob_compar
1240: 65 28 26 66 31 2c 26 61 31 29 20 7c 7c 20 62 6c  e(&f1,&a1) || bl
1250: 6f 62 5f 63 6f 6d 70 61 72 65 28 26 66 32 2c 20  ob_compare(&f2, 
1260: 26 61 32 29 20 29 7b 0a 20 20 20 20 66 6f 73 73  &a2) ){.    foss
1270: 69 6c 5f 70 61 6e 69 63 28 22 64 65 6c 74 61 20  il_panic("delta 
1280: 74 65 73 74 20 66 61 69 6c 65 64 22 29 3b 0a 20  test failed");. 
1290: 20 7d 0a 20 20 70 72 69 6e 74 66 28 22 6f 6b 5c   }.  printf("ok\
12a0: 6e 22 29 3b 0a 7d 0a                             n");.}.