5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * These are general utility functions that doesn't belong to any specific
26 #include "silcincludes.h"
28 /* Reads a file to a buffer. The allocated buffer is returned. Length of
29 the file read is returned to the return_len argument. */
31 char *silc_file_read(const char *filename, int *return_len)
37 fd = open(filename, O_RDONLY);
39 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
43 filelen = lseek(fd, (off_t)0L, SEEK_END);
46 if (lseek(fd, (off_t)0L, SEEK_SET) < 0)
50 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
54 buffer = silc_calloc(filelen + 1, sizeof(char));
56 if ((read(fd, buffer, filelen)) == -1) {
57 memset(buffer, 0, sizeof(buffer));
59 SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
65 buffer[filelen] = EOF;
68 *return_len = filelen;
73 /* Writes a buffer to the file. */
75 int silc_file_write(const char *filename, const char *buffer, int len)
79 if ((fd = creat(filename, 0644)) == -1) {
80 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", strerror(errno)));
84 if ((write(fd, buffer, len)) == -1) {
85 SILC_LOG_ERROR(("Cannot write to file %s: %s", strerror(errno)));
94 /* Writes a buffer to the file. If the file is created specific mode is
97 int silc_file_write_mode(const char *filename, const char *buffer,
102 if ((fd = creat(filename, mode)) == -1) {
103 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", strerror(errno)));
107 if ((write(fd, buffer, len)) == -1) {
108 SILC_LOG_ERROR(("Cannot write to file %s: %s", strerror(errno)));
117 /* Gets line from a buffer. Stops reading when a newline or EOF occurs.
118 This doesn't remove the newline sign from the destination buffer. The
119 argument begin is returned and should be passed again for the function. */
121 int silc_gets(char *dest, int destlen, const char *src, int srclen, int begin)
123 static int start = 0;
126 memset(dest, 0, destlen);
132 for ( ; start <= srclen; i++, start++) {
136 dest[i] = src[start];
149 /* Checks line for illegal characters. Return -1 when illegal character
150 were found. This is used to check for bad lines when reading data from
151 for example a configuration file. */
153 int silc_check_line(char *buf)
155 /* Illegal characters in line */
156 if (strchr(buf, '#')) return -1;
157 if (strchr(buf, '\'')) return -1;
158 if (strchr(buf, '\\')) return -1;
159 if (strchr(buf, '\r')) return -1;
160 if (strchr(buf, '\a')) return -1;
161 if (strchr(buf, '\b')) return -1;
162 if (strchr(buf, '\f')) return -1;
171 /* Returns current time as string. */
173 char *silc_get_time()
178 curtime = time(NULL);
179 return_time = ctime(&curtime);
180 return_time[strlen(return_time) - 1] = '\0';
185 /* Converts string to capital characters */
187 char *silc_to_upper(char *string)
190 char *ret = silc_calloc(strlen(string) + 1, sizeof(char));
192 for (i = 0; i < strlen(string); i++)
193 ret[i] = toupper(string[i]);
198 /* Compares two strings. Strings may include wildcards * and ?.
199 Returns TRUE if strings match. */
201 int silc_string_compare(char *string1, char *string2)
204 int slen1 = strlen(string1);
205 int slen2 = strlen(string2);
206 char *tmpstr1, *tmpstr2;
208 if (!string1 || !string2)
211 /* See if they are same already */
212 if (!strncmp(string1, string2, strlen(string2)))
216 if (!strchr(string1, '*'))
219 /* Take copies of the original strings as we will change them */
220 tmpstr1 = silc_calloc(slen1 + 1, sizeof(char));
221 memcpy(tmpstr1, string1, slen1);
222 tmpstr2 = silc_calloc(slen2 + 1, sizeof(char));
223 memcpy(tmpstr2, string2, slen2);
225 for (i = 0; i < slen2; i++) {
227 /* * wildcard. Only one * wildcard is possible. */
228 if (tmpstr1[i] == '*')
229 if (!strncmp(tmpstr1, tmpstr2, i)) {
230 memset(tmpstr2, 0, slen2);
231 strncpy(tmpstr2, tmpstr1, i);
236 if (tmpstr1[i] == '?') {
237 if (!strncmp(tmpstr1, tmpstr2, i)) {
238 if (!(slen1 < i + 1))
239 if (tmpstr1[i + 1] != '?' &&
240 tmpstr1[i + 1] != tmpstr2[i + 1])
243 if (!(slen1 < slen2))
248 if (strncmp(tmpstr1, tmpstr2, i))
249 strncpy(tmpstr2, string2, slen2);
254 /* if using *, remove it */
255 if (strchr(tmpstr1, '*'))
256 *strchr(tmpstr1, '*') = 0;
258 if (!strcmp(tmpstr1, tmpstr2)) {
259 memset(tmpstr1, 0, slen1);
260 memset(tmpstr2, 0, slen2);
266 memset(tmpstr1, 0, slen1);
267 memset(tmpstr2, 0, slen2);
273 static unsigned char pem_enc[64] =
274 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
276 /* Encodes data into PEM encoding. Returns NULL terminated PEM encoded
277 data string. Note: This is originally public domain code and is
280 char *silc_encode_pem(unsigned char *data, unsigned int len)
283 unsigned int bits, c, char_count;
290 pem = silc_calloc(((len * 8 + 5) / 6) + 5, sizeof(*pem));
292 for (i = 0; i < len; i++) {
297 if (char_count == 3) {
298 pem[j++] = pem_enc[bits >> 18];
299 pem[j++] = pem_enc[(bits >> 12) & 0x3f];
300 pem[j++] = pem_enc[(bits >> 6) & 0x3f];
301 pem[j++] = pem_enc[bits & 0x3f];
309 if (char_count != 0) {
310 bits <<= 16 - (8 * char_count);
311 pem[j++] = pem_enc[bits >> 18];
312 pem[j++] = pem_enc[(bits >> 12) & 0x3f];
314 if (char_count == 1) {
318 pem[j++] = pem_enc[(bits >> 6) & 0x3f];
326 /* Same as above but puts newline ('\n') every 72 characters. */
328 char *silc_encode_pem_file(unsigned char *data, unsigned int data_len)
331 unsigned int len, cols;
334 pem = silc_encode_pem(data, data_len);
337 pem2 = silc_calloc(len + (len / 72) + 1, sizeof(*pem2));
339 for (i = 0, j = 0, cols = 1; i < len; i++, cols++) {
353 /* Decodes PEM into data. Returns the decoded data. Note: This is
354 originally public domain code and is still PD. */
356 unsigned char *silc_decode_pem(unsigned char *pem, unsigned int pem_len,
357 unsigned int *ret_len)
360 unsigned int len, c, char_count, bits;
362 static char ialpha[256], decoder[256];
364 for (i = 64 - 1; i >= 0; i--) {
365 ialpha[pem_enc[i]] = 1;
366 decoder[pem_enc[i]] = i;
378 data = silc_calloc(((len * 6) / 8), sizeof(*data));
380 for (i = 0; i < len; i++) {
386 if (c > 127 || !ialpha[c])
392 if (char_count == 4) {
393 data[j++] = bits >> 16;
394 data[j++] = (bits >> 8) & 0xff;
395 data[j++] = bits & 0xff;
409 data[j++] = bits >> 10;
412 data[j++] = bits >> 16;
413 data[j++] = (bits >> 8) & 0xff;
423 /* Parse nickname string. The format may be <num>!<nickname>@<server> to
424 support multiple same nicknames. The <num> is the final unifier if same
425 nickname is on same server. Note, this is only local format and server
426 does not know anything about these. */
428 int silc_parse_nickname(char *string, char **nickname, char **server,
437 if (strchr(string, '!')) {
438 tlen = strcspn(string, "!");
439 memset(tmp, 0, sizeof(tmp));
440 memcpy(tmp, string, tlen);
445 if (tlen >= strlen(string))
451 if (strchr(string, '@')) {
452 tlen = strcspn(string, "@");
455 *nickname = silc_calloc(tlen + 1, sizeof(char));
456 memcpy(*nickname, string, tlen);
460 *server = silc_calloc((strlen(string) - tlen) + 1, sizeof(char));
461 memcpy(*server, string + tlen + 1, strlen(string) - tlen - 1);
465 *nickname = strdup(string);
471 /* Parses command line. At most `max_args' is taken. Rest of the line
472 will be allocated as the last argument if there are more than `max_args'
473 arguments in the line. Note that the command name is counted as one
474 argument and is saved. */
476 void silc_parse_command_line(unsigned char *buffer,
477 unsigned char ***parsed,
478 unsigned int **parsed_lens,
479 unsigned int **parsed_types,
480 unsigned int *parsed_num,
481 unsigned int max_args)
485 const char *cp = buffer;
487 *parsed = silc_calloc(1, sizeof(**parsed));
488 *parsed_lens = silc_calloc(1, sizeof(**parsed_lens));
490 /* Get the command first */
491 len = strcspn(cp, " ");
492 (*parsed)[0] = silc_to_upper((char *)cp);
493 (*parsed_lens)[0] = len;
497 /* Parse arguments */
498 if (strchr(cp, ' ') || strlen(cp) != 0) {
499 for (i = 1; i < max_args; i++) {
501 if (i != max_args - 1)
502 len = strcspn(cp, " ");
506 *parsed = silc_realloc(*parsed, sizeof(**parsed) * (argc + 1));
507 *parsed_lens = silc_realloc(*parsed_lens,
508 sizeof(**parsed_lens) * (argc + 1));
509 (*parsed)[argc] = silc_calloc(len + 1, sizeof(char));
510 memcpy((*parsed)[argc], cp, len);
511 (*parsed_lens)[argc] = len;
522 /* Save argument types. Protocol defines all argument types but
523 this implementation makes sure that they are always in correct
524 order hence this simple code. */
525 *parsed_types = silc_calloc(argc, sizeof(**parsed_types));
526 for (i = 0; i < argc; i++)
527 (*parsed_types)[i] = i;
532 /* Formats arguments to a string and returns it after allocating memory
533 for it. It must be remembered to free it later. */
535 char *silc_format(char *fmt, ...)
538 static char buf[8192];
540 memset(buf, 0, sizeof(buf));
542 vsnprintf(buf, sizeof(buf) - 1, fmt, args);
548 /* Renders ID to suitable to print for example to log file. */
550 static char rid[256];
552 char *silc_id_render(void *id, unsigned short type)
555 unsigned char tmps[2];
557 memset(rid, 0, sizeof(rid));
561 SilcServerID *server_id = (SilcServerID *)id;
562 strcat(rid, inet_ntoa(server_id->ip));
563 memset(tmp, 0, sizeof(tmp));
564 snprintf(tmp, sizeof(tmp), ",%d,", ntohs(server_id->port));
566 SILC_PUT16_MSB(server_id->rnd, tmps);
567 memset(tmp, 0, sizeof(tmp));
568 snprintf(tmp, sizeof(tmp), "[%02x %02x]", tmps[0], tmps[1]);
574 SilcClientID *client_id = (SilcClientID *)id;
575 strcat(rid, inet_ntoa(client_id->ip));
576 memset(tmp, 0, sizeof(tmp));
577 snprintf(tmp, sizeof(tmp), ",%02x,", client_id->rnd);
579 memset(tmp, 0, sizeof(tmp));
580 snprintf(tmp, sizeof(tmp), "[%02x %02x %02x %02x...]",
581 client_id->hash[0], client_id->hash[1],
582 client_id->hash[2], client_id->hash[3]);
586 case SILC_ID_CHANNEL:
588 SilcChannelID *channel_id = (SilcChannelID *)id;
589 strcat(rid, inet_ntoa(channel_id->ip));
590 memset(tmp, 0, sizeof(tmp));
591 snprintf(tmp, sizeof(tmp), ",%d,", ntohs(channel_id->port));
593 SILC_PUT16_MSB(channel_id->rnd, tmps);
594 memset(tmp, 0, sizeof(tmp));
595 snprintf(tmp, sizeof(tmp), "[%02x %02x]", tmps[0], tmps[1]);