040eee612655f37fcc02d77be2fc1049ffcf041c
[citadel.git] / webcit / cookie_conversion.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup CookieConversion Grep Cookies
6  * Utility functions which convert the HTTP cookie format we use to and
7  * from user/password/room strings.
8  *
9  */
10 /*@{*/
11 #include "webcit.h"
12
13
14 #define TRUE  1    /**< for sure? */
15 #define FALSE 0    /**< nope. */
16
17 typedef unsigned char byte;           /**< Byte type */
18
19 /**
20  * \brief find cookie
21  * Pack all session info into one easy-to-digest cookie. Healthy and delicious!
22  * \param cookie cookie string to create???
23  * \param session the session we want to convert into a cookie
24  * \param user the user to be associated with the cookie
25  * \param pass his passphrase
26  * \param room the room he wants to enter
27  */
28 void stuff_to_cookie(char *cookie, int session,
29                 char *user, char *pass, char *room)
30 {
31         char buf[SIZ];
32         int i;
33
34         sprintf(buf, "%d|%s|%s|%s|", session, user, pass, room);
35         strcpy(cookie, "");
36         for (i=0; i<strlen(buf); ++i) {
37                 sprintf(&cookie[i*2], "%02X", buf[i]);
38         }
39 }
40
41 /**
42  * \brief some bytefoo ????
43  * \param in the string to chop
44  * \param len the length of the string
45  * \return the corrosponding integer value
46  */
47 int xtoi(char *in, size_t len)
48 {
49     int val = 0;
50     while (isxdigit((byte) *in) && (len-- > 0)) {
51         char c = *in++;
52         val <<= 4;
53         val += isdigit((unsigned char)c)
54             ? (c - '0')
55             : (tolower((unsigned char)c) - 'a' + 10);
56     }
57     return val;
58 }
59
60 /**
61  * \brief Extract all that fun stuff out of the cookie.
62  * \param cookie the cookie string
63  * \param session the corrosponding session to return
64  * \param user the user string
65  * \param user_len the user stringlength
66  * \param pass the passphrase
67  * \param pass_len length of the passphrase string 
68  * \param room the room he is in
69  * \param room_len the length of the room string
70  */
71 void cookie_to_stuff(char *cookie, int *session,
72                 char *user, size_t user_len,
73                 char *pass, size_t pass_len,
74                 char *room, size_t room_len)
75 {
76         char buf[SIZ];
77         int i, len;
78
79         strcpy(buf, "");
80         len = strlen(cookie) * 2 ;
81         for (i=0; i<len; ++i) {
82                 buf[i] = xtoi(&cookie[i*2], 2);
83                 buf[i+1] = 0;
84         }
85
86         if (session != NULL)
87                 *session = extract_int(buf, 0);
88         if (user != NULL)
89                 extract_token(user, buf, 1, '|', user_len);
90         if (pass != NULL)
91                 extract_token(pass, buf, 2, '|', pass_len);
92         if (room != NULL)
93                 extract_token(room, buf, 3, '|', room_len);
94 }
95 /*@}*/