22892cdc7fb800dfcfcde186d16679a7368e971e
[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
23 #ifndef _BYTE_ARRAY_H_
24 #define _BYTE_ARRAY_H_
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif //__cplusplus
29
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdint.h>
33
34 /**
35  * @struct ByteArray
36  *
37  * General purpose byte array structure.
38  *
39  * Contains pointer to array of bytes and it's length.
40  */
41
42 typedef struct
43 {
44     uint8_t *data;    /**< Pointer to the byte array */
45     size_t len;      /**< Data size */
46 } ByteArray;
47
48
49 /**@def BYTE_ARRAY_INITIALIZER
50  *
51  * Initializes of existing byte array pointer to \a NULL.
52  */
53 #undef BYTE_ARRAY_INITIALIZER
54 #define BYTE_ARRAY_INITIALIZER {NULL, 0}
55
56 /**@def INIT_BYTE_ARRAY(array)
57  *
58  * Initializes of existing byte array \a array.
59  *
60  * @param array ByteArray
61  */
62 #undef INIT_BYTE_ARRAY
63 #define INIT_BYTE_ARRAY(array) do{  \
64         (array).data = NULL;        \
65         (array).len = 0;            \
66     }while(0)
67
68 /**@def PRINT_BYTE_ARRAY(msg, array)
69  *
70  * Prints out byte array \a array in hex representation with message \a msg.
71  *
72  * @param msg string of characters
73  * @param array byte array
74  */
75 #undef PRINT_BYTE_ARRAY
76 #define PRINT_BYTE_ARRAY(msg, array) do{                \
77         size_t i;                                       \
78         printf("%10s", msg);                            \
79         for( i=0; i< (array).len; i++) {                \
80             if( (i!=0) && ((i%16)==0) ) putchar('\n');  \
81             printf("%02X ", (array).data[i]);        \
82         }                                               \
83         putchar('\n');                                  \
84     }while(0)
85
86 /**@def INC_BYTE_ARRAY_PTR(array, size)
87  *
88  * Increments byte array pointer \a array by \a size.
89  *
90  * @param array byte array pointer
91  * @param size number of positions
92  */
93 #undef INC_BYTE_ARRAY_PTR
94 #define INC_BYTE_ARRAY_PTR(array, size) do{      \
95         (array)->data += size;                   \
96         (array)->len -= size;                    \
97     }while(0)
98
99 /**@def INC_BYTE_ARRAY(array, size)
100  *
101  * Increments byte array \a array by \a size.
102  *
103  * @param array byte array
104  * @param size number of positions
105  */
106 #undef INC_BYTE_ARRAY
107 #define INC_BYTE_ARRAY(array, size) do{      \
108         (array).data += size;                \
109         (array).len -= size;                 \
110     }while(0)
111
112 #ifdef __cplusplus
113 }
114 #endif //__cplusplus
115
116 #endif // _BYTE_ARRAY_H_