Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f59d60

Browse files
committed
* 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().
1 parent 8de83e0 commit 5f59d60

6 files changed

Lines changed: 2176 additions & 4 deletions

File tree

Include/mymalloc.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5353
#define NULL 0
5454
#endif
5555

56-
#define NEW(type, n) ( (type *) malloc((n) * sizeof(type)) )
56+
/* XXX Always allocate one extra byte, since some malloc's return NULL
57+
XXX for malloc(0) or realloc(p, 0). */
58+
#define NEW(type, n) ( (type *) malloc(1 + (n) * sizeof(type)) )
5759
#define RESIZE(p, type, n) \
5860
if ((p) == NULL) \
59-
(p) = (type *) malloc((n) * sizeof(type)); \
61+
(p) = (type *) malloc(1 + (n) * sizeof(type)); \
6062
else \
61-
(p) = (type *) realloc((ANY *)(p), (n) * sizeof(type))
63+
(p) = (type *) realloc((ANY *)(p), 1 + (n) * sizeof(type))
6264
#define DEL(p) free((ANY *)p)
6365
#define XDEL(p) if ((p) == NULL) ; else DEL(p)
6466

Modules/imageop.c

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ imageop_crop(self, args)
6262
xstep = (newx1 < newx2)? 1 : -1;
6363
ystep = (newy1 < newy2)? 1 : -1;
6464

