2006-12-21 01:30:28 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the Sofia-SIP package
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Nokia Corporation.
|
|
|
|
*
|
|
|
|
* Contact: Pekka Pessi <pekka.pessi@nokia.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-12-16 13:05:22 -05:00
|
|
|
/**@internal @file token64.c
|
2006-12-21 01:30:28 -05:00
|
|
|
*
|
|
|
|
* Token encoding.
|
|
|
|
*
|
|
|
|
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
|
|
|
|
*
|
2008-12-16 13:05:22 -05:00
|
|
|
* @date Created: Wed Apr 3 10:45:47 2002 ppessi
|
2006-12-21 01:30:28 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2007-11-19 13:09:15 -05:00
|
|
|
#include <stdio.h>
|
2006-12-21 01:30:28 -05:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "sofia-sip/token64.h"
|
|
|
|
|
2008-12-16 13:05:22 -05:00
|
|
|
static const char code[65] =
|
2006-12-21 01:30:28 -05:00
|
|
|
"0123456789-abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
|
2008-12-16 13:05:22 -05:00
|
|
|
/** Encode data as a SIP/HTTP token.
|
2006-12-21 01:30:28 -05:00
|
|
|
*
|
|
|
|
* @note
|
|
|
|
* A token is case-independent, so this is really not a good idea.
|
|
|
|
* Use msg_random_token() instead.
|
|
|
|
*/
|
|
|
|
isize_t token64_e(char b[], isize_t bsiz, void const *data, isize_t dlen)
|
|
|
|
{
|
|
|
|
size_t i, n, slack;
|
|
|
|
unsigned char const *h = data;
|
|
|
|
char *s = b, *end = b + bsiz;
|
|
|
|
long w;
|
|
|
|
|
|
|
|
if (dlen <= 0) {
|
|
|
|
if (bsiz && b) *b = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = (8 * dlen + 5) / 6;
|
|
|
|
if (bsiz == 0 || b == NULL)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
if (b + n >= end)
|
|
|
|
dlen = 6 * bsiz / 8;
|
|
|
|
else
|
|
|
|
end = b + n + 1;
|
|
|
|
|
|
|
|
slack = dlen % 3;
|
|
|
|
dlen -= slack;
|
|
|
|
|
|
|
|
for (i = 0; i < dlen; i += 3, s += 4) {
|
|
|
|
unsigned char h0 = h[i], h1 = h[i + 1], h2 = h[i + 2];
|
2008-12-16 13:05:22 -05:00
|
|
|
|
2006-12-21 01:30:28 -05:00
|
|
|
s[0] = code[h0 >> 2];
|
|
|
|
s[1] = code[((h0 << 4)|(h1 >> 4)) & 63];
|
|
|
|
s[2] = code[((h1 << 4)|(h2 >> 6)) & 63];
|
|
|
|
s[3] = code[(h2) & 63];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slack) {
|
|
|
|
if (slack == 2)
|
|
|
|
w = (h[i] << 16) | (h[i+1] << 8);
|
|
|
|
else
|
|
|
|
w = (h[i] << 16);
|
|
|
|
|
|
|
|
if (s < end) *s++ = code[(w >> 18) & 63];
|
|
|
|
if (s < end) *s++ = code[(w >> 12) & 63];
|
|
|
|
if (s < end && slack == 2) *s++ = code[(w >> 6) & 63];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s < end)
|
|
|
|
*s++ = '\0';
|
|
|
|
else
|
|
|
|
end[-1] = '\0';
|
|
|
|
|
|
|
|
assert(end == s);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|