Browse Source

[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>
tags/v1.20.1
Michael Brown 6 years ago
parent
commit
b506528c1e
1 changed files with 24 additions and 0 deletions
  1. 24
    0
      src/include/ipxe/asn1.h

+ 24
- 0
src/include/ipxe/asn1.h View File

@@ -9,7 +9,9 @@
9 9
 
10 10
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 11
 
12
+#include <stddef.h>
12 13
 #include <stdint.h>
14
+#include <assert.h>
13 15
 #include <time.h>
14 16
 #include <ipxe/tables.h>
15 17
 
@@ -337,6 +339,28 @@ asn1_type ( const struct asn1_cursor *cursor ) {
337 339
 	return ( ( cursor->len >= sizeof ( *type ) ) ? *type : ASN1_END );
338 340
 }
339 341
 
342
+/**
343
+ * Get cursor for built object
344
+ *
345
+ * @v builder		ASN.1 object builder
346
+ * @ret cursor		ASN.1 object cursor
347
+ */
348
+static inline __attribute__ (( always_inline )) struct asn1_cursor *
349
+asn1_built ( struct asn1_builder *builder ) {
350
+	union {
351
+		struct asn1_builder builder;
352
+		struct asn1_cursor cursor;
353
+	} *u = container_of ( builder, typeof ( *u ), builder );
354
+
355
+	/* Sanity check */
356
+	linker_assert ( ( ( const void * ) &u->builder.data ) ==
357
+			&u->cursor.data, asn1_builder_cursor_data_mismatch );
358
+	linker_assert ( &u->builder.len == &u->cursor.len,
359
+			asn1_builder_cursor_len_mismatch );
360
+
361
+	return &u->cursor;
362
+}
363
+
340 364
 extern int asn1_start ( struct asn1_cursor *cursor, unsigned int type,
341 365
 			size_t extra );
342 366
 extern int asn1_enter ( struct asn1_cursor *cursor, unsigned int type );

Loading…
Cancel
Save