1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * LICENSE-2.0" target="_blank">http://www.apache.org/licenses/LICENSE-2.0
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.
20 ******************************************************************/
22 #ifndef _BYTE_ARRAY_H_
23 #define _BYTE_ARRAY_H_
36 * General purpose byte array structure.
38 * Contains pointer to array of bytes and it's length.
43 uint8_t *data; /**< Pointer to the byte array */
44 size_t len; /**< Data size */
50 * Initializes of existing byte array pointer to \a NULL.
52 #undef BYTE_ARRAY_INITIALIZER
53 #define BYTE_ARRAY_INITIALIZER {NULL, 0}
57 * Initializes of existing byte array \a array.
59 * @param array ByteArray
61 #undef INIT_BYTE_ARRAY
62 #define INIT_BYTE_ARRAY(array) do{ \
63 (array).data = NULL; \
69 * Prints out byte array \a array in hex representation with message \a msg.
71 * @param msg string of characters
72 * @param array byte array
74 #undef PRINT_BYTE_ARRAY
75 #define PRINT_BYTE_ARRAY(msg, array) do{ \
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]); \
87 * Increments byte array pointer \a array by \a size.
89 * @param array byte array pointer
90 * @param size number of positions
92 #undef INC_BYTE_ARRAY_PTR
93 #define INC_BYTE_ARRAY_PTR(array, size) do{ \
94 (array)->data += size; \
95 (array)->len -= size; \
100 * Increments byte array \a array by \a size.
102 * @param array byte array
103 * @param size number of positions
105 #undef INC_BYTE_ARRAY
106 #define INC_BYTE_ARRAY(array, size) do{ \
107 (array).data += size; \
108 (array).len -= size; \
113 * Initializes of existing byte array.
115 * @param array of uint8_t
117 * @note Array must be statically allocated.
120 #define BYTE_ARRAY_CONSTRUCTOR(array) {array, sizeof(array)}
122 #define BYTE_ARRAY_CONSTRUCTOR(array) {.data = array, .len = sizeof(array)}
129 #endif // _BYTE_ARRAY_H_