/*
* Dolda Connect - Modular multiuser Direct Connect-style client
- * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
+ * 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
#include <unistd.h>
#include <sys/time.h>
#include <netinet/in.h>
+#include <alloca.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
char *hexdecode(char *data, size_t *len)
{
- char *buf, this;
+ char *buf, this, bit;
size_t bufsize, bufdata;
buf = NULL;
bufsize = bufdata = 0;
- for(; *data; data++)
+ for(bit = 4, this = 0; *data; data++)
{
if((*data >= 'A') && (*data <= 'F'))
{
- this = (this & 0x0F) | ((*data - 'A' + 10) << 4);
+ this |= (this & 0x0F) | ((*data - 'A' + 10) << bit);
+ } else if((*data >= 'a') && (*data <= 'f')) {
+ this |= (this & 0x0F) | ((*data - 'a' + 10) << bit);
} else if((*data >= '0') && (*data <= '9')) {
- this = (this & 0x0F) | ((*data - '0') << 4);
+ this |= (this & 0x0F) | ((*data - '0') << bit);
+ } else if(*data == '\n') {
+ continue;
} 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');
+ if(bit == 4) {
+ bit = 0;
} else {
- if(buf != NULL)
- free(buf);
- return(NULL);
+ bit = 4;
+ addtobuf(buf, this);
+ this = 0;
}
- addtobuf(buf, this);
+ }
+ if(bit != 4) {
+ if(buf != NULL)
+ free(buf);
+ return(NULL);
}
addtobuf(buf, 0);
if(len != NULL)
}
}
+struct strpair *newstrpair(char *key, char *val, struct strpair **list)
+{
+ struct strpair *pair;
+
+ pair = smalloc(sizeof(*pair));
+ memset(pair, 0, sizeof(*pair));
+ if(key != NULL)
+ pair->key = sstrdup(key);
+ if(val != NULL)
+ pair->val = sstrdup(val);
+ if(list != NULL)
+ {
+ pair->next = *list;
+ *list = pair;
+ }
+ return(pair);
+}
+
+void freestrpair(struct strpair *pair, struct strpair **list)
+{
+ struct strpair *cur;
+
+ for(cur = *list; cur != NULL; list = &(cur->next), cur = cur->next)
+ {
+ if(cur == pair)
+ {
+ *list = cur->next;
+ break;
+ }
+ }
+ free(pair->key);
+ free(pair->val);
+ free(pair);
+}
+
+char *spfind(struct strpair *list, char *key)
+{
+ for(; list != NULL; list = list->next)
+ {
+ if(!strcmp(list->key, key))
+ return(list->val);
+ }
+ return(NULL);
+}
+
struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list)
{
struct wcspair *pair;
pair->key = swcsdup(key);
if(val != NULL)
pair->val = swcsdup(val);
- if(list == NULL)
+ if(list != NULL)
{
- pair->next = NULL;
- } else {
pair->next = *list;
*list = pair;
}