示例#1
0
文件: base64_test.c 项目: dun/munge
int
encode_context (char *dst, int *dstlen, const void *src, int srclen)
{
    base64_ctx x;
    int i;
    int n;
    int m;
    const unsigned char *p = src;

    if (base64_init (&x) < 0)
        return (-1);
    for (i = 0, n = 0; i < srclen; i++) {
        if (base64_encode_update (&x, dst, &m, p + i, 1) < 0)
            return (-1);
        dst += m;
        n += m;
    }
    if (base64_encode_final (&x, dst, &m) < 0)
        return (-1);
    if (base64_cleanup (&x) < 0)
        return (-1);
    n += m;
    *dstlen = n;
    return (0);
}
示例#2
0
static void
test_fuzz_once(struct base64_encode_ctx *encode,
	       struct base64_decode_ctx *decode,
	       size_t size, const uint8_t *input)
{
  size_t base64_len = BASE64_ENCODE_RAW_LENGTH (size);
  size_t out_len;
  uint8_t *base64 = xalloc (base64_len + 2);
  uint8_t *decoded = xalloc (size + 2);

  *base64++ = 0x12;
  base64[base64_len] = 0x34;

  *decoded++ = 0x56;
  decoded[size] = 0x78;

  out_len = base64_encode_update(encode, base64, size, input);
  ASSERT (out_len <= base64_len);
  out_len += base64_encode_final(encode, base64 + out_len);
  ASSERT (out_len == base64_len);
  ASSERT (base64[-1] == 0x12);
  ASSERT (base64[base64_len] == 0x34);

  ASSERT(base64_decode_update(decode, &out_len, decoded,
			      base64_len, base64));
  ASSERT(base64_decode_final(decode));
  ASSERT (out_len == size);
  ASSERT (decoded[-1] == 0x56);
  ASSERT (decoded[size] == 0x78);
  
  ASSERT(MEMEQ(size, input, decoded));
  free (base64 - 1);
  free (decoded - 1);
}
示例#3
0
// Convert uint8 array to string; return outsize
size_t uint8_to_str(uint8_t *in, unsigned char *out, size_t insize) {

    size_t outsize; //number of outgoing encoded bytes
    struct base64_encode_ctx encoder; //nettle encoder

    base64_encode_init(&encoder); //init encoder

    outsize = base64_encode_update(&encoder, out, insize, in); //encode first chunk
	out += outsize; //increment pointer
	outsize += base64_encode_final(&encoder, out); //encode final chunk, add padding

	return outsize; //return number of bytes written to out

}
示例#4
0
stl_string base64_encode (const char *buff, int bufflen) {
  stl_string ret;

  unsigned len = strlen (buff);
  char *res = (char *) malloc (BASE64_ENCODE_LENGTH (bufflen));

  struct base64_encode_ctx str;
  base64_encode_init (&str);
  len = base64_encode_update (&str, (uint8_t *) res, len, (uint8_t*) buff); // TODO: args?
  base64_encode_final (&str, (uint8_t*) res);
  ret.append (res, len);
  free (res);

  return ret;
}
示例#5
0
int
pgp_armor(struct nettle_buffer *buffer,
	  const char *tag,
	  unsigned length,
	  const uint8_t *data)
{
  struct base64_encode_ctx ctx;
  
  unsigned crc = pgp_crc24(length, data);

  base64_encode_init(&ctx);
  
  if (! (write_string(buffer, "BEGIN PGP ")
	 && write_string(buffer, tag)
	 && write_string(buffer, "\nComment: Nettle\n\n")))
    return 0;

  for (;
       length >= BINARY_PER_LINE;
       length -= BINARY_PER_LINE, data += BINARY_PER_LINE)
    {
      unsigned done;
      uint8_t *p
	= nettle_buffer_space(buffer, TEXT_PER_LINE);
      
      if (!p)
	return 0;

      done = base64_encode_update(&ctx, p, BINARY_PER_LINE, data);
      assert(done <= TEXT_PER_LINE);

      /* FIXME: Create some official way to do this */
      buffer->size -= (TEXT_PER_LINE - done);
      
      if (!NETTLE_BUFFER_PUTC(buffer, '\n'))
	return 0;
    }

  if (length)
    {
      unsigned text_size = BASE64_ENCODE_LENGTH(length)
	+ BASE64_ENCODE_FINAL_LENGTH;
      unsigned done;
      
      uint8_t *p
	= nettle_buffer_space(buffer, text_size);
      if (!p)
	return 0;

      done = base64_encode_update(&ctx, p, length, data);
      done += base64_encode_final(&ctx, p + done);

      /* FIXME: Create some official way to do this */
      buffer->size -= (text_size - done);
      
      if (!NETTLE_BUFFER_PUTC(buffer, '\n'))
	return 0;
    }
  /* Checksum */
  if (!NETTLE_BUFFER_PUTC(buffer, '='))
    return 0;

  {
    uint8_t *p = nettle_buffer_space(buffer, 4);
    if (!p)
      return 0;
    base64_encode_group(p, crc);
  }
  
  return (write_string(buffer, "\nBEGIN PGP ")
	  && write_string(buffer, tag)
	  && NETTLE_BUFFER_PUTC(buffer, '\n'));
}