Neels Hofmeyr | f13a8bc | 2019-11-20 02:36:45 +0100 | [diff] [blame^] | 1 | /* Copyright 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <string.h> |
| 21 | #include <osmocom/core/utils.h> |
| 22 | #include <osmocom/gsupclient/ipa_name.h> |
| 23 | |
| 24 | int osmo_ipa_name_set(struct osmo_ipa_name *ipa_name, const uint8_t *val, size_t len) |
| 25 | { |
| 26 | if (!val || !len) { |
| 27 | *ipa_name = (struct osmo_ipa_name){}; |
| 28 | return 0; |
| 29 | } |
| 30 | if (len > sizeof(ipa_name->val)) |
| 31 | return -ENOSPC; |
| 32 | ipa_name->len = len; |
| 33 | memcpy(ipa_name->val, val, len); |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | int osmo_ipa_name_set_str(struct osmo_ipa_name *ipa_name, const char *str_fmt, ...) |
| 38 | { |
| 39 | va_list ap; |
| 40 | if (!str_fmt) |
| 41 | return osmo_ipa_name_set(ipa_name, NULL, 0); |
| 42 | |
| 43 | va_start(ap, str_fmt); |
| 44 | vsnprintf((char*)(ipa_name->val), sizeof(ipa_name->val), str_fmt, ap); |
| 45 | va_end(ap); |
| 46 | ipa_name->len = strlen((char*)(ipa_name->val))+1; |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | int osmo_ipa_name_cmp(const struct osmo_ipa_name *a, const struct osmo_ipa_name *b) |
| 51 | { |
| 52 | int cmp; |
| 53 | if (a == b) |
| 54 | return 0; |
| 55 | if (!a) |
| 56 | return -1; |
| 57 | if (!b) |
| 58 | return 1; |
| 59 | if (!a->len && !b->len) |
| 60 | return 0; |
| 61 | if (!a->len && b->len) |
| 62 | return -1; |
| 63 | if (!b->len && a->len) |
| 64 | return 1; |
| 65 | |
| 66 | if (a->len == b->len) |
| 67 | return memcmp(a->val, b->val, a->len); |
| 68 | else if (a->len < b->len) { |
| 69 | cmp = memcmp(a->val, b->val, a->len); |
| 70 | if (!cmp) |
| 71 | cmp = -1; |
| 72 | return cmp; |
| 73 | } else { |
| 74 | /* a->len > b->len */ |
| 75 | cmp = memcmp(a->val, b->val, b->len); |
| 76 | if (!cmp) |
| 77 | cmp = 1; |
| 78 | return cmp; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* Return an unquoted string, not including the terminating zero. Used for writing VTY config. */ |
| 83 | const char *osmo_ipa_name_to_str(const struct osmo_ipa_name *ipa_name) |
| 84 | { |
| 85 | size_t len = ipa_name->len; |
| 86 | if (!len) |
| 87 | return ""; |
| 88 | if (ipa_name->val[len-1] == '\0') |
| 89 | len--; |
| 90 | return osmo_escape_str_c(OTC_SELECT, (char*)ipa_name->val, len); |
| 91 | } |