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
183 int haschar(char *st,char ch)
187 for (a = 0; a < strlen(st); ++a)
195 * \brief Utility function to "readline" from memory
196 * \param start Location in memory from which we are reading.
197 * \param buf the buffer to place the string in.
198 * \param maxlen Size of string buffer
199 * \return Pointer to the source memory right after we stopped reading.
201 char *memreadline(char *start, char *buf, int maxlen)
205 int len = 0; /**< tally our own length to avoid strlen() delays */
208 memset(buf, 0, maxlen);
212 if ((len < (maxlen - 1)) && (ch != 13) && (ch != 10)) {
213 buf[strlen(buf) + 1] = 0;
214 buf[strlen(buf)] = ch;
217 if ((ch == 10) || (ch == 0)) {
226 * \brief searches for a paternn within asearch string
227 * \param search the string to search
228 * \param patn the pattern to find in string
229 * \returns position in string
231 int pattern2(char *search, char *patn)
234 for (a = 0; a < strlen(search); ++a) {
235 if (!strncasecmp(&search[a], patn, strlen(patn)))
243 * \brief Strip leading and trailing spaces from a string
244 * \param buf the string to modify
246 void striplt(char *buf)
248 if (strlen(buf) == 0) return;
249 while ((strlen(buf) > 0) && (isspace(buf[0])))
250 strcpy(buf, &buf[1]);
251 if (strlen(buf) == 0) return;
252 while (isspace(buf[strlen(buf) - 1]))
253 buf[strlen(buf) - 1] = 0;
258 * \brief Determine whether the specified message number is contained within the
261 * \param mset Message set string
262 * \param msgnum Message number we are looking for
264 * \return Nonzero if the specified message number is in the specified message set string.
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.
441 * \param dest The destination buffer
442 * \param source The base64 data to be decoded.
443 * \param length The number of bytes to decode.
444 * \return The actual length of the decoded data.
446 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
452 for (i = 0; i < 255; i++) {
455 for (i = 'A'; i <= 'Z'; i++) {
456 dtable[i] = 0 + (i - 'A');
458 for (i = 'a'; i <= 'z'; i++) {
459 dtable[i] = 26 + (i - 'a');
461 for (i = '0'; i <= '9'; i++) {
462 dtable[i] = 52 + (i - '0');
468 /**CONSTANTCONDITION*/ while (TRUE) {
469 byte a[4], b[4], o[3];
471 for (i = 0; i < 4; i++) {
472 if (spos >= length) {
483 if (dtable[c] & 0x80) {
484 /** Ignoring errors: discard invalid character */
489 b[i] = (byte) dtable[c];
491 o[0] = (b[0] << 2) | (b[1] >> 4);
492 o[1] = (b[1] << 4) | (b[2] >> 2);
493 o[2] = (b[2] << 6) | b[3];
494 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
511 * \brief Generate a new, globally unique UID parameter for a calendar etc. object
513 * \param buf String buffer into which our newly created UUID should be placed
515 void generate_uuid(char *buf) {
518 sprintf(buf, "%s-%lx-%x-%x",
519 serv_info.serv_nodename,
528 * \brief Local replacement for controversial C library function that generates
529 * names for temporary files. Included to shut up compiler warnings.
530 * \todo return a fd to the file instead of the name for security reasons
531 * \param name the created filename
532 * \param len the length of the filename
534 void CtdlMakeTempFileName(char *name, int len) {
537 while (i++, i < 100) {
538 snprintf(name, len, "/tmp/ctdl.%04x.%04x",
542 if (!access(name, F_OK)) {