Skip to content

Commit

Permalink
[crypto] Provide asn1_built() to construct a cursor from a builder
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mcb30 committed Jun 20, 2017
1 parent e5bfa10 commit b506528
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/include/ipxe/asn1.h
Expand Up @@ -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>

Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit b506528

Please sign in to comment.