dd50893a3f23601fe2c15e481504660ba823f909
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / pkix / byte_array.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      LICENSE-2.0" target="_blank">http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19
20  ******************************************************************/
21
22 #ifndef _BYTE_ARRAY_H_
23 #define _BYTE_ARRAY_H_
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif //__cplusplus
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32
33 /**
34  * @struct ByteArray
35  *
36  * General purpose byte array structure.
37  *
38  * Contains pointer to array of bytes and it's length.
39  */
40
41 typedef struct
42 {
43     uint8_t *data;    /**< Pointer to the byte array */
44     size_t len;      /**< Data size */
45 } ByteArray;
46
47
48 /**
49  *
50  * Initializes of existing byte array pointer to \a NULL.
51  */
52 #undef BYTE_ARRAY_INITIALIZER
53 #define BYTE_ARRAY_INITIALIZER {NULL, 0}
54
55 /**
56  *
57  * Initializes of existing byte array \a array.
58  *
59  * @param array ByteArray
60  */
61 #undef INIT_BYTE_ARRAY
62 #define INIT_BYTE_ARRAY(array) do{  \
63         (array).data = NULL;        \
64         (array).len = 0;            \
65     }while(0)
66
67 /**
68  *
69  * Prints out byte array \a array in hex representation with message \a msg.
70  *
71  * @param msg string of characters
72  * @param array byte array
73  */
74 #undef PRINT_BYTE_ARRAY
75 #define PRINT_BYTE_ARRAY(msg, array) do{                \
76         size_t i;                                       \
77         printf("%10s", msg);                            \
78         for( i=0; i< (array).len; i++) {                \
79             if( (i!=0) && ((i%16)==0) ) putchar('\n');  \
80             printf("%02X ", (array).data[i]);        \
81         }                                               \
82         putchar('\n');                                  \
83     }while(0)
84
85 /**
86  *
87  * Increments byte array pointer \a array by \a size.
88  *
89  * @param array byte array pointer
90  * @param size number of positions
91  */
92 #undef INC_BYTE_ARRAY_PTR
93 #define INC_BYTE_ARRAY_PTR(array, size) do{      \
94         (array)->data += size;                   \
95         (array)->len -= size;                    \
96     }while(0)
97
98 /**
99  *
100  * Increments byte array \a array by \a size.
101  *
102  * @param array byte array
103  * @param size number of positions
104  */
105 #undef INC_BYTE_ARRAY
106 #define INC_BYTE_ARRAY(array, size) do{      \
107         (array).data += size;                \
108         (array).len -= size;                 \
109     }while(0)
110
111 /**
112  *
113  * Initializes of existing byte array.
114  *
115  * @param array of uint8_t
116  *
117  * @note Array must  be statically allocated.
118  */
119 #ifdef __cplusplus
120 #define BYTE_ARRAY_CONSTRUCTOR(array) {array, sizeof(array)}
121 #else
122 #define BYTE_ARRAY_CONSTRUCTOR(array) {.data = array, .len = sizeof(array)}
123 #endif
124
125 #ifdef __cplusplus
126 }
127 #endif //__cplusplus
128
129 #endif // _BYTE_ARRAY_H_