f07f003c073638bc1d87b549a1512fc827f3af32
[platform/core/telephony/tel-plugin-imc.git] / src / s_common.c
1 /*
2  * tel-plugin-imc
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include <glib.h>
26 #include <log.h>
27
28
29 #include "s_common.h"
30
31 #undef  MAX
32 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
33
34 #undef  MIN
35 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
36
37 #define bitsize(type) (sizeof(type) * 8)
38
39 #define copymask(type) ((0xffffffff) >> (32 - bitsize(type)))
40
41 #define MASK(width, offset, data) \
42         (((width) == bitsize(data)) ? (data) :   \
43          ((((copymask(data) << (bitsize(data) - ((width) % bitsize(data)))) & copymask(data)) >> (offset)) & (data))) \
44
45
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)))) \
50
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);
54
55 void util_hex_dump(char *pad, int size, const void *data)
56 {
57         char buf[255] = {0, };
58         char hex[4] = {0, };
59         int i;
60         unsigned char *p;
61
62         if (size <= 0) {
63                 msg("%sno data", pad);
64                 return;
65         }
66
67         p = (unsigned char *) data;
68
69         snprintf(buf, 255, "%s%04X: ", pad, 0);
70         for (i = 0; i < size; i++) {
71                 snprintf(hex, 4, "%02X ", p[i]);
72                 strcat(buf, hex);
73
74                 if ((i + 1) % 8 == 0) {
75                         if ((i + 1) % 16 == 0) {
76                                 msg("%s", buf);
77                                 memset(buf, 0, 255);
78                                 snprintf(buf, 255, "%s%04X: ", pad, i + 1);
79                         } else {
80                                 strcat(buf, "  ");
81                         }
82                 }
83         }
84
85         msg("%s", buf);
86 }
87
88 unsigned char util_hexCharToInt(char c)
89 {
90         if (c >= '0' && c <= '9')
91                 return (c - '0');
92         else if (c >= 'A' && c <= 'F')
93                 return (c - 'A' + 10);
94         else if (c >= 'a' && c <= 'f')
95                 return (c - 'a' + 10);
96         else {
97                 dbg("invalid charater!!");
98                 return -1;
99         }
100 }
101
102 char *util_hex_to_string(const char *src, unsigned int src_len)
103 {
104         char *dest;
105         int i;
106
107         if (src == NULL)
108                 return NULL;
109
110         dest = g_malloc0(src_len * 2 + 1);
111         for (i = 0; i < src_len; i++) {
112                 sprintf(dest + (i * 2), "%02x", (unsigned char)src[i]);
113         }
114
115         dest[src_len * 2] = '\0';
116
117         return dest;
118 }
119
120 char* util_hexStringToBytes(char *s)
121 {
122         char *ret;
123         int i;
124         int sz;
125
126         if (s == NULL)
127                 return NULL;
128
129         sz = strlen(s);
130
131         ret = g_try_malloc0((sz / 2) + 1);
132
133         dbg("Convert String to Binary!!");
134
135         for (i = 0; i < sz; i += 2) {
136                 ret[i / 2] = (char) ((util_hexCharToInt(s[i]) << 4) | util_hexCharToInt(s[i + 1]));
137                 msg("           [%02x]", ret[i / 2]);
138         }
139
140         return ret;
141 }
142
143 char _util_unpackb(const char *src, int pos, int len)
144 {
145         char result = 0;
146         int rshift = 0;
147
148         src += pos / 8;
149         pos %= 8;
150
151         rshift = MAX(8 - (pos + len), 0);
152
153         if (rshift > 0) {
154                 result = MASK_AND_SHIFT(len, pos, rshift, (unsigned char)*src);
155         } else {
156                 result = MASK(8 - pos, pos, (unsigned char)*src);
157                 src++;
158                 len -= 8 - pos;
159
160                 if (len > 0) result = (result << len) | (*src >> (8 - len));   // if any bits left
161         }
162
163         return result;
164 }
165
166 char _util_convert_byte_hexChar(char val)
167 {
168         char hex_char;
169
170         if (val <= 9) {
171                 hex_char = (char) (val + '0');
172         } else if (val >= 10 && val <= 15) {
173                 hex_char = (char) (val - 10 + 'A');
174         } else {
175                 hex_char = '0';
176         }
177
178         return (hex_char);
179 }
180
181 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes)
182 {
183         int i;
184         char nibble;
185         int buf_pos = 0;
186
187         for (i = 0; i < num_bytes * 2; i++) {
188                 nibble = _util_unpackb(byte_pdu, buf_pos, 4);
189                 buf_pos += 4;
190                 hex_pdu[i] = _util_convert_byte_hexChar(nibble);
191         }
192
193         return TRUE;
194 }
195
196 char* util_removeQuotes(void *data)
197 {
198         char *tmp = NULL;
199         int data_len = 0;
200
201         data_len = strlen((const char *) data);
202         dbg("data_len: %d----%s", data_len, data);
203         if (data_len <= 0) {
204                 return NULL;
205         }
206
207         tmp = g_try_malloc0(data_len - 1);
208         memcpy(tmp, data + 1, data_len - 2);
209         dbg("tmp: [%s]", tmp);
210
211         return tmp;
212 }