65-
rv = newsizedstringobject(NULL, (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size);
65+
rv = newsizedstringobject(NULL,
66+
(abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size);
6667
if ( rv == 0 )
6768
return 0;
6869
ncp = (char *)getstringvalue(rv);
@@ -123,6 +124,134 @@ imageop_scale(self, args)
123124
return rv;
124125
}
125126

127+
static object *
128+
imageop_grey2mono(self, args)
129+
object *self;
130+
object *args;
131+
{
132+
int tres, x, y, len;
133+
unsigned char *cp, *ncp;
134+
unsigned char ovalue;
135+
object *rv;
136+
int i, bit;
137+
138+
139+
if ( !getargs(args, "(s#iii)", &cp, &len, &x, &y, &tres) )
140+
return 0;
141+
142+
if ( x*y != len ) {
143+
err_setstr(ImageopError, "String has incorrect length");
144+
return 0;
145+
}
146+
147+
rv = newsizedstringobject(NULL, (len+7)/8);
148+
if ( rv == 0 )
149+
return 0;
150+
ncp = (unsigned char *)getstringvalue(rv);
151+
152+
bit = 0x80;
153+
ovalue = 0;
154+
for ( i=0; i < len; i++ ) {
155+
if ( cp[i] > tres )
156+
ovalue |= bit;
157+
bit >>= 1;
158+
if ( bit == 0 ) {
159+
*ncp++ = ovalue;
160+
bit = 0x80;
161+
ovalue = 0;
162+
}
163+
}
164+
if ( bit != 0x80 )
165+
*ncp++ = ovalue;
166+
return rv;
167+
}
168+
169+
static object *
170+
imageop_dither2mono(self, args)
171+
object *self;
172+
object *args;
173+
{
174+
int sum, x, y, len;
175+
unsigned char *cp, *ncp;
176+
unsigned char ovalue;
177+
object *rv;
178+
int i, bit;
179+
180+
181+
if ( !getargs(args, "(s#ii)", &cp, &len, &x, &y) )
182+
return 0;
183+
184+
if ( x*y != len ) {
185+
err_setstr(ImageopError, "String has incorrect length");
186+
return 0;
187+
}
188+
189+
rv = newsizedstringobject(NULL, (len+7)/8);
190+
if ( rv == 0 )
191+
return 0;
192+
ncp = (unsigned char *)getstringvalue(rv);
193+
194+
bit = 0x80;
195+
ovalue = 0;
196+
sum = 0;
197+
for ( i=0; i < len; i++ ) {
198+
sum += cp[i];
199+
if ( sum >= 256 ) {
200+
sum -= 256;
201+
ovalue |= bit;
202+
}
203+
bit >>= 1;
204+
if ( bit == 0 ) {
205+
*ncp++ = ovalue;
206+
bit = 0x80;
207+
ovalue = 0;
208+
}
209+
}
210+
if ( bit != 0x80 )
211+
*ncp++ = ovalue;
212+
return rv;
213+
}
214+
215+
static object *
216+
imageop_mono2grey(self, args)
217+
object *self;
218+
object *args;
219+
{
220+
int v0, v1, x, y, len, nlen;
221+
unsigned char *cp, *ncp;
222+
unsigned char ovalue;
223+
object *rv;
224+
int i, bit, value;
225+
226+
if ( !getargs(args, "(s#iiii)", &cp, &len, &x, &y, &v0, &v1) )
227+
return 0;
228+
229+
nlen = x*y;
230+
if ( (nlen+7)/8 != len ) {
231+
err_setstr(ImageopError, "String has incorrect length");
232+
return 0;
233+
}
234+
235+
rv = newsizedstringobject(NULL, nlen);
236+
if ( rv == 0 )
237+
return 0;
238+
ncp = (unsigned char *)getstringvalue(rv);
239+
240+
bit = 0x80;
241+
for ( i=0; i < nlen; i++ ) {
242+
if ( *cp & bit )
243+
*ncp++ = v1;
244+
else
245+
*ncp++ = v0;
246+
bit >>= 1;
247+
if ( bit == 0 ) {
248+
bit = 0x80;
249+
cp++;
250+
}
251+
}
252+
return rv;
253+
}
254+
126255
/*
127256
static object *
128257
imageop_mul(self, args)
@@ -161,6 +290,9 @@ imageop_mul(self, args)
161290
static struct methodlist imageop_methods[] = {
162291
{ "crop", imageop_crop },
163292
{ "scale", imageop_scale },
293+
{ "grey2mono", imageop_grey2mono },
294+
{ "dither2mono", imageop_dither2mono },
295+
{ "mono2grey", imageop_mono2grey },
164296
{ 0, 0 }
165297
};
166298

Modules/md5module.c

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/***********************************************************
2+
Copyright 1992 by Stichting Mathematisch Centrum, Amsterdam, The
3+
Netherlands.
4+
5+
All Rights Reserved
6+
7+
Permission to use, copy, modify, and distribute this software and its
8+
documentation for any purpose and without fee is hereby granted,
9+
provided that the above copyright notice appear in all copies and that
10+
both that copyright notice and this permission notice appear in
11+
supporting documentation, and that the names of Stichting Mathematisch
12+
Centrum or CWI not be used in advertising or publicity pertaining to
13+
distribution of the software without specific, written prior permission.
14+
15+
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16+
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17+
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18+
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21+
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22+
23+
******************************************************************/
24+
/* MD5 module */
25+
26+
/* This module provides an interface to a message digest algorithm,
27+
MD5 in this case */
28+
29+
/* MD5 objects */
30+
31+
#include "allobjects.h"
32+
#include "modsupport.h" /* For getargs() etc. */
33+
34+
#include "md5.h"
35+
typedef struct {
36+
OB_HEAD
37+
MD5_CTX md5; /* the context holder */
38+
} md5object;
39+
40+
extern typeobject MD5type; /* Really static, forward */
41+
42+
#define is_md5object(v) ((v)->ob_type == &MD5type)
43+
44+
static const char initialiser_name[] = "md5";
45+
46+
/* #define MD5_DEBUG */
47+
48+
static md5object *
49+
newmd5object()
50+
{
51+
md5object *md5p;
52+
53+
54+
#ifdef MD5_DEBUG
55+
fputs( "md5_object() called...\n", stderr );
56+
#endif /* def MD5_DEBUG */
57+
md5p = NEWOBJ(md5object, &MD5type);
58+
if (md5p == NULL)
59+
return NULL;
60+
61+
MD5Init(&md5p->md5); /* actual initialisation */
62+
return md5p;
63+
} /* newmd5object() */
64+
65+
66+
/* MD5 methods */
67+
68+
static void
69+
md5_dealloc(md5p)
70+
md5object *md5p;
71+
{
72+
#ifdef MD5_DEBUG
73+
fputs( "md5_dealloc() called...\n", stderr );
74+
#endif /* def MD5_DEBUG */
75+
76+
DEL(md5p);
77+
} /* md5_dealloc() */
78+
79+
80+
/* MD5 initialisation */
81+
82+
static object *
83+
MD5_md5(self, args)
84+
object *self;
85+
object *args;
86+
{
87+
md5object *md5p;
88+
char *cp = (char *)NULL;
89+
int len;
90+
91+
92+
#ifdef MD5_DEBUG
93+
fputs("MD5_md5() called...\n", stderr);
94+
#endif /* def MD5_DEBUG */
95+
96+
if (!getargs(args, "")) {
97+
err_clear();
98+
if (!getargs(args, "s#", &cp, &len))
99+
return NULL;
100+
}
101+
102+
if ((md5p = newmd5object()) == NULL)
103+
return NULL;
104+
105+
if (cp)
106+
MD5Update(&md5p->md5, cp, len);
107+
108+
return (object *)md5p;
109+
} /* MD5_md5() */
110+
111+
112+
/* MD5 methods-as-attributes */
113+
static object *
114+
md5_update(self, args)
115+
md5object *self;
116+
object *args;
117+
{
118+
char *cp;
119+
int len;
120+
121+
122+
if (!getargs(args, "s#", &cp, &len))
123+
return NULL;
124+
125+
MD5Update(&self->md5, cp, len);
126+
127+
INCREF(None);
128+
return None;
129+
} /* md5_update() */
130+
131+
static object *
132+
md5_digest(self, args)
133+
md5object *self;
134+
object *args;
135+
{
136+
MD5_CTX mdContext;
137+
stringobject *strobjp;
138+
139+
if (!getnoarg(args))
140+
return NULL;
141+
142+
/* make a temporary copy, and perform the final */
143+
mdContext = self->md5;
144+
MD5Final(&mdContext);
145+
146+
return newsizedstringobject((char *)mdContext.digest, 16);
147+
} /* md5_digest() */
148+
149+
static object *
150+
md5_copy(self, args)
151+
md5object *self;
152+
object *args;
153+
{
154+
md5object *md5p;
155+
156+
157+
if (!getnoarg(args))
158+
return NULL;
159+
160+
if ((md5p = newmd5object()) == NULL)
161+
return NULL;
162+
163+
md5p->md5 = self->md5;
164+
165+
return (object *)md5p;
166+
} /* md5_copy() */
167+
168+
169+
static struct methodlist md5_methods[] = {
170+
{"update", md5_update},
171+
{"digest", md5_digest},
172+
{"copy", md5_copy},
173+
{NULL, NULL} /* sentinel */
174+
};
175+
176+
static object *
177+
md5_getattr(self, name)
178+
md5object *self;
179+
char *name;
180+
{
181+
return findmethod(md5_methods, (object *)self, name);
182+
} /* md5_getattr() */
183+
184+
185+
static typeobject MD5type = {
186+
OB_HEAD_INIT(&Typetype)
187+
0, /*ob_size*/
188+
"md5", /*tp_name*/
189+
sizeof(md5object), /*tp_size*/
190+
0, /*tp_itemsize*/
191+
/* methods */
192+
md5_dealloc, /*tp_dealloc*/
193+
0, /*tp_print*/
194+
md5_getattr, /*tp_getattr*/
195+
0, /*tp_setattr*/
196+
0, /*tp_compare*/
197+
0, /*tp_repr*/
198+
0, /*tp_as_number*/
199+
};
200+
201+
/* List of functions exported by this module */
202+
203+
static struct methodlist md5_functions[] = {
204+
{initialiser_name, MD5_md5},
205+
{NULL, NULL} /* Sentinel */
206+
};
207+
208+
209+
/* Initialize this module. */
210+
211+
void
212+
initmd5()
213+
{
214+
#ifdef MD5_DEBUG
215+
fputs( "initmd5() called...\n", stderr );
216+
#endif /* def MD5_DEBUG */
217+
(void)initmodule("md5", md5_functions);
218+
} /* initmd5() */
219+
220+
#ifdef MAKEDUMMYINT
221+
int _md5_dummy_int; /* XXX otherwise, we're .bss-less (DYNLOAD->Jack?) */
222+
#endif /* def MAKEDUMMYINT */

0 commit comments

Comments
 (0)