/*
- *
+ * A basic toolset containing miscellaneous functions for string manipluation,
+ * encoding/decoding, and a bunch of other stuff.
*/
#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 */
-/* Month strings for date conversions */
-char *ascmonths[12] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+/* 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
};
+
char *safestrncpy(char *dest, const char *src, size_t n)
{
int i = 0;
return(-1);
}
-// cit_backtrace();
-// lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
+ //cit_backtrace();
+ //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
dest[0] = 0;
if (s == NULL) {
dest[len] = '\0';
if (current_token < parmnum) {
-// lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
+ //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
return(-1);
}
-// lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
+ //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
return(len);
}
//*/
int len = 0;
-// cit_backtrace();
- lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
+ //cit_backtrace();
+ //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
strcpy(dest, "");
// Locate desired parameter
s++;
}
if (!s) {
- lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
+ //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
return -1; // Parameter not found
}
*d = *s;
}
*d = 0;
- lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
+ //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
return 0;
}
*/
{
char buf[32];
- extract_token(buf, source, parmnum, '|', sizeof buf);
- return(atoi(buf));
+ if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
+ return(atoi(buf));
+ else
+ return 0;
}
/*
{
char buf[32];
- extract_token(buf, source, parmnum, '|', sizeof buf);
- return(atol(buf));
+ if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
+ return(atol(buf));
+ else
+ return 0;
}
{
char buf[32];
- extract_token(buf, source, parmnum, '|', sizeof buf);
- return strtoul(buf, NULL, 10);
-}
-
-
-
-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;
+ if (extract_token(buf, source, parmnum, '|', sizeof buf) > 0)
+ return strtoul(buf, NULL, 10);
+ else
+ return 0;
}
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];
struct tm tm;
int hour;
+ /* Month strings for date conversions ... this needs to be localized eventually */
+ char *fmt_date_months[12] = {
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+ };
+
strcpy(buf, "");
localtime_r(&thetime, &tm);
if (seconds) {
snprintf(buf, n, "%s %d %4d %d:%02d:%02d%s",
- ascmonths[tm.tm_mon],
+ fmt_date_months[tm.tm_mon],
tm.tm_mday,
tm.tm_year + 1900,
hour,
);
} else {
snprintf(buf, n, "%s %d %4d %d:%02d%s",
- ascmonths[tm.tm_mon],
+ fmt_date_months[tm.tm_mon],
tm.tm_mday,
tm.tm_year + 1900,
hour,
return ret;
}
-/*
- * Escape a string for feeding out as a URL.
- * Output buffer must be big enough to handle escape expansion!
+/**
+ * \brief Escape a string for feeding out as a URL.
+ * \param outbuf the output buffer
+ * \param oblen the size of outbuf to sanitize
+ * \param strbuf the input buffer
*/
-void urlesc(char *outbuf, char *strbuf)
+void urlesc(char *outbuf, size_t oblen, char *strbuf)
{
- int a, b, c;
- char *ec = " #&;`'|*?-~<>^()[]{}$\\";
+ int a, b, c, len, eclen, olen;
+ char *ec = " +#&;`'|*?-~<>^()[]{}/$\"\\";
strcpy(outbuf, "");
-
- for (a = 0; a < (int)strlen(strbuf); ++a) {
+ len = strlen(strbuf);
+ eclen = strlen(ec);
+ olen = 0;
+ for (a = 0; a < len; ++a) {
c = 0;
- for (b = 0; b < strlen(ec); ++b) {
+ for (b = 0; b < eclen; ++b) {
if (strbuf[a] == ec[b])
c = 1;
}
- b = strlen(outbuf);
- if (c == 1)
- sprintf(&outbuf[b], "%%%02x", strbuf[a]);
- else
- sprintf(&outbuf[b], "%c", strbuf[a]);
+ if (c == 1) {
+ snprintf(&outbuf[olen], oblen - olen, "%%%02x", strbuf[a]);
+ olen += 3;
+ }
+ else
+ outbuf[olen ++] = strbuf[a];
}
+ outbuf[olen] = '\0';
}
int i = 0;
while (i++, i < 100) {
- snprintf(name, len, "/tmp/ctdl.%4lx.%04x",
+ snprintf(name, len, "/tmp/ctdl.%04lx.%04x",
(long)getpid(),
rand()
);
}
-/**
+/*
* \brief searches for a paternn within asearch string
* \param search the string to search
* \param patn the pattern to find in string