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
27 * Revision 1.3 2000/07/10 05:34:40 priikone
28 * Added PEM encoding/decoding functions.
30 * Revision 1.2 2000/07/05 06:06:12 priikone
31 * Added file saving with specific mode.
33 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
34 * Imported from internal CVS/Added Log headers.
39 #include "silcincludes.h"
41 /* Reads a file to a buffer. The allocated buffer is returned. Length of
42 the file read is returned to the return_len argument. */
44 char *silc_file_read(const char *filename, int *return_len)
50 fd = open(filename, O_RDONLY);
52 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
56 filelen = lseek(fd, (off_t)0L, SEEK_END);
59 if (lseek(fd, (off_t)0L, SEEK_SET) < 0)
63 SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
67 buffer = silc_calloc(filelen + 1, sizeof(char));
69 if ((read(fd, buffer, filelen)) == -1) {
70 memset(buffer, 0, sizeof(buffer));
72 SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
78 buffer[filelen] = EOF;
80 *return_len = filelen;
84 /* Writes a buffer to the file. */
86 int silc_file_write(const char *filename, const char *buffer, int len)
90 if ((fd = creat(filename, 0644)) == -1) {
91 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", strerror(errno)));
95 if ((write(fd, buffer, len)) == -1) {
96 SILC_LOG_ERROR(("Cannot write to file %s: %s", strerror(errno)));
105 /* Writes a buffer to the file. If the file is created specific mode is
108 int silc_file_write_mode(const char *filename, const char *buffer,
113 if ((fd = creat(filename, mode)) == -1) {
114 SILC_LOG_ERROR(("Cannot open file %s for writing: %s", strerror(errno)));
118 if ((write(fd, buffer, len)) == -1) {
119 SILC_LOG_ERROR(("Cannot write to file %s: %s", strerror(errno)));
128 /* Gets line from a buffer. Stops reading when a newline or EOF occurs.
129 This doesn't remove the newline sign from the destination buffer. The
130 argument begin is returned and should be passed again for the function. */
132 int silc_gets(char *dest, int destlen, const char *src, int srclen, int begin)
134 static int start = 0;
137 memset(dest, 0, destlen);
143 for ( ; start <= srclen; i++, start++) {
147 dest[i] = src[start];
160 /* Checks line for illegal characters. Return -1 when illegal character
161 were found. This is used to check for bad lines when reading data from
162 for example a configuration file. */
164 int silc_check_line(char *buf)
166 /* Illegal characters in line */
167 if (strchr(buf, '#')) return -1;
168 if (strchr(buf, '\'')) return -1;
169 if (strchr(buf, '\\')) return -1;
170 if (strchr(buf, '\r')) return -1;
171 if (strchr(buf, '\a')) return -1;
172 if (strchr(buf, '\b')) return -1;
173 if (strchr(buf, '\f')) return -1;
182 /* Returns current time as string. */
184 char *silc_get_time()
189 curtime = time(NULL);
190 return_time = ctime(&curtime);
191 return_time[strlen(return_time) - 1] = '\0';
196 /* Converts string to capital characters */
198 char *silc_to_upper(char *string)
201 char *ret = silc_calloc(strlen(string) + 1, sizeof(char));
203 for (i = 0; i < strlen(string); i++)
204 ret[i] = toupper(string[i]);
209 /* Compares two strings. Strings may include wildcards * and ?.
210 Returns TRUE if strings match. */
212 int silc_string_compare(char *string1, char *string2)
215 int slen1 = strlen(string1);
216 int slen2 = strlen(string2);
217 char *tmpstr1, *tmpstr2;
219 if (!string1 || !string2)
222 /* See if they are same already */
223 if (!strncmp(string1, string2, strlen(string2)))
227 if (!strchr(string1, '*'))
230 /* Take copies of the original strings as we will change them */
231 tmpstr1 = silc_calloc(slen1 + 1, sizeof(char));
232 memcpy(tmpstr1, string1, slen1);
233 tmpstr2 = silc_calloc(slen2 + 1, sizeof(char));
234 memcpy(tmpstr2, string2, slen2);
236 for (i = 0; i < slen2; i++) {
238 /* * wildcard. Only one * wildcard is possible. */
239 if (tmpstr1[i] == '*')
240 if (!strncmp(tmpstr1, tmpstr2, i)) {
241 memset(tmpstr2, 0, slen2);
242 strncpy(tmpstr2, tmpstr1, i);
247 if (tmpstr1[i] == '?') {
248 if (!strncmp(tmpstr1, tmpstr2, i)) {
249 if (!(slen1 < i + 1))
250 if (tmpstr1[i + 1] != '?' &&
251 tmpstr1[i + 1] != tmpstr2[i + 1])
254 if (!(slen1 < slen2))
259 if (strncmp(tmpstr1, tmpstr2, i))
260 strncpy(tmpstr2, string2, slen2);
265 /* if using *, remove it */
266 if (strchr(tmpstr1, '*'))
267 *strchr(tmpstr1, '*') = 0;
269 if (!strcmp(tmpstr1, tmpstr2)) {
270 memset(tmpstr1, 0, slen1);
271 memset(tmpstr2, 0, slen2);
277 memset(tmpstr1, 0, slen1);
278 memset(tmpstr2, 0, slen2);
284 unsigned char pem_enc[64] =
285 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
287 /* Encodes data into PEM encoding. Returns NULL terminated PEM encoded
288 data string. Note: This is originally public domain code and is
291 char *silc_encode_pem(unsigned char *data, unsigned int len)
294 unsigned int bits, c, char_count;
301 pem = silc_calloc(((len * 8 + 5) / 6) + 5, sizeof(*pem));
303 for (i = 0; i < len; i++) {
308 if (char_count == 3) {
309 pem[j++] = pem_enc[bits >> 18];
310 pem[j++] = pem_enc[(bits >> 12) & 0x3f];
311 pem[j++] = pem_enc[(bits >> 6) & 0x3f];
312 pem[j++] = pem_enc[bits & 0x3f];
320 if (char_count != 0) {
321 bits <<= 16 - (8 * char_count);
322 pem[j++] = pem_enc[bits >> 18];
323 pem[j++] = pem_enc[(bits >> 12) & 0x3f];
325 if (char_count == 1) {
329 pem[j++] = pem_enc[(bits >> 6) & 0x3f];
337 /* Same as above but puts newline ('\n') every 72 characters. */
339 char *silc_encode_pem_file(unsigned char *data, unsigned int data_len)
342 unsigned int len, cols;
345 pem = silc_encode_pem(data, data_len);
348 pem2 = silc_calloc(len + (len / 72) + 1, sizeof(*pem2));
350 for (i = 0, j = 0, cols = 1; i < len; i++, cols++) {
364 /* Decodes PEM into data. Returns the decoded data. Note: This is
365 originally public domain code and is still PD. */
367 unsigned char *silc_decode_pem(unsigned char *pem, unsigned int pem_len,
368 unsigned int *ret_len)
371 unsigned int len, c, char_count, bits;
373 static char ialpha[256], decoder[256];
375 for (i = 64 - 1; i >= 0; i--) {
376 ialpha[pem_enc[i]] = 1;
377 decoder[pem_enc[i]] = i;
389 data = silc_calloc(((len * 6) / 8), sizeof(*data));
391 for (i = 0; i < len; i++) {
397 if (c > 127 || !ialpha[c])
403 if (char_count == 4) {
404 data[j++] = bits >> 16;
405 data[j++] = (bits >> 8) & 0xff;
406 data[j++] = bits & 0xff;
420 data[j] = bits >> 10;
423 data[j++] = bits >> 16;
424 data[j] = (bits >> 8) & 0xff;