blob: 004e20ff2d7ddf6b7dd39e0a34f9d8203fe123cb [file] [log] [blame]
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02001/*! \file gsm23003.c
2 * Utility function implementations related to 3GPP TS 23.003 */
3/*
4 * (C) 2017 sysmocom s.f.m.c. GmbH <info@sysmocom.de>
5 * All Rights Reserved
6 *
7 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <ctype.h>
26
27#include <osmocom/gsm/gsm23003.h>
28#include <osmocom/gsm/protocol/gsm_23_003.h>
29
30static bool is_n_digits(const char *str, int min_digits, int max_digits)
31{
32 int len;
33 /* Use unsigned char * to avoid a compiler warning of
34 * "error: array subscript has type 'char' [-Werror=char-subscripts]" */
35 const unsigned char *pos = (const unsigned char *)str;
36 for (len = 0; *pos && len < max_digits; len++, pos++)
37 if (!isdigit(*pos))
38 return false;
39 if (len < min_digits)
40 return false;
41 /* With not too many digits, we should have reached *str == nul */
42 if (*pos)
43 return false;
44 return true;
45}
46
47/*! Determine whether the given IMSI is valid according to 3GPP TS 23.003.
48 * \param imsi IMSI digits in ASCII string representation.
49 * \returns true when the IMSI is valid, false for invalid characters or number
50 * of digits.
51 */
52bool osmo_imsi_str_valid(const char *imsi)
53{
54 return is_n_digits(imsi, GSM23003_IMSI_MIN_DIGITS, GSM23003_IMSI_MAX_DIGITS);
55}
56
57/*! Determine whether the given MSISDN is valid according to 3GPP TS 23.003.
58 * \param msisdn MSISDN digits in ASCII string representation.
59 * \returns true when the MSISDN is valid, false for invalid characters or number
60 * of digits.
61 */
62bool osmo_msisdn_str_valid(const char *msisdn)
63{
64 return is_n_digits(msisdn, 1, 15);
65}