small stuff innd/python.c

greg andruk meowing at banet.net
Sun Oct 10 00:15:49 UTC 1999


Add a msg hash function, tweak the art buffer to work with it,
and make the new FromWireFmt stuff work.

*** innd/python.c.dist	Sat Oct  9 18:56:20 1999
--- innd/python.c	Sat Oct  9 19:03:49 1999
***************
*** 108,114 ****
  
      /* ...then the body... */
      if (artBody != NULL) {
! 	PYheaditem[hdrnum] = PyBuffer_FromMemory(artBody, artLen);
  	PyDict_SetItem(PYheaders, PYbodykey, PYheaditem[hdrnum++]);
      }
  
--- 108,114 ----
  
      /* ...then the body... */
      if (artBody != NULL) {
! 	PYheaditem[hdrnum] = PyBuffer_FromMemory(artBody, --artLen);
  	PyDict_SetItem(PYheaders, PYbodykey, PYheaditem[hdrnum++]);
      }
  
***************
*** 351,357 ****
      ARTHANDLE	*art;
      PyObject	*header;
      int		headerlen;
-     char	*headertxt;
  
      if (!PyArg_ParseTuple(args, "s#", &msgid, &msgidlen))
  	return NULL;
--- 351,356 ----
***************
*** 363,372 ****
  	return Py_BuildValue("s", "");	
      p = FromWireFmt(art->data, art->len, &headerlen);
      SMfreearticle(art);
!     headerlen++;
!     header = PyString_FromStringAndSize(NULL, headerlen);
!     headertxt = PyString_AS_STRING(header);
!     strncpy(headertxt, p, headerlen);
      DISPOSE(p);
  
      return header;
--- 362,368 ----
  	return Py_BuildValue("s", "");	
      p = FromWireFmt(art->data, art->len, &headerlen);
      SMfreearticle(art);
!     header = PyString_FromStringAndSize(p, headerlen);
      DISPOSE(p);
  
      return header;
***************
*** 388,394 ****
      ARTHANDLE	*arth;
      PyObject	*art;
      int		artlen;
-     char	*arttxt;
  
      if (!PyArg_ParseTuple(args, "s#", &msgid, &msgidlen))
  	return NULL;
--- 384,389 ----
***************
*** 400,409 ****
  	return Py_BuildValue("s", "");	
      p = FromWireFmt(arth->data, arth->len, &artlen);
      SMfreearticle(arth);
!     artlen++;
!     art = PyString_FromStringAndSize(NULL, artlen);
!     arttxt = PyString_AS_STRING(header);
!     strncpy(arttxt, p, artlen);
      DISPOSE(p);
  
      return art;
--- 395,401 ----
  	return Py_BuildValue("s", "");	
      p = FromWireFmt(arth->data, arth->len, &artlen);
      SMfreearticle(arth);
!     art = PyString_FromStringAndSize(p, artlen);
      DISPOSE(p);
  
      return art;
***************
*** 450,455 ****
--- 442,522 ----
  
  
  /*
+ **  Compute a hash digest for a string.
+ */
+ static PyObject *
+ PY_hashstring(self, args)
+     PyObject *self, *args;
+ {
+     char	*instring, *wpos, *p, *q;
+     char	*workstring = NULL;
+     int		insize, worksize, newsize, i, wasspace;
+     int		lines = 0;
+     HASH	myhash;
+ 
+     if (!PyArg_ParseTuple(args, "s#|i", &instring, &insize, &lines))
+ 	return NULL;
+ 
+     /* If a linecount is provided, munge before hashing. */
+     if (lines > 0) {
+ 	worksize = insize;
+ 
+ 	/* chop leading whitespace */
+ 	for (p=instring ; worksize>0 && isspace(*p) ; p++) {
+ 	    if (*p == '\n')
+ 		lines--;
+ 	    worksize--;
+ 	}
+ 	wpos = p;
+ 
+ 	/* and trailing */
+ 	for (p=&wpos[worksize] ; worksize>0 && isspace(*p) ; p--) {
+ 	    if (*p == '\n')
+ 		lines--;
+ 	    worksize--;
+ 	}
+ 
+ 	/* chop last 3 lines if we have >= 5.  From above chop the
+ 	 * last line has no CR so we use 1 less here. */
+ 	if (lines >= 4) {
+ 	    for (i=0, p=wpos+worksize ; i<2 ; p--)
+ 		if (*p == '\n')
+ 		    i++;
+ 	    worksize = p - wpos;
+ 	}
+ 
+ 	/* Compress out multiple whitespace in the trimmed string.  We
+ 	 * do a copy because this is probably an original art
+ 	 * buffer. */
+ 	workstring =  memcpy(NEW(char, worksize), wpos, worksize);
+ 	newsize = wasspace = 0;
+ 	p = wpos;
+ 	q = workstring;
+ 	for (i=0 ; i<worksize ; i++) {
+ 	    if isspace(*p) {
+ 		if (!wasspace)
+ 		    *q++ = ' ';
+ 		wasspace = 1;
+ 	    }
+ 	    else {
+ 		*q++ = tolower(*p);
+ 		wasspace = 0;
+ 	    }
+ 	    p++;
+ 	}
+ 	worksize = q - workstring;
+ 	myhash = Hash(workstring, worksize);
+ 	DISPOSE(workstring);
+     }
+     else
+ 	myhash = Hash(instring, insize);
+ 
+     return PyString_FromStringAndSize((const char *)&myhash, sizeof(myhash));
+ }
+ 
+ 
+ 
+ /*
  **  Make the internal INN module's functions visible to Python.
  */
  static PyMethodDef INNPyMethods[] = {
***************
*** 461,466 ****
--- 528,534 ----
      {"head",		PY_head,		METH_VARARGS},
      {"article",		PY_article,		METH_VARARGS},
      {"syslog",		PY_syslog,		METH_VARARGS},
+     {"hashstring",	PY_hashstring,		METH_VARARGS},
      {NULL,		NULL}
  };
  


More information about the inn-patches mailing list