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 if (strlen(source) == 0) {
122 for (i = 0; i < strlen(source); ++i) {
123 if ((start < 0) && (curr_parm == parmnum)) {
127 if ((end < 0) && (curr_parm == (parmnum + 1))) {
131 if (source[i] == separator) {
137 end = strlen(source);
139 strcpy(&source[start], &source[end]);
146 * \brief extract an int parm w/o supplying a buffer
147 * \param source the string to locate the int in
148 * \param parmnum the n'th token to grab the int from
149 * \return the integer
151 int extract_int(const char *source, int parmnum)
155 extract_token(buf, source, parmnum, '|', sizeof buf);
160 * \brief extract an long parm w/o supplying a buffer
161 * \param source string to examine
162 * \param parmnum n'th token to search long in
163 * \return the found long value
165 long extract_long(const char *source, int parmnum)
169 extract_token(buf, source, parmnum, '|', sizeof buf);
179 * \brief check for the presence of a character within a string (returns count)
180 * \param st the string to examine
181 * \param ch the char to search
182 * \return the position inside of st
184 int haschar(char *st,char ch)
188 for (a = 0; a < strlen(st); ++a)
196 * \brief Utility function to "readline" from memory
197 * \param start Location in memory from which we are reading.
198 * \param buf the buffer to place the string in.
199 * \param maxlen Size of string buffer
200 * \return Pointer to the source memory right after we stopped reading.
202 char *memreadline(char *start, char *buf, int maxlen)
206 int len = 0; /**< tally our own length to avoid strlen() delays */
209 memset(buf, 0, maxlen);
213 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
214 buf[strlen(buf) + 1] = 0;
215 buf[strlen(buf)] = ch;
218 if ((ch == 10) || (ch == 0)) {
227 * \brief searches for a paternn within asearch string
228 * \param search the string to search
229 * \param patn the pattern to find in string
230 * \returns position in string
232 int pattern2(char *search, char *patn)
235 for (a = 0; a < strlen(search); ++a) {
236 if (!strncasecmp(&search[a], patn, strlen(patn)))
244 * \brief Strip leading and trailing spaces from a string
245 * \param buf the string to modify
247 void striplt(char *buf)
249 if (strlen(buf) == 0) return;
250 while ((strlen(buf) > 0) && (isspace(buf[0])))
251 strcpy(buf, &buf[1]);
252 if (strlen(buf) == 0) return;
253 while (isspace(buf[strlen(buf) - 1]))
254 buf[strlen(buf) - 1] = 0;
259 * \brief Determine whether the specified message number is contained within the
262 * \param mset Message set string
263 * \param msgnum Message number we are looking for
265 * \return Nonzero if the specified message number is in the specified message set string.
267 int is_msg_in_mset(char *mset, long msgnum) {
270 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
274 * Now set it for all specified messages.
276 num_sets = num_tokens(mset, ',');
277 for (s=0; s<num_sets; ++s) {
278 extract_token(setstr, mset, s, ',', sizeof setstr);
280 extract_token(lostr, setstr, 0, ':', sizeof lostr);
281 if (num_tokens(setstr, ':') >= 2) {
282 extract_token(histr, setstr, 1, ':', sizeof histr);
283 if (!strcmp(histr, "*")) {
284 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
288 strcpy(histr, lostr);
293 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
302 * \brief Strip a boundarized substring out of a string
303 * (for example, remove
304 * parentheses and anything inside them).
306 * This improved version can strip out *multiple* boundarized substrings.
307 * \param str the string to process
308 * \param leftboundary the boundary character on the left side of the target string
309 * \param rightboundary the boundary character on the right side of the target string
311 void stripout(char *str, char leftboundary, char rightboundary)
321 for (a = 0; a < strlen(str); ++a) {
322 if (str[a] == leftboundary)
324 if (str[a] == rightboundary)
328 if ((lb > 0) && (rb > lb)) {
329 strcpy(&str[lb - 1], &str[rb + 1]);
332 } while ((lb > 0) && (rb > lb));
339 * \brief Replacement for sleep() that uses select() in order to avoid SIGALRM
340 * \param seconds how many seconds should we sleep?
342 void sleeeeeeeeeep(int seconds)
348 select(0, NULL, NULL, NULL, &tv);
354 * \brief encode a string into base64 to for example tunnel it through mail transport
355 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
356 * John Walker, copied over from the Citadel server.
357 * \param dest encrypted string
358 * \param source the string to encrypt
359 * \param sourcelen the length of the source data (may contain string terminators)
362 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
364 int i, hiteof = FALSE;
369 /** Fill dtable with character encodings. */
371 for (i = 0; i < 26; i++) {
373 dtable[26 + i] = 'a' + i;
375 for (i = 0; i < 10; i++) {
376 dtable[52 + i] = '0' + i;
382 byte igroup[3], ogroup[4];
385 igroup[0] = igroup[1] = igroup[2] = 0;
386 for (n = 0; n < 3; n++) {
387 if (spos >= sourcelen) {
392 igroup[n] = (byte) c;
395 ogroup[0] = dtable[igroup[0] >> 2];
397 dtable[((igroup[0] & 3) << 4) |
400 dtable[((igroup[1] & 0xF) << 2) |
402 ogroup[3] = dtable[igroup[2] & 0x3F];
405 * Replace characters in output stream with "=" pad
406 * characters if fewer than three characters were
407 * read from the end of the input stream.
416 for (i = 0; i < 4; i++) {
417 dest[dpos++] = ogroup[i];
439 * \brief Convert base64-encoded to binary.
440 * It will stop after reading 'length' bytes.
442 * \param dest The destination buffer
443 * \param source The base64 data to be decoded.
444 * \param length The number of bytes to decode.
445 * \return The actual length of the decoded data.
447 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
453 for (i = 0; i < 255; i++) {
456 for (i = 'A'; i <= 'Z'; i++) {
457 dtable[i] = 0 + (i - 'A');
459 for (i = 'a'; i <= 'z'; i++) {
460 dtable[i] = 26 + (i - 'a');
462 for (i = '0'; i <= '9'; i++) {
463 dtable[i] = 52 + (i - '0');
469 /**CONSTANTCONDITION*/ while (TRUE) {
470 byte a[4], b[4], o[3];
472 for (i = 0; i < 4; i++) {
473 if (spos >= length) {
484 if (dtable[c] & 0x80) {
485 /** Ignoring errors: discard invalid character */
490 b[i] = (byte) dtable[c];
492 o[0] = (b[0] << 2) | (b[1] >> 4);
493 o[1] = (b[1] << 4) | (b[2] >> 2);
494 o[2] = (b[2] << 6) | b[3];
495 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
512 * \brief Generate a new, globally unique UID parameter for a calendar etc. object
514 * \param buf String buffer into which our newly created UUID should be placed
516 void generate_uuid(char *buf) {
519 sprintf(buf, "%s-%lx-%x-%x",
520 serv_info.serv_nodename,
529 * \brief Local replacement for controversial C library function that generates
530 * names for temporary files. Included to shut up compiler warnings.
531 * \todo return a fd to the file instead of the name for security reasons
532 * \param name the created filename
533 * \param len the length of the filename
535 void CtdlMakeTempFileName(char *name, int len) {
538 while (i++, i < 100) {
539 snprintf(name, len, "/tmp/ctdl.%04x.%04x",
543 if (!access(name, F_OK)) {