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 position in buf
197 * \param buf the buffer to create string in???
198 * \param maxlen how big may the buffer get?
199 * \return new pointer to read string
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
260 * \param mset stringset???
261 * \param msgnum a citadel server indexnumber
262 * \return yes / no ???
264 int is_msg_in_mset(char *mset, long msgnum) {
267 char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
271 * Now set it for all specified messages.
273 num_sets = num_tokens(mset, ',');
274 for (s=0; s<num_sets; ++s) {
275 extract_token(setstr, mset, s, ',', sizeof setstr);
277 extract_token(lostr, setstr, 0, ':', sizeof lostr);
278 if (num_tokens(setstr, ':') >= 2) {
279 extract_token(histr, setstr, 1, ':', sizeof histr);
280 if (!strcmp(histr, "*")) {
281 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
285 strcpy(histr, lostr);
290 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
299 * \brief Strip a boundarized substring out of a string
300 * (for example, remove
301 * parentheses and anything inside them).
303 * This improved version can strip out *multiple* boundarized substrings.
304 * \param str the string to process
305 * \param leftboundary the boundary character on the left side of the target string
306 * \param rightboundary the boundary character on the right side of the target string
308 void stripout(char *str, char leftboundary, char rightboundary)
318 for (a = 0; a < strlen(str); ++a) {
319 if (str[a] == leftboundary)
321 if (str[a] == rightboundary)
325 if ((lb > 0) && (rb > lb)) {
326 strcpy(&str[lb - 1], &str[rb + 1]);
329 } while ((lb > 0) && (rb > lb));
336 * \brief Replacement for sleep() that uses select() in order to avoid SIGALRM
337 * \param seconds how many seconds should we sleep?
339 void sleeeeeeeeeep(int seconds)
345 select(0, NULL, NULL, NULL, &tv);
351 * \brief encode a string into base64 to for example tunnel it through mail transport
352 * CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by
353 * John Walker, copied over from the Citadel server.
354 * \param dest encrypted string
355 * \param source the string to encrypt
356 * \param sourcelen the length of the source data (may contain string terminators)
359 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen)
361 int i, hiteof = FALSE;
366 /** Fill dtable with character encodings. */
368 for (i = 0; i < 26; i++) {
370 dtable[26 + i] = 'a' + i;
372 for (i = 0; i < 10; i++) {
373 dtable[52 + i] = '0' + i;
379 byte igroup[3], ogroup[4];
382 igroup[0] = igroup[1] = igroup[2] = 0;
383 for (n = 0; n < 3; n++) {
384 if (spos >= sourcelen) {
389 igroup[n] = (byte) c;
392 ogroup[0] = dtable[igroup[0] >> 2];
394 dtable[((igroup[0] & 3) << 4) |
397 dtable[((igroup[1] & 0xF) << 2) |
399 ogroup[3] = dtable[igroup[2] & 0x3F];
402 * Replace characters in output stream with "=" pad
403 * characters if fewer than three characters were
404 * read from the end of the input stream.
413 for (i = 0; i < 4; i++) {
414 dest[dpos++] = ogroup[i];
436 * \brief Convert base64-encoded to binary.
437 * It will stop after reading 'length' bytes.
438 * \param dest the destination buffer
439 * \param source the encrypted string
440 * \param length the maximal length of dest???
441 * \return the length actual length of the decoded data.
443 int CtdlDecodeBase64(char *dest, const char *source, size_t length)
449 for (i = 0; i < 255; i++) {
452 for (i = 'A'; i <= 'Z'; i++) {
453 dtable[i] = 0 + (i - 'A');
455 for (i = 'a'; i <= 'z'; i++) {
456 dtable[i] = 26 + (i - 'a');
458 for (i = '0'; i <= '9'; i++) {
459 dtable[i] = 52 + (i - '0');
465 /**CONSTANTCONDITION*/ while (TRUE) {
466 byte a[4], b[4], o[3];
468 for (i = 0; i < 4; i++) {
469 if (spos >= length) {
480 if (dtable[c] & 0x80) {
481 /** Ignoring errors: discard invalid character */
486 b[i] = (byte) dtable[c];
488 o[0] = (b[0] << 2) | (b[1] >> 4);
489 o[1] = (b[1] << 4) | (b[2] >> 2);
490 o[2] = (b[2] << 6) | b[3];
491 i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
508 * \brief Generate a new, globally unique UID parameter for a calendar etc. object
509 * \param buf uid to create ????
511 void generate_uuid(char *buf) {
514 sprintf(buf, "%s-%lx-%x-%x",
515 serv_info.serv_nodename,
524 * \brief Local replacement for controversial C library function that generates
525 * names for temporary files. Included to shut up compiler warnings.
526 * \todo return a fd to the file instead of the name for security reasons
527 * \param name the created filename
528 * \param len the length of the filename
530 void CtdlMakeTempFileName(char *name, int len) {
533 while (i++, i < 100) {
534 snprintf(name, len, "/tmp/ctdl.%04x.%04x",
538 if (!access(name, F_OK)) {