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
24 #include "keyval_type.h"
26 #include "bundle_log.h"
29 static keyval_method_collection_t method = {
32 keyval_get_encoded_size,
37 keyval_t *keyval_new(keyval_t *kv, const char *key,
38 const int type, const void *val, const size_t size)
40 int must_free_obj = kv ? 0 : 1;
43 kv = calloc(1, sizeof(keyval_t));
45 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
51 keyval_free(kv, must_free_obj);
55 kv->key = strdup(key);
57 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
58 keyval_free(kv, must_free_obj);
66 kv->val = calloc(1, size);
68 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
73 memcpy(kv->val, val, size);
81 void keyval_free(keyval_t *kv, int do_free_object)
91 if (kv->val != NULL) {
102 int keyval_get_data(keyval_t *kv, int *type, void **val, size_t *size)
105 return BUNDLE_ERROR_INVALID_PARAMETER;
107 if (keyval_type_is_array(kv->type))
108 return BUNDLE_ERROR_INVALID_PARAMETER;
120 /* LCOV_EXCL_START */
121 int keyval_compare(keyval_t *kv1, keyval_t *kv2)
126 if (strcmp(kv1->key, kv2->key) != 0)
128 if (kv1->type != kv2->type)
130 if (kv1->size != kv2->size)
133 if (kv1->val == NULL && kv2->val == NULL)
135 if (kv1->val == NULL || kv2->val == NULL)
137 if (memcmp(kv1->val, kv2->val, kv1->size) != 0)
144 size_t keyval_get_encoded_size(keyval_t *kv)
151 encoded_size = sizeof(size_t) /* total size */
152 + sizeof(int) /* type */
153 + sizeof(size_t) /* key size */
154 + strlen(kv->key) + 1 /* key (+ null byte) */
155 + sizeof(size_t) /* size */
156 + kv->size; /* val */
162 * encode a keyval to byte
164 * @pre kv must be valid.
165 * @post byte must be freed.
168 * @param[out] byte_len
171 size_t keyval_encode(keyval_t *kv, unsigned char **byte, size_t *byte_len)
173 static const size_t sz_type = sizeof(int);
174 static const size_t sz_keysize = sizeof(size_t);
175 size_t sz_key = strlen(kv->key) + 1;
176 static const size_t sz_size = sizeof(size_t);
177 size_t sz_val = kv->size;
180 *byte_len = keyval_get_encoded_size(kv);
182 *byte = calloc(1, *byte_len);
188 memcpy(p, byte_len, sizeof(size_t));
190 memcpy(p, &(kv->type), sz_type);
192 memcpy(p, &sz_key, sz_keysize);
194 memcpy(p, kv->key, sz_key);
196 memcpy(p, &(kv->size), sz_size);
198 memcpy(p, kv->val, sz_val);
205 * decode a byte stream to a keyval
207 * @param[in] byte byte stream.
208 * @param[in|out] kv keyval.
209 * If kv is NULL, new keyval_t object comes.
210 * If kv is not NULL, given kv is used. (No new kv is created.)
211 * @return Number of bytes read from byte.
213 size_t keyval_decode(unsigned char *byte, keyval_t **kv)
215 static const size_t sz_byte_len = sizeof(size_t);
216 static const size_t sz_type = sizeof(int);
217 static const size_t sz_keysize = sizeof(size_t);
218 static const size_t sz_size = sizeof(size_t);
225 unsigned char *p = byte;
227 byte_len = *((size_t *)p);
231 keysize = *((size_t *)p);
235 size = *((size_t *)p);
241 *kv = keyval_new(*kv, key, type, val, size);
246 int keyval_get_type_from_encoded_byte(unsigned char *byte)
248 static const size_t sz_byte_len = sizeof(size_t);
249 unsigned char *p = byte;