aboutsummaryrefslogtreecommitdiff
path: root/src/routines.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/routines.c')
-rw-r--r--src/routines.c180
1 files changed, 90 insertions, 90 deletions
diff --git a/src/routines.c b/src/routines.c
index 8ce937b..9e8de98 100644
--- a/src/routines.c
+++ b/src/routines.c
@@ -8,17 +8,17 @@
*
* This package 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; version 2 dated June, 1991.
+ * the Free Software Foundation; version 2 dated June, 1991.
*
* This package 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.
+ * GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this package; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * 02111-1307, USA.
*
*/
@@ -71,11 +71,11 @@ char *routines_lindex(char *string, unsigned int pos) {
char *minibuffer;
unsigned int max_output_length = strlen(string);
for ( i=0; i<strlen(string); i++ ) {
-
+
if ( string[i] == ' ' )
s++;
if ( s == pos ) {
-
+
char ch='z';
unsigned int p=0;
char *result;
@@ -83,7 +83,7 @@ char *routines_lindex(char *string, unsigned int pos) {
if ( s == 0 )
i--;
while ((ch != '\0') && (p<max_output_length)) {
-
+
ch = string[i+p+1];
if ( ch == ' ' )
ch = '\0';
@@ -93,18 +93,18 @@ char *routines_lindex(char *string, unsigned int pos) {
ch = '\0';
result[p]=ch;
p++;
-
+
}
result[p]='\0';
-
+
/* now copy the string into a buffer that's the right size */
minibuffer=malloc(strlen(result)+sizeof(char));
strcpy(minibuffer, result);
free(result);
return minibuffer;
-
+
}
-
+
}
/* whoopsie didn't find it */
minibuffer = malloc(1);
@@ -141,7 +141,7 @@ char *routines_lindexend(char *string, unsigned int pos) {
p++;
}
result[p]='\0';
-
+
/* now copy the string into a buffer that's the right size */
minibuffer=malloc(strlen(result)+sizeof(char));
strcpy(minibuffer, result);
@@ -183,9 +183,9 @@ static unsigned int routines_unhex(char ch) {
case 'E': return 14;
case 'F': return 15; /* yes I know i could have done it differently... */
}
-
+
return 0; /* clearly this would be the caller's fault... */
-
+
}
char *routines_urldecode(const char *words) {
@@ -196,17 +196,17 @@ char *routines_urldecode(const char *words) {
result = malloc(strlen(words)+1);
for ( i=0; i<=strlen(words); i++ ) {
-
+
char ch;
ch=words[i];
if ( isxdigit(ch) && (s==2) ) {
-
+
unsigned int new_ch = 0;
unsigned int first_num, second_num;
j-=2;
second_char=ch;
-
+
/* now we take our two hex digits and decimalize... */
first_num = routines_unhex(first_char);
second_num = routines_unhex(second_char);
@@ -217,10 +217,10 @@ char *routines_urldecode(const char *words) {
new_ch = ' '; /* f--k off... */
if ( new_ch == '\13' )
new_ch = ' '; /* grrr */
-
+
ch=new_ch;
s=0;
-
+
}
if ( isxdigit(ch) && (s==1) ) {
s=2;
@@ -231,10 +231,10 @@ char *routines_urldecode(const char *words) {
}
result[j]=ch;
j++;
-
+
}
return result;
-
+
}
char *routines_urlencode(const char *words) {
@@ -320,23 +320,23 @@ char *routines_hostname(const char *input) {
/* if there's no colon, host is just the same as the input */
if ( n == 0 )
return strdup(input);
-
+
i = 0;
/* A few bytes get wasted here */
output = malloc(strlen(input));
while ( ch != '\0' ) {
-
+
ch = input[i];
if ( ch == ':' ) {
ch = '\0';
}
output[i] = ch;
i++;
-
+
}
return output;
/* You can free the output string when you've finished with it */
-
+
}
/* extract the port number from hostname:port */
@@ -354,20 +354,20 @@ int routines_port(const char *input) {
if ( n == 0 ) {
return 0;
}
-
+
i=0;
while ( (ch != ':') && (i<=strlen(input)) ) {
ch = input[i];
i++;
}
-
+
return atoi(input+i);
}
static char routines_base64char(unsigned int sextet) {
switch (sextet) {
-
+
case 0 : return 'A';
case 1 : return 'B';
case 2 : return 'C';
@@ -432,17 +432,17 @@ static char routines_base64char(unsigned int sextet) {
case 61 : return '9';
case 62 : return '+';
case 63 : return '/';
-
+
}
-
+
return 'A';
-
+
}
static int routines_unbase64char(unsigned char sextet) {
switch (sextet) {
-
+
case 'A' : return 0;
case 'B' : return 1;
case 'C' : return 2;
@@ -507,11 +507,11 @@ static int routines_unbase64char(unsigned char sextet) {
case '9' : return 61;
case '+' : return 62;
case '/' : return 63;
-
+
}
-
+
return 0;
-
+
}
char *routines_base64givenlength(char *base64_input, ssize_t length) {
@@ -520,74 +520,74 @@ char *routines_base64givenlength(char *base64_input, ssize_t length) {
char *base64_output;
unsigned int sextet;
size_t j = 0;
-
+
/* One extra byte for \0 terminator, another for rounding error. */
base64_output = malloc((4*length)/3 + 4);
-
+
for ( i=0; i<=length-3; i+=3 ) {
-
+
/* First sextet */
sextet = (base64_input[i] & 0xfc) >> 2;
base64_output[j++] = routines_base64char(sextet);
-
+
/* Second sextet */
sextet = ((base64_input[i] & 0x3) << 4) + ((base64_input[i+1] & 0xf0) >> 4);
base64_output[j++] = routines_base64char(sextet);
-
+
/* Third sextet */
sextet = ((base64_input[i+1] & 0x0f) << 2) + ((base64_input[i+2] & 0xc0) >> 6);
base64_output[j++] = routines_base64char(sextet);
-
+
/* Fourth sextet */
sextet = (base64_input[i+2] & 0x3f);
base64_output[j++] = routines_base64char(sextet);
-
+
}
-
+
switch ((length) % 3) {
-
+
case 1 : {
-
+
/* One byte left over */
i = length-1;
-
+
sextet = (base64_input[i] & 0xfc) >> 2;
base64_output[j++] = routines_base64char(sextet);
-
+
sextet = (base64_input[i] & 0x03) << 4;
base64_output[j++] = routines_base64char(sextet);
-
+
base64_output[j++] = '=';
base64_output[j++] = '=';
-
+
break;
-
+
}
-
+
case 2 : {
-
+
/* Two bytes left over */
i = length-2;
-
+
sextet = (base64_input[i] & 0xfc) >> 2;
base64_output[j++] = routines_base64char(sextet);
-
+
sextet = ((base64_input[i] & 0x3) << 4) + ((base64_input[i+1] & 0xf0) >> 4);
base64_output[j++] = routines_base64char(sextet);
-
+
sextet = (base64_input[i+1] & 0x0f) << 2;
base64_output[j++] = routines_base64char(sextet);
-
+
base64_output[j++] = '=';
-
+
break;
-
+
}
-
+
}
-
+
base64_output[j++] = '\0';
-
+
return base64_output;
}
@@ -605,25 +605,25 @@ size_t routines_base64decode(const char *base64_input, char **output) {
char *op = malloc(strlen(base64_input));
assert(strlen(base64_input) % 4 == 0 );
-
+
while ( ptr < strlen(base64_input) ) {
-
+
op[len] = (routines_unbase64char(base64_input[ptr]) << 2) + ((routines_unbase64char(base64_input[ptr+1]) & 48) >> 4);
op[len+1] = ((routines_unbase64char(base64_input[ptr+1]) & 15) << 4) + ((routines_unbase64char(base64_input[ptr+2]) & 60) >> 2);
op[len+2] = ((routines_unbase64char(base64_input[ptr+2]) & 3) << 6) + routines_unbase64char(base64_input[ptr+3]);
-
+
len+=3;
ptr+=4;
-
+
}
-
+
if ( base64_input[ptr-1] == '=' ) {
len--;
}
if ( base64_input[ptr-2] == '=' ) {
len--;
}
-
+
*output = op;
return len;
@@ -633,13 +633,13 @@ static char *routines_randomhexbyte() {
int j;
char *response = malloc(3);
-
+
/* Taken from the "rand()" man page. */
j = 1 +(int)(256.0*rand()/(RAND_MAX+1.0));
sprintf(response, "%2hhx", j);
-
+
return response;
-
+
}
/* Generate a GUID */
@@ -647,56 +647,56 @@ char *routines_guid() {
int i, k;
char *guid;
-
+
guid = malloc(39);
assert(guid != NULL);
strcpy(guid, "{");
-
+
for ( i=0; i<4; i++ ) {
-
+
char *byte;
byte = routines_randomhexbyte();
strncat(guid, byte, 2);
free(byte);
-
+
}
-
+
for ( k=0; k<3; k++ ) {
strcat(guid, "-");
for ( i=0; i<2; i++ ) {
-
+
char *byte;
byte = routines_randomhexbyte();
strncat(guid, byte, 2);
free(byte);
-
+
}
}
-
+
/* To be a valid GUID, the version and variant fields need to be set appropriately. */
guid[15] = '4';
guid[20] = 'a';
-
+
strcat(guid, "-");
for ( i=0; i<6; i++ ) {
-
+
char *byte;
byte = routines_randomhexbyte();
strncat(guid, byte, 2);
free(byte);
-
+
}
strcat(guid, "}");
-
+
/* Fix up spaces. */
for ( i=0; i<(strlen(guid)-1); i++ ) {
if ( guid[i] == ' ' ) {
guid[i] = '0';
}
}
-
+
return guid;
}
@@ -707,7 +707,7 @@ char *routines_killtriangles(const char *input) {
char *output;
size_t i;
size_t j = 0;
-
+
output = malloc(2*strlen(input)+1);
for ( i=0; i<=strlen(input); i++ ) {
if ( input[i] == '<' ) {
@@ -735,7 +735,7 @@ char *routines_killtriangles_and_ampersands(const char *input) {
char *output;
size_t i;
size_t j = 0;
-
+
output = malloc(2*strlen(input)+1);
for ( i=0; i<=strlen(input); i++ ) {
if ( input[i] == '<' ) {
@@ -769,27 +769,27 @@ char *routines_flipcolour(const char *colour) {
char scratch;
strncpy(flipped, colour, 6);
flipped[6] = '\0';
-
+
scratch = flipped[0];
flipped[0] = flipped[4];
flipped[4] = scratch;
-
+
scratch = flipped[1];
flipped[1] = flipped[5];
flipped[5] = scratch;
-
+
return flipped;
}
char *routines_gdk_to_hashrgb(const GdkColor *colour) {
-
+
char *string;
-
+
if ( colour == NULL ) {
return NULL;
}
-
+
string = malloc(8);
/* RGB order */
snprintf(string, 8, "#%02hhx%02hhx%02hhx", colour->red >> 8, colour->green >> 8, colour->blue >> 8);