vv9k / mobi-rs

A crate to work with MOBI format ebooks
MIT License
38 stars 10 forks source link

lz77 decompression #2

Closed vv9k closed 5 years ago

vv9k commented 5 years ago

So the code on the branch lz77 is able to produce readable output. I'm very close to getting it right so I thought maybe someone else's pov will yield some helpful tips.

Example record

good friends now, good friends and very strong. They would help him. Baggins would pay for it. 
That was his chief thought. He hated Bilbo and cursed his name. What is more, he knew where he 
came from.â </p><p height="0pt" width="1em">âBut how did he find that out?â asked Frodo.</p>
<p height="0pt" width="1em">âWell, as for the name, Bilbo very foolishly told Gollum himself; 
and after that it would not be difficult to discover his country, once Gollum came out. Oh yes, 
he came out. His longing for the Ring proved stronger than his fear of the Orcs, or even of the 
light. After a year or two he left the mountains. You see, though still bound by desire of it, 
the Ring was no longer devouring him; he began to revive a little. He felt old, terribly old, 
yet less timid, and he was mortally hungry. </p><p height="0pt" width="1em">âLight, light of Sun 
and Moon, he still feared and hated, and he always will, I think; but he was cunning. He found 
he could hide from daylight and moonshine, and make his way swiftly and softly by dead of night 
with his pale cold eyes, and catch small frightened or unwary things. He grew stronger and bolder 
with new food and new air. He found his way into Mirkwood, as one would expect.â </p><p height="0pt" 
width="1em">âIs that where you found him?â asked Frodo.</p><a/><p height="0pt" width="1em">âI saw 
him there,â answered Gandalf, âbut before that he had wandered far, following Bilboâs trail. It was 
difficult to learn anything from him for certain, for his talk was constantly interrupted by curses 
and threats. âWhat had it got in its pocketses?â he said. âIt wouldnât say, no precious. Little 
cheat. Not a fair question. It cheated first, it did. It broke the rules. We ought to have squeezed 
it, yes precious. And we will, precious!â </p><p height="0pt" width="1em">â

But I also get some like this:

Rivcurlartsyighketopplas swed te,dmplungarto Rivnk tnk aoMfalRauroskseeinaunkamas spit;
ke glimmeras sr plbow ss yartitting utfumg. A,dmEthir A,duinard sawHen,utmonlyy deltaEye 
of tat Rikse,dmmyriadoMfalsea-birdoMwhirlas ssyighkewhite dustkse of tsu Hee,dmben Grh of 
mke greoine, usil Rivsea,eripplas sse endamelelasng i</p><pardonly="0pt"nmedth="1em">But 
everywmote.rd he lookrd saw of tsigla falrth imageMisty Mas t pla wote.crawlas ssyighanthi 
ao: orca wote.issuing out falkethouse, uuoame. UndRivnk tbougha falMirkwoohenk te. 
Thedeadundstrif Eye El Rsne, us. Ms, une asbe. Eg imageseelEye of tBeorninga wTheafl, 
ke cloud wTheo RivMoria;e m lo ros Eye of tbordRia falLórien i</p><a/><pardonly="0pt"nmedth="1em">
Horsorln wote.gk aoping oe of tgraelefalRohan;kwol Rsnpou a unrom Isengthw. From of tmed la 
falHarad shipa falrth put fut einsea;ke, uout falof ts. Eus. Mwote.moight endamelly: ss. dsrln,
spearrln, bowrln ittinhorsoain uncioresfalchieft pla e, ulad. Mw pla. A asn,utpowot falof tDark
L. d wThese  yeion image ourning wasrh ag plkrd beheTheMinTheTirith. Fth awrelite worldHee,
dmbeautiful:ewhite-wk andHemany-towotndHeproud shrunkir ittiniresmas t pl-seat;kiresbattlo
rlnresglittotndnmerh sgeelHee,dmiresourrereswote., only merh manysbannRia siopgeseapookse his
hearn sBut ag pl EusinTheTirithent. Hetuanonk tunk t aoaingreatotse,dmmxpldstrong imaithRikse.
Erthw,stswi aing his e. E Thedrawn iIt p. sld srd ruinld , oding falOsgiliGrhHen,utgrasning gatoa
falMinTheMorgulHee,dmof tmes tld Mas t plaHee,dmir he lookittinGorgoronkHen,utvk any faloerrorkse
of tLeelEye MordmenoDarksnglelrelnk te.undRivnk tSun iFite.glowookemidmof t m lo. Mas t Doom

use this as main.rs:

use mobi::Mobi;
use std::{thread, time};

fn main() {
    let m = Mobi::init("/home/wojtek/Downloads/lotr.mobi").unwrap();
    let ten_millis = time::Duration::from_millis(70);
    let mut count = 0;
    for record in m.records {
        if count == 50 {
            break;
        }
        println!("{}", record.record_data);
        thread::sleep(ten_millis);
        count += 1;
    }
}

This is the type of output it's able to produce. You can check it out by switching to lz77 branch.

vv9k commented 5 years ago

Managed to solve it :)