summaryrefslogtreecommitdiffstats
path: root/src/include
diff options
context:
space:
mode:
authorMichael Brown2017-06-13 13:12:11 +0200
committerMichael Brown2017-06-20 10:49:00 +0200
commitb506528c1e4ce70dc24da87c0f7cd16ddac60701 (patch)
treefc03fd8c44ccb86c8d1775a0a23bf2864ba71711 /src/include
parent[crypto] Expose asn1_grow() (diff)
downloadipxe-b506528c1e4ce70dc24da87c0f7cd16ddac60701.tar.gz
ipxe-b506528c1e4ce70dc24da87c0f7cd16ddac60701.tar.xz
ipxe-b506528c1e4ce70dc24da87c0f7cd16ddac60701.zip
[crypto] Provide asn1_built() to construct a cursor from a builder
Our ASN.1 parsing code uses a struct asn1_cursor, while the object construction code uses a struct asn1_builder. These structures are identical apart from the const modifier applied to the data pointer in struct asn1_cursor. Provide asn1_built() to safely typecast a struct asn1_builder to a struct asn1_cursor, allowing constructed objects to be passed to functions expecting a struct asn1_cursor. Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/include')
-rw-r--r--src/include/ipxe/asn1.h24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/include/ipxe/asn1.h b/src/include/ipxe/asn1.h
index 5a6c0d9c..847d845b 100644
--- a/src/include/ipxe/asn1.h
+++ b/src/include/ipxe/asn1.h
@@ -9,7 +9,9 @@
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
+#include <stddef.h>
#include <stdint.h>
+#include <assert.h>
#include <time.h>
#include <ipxe/tables.h>
@@ -337,6 +339,28 @@ asn1_type ( const struct asn1_cursor *cursor ) {
return ( ( cursor->len >= sizeof ( *type ) ) ? *type : ASN1_END );
}
+/**
+ * Get cursor for built object
+ *
+ * @v builder ASN.1 object builder
+ * @ret cursor ASN.1 object cursor
+ */
+static inline __attribute__ (( always_inline )) struct asn1_cursor *
+asn1_built ( struct asn1_builder *builder ) {
+ union {
+ struct asn1_builder builder;
+ struct asn1_cursor cursor;
+ } *u = container_of ( builder, typeof ( *u ), builder );
+
+ /* Sanity check */
+ linker_assert ( ( ( const void * ) &u->builder.data ) ==
+ &u->cursor.data, asn1_builder_cursor_data_mismatch );
+ linker_assert ( &u->builder.len == &u->cursor.len,
+ asn1_builder_cursor_len_mismatch );
+
+ return &u->cursor;
+}
+
extern int asn1_start ( struct asn1_cursor *cursor, unsigned int type,
size_t extra );
extern int asn1_enter ( struct asn1_cursor *cursor, unsigned int type );