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.
29 #include "imc_common.h"
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 unsigned char util_hexCharToInt(char c)
57 if (c >= '0' && c <= '9')
59 else if (c >= 'A' && c <= 'F')
60 return (c - 'A' + 10);
61 else if (c >= 'a' && c <= 'f')
62 return (c - 'a' + 10);
64 dbg("invalid charater!!");
69 char *util_hex_to_string(const char *src, unsigned int src_len)
77 dest = g_malloc0(src_len * 2 + 1);
79 err("Memory allocation failed!!");
83 for (i = 0; i < src_len; i++) {
84 snprintf(dest + (i * 2), (src_len * 2 + 1) - (i * 2),
85 "%02x", (unsigned char)src[i]);
88 dest[src_len * 2] = '\0';
93 char *util_hexStringToBytes(char *s)
104 ret = g_malloc0((sz / 2) + 1);
106 err("Memory allocation failed!!");
110 dbg("Convert String to Binary!!");
112 for (i = 0; i < sz; i += 2) {
113 ret[i / 2] = (char) ((util_hexCharToInt(s[i]) << 4) | util_hexCharToInt(s[i + 1]));
114 msg(" [%02x]", ret[i / 2]);
120 char _util_unpackb(const char *src, int pos, int len)
128 rshift = MAX(8 - (pos + len), 0);
131 result = MASK_AND_SHIFT(len, pos, rshift, (unsigned char)*src);
133 result = MASK(8 - pos, pos, (unsigned char)*src);
137 if (len > 0) result = (result << len) | (*src >> (8 - len)); /* if any bits left */
143 char _util_convert_byte_hexChar(char val)
148 hex_char = (char) (val + '0');
149 else if (val >= 10 && val <= 15)
150 hex_char = (char) (val - 10 + 'A');
157 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes)
163 for (i = 0; i < num_bytes * 2; i++) {
164 nibble = _util_unpackb(byte_pdu, buf_pos, 4);
166 hex_pdu[i] = _util_convert_byte_hexChar(nibble);