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