char **nonce,
char **default_domain)
{
- int clen, decodelen;
+ gsize clen;
NTLMString domain;
guchar *chall;
- int state;
- unsigned int save;
if (strncmp (challenge, "NTLM ", 5) != 0)
return FALSE;
- decodelen = strlen (challenge) - 5;
- chall = g_malloc (decodelen);
-
- state = save = 0;
- clen = soup_base64_decode_step ((const guchar *) challenge + 5,
- decodelen,
- chall,
- &state,
- &save);
-
+ chall = g_base64_decode (challenge + 5, &clen);
if (clen < NTLM_CHALLENGE_DOMAIN_STRING_OFFSET ||
clen < NTLM_CHALLENGE_NONCE_OFFSET + NTLM_CHALLENGE_NONCE_LENGTH) {
g_free (chall);
int hlen, dlen, ulen, offset;
guchar hash[21], lm_resp[24], nt_resp[24];
NTLMResponse resp;
- guchar *out, *p;
+ char *out, *p;
int state, save;
nt_hash (password, hash);
ntlm_set_string (&resp.nt_resp, &offset, sizeof (nt_resp));
out = g_malloc (((offset + 3) * 4) / 3 + 6);
- strncpy ((char *)out, "NTLM ", 5);
+ strncpy (out, "NTLM ", 5);
p = out + 5;
state = save = 0;
- p += soup_base64_encode_step ((guchar *) &resp,
- sizeof (resp),
- FALSE,
- p,
- &state,
- &save);
- p += soup_base64_encode_step ((const guchar *) domain,
- dlen,
- FALSE,
- p,
- &state,
- &save);
- p += soup_base64_encode_step ((const guchar *) user,
- ulen,
- FALSE,
- p,
- &state,
- &save);
- p += soup_base64_encode_step ((const guchar *) host,
- hlen,
- FALSE,
- p,
- &state,
- &save);
- p += soup_base64_encode_step (lm_resp,
- sizeof (lm_resp),
- FALSE,
- p,
- &state,
- &save);
-
- p += soup_base64_encode_close (nt_resp,
- sizeof (nt_resp),
- FALSE,
- p,
- &state,
- &save);
+ p += g_base64_encode_step ((const guchar *) &resp, sizeof (resp),
+ FALSE, p, &state, &save);
+ p += g_base64_encode_step ((const guchar *) domain, dlen,
+ FALSE, p, &state, &save);
+ p += g_base64_encode_step ((const guchar *) user, ulen,
+ FALSE, p, &state, &save);
+ p += g_base64_encode_step ((const guchar *) host, hlen,
+ FALSE, p, &state, &save);
+ p += g_base64_encode_step (lm_resp, sizeof (lm_resp),
+ FALSE, p, &state, &save);
+ p += g_base64_encode_step (nt_resp, sizeof (nt_resp),
+ FALSE, p, &state, &save);
+ p += g_base64_encode_close (FALSE, p, &state, &save);
*p = '\0';
- return (char *)out;
+ return out;
}
/* DES utils */
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
-static const int days_before[] = {
- 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
-};
-
static int
parse_month (const char *month)
{
time_t
soup_mktime_utc (struct tm *tm)
{
+#if HAVE_TIMEGM
+ return timegm (tm);
+#else
time_t tt;
+ static const int days_before[] = {
+ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
+ };
/* We check the month because (a) if we don't, the
* days_before[] part below may access random memory, and (b)
* soup_date_parse() doesn't check the return value of
- * parse_month(). The caller is responsible for ensure the
+ * parse_month(). The caller is responsible for ensuring the
* sanity of everything else.
*/
if (tm->tm_mon < 0 || tm->tm_mon > 11)
if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
tt--;
tt = ((((tt * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
+
return tt;
+#endif
}
/**
soup_date_generate (time_t when)
{
struct tm tm;
+
soup_gmtime (&when, &tm);
/* RFC1123 format, eg, "Sun, 06 Nov 1994 08:49:37 GMT" */
time_t
soup_date_iso8601_parse (const char *timestamp)
{
- struct tm tm;
- long val;
- time_t tt;
-
- val = strtoul (timestamp, (char **)×tamp, 10);
- if (*timestamp == '-') {
- // YYYY-MM-DD
- tm.tm_year = val - 1900;
- timestamp++;
- tm.tm_mon = strtoul (timestamp, (char **)×tamp, 10) - 1;
- if (*timestamp++ != '-')
- return -1;
- tm.tm_mday = strtoul (timestamp, (char **)×tamp, 10);
- } else {
- // YYYYMMDD
- tm.tm_mday = val % 100;
- tm.tm_mon = (val % 10000) / 100 - 1;
- tm.tm_year = val / 10000 - 1900;
- }
-
- if (*timestamp++ != 'T')
- return -1;
-
- val = strtoul (timestamp, (char **)×tamp, 10);
- if (*timestamp == ':') {
- // hh:mm:ss
- tm.tm_hour = val;
- timestamp++;
- tm.tm_min = strtoul (timestamp, (char **)×tamp, 10);
- if (*timestamp++ != ':')
- return -1;
- tm.tm_sec = strtoul (timestamp, (char **)×tamp, 10);
- } else {
- // hhmmss
- tm.tm_sec = val % 100;
- tm.tm_min = (val % 10000) / 100;
- tm.tm_hour = val / 10000;
- }
+ GTimeVal timeval;
- tt = soup_mktime_utc (&tm);
+ if (!g_time_val_from_iso8601 (timestamp, &timeval))
+ return (time_t) -1;
- if (*timestamp == '.')
- strtoul (timestamp + 1, (char **)×tamp, 10);
-
- if (*timestamp == '+' || *timestamp == '-') {
- int sign = (*timestamp == '+') ? -1 : 1;
- val = strtoul (timestamp + 1, (char **)×tamp, 10);
- if (*timestamp == ':')
- val = 60 * val + strtoul (timestamp + 1, NULL, 10);
- else
- val = 60 * (val / 100) + (val % 100);
- tt += sign * val;
- }
-
- return tt;
+ return (time_t) timeval.tv_sec;
}
return g_ascii_strcasecmp (string1, string2) == 0;
}
-/* Base64 utils (straight from camel-mime-utils.c) */
-#define d(x)
-
-static const char *base64_alphabet =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-/*
- * call this when finished encoding everything, to
- * flush off the last little bit
- */
int
soup_base64_encode_close (const guchar *in,
int inlen,
int *state,
int *save)
{
- int c1, c2;
- unsigned char *outptr = out;
-
- if (inlen > 0)
- outptr += soup_base64_encode_step (in,
- inlen,
- break_lines,
- outptr,
- state,
- save);
-
- c1 = ((unsigned char *) save) [1];
- c2 = ((unsigned char *) save) [2];
-
- d(printf("mode = %d\nc1 = %c\nc2 = %c\n",
- (int)((char *) save) [0],
- (int)((char *) save) [1],
- (int)((char *) save) [2]));
-
- switch (((char *) save) [0]) {
- case 2:
- outptr [2] = base64_alphabet[ ( (c2 &0x0f) << 2 ) ];
- g_assert (outptr [2] != 0);
- goto skip;
- case 1:
- outptr[2] = '=';
- skip:
- outptr [0] = base64_alphabet [ c1 >> 2 ];
- outptr [1] = base64_alphabet [ c2 >> 4 | ( (c1&0x3) << 4 )];
- outptr [3] = '=';
- outptr += 4;
- break;
+ if (inlen > 0) {
+ out += soup_base64_encode_step (in,
+ inlen,
+ break_lines,
+ out,
+ state,
+ save);
}
- if (break_lines)
- *outptr++ = '\n';
- *save = 0;
- *state = 0;
-
- return outptr-out;
+ return (int)g_base64_encode_close (break_lines, (char *) out,
+ state, save);
}
-/*
- * performs an 'encode step', only encodes blocks of 3 characters to the
- * output at a time, saves left-over state in state and save (initialise to
- * 0 on first invocation).
- */
int
soup_base64_encode_step (const guchar *in,
int len,
int *state,
int *save)
{
- register guchar *outptr;
- register const guchar *inptr;
-
- if (len <= 0)
- return 0;
-
- inptr = in;
- outptr = out;
-
- d (printf ("we have %d chars, and %d saved chars\n",
- len,
- ((char *) save) [0]));
-
- if (len + ((char *) save) [0] > 2) {
- const guchar *inend = in+len-2;
- register int c1, c2, c3;
- register int already;
-
- already = *state;
-
- switch (((char *) save) [0]) {
- case 1: c1 = ((unsigned char *) save) [1]; goto skip1;
- case 2: c1 = ((unsigned char *) save) [1];
- c2 = ((unsigned char *) save) [2]; goto skip2;
- }
-
- /*
- * yes, we jump into the loop, no i'm not going to change it,
- * it's beautiful!
- */
- while (inptr < inend) {
- c1 = *inptr++;
- skip1:
- c2 = *inptr++;
- skip2:
- c3 = *inptr++;
- *outptr++ = base64_alphabet [ c1 >> 2 ];
- *outptr++ = base64_alphabet [ c2 >> 4 |
- ((c1&0x3) << 4) ];
- *outptr++ = base64_alphabet [ ((c2 &0x0f) << 2) |
- (c3 >> 6) ];
- *outptr++ = base64_alphabet [ c3 & 0x3f ];
- /* this is a bit ugly ... */
- if (break_lines && (++already)>=19) {
- *outptr++='\n';
- already = 0;
- }
- }
-
- ((char *)save)[0] = 0;
- len = 2-(inptr-inend);
- *state = already;
- }
-
- d(printf("state = %d, len = %d\n",
- (int)((char *)save)[0],
- len));
-
- if (len>0) {
- register char *saveout;
-
- /* points to the slot for the next char to save */
- saveout = & (((char *)save)[1]) + ((char *)save)[0];
-
- /* len can only be 0 1 or 2 */
- switch(len) {
- case 2: *saveout++ = *inptr++;
- case 1: *saveout++ = *inptr++;
- }
- ((char *)save)[0]+=len;
- }
-
- d(printf("mode = %d\nc1 = %c\nc2 = %c\n",
- (int)((char *)save)[0],
- (int)((char *)save)[1],
- (int)((char *)save)[2]));
-
- return outptr-out;
+ return (int)g_base64_encode_step (in, len, break_lines,
+ (char *)out, state, save);
}
-/**
- * soup_base64_encode:
- * @text: the binary data to encode.
- * @len: the length of @text.
- *
- * Encode a sequence of binary data into it's Base-64 stringified
- * representation.
- *
- * Return value: The Base-64 encoded string representing @text.
- */
char *
soup_base64_encode (const char *text, int len)
{
- unsigned char *out;
- int state = 0, outlen, save = 0;
-
- out = g_malloc (len * 4 / 3 + 5);
- outlen = soup_base64_encode_close ((const guchar *)text,
- len,
- FALSE,
- out,
- &state,
- &save);
- out[outlen] = '\0';
- return (char *) out;
+ return g_base64_encode ((const guchar *)text, len);
}
-static unsigned char camel_mime_base64_rank[256] = {
- 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
- 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
- 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
- 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
- 255, 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,255,255,255,255,255,
- 255, 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,255,255,255,255,255,
- 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
- 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
- 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
- 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
- 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
- 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
- 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
- 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
-};
-
-/**
- * base64_decode_step: decode a chunk of base64 encoded data
- * @in: input stream
- * @len: max length of data to decode
- * @out: output stream
- * @state: holds the number of bits that are stored in @save
- * @save: leftover bits that have not yet been decoded
- *
- * Decodes a chunk of base64 encoded data
- **/
int
soup_base64_decode_step (const guchar *in,
int len,
int *state,
guint *save)
{
- register const guchar *inptr;
- register guchar *outptr;
- const guchar *inend;
- guchar c;
- register unsigned int v;
- int i;
-
- inend = in+len;
- outptr = out;
-
- /* convert 4 base64 bytes to 3 normal bytes */
- v=*save;
- i=*state;
- inptr = in;
- while (inptr < inend) {
- c = camel_mime_base64_rank [*inptr++];
- if (c != 0xff) {
- v = (v<<6) | c;
- i++;
- if (i==4) {
- *outptr++ = v>>16;
- *outptr++ = v>>8;
- *outptr++ = v;
- i=0;
- }
- }
- }
-
- *save = v;
- *state = i;
-
- /* quick scan back for '=' on the end somewhere */
- /* fortunately we can drop 1 output char for each trailing = (upto 2) */
- i=2;
- while (inptr > in && i) {
- inptr--;
- if (camel_mime_base64_rank [*inptr] != 0xff) {
- if (*inptr == '=')
- outptr--;
- i--;
- }
- }
-
- /* if i!= 0 then there is a truncation error! */
- return outptr - out;
+ return (int) g_base64_decode_step ((const char *)in, len,
+ out, state, save);
}
char *
soup_base64_decode (const char *text,
int *out_len)
{
- guchar *ret;
- int inlen, state = 0;
- unsigned int save = 0;
-
- inlen = strlen (text);
- ret = g_malloc0 (inlen);
-
- *out_len = soup_base64_decode_step ((const guchar *)text, inlen,
- ret, &state, &save);
+ char *ret;
+ gsize out_len_tmp;
- return (char *)ret;
+ ret = (char *) g_base64_decode (text, &out_len_tmp);
+ *out_len = out_len_tmp;
+ return ret;
}
typedef struct {