Commit message: Add appheader tests; fix buffer overflow in main_get_appheader
CVE: CVE-2014-9765
CWE: CWE-119
Vulnerability description: Buffer overflow in the main_get_appheader function in xdelta3-main.h in xdelta3 before 3.0.9 allows remote attackers to execute arbitrary code via a crafted input file.

Project: xdelta-devel

test_iopt_flush_instructions (xd3_stream *stream, int ignore)
{
  int ret, i;
  usize_t tpos = 0;
  usize_t delta_size, recon_size;
  xd3_config config;
  uint8_t target[TESTBUFSIZE];
  uint8_t delta[TESTBUFSIZE];
  uint8_t recon[TESTBUFSIZE];

  xd3_free_stream (stream);
  xd3_init_config (& config, 0);

  config.smatch_cfg    = XD3_SMATCH_SOFT;
  config.smatcher_soft.large_look    = 16;
  config.smatcher_soft.large_step    = 16;
  config.smatcher_soft.small_look    = 4;
  config.smatcher_soft.small_chain   = 128;
  config.smatcher_soft.small_lchain  = 16;
  config.smatcher_soft.max_lazy      = 8;
  config.smatcher_soft.long_enough   = 128;

  if ((ret = xd3_config_stream (stream, & config))) { return ret; }

  for (i = 1; i < 250; i++)
    {
      target[tpos++] = i;
      target[tpos++] = i+1;
      target[tpos++] = i+2;
      target[tpos++] = i+3;
      target[tpos++] = 0;
    }
  for (i = 1; i < 253; i++)
    {
      target[tpos++] = i;
    }

  if ((ret = xd3_encode_stream (stream, target, tpos,
				    delta, & delta_size, sizeof (delta))))
    {
      return ret;
    }

  xd3_free_stream(stream);
  if ((ret = xd3_config_stream (stream, & config))) { return ret; }

  if ((ret = xd3_decode_stream (stream, delta, delta_size,
				recon, & recon_size, sizeof (recon))))
    {
      return ret;
    }

  CHECK(tpos == recon_size);
  CHECK(memcmp(target, recon, recon_size) == 0);

  return 0;
}

Project: xdelta-devel

main_write_output (xd3_stream* stream, main_file *ofile)
{
  int ret;

  if (option_no_output)
    {
      return 0;
    }

  if (stream->avail_out > 0 &&
      (ret = main_file_write (ofile, stream->next_out,
			      stream->avail_out, "write failed")))
    {
      return ret;
    }

  return 0;
}

Project: xdelta-devel

main_file_cleanup (main_file *xfile)
{
  XD3_ASSERT (xfile != NULL);

  if (main_file_isopen (xfile))
    {
      main_file_close (xfile);
    }

  if (xfile->snprintf_buf != NULL)
    {
      main_free(xfile->snprintf_buf);
      xfile->snprintf_buf = NULL;
    }

  if (xfile->filename_copy != NULL)
    {
      main_free(xfile->filename_copy);
      xfile->filename_copy = NULL;
    }
}

Project: xdelta-devel

main_help (void)
{
  main_version();

  /* Note: update wiki when command-line features change */
  XPR(NTR "usage: xdelta3 [command/options] [input [output]]\n");
  XPR(NTR "make patch:\n");
  XPR(NTR "\n");
  XPR(NTR "  xdelta3.exe -e -s old_file new_file delta_file\n");
  XPR(NTR "\n");
  XPR(NTR "apply patch:\n");
  XPR(NTR "\n");
  XPR(NTR "  xdelta3.exe -d -s old_file delta_file decoded_new_file\n");
  XPR(NTR "\n");
  XPR(NTR "special command names:\n");
  XPR(NTR "    config      prints xdelta3 configuration\n");
  XPR(NTR "    decode      decompress the input\n");
  XPR(NTR "    encode      compress the input%s\n",
     XD3_ENCODER ? "" : " [Not compiled]");
#if REGRESSION_TEST
  XPR(NTR "    test        run the builtin tests\n");
#endif
#if VCDIFF_TOOLS
  XPR(NTR "special commands for VCDIFF inputs:\n");
  XPR(NTR "    printdelta  print information about the entire delta\n");
  XPR(NTR "    printhdr    print information about the first window\n");
  XPR(NTR "    printhdrs   print information about all windows\n");
  XPR(NTR "    recode      encode with new application/secondary settings\n");
  XPR(NTR "    merge       merge VCDIFF inputs (see below)\n");
#endif
  XPR(NTR "merge patches:\n");
  XPR(NTR "\n");
  XPR(NTR "  xdelta3 merge -m 1.vcdiff -m 2.vcdiff 3.vcdiff merged.vcdiff\n");
  XPR(NTR "\n");
  XPR(NTR "standard options:\n");
  XPR(NTR "   -0 .. -9     compression level\n");
  XPR(NTR "   -c           use stdout\n");
  XPR(NTR "   -d           decompress\n");
  XPR(NTR "   -e           compress%s\n",
     XD3_ENCODER ? "" : " [Not compiled]");
  XPR(NTR "   -f           force (overwrite, ignore trailing garbage)\n");
#if EXTERNAL_COMPRESSION
  XPR(NTR "   -F           force the external-compression subprocess\n");
#endif
  XPR(NTR "   -h           show help\n");
  XPR(NTR "   -q           be quiet\n");
  XPR(NTR "   -v           be verbose (max 2)\n");
  XPR(NTR "   -V           show version\n");

  XPR(NTR "memory options:\n");
  XPR(NTR "   -B bytes     source window size\n");
  XPR(NTR "   -W bytes     input window size\n");
  XPR(NTR "   -P size      compression duplicates window\n");
  XPR(NTR "   -I size      instruction buffer size (0 = unlimited)\n");

  XPR(NTR "compression options:\n");
  XPR(NTR "   -s source    source file to copy from (if any)\n");
  XPR(NTR "   -S [djw|fgk] enable/disable secondary compression\n");
  XPR(NTR "   -N           disable small string-matching compression\n");
  XPR(NTR "   -D           disable external decompression (encode/decode)\n");
  XPR(NTR "   -R           disable external recompression (decode)\n");
  XPR(NTR "   -n           disable checksum (encode/decode)\n");
  XPR(NTR "   -C           soft config (encode, undocumented)\n");
  XPR(NTR "   -A [apphead] disable/provide application header (encode)\n");
  XPR(NTR "   -J           disable output (check/compute only)\n");
  XPR(NTR "   -T           use alternate code table (test)\n");
  XPR(NTR "   -m           arguments for \"merge\"\n");

  XPR(NTR "the XDELTA environment variable may contain extra args:\n");
  XPR(NTR "   XDELTA=\"-s source-x.y.tar.gz\" \\\n");
  XPR(NTR "   tar --use-compress-program=xdelta3 \\\n");
  XPR(NTR "       -cf target-x.z.tar.gz.vcdiff target-x.y\n");
  return EXIT_FAILURE;
}

Project: xdelta-devel

test_copy_to (const char *from, const char *to)
{
  char buf[TESTBUFSIZE];
  int ret;

  snprintf_func (buf, TESTBUFSIZE, "cp -f %s %s", from, to);

  if ((ret = system (buf)) != 0)
    {
      return XD3_INTERNAL;
    }

  return 0;
}

Project: xdelta-devel

main_print_vcdiff_file (main_file *xfile, main_file *file, const char *type)
{
  int ret;  /* Used by above macros */
  if (file->filename)
    {
      VC(UT "XDELTA filename (%s):     %s\n", type,
	 file->filename)VE;
    }
  if (file->compressor)
    {
      VC(UT "XDELTA ext comp (%s):     %s\n", type,
	 file->compressor->recomp_cmdname)VE;
    }
  return 0;
}

Project: xdelta-devel

test_recode_command2 (xd3_stream *stream, int has_source,
		      int variant, int change)
{
  int has_adler32 = (variant & 0x1) != 0;
  int has_apphead = (variant & 0x2) != 0;
  int has_secondary = (variant & 0x4) != 0;

  int change_adler32 = (change & 0x1) != 0;
  int change_apphead = (change & 0x2) != 0;
  int change_secondary = (change & 0x4) != 0;

  int recoded_adler32 = change_adler32 ? !has_adler32 : has_adler32;
  int recoded_apphead = change_apphead ? !has_apphead : has_apphead;
  int recoded_secondary = change_secondary ? !has_secondary : has_secondary;

  char ecmd[TESTBUFSIZE], recmd[TESTBUFSIZE], dcmd[TESTBUFSIZE];
  xoff_t tsize, ssize;
  int ret;

  test_setup ();

  if ((ret = test_make_inputs (stream, has_source ? & ssize : NULL, & tsize)))
    {
      return ret;
    }

  /* First encode */
  snprintf_func (ecmd, TESTBUFSIZE, "%s %s -f %s %s %s %s %s %s %s",
	    program_name, test_softcfg_str,
	    has_adler32 ? "" : "-n ",
	    has_apphead ? "-A=encode_apphead " : "-A= ",
	    has_secondary ? "-S djw " : "-S none ",
	    has_source ? "-s " : "",
	    has_source ? TEST_SOURCE_FILE : "",
	    TEST_TARGET_FILE,
	    TEST_DELTA_FILE);

  if ((ret = system (ecmd)) != 0)
    {
      XPR(NT "encode command: %s\n", ecmd);
      stream->msg = "encode cmd failed";
      return XD3_INTERNAL;
    }

  /* Now recode */
  snprintf_func (recmd, TESTBUFSIZE,
	    "%s recode %s -f %s %s %s %s %s", program_name, test_softcfg_str,
	    recoded_adler32 ? "" : "-n ",
	    !change_apphead ? "" :
	        (recoded_apphead ? "-A=recode_apphead " : "-A= "),
	    recoded_secondary ? "-S djw " : "-S none ",
	    TEST_DELTA_FILE,
	    TEST_COPY_FILE);

  if ((ret = system (recmd)) != 0)
    {
      XPR(NT "recode command: %s\n", recmd);
      stream->msg = "recode cmd failed";
      return XD3_INTERNAL;
    }

  /* Check recode changes. */

  if ((ret = check_vcdiff_header (stream,
				  TEST_COPY_FILE,
				  "VCDIFF window indicator",
				  "VCD_SOURCE",
				  has_source))) { return ret; }

  if ((ret = check_vcdiff_header (stream,
				  TEST_COPY_FILE,
				  "VCDIFF header indicator",
				  "VCD_SECONDARY",
				  recoded_secondary))) { return ret; }

  if ((ret = check_vcdiff_header (stream,
				  TEST_COPY_FILE,
				  "VCDIFF window indicator",
				  "VCD_ADLER32",
				  /* Recode can't generate an adler32
				   * checksum, it can only preserve it or
				   * remove it. */
				  has_adler32 && recoded_adler32)))
    {
      return ret;
    }

  if (!change_apphead)
    {
      if ((ret = check_vcdiff_header (stream,
				      TEST_COPY_FILE,
				      "VCDIFF header indicator",
				      "VCD_APPHEADER",
				      has_apphead)))
	{
	  return ret;
	}
      if ((ret = check_vcdiff_header (stream,
				      TEST_COPY_FILE,
				      "VCDIFF application header",
				      "encode_apphead",
				      has_apphead)))
	{
	  return ret;
	}
    }
  else
    {
      if ((ret = check_vcdiff_header (stream,
				      TEST_COPY_FILE,
				      "VCDIFF header indicator",
				      "VCD_APPHEADER",
				      recoded_apphead)))
	{
	  return ret;
	}
      if (recoded_apphead &&
	  (ret = check_vcdiff_header (stream,
				      TEST_COPY_FILE,
				      "VCDIFF application header",
				      "recode_apphead",
				      1)))
	{
	  return ret;
	}
    }

  /* Now decode */
  snprintf_func (dcmd, TESTBUFSIZE, "%s -fd %s %s %s %s ", program_name,
	    has_source ? "-s " : "",
	    has_source ? TEST_SOURCE_FILE : "",
	    TEST_COPY_FILE,
	    TEST_RECON_FILE);

  if ((ret = system (dcmd)) != 0)
    {
      XPR(NT "decode command: %s\n", dcmd);
      stream->msg = "decode cmd failed";
      return XD3_INTERNAL;
    }

  /* Now compare. */
  if ((ret = test_compare_files (TEST_TARGET_FILE, TEST_RECON_FILE)))
    {
      return ret;
    }

  return 0;
}

Project: xdelta-devel

mt_exp_rand (uint32_t mean, uint32_t max_value)
{
  double mean_d = mean;
  double erand  = log (1.0 / (mt_random (&static_mtrand) /
			      (double)UINT32_MAX));
  uint32_t x = (uint32_t) (mean_d * erand + 0.5);

  return min (x, max_value);
}

Project: xdelta-devel

