File: jigsum-sha256.c

package info (click to toggle)
jigit 1.22-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,360 kB
  • sloc: ansic: 7,227; sh: 4,433; perl: 611; makefile: 157; lisp: 4
file content (289 lines) | stat: -rw-r--r-- 7,057 bytes parent folder | download | duplicates (2)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * jigsum-sha256
 *
 * Tool to calculate and print SHA256 checksums in jigdo's awkward
 * base64-ish encoding.
 *
 * Copyright (c) 2019 Steve McIntyre <steve@einval.com>
 *
 * GPL v2 - see COPYING
 */

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include "jig-base64.h"
#include "sha256.h"

#define BUF_SIZE 65536

/* SHA256 is 256-bit! */
#define CKSUM_BITS 256
#define CKSUM_BYTES (CKSUM_BITS / 8)
#define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
#define BASE64_CKSUM_BYTES ((ROUND_UP (CKSUM_BITS, 6)) / 6)

#ifndef MIN
#define MIN(x,y)        ( ((x) < (y)) ? (x) : (y))
#endif

enum mode_e
{
    MODE_CHECK,
    MODE_CALC
};

static int sha256_file(char *filename, char *sha256, int verbose)
{
    FILE *file = NULL;
    char buf[BUF_SIZE];
    unsigned char file_sha256[CKSUM_BYTES] = {0};
    char *base64_sha256 = NULL;
    struct sha256_ctx file_context;
    int done = 0;
    int bytes_read = 0;
    int error = 0;
    off_t file_size = -1;

    sha256_init_ctx(&file_context);

    /* Check if we're reading from stdin */
    if (!strcmp("-", filename))
        file = stdin;
    else
    {
        file = fopen(filename, "rb");
        if (!file)
        {
            switch (errno)
            {
                case EACCES:
                case EISDIR:
                    break;
                default:
                    fprintf(stderr, "Unable to open file %s; error %d\n", filename, errno);
                    break;
            }
            return errno;
        }
    }

    if (verbose == 3)
    {
        fprintf(stderr, "Checking %s:\r", filename);
        fflush(stdout);
    }

    if (verbose == 4)
    {
        struct stat st;
        if (file != stdin)
        {
            stat(filename, &st);
            file_size = st.st_size;
            fprintf(stderr, "Checking %s: 0 / %lld bytes\r", filename, (long long)file_size);
        }
        else
        {
            fprintf(stderr, "Checking stdin: 0 bytes\r");
        }
        fflush(stdout);
    }
    
    while (!done)
    {
        int used = 0;
        memset(buf, 0, BUF_SIZE);

        used = fread(buf, 1, BUF_SIZE, file);
        bytes_read += used;
        if (used)
            sha256_process_bytes((unsigned char *)buf, used, &file_context);
        else
        {
            if (ferror(file) && (EISDIR != errno))
            {
                fprintf(stderr, "Unable to read from file %s; error %d\n",
                        filename, errno);
                error = errno;
            }
            break;
        }
        if (verbose == 4)
        {
            if (file != stdin)
                fprintf(stderr, "Checking %s: %lld / %lld bytes\r",
                        filename, (long long) bytes_read, (long long)file_size);
            else
                fprintf(stderr, "Checking stdin: %lld bytes\r", (long long) bytes_read);
            fflush(stdout);
        }
    }
    if (verbose == 4)
        fprintf(stderr, "\n");
    
    sha256_finish_ctx(&file_context, file_sha256);
    base64_sha256 = base64_dump(file_sha256, CKSUM_BYTES);
    memcpy(sha256, base64_sha256, BASE64_CKSUM_BYTES + 1);
    fflush(stdout);
    free(base64_sha256);

    if (file != stdin)
        fclose(file);

    return error;
}

static int sha256_check(char *filename, int verbose)
{
    FILE *file = NULL;
    char *line = NULL;
    size_t len = 0;
    ssize_t read;
    char base64_sha256[BASE64_CKSUM_BYTES + 1] = {0};
    int error = 0;

    /* Check if we're reading from stdin */
    if (!strcmp("-", filename))
        file = stdin;
    else
    {
        file = fopen(filename, "rb");
        if (!file)
        {
            switch (errno)
            {
                case EACCES:
                case EISDIR:
                    break;
                default:
                    fprintf(stderr, "Unable to open file %s; error %d\n", filename, errno);
                    break;
            }
            return errno;
        }
    }

    while ((read = getline(&line, &len, file)) != -1) {
        /* Check the format of the line we've read. Should be:

           <N chars of sum><SPACE><SPACE><filename>

	   where N == BASE64_CKSUM_BYTES

           Look for the spaces and length at least. Use the strings
           directly in the buffer, add pointers to them in place.
        */
        char *this_sha256;
        char *this_filename;
        int this_error = 0;

        if (read > (BASE64_CKSUM_BYTES + 2)
	    && line[BASE64_CKSUM_BYTES] == ' '
	    && line[BASE64_CKSUM_BYTES + 1] == ' ')
        {
            line[BASE64_CKSUM_BYTES] = 0;
            this_sha256 = line;
            this_filename = &line[BASE64_CKSUM_BYTES + 2];
            if (line[read - 1] == '\n')
                line[--read] = '\0';
            
            this_error = sha256_file(this_filename, base64_sha256, verbose);
            if (this_error)
            {
                fprintf(stderr, "Failed to read %s, error %d (%s)\n",
                        this_filename, this_error, strerror(errno));
            }
            else
            {
                if (strcmp(base64_sha256, this_sha256))
                {
                    if (verbose > 0)
                        fprintf(stderr, "FAILED: %s\n", this_filename);
                    this_error++;
                }
                else
                {
                    if (verbose > 1)
                        fprintf(stderr, "OK: %s\n", this_filename);
                }
            }                
        }
        else
        {
            printf("ignoring malformed line %s\n", line);
        }
        error += this_error;
    }

    if (file != stdin)
        fclose(file);

    return error;
}

int main(int argc, char **argv)
{
    int i = 0;
    char base64_sha256[BASE64_CKSUM_BYTES];
    enum mode_e mode = MODE_CALC;
    int verbose = 1;
    int c = -1;
    int error = 0;

    while(1)
    {
        c = getopt(argc, argv, "cv");
        if (-1 == c)
            break;

        switch(c)
        {
            case 'v':
                verbose++;
                break;
            case 'c':
                mode = MODE_CHECK;
                break;
        }
    }

    if (mode == MODE_CALC)
    {
        if (argc == optind)
        {
            if (!sha256_file("-", base64_sha256, verbose))
                printf("%s  %s\n", base64_sha256, "-");
        }
        else
        {
            for (i = optind; i < argc; i++)
            {
                if (!sha256_file(argv[i], base64_sha256, verbose))
                    printf("%s  %s\n", base64_sha256, argv[i]);
                fflush(stdout);
            }
        }
    }
    else
    {
        if (argc == optind)
        {
            error += sha256_check("-", verbose);
        }
        else
        {
            for (i = optind; i < argc; i++)
                error += sha256_check(argv[i], verbose);
        }
    }
    
    return error;
}