#define FALSE 0
typedef unsigned char byte; /* Byte type */
-static byte dtable[256] = "\0"; /* base64 decode table */
-static byte etable[256] = "\0"; /* base64 encode table */
-char *safestrncpy(char *dest, const char *src, size_t n)
+/* Base64 encoding table */
+const byte etable[256] = {
+ 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
+ 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103,
+ 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
+ 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43,
+ 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+/* Base64 decoding table */
+const byte dtable[256] = {
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
+ 128, 128, 128, 0, 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
+ 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 128, 128, 128,
+ 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
+ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128,
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 128, 0
+};
+
+/*
+ * copy a string into a buffer of a known size. abort if we exceed the limits
+ *
+ * dest the targetbuffer
+ * src the source string
+ * n the size od dest
+ *
+ * returns the number of characters copied if dest is big enough, -n if not.
+ */
+int safestrncpy(char *dest, const char *src, size_t n)
{
int i = 0;
do {
dest[i] = src[i];
- if (dest[i] == 0) return(dest);
+ if (dest[i] == 0) return i;
++i;
} while (i<n);
dest[n - 1] = 0;
- return dest;
+ return -i;
}
}
-
-void CtdlInitBase64Table(void)
-{
- int i;
- /* Fill dtable with character encodings. */
-
- /* Encoder Table */
- for (i = 0; i < 26; i++) {
- etable[i] = 'A' + i;
- etable[26 + i] = 'a' + i;
- }
- for (i = 0; i < 10; i++) {
- etable[52 + i] = '0' + i;
- }
- etable[62] = '+';
- etable[63] = '/';
-
- /* Decoder Table */
- for (i = 0; i < 255; i++) {
- dtable[i] = 0x80;
- }
- for (i = 'A'; i <= 'Z'; i++) {
- dtable[i] = 0 + (i - 'A');
- }
- for (i = 'a'; i <= 'z'; i++) {
- dtable[i] = 26 + (i - 'a');
- }
- for (i = '0'; i <= '9'; i++) {
- dtable[i] = 52 + (i - '0');
- }
- dtable['+'] = 62;
- dtable['/'] = 63;
- dtable['='] = 0;
-}
-
-
/*
* CtdlDecodeBase64() and CtdlEncodeBase64() are adaptations of code by John Walker.
*/
int dpos = 0;
int thisline = 0;
- /** Fill dtable with character encodings. */
-
- for (i = 0; i < 26; i++) {
- dtable[i] = 'A' + i;
- dtable[26 + i] = 'a' + i;
- }
- for (i = 0; i < 10; i++) {
- dtable[52 + i] = '0' + i;
- }
- dtable[62] = '+';
- dtable[63] = '/';
-
while (!hiteof) {
byte igroup[3], ogroup[4];
int c, n;
igroup[n] = (byte) c;
}
if (n > 0) {
- ogroup[0] = dtable[igroup[0] >> 2];
+ ogroup[0] = etable[igroup[0] >> 2];
ogroup[1] =
- dtable[((igroup[0] & 3) << 4) |
+ etable[((igroup[0] & 3) << 4) |
(igroup[1] >> 4)];
ogroup[2] =
- dtable[((igroup[1] & 0xF) << 2) |
+ etable[((igroup[1] & 0xF) << 2) |
(igroup[2] >> 6)];
- ogroup[3] = dtable[igroup[2] & 0x3F];
+ ogroup[3] = etable[igroup[2] & 0x3F];
- /**
+ /*
* Replace characters in output stream with "=" pad
* characters if fewer than three characters were
* read from the end of the input stream.
int dpos = 0;
int spos = 0;
-
- /*CONSTANTCONDITION*/
while (TRUE) {
byte a[4], b[4], o[3];
return strdup(line);
}
- result = (char*) malloc(strlen(UTF8_HEADER) + 4 + length * 2);
+ result = (char*) malloc(sizeof(UTF8_HEADER) + 4 + length * 2);
strncpy (result, UTF8_HEADER, strlen (UTF8_HEADER));
CtdlEncodeBase64(result + strlen(UTF8_HEADER), line, length, 0);
end = strlen (result);
return result;
}
-
/*
- * Strip leading and trailing spaces from a string
+ * removes double slashes from pathnames
+ * allows / disallows trailing slashes
*/
-void striplt(char *buf)
+void StripSlashes(char *Dir, int TrailingSlash)
{
- size_t len;
- int a;
+ char *a, *b;
+
+ a = b = Dir;
+
+ while (!IsEmptyStr(a)) {
+ if (*a == '/') {
+ while (*a == '/')
+ a++;
+ *b = '/';
+ b++;
+ }
+ else {
+ *b = *a;
+ b++; a++;
+ }
+ }
+ if ((TrailingSlash) && (*(b - 1) != '/')){
+ *b = '/';
+ b++;
+ }
+ *b = '\0';
- if (buf==NULL) return;
- if (IsEmptyStr(buf)) return;
- len = strlen(buf);
- while ((!IsEmptyStr(buf)) && (isspace(buf[len - 1])))
- buf[--len] = 0;
- if (IsEmptyStr(buf)) return;
- a = 0;
- while ((!IsEmptyStr(buf)) && (isspace(buf[a])))
- a++;
- if (a > 0)
- memmove(buf, &buf[a], len - a + 1);
}
+/*
+ * Strip leading and trailing spaces from a string
+ */
+size_t striplt(char *buf) {
+ char *first_nonspace = NULL;
+ char *last_nonspace = NULL;
+ char *ptr;
+ size_t new_len = 0;
+
+ if ((buf == NULL) || (*buf == '\0')) {
+ return 0;
+ }
+
+ for (ptr=buf; *ptr!=0; ++ptr) {
+ if (!isspace(*ptr)) {
+ if (!first_nonspace) {
+ first_nonspace = ptr;
+ }
+ last_nonspace = ptr;
+ }
+ }
+ if ((!first_nonspace) || (!last_nonspace)) {
+ buf[0] = 0;
+ return 0;
+ }
+ new_len = last_nonspace - first_nonspace + 1;
+ memmove(buf, first_nonspace, new_len);
+ buf[new_len] = 0;
+ return new_len;
+}
/**
* Determine whether the specified message number is contained within the
* specified sequence set.
*/
-int is_msg_in_sequence_set(char *mset, long msgnum) {
+int is_msg_in_sequence_set(const char *mset, long msgnum) {
int num_sets;
int s;
char setstr[128], lostr[128], histr[128];
* Strip a boundarized substring out of a string (for example, remove
* parentheses and anything inside them).
*/
-void stripout(char *str, char leftboundary, char rightboundary) {
+int stripout(char *str, char leftboundary, char rightboundary) {
int a;
int lb = (-1);
int rb = (-1);
if ( (lb > 0) && (rb > lb) ) {
strcpy(&str[lb - 1], &str[rb + 1]);
+ return 1;
}
else if ( (lb == 0) && (rb > lb) ) {
strcpy(str, &str[rb + 1]);
+ return 1;
}
-
+ return 0;
}
* The code is roughly based on the strstr() replacement from 'tin' written
* by Urs Jannsen.
*/
-char *bmstrcasestr(char *text, char *pattern) {
+inline char *_bmstrcasestr_len(char *text, size_t textlen, char *pattern, size_t patlen) {
register unsigned char *p, *t;
register int i, j, *delta;
register size_t p1;
int deltaspace[256];
- size_t textlen;
- size_t patlen;
- textlen = strlen (text);
- patlen = strlen (pattern);
+ if (!text) return(NULL);
+ if (!pattern) return(NULL);
/* algorithm fails if pattern is empty */
if ((p1 = patlen) == 0)
return (NULL);
}
+/*
+ * bmstrcasestr() -- case-insensitive substring search
+ *
+ * This uses the Boyer-Moore search algorithm and is therefore quite fast.
+ * The code is roughly based on the strstr() replacement from 'tin' written
+ * by Urs Jannsen.
+ */
+char *bmstrcasestr(char *text, char *pattern) {
+ size_t textlen;
+ size_t patlen;
+ if (!text) return(NULL);
+ if (!pattern) return(NULL);
+
+ textlen = strlen (text);
+ patlen = strlen (pattern);
+
+ return _bmstrcasestr_len(text, textlen, pattern, patlen);
+}
+
+char *bmstrcasestr_len(char *text, size_t textlen, char *pattern, size_t patlen) {
+ return _bmstrcasestr_len(text, textlen, pattern, patlen);
+}
/*
* Local replacement for controversial C library function that generates
int i = 0;
while (i++, i < 100) {
- snprintf(name, len, "/tmp/ctdl.%4lx.%04x",
+ snprintf(name, len, "/tmp/ctdl.%04lx.%04x",
(long)getpid(),
rand()
);
* Determine whether the specified message number is contained within the specified set.
* Returns nonzero if the specified message number is in the specified message set string.
*/
-int is_msg_in_mset(char *mset, long msgnum) {
+int is_msg_in_mset(const char *mset, long msgnum) {
int num_sets;
int s;
char setstr[SIZ], lostr[SIZ], histr[SIZ]; /* was 1024 */
/*
- * \brief searches for a paternn within asearch string
- * \param search the string to search
- * \param patn the pattern to find in string
- * \returns position in string
+ * searches for a pattern within a search string
+ * returns position in string
*/
int pattern2(char *search, char *patn)
{
}
-/**
- * \brief Strip leading and trailing spaces from a string; with premeasured and adjusted length.
- * \param buf the string to modify
- * \param len length of the string.
+/*
+ * Strip leading and trailing spaces from a string; with premeasured and adjusted length.
+ * buf - the string to modify
+ * len - length of the string.
*/
void stripltlen(char *buf, int *len)
{
}
}
+
+/*
+ * Convert all whitespace characters in a supplied string to underscores
+ */
+void convert_spaces_to_underscores(char *str)
+{
+ int len;
+ int i;
+
+ if (!str) return;
+
+ len = strlen(str);
+ for (i=0; i<len; ++i) {
+ if (isspace(str[i])) {
+ str[i] = '_';
+ }
+ }
+}
+
+