1 /*-
2  * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3  * Redistribution and modifications are permitted subject to BSD license.
4  */
5 #ifndef	_OCTET_STRING_H_
6 #define	_OCTET_STRING_H_
7 
8 #include "asn1/asn1c/asn_application.h"
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 /*
15  * Note: Though this sometimes represents an actual string, I don't see any
16  * guarantees of a NULL character.
17  *
18  * To be safe, if you want to print it, do something like
19  *
20  * 	printf("%.*s", (int)ia5->size, (char *)ia5->buf);
21  */
22 typedef struct OCTET_STRING {
23 	uint8_t *buf;	/* Buffer with consecutive OCTET_STRING bits */
24 	size_t size;	/* Size of the buffer */
25 
26 	asn_struct_ctx_t _asn_ctx;	/* Parsing across buffer boundaries */
27 } OCTET_STRING_t;
28 
29 extern asn_TYPE_descriptor_t asn_DEF_OCTET_STRING;
30 extern asn_TYPE_operation_t asn_OP_OCTET_STRING;
31 
32 asn_struct_free_f OCTET_STRING_free;
33 asn_struct_print_f OCTET_STRING_print;
34 asn_struct_print_f OCTET_STRING_print_utf8;
35 asn_struct_compare_f OCTET_STRING_compare;
36 ber_type_decoder_f OCTET_STRING_decode_ber;
37 der_type_encoder_f OCTET_STRING_encode_der;
38 xer_type_decoder_f OCTET_STRING_decode_xer_hex;		/* Hexadecimal */
39 xer_type_decoder_f OCTET_STRING_decode_xer_binary;	/* 01010111010 */
40 xer_type_decoder_f OCTET_STRING_decode_xer_utf8;	/* ASCII/UTF-8 */
41 xer_type_encoder_f OCTET_STRING_encode_xer;
42 xer_type_encoder_f OCTET_STRING_encode_xer_utf8;
43 oer_type_decoder_f OCTET_STRING_decode_oer;
44 oer_type_encoder_f OCTET_STRING_encode_oer;
45 per_type_decoder_f OCTET_STRING_decode_uper;
46 per_type_encoder_f OCTET_STRING_encode_uper;
47 asn_random_fill_f  OCTET_STRING_random_fill;
48 
49 #define OCTET_STRING_constraint  asn_generic_no_constraint
50 #define OCTET_STRING_decode_xer  OCTET_STRING_decode_xer_hex
51 
52 /******************************
53  * Handy conversion routines. *
54  ******************************/
55 
56 /*
57  * This function clears the previous value of the OCTET STRING (if any)
58  * and then allocates a new memory with the specified content (str/size).
59  * If size = -1, the size of the original string will be determined
60  * using strlen(str).
61  * If str equals to NULL, the function will silently clear the
62  * current contents of the OCTET STRING.
63  * Returns 0 if it was possible to perform operation, -1 otherwise.
64  */
65 int OCTET_STRING_fromBuf(OCTET_STRING_t *s, const char *str, int size);
66 
67 /* Handy conversion from the C string into the OCTET STRING. */
68 #define	OCTET_STRING_fromString(s, str)	OCTET_STRING_fromBuf(s, str, -1)
69 
70 /*
71  * Allocate and fill the new OCTET STRING and return a pointer to the newly
72  * allocated object. NULL is permitted in str: the function will just allocate
73  * empty OCTET STRING.
74  */
75 OCTET_STRING_t *OCTET_STRING_new_fromBuf(const asn_TYPE_descriptor_t *td,
76                                          const char *str, int size);
77 
78 /****************************
79  * Internally useful stuff. *
80  ****************************/
81 
82 typedef struct asn_OCTET_STRING_specifics_s {
83     /*
84      * Target structure description.
85      */
86     unsigned struct_size;   /* Size of the structure */
87     unsigned ctx_offset;    /* Offset of the asn_struct_ctx_t member */
88 
89     enum asn_OS_Subvariant {
90         ASN_OSUBV_ANY, /* The open type (ANY) */
91         ASN_OSUBV_BIT, /* BIT STRING */
92         ASN_OSUBV_STR, /* String types, not {BMP,Universal}String  */
93         ASN_OSUBV_U16, /* 16-bit character (BMPString) */
94         ASN_OSUBV_U32  /* 32-bit character (UniversalString) */
95     } subvariant;
96 } asn_OCTET_STRING_specifics_t;
97 
98 extern asn_OCTET_STRING_specifics_t asn_SPC_OCTET_STRING_specs;
99 
100 size_t OCTET_STRING_random_length_constrained(
101     const asn_TYPE_descriptor_t *, const asn_encoding_constraints_t *,
102     size_t max_length);
103 
104 #ifdef __cplusplus
105 }
106 #endif
107 
108 #endif	/* _OCTET_STRING_H_ */
109