2009-09-14 11:34:00 +00:00
|
|
|
/*
|
|
|
|
*
|
2010-10-14 12:29:58 +00:00
|
|
|
* oFono - Open Source Telephony
|
2009-09-14 11:34:00 +00:00
|
|
|
*
|
2011-10-10 20:40:08 +00:00
|
|
|
* Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
|
2009-09-14 11:34:00 +00:00
|
|
|
*
|
2010-10-14 12:29:58 +00:00
|
|
|
* 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.
|
2009-09-14 11:34:00 +00:00
|
|
|
*
|
2010-10-14 12:29:58 +00:00
|
|
|
* 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.
|
2009-09-14 11:34:00 +00:00
|
|
|
*
|
2010-10-14 12:29:58 +00:00
|
|
|
* 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
|
2009-09-14 11:34:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include "iter.h"
|
|
|
|
|
2009-10-14 19:15:10 +00:00
|
|
|
static inline void bcd_to_mccmnc(const uint8_t *restrict bcd,
|
|
|
|
char *mcc, char *mnc)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2010-11-14 16:20:08 +00:00
|
|
|
void g_isi_sb_iter_init_full(GIsiSubBlockIter *iter, const GIsiMessage *msg,
|
|
|
|
size_t used, gboolean longhdr,
|
2009-10-14 19:15:10 +00:00
|
|
|
uint16_t sub_blocks)
|
2009-09-14 20:43:17 +00:00
|
|
|
{
|
2010-11-14 16:20:08 +00:00
|
|
|
const uint8_t *data = g_isi_msg_data(msg);
|
|
|
|
size_t len = g_isi_msg_data_len(msg);
|
|
|
|
|
2010-12-03 07:53:32 +00:00
|
|
|
if (data == NULL)
|
2009-10-08 16:47:15 +00:00
|
|
|
len = used = 0;
|
2009-10-14 19:15:10 +00:00
|
|
|
|
2011-03-03 07:22:39 +00:00
|
|
|
iter->cursor = longhdr ? 4 : 2;
|
2011-03-04 08:28:44 +00:00
|
|
|
iter->start = (uint8_t *) data + used;
|
2009-09-14 20:43:17 +00:00
|
|
|
iter->end = iter->start + len;
|
|
|
|
iter->longhdr = longhdr;
|
2009-10-08 16:47:15 +00:00
|
|
|
iter->sub_blocks = len > used ? sub_blocks : 0;
|
|
|
|
}
|
2009-09-14 20:43:17 +00:00
|
|
|
|
2010-11-14 16:20:08 +00:00
|
|
|
void g_isi_sb_iter_init(GIsiSubBlockIter *iter, const GIsiMessage *msg,
|
|
|
|
size_t used)
|
2009-10-08 16:47:15 +00:00
|
|
|
{
|
2010-11-14 16:20:08 +00:00
|
|
|
const uint8_t *data = g_isi_msg_data(msg);
|
|
|
|
size_t len = g_isi_msg_data_len(msg);
|
|
|
|
|
2010-12-03 07:53:32 +00:00
|
|
|
if (data == NULL)
|
2009-10-08 16:47:15 +00:00
|
|
|
len = used = 0;
|
2009-10-14 19:15:10 +00:00
|
|
|
|
2011-03-03 07:22:39 +00:00
|
|
|
iter->cursor = 2;
|
2011-03-04 08:28:44 +00:00
|
|
|
iter->start = (uint8_t *) data + used;
|
2009-10-08 16:47:15 +00:00
|
|
|
iter->end = iter->start + len;
|
2010-05-27 16:12:45 +00:00
|
|
|
iter->longhdr = FALSE;
|
2009-10-08 16:47:15 +00:00
|
|
|
iter->sub_blocks = len > used ? iter->start[-1] : 0;
|
2009-09-14 20:43:17 +00:00
|
|
|
}
|
|
|
|
|
2010-11-23 14:09:37 +00:00
|
|
|
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;
|
|
|
|
|
2011-03-03 07:22:39 +00:00
|
|
|
inner->cursor = 2;
|
2010-11-23 14:09:37 +00:00
|
|
|
inner->start = outer->start + used;
|
|
|
|
inner->end = inner->start + len;
|
|
|
|
inner->longhdr = FALSE;
|
|
|
|
inner->sub_blocks = len > used ? inner->start[-1] : 0;
|
|
|
|
}
|
|
|
|
|
2010-12-22 15:47:11 +00:00
|
|
|
void g_isi_sb_subiter_init_full(GIsiSubBlockIter *outer,
|
|
|
|
GIsiSubBlockIter *inner, size_t used,
|
|
|
|
gboolean longhdr, uint16_t sub_blocks)
|
2010-11-23 14:09:37 +00:00
|
|
|
{
|
|
|
|
size_t len = g_isi_sb_iter_get_len(outer);
|
|
|
|
|
|
|
|
if (outer->start + len > outer->end ||
|
|
|
|
outer->start + used > outer->end)
|
|
|
|
len = used = 0;
|
|
|
|
|
2011-03-03 07:22:39 +00:00
|
|
|
inner->cursor = longhdr ? 4 : 2;
|
2010-11-23 14:09:37 +00:00
|
|
|
inner->start = outer->start + used;
|
|
|
|
inner->end = inner->start + len;
|
|
|
|
inner->longhdr = longhdr;
|
|
|
|
inner->sub_blocks = len > used ? sub_blocks : 0;
|
|
|
|
}
|
|
|
|
|
2010-05-27 16:12:45 +00:00
|
|
|
gboolean g_isi_sb_iter_is_valid(const GIsiSubBlockIter *iter)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
2010-11-27 19:38:58 +00:00
|
|
|
if (iter == NULL)
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-10-08 16:47:15 +00:00
|
|
|
|
|
|
|
if (iter->sub_blocks == 0)
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-10-08 16:47:15 +00:00
|
|
|
|
|
|
|
if (iter->start + (iter->longhdr ? 4 : 2) > iter->end)
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-09-14 11:34:00 +00:00
|
|
|
|
2009-09-14 20:43:17 +00:00
|
|
|
if (iter->start + g_isi_sb_iter_get_len(iter) > iter->end)
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-09-14 11:34:00 +00:00
|
|
|
|
2010-05-27 16:12:45 +00:00
|
|
|
return TRUE;
|
2009-09-14 11:34:00 +00:00
|
|
|
}
|
|
|
|
|
2009-09-30 20:12:17 +00:00
|
|
|
int g_isi_sb_iter_get_id(const GIsiSubBlockIter *iter)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
2009-10-08 16:47:15 +00:00
|
|
|
if (iter->longhdr)
|
2010-12-22 15:47:11 +00:00
|
|
|
return (iter->start[0] << 8) | iter->start[1];
|
|
|
|
|
2009-09-14 11:34:00 +00:00
|
|
|
return iter->start[0];
|
|
|
|
}
|
|
|
|
|
2009-09-30 20:12:17 +00:00
|
|
|
size_t g_isi_sb_iter_get_len(const GIsiSubBlockIter *iter)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
2009-10-08 16:47:15 +00:00
|
|
|
if (iter->longhdr)
|
2010-12-22 15:47:11 +00:00
|
|
|
return (iter->start[2] << 8) | iter->start[3];
|
|
|
|
|
2009-09-14 11:34:00 +00:00
|
|
|
return iter->start[1];
|
|
|
|
}
|
|
|
|
|
2010-05-27 16:12:45 +00:00
|
|
|
gboolean g_isi_sb_iter_get_data(const GIsiSubBlockIter *restrict iter,
|
2009-10-14 19:15:10 +00:00
|
|
|
void **data, unsigned pos)
|
|
|
|
{
|
2011-03-04 08:28:44 +00:00
|
|
|
if ((size_t) pos > g_isi_sb_iter_get_len(iter)
|
2010-12-22 15:47:11 +00:00
|
|
|
|| iter->start + pos > iter->end)
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2010-11-14 16:20:08 +00:00
|
|
|
|
2011-03-04 08:28:44 +00:00
|
|
|
*data = (void *) iter->start + pos;
|
2010-05-27 16:12:45 +00:00
|
|
|
return TRUE;
|
2009-10-14 19:15:10 +00:00
|
|
|
}
|
|
|
|
|
2010-05-27 16:12:45 +00:00
|
|
|
gboolean g_isi_sb_iter_get_byte(const GIsiSubBlockIter *restrict iter,
|
2009-10-14 19:15:10 +00:00
|
|
|
uint8_t *byte, unsigned pos)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
2011-03-04 08:28:44 +00:00
|
|
|
if ((size_t) pos > g_isi_sb_iter_get_len(iter)
|
2010-12-22 15:47:11 +00:00
|
|
|
|| iter->start + pos > iter->end)
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2010-11-14 16:20:08 +00:00
|
|
|
|
2009-09-14 11:34:00 +00:00
|
|
|
*byte = iter->start[pos];
|
2010-05-27 16:12:45 +00:00
|
|
|
return TRUE;
|
2009-09-14 11:34:00 +00:00
|
|
|
}
|
|
|
|
|
2010-05-27 16:12:45 +00:00
|
|
|
gboolean g_isi_sb_iter_get_word(const GIsiSubBlockIter *restrict iter,
|
2009-10-14 19:15:10 +00:00
|
|
|
uint16_t *word, unsigned pos)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
|
|
|
uint16_t val;
|
|
|
|
|
2009-10-14 19:15:10 +00:00
|
|
|
if (pos + 1 > g_isi_sb_iter_get_len(iter))
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-09-14 11:34:00 +00:00
|
|
|
|
|
|
|
memcpy(&val, iter->start + pos, sizeof(uint16_t));
|
|
|
|
*word = ntohs(val);
|
2010-05-27 16:12:45 +00:00
|
|
|
return TRUE;
|
2009-09-14 11:34:00 +00:00
|
|
|
}
|
|
|
|
|
2010-05-27 16:12:45 +00:00
|
|
|
gboolean g_isi_sb_iter_get_dword(const GIsiSubBlockIter *restrict iter,
|
|
|
|
uint32_t *dword, unsigned pos)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
|
|
|
uint32_t val;
|
|
|
|
|
2009-10-14 19:15:10 +00:00
|
|
|
if (pos + 3 > g_isi_sb_iter_get_len(iter))
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-09-14 11:34:00 +00:00
|
|
|
|
|
|
|
memcpy(&val, iter->start + pos, sizeof(uint32_t));
|
|
|
|
*dword = ntohl(val);
|
2010-05-27 16:12:45 +00:00
|
|
|
return TRUE;
|
2009-09-14 11:34:00 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 07:22:39 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-05-27 16:12:45 +00:00
|
|
|
gboolean g_isi_sb_iter_get_oper_code(const GIsiSubBlockIter *restrict iter,
|
2009-10-14 19:15:10 +00:00
|
|
|
char *mcc, char *mnc, unsigned pos)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
2009-10-14 19:15:10 +00:00
|
|
|
if (pos + 2 > g_isi_sb_iter_get_len(iter))
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-09-14 11:34:00 +00:00
|
|
|
|
|
|
|
bcd_to_mccmnc(iter->start + pos, mcc, mnc);
|
2010-05-27 16:12:45 +00:00
|
|
|
return TRUE;
|
2009-09-14 11:34:00 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 07:22:39 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-05-27 16:12:45 +00:00
|
|
|
gboolean g_isi_sb_iter_get_alpha_tag(const GIsiSubBlockIter *restrict iter,
|
2009-10-14 19:15:10 +00:00
|
|
|
char **utf8, size_t len, unsigned pos)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
|
|
|
uint8_t *ucs2 = NULL;
|
|
|
|
|
2009-10-14 19:15:10 +00:00
|
|
|
if (pos > g_isi_sb_iter_get_len(iter))
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-09-14 11:34:00 +00:00
|
|
|
|
2010-12-03 07:53:32 +00:00
|
|
|
if (utf8 == NULL || len == 0 || pos + len > g_isi_sb_iter_get_len(iter))
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-09-14 11:34:00 +00:00
|
|
|
|
2009-09-14 13:21:06 +00:00
|
|
|
ucs2 = iter->start + pos;
|
2010-12-22 15:47:11 +00:00
|
|
|
|
2009-09-14 13:21:06 +00:00
|
|
|
if (ucs2 + len > iter->end)
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-09-14 11:34:00 +00:00
|
|
|
|
2011-03-04 08:28:44 +00:00
|
|
|
*utf8 = g_convert((const char *) ucs2, len, "UTF-8//TRANSLIT",
|
|
|
|
"UCS-2BE", NULL, NULL, NULL);
|
2010-04-20 09:59:12 +00:00
|
|
|
return *utf8 != NULL;
|
2009-09-14 11:34:00 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 07:22:39 +00:00
|
|
|
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;
|
|
|
|
}
|
2010-05-27 16:12:45 +00:00
|
|
|
gboolean g_isi_sb_iter_get_latin_tag(const GIsiSubBlockIter *restrict iter,
|
2009-10-14 19:15:10 +00:00
|
|
|
char **latin, size_t len, unsigned pos)
|
2009-09-14 13:21:06 +00:00
|
|
|
{
|
|
|
|
uint8_t *str = NULL;
|
|
|
|
|
2009-10-14 19:15:10 +00:00
|
|
|
if (pos > g_isi_sb_iter_get_len(iter))
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-09-14 13:21:06 +00:00
|
|
|
|
2010-12-22 15:47:11 +00:00
|
|
|
if (latin == NULL || len == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (pos + len > g_isi_sb_iter_get_len(iter))
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-09-14 13:21:06 +00:00
|
|
|
|
|
|
|
str = iter->start + pos;
|
2010-12-22 15:47:11 +00:00
|
|
|
|
2009-09-14 13:21:06 +00:00
|
|
|
if (str + len > iter->end)
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-09-14 13:21:06 +00:00
|
|
|
|
2011-03-04 08:28:44 +00:00
|
|
|
*latin = g_strndup((char *) str, len);
|
2009-09-14 13:21:06 +00:00
|
|
|
|
2010-04-20 09:59:12 +00:00
|
|
|
return *latin != NULL;
|
2009-09-14 13:21:06 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 07:22:39 +00:00
|
|
|
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;
|
|
|
|
}
|
2010-05-27 16:12:45 +00:00
|
|
|
gboolean g_isi_sb_iter_next(GIsiSubBlockIter *iter)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
2009-09-14 20:43:17 +00:00
|
|
|
uint8_t len = g_isi_sb_iter_get_len(iter);
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
len = iter->longhdr ? 4 : 2;
|
2009-09-14 11:34:00 +00:00
|
|
|
|
2009-10-08 16:47:15 +00:00
|
|
|
if (iter->sub_blocks == 0)
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-10-08 16:47:15 +00:00
|
|
|
|
2009-09-14 11:34:00 +00:00
|
|
|
if (iter->start + len > iter->end)
|
2010-05-27 16:12:45 +00:00
|
|
|
return FALSE;
|
2009-09-14 11:34:00 +00:00
|
|
|
|
2011-03-03 07:22:39 +00:00
|
|
|
|
|
|
|
iter->cursor = iter->longhdr ? 4 : 2;
|
2009-09-14 11:34:00 +00:00
|
|
|
iter->start += len;
|
2009-10-08 16:47:15 +00:00
|
|
|
iter->sub_blocks--;
|
|
|
|
|
2010-05-27 16:12:45 +00:00
|
|
|
return TRUE;
|
2009-09-14 11:34:00 +00:00
|
|
|
}
|
2010-11-14 16:20:38 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|