r/dailyprogrammer • u/fvandepitte 0 0 • Aug 16 '16
[2016-08-16] Challenge #279 [Easy] Uuencoding
You are trapped at uninhabited island only with your laptop. Still you don't want your significant other to worry about you, so you are going to send a message in a bottle with your picture or at least a couple of words from you (sure, you could just write down the words, but that would be less fun). You're going to use uuencoding for that.
Uuencoding is a form of binary-to-text encoding, which uses only symbols from 32-95 diapason, which means all symbols used in the encoding are printable.
Description of encoding
A uuencoded file starts with a header line of the form:
begin <mode> <file><newline><mode> is the file's Unix file permissions as three octal digits (e.g. 644, 744). For Windows 644 is always used.
<file> is the file name to be used when recreating the binary data.
<newline> signifies a newline character, used to terminate each line.
Each data line uses the format:
<length character><formatted characters><newline><length character> is a character indicating the number of data bytes which have been encoded on that line. This is an ASCII character determined by adding 32 to the actual byte count, with the sole exception of a grave accent "`" (ASCII code 96) signifying zero bytes. All data lines except the last (if the data was not divisible by 45), have 45 bytes of encoded data (60 characters after encoding). Therefore, the vast majority of length values is 'M', (32 + 45 = ASCII code 77 or "M").
<formatted characters> are encoded characters.
The mechanism of uuencoding repeats the following for every 3 bytes (if there are less than 3 bytes left, trailing 0 are added):
- Start with 3 bytes from the source, 24 bits in total.
- Split into 4 6-bit groupings, each representing a value in the range 0 to 63: bits (00-05), (06-11), (12-17) and (18-23).
- Add 32 to each of the values. With the addition of 32 this means that the possible results can be between 32 (" " space) and 95 ("_" underline). 96 ("`" grave accent) as the "special character" is a logical extension of this range.
- Output the ASCII equivalent of these numbers.
For example, we want to encode a word "Cat". ASCII values for C,a,t are 67,97,116, or 010000110110000101110100 in binary. After dividing into four groups, we get 010000 110110 000101 110100, which is 16,54,5,52 in decimal. Adding 32 to this values and encoding back in ASCII, the final result is 0V%T.
The file ends with two lines:
`<newline>
end<newline>Formal Inputs & Outputs
Input
a byte array or string.
Output
a string containing uuencoded input.
Examples
Input: Cat
Output:
begin 644 cat.txt
#0V%T
`
endInput: I feel very strongly about you doing duty. Would you give me a little more documentation about your reading in French? I am glad you are happy — but I never believe much in happiness. I never believe in misery either. Those are things you see on the stage or the screen or the printed pages, they never really happen to you in life.
Output:
begin 644 file.txt
M22!F965L('9E<GD@<W1R;VYG;'D@86)O=70@>6]U(&1O:6YG(&1U='DN(%=O
M=6QD('EO=2!G:79E(&UE(&$@;&ET=&QE(&UO<F4@9&]C=6UE;G1A=&EO;B!A
M8F]U="!Y;W5R(')E861I;F<@:6X@1G)E;F-H/R!)(&%M(&=L860@>6]U(&%R
M92!H87!P>2#B@)0@8G5T($D@;F5V97(@8F5L:65V92!M=6-H(&EN(&AA<'!I
M;F5S<RX@22!N979E<B!B96QI979E(&EN(&UI<V5R>2!E:71H97(N(%1H;W-E
M(&%R92!T:&EN9W,@>6]U('-E92!O;B!T:&4@<W1A9V4@;W(@=&AE('-C<F5E
M;B!O<B!T:&4@<')I;G1E9"!P86=E<RP@=&AE>2!N979E<B!R96%L;'D@:&%P
3<&5N('1O('EO=2!I;B!L:69E+C P
`
endBonuses
Bonus 1
Write uudecoder, which decodes uuencoded input back to a byte array or string
Bonus 2
Write encoder for files as well.
Bonus 3
Make encoding parallel.
Further Reading
Binary-to-text encoding on Wikipedia.
Finally
This challenge is posted by /u/EvgeniyZh
Also have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
8
u/rakkar16 Aug 16 '16
Python 3
:^)
Don't worry. A serious solution is incoming.
5
u/andriii25 Aug 16 '16
Java with Bonus 2, may do the others too later.
Tested with running it on its .class file, and
diffshowed no difference from the actualuuencode, so I guess it works.One thing to note though: There is certainly a difference between trailing '0' (0x30 = 48) and trailing 0 (0x00 = 0).
uuencodeseems to use the latter so does my code, that's why the end of my output is different.Suggestions are always welcome and appreciated
8
u/lukz 2 0 Aug 16 '16 edited Aug 16 '16
Z80 assembly
Solving this in assembly, which makes it more difficult, so I will simplify the problem a bit for myself by only allowing input of maximal size 40 characters so that the output is only one line.
Doing this for Sharp MZ-800 8-bit computer, so there is no fancy stuff available like input from files. Instead, you type in the input as a sequence of hex codes, two characters encode 8 bits, i.e. 43 encodes 'C'.
The program will be loaded into computer from address 1200h. Run the program from address 1200h, type in the hex codes of input and the program prints one line of text containing the encoded data.
Program length is 86 bytes.
Example session:
Screenshot
1
u/[deleted] Aug 16 '16
Python 3
Without bonus. Maybe I will add them later.
I'm not sure about the "trailing zeros" thing. I just add "0" characters at the end when needing. Is it good?
2
u/leme88 Aug 16 '16 edited Aug 16 '16
Dart
You can try it in dartpad
It's not beautiful but it works. I used - instead of — as well.
Output:
2
u/[deleted] Aug 16 '16
It seems like the 2nd output is broken.
Python yields:
Instead the last line of the output should be
2
u/kira4 Aug 16 '16
C
Pretty messy and not very optimized either. I think it works but the dash in the example was messing things up.
2
u/Godspiral 3 3 Aug 17 '16
in J,
2
u/Rivaw Aug 18 '16 edited Aug 18 '16
My solution in Java. I've done it using just strings and chars, I would really like to actually KNOW what the other solutions are doing with binary operators, I should really look into it and do this the "right way".
Criticism is welcome.
Output:
There is something wrong with the example output: the character — is out of the bounds defined in the problem, so I, like others, have used '-' instead. But, there's one thing wrong in my output, in the last line, the first character denoting the length of the encoded bytes is 1, while others' is a 2, counting the bytes in the final answer it should be one, but I'd like someone to explain that to me.
1
u/13467 1 1 Aug 16 '16
In CJam:
Try it online.
1
u/skeeto -9 8 Aug 16 '16 edited Aug 16 '16
C, encoder and decoder. For simplicity, it completely dicards filenames and modes, operating only on standard input and output.
encode.c:
decode.c:
The third bonus is pointless since this kind of program is entirely IO-bound on any machine that would support parallelism.
Interesting side note: I also built this with Visual Studio 2015 and ran into their lovely fgets bug: stream functions attached to pipes quietly lose data after N lines of input, where N is a small random number. Pretty scary, and makes me wonder if anything in their UCRT can be trusted to work correctly in production.
1
u/goggthegrunt Aug 16 '16
My Python 2 implementation. No bonuses yet. It's a little sloppy. I initially ran into issues with the box drawing character (—), but I learned how to specify the encoding today and it worked after setting it to unicode!
1
u/LiveOnTheSun Aug 16 '16
Wanted to get the something working before bed so here's my work in progress in J. Need to go through 3 bytes at a time for longer input and sort out the length character still.
Ouput:
1
u/DrEuclidean Aug 16 '16
programmed in C, no bonuses would love feedback //main.c
1
u/Badel2 Aug 17 '16
Rust encoder. Only accepts input from stdin, but it looks pretty good for a begginer like me.
1
u/weekendblues Aug 17 '16 edited Aug 17 '16
Haskell
Not the cleanest (and probably not the most efficient), but I'm still learning. Suggestions welcome! Edit: reformatted slightly for clarity.
Output: (I also opted to use "-" instead of "—")
1
u/iamnotposting Aug 17 '16
Rust, encoder and decoder. i mainly wrote it this way as an excuse to use impl Iterator. I probably could have made it more iterator-heavy if i thought about it longer.
1
u/sad_panda91 Aug 17 '16
So I tried it in Python and it works-ish. I get the right answer for 'Cat' but not the right answer for the long text. Can someone help?
1
u/SethDusek5 Aug 17 '16
Rust
Does bonus 2 and 3, supports parallelism. Here are some benchmarks using data from /dev/urandom
Siingle threaded: test uuencode_bench ... bench: 134,981,171 ns/iter (+/- 39,809,674)
Multi threaded: test uuencode_bench ... bench: 78,444,879 ns/iter (+/- 9,709,473)
1
u/gju_ Aug 18 '16
Clojure
I'm not too happy with it.
1
u/[deleted] Aug 19 '16 edited Aug 19 '16
Crystal, no bonus. Super imperative, but should be as efficient as possible:
1
u/animejunkied Aug 19 '16 edited Aug 19 '16
In Python 3. Also had to change the — character to - because I was getting a UnicodeDecodeError. Overall I'd say this isn't my best code.
Output: (note space at the last line)
1
u/[deleted] Aug 19 '16
C# with Bonus1&2 hardcoded paths..was too lazy to ask user to give me file name etc..
1
u/iKyriaki Aug 20 '16 edited Aug 20 '16
C++11. I decided to give one of these a shot, but I cannot manage to figure out how to add the lengthcharacter.
Input:
Output:
1
u/Di0ny Aug 21 '16 edited Aug 21 '16
Java
I did TDD and I tried to write clean readable code. I had some issues with the "—" in the example text, so I used "-" for now. Criticism is welcome. Here is the code:
And here are the tests:
1
u/[deleted] Aug 22 '16
Python 3: Partially working solution.
This is my first attempt at one of these challenges. I’m new to coding. This is what I’ve been able to do after working with Python for about 5 weeks.
It encodes properly for the first few lines, and then goes wrong somewhere. I got stuck trying to figure out where I went wrong.
At this point I just wanted to submit something to get some feedback. I’d love suggestions on what to focus on to improve this. Modules, algorithms, coding techniques to learn? Right now I don’t even know what I don’t know! :)
1
u/juanchi35 Aug 25 '16
c++11, includes bonus 1 and 2
Input: I feel very strongly about you doing duty. Would you give me a little more documentation about your reading in French? I am glad you are happy — but I never believe much in happiness. I never believe in misery either. Those are things you see on the stage or the screen or the printed pages, they never really happen to you in life.
Output:
Output isn't the same as the one in the example, but some people got the same output as me so I guess it is right, otherwise correct me please (:
1
u/confused_banda Aug 26 '16
Go solution for Bonus 1 and 2. I'd love to hear feedback from you guys on this, since it's one my first useful Go programs.
1
u/evil_rabbit Aug 28 '16
LiveScript - Bonus 1.
1
u/Maplicant Sep 04 '16 edited Sep 04 '16
I've made one in Rust with bonus 2
https://github.com/Maplicant/uuencode-rust
It currently does 3Mbps, so there's still some improvement to be made.
It's buffered, though. Only uses 0.2MB of RAM.
1
u/KingRodian Sep 18 '16 edited Sep 18 '16
C++ This turned quite a bit larger than I had expected. It is commandline driven (unix only because of getopt) and can take input 2 ways, -i for interactively until EOF ctrl input and -f filename for reading files. These can be used several times and in any order and will make one encoded bulk of text. It can also decode a file.
And here is use:
1
u/Tetsumi- 1 0 Oct 24 '16
Racket with bonus 2
1
u/elpasmo Dec 06 '16
I think there is an error with the second example:
Last line of encoding is not:
As the challenge states. I've found it is:
with a trailing whitespace. That decoded is:
Edit: formatting.
0
u/[deleted] Aug 16 '16
[deleted] — view deleted comment