static void test_append_as_base16(void) { struct sol_buffer buf; struct sol_str_slice slice; const char to_encode[] = "Test \x01\x09\x0a\x0f Hello"; int err; #define B16_ENCODED "546573742001090a0f2048656c6c6f" sol_buffer_init(&buf); slice = sol_str_slice_from_str("XYZ"); err = sol_buffer_append_slice(&buf, slice); ASSERT_INT_EQ(err, 0); ASSERT_INT_EQ(buf.used, strlen("XYZ")); ASSERT_STR_EQ(buf.data, "XYZ"); slice = sol_str_slice_from_str(to_encode); err = sol_buffer_append_as_base16(&buf, slice, false); ASSERT_INT_EQ(err, 0); ASSERT_INT_EQ(buf.used, strlen("XYZ" B16_ENCODED)); ASSERT_STR_EQ(buf.data, "XYZ" B16_ENCODED); sol_buffer_fini(&buf); #undef B16_ENCODED }
SOL_API int sol_buffer_insert_as_base16(struct sol_buffer *buf, size_t pos, const struct sol_str_slice slice, bool uppercase) { char *p; size_t new_size; ssize_t encoded_size, r; const size_t nul_size = nul_byte_size(buf); int err; SOL_NULL_CHECK(buf, -EINVAL); SOL_INT_CHECK(pos, > buf->used, -EINVAL); if (slice.len == 0) return 0; if (pos == buf->used) return sol_buffer_append_as_base16(buf, slice, uppercase); encoded_size = sol_util_base16_calculate_encoded_len(slice); if (encoded_size < 0) return encoded_size; err = sol_util_size_add(buf->used, encoded_size, &new_size); if (err < 0) return err; if (nul_size) { err = sol_util_size_add(new_size, nul_size, &new_size); if (err < 0) return err; } err = sol_buffer_ensure(buf, new_size); if (err < 0) return err; p = sol_buffer_at(buf, pos); memmove(p + encoded_size, p, buf->used - pos); r = sol_util_base16_encode(p, encoded_size, slice, uppercase); if (r != encoded_size) { memmove(p, p + encoded_size, buf->used - pos); if (nul_size) sol_buffer_ensure_nul_byte(buf); if (r < 0) return r; else return -EINVAL; } buf->used += encoded_size; if (nul_size) return sol_buffer_ensure_nul_byte(buf); return 0; }
static void on_digest_ready_cb(void *data, struct sol_message_digest *md, struct sol_blob *output) { struct update_get_hash_handle *handle = data; struct sol_buffer buffer = SOL_BUFFER_INIT_EMPTY; struct sol_str_slice slice = sol_str_slice_from_blob(output); int r = 0; r = sol_buffer_append_as_base16(&buffer, slice, false); SOL_INT_CHECK_GOTO(r, < 0, end); end: handle->cb((void *)handle->user_data, r, (char *)buffer.data); sol_message_digest_del(md); sol_buffer_fini(&buffer); delete_handle(handle); }
static void on_digest_ready(void *data, struct sol_message_digest *handle, struct sol_blob *digest) { struct feed_ctx *ctx = data; struct sol_buffer buf; int r; sol_buffer_init(&buf); r = sol_buffer_append_as_base16(&buf, sol_str_slice_from_blob(digest), false); if (r == 0) { printf("%s\t%s\n", (char *)buf.data, ctx->file); } sol_buffer_fini(&buf); print_time(ctx, ctx->done, "final"); sol_message_digest_del(handle); free(ctx); pending--; if (pending == 0) sol_quit(); }