2009-09-14 11:34:00 +00:00
|
|
|
/*
|
|
|
|
* This file is part of oFono - Open Source Telephony
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
*
|
|
|
|
* Contact: Rémi Denis-Courmont <remi.denis-courmont@nokia.com>
|
|
|
|
*
|
|
|
|
* 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 <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include "iter.h"
|
|
|
|
|
2009-09-30 20:12:17 +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';
|
|
|
|
}
|
|
|
|
|
2009-09-16 06:05:53 +00:00
|
|
|
bool g_isi_sb_iter_init(const void *restrict data, size_t len,
|
2009-09-14 20:43:17 +00:00
|
|
|
GIsiSubBlockIter *iter, bool longhdr)
|
|
|
|
{
|
|
|
|
if (!iter || !data || len == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
iter->start = (uint8_t *)data;
|
|
|
|
iter->end = iter->start + len;
|
|
|
|
iter->longhdr = longhdr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-30 20:12:17 +00:00
|
|
|
bool g_isi_sb_iter_is_valid(const GIsiSubBlockIter *iter)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
2009-09-14 20:43:17 +00:00
|
|
|
if (!iter || iter->end - iter->start < (iter->longhdr ? 4 : 2))
|
2009-09-14 11:34:00 +00:00
|
|
|
return false;
|
|
|
|
|
2009-09-14 20:43:17 +00:00
|
|
|
if (iter->start + g_isi_sb_iter_get_len(iter) > iter->end)
|
2009-09-14 11:34:00 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-09-14 20:43:17 +00:00
|
|
|
if (iter->longhdr) {
|
|
|
|
uint16_t *hdr = (uint16_t *)iter->start;
|
|
|
|
return (int)ntohs(hdr[0]);
|
|
|
|
}
|
|
|
|
|
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-09-14 20:43:17 +00:00
|
|
|
if (iter->longhdr) {
|
|
|
|
uint16_t *hdr = (uint16_t *)iter->start;
|
|
|
|
return (size_t)ntohs(hdr[1]);
|
|
|
|
}
|
|
|
|
|
2009-09-14 11:34:00 +00:00
|
|
|
return iter->start[1];
|
|
|
|
}
|
|
|
|
|
2009-09-30 20:12:17 +00:00
|
|
|
bool g_isi_sb_iter_get_byte(const GIsiSubBlockIter *restrict iter, uint8_t *byte, int pos)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
2009-09-14 20:43:17 +00:00
|
|
|
if (pos > (int)g_isi_sb_iter_get_len(iter) || iter->start + pos > iter->end)
|
2009-09-14 11:34:00 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*byte = iter->start[pos];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-30 20:12:17 +00:00
|
|
|
bool g_isi_sb_iter_get_word(const GIsiSubBlockIter *restrict iter,
|
|
|
|
uint16_t *word, int pos)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
|
|
|
uint16_t val;
|
|
|
|
|
2009-09-14 20:43:17 +00:00
|
|
|
if (pos + 1 > (int)g_isi_sb_iter_get_len(iter))
|
2009-09-14 11:34:00 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
memcpy(&val, iter->start + pos, sizeof(uint16_t));
|
|
|
|
*word = ntohs(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-30 20:12:17 +00:00
|
|
|
bool g_isi_sb_iter_get_dword(const GIsiSubBlockIter *restrict iter,
|
|
|
|
uint32_t *dword, int pos)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
|
|
|
uint32_t val;
|
|
|
|
|
2009-09-14 20:43:17 +00:00
|
|
|
if (pos + 3 > (int)g_isi_sb_iter_get_len(iter))
|
2009-09-14 11:34:00 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
memcpy(&val, iter->start + pos, sizeof(uint32_t));
|
|
|
|
*dword = ntohl(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-30 20:12:17 +00:00
|
|
|
bool g_isi_sb_iter_get_oper_code(const GIsiSubBlockIter *restrict iter,
|
|
|
|
char *mcc, char *mnc, int pos)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
2009-09-14 20:43:17 +00:00
|
|
|
if (pos + 2 > (int)g_isi_sb_iter_get_len(iter))
|
2009-09-14 11:34:00 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bcd_to_mccmnc(iter->start + pos, mcc, mnc);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-30 20:12:17 +00:00
|
|
|
bool g_isi_sb_iter_get_alpha_tag(const GIsiSubBlockIter *restrict iter,
|
|
|
|
char **utf8, size_t len, int pos)
|
2009-09-14 11:34:00 +00:00
|
|
|
{
|
|
|
|
uint8_t *ucs2 = NULL;
|
|
|
|
|
2009-09-14 20:43:17 +00:00
|
|
|
if (pos > (int)g_isi_sb_iter_get_len(iter))
|
2009-09-14 11:34:00 +00:00
|
|
|
return false;
|
|
|
|
|
2009-09-14 20:43:17 +00:00
|
|
|
if (!utf8 || len == 0 || pos + len > g_isi_sb_iter_get_len(iter))
|
2009-09-14 13:21:06 +00:00
|
|
|
return false;
|
2009-09-14 11:34:00 +00:00
|
|
|
|
2009-09-14 13:21:06 +00:00
|
|
|
ucs2 = iter->start + pos;
|
|
|
|
if (ucs2 + len > iter->end)
|
2009-09-14 11:34:00 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*utf8 = g_convert((const char *)ucs2, len, "UTF-8//TRANSLIT", "UCS-2BE",
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
return utf8 != NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-30 20:12:17 +00:00
|
|
|
bool g_isi_sb_iter_get_latin_tag(const GIsiSubBlockIter *restrict iter,
|
|
|
|
char **latin, size_t len, int pos)
|
2009-09-14 13:21:06 +00:00
|
|
|
{
|
|
|
|
uint8_t *str = NULL;
|
|
|
|
|
2009-09-14 20:43:17 +00:00
|
|
|
if (pos > (int)g_isi_sb_iter_get_len(iter))
|
2009-09-14 13:21:06 +00:00
|
|
|
return false;
|
|
|
|
|
2009-09-14 20:43:17 +00:00
|
|
|
if (!latin || len == 0 || pos + len > g_isi_sb_iter_get_len(iter))
|
2009-09-14 13:21:06 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
str = iter->start + pos;
|
|
|
|
if (str + len > iter->end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*latin = g_strndup((char *)str, len);
|
|
|
|
|
|
|
|
return latin != NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-14 11:34:00 +00:00
|
|
|
bool g_isi_sb_iter_next(GIsiSubBlockIter *iter)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
if (iter->start + len > iter->end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
iter->start += len;
|
|
|
|
return true;
|
|
|
|
}
|