| 1 |
/* |
| 2 |
* A little bit of this |
| 3 |
* A little bit of that |
| 4 |
* It started with a kiss |
| 5 |
* Now we're up to bat |
| 6 |
*/ |
| 7 |
|
| 8 |
#define FAIM_INTERNAL |
| 9 |
#include <aim.h> |
| 10 |
#include <ctype.h> |
| 11 |
|
| 12 |
#ifdef _WIN32 |
| 13 |
#include "win32dep.h" |
| 14 |
#endif |
| 15 |
|
| 16 |
faim_internal void faimdprintf(aim_session_t *sess, int dlevel, const char *format, ...) |
| 17 |
{ |
| 18 |
if (!sess) { |
| 19 |
fprintf(stderr, "faimdprintf: no session! boo! (%d, %s)\n", dlevel, format); |
| 20 |
return; |
| 21 |
} |
| 22 |
if ((dlevel <= sess->debug) && sess->debugcb) { |
| 23 |
va_list ap; |
| 24 |
va_start(ap, format); |
| 25 |
sess->debugcb(sess, dlevel, format, ap); |
| 26 |
va_end(ap); |
| 27 |
} |
| 28 |
|
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
faim_export int aimutil_putstr(char *dest, const char *src, int len) |
| 33 |
{ |
| 34 |
memcpy(dest, src, len); |
| 35 |
return len; |
| 36 |
} |
| 37 |
|
| 38 |
/* |
| 39 |
* Tokenizing functions. Used to portably replace strtok/sep. |
| 40 |
* -- DMP. |
| 41 |
* |
| 42 |
*/ |
| 43 |
faim_export int aimutil_tokslen(char *toSearch, int theindex, char dl) |
| 44 |
{ |
| 45 |
int curCount = 1; |
| 46 |
char *next; |
| 47 |
char *last; |
| 48 |
int toReturn; |
| 49 |
|
| 50 |
last = toSearch; |
| 51 |
next = strchr(toSearch, dl); |
| 52 |
|
| 53 |
while(curCount < theindex && next != NULL) { |
| 54 |
curCount++; |
| 55 |
last = next + 1; |
| 56 |
next = strchr(last, dl); |
| 57 |
} |
| 58 |
|
| 59 |
if ((curCount < theindex) || (next == NULL)) |
| 60 |
toReturn = strlen(toSearch) - (curCount - 1); |
| 61 |
else |
| 62 |
toReturn = next - toSearch - (curCount - 1); |
| 63 |
|
| 64 |
return toReturn; |
| 65 |
} |
| 66 |
|
| 67 |
faim_export int aimutil_itemcnt(char *toSearch, char dl) |
| 68 |
{ |
| 69 |
int curCount; |
| 70 |
char *next; |
| 71 |
|
| 72 |
curCount = 1; |
| 73 |
|
| 74 |
next = strchr(toSearch, dl); |
| 75 |
|
| 76 |
while(next != NULL) { |
| 77 |
curCount++; |
| 78 |
next = strchr(next + 1, dl); |
| 79 |
} |
| 80 |
|
| 81 |
return curCount; |
| 82 |
} |
| 83 |
|
| 84 |
faim_export char *aimutil_itemindex(char *toSearch, int theindex, char dl) |
| 85 |
{ |
| 86 |
int curCount; |
| 87 |
char *next; |
| 88 |
char *last; |
| 89 |
char *toReturn; |
| 90 |
|
| 91 |
curCount = 0; |
| 92 |
|
| 93 |
last = toSearch; |
| 94 |
next = strchr(toSearch, dl); |
| 95 |
|
| 96 |
while (curCount < theindex && next != NULL) { |
| 97 |
curCount++; |
| 98 |
last = next + 1; |
| 99 |
next = strchr(last, dl); |
| 100 |
} |
| 101 |
|
| 102 |
if (curCount < theindex) { |
| 103 |
toReturn = malloc(sizeof(char)); |
| 104 |
*toReturn = '\0'; |
| 105 |
} |
| 106 |
next = strchr(last, dl); |
| 107 |
|
| 108 |
if (curCount < theindex) { |
| 109 |
toReturn = malloc(sizeof(char)); |
| 110 |
*toReturn = '\0'; |
| 111 |
} else { |
| 112 |
if (next == NULL) { |
| 113 |
toReturn = malloc((strlen(last) + 1) * sizeof(char)); |
| 114 |
strcpy(toReturn, last); |
| 115 |
} else { |
| 116 |
toReturn = malloc((next - last + 1) * sizeof(char)); |
| 117 |
memcpy(toReturn, last, (next - last)); |
| 118 |
toReturn[next - last] = '\0'; |
| 119 |
} |
| 120 |
} |
| 121 |
return toReturn; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Calculate the checksum of a given icon. |
| 126 |
* |
| 127 |
*/ |
| 128 |
faim_export fu16_t aimutil_iconsum(const fu8_t *buf, int buflen) |
| 129 |
{ |
| 130 |
fu32_t sum; |
| 131 |
int i; |
| 132 |
|
| 133 |
for (i=0, sum=0; i+1<buflen; i+=2) |
| 134 |
sum += (buf[i+1] << 8) + buf[i]; |
| 135 |
if (i < buflen) |
| 136 |
sum += buf[i]; |
| 137 |
sum = ((sum & 0xffff0000) >> 16) + (sum & 0x0000ffff); |
| 138 |
|
| 139 |
return sum; |
| 140 |
} |
| 141 |
|
| 142 |
faim_export int aim_util_getlocalip(fu8_t *ip) |
| 143 |
{ |
| 144 |
struct hostent *hptr; |
| 145 |
char localhost[129]; |
| 146 |
|
| 147 |
/* XXX if available, use getaddrinfo() */ |
| 148 |
/* XXX allow client to specify which IP to use for multihomed boxes */ |
| 149 |
|
| 150 |
if (gethostname(localhost, 128) < 0) |
| 151 |
return -1; |
| 152 |
|
| 153 |
if (!(hptr = gethostbyname(localhost))) |
| 154 |
return -1; |
| 155 |
memcpy(ip, hptr->h_addr_list[0], 4); |
| 156 |
|
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
/* |
| 161 |
* int snlen(const char *) |
| 162 |
* |
| 163 |
* This takes a screen name and returns its length without |
| 164 |
* spaces. If there are no spaces in the SN, then the |
| 165 |
* return is equal to that of strlen(). |
| 166 |
* |
| 167 |
*/ |
| 168 |
faim_export int aim_snlen(const char *sn) |
| 169 |
{ |
| 170 |
int i = 0; |
| 171 |
|
| 172 |
if (!sn) |
| 173 |
return 0; |
| 174 |
|
| 175 |
while (*sn != '\0') { |
| 176 |
if (*sn != ' ') |
| 177 |
i++; |
| 178 |
sn++; |
| 179 |
} |
| 180 |
|
| 181 |
return i; |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
* int sncmp(const char *, const char *) |
| 186 |
* |
| 187 |
* This takes two screen names and compares them using the rules |
| 188 |
* on screen names for AIM/AOL. Mainly, this means case and space |
| 189 |
* insensitivity (all case differences and spacing differences are |
| 190 |
* ignored, with the exception that screen names can not start with |
| 191 |
* a space). |
| 192 |
* |
| 193 |
* Return: 0 if equal |
| 194 |
* non-0 if different |
| 195 |
* |
| 196 |
*/ |
| 197 |
faim_export int aim_sncmp(const char *sn1, const char *sn2) |
| 198 |
{ |
| 199 |
|
| 200 |
do { |
| 201 |
while (*sn2 == ' ') |
| 202 |
sn2++; |
| 203 |
while (*sn1 == ' ') |
| 204 |
sn1++; |
| 205 |
if (toupper(*sn1) != toupper(*sn2)) |
| 206 |
return 1; |
| 207 |
} while ((*sn1 != '\0') && sn1++ && sn2++); |
| 208 |
|
| 209 |
return 0; |
| 210 |
} |