From df129122ce6763dc6b6d92685d7e6b0ba8918ea1 Mon Sep 17 00:00:00 2001 From: doug Date: Wed, 17 Jun 2015 07:06:22 +0000 Subject: [PATCH] Add CBS_write_bytes() to copy the remaining CBS bytes to the caller. This is a common operation when dealing with CBS. ok miod@ jsing@ --- lib/libssl/bs_cbs.c | 16 +++++++++++++++- lib/libssl/bytestring.h | 10 +++++++++- lib/libssl/src/ssl/bs_cbs.c | 16 +++++++++++++++- lib/libssl/src/ssl/bytestring.h | 10 +++++++++- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/libssl/bs_cbs.c b/lib/libssl/bs_cbs.c index 1368fe0fd7f..b36ba489f3a 100644 --- a/lib/libssl/bs_cbs.c +++ b/lib/libssl/bs_cbs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bs_cbs.c,v 1.11 2015/06/17 07:00:22 doug Exp $ */ +/* $OpenBSD: bs_cbs.c,v 1.12 2015/06/17 07:06:22 doug Exp $ */ /* * Copyright (c) 2014, Google Inc. * @@ -95,6 +95,20 @@ CBS_strdup(const CBS *cbs, char **out_ptr) return (*out_ptr != NULL); } +int +CBS_write_bytes(const CBS *cbs, uint8_t *dst, size_t dst_len, size_t *copied) +{ + if (dst_len < cbs->len) + return 0; + + memmove(dst, cbs->data, cbs->len); + + if (copied != NULL) + *copied = cbs->len; + + return 1; +} + int CBS_contains_zero_byte(const CBS *cbs) { diff --git a/lib/libssl/bytestring.h b/lib/libssl/bytestring.h index 80ca00d77a4..2eb18e207dc 100644 --- a/lib/libssl/bytestring.h +++ b/lib/libssl/bytestring.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bytestring.h,v 1.10 2015/06/17 07:00:22 doug Exp $ */ +/* $OpenBSD: bytestring.h,v 1.11 2015/06/17 07:06:22 doug Exp $ */ /* * Copyright (c) 2014, Google Inc. * @@ -91,6 +91,14 @@ int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len); */ int CBS_strdup(const CBS *cbs, char **out_ptr); +/* + * CBS_write_bytes writes all of the remaining data from |cbs| into |dst| + * if it is at most |dst_len| bytes. If |copied| is not NULL, it will be set + * to the amount copied. It returns one on success and zero otherwise. + */ +int CBS_write_bytes(const CBS *cbs, uint8_t *dst, size_t dst_len, + size_t *copied); + /* * CBS_contains_zero_byte returns one if the current contents of |cbs| contains * a NUL byte and zero otherwise. diff --git a/lib/libssl/src/ssl/bs_cbs.c b/lib/libssl/src/ssl/bs_cbs.c index 1368fe0fd7f..b36ba489f3a 100644 --- a/lib/libssl/src/ssl/bs_cbs.c +++ b/lib/libssl/src/ssl/bs_cbs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bs_cbs.c,v 1.11 2015/06/17 07:00:22 doug Exp $ */ +/* $OpenBSD: bs_cbs.c,v 1.12 2015/06/17 07:06:22 doug Exp $ */ /* * Copyright (c) 2014, Google Inc. * @@ -95,6 +95,20 @@ CBS_strdup(const CBS *cbs, char **out_ptr) return (*out_ptr != NULL); } +int +CBS_write_bytes(const CBS *cbs, uint8_t *dst, size_t dst_len, size_t *copied) +{ + if (dst_len < cbs->len) + return 0; + + memmove(dst, cbs->data, cbs->len); + + if (copied != NULL) + *copied = cbs->len; + + return 1; +} + int CBS_contains_zero_byte(const CBS *cbs) { diff --git a/lib/libssl/src/ssl/bytestring.h b/lib/libssl/src/ssl/bytestring.h index 80ca00d77a4..2eb18e207dc 100644 --- a/lib/libssl/src/ssl/bytestring.h +++ b/lib/libssl/src/ssl/bytestring.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bytestring.h,v 1.10 2015/06/17 07:00:22 doug Exp $ */ +/* $OpenBSD: bytestring.h,v 1.11 2015/06/17 07:06:22 doug Exp $ */ /* * Copyright (c) 2014, Google Inc. * @@ -91,6 +91,14 @@ int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len); */ int CBS_strdup(const CBS *cbs, char **out_ptr); +/* + * CBS_write_bytes writes all of the remaining data from |cbs| into |dst| + * if it is at most |dst_len| bytes. If |copied| is not NULL, it will be set + * to the amount copied. It returns one on success and zero otherwise. + */ +int CBS_write_bytes(const CBS *cbs, uint8_t *dst, size_t dst_len, + size_t *copied); + /* * CBS_contains_zero_byte returns one if the current contents of |cbs| contains * a NUL byte and zero otherwise. -- 2.20.1