* migrate more of the upload handling to strbuf
[citadel.git] / webcit / cookie_conversion.c
index a4865f7d9db02910ce298dbce2d413efeaa6623b..1a3465d3443d624234e3be88d1e8ca8a787dded5 100644 (file)
@@ -1,51 +1,81 @@
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <ctype.h>
-#include <string.h>
+/*
+ * $Id$
+ */
+
 #include "webcit.h"
-#include "child.h"
+
+typedef unsigned char byte;          /* Byte type used by cookie_to_stuff() */
 
 /*
- * 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, int nofr)
+void stuff_to_cookie(char *cookie, size_t clen, int session,
+               StrBuf *user, StrBuf *pass, StrBuf *room, const char *language)
 {
-       char buf[256];
+       char buf[SIZ];
        int i;
+       int len;
 
-       sprintf(buf, "%d|%s|%s|%s|%d", session, user, pass, room, nofr);
-       strcpy(cookie, "");
-
-       for (i = 0; i < strlen(buf); ++i)
-               sprintf(&cookie[strlen(cookie)], "%02X", buf[i]);
+       len = snprintf(buf, SIZ, "%d|%s|%s|%s|%s|", 
+               session, 
+               ChrPtr(user), 
+               ChrPtr(pass), 
+               ChrPtr(room),
+               language
+       );
 
+       strcpy(cookie, "");
+       for (i=0; (i < len) && (i * 2 < clen); ++i) {
+               snprintf(&cookie[i*2], clen - i * 2, "%02X", buf[i]);
+       }
 }
 
+/*
+ * Convert unpacked hex string to an integer
+ */
+int xtoi(const char *in, size_t len)
+{
+       int val = 0;
+       char c = 0;
+       while (!IsEmptyStr(in) && isxdigit((byte) *in) && (len-- > 0))
+       {
+               c = *in++;
+               val <<= 4;
+               if (!isdigit((unsigned char)c)) {
+                       c = tolower((unsigned char) c);
+                       if ((c < 'a') || (c > 'f'))
+                               return 0;
+                       val += c  - 'a' + 10 ;
+               }
+               else
+                       val += c - '0';
+       }
+       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, int *nofr)
+void cookie_to_stuff(StrBuf *cookie,
+                       int *session,
+                       StrBuf *user,
+                       StrBuf *pass,
+                       StrBuf *room,
+                       StrBuf *language)
 {
-       char buf[256];
-       int i;
-
-       for (i = 0; i < strlen(cookie); i = i + 2) {
-               sscanf(&cookie[i], "%02x", (unsigned int *) &buf[i / 2]);
-               buf[(i / 2) + 1] = 0;
+       if (session != NULL) {
+               *session = StrBufExtract_int(cookie, 0, '|');
+       }
+       if (user != NULL) {
+               StrBufExtract_token(user, cookie, 1, '|');
+       }
+       if (pass != NULL) {
+               StrBufExtract_token(pass, cookie, 2, '|');
+       }
+       if (room != NULL) {
+               StrBufExtract_token(room, cookie, 3, '|');
+       }
+       if (language != NULL) {
+               StrBufExtract_token(language, cookie, 4, '|');
        }
-
-       if (session != NULL)
-               *session = extract_int(buf, 0);
-       if (user != NULL)
-               extract(user, buf, 1);
-       if (pass != NULL)
-               extract(pass, buf, 2);
-       if (room != NULL)
-               extract(room, buf, 3);
-       if (nofr != NULL)
-               *nofr = extract_int(buf, 4);
 }