keybox_get_keyblock (KEYBOX_HANDLE hd, iobuf_t *r_iobuf,
                     int *r_pk_no, int *r_uid_no, u32 **r_sigstatus)
{
  gpg_error_t err;
  const unsigned char *buffer, *p;
  size_t length;
  size_t image_off, image_len;
  size_t siginfo_off, siginfo_len;
  u32 *sigstatus, n, n_sigs, sigilen;

  *r_iobuf = NULL;
  *r_sigstatus = NULL;

  if (!hd)
    return gpg_error (GPG_ERR_INV_VALUE);
  if (!hd->found.blob)
    return gpg_error (GPG_ERR_NOTHING_FOUND);

  if (blob_get_type (hd->found.blob) != KEYBOX_BLOBTYPE_PGP)
    return gpg_error (GPG_ERR_WRONG_BLOB_TYPE);

  buffer = _keybox_get_blob_image (hd->found.blob, &length);
  if (length < 40)
    return gpg_error (GPG_ERR_TOO_SHORT);
  image_off = get32 (buffer+8);
  image_len = get32 (buffer+12);
  if (image_off+image_len > length)
    return gpg_error (GPG_ERR_TOO_SHORT);

  err = _keybox_get_flag_location (buffer, length, KEYBOX_FLAG_SIG_INFO,
                                   &siginfo_off, &siginfo_len);
  if (err)
    return err;
  n_sigs  = get16 (buffer + siginfo_off);
  sigilen = get16 (buffer + siginfo_off + 2);
  p = buffer + siginfo_off + 4;
  sigstatus = xtrymalloc ((1+n_sigs) * sizeof *sigstatus);
  if (!sigstatus)
    return gpg_error_from_syserror ();
  sigstatus[0] = n_sigs;
  for (n=1; n <= n_sigs; n++, p += sigilen)
    sigstatus[n] = get32 (p);

  *r_pk_no  = hd->found.pk_no;
  *r_uid_no = hd->found.uid_no;
  *r_sigstatus = sigstatus;
  *r_iobuf = iobuf_temp_with_content (buffer+image_off, image_len);
  return 0;
}
keybox_get_keyblock (KEYBOX_HANDLE hd, iobuf_t *r_iobuf,
                     int *r_pk_no, int *r_uid_no, u32 **r_sigstatus)
{
  gpg_error_t err;
  const unsigned char *buffer, *p;
  size_t length;
  size_t image_off, image_len;
  size_t siginfo_off, siginfo_len;
  u32 *sigstatus, n, n_sigs, sigilen;

  *r_iobuf = NULL;
  *r_sigstatus = NULL;

  if (!hd)
    return gpg_error (GPG_ERR_INV_VALUE);
  if (!hd->found.blob)
    return gpg_error (GPG_ERR_NOTHING_FOUND);

  if (blob_get_type (hd->found.blob) != KEYBOX_BLOBTYPE_PGP)
    return gpg_error (GPG_ERR_WRONG_BLOB_TYPE);

  buffer = _keybox_get_blob_image (hd->found.blob, &length);
  if (length < 40)
    return gpg_error (GPG_ERR_TOO_SHORT);
  image_off = get32 (buffer+8);
  image_len = get32 (buffer+12);
  if (image_off+image_len > length)
    return gpg_error (GPG_ERR_TOO_SHORT);

  err = _keybox_get_flag_location (buffer, length, KEYBOX_FLAG_SIG_INFO,
                                   &siginfo_off, &siginfo_len);
  if (err)
    return err;
  n_sigs  = get16 (buffer + siginfo_off);
  sigilen = get16 (buffer + siginfo_off + 2);
  p = buffer + siginfo_off + 4;
  sigstatus = xtrymalloc ((1+n_sigs) * sizeof *sigstatus);
  if (!sigstatus)
    return gpg_error_from_syserror ();
  sigstatus[0] = n_sigs;
  for (n=1; n <= n_sigs; n++, p += sigilen)
    sigstatus[n] = get32 (p);

  *r_pk_no  = hd->found.pk_no;
  *r_uid_no = hd->found.uid_no;
  *r_sigstatus = sigstatus;
  *r_iobuf = iobuf_temp_with_content (buffer+image_off, image_len);
  return 0;
}