+++ /dev/null
-#ifndef _UTILS_H
-#define _UTILS_H
-
-#include <stdarg.h>
-#include <stdlib.h>
-
-/* "Safe" functions */
-#define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;})
-#define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;})
-#define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs)))
-#define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
-
-char *vsprintf2(char *format, va_list al);
-char *sprintf2(char *format, ...);
-wchar_t *vswprintf2(wchar_t *format, va_list al);
-wchar_t *swprintf2(wchar_t *format, ...);
-wchar_t *icmbstowcs(char *mbs, char *charset);
-wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def);
-char *icwcstombs(wchar_t *wcs, char *charset);
-char *icswcstombs(wchar_t *wcs, char *charset, char *def);
-wchar_t *wcstolower(wchar_t *wcs);
-void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo);
-
-#define sizebuf(b, bs, rs, es, a) _sizebuf((void **)(b), (bs), (rs), (es), (a))
-#define sizebuf2(b, rs, a) _sizebuf((void **)(&(b)), &(b ## size), (rs), sizeof(*(b)), (a))
-#define addtobuf(b, c) \
-do { \
- _sizebuf((void **)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \
- (b)[(b ## data)++] = (c); \
-} while(0)
-#define bufcat(d, s, n) \
-do { \
- size_t __bufcat_size__; \
- __bufcat_size__ = (n); \
- _sizebuf((void **)(&(d)), &(d ## size), (d ## data) + __bufcat_size__, sizeof(*(d)), 1); \
- memcpy((d) + (d ## data), (s), sizeof(*(d)) * __bufcat_size__); \
- (d ## data) += __bufcat_size__; \
-} while (0)
-
-#endif
#endif
#include <doldaconnect/uilib.h>
#include <doldaconnect/uimisc.h>
-#include <doldaconnect/utils.h>
+#include <utils.h>
#ifdef HAVE_KRB5
#include <krb5.h>
int valid, fwd, fwded;
};
-static char *hexencode(char *data, size_t datalen)
-{
- char *buf, this;
- size_t bufsize, bufdata;
- int dig;
-
- buf = NULL;
- bufsize = bufdata = 0;
- for(; datalen > 0; datalen--, data++)
- {
- dig = (*data & 0xF0) >> 4;
- if(dig > 9)
- this = 'A' + dig - 10;
- else
- this = dig + '0';
- addtobuf(buf, this);
- dig = *data & 0x0F;
- if(dig > 9)
- this = 'A' + dig - 10;
- else
- this = dig + '0';
- addtobuf(buf, this);
- }
- addtobuf(buf, 0);
- return(buf);
-}
-
-static char *hexdecode(char *data, size_t *len)
-{
- char *buf, this;
- size_t bufsize, bufdata;
-
- buf = NULL;
- bufsize = bufdata = 0;
- for(; *data; data++)
- {
- if((*data >= 'A') && (*data <= 'F'))
- {
- this = (this & 0x0F) | ((*data - 'A' + 10) << 4);
- } else if((*data >= '0') && (*data <= '9')) {
- this = (this & 0x0F) | ((*data - '0') << 4);
- } else {
- if(buf != NULL)
- free(buf);
- return(NULL);
- }
- data++;
- if(!*data)
- {
- if(buf != NULL)
- free(buf);
- return(NULL);
- }
- if((*data >= 'A') && (*data <= 'F'))
- {
- this = (this & 0xF0) | (*data - 'A' + 10);
- } else if((*data >= '0') && (*data <= '9')) {
- this = (this & 0xF0) | (*data - '0');
- } else {
- if(buf != NULL)
- free(buf);
- return(NULL);
- }
- addtobuf(buf, this);
- }
- addtobuf(buf, 0);
- if(len != NULL)
- *len = bufdata - 1;
- return(buf);
-}
-
static void process_krb5(struct dc_response *resp, struct logindata *data)
{
int ret;
+++ /dev/null
-/*
- * Dolda Connect - Modular multiuser Direct Connect-style client
- * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-*/
-
-#include <stdlib.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <wchar.h>
-#include <iconv.h>
-#include <errno.h>
-#include <string.h>
-#include <wctype.h>
-#include <langinfo.h>
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-#include <doldaconnect/utils.h>
-
-extern int vswprintf (wchar_t *__restrict __s, size_t __n,
- __const wchar_t *__restrict __format,
- __gnuc_va_list __arg);
-
-char *vsprintf2(char *format, va_list al)
-{
- int ret;
- char *buf;
-
- ret = vsnprintf(NULL, 0, format, al);
- if((buf = malloc(ret + 1)) == NULL)
- {
- return(NULL);
- }
- vsnprintf(buf, ret + 1, format, al);
- return(buf);
-}
-
-char *sprintf2(char *format, ...)
-{
- va_list args;
- char *buf;
-
- va_start(args, format);
- buf = vsprintf2(format, args);
- va_end(args);
- return(buf);
-}
-
-wchar_t *vswprintf2(wchar_t *format, va_list al)
-{
- int ret;
- wchar_t *buf;
- size_t bufsize;
-
- buf = smalloc(sizeof(wchar_t) * (bufsize = 1024));
- while((ret = vswprintf(buf, bufsize, format, al)) < 0)
- buf = srealloc(buf, sizeof(wchar_t) * (bufsize *= 2));
- if(bufsize > ret + 1)
- buf = srealloc(buf, sizeof(wchar_t) * (ret + 1));
- return(buf);
-}
-
-wchar_t *swprintf2(wchar_t *format, ...)
-{
- va_list args;
- wchar_t *buf;
-
- va_start(args, format);
- buf = vswprintf2(format, args);
- va_end(args);
- return(buf);
-}
-
-wchar_t *icmbstowcs(char *mbs, char *charset)
-{
- int ret;
- char *buf;
- char *p, *p2;
- size_t len1, len2, bufsize, data;
- iconv_t cd;
-
- len1 = strlen(mbs) + 1;
- bufsize = len2 = len1 * sizeof(wchar_t);
- if((buf = malloc(bufsize)) == NULL)
- {
- return(NULL);
- }
- if(charset == NULL)
- charset = nl_langinfo(CODESET);
- if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1)
- {
- free(buf);
- return(NULL);
- }
- p = buf;
- while(len1 > 0)
- {
- ret = iconv(cd, &mbs, &len1, &p, &len2);
- if(ret < 0)
- {
- if(errno == E2BIG)
- {
- data = p - buf;
- len2 += 128;
- bufsize += 128;
- if((p2 = realloc(buf, bufsize)) == NULL)
- {
- free(buf);
- return(NULL);
- }
- buf = p2;
- p = buf + data;
- } else {
- free(buf);
- return(NULL);
- }
- }
- }
- if(len2 > 0)
- buf = realloc(buf, p - buf);
- iconv_close(cd);
- return((wchar_t *)buf);
-}
-
-wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def)
-{
- static wchar_t *buf = NULL;
-
- if(buf != NULL)
- free(buf);
- if((buf = icmbstowcs(mbs, charset)) == NULL)
- return(def);
- return(buf);
-}
-
-char *icwcstombs(wchar_t *wcs, char *charset)
-{
- int ret;
- char *buf;
- char *p, *p2;
- size_t len1, len2, bufsize, data;
- iconv_t cd;
-
- len1 = sizeof(wchar_t) * (wcslen(wcs) + 1);
- bufsize = len2 = len1;
- if((buf = malloc(bufsize)) == NULL)
- {
- return(NULL);
- }
- if(charset == NULL)
- charset = nl_langinfo(CODESET);
- if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1)
- {
- free(buf);
- return(NULL);
- }
- p = buf;
- while(len1 > 0)
- {
- ret = iconv(cd, (char **)&wcs, &len1, &p, &len2);
- if(ret < 0)
- {
- if(errno == E2BIG)
- {
- data = p - buf;
- len2 += 128;
- bufsize += 128;
- if((p2 = realloc(buf, bufsize)) == NULL)
- {
- free(buf);
- return(NULL);
- }
- buf = p2;
- p = buf + data;
- } else {
- free(buf);
- return(NULL);
- }
- }
- }
- if(len2 > 0)
- buf = realloc(buf, p - buf);
- iconv_close(cd);
- return(buf);
-}
-
-char *icswcstombs(wchar_t *wcs, char *charset, char *def)
-{
- static char *buf = NULL;
-
- if(buf != NULL)
- free(buf);
- if((buf = icwcstombs(wcs, charset)) == NULL)
- return(def);
- return(buf);
-}
-
-wchar_t *wcstolower(wchar_t *wcs)
-{
- wchar_t *p;
-
- for(p = wcs; *p != L'\0'; p++)
- *p = towlower(*p);
- return(wcs);
-}
-
-void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo)
-{
- if(*bufsize >= reqsize)
- return;
- switch(algo)
- {
- case 0:
- *buf = srealloc(*buf, elsize * ((*bufsize) = reqsize));
- break;
- case 1:
- if(*bufsize == 0)
- *bufsize = 1;
- while(*bufsize < reqsize)
- *bufsize *= 2;
- *buf = srealloc(*buf, elsize * (*bufsize));
- break;
- }
-}