/***********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.

STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

******************************************************************/

/* urlop module - patterned after stropmodule.c */
/* Skip Montanaro - March 16, '96 */
/* Copyright 1996, Automatrix, Inc */
/* 6/14/96 - bugfix: character strings should be unsigned char */

#include "allobjects.h"
#include "modsupport.h"

#include <ctype.h>
/* XXX This file assumes that the <ctype.h> is*() functions
   XXX are defined for all 8-bit characters! */

#include <errno.h>

static char always_safe[256];
static char *hexd = "0123456789abcdef";

static object *
urlop_quote(self, args)
	object *self; /* Not used */
	object *args;
{
    unsigned char *s, *s_new, *safechars;
    unsigned char safe[256];
    int i, j, n, safen;
    object *old, *new;
    int changed = 0;

    safechars = "/";
    safen = 1;
    if (!PyArg_ParseTuple(args, "S|s#", &old, &safechars, &safen))
	return NULL;

    n = PyString_Size(old);
    if (!n) {
	INCREF(old);
	return old;
    }
    s = PyString_AsString(old);
    s_new = alloca(n * 3 + 1);
    s_new[0] = 0;

    /* set up mask */
    for (i = 0; i < 256; i++) safe[i] = always_safe[i];
    for (i = 0; i < safen; i++) safe[safechars[i]] = 1;

    /* test chars against masks */
    for (i = 0, j = 0; i < n; i++) {
	if (safe[s[i]]) {
	    s_new[j] = s[i];
	    j++;
	}
	else {
	    s_new[j] = '%'; j++;
	    s_new[j] = hexd[s[i]/16]; j++;
	    s_new[j] = hexd[s[i]%16]; j++;
	    changed = 1;
	}
    }
    if (!changed) {
	INCREF(old);
	return old;
    }
    return newsizedstringobject(s_new, j);

}


static object *
urlop_unquote(self, args)
	object *self; /* Not used */
	object *args;
{
    unsigned char *s, *s_new;
    unsigned char safe[256];
    int i, j, n, safen;
    object *old, *new;
    int changed = 0;
    unsigned char xdigit[3];

    xdigit[2] = 0;
    if (!PyArg_ParseTuple(args, "S", &old)) return NULL;

    n = PyString_Size(old);
    if (!n) {
	INCREF(old);
	return old;
    }
    s = PyString_AsString(old);
    s_new = alloca(n + 1);
    s_new[0] = 0;

    for (i = 0, j = 0; i < n; i++) {
	if (s[i] == '%' && isxdigit(s[i+1]) && isxdigit(s[i+2])) {
	    xdigit[0] = s[i+1];
	    xdigit[1] = s[i+2];
	    s_new[j] = strtol(xdigit, 0, 16);
	    j++;
	    s_new[j] = 0;
	    i += 2;
	    changed = 1;
	}
	else {
	    s_new[j++] = s[i];
	}
    }
    if (!changed) {
	INCREF(old);
	return old;
    }
    return newsizedstringobject(s_new, j);

}


/* List of functions defined in the module */

static struct methodlist urlop_methods[] = {
    {"quote",	urlop_quote, 1},
    {"unquote",	urlop_unquote, 1},
    {NULL,		NULL}	/* sentinel */
};


void
initurlop()
{
    int i;
    char c;

    initmodule("urlop", urlop_methods);
    if (err_occurred())
	fatal("can't initialize module urlop");

    for (i = 0; i < 256; i++) always_safe[i] = 0;
    for (c = 'A'; c <= 'Z'; c++) always_safe[c] = 1;
    for (c = 'a'; c <= 'z'; c++) always_safe[c] = 1;
    for (c = '0'; c <= '9'; c++) always_safe[c] = 1;
    always_safe['_'] = always_safe[','] = always_safe['.'] =
	always_safe['-'] = 1;

}

