mirror of
https://github.com/python/cpython.git
synced 2024-11-24 02:15:30 +08:00
* mymalloc.h: always allocate one extra byte, since some malloc's
return NULL for malloc(0) or realloc(p, 0). (This should be done differently than wasting one byte, but alas...) * Moved "add'l libraries" option in Makefile to an earlier place. * Remove argument compatibility hacks (b) and (c). * Add grey2mono, dither2mono and mono2grey to imageop. * Dup the fd in socket.fromfd(). * Added new modules mpz, md5 (by JH, requiring GNU MP 1.2). Affects Makefile and config.c. * socketmodule.c: added socket.fromfd(fd, family, type, [proto]), converted socket() to use of getargs().
This commit is contained in:
parent
8de83e041c
commit
5f59d6018e
@ -53,12 +53,14 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
#define NEW(type, n) ( (type *) malloc((n) * sizeof(type)) )
|
||||
/* XXX Always allocate one extra byte, since some malloc's return NULL
|
||||
XXX for malloc(0) or realloc(p, 0). */
|
||||
#define NEW(type, n) ( (type *) malloc(1 + (n) * sizeof(type)) )
|
||||
#define RESIZE(p, type, n) \
|
||||
if ((p) == NULL) \
|
||||
(p) = (type *) malloc((n) * sizeof(type)); \
|
||||
(p) = (type *) malloc(1 + (n) * sizeof(type)); \
|
||||
else \
|
||||
(p) = (type *) realloc((ANY *)(p), (n) * sizeof(type))
|
||||
(p) = (type *) realloc((ANY *)(p), 1 + (n) * sizeof(type))
|
||||
#define DEL(p) free((ANY *)p)
|
||||
#define XDEL(p) if ((p) == NULL) ; else DEL(p)
|
||||
|
||||
|
@ -62,7 +62,8 @@ imageop_crop(self, args)
|
||||
xstep = (newx1 < newx2)? 1 : -1;
|
||||
ystep = (newy1 < newy2)? 1 : -1;
|
||||
|
||||
rv = newsizedstringobject(NULL, (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size);
|
||||
rv = newsizedstringobject(NULL,
|
||||
(abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size);
|
||||
if ( rv == 0 )
|
||||
return 0;
|
||||
ncp = (char *)getstringvalue(rv);
|
||||
@ -123,6 +124,134 @@ imageop_scale(self, args)
|
||||
return rv;
|
||||
}
|
||||
|
||||
static object *
|
||||
imageop_grey2mono(self, args)
|
||||
object *self;
|
||||
object *args;
|
||||
{
|
||||
int tres, x, y, len;
|
||||
unsigned char *cp, *ncp;
|
||||
unsigned char ovalue;
|
||||
object *rv;
|
||||
int i, bit;
|
||||
|
||||
|
||||
if ( !getargs(args, "(s#iii)", &cp, &len, &x, &y, &tres) )
|
||||
return 0;
|
||||
|
||||
if ( x*y != len ) {
|
||||
err_setstr(ImageopError, "String has incorrect length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
rv = newsizedstringobject(NULL, (len+7)/8);
|
||||
if ( rv == 0 )
|
||||
return 0;
|
||||
ncp = (unsigned char *)getstringvalue(rv);
|
||||
|
||||
bit = 0x80;
|
||||
ovalue = 0;
|
||||
for ( i=0; i < len; i++ ) {
|
||||
if ( cp[i] > tres )
|
||||
ovalue |= bit;
|
||||
bit >>= 1;
|
||||
if ( bit == 0 ) {
|
||||
*ncp++ = ovalue;
|
||||
bit = 0x80;
|
||||
ovalue = 0;
|
||||
}
|
||||
}
|
||||
if ( bit != 0x80 )
|
||||
*ncp++ = ovalue;
|
||||
return rv;
|
||||
}
|
||||
|
||||
static object *
|
||||
imageop_dither2mono(self, args)
|
||||
object *self;
|
||||
object *args;
|
||||
{
|
||||
int sum, x, y, len;
|
||||
unsigned char *cp, *ncp;
|
||||
unsigned char ovalue;
|
||||
object *rv;
|
||||
int i, bit;
|
||||
|
||||
|
||||
if ( !getargs(args, "(s#ii)", &cp, &len, &x, &y) )
|
||||
return 0;
|
||||
|
||||
if ( x*y != len ) {
|
||||
err_setstr(ImageopError, "String has incorrect length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
rv = newsizedstringobject(NULL, (len+7)/8);
|
||||
if ( rv == 0 )
|
||||
return 0;
|
||||
ncp = (unsigned char *)getstringvalue(rv);
|
||||
|
||||
bit = 0x80;
|
||||
ovalue = 0;
|
||||
sum = 0;
|
||||
for ( i=0; i < len; i++ ) {
|
||||
sum += cp[i];
|
||||
if ( sum >= 256 ) {
|
||||
sum -= 256;
|
||||
ovalue |= bit;
|
||||
}
|
||||
bit >>= 1;
|
||||
if ( bit == 0 ) {
|
||||
*ncp++ = ovalue;
|
||||
bit = 0x80;
|
||||
ovalue = 0;
|
||||
}
|
||||
}
|
||||
if ( bit != 0x80 )
|
||||
*ncp++ = ovalue;
|
||||
return rv;
|
||||
}
|
||||
|
||||
static object *
|
||||
imageop_mono2grey(self, args)
|
||||
object *self;
|
||||
object *args;
|
||||
{
|
||||
int v0, v1, x, y, len, nlen;
|
||||
unsigned char *cp, *ncp;
|
||||
unsigned char ovalue;
|
||||
object *rv;
|
||||
int i, bit, value;
|
||||
|
||||
if ( !getargs(args, "(s#iiii)", &cp, &len, &x, &y, &v0, &v1) )
|
||||
return 0;
|
||||
|
||||
nlen = x*y;
|
||||
if ( (nlen+7)/8 != len ) {
|
||||
err_setstr(ImageopError, "String has incorrect length");
|
||||
return 0;
|
||||
}
|
||||
|
||||
rv = newsizedstringobject(NULL, nlen);
|
||||
if ( rv == 0 )
|
||||
return 0;
|
||||
ncp = (unsigned char *)getstringvalue(rv);
|
||||
|
||||
bit = 0x80;
|
||||
for ( i=0; i < nlen; i++ ) {
|
||||
if ( *cp & bit )
|
||||
*ncp++ = v1;
|
||||
else
|
||||
*ncp++ = v0;
|
||||
bit >>= 1;
|
||||
if ( bit == 0 ) {
|
||||
bit = 0x80;
|
||||
cp++;
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/*
|
||||
static object *
|
||||
imageop_mul(self, args)
|
||||
@ -161,6 +290,9 @@ imageop_mul(self, args)
|
||||
static struct methodlist imageop_methods[] = {
|
||||
{ "crop", imageop_crop },
|
||||
{ "scale", imageop_scale },
|
||||
{ "grey2mono", imageop_grey2mono },
|
||||
{ "dither2mono", imageop_dither2mono },
|
||||
{ "mono2grey", imageop_mono2grey },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
|
222
Modules/md5module.c
Normal file
222
Modules/md5module.c
Normal file
@ -0,0 +1,222 @@
|
||||
/***********************************************************
|
||||
Copyright 1992 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.
|
||||
|
||||
******************************************************************/
|
||||
/* MD5 module */
|
||||
|
||||
/* This module provides an interface to a message digest algorithm,
|
||||
MD5 in this case */
|
||||
|
||||
/* MD5 objects */
|
||||
|
||||
#include "allobjects.h"
|
||||
#include "modsupport.h" /* For getargs() etc. */
|
||||
|
||||
#include "md5.h"
|
||||
typedef struct {
|
||||
OB_HEAD
|
||||
MD5_CTX md5; /* the context holder */
|
||||
} md5object;
|
||||
|
||||
extern typeobject MD5type; /* Really static, forward */
|
||||
|
||||
#define is_md5object(v) ((v)->ob_type == &MD5type)
|
||||
|
||||
static const char initialiser_name[] = "md5";
|
||||
|
||||
/* #define MD5_DEBUG */
|
||||
|
||||
static md5object *
|
||||
newmd5object()
|
||||
{
|
||||
md5object *md5p;
|
||||
|
||||
|
||||
#ifdef MD5_DEBUG
|
||||
fputs( "md5_object() called...\n", stderr );
|
||||
#endif /* def MD5_DEBUG */
|
||||
md5p = NEWOBJ(md5object, &MD5type);
|
||||
if (md5p == NULL)
|
||||
return NULL;
|
||||
|
||||
MD5Init(&md5p->md5); /* actual initialisation */
|
||||
return md5p;
|
||||
} /* newmd5object() */
|
||||
|
||||
|
||||
/* MD5 methods */
|
||||
|
||||
static void
|
||||
md5_dealloc(md5p)
|
||||
md5object *md5p;
|
||||
{
|
||||
#ifdef MD5_DEBUG
|
||||
fputs( "md5_dealloc() called...\n", stderr );
|
||||
#endif /* def MD5_DEBUG */
|
||||
|
||||
DEL(md5p);
|
||||
} /* md5_dealloc() */
|
||||
|
||||
|
||||
/* MD5 initialisation */
|
||||
|
||||
static object *
|
||||
MD5_md5(self, args)
|
||||
object *self;
|
||||
object *args;
|
||||
{
|
||||
md5object *md5p;
|
||||
char *cp = (char *)NULL;
|
||||
int len;
|
||||
|
||||
|
||||
#ifdef MD5_DEBUG
|
||||
fputs("MD5_md5() called...\n", stderr);
|
||||
#endif /* def MD5_DEBUG */
|
||||
|
||||
if (!getargs(args, "")) {
|
||||
err_clear();
|
||||
if (!getargs(args, "s#", &cp, &len))
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((md5p = newmd5object()) == NULL)
|
||||
return NULL;
|
||||
|
||||
if (cp)
|
||||
MD5Update(&md5p->md5, cp, len);
|
||||
|
||||
return (object *)md5p;
|
||||
} /* MD5_md5() */
|
||||
|
||||
|
||||
/* MD5 methods-as-attributes */
|
||||
static object *
|
||||
md5_update(self, args)
|
||||
md5object *self;
|
||||
object *args;
|
||||
{
|
||||
char *cp;
|
||||
int len;
|
||||
|
||||
|
||||
if (!getargs(args, "s#", &cp, &len))
|
||||
return NULL;
|
||||
|
||||
MD5Update(&self->md5, cp, len);
|
||||
|
||||
INCREF(None);
|
||||
return None;
|
||||
} /* md5_update() */
|
||||
|
||||
static object *
|
||||
md5_digest(self, args)
|
||||
md5object *self;
|
||||
object *args;
|
||||
{
|
||||
MD5_CTX mdContext;
|
||||
stringobject *strobjp;
|
||||
|
||||
if (!getnoarg(args))
|
||||
return NULL;
|
||||
|
||||
/* make a temporary copy, and perform the final */
|
||||
mdContext = self->md5;
|
||||
MD5Final(&mdContext);
|
||||
|
||||
return newsizedstringobject((char *)mdContext.digest, 16);
|
||||
} /* md5_digest() */
|
||||
|
||||
static object *
|
||||
md5_copy(self, args)
|
||||
md5object *self;
|
||||
object *args;
|
||||
{
|
||||
md5object *md5p;
|
||||
|
||||
|
||||
if (!getnoarg(args))
|
||||
return NULL;
|
||||
|
||||
if ((md5p = newmd5object()) == NULL)
|
||||
return NULL;
|
||||
|
||||
md5p->md5 = self->md5;
|
||||
|
||||
return (object *)md5p;
|
||||
} /* md5_copy() */
|
||||
|
||||
|
||||
static struct methodlist md5_methods[] = {
|
||||
{"update", md5_update},
|
||||
{"digest", md5_digest},
|
||||
{"copy", md5_copy},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
static object *
|
||||
md5_getattr(self, name)
|
||||
md5object *self;
|
||||
char *name;
|
||||
{
|
||||
return findmethod(md5_methods, (object *)self, name);
|
||||
} /* md5_getattr() */
|
||||
|
||||
|
||||
static typeobject MD5type = {
|
||||
OB_HEAD_INIT(&Typetype)
|
||||
0, /*ob_size*/
|
||||
"md5", /*tp_name*/
|
||||
sizeof(md5object), /*tp_size*/
|
||||
0, /*tp_itemsize*/
|
||||
/* methods */
|
||||
md5_dealloc, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
md5_getattr, /*tp_getattr*/
|
||||
0, /*tp_setattr*/
|
||||
0, /*tp_compare*/
|
||||
0, /*tp_repr*/
|
||||
0, /*tp_as_number*/
|
||||
};
|
||||
|
||||
/* List of functions exported by this module */
|
||||
|
||||
static struct methodlist md5_functions[] = {
|
||||
{initialiser_name, MD5_md5},
|
||||
{NULL, NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
|
||||
/* Initialize this module. */
|
||||
|
||||
void
|
||||
initmd5()
|
||||
{
|
||||
#ifdef MD5_DEBUG
|
||||
fputs( "initmd5() called...\n", stderr );
|
||||
#endif /* def MD5_DEBUG */
|
||||
(void)initmodule("md5", md5_functions);
|
||||
} /* initmd5() */
|
||||
|
||||
#ifdef MAKEDUMMYINT
|
||||
int _md5_dummy_int; /* XXX otherwise, we're .bss-less (DYNLOAD->Jack?) */
|
||||
#endif /* def MAKEDUMMYINT */
|
1808
Modules/mpzmodule.c
Normal file
1808
Modules/mpzmodule.c
Normal file
File diff suppressed because it is too large
Load Diff
@ -975,6 +975,10 @@ socket_fromfd(self, args)
|
||||
if (!getargs(args, "(iiii)", &fd, &family, &type, &proto))
|
||||
return NULL;
|
||||
}
|
||||
/* Dup the fd so it and the socket can be closed independently */
|
||||
fd = dup(fd);
|
||||
if (fd < 0)
|
||||
return socket_error();
|
||||
s = newsockobject(fd, family, type, proto);
|
||||
/* From now on, ignore SIGPIPE and let the error checking
|
||||
do the work. */
|
||||
|
@ -790,6 +790,8 @@ eval_code(co, globals, locals, arg)
|
||||
(a) f(a,b,...) should accept f((1,2,...))
|
||||
(b) f((a,b,...)) should accept f(1,2,...)
|
||||
(c) f(self,(a,b,...)) should accept f(x,1,2,...)
|
||||
Actually, (c) is dangerous, and (b) seems
|
||||
unnecessary, but (a) can't be missed...
|
||||
*/
|
||||
{
|
||||
int n;
|
||||
@ -817,6 +819,7 @@ eval_code(co, globals, locals, arg)
|
||||
n = gettuplesize(v);
|
||||
}
|
||||
}
|
||||
#if 0 /* Compatibility hacks no longer needed (I think) */
|
||||
else if (n != 1 && oparg == 1) {
|
||||
/* Rule (b) */
|
||||
PUSH(v);
|
||||
@ -848,6 +851,7 @@ eval_code(co, globals, locals, arg)
|
||||
v = u;
|
||||
n = 2;
|
||||
}
|
||||
#endif /* Disabled compatibility hacks */
|
||||
if (n != oparg) {
|
||||
err_setstr(TypeError,
|
||||
"arg count mismatch");
|
||||
|
Loading…
Reference in New Issue
Block a user