summaryrefslogtreecommitdiffstats
path: root/src/utils.c
Commit message (Collapse)AuthorAgeFilesLines
* get_value_string(): guard against NULLNeels Hofmeyr2019-02-151-0/+3
| | | | | | | | | | | | get_value_string() conveniently prints the value number to a static buffer if it is unknown in a value_string array. Do the same if the value_string array pointer itself is NULL. If a value string array is user supplied and might be NULL, one could add a separate NULL check around it; but by making get_value_string() itself guard against NULL, another static char buffer to print the value number is avoided. Change-Id: Ie640e9258a959da8f4f9089478de993509853997
* add osmo_hexdump_buf() and testNeels Hofmeyr2019-01-281-11/+38
| | | | | | | | | | | | | | | | | | Add osmo_hexdump_buf() as an all-purpose hexdump function, which all other osmo_hexdump_*() implementations now call. It absorbs the static _osmo_hexdump(). Add tests for osmo_hexdump_buf(). Rationale: recently during patch review, a situation came up where two hexdumps in a single printf would have been useful. Now I've faced a similar situation again, in ongoing development. So I decided it is time to provide this API. The traditional osmo_hexdump() API returns a non-const char*, which should probably have been a const instead. Particularly this new function may return a string constant "" if the buf is NULL or empty, so return const char*. That is why the older implementations calling osmo_hexdump_buf() separately return the buffer instead of the const return value directly. Change-Id: I590595567b218b24e53c9eb1fd8736c0324d371d
* gsm23003: add osmo_imei_str_valid()Oliver Smith2019-01-141-0/+31
| | | | | | | | | | | Verify 14 digit and 15 digit IMEI strings. OsmoHLR will use the 14 digit version to check IMEIs before writing them to the DB. Place the Luhn checksum code in a dedicated osmo_luhn() function, so it can be used elsewhere. Related: OS#2541 Change-Id: Id2d2a3a93b033bafc74c62e15297034bf4aafe61
* fix api doc for osmo_bcd2str()Neels Hofmeyr2018-12-111-1/+1
| | | | Change-Id: I504ea849fc9daeb34a1b3c5343371161deba743e
* add osmo_bcd2str()Neels Hofmeyr2018-12-101-0/+41
| | | | | | | | | | | | | | | | | | Add a standalone bcd-to-string conversion function with generic parameters. Add a regression test in utils_test.c. So far there is no single universal implementation that converts a BCD to a string. I could only find gsm48_mi_to_string(), which also interprets surrounding bytes, MI type and TMSI as non-BCD value. The idea is to use this function from gsm48_mi_to_string() and similar implementations in subsequent commits. Root cause: in osmo-msc, I want to have an alternative MI-to-string function for composing an FSM name, which needs the BCD part of gsm48_mi_to_string() but not the TMSI part. Change-Id: I86b09d37ceef33331c1a56046a5443127d6c6be0
* add osmo_str_tolower() and _toupper() with testNeels Hofmeyr2018-09-071-0/+86
| | | | | | | | | | | | | | | | | | | | | | | | | We already have osmo_str2lower() and osmo_str2upper(), but these lack: * proper destination buffer bounds checking, * ability to call directly as printf() argument. Deprecate osmo_str2upper() and osmo_str2lower() because of missing bounds checking. Introduce osmo_str_tolower_buf(), osmo_str_toupper_buf() to provide bounds-safe conversion, also able to safely convert a buffer in-place. Introduce osmo_str_tolower(), osmo_str_toupper() that call the above _buf() equivalents using a static buffer[128] and returning the resulting string directly, convenient for direct printing. Possibly truncated but always safe. Add unit tests to utils_test.c. Replace all libosmocore uses of now deprecated osmo_str2lower(). Naming: the ctype.h API is called tolower() and toupper(), so just prepend 'osmo_str_' and don't separate 'to_lower'. Change-Id: Ib0ee1206b9f31d7ba25c31f8008119ac55440797
* api doc: fix osmo_quote_str_*Neels Hofmeyr2018-09-071-2/+8
| | | | Change-Id: Ibfd8ff213630e34009936b0fcb3ee50dda65fb70
* Add osmo_isqrt32() to compute 32bit integer square rootHarald Welte2018-06-061-0/+40
| | | | Change-Id: I2b96db6e037e72e92317fec874877e473a1cf909
* add osmo_quote_str(),osmo_quote_str_buf() and testNeels Hofmeyr2018-04-091-0/+38
| | | | | | | | | | | | | | | | | | | | | | | | | | | Rationale: with osmo_escape_str(), you get the escaped contents of the string, but not so graceful handling of NULL strings. The caller needs to quote it, and for NULL strings not quote it. osmo_quote_str() is like osmo_escape_str() but always quotes a non-NULL string, and for a NULL string returns a literal NULL, i.e. it should (tm) give the exact C representation of a string. That's useful in testing, to show exactly what char* situation we have, without jumping through hoops like if (str) printf("\"%s\"", osmo_escape_str(str, -1)); else printf("NULL"); Copy the unit test for osmo_escape_str() and adjust. To indicate that the double quotes are returned by osmo_quote_str(), use single quotes in the test printf()s. I considered allowing to pick the quoting characters by further arguments, but that complicates things: we'd need to escape the quoting characters. Just hardcode double quotes like C. Change-Id: I6f1b3709b32c23fc52f70ad9ecc9439c62b02a12
* utils: add osmo_escape_str()Neels Hofmeyr2017-12-181-0/+87
| | | | | | | | | | | | | To report invalid characters in identifiers, it is desirable to escape any weird characters. Otherwise we might print stray newlines or control characters in the log output. ctrl_test.c already uses a print_escaped() function, which will be replaced by osmo_escape_str() in a subsequent patch. control_cmd.c will use osmo_escape_str() to log invalid identifiers. Change-Id: Ic685eb63dead3967d01aaa4f1e9899e5461ca49a
* utils: add osmo_separated_identifiers_valid()Neels Hofmeyr2017-12-171-4/+17
| | | | | | | | For validating CTRL input, we want to verify that an input variable is a series of valid osmo_identifier_valid() separated by dots. Allow validating any additional chars with identifiers, for CTRL vars will be just ".". Change-Id: I13dfd02c8c870620f937d789873ad84c6b1c45de
* fix osmo_identifier_valid(): only allow printable charactersNeels Hofmeyr2017-12-171-0/+2
| | | | | | In particular disallow ASCII 0..31 (control characters) and 127 (DEL). Change-Id: I04469988495af03c881fe81d7786ca7f71636299
* osmo_strlcpy: sanitize: don't memcpy from NULL src even if len is 0Neels Hofmeyr2017-11-211-1/+2
| | | | | | | | Some callers pass NULL and len == 0. The semantics are that we then nul-terminate an emtpy string. Avoid a sanitizer warning by not calling memcpy() for the NULL case. Change-Id: I883048cf2807e606c6481634dbd569fc12aed889
* Fix/Update copyright notices; Add SPDX annotationHarald Welte2017-11-131-0/+2
| | | | | | | | Let's fix some erroneous/accidential references to wrong license, update copyright information where applicable and introduce a SPDX-License-Identifier to all files. Change-Id: I39af26c6aaaf5c926966391f6565fc5936be21af
* osmo_char2bcd(): Implment hex digits a-f and A-FHarald Welte2017-10-271-1/+8
| | | | | | | | | | | | | | osmo_bcd2char() has always supported both decimal and hex. However, osmo_char2bcd() use to only implement decimal digits. With this patch, it also suppots conversion of hex characters from ASCII to BCD. This would be relevant in cases where somebdoy would want to use 'code 11', 'code 12' or 'ST' signals in any addresses (SCCP GT e.g.) Change-Id: I7bbcc6de08024567ab64765c12d7de71df787a7a
* utils: avoid segfault when calling osmo_strlcpy(src=NULL)Neels Hofmeyr2017-10-251-1/+1
| | | | Change-Id: Ieba7ba262ace2e370a4b9a550b3131fb13f07413
* Introduce osmo_identifier_valid() function to check validity of identifierHarald Welte2017-10-241-0/+28
| | | | | | | | | We define the notion of an 'osmocom identifier' which is basically a 7-bit US-ASCII without any special characters beyond "-_:@". We introduce a function to verify if an identifier consists only of the permitted characters. Change-Id: I96a8d345c5a69238a12d040f39b70c485a5c421c
* [doc] Doxygen documentation on osmo_hexdump print buffer sizeHarald Welte2017-10-161-0/+6
| | | | Change-Id: I83ca2a3a555e5b3c1b7f23bf9e463a5063a8006c
* utils: add osmo_is_hexstr(), add unit testNeels Hofmeyr2017-10-091-0/+33
| | | | | | Will be used by OsmoHLR to validate VTY and CTRL input. Change-Id: Idf75946eb0a84e145adad13fc7c78bb7a267aa0a
* Fix warnings: tolower() and similar require ucharPau Espin Pedrol2017-06-231-2/+2
| | | | | | | | | | | | utils.c: In function 'osmo_str2lower': utils.c:277:3: warning: array subscript has type 'char' [-Wchar-subscripts] out[i] = tolower(in[i]); And according to man: If c is neither an unsigned char value nor EOF, the behavior of these func‐ tions is undefined. Change-Id: I3fed2ab6a4efba9f8a21fcf84a5b3a91e8df084f
* Fix compilation warnings: use correct log type for uint32_tPau Espin Pedrol2017-06-231-1/+2
| | | | Change-Id: Ic1e3255800999669ca9619bfceb4124c773eff2d
* doxygen: unify use of \file across the boardNeels Hofmeyr2017-06-231-3/+2
| | | | | | | | | | | | | | | | | Considering the various styles and implications found in the sources, edit scores of files to follow the same API doc guidelines around the doxygen grouping and the \file tag. Many files now show a short description in the generated API doc that was so far only available as C comment. The guidelines and reasoning behind it is documented at https://osmocom.org/projects/cellular-infrastructure/wiki/Guidelines_for_API_documentation In some instances, remove file comments and add to the corresponding group instead, to be shared among several files (e.g. bitvec). Change-Id: Ifa70e77e90462b5eb2b0457c70fd25275910c72b
* doxygen: enable AUTOBRIEF, drop \briefNeels Hofmeyr2017-06-231-16/+16
| | | | | | | | | | Especially for short descriptions, it is annoying to have to type \brief for every single API doc. Drop all \brief and enable the AUTOBRIEF feature of doxygen, which always takes the first sentence of an API doc as the brief description. Change-Id: I11a8a821b065a128108641a2a63fb5a2b1916e87
* update/extend doxygen documentationHarald Welte2017-06-121-0/+1
| | | | | | | | | It's a pity that even with this patch we still are fare away from having the whole API documented. However, at least we have a more solid foundation. Updates not only extend the documentation, but also make sure it is rendered properly in the doxygen HTML. Change-Id: I1344bd1a6869fb00de7c1899a8db93bba9bafce3
* osmo_hexparse: allow whitespace in parsed string, add ws testNeels Hofmeyr2017-02-141-9/+22
| | | | | | | | This is particularly useful for hex dumps containing spaces found in a log (e.g. osmo-nitb authentication rand token), which can now be passed in quotes to osmo-auc-gen without having to edit the spaces away. Change-Id: Ib7af07f674a2d26c8569acdee98835fb3e626c45
* doc: fix doxygen 'utils' group closing braceNeels Hofmeyr2017-01-181-1/+2
| | | | | | | osmo_strlcpy() was excluded from the group because the closing brace was above it. Change-Id: I6701261f5854342ac4cd4f2da62e49eb40362938
* doc: clarify osmo_strlcpy() docNeels Hofmeyr2017-01-151-5/+5
| | | | | | | Stating that it 'truncates src' is misleading. Also clarify whether siz includes the space needed for the terminating NUL. Change-Id: I01c1a94408b471f7f54576178a60938bf9ee3261
* doc: fix typo for osmo_hexparseNeels Hofmeyr2016-12-211-1/+1
| | | | Change-Id: Ifc2b499792fda378c807c678b8e06630cb64d273
* Introduce osmo_strlcpy() function so we can stop using strncpy()Harald Welte2016-11-261-0/+22
| | | | | | | | | | | | | | | | I'm aware of the existing criticism on stlrcpy(), but I think it is still better than what we have now: stnrcpy(), sometimes with Coverity warnings and sometimes with a manual setting of the termination byte. The implementation follows the linux kernel strlcpy() which is claimed to be BSD compatible. We could of course link against libbsd on Linux instead, but I think it's reasonably small and simple to provide our own implementation. Future versions of libosmocore could use some autoconf magic and preprocessor macros to use the system-provided strlcpy() if it exists. Change-Id: Ifdc99b0e3b8631f1e771e58acaf9efb00a9cd493
* add get_value_string_or_null() to handle unknown itemsNeels Hofmeyr2016-06-131-3/+18
| | | | | | | | | | | | | | | | | Add get_value_string_or_null() to return NULL in case the given value is not found in the list of strings, to be able to cleanly fall back to another list of strings. Absorb the lookup loop from get_value_string(). Context: in osmo-bts, I want to look up an RSL message name in rsl_msgt_names and fall back to rsl_ipac_msgt_names if not found, because the IPAC PDCH ACT and DEACT messages are sent in a standard ABIS_RSL_MDISC_DED_CHAN. In a subsequent commit, get_value_string_or_null() will be used by new rsl_or_ipac_msg_name(). Change-Id: I1fa3907e28d528d2758bc3eae9d19e6c1168f5e5 Reviewed-on: https://gerrit.osmocom.org/230 Reviewed-by: Harald Welte <laforge@gnumonks.org> Tested-by: Jenkins Builder
* api doc: get_value_string(): mention composition in static bufferNeels Hofmeyr2016-06-131-0/+4
| | | | | | | Change-Id: I93dad98711ef69f8a1e196efa029a842a1ff5bd6 Reviewed-on: https://gerrit.osmocom.org/229 Reviewed-by: Harald Welte <laforge@gnumonks.org> Tested-by: Jenkins Builder
* Import osmo_{encode,decode}_big_endian() from openbscHarald Welte2016-05-051-0/+62
|
* utils: add 'returns' doc to osmo_hexparse().Neels Hofmeyr2015-09-231-0/+1
|
* utils: Disable the weak symbol for darwinHolger Hans Peter Freyther2015-04-111-1/+5
| | | | | | | | We should consider simply removing the alias as we have had several ABI changes since introducint the alias utils.c:223:23: error: only weak aliases are supported on darwin __attribute__((weak, alias("osmo_hexdump_nospc")));
* add missing copyright statements to source code filesHarald Welte2014-09-111-0/+23
| | | | | | Some source code files didn't have the usual copyright and licence statement at their top. I'm adding them baesed on information in the commitlog.
* utils: Greatly improve performance of osmo_hexdump routinesNils O. Selåsdal2014-01-021-5/+13
| | | | | | | | | | | | | | | | | | | | | | | | In the osmo-bts and libosmo-abis code the hexdump routine is used for every incoming/outgoing packet (including voice frames) and the usage of snprintf showed up inside profiles. There is a semantic change when more than 4096 characters are used. The code will now truncate at byte boundaries (and not nibbles). Code: static const int lengths[] = { 23, 1000, 52 }; char buf[4096]; int i; for (i = 0; i < 30000; ++i) char *res = osmo_hexdump(buf, lengths[i & 3]); Results: before: after: real 0m3.233s real 0m0.085s user 0m3.212s user 0m0.084s sys 0m0.000s sys 0m0.000s
* get_value_string: Null terminate after the call to snprintfHolger Hans Peter Freyther2013-07-041-0/+1
| | | | | | | It is impossible that the snprintf will fill the entire namebuf but just follow the idiom to make sure it is null terminated. Related: Coverity CID 1040676
* add some missing doxygen annotation for libosmocore functionsHarald Welte2013-01-111-1/+9
|
* doc: Fix the Doxygen section endingsSylvain Munaut2012-04-181-1/+1
| | | | Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
* utils: Fix alias to weak alias to allow OSX compilationSylvain Munaut2011-11-191-1/+1
| | | | | | Using a weak alias works just as good AFAICT. Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
* core/utils: Add a symbol alias for a previous typo for compatibilitySylvain Munaut2011-11-111-0/+4
| | | | Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
* utils: Fix a bad double osmo_ prefix for osmo_hexdump_nospcSylvain Munaut2011-11-101-1/+1
| | | | | | Hopefully no project where using them it seems Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
* doxygen: add doxygen module 'utils'Harald Welte2011-08-171-0/+51
|
* osmo_hexdump: Fix segfault when input is too long.Holger Hans Peter Freyther2011-07-151-0/+2
| | | | | | | | | | In snprinftf the size is a size_t (unsigned) in case we want to write more than we have available, len_remain will be < 0. This was spotted while removing hexdump from simtrace and comparing it to our implementation. int snprintf(char *str, size_t size, const char *format, ...);
* utils: use namespace prefix osmo_*Pablo Neira Ayuso2011-05-071-9/+9
| | | | | | | | | | | | Summary of changes: s/bcd2char/osmo_bcd2char/g s/char2bcd/osmo_char2bcd/g s/hexparse/osmo_hexparse/g s/hexdump/osmo_hexdump/g s/hexdump_nospc/osmo_hexdump_nospc/g s/ubit_dump/osmo_ubit_dump/g s/static_assert/osmo_static_assert/g
* include: reorganize headers file to include/osmocom/[gsm|core]Pablo Neira Ayuso2011-03-231-1/+1
| | | | | | | | | | | | This patch moves all GSM-specific definitions to include/osmocom/gsm. Moreover, the headers in include/osmocore/ have been moved to include/osmocom/core. This has been proposed by Harald Welte and Sylvain Munaunt. Tested with `make distcheck'. Signed-off-by: Pablo Neira Ayuso <pablo@gnumonks.org>
* LOGGING: configure logging from the vtyHarald Welte2011-02-181-0/+22
| | | | | We can now configure logging to (multiple) files, stderr and syslog from the vty command line in a persistent way (config file)
* [utils] introduce ubit_dump to dump buffers of unpacked bitsHarald Welte2011-02-081-1/+1
|
* [utils] add 'bitdump' function for bitfieldsHarald Welte2011-02-081-0/+30
|
* utils: Fix typo. It is ascii.Holger Hans Peter Freyther2010-11-191-1/+1
|