/*=======================================================================
  (C) Copyright SAP AG, 2002


 Author  : Dostert/SAP
 Project : Get data from R/3 using XMB
 Used    : SXMI_LOGON, SXMI_LOGOFF
 Using   : RFCSDK
 ========================================================================*/
#define MAX_INPUT_BUFFER 1024

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* encodes a string in proper URL format */
char * url_encode(char *input){
        int len,output_len;
        int x,y;
        char temp_expansion[4];
	
	char *encoded_url_string = (char *)calloc(MAX_INPUT_BUFFER, sizeof(char));
	

        len=(int)strlen(input);
        output_len= MAX_INPUT_BUFFER;

        encoded_url_string[0]='\x0';

        for(x=0,y=0;x<=len && y<output_len-1;x++){

                /* end of string */
                if((char)input[x]==(char)'\x0'){
                        encoded_url_string[y]='\x0';
                        break;
                }

                /* alpha-numeric characters don't get encoded */
                else if(((char)input[x]>='0' && (char)input[x]<='9') || ((char)input[x]>='A' && (char)input[x]<='Z') ||
                                ((char)input[x]>=(char)'a' && (char)input[x]<=(char)'z')){
                        encoded_url_string[y]=input[x];
                        y++;
                }

                /* spaces are pluses */
                else if((unsigned char)input[x]<=(char)' '){
                        encoded_url_string[y]='+';
                        y++;
                }
                /* anything else gets represented by its hex value */
                else{
                        encoded_url_string[y]='\x0';
                        if((int)strlen(encoded_url_string)<(output_len-3)){
                                sprintf(temp_expansion,"%%%02X",(unsigned char)input[x]);
                                strcat(encoded_url_string,temp_expansion);
                                y+=3;
                        }
                }
        }

        encoded_url_string[MAX_INPUT_BUFFER-1]='\x0';

        return &encoded_url_string[0];
}

/* decodes a string from proper URL format */
char * url_decode(char *input){
        int len,output_len;
        int x,y;
        char temp_expansion[4];

	char *decoded_url_string = (char *) calloc(MAX_INPUT_BUFFER, sizeof(char));

        len=(int)strlen(input);
        output_len= MAX_INPUT_BUFFER;

        decoded_url_string[0]='\x0';

        for(x=0,y=0;x<=len && y<output_len-1;x++){
                /* end of string */
                if((char)input[x]==(char)'\x0'){
                        decoded_url_string[y]='\x0';
                        break;
                }

                /* alpha-numeric characters don't get decoded */
                else if(((char)input[x]>='0' && (char)input[x]<='9') || ((char)input[x]>='A' && (char)input[x]<='Z') ||
                                ((char)input[x]>=(char)'a' && (char)input[x]<=(char)'z')){
                        decoded_url_string[y]=input[x];
                        y++;
                }

                /* pluses are spaces */
                else if((char)input[x] == (char)'+'){
                        decoded_url_string[y]=' ';
                        y++;
                }

                /* anything else is represented by its hex value */
                else if((char)input[x] =='%') {
                        decoded_url_string[y]='\x0';
                        if(x + 3 <=(int)strlen(input)){
				strncpy(temp_expansion, &(input[x+1]), 2);
				temp_expansion[3] = '\0';
				
				sscanf(temp_expansion, "%02X", (unsigned int *)&(decoded_url_string[y++]));
				x += 2; 
                        }
                } else {
			decoded_url_string[y]=input[x];
                        y++;
		}
        }

        decoded_url_string[MAX_INPUT_BUFFER-1]='\x0';

        return &decoded_url_string[0];
}




