M4p.xs

filter: Statementstmt 74.6% 183 / 245 Branchbran 27.6% 21 / 76 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 63.5% 204 / 321 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Digest/MD5/M4p.pm ↑ Summary
1 /* M4p.xs version 0.01 */
2
3 /*
4 * This library is free software; you can redistribute it and/or
5 * modify it under the same terms as Perl itself.
6 *
7 * From observation of iTunes implementations.
8 *
9 * Copyright 2004 William Herrera.
10 * Copyright 1998-2000 Gisle Aas.
11 * Copyright 1995-1996 Neil Winton.
12 * Copyright 1991-1992 RSA Data Security, Inc.
13 *
14 * This code is shamelessly copied from Gisle Aas's Digest::MD5 code,
15 * which in turn is derived from Neil Winton's MD5-1.7 Perl module, which in
16 * turn is derived from the reference implementation in RFC 1321, which is
17 * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
18 * rights reserved.
19 *
20 * License to copy and use this software is granted provided that it
21 * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
22 * Algorithm" in all material mentioning or referencing this software
23 * or this function.
24 *
25 * License is also granted to make and use derivative works provided
26 * that such works are identified as "derived from the RSA Data
27 * Security, Inc. MD5 Message-Digest Algorithm" in all material
28 * mentioning or referencing the derived work.
29 *
30 * RSA Data Security, Inc. makes no representations concerning either
31 * the merchantability of this software or the suitability of this
32 * software for any particular purpose. It is provided "as is"
33 * without express or implied warranty of any kind.
34 *
35 * These notices must be retained in any copies of any part of this
36 * documentation and/or software.
37 */
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 #define PERL_NO_GET_CONTEXT /* we want efficiency */
43 #include "EXTERN.h"
44 #include "perl.h"
45 #include "XSUB.h"
46 #ifdef __cplusplus
47 }
48 #endif
49
50 #ifndef PERL_VERSION
51 # include <patchlevel.h>
52 # if !(defined(PERL_VERSION) || (SUBVERSION > 0 && defined(PATCHLEVEL)))
53 # include <could_not_find_Perl_patchlevel.h>
54 # endif
55 # define PERL_REVISION 5
56 # define PERL_VERSION PATCHLEVEL
57 # define PERL_SUBVERSION SUBVERSION
58 #endif
59
60 #if PERL_VERSION <= 4 && !defined(PL_dowarn)
61 #define PL_dowarn dowarn
62 #endif
63
64 #ifdef G_WARN_ON
65 #define DOWARN (PL_dowarn & G_WARN_ON)
66 #else
67 #define DOWARN PL_dowarn
68 #endif
69
70 #ifdef SvPVbyte
71 #if PERL_REVISION == 5 && PERL_VERSION < 7
72 /* SvPVbyte does not work in perl-5.6.1, borrowed version for 5.7.3 */
73 #undef SvPVbyte
74 #define SvPVbyte(sv, lp) \
75 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK) \
76 ? ((lp = SvCUR(sv)), SvPVX(sv)) : my_sv_2pvbyte(aTHX_ sv, &lp))
77
78 static char *
79 my_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
80 {
81 sv_utf8_downgrade(sv,0);
82 return SvPV(sv,*lp);
83 }
84 #endif
85 #else
86 #define SvPVbyte SvPV
87 #endif
88
89 #ifndef dTHX
90 #define pTHX_
91 #define aTHX_
92 #endif
93
94 /* Perl does not guarantee that U32 is exactly 32 bits. Some system
95 * has no integral type with exactly 32 bits. For instance, A Cray has
96 * short, int and long all at 64 bits so we need to apply this macro
97 * to reduce U32 values to 32 bits at appropriate places. If U32
98 * really does have 32 bits then this is a no-op.
99 */
100 #if BYTEORDER > 0x4321 || defined(TRUNCATE_U32)
101 #define TO32(x) ((x) & 0xFFFFffff)
102 #define TRUNC32(x) ((x) &= 0xFFFFffff)
103 #else
104 #define TO32(x) (x)
105 #define TRUNC32(x) /*nothing*/
106 #endif
107
108 /* The MD5 algorithm is defined in terms of little endian 32-bit
109 * values. The following macros (and functions) allow us to convert
110 * between native integers and such values.
111 */
112 #undef BYTESWAP
113 #ifndef U32_ALIGNMENT_REQUIRED
114 #if BYTEORDER == 0x1234 /* 32-bit little endian */
115 #define BYTESWAP(x) (x) /* no-op */
116
117 #elif BYTEORDER == 0x4321 /* 32-bit big endian */
118 #define BYTESWAP(x) ((((x)&0xFF)<<24) \
119 |(((x)>>24)&0xFF) \
120 |(((x)&0x0000FF00)<<8) \
121 |(((x)&0x00FF0000)>>8) )
122 #endif
123 #endif
124
125 #ifndef BYTESWAP
126 60 static void u2s(U32 u, U8* s)
127 {
128 60 *s++ = (U8)(u & 0xFF);
129 60 *s++ = (U8)((u >> 8) & 0xFF);
130 60 *s++ = (U8)((u >> 16) & 0xFF);
131 60 *s = (U8)((u >> 24) & 0xFF);
132 60 }
133
134 #define s2u(s,u) ((u) = (U32)(*s) | \
135 ((U32)(*(s+1)) << 8) | \
136 ((U32)(*(s+2)) << 16) | \
137 ((U32)(*(s+3)) << 24))
138 #endif
139
140 #define MD5_CTX_SIGNATURE 200003165
141
142 /* this defines us to be at modification level 1 */
143 #define AppleModLevel 1
144
145 /* This stucture keeps the current state of algorithm.
146 */
147 typedef struct {
148 U32 signature; /* safer cast in get_md5_ctx() */
149 U32 A, B, C, D; /* current digest */
150 U32 bytes_low; /* counts bytes in message */
151 U32 bytes_high; /* turn it into a 64-bit counter */
152 U8 buffer[128]; /* collect complete 64 byte blocks */
153 } MD5_CTX;
154
155
156 /* Padding is added at the end of the message in order to fill a
157 * complete 64 byte block (- 8 bytes for the message length). The
158 * padding is also the reason the buffer in MD5_CTX have to be
159 * 128 bytes.
160 */
161 static unsigned char PADDING[64] = {
162 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
165 };
166
167 /* Constants for MD5Transform routine.
168 */
169 #define S11 7
170 #define S12 12
171 #define S13 17
172 #define S14 22
173 #define S21 5
174 #define S22 9
175 #define S23 14
176 #define S24 20
177 #define S31 4
178 #define S32 11
179 #define S33 16
180 #define S34 23
181 #define S41 6
182 #define S42 10
183 #define S43 15
184 #define S44 21
185
186 /* F, G, H and I are basic MD5 functions.
187 */
188 #define F(x, y, z) ((((x) & ((y) ^ (z))) ^ (z)))
189 #define G(x, y, z) F(z, x, y)
190 #define H(x, y, z) ((x) ^ (y) ^ (z))
191 #define I(x, y, z) ((y) ^ ((x) | (~z)))
192
193 /* ROTATE_LEFT rotates x left n bits.
194 */
195 #define ROTATE_LEFT(x, n) (((x) << (n) | ((x) >> (32-(n)))))
196
197 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
198 * Rotation is separate from addition to prevent recomputation.
199 */
200 #define FF(a, b, c, d, s, ac) \
201 (a) += F ((b), (c), (d)) + (NEXTx) + (U32)(ac); \
202 TRUNC32((a)); \
203 (a) = ROTATE_LEFT ((a), (s)); \
204 (a) += (b); \
205 TRUNC32((a));
206
207 #define GG(a, b, c, d, x, s, ac) \
208 (a) += G ((b), (c), (d)) + X[x] + (U32)(ac); \
209 TRUNC32((a)); \
210 (a) = ROTATE_LEFT ((a), (s)); \
211 (a) += (b); \
212 TRUNC32((a));
213
214 #define HH(a, b, c, d, x, s, ac) \
215 (a) += H ((b), (c), (d)) + X[x] + (U32)(ac); \
216 TRUNC32((a)); \
217 (a) = ROTATE_LEFT ((a), (s)); \
218 (a) += (b); \
219 TRUNC32((a));
220
221 #define II(a, b, c, d, x, s, ac) \
222 (a) += I ((b), (c), (d)) + X[x] + (U32)(ac); \
223 TRUNC32((a)); \
224 (a) = ROTATE_LEFT ((a), (s)); \
225 (a) += (b); \
226 TRUNC32((a));
227
228
229 static void
230 11 MD5Init(MD5_CTX *ctx)
231 {
232 /* Start state */
233 11 ctx->A = 0x67452301;
234 11 ctx->B = 0xefcdab89;
235 11 ctx->C = 0x98badcfe;
236 11 ctx->D = 0x10325476;
237
238 /* message length */
239 11 ctx->bytes_low = ctx->bytes_high = 0;
240 11 }
241
242
243 static void
244 10 MD5Transform(MD5_CTX* ctx, const U8* buf, STRLEN blocks)
245 {
246 #ifdef MD5_DEBUG
247 static int tcount = 0;
248 #endif
249
250 10 U32 A = ctx->A;
251 10 U32 B = ctx->B;
252 10 U32 C = ctx->C;
253 10 U32 D = ctx->D;
254
255 #ifndef U32_ALIGNMENT_REQUIRED
256 const U32 *x = (U32*)buf; /* really just type casting */
257 #endif
258
259 do {
260 10 U32 a = A;
261 10 U32 b = B;
262 10 U32 c = C;
263 10 U32 d = D;
264
265 #if BYTEORDER == 0x1234 && !defined(U32_ALIGNMENT_REQUIRED)
266 const U32 *X = x;
267 #define NEXTx (*x++)
268 #else
269 U32 X[16]; /* converted values, used in round 2-4 */
270 10 U32 *uptr = X;
271 U32 tmp;
272 #ifdef BYTESWAP
273 #define NEXTx (tmp=*x++, *uptr++ = BYTESWAP(tmp))
274 #else
275 #define NEXTx (s2u(buf,tmp), buf += 4, *uptr++ = tmp)
276 #endif
277 #endif
278
279 #ifdef MD5_DEBUG
280 if (buf == ctx->buffer)
281 fprintf(stderr,"%5d: Transform ctx->buffer", ++tcount);
282 else
283 fprintf(stderr,"%5d: Transform %p (%d)", ++tcount, buf, blocks);
284
285 {
286 int i;
287 fprintf(stderr,"[");
288 for (i = 0; i < 16; i++) {
289 fprintf(stderr,"%x,", x[i]);
290 }
291 fprintf(stderr,"]\n");
292 }
293 #endif
294
295 /* Round 1 */
296 10 FF (a, b, c, d, S11, 0xd76aa478); /* 1 */
297 10 FF (d, a, b, c, S12, 0xe8c7b756); /* 2 */
298 10 FF (c, d, a, b, S13, 0x242070db); /* 3 */
299 10 FF (b, c, d, a, S14, 0xc1bdceee); /* 4 */
300 10 FF (a, b, c, d, S11, 0xf57c0faf); /* 5 */
301 10 FF (d, a, b, c, S12, 0x4787c62a); /* 6 */
302 10 FF (c, d, a, b, S13, 0xa8304613); /* 7 */
303 10 FF (b, c, d, a, S14, 0xfd469501); /* 8 */
304 10 FF (a, b, c, d, S11, 0x698098d8); /* 9 */
305 10 FF (d, a, b, c, S12, 0x8b44f7af); /* 10 */
306 10 FF (c, d, a, b, S13, 0xffff5bb1); /* 11 */
307 10 FF (b, c, d, a, S14, 0x895cd7be); /* 12 */
308 10 FF (a, b, c, d, S11, 0x6b901122); /* 13 */
309 10 FF (d, a, b, c, S12, 0xfd987193); /* 14 */
310 10 FF (c, d, a, b, S13, 0xa679438e); /* 15 */
311 10 FF (b, c, d, a, S14, 0x49b40821); /* 16 */
312
313 /* Round 2 */
314 10 GG (a, b, c, d, 1, S21, 0xf61e2562); /* 17 */
315 10 GG (d, a, b, c, 6, S22, 0xc040b340); /* 18 */
316 10 GG (c, d, a, b, 11, S23, 0x265e5a51); /* 19 */
317 10 GG (b, c, d, a, 0, S24, 0xe9b6c7aa); /* 20 */
318 10 GG (a, b, c, d, 5, S21, 0xd62f105d); /* 21 */
319 10 GG (d, a, b, c, 10, S22, 0x2441453); /* 22 */
320 10 GG (c, d, a, b, 15, S23, 0xd8a1e681); /* 23 */
321 10 GG (b, c, d, a, 4, S24, 0xe7d3fbc8); /* 24 */
322 10 GG (a, b, c, d, 9, S21, 0x21e1cde6); /* 25 */
323 10 GG (d, a, b, c, 14, S22, 0xc33707d6); /* 26 */
324 10 GG (c, d, a, b, 3, S23, 0xf4d50d87); /* 27 */
325
326 /* begin apple md5 modification type 1 */
327 /* Apple does this to make it hard for us ordinary mortals */
328 if(AppleModLevel == 1)
329 {
330 10 GG (b, c, d, a, 8, S24, 0x445a14ed); /* 28 */
331 }
332 else /* real MD5 */
333 {
334 GG (b, c, d, a, 8, S24, 0x455a14ed); /* 28 */
335 }
336 /* end apple md5 modification type 1, hopefully there will be no part 2 etc */
337
338 10 GG (a, b, c, d, 13, S21, 0xa9e3e905); /* 29 */
339 10 GG (d, a, b, c, 2, S22, 0xfcefa3f8); /* 30 */
340 10 GG (c, d, a, b, 7, S23, 0x676f02d9); /* 31 */
341 10 GG (b, c, d, a, 12, S24, 0x8d2a4c8a); /* 32 */
342
343 /* Round 3 */
344 10 HH (a, b, c, d, 5, S31, 0xfffa3942); /* 33 */
345 10 HH (d, a, b, c, 8, S32, 0x8771f681); /* 34 */
346 10 HH (c, d, a, b, 11, S33, 0x6d9d6122); /* 35 */
347 10 HH (b, c, d, a, 14, S34, 0xfde5380c); /* 36 */
348 10 HH (a, b, c, d, 1, S31, 0xa4beea44); /* 37 */
349 10 HH (d, a, b, c, 4, S32, 0x4bdecfa9); /* 38 */
350 10 HH (c, d, a, b, 7, S33, 0xf6bb4b60); /* 39 */
351 10 HH (b, c, d, a, 10, S34, 0xbebfbc70); /* 40 */
352 10 HH (a, b, c, d, 13, S31, 0x289b7ec6); /* 41 */
353 10 HH (d, a, b, c, 0, S32, 0xeaa127fa); /* 42 */
354 10 HH (c, d, a, b, 3, S33, 0xd4ef3085); /* 43 */
355 10 HH (b, c, d, a, 6, S34, 0x4881d05); /* 44 */
356 10 HH (a, b, c, d, 9, S31, 0xd9d4d039); /* 45 */
357 10 HH (d, a, b, c, 12, S32, 0xe6db99e5); /* 46 */
358 10 HH (c, d, a, b, 15, S33, 0x1fa27cf8); /* 47 */
359 10 HH (b, c, d, a, 2, S34, 0xc4ac5665); /* 48 */
360
361 /* Round 4 */
362 10 II (a, b, c, d, 0, S41, 0xf4292244); /* 49 */
363 10 II (d, a, b, c, 7, S42, 0x432aff97); /* 50 */
364 10 II (c, d, a, b, 14, S43, 0xab9423a7); /* 51 */
365 10 II (b, c, d, a, 5, S44, 0xfc93a039); /* 52 */
366 10 II (a, b, c, d, 12, S41, 0x655b59c3); /* 53 */
367 10 II (d, a, b, c, 3, S42, 0x8f0ccc92); /* 54 */
368 10 II (c, d, a, b, 10, S43, 0xffeff47d); /* 55 */
369 10 II (b, c, d, a, 1, S44, 0x85845dd1); /* 56 */
370 10 II (a, b, c, d, 8, S41, 0x6fa87e4f); /* 57 */
371 10 II (d, a, b, c, 15, S42, 0xfe2ce6e0); /* 58 */
372 10 II (c, d, a, b, 6, S43, 0xa3014314); /* 59 */
373 10 II (b, c, d, a, 13, S44, 0x4e0811a1); /* 60 */
374 10 II (a, b, c, d, 4, S41, 0xf7537e82); /* 61 */
375 10 II (d, a, b, c, 11, S42, 0xbd3af235); /* 62 */
376 10 II (c, d, a, b, 2, S43, 0x2ad7d2bb); /* 63 */
377 10 II (b, c, d, a, 9, S44, 0xeb86d391); /* 64 */
378
379 10 A += a; TRUNC32(A);
380 10 B += b; TRUNC32(B);
381 10 C += c; TRUNC32(C);
382 10 D += d; TRUNC32(D);
383
384 10 ▶ } while (--blocks);
Branch: 1 branch
BranchTrueFalseTotal
} while (--blocks); 0 10 10
385 10 ctx->A = A;
386 10 ctx->B = B;
387 10 ctx->C = C;
388 10 ctx->D = D;
389 10 }
390
391
392 #ifdef MD5_DEBUG
393 static char*
394 ctx_dump(MD5_CTX* ctx)
395 {
396 static char buf[1024];
397 sprintf(buf, "{A=%x,B=%x,C=%x,D=%x,%d,%d(%d)}",
398 ctx->A, ctx->B, ctx->C, ctx->D,
399 ctx->bytes_low, ctx->bytes_high, (ctx->bytes_low&0x3F));
400 return buf;
401 }
402 #endif
403
404
405 static void
406 9 MD5Update(MD5_CTX* ctx, const U8* buf, STRLEN len)
407 {
408 STRLEN blocks;
409 9 STRLEN fill = ctx->bytes_low & 0x3F;
410
411 #ifdef MD5_DEBUG
412 static int ucount = 0;
413 fprintf(stderr,"%5i: Update(%s, %p, %d)\n", ++ucount, ctx_dump(ctx),
414 buf, len);
415 #endif
416
417 9 ctx->bytes_low += len;
418 9 ▶ if (ctx->bytes_low < len) /* wrap around */
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->bytes_low < len) /* wrap around */ 0 9 9
419 0 ctx->bytes_high++;
420
421 9 ▶ if (fill) {
Branch: 1 branch
BranchTrueFalseTotal
if (fill) { 3 6 9
422 3 STRLEN missing = 64 - fill;
423 3 ▶ if (len < missing) {
Branch: 1 branch
BranchTrueFalseTotal
if (len < missing) { 3 0 3
424 3 Copy(buf, ctx->buffer + fill, len, U8);
425 3 return;
426 }
427 0 Copy(buf, ctx->buffer + fill, missing, U8);
428 0 MD5Transform(ctx, ctx->buffer, 1);
429 0 buf += missing;
430 0 len -= missing;
431 }
432
433 6 blocks = len >> 6;
434 6 ▶ if (blocks)
Branch: 1 branch
BranchTrueFalseTotal
if (blocks) 0 6 6
435 0 MD5Transform(ctx, buf, blocks);
436 6 ▶ if ( (len &= 0x3F)) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (len &= 0x3F)) { 5 1 6
437 5 Copy(buf + (blocks << 6), ctx->buffer, len, U8);
438 }
439 }
440
441
442 static void
443 10 MD5Final(U8* digest, MD5_CTX *ctx)
444 {
445 10 STRLEN fill = ctx->bytes_low & 0x3F;
446 10 ▶ STRLEN padlen = (fill < 56 ? 56 : 120) - fill;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN padlen = (fill < 56 ? 56 : 120) - fill; 10 0 10
447 U32 bits_low, bits_high;
448 #ifdef MD5_DEBUG
449 fprintf(stderr," Final: %s\n", ctx_dump(ctx));
450 #endif
451 10 Copy(PADDING, ctx->buffer + fill, padlen, U8);
452 10 fill += padlen;
453
454 10 bits_low = ctx->bytes_low << 3;
455 10 bits_high = (ctx->bytes_high << 3) | (ctx->bytes_low >> 29);
456 #ifdef BYTESWAP
457 *(U32*)(ctx->buffer + fill) = BYTESWAP(bits_low); fill += 4;
458 *(U32*)(ctx->buffer + fill) = BYTESWAP(bits_high); fill += 4;
459 #else
460 10 u2s(bits_low, ctx->buffer + fill); fill += 4;
461 10 u2s(bits_high, ctx->buffer + fill); fill += 4;
462 #endif
463
464 10 MD5Transform(ctx, ctx->buffer, fill >> 6);
465 #ifdef MD5_DEBUG
466 fprintf(stderr," Result: %s\n", ctx_dump(ctx));
467 #endif
468
469 #ifdef BYTESWAP
470 *(U32*)digest = BYTESWAP(ctx->A); digest += 4;
471 *(U32*)digest = BYTESWAP(ctx->B); digest += 4;
472 *(U32*)digest = BYTESWAP(ctx->C); digest += 4;
473 *(U32*)digest = BYTESWAP(ctx->D);
474 #else
475 10 u2s(ctx->A, digest);
476 10 u2s(ctx->B, digest+4);
477 10 u2s(ctx->C, digest+8);
478 10 u2s(ctx->D, digest+12);
479 #endif
480 10 }
481
482 #ifndef INT2PTR
483 #define INT2PTR(any,d) (any)(d)
484 #endif
485
486 18 static MD5_CTX* get_md5_ctx(pTHX_ SV* sv)
487 {
488 18 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 18 0 18
489 18 sv = SvRV(sv);
490 18 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 18 0 18
491 18 MD5_CTX* ctx = INT2PTR(MD5_CTX*, SvIV(sv));
492 18 ▶ if (ctx && ctx->signature == MD5_CTX_SIGNATURE) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx && ctx->signature == MD5_CTX_SIGNATURE) { 18 0 18
if (ctx && ctx->signature == MD5_CTX_SIGNATURE) { 18 0 18
493 18 return ctx;
494 }
495 }
496 }
497 0 croak("Not a reference to a Digest::MD5::M4p object");
498 return (MD5_CTX*)0; /* some compilers insist on a return value */
499 }
500
501
502 10 static char* hex_16(const unsigned char* from, char* to)
503 {
504 static char *hexdigits = "0123456789abcdef";
505 10 const unsigned char *end = from + 16;
506 10 char *d = to;
507
508 170 ▶ while (from < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (from < end) { 160 10 170
509 160 *d++ = hexdigits[(*from >> 4)];
510 160 *d++ = hexdigits[(*from & 0x0F)];
511 160 from++;
512 }
513 10 *d = '\0';
514 10 return to;
515 }
516
517 0 static char* base64_16(const unsigned char* from, char* to)
518 {
519 static char* base64 =
520 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
521 0 const unsigned char *end = from + 16;
522 unsigned char c1, c2, c3;
523 0 char *d = to;
524
525 while (1) {
526 0 c1 = *from++;
527 0 *d++ = base64[c1>>2];
528 0 ▶ if (from == end) {
Branch: 1 branch
BranchTrueFalseTotal
if (from == end) { 0 0 0
529 0 *d++ = base64[(c1 & 0x3) << 4];
530 0 break;
531 }
532 0 c2 = *from++;
533 0 c3 = *from++;
534 0 *d++ = base64[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)];
535 0 *d++ = base64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
536 0 *d++ = base64[c3 & 0x3F];
537 }
538 0 *d = '\0';
539 0 return to;
540 }
541
542 /* Formats */
543 #define F_BIN 0
544 #define F_HEX 1
545 #define F_B64 2
546
547 10 static SV* make_mortal_sv(pTHX_ const unsigned char *src, int type)
548 {
549 STRLEN len;
550 char result[33];
551 char *ret;
552
553 10 switch (type) {
554 0 case F_BIN:
555 0 ret = (char*)src;
556 0 len = 16;
557 0 break;
558 10 case F_HEX:
559 10 ret = hex_16(src, result);
560 10 len = 32;
561 10 break;
562 0 case F_B64:
563 0 ret = base64_16(src, result);
564 0 len = 22;
565 0 break;
566 0 default:
567 0 croak("Bad convertion type (%d)", type);
568 break;
569 }
570 10 return sv_2mortal(newSVpv(ret,len));
571 }
572
573
574 /********************************************************************/
575
576 typedef PerlIO* InputStream;
577
578 MODULE = Digest::MD5::M4p PACKAGE = Digest::MD5::M4p
579
580 PROTOTYPES: DISABLE
581
582 void
583 new(xclass)
584 SV* xclass
585 PREINIT:
586 MD5_CTX* context;
587 PPCODE:
588 1 ▶ if (!SvROK(xclass)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(xclass)) { 1 0 1
589 STRLEN my_na;
590 1 char *sclass = SvPV(xclass, my_na);
591 1 New(55, context, 1, MD5_CTX);
592 1 context->signature = MD5_CTX_SIGNATURE;
593 1 ST(0) = sv_newmortal();
594 1 sv_setref_pv(ST(0), sclass, (void*)context);
595 1 SvREADONLY_on(SvRV(ST(0)));
596 } else {
597 0 context = get_md5_ctx(aTHX_ xclass);
598 }
599 1 MD5Init(context);
600 1 XSRETURN(1);
601
602 void
603 clone(self)
604 SV* self
605 PREINIT:
606 4 MD5_CTX* cont = get_md5_ctx(aTHX_ self);
607 4 char *myname = sv_reftype(SvRV(self),TRUE);
608 MD5_CTX* context;
609 PPCODE:
610 STRLEN my_na;
611 4 New(55, context, 1, MD5_CTX);
612 4 ST(0) = sv_newmortal();
613 4 sv_setref_pv(ST(0), myname , (void*)context);
614 4 SvREADONLY_on(SvRV(ST(0)));
615 4 memcpy(context,cont,sizeof(MD5_CTX));
616 4 XSRETURN(1);
617
618 void
619 DESTROY(context)
620 MD5_CTX* context
621 CODE:
622 5 Safefree(context);
623
624 void
625 add(self, ...)
626 SV* self
627 PREINIT:
628 4 MD5_CTX* context = get_md5_ctx(aTHX_ self);
629 int i;
630 unsigned char *data;
631 STRLEN len;
632 PPCODE:
633 8 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 4 4 8
634 4 data = (unsigned char *)(SvPVbyte(ST(i), len));
635 4 MD5Update(context, data, len);
636 }
637 4 XSRETURN(1); /* self */
638
639 void
640 addfile(self, fh)
641 SV* self
642 InputStream fh
643 PREINIT:
644 0 MD5_CTX* context = get_md5_ctx(aTHX_ self);
645 0 STRLEN fill = context->bytes_low & 0x3F;
646 unsigned char buffer[4096];
647 int n;
648 CODE:
649 0 ▶ if (fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (fh) { 0 0 0
650 0 ▶ if (fill) {
Branch: 1 branch
BranchTrueFalseTotal
if (fill) { 0 0 0
651 /* The MD5Update() function is faster if it can work with
652 * complete blocks. This will fill up any buffered block
653 * first.
654 */
655 0 STRLEN missing = 64 - fill;
656 0 ▶ if ( (n = PerlIO_read(fh, buffer, missing)) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ( (n = PerlIO_read(fh, buffer, missing)) > 0) 0 0 0
657 0 MD5Update(context, buffer, n);
658 else
659 0 XSRETURN(1); /* self */
660 }
661
662 /* Process blocks until EOF or error */
663 0 ▶ while ( (n = PerlIO_read(fh, buffer, sizeof(buffer))) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (n = PerlIO_read(fh, buffer, sizeof(buffer))) > 0) { 0 0 0
664 0 MD5Update(context, buffer, n);
665 }
666
667 0 ▶ if (PerlIO_error(fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_error(fh)) { 0 0 0
668 0 croak("Reading from filehandle failed");
669 }
670 }
671 else {
672 0 croak("No filehandle passed");
673 }
674 0 XSRETURN(1); /* self */
675
676 void
677 digest(context)
678 MD5_CTX* context
679 ALIAS:
680 Digest::MD5::M4p::digest = F_BIN
681 Digest::MD5::M4p::hexdigest = F_HEX
682 Digest::MD5::M4p::b64digest = F_B64
683 PREINIT:
684 unsigned char digeststr[16];
685 PPCODE:
686 5 MD5Final(digeststr, context);
687 5 MD5Init(context); /* In case it is reused */
688 5 ST(0) = make_mortal_sv(aTHX_ digeststr, ix);
689 5 XSRETURN(1);
690
691 void
692 md5(...)
693 ALIAS:
694 Digest::MD5::M4p::md5 = F_BIN
695 Digest::MD5::M4p::md5_hex = F_HEX
696 Digest::MD5::M4p::md5_base64 = F_B64
697 PREINIT:
698 MD5_CTX ctx;
699 int i;
700 unsigned char *data;
701 STRLEN len;
702 unsigned char digeststr[16];
703 PPCODE:
704 5 MD5Init(&ctx);
705
706 5 ▶ if (DOWARN) {
Branch: 1 branch
BranchTrueFalseTotal
if (DOWARN) { 0 5 5
707 0 char *msg = 0;
708 0 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 0 0 0
709 0 ▶ if (SvROK(ST(0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(ST(0))) { 0 0 0
710 0 SV* sv = SvRV(ST(0));
711 0 ▶ if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD5"))
Branch: 8 branches
BranchTrueFalseTotal
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD5")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD5")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD5")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD5")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD5")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD5")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD5")) 0 0 0
if (SvOBJECT(sv) && strEQ(HvNAME(SvSTASH(sv)), "Digest::MD5")) 0 0 0
712 0 msg = "probably called as method";
713 else
714 0 msg = "called with reference argument";
715 }
716 }
717 0 ▶ else if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items > 1) { 0 0 0
718 0 data = (unsigned char *)SvPVbyte(ST(0), len);
719 0 ▶ if (len == 11 && memEQ("Digest::MD5::M4p", data, 11)) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 11 && memEQ("Digest::MD5::M4p", data, 11)) { 0 0 0
if (len == 11 && memEQ("Digest::MD5::M4p", data, 11)) { 0 0 0
720 0 msg = "probably called as class method";
721 }
722 }
723 0 ▶ if (msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (msg) { 0 0 0
724 0 ▶ char *f = (ix == F_BIN) ? "md5" :
Branch: 1 branch
BranchTrueFalseTotal
char *f = (ix == F_BIN) ? "md5" : 0 0 0
725 0 ▶ (ix == F_HEX) ? "md5_hex" : "md5_base64";
Branch: 1 branch
BranchTrueFalseTotal
(ix == F_HEX) ? "md5_hex" : "md5_base64"; 0 0 0
726 0 warn("&Digest::MD5::M4p::%s function %s", f, msg);
727 }
728 }
729
730 10 ▶ for (i = 0; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) { 5 5 10
731 5 data = (unsigned char *)(SvPVbyte(ST(i), len));
732 5 MD5Update(&ctx, data, len);
733 }
734 5 MD5Final(digeststr, &ctx);
735 5 ST(0) = make_mortal_sv(aTHX_ digeststr, ix);
736 5 XSRETURN(1);
« blib/lib/Digest/MD5/M4p.pm ↑ Summary