move BCD string encoding/decoding functions to iu_helpers.[ch]

This commit is contained in:
Harald Welte 2015-09-11 17:13:51 +02:00
parent 3af1db87ed
commit 393f2bd9fb
4 changed files with 55 additions and 47 deletions

View File

@ -38,7 +38,7 @@ rua/libosmo-asn1-rua.a:
ranap/libosmo-asn1-ranap.a:
$(MAKE) -C ranap
hnbgw: asn1helpers.o hnbgw.o hnbgw_hnbap.o hnbgw_rua.o hnbgw_ranap.o $(HNBAP_OBJS) $(RUA_OBJS) $(RANAP_OBJS) $(LIBS)
hnbgw: iu_helpers.o asn1helpers.o hnbgw.o hnbgw_hnbap.o hnbgw_rua.o hnbgw_ranap.o $(HNBAP_OBJS) $(RUA_OBJS) $(RANAP_OBJS) $(LIBS)
$(CC) $(LDFLAGS) -o $@ $^
%.o: %.c

View File

@ -24,52 +24,6 @@ static int hnbgw_hnbap_tx(struct hnb_context *ctx, struct msgb *msg)
return osmo_wqueue_enqueue(&ctx->wqueue, msg);
}
int decode_iu_bcd(char *out, size_t out_len, const uint8_t *in, size_t in_len)
{
const uint8_t *ch;
char *outch = out;
for (ch = in; ch < in + in_len; ch++) {
char c = osmo_bcd2char(*ch & 0xF);
*outch++ = c;
if (outch + 1 >= out + out_len)
break;
c = osmo_bcd2char(*ch >> 4);
/* skip padding nibble at end */
if (c == 'F')
break;
*outch++ = c;
}
*outch++ = '\0';
return outch - out;
}
int encode_iu_imsi(uint8_t *out, size_t out_len,
const char *in)
{
unsigned int len = strlen(in);
uint8_t odd = (len & 0x01) == 1;
unsigned int off = 0;
unsigned int i;
len /= 2;
if (odd)
len++;
for (i = 0; i < len; i++) {
uint8_t lower, upper;
lower = osmo_char2bcd(in[++off]) & 0x0f;
if (!odd && off + 1 == len)
upper = 0x0f;
else
upper = osmo_char2bcd(in[++off]) & 0x0f;
out[i] = (upper << 4) | lower;
}
return i;
}
static int hnbgw_tx_hnb_register_acc(struct hnb_context *ctx)
{
HNBRegisterAccept_t accept_out;

48
src/iu_helpers.c Normal file
View File

@ -0,0 +1,48 @@
#include <stdint.h>
#include <osmocom/core/utils.h>
int decode_iu_bcd(char *out, size_t out_len, const uint8_t *in, size_t in_len)
{
const uint8_t *ch;
char *outch = out;
for (ch = in; ch < in + in_len; ch++) {
char c = osmo_bcd2char(*ch & 0xF);
*outch++ = c;
if (outch + 1 >= out + out_len)
break;
c = osmo_bcd2char(*ch >> 4);
/* skip padding nibble at end */
if (c == 'F')
break;
*outch++ = c;
}
*outch++ = '\0';
return outch - out;
}
int encode_iu_imsi(uint8_t *out, size_t out_len, const char *in)
{
unsigned int len = strlen(in);
uint8_t odd = (len & 0x01) == 1;
unsigned int off = 0;
unsigned int i;
len /= 2;
if (odd)
len++;
for (i = 0; i < len; i++) {
uint8_t lower, upper;
lower = osmo_char2bcd(in[++off]) & 0x0f;
if (!odd && off + 1 == len)
upper = 0x0f;
else
upper = osmo_char2bcd(in[++off]) & 0x0f;
out[i] = (upper << 4) | lower;
}
return i;
}

6
src/iu_helpers.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <stdint.h>
int decode_iu_bcd(char *out, size_t out_len, const uint8_t *in, size_t in_len);
int encode_iu_imsi(uint8_t *out, size_t out_len, const char *in);