This is a common operation when dealing with CBS.
ok miod@ jsing@
-/* $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.
*
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)
{
-/* $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.
*
*/
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.
-/* $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.
*
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)
{
-/* $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.
*
*/
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.