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 int keyval_compare(keyval_t *kv1, keyval_t *kv2)
125 if (strcmp(kv1->key, kv2->key) != 0)
127 if (kv1->type != kv2->type)
129 if (kv1->size != kv2->size)
132 if (kv1->val == NULL && kv2->val == NULL)
134 if (kv1->val == NULL || kv2->val == NULL)
136 if (memcmp(kv1->val, kv2->val, kv1->size) != 0)
142 size_t keyval_get_encoded_size(keyval_t *kv)
149 encoded_size = sizeof(size_t) /* total size */
150 + sizeof(int) /* type */
151 + sizeof(size_t) /* key size */
152 + strlen(kv->key) + 1 /* key (+ null byte) */
153 + sizeof(size_t) /* size */
154 + kv->size; /* val */
160 * encode a keyval to byte
162 * @pre kv must be valid.
163 * @post byte must be freed.
166 * @param[out] byte_len
169 size_t keyval_encode(keyval_t *kv, unsigned char **byte, size_t *byte_len)
171 static const size_t sz_type = sizeof(int);
172 static const size_t sz_keysize = sizeof(size_t);
173 size_t sz_key = strlen(kv->key) + 1;
174 static const size_t sz_size = sizeof(size_t);
175 size_t sz_val = kv->size;
178 *byte_len = keyval_get_encoded_size(kv);
180 *byte = calloc(1, *byte_len);
186 memcpy(p, byte_len, sizeof(size_t));
188 memcpy(p, &(kv->type), sz_type);
190 memcpy(p, &sz_key, sz_keysize);
192 memcpy(p, kv->key, sz_key);
194 memcpy(p, &(kv->size), sz_size);
196 memcpy(p, kv->val, sz_val);
203 * decode a byte stream to a keyval
205 * @param[in] byte byte stream.
206 * @param[in|out] kv keyval.
207 * If kv is NULL, new keyval_t object comes.
208 * If kv is not NULL, given kv is used. (No new kv is created.)
209 * @return Number of bytes read from byte.
211 size_t keyval_decode(unsigned char *byte, keyval_t **kv)
213 static const size_t sz_byte_len = sizeof(size_t);
214 static const size_t sz_type = sizeof(int);
215 static const size_t sz_keysize = sizeof(size_t);
216 static const size_t sz_size = sizeof(size_t);
223 unsigned char *p = byte;
225 byte_len = *((size_t *)p);
229 keysize = *((size_t *)p);
233 size = *((size_t *)p);
239 *kv = keyval_new(*kv, key, type, val, size);
244 int keyval_get_type_from_encoded_byte(unsigned char *byte)
246 static const size_t sz_byte_len = sizeof(size_t);
247 unsigned char *p = byte;