4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Ja-young Gu <jygu@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
32 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
35 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
37 #define bitsize(type) (sizeof(type) * 8)
39 #define copymask(type) ((0xffffffff) >> (32 - bitsize(type)))
41 #define MASK(width, offset, data) \
42 (((width) == bitsize(data)) ? (data) : \
43 ((((copymask(data) << (bitsize(data) - ((width) % bitsize(data)))) & copymask(data)) >> (offset)) & (data))) \
46 #define MASK_AND_SHIFT(width, offset, shift, data) \
47 ((((signed) (shift)) < 0) ? \
48 MASK((width), (offset), (data)) << -(shift) : \
49 MASK((width), (offset), (data)) >> (((signed) (shift)))) \
51 char _util_unpackb(const char *src, int pos, int len);
52 char _util_convert_byte_hexChar(char val);
53 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes);
55 void util_hex_dump(char *pad, int size, const void *data)
57 char buf[255] = {0, };
63 msg("%sno data", pad);
67 p = (unsigned char *) data;
69 snprintf(buf, 255, "%s%04X: ", pad, 0);
70 for (i = 0; i < size; i++) {
71 snprintf(hex, 4, "%02X ", p[i]);
74 if ((i + 1) % 8 == 0) {
75 if ((i + 1) % 16 == 0) {
78 snprintf(buf, 255, "%s%04X: ", pad, i + 1);
88 unsigned char util_hexCharToInt(char c)
90 if (c >= '0' && c <= '9')
92 else if (c >= 'A' && c <= 'F')
93 return (c - 'A' + 10);
94 else if (c >= 'a' && c <= 'f')
95 return (c - 'a' + 10);
97 dbg("invalid charater!!");
102 char* util_hexStringToBytes(char *s)
113 ret = g_try_malloc0((sz / 2) + 1);
115 dbg("Convert String to Binary!!");
117 for (i = 0; i < sz; i += 2) {
118 ret[i / 2] = (char) ((util_hexCharToInt(s[i]) << 4) | util_hexCharToInt(s[i + 1]));
119 dbg("[%02x]", ret[i / 2]);
125 char _util_unpackb(const char *src, int pos, int len)
133 rshift = MAX(8 - (pos + len), 0);
136 result = MASK_AND_SHIFT(len, pos, rshift, (unsigned char)*src);
138 result = MASK(8 - pos, pos, (unsigned char)*src);
142 if (len > 0) result = (result << len) | (*src >> (8 - len)); // if any bits left
148 char _util_convert_byte_hexChar(char val)
153 hex_char = (char) (val + '0');
154 } else if (val >= 10 && val <= 15) {
155 hex_char = (char) (val - 10 + 'A');
163 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes)
169 for (i = 0; i < num_bytes * 2; i++) {
170 nibble = _util_unpackb(byte_pdu, buf_pos, 4);
172 hex_pdu[i] = _util_convert_byte_hexChar(nibble);
178 char* util_removeQuotes(void *data)
183 data_len = strlen((const char *) data);
184 dbg("data_len: %d----%s", data_len, data);
189 tmp = g_try_malloc(data_len - 1);
190 memcpy(tmp, data + 1, data_len - 2);
191 dbg("tmp: [%s]", tmp);