Branch data Line data Source code
1 : : /* -*- mode: C -*-
2 : : *
3 : : * File: pdf-stm-f-md5.c
4 : : * Date: Fri Dec 5 16:40:50 2008
5 : : *
6 : : * GNU PDF Library - Message-digest stream filter
7 : : *
8 : : */
9 : :
10 : : /* Copyright (C) 2008-2011 Free Software Foundation, Inc. */
11 : :
12 : : /* This program is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU General Public License as published by
14 : : * the Free Software Foundation, either version 3 of the License, or
15 : : * (at your option) any later version.
16 : : *
17 : : * This program is distributed in the hope that it will be useful,
18 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 : : * GNU General Public License for more details.
21 : : *
22 : : * You should have received a copy of the GNU General Public License
23 : : * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 : : */
25 : :
26 : : #include <config.h>
27 : :
28 : : #include <string.h>
29 : :
30 : : #include <pdf-types.h>
31 : : #include <pdf-types-buffer.h>
32 : : #include <pdf-hash.h>
33 : : #include <pdf-alloc.h>
34 : : #include <pdf-crypt.h>
35 : : #include <pdf-stm-f-md5.h>
36 : :
37 : : /* Define MD5 encoder */
38 : 0 : PDF_STM_FILTER_DEFINE (pdf_stm_f_md5enc_get,
39 : : stm_f_md5enc_init,
40 : : stm_f_md5enc_apply,
41 : : stm_f_md5enc_deinit);
42 : :
43 : : #define MD5_OUTPUT_SIZE 16
44 : :
45 : : /* Internal state */
46 : : struct pdf_stm_f_md5_s
47 : : {
48 : : pdf_crypt_md_t *md;
49 : : pdf_buffer_t *cache;
50 : : };
51 : :
52 : : static pdf_bool_t
53 : 0 : stm_f_md5enc_init (const pdf_hash_t *params,
54 : : void **state,
55 : : pdf_error_t **error)
56 : : {
57 : : struct pdf_stm_f_md5_s *filter_state;
58 : :
59 : 0 : filter_state = pdf_alloc (sizeof (struct pdf_stm_f_md5_s));
60 [ # # ]: 0 : if (!filter_state)
61 : : {
62 : 0 : pdf_set_error (error,
63 : : PDF_EDOMAIN_BASE_STM,
64 : : PDF_ENOMEM,
65 : : "cannot create V2 encoder/decoder internal state: "
66 : : "couldn't allocate %lu bytes",
67 : : (unsigned long)sizeof (struct pdf_stm_f_md5_s));
68 : 0 : return PDF_FALSE;
69 : : }
70 : :
71 : : /* Initialize internal buffer */
72 : 0 : filter_state->cache = pdf_buffer_new (MD5_OUTPUT_SIZE, error);
73 [ # # ]: 0 : if (!(filter_state->cache))
74 : : {
75 : 0 : stm_f_md5enc_deinit (filter_state);
76 : 0 : return PDF_FALSE;
77 : : }
78 : :
79 : 0 : filter_state->md = pdf_crypt_md_new (PDF_CRYPT_MD_MD5, error);
80 [ # # ]: 0 : if (!filter_state->md)
81 : : {
82 : 0 : stm_f_md5enc_deinit (filter_state);
83 : 0 : return PDF_FALSE;
84 : : }
85 : :
86 : 0 : *state = filter_state;
87 : 0 : return PDF_TRUE;
88 : : }
89 : :
90 : : static void
91 : 0 : stm_f_md5enc_deinit (void *state)
92 : : {
93 : 0 : struct pdf_stm_f_md5_s *filter_state = state;
94 : :
95 [ # # ]: 0 : if (filter_state->md)
96 : 0 : pdf_crypt_md_destroy (filter_state->md);
97 [ # # ]: 0 : if (filter_state->cache)
98 : 0 : pdf_buffer_destroy (filter_state->cache);
99 : 0 : pdf_dealloc (state);
100 : 0 : }
101 : :
102 : : static enum pdf_stm_filter_apply_status_e
103 : 0 : stm_f_md5enc_apply (void *state,
104 : : pdf_buffer_t *in,
105 : : pdf_buffer_t *out,
106 : : pdf_bool_t finish,
107 : : pdf_error_t **error)
108 : : {
109 : 0 : struct pdf_stm_f_md5_s *filter_state = state;
110 : 0 : pdf_buffer_t *cache = filter_state->cache;
111 : : pdf_size_t in_size;
112 : : pdf_size_t bytes_to_write;
113 : : pdf_size_t cache_size;
114 : : pdf_size_t out_size;
115 : :
116 [ # # ]: 0 : PDF_ASSERT (in->wp >= in->rp);
117 : 0 : in_size = in->wp - in->rp;
118 : :
119 [ # # ]: 0 : if (!pdf_crypt_md_write (filter_state->md,
120 : : (pdf_char_t *)in->data,
121 : : in_size,
122 : : error))
123 : 0 : return PDF_STM_FILTER_APPLY_STATUS_ERROR;
124 : :
125 : 0 : in->rp += in_size;
126 : :
127 [ # # ]: 0 : if (!finish)
128 : 0 : return PDF_STM_FILTER_APPLY_STATUS_NO_INPUT;
129 : :
130 [ # # ]: 0 : if (pdf_buffer_eob_p (cache))
131 : : {
132 : : /* If we have reached the end, read the hash value in cache */
133 [ # # ]: 0 : if (!pdf_crypt_md_read (filter_state->md,
134 : : (pdf_char_t *)cache->data,
135 : : cache->size,
136 : : error))
137 : 0 : return PDF_STM_FILTER_APPLY_STATUS_ERROR;
138 : 0 : cache->wp = cache->size;
139 : : }
140 : :
141 [ # # ]: 0 : PDF_ASSERT (out->size >= out->wp);
142 [ # # ]: 0 : PDF_ASSERT (cache->wp >= cache->rp);
143 : :
144 : 0 : out_size = out->size - out->wp;
145 : 0 : cache_size = cache->wp - cache->rp;
146 : 0 : bytes_to_write = PDF_MIN (out_size, cache_size);
147 : :
148 : 0 : memcpy (out->data, cache->data + cache->rp, bytes_to_write);
149 : :
150 : 0 : cache->rp += bytes_to_write;
151 : 0 : out->wp += bytes_to_write;
152 : :
153 [ # # ]: 0 : if (out_size >= cache_size)
154 : 0 : return PDF_STM_FILTER_APPLY_STATUS_EOF;
155 : :
156 : 0 : return PDF_STM_FILTER_APPLY_STATUS_NO_OUTPUT;
157 : : }
158 : :
159 : : /* End of pdf_stm_f_md5.c */
|