* Cleaned up the rcs/cvs Id tags and leading comments at the top of some files
[citadel.git] / webcit / cookie_conversion.c
index 5f139fdf54f6b9d6c9ec6af6b5418462330579d2..644dbfc32a3b84259d9c71f7818a689b072346ac 100644 (file)
@@ -1,3 +1,10 @@
+/*
+ * $Id$
+ *
+ * Utility functions which convert the HTTP cookie format we use to and
+ * from user/password/room strings.
+ *
+ */
 
 #include <ctype.h>
 #include <stdlib.h>
 #include <signal.h>
 #include "webcit.h"
 
+#define TRUE  1
+#define FALSE 0
+
+typedef unsigned char byte;          /* Byte type */
+
 /*
- * Pack all session info into one easy-to-digest cookie.  Healthy and delicious!
+ * Pack all session info into one easy-to-digest cookie. Healthy and delicious!
  */
-void stuff_to_cookie(char *cookie, int session, char *user, char *pass, char *room)
+void stuff_to_cookie(char *cookie, int session,
+               char *user, char *pass, char *room)
 {
-       char buf[256];
+       char buf[SIZ];
        int i;
 
-       sprintf(buf, "%d|%s|%s|%s", session, user, pass, room);
+       sprintf(buf, "%d|%s|%s|%s|", session, user, pass, room);
        strcpy(cookie, "");
-
-       for (i = 0; i < strlen(buf); ++i)
-               sprintf(&cookie[strlen(cookie)], "%02X", buf[i]);
-
+       for (i=0; i<strlen(buf); ++i) {
+               sprintf(&cookie[i*2], "%02X", buf[i]);
+       }
 }
 
+int xtoi(char *in, size_t len)
+{
+    int val = 0;
+    while (isxdigit((byte) *in) && (len-- > 0)) {
+       char c = *in++;
+       val <<= 4;
+       val += isdigit((unsigned char)c)
+           ? (c - '0')
+           : (tolower((unsigned char)c) - 'a' + 10);
+    }
+    return val;
+}
 
 /*
  * Extract all that fun stuff out of the cookie.
  */
 void cookie_to_stuff(char *cookie, int *session, char *user, char *pass, char *room)
 {
-       char buf[256];
-       int i;
+       char buf[SIZ];
+       int i, len;
 
-       for (i = 0; i < strlen(cookie); i = i + 2) {
-               sscanf(&cookie[i], "%02x", (unsigned int *) &buf[i / 2]);
-               buf[(i / 2) + 1] = 0;
+       strcpy(buf, "");
+       len = strlen(cookie) * 2 ;
+       for (i=0; i<len; ++i) {
+               buf[i] = xtoi(&cookie[i*2], 2);
+               buf[i+1] = 0;
        }
 
        if (session != NULL)