Rev 154 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 154 | Rev 178 | ||
---|---|---|---|
Line 144... | Line 144... | ||
144 | } |
144 | } |
145 | 145 | ||
146 | *pos = 0; // drop a string terminator where the decoded stream ends (but don't take it in account in the length) |
146 | *pos = 0; // drop a string terminator where the decoded stream ends (but don't take it in account in the length) |
147 | return (pos - dest); // source has been base64-decoded in dest, return decoded length |
147 | return (pos - dest); // source has been base64-decoded in dest, return decoded length |
148 | } |
148 | } |
- | 149 | ||
- | 150 | ||
- | 151 | #ifdef TEST_BASE64 |
|
- | 152 | #include <stdlib.h> |
|
- | 153 | #include <string.h> |
|
- | 154 | int main (int argc, char **argv) |
|
- | 155 | { |
|
- | 156 | FILE *fp; |
|
- | 157 | char *inbuf; |
|
- | 158 | size_t inbuf_size; |
|
- | 159 | char *outbuf; |
|
- | 160 | size_t outbuf_size; |
|
- | 161 | int should_decode; |
|
- | 162 | ||
- | 163 | if ((argc < 4) || ((strcmp (argv[1], "encode") != 0) && (strcmp (argv[1], "decode") != 0))) |
|
- | 164 | { |
|
- | 165 | fprintf (stderr, "usage: %s <encode|decode> <infile> <outfile>\n", argv[0]); |
|
- | 166 | exit (1); |
|
- | 167 | } |
|
- | 168 | ||
- | 169 | should_decode = (strcmp (argv[1], "decode") == 0); |
|
- | 170 | ||
- | 171 | fp = fopen (argv[2], "rb"); |
|
- | 172 | if (fp == NULL) |
|
- | 173 | { |
|
- | 174 | fprintf (stderr, "error: unable to open input file '%s'\n", argv[2]); |
|
- | 175 | exit (1); |
|
- | 176 | } |
|
- | 177 | fseek (fp, 0, SEEK_END); |
|
- | 178 | inbuf_size = ftell (fp); |
|
- | 179 | fseek (fp, 0, SEEK_SET); |
|
- | 180 | inbuf = (char *) malloc (inbuf_size); |
|
- | 181 | if (inbuf == NULL) |
|
- | 182 | { |
|
- | 183 | fprintf (stderr, "error: malloc() failed for %zd bytes while reading input file '%s'\n", inbuf_size, argv[2]); |
|
- | 184 | exit (1); |
|
- | 185 | } |
|
- | 186 | fread (inbuf, inbuf_size, 1, fp); |
|
- | 187 | fclose (fp); |
|
- | 188 | ||
- | 189 | outbuf_size = 2 * inbuf_size; |
|
- | 190 | outbuf = (char *) malloc (outbuf_size); |
|
- | 191 | if (outbuf == NULL) |
|
- | 192 | { |
|
- | 193 | fprintf (stderr, "error: malloc() failed for %zd bytes while preparing output file '%s'\n", outbuf_size, argv[3]); |
|
- | 194 | exit (1); |
|
- | 195 | } |
|
- | 196 | ||
- | 197 | if (should_decode) |
|
- | 198 | outbuf_size = base64_decode ((unsigned char *) outbuf, (unsigned char *) inbuf, inbuf_size); |
|
- | 199 | else |
|
- | 200 | outbuf_size = base64_encode (outbuf, inbuf, inbuf_size); |
|
- | 201 | ||
- | 202 | fp = fopen (argv[3], "wb"); |
|
- | 203 | if (fp == NULL) |
|
- | 204 | { |
|
- | 205 | fprintf (stderr, "error: unable to open output file '%s'\n", argv[3]); |
|
- | 206 | exit (1); |
|
- | 207 | } |
|
- | 208 | fwrite (outbuf, outbuf_size, 1, fp); |
|
- | 209 | fclose (fp); |
|
- | 210 | ||
- | 211 | free (inbuf); |
|
- | 212 | free (outbuf); |
|
- | 213 | return (0); |
|
- | 214 | } |
|
- | 215 | #endif // TEST_BASE64 |