5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2008 Pekka Riikonen
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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 * These are general utility functions that doesn't belong to any specific
24 #include "silcruntime.h"
26 /* Gets line from a buffer. Stops reading when a newline or EOF occurs.
27 This doesn't remove the newline sign from the destination buffer. The
28 argument begin is returned and should be passed again for the function. */
30 int silc_gets(char *dest, int destlen, const char *src, int srclen, int begin)
35 memset(dest, 0, destlen);
41 for ( ; start <= srclen; i++, start++) {
43 silc_set_errno(SILC_ERR_OVERFLOW);
50 silc_set_errno(SILC_ERR_EOF);
62 /* Converts string to capital characters. */
64 SilcBool silc_to_upper(const char *string, char *dest, SilcUInt32 dest_size)
68 if (strlen(string) > dest_size) {
69 silc_set_errno(SILC_ERR_OVERFLOW);
73 for (i = 0; i < strlen(string); i++)
74 dest[i] = (char)toupper((int)string[i]);
79 /* Converts string to lower letter characters. */
81 SilcBool silc_to_lower(const char *string, char *dest, SilcUInt32 dest_size)
85 if (strlen(string) > dest_size) {
86 silc_set_errno(SILC_ERR_OVERFLOW);
90 for (i = 0; i < strlen(string); i++)
91 dest[i] = (char)tolower((int)string[i]);
96 /* Parse userfqdn string which is in user@fqdn format. */
98 int silc_parse_userfqdn(const char *string,
99 char *user, SilcUInt32 user_size,
100 char *fqdn, SilcUInt32 fqdn_size)
104 if (!user && !fqdn) {
105 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
109 memset(user, 0, user_size);
110 memset(fqdn, 0, fqdn_size);
113 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
117 if (string[0] == '@') {
119 silc_strncat(user, user_size, string, strlen(string));
124 if (strchr(string, '@')) {
125 tlen = strcspn(string, "@");
128 silc_strncat(user, user_size, string, tlen);
131 silc_strncat(fqdn, fqdn_size, string + tlen + 1,
132 strlen(string) - tlen - 1);
138 silc_strncat(user, user_size, string, strlen(string));
143 /* Parses command line. At most `max_args' is taken. Rest of the line
144 will be allocated as the last argument if there are more than `max_args'
145 arguments in the line. Note that the command name is counted as one
146 argument and is saved. */
148 void silc_parse_command_line(unsigned char *buffer,
149 unsigned char ***parsed,
150 SilcUInt32 **parsed_lens,
151 SilcUInt32 **parsed_types,
152 SilcUInt32 *parsed_num,
157 const char *cp = (const char *)buffer;
160 *parsed = silc_calloc(1, sizeof(**parsed));
161 *parsed_lens = silc_calloc(1, sizeof(**parsed_lens));
163 /* Get the command first */
164 len = strcspn(cp, " ");
165 tmp = silc_calloc(strlen(cp) + 1, sizeof(*tmp));
168 silc_to_upper(cp, tmp, strlen(cp));
169 (*parsed)[0] = silc_calloc(len + 1, sizeof(char));
170 memcpy((*parsed)[0], tmp, len);
172 (*parsed_lens)[0] = len;
178 /* Parse arguments */
179 if (strchr(cp, ' ') || strlen(cp) != 0) {
180 for (i = 1; i < max_args; i++) {
182 if (i != max_args - 1)
183 len = strcspn(cp, " ");
186 while (len && cp[len - 1] == ' ')
191 *parsed = silc_realloc(*parsed, sizeof(**parsed) * (argc + 1));
192 *parsed_lens = silc_realloc(*parsed_lens,
193 sizeof(**parsed_lens) * (argc + 1));
194 (*parsed)[argc] = silc_calloc(len + 1, sizeof(char));
195 memcpy((*parsed)[argc], cp, len);
196 (*parsed_lens)[argc] = len;
208 /* Save argument types. Protocol defines all argument types but
209 this implementation makes sure that they are always in correct
210 order hence this simple code. */
211 *parsed_types = silc_calloc(argc, sizeof(**parsed_types));
212 for (i = 0; i < argc; i++)
213 (*parsed_types)[i] = i;
218 /* Formats arguments to a string and returns it after allocating memory
219 for it. It must be remembered to free it later. */
221 char *silc_format(char *fmt, ...)
227 silc_vsnprintf(buf, sizeof(buf), fmt, args);
230 return silc_strdup(buf);
233 /* Creates fingerprint from data, usually used with SHA1 digests */
235 char *silc_fingerprint(const unsigned char *data, SilcUInt32 data_len)
237 unsigned char *fingerprint, *cp;
238 unsigned int len, blocks, i;
240 if (!data || !data_len) {
241 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
248 /* Align and calculate total length */
249 len = ((data_len + 19) / 20) * 20;
251 len = (len * 2) + ((blocks - 1) * 2) + (4 * blocks) + 2 + 1;
253 cp = fingerprint = silc_calloc(len, sizeof(*fingerprint));
257 for (i = 0; i < data_len; i++) {
258 silc_snprintf((char *)cp, len, "%02X", data[i]);
262 if ((i + 1) % 2 == 0)
263 silc_snprintf((char *)cp++, len--, " ");
264 if ((i + 1) % 10 == 0)
265 silc_snprintf((char *)cp++, len--, " ");
268 if ((i + 1) % 10 == 0)
270 if ((i + 1) % 2 == 0)
273 return (char *)fingerprint;
276 /* Return TRUE if the `data' is ASCII string. */
278 SilcBool silc_string_is_ascii(const unsigned char *data, SilcUInt32 data_len)
282 for (i = 0; i < data_len; i++) {
283 if (!isascii(data[i]))
290 /* Displays input prompt on command line and takes input data from user */
292 char *silc_get_input(const char *prompt, SilcBool echo_off)
300 #ifdef HAVE_TERMIOS_H
302 struct termios to_old;
304 fd = open("/dev/tty", O_RDONLY);
306 silc_set_errno_posix(errno);
310 signal(SIGINT, SIG_IGN);
312 /* Get terminal info */
316 /* Echo OFF, and assure we can prompt and get input */
317 to.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
318 to.c_lflag |= ICANON;
320 tcsetattr(fd, TCSANOW, &to);
322 memset(input, 0, sizeof(input));
324 printf("%s", prompt);
327 if ((read(fd, input, sizeof(input))) < 0) {
328 silc_set_errno_posix(errno);
329 tcsetattr(fd, TCSANOW, &to_old);
333 if (strlen(input) <= 1) {
334 tcsetattr(fd, TCSANOW, &to_old);
335 silc_set_errno(SILC_ERR_EOF);
339 if (strchr(input, '\n'))
340 *strchr(input, '\n') = '\0';
342 /* Restore old terminfo */
343 tcsetattr(fd, TCSANOW, &to_old);
344 signal(SIGINT, SIG_DFL);
346 ret = silc_memdup(input, strlen(input));
347 memset(input, 0, sizeof(input));
348 #endif /* HAVE_TERMIOS_H */
351 fd = open("/dev/tty", O_RDONLY);
353 silc_set_errno_posix(errno);
357 memset(input, 0, sizeof(input));
359 printf("%s", prompt);
362 if ((read(fd, input, sizeof(input))) < 0) {
363 silc_set_errno_posix(errno);
367 if (strlen(input) <= 1) {
368 silc_set_errno(SILC_ERR_EOF);
372 if (strchr(input, '\n'))
373 *strchr(input, '\n') = '\0';
375 return silc_strdup(input);
379 #endif /* SILC_UNIX */
384 void silc_hexdump(const unsigned char *data, SilcUInt32 data_len,
397 if ((len - pos) < 16 && (len - pos <= len - off))
407 fprintf(output, "%08X ", k++ * 16);
409 for (i = 0; i < count; i++) {
410 fprintf(output, "%02X ", data[pos + i]);
412 if ((i + 1) % 4 == 0)
413 fprintf(output, " ");
416 if (count && count < 16) {
419 for (j = 0; j < 16 - count; j++) {
420 fprintf(output, " ");
422 if ((j + count + 1) % 4 == 0)
423 fprintf(output, " ");
427 for (i = 0; i < count; i++) {
430 if (data[pos] < 32 || data[pos] >= 127)
435 fprintf(output, "%c", ch);
440 fprintf(output, "\n");
447 /* Convert hex string to data. Each hex number must have two characters. */
449 SilcBool silc_hex2data(const char *hex, unsigned char *data,
450 SilcUInt32 data_size, SilcUInt32 *ret_data_len)
452 char *cp = (char *)hex;
456 if (data_size < strlen(hex) / 2) {
457 silc_set_errno(SILC_ERR_OVERFLOW);
461 for (i = 0; i < strlen(hex) / 2; i++) {
465 h -= h < 'A' ? '0' : 'A' - 10;
466 l -= l < 'A' ? '0' : 'A' - 10;
468 data[i] = (h << 4) | (l & 0xf);
477 /* Converts binary data to HEX string */
479 SilcBool silc_data2hex(const unsigned char *data, SilcUInt32 data_len,
480 char *hex, SilcUInt32 hex_size)
486 if (hex_size - 1 < data_len * 2) {
487 silc_set_errno(SILC_ERR_OVERFLOW);
491 memset(hex, 0, hex_size);
493 for (i = 0; i < data_len; i++) {
498 *cp++ = h + (h > 9 ? 'A' - 10 : '0');
499 *cp++ = l + (l > 9 ? 'A' - 10 : '0');