5 * \defgroup MiscRout Miscellaneous routines
11 #include "webserver.h"
14 typedef unsigned char byte; /**< byte data type */
16 #define FALSE 0 /**< no. */
17 #define TRUE 1 /**< yes. */
19 static byte dtable[256]; /**< base64 encode / decode table */
22 * \brief sanitize strncopy.
23 * \param dest destination string
24 * \param src source string
25 * \param n length of source to copy
26 * \return result string
28 char *safestrncpy(char *dest, const char *src, size_t n)
30 if (dest == NULL || src == NULL) {
33 strncpy(dest, src, n);
41 * \brief discover number of parameters/tokens in a string
42 * \param source string to inspect
43 * \param tok seperation token
44 * \return number of tokenized parts found
46 int num_tokens(char *source, char tok)
53 for (a = 0; a < strlen(source); ++a) {
61 * brief a string tokenizer
62 * \param dest destination string
63 * \param source the string to grab tokens from
64 * \param parmnum the n'th token to grab
65 * \param separator the tokenizer string
66 * \param maxlen the length of dest
68 void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
71 const char *s; /* source */
77 /* Locate desired parameter */
79 while (count < parmnum) {
80 /* End of string, bail! */
85 if (*s == separator) {
90 if (!s) return; /* Parameter not found */
92 for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
101 * \brief a tokenizer that kills, maims, and destroys
102 * \param source the string to process
103 * \param parmnum which token to kill
104 * \param separator the tokenizer string
106 void remove_token(char *source, int parmnum, char separator)
118 slen = strlen(source);
124 ( (i < slen) && (end == -1) );
126 if ((start < 0) && (curr_parm == parmnum)) {
130 if ((end < 0) && (curr_parm == (parmnum + 1))) {
134 if (source[i] == separator) {
142 memmove(&source[start], &source[end], slen - end + 1);
149 * \brief extract an int parm w/o supplying a buffer
150 * \param source the string to locate the int in
151 * \param parmnum the n'th token to grab the int from
152 * \return the integer
154 int extract_int(const char *source, int parmnum)
158 extract_token(buf, source, parmnum, '|', sizeof buf);
163 * \brief extract an long parm w/o supplying a buffer
164 * \param source string to examine
165 * \param parmnum n'th token to search long in
166 * \return the found long value
168 long extract_long(const char *source, int parmnum)
172 extract_token(buf, source, parmnum, '|', sizeof buf);
182 * \brief check for the presence of a character within a string (returns count)
183 * \param st the string to examine
184 * \param ch the char to search
185 * \return the position inside of st
187 int haschar(char *st,char ch)
191 for (a = 0; a < strlen(st); ++a)
199 * \brief Utility function to "readline" from memory
200 * \param start Location in memory from which we are reading.
201 * \param buf the buffer to place the string in.
202 * \param maxlen Size of string buffer
203 * \return Pointer to the source memory right after we stopped reading.
205 char *memreadline(char *start, char *buf, int maxlen)
209 int len = 0; /**< tally our own length to avoid strlen() delays */
212 memset(buf, 0, maxlen);
216 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
217 buf[strlen(buf) + 1] = 0;
218 buf[strlen(buf)] = ch;
221 if ((ch == 10) || (ch == 0)) {
230 * \brief searches for a paternn within asearch string
231 * \param search the string to search
232 * \param patn the pattern to find in string
233 * \returns position in string
235 int pattern2(char *search, char *patn)
238 for (a = 0; a < strlen(search); ++a) {
239 if (!strncasecmp(&search[a], patn, strlen(patn)))
247 * \brief Strip leading and trailing spaces from a string
248 * \param buf the string to modify
250 void striplt(char *buf)
252 if (strlen(buf) == 0) return;
253 while ((strlen(buf) > 0) && (isspace(buf[0])))
254 strcpy(buf, &buf[1]);
255 if (strlen(buf) == 0) return;
256 while (isspace(buf[strlen(buf) - 1]))
257 buf[strlen(buf) - 1] = 0;
262 * \brief Determine whether the specified message number is contained within the
265 * \param mset Message set string
266 * \param msgnum Message number we are looking for
268 * \return Nonzero if the specified message number is in the specified message set string.
270 int is_msg_in_mset(char *mset, long msgnum) {
273 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
277 * Now set it for all specified messages.
279 num_sets = num_tokens(mset, ',');
280 for (s=0; s<num_sets; ++s) {
281 extract_token(setstr, mset, s, ',', sizeof setstr);
283 extract_token(lostr, setstr, 0, ':', sizeof lostr);
284 if (num_tokens(setstr, ':') >= 2) {
285 extract_token(histr, setstr, 1, ':', sizeof histr);
286 if (!strcmp(histr, "*")) {
287 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
291 strcpy(histr, lostr);
296 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
305 * \brief Strip a boundarized substring out of a string
306 * (for example, remove
307 * parentheses and anything inside them).
309 * This improved version can strip out *multiple* boundarized substrings.
310 * \param str the string to process
311 * \param leftboundary the boundary character on the left side of the target string
312 * \param rightboundary the boundary character on the right side of the target string
314 void stripout(char *str, char leftboundary, char rightboundary)
324 for (a = 0; a < strlen(str); ++a) {
325 if (str[a] == leftboundary)
327 if (str[a] == rightboundary)
331 if ((lb > 0) && (rb > lb)) {
332 strcpy(&str[lb - 1], &str[rb + 1]);
335 } while ((lb > 0) && (rb > lb));
342 * \brief Replacement for sleep() that uses select() in order to avoid SIGALRM
343 * \param seconds how many seconds should we sleep?
345 void sleeeeeeeeeep(int seconds)
351 select(0, NULL, NULL, NULL, &tv);
357 * \brief encode a string into base64 to for example tunnel it through mail transport
358 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
359 * John Walker, copied over from the Citadel server.
360 * \param dest encrypted string
361 * \param source the string to encrypt
362 * \param sourcelen the length of the source data (may contain string terminators)
365 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int linebreaks)
367 int i, hiteof = FALSE;
372 /** Fill dtable with character encodings. */
374 for (i = 0; i < 26; i++) {
376 dtable[26 + i] = 'a' + i;
378 for (i = 0; i < 10; i++) {
379 dtable[52 + i] = '0' + i;
385 byte igroup[3], ogroup[4];
388 igroup[0] = igroup[1] = igroup[2] = 0;
389 for (n = 0; n < 3; n++) {
390 if (spos >= sourcelen) {
395 igroup[n] = (byte) c;
398 ogroup[0] = dtable[igroup[0] >> 2];
400 dtable[((igroup[0] & 3) << 4) |
403 dtable[((igroup[1] & 0xF) << 2) |
405 ogroup[3] = dtable[igroup[2] & 0x3F];
408 * Replace characters in output stream with "=" pad
409 * characters if fewer than three characters were
410 * read from the end of the input stream.
419 for (i = 0; i < 4; i++) {
420 dest[dpos++] = ogroup[i];
424 if ( (linebreaks) && (thisline > 70) ) {
432 if ( (linebreaks) && (thisline > 70) ) {
442 * \brief Convert base64-encoded to binary.
443 * It will stop after reading 'length' bytes.
445 * \param dest The destination buffer
446 * \param source The base64 data to be decoded.
447 * \param length The number of bytes to decode.
448 * \return The actual length of the decoded data.
450 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
456 for (i = 0; i < 255; i++) {
459 for (i = 'A'; i <= 'Z'; i++) {
460 dtable[i] = 0 + (i - 'A');
462 for (i = 'a'; i <= 'z'; i++) {
463 dtable[i] = 26 + (i - 'a');
465 for (i = '0'; i <= '9'; i++) {
466 dtable[i] = 52 + (i - '0');
472 /**CONSTANTCONDITION*/ while (TRUE) {
473 byte a[4], b[4], o[3];
475 for (i = 0; i < 4; i++) {
476 if (spos >= length) {
487 if (dtable[c] & 0x80) {
488 /** Ignoring errors: discard invalid character */
493 b[i] = (byte) dtable[c];
495 o[0] = (b[0] << 2) | (b[1] >> 4);
496 o[1] = (b[1] << 4) | (b[2] >> 2);
497 o[2] = (b[2] << 6) | b[3];
498 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
515 * \brief Generate a new, globally unique UID parameter for a calendar etc. object
517 * \param buf String buffer into which our newly created UUID should be placed
519 void generate_uuid(char *buf) {
522 sprintf(buf, "%s-%lx-%lx-%x",
523 serv_info.serv_nodename,
532 * \brief Local replacement for controversial C library function that generates
533 * names for temporary files. Included to shut up compiler warnings.
534 * \todo return a fd to the file instead of the name for security reasons
535 * \param name the created filename
536 * \param len the length of the filename
538 void CtdlMakeTempFileName(char *name, int len) {
541 while (i++, i < 100) {
542 snprintf(name, len, "/tmp/ctdl.%04x.%04x",
546 if (!access(name, F_OK)) {
555 * \brief case-insensitive substring search
557 * This uses the Boyer-Moore search algorithm and is therefore quite fast.
558 * The code is roughly based on the strstr() replacement from 'tin' written
561 * \param text String to be searched
562 * \param pattern String to search for
564 char *bmstrcasestr(char *text, char *pattern) {
566 register unsigned char *p, *t;
567 register int i, j, *delta;
573 textlen = strlen (text);
574 patlen = strlen (pattern);
576 /* algorithm fails if pattern is empty */
577 if ((p1 = patlen) == 0)
580 /* code below fails (whenever i is unsigned) if pattern too long */
586 for (i = 0; i <= 255; i++)
588 for (p = (unsigned char *) pattern, i = p1; --i > 0;)
589 delta[tolower(*p++)] = i;
592 * From now on, we want patlen - 1.
593 * In the loop below, p points to the end of the pattern,
594 * t points to the end of the text to be tested against the
595 * pattern, and i counts the amount of text remaining, not
596 * including the part to be tested.
599 p = (unsigned char *) pattern + p1;
600 t = (unsigned char *) text + p1;
601 i = textlen - patlen;
603 if (tolower(p[0]) == tolower(t[0])) {
604 if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
605 return ((char *)t - p1);
608 j = delta[tolower(t[0])];