Exemplo n.º 1
0
static void
do_pack(const void *in, size_t in_len, size_t buf_len,
        size_t offset, size_t fp_len, Byte options) {
  size_t buflen = bmz_pack_buflen(in_len), out_len = buflen;
  size_t worklen = bmz_pack_worklen(in_len, fp_len);
  int ret, bm_only = (options & BMZ_O_BM_ONLY) || s_bm_dump;
  Byte *out, *work_mem;

  if (bm_only) {
    out_len = in_len + 1;

    if (buf_len > in_len + worklen) {
      out = (Byte *)in + in_len;
      work_mem = out + out_len;
    }
    else {
      out = malloc(worklen); /* bmz_pack_worklen includes out_len for bm */

      if (!out)
        DIE("error allocating %lu bytes memory", (Lu)worklen);

      work_mem = out + out_len;
    }
    /* calling internal API need to align work memory */
    work_mem = BMZ_ALIGN(work_mem, 8);
  }
  else if (buf_len > buflen + worklen) {
    work_mem = (Byte *)in + buflen;
    out = (Byte *)in; /* inplace */
  }
  else {
    out = malloc(buflen + worklen);

    if (!out)
      DIE("error allocating %lu bytes memory", (Lu)buflen + worklen);

    work_mem = out + buflen;
  }

  if (bm_only) {
    ret = bmz_bm_pack_mask(in, in_len, out, &out_len, offset, fp_len,
                           work_mem, 257);
    if (ret != BMZ_E_OK)
      DIE("error encoding bm output (error %d)", ret);

    if (s_bm_dump) {
      if ((ret = bmz_bm_dump(out, out_len)) != BMZ_E_OK)
        WARN("error dumping bm encoding (ret=%d)", ret);

      return;
    }
  }
  else if ((ret = bmz_pack(in, in_len, out, &out_len, offset, fp_len,
                           (s_bm_hash << 24), work_mem))
           != BMZ_E_OK) {
    DIE("error compressing input (error %d)", ret);
  }
  write_bmz_header(1, in_len, bmz_checksum(out, out_len), options);
  write(1, out, out_len);
}
Exemplo n.º 2
0
static void
dump_bm(const char *label, const char *in, size_t len) {
  int ret;

  if (s_verbosity > 1) {
    printf("----%s encoded:\n", label);
    fwrite(in, 1, len, stdout);
  }
  if (s_bm_dump) {
    printf("\n----%s dumped:\n", label);

    if ((ret = bmz_bm_dump(in, len)) != BMZ_E_OK)
      LOG(1, "error: bad encoded data (ret=%d)", ret);

    puts("\n----end-dump");
  }
}