drc: handle upto 64k page size
[pcsx_rearmed.git] / deps / libretro-common / samples / utils / sha1_main.c
1 /*
2  *  sha.cpp
3  *
4  *  Copyright (C) 1998, 2009
5  *  Paul E. Jones <paulej@packetizer.com>
6  *  All Rights Reserved
7  *
8  *****************************************************************************
9  *  $Id: sha.c 12 2009-06-22 19:34:25Z paulej $
10  *****************************************************************************
11  *
12  *  Description:
13  *      This utility will display the message digest (fingerprint) for
14  *      the specified file(s).
15  *
16  *  Portability Issues:
17  *      None.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #ifdef _WIN32
23 #include <io.h>
24 #endif
25 #include <fcntl.h>
26 #include <string/stdstring.h>
27 /*#include "sha1.h"*/
28
29 /*
30  *  Function prototype
31  */
32 void usage(void);
33
34 /*
35  *  main
36  *
37  *  Description:
38  *      This is the entry point for the program
39  *
40  *  Parameters:
41  *      argc: [in]
42  *          This is the count of arguments in the argv array
43  *      argv: [in]
44  *          This is an array of filenames for which to compute message
45  *          digests
46  *
47  *  Returns:
48  *      Nothing.
49  *
50  *  Comments:
51  *
52  */
53 typedef struct SHA1Context
54 {
55    unsigned Message_Digest[5]; /* Message Digest (output)          */
56
57    unsigned Length_Low;        /* Message length in bits           */
58    unsigned Length_High;       /* Message length in bits           */
59
60    unsigned char Message_Block[64]; /* 512-bit message blocks      */
61    int Message_Block_Index;    /* Index into message block array   */
62
63    int Computed;               /* Is the digest computed?          */
64    int Corrupted;              /* Is the message digest corruped?  */
65 } SHA1Context;
66
67 int main(int argc, char *argv[])
68 {
69    struct SHA1Context sha;         /* SHA-1 context                 */
70    FILE        *fp;                /* File pointer for reading files*/
71    char        c;                  /* Character read from file      */
72    int         i;                  /* Counter                       */
73    int         reading_stdin;      /* Are we reading standard in?   */
74    int         read_stdin = 0;     /* Have we read stdin?           */
75
76    /*
77     *  Check the program arguments and print usage information if -?
78     *  or --help is passed as the first argument.
79     */
80    if (argc > 1 && (string_is_equal(argv[1],"-?") ||
81             string_is_equal(argv[1],"--help")))
82    {
83       usage();
84       return 1;
85    }
86
87    /*
88     *  For each filename passed in on the command line, calculate the
89     *  SHA-1 value and display it.
90     */
91    for (i = 0; i < argc; i++)
92    {
93       /*
94        *  We start the counter at 0 to guarantee entry into the for
95        *  loop. So if 'i' is zero, we will increment it now.  If there
96        *  is no argv[1], we will use STDIN below.
97        */
98       if (i == 0)
99          i++;
100
101       if (argc == 1 || string_is_equal(argv[i],"-"))
102       {
103 #ifdef WIN32
104          setmode(fileno(stdin), _O_BINARY);
105 #endif
106          fp = stdin;
107          reading_stdin = 1;
108       }
109       else
110       {
111          if (!(fp = fopen(argv[i],"rb")))
112          {
113             fprintf(stderr,
114                   "sha: unable to open file %s\n",
115                   argv[i]);
116             return 2;
117          }
118          reading_stdin = 0;
119       }
120
121       /*
122        *  We do not want to read STDIN multiple times
123        */
124       if (reading_stdin)
125       {
126          if (read_stdin)
127             continue;
128
129          read_stdin = 1;
130       }
131
132       /*
133        *  Reset the SHA-1 context and process input
134        */
135       SHA1Reset(&sha);
136
137       c = fgetc(fp);
138       while(!feof(fp))
139       {
140          SHA1Input(&sha, &c, 1);
141          c = fgetc(fp);
142       }
143
144       if (!reading_stdin)
145          fclose(fp);
146
147       if (!SHA1Result(&sha))
148       {
149          fprintf(stderr,
150                "sha: could not compute message digest for %s\n",
151                reading_stdin?"STDIN":argv[i]);
152       }
153       else
154       {
155          printf( "%08X %08X %08X %08X %08X - %s\n",
156                sha.Message_Digest[0],
157                sha.Message_Digest[1],
158                sha.Message_Digest[2],
159                sha.Message_Digest[3],
160                sha.Message_Digest[4],
161                reading_stdin?"STDIN":argv[i]);
162       }
163    }
164
165    return 0;
166 }
167
168 /*
169  *  usage
170  *
171  *  Description:
172  *      This function will display program usage information to the
173  *      user.
174  *
175  *  Parameters:
176  *      None.
177  *
178  *  Returns:
179  *      Nothing.
180  *
181  *  Comments:
182  *
183  */
184 void usage(void)
185 {
186    printf("usage: sha <file> [<file> ...]\n");
187    printf("\tThis program will display the message digest\n");
188    printf("\tfor files using the Secure Hashing Algorithm (SHA-1).\n");
189 }