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);
110 memset(user, 0, user_size);
112 memset(fqdn, 0, fqdn_size);
115 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
119 if (string[0] == '@') {
121 silc_strncat(user, user_size, string, strlen(string));
126 if (strchr(string, '@')) {
127 tlen = strcspn(string, "@");
130 silc_strncat(user, user_size, string, tlen);
133 silc_strncat(fqdn, fqdn_size, string + tlen + 1,
134 strlen(string) - tlen - 1);
140 silc_strncat(user, user_size, string, strlen(string));
145 /* Parses command line. At most `max_args' is taken. Rest of the line
146 will be allocated as the last argument if there are more than `max_args'
147 arguments in the line. Note that the command name is counted as one
148 argument and is saved. */
150 void silc_parse_command_line(unsigned char *buffer,
151 unsigned char ***parsed,
152 SilcUInt32 **parsed_lens,
153 SilcUInt32 **parsed_types,
154 SilcUInt32 *parsed_num,
159 const char *cp = (const char *)buffer;
162 *parsed = silc_calloc(1, sizeof(**parsed));
163 *parsed_lens = silc_calloc(1, sizeof(**parsed_lens));
165 /* Get the command first */
166 len = strcspn(cp, " ");
167 tmp = silc_calloc(strlen(cp) + 1, sizeof(*tmp));
170 silc_to_upper(cp, tmp, strlen(cp));
171 (*parsed)[0] = silc_calloc(len + 1, sizeof(char));
172 memcpy((*parsed)[0], tmp, len);
174 (*parsed_lens)[0] = len;
180 /* Parse arguments */
181 if (strchr(cp, ' ') || strlen(cp) != 0) {
182 for (i = 1; i < max_args; i++) {
184 if (i != max_args - 1)
185 len = strcspn(cp, " ");
188 while (len && cp[len - 1] == ' ')
193 *parsed = silc_realloc(*parsed, sizeof(**parsed) * (argc + 1));
194 *parsed_lens = silc_realloc(*parsed_lens,
195 sizeof(**parsed_lens) * (argc + 1));
196 (*parsed)[argc] = silc_calloc(len + 1, sizeof(char));
197 memcpy((*parsed)[argc], cp, len);
198 (*parsed_lens)[argc] = len;
210 /* Save argument types. Protocol defines all argument types but
211 this implementation makes sure that they are always in correct
212 order hence this simple code. */
213 *parsed_types = silc_calloc(argc, sizeof(**parsed_types));
214 for (i = 0; i < argc; i++)
215 (*parsed_types)[i] = i;
220 /* Formats arguments to a string and returns it after allocating memory
221 for it. It must be remembered to free it later. */
223 char *silc_format(char *fmt, ...)
229 silc_vsnprintf(buf, sizeof(buf), fmt, args);
232 return silc_strdup(buf);
235 /* Creates fingerprint from data, usually used with SHA1 digests */
237 char *silc_fingerprint(const unsigned char *data, SilcUInt32 data_len)
239 unsigned char *fingerprint, *cp;
240 unsigned int len, blocks, i;
242 if (!data || !data_len) {
243 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
250 /* Align and calculate total length */
251 len = ((data_len + 19) / 20) * 20;
253 len = (len * 2) + ((blocks - 1) * 2) + (4 * blocks) + 2 + 1;
255 cp = fingerprint = silc_calloc(len, sizeof(*fingerprint));
259 for (i = 0; i < data_len; i++) {
260 silc_snprintf((char *)cp, len, "%02X", data[i]);
264 if ((i + 1) % 2 == 0)
265 silc_snprintf((char *)cp++, len--, " ");
266 if ((i + 1) % 10 == 0)
267 silc_snprintf((char *)cp++, len--, " ");
270 if ((i + 1) % 10 == 0)
272 if ((i + 1) % 2 == 0)
275 return (char *)fingerprint;
278 /* Return TRUE if the `data' is ASCII string. */
280 SilcBool silc_string_is_ascii(const unsigned char *data, SilcUInt32 data_len)
284 for (i = 0; i < data_len; i++) {
285 if (!isascii(data[i]))
292 /* Displays input prompt on command line and takes input data from user */
294 char *silc_get_input(const char *prompt, SilcBool echo_off)
302 #ifdef HAVE_TERMIOS_H
304 struct termios to_old;
306 fd = open("/dev/tty", O_RDONLY);
308 silc_set_errno_posix(errno);
312 signal(SIGINT, SIG_IGN);
314 /* Get terminal info */
318 /* Echo OFF, and assure we can prompt and get input */
319 to.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
320 to.c_lflag |= ICANON;
322 tcsetattr(fd, TCSANOW, &to);
324 memset(input, 0, sizeof(input));
326 printf("%s", prompt);
329 if ((read(fd, input, sizeof(input))) < 0) {
330 silc_set_errno_posix(errno);
331 tcsetattr(fd, TCSANOW, &to_old);
335 if (strlen(input) <= 1) {
336 tcsetattr(fd, TCSANOW, &to_old);
337 silc_set_errno(SILC_ERR_EOF);
341 if (strchr(input, '\n'))
342 *strchr(input, '\n') = '\0';
344 /* Restore old terminfo */
345 tcsetattr(fd, TCSANOW, &to_old);
346 signal(SIGINT, SIG_DFL);
348 ret = silc_memdup(input, strlen(input));
349 memset(input, 0, sizeof(input));
350 #endif /* HAVE_TERMIOS_H */
353 fd = open("/dev/tty", O_RDONLY);
355 silc_set_errno_posix(errno);
359 memset(input, 0, sizeof(input));
361 printf("%s", prompt);
364 if ((read(fd, input, sizeof(input))) < 0) {
365 silc_set_errno_posix(errno);
369 if (strlen(input) <= 1) {
370 silc_set_errno(SILC_ERR_EOF);
374 if (strchr(input, '\n'))
375 *strchr(input, '\n') = '\0';
377 return silc_strdup(input);
381 #endif /* SILC_UNIX */
386 void silc_hexdump(const unsigned char *data, SilcUInt32 data_len,
399 if ((len - pos) < 16 && (len - pos <= len - off))
409 fprintf(output, "%08X ", k++ * 16);
411 for (i = 0; i < count; i++) {
412 fprintf(output, "%02X ", data[pos + i]);
414 if ((i + 1) % 4 == 0)
415 fprintf(output, " ");
418 if (count && count < 16) {
421 for (j = 0; j < 16 - count; j++) {
422 fprintf(output, " ");
424 if ((j + count + 1) % 4 == 0)
425 fprintf(output, " ");
429 for (i = 0; i < count; i++) {
432 if (data[pos] < 32 || data[pos] >= 127)
437 fprintf(output, "%c", ch);
442 fprintf(output, "\n");
449 /* Convert hex string to data. Each hex number must have two characters. */
451 SilcBool silc_hex2data(const char *hex, unsigned char *data,
452 SilcUInt32 data_size, SilcUInt32 *ret_data_len)
454 char *cp = (char *)hex;
458 if (data_size < strlen(hex) / 2) {
459 silc_set_errno(SILC_ERR_OVERFLOW);
463 for (i = 0; i < strlen(hex) / 2; i++) {
467 h -= h < 'A' ? '0' : 'A' - 10;
468 l -= l < 'A' ? '0' : 'A' - 10;
470 data[i] = (h << 4) | (l & 0xf);
479 /* Converts binary data to HEX string */
481 SilcBool silc_data2hex(const unsigned char *data, SilcUInt32 data_len,
482 char *hex, SilcUInt32 hex_size)
488 if (hex_size - 1 < data_len * 2) {
489 silc_set_errno(SILC_ERR_OVERFLOW);
493 memset(hex, 0, hex_size);
495 for (i = 0; i < data_len; i++) {
500 *cp++ = h + (h > 9 ? 'A' - 10 : '0');
501 *cp++ = l + (l > 9 ? 'A' - 10 : '0');