improve treatment of multi-line replies, ignore empty lines
[python/dscho.git] / Modules / md5module.c
blobbb98cc229d280a1d0b63458ec74129d1f4661eee
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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.
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.
23 ******************************************************************/
25 /* MD5 module */
27 /* This module provides an interface to the RSA Data Security,
28 Inc. MD5 Message-Digest Algorithm, described in RFC 1321.
29 It requires the files md5c.c and md5.h (which are slightly changed
30 from the versions in the RFC to avoid the "global.h" file.) */
33 /* MD5 objects */
35 #include "allobjects.h"
36 #include "modsupport.h"
38 #include "md5.h"
40 typedef struct {
41 OB_HEAD
42 MD5_CTX md5; /* the context holder */
43 } md5object;
45 staticforward typeobject MD5type;
47 #define is_md5object(v) ((v)->ob_type == &MD5type)
49 static md5object *
50 newmd5object()
52 md5object *md5p;
54 md5p = NEWOBJ(md5object, &MD5type);
55 if (md5p == NULL)
56 return NULL;
58 MD5Init(&md5p->md5); /* actual initialisation */
59 return md5p;
63 /* MD5 methods */
65 static void
66 md5_dealloc(md5p)
67 md5object *md5p;
69 DEL(md5p);
73 /* MD5 methods-as-attributes */
75 static object *
76 md5_update(self, args)
77 md5object *self;
78 object *args;
80 unsigned char *cp;
81 int len;
83 if (!getargs(args, "s#", &cp, &len))
84 return NULL;
86 MD5Update(&self->md5, cp, len);
88 INCREF(None);
89 return None;
92 static object *
93 md5_digest(self, args)
94 md5object *self;
95 object *args;
98 MD5_CTX mdContext;
99 unsigned char aDigest[16];
101 if (!getnoarg(args))
102 return NULL;
104 /* make a temporary copy, and perform the final */
105 mdContext = self->md5;
106 MD5Final(aDigest, &mdContext);
108 return newsizedstringobject((char *)aDigest, 16);
111 static object *
112 md5_copy(self, args)
113 md5object *self;
114 object *args;
116 md5object *md5p;
118 if (!getnoarg(args))
119 return NULL;
121 if ((md5p = newmd5object()) == NULL)
122 return NULL;
124 md5p->md5 = self->md5;
126 return (object *)md5p;
129 static struct methodlist md5_methods[] = {
130 {"update", (method)md5_update},
131 {"digest", (method)md5_digest},
132 {"copy", (method)md5_copy},
133 {NULL, NULL} /* sentinel */
136 static object *
137 md5_getattr(self, name)
138 md5object *self;
139 char *name;
141 return findmethod(md5_methods, (object *)self, name);
144 static typeobject MD5type = {
145 OB_HEAD_INIT(&Typetype)
146 0, /*ob_size*/
147 "md5", /*tp_name*/
148 sizeof(md5object), /*tp_size*/
149 0, /*tp_itemsize*/
150 /* methods */
151 (destructor)md5_dealloc, /*tp_dealloc*/
152 0, /*tp_print*/
153 (getattrfunc)md5_getattr, /*tp_getattr*/
154 0, /*tp_setattr*/
155 0, /*tp_compare*/
156 0, /*tp_repr*/
157 0, /*tp_as_number*/
161 /* MD5 functions */
163 static object *
164 MD5_new(self, args)
165 object *self;
166 object *args;
168 md5object *md5p;
169 unsigned char *cp = NULL;
170 int len;
172 if (!getargs(args, "")) {
173 err_clear();
174 if (!getargs(args, "s#", &cp, &len))
175 return NULL;
178 if ((md5p = newmd5object()) == NULL)
179 return NULL;
181 if (cp)
182 MD5Update(&md5p->md5, cp, len);
184 return (object *)md5p;
188 /* List of functions exported by this module */
190 static struct methodlist md5_functions[] = {
191 {"new", (method)MD5_new},
192 {"md5", (method)MD5_new}, /* Backward compatibility */
193 {NULL, NULL} /* Sentinel */
197 /* Initialize this module. */
199 void
200 initmd5()
202 (void)initmodule("md5", md5_functions);