4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7 * Jaeho Lee <jaeho81.lee@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
26 * Implementation of keyval object
29 #include "keyval_type.h"
31 #include "bundle_log.h"
35 static keyval_method_collection_t method = {
38 keyval_get_encoded_size,
44 keyval_new(keyval_t *kv, const char *key, const int type, const void *val,
48 must_free_obj = kv ? 0 : 1;
51 kv = calloc(1, sizeof(keyval_t));
53 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
59 keyval_free(kv, must_free_obj);
62 kv->key = strdup(key);
64 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
65 keyval_free(kv, must_free_obj);
73 kv->val = calloc(1, size);
75 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
80 memcpy(kv->val, val, size);
89 keyval_free(keyval_t *kv, int do_free_object)
99 if (NULL != kv->val) {
111 keyval_get_data(keyval_t *kv, int *type, void **val, size_t *size)
114 return BUNDLE_ERROR_INVALID_PARAMETER;
115 if (keyval_type_is_array(kv->type))
116 return BUNDLE_ERROR_INVALID_PARAMETER;
129 keyval_compare(keyval_t *kv1, keyval_t *kv2)
134 if (0 != strcmp(kv1->key, kv2->key))
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 (0 != memcmp(kv1->val, kv2->val, kv1->size))
152 keyval_get_encoded_size(keyval_t *kv)
158 = sizeof(size_t) /* total size */
159 + sizeof(int) /* type */
160 + sizeof(size_t) /* key size */
161 + strlen(kv->key) + 1 /* key (+ null byte) */
162 + sizeof(size_t) /* size */
163 + kv->size; /* val */
169 * encode a keyval to byte
171 * @pre kv must be valid.
172 * @post byte must be freed.
175 * @param[out] byte_len
179 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;
187 *byte_len = keyval_get_encoded_size(kv);
189 *byte = calloc(1, *byte_len);
193 unsigned char *p = *byte;
195 memcpy(p, byte_len, sizeof(size_t)); p += sizeof(size_t);
196 memcpy(p, &(kv->type), sz_type); p += sz_type;
197 memcpy(p, &sz_key, sz_keysize); p += sz_keysize;
198 memcpy(p, kv->key, sz_key); p += sz_key;
199 memcpy(p, &(kv->size), sz_size); p += sz_size;
200 memcpy(p, kv->val, sz_val); p += 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.
215 keyval_decode(unsigned char *byte, keyval_t **kv)
217 static const size_t sz_byte_len = sizeof(size_t);
218 static const size_t sz_type = sizeof(int);
219 static const size_t sz_keysize = sizeof(size_t);
220 static const size_t sz_size = sizeof(size_t);
222 unsigned char *p = byte;
224 size_t byte_len = *((size_t *)p); p += sz_byte_len;
225 int type = *((int *)p); p += sz_type;
226 size_t keysize = *((size_t *)p); p += sz_keysize;
227 char *key = (char *)p; p += keysize;
228 size_t size = *((size_t *)p); p += sz_size;
229 void *val = (void *)p; p += size;
232 *kv = keyval_new(*kv, key, type, val, size);
239 keyval_get_type_from_encoded_byte(unsigned char *byte)
241 static const size_t sz_byte_len = sizeof(size_t);
243 unsigned char *p = byte;
245 int type = *((int *)p);