5 * \defgroup MiscRout Miscellaneous routines
10 #include "webserver.h"
13 typedef unsigned char byte; /**< byte data type */
15 #define FALSE 0 /**< no. */
16 #define TRUE 1 /**< yes. */
18 static byte dtable[256]; /**< base64 encode / decode table */
21 * \brief sanitize strncopy.
22 * \param dest destination string
23 * \param src source string
24 * \param n length of source to copy
25 * \return result string
27 char *safestrncpy(char *dest, const char *src, size_t n)
29 if (dest == NULL || src == NULL) {
32 strncpy(dest, src, n);
40 * \brief discover number of parameters/tokens in a string
41 * \param source string to inspect
42 * \param tok seperation token
43 * \return number of tokenized parts found
45 int num_tokens(char *source, char tok)
52 for (a = 0; a < strlen(source); ++a) {
60 * brief a string tokenizer
61 * \param dest destination string
62 * \param source the string to grab tokens from
63 * \param parmnum the n'th token to grab
64 * \param separator the tokenizer string
65 * \param maxlen the length of dest
67 void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
70 const char *s; /* source */
76 /* Locate desired parameter */
78 while (count < parmnum) {
79 /* End of string, bail! */
84 if (*s == separator) {
89 if (!s) return; /* Parameter not found */
91 for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
100 * \brief a tokenizer that kills, maims, and destroys
101 * \param source the string to process
102 * \param parmnum which token to kill
103 * \param separator the tokenizer string
105 void remove_token(char *source, int parmnum, char separator)
117 if (strlen(source) == 0) {
121 for (i = 0; i < strlen(source); ++i) {
122 if ((start < 0) && (curr_parm == parmnum)) {
126 if ((end < 0) && (curr_parm == (parmnum + 1))) {
130 if (source[i] == separator) {
136 end = strlen(source);
138 strcpy(&source[start], &source[end]);
145 * \brief extract an int parm w/o supplying a buffer
146 * \param source the string to locate the int in
147 * \param parmnum the n'th token to grab the int from
148 * \return the integer
150 int extract_int(const char *source, int parmnum)
154 extract_token(buf, source, parmnum, '|', sizeof buf);
159 * \brief extract an long parm w/o supplying a buffer
160 * \param source string to examine
161 * \param parmnum n'th token to search long in
162 * \return the found long value
164 long extract_long(const char *source, int parmnum)
168 extract_token(buf, source, parmnum, '|', sizeof buf);
178 * \brief check for the presence of a character within a string (returns count)
179 * \param st the string to examine
180 * \param ch the char to search
181 * \return the position inside of st
189 for (a = 0; a < strlen(st); ++a)
197 * \brief Utility function to "readline" from memory
198 * \param start position in buf
199 * \param buf the buffer to create string in???
200 * \param maxlen how big may the buffer get?
201 * \return new pointer to read string
203 char *memreadline(char *start, char *buf, int maxlen)
207 int len = 0; /**< tally our own length to avoid strlen() delays */
210 memset(buf, 0, maxlen);
214 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
215 buf[strlen(buf) + 1] = 0;
216 buf[strlen(buf)] = ch;
219 if ((ch == 10) || (ch == 0)) {
228 * \brief searches for a paternn within asearch string
229 * \param search the string to search
230 * \param patn the pattern to find in string
231 * \returns position in string
233 int pattern2(char *search, char *patn)
236 for (a = 0; a < strlen(search); ++a) {
237 if (!strncasecmp(&search[a], patn, strlen(patn)))
245 * \brief Strip leading and trailing spaces from a string
246 * \param buf the string to modify
248 void striplt(char *buf)
250 if (strlen(buf) == 0) return;
251 while ((strlen(buf) > 0) && (isspace(buf[0])))
252 strcpy(buf, &buf[1]);
253 if (strlen(buf) == 0) return;
254 while (isspace(buf[strlen(buf) - 1]))
255 buf[strlen(buf) - 1] = 0;
260 * \brief Determine whether the specified message number is contained within the
262 * \param mset stringset???
263 * \param msgnum a citadel server indexnumber
264 * \return yes / no ???
266 int is_msg_in_mset(char *mset, long msgnum) {
269 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
273 * Now set it for all specified messages.
275 num_sets = num_tokens(mset, ',');
276 for (s=0; s<num_sets; ++s) {
277 extract_token(setstr, mset, s, ',', sizeof setstr);
279 extract_token(lostr, setstr, 0, ':', sizeof lostr);
280 if (num_tokens(setstr, ':') >= 2) {
281 extract_token(histr, setstr, 1, ':', sizeof histr);
282 if (!strcmp(histr, "*")) {
283 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
287 strcpy(histr, lostr);
292 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
301 * \brief Strip a boundarized substring out of a string
302 * (for example, remove
303 * parentheses and anything inside them).
305 * This improved version can strip out *multiple* boundarized substrings.
306 * \param str the string to process
307 * \param leftboundary the boundary character on the left side of the target string
308 * \param rightboundary the boundary character on the right side of the target string
310 void stripout(char *str, char leftboundary, char rightboundary)
320 for (a = 0; a < strlen(str); ++a) {
321 if (str[a] == leftboundary)
323 if (str[a] == rightboundary)
327 if ((lb > 0) && (rb > lb)) {
328 strcpy(&str[lb - 1], &str[rb + 1]);
331 } while ((lb > 0) && (rb > lb));
338 * \brief Replacement for sleep() that uses select() in order to avoid SIGALRM
339 * \param seconds how many seconds should we sleep?
341 void sleeeeeeeeeep(int seconds)
347 select(0, NULL, NULL, NULL, &tv);
353 * \brief encode a string into base64 to for example tunnel it through mail transport
354 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
355 * John Walker, copied over from the Citadel server.
356 * \param dest encrypted string
357 * \param source the string to encrypt
358 * \param sourcelen the length of the source data (may contain string terminators)
361 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
363 int i, hiteof = FALSE;
368 /** Fill dtable with character encodings. */
370 for (i = 0; i < 26; i++) {
372 dtable[26 + i] = 'a' + i;
374 for (i = 0; i < 10; i++) {
375 dtable[52 + i] = '0' + i;
381 byte igroup[3], ogroup[4];
384 igroup[0] = igroup[1] = igroup[2] = 0;
385 for (n = 0; n < 3; n++) {
386 if (spos >= sourcelen) {
391 igroup[n] = (byte) c;
394 ogroup[0] = dtable[igroup[0] >> 2];
396 dtable[((igroup[0] & 3) << 4) |
399 dtable[((igroup[1] & 0xF) << 2) |
401 ogroup[3] = dtable[igroup[2] & 0x3F];
404 * Replace characters in output stream with "=" pad
405 * characters if fewer than three characters were
406 * read from the end of the input stream.
415 for (i = 0; i < 4; i++) {
416 dest[dpos++] = ogroup[i];
438 * \brief Convert base64-encoded to binary.
439 * It will stop after reading 'length' bytes.
440 * \param dest the destination buffer
441 * \param source the encrypted string
442 * \param length the maximal length of dest???
443 * \return the length actual length of the decoded data.
445 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
451 for (i = 0; i < 255; i++) {
454 for (i = 'A'; i <= 'Z'; i++) {
455 dtable[i] = 0 + (i - 'A');
457 for (i = 'a'; i <= 'z'; i++) {
458 dtable[i] = 26 + (i - 'a');
460 for (i = '0'; i <= '9'; i++) {
461 dtable[i] = 52 + (i - '0');
467 /**CONSTANTCONDITION*/ while (TRUE) {
468 byte a[4], b[4], o[3];
470 for (i = 0; i < 4; i++) {
471 if (spos >= length) {
482 if (dtable[c] & 0x80) {
483 /** Ignoring errors: discard invalid character */
488 b[i] = (byte) dtable[c];
490 o[0] = (b[0] << 2) | (b[1] >> 4);
491 o[1] = (b[1] << 4) | (b[2] >> 2);
492 o[2] = (b[2] << 6) | b[3];
493 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
510 * \brief Generate a new, globally unique UID parameter for a calendar etc. object
511 * \param buf uid to create ????
513 void generate_uuid(char *buf) {
516 sprintf(buf, "%s-%lx-%x-%x",
517 serv_info.serv_nodename,
526 * \brief Local replacement for controversial C library function that generates
527 * names for temporary files. Included to shut up compiler warnings.
528 * \todo return a fd to the file instead of the name for security reasons
529 * \param name the created filename
530 * \param len the length of the filename
532 void CtdlMakeTempFileName(char *name, int len) {
535 while (i++, i < 100) {
536 snprintf(name, len, "/tmp/ctdl.%04x.%04x",
540 if (!access(name, F_OK)) {