See Decoding base 64 authorization strings XML * user_authenticate(XML * cgi_env, const char * realm) { XML * ret; char username[512]; char * password; _base64decode (username, xml_attrval (cgi_env, "HTTP_AUTHORIZATION") + 6); password = strchr (username, ':'); if (password) *password++ = '\0'; else password = ""; ret = user_get (username); if (ret) { if (!strcmp (password, xml_attrval (ret, "password"))) return (ret); xml_free (ret); } printf ("Status: 401 Authentication required\n"); printf ("WWW-Authenticate: BASIC realm=\"%s\"\n\n", realm); return NULL; } |
void _base64decode (char * buf, const char * encoded) { int bytes = 0; unsigned long chunk = 0; unsigned long remainder = 0; repeat: while (*encoded && bytes < 4) { if (*encoded == '=') chunk = chunk * 64; else if (*encoded > 96) chunk = chunk * 64 + (*encoded - 71); else if (*encoded > 64) chunk = chunk * 64 + (*encoded - 65); else if (*encoded > 47) chunk = chunk * 64 + (*encoded + 4); else if (*encoded == 43) chunk = chunk * 64 + 62; else if (*encoded == 47) chunk = chunk * 64 + 63; else chunk = chunk * 64; bytes ++; encoded ++; } bytes = 2; while (chunk > 0 && bytes > -1) { remainder = chunk % (1 << (bytes * 8)); *buf++ = (chunk - remainder) / (1 << (bytes * 8)); chunk = remainder; bytes--; } bytes = 0; if (*encoded) goto repeat; *buf = '\0'; } |
This code and documentation are released under the terms of the GNU license. They are additionally copyright (c) 2000, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license. |