2 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * Implementation of keyval object
25 #include "keyval_type.h"
27 #include "bundle_log.h"
30 static keyval_method_collection_t method = {
33 keyval_get_encoded_size,
38 keyval_t *keyval_new(keyval_t *kv, const char *key,
39 const int type, const void *val, const size_t size)
41 int must_free_obj = kv ? 0 : 1;
45 kv = calloc(1, sizeof(keyval_t));
47 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
53 keyval_free(kv, must_free_obj);
57 kv->key = strdup(key);
59 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
60 keyval_free(kv, must_free_obj);
68 kv->val = calloc(1, size);
70 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
71 keyval_free(kv, must_free_obj);
75 memcpy(kv->val, val, size);
76 if (type == BUNDLE_TYPE_STR) {
77 str = (char *)kv->val;
78 if (str[size - 1] != '\0')
89 void keyval_free(keyval_t *kv, int do_free_object)
99 if (kv->val != NULL) {
110 int keyval_get_data(keyval_t *kv, int *type, void **val, size_t *size)
113 return BUNDLE_ERROR_INVALID_PARAMETER;
115 if (keyval_type_is_array(kv->type))
116 return BUNDLE_ERROR_INVALID_PARAMETER;
128 /* LCOV_EXCL_START */
129 int keyval_compare(keyval_t *kv1, keyval_t *kv2)
134 if (strcmp(kv1->key, kv2->key) != 0)
136 if (kv1->type != kv2->type)
138 if (kv1->size != kv2->size)
141 if (kv1->val == NULL && kv2->val == NULL)
143 if (kv1->val == NULL || kv2->val == NULL)
145 if (memcmp(kv1->val, kv2->val, kv1->size) != 0)
152 size_t keyval_get_encoded_size(keyval_t *kv)
159 encoded_size = sizeof(size_t) /* total size */
160 + sizeof(int) /* type */
161 + sizeof(size_t) /* key size */
162 + strlen(kv->key) + 1 /* key (+ null byte) */
163 + sizeof(size_t) /* size */
164 + kv->size; /* val */
170 * encode a keyval to byte
172 * @pre kv must be valid.
173 * @post byte must be freed.
176 * @param[out] byte_len
179 size_t keyval_encode(keyval_t *kv, unsigned char **byte, size_t *byte_len)
181 static const size_t sz_type = sizeof(int);
182 static const size_t sz_keysize = sizeof(size_t);
183 size_t sz_key = strlen(kv->key) + 1;
184 static const size_t sz_size = sizeof(size_t);
185 size_t sz_val = kv->size;
188 *byte_len = keyval_get_encoded_size(kv);
190 *byte = calloc(1, *byte_len);
196 memcpy(p, byte_len, sizeof(size_t));
198 memcpy(p, &(kv->type), sz_type);
200 memcpy(p, &sz_key, sz_keysize);
202 memcpy(p, kv->key, sz_key);
204 memcpy(p, &(kv->size), sz_size);
206 memcpy(p, kv->val, sz_val);
213 * decode a byte stream to a keyval
215 * @param[in] byte byte stream.
216 * @param[in|out] kv keyval.
217 * If kv is NULL, new keyval_t object comes.
218 * If kv is not NULL, given kv is used. (No new kv is created.)
219 * @return Number of bytes read from byte.
221 size_t keyval_decode(unsigned char *byte, keyval_t **kv, size_t byte_size)
223 static const size_t sz_byte_len = sizeof(size_t);
224 static const size_t sz_type = sizeof(int);
225 static const size_t sz_keysize = sizeof(size_t);
226 static const size_t sz_size = sizeof(size_t);
233 unsigned char *p = byte;
236 byte_len = *((size_t *)p);
238 if (byte_size < sz_byte_len)
241 byte_size -= sz_byte_len;
245 if (byte_size < sz_type)
248 byte_size -= sz_type;
250 keysize = *((size_t *)p);
252 if (byte_size < sz_keysize)
255 byte_size -= sz_keysize;
259 if (byte_size < keysize)
262 if (!key || (strnlen(key, keysize) + 1) != keysize)
265 byte_size -= keysize;
267 size = *((size_t *)p);
269 if (byte_size < sz_size)
272 byte_size -= sz_size;
276 encoded_size = sz_byte_len + sz_type + sz_keysize + keysize +
278 if (encoded_size != byte_len)
284 *kv = keyval_new(*kv, key, type, val, size);
289 int keyval_get_type_from_encoded_byte(unsigned char *byte)
291 static const size_t sz_byte_len = sizeof(size_t);
292 unsigned char *p = byte;