From 1a6ec4dba6f1322011c578d9ef1a3d4898ddfacd Mon Sep 17 00:00:00 2001 From: doug <> Date: Wed, 17 Jun 2015 07:06:22 +0000 Subject: 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@ --- src/lib/libssl/bs_cbs.c | 16 +++++++++++++++- src/lib/libssl/bytestring.h | 10 +++++++++- src/lib/libssl/src/ssl/bs_cbs.c | 16 +++++++++++++++- src/lib/libssl/src/ssl/bytestring.h | 10 +++++++++- 4 files changed, 48 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/lib/libssl/bs_cbs.c b/src/lib/libssl/bs_cbs.c index 1368fe0fd7..b36ba489f3 100644 --- a/src/lib/libssl/bs_cbs.c +++ b/src/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/src/lib/libssl/bytestring.h b/src/lib/libssl/bytestring.h index 80ca00d77a..2eb18e207d 100644 --- a/src/lib/libssl/bytestring.h +++ b/src/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/src/lib/libssl/src/ssl/bs_cbs.c b/src/lib/libssl/src/ssl/bs_cbs.c index 1368fe0fd7..b36ba489f3 100644 --- a/src/lib/libssl/src/ssl/bs_cbs.c +++ b/src/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/src/lib/libssl/src/ssl/bytestring.h b/src/lib/libssl/src/ssl/bytestring.h index 80ca00d77a..2eb18e207d 100644 --- a/src/lib/libssl/src/ssl/bytestring.h +++ b/src/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. -- cgit v1.2.3-55-g6feb