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