Exemplo n.º 1
0
int test_printwchartobuf_ascii(void)
{
    Buf *buf = newbuf();

    printwchartobuf(L'a', ESCAPE_NONE, buf);
    assert(strcmp(bufstring(buf), "a") == 0);
    assert(bufpos(buf) == 1);
    assert(bufscreenpos(buf) == 1);
    return 0;
}
Exemplo n.º 2
0
int test_append_string(void)
{
    Buf *buf = newbuf();

    bufappend(buf, "abc", 3, 3);
    assert(strcmp(bufstring(buf), "abc") == 0);
    assert(bufpos(buf) == 3);
    assert(bufscreenpos(buf) == 3);
    return 0;
}
Exemplo n.º 3
0
void md5_update(struct md5_ctx *ctx, const void *data, unsigned int len)
{
	unsigned int n;
	const uint8_t *ptr = data;
	uint8_t *buf = (uint8_t *)ctx->buf;

	while (len > 0) {
		n = MD5_BLOCK_LENGTH - bufpos(ctx);
		if (n > len)
			n = len;
		memcpy(buf + bufpos(ctx), ptr, n);
		ptr += n;
		len -= n;
		ctx->nbytes += n;
		if (bufpos(ctx) == 0) {
			swap_words(ctx->buf, 16);
			md5_mix(ctx, ctx->buf);
		}
	}
}
Exemplo n.º 4
0
void md5_final(uint8_t *dst, struct md5_ctx *ctx)
{
	static const uint8_t padding[MD5_BLOCK_LENGTH] = { 0x80 };
	uint64_t final_len = ctx->nbytes * 8;
	int pad_len, pos = bufpos(ctx);

	/* add padding */
	pad_len = MD5_BLOCK_LENGTH - 8 - pos;
	if (pad_len <= 0)
		pad_len += MD5_BLOCK_LENGTH;
	md5_update(ctx, padding, pad_len);

	/* add length directly */
	swap_words(ctx->buf, 14);
	ctx->buf[14] = final_len;
	ctx->buf[15] = final_len >> 32;

	/* final result */
	md5_mix(ctx, ctx->buf);
	le32enc(dst + 0, ctx->a);
	le32enc(dst + 4, ctx->b);
	le32enc(dst + 8, ctx->c);
	le32enc(dst + 12, ctx->d);
}