Hello. I couldn't understand ratio implementation. I think that there are some overflow issues in current implementation. Please correct me if I am wrong.
I see the main idea: we have source and destination.
Ratio equals to source_length / destination_length = s / d.
New ratio (s + 2) / (d + 1) is good, (s + 1) / (d + 2) is bad.
So we want to reset when new_ratio < old_ratio.
We won't reset when (s + 2) / (d + 1) > s / d, we will reset when (s + 1) / (d + 2) < s / d.
Than we added 10000 bytes lag for source_length to receive more consolidated ratio.
I see implementation for this algorithm in ruby in rb-compress-lzw. I have no questions about this implementation because there is a gmp library behind it, it will never overflow.
Now I am trying to read implementation in void compress(fdin, fdout).
I see manipulations:
int bytes_out = 0; bytes_in = 0
bytes_out += OBUFSIZ
bytes_out += (outbits+7)>>3
bytes_in += i
if (rpos > rlop) bytes_in += rpos-rlop
Lets imagine large input.
Both bytes_in and bytes_out will overflow.
When dictionary will be filled, we will use bytes_in and bytes_out to count wrong ratio.
I see same problems here.
rat = (bytes_out+(outbits>>3)) >> 8;: bytes_out + (outbits >> 3) can provide overflow and rat will be invalid.
How to fix it?
I have not yet invented any solution =)
Hello. I couldn't understand ratio implementation. I think that there are some overflow issues in current implementation. Please correct me if I am wrong.
I see the main idea: we have source and destination.
Ratio equals to
source_length / destination_length=s / d.New ratio
(s + 2) / (d + 1)is good,(s + 1) / (d + 2)is bad.So we want to reset when
new_ratio < old_ratio.We won't reset when
(s + 2) / (d + 1) > s / d, we will reset when(s + 1) / (d + 2) < s / d.Than we added
10000bytes lag forsource_lengthto receive more consolidated ratio.I see implementation for this algorithm in ruby in rb-compress-lzw. I have no questions about this implementation because there is a gmp library behind it, it will never overflow.
Now I am trying to read implementation in
void compress(fdin, fdout).I see manipulations:
int bytes_out = 0; bytes_in = 0bytes_out += OBUFSIZbytes_out += (outbits+7)>>3bytes_in += iif (rpos > rlop) bytes_in += rpos-rlopLets imagine large input.
Both
bytes_inandbytes_outwill overflow.When dictionary will be filled, we will use
bytes_inandbytes_outto count wrong ratio.I see same problems here.
rat = (bytes_out+(outbits>>3)) >> 8;:bytes_out + (outbits >> 3)can provide overflow andratwill be invalid.How to fix it?
I have not yet invented any solution =)