/src/ghostpdl/base/ssha2.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2021 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato, |
13 | | CA 94945, U.S.A., +1(415)492-9861, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* SHA256Encode filter */ |
18 | | #include "memory_.h" |
19 | | #include "strimpl.h" |
20 | | #include "stream.h" |
21 | | #include "ssha2.h" |
22 | | |
23 | | /* ------ SHA256Encode ------ */ |
24 | | |
25 | | private_st_SHA256E_state(); |
26 | | |
27 | | /* Initialize the state. */ |
28 | | static int |
29 | | s_SHA256E_init(stream_state * st) |
30 | 0 | { |
31 | 0 | stream_SHA256E_state *const ss = (stream_SHA256E_state *) st; |
32 | |
|
33 | 0 | pSHA256_Init(&ss->sha256); |
34 | 0 | return 0; |
35 | 0 | } |
36 | | |
37 | | /* Process a buffer. */ |
38 | | static int |
39 | | s_SHA256E_process(stream_state * st, stream_cursor_read * pr, |
40 | | stream_cursor_write * pw, bool last) |
41 | 0 | { |
42 | 0 | stream_SHA256E_state *const ss = (stream_SHA256E_state *) st; |
43 | 0 | int status = 0; |
44 | |
|
45 | 0 | if (pr->ptr < pr->limit) { |
46 | 0 | pSHA256_Update(&ss->sha256, pr->ptr + 1, pr->limit - pr->ptr); |
47 | 0 | pr->ptr = pr->limit; |
48 | 0 | } |
49 | 0 | if (last) { |
50 | 0 | if (pw->limit - pw->ptr >= 32) { |
51 | 0 | pSHA256_Final(pw->ptr + 1, &ss->sha256); |
52 | 0 | pw->ptr += 32; |
53 | 0 | status = EOFC; |
54 | 0 | } else |
55 | 0 | status = 1; |
56 | 0 | } |
57 | 0 | return status; |
58 | 0 | } |
59 | | |
60 | | /* Stream template */ |
61 | | const stream_template s_SHA256E_template = { |
62 | | &st_SHA256E_state, s_SHA256E_init, s_SHA256E_process, 1, 32 |
63 | | }; |
64 | | |
65 | | stream * |
66 | | s_SHA256E_make_stream(gs_memory_t *mem, byte *digest, int digest_size) |
67 | 0 | { |
68 | 0 | stream *s = s_alloc(mem, "s_SHA256E_make_stream"); |
69 | 0 | stream_state *ss = s_alloc_state(mem, s_SHA256E_template.stype, "s_SHA256E_make_stream"); |
70 | |
|
71 | 0 | if (ss == NULL || s == NULL) |
72 | 0 | goto err; |
73 | 0 | ss->templat = &s_SHA256E_template; |
74 | 0 | if (s_init_filter(s, ss, digest, digest_size, NULL) < 0) |
75 | 0 | goto err; |
76 | 0 | s->strm = s; |
77 | 0 | return s; |
78 | 0 | err: |
79 | 0 | gs_free_object(mem, ss, "s_SHA256E_make_stream"); |
80 | 0 | gs_free_object(mem, s, "s_SHA256E_make_stream"); |
81 | 0 | return NULL; |
82 | 0 | } |