main_recompress_output (main_file *ofile)
{
  pid_t recomp_id;  /* One subproc. */
  int   pipefd[2];  /* One pipe. */
  int   output_fd = -1;
  int   ret;
  const main_extcomp *recomp = ofile->compressor;

  pipefd[0] = pipefd[1] = -1;

  if (pipe (pipefd))
    {
      XPR(NT "pipe failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

  if ((recomp_id = fork ()) < 0)
    {
      XPR(NT "fork failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

  /* The child runs the recompression process: */
  if (recomp_id == 0)
    {
      if (option_verbose > 2)
	{
	  XPR(NT "external recompression pid %d\n", getpid ());
	}

      /* Setup pipes: write to the output file, read from the pipe. */
      if (dup2 (XFNO (ofile), STDOUT_FILENO) < 0 ||
	  dup2 (pipefd[PIPE_READ_FD], STDIN_FILENO) < 0 ||
	  close (pipefd[PIPE_READ_FD]) ||
	  close (pipefd[PIPE_WRITE_FD]) ||
	  execlp (recomp->recomp_cmdname, recomp->recomp_cmdname,
		  recomp->recomp_options,
		  option_force2 ? "-f" : NULL,
		  NULL))
	{
	  XPR(NT "child process %s failed to execute: %s\n",
	      recomp->recomp_cmdname, xd3_mainerror (get_errno ()));
	}

      _exit (127);
    }

  XD3_ASSERT(num_subprocs < MAX_SUBPROCS);
  ext_subprocs[num_subprocs++] = recomp_id;

  /* The parent closes both pipes after duplicating the output-fd for
   * writing to the compression pipe. */
  output_fd = dup (pipefd[PIPE_WRITE_FD]);

  if (output_fd < 0 ||
      main_file_close (ofile) ||
      close (pipefd[PIPE_READ_FD]) ||
      close (pipefd[PIPE_WRITE_FD]))
    {
      XPR(NT "close failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

#if XD3_STDIO
  /* Note: fdopen() acquires the fd, closes it when finished. */
  if ((ofile->file = fdopen (output_fd, "w")) == NULL)
    {
      XPR(NT "fdopen failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

#elif XD3_POSIX
  ofile->file = output_fd;
#endif

  /* Now the output file will be compressed. */
  return 0;

 pipe_cleanup:
  close (output_fd);
  close (pipefd[PIPE_READ_FD]);
  close (pipefd[PIPE_WRITE_FD]);
  return ret;
}

Project: xdelta-devel

test_read_integer_error (xd3_stream *stream, usize_t trunto, const char *msg)
{
  uint64_t eval = 1ULL << 34;
  uint32_t rval;
  xd3_output *buf = NULL;
  const uint8_t *max;
  const uint8_t *inp;
  int ret;

  buf = xd3_alloc_output (stream, buf);

  if ((ret = xd3_emit_uint64_t (stream, & buf, eval)))
    {
      goto fail;
    }

 again:

  inp = buf->base;
  max = buf->base + buf->next - trunto;

  if ((ret = xd3_read_uint32_t (stream, & inp, max, & rval)) !=
      XD3_INVALID_INPUT ||
      !MSG_IS (msg))
    {
      ret = XD3_INTERNAL;
    }
  else if (trunto && trunto < buf->next)
    {
      trunto += 1;
      goto again;
    }
  else
    {
      ret = 0;
    }

 fail:
  xd3_free_output (stream, buf);
  return ret;
}

Project: xdelta-devel

main_file_write (main_file *ofile, uint8_t *buf, usize_t size, const char *msg)
{
  int ret = 0;

#if XD3_STDIO
  usize_t result;

  result = fwrite (buf, 1, size, ofile->file);

  if (result != size) { ret = get_errno (); }

#elif XD3_POSIX
  ret = xd3_posix_io (ofile->file, buf, size, (xd3_posix_func*) &write, NULL);

#elif XD3_WIN32
  ret = xd3_win32_io (ofile->file, buf, size, 0, NULL);

#endif

  if (ret)
    {
      XPR(NT "%s: %s: %s\n", msg, ofile->filename, xd3_mainerror (ret));
    }
  else
    {
      if (option_verbose > 5) { XPR(NT "write %s: %u bytes\n",
				    ofile->filename, size); }
      ofile->nwrite += size;
    }

  return ret;
}

Project: xdelta-devel

static int do_fail (xd3_stream *stream, const char *buf)
{
  int ret;
  ret = system (buf);
  if (! WIFEXITED (ret) || WEXITSTATUS (ret) != 1)
    {
      stream->msg = "command should have not succeeded";
      XPR(NT "command was %s\n", buf);
      return XD3_INTERNAL;
    }
  return 0;
}

Project: xdelta-devel

test_address_cache (xd3_stream *stream, int unused)
{
  int ret;
  usize_t i;
  usize_t offset;
  usize_t *addrs;
  uint8_t *big_buf, *buf_max;
  const uint8_t *buf;
  xd3_output *outp;
  uint8_t *modes;
  int mode_counts[16];

  stream->acache.s_near = stream->code_table_desc->near_modes;
  stream->acache.s_same = stream->code_table_desc->same_modes;

  if ((ret = xd3_encode_init_partial (stream))) { return ret; }

  addrs = (usize_t*) xd3_alloc (stream, sizeof (usize_t), ADDR_CACHE_ROUNDS);
  modes = (uint8_t*) xd3_alloc (stream, sizeof (uint8_t), ADDR_CACHE_ROUNDS);

  memset (mode_counts, 0, sizeof (mode_counts));
  memset (modes, 0, ADDR_CACHE_ROUNDS);

  addrs[0] = 0;

  mt_init (& static_mtrand, 0x9f73f7fc);

  /* First pass: encode addresses */
  xd3_init_cache (& stream->acache);

  for (offset = 1; offset < ADDR_CACHE_ROUNDS; offset += 1)
    {
      double p;
      usize_t addr;
      usize_t prev_i;
      usize_t nearby;

      p         = (mt_random (&static_mtrand) / (double)USIZE_T_MAX);
      prev_i    = mt_random (&static_mtrand) % offset;
      nearby    = (mt_random (&static_mtrand) % 256) % offset;
      nearby    = max (1U, nearby);

      if (p < 0.1)      { addr = addrs[offset-nearby]; }
      else if (p < 0.4) { addr = min (addrs[prev_i] + nearby, offset-1); }
      else              { addr = prev_i; }

      if ((ret = xd3_encode_address (stream, addr, offset, & modes[offset]))) { return ret; }

      addrs[offset] = addr;
      mode_counts[modes[offset]] += 1;
    }

  /* Copy addresses into a contiguous buffer. */
  big_buf = (uint8_t*) xd3_alloc (stream, xd3_sizeof_output (ADDR_HEAD (stream)), 1);

  for (offset = 0, outp = ADDR_HEAD (stream); outp != NULL; offset += outp->next, outp = outp->next_page)
    {
      memcpy (big_buf + offset, outp->base, outp->next);
    }

  buf_max = big_buf + offset;
  buf     = big_buf;

  /* Second pass: decode addresses */
  xd3_init_cache (& stream->acache);

  for (offset = 1; offset < ADDR_CACHE_ROUNDS; offset += 1)
    {
      uint32_t addr;

      if ((ret = xd3_decode_address (stream, offset, modes[offset], & buf, buf_max, & addr))) { return ret; }

      if (addr != addrs[offset])
	{
	  stream->msg = "incorrect decoded address";
	  return XD3_INTERNAL;
	}
    }

  /* Check that every byte, mode was used. */
  if (buf != buf_max)
    {
      stream->msg = "address bytes not used";
      return XD3_INTERNAL;
    }

  for (i = 0; i < (2 + stream->acache.s_same + stream->acache.s_near); i += 1)
    {
      if (mode_counts[i] == 0)
	{
	  stream->msg = "address mode not used";
	  return XD3_INTERNAL;
	}
    }

  xd3_free (stream, modes);
  xd3_free (stream, addrs);
  xd3_free (stream, big_buf);

  return 0;
}

Project: xdelta-devel

main_file_open (main_file *xfile, const char* name, int mode)
{
  int ret = 0;

  xfile->mode = mode;

  XD3_ASSERT (name != NULL);
  XD3_ASSERT (! main_file_isopen (xfile));
  if (name[0] == 0)
    {
      XPR(NT "invalid file name: empty string\n");
      return XD3_INVALID;
    }

#if XD3_STDIO
  xfile->file = fopen (name, XOPEN_STDIO);

  ret = (xfile->file == NULL) ? get_errno () : 0;

#elif XD3_POSIX
  /* TODO: Should retry this call if interrupted, similar to read/write */
  if ((ret = open (name, XOPEN_POSIX, XOPEN_MODE)) < 0)
    {
      ret = get_errno ();
    }
  else
    {
      xfile->file = ret;
      ret = 0;
    }

#elif XD3_WIN32
  xfile->file = CreateFile(name,
			   (mode == XO_READ) ? GENERIC_READ : GENERIC_WRITE,
			   FILE_SHARE_READ,
			   NULL,
			   (mode == XO_READ) ?
			   OPEN_EXISTING :
			   (option_force ? CREATE_ALWAYS : CREATE_NEW),
			   FILE_ATTRIBUTE_NORMAL,
			   NULL);
  if (xfile->file == INVALID_HANDLE_VALUE)
    {
      ret = get_errno ();
    }
#endif
  if (ret) { XF_ERROR ("open", name, ret); }
  else     { xfile->realname = name; xfile->nread = 0; }
  return ret;
}

Project: xdelta-devel

main_waitpid_check(pid_t pid)
{
  int status;
  int ret = 0;

  if (waitpid (pid, & status, 0) < 0)
    {
      ret = get_errno ();
      XPR(NT "external compression [pid %d] wait: %s\n",
	  pid, xd3_mainerror (ret));
    }
  else if (! WIFEXITED (status))
    {
      // SIGPIPE will be delivered to the child process whenever it
      // writes data after this process closes the pipe, 
      // happens if xdelta does not require access to the entire 
      // source file.  Considered normal.
      if (! WIFSIGNALED (status) || WTERMSIG (status) != SIGPIPE) 
	{
	  ret = ECHILD;
	  XPR(NT "external compression [pid %d] signal %d\n", pid, 
	      WIFSIGNALED (status) ? WTERMSIG (status) : WSTOPSIG (status));
	}
      else if (option_verbose)
	{
	  XPR(NT "external compression sigpipe\n");
	}
    }
  else if (WEXITSTATUS (status) != 0)
    {
      ret = ECHILD;
      if (option_verbose > 1)
	{
	  /* Presumably, the error was printed by the subprocess. */
	  XPR(NT "external compression [pid %d] exit %d\n",
	      pid, WEXITSTATUS (status));
	}
    }

  return ret;
}

Project: xdelta-devel

setup_environment (int argc,
		   char **argv,
		   int *argc_out,
		   char ***argv_out,
		   char ***argv_free,
		   char **env_free)
{
  int n, i, i0;
  char *p, *v = getenv("XDELTA");
  if (v == NULL) {
    (*argc_out) = argc;
    (*argv_out) = argv;
    (*argv_free) = NULL;
    (*env_free) = NULL;
    return;
  }

  (*env_free) = (char*) main_malloc((usize_t) strlen(v) + 1);
  strcpy(*env_free, v);

  /* Space needed for extra args, at least # of spaces */
  n = argc + 1;
  for (p = *env_free; *p != 0; ) {
    if (*p++ == ' ') {
      n++;
    }
  }

  (*argv_free) = (char**) main_malloc(sizeof(char*) * (n + 1));
  (*argv_out) = (*argv_free);
  (*argv_out)[0] = argv[0];
  (*argv_out)[n] = NULL;

  i = 1;
  for (p = *env_free; *p != 0; ) {
    (*argv_out)[i++] = p;
    while (*p != ' ' && *p != 0) {
      p++;
    }
    while (*p == ' ') {
      *p++ = 0;
    }
  }

  for (i0 = 1; i0 < argc; i0++) {
    (*argv_out)[i++] = argv[i0];
  }

  /* Counting spaces is an upper bound, argv stays NULL terminated. */
  (*argc_out) = i;
  while (i <= n) {
    (*argv_out)[i++] = NULL;
  }
}

Project: xdelta-devel

test_compress_text (xd3_stream  *stream,
		    uint8_t     *encoded,
		    usize_t     *encoded_size)
{
  int ret;
  xd3_config cfg;
  int oflags = stream->flags;
  int flags = stream->flags | XD3_FLUSH;

  xd3_free_stream (stream);
  xd3_init_config (& cfg, flags);

  /* This configuration is fixed so that the "expected non-error" the counts in
   * decompress_single_bit_errors are too.  See test_coftcfg_str. */
  cfg.smatch_cfg = XD3_SMATCH_SOFT;
  cfg.smatcher_soft.name = "test";
  cfg.smatcher_soft.large_look = 64; /* no source, not used */
  cfg.smatcher_soft.large_step = 64; /* no source, not used */
  cfg.smatcher_soft.small_look = 4;
  cfg.smatcher_soft.small_chain = 128;
  cfg.smatcher_soft.small_lchain = 16;
  cfg.smatcher_soft.max_lazy = 8;
  cfg.smatcher_soft.long_enough = 128;

  xd3_config_stream (stream, & cfg);

  (*encoded_size) = 0;

  xd3_set_appheader (stream, test_apphead,
		     (usize_t) strlen ((char*) test_apphead));

  if ((ret = xd3_encode_stream (stream, test_text, sizeof (test_text),
				encoded, encoded_size, 4*sizeof (test_text)))) { goto fail; }

  if ((ret = xd3_close_stream (stream))) { goto fail; }

 fail:
  xd3_free_stream (stream);
  xd3_init_config (& cfg, oflags);
  xd3_config_stream (stream, & cfg);
  return ret;
}

Project: xdelta-devel

sec_dist_func2 (xd3_stream *stream, xd3_output *data)
{
  int i, ret;
  for (i = 0; i < ALPHABET_SIZE; i += 1)
    {
      if ((ret = xd3_emit_byte (stream, & data, i%(ALPHABET_SIZE/2)))) { return ret; }
    }
  return 0;
}

Project: xdelta-devel

sec_dist_func10 (xd3_stream *stream, xd3_output *data)
{
  int i, j, ret;
  for (i = 0; i < ALPHABET_SIZE; i += 1)
    {
      for (j = 0; j <= (i*i); j += 1)
	{
	  if ((ret = xd3_emit_byte (stream, & data, i))) { return ret; }
	}
    }
  return 0;
}

Project: xdelta-devel

test_streaming (xd3_stream *in_stream, uint8_t *encbuf, uint8_t *decbuf, uint8_t *delbuf, usize_t megs)
{
  xd3_stream estream, dstream;
  int ret;
  usize_t i, delsize, decsize;
  xd3_config cfg;
  xd3_init_config (& cfg, in_stream->flags);
  cfg.flags |= XD3_COMPLEVEL_6;

  if ((ret = xd3_config_stream (& estream, & cfg)) ||
      (ret = xd3_config_stream (& dstream, & cfg)))
    {
      goto fail;
    }

  for (i = 0; i < megs; i += 1)
    {
      ((usize_t*) encbuf)[0] = i;

      if ((i % 200) == 199) { DOT (); }

      if ((ret = xd3_process_stream (1, & estream, xd3_encode_input, 0,
				     encbuf, 1 << 20,
				     delbuf, & delsize, 1 << 20)))
	{
	  in_stream->msg = estream.msg;
	  goto fail;
	}

      if ((ret = xd3_process_stream (0, & dstream, xd3_decode_input, 0,
				     delbuf, delsize,
				     decbuf, & decsize, 1 << 20)))
	{
	  in_stream->msg = dstream.msg;
	  goto fail;
	}

      if (decsize != 1 << 20 ||
	  memcmp (encbuf, decbuf, 1 << 20) != 0)
	{
	  in_stream->msg = "wrong result";
	  ret = XD3_INTERNAL;
	  goto fail;
	}
    }

  if ((ret = xd3_close_stream (& estream)) ||
      (ret = xd3_close_stream (& dstream)))
    {
      goto fail;
    }

 fail:
  xd3_free_stream (& estream);
  xd3_free_stream (& dstream);
  return ret;
}

Project: xdelta-devel

sec_dist_func7 (xd3_stream *stream, xd3_output *data)
{
  int i, ret, x;
  for (i = 0; i < ALPHABET_SIZE*200; i += 1)
    {
      x = mt_random (&static_mtrand) % ALPHABET_SIZE;
      if ((ret = xd3_emit_byte (stream, & data, x))) { return ret; }
    }
  return 0;
}

Project: xdelta-devel

get_millisecs_now (void)
{
#ifndef _WIN32
  struct timeval tv;

  gettimeofday (& tv, NULL);

  return (tv.tv_sec) * 1000L + (tv.tv_usec) / 1000;
#else
  SYSTEMTIME st;
  FILETIME ft;
  __int64 *pi = (__int64*)&ft;
  GetLocalTime(&st);
  SystemTimeToFileTime(&st, &ft);
  return (long)((*pi) / 10000);
#endif
}

Project: xdelta-devel

main_file_init (main_file *xfile)
{
  memset (xfile, 0, sizeof (*xfile));

#if XD3_POSIX
  xfile->file = -1;
#endif
#if XD3_WIN32
  xfile->file = INVALID_HANDLE_VALUE;
#endif
}

Project: xdelta-devel

test_cleanup (void)
{
#if 1
  test_unlink (TEST_TARGET_FILE);
  test_unlink (TEST_SOURCE_FILE);
  test_unlink (TEST_DELTA_FILE);
  test_unlink (TEST_RECON_FILE);
  test_unlink (TEST_RECON2_FILE);
  test_unlink (TEST_COPY_FILE);
  test_unlink (TEST_NOPERM_FILE);
#endif
}

Project: xdelta-devel

sec_dist_func1 (xd3_stream *stream, xd3_output *data)
{
  int i, ret;
  for (i = 0; i < 100; i += 1)
    {
      if ((ret = xd3_emit_byte (stream, & data, 13))) { return ret; }
    }
  return 0;
}

Project: xdelta-devel

main_print_window (xd3_stream* stream, main_file *xfile)
{
  int ret;
  usize_t size = 0;

  VC(UT "  Offset Code Type1 Size1  @Addr1 + Type2 Size2 @Addr2\n")VE;

  while (stream->inst_sect.buf < stream->inst_sect.buf_max)
    {
      usize_t code = stream->inst_sect.buf[0];
      const uint8_t *addr_before = stream->addr_sect.buf;
      const uint8_t *inst_before = stream->inst_sect.buf;
      usize_t addr_bytes;
      usize_t inst_bytes;
      usize_t size_before = size;

      if ((ret = xd3_decode_instruction (stream)))
	{
	  XPR(NT "instruction decode error at %"Q"u: %s\n",
	      stream->dec_winstart + size, stream->msg);
	  return ret;
	}

      addr_bytes = (usize_t)(stream->addr_sect.buf - addr_before);
      inst_bytes = (usize_t)(stream->inst_sect.buf - inst_before);

      VC(UT "  %06"Q"u %03u  %s %6u", stream->dec_winstart + size,
	 option_print_cpymode ? code : 0,
	 xd3_rtype_to_string ((xd3_rtype) stream->dec_current1.type,
			      option_print_cpymode),
	 stream->dec_current1.size)VE;

      if (stream->dec_current1.type != XD3_NOOP)
	{
	  if (stream->dec_current1.type >= XD3_CPY)
	    {
	      if (stream->dec_current1.addr >= stream->dec_cpylen)
		{
		  VC(UT " T@%-6u",
		     stream->dec_current1.addr - stream->dec_cpylen)VE;
		}
	      else
		{
		  VC(UT " S@%-6"Q"u",
		     stream->dec_cpyoff + stream->dec_current1.addr)VE;
		}
	    }
	  else
	    {
	      VC(UT "        ")VE;
	    }

	  size += stream->dec_current1.size;
	}

      if (stream->dec_current2.type != XD3_NOOP)
	{
	  VC(UT "  %s %6u",
	     xd3_rtype_to_string ((xd3_rtype) stream->dec_current2.type,
				  option_print_cpymode),
	     stream->dec_current2.size)VE;

	  if (stream->dec_current2.type >= XD3_CPY)
	    {
	      if (stream->dec_current2.addr >= stream->dec_cpylen)
		{
		  VC(UT " T@%-6u",
		     stream->dec_current2.addr - stream->dec_cpylen)VE;
		}
	      else
		{
		  VC(UT " S@%-6"Q"u",
		     stream->dec_cpyoff + stream->dec_current2.addr)VE;
		}
	    }

	  size += stream->dec_current2.size;
	}

      VC(UT "\n")VE;

      if (option_verbose &&
	  addr_bytes + inst_bytes >= (size - size_before) &&
	  (stream->dec_current1.type >= XD3_CPY ||
	   stream->dec_current2.type >= XD3_CPY))
	{
	  VC(UT "  %06"Q"u (inefficiency) %u encoded as %u bytes\n",
	     stream->dec_winstart + size_before,
	     size - size_before,
	     addr_bytes + inst_bytes)VE;
	}
    }

  if (stream->dec_tgtlen != size && (stream->flags & XD3_SKIP_WINDOW) == 0)
    {
      XPR(NT "target window size inconsistency");
      return XD3_INTERNAL;
    }

  if (stream->dec_position != stream->dec_maxpos)
    {
      XPR(NT "target window position inconsistency");
      return XD3_INTERNAL;
    }

  if (stream->addr_sect.buf != stream->addr_sect.buf_max)
    {
      XPR(NT "address section inconsistency");
      return XD3_INTERNAL;
    }

  return 0;
}

Project: xdelta-devel

main_pipe_copier (uint8_t     *pipe_buf,
		  usize_t      pipe_bufsize,
		  size_t       nread,
		  main_file   *ifile,
		  int          outfd)
{
  int ret;
  xoff_t skipped = 0;

  /* Prevent SIGPIPE signals, allow EPIPE return values instead.  This
   * is safe to comment-out, except that the -F flag will not work
   * properly (the parent would need to treat WTERMSIG(status) ==
   * SIGPIPE). */
  struct sigaction sa;
  sa.sa_handler = SIG_IGN;
  sigaction (SIGPIPE, &sa, NULL);

  for (;;)
    {
      /* force_drain will be set when option_force and EPIPE cause us
       * to skip data.  This is reset each time through the loop, so
       * the break condition below works. */
      int force_drain = 0;
      if (nread > 0 && (ret = main_pipe_write (outfd, pipe_buf, nread)))
	{
	  if (ret == EPIPE)
	    {
	      /* This causes the loop to continue reading until nread
	       * == 0. */
	      skipped += nread;
	      force_drain = 1;
	    }
	  else
	    {
	      XPR(NT "pipe write failed: %s\n", xd3_mainerror (ret));
	      return ret;
	    }
	}

      if (nread < pipe_bufsize && !force_drain)
	{
	  break;
	}

      if ((ret = main_file_read (ifile, pipe_buf, pipe_bufsize,
				 & nread, "pipe read failed")) < 0)
	{
	  return ret;
	}
    }

  if (option_verbose && skipped != 0)
    {
      XPR(NT "skipping %"Q"u bytes in %s\n",
	  skipped, ifile->filename);
    }
  return 0;
}

Project: xdelta-devel

main_atoux (const char* arg, xoff_t *xo, xoff_t low,
	    xoff_t high, char which)
{
  xoff_t x;
  int ret;

  if ((ret = main_strtoxoff (arg, & x, which))) { return ret; }

  if (x < low)
    {
      XPR(NT "-%c: minimum value: %"Q"u\n", which, low);
      return EXIT_FAILURE;
    }
  if (high != 0 && x > high)
    {
      XPR(NT "-%c: maximum value: %"Q"u\n", which, high);
      return EXIT_FAILURE;
    }
  (*xo) = x;
  return 0;
}

Project: xdelta-devel

main_read_primary_input (main_file   *file,
			 uint8_t     *buf,
			 size_t       size,
			 size_t      *nread)
{
#if EXTERNAL_COMPRESSION
  if (option_decompress_inputs && file->flags & RD_FIRST)
    {
      file->flags &= ~RD_FIRST;
      return main_secondary_decompress_check (file, buf, size, nread);
    }
#endif

  return main_file_read (file, buf, size, nread, "input read failed");
}

Project: xdelta-devel

test_usize_t_overflow (xd3_stream *stream, int unused)
{
  if (USIZE_T_OVERFLOW (USIZE_T_MAX, 0)) { goto fail; }
  if (USIZE_T_OVERFLOW (0, USIZE_T_MAX)) { goto fail; }
  if (USIZE_T_OVERFLOW (USIZE_T_MAX / 2, USIZE_T_MAX / 2)) { goto fail; }
  if (USIZE_T_OVERFLOW (USIZE_T_MAX / 2, USIZE_T_MAX / 2 + 1)) { goto fail; }

  if (! USIZE_T_OVERFLOW (USIZE_T_MAX, 1)) { goto fail; }
  if (! USIZE_T_OVERFLOW (1, USIZE_T_MAX)) { goto fail; }
  if (! USIZE_T_OVERFLOW (USIZE_T_MAX / 2 + 1, USIZE_T_MAX / 2 + 1)) { goto fail; }

  return 0;

 fail:
  stream->msg = "incorrect overflow computation";
  return XD3_INTERNAL;
}

Project: xdelta-devel

test_file_size (const char* file, xoff_t *size)
{
  struct stat sbuf;
  int ret;
  (*size) = 0;

  if (stat (file, & sbuf) < 0)
    {
      ret = get_errno ();
      XPR(NT "stat failed: %s: %s\n", file, strerror (ret));
      return ret;
    }

  if (! S_ISREG (sbuf.st_mode))
    {
      ret = XD3_INTERNAL;
      XPR(NT "not a regular file: %s: %s\n", file, strerror (ret));
      return ret;
    }

  (*size) = sbuf.st_size;
  return 0;
}

Project: xdelta-devel

test_compressed_stream_overflow (xd3_stream *stream, int ignore)
{
  int ret;
  int i;
  uint8_t *buf;

  if ((buf = (uint8_t*) malloc (TWO_MEGS_AND_DELTA)) == NULL) { return ENOMEM; }

  memset (buf, 0, TWO_MEGS_AND_DELTA);
  for (i = 0; i < (2 << 20); i += 256)
    {
      int j;
      int off = mt_random(& static_mtrand) % 10;
      for (j = 0; j < 256; j++)
	{
	  buf[i + j] = j + off;
	}
    }

  /* Test overflow of a 32-bit file offset. */
  if (SIZEOF_XOFF_T == 4)
    {
      ret = test_streaming (stream, buf, buf + (1 << 20), buf + (2 << 20), (1 << 12) + 1);

      if (ret == XD3_INVALID_INPUT && MSG_IS ("decoder file offset overflow"))
	{
	  ret = 0;
	}
      else
	{
          XPR(NT XD3_LIB_ERRMSG (stream, ret));
	  stream->msg = "expected overflow condition";
	  ret = XD3_INTERNAL;
	  goto fail;
	}
    }

  /* Test transfer of exactly 32bits worth of data. */
  if ((ret = test_streaming (stream,
			     buf,
			     buf + (1 << 20),
			     buf + (2 << 20),
			     1 << 12)))
    {
      goto fail;
    }
 fail:
  free (buf);
  return ret;
}

Project: xdelta-devel

main_file_exists (main_file *xfile)
{
  struct stat sbuf;
  return stat (xfile->filename, & sbuf) == 0 && S_ISREG (sbuf.st_mode);
}

Project: xdelta-devel

test_appheader (xd3_stream *stream, int ignore)
{
  int i;
  int ret;
  char buf[TESTBUFSIZE];
  char bogus[TESTBUFSIZE];
  xoff_t ssize, tsize;
  test_setup ();

  if ((ret = test_make_inputs (stream, &ssize, &tsize))) { return ret; }

  snprintf_func (buf, TESTBUFSIZE, "%s -q -f -e -s %s %s %s", program_name,
		 TEST_SOURCE_FILE, TEST_TARGET_FILE, TEST_DELTA_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  if ((ret = test_copy_to (program_name, TEST_RECON2_FILE))) { return ret; }

  snprintf_func (buf, TESTBUFSIZE, "chmod 0700 %s", TEST_RECON2_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  if ((ret = test_save_copy (TEST_TARGET_FILE))) { return ret; }
  if ((ret = test_copy_to (TEST_SOURCE_FILE, TEST_TARGET_FILE))) { return ret; }

  if ((ret = test_compare_files (TEST_TARGET_FILE, TEST_COPY_FILE)) == 0)
    {
      return XD3_INVALID;  // I.e., files are different!
    }

  // Test that the target file is restored.
  snprintf_func (buf, TESTBUFSIZE, "(cd /tmp && %s -q -f -d %s)",
		 TEST_RECON2_FILE,
		 TEST_DELTA_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  if ((ret = test_compare_files (TEST_TARGET_FILE, TEST_COPY_FILE)) != 0)
    {
      return ret;
    }

  // Test a malicious string w/ entries > 4 in the appheader by having
  // the encoder write it:
  for (i = 0; i < TESTBUFSIZE / 4; ++i)
    {
      bogus[2*i] = 'G';
      bogus[2*i+1] = '/';
    }
  bogus[TESTBUFSIZE/2-1] = 0;

  snprintf_func (buf, TESTBUFSIZE, 
		 "%s -q -f -A=%s -e -s %s %s %s", program_name, bogus,
		 TEST_SOURCE_FILE, TEST_TARGET_FILE, TEST_DELTA_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }
  // Then read it:
  snprintf_func (buf, TESTBUFSIZE, "(cd /tmp && %s -q -f -d %s)",
		 TEST_RECON2_FILE,
		 TEST_DELTA_FILE);
  if ((ret = do_cmd (stream, buf)) == 0) 
    { 
      return XD3_INVALID;  // Impossible
    }
  if (!WIFEXITED(ret))
    {
      return XD3_INVALID;  // Must have crashed!
    }

  return 0;
}

Project: xdelta-devel

static void DOT (void) { XPR(NTR "."); }

Project: xdelta-devel

main_malloc (size_t size)
{
  void *r = main_malloc1 (size);
  if (r) { IF_DEBUG (main_mallocs += 1); }
  return r;
}

Project: xdelta-devel

test_make_inputs (xd3_stream *stream, xoff_t *ss_out, xoff_t *ts_out)
{
  usize_t ts = (mt_random (&static_mtrand) % TEST_FILE_MEAN) +
    TEST_FILE_MEAN / 2;
  usize_t ss = (mt_random (&static_mtrand) % TEST_FILE_MEAN) +
    TEST_FILE_MEAN / 2;
  uint8_t *buf = (uint8_t*) malloc (ts + ss), *sbuf = buf, *tbuf = buf + ss;
  usize_t sadd = 0, sadd_max = (usize_t)(ss * TEST_ADD_RATIO);
  FILE  *tf = NULL, *sf = NULL;
  usize_t i, j;
  int ret;

  if (buf == NULL) { return ENOMEM; }

  if ((tf = fopen (TEST_TARGET_FILE, "w")) == NULL ||
      (ss_out != NULL && (sf = fopen (TEST_SOURCE_FILE, "w")) == NULL))
    {
      stream->msg = "write failed";
      ret = get_errno ();
      goto failure;
    }

  if (ss_out != NULL)
    {
      for (i = 0; i < ss; )
	{
	  sbuf[i++] = (uint8_t) mt_random (&static_mtrand);
	}
    }

  /* Then modify the data to produce copies, everything not copied is
   * an add.  The following logic produces the TEST_ADD_RATIO.  The
   * variable SADD contains the number of adds so far, which should
   * not exceed SADD_MAX. */

  /* XPR(NT "ss = %u ts = %u\n", ss, ts); */
  for (i = 0; i < ts; )
    {
      usize_t left = ts - i;
      usize_t next = mt_exp_rand ((uint32_t) TEST_ADD_MEAN,
				  (uint32_t) TEST_ADD_MAX);
      usize_t add_left = sadd_max - sadd;
      double add_prob = (left == 0) ? 0 : (add_left / (double) left);
      int do_copy;

      next = min (left, next);
      do_copy = (next > add_left ||
		 (mt_random (&static_mtrand) / \
		  (double)USIZE_T_MAX) >= add_prob);

      if (ss_out == NULL)
	{
	  do_copy &= (i > 0);
	}
      else
	{
	  do_copy &= (ss - next) > 0;
	}

      if (do_copy)
	{
	  /* Copy */
	  size_t offset = mt_random (&static_mtrand) % ((ss_out == NULL) ?
							i :
							(ss - next));
	  /* XPR(NT "[%u] copy %u at %u ", i, next, offset); */

	  for (j = 0; j < next; j += 1)
	    {
	      char c = ((ss_out == NULL) ? tbuf : sbuf)[offset + j];
	      /* XPR(NT "%x%x", (c >> 4) & 0xf, c & 0xf); */
	      tbuf[i++] = c;
	    }
	  /* XPR(NT "\n"); */
	}
      else
	{
	  /* Add */
	  /* XPR(NT "[%u] add %u ", i, next); */
	  for (j = 0; j < next; j += 1)
	    {
	      char c = (char) mt_random (&static_mtrand);
	      /* XPR(NT "%x%x", (c >> 4) & 0xf, c & 0xf); */
	      tbuf[i++] = c;
	    }
	  /* XPR(NT "\n"); */
	  sadd += next;
	}
    }

  /* XPR(NT "sadd = %u max = %u\n", sadd, sadd_max); */

  if ((fwrite (tbuf, 1, ts, tf) != ts) ||
      (ss_out != NULL && (fwrite (sbuf, 1, ss, sf) != ss)))
    {
      stream->msg = "write failed";
      ret = get_errno ();
      goto failure;
    }

  if ((ret = fclose (tf)) || (ss_out != NULL && (ret = fclose (sf))))
    {
      stream->msg = "close failed";
      ret = get_errno ();
      goto failure;
    }

  if (ts_out) { (*ts_out) = ts; }
  if (ss_out) { (*ss_out) = ss; }

 failure:
  free (buf);
  return ret;
}

Project: xdelta-devel

main_print_func (xd3_stream* stream, main_file *xfile)
{
  int ret;

  if (option_no_output)
    {
      return 0;
    }

  if (xfile->snprintf_buf == NULL)
    {
      if ((xfile->snprintf_buf =
	   (uint8_t*)main_malloc(SNPRINTF_BUFSIZE)) == NULL)
	{
	  return ENOMEM;
	}
    }

  if (stream->dec_winstart == 0)
    {
      VC(UT "VCDIFF version:               0\n")VE;
      VC(UT "VCDIFF header size:           %d\n",
	 stream->dec_hdrsize)VE;
      VC(UT "VCDIFF header indicator:      ")VE;
      if ((stream->dec_hdr_ind & VCD_SECONDARY) != 0)
	VC(UT "VCD_SECONDARY ")VE;
      if ((stream->dec_hdr_ind & VCD_CODETABLE) != 0)
	VC(UT "VCD_CODETABLE ")VE;
      if ((stream->dec_hdr_ind & VCD_APPHEADER) != 0)
	VC(UT "VCD_APPHEADER ")VE;
      if (stream->dec_hdr_ind == 0)
	VC(UT "none")VE;
      VC(UT "\n")VE;

      IF_SEC(VC(UT "VCDIFF secondary compressor:  %s\n",
		stream->sec_type ? stream->sec_type->name : "none")VE);
      IF_NSEC(VC(UT "VCDIFF secondary compressor: unsupported\n")VE);

      if (stream->dec_hdr_ind & VCD_APPHEADER)
	{
	  uint8_t *apphead;
	  usize_t appheadsz;
	  ret = xd3_get_appheader (stream, & apphead, & appheadsz);

	  if (ret == 0 && appheadsz > 0)
	    {
	      int sq = option_quiet;
	      main_file i, o, s;
	      XD3_ASSERT (apphead != NULL);
	      VC(UT "VCDIFF application header:    ")VE;
	      if ((ret = main_file_write (xfile, apphead,
					  appheadsz, "print")) != 0)
		{ return ret; }
	      VC(UT "\n")VE;

	      main_file_init (& i);
	      main_file_init (& o);
	      main_file_init (& s);
	      option_quiet = 1;
	      main_get_appheader (stream, &i, & o, & s);
	      option_quiet = sq;
	      if ((ret = main_print_vcdiff_file (xfile, & o, "output")))
		{ return ret; }
	      if ((ret = main_print_vcdiff_file (xfile, & s, "source")))
		{ return ret; }
	      main_file_cleanup (& i);
	      main_file_cleanup (& o);
	      main_file_cleanup (& s);
	    }
	}
    }
  else
    {
      VC(UT "\n")VE;
    }

  VC(UT "VCDIFF window number:         %"Q"u\n", stream->current_window)VE;
  VC(UT "VCDIFF window indicator:      ")VE;
  if ((stream->dec_win_ind & VCD_SOURCE) != 0) VC(UT "VCD_SOURCE ")VE;
  if ((stream->dec_win_ind & VCD_TARGET) != 0) VC(UT "VCD_TARGET ")VE;
  if ((stream->dec_win_ind & VCD_ADLER32) != 0) VC(UT "VCD_ADLER32 ")VE;
  if (stream->dec_win_ind == 0) VC(UT "none")VE;
  VC(UT "\n")VE;

  if ((stream->dec_win_ind & VCD_ADLER32) != 0)
    {
      VC(UT "VCDIFF adler32 checksum:      %08X\n",
	 (usize_t)stream->dec_adler32)VE;
    }

  if (stream->dec_del_ind != 0)
    {
      VC(UT "VCDIFF delta indicator:       ")VE;
      if ((stream->dec_del_ind & VCD_DATACOMP) != 0) VC(UT "VCD_DATACOMP ")VE;
      if ((stream->dec_del_ind & VCD_INSTCOMP) != 0) VC(UT "VCD_INSTCOMP ")VE;
      if ((stream->dec_del_ind & VCD_ADDRCOMP) != 0) VC(UT "VCD_ADDRCOMP ")VE;
      if (stream->dec_del_ind == 0) VC(UT "none")VE;
      VC(UT "\n")VE;
    }

  if (stream->dec_winstart != 0)
    {
      VC(UT "VCDIFF window at offset:      %"Q"u\n", stream->dec_winstart)VE;
    }

  if (SRCORTGT (stream->dec_win_ind))
    {
      VC(UT "VCDIFF copy window length:    %u\n",
	 (usize_t)stream->dec_cpylen)VE;
      VC(UT "VCDIFF copy window offset:    %"Q"u\n",
	 stream->dec_cpyoff)VE;
    }

  VC(UT "VCDIFF delta encoding length: %u\n",
     (usize_t)stream->dec_enclen)VE;
  VC(UT "VCDIFF target window length:  %u\n",
     (usize_t)stream->dec_tgtlen)VE;

  VC(UT "VCDIFF data section length:   %u\n",
     (usize_t)stream->data_sect.size)VE;
  VC(UT "VCDIFF inst section length:   %u\n",
     (usize_t)stream->inst_sect.size)VE;
  VC(UT "VCDIFF addr section length:   %u\n",
     (usize_t)stream->addr_sect.size)VE;

  ret = 0;
  if ((stream->flags & XD3_JUST_HDR) != 0)
    {
      /* Print a header -- finished! */
      ret = PRINTHDR_SPECIAL;
    }
  else if ((stream->flags & XD3_SKIP_WINDOW) == 0)
    {
      ret = main_print_window (stream, xfile);
    }

  return ret;
}

Project: xdelta-devel

sec_dist_func8 (xd3_stream *stream, xd3_output *data)
{
  int i, ret;
  for (i = 0; i < ALPHABET_SIZE*5; i += 1)
    {
      if ((ret = xd3_emit_byte (stream, & data, 0))) { return ret; }
      if ((ret = xd3_emit_byte (stream, & data, 64))) { return ret; }
      if ((ret = xd3_emit_byte (stream, & data, 128))) { return ret; }
      if ((ret = xd3_emit_byte (stream, & data, 255))) { return ret; }
    }
  return 0;
}

Project: xdelta-devel

main_file_read (main_file  *ifile,
		uint8_t    *buf,
		size_t      size,
		size_t     *nread,
		const char *msg)
{
  int ret = 0;

#if XD3_STDIO
  size_t result;

  result = fread (buf, 1, size, ifile->file);

  if (result < size && ferror (ifile->file))
    {
      ret = get_errno ();
    }
  else
    {
      *nread = result;
    }

#elif XD3_POSIX
  ret = xd3_posix_io (ifile->file, buf, size, (xd3_posix_func*) &read, nread);
#elif XD3_WIN32
  ret = xd3_win32_io (ifile->file, buf, size, 1 /* is_read */, nread);
#endif

  if (ret)
    {
      XPR(NT "%s: %s: %s\n", msg, ifile->filename, xd3_mainerror (ret));
    }
  else
    {
      if (option_verbose > 4) { XPR(NT "read %s: %zu bytes\n",
				    ifile->filename, (*nread)); }
      ifile->nread += (*nread);
    }

  return ret;
}

Project: xdelta-devel

test_in_memory (xd3_stream *stream, int ignore)
{
  // test_text is 256 bytes
  uint8_t ibuf[sizeof(test_text)];
  uint8_t dbuf[sizeof(test_text)];
  uint8_t obuf[sizeof(test_text)];
  usize_t size = sizeof(test_text);
  usize_t dsize, osize;
  int r1, r2;
  int eflags = SECONDARY_DJW ? XD3_SEC_DJW : 0;

  memcpy(ibuf, test_text, size);
  memset(ibuf + 128, 0, 16);

  r1 = xd3_encode_memory(ibuf, size,
			 test_text, size,
			 dbuf, &dsize, size, eflags);

  r2 = xd3_decode_memory(dbuf, dsize,
			 test_text, size,
			 obuf, &osize, size, 0);

  if (r1 != 0 || r2 != 0 || dsize >= (size/2) || dsize < 1 ||
      osize != size) {
    stream->msg = "encode/decode size error";
    return XD3_INTERNAL;
  }

  if (memcmp(obuf, ibuf, size) != 0) {
    stream->msg = "encode/decode data error";
    return XD3_INTERNAL;
  }

  return 0;
}

Project: xdelta-devel

test_save_copy (const char *origname)
{
  return test_copy_to(origname, TEST_COPY_FILE);
}

Project: xdelta-devel

xd3_win32_io (HANDLE file, uint8_t *buf, size_t size,
	      int is_read, size_t *nread)
{
  int ret = 0;
  size_t nproc = 0;

  while (nproc < size)
    {
      DWORD nproc2 = 0;  /* hmm */
	  DWORD nremain = size - nproc;
      if ((is_read ?
	   ReadFile (file, buf + nproc, nremain, &nproc2, NULL) :
	   WriteFile (file, buf + nproc, nremain, &nproc2, NULL)) == 0)
	{
	  ret = get_errno();
	  if (ret != ERROR_HANDLE_EOF && ret != ERROR_BROKEN_PIPE)
	    {
	      return ret;
	    }
	  /* By falling through here, we'll break this loop in the
	   * read case in case of eof or broken pipe. */
	}

      nproc += nproc2;

      if (nread != NULL && nproc2 == 0) { break; }
    }
  if (nread != NULL) { (*nread) = nproc; }
  return 0;
}

Project: xdelta-devel

main_recode_func (xd3_stream* stream, main_file *ofile)
{
  int ret;
  xd3_source decode_source;

  XD3_ASSERT(stream->dec_state == DEC_FINISH);
  XD3_ASSERT(recode_stream->enc_state == ENC_INIT ||
	     recode_stream->enc_state == ENC_INPUT);

  // Copy partial decoder output to partial encoder inputs
  if ((ret = main_recode_copy (recode_stream,
			       DATA_HEAD(recode_stream),
			       &stream->data_sect)) ||
      (ret = main_recode_copy (recode_stream,
			       INST_HEAD(recode_stream),
			       &stream->inst_sect)) ||
      (ret = main_recode_copy (recode_stream,
			       ADDR_HEAD(recode_stream),
			       &stream->addr_sect)))
    {
      return ret;
    }

  // This jumps to xd3_emit_hdr()
  recode_stream->enc_state = ENC_FLUSH;
  recode_stream->avail_in = stream->dec_tgtlen;

  if (SRCORTGT (stream->dec_win_ind))
    {
      recode_stream->src = & decode_source;
      decode_source.srclen = stream->dec_cpylen;
      decode_source.srcbase = stream->dec_cpyoff;
    }

  if (option_use_checksum &&
      (stream->dec_win_ind & VCD_ADLER32) != 0)
    {
      recode_stream->flags |= XD3_ADLER32_RECODE;
      recode_stream->recode_adler32 = stream->dec_adler32;
    }

  if (option_use_appheader != 0 &&
      option_appheader != NULL)
    {
      xd3_set_appheader (recode_stream, option_appheader,
			 (usize_t) strlen ((char*) option_appheader));
    }
  else if (option_use_appheader != 0 &&
	   option_appheader == NULL)
    {
      if (stream->dec_appheader != NULL)
	{
	  xd3_set_appheader (recode_stream,
			     stream->dec_appheader, stream->dec_appheadsz);
	}
    }

  // Output loop
  for (;;)
    {
      switch((ret = xd3_encode_input (recode_stream)))
	{
	case XD3_INPUT: {
	  /* finished recoding one window */
	  stream->total_out = recode_stream->total_out;
	  return 0;
	}
	case XD3_OUTPUT: {
	  /* main_file_write below */
	  break;
	}
	case XD3_GOTHEADER:
	case XD3_WINSTART:
	case XD3_WINFINISH: {
	  /* ignore */
	  continue;
	}
	case XD3_GETSRCBLK:
	case 0: {
	    return XD3_INTERNAL;
	  }
	default:
	  return ret;
	}

      if ((ret = main_write_output (recode_stream, ofile)))
	{
	  return ret;
	}

      xd3_consume_output (recode_stream);
    }
}

Project: xdelta-devel

main_input (xd3_cmd     cmd,
	    main_file   *ifile,
	    main_file   *ofile,
	    main_file   *sfile)
{
  int        ret;
  xd3_stream stream;
  size_t     nread = 0;
  usize_t    winsize;
  int        stream_flags = 0;
  xd3_config config;
  xd3_source source;
  xoff_t     last_total_in = 0;
  xoff_t     last_total_out = 0;
  long       start_time;
  int        stdout_only = 0;
  int (*input_func) (xd3_stream*);
  int (*output_func) (xd3_stream*, main_file *);

  memset (& stream, 0, sizeof (stream));
  memset (& source, 0, sizeof (source));
  memset (& config, 0, sizeof (config));

  config.alloc = main_alloc;
  config.freef = main_free1;

  config.iopt_size = option_iopt_size;
  config.sprevsz = option_sprevsz;

  do_src_fifo = 0;

  start_time = get_millisecs_now ();

  if (option_use_checksum) { stream_flags |= XD3_ADLER32; }

  /* main_input setup. */
  switch ((int) cmd)
    {
#if VCDIFF_TOOLS
           if (1) { case CMD_PRINTHDR:   stream_flags |= XD3_JUST_HDR; }
      else if (1) { case CMD_PRINTHDRS:  stream_flags |= XD3_SKIP_WINDOW; }
      else        { case CMD_PRINTDELTA: stream_flags |= XD3_SKIP_EMIT; }
      ifile->flags |= RD_NONEXTERNAL;
      input_func    = xd3_decode_input;
      output_func   = main_print_func;
      stream_flags |= XD3_ADLER32_NOVER;
      stdout_only   = 1;
      break;

    case CMD_RECODE:
    case CMD_MERGE:
    case CMD_MERGE_ARG:
      /* No source will be read */
      stream_flags |= XD3_ADLER32_NOVER | XD3_SKIP_EMIT;
      ifile->flags |= RD_NONEXTERNAL;
      input_func = xd3_decode_input;

      if ((ret = main_init_recode_stream ()))
        {
	  return EXIT_FAILURE;
        }

      if (cmd == CMD_RECODE) { output_func = main_recode_func; }
      else                   { output_func = main_merge_func; }
      break;
#endif /* VCDIFF_TOOLS */

#if XD3_ENCODER
    case CMD_ENCODE:
      do_src_fifo = 1;
      input_func  = xd3_encode_input;
      output_func = main_write_output;

      if (option_no_compress)      { stream_flags |= XD3_NOCOMPRESS; }
      if (option_use_altcodetable) { stream_flags |= XD3_ALT_CODE_TABLE; }
      if (option_smatch_config)
	{
	  const char *s = option_smatch_config;
	  char *e;
	  int values[XD3_SOFTCFG_VARCNT];
	  int got;

	  config.smatch_cfg = XD3_SMATCH_SOFT;

	  for (got = 0; got < XD3_SOFTCFG_VARCNT; got += 1, s = e + 1)
	    {
	      values[got] = strtol (s, &e, 10);

	      if ((values[got] < 0) ||
		  (e == s) ||
		  (got < XD3_SOFTCFG_VARCNT-1 && *e == 0) ||
		  (got == XD3_SOFTCFG_VARCNT-1 && *e != 0))
		{
		  XPR(NT "invalid string match specifier (-C) %d: %s\n",
		      got, s);
		  return EXIT_FAILURE;
		}
	    }

	  config.smatcher_soft.large_look    = values[0];
	  config.smatcher_soft.large_step    = values[1];
	  config.smatcher_soft.small_look    = values[2];
	  config.smatcher_soft.small_chain   = values[3];
	  config.smatcher_soft.small_lchain  = values[4];
	  config.smatcher_soft.max_lazy      = values[5];
	  config.smatcher_soft.long_enough   = values[6];
	}
      else
	{
	  if (option_verbose > 2)
	    {
	      XPR(NT "compression level: %d\n", option_level);
	    }
	  if (option_level == 0)
	    {
	      stream_flags |= XD3_NOCOMPRESS;
	      config.smatch_cfg = XD3_SMATCH_FASTEST;
	    }
	  else if (option_level == 1)
	    { config.smatch_cfg = XD3_SMATCH_FASTEST; }
	  else if (option_level == 2)
	    { config.smatch_cfg = XD3_SMATCH_FASTER; }
	  else if (option_level <= 5)
	    { config.smatch_cfg = XD3_SMATCH_FAST; }
	  else if (option_level == 6)
	    { config.smatch_cfg = XD3_SMATCH_DEFAULT; }
	  else
	    { config.smatch_cfg = XD3_SMATCH_SLOW; }
	}
      break;
#endif
    case CMD_DECODE:
      if (option_use_checksum == 0) { stream_flags |= XD3_ADLER32_NOVER; }
      ifile->flags |= RD_NONEXTERNAL;
      input_func    = xd3_decode_input;
      output_func   = main_write_output;
      break;
    default:
      XPR(NT "internal error\n");
      return EXIT_FAILURE;
    }

  main_bsize = winsize = main_get_winsize (ifile);

  if ((main_bdata = (uint8_t*) main_bufalloc (winsize)) == NULL)
    {
      return EXIT_FAILURE;
    }

  config.winsize = winsize;
  config.getblk = main_getblk_func;
  config.flags = stream_flags;

  if ((ret = main_set_secondary_flags (&config)) ||
      (ret = xd3_config_stream (& stream, & config)))
    {
      XPR(NT XD3_LIB_ERRMSG (& stream, ret));
      return EXIT_FAILURE;
    }

#if VCDIFF_TOOLS
  if ((cmd == CMD_MERGE || cmd == CMD_MERGE_ARG) &&
      (ret = xd3_whole_state_init (& stream)))
    {
      XPR(NT XD3_LIB_ERRMSG (& stream, ret));
      return EXIT_FAILURE;
    }
#endif

  if (cmd != CMD_DECODE)
    {
      /* When not decoding, set source now.  The decoder delays this
       * step until XD3_GOTHEADER. */
      if (sfile && sfile->filename != NULL)
	{
	  if ((ret = main_set_source (& stream, cmd, sfile, & source)))
	    {
	      return EXIT_FAILURE;
	    }

	  XD3_ASSERT(stream.src != NULL);
	}
    }

  if (cmd == CMD_PRINTHDR ||
      cmd == CMD_PRINTHDRS ||
      cmd == CMD_PRINTDELTA ||
      cmd == CMD_RECODE)
    {
      if (sfile->filename == NULL)
	{
	  allow_fake_source = 1;
	  sfile->filename = "<placeholder>";
	  main_set_source (& stream, cmd, sfile, & source);
	}
    }

  /* This times each window. */
  get_millisecs_since ();

  /* Main input loop. */
  do
    {
      xoff_t input_offset;
      xoff_t input_remain;
      usize_t try_read;

      input_offset = ifile->nread;

      input_remain = XOFF_T_MAX - input_offset;

      try_read = (usize_t) min ((xoff_t) config.winsize, input_remain);

      if ((ret = main_read_primary_input (ifile, main_bdata,
					  try_read, & nread)))
	{
	  return EXIT_FAILURE;
	}

      /* If we've reached EOF tell the stream to flush. */
      if (nread < try_read)
	{
	  stream.flags |= XD3_FLUSH;
	}

#if XD3_ENCODER
      /* After the first main_read_primary_input completes, we know
       * all the information needed to encode the application
       * header. */
      if (cmd == CMD_ENCODE &&
	  (ret = main_set_appheader (& stream, ifile, sfile)))
	{
	  return EXIT_FAILURE;
	}
#endif
      xd3_avail_input (& stream, main_bdata, nread);

      /* If we read zero bytes after encoding at least one window... */
      if (nread == 0 && stream.current_window > 0) {
	break;
      }

    again:
      ret = input_func (& stream);

      switch (ret)
	{
	case XD3_INPUT:
	  continue;

	case XD3_GOTHEADER:
	  {
	    XD3_ASSERT (stream.current_window == 0);

	    /* Need to process the appheader as soon as possible.  It may
	     * contain a suggested default filename/decompression routine for
	     * the ofile, and it may contain default/decompression routine for
	     * the sources. */
	    if (cmd == CMD_DECODE)
	      {
		/* May need to set the sfile->filename if none was given. */
		main_get_appheader (& stream, ifile, ofile, sfile);

		/* Now open the source file. */
		  if ((sfile->filename != NULL) &&
		      (ret = main_set_source (& stream, cmd, sfile, & source)))
		  {
		    return EXIT_FAILURE;
		  }
	      }
	  }
	/* FALLTHROUGH */
	case XD3_WINSTART:
	  {
	    /* e.g., set or unset XD3_SKIP_WINDOW. */
	    goto again;
	  }

	case XD3_OUTPUT:
	  {
	    /* Defer opening the output file until the stream produces its
	     * first output for both encoder and decoder, this way we
	     * delay long enough for the decoder to receive the
	     * application header.  (Or longer if there are skipped
	     * windows, but I can't think of any reason not to delay
	     * open.) */
	    if (ofile != NULL &&
		! main_file_isopen (ofile) &&
		(ret = main_open_output (& stream, ofile)) != 0)
	      {
		return EXIT_FAILURE;
	      }

	    if ((ret = output_func (& stream, ofile)) &&
		(ret != PRINTHDR_SPECIAL))
	      {
		return EXIT_FAILURE;
	      }

	    if (ret == PRINTHDR_SPECIAL)
	      {
		xd3_abort_stream (& stream);
		ret = EXIT_SUCCESS;
		goto done;
	      }

	    ret = 0;

	    xd3_consume_output (& stream);
	    goto again;
	  }

	case XD3_WINFINISH:
	  {
	    if (IS_ENCODE (cmd) || cmd == CMD_DECODE || cmd == CMD_RECODE)
	      {
		if (! option_quiet && IS_ENCODE (cmd) &&
		    main_file_isopen (sfile))
		  {
		    /* Warn when no source copies are found */
		    if (option_verbose && ! xd3_encoder_used_source (& stream))
		      {
			XPR(NT "warning: input window %"Q"u..%"Q"u has "
			    "no source copies\n",
			    stream.current_window * winsize,
			    (stream.current_window+1) * winsize);
			XD3_ASSERT (stream.src != NULL);
		      }

		    /* Limited i-buffer size affects source copies
		     * when the sourcewin is decided early. */
		    if (option_verbose > 1 &&
			stream.srcwin_decided_early &&
			stream.i_slots_used > stream.iopt_size)
		      {
			XPR(NT "warning: input position %"Q"u overflowed "
			    "instruction buffer, needed %u (vs. %u), "
			    "consider changing -I\n",
			    stream.current_window * winsize,
			    stream.i_slots_used, stream.iopt_size);
		      }
		  }

		if (option_verbose)
		  {
		    shortbuf rrateavg, wrateavg, tm;
		    shortbuf rdb, wdb;
		    shortbuf trdb, twdb;
		    shortbuf srcpos;
		    long millis = get_millisecs_since ();
		    usize_t this_read = (usize_t)(stream.total_in -
						  last_total_in);
		    usize_t this_write = (usize_t)(stream.total_out -
						   last_total_out);
		    last_total_in = stream.total_in;
		    last_total_out = stream.total_out;

		    if (option_verbose > 1)
		      {
			XPR(NT "%"Q"u: in %s (%s): out %s (%s): "
			    "total in %s: out %s: %s: srcpos %s\n",
			    stream.current_window,
			    main_format_bcnt (this_read, &rdb),
			    main_format_rate (this_read, millis, &rrateavg),
			    main_format_bcnt (this_write, &wdb),
			    main_format_rate (this_write, millis, &wrateavg),
			    main_format_bcnt (stream.total_in, &trdb),
			    main_format_bcnt (stream.total_out, &twdb),
			    main_format_millis (millis, &tm),
			    main_format_bcnt (sfile->source_position, &srcpos));
		      }
		    else
		      {
			XPR(NT "%"Q"u: in %s: out %s: total in %s: "
			    "out %s: %s\n",
 			    stream.current_window,
			    main_format_bcnt (this_read, &rdb),
			    main_format_bcnt (this_write, &wdb),
			    main_format_bcnt (stream.total_in, &trdb),
			    main_format_bcnt (stream.total_out, &twdb),
			    main_format_millis (millis, &tm));
		      }
		  }
	      }
	    goto again;
	  }

	default:
	  /* input_func() error */
	  XPR(NT XD3_LIB_ERRMSG (& stream, ret));
	  if (! option_quiet && ret == XD3_INVALID_INPUT)
	    {
	      XPR(NT "normally this indicates that the source file is incorrect\n");
	      XPR(NT "please verify the source file with sha1sum or equivalent\n");
	    }
	  return EXIT_FAILURE;
	}
    }
  while (nread == config.winsize);
done:
  /* Close the inputs. (ifile must be open, sfile may be open) */
  main_file_close (ifile);
  if (sfile != NULL)
    {
      main_file_close (sfile);
    }

#if VCDIFF_TOOLS
  if (cmd == CMD_MERGE &&
      (ret = main_merge_output (& stream, ofile)))
    {
      return EXIT_FAILURE;
    }

  if (cmd == CMD_MERGE_ARG)
    {
      xd3_swap_whole_state (& stream.whole_target,
			    & recode_stream->whole_target);
    }
#endif /* VCDIFF_TOOLS */

  /* If output file is not open yet because of delayed-open, it means
   * we never encountered a window in the delta, but it could have had
   * a VCDIFF header?  TODO: solve this elsewhere.  For now, it prints
   * "nothing to output" below, but the check doesn't happen in case
   * of option_no_output.  */
  if (! option_no_output && ofile != NULL)
    {
      if (!stdout_only && ! main_file_isopen (ofile))
	{
	  XPR(NT "nothing to output: %s\n", ifile->filename);
	  return EXIT_FAILURE;
	}

      /* Have to close the output before calling
       * main_external_compression_finish, or else it hangs. */
      if (main_file_close (ofile) != 0)
	{
	  return EXIT_FAILURE;
	}
    }

#if EXTERNAL_COMPRESSION
  if ((ret = main_external_compression_finish ()))
    {
      XPR(NT "external compression commands failed\n");
      return EXIT_FAILURE;
    }
#endif

  if ((ret = xd3_close_stream (& stream)))
    {
      XPR(NT XD3_LIB_ERRMSG (& stream, ret));
      return EXIT_FAILURE;
    }

#if XD3_ENCODER
  if (option_verbose > 1 && cmd == CMD_ENCODE)
    {
      XPR(NT "scanner configuration: %s\n", stream.smatcher.name);
      XPR(NT "target hash table size: %u\n", stream.small_hash.size);
      if (sfile != NULL && sfile->filename != NULL)
	{
	  XPR(NT "source hash table size: %u\n", stream.large_hash.size);
	}
    }

  if (option_verbose > 2 && cmd == CMD_ENCODE)
    {
      XPR(NT "source copies: %"Q"u (%"Q"u bytes)\n",
	  stream.n_scpy, stream.l_scpy);
      XPR(NT "target copies: %"Q"u (%"Q"u bytes)\n",
	  stream.n_tcpy, stream.l_tcpy);
      XPR(NT "adds: %"Q"u (%"Q"u bytes)\n", stream.n_add, stream.l_add);
      XPR(NT "runs: %"Q"u (%"Q"u bytes)\n", stream.n_run, stream.l_run);
    }
#endif

  xd3_free_stream (& stream);

  if (option_verbose)
    {
      shortbuf tm;
      long end_time = get_millisecs_now ();
      xoff_t nwrite = ofile != NULL ? ofile->nwrite : 0;

      XPR(NT "finished in %s; input %"Q"u output %"Q"u bytes (%0.2f%%)\n",
	  main_format_millis (end_time - start_time, &tm),
	  ifile->nread, nwrite, 100.0 * nwrite / ifile->nread);
    }

  return EXIT_SUCCESS;
}

Project: xdelta-devel

get_millisecs_since (void)
{
  static long last = 0;
  long now = get_millisecs_now();
  long diff = now - last;
  last = now;
  return diff;
}

Project: xdelta-devel

test_encode_decode_uint64_t (xd3_stream *stream, int unused)
{
  TEST_ENCODE_DECODE_INTEGER(uint64_t,1ULL,UINT64_MAX);
}

Project: xdelta-devel

main_version (void)
{
  /* $Format: "  XPR(NTR \"Xdelta version $Xdelta3Version$, Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, Joshua MacDonald\\n\");" $ */
  XPR(NTR "Xdelta version 3.0.8, Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Joshua MacDonald\n");
  XPR(NTR "Xdelta comes with ABSOLUTELY NO WARRANTY.\n");
  XPR(NTR "This is free software, and you are welcome to redistribute it\n");
  XPR(NTR "under certain conditions; see \"COPYING\" for details.\n");
  return EXIT_SUCCESS;
}

Project: xdelta-devel

main_recode_copy (xd3_stream* stream,
		  xd3_output* output,
		  xd3_desect* input)
{
  int ret;

  XD3_ASSERT(output != NULL);
  XD3_ASSERT(output->next_page == NULL);

  if ((ret = xd3_decode_allocate (recode_stream,
				  input->size,
				  &output->base,
				  &output->avail)))
    {
      XPR(NT XD3_LIB_ERRMSG (stream, ret));
      return ret;
    }

  memcpy (output->base,
	  /* Note: decoder advances buf, so get base of buffer with
	   * buf_max - size */
	  input->buf_max - input->size,
	  input->size);
  output->next = input->size;
  return 0;
}

Project: xdelta-devel

test_decode_integer_overflow (xd3_stream *stream, int unused)
{
  return test_read_integer_error (stream, 0, "overflow in read_intger");
}

Project: xdelta-devel

test_identical_behavior (xd3_stream *stream, int ignore)
{
#define IDB_TGTSZ 10000  /* Not a power of two b/c of hard-coded expectations below. */
#define IDB_BLKSZ 512
#define IDB_WINSZ 1000
#define IDB_DELSZ 1000
#define IDB_WINCNT (IDB_TGTSZ / IDB_WINSZ)

  int ret, i;
  uint8_t buf[IDB_TGTSZ];
  uint8_t del[IDB_DELSZ];
  uint8_t rec[IDB_TGTSZ];
  xd3_source source;
  int nextencwin = 0;
  int winstarts = 0, winfinishes = 0;
  usize_t delpos = 0, recsize;
  xd3_config config;
  memset(&source, 0, sizeof(source));

  for (i = 0; i < IDB_TGTSZ; i += 1)
    {
      buf[i] = (uint8_t) mt_random (&static_mtrand);
    }

  stream->winsize = IDB_WINSZ;

  source.blksize  = IDB_BLKSZ;
  source.name     = "";
  source.curblk   = NULL;
  source.curblkno = 0;

  if ((ret = xd3_set_source (stream, & source))) { goto fail; }

  /* Compute an delta between identical source and targets. */
  for (;;)
    {
      ret = xd3_encode_input (stream);

      if (ret == XD3_INPUT)
	{
	  xd3_avail_input (stream, buf + (IDB_WINSZ * nextencwin), IDB_WINSZ);
	  nextencwin += 1;
	  continue;
	}

      if (ret == XD3_GETSRCBLK)
	{
	  source.curblkno = source.getblkno;
	  source.onblk    = IDB_BLKSZ;
	  source.curblk   = buf + source.getblkno * IDB_BLKSZ;
	  continue;
	}

      if (ret == XD3_WINSTART)
	{
	  winstarts++;
	  continue;
	}
      if (ret == XD3_WINFINISH)
	{
	  winfinishes++;
	  if (winfinishes == IDB_WINCNT)
	    {
	      break;
	    }
	  continue;
	}

      if (ret != XD3_OUTPUT) { goto fail; }

      CHECK(delpos + stream->avail_out <= IDB_DELSZ);

      memcpy (del + delpos, stream->next_out, stream->avail_out);

      delpos += stream->avail_out;

      xd3_consume_output (stream);
    }

  CHECK(winfinishes == IDB_WINCNT);
  CHECK(winstarts == IDB_WINCNT);
  CHECK(nextencwin == IDB_WINCNT);

  /* Reset. */
  memset(&source, 0, sizeof(source));
  source.blksize  = IDB_TGTSZ;
  source.onblk    = IDB_TGTSZ;
  source.curblk   = buf;
  source.curblkno = 0;

  if ((ret = xd3_close_stream (stream))) { goto fail; }
  xd3_free_stream (stream);
  xd3_init_config (& config, 0);
  if ((ret = xd3_config_stream (stream, & config))) { goto fail; }
  if ((ret = xd3_set_source_and_size (stream, & source, IDB_TGTSZ))) { goto fail; }

  /* Decode. */
  if ((ret = xd3_decode_stream (stream, del, delpos, rec, & recsize, IDB_TGTSZ))) { goto fail; }

  /* Check result size and data. */
  if (recsize != IDB_TGTSZ) { stream->msg = "wrong size reconstruction"; goto fail; }
  if (memcmp (rec, buf, IDB_TGTSZ) != 0) { stream->msg = "wrong data reconstruction"; goto fail; }

  /* Check that there was one copy per window. */
  IF_DEBUG (if (stream->n_scpy != IDB_WINCNT ||
		stream->n_add != 0 ||
		stream->n_run != 0) { stream->msg = "wrong copy count"; goto fail; });

  /* Check that no checksums were computed because the initial match
     was presumed. */
  IF_DEBUG (if (stream->large_ckcnt != 0) { stream->msg = "wrong checksum behavior"; goto fail; });

  ret = 0;
 fail:
  return ret;
}

Project: xdelta-devel

test_decode_integer_end_of_input (xd3_stream *stream, int unused)
{
  return test_read_integer_error (stream, 1, "end-of-input in read_integer");
}

Project: xdelta-devel

test_random_numbers (xd3_stream *stream, int ignore)
{
  usize_t i;
  usize_t sum = 0;
  usize_t mean = 50;
  usize_t n_rounds = 1000000;
  double average, error;
  double allowed_error = 0.1;

  mt_init (& static_mtrand, 0x9f73f7fe);

  for (i = 0; i < n_rounds; i += 1)
    {
      sum += mt_exp_rand (mean, USIZE_T_MAX);
    }

  average = (double) sum / (double) n_rounds;
  error   = average - (double) mean;

  if (error < allowed_error && error > -allowed_error)
    {
      return 0;
    }

  /*XPR(NT "error is %f\n", error);*/
  stream->msg = "random distribution looks broken";
  return XD3_INTERNAL;
}

Project: xdelta-devel

int main (int argc, char **argv)
#endif
{
  static const char *flags =
    "0123456789cdefhnqvDFJNORTVs:m:B:C:E:I:L:O:M:P:W:A::S::";
  xd3_cmd cmd;
  main_file ifile;
  main_file ofile;
  main_file sfile;
  main_merge_list merge_order;
  main_merge *merge;
  int my_optind;
  const char *my_optarg;
  const char *my_optstr;
  const char *sfilename;
  int env_argc;
  char **env_argv;
  char **free_argv;  /* malloc() in setup_environment() */
  char *free_value;  /* malloc() in setup_environment() */
  int ret;

#ifdef _WIN32
  GetStartupInfo(&winStartupInfo);
  setvbuf(stderr, NULL, _IONBF, 0);  /* Do not buffer stderr */
#endif

  main_file_init (& ifile);
  main_file_init (& ofile);
  main_file_init (& sfile);
  main_merge_list_init (& merge_order);

  reset_defaults();

  free_argv = NULL;
  free_value = NULL;
  setup_environment(argc, argv, &env_argc, &env_argv,
		    &free_argv, &free_value);
  cmd = CMD_NONE;
  sfilename = NULL;
  my_optind = 1;
  argv = env_argv;
  argc = env_argc;
  program_name = env_argv[0];

 takearg:
  my_optarg = NULL;
  my_optstr = argv[my_optind];

  /* This doesn't use getopt() because it makes trouble for -P & python which
   * reenter main() and thus care about freeing all memory.  I never had much
   * trust for getopt anyway, it's too opaque.  This implements a fairly
   * standard non-long-option getopt with support for named operations (e.g.,
   * "xdelta3 [encode|decode|printhdr...] < in > out"). */
  if (my_optstr)
    {
      if (*my_optstr == '-')    { my_optstr += 1; }
      else if (cmd == CMD_NONE) { goto nonflag; }
      else                      { my_optstr = NULL; }
    }
  while (my_optstr)
    {
      const char *s;
      my_optarg = NULL;
      if ((ret = *my_optstr++) == 0) { my_optind += 1; goto takearg; }

      /* Option handling: first check for one ':' following the option in
       * flags, then check for two.  The syntax allows:
       *
       * 1. -Afoo                   defines optarg="foo"
       * 2. -A foo                  defines optarg="foo"
       * 3. -A ""                   defines optarg="" (allows empty-string)
       * 4. -A [EOA or -moreargs]   error (mandatory case)
       * 5. -A [EOA -moreargs]      defines optarg=NULL (optional case)
       * 6. -A=foo                  defines optarg="foo"
       * 7. -A=                     defines optarg="" (mandatory case)
       * 8. -A=                     defines optarg=NULL (optional case)
       *
       * See tests in test_command_line_arguments().
       */
      s = strchr (flags, ret);
      if (s && s[1] && s[1] == ':')
	{
	  int option = s[2] && s[2] == ':';

	  /* Case 1, set optarg to the remaining characters. */
	  my_optarg = my_optstr;
	  my_optstr = "";

	  /* Case 2-5 */
	  if (*my_optarg == 0)
	    {
	      /* Condition 4-5 */
	      int have_arg = (my_optind < (argc - 1) &&
			      *argv[my_optind+1] != '-');

	      if (! have_arg)
		{
		  if (! option)
		  {
		    /* Case 4 */
		    XPR(NT "-%c: requires an argument\n", ret);
		    ret = EXIT_FAILURE;
		    goto cleanup;
		  }
		  /* Case 5. */
		  my_optarg = NULL;
		}
	      else
		{
		  /* Case 2-3. */
		  my_optarg = argv[++my_optind];
		}
	    }
	  /* Case 6-8. */
	  else if (*my_optarg == '=')
	    {
	      /* Remove the = in all cases. */
	      my_optarg += 1;

	      if (option && *my_optarg == 0)
		{
		  /* Case 8. */
		  my_optarg = NULL;
		}
	    }
	}

      switch (ret)
	{
	/* case: if no '-' was found, maybe check for a command name. */
	nonflag:
	       if (strcmp (my_optstr, "decode") == 0) { cmd = CMD_DECODE; }
	  else if (strcmp (my_optstr, "encode") == 0)
	    {
#if XD3_ENCODER
	      cmd = CMD_ENCODE;
#else
	      XPR(NT "encoder support not compiled\n");
	      return EXIT_FAILURE;
#endif
	    }
	  else if (strcmp (my_optstr, "config") == 0) { cmd = CMD_CONFIG; }
#if REGRESSION_TEST
	  else if (strcmp (my_optstr, "test") == 0) { cmd = CMD_TEST; }
#endif
#if VCDIFF_TOOLS
	  else if (strcmp (my_optstr, "printhdr") == 0) { cmd = CMD_PRINTHDR; }
	  else if (strcmp (my_optstr, "printhdrs") == 0)
	    { cmd = CMD_PRINTHDRS; }
	  else if (strcmp (my_optstr, "printdelta") == 0)
	    { cmd = CMD_PRINTDELTA; }
	  else if (strcmp (my_optstr, "recode") == 0) { cmd = CMD_RECODE; }
	  else if (strcmp (my_optstr, "merge") == 0) { cmd = CMD_MERGE; }
#endif

	  /* If no option was found and still no command, let the default
	   * command be encode.  The remaining args are treated as
	   * filenames. */
	  if (cmd == CMD_NONE)
	    {
	      cmd = CMD_DEFAULT;
	      my_optstr = NULL;
	      break;
	    }
	  else
	    {
	      /* But if we find a command name, continue the getopt loop. */
	      my_optind += 1;
	      goto takearg;
	    }

	  /* gzip-like options */
	case '0': case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
	  option_level = ret - '0';
	  break;
	case 'f': option_force = 1; break;
	case 'F':
#if EXTERNAL_COMPRESSION
	  option_force2 = 1;
#else
	  XPR(NT "warning: -F option ignored, "
	      "external compression support was not compiled\n");
	  break;
#endif
	case 'v': option_verbose += 1; option_quiet = 0; break;
	case 'q': option_quiet = 1; option_verbose = 0; break;
	case 'c': option_stdout = 1; break;
	case 'd':
	  if (cmd == CMD_NONE) { cmd = CMD_DECODE; }
	  else { ret = main_help (); goto exit; }
	  break;
	case 'e':
#if XD3_ENCODER
	  if (cmd == CMD_NONE) { cmd = CMD_ENCODE; }
	  else { ret = main_help (); goto exit; }
	  break;
#else
	  XPR(NT "encoder support not compiled\n");
	  return EXIT_FAILURE;
#endif

	case 'n': option_use_checksum = 0; break;
	case 'N': option_no_compress = 1; break;
	case 'T': option_use_altcodetable = 1; break;
	case 'C': option_smatch_config = my_optarg; break;
	case 'J': option_no_output = 1; break;
	case 'S': if (my_optarg == NULL)
	    {
	      option_use_secondary = 1;
	      option_secondary = "none";
	    }
	  else
	    {
	      option_use_secondary = 1;
	      option_secondary = my_optarg;
	    }
	  break;
	case 'A': if (my_optarg == NULL) { option_use_appheader = 0; }
	          else { option_appheader = (uint8_t*) my_optarg; } break;
	case 'B': {
	  xoff_t bsize;
	  if ((ret = main_atoux (my_optarg, & bsize,
				 XD3_MINSRCWINSZ, XD3_MAXSRCWINSZ, 'B')))
	    {
	      goto exit;
	    }
	  option_srcwinsz = bsize;
	  break;
	}
	case 'I':
	  if ((ret = main_atou (my_optarg, & option_iopt_size, 0,
				0, 'I')))
	    {
	      goto exit;
	    }
	  break;
	case 'P':
	  if ((ret = main_atou (my_optarg, & option_sprevsz, 0,
				0, 'P')))
	    {
	      goto exit;
	    }
	  break;
	case 'W':
	  if ((ret = main_atou (my_optarg, & option_winsize, XD3_ALLOCSIZE,
				XD3_HARDMAXWINSIZE, 'W')))
	  {
	    goto exit;
	  }
	  break;
	case 'D':
#if EXTERNAL_COMPRESSION == 0
	  if (option_verbose > 0)
	    {
	      XPR(NT "warning: -D option ignored, "
		  "external compression support was not compiled\n");
	    }
#else
	  option_decompress_inputs  = 0;
#endif
	  break;
	case 'R':
#if EXTERNAL_COMPRESSION == 0
	  if (option_verbose > 0)
	    {
	      XPR(NT "warning: -R option ignored, "
		  "external compression support was not compiled\n");
	    }
#else
	  option_recompress_outputs = 0;
#endif
	  break;
	case 's':
	  if (sfilename != NULL)
	    {
	      XPR(NT "specify only one source file\n");
	      goto cleanup;
	    }

	  sfilename = my_optarg;
	  break;
	case 'm':
	  if ((merge = (main_merge*)
	       main_malloc (sizeof (main_merge))) == NULL)
	    {
	      goto cleanup;
	    }
	  main_merge_list_push_back (& merge_order, merge);
	  merge->filename = my_optarg;
	  break;
	case 'V':
	  ret = main_version (); goto exit;
	default:
	  ret = main_help (); goto exit;
	}
    }

  option_source_filename = sfilename;

  /* In case there were no arguments, set the default command. */
  if (cmd == CMD_NONE) { cmd = CMD_DEFAULT; }

  argc -= my_optind;
  argv += my_optind;

  /* There may be up to two more arguments. */
  if (argc > 2)
    {
      XPR(NT "too many filenames: %s ...\n", argv[2]);
      goto cleanup;
    }

  ifile.flags    = RD_FIRST | RD_MAININPUT;
  sfile.flags    = RD_FIRST;
  sfile.filename = option_source_filename;

  /* The infile takes the next argument, if there is one.  But if not, infile
   * is set to stdin. */
  if (argc > 0)
    {
      ifile.filename = argv[0];

      if ((ret = main_file_open (& ifile, ifile.filename, XO_READ)))
	{
	  goto cleanup;
	}
    }
  else
    {
      XSTDIN_XF (& ifile);
    }

  /* The ofile takes the following argument, if there is one.  But if not, it
   * is left NULL until the application header is processed.  It will be set
   * in main_open_output. */
  if (argc > 1)
    {
      /* Check for conflicting arguments. */
      if (option_stdout && ! option_quiet)
	{
	  XPR(NT "warning: -c option overrides output filename: %s\n",
	      argv[1]);
	}

      if (! option_stdout) { ofile.filename = argv[1]; }
    }

#if VCDIFF_TOOLS
  if (cmd == CMD_MERGE &&
      (ret = main_merge_arguments (&merge_order)))
    {
      goto cleanup;
    }
#endif /* VCDIFF_TOOLS */

  switch (cmd)
    {
    case CMD_PRINTHDR:
    case CMD_PRINTHDRS:
    case CMD_PRINTDELTA:
#if XD3_ENCODER
    case CMD_ENCODE:
    case CMD_RECODE:
    case CMD_MERGE:
#endif
    case CMD_DECODE:
      ret = main_input (cmd, & ifile, & ofile, & sfile);
      break;

#if REGRESSION_TEST
    case CMD_TEST:
      main_config ();
      ret = xd3_selftest ();
      break;
#endif

    case CMD_CONFIG:
      ret = main_config ();
      break;

    default:
      ret = main_help ();
      break;
    }

  if (0)
    {
    cleanup:
      ret = EXIT_FAILURE;
    exit:
      (void)0;
    }

#if EXTERNAL_COMPRESSION
  main_external_compression_cleanup ();
#endif

  main_file_cleanup (& ifile);
  main_file_cleanup (& ofile);
  main_file_cleanup (& sfile);

  while (! main_merge_list_empty (& merge_order))
    {
      merge = main_merge_list_pop_front (& merge_order);
      main_free (merge);
    }

  main_free (free_argv);
  main_free (free_value);

  main_cleanup ();

  fflush (stdout);
  fflush (stderr);
  return ret;
}

Project: xdelta-devel

main_cleanup (void)
{
  if (appheader_used != NULL &&
      appheader_used != option_appheader)
    {
      main_free (appheader_used);
      appheader_used = NULL;
    }

  main_buffree (main_bdata);
  main_bdata = NULL;
  main_bsize = 0;

  main_lru_cleanup();

  if (recode_stream != NULL)
    {
      xd3_free_stream (recode_stream);
      main_free (recode_stream);
      recode_stream = NULL;
    }

  if (merge_stream != NULL)
    {
      xd3_free_stream (merge_stream);
      main_free (merge_stream);
      merge_stream = NULL;
    }

  XD3_ASSERT (main_mallocs == 0);
}

Project: xdelta-devel

test_decompress_text (xd3_stream *stream, uint8_t *enc, usize_t enc_size, usize_t test_desize)
{
  xd3_config cfg;
  char decoded[sizeof (test_text)];
  uint8_t *apphead;
  usize_t apphead_size;
  usize_t decoded_size;
  const char *msg;
  int  ret;
  usize_t pos = 0;
  int flags = stream->flags;
  usize_t take;

 input:
  /* Test decoding test_desize input bytes at a time */
  take = min (enc_size - pos, test_desize);
  CHECK(take > 0);

  xd3_avail_input (stream, enc + pos, take);
 again:
  ret = xd3_decode_input (stream);

  pos += take;
  take = 0;

  switch (ret)
    {
    case XD3_OUTPUT:
      break;
    case XD3_WINSTART:
    case XD3_GOTHEADER:
      goto again;
    case XD3_INPUT:
      if (pos < enc_size) { goto input; }
      /* else fallthrough */
    case XD3_WINFINISH:
    default:
      goto fail;
    }

  CHECK(ret == XD3_OUTPUT);
  CHECK(pos == enc_size);

  if (stream->avail_out != sizeof (test_text))
    {
      stream->msg = "incorrect output size";
      ret = XD3_INTERNAL;
      goto fail;
    }

  decoded_size = stream->avail_out;
  memcpy (decoded, stream->next_out, stream->avail_out);

  xd3_consume_output (stream);

  if ((ret = xd3_get_appheader (stream, & apphead, & apphead_size))) { goto fail; }

  if (apphead_size != strlen ((char*) test_apphead) ||
      memcmp (apphead, test_apphead, strlen ((char*) test_apphead)) != 0)
    {
      stream->msg = "incorrect appheader";
      ret = XD3_INTERNAL;
      goto fail;
    }

  if ((ret = xd3_decode_input (stream)) != XD3_WINFINISH ||
      (ret = xd3_close_stream (stream)) != 0)
    {
      goto fail;
    }

  if (decoded_size != sizeof (test_text) ||
      memcmp (decoded, test_text, sizeof (test_text)) != 0)
    {
      stream->msg = "incorrect output text";
      ret = EIO;
    }

 fail:
  msg = stream->msg;
  xd3_free_stream (stream);
  xd3_init_config (& cfg, flags);
  xd3_config_stream (stream, & cfg);
  stream->msg = msg;

  return ret;
}

Project: xdelta-devel

test_recode_command (xd3_stream *stream, int ignore)
{
  /* Things to test:
   * - with and without a source file (recode does not change)
   *
   * (recode may or may not change -- 8 variations)
   * - with and without adler32
   * - with and without app header
   * - with and without secondary
   */
  int has_source;
  int variant;
  int change;
  int ret;

  for (has_source = 0; has_source < 2; has_source++)
    {
      for (variant = 0; variant < 8; variant++)
	{
	  for (change = 0; change < 8; change++)
	    {
	      if ((ret = test_recode_command2 (stream, has_source,
					       variant, change)))
		{
		  return ret;
		}
	    }
	  DOT ();
	}
    }

  return 0;
}

Project: xdelta-devel

main_set_secondary_flags (xd3_config *config)
{
  int ret;
  if (option_use_secondary)
    {
      /* The default secondary compressor is DJW, if it's compiled. */
      if (option_secondary == NULL)
	{
	  if (SECONDARY_DJW)
	    {
	      config->flags |= XD3_SEC_DJW;
	    }
	}
      else
	{
	  if (strcmp (option_secondary, "fgk") == 0 && SECONDARY_FGK)
	    {
	      config->flags |= XD3_SEC_FGK;
	    }
	  else if (strcmp (option_secondary, "lzma") == 0 && SECONDARY_LZMA)
	    {
	      config->flags |= XD3_SEC_LZMA;
	    }
	  else if (strncmp (option_secondary, "djw", 3) == 0 && SECONDARY_DJW)
	    {
	      usize_t level = XD3_DEFAULT_SECONDARY_LEVEL;

	      config->flags |= XD3_SEC_DJW;

	      if (strlen (option_secondary) > 3 &&
		  (ret = main_atou (option_secondary + 3,
				    &level,
				    0, 9, 'S')) != 0 &&
		  !option_quiet)
		{
		  return XD3_INVALID;
		}

	      /* XD3_SEC_NOXXXX flags disable secondary compression on
	       * a per-section basis.  For djw, ngroups=1 indicates
	       * minimum work, ngroups=0 uses default settings, which
	       * is > 1 groups by default. */
	      if (level < 1) { config->flags |= XD3_SEC_NODATA; }
	      if (level < 7) { config->sec_data.ngroups = 1; }
	      else { config->sec_data.ngroups = 0; }

	      if (level < 3) { config->flags |= XD3_SEC_NOINST; }
	      if (level < 8) { config->sec_inst.ngroups = 1; }
	      else { config->sec_inst.ngroups = 0; }

	      if (level < 5) { config->flags |= XD3_SEC_NOADDR; }
	      if (level < 9) { config->sec_addr.ngroups = 1; }
	      else { config->sec_addr.ngroups = 0; }
	    }
	  else if (strcmp (option_secondary, "none") == 0 && SECONDARY_DJW)
	    {
	      /* No secondary */
	    }
	  else
	    {
	      if (!option_quiet)
		{
		  XPR(NT "unrecognized secondary compressor type: %s\n",
		      option_secondary);
		  return XD3_INVALID;
		}
	    }
	}
    }

  return 0;
}

Project: xdelta-devel

main_secondary_decompress_check (main_file  *file,
				 uint8_t    *input_buf,
				 size_t      input_size,
				 size_t     *nread)
{
  int ret;
  usize_t i;
  usize_t try_read = min (input_size, XD3_ALLOCSIZE);
  size_t  check_nread = 0;
  uint8_t check_buf[XD3_ALLOCSIZE];  /* TODO: stack limit */
  const main_extcomp *decompressor = NULL;

  if ((ret = main_file_read (file, check_buf,
			     try_read,
			     & check_nread, "input read failed")))
    {
      return ret;
    }

  if (file->flags & RD_DECOMPSET)
    {
      /* This allows the application header to override the magic
       * number, for whatever reason. */
      decompressor = file->compressor;
    }
  else
    {
      for (i = 0; i < SIZEOF_ARRAY (extcomp_types); i += 1)
	{
	  const main_extcomp *decomp = & extcomp_types[i];

	  if (check_nread > decomp->magic_size)
	    {
	      /* The following expr checks if we are trying to read a
	       * VCDIFF input, in which case do not treat it as
	       * "secondary" decompression. */
	      int skip_this_type = (decomp->flags & RD_NONEXTERNAL) &&
  	                           (file->flags & RD_NONEXTERNAL);

	      if (skip_this_type)
		{
		  continue;
		}

	      if (memcmp (check_buf, decomp->magic, decomp->magic_size) == 0)
		{
		  decompressor = decomp;
		  break;
		}
	    }
	}
    }

  if (decompressor != NULL)
    {
      if (! option_quiet)
	{
	  XPR(NT "externally compressed input: %s %s%s < %s\n",
	      decompressor->decomp_cmdname,
	      decompressor->decomp_options,
	      (option_force2 ? " -f" : ""),
	      file->filename);
	  if (file->flags & RD_MAININPUT)
	    {
	      XPR(NT
  "WARNING: the encoder is automatically decompressing the input file;\n");
	      XPR(NT
  "WARNING: the decoder will automatically recompress the output file;\n");
	      XPR(NT
  "WARNING: this may result in different compressed data and checksums\n");
	      XPR(NT
  "WARNING: despite being identical data; if this is an issue, use -D\n");
	      XPR(NT
  "WARNING: to avoid decompression and/or use -R to avoid recompression\n");
	      XPR(NT
  "WARNING: and/or manually decompress the input file; if you know the\n");
	      XPR(NT
  "WARNING: compression settings that will produce identical output\n");
	      XPR(NT
  "WARNING: you may set those flags using the environment (e.g., GZIP=-9)\n");
	    }
	}

      file->size_known = 0;
      return main_input_decompress_setup (decompressor, file,
					  input_buf, input_size,
					  check_buf, XD3_ALLOCSIZE,
					  check_nread, nread);
    }

  /* Now read the rest of the input block. */
  (*nread) = 0;

  if (check_nread == try_read)
    {
      ret = main_file_read (file,
			    input_buf + try_read,
			    input_size - try_read,
			    nread,
			    "input read failed");
    }

  memcpy (input_buf, check_buf, check_nread);

  (*nread) += check_nread;

  return 0;
}

Project: xdelta-devel

test_encode_decode_uint32_t (xd3_stream *stream, int unused)
{
  TEST_ENCODE_DECODE_INTEGER(uint32_t,1U,UINT32_MAX);
}

Project: xdelta-devel

main_file_isopen (main_file *xfile)
{
#if XD3_STDIO
  return xfile->file != NULL;

#elif XD3_POSIX
  return xfile->file != -1;

#elif XD3_WIN32
  return xfile->file != INVALID_HANDLE_VALUE;
#endif
}

Project: xdelta-devel

test_encode_code_table (xd3_stream *stream, int ignore)
{
  int ret;
  const uint8_t *comp_data;
  usize_t comp_size;

  if ((ret = xd3_compute_alternate_table_encoding (stream, & comp_data, & comp_size)))
    {
      return ret;
    }

  stream->acache.s_near = __alternate_code_table_desc.near_modes;
  stream->acache.s_same = __alternate_code_table_desc.same_modes;

  if ((ret = xd3_apply_table_encoding (stream, comp_data, comp_size)))
    {
      return ret;
    }

  if (memcmp (stream->code_table, xd3_alternate_code_table (), sizeof (xd3_dinst) * 256) != 0)
    {
      stream->msg = "wrong code table reconstruction";
      return XD3_INTERNAL;
    }

  return 0;
}

Project: xdelta-devel

void main_buffree (void *ptr) {
#if XD3_WIN32
  VirtualFree(ptr, 0, MEM_RELEASE);
#else
  main_free1(NULL, ptr);
#endif
}

Project: xdelta-devel

sec_dist_func9 (xd3_stream *stream, xd3_output *data)
{
  int i, ret;

  int ramp   = 0;
  int rcount = 0;
  int prom   = 0;
  int pcount = 0;

  /* 200 was long enough to trigger it--only when stricter checking
   * that counted all blocks was turned on, but it seems I deleted
   * this code. (missing fgk_free_block on line 398). */
  for (i = 0; i < ALPHABET_SIZE*200; i += 1)
    {
    repeat:
      if (ramp < ALPHABET_SIZE)
	{
	  /* Initially Nth symbol has (N+1) frequency */
	  if (rcount <= ramp)
	    {
	      rcount += 1;
	      if ((ret = xd3_emit_byte (stream, & data, ramp))) { return ret; }
	      continue;
	    }

	  ramp   += 1;
	  rcount  = 0;
	  goto repeat;
	}

      /* Thereafter, promote least freq to max freq */
      if (pcount == ALPHABET_SIZE)
	{
	  pcount = 0;
	  prom   = (prom + 1) % ALPHABET_SIZE;
	}

      pcount += 1;
      if ((ret = xd3_emit_byte (stream, & data, prom))) { return ret; }
    }

  return 0;
}

Project: xdelta-devel

main_format_rate (xoff_t bytes, long millis, shortbuf *buf)
{
  xoff_t r = (xoff_t)(1.0 * bytes / (1.0 * millis / 1000.0));
  static shortbuf lbuf;

  main_format_bcnt (r, &lbuf);
  short_sprintf (*buf, "%s/s", lbuf.buf);
  return buf->buf;
}

Project: xdelta-devel

check_vcdiff_header (xd3_stream *stream,
		     const char *input,
		     const char *line_start,
		     const char *matches,
		     int yes_or_no)
{
  int ret;
  char vcmd[TESTBUFSIZE], gcmd[TESTBUFSIZE];

  snprintf_func (vcmd, TESTBUFSIZE, "%s printhdr -f %s %s",
	    program_name, input, TEST_RECON2_FILE);

  if ((ret = system (vcmd)) != 0)
    {
      XPR(NT "printhdr command: %s\n", vcmd);
      stream->msg = "printhdr cmd failed";
      return XD3_INTERNAL;
    }

  snprintf_func (gcmd, TESTBUFSIZE, "grep \"%s.*%s.*\" %s > /dev/null",
	    line_start, matches, TEST_RECON2_FILE);

  if (yes_or_no)
    {
      if ((ret = do_cmd (stream, gcmd)))
	{
	  XPR(NT "%s\n", gcmd);
	  return ret;
	}
    }
  else
    {
      if ((ret = do_fail (stream, gcmd)))
	{
	  XPR(NT "%s\n", gcmd);
	  return ret;
	}
    }

  return 0;
}

Project: xdelta-devel

main_strtoxoff (const char* s, xoff_t *xo, char which)
{
  char *e;
  xoff_t x;

  XD3_ASSERT(s && *s != 0);

  {
    /* Should check LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX? */
#if SIZEOF_XOFF_T == 4
    long xx = strtol (s, &e, 0);
#else
    long long xx = strtoll (s, &e, 0);
#endif

    if (xx < 0)
      {
	XPR(NT "-%c: negative integer: %s\n", which, s);
	return EXIT_FAILURE;
      }

    x = xx;
  }

  if (*e != 0)
    {
      XPR(NT "-%c: invalid integer: %s\n", which, s);
      return EXIT_FAILURE;
    }

  (*xo) = x;
  return 0;
}

Project: xdelta-devel

xd3_posix_io (int fd, uint8_t *buf, size_t size,
	      xd3_posix_func *func, size_t *nread)
{
  int ret;
  size_t nproc = 0;

  while (nproc < size)
    {
      size_t tryread = min(size - nproc, 1U << 30);
      ssize_t result = (*func) (fd, buf + nproc, tryread);

      if (result < 0)
	{
	  ret = get_errno ();
	  if (ret != EAGAIN && ret != EINTR)
	    {
	      return ret;
	    }
	  continue;
	}

      if (nread != NULL && result == 0) { break; }

      nproc += result;
    }
  if (nread != NULL) { (*nread) = nproc; }
  return 0;
}

Project: xdelta-devel

uint32_t mt_random (mtrand *mt) {
  uint32_t y;
  unsigned long mag01[2];
  mag01[0] = 0;
  mag01[1] = MATRIX_A;

  if (mt->mt_index_ >= MT_LEN) {
    int kk;

    for (kk = 0; kk < MT_LEN - MT_IA; kk++) {
      y = (mt->mt_buffer_[kk] & UPPER_MASK) |
	(mt->mt_buffer_[kk + 1] & LOWER_MASK);
      mt->mt_buffer_[kk] = mt->mt_buffer_[kk + MT_IA] ^
	(y >> 1) ^ mag01[y & 0x1UL];
    }
    for (;kk < MT_LEN - 1; kk++) {
      y = (mt->mt_buffer_[kk] & UPPER_MASK) |
	(mt->mt_buffer_[kk + 1] & LOWER_MASK);
      mt->mt_buffer_[kk] = mt->mt_buffer_[kk + (MT_IA - MT_LEN)] ^
	(y >> 1) ^ mag01[y & 0x1UL];
    }
    y = (mt->mt_buffer_[MT_LEN - 1] & UPPER_MASK) |
      (mt->mt_buffer_[0] & LOWER_MASK);
    mt->mt_buffer_[MT_LEN - 1] = mt->mt_buffer_[MT_IA - 1] ^
      (y >> 1) ^ mag01[y & 0x1UL];
    mt->mt_index_ = 0;
  }

  y = mt->mt_buffer_[mt->mt_index_++];

  y ^= (y >> 11);
  y ^= (y << 7) & 0x9d2c5680UL;
  y ^= (y << 15) & 0xefc60000UL;
  y ^= (y >> 18);

  return y;
}

Project: xdelta-devel

test_command_line_arguments (xd3_stream *stream, int ignore)
{
  int i, ret;

  static const char* cmdpairs[] =
  {
    /* standard input, output */
    "%s %s -A < %s > %s", "%s -d < %s > %s",
    "%s %s -A -e < %s > %s", "%s -d < %s > %s",
    "%s %s -A= encode < %s > %s", "%s decode < %s > %s",
    "%s %s -A -q encode < %s > %s", "%s -qdq < %s > %s",

    /* file input, standard output */
    "%s %s -A= %s > %s", "%s -d %s > %s",
    "%s %s -A -e %s > %s", "%s -d %s > %s",
    "%s %s encode -A= %s > %s", "%s decode %s > %s",

    /* file input, output */
    "%s %s -A= %s %s", "%s -d %s %s",
    "%s %s -A -e %s %s", "%s -d %s %s",
    "%s %s -A= encode %s %s", "%s decode %s %s",

    /* option placement */
    "%s %s -A -f %s %s", "%s -f -d %s %s",
    "%s %s -e -A= %s %s", "%s -d -f %s %s",
    "%s %s -f encode -A= %s %s", "%s -f decode -f %s %s",
  };

  char ecmd[TESTBUFSIZE], dcmd[TESTBUFSIZE];
  int pairs = SIZEOF_ARRAY (cmdpairs) / 2;
  xoff_t tsize;
  xoff_t dsize;
  double ratio;

  mt_init (& static_mtrand, 0x9f73f7fc);

  for (i = 0; i < pairs; i += 1)
    {
      test_setup ();
      if ((ret = test_make_inputs (stream, NULL, & tsize))) { return ret; }

      snprintf_func (ecmd, TESTBUFSIZE, cmdpairs[2*i], program_name,
	       test_softcfg_str, TEST_TARGET_FILE, TEST_DELTA_FILE);
      snprintf_func (dcmd, TESTBUFSIZE, cmdpairs[2*i+1], program_name,
	       TEST_DELTA_FILE, TEST_RECON_FILE);

      /* Encode and decode. */
      if ((ret = system (ecmd)) != 0)
	{
	  XPR(NT "encode command: %s\n", ecmd);
	  stream->msg = "encode cmd failed";
	  return XD3_INTERNAL;
	}

      if ((ret = system (dcmd)) != 0)
	{
	  XPR(NT "decode command: %s\n", dcmd);
	  stream->msg = "decode cmd failed";
	  return XD3_INTERNAL;
	}

      /* Compare the target file. */
      if ((ret = test_compare_files (TEST_TARGET_FILE, TEST_RECON_FILE)))
	{
	  return ret;
	}

      if ((ret = test_file_size (TEST_DELTA_FILE, & dsize)))
	{
	  return ret;
	}

      ratio = (double) dsize / (double) tsize;

      /* Check that it is not too small, not too large. */
      if (ratio >= TEST_ADD_RATIO + TEST_EPSILON)
	{
	  XPR(NT "test encode with size ratio %.4f, "
	     "expected < %.4f (%"Q"u, %"Q"u)\n",
	    ratio, TEST_ADD_RATIO + TEST_EPSILON, dsize, tsize);
	  stream->msg = "strange encoding";
	  return XD3_INTERNAL;
	}

      if (ratio <= TEST_ADD_RATIO * (1.0 - 2 * TEST_EPSILON))
	{
	  XPR(NT "test encode with size ratio %.4f, "
	     "expected > %.4f\n",
	    ratio, TEST_ADD_RATIO - TEST_EPSILON);
	  stream->msg = "strange encoding";
	  return XD3_INTERNAL;
	}

      /* Also check that test_compare_files works.  The delta and original should
       * not be identical. */
      if ((ret = test_compare_files (TEST_DELTA_FILE,
				TEST_TARGET_FILE)) == 0)
	{
	  stream->msg = "broken test_compare_files";
	  return XD3_INTERNAL;
	}

      test_cleanup ();
      DOT ();
    }

  return 0;
}

Project: xdelta-devel

main_ident_compressor (const char *ident)
{
  usize_t i;

  for (i = 0; i < SIZEOF_ARRAY (extcomp_types); i += 1)
    {
      if (strcmp (extcomp_types[i].ident, ident) == 0)
	{
	  return & extcomp_types[i];
	}
    }

  return NULL;
}

Project: xdelta-devel

main_input_decompress_setup (const main_extcomp   *decomp,
			     main_file            *ifile,
			     uint8_t              *input_buf,
			     usize_t               input_bufsize,
			     uint8_t              *pipe_buf,
			     usize_t               pipe_bufsize,
			     usize_t               pipe_avail,
			     size_t               *nread)
{
  /* The two pipes: input and output file descriptors. */
  int outpipefd[2], inpipefd[2];
  int input_fd = -1;  /* The resulting input_fd (output of decompression). */
  pid_t decomp_id, copier_id;  /* The two subprocs. */
  int ret;

  outpipefd[0] = outpipefd[1] = -1;
  inpipefd[0]  = inpipefd[1]  = -1;

  if (pipe (outpipefd) || pipe (inpipefd))
    {
      XPR(NT "pipe failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

  if ((decomp_id = fork ()) < 0)
    {
      XPR(NT "fork failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

  /* The first child runs the decompression process: */
  if (decomp_id == 0)
    {
      if (option_verbose > 2)
	{
	  XPR(NT "external decompression pid %d\n", getpid ());
	}

      /* Setup pipes: write to the outpipe, read from the inpipe. */
      if (dup2 (outpipefd[PIPE_WRITE_FD], STDOUT_FILENO) < 0 ||
	  dup2 (inpipefd[PIPE_READ_FD], STDIN_FILENO) < 0 ||
	  close (outpipefd[PIPE_READ_FD]) ||
	  close (outpipefd[PIPE_WRITE_FD]) ||
	  close (inpipefd[PIPE_READ_FD]) ||
	  close (inpipefd[PIPE_WRITE_FD]) ||
	  execlp (decomp->decomp_cmdname, decomp->decomp_cmdname,
		  decomp->decomp_options,
		  option_force2 ? "-f" : NULL,
		  NULL))
	{
	  XPR(NT "child process %s failed to execute: %s\n",
	      decomp->decomp_cmdname, xd3_mainerror (get_errno ()));
	}

      _exit (127);
    }

  XD3_ASSERT(num_subprocs < MAX_SUBPROCS);
  ext_subprocs[num_subprocs++] = decomp_id;

  if ((copier_id = fork ()) < 0)
    {
      XPR(NT "fork failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

  /* The second child runs the copier process: */
  if (copier_id == 0)
    {
      int exitval = 0;

      if (option_verbose > 2)
	{
	  XPR(NT "child pipe-copier pid %d\n", getpid ());
	}

      if (close (inpipefd[PIPE_READ_FD]) ||
	  close (outpipefd[PIPE_READ_FD]) ||
	  close (outpipefd[PIPE_WRITE_FD]) ||
	  main_pipe_copier (pipe_buf, pipe_bufsize, pipe_avail,
			    ifile, inpipefd[PIPE_WRITE_FD]) ||
	  close (inpipefd[PIPE_WRITE_FD]))
	{
	  XPR(NT "child copier process failed: %s\n",
	      xd3_mainerror (get_errno ()));
	  exitval = 1;
	}

      _exit (exitval);
    }

  XD3_ASSERT(num_subprocs < MAX_SUBPROCS);
  ext_subprocs[num_subprocs++] = copier_id;

  /* The parent closes both pipes after duplicating the output of
   * compression. */
  input_fd = dup (outpipefd[PIPE_READ_FD]);

  if (input_fd < 0 ||
      main_file_close (ifile) ||
      close (outpipefd[PIPE_READ_FD]) ||
      close (outpipefd[PIPE_WRITE_FD]) ||
      close (inpipefd[PIPE_READ_FD]) ||
      close (inpipefd[PIPE_WRITE_FD]))
    {
      XPR(NT "dup/close failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

#if XD3_STDIO
  /* Note: fdopen() acquires the fd, closes it when finished. */
  if ((ifile->file = fdopen (input_fd, "r")) == NULL)
    {
      XPR(NT "fdopen failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

#elif XD3_POSIX
  ifile->file = input_fd;
#endif

  ifile->compressor = decomp;

  /* Now the input file is decompressed. */
  return main_file_read (ifile, input_buf, input_bufsize,
			 nread, "input decompression failed");

 pipe_cleanup:
  close (input_fd);
  close (outpipefd[PIPE_READ_FD]);
  close (outpipefd[PIPE_WRITE_FD]);
  close (inpipefd[PIPE_READ_FD]);
  close (inpipefd[PIPE_WRITE_FD]);
  return ret;
}

Project: xdelta-devel

test_externally_compressed_io (xd3_stream *stream, int ignore)
{
  usize_t i;
  int ret;
  char buf[TESTBUFSIZE];

  mt_init (& static_mtrand, 0x9f73f7fc);

  if ((ret = test_make_inputs (stream, NULL, NULL))) { return ret; }

  for (i = 0; i < SIZEOF_ARRAY (extcomp_types); i += 1)
    {
      main_extcomp *ext = & extcomp_types[i];

      /* Test for the existence of the external command first, if not skip. */
      snprintf_func (buf, TESTBUFSIZE, "%s %s < /dev/null > /dev/null", ext->recomp_cmdname, ext->recomp_options);

      if ((ret = system (buf)) != 0)
	{
	  XPR(NT "%s=0", ext->recomp_cmdname);
	  continue;
	}

      if ((ret = test_compressed_pipe (stream, ext, buf, "-cfq", "-dcfq", 1,
				       "compression failed: identity pipe")) ||
	  (ret = test_compressed_pipe (stream, ext, buf, "-cfq", "-Rdcfq", 0,
				       "compression failed: without recompression")) ||
	  (ret = test_compressed_pipe (stream, ext, buf, "-Dcfq", "-Rdcfq", 1,
				       "compression failed: without decompression")))
	{
	  return ret;
	}
    }

  return 0;
}

Project: xdelta-devel

main_free1 (void *opaque, void *ptr)
{
  free (ptr);
}

Project: xdelta-devel

test_no_output (xd3_stream *stream, int ignore)
{
  int ret;
  char buf[TESTBUFSIZE];

  test_setup ();

  snprintf_func (buf, TESTBUFSIZE, "touch %s && chmod 0000 %s",
	   TEST_NOPERM_FILE, TEST_NOPERM_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  if ((ret = test_make_inputs (stream, NULL, NULL))) { return ret; }

  /* Try no_output encode w/out unwritable output file */
  snprintf_func (buf, TESTBUFSIZE, "%s -q -f -e %s %s", program_name,
	   TEST_TARGET_FILE, TEST_NOPERM_FILE);
  if ((ret = do_fail (stream, buf))) { return ret; }
  snprintf_func (buf, TESTBUFSIZE, "%s -J -e %s %s", program_name,
	   TEST_TARGET_FILE, TEST_NOPERM_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  /* Now really write the delta to test decode no-output */
  snprintf_func (buf, TESTBUFSIZE, "%s -e %s %s", program_name,
	   TEST_TARGET_FILE, TEST_DELTA_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  snprintf_func (buf, TESTBUFSIZE, "%s -q -f -d %s %s", program_name,
	   TEST_DELTA_FILE, TEST_NOPERM_FILE);
  if ((ret = do_fail (stream, buf))) { return ret; }
  snprintf_func (buf, TESTBUFSIZE, "%s -J -d %s %s", program_name,
	   TEST_DELTA_FILE, TEST_NOPERM_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }
  test_cleanup ();
  return 0;
}

Project: xdelta-devel

main_merge_func (xd3_stream* stream, main_file *no_write)
{
  int ret;

  if ((ret = xd3_whole_append_window (stream)))
    {
      return ret;
    }

  return 0;
}

Project: xdelta-devel

test_unlink (char* file)
{
  int ret;
  if ((ret = unlink (file)) != 0 && errno != ENOENT)
    {
      XPR(NT "unlink %s failed: %s\n", file, strerror(ret));
    }
}

Project: xdelta-devel

sec_dist_func11 (xd3_stream *stream, xd3_output *data)
{
  int sum0 = 0;
  int sum1 = 1;
  int i, j, ret;
  for (i = 0; i < 33; ++i)
    {
      for (j = 0; j < (sum0 + sum1); ++j)
	{
	  if ((ret = xd3_emit_byte (stream, & data, i))) { return ret; }
	}
      sum0 = sum1;
      sum1 = j;
    }
  return 0;
}

Project: xdelta-devel

test_choose_instruction (xd3_stream *stream, int ignore)
{
  int i;

  stream->code_table = (*stream->code_table_func) ();

  for (i = 0; i < 256; i += 1)
    {
      const xd3_dinst *d = stream->code_table + i;
      xd3_rinst prev, inst;

      CHECK(d->type1 > 0);

      memset (& prev, 0, sizeof (prev));
      memset (& inst, 0, sizeof (inst));

      if (d->type2 == 0)
	{
	  inst.type = d->type1;

	  if ((inst.size = d->size1) == 0)
	    {
	      inst.size = TESTBUFSIZE;
	    }

	  XD3_CHOOSE_INSTRUCTION (stream, NULL, & inst);

	  if (inst.code2 != 0 || inst.code1 != i)
	    {
	      stream->msg = "wrong single instruction";
	      return XD3_INTERNAL;
	    }
	}
      else
	{
	  prev.type = d->type1;
	  prev.size = d->size1;
	  inst.type = d->type2;
	  inst.size = d->size2;

	  XD3_CHOOSE_INSTRUCTION (stream, & prev, & inst);

	  if (prev.code2 != i)
	    {
	      stream->msg = "wrong double instruction";
	      return XD3_INTERNAL;
	    }
	}
    }

  return 0;
}

Project: xdelta-devel

main_apphead_string (const char* x)
{
  const char *y;

  if (x == NULL) { return ""; }

  if (strcmp (x, "/dev/stdin") == 0 ||
      strcmp (x, "/dev/stdout") == 0 ||
      strcmp (x, "/dev/stderr") == 0) { return "-"; }

  // TODO: this is not portable
  return (y = strrchr (x, '/')) == NULL ? x : y + 1;
}

Project: xdelta-devel

main_atou (const char* arg, usize_t *uo, usize_t low,
	   usize_t high, char which) 
{
  int ret;
  xoff_t xo;
  if ((ret = main_atoux (arg, &xo, low, high, which)))
    {
      return ret;
    }
  *uo = (usize_t)xo;
  return 0;
}

Project: xdelta-devel

main_open_output (xd3_stream *stream, main_file *ofile)
{
  int ret;

  if (option_no_output)
    {
      return 0;
    }

  if (ofile->filename == NULL)
    {
      XSTDOUT_XF (ofile);

      if (option_verbose > 1)
	{
	  XPR(NT "using standard output: %s\n", ofile->filename);
	}
    }
  else
    {
      /* Stat the file to check for overwrite. */
      if (option_force == 0 && main_file_exists (ofile))
	{
	  if (!option_quiet)
	    {
	      XPR(NT "to overwrite output file specify -f: %s\n",
		  ofile->filename);
	    }
	  return EEXIST;
	}

      if ((ret = main_file_open (ofile, ofile->filename, XO_WRITE)))
	{
	  return ret;
	}

      if (option_verbose > 1) { XPR(NT "output %s\n", ofile->filename); }
    }

#if EXTERNAL_COMPRESSION
  /* Do output recompression. */
  if (ofile->compressor != NULL && option_recompress_outputs == 1)
    {
      if (! option_quiet)
	{
	  XPR(NT "externally compressed output: %s %s%s > %s\n",
	      ofile->compressor->recomp_cmdname,
	      ofile->compressor->recomp_options,
	      (option_force2 ? " -f" : ""),
	      ofile->filename);
	}

      if ((ret = main_recompress_output (ofile)))
	{
	  return ret;
	}
    }
#endif

  return 0;
}

Project: xdelta-devel

main_merge_output (xd3_stream *stream, main_file *ofile)
{
  int ret;
  usize_t inst_pos = 0;
  xoff_t output_pos = 0;
  xd3_source recode_source;
  usize_t window_num = 0;
  int at_least_once = 0;

  /* merge_stream is set if there were arguments.  this stream's input
   * needs to be applied to the merge_stream source. */
  if ((merge_stream != NULL) &&
      (ret = xd3_merge_input_output (stream,
				     & merge_stream->whole_target)))
    {
      XPR(NT XD3_LIB_ERRMSG (stream, ret));
      return ret;
    }

  if (option_use_appheader != 0 &&
      option_appheader != NULL)
    {
      xd3_set_appheader (recode_stream, option_appheader,
			 (usize_t) strlen ((char*) option_appheader));
    }

  /* Enter the ENC_INPUT state and bypass the next_in == NULL test
   * and (leftover) input buffering logic. */
  XD3_ASSERT(recode_stream->enc_state == ENC_INIT);
  recode_stream->enc_state = ENC_INPUT;
  recode_stream->next_in = main_bdata;
  recode_stream->flags |= XD3_FLUSH;

  /* This encodes the entire target. */
  while (inst_pos < stream->whole_target.instlen || !at_least_once)
    {
      xoff_t window_start = output_pos;
      int window_srcset = 0;
      xoff_t window_srcmin = 0;
      xoff_t window_srcmax = 0;
      usize_t window_pos = 0;
      usize_t window_size;

      /* at_least_once ensures that we encode at least one window,
       * which handles the 0-byte case. */
      at_least_once = 1;

      XD3_ASSERT (recode_stream->enc_state == ENC_INPUT);

      if ((ret = xd3_encode_input (recode_stream)) != XD3_WINSTART)
	{
	  XPR(NT "invalid merge state: %s\n", xd3_mainerror (ret));
	  return XD3_INVALID;
	}

      /* Window sizes must match from the input to the output, so that
       * target copies are in-range (and so that checksums carry
       * over). */
      XD3_ASSERT (window_num < stream->whole_target.wininfolen);
      window_size = stream->whole_target.wininfo[window_num].length;

      /* Output position should also match. */
      if (output_pos != stream->whole_target.wininfo[window_num].offset)
	{
	  XPR(NT "internal merge error: offset mismatch\n");
	  return XD3_INVALID;
	}

      if (option_use_checksum &&
	  (stream->dec_win_ind & VCD_ADLER32) != 0)
	{
	  recode_stream->flags |= XD3_ADLER32_RECODE;
	  recode_stream->recode_adler32 =
	    stream->whole_target.wininfo[window_num].adler32;
	}

      window_num++;

      if (main_bsize < window_size)
	{
	  main_buffree (main_bdata);
	  main_bdata = NULL;
	  main_bsize = 0;
	  if ((main_bdata = (uint8_t*)
	       main_bufalloc (window_size)) == NULL)
	    {
	      return ENOMEM;
	    }
	  main_bsize = window_size;
	}

      /* This encodes a single target window. */
      while (window_pos < window_size &&
	     inst_pos < stream->whole_target.instlen)
	{
	  xd3_winst *inst = &stream->whole_target.inst[inst_pos];
	  usize_t take = min(inst->size, window_size - window_pos);
	  xoff_t addr;

	  switch (inst->type)
	    {
	    case XD3_RUN:
	      if ((ret = xd3_emit_run (recode_stream, window_pos, take,
				       &stream->whole_target.adds[inst->addr])))
		{
		  return ret;
		}
	      break;

	    case XD3_ADD:
	      /* Adds are implicit, put them into the input buffer. */
	      memcpy (main_bdata + window_pos,
		      stream->whole_target.adds + inst->addr, take);
	      break;

	    default: /* XD3_COPY + copy mode */
	      if (inst->mode != 0)
		{
		  if (window_srcset) {
		    window_srcmin = min(window_srcmin, inst->addr);
		    window_srcmax = max(window_srcmax, inst->addr + take);
		  } else {
		    window_srcset = 1;
		    window_srcmin = inst->addr;
		    window_srcmax = inst->addr + take;
		  }
		  addr = inst->addr;
		}
	      else
		{
		  XD3_ASSERT (inst->addr >= window_start);
		  addr = inst->addr - window_start;
		}
	      IF_DEBUG2 (XPR(NTR "[merge copy] winpos %u take %u addr %"Q"u mode %u\n",
			    window_pos, take, addr, inst->mode));
	      if ((ret = xd3_found_match (recode_stream, window_pos, take,
					  addr, inst->mode != 0)))
		{
		  return ret;
		}
	      break;
	    }

	  window_pos += take;
	  output_pos += take;

	  if (take == inst->size)
	    {
	      inst_pos += 1;
	    }
	  else
	    {
	      /* Modify the instruction for the next pass. */
	      if (inst->type != XD3_RUN)
		{
		  inst->addr += take;
		}
	      inst->size -= take;
	    }
	}

      xd3_avail_input (recode_stream, main_bdata, window_pos);

      recode_stream->enc_state = ENC_INSTR;

      if (window_srcset) {
	recode_stream->srcwin_decided = 1;
	recode_stream->src = &recode_source;
	recode_source.srclen = (usize_t)(window_srcmax - window_srcmin);
	recode_source.srcbase = window_srcmin;
	recode_stream->taroff = recode_source.srclen;

	XD3_ASSERT (recode_source.srclen != 0);
      } else {
	recode_stream->srcwin_decided = 0;
	recode_stream->src = NULL;
	recode_stream->taroff = 0;
      }

      for (;;)
	{
	  switch ((ret = xd3_encode_input (recode_stream)))
	    {
	    case XD3_INPUT: {
	      goto done_window;
	    }
	    case XD3_OUTPUT: {
	      /* main_file_write below */
	      break;
	    }
	    case XD3_GOTHEADER:
	    case XD3_WINSTART:
	    case XD3_WINFINISH: {
	      /* ignore */
	      continue;
	    }
	    case XD3_GETSRCBLK:
	    case 0: {
	      return XD3_INTERNAL;
	    }
	    default:
	      return ret;
	    }

	  if ((ret = main_write_output(recode_stream, ofile)))
	    {
	      return ret;
	    }

	  xd3_consume_output (recode_stream);
	}
    done_window:
      (void) 0;
    }

  return 0;
}

Project: xdelta-devel

test_decompress_single_bit_error (xd3_stream *stream, int expected_non_failures)
{
  int ret;
  usize_t i;
  uint8_t encoded[4*sizeof (test_text)]; /* make room for alt code table */
  usize_t  encoded_size;
  int non_failures = 0;
  int cksum = (stream->flags & XD3_ADLER32) != 0;

//#define DEBUG_TEST_FAILURES
#ifndef DEBUG_TEST_FAILURES
#define TEST_FAILURES()
#else
  /* For checking non-failure cases by hand, enable this macro and run
   * xdelta printdelta with print_cpymode disabled.  Every non-failure
   * should change a copy address mode, which doesn't cause a failure
   * because the address cache starts out with all zeros.

    ./xdelta3 test
    for i in test_text.xz.*; do ./xdelta3 printdelta $i > $i.out;
    diff $i.out test_text.xz.0.out; done

   */
  system ("rm -rf test_text.*");
  {
    char buf[TESTBUFSIZE];
    FILE *f;
    snprintf_func (buf, TESTBUFSIZE, "test_text");
    f = fopen (buf, "w");
    fwrite (test_text,1,sizeof (test_text),f);
    fclose (f);
  }
#define TEST_FAILURES()                                         \
  do {                                                          \
    char buf[TESTBUFSIZE];      				\
    FILE *f;                                                    \
    snprintf_func (buf, TESTBUFSIZE, "test_text.xz.%d", non_failures);	\
    f = fopen (buf, "w");                                       \
    fwrite (encoded,1,encoded_size,f);                          \
    fclose (f);                                                 \
  } while (0)
#endif

  stream->sec_data.inefficient = 1;
  stream->sec_inst.inefficient = 1;
  stream->sec_addr.inefficient = 1;

  /* Encode text, test correct input */
  if ((ret = test_compress_text (stream, encoded, & encoded_size)))
    {
      /*stream->msg = "without error: encode failure";*/
      return ret;
    }

  if ((ret = test_decompress_text (stream, encoded, encoded_size,
				   sizeof (test_text) / 4)))
    {
      /*stream->msg = "without error: decode failure";*/
      return ret;
    }

  TEST_FAILURES();

  for (i = 0; i < encoded_size*8; i += 1)
    {
      /* Single bit error. */
      encoded[i/8] ^= 1 << (i%8);

      if ((ret = test_decompress_text (stream, encoded,
				       encoded_size, sizeof (test_text))) == 0)
	{
	  non_failures += 1;
#ifdef DEBUG_TEST_FAILURES
	  XPR(NT "%u[%u] non-failure %u\n", i/8, i%8, non_failures);
#endif
	  TEST_FAILURES();
	}
      else
	{
	  /*XPR(NT "%u[%u] failure: %s\n", i/8, i%8, stream->msg);*/
	}

      /* decompress_text returns EIO when the final memcmp() fails, but that
       * should never happen with checksumming on. */
      if (cksum && ret == EIO)
	{
	  /*XPR(NT "%u[%u] cksum mismatch\n", i/8, i%8);*/
	  stream->msg = "checksum mismatch";
	  return XD3_INTERNAL;
	}

      /* Undo single bit error. */
      encoded[i/8] ^= 1 << (i%8);
    }

  /* Test correct input again */
  if ((ret = test_decompress_text (stream, encoded, encoded_size, 1)))
    {
      /*stream->msg = "without error: decode failure";*/
      return ret;
    }

  /* Check expected non-failures */
  if (non_failures != expected_non_failures)
    {
      XPR(NT "non-failures %u; expected %u",
	 non_failures, expected_non_failures);
      stream->msg = "incorrect";
      return XD3_INTERNAL;
    }

  DOT ();

  return 0;
}

Project: xdelta-devel

main_file_stat (main_file *xfile, xoff_t *size)
{
  int ret = 0;
#if XD3_WIN32
  if (GetFileType(xfile->file) != FILE_TYPE_DISK)
    {
      return -1;
    }
# if (_WIN32_WINNT >= 0x0500)
  {
    LARGE_INTEGER li;
    if (GetFileSizeEx(xfile->file, &li) == 0)
      {
	return get_errno ();
      }
    *size = li.QuadPart;
  }
# else
  {
    DWORD filesize = GetFileSize(xfile->file, NULL);
    if (filesize == INVALID_FILE_SIZE)
      {
	return get_errno ()
      }
    *size = filesize;
  }
# endif
#else
  struct stat sbuf;
  if (fstat (XFNO (xfile), & sbuf) < 0)
    {
      ret = get_errno ();
      return ret;
    }

  if (! S_ISREG (sbuf.st_mode))
    {
      return ESPIPE;
    }
  (*size) = sbuf.st_size;
#endif
  return ret;
}

Project: xdelta-devel

sec_dist_func4 (xd3_stream *stream, xd3_output *data)
{
  int i, ret, x;
  for (i = 0; i < ALPHABET_SIZE*20; i += 1)
    {
      x = mt_exp_rand (10, ALPHABET_SIZE/2);
      if ((ret = xd3_emit_byte (stream, & data, x))) { return ret; }
    }
  return 0;
}

Project: xdelta-devel

test_forward_match (xd3_stream *stream, int unused)
{
  usize_t i;
  uint8_t buf1[256], buf2[256];

  memset(buf1, 0, 256);
  memset(buf2, 0, 256);

  for (i = 0; i < 256; i++)
    {
      CHECK(xd3_forward_match(buf1, buf2, i) == (int)i);
    }

  for (i = 0; i < 255; i++)
    {
      buf2[i] = 1;
      CHECK(xd3_forward_match(buf1, buf2, 256) == (int)i);
      buf2[i] = 0;
    }

  return 0;
}

Project: xdelta-devel

xd3_mainerror(int err_num) {
#ifndef _WIN32
	const char* x = xd3_strerror (err_num);
	if (x != NULL)
	  {
	    return x;
	  }
	return strerror(err_num);
#else
	static char err_buf[256];
	const char* x = xd3_strerror (err_num);
	if (x != NULL)
	  {
	    return x;
	  }
	memset (err_buf, 0, 256);
	FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM |
		       FORMAT_MESSAGE_IGNORE_INSERTS,
		       NULL, err_num,
		       MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
		       err_buf, 256, NULL);
	if (err_buf[0] != 0 && err_buf[strlen(err_buf) - 1] == '\n')
	  {
	    err_buf[strlen(err_buf) - 1] = 0;
	  }
	return err_buf;
#endif
}

Project: xdelta-devel

test_secondary (xd3_stream *stream, const xd3_sec_type *sec, usize_t groups)
{
  usize_t test_i;
  int ret;
  xd3_output *in_head, *out_head, *p;
  usize_t p_off, input_size, compress_size;
  uint8_t *dec_input = NULL, *dec_output = NULL, *dec_correct = NULL;
  xd3_sec_stream *enc_stream;
  xd3_sec_cfg cfg;

  memset (& cfg, 0, sizeof (cfg));

  cfg.inefficient = 1;

  for (cfg.ngroups = 1; cfg.ngroups <= groups; cfg.ngroups += 1)
    {
      XPR(NTR "\n...");
      for (test_i = 0; test_i < SIZEOF_ARRAY (sec_dists); test_i += 1)
	{
	  mt_init (& static_mtrand, 0x9f73f7fc);

	  in_head  = xd3_alloc_output (stream, NULL);
	  out_head = xd3_alloc_output (stream, NULL);
	  enc_stream = sec->alloc (stream);
	  dec_input = NULL;
	  dec_output = NULL;
	  dec_correct = NULL;

	  if (in_head == NULL || out_head == NULL || enc_stream == NULL)
	    {
	      goto nomem;
	    }

	  if ((ret = sec_dists[test_i] (stream, in_head))) { goto fail; }

	  if ((ret = sec->init (stream, enc_stream, 1)) != 0) { goto fail; }

	  /* Encode data */
	  if ((ret = sec->encode (stream, enc_stream,
				  in_head, out_head, & cfg)))
	    {
	      XPR(NT "test %u: encode: %s", test_i, stream->msg);
	      goto fail;
	    }

	  /* Calculate sizes, allocate contiguous arrays for decoding */
	  input_size    = xd3_sizeof_output (in_head);
	  compress_size = xd3_sizeof_output (out_head);

	  XPR(NTR "%.3f", 8.0 * (double) compress_size / (double) input_size);

	  if ((dec_input   = (uint8_t*) xd3_alloc (stream, compress_size, 1)) == NULL ||
	      (dec_output  = (uint8_t*) xd3_alloc (stream, input_size, 1)) == NULL ||
	      (dec_correct = (uint8_t*) xd3_alloc (stream, input_size, 1)) == NULL)
	    {
	      goto nomem;
	    }

	  /* Fill the compressed data array */
	  for (p_off = 0, p = out_head; p != NULL;
	       p_off += p->next, p = p->next_page)
	    {
	      memcpy (dec_input + p_off, p->base, p->next);
	    }

	  CHECK(p_off == compress_size);

	  /* Fill the input data array */
	  for (p_off = 0, p = in_head; p != NULL;
	       p_off += p->next, p = p->next_page)
	    {
	      memcpy (dec_correct + p_off, p->base, p->next);
	    }

	  CHECK(p_off == input_size);

	  if ((ret = test_secondary_decode (stream, sec, input_size,
					    compress_size, dec_input,
					    dec_correct, dec_output)))
	    {
	      XPR(NT "test %u: decode: %s", test_i, stream->msg);
	      goto fail;
	    }

	  /* Single-bit error test, only cover the first 10 bytes.
	   * Some non-failures are expected in the Huffman case:
	   * Changing the clclen array, for example, may not harm the
	   * decoding.  Really looking for faults here. */
	  {
	    int i;
	    int bytes = min (compress_size, 10U);
	    for (i = 0; i < bytes * 8; i += 1)
	      {
		dec_input[i/8] ^= 1 << (i%8);

		if ((ret = test_secondary_decode (stream, sec, input_size,
						  compress_size, dec_input,
						  dec_correct, dec_output))
		    == 0)
		  {
		    /*XPR(NT "test %u: decode single-bit [%u/%u]
		      error non-failure", test_i, i/8, i%8);*/
		  }

		dec_input[i/8] ^= 1 << (i%8);

		if ((i % (2*bytes)) == (2*bytes)-1)
		  {
		    DOT ();
		  }
	      }
	    ret = 0;
	  }

	  if (0) { nomem: ret = ENOMEM; }

	fail:
	  sec->destroy (stream, enc_stream);
	  xd3_free_output (stream, in_head);
	  xd3_free_output (stream, out_head);
	  xd3_free (stream, dec_input);
	  xd3_free (stream, dec_output);
	  xd3_free (stream, dec_correct);

	  if (ret != 0) { return ret; }
	}
    }

  return 0;
}

Project: xdelta-devel

reset_defaults(void)
{
  option_stdout = 0;
  option_force = 0;
  option_verbose = DEFAULT_VERBOSE;
  option_quiet = 0;
  option_appheader = NULL;
  option_use_secondary = 0;
  option_secondary = NULL;
  option_use_altcodetable = 0;
  option_smatch_config = NULL;
  option_no_compress = 0;
  option_no_output = 0;
  option_source_filename = NULL;
  program_name = NULL;
  appheader_used = NULL;
  main_bdata = NULL;
  main_bsize = 0;
  allow_fake_source = 0;
  option_smatch_config = NULL;

  main_lru_reset();

  option_use_appheader = 1;
  option_use_checksum = 1;
#if EXTERNAL_COMPRESSION
  option_force2 = 0;
  option_decompress_inputs  = 1;
  option_recompress_outputs = 1;
  num_subprocs = 0;
#endif
#if VCDIFF_TOOLS
  option_print_cpymode = 1;
#endif
  option_level = XD3_DEFAULT_LEVEL;
  option_iopt_size = XD3_DEFAULT_IOPT_SIZE;
  option_winsize = XD3_DEFAULT_WINSIZE;
  option_srcwinsz = XD3_DEFAULT_SRCWINSZ;
  option_sprevsz = XD3_DEFAULT_SPREVSZ;
}

Project: xdelta-devel

main_format_millis (long millis, shortbuf *buf)
{
  if (millis < 1000)
    { 
      short_sprintf (*buf, "%lu ms", millis); 
    }
  else if (millis < 10000) 
    {
      short_sprintf (*buf, "%.1f sec", millis / 1000.0);
    }
  else
    {
      short_sprintf (*buf, "%lu sec", millis / 1000L); 
    }
  return buf->buf;
}

Project: xdelta-devel

main_alloc (void   *opaque,
	    size_t  items,
	    usize_t  size)
{
  return main_malloc1 (items * size);
}

Project: xdelta-devel

main_print_overflow (int x)
{
  XPR(NT "internal print buffer overflow: %d bytes\n", x);
  return XD3_INTERNAL;
}

Project: xdelta-devel

main_free (void *ptr)
{
  if (ptr)
    {
      IF_DEBUG (main_mallocs -= 1);
      main_free1 (NULL, ptr);
      IF_DEBUG (XD3_ASSERT(main_mallocs >= 0));
    }
}

Project: xdelta-devel

xprintf (const char *fmt, ...)
{
  char buf[1000];
  va_list a;
  int size;
  va_start (a, fmt);
  size = vsnprintf_func (buf, 1000, fmt, a);
  va_end (a);
  if (size < 0)
    {
      size = sizeof(buf) - 1;
      buf[size] = 0;
    }
  if (xprintf_message_func != NULL) {
    xprintf_message_func(buf);
  } else {
    size_t ignore = fwrite(buf, 1, size, stderr);
    (void) ignore;
  }
}

Project: xdelta-devel

main_format_bcnt (xoff_t r, shortbuf *buf)
{
  static const char* fmts[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
  usize_t i;

  for (i = 0; i < SIZEOF_ARRAY(fmts) - 1; i += 1)
    {
      xoff_t new_r;

      if (r == 0)
	{
	  short_sprintf (*buf, "0 %s", fmts[i]);
	  return buf->buf;
	}

      if (r >= 1 && r < 10)
	{
	  short_sprintf (*buf, "%.2f %s", (double) r, fmts[i]);
	  return buf->buf;
	}

      if (r >= 10 && r < 100)
	{
	  short_sprintf (*buf, "%.1f %s", (double) r, fmts[i]);
	  return buf->buf;
	}

      if (r >= 100 && r < 1000)
	{
	  short_sprintf (*buf, "%"Q"u %s", r, fmts[i]);
	  return buf->buf;
	}

      new_r = r / 1024;

      if (new_r < 10)
	{
	  short_sprintf (*buf, "%.2f %s", (double) r / 1024.0, fmts[i + 1]);
	  return buf->buf;
	}

      if (new_r < 100)
	{
	  short_sprintf (*buf, "%.1f %s", (double) r / 1024.0, fmts[i + 1]);
	  return buf->buf;
	}

      r = new_r;
    }
  XD3_ASSERT (0);
  return "";
}

Project: xdelta-devel

test_force_behavior (xd3_stream *stream, int ignore)
{
  int ret;
  char buf[TESTBUFSIZE];

  /* Create empty target file */
  test_setup ();
  snprintf_func (buf, TESTBUFSIZE, "cp /dev/null %s", TEST_TARGET_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  /* Encode to delta file */
  snprintf_func (buf, TESTBUFSIZE, "%s -e %s %s", program_name,
	   TEST_TARGET_FILE, TEST_DELTA_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  /* Encode again, should fail. */
  snprintf_func (buf, TESTBUFSIZE, "%s -q -e %s %s ", program_name,
	   TEST_TARGET_FILE, TEST_DELTA_FILE);
  if ((ret = do_fail (stream, buf))) { return ret; }

  /* Force it, should succeed. */
  snprintf_func (buf, TESTBUFSIZE, "%s -f -e %s %s", program_name,
	   TEST_TARGET_FILE, TEST_DELTA_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }
  test_cleanup();
  return 0;
}

Project: xdelta-devel

main_get_appheader_params (main_file *file, char **parsed,
			   int output, const char *type,
			   main_file *other)
{
  /* Set the filename if it was not specified.  If output, option_stdout (-c)
   * overrides. */
  if (file->filename == NULL &&
      ! (output && option_stdout) &&
      strcmp (parsed[0], "-") != 0)
    {
      file->filename = parsed[0];

      if (other->filename != NULL) {
	/* Take directory from the other file, if it has one. */
	/* TODO: This results in nonsense names like /dev/foo.tar.gz
	 * and probably the filename-default logic interferes with
	 * multi-file operation and the standard file extension?
	 * Possibly the name header is bad, should be off by default.
	 * Possibly we just want to remember external/compression
	 * settings. */
	const char *last_slash = strrchr(other->filename, '/');

	if (last_slash != NULL) {
	  usize_t dlen = (usize_t) (last_slash - other->filename);

	  XD3_ASSERT(file->filename_copy == NULL);
	  file->filename_copy =
	    (char*) main_malloc(dlen + 2 + (usize_t) strlen(file->filename));

	  strncpy(file->filename_copy, other->filename, dlen);
	  file->filename_copy[dlen] = '/';
	  strcpy(file->filename_copy + dlen + 1, parsed[0]);

	  file->filename = file->filename_copy;
	}
      }

      if (! option_quiet)
	{
	  XPR(NT "using default %s filename: %s\n", type, file->filename);
	}
    }

  /* Set the compressor, initiate de/recompression later. */
  if (file->compressor == NULL && *parsed[1] != 0)
    {
      file->flags |= RD_DECOMPSET;
      file->compressor = main_get_compressor (parsed[1]);
    }
}

Project: xdelta-devel

main_file_seek (main_file *xfile, xoff_t pos)
{
  int ret = 0;

#if XD3_STDIO
  if (fseek (xfile->file, pos, SEEK_SET) != 0) { ret = get_errno (); }

#elif XD3_POSIX
  if ((xoff_t) lseek (xfile->file, pos, SEEK_SET) != pos)
    { ret = get_errno (); }

#elif XD3_WIN32
# if (_WIN32_WINNT >= 0x0500)
  LARGE_INTEGER move, out;
  move.QuadPart = pos;
  if (SetFilePointerEx(xfile->file, move, &out, FILE_BEGIN) == 0)
    {
      ret = get_errno ();
    }
# else
  if (SetFilePointer(xfile->file, (LONG)pos, NULL, FILE_BEGIN) ==
      INVALID_SET_FILE_POINTER)
    {
      ret = get_errno ();
    }
# endif
#endif

  return ret;
}

Project: xdelta-devel

test_stdout_behavior (xd3_stream *stream, int ignore)
{
  int ret;
  char buf[TESTBUFSIZE];

  test_setup();
  snprintf_func (buf, TESTBUFSIZE, "cp /dev/null %s", TEST_TARGET_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  /* Without -c, encode writes to delta file */
  snprintf_func (buf, TESTBUFSIZE, "%s -e %s %s", program_name,
	   TEST_TARGET_FILE, TEST_DELTA_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  /* With -c, encode writes to stdout */
  snprintf_func (buf, TESTBUFSIZE, "%s -e -c %s > %s", program_name,
	   TEST_TARGET_FILE, TEST_DELTA_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  /* Without -c, decode writes to target file name, but it fails because the
   * file exists. */
  snprintf_func (buf, TESTBUFSIZE, "%s -q -d %s ", program_name, TEST_DELTA_FILE);
  if ((ret = do_fail (stream, buf))) { return ret; }

  /* With -c, decode writes to stdout */
  snprintf_func (buf, TESTBUFSIZE, "%s -d -c %s > /dev/null", program_name, TEST_DELTA_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }
  test_cleanup();

  return 0;
}

Project: xdelta-devel

main_get_winsize (main_file *ifile) {
  xoff_t file_size = 0;
  usize_t size = option_winsize;
  static shortbuf iszbuf;

  if (main_file_stat (ifile, &file_size) == 0)
    {
      size = (usize_t) min(file_size, (xoff_t) size);
    }

  size = max(size, XD3_ALLOCSIZE);

  if (option_verbose > 1)
    {
      XPR(NT "input %s window size %s\n",
	  ifile->filename,
	  main_format_bcnt (size, &iszbuf));
    }

  return size;
}

Project: xdelta-devel

main_external_compression_cleanup (void)
{
  int i;

  for (i = 0; i < num_subprocs; i += 1)
    {
      if (! ext_subprocs[i]) { continue; }

      kill (ext_subprocs[i], SIGTERM);

      ext_subprocs[i] = 0;
    }
}

Project: xdelta-devel

test_string_matching (xd3_stream *stream, int ignore)
{
  usize_t i;
  int ret;
  xd3_config config;
  char rbuf[TESTBUFSIZE];

  for (i = 0; i < SIZEOF_ARRAY (match_tests); i += 1)
    {
      const string_match_test *test = & match_tests[i];
      char *rptr = rbuf;
      usize_t len = (usize_t) strlen (test->input);

      xd3_free_stream (stream);
      xd3_init_config (& config, 0);

      config.smatch_cfg   = XD3_SMATCH_SOFT;
      config.smatcher_soft.large_look   = 4;
      config.smatcher_soft.large_step   = 4;
      config.smatcher_soft.small_look   = 4;
      config.smatcher_soft.small_chain  = 10;
      config.smatcher_soft.small_lchain = 10;
      config.smatcher_soft.max_lazy     = (test->flags & SM_LAZY) ? 10 : 0;
      config.smatcher_soft.long_enough  = 10;

      if ((ret = xd3_config_stream (stream, & config))) { return ret; }
      if ((ret = xd3_encode_init_full (stream))) { return ret; }

      xd3_avail_input (stream, (uint8_t*)test->input, len);

      if ((ret = stream->smatcher.string_match (stream))) { return ret; }

      *rptr = 0;
      while (! xd3_rlist_empty (& stream->iopt_used))
	{
	  xd3_rinst *inst = xd3_rlist_pop_front (& stream->iopt_used);

	  switch (inst->type)
	    {
	    case XD3_RUN: *rptr++ = 'R'; break;
	    case XD3_CPY: *rptr++ = 'C'; break;
	    default: CHECK(0);
	    }

	  snprintf_func (rptr, rbuf+TESTBUFSIZE-rptr, "%d/%d",
			 inst->pos, inst->size);
	  rptr += strlen (rptr);

	  if (inst->type == XD3_CPY)
	    {
	      *rptr++ = '@';
	      snprintf_func (rptr, rbuf+TESTBUFSIZE-rptr, "%"Q"d", inst->addr);
	      rptr += strlen (rptr);
	    }

	  *rptr++ = ' ';

	  xd3_rlist_push_back (& stream->iopt_free, inst);
	}

      if (rptr != rbuf)
	{
	  rptr -= 1; *rptr = 0;
	}

      if (strcmp (rbuf, test->result) != 0)
	{
	  XPR(NT "test %u: expected %s: got %s", i, test->result, rbuf);
	  stream->msg = "wrong result";
	  return XD3_INTERNAL;
	}
    }

  return 0;
}

Project: xdelta-devel

test_compressed_pipe (xd3_stream *stream, main_extcomp *ext, char* buf,
		      const char* comp_options, const char* decomp_options,
		      int do_ext_recomp, const char* msg)
{
  int ret;
  char decomp_buf[TESTBUFSIZE];

  if (do_ext_recomp)
    {
      snprintf_func (decomp_buf, TESTBUFSIZE,
		" | %s %s", ext->decomp_cmdname, ext->decomp_options);
    }
  else
    {
      decomp_buf[0] = 0;
    }

  snprintf_func (buf, TESTBUFSIZE, "%s %s < %s | %s %s | %s %s%s > %s",
	   ext->recomp_cmdname, ext->recomp_options,
	   TEST_TARGET_FILE,
	   program_name, comp_options,
	   program_name, decomp_options,
	   decomp_buf,
	   TEST_RECON_FILE);

  if ((ret = system (buf)) != 0)
    {
      stream->msg = msg;
      return XD3_INTERNAL;
    }

  if ((ret = test_compare_files (TEST_TARGET_FILE, TEST_RECON_FILE)))
    {
      return XD3_INTERNAL;
    }

  DOT ();
  return 0;
}

Project: xdelta-devel

get_errno (void)
{
#ifndef _WIN32
  if (errno == 0)
    {
      XPR(NT "you found a bug: expected errno != 0\n");
      errno = XD3_INTERNAL;
    }
  return errno;
#else
  DWORD err_num = GetLastError();
  if (err_num == NO_ERROR)
    {
      err_num = XD3_INTERNAL;
    }
  return err_num;
#endif
}

Project: xdelta-devel

void mt_init(mtrand *mt, uint32_t seed) {
  int i;
  mt->mt_buffer_[0] = seed;
  mt->mt_index_ = MT_LEN;
  for (i = 1; i < MT_LEN; i++) {
    /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
    /* In the previous versions, MSBs of the seed affect   */
    /* only MSBs of the array mt[].                        */
    /* 2002/01/09 modified by Makoto Matsumoto             */
    mt->mt_buffer_[i] =
	(1812433253UL * (mt->mt_buffer_[i-1] ^
			 (mt->mt_buffer_[i-1] >> 30)) + i);
  }
}

Project: xdelta-devel

sec_dist_func5 (xd3_stream *stream, xd3_output *data)
{
  int i, ret, x;
  for (i = 0; i < ALPHABET_SIZE*20; i += 1)
    {
      x = mt_exp_rand (10, ALPHABET_SIZE-1);
      if ((ret = xd3_emit_byte (stream, & data, x))) { return ret; }
    }
  return 0;
}

Project: xdelta-devel

xsnprintf_func (char *str, int n, const char *fmt, ...)
{
  va_list a;
  int ret;
  va_start (a, fmt);
  ret = vsnprintf_func (str, n, fmt, a);
  va_end (a);
  if (ret < 0)
    {
      ret = n;
    }
  return ret;
}

Project: xdelta-devel

test_source_cksum_offset (xd3_stream *stream, int ignore)
{
  xd3_source source;

  // Inputs are:
  struct {
    xoff_t   cpos;   // stream->srcwin_cksum_pos;
    xoff_t   ipos;   // stream->total_in;
    xoff_t   size;   // stream->src->size;

    usize_t  input;  // input  32-bit offset
    xoff_t   output; // output 64-bit offset

  } cksum_test[] = {
    // If cpos is <= 2^32
    { 1, 1, 1, 1, 1 },

#if XD3_USE_LARGEFILE64
//    cpos            ipos            size            input         output
//    0x____xxxxxULL, 0x____xxxxxULL, 0x____xxxxxULL, 0x___xxxxxUL, 0x____xxxxxULL
    { 0x100100000ULL, 0x100000000ULL, 0x100200000ULL, 0x00000000UL, 0x100000000ULL },
    { 0x100100000ULL, 0x100000000ULL, 0x100200000ULL, 0xF0000000UL, 0x0F0000000ULL },

    { 0x100200000ULL, 0x100100000ULL, 0x100200000ULL, 0x00300000UL, 0x000300000ULL },

    { 25771983104ULL, 25770000000ULL, 26414808769ULL, 2139216707UL, 23614053187ULL },

#endif

    { 0, 0, 0, 0, 0 },
  }, *test_ptr;

  stream->src = &source;

  for (test_ptr = cksum_test; test_ptr->cpos; test_ptr++) {
	xoff_t r;
    stream->srcwin_cksum_pos = test_ptr->cpos;
    stream->total_in = test_ptr->ipos;

    r = xd3_source_cksum_offset(stream, test_ptr->input);
    CHECK(r == test_ptr->output);
  }
  return 0;
}

Project: xdelta-devel

void* main_bufalloc (size_t size) {
#if XD3_WIN32
  return VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
#else
  return main_malloc1(size);
#endif
}

Project: xdelta-devel

main_malloc1 (size_t size)
{
  void* r = malloc (size);
  if (r == NULL) { XPR(NT "malloc: %s\n", xd3_mainerror (ENOMEM)); }
  return r;
}

Project: xdelta-devel

test_secondary_decode (xd3_stream         *stream,
		       const xd3_sec_type *sec,
		       usize_t              input_size,
		       usize_t              compress_size,
		       const uint8_t      *dec_input,
		       const uint8_t      *dec_correct,
		       uint8_t            *dec_output)
{
  int ret;
  xd3_sec_stream *dec_stream;
  const uint8_t *dec_input_used, *dec_input_end;
  uint8_t *dec_output_used, *dec_output_end;

  if ((dec_stream = sec->alloc (stream)) == NULL) { return ENOMEM; }

  if ((ret = sec->init (stream, dec_stream, 0)) != 0) { goto fail; }

  dec_input_used = dec_input;
  dec_input_end  = dec_input + compress_size;

  dec_output_used = dec_output;
  dec_output_end  = dec_output + input_size;

  if ((ret = sec->decode (stream, dec_stream,
			  & dec_input_used, dec_input_end,
			  & dec_output_used, dec_output_end)))
    {
      goto fail;
    }

  if (dec_input_used != dec_input_end)
    {
      stream->msg = "unused input";
      ret = XD3_INTERNAL;
      goto fail;
    }

  if (dec_output_used != dec_output_end)
    {
      stream->msg = "unfinished output";
      ret = XD3_INTERNAL;
      goto fail;
    }

  if (memcmp (dec_output, dec_correct, input_size) != 0)
    {
      stream->msg = "incorrect output";
      ret = XD3_INTERNAL;
      goto fail;
    }

 fail:
  sec->destroy (stream, dec_stream);
  return ret;
}

Project: xdelta-devel

test_source_decompression (xd3_stream *stream, int ignore)
{
  int ret;
  char buf[TESTBUFSIZE];
  const main_extcomp *ext;
  xoff_t dsize;

  mt_init (& static_mtrand, 0x9f73f7fc);

  test_setup ();
  if ((ret = test_make_inputs (stream, NULL, NULL))) { return ret; }

  /* Use gzip. */
  if ((ext = main_get_compressor ("G")) == NULL)
    {
      XPR(NT "skipped");
      return 0;
    }

  /* Save an uncompressed copy. */
  if ((ret = test_save_copy (TEST_TARGET_FILE))) { return ret; }

  /* Compress the source. */
  snprintf_func (buf, TESTBUFSIZE, "%s -1 %s < %s > %s", ext->recomp_cmdname,
	   ext->recomp_options, TEST_COPY_FILE, TEST_SOURCE_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }
  /* Compress the target. */
  snprintf_func (buf, TESTBUFSIZE, "%s -9 %s < %s > %s", ext->recomp_cmdname,
	   ext->recomp_options, TEST_COPY_FILE, TEST_TARGET_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  /* Now the two identical files are compressed.  Delta-encode the target,
   * with decompression. */
  snprintf_func (buf, TESTBUFSIZE, "%s -e -vfq -s%s %s %s", program_name, TEST_SOURCE_FILE,
	   TEST_TARGET_FILE, TEST_DELTA_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  /* Check that the compressed file is small (b/c inputs are
   * identical). */
  if ((ret = test_file_size (TEST_DELTA_FILE, & dsize))) { return ret; }
  /* Deltas for identical files should be very small. */
  if (dsize > 200)
    {
      XPR(NT "external compression did not happen\n");
      stream->msg = "external compression did not happen";
      return XD3_INTERNAL;
    }

  /* Decode the delta file with recompression disabled, should get an
   * uncompressed file out. */
  snprintf_func (buf, TESTBUFSIZE, "%s -v -dq -R -s%s %s %s", program_name,
	   TEST_SOURCE_FILE, TEST_DELTA_FILE, TEST_RECON_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }
  if ((ret = test_compare_files (TEST_COPY_FILE,
			    TEST_RECON_FILE))) { return ret; }

  /* Decode the delta file with recompression, should get a compressed file
   * out.  But we can't compare compressed files directly. */
  snprintf_func (buf, TESTBUFSIZE, "%s -v -dqf -s%s %s %s", program_name,
	   TEST_SOURCE_FILE, TEST_DELTA_FILE, TEST_RECON_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }
  snprintf_func (buf, TESTBUFSIZE, "%s %s < %s > %s", ext->decomp_cmdname, ext->decomp_options,
	   TEST_RECON_FILE, TEST_RECON2_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }
  if ((ret = test_compare_files (TEST_COPY_FILE,
			    TEST_RECON2_FILE))) { return ret; }

  /* Encode with decompression disabled */
  snprintf_func (buf, TESTBUFSIZE, "%s -e -D -vfq -s%s %s %s", program_name,
	   TEST_SOURCE_FILE, TEST_TARGET_FILE, TEST_DELTA_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }

  /* Decode the delta file with decompression disabled, should get the
   * identical compressed file out. */
  snprintf_func (buf, TESTBUFSIZE, "%s -d -D -vfq -s%s %s %s", program_name,
	   TEST_SOURCE_FILE, TEST_DELTA_FILE, TEST_RECON_FILE);
  if ((ret = do_cmd (stream, buf))) { return ret; }
  if ((ret = test_compare_files (TEST_TARGET_FILE,
			    TEST_RECON_FILE))) { return ret; }

  test_cleanup();
  return 0;
}

Project: xdelta-devel

sec_dist_func3 (xd3_stream *stream, xd3_output *data)
{
  int i, ret;
  for (i = 0; i < ALPHABET_SIZE; i += 1)
    {
      if ((ret = xd3_emit_byte (stream, & data, i%ALPHABET_SIZE))) { return ret; }
    }
  return 0;
}

Project: xdelta-devel

test_compare_files (const char* tgt, const char *rec)
{
  FILE *orig, *recons;
  static uint8_t obuf[TESTBUFSIZE], rbuf[TESTBUFSIZE];
  xoff_t offset = 0;
  size_t i;
  size_t oc, rc;
  xoff_t diffs = 0;

  if ((orig = fopen (tgt, "r")) == NULL)
    {
      XPR(NT "open %s failed\n", tgt);
      return get_errno ();
    }

  if ((recons = fopen (rec, "r")) == NULL)
    {
      XPR(NT "open %s failed\n", rec);
      return get_errno ();
    }

  for (;;)
    {
      oc = fread (obuf, 1, TESTBUFSIZE, orig);
      rc = fread (rbuf, 1, TESTBUFSIZE, recons);

      if (oc != rc)
	{
	  return XD3_INTERNAL;
	}

      if (oc == 0)
	{
	  break;
	}

      for (i = 0; i < oc; i += 1)
	{
	  if (obuf[i] != rbuf[i])
 	    {
	      XPR(NT "byte %u (read %u @ %"Q"u) %d != %d\n",
		  (int)i, (int)oc, offset, obuf[i], rbuf[i]);
	      diffs++;
	      return XD3_INTERNAL;
	    }
	}

      offset += oc;
    }

    fclose (orig);
    fclose (recons);
    if (diffs != 0)
      {
	return XD3_INTERNAL;
      }
    return 0;
}

Project: xdelta-devel

main_init_recode_stream (void)
{
  int ret;
  int stream_flags = XD3_ADLER32_NOVER | XD3_SKIP_EMIT;
  int recode_flags;
  xd3_config recode_config;

  XD3_ASSERT (recode_stream == NULL);

  if ((recode_stream = (xd3_stream*) main_malloc(sizeof(xd3_stream))) == NULL)
    {
      return ENOMEM;
    }

  recode_flags = (stream_flags & XD3_SEC_TYPE);

  recode_config.alloc = main_alloc;
  recode_config.freef = main_free1;

  xd3_init_config(&recode_config, recode_flags);

  if ((ret = main_set_secondary_flags (&recode_config)) ||
      (ret = xd3_config_stream (recode_stream, &recode_config)) ||
      (ret = xd3_encode_init_partial (recode_stream)) ||
      (ret = xd3_whole_state_init (recode_stream)))
    {
      XPR(NT XD3_LIB_ERRMSG (recode_stream, ret));
      xd3_free_stream (recode_stream);
      recode_stream = NULL;
      return ret;
    }

  return 0;
}

Project: xdelta-devel

main_external_compression_finish (void)
{
  int i;
  int ret;

  for (i = 0; i < num_subprocs; i += 1)
    {
      if (! ext_subprocs[i]) { continue; }

      if ((ret = main_waitpid_check (ext_subprocs[i])))
	{
	  return ret;
	}

      ext_subprocs[i] = 0;
    }

  return 0;
}

Project: xdelta-devel

main_get_compressor (const char *ident)
{
  const main_extcomp *ext = main_ident_compressor (ident);

  if (ext == NULL)
    {
      if (! option_quiet)
	{
	  XPR(NT "warning: cannot recompress output: "
		   "unrecognized external compression ID: %s\n", ident);
	}
      return NULL;
    }
  else if (! EXTERNAL_COMPRESSION)
    {
      if (! option_quiet)
	{
	  XPR(NT "warning: external support not compiled: "
		   "original input was compressed: %s\n", ext->recomp_cmdname);
	}
      return NULL;
    }
  else
    {
      return ext;
    }
}

Project: xdelta-devel

xd3_selftest (void)
{
#define DO_TEST(fn,flags,arg)                                         \
  do {                                                                \
    xd3_stream stream;                                                \
    xd3_config config;                                                \
    xd3_init_config (& config, flags);                                \
    XPR(NT "testing " #fn "%s...",                          \
             flags ? (" (" #flags ")") : "");                         \
    if ((ret = xd3_config_stream (& stream, & config) == 0) &&        \
        (ret = test_ ## fn (& stream, arg)) == 0) {                   \
      XPR(NTR " success\n");                                          \
    } else {                                                          \
      XPR(NTR " failed: %s: %s\n", xd3_errstring (& stream),          \
               xd3_mainerror (ret)); }                                \
    xd3_free_stream (& stream);                                       \
    if (ret != 0) { goto failure; }                                   \
  } while (0)

  int ret;
  DO_TEST (random_numbers, 0, 0);

  DO_TEST (decode_integer_end_of_input, 0, 0);
  DO_TEST (decode_integer_overflow, 0, 0);
  DO_TEST (encode_decode_uint32_t, 0, 0);
  DO_TEST (encode_decode_uint64_t, 0, 0);
  DO_TEST (usize_t_overflow, 0, 0);
  DO_TEST (forward_match, 0, 0);

  DO_TEST (address_cache, 0, 0);
  IF_GENCODETBL (DO_TEST (address_cache, XD3_ALT_CODE_TABLE, 0));

  DO_TEST (string_matching, 0, 0);
  DO_TEST (choose_instruction, 0, 0);
  DO_TEST (identical_behavior, 0, 0);
  DO_TEST (in_memory, 0, 0);

  IF_GENCODETBL (DO_TEST (choose_instruction, XD3_ALT_CODE_TABLE, 0));
  IF_GENCODETBL (DO_TEST (encode_code_table, 0, 0));

  DO_TEST (iopt_flush_instructions, 0, 0);
  DO_TEST (source_cksum_offset, 0, 0);

  DO_TEST (decompress_single_bit_error, 0, 3);
  DO_TEST (decompress_single_bit_error, XD3_ADLER32, 3);

  IF_LZMA (DO_TEST (decompress_single_bit_error, XD3_SEC_LZMA, 54));
  IF_FGK (DO_TEST (decompress_single_bit_error, XD3_SEC_FGK, 3));
  IF_DJW (DO_TEST (decompress_single_bit_error, XD3_SEC_DJW, 8));

  /* There are many expected non-failures for ALT_CODE_TABLE because
   * not all of the instruction codes are used. */
  IF_GENCODETBL (
	 DO_TEST (decompress_single_bit_error, XD3_ALT_CODE_TABLE, 224));

#if SHELL_TESTS
  DO_TEST (force_behavior, 0, 0);
  DO_TEST (stdout_behavior, 0, 0);
  DO_TEST (no_output, 0, 0);
  DO_TEST (appheader, 0, 0);
  DO_TEST (command_line_arguments, 0, 0);

#if EXTERNAL_COMPRESSION
  DO_TEST (source_decompression, 0, 0);
  DO_TEST (externally_compressed_io, 0, 0);
#endif

  DO_TEST (recode_command, 0, 0);
#endif

  IF_LZMA (DO_TEST (secondary_lzma, 0, 1));
  IF_DJW (DO_TEST (secondary_huff, 0, DJW_MAX_GROUPS));
  IF_FGK (DO_TEST (secondary_fgk, 0, 1));

  DO_TEST (compressed_stream_overflow, 0, 0);
  IF_LZMA (DO_TEST (compressed_stream_overflow, XD3_SEC_LZMA, 0));

failure:
  test_cleanup ();
  return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
#undef DO_TEST
}

Project: xdelta-devel

static int do_cmd (xd3_stream *stream, const char *buf)
{
  int ret;
  if ((ret = system (buf)) != 0)
    {
      if (WIFEXITED (ret))
	{
	  stream->msg = "command exited non-zero";
	  IF_DEBUG1 (XPR(NT "command was: %s\n", buf));
	}
      else
	{
	  stream->msg = "abnormal command termination";
	}
      return ret;
    }
  return 0;
}

Project: xdelta-devel

int test_setup (void)
{
  static int x = 0;
  x++;
  snprintf_func (TEST_TARGET_FILE, TESTFILESIZE, "/tmp/xdtest.target.%d", x);
  snprintf_func (TEST_SOURCE_FILE, TESTFILESIZE, "/tmp/xdtest.source.%d", x);
  snprintf_func (TEST_DELTA_FILE, TESTFILESIZE, "/tmp/xdtest.delta.%d", x);
  snprintf_func (TEST_RECON_FILE, TESTFILESIZE, "/tmp/xdtest.recon.%d", x);
  snprintf_func (TEST_RECON2_FILE, TESTFILESIZE, "/tmp/xdtest.recon2.%d", x);
  snprintf_func (TEST_COPY_FILE, TESTFILESIZE, "/tmp/xdtest.copy.%d", x);
  snprintf_func (TEST_NOPERM_FILE, TESTFILESIZE, "/tmp/xdtest.noperm.%d", x);
  test_cleanup();
  return 0;
}

Project: xdelta-devel

main_file_close (main_file *xfile)
{
  int ret = 0;

  if (! main_file_isopen (xfile))
    {
      return 0;
    }

#if XD3_STDIO
  ret = fclose (xfile->file);
  xfile->file = NULL;

#elif XD3_POSIX
  ret = close (xfile->file);
  xfile->file = -1;

#elif XD3_WIN32
  if (!CloseHandle(xfile->file)) {
    ret = get_errno ();
  }
  xfile->file = INVALID_HANDLE_VALUE;
#endif

  if (ret != 0) { XF_ERROR ("close", xfile->filename, ret = get_errno ()); }
  return ret;
}

Project: xdelta-devel

sec_dist_func6 (xd3_stream *stream, xd3_output *data)
{
  int i, ret, x;
  for (i = 0; i < ALPHABET_SIZE*20; i += 1)
    {
      x = mt_random (&static_mtrand) % (ALPHABET_SIZE/2);
      if ((ret = xd3_emit_byte (stream, & data, x))) { return ret; }
    }
  return 0;
}

Project: xdelta-devel

main_get_appheader (xd3_stream *stream, main_file *ifile,
		    main_file *output, main_file *sfile)
{
  uint8_t *apphead;
  usize_t appheadsz;
  int ret;

  /* The user may disable the application header.  Once the appheader
   * is set, this disables setting it again. */
  if (! option_use_appheader) { return; }

  ret = xd3_get_appheader (stream, & apphead, & appheadsz);

  /* Ignore failure, it only means we haven't received a header yet. */
  if (ret != 0) { return; }

  if (appheadsz > 0)
    {
      const int kMaxArgs = 4;
      char *start = (char*)apphead;
      char *slash;
      int   place = 0;
      char *parsed[kMaxArgs];

      memset (parsed, 0, sizeof (parsed));

      while ((slash = strchr (start, '/')) != NULL && place < (kMaxArgs-1))
	{
	  *slash = 0;
	  parsed[place++] = start;
	  start = slash + 1;
	}

      parsed[place++] = start;

      /* First take the output parameters. */
      if (place == 2 || place == 4)
	{
	  main_get_appheader_params (output, parsed, 1, "output", ifile);
	}

      /* Then take the source parameters. */
      if (place == 4)
	{
	  main_get_appheader_params (sfile, parsed+2, 0, "source", ifile);
	}
    }

  option_use_appheader = 0;
  return;
}

Project: xdelta-devel

main_set_appheader (xd3_stream *stream, main_file *input, main_file *sfile)
{
  /* The user may disable the application header.  Once the appheader
   * is set, this disables setting it again. */
  if (appheader_used || ! option_use_appheader) { return 0; }

  /* The user may specify the application header, otherwise format the
     default header. */
  if (option_appheader)
    {
      appheader_used = option_appheader;
    }
  else
    {
      const char *iname;
      const char *icomp;
      const char *sname;
      const char *scomp;
      usize_t len;

      iname = main_apphead_string (input->filename);
      icomp = (input->compressor == NULL) ? "" : input->compressor->ident;
      len = (usize_t) strlen (iname) + (usize_t) strlen (icomp) + 2;

      if (sfile->filename != NULL)
	{
	  sname = main_apphead_string (sfile->filename);
	  scomp = (sfile->compressor == NULL) ? "" : sfile->compressor->ident;
	  len += (usize_t) strlen (sname) + (usize_t) strlen (scomp) + 2;
	}
      else
	{
	  sname = scomp = "";
	}

      if ((appheader_used = (uint8_t*) main_malloc (len)) == NULL)
	{
	  return ENOMEM;
	}

      if (sfile->filename == NULL)
	{
	  snprintf_func ((char*)appheader_used, len, "%s/%s", iname, icomp);
	}
      else
	{
	  snprintf_func ((char*)appheader_used, len, "%s/%s/%s/%s",
		    iname, icomp, sname, scomp);
	}
    }

  xd3_set_appheader (stream, appheader_used,
		     (usize_t) strlen ((char*)appheader_used));

  return 0;
}

Project: xdelta-devel

main_config (void)
{
  main_version ();

  XPR(NTR "EXTERNAL_COMPRESSION=%d\n", EXTERNAL_COMPRESSION);
  XPR(NTR "GENERIC_ENCODE_TABLES=%d\n", GENERIC_ENCODE_TABLES);
  XPR(NTR "GENERIC_ENCODE_TABLES_COMPUTE=%d\n", GENERIC_ENCODE_TABLES_COMPUTE);
  XPR(NTR "REGRESSION_TEST=%d\n", REGRESSION_TEST);
  XPR(NTR "SECONDARY_DJW=%d\n", SECONDARY_DJW);
  XPR(NTR "SECONDARY_FGK=%d\n", SECONDARY_FGK);
  XPR(NTR "SECONDARY_LZMA=%d\n", SECONDARY_LZMA);
  XPR(NTR "UNALIGNED_OK=%d\n", UNALIGNED_OK);
  XPR(NTR "VCDIFF_TOOLS=%d\n", VCDIFF_TOOLS);
  XPR(NTR "XD3_ALLOCSIZE=%d\n", XD3_ALLOCSIZE);
  XPR(NTR "XD3_DEBUG=%d\n", XD3_DEBUG);
  XPR(NTR "XD3_ENCODER=%d\n", XD3_ENCODER);
  XPR(NTR "XD3_POSIX=%d\n", XD3_POSIX);
  XPR(NTR "XD3_STDIO=%d\n", XD3_STDIO);
  XPR(NTR "XD3_WIN32=%d\n", XD3_WIN32);
  XPR(NTR "XD3_USE_LARGEFILE64=%d\n", XD3_USE_LARGEFILE64);
  XPR(NTR "XD3_DEFAULT_LEVEL=%d\n", XD3_DEFAULT_LEVEL);
  XPR(NTR "XD3_DEFAULT_IOPT_SIZE=%d\n", XD3_DEFAULT_IOPT_SIZE);
  XPR(NTR "XD3_DEFAULT_SPREVSZ=%d\n", XD3_DEFAULT_SPREVSZ);
  XPR(NTR "XD3_DEFAULT_SRCWINSZ=%d\n", XD3_DEFAULT_SRCWINSZ);
  XPR(NTR "XD3_DEFAULT_WINSIZE=%d\n", XD3_DEFAULT_WINSIZE);
  XPR(NTR "XD3_HARDMAXWINSIZE=%d\n", XD3_HARDMAXWINSIZE);
  XPR(NTR "sizeof(void*)=%d\n", (int)sizeof(void*));
  XPR(NTR "sizeof(int)=%d\n", (int)sizeof(int));
  XPR(NTR "sizeof(size_t)=%d\n", (int)sizeof(size_t));
  XPR(NTR "sizeof(uint32_t)=%d\n", (int)sizeof(uint32_t));
  XPR(NTR "sizeof(uint64_t)=%d\n", (int)sizeof(uint64_t));
  XPR(NTR "sizeof(usize_t)=%d\n", (int)sizeof(usize_t));
  XPR(NTR "sizeof(xoff_t)=%d\n", (int)sizeof(xoff_t));

  return EXIT_SUCCESS;
}

Project: xdelta-devel

main_merge_arguments (main_merge_list* merges)
{
  int ret = 0;
  int count = 0;
  main_merge *merge = NULL;
  xd3_stream merge_input;

  if (main_merge_list_empty (merges))
    {
      return 0;
    }

  if ((ret = xd3_config_stream (& merge_input, NULL)) ||
      (ret = xd3_whole_state_init (& merge_input)))
    {
      XPR(NT XD3_LIB_ERRMSG (& merge_input, ret));
      return ret;
    }

  merge = main_merge_list_front (merges);
  while (!main_merge_list_end (merges, merge))
    {
      main_file mfile;
      main_file_init (& mfile);
      mfile.filename = merge->filename;
      mfile.flags = RD_NONEXTERNAL;

      if ((ret = main_file_open (& mfile, merge->filename, XO_READ)))
        {
          goto error;
        }

      ret = main_input (CMD_MERGE_ARG, & mfile, NULL, NULL);

      if (ret == 0)
	{
	  if (count++ == 0)
	    {
	      /* The first merge source is the next merge input. */
	      xd3_swap_whole_state (& recode_stream->whole_target,
				    & merge_input.whole_target);
	    }
	  else
	    {
	      /* Merge the recode_stream with merge_input. */
	      ret = xd3_merge_input_output (recode_stream,
					    & merge_input.whole_target);

	      /* Save the next merge source in merge_input. */
	      xd3_swap_whole_state (& recode_stream->whole_target,
				    & merge_input.whole_target);
	    }
	}

      main_file_cleanup (& mfile);

      if (recode_stream != NULL)
        {
          xd3_free_stream (recode_stream);
          main_free (recode_stream);
          recode_stream = NULL;
        }

      if (main_bdata != NULL)
        {
          main_buffree (main_bdata);
          main_bdata = NULL;
	  main_bsize = 0;
        }

      if (ret != 0)
        {
	  goto error;
        }

      merge = main_merge_list_next (merge);
    }

  XD3_ASSERT (merge_stream == NULL);

  if ((merge_stream = (xd3_stream*) main_malloc (sizeof(xd3_stream))) == NULL)
    {
      ret = ENOMEM;
      goto error;
    }

  if ((ret = xd3_config_stream (merge_stream, NULL)) ||
      (ret = xd3_whole_state_init (merge_stream)))
    {
      XPR(NT XD3_LIB_ERRMSG (& merge_input, ret));
      goto error;
    }

  xd3_swap_whole_state (& merge_stream->whole_target,
			& merge_input.whole_target);
  ret = 0;
 error:
  xd3_free_stream (& merge_input);
  return ret;
}

Project: xdelta-devel

main_pipe_write (int outfd, uint8_t *exist_buf, usize_t remain)
{
  int ret;

  if ((ret = xd3_posix_io (outfd, exist_buf, remain,
			   (xd3_posix_func*) &write, NULL)))
    {
      return ret;
    }

  return 0;
}