Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void decodeblock( unsigned char *in, unsigned char *out )
- {
- out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
- out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
- out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
- }
- static int decode( FILE *infile, FILE *outfile )
- {
- int retcode = 0;
- unsigned char in[4];
- unsigned char out[3];
- int v;
- int i, len;
- *in = (unsigned char) 0;
- *out = (unsigned char) 0;
- while( feof( infile ) == 0 ) {
- for( len = 0, i = 0; i < 4 && feof( infile ) == 0; i++ ) {
- v = 0;
- while( feof( infile ) == 0 && v == 0 ) {
- v = getc( infile );
- if( feof( infile ) == 0 ) {
- v = ((v < 43 || v > 122) ? 0 : (int) cd64[ v - 43 ]);
- if( v != 0 ) {
- v = ((v == (int)'$') ? 0 : v - 61);
- }
- }
- }
- if( feof( infile ) == 0 ) {
- len++;
- if( v != 0 ) {
- in[ i ] = (unsigned char) (v - 1);
- }
- }
- else {
- in[i] = (unsigned char) 0;
- }
- }
- if( len > 0 ) {
- decodeblock( in, out );
- for( i = 0; i < len - 1; i++ ) {
- if( putc( (int) out[i], outfile ) == EOF ){
- if( ferror( outfile ) != 0 ) {
- perror( b64_message( B64_FILE_IO_ERROR ) );
- retcode = B64_FILE_IO_ERROR;
- }
- break;
- }
- }
- }
- }
- return( retcode );
- }
- #include <SdFat.h>
- #include <SdFatUtil.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <SPI.h>
- SdFat SD;
- File myFile;
- #define B64_DEF_LINE_SIZE 72
- #define B64_MIN_LINE_SIZE 4
- #define THIS_OPT(ac, av) ((char)(ac > 1 ? av[1][0] == '-' ? av[1][1] : 0 : 0))
- char name[] = "a.jpg";
- const boolean debugPrint = true; // print details of process over serial?
- SdFat sd;
- SdFile file;
- const uint8_t chipSelect = 8; // pin that the SD is connected to (d8 for SparkFun MicroSD shield)
- /*
- ** Translation Table as described in RFC1113
- */
- static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- /*
- ** Translation Table to decode (created by author)
- */
- static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\]^_`abcdefghijklmnopq";
- /*
- ** returnable errors
- **
- ** Error codes returned to the operating system.
- **
- */
- #define B64_SYNTAX_ERROR 1
- #define B64_FILE_ERROR 2
- #define B64_FILE_IO_ERROR 3
- #define B64_ERROR_OUT_CLOSE 4
- #define B64_LINE_SIZE_TO_MIN 5
- #define B64_SYNTAX_TOOMANYARGS 6
- /*
- ** b64_message
- **
- ** Gather text messages in one place.
- **
- */
- #define B64_MAX_MESSAGES 7
- static char *b64_msgs[ B64_MAX_MESSAGES ] = {
- "b64:000:Invalid Message Code.",
- "b64:001:Syntax Error -- check help (-h) for usage.",
- "b64:002:File Error Opening/Creating Files.",
- "b64:003:File I/O Error -- Note: output file not removed.",
- "b64:004:Error on output file close.",
- "b64:005:linesize set to minimum.",
- "b64:006:Syntax: Too many arguments."
- };
- #define b64_message( ec ) ((ec > 0 && ec < B64_MAX_MESSAGES ) ? b64_msgs[ ec ] : b64_msgs[ 0 ])
- /*
- ** encodeblock
- **
- ** encode 3 8-bit binary bytes as 4 '6-bit' characters
- */
- static void encodeblock( unsigned char *in, unsigned char *out, int len )
- {
- out[0] = (unsigned char) cb64[ (int)(in[0] >> 2) ];
- out[1] = (unsigned char) cb64[ (int)(((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)) ];
- out[2] = (unsigned char) (len > 1 ? cb64[ (int)(((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)) ] : '=');
- out[3] = (unsigned char) (len > 2 ? cb64[ (int)(in[2] & 0x3f) ] : '=');
- }
- /*
- ** decodeblock
- **
- ** decode 4 '6-bit' characters into 3 8-bit binary bytes
- */
- static void decodeblock( unsigned char *in, unsigned char *out )
- {
- out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
- out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
- out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
- }
- /*
- ** decode
- **
- ** decode a base64 encoded stream discarding padding, line breaks and noise
- */
- static int decode()
- {
- int retcode = 0;
- unsigned char in[4];
- unsigned char out[3];
- int v;
- int i, len;
- *in = (unsigned char) 0;
- *out = (unsigned char) 0;
- while( myFile.available() ) {
- for( len = 0, i = 0; i < 4 && myFile.available(); i++ ) {
- v = 0;
- while( myFile.available() && v == 0 ) {
- v =myFile.read();
- if( myFile.available() ) {
- v = ((v < 43 || v > 122) ? 0 : (int) cd64[ v - 43 ]);
- if( v != 0 ) {
- v = ((v == (int)'$') ? 0 : v - 61);
- }
- }
- }
- if(myFile.available() ) {
- len++;
- if( v != 0 ) {
- in[ i ] = (unsigned char) (v - 1);
- }
- }
- else {
- in[i] = (unsigned char) 0;
- }
- }
- if( len > 0 ) {
- decodeblock( in, out );
- for( i = 0; i < len - 1; i++ ) {
- if( file.write((int)out[i])){
- break;
- }
- }
- }
- }
- }
- void setup()
- {
- myFile = SD.open("a.b64");
- // SD setup
- Serial.begin(9600);
- if (!sd.begin(SS, SD_SCK_MHZ(50))) {
- sd.initErrorHalt();
- }
- file.open(name, O_CREAT | O_EXCL | O_WRITE);
- decode();
- file.close();
- }
- void loop()
- {
- }
Add Comment
Please, Sign In to add comment