MOON
Server: Apache/2.2.34 (Unix) mod_ssl/2.2.34 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4 FrontPage/5.0.2.2635
System: Linux server.asjudinet.com 2.6.32-042stab141.3 #1 SMP Fri Nov 15 22:45:34 MSK 2019 i686
User: asjudine (504)
PHP: 5.2.17
Disabled: NONE
Upload Files
File: //usr/share/doc/libidn-devel-0.6.5/contrib/idn-python/idn.c
/*
 * This is a Python interface over Simon Josefsson's libidn
 * <URL:http://josefsson.org/libidn/>.
 *
 * Stephane Bortzmeyer <bortzmeyer@nic.fr>
 *
 * $Id: idn.c,v 1.2 2004-04-05 20:52:12 jas Exp $
 */

#include <Python.h>
#include <string.h>
#include <idna.h>

#define MESSAGE_SIZE 512

static PyObject *IDNError;
static PyObject *IDNInvLengthError;

#define onError(message) { PyErr_SetString(IDNError, message); free(message); return NULL; }

static PyObject *
idn2ace (PyObject * self, PyObject * args)
{
  char *instr, *result;
  int rc;
  PyObject *outstr;
  if (!PyArg_ParseTuple (args, "s", &instr))
    onError ("Invalid argument");
  rc = idna_to_ascii_8z (instr, &result);
  if (rc != IDNA_SUCCESS)
    {
      switch (rc)
	{
	case IDNA_INVALID_LENGTH:
	  result = malloc (MESSAGE_SIZE);
	  sprintf (result, "%d bytes", strlen (instr));
	  PyErr_SetString (IDNInvLengthError, result);
	  free (result);
	  return NULL;
	  break;
	default:
	  result = malloc (MESSAGE_SIZE);
	  sprintf (result, "IDN error: %d (see idna.h)", rc);
	  onError (result);
	}
    }
  outstr = Py_BuildValue ("s", result);
  return outstr;
}

static PyObject *
ace2idn (PyObject * self, PyObject * args)
{
  char *instr, *result;
  int rc;
  PyObject *outstr;
  if (!PyArg_ParseTuple (args, "s", &instr))
    onError ("Invalid argument");
  rc = idna_to_unicode_8z8z (instr, &result);
  if (rc != IDNA_SUCCESS)
    {
      result = malloc (MESSAGE_SIZE);
      sprintf (result, "IDN error: %d (see idna.h)", rc);
      onError (result);
    }
  outstr = Py_BuildValue ("s", result);
  return outstr;
}

static struct PyMethodDef methods[] = {
  {"idn2ace", idn2ace, 1},
  {"ace2idn", ace2idn, 1},
  {NULL, NULL}
};

void
initidn ()
{
  Py_InitModule ("idn", methods);
  IDNError = PyErr_NewException ("idn.error", NULL, NULL);
  IDNInvLengthError = PyErr_NewException ("idn.invalidLength", NULL, NULL);
}