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 | /* |
| 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 | |
| 30 | static 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; |
Neels Hofmeyr | 4b7c791 | 2017-10-07 04:45:01 +0200 | [diff] [blame] | 36 | if (!pos) |
| 37 | return min_digits < 1; |
Neels Hofmeyr | 9cd1e74 | 2017-10-04 03:15:47 +0200 | [diff] [blame] | 38 | for (len = 0; *pos && len < max_digits; len++, pos++) |
| 39 | if (!isdigit(*pos)) |
| 40 | return false; |
| 41 | if (len < min_digits) |
| 42 | return false; |
| 43 | /* With not too many digits, we should have reached *str == nul */ |
| 44 | if (*pos) |
| 45 | return false; |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | /*! Determine whether the given IMSI is valid according to 3GPP TS 23.003. |
| 50 | * \param imsi IMSI digits in ASCII string representation. |
| 51 | * \returns true when the IMSI is valid, false for invalid characters or number |
| 52 | * of digits. |
| 53 | */ |
| 54 | bool osmo_imsi_str_valid(const char *imsi) |
| 55 | { |
| 56 | return is_n_digits(imsi, GSM23003_IMSI_MIN_DIGITS, GSM23003_IMSI_MAX_DIGITS); |
| 57 | } |
| 58 | |
| 59 | /*! Determine whether the given MSISDN is valid according to 3GPP TS 23.003. |
| 60 | * \param msisdn MSISDN digits in ASCII string representation. |
| 61 | * \returns true when the MSISDN is valid, false for invalid characters or number |
| 62 | * of digits. |
| 63 | */ |
| 64 | bool osmo_msisdn_str_valid(const char *msisdn) |
| 65 | { |
| 66 | return is_n_digits(msisdn, 1, 15); |
| 67 | } |