1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/* Copyright (C) 2001-2019 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
CA 94945, U.S.A., +1(415)492-9861, for further information.
*/
/* MD5Encode filter */
#include "memory_.h"
#include "strimpl.h"
#include "stream.h"
#include "smd5.h"
/* ------ MD5Encode ------ */
private_st_MD5E_state();
/* Initialize the state. */
static int
s_MD5E_init(stream_state * st)
{
stream_MD5E_state *const ss = (stream_MD5E_state *) st;
gs_md5_init(&ss->md5);
return 0;
}
/* Process a buffer. */
static int
s_MD5E_process(stream_state * st, stream_cursor_read * pr,
stream_cursor_write * pw, bool last)
{
stream_MD5E_state *const ss = (stream_MD5E_state *) st;
int status = 0;
if (pr->ptr < pr->limit) {
gs_md5_append(&ss->md5, pr->ptr + 1, pr->limit - pr->ptr);
pr->ptr = pr->limit;
}
if (last) {
if (pw->limit - pw->ptr >= 16) {
gs_md5_finish(&ss->md5, pw->ptr + 1);
pw->ptr += 16;
status = EOFC;
} else
status = 1;
}
return status;
}
/* Stream template */
const stream_template s_MD5E_template = {
&st_MD5E_state, s_MD5E_init, s_MD5E_process, 1, 16
};
stream *
s_MD5E_make_stream(gs_memory_t *mem, byte *digest, int digest_size)
{
stream *s = s_alloc(mem, "s_MD5E_make_stream");
stream_state *ss = s_alloc_state(mem, s_MD5E_template.stype, "s_MD5E_make_stream");
if (ss == NULL || s == NULL)
goto err;
ss->templat = &s_MD5E_template;
if (s_init_filter(s, ss, digest, digest_size, NULL) < 0)
goto err;
s->strm = s;
return s;
err:
gs_free_object(mem, ss, "s_MD5E_make_stream");
gs_free_object(mem, s, "s_MD5E_make_stream");
return NULL;
}
/* Process a buffer. */
static int
s_MD5C_process(stream_state * st, stream_cursor_read * pr,
stream_cursor_write * pw, bool last)
{
stream_MD5E_state *const ss = (stream_MD5E_state *) st;
int nr = pr->limit - pr->ptr;
int nw = pw->limit - pw->ptr;
int n = min(nr, nw);
gs_md5_append(&ss->md5, pr->ptr + 1, n);
memcpy(pw->ptr + 1, pr->ptr + 1, n);
pr->ptr += n;
pw->ptr += n;
if (pw->limit == pw->ptr)
return 1;
return 0;
}
/* Stream template */
const stream_template s_MD5C_template = {
&st_MD5E_state, s_MD5E_init, s_MD5C_process, 1, 1024
};
stream *
s_MD5C_make_stream(gs_memory_t *mem, stream *strm)
{
stream *s = s_alloc(mem, "s_MD5E_make_stream");
stream_state *ss = s_alloc_state(mem, s_MD5E_template.stype, "s_MD5E_make_stream");
int buffer_size = 1024;
byte *buffer = gs_alloc_bytes(mem, buffer_size, "s_MD5E_make_stream(buffer)");
if (ss == NULL || s == NULL || buffer == NULL)
goto err;
ss->templat = &s_MD5C_template;
if (s_init_filter(s, ss, buffer, buffer_size, NULL) < 0)
goto err;
s->strm = strm;
s->close_strm = true;
return s;
err:
gs_free_object(mem, ss, "s_MD5E_make_stream");
gs_free_object(mem, s, "s_MD5E_make_stream");
gs_free_object(mem, buffer, "s_MD5E_make_stream");
return NULL;
}
/* Get a digest. Apply 'flush' before calling. */
int
s_MD5C_get_digest(stream *s, byte *buf, int buf_length)
{
stream_MD5E_state *const ss = (stream_MD5E_state *)s->state;
gs_md5_state_t md5;
byte b[16], *p;
int l = min(16, buf_length), k;
if (s->procs.process != s_MD5C_process)
return 0; /* Must not happen. */
md5 = ss->md5;
gs_md5_finish(&md5, b);
memcpy(buf, b, l);
for (p = b + l; p < b + sizeof(b); p += l) {
for (k = 0; k < l && p + k < b + sizeof(b); k++)
buf[k] ^= p[k];
}
return l;
}
|