| 187 | } |
| 188 | |
| 189 | bool LZSSUtil::Encode( |
| 190 | const lUInt8 * in_buf, |
| 191 | int in_length, |
| 192 | lUInt8 * out_buf, |
| 193 | int & out_length |
| 194 | ) |
| 195 | { |
| 196 | InBuf in( in_buf, in_length ); |
| 197 | OutBuf out( out_buf, out_length ); |
| 198 | int i, len, r, s, last_match_length, code_buf_ptr; |
| 199 | lUInt32 c; |
| 200 | lUInt8 code_buf[17], mask; |
| 201 | code_buf[0] = 0; /* code_buf[1..16] saves eight units of code, and |
| 202 | code_buf[0] works as eight flags, "1" representing that the unit |
| 203 | is an unencoded letter (1 byte), "0" a position-and-length pair |
| 204 | (2 bytes). Thus, eight units require at most 16 bytes of code. */ |
| 205 | code_buf_ptr = mask = 1; |
| 206 | s = 0; r = N - F; |
| 207 | for (i = s; i < r; i++) |
| 208 | text_buf[i] = ' '; /* Clear the buffer with |
| 209 | any character that will appear often. */ |
| 210 | for (len = 0; len < F && in.get(c); len++) |
| 211 | text_buf[r + len] = (lUInt8)c; /* Read F bytes into the last F bytes of |
| 212 | the buffer */ |
| 213 | if ((textsize = len) == 0) |
| 214 | return false; /* text of size zero */ |
| 215 | //for (i = 1; i <= F; i++) InsertNode(r - i); |
| 216 | /* Insert the F strings, |
| 217 | each of which begins with one or more 'space' characters. Note |
| 218 | the order in which these strings are inserted. This way, |
| 219 | degenerate trees will be less likely to occur. */ |
| 220 | InsertNode(r); /* Finally, insert the whole string just read. The |
| 221 | global variables match_length and match_position are set. */ |
| 222 | do { |
| 223 | if (match_length > len) match_length = len; /* match_length |
| 224 | may be spuriously long near the end of text. */ |
| 225 | if (match_length <= THRESHOLD) { |
| 226 | match_length = 1; /* Not long enough match. Send one byte. */ |
| 227 | code_buf[0] |= mask; /* 'send one byte' flag */ |
| 228 | code_buf[code_buf_ptr++] = text_buf[r]; /* Send uncoded. */ |
| 229 | } else { |
| 230 | code_buf[code_buf_ptr++] = (lUInt8) match_position; |
| 231 | code_buf[code_buf_ptr++] = (lUInt8) |
| 232 | (((match_position >> 4) & 0xf0) |
| 233 | | (match_length - (THRESHOLD + 1))); /* Send position and |
| 234 | length pair. Note match_length > THRESHOLD. */ |
| 235 | } |
| 236 | if ((mask <<= 1) == 0) { /* Shift mask left one bit. */ |
| 237 | for (i = 0; i < code_buf_ptr; i++) /* Send at most 8 units of */ |
| 238 | out.put(code_buf[i]); /* code together */ |
| 239 | codesize += code_buf_ptr; |
| 240 | code_buf[0] = 0; code_buf_ptr = mask = 1; |
| 241 | } |
| 242 | last_match_length = match_length; |
| 243 | for (i = 0; i < last_match_length && in.get(c); i++) { |
| 244 | DeleteNode(s); /* Delete old strings and */ |
| 245 | text_buf[s] = (lUInt8)c; /* read new bytes */ |
| 246 | if (s < F - 1) |
no test coverage detected