* migrate more of the upload handling to strbuf
[citadel.git] / webcit / cookie_conversion.c
index 644dbfc32a3b84259d9c71f7818a689b072346ac..1a3465d3443d624234e3be88d1e8ca8a787dded5 100644 (file)
@@ -1,87 +1,81 @@
 /*
  * $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 <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#include <sys/time.h>
-#include <limits.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <string.h>
-#include <pwd.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <pthread.h>
-#include <signal.h>
 #include "webcit.h"
 
-#define TRUE  1
-#define FALSE 0
-
-typedef unsigned char byte;          /* Byte type */
+typedef unsigned char byte;          /* Byte type used by cookie_to_stuff() */
 
 /*
  * 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, size_t clen, int session,
+               StrBuf *user, StrBuf *pass, StrBuf *room, const char *language)
 {
        char buf[SIZ];
        int i;
+       int len;
+
+       len = snprintf(buf, SIZ, "%d|%s|%s|%s|%s|", 
+               session, 
+               ChrPtr(user), 
+               ChrPtr(pass), 
+               ChrPtr(room),
+               language
+       );
 
-       sprintf(buf, "%d|%s|%s|%s|", session, user, pass, room);
        strcpy(cookie, "");
-       for (i=0; i<strlen(buf); ++i) {
-               sprintf(&cookie[i*2], "%02X", buf[i]);
+       for (i=0; (i < len) && (i * 2 < clen); ++i) {
+               snprintf(&cookie[i*2], clen - i * 2, "%02X", buf[i]);
        }
 }
 
-int xtoi(char *in, size_t len)
+/*
+ * Convert unpacked hex string to an integer
+ */
+int xtoi(const 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;
+       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)
+void cookie_to_stuff(StrBuf *cookie,
+                       int *session,
+                       StrBuf *user,
+                       StrBuf *pass,
+                       StrBuf *room,
+                       StrBuf *language)
 {
-       char buf[SIZ];
-       int i, len;
-
-       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) {
+               *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);
 }