/*****************************************************************************
 *                                                                           *
 * junidec v0.1. Juniper Secure Access products has a feature for masking    *
 * internal URL (Mask hostnames while browsing). This little program will    *
 * help you to decode or encode these URLs (DanaInfo or url variables).      *
 *                                                                           *
 * Copyright (c) 2010 @ Mediaservice.net Srl. All rights reserved            *
 * Wrote by Maurizio Agazzini <inode[at]mediaservice.net>                    *
 *                                                                           *
 * 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., 59 Temple Place                                         *
 * Suite 330, Boston, MA  02111-1307, USA.                                   *
 *                                                                           *
 *****************************************************************************/

#include <stdio.h>
#include <string.h>
#include <unistd.h>

char charset[]="0123456789-.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

int posizione(char c)
{
	return strchr(charset,c)-charset;
}

char left(int j, int i)
{
	for( ; i > 0; i--)
	{
		j--;
		if( j < 0 ) 
			j = strlen(charset)-1;
	}

	return charset[j];
}

char right(int j, int i)
{

        for( ; i > 0; i--)
        {
                j++;
                if( j >= strlen(charset)) 
                        j = 0;
        }

        return charset[j];
}

int main(int argc, char ** argv)
{
	int i,j,mode=0;

	char l, opt;
	char * stringa = NULL;

	printf("\nJuniper URL decoder/encoder\n");
 	printf("Copyright (c) 2010 @ Mediaservice.net Srl. All rights reserved\n"); 

	while((opt = getopt(argc, argv, "d:e:")) != -1)
	{ 
		
		switch (opt)
                {
			case 'd':
				stringa = optarg;
				mode = 1;
				break;
			case 'e': 
				stringa = optarg;
				mode = 2;
				break;
		}

		

	}

	if( mode == 0 || stringa == NULL ) {

		printf("\nUsage:\n");
		printf("	%s -d \"url encoded\"\n", argv[0]);
		printf("        %s -e \"url\"\n\n", argv[0]);

		return 1;

	}

	if( mode == 1 ) {
		printf("\nDecoded string: ");
		stringa += 2;
	}
	else
		printf("\nEncoded string: .a");


	for( i = 0; i< strlen(stringa); i++){

		l = stringa[i];

		switch (l) {
		
			case ':':
			case '/':
				break;
			case '+':
				printf("\n\n");
				return 0;

			default:
				if( mode == 1 ) {
					j = posizione(l);
					l = left(j,i);
				} else {
					j = posizione(l);
					l = right(j,i);

				}
				break;


		}

		printf("%c",l);
	}

	printf("\n\n");
	return 0;
}
