Guest User

Untitled

a guest
May 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. static void decodeblock( unsigned char *in, unsigned char *out )
  2. {
  3. out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
  4. out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
  5. out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
  6. }
  7. static int decode( FILE *infile, FILE *outfile )
  8. {
  9. int retcode = 0;
  10. unsigned char in[4];
  11. unsigned char out[3];
  12. int v;
  13. int i, len;
  14.  
  15. *in = (unsigned char) 0;
  16. *out = (unsigned char) 0;
  17. while( feof( infile ) == 0 ) {
  18. for( len = 0, i = 0; i < 4 && feof( infile ) == 0; i++ ) {
  19. v = 0;
  20. while( feof( infile ) == 0 && v == 0 ) {
  21. v = getc( infile );
  22. if( feof( infile ) == 0 ) {
  23. v = ((v < 43 || v > 122) ? 0 : (int) cd64[ v - 43 ]);
  24. if( v != 0 ) {
  25. v = ((v == (int)'$') ? 0 : v - 61);
  26. }
  27. }
  28. }
  29. if( feof( infile ) == 0 ) {
  30. len++;
  31. if( v != 0 ) {
  32. in[ i ] = (unsigned char) (v - 1);
  33. }
  34. }
  35. else {
  36. in[i] = (unsigned char) 0;
  37. }
  38. }
  39. if( len > 0 ) {
  40. decodeblock( in, out );
  41. for( i = 0; i < len - 1; i++ ) {
  42. if( putc( (int) out[i], outfile ) == EOF ){
  43. if( ferror( outfile ) != 0 ) {
  44. perror( b64_message( B64_FILE_IO_ERROR ) );
  45. retcode = B64_FILE_IO_ERROR;
  46. }
  47. break;
  48. }
  49. }
  50. }
  51. }
  52. return( retcode );
  53. }
  54.  
  55. #include <SdFat.h>
  56. #include <SdFatUtil.h>
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <SPI.h>
  60. SdFat SD;
  61.  
  62. File myFile;
  63.  
  64. #define B64_DEF_LINE_SIZE 72
  65. #define B64_MIN_LINE_SIZE 4
  66.  
  67. #define THIS_OPT(ac, av) ((char)(ac > 1 ? av[1][0] == '-' ? av[1][1] : 0 : 0))
  68.  
  69. char name[] = "a.jpg";
  70. const boolean debugPrint = true; // print details of process over serial?
  71.  
  72. SdFat sd;
  73. SdFile file;
  74. const uint8_t chipSelect = 8; // pin that the SD is connected to (d8 for SparkFun MicroSD shield)
  75.  
  76.  
  77. /*
  78. ** Translation Table as described in RFC1113
  79. */
  80. static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  81.  
  82. /*
  83. ** Translation Table to decode (created by author)
  84. */
  85. static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\]^_`abcdefghijklmnopq";
  86.  
  87. /*
  88. ** returnable errors
  89. **
  90. ** Error codes returned to the operating system.
  91. **
  92. */
  93. #define B64_SYNTAX_ERROR 1
  94. #define B64_FILE_ERROR 2
  95. #define B64_FILE_IO_ERROR 3
  96. #define B64_ERROR_OUT_CLOSE 4
  97. #define B64_LINE_SIZE_TO_MIN 5
  98. #define B64_SYNTAX_TOOMANYARGS 6
  99.  
  100. /*
  101. ** b64_message
  102. **
  103. ** Gather text messages in one place.
  104. **
  105. */
  106. #define B64_MAX_MESSAGES 7
  107. static char *b64_msgs[ B64_MAX_MESSAGES ] = {
  108. "b64:000:Invalid Message Code.",
  109. "b64:001:Syntax Error -- check help (-h) for usage.",
  110. "b64:002:File Error Opening/Creating Files.",
  111. "b64:003:File I/O Error -- Note: output file not removed.",
  112. "b64:004:Error on output file close.",
  113. "b64:005:linesize set to minimum.",
  114. "b64:006:Syntax: Too many arguments."
  115. };
  116.  
  117. #define b64_message( ec ) ((ec > 0 && ec < B64_MAX_MESSAGES ) ? b64_msgs[ ec ] : b64_msgs[ 0 ])
  118.  
  119. /*
  120. ** encodeblock
  121. **
  122. ** encode 3 8-bit binary bytes as 4 '6-bit' characters
  123. */
  124. static void encodeblock( unsigned char *in, unsigned char *out, int len )
  125. {
  126. out[0] = (unsigned char) cb64[ (int)(in[0] >> 2) ];
  127. out[1] = (unsigned char) cb64[ (int)(((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)) ];
  128. out[2] = (unsigned char) (len > 1 ? cb64[ (int)(((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)) ] : '=');
  129. out[3] = (unsigned char) (len > 2 ? cb64[ (int)(in[2] & 0x3f) ] : '=');
  130. }
  131.  
  132.  
  133. /*
  134. ** decodeblock
  135. **
  136. ** decode 4 '6-bit' characters into 3 8-bit binary bytes
  137. */
  138. static void decodeblock( unsigned char *in, unsigned char *out )
  139. {
  140. out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
  141. out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
  142. out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
  143. }
  144.  
  145. /*
  146. ** decode
  147. **
  148. ** decode a base64 encoded stream discarding padding, line breaks and noise
  149. */
  150. static int decode()
  151. {
  152. int retcode = 0;
  153. unsigned char in[4];
  154. unsigned char out[3];
  155. int v;
  156. int i, len;
  157.  
  158. *in = (unsigned char) 0;
  159. *out = (unsigned char) 0;
  160. while( myFile.available() ) {
  161. for( len = 0, i = 0; i < 4 && myFile.available(); i++ ) {
  162. v = 0;
  163. while( myFile.available() && v == 0 ) {
  164. v =myFile.read();
  165. if( myFile.available() ) {
  166. v = ((v < 43 || v > 122) ? 0 : (int) cd64[ v - 43 ]);
  167. if( v != 0 ) {
  168. v = ((v == (int)'$') ? 0 : v - 61);
  169. }
  170. }
  171. }
  172. if(myFile.available() ) {
  173. len++;
  174. if( v != 0 ) {
  175. in[ i ] = (unsigned char) (v - 1);
  176. }
  177. }
  178. else {
  179. in[i] = (unsigned char) 0;
  180. }
  181. }
  182. if( len > 0 ) {
  183. decodeblock( in, out );
  184. for( i = 0; i < len - 1; i++ ) {
  185. if( file.write((int)out[i])){
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. }
  192.  
  193. void setup()
  194. {
  195. myFile = SD.open("a.b64");
  196. // SD setup
  197. Serial.begin(9600);
  198. if (!sd.begin(SS, SD_SCK_MHZ(50))) {
  199. sd.initErrorHalt();
  200. }
  201. file.open(name, O_CREAT | O_EXCL | O_WRITE);
  202. decode();
  203. file.close();
  204. }
  205. void loop()
  206. {
  207.  
  208. }
Add Comment
Please, Sign In to add comment