Pure Programmer
Blue Matrix


Cluster Map

Project: Base64 Decoding

[[Base64]] is a binary-to-text encoding scheme used in the early days of computing when SMTP (email) and other network protocols only supported 7-bit bytes. It is still used today for attachments to email files. The idea is to convert a file that uses 8-bit bytes into a stream of 7-bit ASCII characters. By using a radix of 64 we can represent 6-bit values with one of the characters A-Z, a-z, 0-9, +, /. So the idea is to break three octets up into four sextets, then convert the sextet to one of our 64 7-bit safe ASCII characters. Depending on the length of the data one or two equal signs '=' will need to be output at the end to pad the output to an even multiple of four. Also to make things easier to read, a newline is output after every 76 output characters.

Write a program that accepts a filename on the command line of a Base64 encoded file, converts the file back to its original binary representation and outputs it to a second filename supplied on the command line.

See Base64Encoding

Output
$ rustc Base64Decoding.rs error[E0425]: cannot find value `ofh` in this scope --> Base64Decoding.rs:31:10 | 31 | binmode(ofh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> Base64Decoding.rs:36:23 | 36 | while getcodepoint(c,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ofh` in this scope --> Base64Decoding.rs:52:30 | 52 | putbyte(outputBytes[i],ofh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> Base64Decoding.rs:59:8 | 59 | close(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ofh` in this scope --> Base64Decoding.rs:60:8 | 60 | close(ofh)?; | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> Base64Decoding.rs:67:3 | 67 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0277]: the type `[i32]` cannot be indexed by `isize` --> Base64Decoding.rs:20:12 | 20 | sextets[i] = find(base64Codes,inputBytes[i]); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[i32]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<i32>` to implement `Index<isize>` error[E0277]: the type `[i32]` cannot be indexed by `isize` --> Base64Decoding.rs:20:45 | 20 | sextets[i] = find(base64Codes,inputBytes[i]); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[i32]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<i32>` to implement `Index<isize>` error[E0425]: cannot find function `find` in this scope --> Base64Decoding.rs:20:17 | 20 | sextets[i] = find(base64Codes,inputBytes[i]); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 20 - sextets[i] = find(base64Codes,inputBytes[i]); 20 + sextets[i] = base64Codes.find(inputBytes[i]); | error[E0425]: cannot find function `binmode` in this scope --> Base64Decoding.rs:31:2 | 31 | binmode(ofh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `getcodepoint` in this scope --> Base64Decoding.rs:36:8 | 36 | while getcodepoint(c,ifh) { | ^^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `isspace` in this scope --> Base64Decoding.rs:37:8 | 37 | if !(isspace(c)) { | ^^^^^^^ not found in this scope error[E0277]: the type `[i32]` cannot be indexed by `isize` --> Base64Decoding.rs:38:15 | 38 | inputBytes[count % 4] = c; | ^^^^^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[i32]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<i32>` to implement `Index<isize>` error[E0277]: the type `[i32]` cannot be indexed by `isize` --> Base64Decoding.rs:52:27 | 52 | putbyte(outputBytes[i],ofh); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[i32]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<i32>` to implement `Index<isize>` error[E0425]: cannot find function `putbyte` in this scope --> Base64Decoding.rs:52:7 | 52 | putbyte(outputBytes[i],ofh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> Base64Decoding.rs:59:2 | 59 | close(ifh); | ^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> Base64Decoding.rs:60:2 | 60 | close(ofh)?; | ^^^^^ not found in this scope error[E0308]: mismatched types --> Base64Decoding.rs:30:61 | 30 | fn convertFromBase64(fromFilespec:&str, toFilespec:&str) -> Result<(), Arc<dyn error::Error>> { | ----------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), Arc<dyn Error>>`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression | = note: expected enum `Result<(), Arc<(dyn std::error::Error + 'static)>>` found unit type `()` error[E0433]: failed to resolve: use of undeclared crate or module `io` --> Base64Decoding.rs:77:59 | 77 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 19 previous errors Some errors have detailed explanations: E0277, E0308, E0425, E0433. For more information about an error, try `rustc --explain E0277`. $ diff ../../data/text/GettysburgAddress.txt output/testBase64Decoding.txt diff: output/testBase64Decoding.txt: No such file or directory $ rustc Base64Decoding.rs error[E0425]: cannot find value `ofh` in this scope --> Base64Decoding.rs:31:10 | 31 | binmode(ofh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> Base64Decoding.rs:36:23 | 36 | while getcodepoint(c,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ofh` in this scope --> Base64Decoding.rs:52:30 | 52 | putbyte(outputBytes[i],ofh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> Base64Decoding.rs:59:8 | 59 | close(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ofh` in this scope --> Base64Decoding.rs:60:8 | 60 | close(ofh)?; | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> Base64Decoding.rs:67:3 | 67 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0277]: the type `[i32]` cannot be indexed by `isize` --> Base64Decoding.rs:20:12 | 20 | sextets[i] = find(base64Codes,inputBytes[i]); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[i32]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<i32>` to implement `Index<isize>` error[E0277]: the type `[i32]` cannot be indexed by `isize` --> Base64Decoding.rs:20:45 | 20 | sextets[i] = find(base64Codes,inputBytes[i]); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[i32]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<i32>` to implement `Index<isize>` error[E0425]: cannot find function `find` in this scope --> Base64Decoding.rs:20:17 | 20 | sextets[i] = find(base64Codes,inputBytes[i]); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 20 - sextets[i] = find(base64Codes,inputBytes[i]); 20 + sextets[i] = base64Codes.find(inputBytes[i]); | error[E0425]: cannot find function `binmode` in this scope --> Base64Decoding.rs:31:2 | 31 | binmode(ofh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `getcodepoint` in this scope --> Base64Decoding.rs:36:8 | 36 | while getcodepoint(c,ifh) { | ^^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `isspace` in this scope --> Base64Decoding.rs:37:8 | 37 | if !(isspace(c)) { | ^^^^^^^ not found in this scope error[E0277]: the type `[i32]` cannot be indexed by `isize` --> Base64Decoding.rs:38:15 | 38 | inputBytes[count % 4] = c; | ^^^^^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[i32]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<i32>` to implement `Index<isize>` error[E0277]: the type `[i32]` cannot be indexed by `isize` --> Base64Decoding.rs:52:27 | 52 | putbyte(outputBytes[i],ofh); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[i32]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<i32>` to implement `Index<isize>` error[E0425]: cannot find function `putbyte` in this scope --> Base64Decoding.rs:52:7 | 52 | putbyte(outputBytes[i],ofh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> Base64Decoding.rs:59:2 | 59 | close(ifh); | ^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> Base64Decoding.rs:60:2 | 60 | close(ofh)?; | ^^^^^ not found in this scope error[E0308]: mismatched types --> Base64Decoding.rs:30:61 | 30 | fn convertFromBase64(fromFilespec:&str, toFilespec:&str) -> Result<(), Arc<dyn error::Error>> { | ----------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), Arc<dyn Error>>`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression | = note: expected enum `Result<(), Arc<(dyn std::error::Error + 'static)>>` found unit type `()` error[E0433]: failed to resolve: use of undeclared crate or module `io` --> Base64Decoding.rs:77:59 | 77 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 19 previous errors Some errors have detailed explanations: E0277, E0308, E0425, E0433. For more information about an error, try `rustc --explain E0277`. $ diff ../../data/text/UnicodeTest.utf8 output/testBase64Decoding.utf8 diff: output/testBase64Decoding.utf8: No such file or directory $ rustc Base64Decoding.rs error[E0425]: cannot find value `ofh` in this scope --> Base64Decoding.rs:31:10 | 31 | binmode(ofh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> Base64Decoding.rs:36:23 | 36 | while getcodepoint(c,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ofh` in this scope --> Base64Decoding.rs:52:30 | 52 | putbyte(outputBytes[i],ofh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> Base64Decoding.rs:59:8 | 59 | close(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ofh` in this scope --> Base64Decoding.rs:60:8 | 60 | close(ofh)?; | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> Base64Decoding.rs:67:3 | 67 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0277]: the type `[i32]` cannot be indexed by `isize` --> Base64Decoding.rs:20:12 | 20 | sextets[i] = find(base64Codes,inputBytes[i]); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[i32]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<i32>` to implement `Index<isize>` error[E0277]: the type `[i32]` cannot be indexed by `isize` --> Base64Decoding.rs:20:45 | 20 | sextets[i] = find(base64Codes,inputBytes[i]); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[i32]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<i32>` to implement `Index<isize>` error[E0425]: cannot find function `find` in this scope --> Base64Decoding.rs:20:17 | 20 | sextets[i] = find(base64Codes,inputBytes[i]); | ^^^^ not found in this scope | help: use the `.` operator to call the method `find` on `&str` | 20 - sextets[i] = find(base64Codes,inputBytes[i]); 20 + sextets[i] = base64Codes.find(inputBytes[i]); | error[E0425]: cannot find function `binmode` in this scope --> Base64Decoding.rs:31:2 | 31 | binmode(ofh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `getcodepoint` in this scope --> Base64Decoding.rs:36:8 | 36 | while getcodepoint(c,ifh) { | ^^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `isspace` in this scope --> Base64Decoding.rs:37:8 | 37 | if !(isspace(c)) { | ^^^^^^^ not found in this scope error[E0277]: the type `[i32]` cannot be indexed by `isize` --> Base64Decoding.rs:38:15 | 38 | inputBytes[count % 4] = c; | ^^^^^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[i32]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<i32>` to implement `Index<isize>` error[E0277]: the type `[i32]` cannot be indexed by `isize` --> Base64Decoding.rs:52:27 | 52 | putbyte(outputBytes[i],ofh); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[i32]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<i32>` to implement `Index<isize>` error[E0425]: cannot find function `putbyte` in this scope --> Base64Decoding.rs:52:7 | 52 | putbyte(outputBytes[i],ofh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> Base64Decoding.rs:59:2 | 59 | close(ifh); | ^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> Base64Decoding.rs:60:2 | 60 | close(ofh)?; | ^^^^^ not found in this scope error[E0308]: mismatched types --> Base64Decoding.rs:30:61 | 30 | fn convertFromBase64(fromFilespec:&str, toFilespec:&str) -> Result<(), Arc<dyn error::Error>> { | ----------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), Arc<dyn Error>>`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression | = note: expected enum `Result<(), Arc<(dyn std::error::Error + 'static)>>` found unit type `()` error[E0433]: failed to resolve: use of undeclared crate or module `io` --> Base64Decoding.rs:77:59 | 77 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 19 previous errors Some errors have detailed explanations: E0277, E0308, E0425, E0433. For more information about an error, try `rustc --explain E0277`. $ diff ../../data/images/GlowingCat.jpg output/testBase64Decoding.jpg diff: output/testBase64Decoding.jpg: No such file or directory

Solution