diff options
| author | 2023-05-17 09:42:31 +0200 | |
|---|---|---|
| committer | 2023-05-17 09:42:31 +0200 | |
| commit | 7f1b87a6b69055e5f0a3dcc890f9fecd45466a90 (patch) | |
| tree | fee2677ffc44d4d464e9499eab92385afbaeb3f5 /xs.h | |
| parent | Don't autocomplete local users if they are already added. (diff) | |
| download | penes-snac2-7f1b87a6b69055e5f0a3dcc890f9fecd45466a90.tar.gz penes-snac2-7f1b87a6b69055e5f0a3dcc890f9fecd45466a90.tar.xz penes-snac2-7f1b87a6b69055e5f0a3dcc890f9fecd45466a90.zip | |
Backport from xs.
Diffstat (limited to 'xs.h')
| -rw-r--r-- | xs.h | 72 |
1 files changed, 71 insertions, 1 deletions
| @@ -118,6 +118,10 @@ void xs_data_get(const xs_data *value, void *data); | |||
| 118 | 118 | ||
| 119 | void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size); | 119 | void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size); |
| 120 | 120 | ||
| 121 | xs_str *xs_hex_enc(const xs_val *data, int size); | ||
| 122 | xs_val *xs_hex_dec(const xs_str *hex, int *size); | ||
| 123 | int xs_is_hex(const char *str); | ||
| 124 | |||
| 121 | 125 | ||
| 122 | #ifdef XS_ASSERT | 126 | #ifdef XS_ASSERT |
| 123 | #include <assert.h> | 127 | #include <assert.h> |
| @@ -1053,7 +1057,7 @@ const char *xs_number_str(const xs_number *v) | |||
| 1053 | } | 1057 | } |
| 1054 | 1058 | ||
| 1055 | 1059 | ||
| 1056 | /* raw data blocks */ | 1060 | /** raw data blocks **/ |
| 1057 | 1061 | ||
| 1058 | xs_data *xs_data_new(const void *data, int size) | 1062 | xs_data *xs_data_new(const void *data, int size) |
| 1059 | /* returns a new raw data value */ | 1063 | /* returns a new raw data value */ |
| @@ -1107,6 +1111,72 @@ void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size | |||
| 1107 | } | 1111 | } |
| 1108 | 1112 | ||
| 1109 | 1113 | ||
| 1114 | /** hex **/ | ||
| 1115 | |||
| 1116 | xs_str *xs_hex_enc(const xs_val *data, int size) | ||
| 1117 | /* returns an hexdump of data */ | ||
| 1118 | { | ||
| 1119 | xs_str *s; | ||
| 1120 | char *p; | ||
| 1121 | int n; | ||
| 1122 | |||
| 1123 | p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1)); | ||
| 1124 | |||
| 1125 | for (n = 0; n < size; n++) { | ||
| 1126 | snprintf(p, 3, "%02x", (unsigned char)data[n]); | ||
| 1127 | p += 2; | ||
| 1128 | } | ||
| 1129 | |||
| 1130 | *p = '\0'; | ||
| 1131 | |||
| 1132 | return s; | ||
| 1133 | } | ||
| 1134 | |||
| 1135 | |||
| 1136 | xs_val *xs_hex_dec(const xs_str *hex, int *size) | ||
| 1137 | /* decodes an hexdump into data */ | ||
| 1138 | { | ||
| 1139 | int sz = strlen(hex); | ||
| 1140 | xs_val *s = NULL; | ||
| 1141 | char *p; | ||
| 1142 | int n; | ||
| 1143 | |||
| 1144 | if (sz % 2) | ||
| 1145 | return NULL; | ||
| 1146 | |||
| 1147 | p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1)); | ||
| 1148 | |||
| 1149 | for (n = 0; n < sz; n += 2) { | ||
| 1150 | int i; | ||
| 1151 | if (sscanf(&hex[n], "%02x", &i) == 0) { | ||
| 1152 | /* decoding error */ | ||
| 1153 | return xs_free(s); | ||
| 1154 | } | ||
| 1155 | else | ||
| 1156 | *p = i; | ||
| 1157 | |||
| 1158 | p++; | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | *p = '\0'; | ||
| 1162 | *size = sz / 2; | ||
| 1163 | |||
| 1164 | return s; | ||
| 1165 | } | ||
| 1166 | |||
| 1167 | |||
| 1168 | int xs_is_hex(const char *str) | ||
| 1169 | /* returns 1 if str is an hex string */ | ||
| 1170 | { | ||
| 1171 | while (*str) { | ||
| 1172 | if (strchr("0123456789abcdefABCDEF", *str++) == NULL) | ||
| 1173 | return 0; | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | return 1; | ||
| 1177 | } | ||
| 1178 | |||
| 1179 | |||
| 1110 | #endif /* XS_IMPLEMENTATION */ | 1180 | #endif /* XS_IMPLEMENTATION */ |
| 1111 | 1181 | ||
| 1112 | #endif /* _XS_H */ | 1182 | #endif /* _XS_H */ |