/*      Get and Resume Elite EDition source code


Get and Resume Elite EDition (GREED)
Copyright (C) 1999  Anoakie Turner

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., 675 Mass Ave, Cambridge, MA 02139, USA.

For more information on the GPL, please go to:
http://www.gnu.org/copyleft/gpl.html


        Contact:  Anoakie Turner
                  Anoakie.Turner@asu.edu

                  13240 N. 94th Pl.
                  Scottsdale, AZ 85260
*/

#include "main.h"


char *ReadString(char *str1, int *ind, char stopchar, int stopint)
/*************************************
**	void ReadString(char *str1, int &ind, char stopchar,
**			int stopint, char* str2);
**
** Pre:  Assigned (str1) && Assigned (ind) && Assigned (stopchar)
**	 && Assigned (stopint)
** Post: a string starting at ind and ending at (stopchar ||
**	 stopint) is copied int str2
*************************************/
{	int stringIndex = *ind;
	char *str2;

	while (str1[stringIndex] != stopchar && stringIndex < stopint)
		stringIndex ++;
	str2 = malloc(stringIndex + 1);

	stringIndex = 0;

	while (*ind < stopint && str1[*ind] != stopchar)
	{	str2 [stringIndex++] = str1[*ind];
		(*ind)++;
	}
	str2 [stringIndex] = '\0';
	return(str2);
}


char *ReadString2(char *str1, char stopchar)
/*************************************
**      void ReadString(char *str1, int &ind, char stopchar,
**                      int stopint, char* str2);
**
** Pre:  Assigned (str1) && Assigned (ind) && Assigned (stopchar)
**       && Assigned (stopint)
** Post: a string starting at ind and ending at (stopchar ||
**       stopint) is copied int str2
*************************************/
{       int stringIndex = 0;
	int ind = 0;
        char *str2;

        if (str1 == NULL)
                return(NULL);

        while (str1[stringIndex] != stopchar)
                stringIndex ++;
        str2 = malloc(stringIndex + 1);

        stringIndex = 0;

        while (str1[ind] != stopchar)
        {       str2 [stringIndex++] = str1[ind];
                (ind)++;
        }
        str2 [stringIndex] = '\0';
        return (str2);
}

/*
char *ReadString2(char *str, char stopchar)
{	char *retval = malloc(strlen(str) - strlen(strchr(str, stopchar)) + 1);
	strncpy(retval, str, strlen(str) - strlen(strchr(str, stopchar)));
	retval[strlen(str) - strlen(strchr(str, stopchar))] = '\0';
	return (retval);
}
*/

void unescape_url (char *url)
/*************************************
**	unescape_url (char *url)
**
** NOTES: This routine was taken from an example that comes with the NSCA server
**	  This routine is to decode the %HH characters recieved from the server
*************************************/
{
        register int x, y;
        for (x = 0, y = 0; url [y]; ++x, ++y)
        {
                if ((url [x] = url [y]) == '%')
                {
                        url [x] = x2c (&url [y+1]);
                        y += 2;
                }
                else if (url [x] == '+')
                        url [x] = ' ';
   }
        url [x] = '\0';
}

char x2c (char *what)
/*************************************
**
*************************************/
{	register char digit;
        digit = (what [0] >= 'A' ? ((what [0] & 0xdf) - 'A') + 10 : (what [0] - '0'));
        digit *= 16;
        digit += (what [1] >= 'A' ? ((what [1] & 0xdf) - 'A') + 10 : (what [1] - '0'));
        return (digit);
}



char *Base64Encode(char *str1)
/*************************************
**	char Base64Encode (char *)
**
** PRE:  exists(str1)
** POST: returns a Base 64 encoded version of (str1)
*************************************/
{	int len = strlen(str1);
	int i = 0;
	int j = 0;
	char *str2 = malloc((len/3)*4 + 3);

	while (j < len - 2)
	{	str2[i++] = BASE64[str1[j]>>2];
		str2[i++] = BASE64[((str1[j]&3)<<4)|(str1[j + 1]>>4)];
		str2[i++] = BASE64[((str1[j + 1]&15)<<2)|(str1[j + 2]>>6)];
		str2[i++] = BASE64[str1[j+2]&63];
		j+=3;
	}

	switch(len - j)
	{	case 1:
			str2[i++] = BASE64[str1[j]>>2];
			str2[i++] = BASE64[((str1[j]&3)<<4)];
			str2[i++] = '=';
			str2[i++] = '=';
			break;
		case 2:
			str2[i++] = BASE64[str1[j]>>2];
			str2[i++] = BASE64[((str1[j]&3)<<4)|(str1[j + 1]>>4)];
			str2[i++] = BASE64[((str1[j + 1]&15)<<2)];
			str2[i++] = '=';
			break;
		default:
			break;
	}
	str2[i] = '\0';
	return(str2);
}
