1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/* Misc utility functions for the Gaim-Encryption plugin */
/* Copyright (C) 2001-2003 William Tompkins */
/* This plugin is free software, distributed under the GNU General Public */
/* License. */
/* Please see the file "COPYING" distributed with the Gaim source code */
/* for more details */
/* */
/* */
/* This software 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. */
/* To compile and use: */
/* See INSTALL file. */
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <debug.h>
#include <gaim.h>
#ifdef _WIN32
#include <win32dep.h>
#endif
#include "nls.h"
#include "cryptutil.h"
#include "rsa_nss.h"
#include <base64.h>
void GE_clear_string(unsigned char* s) {
while(*s != 0) {
*(s++) = 0;
}
}
void GE_escape_name(GString* name) {
int pos = 0;
// Note: name->len and name->str can change as we modify name...
while (pos < name->len) {
switch (name->str[pos]) {
case ' ':
// space -> "\s"
g_string_erase(name, pos, 1);
g_string_insert(name, pos, "\\s");
pos += 2;
break;
case ',':
// comma -> "\c"
g_string_erase(name, pos, 1);
g_string_insert(name, pos, "\\c");
pos += 2;
break;
case '\\':
// backslash -> "\\"
g_string_erase(name, pos, 1);
g_string_insert(name, pos, "\\");
pos += 2;
break;
default:
++pos;
break;
}
}
}
void GE_unescape_name(char* origname) {
GString *name = g_string_new(origname);
int pos = 0;
while (pos < name->len) {
if (name->str[pos] == '\\') {
g_string_erase(name, pos, 1);
switch (name->str[pos]) {
case 's':
// \s -> space
name->str[pos] = ' ';
++pos;
break;
case 'c':
// \c -> comma
name->str[pos] = ',';
++pos;
break;
default:
// leave the char that followed the backslash
++pos;
break;
}
} else {
++pos;
}
}
// string can only be shorter, so this copy is ok
strcpy(origname, name->str);
g_string_free(name, TRUE);
}
/* Convert 'num' bytes into an ascii string. */
void GE_bytes_to_str(unsigned char *str, unsigned char *bytes, int num) {
unsigned char* tmp = BTOA_DataToAscii(bytes, num);
GString* tmp2 = g_string_new(tmp);
GE_strip_returns(tmp2);
strcpy(str, tmp2->str);
PORT_Free(tmp);
g_string_free(tmp2, TRUE);
}
/* Convert ascii string back into bytes. Returns number of bytes */
unsigned int GE_str_to_bytes(unsigned char *bytes, unsigned char *cstr) {
unsigned int tmplen;
unsigned char* tmp = ATOB_AsciiToData(cstr, &tmplen);
if (tmp == NULL) {
gaim_debug(GAIM_DEBUG_ERROR, "gaim-encryption", _("Invalid Base64 data, length %d\n"),
strlen(cstr));
return 0;
}
memcpy(bytes, tmp, tmplen);
PORT_Free(tmp);
return tmplen;
}
GString* GE_strip_returns(GString* s) {
gchar *strippedStr, **strArray;
int i;
strArray = g_strsplit(s->str, "\n", 100);
for (i = 0; strArray[i] != 0; ++i) {
g_strstrip(strArray[i]);
}
strippedStr = g_strjoinv(0, strArray);
g_string_assign(s, strippedStr);
g_strfreev(strArray);
g_free(strippedStr);
return s;
}
gboolean GE_msg_starts_with_link(const char* c) {
/* This seems easy. But, we need to filter out intermediate HTML
(like <font>) as well. And, to be really compliant, we should
parse things like "< a href=" (even if noone else seems to).
*/
while (*c != 0) {
/* If we don't start with a tag, we can't start with a link */
if (*(c++) != '<') return FALSE;
while (isspace(*c)) ++c; /* skip leading whitespace in tag */
if (*c == 'A' || *c == 'a') return TRUE;
c = strchr(c, '>'); /* skip to end of tag */
if (*c) ++c; /* watch out for unclosed tags! */
}
return FALSE;
}
/* Removed from Gaim, so added in here :) */
GSList *GE_message_split(char *message, int limit) {
static GSList *ret = NULL;
int lastgood = 0, curgood = 0, curpos = 0, len = strlen(message);
gboolean intag = FALSE;
if (ret) {
GSList *tmp = ret;
while (tmp) {
g_free(tmp->data);
tmp = g_slist_remove(tmp, tmp->data);
}
ret = NULL;
}
while (TRUE) {
if (lastgood >= len)
return ret;
if (len - lastgood < limit) {
ret = g_slist_append(ret, g_strdup(&message[lastgood]));
return ret;
}
curgood = curpos = 0;
intag = FALSE;
while (curpos <= limit) {
if (isspace(message[curpos + lastgood]) && !intag)
curgood = curpos;
if (message[curpos + lastgood] == '<')
intag = TRUE;
if (message[curpos + lastgood] == '>')
intag = FALSE;
curpos++;
}
if (curgood) {
ret = g_slist_append(ret, g_strndup(&message[lastgood], curgood));
if (isspace(message[curgood + lastgood]))
lastgood += curgood + 1;
else
lastgood += curgood;
} else {
/* whoops, guess we have to fudge it here */
ret = g_slist_append(ret, g_strndup(&message[lastgood], limit));
lastgood += limit;
}
}
}
/* Old versions of utility functions, using Base16 rather than Base64 */
/* Note: str will _NOT_ be null terminated. Its length will be returned
from the function */
/* int GE_bytes_to_colonstr(unsigned char *str, unsigned char *bytes, int num) { */
/* int bytes_cursor=0, str_cursor=0; */
/* while (bytes_cursor < num) { */
/* sprintf(str + str_cursor, "%02x", bytes[bytes_cursor++]); */
/* str_cursor += 2; */
/* if (bytes_cursor < num) str[str_cursor++] = ':'; */
/* } */
/* return str_cursor; */
/* } */
/* /\* Note: str will _NOT_ be null terminated. Its length will be returned */
/* from the function *\/ */
/* int GE_bytes_to_str(unsigned char *str, unsigned char *bytes, int num) { */
/* int bytes_cursor=0, str_cursor=0; */
/* while (bytes_cursor < num) { */
/* sprintf(str + str_cursor, "%02x", bytes[bytes_cursor++]); */
/* str_cursor += 2; */
/* } */
/* return str_cursor; */
/* } */
/* /\* Note: str does not need to be null terminated. num bytes are pulled */
/* off the string (unless the end of the string is reached first). */
/* The number of chars pulled off the string is returned, or -1 */
/* if there is an error or the end of the string is reached first. *\/ */
/* int GE_nstr_to_bytes(unsigned char *bytes, unsigned char *nstr, int num) { */
/* int bytes_cursor, str_cursor = 0; */
/* unsigned char minibuf[3] = "00"; */
/* for (bytes_cursor = 0; bytes_cursor < num; ++bytes_cursor) { */
/* minibuf[0] = nstr[str_cursor++]; */
/* if (minibuf[0] == 0) return -1; */
/* minibuf[1] = nstr[str_cursor++]; */
/* if (minibuf[1] == 0) return -1; */
/* bytes[bytes_cursor] = (unsigned char) strtoul(minibuf, 0, 16); */
/* } */
/* return str_cursor; */
/* } */
/* /\* Note: cstr must be null terminated. Up to num bytes are pulled */
/* off the string (unless the end of the string is reached first). */
/* The number of bytes gotten is returned. */
/* A parse error returns -1 *\/ */
/* int GE_cstr_to_bytes(unsigned char *bytes, unsigned char *cstr, int num) { */
/* int bytes_cursor, str_cursor = 0; */
/* unsigned char minibuf[3] = "00"; */
/* for (bytes_cursor = 0; bytes_cursor < num; ++bytes_cursor) { */
/* minibuf[0] = cstr[str_cursor++]; */
/* if (minibuf[0] == 0) return bytes_cursor; */
/* minibuf[1] = cstr[str_cursor++]; */
/* if (minibuf[1] == 0) return -1; */
/* bytes[bytes_cursor] = (unsigned char) strtoul(minibuf, 0, 16); */
/* } */
/* return bytes_cursor; */
/* } */
|