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;
44 kv = calloc(1, sizeof(keyval_t));
46 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
52 keyval_free(kv, must_free_obj);
56 kv->key = strdup(key);
58 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
59 keyval_free(kv, must_free_obj);
67 kv->val = calloc(1, size);
69 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
70 keyval_free(kv, must_free_obj);
74 memcpy(kv->val, val, size);
82 void keyval_free(keyval_t *kv, int do_free_object)
92 if (kv->val != NULL) {
103 int keyval_get_data(keyval_t *kv, int *type, void **val, size_t *size)
106 return BUNDLE_ERROR_INVALID_PARAMETER;
108 if (keyval_type_is_array(kv->type))
109 return BUNDLE_ERROR_INVALID_PARAMETER;
121 /* LCOV_EXCL_START */
122 int keyval_compare(keyval_t *kv1, keyval_t *kv2)
127 if (strcmp(kv1->key, kv2->key) != 0)
129 if (kv1->type != kv2->type)
131 if (kv1->size != kv2->size)
134 if (kv1->val == NULL && kv2->val == NULL)
136 if (kv1->val == NULL || kv2->val == NULL)
138 if (memcmp(kv1->val, kv2->val, kv1->size) != 0)
145 size_t keyval_get_encoded_size(keyval_t *kv)
152 encoded_size = sizeof(size_t) /* total size */
153 + sizeof(int) /* type */
154 + sizeof(size_t) /* key size */
155 + strlen(kv->key) + 1 /* key (+ null byte) */
156 + sizeof(size_t) /* size */
157 + kv->size; /* val */
163 * encode a keyval to byte
165 * @pre kv must be valid.
166 * @post byte must be freed.
169 * @param[out] byte_len
172 size_t keyval_encode(keyval_t *kv, unsigned char **byte, size_t *byte_len)
174 static const size_t sz_type = sizeof(int);
175 static const size_t sz_keysize = sizeof(size_t);
176 size_t sz_key = strlen(kv->key) + 1;
177 static const size_t sz_size = sizeof(size_t);
178 size_t sz_val = kv->size;
181 *byte_len = keyval_get_encoded_size(kv);
183 *byte = calloc(1, *byte_len);
189 memcpy(p, byte_len, sizeof(size_t));
191 memcpy(p, &(kv->type), sz_type);
193 memcpy(p, &sz_key, sz_keysize);
195 memcpy(p, kv->key, sz_key);
197 memcpy(p, &(kv->size), sz_size);
199 memcpy(p, kv->val, sz_val);
206 * decode a byte stream to a keyval
208 * @param[in] byte byte stream.
209 * @param[in|out] kv keyval.
210 * If kv is NULL, new keyval_t object comes.
211 * If kv is not NULL, given kv is used. (No new kv is created.)
212 * @return Number of bytes read from byte.
214 size_t keyval_decode(unsigned char *byte, keyval_t **kv, size_t byte_size)
216 static const size_t sz_byte_len = sizeof(size_t);
217 static const size_t sz_type = sizeof(int);
218 static const size_t sz_keysize = sizeof(size_t);
219 static const size_t sz_size = sizeof(size_t);
226 unsigned char *p = byte;
229 byte_len = *((size_t *)p);
231 if (byte_size < sz_byte_len)
234 byte_size -= sz_byte_len;
238 if (byte_size < sz_type)
241 byte_size -= sz_type;
243 keysize = *((size_t *)p);
245 if (byte_size < sz_keysize)
248 byte_size -= sz_keysize;
252 if (byte_size < keysize)
255 if (!key || (strnlen(key, keysize) + 1) != keysize)
258 byte_size -= keysize;
260 size = *((size_t *)p);
262 if (byte_size < sz_size)
265 byte_size -= sz_size;
269 encoded_size = sz_byte_len + sz_type + sz_keysize + keysize +
271 if (encoded_size != byte_len)
277 *kv = keyval_new(*kv, key, type, val, size);
282 int keyval_get_type_from_encoded_byte(unsigned char *byte)
284 static const size_t sz_byte_len = sizeof(size_t);
285 unsigned char *p = byte;