Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | /*
*
* oFono - Open Source Telephony
*
* Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdint.h>
#include <string.h>
#include <glib.h>
#include <arpa/inet.h>
#include "iter.h"
static inline void bcd_to_mccmnc(const uint8_t *restrict bcd,
char *mcc, char *mnc)
{
mcc[0] = '0' + (bcd[0] & 0x0F);
mcc[1] = '0' + ((bcd[0] & 0xF0) >> 4);
mcc[2] = '0' + (bcd[1] & 0x0F);
mcc[3] = '\0';
mnc[0] = '0' + (bcd[2] & 0x0F);
mnc[1] = '0' + ((bcd[2] & 0xF0) >> 4);
mnc[2] = (bcd[1] & 0xF0) == 0xF0 ? '\0' : '0' +
(bcd[1] & 0xF0);
mnc[3] = '\0';
}
void g_isi_sb_iter_init_full(GIsiSubBlockIter *iter, const GIsiMessage *msg,
size_t used, gboolean longhdr,
uint16_t sub_blocks)
{
const uint8_t *data = g_isi_msg_data(msg);
size_t len = g_isi_msg_data_len(msg);
if (data == NULL)
len = used = 0;
iter->cursor = longhdr ? 4 : 2;
iter->start = (uint8_t *) data + used;
iter->end = iter->start + len;
iter->longhdr = longhdr;
iter->sub_blocks = len > used ? sub_blocks : 0;
}
void g_isi_sb_iter_init(GIsiSubBlockIter *iter, const GIsiMessage *msg,
size_t used)
{
const uint8_t *data = g_isi_msg_data(msg);
size_t len = g_isi_msg_data_len(msg);
if (data == NULL)
len = used = 0;
iter->cursor = 2;
iter->start = (uint8_t *) data + used;
iter->end = iter->start + len;
iter->longhdr = FALSE;
iter->sub_blocks = len > used ? iter->start[-1] : 0;
}
void g_isi_sb_subiter_init(GIsiSubBlockIter *outer, GIsiSubBlockIter *inner,
size_t used)
{
size_t len = g_isi_sb_iter_get_len(outer);
if (outer->start + len > outer->end ||
outer->start + used > outer->end)
len = used = 0;
inner->cursor = 2;
inner->start = outer->start + used;
inner->end = inner->start + len;
inner->longhdr = FALSE;
inner->sub_blocks = len > used ? inner->start[-1] : 0;
}
void g_isi_sb_subiter_init_full(GIsiSubBlockIter *outer,
GIsiSubBlockIter *inner, size_t used,
gboolean longhdr, uint16_t sub_blocks)
{
size_t len = g_isi_sb_iter_get_len(outer);
if (outer->start + len > outer->end ||
outer->start + used > outer->end)
len = used = 0;
inner->cursor = longhdr ? 4 : 2;
inner->start = outer->start + used;
inner->end = inner->start + len;
inner->longhdr = longhdr;
inner->sub_blocks = len > used ? sub_blocks : 0;
}
gboolean g_isi_sb_iter_is_valid(const GIsiSubBlockIter *iter)
{
if (iter == NULL)
return FALSE;
if (iter->sub_blocks == 0)
return FALSE;
if (iter->start + (iter->longhdr ? 4 : 2) > iter->end)
return FALSE;
if (iter->start + g_isi_sb_iter_get_len(iter) > iter->end)
return FALSE;
return TRUE;
}
int g_isi_sb_iter_get_id(const GIsiSubBlockIter *iter)
{
if (iter->longhdr)
return (iter->start[0] << 8) | iter->start[1];
return iter->start[0];
}
size_t g_isi_sb_iter_get_len(const GIsiSubBlockIter *iter)
{
if (iter->longhdr)
return (iter->start[2] << 8) | iter->start[3];
return iter->start[1];
}
gboolean g_isi_sb_iter_get_data(const GIsiSubBlockIter *restrict iter,
void **data, unsigned pos)
{
if ((size_t) pos > g_isi_sb_iter_get_len(iter)
|| iter->start + pos > iter->end)
return FALSE;
*data = (void *) iter->start + pos;
return TRUE;
}
gboolean g_isi_sb_iter_get_byte(const GIsiSubBlockIter *restrict iter,
uint8_t *byte, unsigned pos)
{
if ((size_t) pos > g_isi_sb_iter_get_len(iter)
|| iter->start + pos > iter->end)
return FALSE;
*byte = iter->start[pos];
return TRUE;
}
gboolean g_isi_sb_iter_get_word(const GIsiSubBlockIter *restrict iter,
uint16_t *word, unsigned pos)
{
uint16_t val;
if (pos + 1 > g_isi_sb_iter_get_len(iter))
return FALSE;
memcpy(&val, iter->start + pos, sizeof(uint16_t));
*word = ntohs(val);
return TRUE;
}
gboolean g_isi_sb_iter_get_dword(const GIsiSubBlockIter *restrict iter,
uint32_t *dword, unsigned pos)
{
uint32_t val;
if (pos + 3 > g_isi_sb_iter_get_len(iter))
return FALSE;
memcpy(&val, iter->start + pos, sizeof(uint32_t));
*dword = ntohl(val);
return TRUE;
}
gboolean g_isi_sb_iter_eat_byte(GIsiSubBlockIter *restrict iter,
uint8_t *byte)
{
if (!g_isi_sb_iter_get_byte(iter, byte, iter->cursor))
return FALSE;
iter->cursor += 1;
return TRUE;
}
gboolean g_isi_sb_iter_eat_word(GIsiSubBlockIter *restrict iter,
uint16_t *word)
{
if (!g_isi_sb_iter_get_word(iter, word, iter->cursor))
return FALSE;
iter->cursor += 2;
return TRUE;
}
gboolean g_isi_sb_iter_eat_dword(GIsiSubBlockIter *restrict iter,
uint32_t *dword)
{
if (!g_isi_sb_iter_get_dword(iter, dword, iter->cursor))
return FALSE;
iter->cursor += 4;
return TRUE;
}
gboolean g_isi_sb_iter_get_oper_code(const GIsiSubBlockIter *restrict iter,
char *mcc, char *mnc, unsigned pos)
{
if (pos + 2 > g_isi_sb_iter_get_len(iter))
return FALSE;
bcd_to_mccmnc(iter->start + pos, mcc, mnc);
return TRUE;
}
gboolean g_isi_sb_iter_eat_oper_code(GIsiSubBlockIter *restrict iter,
char *mcc, char *mnc)
{
if (!g_isi_sb_iter_get_oper_code(iter, mcc, mnc, iter->cursor))
return FALSE;
iter->cursor += 3;
return TRUE;
}
gboolean g_isi_sb_iter_get_alpha_tag(const GIsiSubBlockIter *restrict iter,
char **utf8, size_t len, unsigned pos)
{
uint8_t *ucs2 = NULL;
if (pos > g_isi_sb_iter_get_len(iter))
return FALSE;
if (utf8 == NULL || len == 0 || pos + len > g_isi_sb_iter_get_len(iter))
return FALSE;
ucs2 = iter->start + pos;
if (ucs2 + len > iter->end)
return FALSE;
*utf8 = g_convert((const char *) ucs2, len, "UTF-8//TRANSLIT",
"UCS-2BE", NULL, NULL, NULL);
return *utf8 != NULL;
}
gboolean g_isi_sb_iter_eat_alpha_tag(GIsiSubBlockIter *restrict iter,
char **utf8, size_t len)
{
if (!g_isi_sb_iter_get_alpha_tag(iter, utf8, len, iter->cursor))
return FALSE;
iter->cursor += len;
return TRUE;
}
gboolean g_isi_sb_iter_get_latin_tag(const GIsiSubBlockIter *restrict iter,
char **latin, size_t len, unsigned pos)
{
uint8_t *str = NULL;
if (pos > g_isi_sb_iter_get_len(iter))
return FALSE;
if (latin == NULL || len == 0)
return FALSE;
if (pos + len > g_isi_sb_iter_get_len(iter))
return FALSE;
str = iter->start + pos;
if (str + len > iter->end)
return FALSE;
*latin = g_strndup((char *) str, len);
return *latin != NULL;
}
gboolean g_isi_sb_iter_eat_latin_tag(GIsiSubBlockIter *restrict iter,
char **latin, size_t len)
{
if (!g_isi_sb_iter_get_latin_tag(iter, latin, len, iter->cursor))
return FALSE;
iter->cursor += len;
return TRUE;
}
gboolean g_isi_sb_iter_next(GIsiSubBlockIter *iter)
{
uint8_t len = g_isi_sb_iter_get_len(iter);
if (len == 0)
len = iter->longhdr ? 4 : 2;
if (iter->sub_blocks == 0)
return FALSE;
if (iter->start + len > iter->end)
return FALSE;
iter->cursor = iter->longhdr ? 4 : 2;
iter->start += len;
iter->sub_blocks--;
return TRUE;
}
gboolean g_isi_sb_iter_get_struct(const GIsiSubBlockIter *restrict iter,
void **type, size_t len, unsigned pos)
{
if (iter->start + pos + len > iter->end)
return FALSE;
return g_isi_sb_iter_get_data(iter, type, pos);
}
|