src/mame/layout/30test.lay
| r14439 | r14440 | |
| 1 | 1 | <?xml version="1.0"?> |
| 2 | 2 | <mamelayout version="2"> |
| 3 | | <element name="digit" defstate="10"> |
| 3 | |
| 4 | <!-- define elements --> |
| 5 | |
| 6 | <element name="digit" defstate="0"> |
| 4 | 7 | <led7seg> |
| 5 | | <color red="1.0" green="0.0" blue="0.0" /> |
| 8 | <color red="1.0" green="0.1" blue="0.1" /> |
| 6 | 9 | </led7seg> |
| 7 | 10 | </element> |
| 8 | 11 | |
| 9 | | <element name="rank"> |
| 10 | | <rect state="1"> |
| 11 | | <color red="1.0" green="0.0" blue="0.0" /> |
| 12 | <element name="disk_lamp"> |
| 13 | <disk state="1"> |
| 14 | <color red="1.0" green="0.1" blue="0.1" /> |
| 15 | </disk> |
| 16 | <disk state="0"> |
| 17 | <color red="0.1" green="0.0" blue="0.0" /> |
| 18 | </disk> |
| 19 | </element> |
| 20 | |
| 21 | <element name="disk_blue"> |
| 22 | <disk> |
| 23 | <color red="0.1" green="0.35" blue="0.8" /> |
| 24 | </disk> |
| 25 | </element> |
| 26 | <element name="disk_red"> |
| 27 | <disk> |
| 28 | <color red="0.8" green="0.1" blue="0.0" /> |
| 29 | </disk> |
| 30 | </element> |
| 31 | |
| 32 | <element name="static_black"> |
| 33 | <rect> |
| 34 | <color red="0.0" green="0.0" blue="0.0" /> |
| 12 | 35 | </rect> |
| 36 | </element> |
| 37 | <element name="static_blue"> |
| 38 | <rect> |
| 39 | <color red="0.1" green="0.35" blue="0.8" /> |
| 40 | </rect> |
| 41 | </element> |
| 42 | <element name="static_yellow"> |
| 43 | <rect> |
| 44 | <color red="0.95" green="0.8" blue="0.1" /> |
| 45 | </rect> |
| 46 | </element> |
| 47 | <element name="static_red"> |
| 48 | <rect> |
| 49 | <color red="0.8" green="0.1" blue="0.0" /> |
| 50 | </rect> |
| 51 | </element> |
| 52 | |
| 53 | <!-- note: cab has a white button underneath, --> |
| 54 | <!-- make the border white-ish instead (normally red), --> |
| 55 | <!-- implying that it should be clickable --> |
| 56 | <element name="led_box" defstate="0"> |
| 13 | 57 | <rect state="0"> |
| 14 | | <color red="0.1" green="0.0" blue="0.0" /> |
| 58 | <color red="0.7" green="0.7" blue="0.8" /> |
| 59 | <bounds x="0" y="0" width="30" height="25" /> |
| 15 | 60 | </rect> |
| 61 | <rect state="0"> |
| 62 | <color red="0.0" green="0.0" blue="0.0" /> |
| 63 | <bounds x="2" y="2" width="26" height="21" /> |
| 64 | </rect> |
| 65 | <rect state="0"> |
| 66 | <color red="0.0" green="0.0" blue="0.0" /> |
| 67 | <bounds x="2" y="2" width="26" height="21" /> |
| 68 | </rect> |
| 69 | <rect state="1"> |
| 70 | <color red="1.0" green="0.7" blue="0.8" /> |
| 71 | <bounds x="1" y="1" width="28" height="23" /> |
| 72 | </rect> |
| 73 | <rect state="1"> |
| 74 | <color red="0.0" green="0.0" blue="0.0" /> |
| 75 | <bounds x="3" y="3" width="24" height="19" /> |
| 76 | </rect> |
| 16 | 77 | </element> |
| 17 | 78 | |
| 18 | | <view name="Simple LEDs"> |
| 19 | | <bounds left="0" top="0" right="3.4" bottom="4.3" /> |
| 20 | 79 | |
| 80 | <!-- define text --> |
| 21 | 81 | |
| 22 | | <!-- the grid --> |
| 82 | <element name="text_score"> |
| 83 | <text string="SCORE"> |
| 84 | <color red="0.1" green="0.35" blue="0.8" /> |
| 85 | </text> |
| 86 | </element> |
| 87 | <element name="text_time"> |
| 88 | <text string="TIME"> |
| 89 | <color red="0.1" green="0.35" blue="0.8" /> |
| 90 | </text> |
| 91 | </element> |
| 92 | <element name="text_best"> |
| 93 | <text string="BEST"> |
| 94 | <color red="0.1" green="0.35" blue="0.8" /> |
| 95 | </text> |
| 96 | </element> |
| 23 | 97 | |
| 24 | | <bezel name="digit0" element="digit"> |
| 25 | | <bounds x="0.0" y="1.3" width="0.2" height="0.3" /> |
| 26 | | </bezel> |
| 98 | <element name="text_1"> |
| 99 | <text string="1." align="2"> |
| 100 | <color red="0.1" green="0.35" blue="0.8" /> |
| 101 | </text> |
| 102 | </element> |
| 103 | <element name="text_2"> |
| 104 | <text string="2." align="2"> |
| 105 | <color red="0.1" green="0.35" blue="0.8" /> |
| 106 | </text> |
| 107 | </element> |
| 108 | <element name="text_3"> |
| 109 | <text string="3." align="2"> |
| 110 | <color red="0.1" green="0.35" blue="0.8" /> |
| 111 | </text> |
| 112 | </element> |
| 27 | 113 | |
| 28 | | <bezel name="digit1" element="digit"> |
| 29 | | <bounds x="0.2" y="1.3" width="0.2" height="0.3" /> |
| 30 | | </bezel> |
| 114 | <element name="text_rank1"> |
| 115 | <text string="great!!" align="1"> |
| 116 | <color red="0.1" green="0.35" blue="0.8" /> |
| 117 | </text> |
| 118 | </element> |
| 119 | <element name="text_rank2"> |
| 120 | <text string="pretty good" align="1"> |
| 121 | <color red="0.1" green="0.35" blue="0.8" /> |
| 122 | </text> |
| 123 | </element> |
| 124 | <element name="text_rank3"> |
| 125 | <text string="not bad" align="1"> |
| 126 | <color red="0.1" green="0.35" blue="0.8" /> |
| 127 | </text> |
| 128 | </element> |
| 129 | <element name="text_rank4"> |
| 130 | <text string="mediocre" align="1"> |
| 131 | <color red="0.1" green="0.35" blue="0.8" /> |
| 132 | </text> |
| 133 | </element> |
| 134 | <element name="text_rank5"> |
| 135 | <text string="pretty bad" align="1"> |
| 136 | <color red="0.1" green="0.35" blue="0.8" /> |
| 137 | </text> |
| 138 | </element> |
| 139 | <element name="text_rank6"> |
| 140 | <text string="awful" align="1"> |
| 141 | <color red="0.1" green="0.35" blue="0.8" /> |
| 142 | </text> |
| 143 | </element> |
| 31 | 144 | |
| 145 | <element name="text_gameover"> |
| 146 | <text string="game over" align="1"> |
| 147 | <color red="0.1" green="0.35" blue="0.8" /> |
| 148 | </text> |
| 149 | </element> |
| 32 | 150 | |
| 33 | | <bezel name="digit2" element="digit"> |
| 34 | | <bounds x="0.6" y="1.3" width="0.2" height="0.3" /> |
| 35 | | </bezel> |
| 36 | 151 | |
| 37 | | <bezel name="digit3" element="digit"> |
| 38 | | <bounds x="0.8" y="1.3" width="0.2" height="0.3" /> |
| 39 | | </bezel> |
| 40 | 152 | |
| 41 | 153 | |
| 42 | | <bezel name="digit4" element="digit"> |
| 43 | | <bounds x="1.2" y="1.3" width="0.2" height="0.3" /> |
| 44 | | </bezel> |
| 45 | 154 | |
| 46 | | <bezel name="digit5" element="digit"> |
| 47 | | <bounds x="1.4" y="1.3" width="0.2" height="0.3" /> |
| 155 | <view name="Internal Layout"> |
| 156 | <bounds left="0" top="0" right="246" bottom="246" /> |
| 157 | <bezel element="static_blue"> |
| 158 | <bounds left="0" right="246" top="0" bottom="246" /> |
| 48 | 159 | </bezel> |
| 49 | 160 | |
| 50 | 161 | |
| 51 | | <bezel name="digit6" element="digit"> |
| 52 | | <bounds x="1.8" y="1.3" width="0.2" height="0.3" /> |
| 162 | <!-- info box --> |
| 163 | |
| 164 | <bezel element="static_red"> |
| 165 | <bounds x="18" y="5" width="210" height="69" /> |
| 53 | 166 | </bezel> |
| 54 | 167 | |
| 55 | | <bezel name="digit7" element="digit"> |
| 56 | | <bounds x="2.0" y="1.3" width="0.2" height="0.3" /> |
| 168 | <bezel element="static_blue"> |
| 169 | <bounds x="18" y="5" width="4" height="4" /> |
| 57 | 170 | </bezel> |
| 171 | <bezel element="disk_red"> |
| 172 | <bounds x="18" y="5" width="8" height="8" /> |
| 173 | </bezel> |
| 58 | 174 | |
| 59 | | |
| 60 | | <bezel name="digit8" element="digit"> |
| 61 | | <bounds x="2.4" y="1.3" width="0.2" height="0.3" /> |
| 175 | <bezel element="static_blue"> |
| 176 | <bounds x="18" y="70" width="4" height="4" /> |
| 62 | 177 | </bezel> |
| 178 | <bezel element="disk_red"> |
| 179 | <bounds x="18" y="66" width="8" height="8" /> |
| 180 | </bezel> |
| 63 | 181 | |
| 64 | | <bezel name="digit9" element="digit"> |
| 65 | | <bounds x="2.6" y="1.3" width="0.2" height="0.3" /> |
| 182 | <bezel element="static_blue"> |
| 183 | <bounds x="224" y="5" width="4" height="4" /> |
| 66 | 184 | </bezel> |
| 185 | <bezel element="disk_red"> |
| 186 | <bounds x="220" y="5" width="8" height="8" /> |
| 187 | </bezel> |
| 67 | 188 | |
| 68 | | |
| 69 | | <bezel name="digit10" element="digit"> |
| 70 | | <bounds x="3.0" y="1.3" width="0.2" height="0.3" /> |
| 189 | <bezel element="static_blue"> |
| 190 | <bounds x="224" y="70" width="4" height="4" /> |
| 71 | 191 | </bezel> |
| 192 | <bezel element="disk_red"> |
| 193 | <bounds x="220" y="66" width="8" height="8" /> |
| 194 | </bezel> |
| 72 | 195 | |
| 73 | | <bezel name="digit11" element="digit"> |
| 74 | | <bounds x="3.2" y="1.3" width="0.2" height="0.3" /> |
| 196 | <bezel element="static_yellow"> |
| 197 | <bounds x="20" y="7" width="206" height="65" /> |
| 75 | 198 | </bezel> |
| 76 | 199 | |
| 77 | 200 | |
| 201 | <!-- score --> |
| 78 | 202 | |
| 79 | | |
| 80 | | <bezel name="digit12" element="digit"> |
| 81 | | <bounds x="0.0" y="1.9" width="0.2" height="0.3" /> |
| 203 | <bezel element="text_score"> |
| 204 | <bounds x="25" y="7" width="37.4" height="7" /> |
| 82 | 205 | </bezel> |
| 83 | 206 | |
| 84 | | <bezel name="digit13" element="digit"> |
| 85 | | <bounds x="0.2" y="1.9" width="0.2" height="0.3" /> |
| 207 | <bezel element="static_blue"> |
| 208 | <bounds x="25" y="14.3" width="37.4" height="28.9" /> |
| 86 | 209 | </bezel> |
| 87 | | |
| 88 | | |
| 89 | | <bezel name="digit14" element="digit"> |
| 90 | | <bounds x="0.6" y="1.9" width="0.2" height="0.3" /> |
| 210 | <bezel element="static_black"> |
| 211 | <bounds x="25.7" y="15" width="36" height="27.5" /> |
| 91 | 212 | </bezel> |
| 92 | 213 | |
| 93 | | <bezel name="digit15" element="digit"> |
| 94 | | <bounds x="0.8" y="1.9" width="0.2" height="0.3" /> |
| 214 | <bezel name="digit70" element="digit"> |
| 215 | <bounds x="26.7" y="16" width="17" height="25.5" /> |
| 95 | 216 | </bezel> |
| 96 | | |
| 97 | | |
| 98 | | <bezel name="digit16" element="digit"> |
| 99 | | <bounds x="1.2" y="1.9" width="0.2" height="0.3" /> |
| 217 | <bezel name="digit71" element="digit"> |
| 218 | <bounds x="43.7" y="16" width="17" height="25.5" /> |
| 100 | 219 | </bezel> |
| 101 | 220 | |
| 102 | | <bezel name="digit17" element="digit"> |
| 103 | | <bounds x="1.4" y="1.9" width="0.2" height="0.3" /> |
| 104 | | </bezel> |
| 105 | 221 | |
| 222 | <!-- time --> |
| 106 | 223 | |
| 107 | | <bezel name="digit18" element="digit"> |
| 108 | | <bounds x="1.8" y="1.9" width="0.2" height="0.3" /> |
| 224 | <bezel element="text_time"> |
| 225 | <bounds x="22.3" y="43" width="43.4" height="7" /> |
| 109 | 226 | </bezel> |
| 110 | 227 | |
| 111 | | <bezel name="digit19" element="digit"> |
| 112 | | <bounds x="2.0" y="1.9" width="0.2" height="0.3" /> |
| 228 | <bezel element="static_blue"> |
| 229 | <bounds x="22.3" y="50.3" width="43.4" height="18.4" /> |
| 113 | 230 | </bezel> |
| 231 | <bezel element="static_black"> |
| 232 | <bounds x="23" y="51" width="42" height="17" /> |
| 233 | </bezel> |
| 114 | 234 | |
| 115 | | |
| 116 | | <bezel name="digit20" element="digit"> |
| 117 | | <bounds x="2.4" y="1.9" width="0.2" height="0.3" /> |
| 235 | <bezel name="digit60" element="digit"> |
| 236 | <bounds x="24" y="52" width="10" height="15" /> |
| 118 | 237 | </bezel> |
| 119 | 238 | |
| 120 | | <bezel name="digit21" element="digit"> |
| 121 | | <bounds x="2.6" y="1.9" width="0.2" height="0.3" /> |
| 239 | <bezel name="digit61" element="digit"> |
| 240 | <bounds x="34" y="52" width="10" height="15" /> |
| 122 | 241 | </bezel> |
| 123 | 242 | |
| 124 | | |
| 125 | | <bezel name="digit22" element="digit"> |
| 126 | | <bounds x="3.0" y="1.9" width="0.2" height="0.3" /> |
| 243 | <bezel name="digit62" element="digit"> |
| 244 | <bounds x="44" y="52" width="10" height="15" /> |
| 127 | 245 | </bezel> |
| 128 | 246 | |
| 129 | | <bezel name="digit23" element="digit"> |
| 130 | | <bounds x="3.2" y="1.9" width="0.2" height="0.3" /> |
| 247 | <bezel name="digit63" element="digit"> |
| 248 | <bounds x="54" y="52" width="10" height="15" /> |
| 131 | 249 | </bezel> |
| 132 | 250 | |
| 133 | 251 | |
| 252 | <!-- today's highscores --> |
| 134 | 253 | |
| 135 | | |
| 136 | | <bezel name="digit24" element="digit"> |
| 137 | | <bounds x="0.0" y="2.5" width="0.2" height="0.3" /> |
| 254 | <bezel element="text_best"> |
| 255 | <bounds x="73.3" y="7" width="23.4" height="7" /> |
| 138 | 256 | </bezel> |
| 139 | 257 | |
| 140 | | <bezel name="digit25" element="digit"> |
| 141 | | <bounds x="0.2" y="2.5" width="0.2" height="0.3" /> |
| 258 | <bezel element="text_1"> |
| 259 | <bounds x="63" y="25.5" width="10" height="7" /> |
| 142 | 260 | </bezel> |
| 143 | | |
| 144 | | |
| 145 | | <bezel name="digit26" element="digit"> |
| 146 | | <bounds x="0.6" y="2.5" width="0.2" height="0.3" /> |
| 261 | <bezel element="text_2"> |
| 262 | <bounds x="63" y="43.5" width="10" height="7" /> |
| 147 | 263 | </bezel> |
| 148 | | |
| 149 | | <bezel name="digit27" element="digit"> |
| 150 | | <bounds x="0.8" y="2.5" width="0.2" height="0.3" /> |
| 264 | <bezel element="text_3"> |
| 265 | <bounds x="63" y="61.5" width="10" height="7" /> |
| 151 | 266 | </bezel> |
| 152 | 267 | |
| 153 | | |
| 154 | | <bezel name="digit28" element="digit"> |
| 155 | | <bounds x="1.2" y="2.5" width="0.2" height="0.3" /> |
| 268 | <bezel element="static_blue"> |
| 269 | <bounds x="73.3" y="14.3" width="23.4" height="54.4" /> |
| 156 | 270 | </bezel> |
| 157 | 271 | |
| 158 | | <bezel name="digit29" element="digit"> |
| 159 | | <bounds x="1.4" y="2.5" width="0.2" height="0.3" /> |
| 272 | <bezel element="static_black"> |
| 273 | <bounds x="74" y="15" width="22" height="17" /> |
| 160 | 274 | </bezel> |
| 161 | | |
| 162 | | |
| 163 | | <bezel name="digit30" element="digit"> |
| 164 | | <bounds x="1.8" y="2.5" width="0.2" height="0.3" /> |
| 275 | <bezel element="static_black"> |
| 276 | <bounds x="74" y="33" width="22" height="17" /> |
| 165 | 277 | </bezel> |
| 166 | | |
| 167 | | <bezel name="digit31" element="digit"> |
| 168 | | <bounds x="2.0" y="2.5" width="0.2" height="0.3" /> |
| 278 | <bezel element="static_black"> |
| 279 | <bounds x="74" y="51" width="22" height="17" /> |
| 169 | 280 | </bezel> |
| 170 | 281 | |
| 171 | | |
| 172 | | <bezel name="digit32" element="digit"> |
| 173 | | <bounds x="2.4" y="2.5" width="0.2" height="0.3" /> |
| 282 | <bezel name="digit64" element="digit"> |
| 283 | <bounds x="75" y="16" width="10" height="15" /> |
| 174 | 284 | </bezel> |
| 175 | | |
| 176 | | <bezel name="digit33" element="digit"> |
| 177 | | <bounds x="2.6" y="2.5" width="0.2" height="0.3" /> |
| 285 | <bezel name="digit65" element="digit"> |
| 286 | <bounds x="85" y="16" width="10" height="15" /> |
| 178 | 287 | </bezel> |
| 179 | 288 | |
| 180 | | |
| 181 | | <bezel name="digit34" element="digit"> |
| 182 | | <bounds x="3.0" y="2.5" width="0.2" height="0.3" /> |
| 289 | <bezel name="digit66" element="digit"> |
| 290 | <bounds x="75" y="34" width="10" height="15" /> |
| 183 | 291 | </bezel> |
| 292 | <bezel name="digit67" element="digit"> |
| 293 | <bounds x="85" y="34" width="10" height="15" /> |
| 294 | </bezel> |
| 184 | 295 | |
| 185 | | <bezel name="digit35" element="digit"> |
| 186 | | <bounds x="3.2" y="2.5" width="0.2" height="0.3" /> |
| 296 | <bezel name="digit68" element="digit"> |
| 297 | <bounds x="75" y="52" width="10" height="15" /> |
| 187 | 298 | </bezel> |
| 299 | <bezel name="digit69" element="digit"> |
| 300 | <bounds x="85" y="52" width="10" height="15" /> |
| 301 | </bezel> |
| 188 | 302 | |
| 189 | 303 | |
| 304 | <!-- ranking --> |
| 190 | 305 | |
| 191 | | |
| 192 | | <bezel name="digit36" element="digit"> |
| 193 | | <bounds x="0.0" y="3.1" width="0.2" height="0.3" /> |
| 306 | <bezel element="disk_blue"> |
| 307 | <bounds x="102.75" y="15.75" width="6.25" height="6.25" /> |
| 194 | 308 | </bezel> |
| 195 | | |
| 196 | | <bezel name="digit37" element="digit"> |
| 197 | | <bounds x="0.2" y="3.1" width="0.2" height="0.3" /> |
| 309 | <bezel name="lamp0" element="disk_lamp"> |
| 310 | <bounds x="103" y="16" width="5.75" height="5.75" /> |
| 198 | 311 | </bezel> |
| 199 | 312 | |
| 200 | | |
| 201 | | <bezel name="digit38" element="digit"> |
| 202 | | <bounds x="0.6" y="3.1" width="0.2" height="0.3" /> |
| 313 | <bezel element="disk_blue"> |
| 314 | <bounds x="102.75" y="22.25" width="6.25" height="6.25" /> |
| 203 | 315 | </bezel> |
| 204 | | |
| 205 | | <bezel name="digit39" element="digit"> |
| 206 | | <bounds x="0.8" y="3.1" width="0.2" height="0.3" /> |
| 316 | <bezel name="lamp1" element="disk_lamp"> |
| 317 | <bounds x="103" y="22.5" width="5.75" height="5.75" /> |
| 207 | 318 | </bezel> |
| 208 | 319 | |
| 209 | | |
| 210 | | <bezel name="digit40" element="digit"> |
| 211 | | <bounds x="1.2" y="3.1" width="0.2" height="0.3" /> |
| 320 | <bezel element="disk_blue"> |
| 321 | <bounds x="102.75" y="28.75" width="6.25" height="6.25" /> |
| 212 | 322 | </bezel> |
| 213 | | |
| 214 | | <bezel name="digit41" element="digit"> |
| 215 | | <bounds x="1.4" y="3.1" width="0.2" height="0.3" /> |
| 323 | <bezel name="lamp2" element="disk_lamp"> |
| 324 | <bounds x="103" y="29" width="5.75" height="5.75" /> |
| 216 | 325 | </bezel> |
| 217 | 326 | |
| 218 | | |
| 219 | | <bezel name="digit42" element="digit"> |
| 220 | | <bounds x="1.8" y="3.1" width="0.2" height="0.3" /> |
| 327 | <bezel element="disk_blue"> |
| 328 | <bounds x="102.75" y="35.25" width="6.25" height="6.25" /> |
| 221 | 329 | </bezel> |
| 330 | <bezel name="lamp3" element="disk_lamp"> |
| 331 | <bounds x="103" y="35.5" width="5.75" height="5.75" /> |
| 332 | </bezel> |
| 222 | 333 | |
| 223 | | <bezel name="digit43" element="digit"> |
| 224 | | <bounds x="2.0" y="3.1" width="0.2" height="0.3" /> |
| 334 | <bezel element="disk_blue"> |
| 335 | <bounds x="102.75" y="41.75" width="6.25" height="6.25" /> |
| 225 | 336 | </bezel> |
| 337 | <bezel name="lamp4" element="disk_lamp"> |
| 338 | <bounds x="103" y="42" width="5.75" height="5.75" /> |
| 339 | </bezel> |
| 226 | 340 | |
| 227 | | |
| 228 | | <bezel name="digit44" element="digit"> |
| 229 | | <bounds x="2.4" y="3.1" width="0.2" height="0.3" /> |
| 341 | <bezel element="disk_blue"> |
| 342 | <bounds x="102.75" y="48.25" width="6.25" height="6.25" /> |
| 230 | 343 | </bezel> |
| 344 | <bezel name="lamp5" element="disk_lamp"> |
| 345 | <bounds x="103" y="48.5" width="5.75" height="5.75" /> |
| 346 | </bezel> |
| 231 | 347 | |
| 232 | | <bezel name="digit45" element="digit"> |
| 233 | | <bounds x="2.6" y="3.1" width="0.2" height="0.3" /> |
| 348 | <bezel element="disk_blue"> |
| 349 | <bounds x="102.75" y="54.75" width="6.25" height="6.25" /> |
| 234 | 350 | </bezel> |
| 351 | <bezel name="lamp6" element="disk_lamp"> |
| 352 | <bounds x="103" y="55" width="5.75" height="5.75" /> |
| 353 | </bezel> |
| 235 | 354 | |
| 236 | | |
| 237 | | <bezel name="digit46" element="digit"> |
| 238 | | <bounds x="3.0" y="3.1" width="0.2" height="0.3" /> |
| 355 | <bezel element="text_rank1"> |
| 356 | <bounds x="111" y="15.25" width="40" height="6.5" /> |
| 239 | 357 | </bezel> |
| 358 | <bezel element="text_rank2"> |
| 359 | <bounds x="111" y="21.75" width="40" height="6.5" /> |
| 360 | </bezel> |
| 361 | <bezel element="text_rank3"> |
| 362 | <bounds x="111" y="28.25" width="40" height="6.5" /> |
| 363 | </bezel> |
| 364 | <bezel element="text_rank4"> |
| 365 | <bounds x="111" y="34.75" width="40" height="6.5" /> |
| 366 | </bezel> |
| 367 | <bezel element="text_rank5"> |
| 368 | <bounds x="111" y="41.25" width="40" height="6.5" /> |
| 369 | </bezel> |
| 370 | <bezel element="text_rank6"> |
| 371 | <bounds x="111" y="47.75" width="40" height="6.5" /> |
| 372 | </bezel> |
| 240 | 373 | |
| 241 | | <bezel name="digit47" element="digit"> |
| 242 | | <bounds x="3.2" y="3.1" width="0.2" height="0.3" /> |
| 374 | <bezel element="text_gameover"> |
| 375 | <bounds x="111" y="54.25" width="40" height="6.5" /> |
| 243 | 376 | </bezel> |
| 244 | 377 | |
| 245 | 378 | |
| 379 | <!-- the grid --> |
| 380 | <!-- row 1 --> |
| 246 | 381 | |
| 247 | | |
| 248 | | <bezel name="digit48" element="digit"> |
| 249 | | <bounds x="0.0" y="3.7" width="0.2" height="0.3" /> |
| 382 | <bezel element="led_box" inputtag="IN0" inputmask="0x01"> |
| 383 | <bounds x="18" y="80" width="30" height="25" /> |
| 250 | 384 | </bezel> |
| 385 | <bezel name="digit0" element="digit"> |
| 386 | <bounds x="23" y="85" width="10" height="15" /> |
| 387 | </bezel> |
| 388 | <bezel name="digit1" element="digit"> |
| 389 | <bounds x="33" y="85" width="10" height="15" /> |
| 390 | </bezel> |
| 251 | 391 | |
| 252 | | <bezel name="digit49" element="digit"> |
| 253 | | <bounds x="0.2" y="3.7" width="0.2" height="0.3" /> |
| 392 | <bezel element="led_box" inputtag="IN0" inputmask="0x02"> |
| 393 | <bounds x="54" y="80" width="30" height="25" /> |
| 254 | 394 | </bezel> |
| 395 | <bezel name="digit2" element="digit"> |
| 396 | <bounds x="59" y="85" width="10" height="15" /> |
| 397 | </bezel> |
| 398 | <bezel name="digit3" element="digit"> |
| 399 | <bounds x="69" y="85" width="10" height="15" /> |
| 400 | </bezel> |
| 255 | 401 | |
| 256 | | |
| 257 | | <bezel name="digit50" element="digit"> |
| 258 | | <bounds x="0.6" y="3.7" width="0.2" height="0.3" /> |
| 402 | <bezel element="led_box" inputtag="IN0" inputmask="0x04"> |
| 403 | <bounds x="90" y="80" width="30" height="25" /> |
| 259 | 404 | </bezel> |
| 405 | <bezel name="digit4" element="digit"> |
| 406 | <bounds x="95" y="85" width="10" height="15" /> |
| 407 | </bezel> |
| 408 | <bezel name="digit5" element="digit"> |
| 409 | <bounds x="105" y="85" width="10" height="15" /> |
| 410 | </bezel> |
| 260 | 411 | |
| 261 | | <bezel name="digit51" element="digit"> |
| 262 | | <bounds x="0.8" y="3.7" width="0.2" height="0.3" /> |
| 412 | <bezel element="led_box" inputtag="IN0" inputmask="0x08"> |
| 413 | <bounds x="126" y="80" width="30" height="25" /> |
| 263 | 414 | </bezel> |
| 415 | <bezel name="digit6" element="digit"> |
| 416 | <bounds x="131" y="85" width="10" height="15" /> |
| 417 | </bezel> |
| 418 | <bezel name="digit7" element="digit"> |
| 419 | <bounds x="141" y="85" width="10" height="15" /> |
| 420 | </bezel> |
| 264 | 421 | |
| 265 | | |
| 266 | | <bezel name="digit52" element="digit"> |
| 267 | | <bounds x="1.2" y="3.7" width="0.2" height="0.3" /> |
| 422 | <bezel element="led_box" inputtag="IN0" inputmask="0x10"> |
| 423 | <bounds x="162" y="80" width="30" height="25" /> |
| 268 | 424 | </bezel> |
| 425 | <bezel name="digit8" element="digit"> |
| 426 | <bounds x="167" y="85" width="10" height="15" /> |
| 427 | </bezel> |
| 428 | <bezel name="digit9" element="digit"> |
| 429 | <bounds x="177" y="85" width="10" height="15" /> |
| 430 | </bezel> |
| 269 | 431 | |
| 270 | | <bezel name="digit53" element="digit"> |
| 271 | | <bounds x="1.4" y="3.7" width="0.2" height="0.3" /> |
| 432 | <bezel element="led_box" inputtag="IN0" inputmask="0x20"> |
| 433 | <bounds x="198" y="80" width="30" height="25" /> |
| 272 | 434 | </bezel> |
| 435 | <bezel name="digit10" element="digit"> |
| 436 | <bounds x="203" y="85" width="10" height="15" /> |
| 437 | </bezel> |
| 438 | <bezel name="digit11" element="digit"> |
| 439 | <bounds x="213" y="85" width="10" height="15" /> |
| 440 | </bezel> |
| 273 | 441 | |
| 442 | <!-- row 2 --> |
| 274 | 443 | |
| 275 | | <bezel name="digit54" element="digit"> |
| 276 | | <bounds x="1.8" y="3.7" width="0.2" height="0.3" /> |
| 444 | <bezel element="led_box" inputtag="IN0" inputmask="0x40"> |
| 445 | <bounds x="18" y="114" width="30" height="25" /> |
| 277 | 446 | </bezel> |
| 447 | <bezel name="digit12" element="digit"> |
| 448 | <bounds x="23" y="119" width="10" height="15" /> |
| 449 | </bezel> |
| 450 | <bezel name="digit13" element="digit"> |
| 451 | <bounds x="33" y="119" width="10" height="15" /> |
| 452 | </bezel> |
| 278 | 453 | |
| 279 | | <bezel name="digit55" element="digit"> |
| 280 | | <bounds x="2.0" y="3.7" width="0.2" height="0.3" /> |
| 454 | <bezel element="led_box" inputtag="IN0" inputmask="0x80"> |
| 455 | <bounds x="54" y="114" width="30" height="25" /> |
| 281 | 456 | </bezel> |
| 457 | <bezel name="digit14" element="digit"> |
| 458 | <bounds x="59" y="119" width="10" height="15" /> |
| 459 | </bezel> |
| 460 | <bezel name="digit15" element="digit"> |
| 461 | <bounds x="69" y="119" width="10" height="15" /> |
| 462 | </bezel> |
| 282 | 463 | |
| 283 | | |
| 284 | | <bezel name="digit56" element="digit"> |
| 285 | | <bounds x="2.4" y="3.7" width="0.2" height="0.3" /> |
| 464 | <bezel element="led_box" inputtag="IN1" inputmask="0x01"> |
| 465 | <bounds x="90" y="114" width="30" height="25" /> |
| 286 | 466 | </bezel> |
| 467 | <bezel name="digit16" element="digit"> |
| 468 | <bounds x="95" y="119" width="10" height="15" /> |
| 469 | </bezel> |
| 470 | <bezel name="digit17" element="digit"> |
| 471 | <bounds x="105" y="119" width="10" height="15" /> |
| 472 | </bezel> |
| 287 | 473 | |
| 288 | | <bezel name="digit57" element="digit"> |
| 289 | | <bounds x="2.6" y="3.7" width="0.2" height="0.3" /> |
| 474 | <bezel element="led_box" inputtag="IN1" inputmask="0x02"> |
| 475 | <bounds x="126" y="114" width="30" height="25" /> |
| 290 | 476 | </bezel> |
| 477 | <bezel name="digit18" element="digit"> |
| 478 | <bounds x="131" y="119" width="10" height="15" /> |
| 479 | </bezel> |
| 480 | <bezel name="digit19" element="digit"> |
| 481 | <bounds x="141" y="119" width="10" height="15" /> |
| 482 | </bezel> |
| 291 | 483 | |
| 292 | | |
| 293 | | <bezel name="digit58" element="digit"> |
| 294 | | <bounds x="3.0" y="3.7" width="0.2" height="0.3" /> |
| 484 | <bezel element="led_box" inputtag="IN1" inputmask="0x04"> |
| 485 | <bounds x="162" y="114" width="30" height="25" /> |
| 295 | 486 | </bezel> |
| 487 | <bezel name="digit20" element="digit"> |
| 488 | <bounds x="167" y="119" width="10" height="15" /> |
| 489 | </bezel> |
| 490 | <bezel name="digit21" element="digit"> |
| 491 | <bounds x="177" y="119" width="10" height="15" /> |
| 492 | </bezel> |
| 296 | 493 | |
| 297 | | <bezel name="digit59" element="digit"> |
| 298 | | <bounds x="3.2" y="3.7" width="0.2" height="0.3" /> |
| 494 | <bezel element="led_box" inputtag="IN1" inputmask="0x08"> |
| 495 | <bounds x="198" y="114" width="30" height="25" /> |
| 299 | 496 | </bezel> |
| 497 | <bezel name="digit22" element="digit"> |
| 498 | <bounds x="203" y="119" width="10" height="15" /> |
| 499 | </bezel> |
| 500 | <bezel name="digit23" element="digit"> |
| 501 | <bounds x="213" y="119" width="10" height="15" /> |
| 502 | </bezel> |
| 300 | 503 | |
| 504 | <!-- row 3 --> |
| 301 | 505 | |
| 302 | | <!-- time --> |
| 303 | | |
| 304 | | <bezel name="digit60" element="digit"> |
| 305 | | <bounds x="0.0" y="0.9" width="0.2" height="0.2" /> |
| 506 | <bezel element="led_box" inputtag="IN1" inputmask="0x10"> |
| 507 | <bounds x="18" y="148" width="30" height="25" /> |
| 306 | 508 | </bezel> |
| 509 | <bezel name="digit24" element="digit"> |
| 510 | <bounds x="23" y="153" width="10" height="15" /> |
| 511 | </bezel> |
| 512 | <bezel name="digit25" element="digit"> |
| 513 | <bounds x="33" y="153" width="10" height="15" /> |
| 514 | </bezel> |
| 307 | 515 | |
| 308 | | <bezel name="digit61" element="digit"> |
| 309 | | <bounds x="0.2" y="0.9" width="0.2" height="0.2" /> |
| 516 | <bezel element="led_box" inputtag="IN1" inputmask="0x20"> |
| 517 | <bounds x="54" y="148" width="30" height="25" /> |
| 310 | 518 | </bezel> |
| 519 | <bezel name="digit26" element="digit"> |
| 520 | <bounds x="59" y="153" width="10" height="15" /> |
| 521 | </bezel> |
| 522 | <bezel name="digit27" element="digit"> |
| 523 | <bounds x="69" y="153" width="10" height="15" /> |
| 524 | </bezel> |
| 311 | 525 | |
| 312 | | <bezel name="digit62" element="digit"> |
| 313 | | <bounds x="0.4" y="0.9" width="0.2" height="0.2" /> |
| 526 | <bezel element="led_box" inputtag="IN1" inputmask="0x40"> |
| 527 | <bounds x="90" y="148" width="30" height="25" /> |
| 314 | 528 | </bezel> |
| 529 | <bezel name="digit28" element="digit"> |
| 530 | <bounds x="95" y="153" width="10" height="15" /> |
| 531 | </bezel> |
| 532 | <bezel name="digit29" element="digit"> |
| 533 | <bounds x="105" y="153" width="10" height="15" /> |
| 534 | </bezel> |
| 315 | 535 | |
| 316 | | <bezel name="digit63" element="digit"> |
| 317 | | <bounds x="0.6" y="0.9" width="0.2" height="0.2" /> |
| 536 | <bezel element="led_box" inputtag="IN1" inputmask="0x80"> |
| 537 | <bounds x="126" y="148" width="30" height="25" /> |
| 318 | 538 | </bezel> |
| 539 | <bezel name="digit30" element="digit"> |
| 540 | <bounds x="131" y="153" width="10" height="15" /> |
| 541 | </bezel> |
| 542 | <bezel name="digit31" element="digit"> |
| 543 | <bounds x="141" y="153" width="10" height="15" /> |
| 544 | </bezel> |
| 319 | 545 | |
| 320 | | |
| 321 | | <!-- today's highscores --> |
| 322 | | |
| 323 | | <bezel name="digit64" element="digit"> |
| 324 | | <bounds x="0.9" y="0.3" width="0.2" height="0.2" /> |
| 546 | <bezel element="led_box" inputtag="IN2" inputmask="0x01"> |
| 547 | <bounds x="162" y="148" width="30" height="25" /> |
| 325 | 548 | </bezel> |
| 549 | <bezel name="digit32" element="digit"> |
| 550 | <bounds x="167" y="153" width="10" height="15" /> |
| 551 | </bezel> |
| 552 | <bezel name="digit33" element="digit"> |
| 553 | <bounds x="177" y="153" width="10" height="15" /> |
| 554 | </bezel> |
| 326 | 555 | |
| 327 | | <bezel name="digit65" element="digit"> |
| 328 | | <bounds x="1.1" y="0.3" width="0.2" height="0.2" /> |
| 556 | <bezel element="led_box" inputtag="IN2" inputmask="0x02"> |
| 557 | <bounds x="198" y="148" width="30" height="25" /> |
| 329 | 558 | </bezel> |
| 559 | <bezel name="digit34" element="digit"> |
| 560 | <bounds x="203" y="153" width="10" height="15" /> |
| 561 | </bezel> |
| 562 | <bezel name="digit35" element="digit"> |
| 563 | <bounds x="213" y="153" width="10" height="15" /> |
| 564 | </bezel> |
| 330 | 565 | |
| 566 | <!-- row 4 --> |
| 331 | 567 | |
| 332 | | <bezel name="digit66" element="digit"> |
| 333 | | <bounds x="0.9" y="0.6" width="0.2" height="0.2" /> |
| 568 | <bezel element="led_box" inputtag="IN2" inputmask="0x04"> |
| 569 | <bounds x="18" y="182" width="30" height="25" /> |
| 334 | 570 | </bezel> |
| 571 | <bezel name="digit36" element="digit"> |
| 572 | <bounds x="23" y="187" width="10" height="15" /> |
| 573 | </bezel> |
| 574 | <bezel name="digit37" element="digit"> |
| 575 | <bounds x="33" y="187" width="10" height="15" /> |
| 576 | </bezel> |
| 335 | 577 | |
| 336 | | <bezel name="digit67" element="digit"> |
| 337 | | <bounds x="1.1" y="0.6" width="0.2" height="0.2" /> |
| 578 | <bezel element="led_box" inputtag="IN2" inputmask="0x08"> |
| 579 | <bounds x="54" y="182" width="30" height="25" /> |
| 338 | 580 | </bezel> |
| 581 | <bezel name="digit38" element="digit"> |
| 582 | <bounds x="59" y="187" width="10" height="15" /> |
| 583 | </bezel> |
| 584 | <bezel name="digit39" element="digit"> |
| 585 | <bounds x="69" y="187" width="10" height="15" /> |
| 586 | </bezel> |
| 339 | 587 | |
| 340 | | |
| 341 | | <bezel name="digit68" element="digit"> |
| 342 | | <bounds x="0.9" y="0.9" width="0.2" height="0.2" /> |
| 588 | <bezel element="led_box" inputtag="IN2" inputmask="0x10"> |
| 589 | <bounds x="90" y="182" width="30" height="25" /> |
| 343 | 590 | </bezel> |
| 591 | <bezel name="digit40" element="digit"> |
| 592 | <bounds x="95" y="187" width="10" height="15" /> |
| 593 | </bezel> |
| 594 | <bezel name="digit41" element="digit"> |
| 595 | <bounds x="105" y="187" width="10" height="15" /> |
| 596 | </bezel> |
| 344 | 597 | |
| 345 | | <bezel name="digit69" element="digit"> |
| 346 | | <bounds x="1.1" y="0.9" width="0.2" height="0.2" /> |
| 598 | <bezel element="led_box" inputtag="IN2" inputmask="0x20"> |
| 599 | <bounds x="126" y="182" width="30" height="25" /> |
| 347 | 600 | </bezel> |
| 601 | <bezel name="digit42" element="digit"> |
| 602 | <bounds x="131" y="187" width="10" height="15" /> |
| 603 | </bezel> |
| 604 | <bezel name="digit43" element="digit"> |
| 605 | <bounds x="141" y="187" width="10" height="15" /> |
| 606 | </bezel> |
| 348 | 607 | |
| 349 | | |
| 350 | | <!-- score --> |
| 351 | | |
| 352 | | <bezel name="digit70" element="digit"> |
| 353 | | <bounds x="0.0" y="0.3" width="0.4" height="0.4" /> |
| 608 | <bezel element="led_box" inputtag="IN2" inputmask="0x40"> |
| 609 | <bounds x="162" y="182" width="30" height="25" /> |
| 354 | 610 | </bezel> |
| 611 | <bezel name="digit44" element="digit"> |
| 612 | <bounds x="167" y="187" width="10" height="15" /> |
| 613 | </bezel> |
| 614 | <bezel name="digit45" element="digit"> |
| 615 | <bounds x="177" y="187" width="10" height="15" /> |
| 616 | </bezel> |
| 355 | 617 | |
| 356 | | <bezel name="digit71" element="digit"> |
| 357 | | <bounds x="0.4" y="0.3" width="0.4" height="0.4" /> |
| 618 | <bezel element="led_box" inputtag="IN2" inputmask="0x80"> |
| 619 | <bounds x="198" y="182" width="30" height="25" /> |
| 358 | 620 | </bezel> |
| 621 | <bezel name="digit46" element="digit"> |
| 622 | <bounds x="203" y="187" width="10" height="15" /> |
| 623 | </bezel> |
| 624 | <bezel name="digit47" element="digit"> |
| 625 | <bounds x="213" y="187" width="10" height="15" /> |
| 626 | </bezel> |
| 359 | 627 | |
| 628 | <!-- row 5 --> |
| 360 | 629 | |
| 361 | | <!-- ranking --> |
| 362 | | |
| 363 | | <bezel name="lamp0" element="rank"> |
| 364 | | <bounds x="1.5" y="0.3" width="0.1" height="0.1" /> |
| 630 | <bezel element="led_box" inputtag="IN3" inputmask="0x01"> |
| 631 | <bounds x="18" y="214" width="30" height="25" /> |
| 365 | 632 | </bezel> |
| 366 | | |
| 367 | | <bezel name="lamp1" element="rank"> |
| 368 | | <bounds x="1.5" y="0.4" width="0.1" height="0.1" /> |
| 633 | <bezel name="digit48" element="digit"> |
| 634 | <bounds x="23" y="219" width="10" height="15" /> |
| 369 | 635 | </bezel> |
| 636 | <bezel name="digit49" element="digit"> |
| 637 | <bounds x="33" y="219" width="10" height="15" /> |
| 638 | </bezel> |
| 370 | 639 | |
| 371 | | <bezel name="lamp2" element="rank"> |
| 372 | | <bounds x="1.5" y="0.5" width="0.1" height="0.1" /> |
| 640 | <bezel element="led_box" inputtag="IN3" inputmask="0x02"> |
| 641 | <bounds x="54" y="214" width="30" height="25" /> |
| 373 | 642 | </bezel> |
| 643 | <bezel name="digit50" element="digit"> |
| 644 | <bounds x="59" y="219" width="10" height="15" /> |
| 645 | </bezel> |
| 646 | <bezel name="digit51" element="digit"> |
| 647 | <bounds x="69" y="219" width="10" height="15" /> |
| 648 | </bezel> |
| 374 | 649 | |
| 375 | | <bezel name="lamp3" element="rank"> |
| 376 | | <bounds x="1.5" y="0.6" width="0.1" height="0.1" /> |
| 650 | <bezel element="led_box" inputtag="IN3" inputmask="0x04"> |
| 651 | <bounds x="90" y="214" width="30" height="25" /> |
| 377 | 652 | </bezel> |
| 653 | <bezel name="digit52" element="digit"> |
| 654 | <bounds x="95" y="219" width="10" height="15" /> |
| 655 | </bezel> |
| 656 | <bezel name="digit53" element="digit"> |
| 657 | <bounds x="105" y="219" width="10" height="15" /> |
| 658 | </bezel> |
| 378 | 659 | |
| 379 | | <bezel name="lamp4" element="rank"> |
| 380 | | <bounds x="1.5" y="0.7" width="0.1" height="0.1" /> |
| 660 | <bezel element="led_box" inputtag="IN3" inputmask="0x08"> |
| 661 | <bounds x="126" y="214" width="30" height="25" /> |
| 381 | 662 | </bezel> |
| 663 | <bezel name="digit54" element="digit"> |
| 664 | <bounds x="131" y="219" width="10" height="15" /> |
| 665 | </bezel> |
| 666 | <bezel name="digit55" element="digit"> |
| 667 | <bounds x="141" y="219" width="10" height="15" /> |
| 668 | </bezel> |
| 382 | 669 | |
| 383 | | <bezel name="lamp5" element="rank"> |
| 384 | | <bounds x="1.5" y="0.8" width="0.1" height="0.1" /> |
| 670 | <bezel element="led_box" inputtag="IN3" inputmask="0x10"> |
| 671 | <bounds x="162" y="214" width="30" height="25" /> |
| 385 | 672 | </bezel> |
| 673 | <bezel name="digit56" element="digit"> |
| 674 | <bounds x="167" y="219" width="10" height="15" /> |
| 675 | </bezel> |
| 676 | <bezel name="digit57" element="digit"> |
| 677 | <bounds x="177" y="219" width="10" height="15" /> |
| 678 | </bezel> |
| 386 | 679 | |
| 387 | | <bezel name="lamp6" element="rank"> |
| 388 | | <bounds x="1.5" y="0.9" width="0.1" height="0.1" /> |
| 680 | <bezel element="led_box" inputtag="IN3" inputmask="0x20"> |
| 681 | <bounds x="198" y="214" width="30" height="25" /> |
| 389 | 682 | </bezel> |
| 683 | <bezel name="digit58" element="digit"> |
| 684 | <bounds x="203" y="219" width="10" height="15" /> |
| 685 | </bezel> |
| 686 | <bezel name="digit59" element="digit"> |
| 687 | <bounds x="213" y="219" width="10" height="15" /> |
| 688 | </bezel> |
| 390 | 689 | |
| 391 | 690 | |
| 392 | 691 | </view> |
src/mame/drivers/30test.c
| r14439 | r14440 | |
| 5 | 5 | driver by Angelo Salese |
| 6 | 6 | |
| 7 | 7 | TODO: |
| 8 | | - clickable artwork; |
| 9 | 8 | - portd meaning is a mystery |
| 10 | 9 | - inputs are annoying to map; |
| 11 | 10 | - EEPROM |
| r14439 | r14440 | |
| 60 | 59 | }; |
| 61 | 60 | |
| 62 | 61 | |
| 63 | | static READ8_HANDLER(hc11_mux_r) |
| 64 | | { |
| 65 | | namco_30test_state *state = space->machine().driver_data<namco_30test_state>(); |
| 62 | static const UINT8 led_map[16] = |
| 63 | { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7c,0x07,0x7f,0x67,0x77,0x7c,0x39,0x5e,0x79,0x00 }; |
| 66 | 64 | |
| 67 | | return state->m_mux_data; |
| 65 | static WRITE8_HANDLER( namco_30test_led_w ) |
| 66 | { |
| 67 | output_set_digit_value(0 + offset * 2, led_map[(data & 0xf0) >> 4]); |
| 68 | output_set_digit_value(1 + offset * 2, led_map[(data & 0x0f) >> 0]); |
| 68 | 69 | } |
| 69 | 70 | |
| 70 | | static WRITE8_HANDLER(hc11_mux_w) |
| 71 | static WRITE8_HANDLER( namco_30test_led_rank_w ) |
| 71 | 72 | { |
| 72 | | namco_30test_state *state = space->machine().driver_data<namco_30test_state>(); |
| 73 | output_set_digit_value(64 + offset * 2, led_map[(data & 0xf0) >> 4]); |
| 74 | output_set_digit_value(65 + offset * 2, led_map[(data & 0x0f) >> 0]); |
| 75 | } |
| 73 | 76 | |
| 74 | | state->m_mux_data = data; |
| 77 | static WRITE8_HANDLER( namco_30test_lamps_w ) |
| 78 | { |
| 79 | // d0-d5: ranking, d6: game over, d7: assume marquee lamp |
| 80 | for (int i = 0; i < 8; i++) |
| 81 | output_set_lamp_value(i, data >> i & 1); |
| 75 | 82 | } |
| 76 | 83 | |
| 77 | 84 | static READ8_HANDLER(namco_30test_mux_r) |
| r14439 | r14440 | |
| 90 | 97 | return res; |
| 91 | 98 | } |
| 92 | 99 | |
| 93 | | static const UINT8 led_map[16] = |
| 94 | | { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7c,0x07,0x7f,0x67,0x77,0x7c,0x39,0x5e,0x79,0x00 }; |
| 95 | | |
| 96 | | static WRITE8_HANDLER( namco_30test_led_w ) |
| 100 | static READ8_HANDLER(hc11_mux_r) |
| 97 | 101 | { |
| 98 | | output_set_digit_value(0 + offset * 2, led_map[(data & 0xf0) >> 4]); |
| 99 | | output_set_digit_value(1 + offset * 2, led_map[(data & 0x0f) >> 0]); |
| 102 | namco_30test_state *state = space->machine().driver_data<namco_30test_state>(); |
| 103 | return state->m_mux_data; |
| 100 | 104 | } |
| 101 | 105 | |
| 102 | | static WRITE8_HANDLER( namco_30test_led_rank_w ) |
| 106 | static WRITE8_HANDLER(hc11_mux_w) |
| 103 | 107 | { |
| 104 | | output_set_digit_value(64 + offset * 2, led_map[(data & 0xf0) >> 4]); |
| 105 | | output_set_digit_value(65 + offset * 2, led_map[(data & 0x0f) >> 0]); |
| 108 | namco_30test_state *state = space->machine().driver_data<namco_30test_state>(); |
| 109 | state->m_mux_data = data; |
| 106 | 110 | } |
| 107 | 111 | |
| 108 | | static WRITE8_HANDLER( namco_30test_lamps_w ) |
| 109 | | { |
| 110 | | if(!(data & 0x80)) // guess: lamps mask |
| 111 | | data = 0; |
| 112 | | |
| 113 | | output_set_lamp_value(0, (data & 0x01) >> 0); // really OK! lamp |
| 114 | | output_set_lamp_value(1, (data & 0x02) >> 1); // pretty good lamp |
| 115 | | output_set_lamp_value(2, (data & 0x04) >> 2); // not bad lamp |
| 116 | | output_set_lamp_value(3, (data & 0x08) >> 3); // normal lamp |
| 117 | | output_set_lamp_value(4, (data & 0x10) >> 4); // pretty bad lamp |
| 118 | | output_set_lamp_value(5, (data & 0x20) >> 5); // worst lamp |
| 119 | | output_set_lamp_value(6, (data & 0x40) >> 6); // game over lamp |
| 120 | | } |
| 121 | | |
| 122 | 112 | static READ8_DEVICE_HANDLER( hc11_okibank_r ) |
| 123 | 113 | { |
| 124 | 114 | namco_30test_state *state = device->machine().driver_data<namco_30test_state>(); |
| r14439 | r14440 | |
| 163 | 153 | |
| 164 | 154 | |
| 165 | 155 | static INPUT_PORTS_START( 30test ) |
| 166 | | /* we use num pad to map system inputs */ |
| 167 | 156 | PORT_START("IN0") |
| 168 | | PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("1-1") PORT_CODE(KEYCODE_1) |
| 169 | | PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("2-1") PORT_CODE(KEYCODE_2) |
| 170 | | PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("3-1") PORT_CODE(KEYCODE_3) |
| 171 | | PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("4-1") PORT_CODE(KEYCODE_4) |
| 172 | | PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("5-1") PORT_CODE(KEYCODE_5) |
| 173 | | PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("6-1") PORT_CODE(KEYCODE_6) |
| 174 | | PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("1-2") PORT_CODE(KEYCODE_Q) |
| 175 | | PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("2-2") PORT_CODE(KEYCODE_W) |
| 157 | PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 1-1") PORT_CODE(KEYCODE_1) |
| 158 | PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 1-2") PORT_CODE(KEYCODE_2) |
| 159 | PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 1-3") PORT_CODE(KEYCODE_3) |
| 160 | PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 1-4") PORT_CODE(KEYCODE_4) |
| 161 | PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 1-5") PORT_CODE(KEYCODE_5) |
| 162 | PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 1-6") PORT_CODE(KEYCODE_6) |
| 163 | PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 2-1") PORT_CODE(KEYCODE_Q) |
| 164 | PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 2-2") PORT_CODE(KEYCODE_W) |
| 176 | 165 | |
| 177 | 166 | PORT_START("IN1") |
| 178 | | PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("3-2") PORT_CODE(KEYCODE_E) |
| 179 | | PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("4-2") PORT_CODE(KEYCODE_R) |
| 180 | | PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("5-2") PORT_CODE(KEYCODE_T) |
| 181 | | PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("6-2") PORT_CODE(KEYCODE_Y) |
| 182 | | PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("1-3") PORT_CODE(KEYCODE_A) |
| 183 | | PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("2-3") PORT_CODE(KEYCODE_S) |
| 184 | | PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("3-3") PORT_CODE(KEYCODE_D) |
| 185 | | PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("4-3") PORT_CODE(KEYCODE_F) |
| 167 | PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 2-3") PORT_CODE(KEYCODE_E) |
| 168 | PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 2-4") PORT_CODE(KEYCODE_R) |
| 169 | PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 2-5") PORT_CODE(KEYCODE_T) |
| 170 | PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 2-6") PORT_CODE(KEYCODE_Y) |
| 171 | PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 3-1") PORT_CODE(KEYCODE_A) |
| 172 | PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 3-2") PORT_CODE(KEYCODE_S) |
| 173 | PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 3-3") PORT_CODE(KEYCODE_D) |
| 174 | PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 3-4") PORT_CODE(KEYCODE_F) |
| 186 | 175 | |
| 187 | 176 | PORT_START("IN2") |
| 188 | | PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("5-3") PORT_CODE(KEYCODE_G) |
| 189 | | PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("6-3") PORT_CODE(KEYCODE_H) |
| 190 | | PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("1-4") PORT_CODE(KEYCODE_Z) |
| 191 | | PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("2-4") PORT_CODE(KEYCODE_X) |
| 192 | | PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("3-4") PORT_CODE(KEYCODE_C) |
| 193 | | PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("4-4") PORT_CODE(KEYCODE_V) |
| 194 | | PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("5-4") PORT_CODE(KEYCODE_B) |
| 195 | | PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("6-4") PORT_CODE(KEYCODE_N) |
| 177 | PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 3-5") PORT_CODE(KEYCODE_G) |
| 178 | PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 3-6") PORT_CODE(KEYCODE_H) |
| 179 | PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 4-1") PORT_CODE(KEYCODE_Z) |
| 180 | PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 4-2") PORT_CODE(KEYCODE_X) |
| 181 | PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 4-3") PORT_CODE(KEYCODE_C) |
| 182 | PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 4-4") PORT_CODE(KEYCODE_V) |
| 183 | PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 4-5") PORT_CODE(KEYCODE_B) |
| 184 | PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 4-6") PORT_CODE(KEYCODE_N) |
| 196 | 185 | |
| 197 | 186 | PORT_START("IN3") |
| 198 | | PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("1-5") PORT_CODE(KEYCODE_LSHIFT) |
| 199 | | PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("2-5") PORT_CODE(KEYCODE_LCONTROL) |
| 200 | | PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("3-5") PORT_CODE(KEYCODE_LALT) |
| 201 | | PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("4-5") PORT_CODE(KEYCODE_SPACE) |
| 202 | | PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("5-5") PORT_CODE(KEYCODE_RALT) |
| 203 | | PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("6-5") PORT_CODE(KEYCODE_RCONTROL) |
| 204 | | PORT_DIPNAME( 0x40, 0x40, DEF_STR( Unknown ) ) |
| 205 | | PORT_DIPSETTING( 0x40, DEF_STR( Off ) ) |
| 206 | | PORT_DIPSETTING( 0x00, DEF_STR( On ) ) |
| 207 | | PORT_DIPNAME( 0x80, 0x80, DEF_STR( Unknown ) ) |
| 208 | | PORT_DIPSETTING( 0x80, DEF_STR( Off ) ) |
| 209 | | PORT_DIPSETTING( 0x00, DEF_STR( On ) ) |
| 187 | PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 5-1") PORT_CODE(KEYCODE_LSHIFT) |
| 188 | PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 5-2") PORT_CODE(KEYCODE_LCONTROL) |
| 189 | PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 5-3") PORT_CODE(KEYCODE_LALT) |
| 190 | PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 5-4") PORT_CODE(KEYCODE_SPACE) |
| 191 | PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 5-5") PORT_CODE(KEYCODE_RALT) |
| 192 | PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_OTHER ) PORT_NAME("Button 5-6") PORT_CODE(KEYCODE_RCONTROL) |
| 193 | PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNKNOWN ) |
| 194 | PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_UNKNOWN ) |
| 210 | 195 | |
| 211 | 196 | PORT_START("SYSTEM") |
| 212 | | PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_SERVICE ) |
| 213 | | PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_COIN1 ) PORT_CODE(KEYCODE_5_PAD) |
| 214 | | PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_COIN2 ) PORT_CODE(KEYCODE_6_PAD) |
| 215 | | PORT_DIPNAME( 0x08, 0x08, "SYSTEM" ) |
| 197 | PORT_SERVICE( 0x01, IP_ACTIVE_LOW ) |
| 198 | PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_COIN1 ) PORT_CODE(KEYCODE_7) |
| 199 | PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_COIN2 ) PORT_CODE(KEYCODE_8) |
| 200 | PORT_DIPNAME( 0x08, 0x08, "UNK3" ) |
| 216 | 201 | PORT_DIPSETTING( 0x08, DEF_STR( Off ) ) |
| 217 | 202 | PORT_DIPSETTING( 0x00, DEF_STR( On ) ) |
| 218 | | PORT_DIPNAME( 0x10, 0x10, DEF_STR( Unknown ) ) |
| 203 | PORT_DIPNAME( 0x10, 0x10, "UNK4" ) |
| 219 | 204 | PORT_DIPSETTING( 0x10, DEF_STR( Off ) ) |
| 220 | 205 | PORT_DIPSETTING( 0x00, DEF_STR( On ) ) |
| 221 | | PORT_DIPNAME( 0x20, 0x20, DEF_STR( Unknown ) ) |
| 206 | PORT_DIPNAME( 0x20, 0x20, "UNK5" ) |
| 222 | 207 | PORT_DIPSETTING( 0x20, DEF_STR( Off ) ) |
| 223 | 208 | PORT_DIPSETTING( 0x00, DEF_STR( On ) ) |
| 224 | | PORT_DIPNAME( 0x40, 0x40, DEF_STR( Unknown ) ) |
| 209 | PORT_DIPNAME( 0x40, 0x40, "UNK6" ) |
| 225 | 210 | PORT_DIPSETTING( 0x40, DEF_STR( Off ) ) |
| 226 | 211 | PORT_DIPSETTING( 0x00, DEF_STR( On ) ) |
| 227 | | PORT_DIPNAME( 0x80, 0x80, DEF_STR( Unknown ) ) |
| 212 | PORT_DIPNAME( 0x80, 0x80, "UNK7" ) |
| 228 | 213 | PORT_DIPSETTING( 0x80, DEF_STR( Off ) ) |
| 229 | 214 | PORT_DIPSETTING( 0x00, DEF_STR( On ) ) |
| 230 | 215 | INPUT_PORTS_END |
| r14439 | r14440 | |
| 267 | 252 | /* sound hardware */ |
| 268 | 253 | MCFG_SPEAKER_STANDARD_MONO("mono") |
| 269 | 254 | |
| 270 | | MCFG_OKIM6295_ADD("oki", 1056000, OKIM6295_PIN7_HIGH) // pin 7 not verified |
| 255 | MCFG_OKIM6295_ADD("oki", 1056000, OKIM6295_PIN7_HIGH) |
| 271 | 256 | MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.0) |
| 272 | 257 | MACHINE_CONFIG_END |
| 273 | 258 | |
src/osd/sdl/drawsdl.c
| r14439 | r14440 | |
| 18 | 18 | #include "ui.h" |
| 19 | 19 | |
| 20 | 20 | // standard SDL headers |
| 21 | | #include <SDL/SDL.h> |
| 21 | #include "sdlinc.h" |
| 22 | 22 | |
| 23 | 23 | // OSD headers |
| 24 | 24 | #include "osdsdl.h" |
| r14439 | r14440 | |
| 38 | 38 | |
| 39 | 39 | typedef struct _sdl_scale_mode sdl_scale_mode; |
| 40 | 40 | |
| 41 | | // FIXME: Only until there is a final decision from SAM on scalemodes |
| 42 | | #if (SDL_VERSION_ATLEAST(1,3,0)) |
| 43 | | #define SDL_SCALEMODE_NONE (0) |
| 44 | | #define SDL_SCALEMODE_FAST (0) |
| 45 | | #define SDL_SCALEMODE_BEST (0) |
| 41 | #if (SDLMAME_SDL2) |
| 42 | #define DRAW2_SCALEMODE_NEAREST "0" |
| 43 | #define DRAW2_SCALEMODE_LINEAR "1" |
| 44 | #define DRAW2_SCALEMODE_BEST "2" |
| 46 | 45 | #endif |
| 47 | 46 | |
| 48 | 47 | /* sdl_info is the information about SDL for the current screen */ |
| r14439 | r14440 | |
| 52 | 51 | INT32 blittimer; |
| 53 | 52 | UINT32 extra_flags; |
| 54 | 53 | |
| 55 | | #if (SDL_VERSION_ATLEAST(1,3,0)) |
| 54 | #if (SDLMAME_SDL2) |
| 55 | SDL_Renderer *sdl_renderer; |
| 56 | 56 | SDL_Texture *texture_id; |
| 57 | 57 | #else |
| 58 | 58 | // SDL surface |
| r14439 | r14440 | |
| 73 | 73 | int last_vofs; |
| 74 | 74 | int old_blitwidth; |
| 75 | 75 | int old_blitheight; |
| 76 | | |
| 77 | | // shortcut to scale mode info |
| 78 | | |
| 79 | | const sdl_scale_mode *scale_mode; |
| 80 | 76 | }; |
| 81 | 77 | |
| 82 | 78 | struct _sdl_scale_mode |
| r14439 | r14440 | |
| 86 | 82 | int is_yuv; /* Yuv mode? */ |
| 87 | 83 | int mult_w; /* Width multiplier */ |
| 88 | 84 | int mult_h; /* Height multiplier */ |
| 89 | | #if (!SDL_VERSION_ATLEAST(1,3,0)) |
| 85 | #if (!SDLMAME_SDL2) |
| 90 | 86 | int extra_flags; /* Texture/surface flags */ |
| 91 | 87 | #else |
| 92 | | //SDL_ScaleMode sdl_scale_mode; /* sdl 1.3 scale mode */ |
| 93 | | int sdl_scale_mode; /* got removed recently - trying to get it in again */ |
| 88 | const char *sdl_scale_mode; /* what to use as a hint ? */ |
| 94 | 89 | #endif |
| 95 | 90 | int pixel_format; /* Pixel/Overlay format */ |
| 96 | 91 | void (*yuv_blit)(UINT16 *bitmap, sdl_info *sdl, UINT8 *ptr, int pitch); |
| r14439 | r14440 | |
| 116 | 111 | static void drawsdl_window_clear(sdl_window_info *window); |
| 117 | 112 | static int drawsdl_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt); |
| 118 | 113 | |
| 119 | | #if (SDL_VERSION_ATLEAST(1,3,0)) |
| 114 | #if (SDLMAME_SDL2) |
| 120 | 115 | static void setup_texture(sdl_window_info *window, int tempwidth, int tempheight); |
| 121 | 116 | #endif |
| 122 | 117 | |
| r14439 | r14440 | |
| 137 | 132 | |
| 138 | 133 | // Static declarations |
| 139 | 134 | |
| 140 | | #if (!SDL_VERSION_ATLEAST(1,3,0)) |
| 135 | #if (!SDLMAME_SDL2) |
| 141 | 136 | static int shown_video_info = 0; |
| 142 | 137 | |
| 143 | 138 | static const sdl_scale_mode scale_modes[] = |
| r14439 | r14440 | |
| 153 | 148 | #else |
| 154 | 149 | static const sdl_scale_mode scale_modes[] = |
| 155 | 150 | { |
| 156 | | { "none", 0, 0, 0, 0, SDL_SCALEMODE_NONE, 0, 0 }, |
| 157 | | { "hwblit", 1, 0, 1, 1, SDL_SCALEMODE_FAST, 0, 0 }, |
| 158 | | { "hwbest", 1, 0, 1, 1, SDL_SCALEMODE_BEST, 0, 0 }, |
| 159 | | { "yv12", 1, 1, 1, 1, SDL_SCALEMODE_NONE, SDL_PIXELFORMAT_YV12, yuv_RGB_to_YV12 }, |
| 160 | | { "yv12x2", 1, 1, 2, 2, SDL_SCALEMODE_NONE, SDL_PIXELFORMAT_YV12, yuv_RGB_to_YV12X2 }, |
| 161 | | { "yuy2", 1, 1, 1, 1, SDL_SCALEMODE_NONE, SDL_PIXELFORMAT_YUY2, yuv_RGB_to_YUY2 }, |
| 162 | | { "yuy2x2", 1, 1, 2, 1, SDL_SCALEMODE_NONE, SDL_PIXELFORMAT_YUY2, yuv_RGB_to_YUY2X2 }, |
| 151 | { "none", 0, 0, 0, 0, DRAW2_SCALEMODE_NEAREST, 0, 0 }, |
| 152 | { "hwblit", 1, 0, 1, 1, DRAW2_SCALEMODE_LINEAR, 0, 0 }, |
| 153 | { "hwbest", 1, 0, 1, 1, DRAW2_SCALEMODE_BEST, 0, 0 }, |
| 154 | { "yv12", 1, 1, 1, 1, DRAW2_SCALEMODE_NEAREST, SDL_PIXELFORMAT_YV12, yuv_RGB_to_YV12 }, |
| 155 | { "yv12x2", 1, 1, 2, 2, DRAW2_SCALEMODE_NEAREST, SDL_PIXELFORMAT_YV12, yuv_RGB_to_YV12X2 }, |
| 156 | { "yuy2", 1, 1, 1, 1, DRAW2_SCALEMODE_NEAREST, SDL_PIXELFORMAT_YUY2, yuv_RGB_to_YUY2 }, |
| 157 | { "yuy2x2", 1, 1, 2, 1, DRAW2_SCALEMODE_NEAREST, SDL_PIXELFORMAT_YUY2, yuv_RGB_to_YUY2X2 }, |
| 163 | 158 | { NULL } |
| 164 | 159 | }; |
| 165 | 160 | #endif |
| r14439 | r14440 | |
| 208 | 203 | callbacks->exit = drawsdl_exit; |
| 209 | 204 | callbacks->attach = drawsdl_attach; |
| 210 | 205 | |
| 211 | | if (SDL_VERSION_ATLEAST(1,3,0)) |
| 212 | | mame_printf_verbose("Using SDL multi-window soft driver (SDL 1.3+)\n"); |
| 206 | if (SDLMAME_SDL2) |
| 207 | mame_printf_verbose("Using SDL multi-window soft driver (SDL 2.0+)\n"); |
| 213 | 208 | else |
| 214 | 209 | mame_printf_verbose("Using SDL single-window soft driver (SDL 1.2)\n"); |
| 215 | 210 | |
| r14439 | r14440 | |
| 254 | 249 | // setup_texture for window |
| 255 | 250 | //============================================================ |
| 256 | 251 | |
| 257 | | #if (SDL_VERSION_ATLEAST(1,3,0)) |
| 252 | #if (SDLMAME_SDL2) |
| 258 | 253 | static void setup_texture(sdl_window_info *window, int tempwidth, int tempheight) |
| 259 | 254 | { |
| 260 | 255 | sdl_info *sdl = (sdl_info *) window->dxdata; |
| 261 | | const sdl_scale_mode *sdl_sm = sdl->scale_mode; |
| 256 | const sdl_scale_mode *sdl_sm = &scale_modes[video_config.scale_mode]; |
| 262 | 257 | SDL_DisplayMode mode; |
| 263 | 258 | UINT32 fmt; |
| 264 | 259 | |
| r14439 | r14440 | |
| 278 | 273 | { |
| 279 | 274 | sdl->hw_scale_width *= video_config.prescale; |
| 280 | 275 | sdl->hw_scale_height *= video_config.prescale; |
| 276 | |
| 277 | /* This must be a multiple of 2 */ |
| 278 | sdl->hw_scale_width = (sdl->hw_scale_width + 1) & ~1; |
| 281 | 279 | } |
| 282 | 280 | } |
| 283 | 281 | |
| r14439 | r14440 | |
| 291 | 289 | int w = sdl->hw_scale_width * sdl_sm->mult_w; |
| 292 | 290 | int h = sdl->hw_scale_height * sdl_sm->mult_h; |
| 293 | 291 | |
| 294 | | sdl->texture_id = SDL_CreateTexture(window->sdl_renderer, fmt, SDL_TEXTUREACCESS_STREAMING, w, h); |
| 292 | sdl->texture_id = SDL_CreateTexture(sdl->sdl_renderer, fmt, SDL_TEXTUREACCESS_STREAMING, w, h); |
| 295 | 293 | |
| 296 | 294 | } |
| 297 | 295 | else |
| 298 | 296 | { |
| 299 | | sdl->texture_id = SDL_CreateTexture(window->sdl_renderer,fmt, SDL_TEXTUREACCESS_STREAMING, |
| 297 | sdl->texture_id = SDL_CreateTexture(sdl->sdl_renderer,fmt, SDL_TEXTUREACCESS_STREAMING, |
| 300 | 298 | tempwidth, tempheight); |
| 301 | 299 | } |
| 302 | | |
| 303 | | //SDL_SetTextureScaleMode(window->sdl_renderer,sdl->texture_id, sdl_sm->sdl_scale_mode); |
| 304 | | |
| 305 | 300 | } |
| 306 | 301 | #endif |
| 307 | 302 | |
| r14439 | r14440 | |
| 309 | 304 | // yuv_overlay_init |
| 310 | 305 | //============================================================ |
| 311 | 306 | |
| 312 | | #if (!SDL_VERSION_ATLEAST(1,3,0)) |
| 307 | #if (!SDLMAME_SDL2) |
| 313 | 308 | static void yuv_overlay_init(sdl_window_info *window) |
| 314 | 309 | { |
| 315 | 310 | sdl_info *sdl = (sdl_info *) window->dxdata; |
| 316 | | const sdl_scale_mode *sdl_sm = sdl->scale_mode; |
| 311 | const sdl_scale_mode *sdl_sm = &scale_modes[video_config.scale_mode]; |
| 317 | 312 | int minimum_width, minimum_height; |
| 318 | 313 | |
| 319 | 314 | window->target->compute_minimum_size(minimum_width, minimum_height); |
| r14439 | r14440 | |
| 364 | 359 | // drawsdl_show_info |
| 365 | 360 | //============================================================ |
| 366 | 361 | |
| 367 | | #if (SDL_VERSION_ATLEAST(1,3,0)) |
| 362 | #if (SDLMAME_SDL2) |
| 368 | 363 | static void drawsdl_show_info(sdl_window_info *window, struct SDL_RendererInfo *render_info) |
| 369 | 364 | { |
| 370 | 365 | #define RF_ENTRY(x) {x, #x } |
| r14439 | r14440 | |
| 400 | 395 | static int drawsdl_window_create(sdl_window_info *window, int width, int height) |
| 401 | 396 | { |
| 402 | 397 | sdl_info *sdl; |
| 398 | const sdl_scale_mode *sm = &scale_modes[video_config.scale_mode]; |
| 403 | 399 | |
| 404 | 400 | // allocate memory for our structures |
| 405 | 401 | sdl = (sdl_info *) osd_malloc(sizeof(sdl_info)); |
| r14439 | r14440 | |
| 407 | 403 | |
| 408 | 404 | window->dxdata = sdl; |
| 409 | 405 | |
| 410 | | sdl->scale_mode = &scale_modes[window->scale_mode]; |
| 406 | #if (SDLMAME_SDL2) |
| 411 | 407 | |
| 412 | | #if (SDL_VERSION_ATLEAST(1,3,0)) |
| 408 | /* set hints ... */ |
| 409 | |
| 410 | SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, sm->sdl_scale_mode); |
| 411 | |
| 413 | 412 | sdl->extra_flags = (window->fullscreen ? |
| 414 | 413 | SDL_WINDOW_BORDERLESS | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS |
| 415 | 414 | | SDL_WINDOW_INPUT_GRABBED : SDL_WINDOW_RESIZABLE); |
| 416 | 415 | |
| 416 | window->sdl_window = SDL_CreateWindow(window->title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, |
| 417 | width, height, sdl->extra_flags); |
| 418 | |
| 417 | 419 | if (window->fullscreen && video_config.switchres) |
| 418 | 420 | { |
| 419 | 421 | SDL_DisplayMode mode; |
| r14439 | r14440 | |
| 427 | 429 | else |
| 428 | 430 | SDL_SetWindowDisplayMode(window->sdl_window, NULL); // Use desktop |
| 429 | 431 | |
| 430 | | window->sdl_window = SDL_CreateWindow(window->title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, |
| 431 | | width, height, sdl->extra_flags); |
| 432 | 432 | SDL_ShowWindow(window->sdl_window); |
| 433 | 433 | |
| 434 | 434 | SDL_SetWindowFullscreen(window->sdl_window, (SDL_bool) window->fullscreen); |
| r14439 | r14440 | |
| 442 | 442 | // create a texture |
| 443 | 443 | |
| 444 | 444 | if (video_config.waitvsync) |
| 445 | | window->sdl_renderer = SDL_CreateRenderer(window->sdl_window, -1, /*SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTDISCARD |*/ SDL_RENDERER_PRESENTVSYNC); |
| 445 | sdl->sdl_renderer = SDL_CreateRenderer(window->sdl_window, -1, /*SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTDISCARD |*/ SDL_RENDERER_PRESENTVSYNC); |
| 446 | 446 | else |
| 447 | | window->sdl_renderer = SDL_CreateRenderer(window->sdl_window, -1, /*SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTDISCARD*/ 0); |
| 447 | sdl->sdl_renderer = SDL_CreateRenderer(window->sdl_window, -1, /*SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTDISCARD*/ 0); |
| 448 | 448 | |
| 449 | 449 | //SDL_SelectRenderer(window->sdl_window); |
| 450 | 450 | |
| 451 | 451 | { |
| 452 | 452 | struct SDL_RendererInfo render_info; |
| 453 | | SDL_GetRendererInfo(window->sdl_renderer, &render_info); |
| 454 | 453 | |
| 454 | SDL_GetRendererInfo(sdl->sdl_renderer, &render_info); |
| 455 | 455 | drawsdl_show_info(window, &render_info); |
| 456 | 456 | |
| 457 | 457 | // Check scale mode |
| 458 | 458 | |
| 459 | | if (sdl->scale_mode->pixel_format) |
| 459 | if (sm->pixel_format) |
| 460 | 460 | { |
| 461 | 461 | int i; |
| 462 | 462 | int found = 0; |
| 463 | 463 | |
| 464 | 464 | for (i=0; i < render_info.num_texture_formats; i++) |
| 465 | | if (sdl->scale_mode->pixel_format == render_info.texture_formats[i]) |
| 465 | if (sm->pixel_format == render_info.texture_formats[i]) |
| 466 | 466 | found = 1; |
| 467 | 467 | |
| 468 | 468 | if (!found) |
| 469 | 469 | { |
| 470 | | mame_printf_verbose("window: Scale mode %s not supported! Using default.\n", sdl->scale_mode->name); |
| 471 | | ui_popup_time(3, "Scale mode %s not supported! Using default.", sdl->scale_mode->name); |
| 472 | | sdl->scale_mode = &scale_modes[0]; |
| 470 | mame_printf_verbose("window: Scale mode %s not supported!\n", sm->name); |
| 471 | ui_popup_time(3, "Scale mode %s not supported!", sm->name); |
| 473 | 472 | } |
| 474 | 473 | } |
| 475 | 474 | } |
| r14439 | r14440 | |
| 478 | 477 | #else |
| 479 | 478 | sdl->extra_flags = (window->fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE); |
| 480 | 479 | |
| 481 | | sdl->extra_flags |= sdl->scale_mode->extra_flags; |
| 480 | sdl->extra_flags |= sm->extra_flags; |
| 482 | 481 | |
| 483 | 482 | sdl->sdlsurf = SDL_SetVideoMode(width, height, |
| 484 | 483 | 0, SDL_SWSURFACE | SDL_ANYFORMAT | sdl->extra_flags); |
| r14439 | r14440 | |
| 489 | 488 | window->width = sdl->sdlsurf->w; |
| 490 | 489 | window->height = sdl->sdlsurf->h; |
| 491 | 490 | |
| 492 | | if (sdl->scale_mode->is_yuv) |
| 491 | if (sm->is_yuv) |
| 493 | 492 | yuv_overlay_init(window); |
| 494 | 493 | |
| 495 | 494 | // set the window title |
| r14439 | r14440 | |
| 508 | 507 | |
| 509 | 508 | static void drawsdl_window_resize(sdl_window_info *window, int width, int height) |
| 510 | 509 | { |
| 511 | | |
| 512 | | #if (SDL_VERSION_ATLEAST(1,3,0)) |
| 510 | #if (!SDLMAME_SDL2) |
| 511 | const sdl_scale_mode *sdl_sm = &scale_modes[video_config.scale_mode]; |
| 512 | #endif |
| 513 | #if (SDLMAME_SDL2) |
| 513 | 514 | SDL_SetWindowSize(window->sdl_window, width, height); |
| 514 | 515 | SDL_GetWindowSize(window->sdl_window, &window->width, &window->height); |
| 515 | 516 | |
| r14439 | r14440 | |
| 529 | 530 | window->width = sdl->sdlsurf->w; |
| 530 | 531 | window->height = sdl->sdlsurf->h; |
| 531 | 532 | |
| 532 | | if (sdl->scale_mode->is_yuv) |
| 533 | if (sdl_sm->is_yuv) |
| 533 | 534 | { |
| 534 | 535 | yuv_overlay_init(window); |
| 535 | 536 | } |
| r14439 | r14440 | |
| 550 | 551 | if (sdl == NULL) |
| 551 | 552 | return; |
| 552 | 553 | |
| 553 | | #if (SDL_VERSION_ATLEAST(1,3,0)) |
| 554 | #if (SDLMAME_SDL2) |
| 554 | 555 | //SDL_SelectRenderer(window->sdl_window); |
| 555 | 556 | SDL_DestroyTexture(sdl->texture_id); |
| 556 | 557 | //SDL_DestroyRenderer(window->sdl_window); |
| r14439 | r14440 | |
| 602 | 603 | static int drawsdl_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt) |
| 603 | 604 | { |
| 604 | 605 | sdl_info *sdl = (sdl_info *) window->dxdata; |
| 606 | const sdl_scale_mode *sm = &scale_modes[video_config.scale_mode]; |
| 605 | 607 | |
| 606 | 608 | *xt = x - sdl->last_hofs; |
| 607 | 609 | *yt = y - sdl->last_vofs; |
| r14439 | r14440 | |
| 609 | 611 | return 0; |
| 610 | 612 | if (*yt<0 || *xt >= window->blitheight) |
| 611 | 613 | return 0; |
| 612 | | if (!sdl->scale_mode->is_scale) |
| 614 | if (!sm->is_scale) |
| 613 | 615 | { |
| 614 | 616 | return 1; |
| 615 | 617 | } |
| r14439 | r14440 | |
| 626 | 628 | static render_primitive_list &drawsdl_window_get_primitives(sdl_window_info *window) |
| 627 | 629 | { |
| 628 | 630 | sdl_info *sdl = (sdl_info *) window->dxdata; |
| 631 | const sdl_scale_mode *sm = &scale_modes[video_config.scale_mode]; |
| 629 | 632 | |
| 630 | 633 | if ((!window->fullscreen) || (video_config.switchres)) |
| 631 | 634 | { |
| r14439 | r14440 | |
| 636 | 639 | sdlwindow_blit_surface_size(window, window->monitor->center_width, window->monitor->center_height); |
| 637 | 640 | } |
| 638 | 641 | |
| 639 | | if (!sdl->scale_mode->is_scale) |
| 642 | if (!sm->is_scale) |
| 640 | 643 | window->target->set_bounds(window->blitwidth, window->blitheight, sdlvideo_monitor_get_aspect(window->monitor)); |
| 641 | 644 | else |
| 642 | 645 | window->target->set_bounds(sdl->hw_scale_width, sdl->hw_scale_height); |
| r14439 | r14440 | |
| 651 | 654 | static int drawsdl_window_draw(sdl_window_info *window, UINT32 dc, int update) |
| 652 | 655 | { |
| 653 | 656 | sdl_info *sdl = (sdl_info *) window->dxdata; |
| 657 | const sdl_scale_mode *sm = &scale_modes[video_config.scale_mode]; |
| 654 | 658 | UINT8 *surfptr; |
| 655 | 659 | INT32 pitch; |
| 656 | 660 | int bpp; |
| 657 | 661 | Uint32 rmask, gmask, bmask; |
| 658 | | #if (SDL_VERSION_ATLEAST(1,3,0)) |
| 662 | #if (SDLMAME_SDL2) |
| 659 | 663 | Uint32 amask; |
| 660 | 664 | #endif |
| 661 | 665 | INT32 vofs, hofs, blitwidth, blitheight, ch, cw; |
| r14439 | r14440 | |
| 670 | 674 | return 1; |
| 671 | 675 | |
| 672 | 676 | // lock it if we need it |
| 673 | | #if (!SDL_VERSION_ATLEAST(1,3,0)) |
| 677 | #if (!SDLMAME_SDL2) |
| 674 | 678 | |
| 675 | 679 | pitch = sdl->sdlsurf->pitch; |
| 676 | 680 | bpp = sdl->sdlsurf->format->BytesPerPixel; |
| r14439 | r14440 | |
| 681 | 685 | |
| 682 | 686 | if (window->blitwidth != sdl->old_blitwidth || window->blitheight != sdl->old_blitheight) |
| 683 | 687 | { |
| 684 | | if (sdl->scale_mode->is_yuv) |
| 688 | if (sm->is_yuv) |
| 685 | 689 | yuv_overlay_init(window); |
| 686 | 690 | sdl->old_blitwidth = window->blitwidth; |
| 687 | 691 | sdl->old_blitheight = window->blitheight; |
| r14439 | r14440 | |
| 698 | 702 | } |
| 699 | 703 | |
| 700 | 704 | |
| 701 | | if (sdl->scale_mode->is_yuv) |
| 705 | if (sm->is_yuv) |
| 702 | 706 | { |
| 703 | 707 | SDL_LockYUVOverlay(sdl->yuvsurf); |
| 704 | 708 | surfptr = sdl->yuvsurf->pixels[0]; // (UINT8 *) sdl->yuv_bitmap; |
| r14439 | r14440 | |
| 711 | 715 | |
| 712 | 716 | if (window->blitwidth != sdl->old_blitwidth || window->blitheight != sdl->old_blitheight) |
| 713 | 717 | { |
| 718 | SDL_RenderSetViewport(sdl->sdl_renderer, NULL); |
| 719 | |
| 714 | 720 | SDL_DestroyTexture(sdl->texture_id); |
| 715 | 721 | setup_texture(window, window->blitwidth, window->blitheight); |
| 716 | 722 | sdl->old_blitwidth = window->blitwidth; |
| r14439 | r14440 | |
| 731 | 737 | if (sdl->blittimer > 0) |
| 732 | 738 | { |
| 733 | 739 | /* SDL Underlays need alpha = 0 ! */ |
| 734 | | SDL_SetRenderDrawColor(window->sdl_renderer,0,0,0,0); |
| 735 | | SDL_RenderFillRect(window->sdl_renderer,NULL); |
| 740 | SDL_SetRenderDrawColor(sdl->sdl_renderer,0,0,0,0); |
| 741 | SDL_RenderFillRect(sdl->sdl_renderer,NULL); |
| 736 | 742 | //SDL_RenderFill(0,0,0,0 /*255*/,NULL); |
| 737 | 743 | sdl->blittimer--; |
| 738 | 744 | } |
| r14439 | r14440 | |
| 784 | 790 | window->primlist->acquire_lock(); |
| 785 | 791 | |
| 786 | 792 | // render to it |
| 787 | | if (!sdl->scale_mode->is_yuv) |
| 793 | if (!sm->is_yuv) |
| 788 | 794 | { |
| 789 | 795 | int mamewidth, mameheight; |
| 790 | 796 | |
| 791 | | if (!sdl->scale_mode->is_scale) |
| 797 | if (!sm->is_scale) |
| 792 | 798 | { |
| 793 | 799 | mamewidth = blitwidth; |
| 794 | 800 | mameheight = blitheight; |
| 795 | | #if !SDL_VERSION_ATLEAST(1,3,0) |
| 801 | #if !SDLMAME_SDL2 |
| 796 | 802 | surfptr += ((vofs * pitch) + (hofs * bpp)); |
| 797 | 803 | #endif |
| 798 | 804 | } |
| r14439 | r14440 | |
| 833 | 839 | assert (sdl->yuv_bitmap != NULL); |
| 834 | 840 | assert (surfptr != NULL); |
| 835 | 841 | drawsdl_rgb555_draw_primitives(*window->primlist, sdl->yuv_bitmap, sdl->hw_scale_width, sdl->hw_scale_height, sdl->hw_scale_width); |
| 836 | | sdl->scale_mode->yuv_blit((UINT16 *)sdl->yuv_bitmap, sdl, surfptr, pitch); |
| 842 | sm->yuv_blit((UINT16 *)sdl->yuv_bitmap, sdl, surfptr, pitch); |
| 837 | 843 | } |
| 838 | 844 | |
| 839 | 845 | window->primlist->release_lock(); |
| 840 | 846 | |
| 841 | 847 | // unlock and flip |
| 842 | | #if (!SDL_VERSION_ATLEAST(1,3,0)) |
| 848 | #if (!SDLMAME_SDL2) |
| 843 | 849 | if (SDL_MUSTLOCK(sdl->sdlsurf)) SDL_UnlockSurface(sdl->sdlsurf); |
| 844 | | if (!sdl->scale_mode->is_yuv) |
| 850 | if (!sm->is_yuv) |
| 845 | 851 | { |
| 846 | 852 | SDL_Flip(sdl->sdlsurf); |
| 847 | 853 | } |
| r14439 | r14440 | |
| 867 | 873 | r.h=blitheight; |
| 868 | 874 | //printf("blitwidth %d %d - %d %d\n", blitwidth, blitheight, window->width, window->height); |
| 869 | 875 | //SDL_UpdateTexture(sdl->sdltex, NULL, sdl->sdlsurf->pixels, pitch); |
| 870 | | SDL_RenderCopy(window->sdl_renderer,sdl->texture_id, NULL, &r); |
| 871 | | SDL_RenderPresent(window->sdl_renderer); |
| 876 | SDL_RenderCopy(sdl->sdl_renderer,sdl->texture_id, NULL, &r); |
| 877 | SDL_RenderPresent(sdl->sdl_renderer); |
| 872 | 878 | } |
| 873 | 879 | #endif |
| 874 | 880 | return 0; |
src/lib/lib7z/Aes.c
| r0 | r14440 | |
| 1 | /* Aes.c -- AES encryption / decryption
|
| 2 | 2009-11-23 : Igor Pavlov : Public domain */
|
| 3 | |
| 4 | #include "Aes.h"
|
| 5 | #include "CpuArch.h"
|
| 6 | |
| 7 | static UInt32 T[256 * 4];
|
| 8 | static Byte Sbox[256] = {
|
| 9 | 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
|
| 10 | 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
|
| 11 | 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
|
| 12 | 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
|
| 13 | 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
|
| 14 | 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
|
| 15 | 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
|
| 16 | 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
|
| 17 | 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
|
| 18 | 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
|
| 19 | 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
|
| 20 | 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
|
| 21 | 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
|
| 22 | 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
|
| 23 | 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
|
| 24 | 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16};
|
| 25 | |
| 26 | void MY_FAST_CALL AesCbc_Encode(UInt32 *ivAes, Byte *data, size_t numBlocks);
|
| 27 | void MY_FAST_CALL AesCbc_Decode(UInt32 *ivAes, Byte *data, size_t numBlocks);
|
| 28 | void MY_FAST_CALL AesCtr_Code(UInt32 *ivAes, Byte *data, size_t numBlocks);
|
| 29 | |
| 30 | void MY_FAST_CALL AesCbc_Encode_Intel(UInt32 *ivAes, Byte *data, size_t numBlocks);
|
| 31 | void MY_FAST_CALL AesCbc_Decode_Intel(UInt32 *ivAes, Byte *data, size_t numBlocks);
|
| 32 | void MY_FAST_CALL AesCtr_Code_Intel(UInt32 *ivAes, Byte *data, size_t numBlocks);
|
| 33 | |
| 34 | AES_CODE_FUNC g_AesCbc_Encode;
|
| 35 | AES_CODE_FUNC g_AesCbc_Decode;
|
| 36 | AES_CODE_FUNC g_AesCtr_Code;
|
| 37 | |
| 38 | static UInt32 D[256 * 4];
|
| 39 | static Byte InvS[256];
|
| 40 | |
| 41 | static Byte Rcon[11] = { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
|
| 42 | |
| 43 | #define xtime(x) ((((x) << 1) ^ (((x) & 0x80) != 0 ? 0x1B : 0)) & 0xFF)
|
| 44 | |
| 45 | #define Ui32(a0, a1, a2, a3) ((UInt32)(a0) | ((UInt32)(a1) << 8) | ((UInt32)(a2) << 16) | ((UInt32)(a3) << 24))
|
| 46 | |
| 47 | #define gb0(x) ( (x) & 0xFF)
|
| 48 | #define gb1(x) (((x) >> ( 8)) & 0xFF)
|
| 49 | #define gb2(x) (((x) >> (16)) & 0xFF)
|
| 50 | #define gb3(x) (((x) >> (24)) & 0xFF)
|
| 51 | |
| 52 | void AesGenTables(void)
|
| 53 | {
|
| 54 | unsigned i;
|
| 55 | for (i = 0; i < 256; i++)
|
| 56 | InvS[Sbox[i]] = (Byte)i;
|
| 57 | for (i = 0; i < 256; i++)
|
| 58 | {
|
| 59 | {
|
| 60 | UInt32 a1 = Sbox[i];
|
| 61 | UInt32 a2 = xtime(a1);
|
| 62 | UInt32 a3 = a2 ^ a1;
|
| 63 | T[ i] = Ui32(a2, a1, a1, a3);
|
| 64 | T[0x100 + i] = Ui32(a3, a2, a1, a1);
|
| 65 | T[0x200 + i] = Ui32(a1, a3, a2, a1);
|
| 66 | T[0x300 + i] = Ui32(a1, a1, a3, a2);
|
| 67 | }
|
| 68 | {
|
| 69 | UInt32 a1 = InvS[i];
|
| 70 | UInt32 a2 = xtime(a1);
|
| 71 | UInt32 a4 = xtime(a2);
|
| 72 | UInt32 a8 = xtime(a4);
|
| 73 | UInt32 a9 = a8 ^ a1;
|
| 74 | UInt32 aB = a8 ^ a2 ^ a1;
|
| 75 | UInt32 aD = a8 ^ a4 ^ a1;
|
| 76 | UInt32 aE = a8 ^ a4 ^ a2;
|
| 77 | D[ i] = Ui32(aE, a9, aD, aB);
|
| 78 | D[0x100 + i] = Ui32(aB, aE, a9, aD);
|
| 79 | D[0x200 + i] = Ui32(aD, aB, aE, a9);
|
| 80 | D[0x300 + i] = Ui32(a9, aD, aB, aE);
|
| 81 | }
|
| 82 | }
|
| 83 | g_AesCbc_Encode = AesCbc_Encode;
|
| 84 | g_AesCbc_Decode = AesCbc_Decode;
|
| 85 | g_AesCtr_Code = AesCtr_Code;
|
| 86 | #ifdef MY_CPU_X86_OR_AMD64
|
| 87 | if (CPU_Is_Aes_Supported())
|
| 88 | {
|
| 89 | g_AesCbc_Encode = AesCbc_Encode_Intel;
|
| 90 | g_AesCbc_Decode = AesCbc_Decode_Intel;
|
| 91 | g_AesCtr_Code = AesCtr_Code_Intel;
|
| 92 | }
|
| 93 | #endif
|
| 94 | }
|
| 95 | |
| 96 | #define HT(i, x, s) (T + (x << 8))[gb ## x(s[(i + x) & 3])]
|
| 97 | #define HT4(m, i, s, p) m[i] = \
|
| 98 | HT(i, 0, s) ^ \
|
| 99 | HT(i, 1, s) ^ \
|
| 100 | HT(i, 2, s) ^ \
|
| 101 | HT(i, 3, s) ^ w[p + i]
|
| 102 | /* such order (2031) in HT16 is for VC6/K8 speed optimization) */
|
| 103 | #define HT16(m, s, p) \
|
| 104 | HT4(m, 2, s, p); \
|
| 105 | HT4(m, 0, s, p); \
|
| 106 | HT4(m, 3, s, p); \
|
| 107 | HT4(m, 1, s, p); \
|
| 108 | |
| 109 | #define FT(i, x) Sbox[gb ## x(m[(i + x) & 3])]
|
| 110 | #define FT4(i) dest[i] = Ui32(FT(i, 0), FT(i, 1), FT(i, 2), FT(i, 3)) ^ w[i];
|
| 111 | |
| 112 | #define HD(i, x, s) (D + (x << 8))[gb ## x(s[(i - x) & 3])]
|
| 113 | #define HD4(m, i, s, p) m[i] = \
|
| 114 | HD(i, 0, s) ^ \
|
| 115 | HD(i, 1, s) ^ \
|
| 116 | HD(i, 2, s) ^ \
|
| 117 | HD(i, 3, s) ^ w[p + i];
|
| 118 | /* such order (0231) in HD16 is for VC6/K8 speed optimization) */
|
| 119 | #define HD16(m, s, p) \
|
| 120 | HD4(m, 0, s, p); \
|
| 121 | HD4(m, 2, s, p); \
|
| 122 | HD4(m, 3, s, p); \
|
| 123 | HD4(m, 1, s, p); \
|
| 124 | |
| 125 | #define FD(i, x) InvS[gb ## x(m[(i - x) & 3])]
|
| 126 | #define FD4(i) dest[i] = Ui32(FD(i, 0), FD(i, 1), FD(i, 2), FD(i, 3)) ^ w[i];
|
| 127 | |
| 128 | void MY_FAST_CALL Aes_SetKey_Enc(UInt32 *w, const Byte *key, unsigned keySize)
|
| 129 | {
|
| 130 | unsigned i, wSize;
|
| 131 | wSize = keySize + 28;
|
| 132 | keySize /= 4;
|
| 133 | w[0] = ((UInt32)keySize / 2) + 3;
|
| 134 | w += 4;
|
| 135 | |
| 136 | for (i = 0; i < keySize; i++, key += 4)
|
| 137 | w[i] = GetUi32(key);
|
| 138 | |
| 139 | for (; i < wSize; i++)
|
| 140 | {
|
| 141 | UInt32 t = w[i - 1];
|
| 142 | unsigned rem = i % keySize;
|
| 143 | if (rem == 0)
|
| 144 | t = Ui32(Sbox[gb1(t)] ^ Rcon[i / keySize], Sbox[gb2(t)], Sbox[gb3(t)], Sbox[gb0(t)]);
|
| 145 | else if (keySize > 6 && rem == 4)
|
| 146 | t = Ui32(Sbox[gb0(t)], Sbox[gb1(t)], Sbox[gb2(t)], Sbox[gb3(t)]);
|
| 147 | w[i] = w[i - keySize] ^ t;
|
| 148 | }
|
| 149 | }
|
| 150 | |
| 151 | void MY_FAST_CALL Aes_SetKey_Dec(UInt32 *w, const Byte *key, unsigned keySize)
|
| 152 | {
|
| 153 | unsigned i, num;
|
| 154 | Aes_SetKey_Enc(w, key, keySize);
|
| 155 | num = keySize + 20;
|
| 156 | w += 8;
|
| 157 | for (i = 0; i < num; i++)
|
| 158 | {
|
| 159 | UInt32 r = w[i];
|
| 160 | w[i] =
|
| 161 | D[ Sbox[gb0(r)]] ^
|
| 162 | D[0x100 + Sbox[gb1(r)]] ^
|
| 163 | D[0x200 + Sbox[gb2(r)]] ^
|
| 164 | D[0x300 + Sbox[gb3(r)]];
|
| 165 | }
|
| 166 | }
|
| 167 | |
| 168 | /* Aes_Encode and Aes_Decode functions work with little-endian words.
|
| 169 | src and dest are pointers to 4 UInt32 words.
|
| 170 | arc and dest can point to same block */
|
| 171 | |
| 172 | static void Aes_Encode(const UInt32 *w, UInt32 *dest, const UInt32 *src)
|
| 173 | {
|
| 174 | UInt32 s[4];
|
| 175 | UInt32 m[4];
|
| 176 | UInt32 numRounds2 = w[0];
|
| 177 | w += 4;
|
| 178 | s[0] = src[0] ^ w[0];
|
| 179 | s[1] = src[1] ^ w[1];
|
| 180 | s[2] = src[2] ^ w[2];
|
| 181 | s[3] = src[3] ^ w[3];
|
| 182 | w += 4;
|
| 183 | for (;;)
|
| 184 | {
|
| 185 | HT16(m, s, 0);
|
| 186 | if (--numRounds2 == 0)
|
| 187 | break;
|
| 188 | HT16(s, m, 4);
|
| 189 | w += 8;
|
| 190 | }
|
| 191 | w += 4;
|
| 192 | FT4(0); FT4(1); FT4(2); FT4(3);
|
| 193 | }
|
| 194 | |
| 195 | static void Aes_Decode(const UInt32 *w, UInt32 *dest, const UInt32 *src)
|
| 196 | {
|
| 197 | UInt32 s[4];
|
| 198 | UInt32 m[4];
|
| 199 | UInt32 numRounds2 = w[0];
|
| 200 | w += 4 + numRounds2 * 8;
|
| 201 | s[0] = src[0] ^ w[0];
|
| 202 | s[1] = src[1] ^ w[1];
|
| 203 | s[2] = src[2] ^ w[2];
|
| 204 | s[3] = src[3] ^ w[3];
|
| 205 | for (;;)
|
| 206 | {
|
| 207 | w -= 8;
|
| 208 | HD16(m, s, 4);
|
| 209 | if (--numRounds2 == 0)
|
| 210 | break;
|
| 211 | HD16(s, m, 0);
|
| 212 | }
|
| 213 | FD4(0); FD4(1); FD4(2); FD4(3);
|
| 214 | }
|
| 215 | |
| 216 | void AesCbc_Init(UInt32 *p, const Byte *iv)
|
| 217 | {
|
| 218 | unsigned i;
|
| 219 | for (i = 0; i < 4; i++)
|
| 220 | p[i] = GetUi32(iv + i * 4);
|
| 221 | }
|
| 222 | |
| 223 | void MY_FAST_CALL AesCbc_Encode(UInt32 *p, Byte *data, size_t numBlocks)
|
| 224 | {
|
| 225 | for (; numBlocks != 0; numBlocks--, data += AES_BLOCK_SIZE)
|
| 226 | {
|
| 227 | p[0] ^= GetUi32(data);
|
| 228 | p[1] ^= GetUi32(data + 4);
|
| 229 | p[2] ^= GetUi32(data + 8);
|
| 230 | p[3] ^= GetUi32(data + 12);
|
| 231 | |
| 232 | Aes_Encode(p + 4, p, p);
|
| 233 | |
| 234 | SetUi32(data, p[0]);
|
| 235 | SetUi32(data + 4, p[1]);
|
| 236 | SetUi32(data + 8, p[2]);
|
| 237 | SetUi32(data + 12, p[3]);
|
| 238 | }
|
| 239 | }
|
| 240 | |
| 241 | void MY_FAST_CALL AesCbc_Decode(UInt32 *p, Byte *data, size_t numBlocks)
|
| 242 | {
|
| 243 | UInt32 in[4], out[4];
|
| 244 | for (; numBlocks != 0; numBlocks--, data += AES_BLOCK_SIZE)
|
| 245 | {
|
| 246 | in[0] = GetUi32(data);
|
| 247 | in[1] = GetUi32(data + 4);
|
| 248 | in[2] = GetUi32(data + 8);
|
| 249 | in[3] = GetUi32(data + 12);
|
| 250 | |
| 251 | Aes_Decode(p + 4, out, in);
|
| 252 | |
| 253 | SetUi32(data, p[0] ^ out[0]);
|
| 254 | SetUi32(data + 4, p[1] ^ out[1]);
|
| 255 | SetUi32(data + 8, p[2] ^ out[2]);
|
| 256 | SetUi32(data + 12, p[3] ^ out[3]);
|
| 257 | |
| 258 | p[0] = in[0];
|
| 259 | p[1] = in[1];
|
| 260 | p[2] = in[2];
|
| 261 | p[3] = in[3];
|
| 262 | }
|
| 263 | }
|
| 264 | |
| 265 | void MY_FAST_CALL AesCtr_Code(UInt32 *p, Byte *data, size_t numBlocks)
|
| 266 | {
|
| 267 | for (; numBlocks != 0; numBlocks--)
|
| 268 | {
|
| 269 | UInt32 temp[4];
|
| 270 | Byte buf[16];
|
| 271 | int i;
|
| 272 | if (++p[0] == 0)
|
| 273 | p[1]++;
|
| 274 | Aes_Encode(p + 4, temp, p);
|
| 275 | SetUi32(buf, temp[0]);
|
| 276 | SetUi32(buf + 4, temp[1]);
|
| 277 | SetUi32(buf + 8, temp[2]);
|
| 278 | SetUi32(buf + 12, temp[3]);
|
| 279 | for (i = 0; i < 16; i++)
|
| 280 | *data++ ^= buf[i];
|
| 281 | }
|
| 282 | }
|
src/lib/lib7z/XzDec.c
| r0 | r14440 | |
| 1 | /* XzDec.c -- Xz Decode
|
| 2 | 2011-02-07 : Igor Pavlov : Public domain */
|
| 3 | |
| 4 | /* #define XZ_DUMP */
|
| 5 | |
| 6 | #ifdef XZ_DUMP
|
| 7 | #include <stdio.h>
|
| 8 | #endif
|
| 9 | |
| 10 | #include <stdlib.h>
|
| 11 | #include <string.h>
|
| 12 | |
| 13 | #include "7zCrc.h"
|
| 14 | #include "Alloc.h"
|
| 15 | #include "Bra.h"
|
| 16 | #include "CpuArch.h"
|
| 17 | #include "Delta.h"
|
| 18 | #include "Lzma2Dec.h"
|
| 19 | |
| 20 | #ifdef USE_SUBBLOCK
|
| 21 | #include "Bcj3Dec.c"
|
| 22 | #include "SbDec.c"
|
| 23 | #endif
|
| 24 | |
| 25 | #include "Xz.h"
|
| 26 | |
| 27 | #define XZ_CHECK_SIZE_MAX 64
|
| 28 | |
| 29 | #define CODER_BUF_SIZE (1 << 17)
|
| 30 | |
| 31 | unsigned Xz_ReadVarInt(const Byte *p, size_t maxSize, UInt64 *value)
|
| 32 | {
|
| 33 | int i, limit;
|
| 34 | *value = 0;
|
| 35 | limit = (maxSize > 9) ? 9 : (int)maxSize;
|
| 36 | |
| 37 | for (i = 0; i < limit;)
|
| 38 | {
|
| 39 | Byte b = p[i];
|
| 40 | *value |= (UInt64)(b & 0x7F) << (7 * i++);
|
| 41 | if ((b & 0x80) == 0)
|
| 42 | return (b == 0 && i != 1) ? 0 : i;
|
| 43 | }
|
| 44 | return 0;
|
| 45 | }
|
| 46 | |
| 47 | /* ---------- BraState ---------- */
|
| 48 | |
| 49 | #define BRA_BUF_SIZE (1 << 14)
|
| 50 | |
| 51 | typedef struct
|
| 52 | {
|
| 53 | size_t bufPos;
|
| 54 | size_t bufConv;
|
| 55 | size_t bufTotal;
|
| 56 | |
| 57 | UInt32 methodId;
|
| 58 | int encodeMode;
|
| 59 | UInt32 delta;
|
| 60 | UInt32 ip;
|
| 61 | UInt32 x86State;
|
| 62 | Byte deltaState[DELTA_STATE_SIZE];
|
| 63 | |
| 64 | Byte buf[BRA_BUF_SIZE];
|
| 65 | } CBraState;
|
| 66 | |
| 67 | void BraState_Free(void *pp, ISzAlloc *alloc)
|
| 68 | {
|
| 69 | alloc->Free(alloc, pp);
|
| 70 | }
|
| 71 | |
| 72 | SRes BraState_SetProps(void *pp, const Byte *props, size_t propSize, ISzAlloc *alloc)
|
| 73 | {
|
| 74 | CBraState *p = ((CBraState *)pp);
|
| 75 | alloc = alloc;
|
| 76 | p->ip = 0;
|
| 77 | if (p->methodId == XZ_ID_Delta)
|
| 78 | {
|
| 79 | if (propSize != 1)
|
| 80 | return SZ_ERROR_UNSUPPORTED;
|
| 81 | p->delta = (unsigned)props[0] + 1;
|
| 82 | }
|
| 83 | else
|
| 84 | {
|
| 85 | if (propSize == 4)
|
| 86 | {
|
| 87 | UInt32 v = GetUi32(props);
|
| 88 | switch(p->methodId)
|
| 89 | {
|
| 90 | case XZ_ID_PPC:
|
| 91 | case XZ_ID_ARM:
|
| 92 | case XZ_ID_SPARC:
|
| 93 | if ((v & 3) != 0)
|
| 94 | return SZ_ERROR_UNSUPPORTED;
|
| 95 | break;
|
| 96 | case XZ_ID_ARMT:
|
| 97 | if ((v & 1) != 0)
|
| 98 | return SZ_ERROR_UNSUPPORTED;
|
| 99 | break;
|
| 100 | case XZ_ID_IA64:
|
| 101 | if ((v & 0xF) != 0)
|
| 102 | return SZ_ERROR_UNSUPPORTED;
|
| 103 | break;
|
| 104 | }
|
| 105 | p->ip = v;
|
| 106 | }
|
| 107 | else if (propSize != 0)
|
| 108 | return SZ_ERROR_UNSUPPORTED;
|
| 109 | }
|
| 110 | return SZ_OK;
|
| 111 | }
|
| 112 | |
| 113 | void BraState_Init(void *pp)
|
| 114 | {
|
| 115 | CBraState *p = ((CBraState *)pp);
|
| 116 | p->bufPos = p->bufConv = p->bufTotal = 0;
|
| 117 | x86_Convert_Init(p->x86State);
|
| 118 | if (p->methodId == XZ_ID_Delta)
|
| 119 | Delta_Init(p->deltaState);
|
| 120 | }
|
| 121 | |
| 122 | #define CASE_BRA_CONV(isa) case XZ_ID_ ## isa: p->bufConv = isa ## _Convert(p->buf, p->bufTotal, p->ip, p->encodeMode); break;
|
| 123 | |
| 124 | static SRes BraState_Code(void *pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
|
| 125 | int srcWasFinished, ECoderFinishMode finishMode, int *wasFinished)
|
| 126 | {
|
| 127 | CBraState *p = ((CBraState *)pp);
|
| 128 | SizeT destLenOrig = *destLen;
|
| 129 | SizeT srcLenOrig = *srcLen;
|
| 130 | *destLen = 0;
|
| 131 | *srcLen = 0;
|
| 132 | finishMode = finishMode;
|
| 133 | *wasFinished = 0;
|
| 134 | while (destLenOrig > 0)
|
| 135 | {
|
| 136 | if (p->bufPos != p->bufConv)
|
| 137 | {
|
| 138 | size_t curSize = p->bufConv - p->bufPos;
|
| 139 | if (curSize > destLenOrig)
|
| 140 | curSize = destLenOrig;
|
| 141 | memcpy(dest, p->buf + p->bufPos, curSize);
|
| 142 | p->bufPos += curSize;
|
| 143 | *destLen += curSize;
|
| 144 | dest += curSize;
|
| 145 | destLenOrig -= curSize;
|
| 146 | continue;
|
| 147 | }
|
| 148 | p->bufTotal -= p->bufPos;
|
| 149 | memmove(p->buf, p->buf + p->bufPos, p->bufTotal);
|
| 150 | p->bufPos = 0;
|
| 151 | p->bufConv = 0;
|
| 152 | {
|
| 153 | size_t curSize = BRA_BUF_SIZE - p->bufTotal;
|
| 154 | if (curSize > srcLenOrig)
|
| 155 | curSize = srcLenOrig;
|
| 156 | memcpy(p->buf + p->bufTotal, src, curSize);
|
| 157 | *srcLen += curSize;
|
| 158 | src += curSize;
|
| 159 | srcLenOrig -= curSize;
|
| 160 | p->bufTotal += curSize;
|
| 161 | }
|
| 162 | if (p->bufTotal == 0)
|
| 163 | break;
|
| 164 | switch(p->methodId)
|
| 165 | {
|
| 166 | case XZ_ID_Delta:
|
| 167 | if (p->encodeMode)
|
| 168 | Delta_Encode(p->deltaState, p->delta, p->buf, p->bufTotal);
|
| 169 | else
|
| 170 | Delta_Decode(p->deltaState, p->delta, p->buf, p->bufTotal);
|
| 171 | p->bufConv = p->bufTotal;
|
| 172 | break;
|
| 173 | case XZ_ID_X86:
|
| 174 | p->bufConv = x86_Convert(p->buf, p->bufTotal, p->ip, &p->x86State, p->encodeMode);
|
| 175 | break;
|
| 176 | CASE_BRA_CONV(PPC)
|
| 177 | CASE_BRA_CONV(IA64)
|
| 178 | CASE_BRA_CONV(ARM)
|
| 179 | CASE_BRA_CONV(ARMT)
|
| 180 | CASE_BRA_CONV(SPARC)
|
| 181 | default:
|
| 182 | return SZ_ERROR_UNSUPPORTED;
|
| 183 | }
|
| 184 | p->ip += (UInt32)p->bufConv;
|
| 185 | |
| 186 | if (p->bufConv == 0)
|
| 187 | {
|
| 188 | if (!srcWasFinished)
|
| 189 | break;
|
| 190 | p->bufConv = p->bufTotal;
|
| 191 | }
|
| 192 | }
|
| 193 | if (p->bufTotal == p->bufPos && srcLenOrig == 0 && srcWasFinished)
|
| 194 | *wasFinished = 1;
|
| 195 | return SZ_OK;
|
| 196 | }
|
| 197 | |
| 198 | SRes BraState_SetFromMethod(IStateCoder *p, UInt64 id, int encodeMode, ISzAlloc *alloc)
|
| 199 | {
|
| 200 | CBraState *decoder;
|
| 201 | if (id != XZ_ID_Delta &&
|
| 202 | id != XZ_ID_X86 &&
|
| 203 | id != XZ_ID_PPC &&
|
| 204 | id != XZ_ID_IA64 &&
|
| 205 | id != XZ_ID_ARM &&
|
| 206 | id != XZ_ID_ARMT &&
|
| 207 | id != XZ_ID_SPARC)
|
| 208 | return SZ_ERROR_UNSUPPORTED;
|
| 209 | p->p = 0;
|
| 210 | decoder = alloc->Alloc(alloc, sizeof(CBraState));
|
| 211 | if (decoder == 0)
|
| 212 | return SZ_ERROR_MEM;
|
| 213 | decoder->methodId = (UInt32)id;
|
| 214 | decoder->encodeMode = encodeMode;
|
| 215 | p->p = decoder;
|
| 216 | p->Free = BraState_Free;
|
| 217 | p->SetProps = BraState_SetProps;
|
| 218 | p->Init = BraState_Init;
|
| 219 | p->Code = BraState_Code;
|
| 220 | return SZ_OK;
|
| 221 | }
|
| 222 | |
| 223 | /* ---------- SbState ---------- */
|
| 224 | |
| 225 | #ifdef USE_SUBBLOCK
|
| 226 | |
| 227 | static void SbState_Free(void *pp, ISzAlloc *alloc)
|
| 228 | {
|
| 229 | CSbDec *p = (CSbDec *)pp;
|
| 230 | SbDec_Free(p);
|
| 231 | alloc->Free(alloc, pp);
|
| 232 | }
|
| 233 | |
| 234 | static SRes SbState_SetProps(void *pp, const Byte *props, size_t propSize, ISzAlloc *alloc)
|
| 235 | {
|
| 236 | pp = pp;
|
| 237 | props = props;
|
| 238 | alloc = alloc;
|
| 239 | return (propSize == 0) ? SZ_OK : SZ_ERROR_UNSUPPORTED;
|
| 240 | }
|
| 241 | |
| 242 | static void SbState_Init(void *pp)
|
| 243 | {
|
| 244 | SbDec_Init((CSbDec *)pp);
|
| 245 | }
|
| 246 | |
| 247 | static SRes SbState_Code(void *pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
|
| 248 | int srcWasFinished, ECoderFinishMode finishMode, int *wasFinished)
|
| 249 | {
|
| 250 | CSbDec *p = (CSbDec *)pp;
|
| 251 | SRes res;
|
| 252 | srcWasFinished = srcWasFinished;
|
| 253 | p->dest = dest;
|
| 254 | p->destLen = *destLen;
|
| 255 | p->src = src;
|
| 256 | p->srcLen = *srcLen;
|
| 257 | p->finish = finishMode; /* change it */
|
| 258 | res = SbDec_Decode((CSbDec *)pp);
|
| 259 | *destLen -= p->destLen;
|
| 260 | *srcLen -= p->srcLen;
|
| 261 | *wasFinished = (*destLen == 0 && *srcLen == 0); /* change it */
|
| 262 | return res;
|
| 263 | }
|
| 264 | |
| 265 | SRes SbState_SetFromMethod(IStateCoder *p, ISzAlloc *alloc)
|
| 266 | {
|
| 267 | CSbDec *decoder;
|
| 268 | p->p = 0;
|
| 269 | decoder = alloc->Alloc(alloc, sizeof(CSbDec));
|
| 270 | if (decoder == 0)
|
| 271 | return SZ_ERROR_MEM;
|
| 272 | p->p = decoder;
|
| 273 | p->Free = SbState_Free;
|
| 274 | p->SetProps = SbState_SetProps;
|
| 275 | p->Init = SbState_Init;
|
| 276 | p->Code = SbState_Code;
|
| 277 | SbDec_Construct(decoder);
|
| 278 | SbDec_SetAlloc(decoder, alloc);
|
| 279 | return SZ_OK;
|
| 280 | }
|
| 281 | #endif
|
| 282 | |
| 283 | /* ---------- Lzma2State ---------- */
|
| 284 | |
| 285 | static void Lzma2State_Free(void *pp, ISzAlloc *alloc)
|
| 286 | {
|
| 287 | Lzma2Dec_Free((CLzma2Dec *)pp, alloc);
|
| 288 | alloc->Free(alloc, pp);
|
| 289 | }
|
| 290 | |
| 291 | static SRes Lzma2State_SetProps(void *pp, const Byte *props, size_t propSize, ISzAlloc *alloc)
|
| 292 | {
|
| 293 | if (propSize != 1)
|
| 294 | return SZ_ERROR_UNSUPPORTED;
|
| 295 | return Lzma2Dec_Allocate((CLzma2Dec *)pp, props[0], alloc);
|
| 296 | }
|
| 297 | |
| 298 | static void Lzma2State_Init(void *pp)
|
| 299 | {
|
| 300 | Lzma2Dec_Init((CLzma2Dec *)pp);
|
| 301 | }
|
| 302 | |
| 303 | static SRes Lzma2State_Code(void *pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
|
| 304 | int srcWasFinished, ECoderFinishMode finishMode, int *wasFinished)
|
| 305 | {
|
| 306 | ELzmaStatus status;
|
| 307 | /* ELzmaFinishMode fm = (finishMode == LZMA_FINISH_ANY) ? LZMA_FINISH_ANY : LZMA_FINISH_END; */
|
| 308 | SRes res = Lzma2Dec_DecodeToBuf((CLzma2Dec *)pp, dest, destLen, src, srcLen, finishMode, &status);
|
| 309 | srcWasFinished = srcWasFinished;
|
| 310 | *wasFinished = (status == LZMA_STATUS_FINISHED_WITH_MARK);
|
| 311 | return res;
|
| 312 | }
|
| 313 | |
| 314 | static SRes Lzma2State_SetFromMethod(IStateCoder *p, ISzAlloc *alloc)
|
| 315 | {
|
| 316 | CLzma2Dec *decoder = alloc->Alloc(alloc, sizeof(CLzma2Dec));
|
| 317 | p->p = decoder;
|
| 318 | if (decoder == 0)
|
| 319 | return SZ_ERROR_MEM;
|
| 320 | p->Free = Lzma2State_Free;
|
| 321 | p->SetProps = Lzma2State_SetProps;
|
| 322 | p->Init = Lzma2State_Init;
|
| 323 | p->Code = Lzma2State_Code;
|
| 324 | Lzma2Dec_Construct(decoder);
|
| 325 | return SZ_OK;
|
| 326 | }
|
| 327 | |
| 328 | |
| 329 | void MixCoder_Construct(CMixCoder *p, ISzAlloc *alloc)
|
| 330 | {
|
| 331 | int i;
|
| 332 | p->alloc = alloc;
|
| 333 | p->buf = 0;
|
| 334 | p->numCoders = 0;
|
| 335 | for (i = 0; i < MIXCODER_NUM_FILTERS_MAX; i++)
|
| 336 | p->coders[i].p = NULL;
|
| 337 | }
|
| 338 | |
| 339 | void MixCoder_Free(CMixCoder *p)
|
| 340 | {
|
| 341 | int i;
|
| 342 | for (i = 0; i < p->numCoders; i++)
|
| 343 | {
|
| 344 | IStateCoder *sc = &p->coders[i];
|
| 345 | if (p->alloc && sc->p)
|
| 346 | sc->Free(sc->p, p->alloc);
|
| 347 | }
|
| 348 | p->numCoders = 0;
|
| 349 | if (p->buf)
|
| 350 | p->alloc->Free(p->alloc, p->buf);
|
| 351 | }
|
| 352 | |
| 353 | void MixCoder_Init(CMixCoder *p)
|
| 354 | {
|
| 355 | int i;
|
| 356 | for (i = 0; i < p->numCoders - 1; i++)
|
| 357 | {
|
| 358 | p->size[i] = 0;
|
| 359 | p->pos[i] = 0;
|
| 360 | p->finished[i] = 0;
|
| 361 | }
|
| 362 | for (i = 0; i < p->numCoders; i++)
|
| 363 | {
|
| 364 | IStateCoder *coder = &p->coders[i];
|
| 365 | coder->Init(coder->p);
|
| 366 | }
|
| 367 | }
|
| 368 | |
| 369 | SRes MixCoder_SetFromMethod(CMixCoder *p, int coderIndex, UInt64 methodId)
|
| 370 | {
|
| 371 | IStateCoder *sc = &p->coders[coderIndex];
|
| 372 | p->ids[coderIndex] = methodId;
|
| 373 | switch(methodId)
|
| 374 | {
|
| 375 | case XZ_ID_LZMA2: return Lzma2State_SetFromMethod(sc, p->alloc);
|
| 376 | #ifdef USE_SUBBLOCK
|
| 377 | case XZ_ID_Subblock: return SbState_SetFromMethod(sc, p->alloc);
|
| 378 | #endif
|
| 379 | }
|
| 380 | if (coderIndex == 0)
|
| 381 | return SZ_ERROR_UNSUPPORTED;
|
| 382 | return BraState_SetFromMethod(sc, methodId, 0, p->alloc);
|
| 383 | }
|
| 384 | |
| 385 | SRes MixCoder_Code(CMixCoder *p, Byte *dest, SizeT *destLen,
|
| 386 | const Byte *src, SizeT *srcLen, int srcWasFinished,
|
| 387 | ECoderFinishMode finishMode, ECoderStatus *status)
|
| 388 | {
|
| 389 | SizeT destLenOrig = *destLen;
|
| 390 | SizeT srcLenOrig = *srcLen;
|
| 391 | Bool allFinished = True;
|
| 392 | *destLen = 0;
|
| 393 | *srcLen = 0;
|
| 394 | *status = CODER_STATUS_NOT_FINISHED;
|
| 395 | |
| 396 | if (p->buf == 0)
|
| 397 | {
|
| 398 | p->buf = p->alloc->Alloc(p->alloc, CODER_BUF_SIZE * (MIXCODER_NUM_FILTERS_MAX - 1));
|
| 399 | if (p->buf == 0)
|
| 400 | return SZ_ERROR_MEM;
|
| 401 | }
|
| 402 | |
| 403 | if (p->numCoders != 1)
|
| 404 | finishMode = CODER_FINISH_ANY;
|
| 405 | |
| 406 | for (;;)
|
| 407 | {
|
| 408 | Bool processed = False;
|
| 409 | int i;
|
| 410 | /*
|
| 411 | if (p->numCoders == 1 && *destLen == destLenOrig && finishMode == LZMA_FINISH_ANY)
|
| 412 | break;
|
| 413 | */
|
| 414 | |
| 415 | for (i = 0; i < p->numCoders; i++)
|
| 416 | {
|
| 417 | SRes res;
|
| 418 | IStateCoder *coder = &p->coders[i];
|
| 419 | Byte *destCur;
|
| 420 | SizeT destLenCur, srcLenCur;
|
| 421 | const Byte *srcCur;
|
| 422 | int srcFinishedCur;
|
| 423 | int encodingWasFinished;
|
| 424 | |
| 425 | if (i == 0)
|
| 426 | {
|
| 427 | srcCur = src;
|
| 428 | srcLenCur = srcLenOrig - *srcLen;
|
| 429 | srcFinishedCur = srcWasFinished;
|
| 430 | }
|
| 431 | else
|
| 432 | {
|
| 433 | srcCur = p->buf + (CODER_BUF_SIZE * (i - 1)) + p->pos[i - 1];
|
| 434 | srcLenCur = p->size[i - 1] - p->pos[i - 1];
|
| 435 | srcFinishedCur = p->finished[i - 1];
|
| 436 | }
|
| 437 | |
| 438 | if (i == p->numCoders - 1)
|
| 439 | {
|
| 440 | destCur = dest;
|
| 441 | destLenCur = destLenOrig - *destLen;
|
| 442 | }
|
| 443 | else
|
| 444 | {
|
| 445 | if (p->pos[i] != p->size[i])
|
| 446 | continue;
|
| 447 | destCur = p->buf + (CODER_BUF_SIZE * i);
|
| 448 | destLenCur = CODER_BUF_SIZE;
|
| 449 | }
|
| 450 | |
| 451 | res = coder->Code(coder->p, destCur, &destLenCur, srcCur, &srcLenCur, srcFinishedCur, finishMode, &encodingWasFinished);
|
| 452 | |
| 453 | if (!encodingWasFinished)
|
| 454 | allFinished = False;
|
| 455 | |
| 456 | if (i == 0)
|
| 457 | {
|
| 458 | *srcLen += srcLenCur;
|
| 459 | src += srcLenCur;
|
| 460 | }
|
| 461 | else
|
| 462 | {
|
| 463 | p->pos[i - 1] += srcLenCur;
|
| 464 | }
|
| 465 | |
| 466 | if (i == p->numCoders - 1)
|
| 467 | {
|
| 468 | *destLen += destLenCur;
|
| 469 | dest += destLenCur;
|
| 470 | }
|
| 471 | else
|
| 472 | {
|
| 473 | p->size[i] = destLenCur;
|
| 474 | p->pos[i] = 0;
|
| 475 | p->finished[i] = encodingWasFinished;
|
| 476 | }
|
| 477 | |
| 478 | if (res != SZ_OK)
|
| 479 | return res;
|
| 480 | |
| 481 | if (destLenCur != 0 || srcLenCur != 0)
|
| 482 | processed = True;
|
| 483 | }
|
| 484 | if (!processed)
|
| 485 | break;
|
| 486 | }
|
| 487 | if (allFinished)
|
| 488 | *status = CODER_STATUS_FINISHED_WITH_MARK;
|
| 489 | return SZ_OK;
|
| 490 | }
|
| 491 | |
| 492 | SRes Xz_ParseHeader(CXzStreamFlags *p, const Byte *buf)
|
| 493 | {
|
| 494 | *p = (CXzStreamFlags)GetBe16(buf + XZ_SIG_SIZE);
|
| 495 | if (CrcCalc(buf + XZ_SIG_SIZE, XZ_STREAM_FLAGS_SIZE) !=
|
| 496 | GetUi32(buf + XZ_SIG_SIZE + XZ_STREAM_FLAGS_SIZE))
|
| 497 | return SZ_ERROR_NO_ARCHIVE;
|
| 498 | return XzFlags_IsSupported(*p) ? SZ_OK : SZ_ERROR_UNSUPPORTED;
|
| 499 | }
|
| 500 | |
| 501 | static Bool Xz_CheckFooter(CXzStreamFlags flags, UInt64 indexSize, const Byte *buf)
|
| 502 | {
|
| 503 | return
|
| 504 | indexSize == (((UInt64)GetUi32(buf + 4) + 1) << 2) &&
|
| 505 | (GetUi32(buf) == CrcCalc(buf + 4, 6) &&
|
| 506 | flags == GetBe16(buf + 8) &&
|
| 507 | memcmp(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE) == 0);
|
| 508 | }
|
| 509 | |
| 510 | #define READ_VARINT_AND_CHECK(buf, pos, size, res) \
|
| 511 | { unsigned s = Xz_ReadVarInt(buf + pos, size - pos, res); \
|
| 512 | if (s == 0) return SZ_ERROR_ARCHIVE; pos += s; }
|
| 513 | |
| 514 | |
| 515 | SRes XzBlock_Parse(CXzBlock *p, const Byte *header)
|
| 516 | {
|
| 517 | unsigned pos;
|
| 518 | int numFilters, i;
|
| 519 | UInt32 headerSize = (UInt32)header[0] << 2;
|
| 520 | |
| 521 | if (CrcCalc(header, headerSize) != GetUi32(header + headerSize))
|
| 522 | return SZ_ERROR_ARCHIVE;
|
| 523 | |
| 524 | pos = 1;
|
| 525 | if (pos == headerSize)
|
| 526 | return SZ_ERROR_ARCHIVE;
|
| 527 | p->flags = header[pos++];
|
| 528 | |
| 529 | if (XzBlock_HasPackSize(p))
|
| 530 | {
|
| 531 | READ_VARINT_AND_CHECK(header, pos, headerSize, &p->packSize);
|
| 532 | if (p->packSize == 0 || p->packSize + headerSize >= (UInt64)1 << 63)
|
| 533 | return SZ_ERROR_ARCHIVE;
|
| 534 | }
|
| 535 | |
| 536 | if (XzBlock_HasUnpackSize(p))
|
| 537 | READ_VARINT_AND_CHECK(header, pos, headerSize, &p->unpackSize);
|
| 538 | |
| 539 | numFilters = XzBlock_GetNumFilters(p);
|
| 540 | for (i = 0; i < numFilters; i++)
|
| 541 | {
|
| 542 | CXzFilter *filter = p->filters + i;
|
| 543 | UInt64 size;
|
| 544 | READ_VARINT_AND_CHECK(header, pos, headerSize, &filter->id);
|
| 545 | READ_VARINT_AND_CHECK(header, pos, headerSize, &size);
|
| 546 | if (size > headerSize - pos || size > XZ_FILTER_PROPS_SIZE_MAX)
|
| 547 | return SZ_ERROR_ARCHIVE;
|
| 548 | filter->propsSize = (UInt32)size;
|
| 549 | memcpy(filter->props, header + pos, (size_t)size);
|
| 550 | pos += (unsigned)size;
|
| 551 | |
| 552 | #ifdef XZ_DUMP
|
| 553 | printf("\nf[%d] = %2X: ", i, filter->id);
|
| 554 | {
|
| 555 | int i;
|
| 556 | for (i = 0; i < size; i++)
|
| 557 | printf(" %2X", filter->props[i]);
|
| 558 | }
|
| 559 | #endif
|
| 560 | }
|
| 561 | |
| 562 | while (pos < headerSize)
|
| 563 | if (header[pos++] != 0)
|
| 564 | return SZ_ERROR_ARCHIVE;
|
| 565 | return SZ_OK;
|
| 566 | }
|
| 567 | |
| 568 | SRes XzDec_Init(CMixCoder *p, const CXzBlock *block)
|
| 569 | {
|
| 570 | int i;
|
| 571 | Bool needReInit = True;
|
| 572 | int numFilters = XzBlock_GetNumFilters(block);
|
| 573 | if (numFilters == p->numCoders)
|
| 574 | {
|
| 575 | for (i = 0; i < numFilters; i++)
|
| 576 | if (p->ids[i] != block->filters[numFilters - 1 - i].id)
|
| 577 | break;
|
| 578 | needReInit = (i != numFilters);
|
| 579 | }
|
| 580 | if (needReInit)
|
| 581 | {
|
| 582 | MixCoder_Free(p);
|
| 583 | p->numCoders = numFilters;
|
| 584 | for (i = 0; i < numFilters; i++)
|
| 585 | {
|
| 586 | const CXzFilter *f = &block->filters[numFilters - 1 - i];
|
| 587 | RINOK(MixCoder_SetFromMethod(p, i, f->id));
|
| 588 | }
|
| 589 | }
|
| 590 | for (i = 0; i < numFilters; i++)
|
| 591 | {
|
| 592 | const CXzFilter *f = &block->filters[numFilters - 1 - i];
|
| 593 | IStateCoder *sc = &p->coders[i];
|
| 594 | RINOK(sc->SetProps(sc->p, f->props, f->propsSize, p->alloc));
|
| 595 | }
|
| 596 | MixCoder_Init(p);
|
| 597 | return SZ_OK;
|
| 598 | }
|
| 599 | |
| 600 | void XzUnpacker_Init(CXzUnpacker *p)
|
| 601 | {
|
| 602 | p->state = XZ_STATE_STREAM_HEADER;
|
| 603 | p->pos = 0;
|
| 604 | p->numStreams = 0;
|
| 605 | }
|
| 606 | |
| 607 | void XzUnpacker_Construct(CXzUnpacker *p, ISzAlloc *alloc)
|
| 608 | {
|
| 609 | MixCoder_Construct(&p->decoder, alloc);
|
| 610 | XzUnpacker_Init(p);
|
| 611 | }
|
| 612 | |
| 613 | void XzUnpacker_Free(CXzUnpacker *p)
|
| 614 | {
|
| 615 | MixCoder_Free(&p->decoder);
|
| 616 | }
|
| 617 | |
| 618 | SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
|
| 619 | const Byte *src, SizeT *srcLen, int finishMode, ECoderStatus *status)
|
| 620 | {
|
| 621 | SizeT destLenOrig = *destLen;
|
| 622 | SizeT srcLenOrig = *srcLen;
|
| 623 | *destLen = 0;
|
| 624 | *srcLen = 0;
|
| 625 | *status = CODER_STATUS_NOT_SPECIFIED;
|
| 626 | for (;;)
|
| 627 | {
|
| 628 | SizeT srcRem = srcLenOrig - *srcLen;
|
| 629 | |
| 630 | if (p->state == XZ_STATE_BLOCK)
|
| 631 | {
|
| 632 | SizeT destLen2 = destLenOrig - *destLen;
|
| 633 | SizeT srcLen2 = srcLenOrig - *srcLen;
|
| 634 | SRes res;
|
| 635 | if (srcLen2 == 0 && destLen2 == 0)
|
| 636 | {
|
| 637 | *status = CODER_STATUS_NOT_FINISHED;
|
| 638 | return SZ_OK;
|
| 639 | }
|
| 640 | |
| 641 | res = MixCoder_Code(&p->decoder, dest, &destLen2, src, &srcLen2, False, finishMode, status);
|
| 642 | XzCheck_Update(&p->check, dest, destLen2);
|
| 643 | |
| 644 | (*srcLen) += srcLen2;
|
| 645 | src += srcLen2;
|
| 646 | p->packSize += srcLen2;
|
| 647 | |
| 648 | (*destLen) += destLen2;
|
| 649 | dest += destLen2;
|
| 650 | p->unpackSize += destLen2;
|
| 651 | |
| 652 | RINOK(res);
|
| 653 | |
| 654 | if (*status == CODER_STATUS_FINISHED_WITH_MARK)
|
| 655 | {
|
| 656 | Byte temp[32];
|
| 657 | unsigned num = Xz_WriteVarInt(temp, p->packSize + p->blockHeaderSize + XzFlags_GetCheckSize(p->streamFlags));
|
| 658 | num += Xz_WriteVarInt(temp + num, p->unpackSize);
|
| 659 | Sha256_Update(&p->sha, temp, num);
|
| 660 | p->indexSize += num;
|
| 661 | p->numBlocks++;
|
| 662 | |
| 663 | p->state = XZ_STATE_BLOCK_FOOTER;
|
| 664 | p->pos = 0;
|
| 665 | p->alignPos = 0;
|
| 666 | }
|
| 667 | else if (srcLen2 == 0 && destLen2 == 0)
|
| 668 | return SZ_OK;
|
| 669 | |
| 670 | continue;
|
| 671 | }
|
| 672 | |
| 673 | if (srcRem == 0)
|
| 674 | {
|
| 675 | *status = CODER_STATUS_NEEDS_MORE_INPUT;
|
| 676 | return SZ_OK;
|
| 677 | }
|
| 678 | |
| 679 | switch(p->state)
|
| 680 | {
|
| 681 | case XZ_STATE_STREAM_HEADER:
|
| 682 | {
|
| 683 | if (p->pos < XZ_STREAM_HEADER_SIZE)
|
| 684 | {
|
| 685 | if (p->pos < XZ_SIG_SIZE && *src != XZ_SIG[p->pos])
|
| 686 | return SZ_ERROR_NO_ARCHIVE;
|
| 687 | p->buf[p->pos++] = *src++;
|
| 688 | (*srcLen)++;
|
| 689 | }
|
| 690 | else
|
| 691 | {
|
| 692 | RINOK(Xz_ParseHeader(&p->streamFlags, p->buf));
|
| 693 | p->state = XZ_STATE_BLOCK_HEADER;
|
| 694 | Sha256_Init(&p->sha);
|
| 695 | p->indexSize = 0;
|
| 696 | p->numBlocks = 0;
|
| 697 | p->pos = 0;
|
| 698 | }
|
| 699 | break;
|
| 700 | }
|
| 701 | |
| 702 | case XZ_STATE_BLOCK_HEADER:
|
| 703 | {
|
| 704 | if (p->pos == 0)
|
| 705 | {
|
| 706 | p->buf[p->pos++] = *src++;
|
| 707 | (*srcLen)++;
|
| 708 | if (p->buf[0] == 0)
|
| 709 | {
|
| 710 | p->indexPreSize = 1 + Xz_WriteVarInt(p->buf + 1, p->numBlocks);
|
| 711 | p->indexPos = p->indexPreSize;
|
| 712 | p->indexSize += p->indexPreSize;
|
| 713 | Sha256_Final(&p->sha, p->shaDigest);
|
| 714 | Sha256_Init(&p->sha);
|
| 715 | p->crc = CrcUpdate(CRC_INIT_VAL, p->buf, p->indexPreSize);
|
| 716 | p->state = XZ_STATE_STREAM_INDEX;
|
| 717 | }
|
| 718 | p->blockHeaderSize = ((UInt32)p->buf[0] << 2) + 4;
|
| 719 | }
|
| 720 | else if (p->pos != p->blockHeaderSize)
|
| 721 | {
|
| 722 | UInt32 cur = p->blockHeaderSize - p->pos;
|
| 723 | if (cur > srcRem)
|
| 724 | cur = (UInt32)srcRem;
|
| 725 | memcpy(p->buf + p->pos, src, cur);
|
| 726 | p->pos += cur;
|
| 727 | (*srcLen) += cur;
|
| 728 | src += cur;
|
| 729 | }
|
| 730 | else
|
| 731 | {
|
| 732 | RINOK(XzBlock_Parse(&p->block, p->buf));
|
| 733 | p->state = XZ_STATE_BLOCK;
|
| 734 | p->packSize = 0;
|
| 735 | p->unpackSize = 0;
|
| 736 | XzCheck_Init(&p->check, XzFlags_GetCheckType(p->streamFlags));
|
| 737 | RINOK(XzDec_Init(&p->decoder, &p->block));
|
| 738 | }
|
| 739 | break;
|
| 740 | }
|
| 741 | |
| 742 | case XZ_STATE_BLOCK_FOOTER:
|
| 743 | {
|
| 744 | if (((p->packSize + p->alignPos) & 3) != 0)
|
| 745 | {
|
| 746 | (*srcLen)++;
|
| 747 | p->alignPos++;
|
| 748 | if (*src++ != 0)
|
| 749 | return SZ_ERROR_CRC;
|
| 750 | }
|
| 751 | else
|
| 752 | {
|
| 753 | UInt32 checkSize = XzFlags_GetCheckSize(p->streamFlags);
|
| 754 | UInt32 cur = checkSize - p->pos;
|
| 755 | if (cur != 0)
|
| 756 | {
|
| 757 | if (cur > srcRem)
|
| 758 | cur = (UInt32)srcRem;
|
| 759 | memcpy(p->buf + p->pos, src, cur);
|
| 760 | p->pos += cur;
|
| 761 | (*srcLen) += cur;
|
| 762 | src += cur;
|
| 763 | }
|
| 764 | else
|
| 765 | {
|
| 766 | Byte digest[XZ_CHECK_SIZE_MAX];
|
| 767 | p->state = XZ_STATE_BLOCK_HEADER;
|
| 768 | p->pos = 0;
|
| 769 | if (XzCheck_Final(&p->check, digest) && memcmp(digest, p->buf, checkSize) != 0)
|
| 770 | return SZ_ERROR_CRC;
|
| 771 | }
|
| 772 | }
|
| 773 | break;
|
| 774 | }
|
| 775 | |
| 776 | case XZ_STATE_STREAM_INDEX:
|
| 777 | {
|
| 778 | if (p->pos < p->indexPreSize)
|
| 779 | {
|
| 780 | (*srcLen)++;
|
| 781 | if (*src++ != p->buf[p->pos++])
|
| 782 | return SZ_ERROR_CRC;
|
| 783 | }
|
| 784 | else
|
| 785 | {
|
| 786 | if (p->indexPos < p->indexSize)
|
| 787 | {
|
| 788 | UInt64 cur = p->indexSize - p->indexPos;
|
| 789 | if (srcRem > cur)
|
| 790 | srcRem = (SizeT)cur;
|
| 791 | p->crc = CrcUpdate(p->crc, src, srcRem);
|
| 792 | Sha256_Update(&p->sha, src, srcRem);
|
| 793 | (*srcLen) += srcRem;
|
| 794 | src += srcRem;
|
| 795 | p->indexPos += srcRem;
|
| 796 | }
|
| 797 | else if ((p->indexPos & 3) != 0)
|
| 798 | {
|
| 799 | Byte b = *src++;
|
| 800 | p->crc = CRC_UPDATE_BYTE(p->crc, b);
|
| 801 | (*srcLen)++;
|
| 802 | p->indexPos++;
|
| 803 | p->indexSize++;
|
| 804 | if (b != 0)
|
| 805 | return SZ_ERROR_CRC;
|
| 806 | }
|
| 807 | else
|
| 808 | {
|
| 809 | Byte digest[SHA256_DIGEST_SIZE];
|
| 810 | p->state = XZ_STATE_STREAM_INDEX_CRC;
|
| 811 | p->indexSize += 4;
|
| 812 | p->pos = 0;
|
| 813 | Sha256_Final(&p->sha, digest);
|
| 814 | if (memcmp(digest, p->shaDigest, SHA256_DIGEST_SIZE) != 0)
|
| 815 | return SZ_ERROR_CRC;
|
| 816 | }
|
| 817 | }
|
| 818 | break;
|
| 819 | }
|
| 820 | |
| 821 | case XZ_STATE_STREAM_INDEX_CRC:
|
| 822 | {
|
| 823 | if (p->pos < 4)
|
| 824 | {
|
| 825 | (*srcLen)++;
|
| 826 | p->buf[p->pos++] = *src++;
|
| 827 | }
|
| 828 | else
|
| 829 | {
|
| 830 | p->state = XZ_STATE_STREAM_FOOTER;
|
| 831 | p->pos = 0;
|
| 832 | if (CRC_GET_DIGEST(p->crc) != GetUi32(p->buf))
|
| 833 | return SZ_ERROR_CRC;
|
| 834 | }
|
| 835 | break;
|
| 836 | }
|
| 837 | |
| 838 | case XZ_STATE_STREAM_FOOTER:
|
| 839 | {
|
| 840 | UInt32 cur = XZ_STREAM_FOOTER_SIZE - p->pos;
|
| 841 | if (cur > srcRem)
|
| 842 | cur = (UInt32)srcRem;
|
| 843 | memcpy(p->buf + p->pos, src, cur);
|
| 844 | p->pos += cur;
|
| 845 | (*srcLen) += cur;
|
| 846 | src += cur;
|
| 847 | if (p->pos == XZ_STREAM_FOOTER_SIZE)
|
| 848 | {
|
| 849 | p->state = XZ_STATE_STREAM_PADDING;
|
| 850 | p->numStreams++;
|
| 851 | p->padSize = 0;
|
| 852 | if (!Xz_CheckFooter(p->streamFlags, p->indexSize, p->buf))
|
| 853 | return SZ_ERROR_CRC;
|
| 854 | }
|
| 855 | break;
|
| 856 | }
|
| 857 | |
| 858 | case XZ_STATE_STREAM_PADDING:
|
| 859 | {
|
| 860 | if (*src != 0)
|
| 861 | {
|
| 862 | if (((UInt32)p->padSize & 3) != 0)
|
| 863 | return SZ_ERROR_NO_ARCHIVE;
|
| 864 | p->pos = 0;
|
| 865 | p->state = XZ_STATE_STREAM_HEADER;
|
| 866 | }
|
| 867 | else
|
| 868 | {
|
| 869 | (*srcLen)++;
|
| 870 | src++;
|
| 871 | p->padSize++;
|
| 872 | }
|
| 873 | break;
|
| 874 | }
|
| 875 | |
| 876 | case XZ_STATE_BLOCK: break; /* to disable GCC warning */
|
| 877 | }
|
| 878 | }
|
| 879 | /*
|
| 880 | if (p->state == XZ_STATE_FINISHED)
|
| 881 | *status = CODER_STATUS_FINISHED_WITH_MARK;
|
| 882 | return SZ_OK;
|
| 883 | */
|
| 884 | }
|
| 885 | |
| 886 | Bool XzUnpacker_IsStreamWasFinished(CXzUnpacker *p)
|
| 887 | {
|
| 888 | return (p->state == XZ_STATE_STREAM_PADDING) && (((UInt32)p->padSize & 3) == 0);
|
| 889 | }
|
src/lib/lib7z/LzFind.c
| r0 | r14440 | |
| 1 | /* LzFind.c -- Match finder for LZ algorithms
|
| 2 | 2009-04-22 : Igor Pavlov : Public domain */
|
| 3 | |
| 4 | #include <string.h>
|
| 5 | |
| 6 | #include "LzFind.h"
|
| 7 | #include "LzHash.h"
|
| 8 | |
| 9 | #define kEmptyHashValue 0
|
| 10 | #define kMaxValForNormalize ((UInt32)0xFFFFFFFF)
|
| 11 | #define kNormalizeStepMin (1 << 10) /* it must be power of 2 */
|
| 12 | #define kNormalizeMask (~(kNormalizeStepMin - 1))
|
| 13 | #define kMaxHistorySize ((UInt32)3 << 30)
|
| 14 | |
| 15 | #define kStartMaxLen 3
|
| 16 | |
| 17 | static void LzInWindow_Free(CMatchFinder *p, ISzAlloc *alloc)
|
| 18 | {
|
| 19 | if (!p->directInput)
|
| 20 | {
|
| 21 | alloc->Free(alloc, p->bufferBase);
|
| 22 | p->bufferBase = 0;
|
| 23 | }
|
| 24 | }
|
| 25 | |
| 26 | /* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */
|
| 27 | |
| 28 | static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *alloc)
|
| 29 | {
|
| 30 | UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;
|
| 31 | if (p->directInput)
|
| 32 | {
|
| 33 | p->blockSize = blockSize;
|
| 34 | return 1;
|
| 35 | }
|
| 36 | if (p->bufferBase == 0 || p->blockSize != blockSize)
|
| 37 | {
|
| 38 | LzInWindow_Free(p, alloc);
|
| 39 | p->blockSize = blockSize;
|
| 40 | p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize);
|
| 41 | }
|
| 42 | return (p->bufferBase != 0);
|
| 43 | }
|
| 44 | |
| 45 | Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
|
| 46 | Byte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; }
|
| 47 | |
| 48 | UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }
|
| 49 | |
| 50 | void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)
|
| 51 | {
|
| 52 | p->posLimit -= subValue;
|
| 53 | p->pos -= subValue;
|
| 54 | p->streamPos -= subValue;
|
| 55 | }
|
| 56 | |
| 57 | static void MatchFinder_ReadBlock(CMatchFinder *p)
|
| 58 | {
|
| 59 | if (p->streamEndWasReached || p->result != SZ_OK)
|
| 60 | return;
|
| 61 | if (p->directInput)
|
| 62 | {
|
| 63 | UInt32 curSize = 0xFFFFFFFF - p->streamPos;
|
| 64 | if (curSize > p->directInputRem)
|
| 65 | curSize = (UInt32)p->directInputRem;
|
| 66 | p->directInputRem -= curSize;
|
| 67 | p->streamPos += curSize;
|
| 68 | if (p->directInputRem == 0)
|
| 69 | p->streamEndWasReached = 1;
|
| 70 | return;
|
| 71 | }
|
| 72 | for (;;)
|
| 73 | {
|
| 74 | Byte *dest = p->buffer + (p->streamPos - p->pos);
|
| 75 | size_t size = (p->bufferBase + p->blockSize - dest);
|
| 76 | if (size == 0)
|
| 77 | return;
|
| 78 | p->result = p->stream->Read(p->stream, dest, &size);
|
| 79 | if (p->result != SZ_OK)
|
| 80 | return;
|
| 81 | if (size == 0)
|
| 82 | {
|
| 83 | p->streamEndWasReached = 1;
|
| 84 | return;
|
| 85 | }
|
| 86 | p->streamPos += (UInt32)size;
|
| 87 | if (p->streamPos - p->pos > p->keepSizeAfter)
|
| 88 | return;
|
| 89 | }
|
| 90 | }
|
| 91 | |
| 92 | void MatchFinder_MoveBlock(CMatchFinder *p)
|
| 93 | {
|
| 94 | memmove(p->bufferBase,
|
| 95 | p->buffer - p->keepSizeBefore,
|
| 96 | (size_t)(p->streamPos - p->pos + p->keepSizeBefore));
|
| 97 | p->buffer = p->bufferBase + p->keepSizeBefore;
|
| 98 | }
|
| 99 | |
| 100 | int MatchFinder_NeedMove(CMatchFinder *p)
|
| 101 | {
|
| 102 | if (p->directInput)
|
| 103 | return 0;
|
| 104 | /* if (p->streamEndWasReached) return 0; */
|
| 105 | return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);
|
| 106 | }
|
| 107 | |
| 108 | void MatchFinder_ReadIfRequired(CMatchFinder *p)
|
| 109 | {
|
| 110 | if (p->streamEndWasReached)
|
| 111 | return;
|
| 112 | if (p->keepSizeAfter >= p->streamPos - p->pos)
|
| 113 | MatchFinder_ReadBlock(p);
|
| 114 | }
|
| 115 | |
| 116 | static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)
|
| 117 | {
|
| 118 | if (MatchFinder_NeedMove(p))
|
| 119 | MatchFinder_MoveBlock(p);
|
| 120 | MatchFinder_ReadBlock(p);
|
| 121 | }
|
| 122 | |
| 123 | static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
|
| 124 | {
|
| 125 | p->cutValue = 32;
|
| 126 | p->btMode = 1;
|
| 127 | p->numHashBytes = 4;
|
| 128 | p->bigHash = 0;
|
| 129 | }
|
| 130 | |
| 131 | #define kCrcPoly 0xEDB88320
|
| 132 | |
| 133 | void MatchFinder_Construct(CMatchFinder *p)
|
| 134 | {
|
| 135 | UInt32 i;
|
| 136 | p->bufferBase = 0;
|
| 137 | p->directInput = 0;
|
| 138 | p->hash = 0;
|
| 139 | MatchFinder_SetDefaultSettings(p);
|
| 140 | |
| 141 | for (i = 0; i < 256; i++)
|
| 142 | {
|
| 143 | UInt32 r = i;
|
| 144 | int j;
|
| 145 | for (j = 0; j < 8; j++)
|
| 146 | r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
|
| 147 | p->crc[i] = r;
|
| 148 | }
|
| 149 | }
|
| 150 | |
| 151 | static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc)
|
| 152 | {
|
| 153 | alloc->Free(alloc, p->hash);
|
| 154 | p->hash = 0;
|
| 155 | }
|
| 156 | |
| 157 | void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc)
|
| 158 | {
|
| 159 | MatchFinder_FreeThisClassMemory(p, alloc);
|
| 160 | LzInWindow_Free(p, alloc);
|
| 161 | }
|
| 162 | |
| 163 | static CLzRef* AllocRefs(UInt32 num, ISzAlloc *alloc)
|
| 164 | {
|
| 165 | size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
|
| 166 | if (sizeInBytes / sizeof(CLzRef) != num)
|
| 167 | return 0;
|
| 168 | return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);
|
| 169 | }
|
| 170 | |
| 171 | int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
|
| 172 | UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
|
| 173 | ISzAlloc *alloc)
|
| 174 | {
|
| 175 | UInt32 sizeReserv;
|
| 176 | if (historySize > kMaxHistorySize)
|
| 177 | {
|
| 178 | MatchFinder_Free(p, alloc);
|
| 179 | return 0;
|
| 180 | }
|
| 181 | sizeReserv = historySize >> 1;
|
| 182 | if (historySize > ((UInt32)2 << 30))
|
| 183 | sizeReserv = historySize >> 2;
|
| 184 | sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);
|
| 185 | |
| 186 | p->keepSizeBefore = historySize + keepAddBufferBefore + 1;
|
| 187 | p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;
|
| 188 | /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */
|
| 189 | if (LzInWindow_Create(p, sizeReserv, alloc))
|
| 190 | {
|
| 191 | UInt32 newCyclicBufferSize = historySize + 1;
|
| 192 | UInt32 hs;
|
| 193 | p->matchMaxLen = matchMaxLen;
|
| 194 | {
|
| 195 | p->fixedHashSize = 0;
|
| 196 | if (p->numHashBytes == 2)
|
| 197 | hs = (1 << 16) - 1;
|
| 198 | else
|
| 199 | {
|
| 200 | hs = historySize - 1;
|
| 201 | hs |= (hs >> 1);
|
| 202 | hs |= (hs >> 2);
|
| 203 | hs |= (hs >> 4);
|
| 204 | hs |= (hs >> 8);
|
| 205 | hs >>= 1;
|
| 206 | hs |= 0xFFFF; /* don't change it! It's required for Deflate */
|
| 207 | if (hs > (1 << 24))
|
| 208 | {
|
| 209 | if (p->numHashBytes == 3)
|
| 210 | hs = (1 << 24) - 1;
|
| 211 | else
|
| 212 | hs >>= 1;
|
| 213 | }
|
| 214 | }
|
| 215 | p->hashMask = hs;
|
| 216 | hs++;
|
| 217 | if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;
|
| 218 | if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;
|
| 219 | if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;
|
| 220 | hs += p->fixedHashSize;
|
| 221 | }
|
| 222 | |
| 223 | {
|
| 224 | UInt32 prevSize = p->hashSizeSum + p->numSons;
|
| 225 | UInt32 newSize;
|
| 226 | p->historySize = historySize;
|
| 227 | p->hashSizeSum = hs;
|
| 228 | p->cyclicBufferSize = newCyclicBufferSize;
|
| 229 | p->numSons = (p->btMode ? newCyclicBufferSize * 2 : newCyclicBufferSize);
|
| 230 | newSize = p->hashSizeSum + p->numSons;
|
| 231 | if (p->hash != 0 && prevSize == newSize)
|
| 232 | return 1;
|
| 233 | MatchFinder_FreeThisClassMemory(p, alloc);
|
| 234 | p->hash = AllocRefs(newSize, alloc);
|
| 235 | if (p->hash != 0)
|
| 236 | {
|
| 237 | p->son = p->hash + p->hashSizeSum;
|
| 238 | return 1;
|
| 239 | }
|
| 240 | }
|
| 241 | }
|
| 242 | MatchFinder_Free(p, alloc);
|
| 243 | return 0;
|
| 244 | }
|
| 245 | |
| 246 | static void MatchFinder_SetLimits(CMatchFinder *p)
|
| 247 | {
|
| 248 | UInt32 limit = kMaxValForNormalize - p->pos;
|
| 249 | UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;
|
| 250 | if (limit2 < limit)
|
| 251 | limit = limit2;
|
| 252 | limit2 = p->streamPos - p->pos;
|
| 253 | if (limit2 <= p->keepSizeAfter)
|
| 254 | {
|
| 255 | if (limit2 > 0)
|
| 256 | limit2 = 1;
|
| 257 | }
|
| 258 | else
|
| 259 | limit2 -= p->keepSizeAfter;
|
| 260 | if (limit2 < limit)
|
| 261 | limit = limit2;
|
| 262 | {
|
| 263 | UInt32 lenLimit = p->streamPos - p->pos;
|
| 264 | if (lenLimit > p->matchMaxLen)
|
| 265 | lenLimit = p->matchMaxLen;
|
| 266 | p->lenLimit = lenLimit;
|
| 267 | }
|
| 268 | p->posLimit = p->pos + limit;
|
| 269 | }
|
| 270 | |
| 271 | void MatchFinder_Init(CMatchFinder *p)
|
| 272 | {
|
| 273 | UInt32 i;
|
| 274 | for (i = 0; i < p->hashSizeSum; i++)
|
| 275 | p->hash[i] = kEmptyHashValue;
|
| 276 | p->cyclicBufferPos = 0;
|
| 277 | p->buffer = p->bufferBase;
|
| 278 | p->pos = p->streamPos = p->cyclicBufferSize;
|
| 279 | p->result = SZ_OK;
|
| 280 | p->streamEndWasReached = 0;
|
| 281 | MatchFinder_ReadBlock(p);
|
| 282 | MatchFinder_SetLimits(p);
|
| 283 | }
|
| 284 | |
| 285 | static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)
|
| 286 | {
|
| 287 | return (p->pos - p->historySize - 1) & kNormalizeMask;
|
| 288 | }
|
| 289 | |
| 290 | void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems)
|
| 291 | {
|
| 292 | UInt32 i;
|
| 293 | for (i = 0; i < numItems; i++)
|
| 294 | {
|
| 295 | UInt32 value = items[i];
|
| 296 | if (value <= subValue)
|
| 297 | value = kEmptyHashValue;
|
| 298 | else
|
| 299 | value -= subValue;
|
| 300 | items[i] = value;
|
| 301 | }
|
| 302 | }
|
| 303 | |
| 304 | static void MatchFinder_Normalize(CMatchFinder *p)
|
| 305 | {
|
| 306 | UInt32 subValue = MatchFinder_GetSubValue(p);
|
| 307 | MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons);
|
| 308 | MatchFinder_ReduceOffsets(p, subValue);
|
| 309 | }
|
| 310 | |
| 311 | static void MatchFinder_CheckLimits(CMatchFinder *p)
|
| 312 | {
|
| 313 | if (p->pos == kMaxValForNormalize)
|
| 314 | MatchFinder_Normalize(p);
|
| 315 | if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)
|
| 316 | MatchFinder_CheckAndMoveAndRead(p);
|
| 317 | if (p->cyclicBufferPos == p->cyclicBufferSize)
|
| 318 | p->cyclicBufferPos = 0;
|
| 319 | MatchFinder_SetLimits(p);
|
| 320 | }
|
| 321 | |
| 322 | static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
|
| 323 | UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
|
| 324 | UInt32 *distances, UInt32 maxLen)
|
| 325 | {
|
| 326 | son[_cyclicBufferPos] = curMatch;
|
| 327 | for (;;)
|
| 328 | {
|
| 329 | UInt32 delta = pos - curMatch;
|
| 330 | if (cutValue-- == 0 || delta >= _cyclicBufferSize)
|
| 331 | return distances;
|
| 332 | {
|
| 333 | const Byte *pb = cur - delta;
|
| 334 | curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];
|
| 335 | if (pb[maxLen] == cur[maxLen] && *pb == *cur)
|
| 336 | {
|
| 337 | UInt32 len = 0;
|
| 338 | while (++len != lenLimit)
|
| 339 | if (pb[len] != cur[len])
|
| 340 | break;
|
| 341 | if (maxLen < len)
|
| 342 | {
|
| 343 | *distances++ = maxLen = len;
|
| 344 | *distances++ = delta - 1;
|
| 345 | if (len == lenLimit)
|
| 346 | return distances;
|
| 347 | }
|
| 348 | }
|
| 349 | }
|
| 350 | }
|
| 351 | }
|
| 352 | |
| 353 | UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
|
| 354 | UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
|
| 355 | UInt32 *distances, UInt32 maxLen)
|
| 356 | {
|
| 357 | CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
|
| 358 | CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
|
| 359 | UInt32 len0 = 0, len1 = 0;
|
| 360 | for (;;)
|
| 361 | {
|
| 362 | UInt32 delta = pos - curMatch;
|
| 363 | if (cutValue-- == 0 || delta >= _cyclicBufferSize)
|
| 364 | {
|
| 365 | *ptr0 = *ptr1 = kEmptyHashValue;
|
| 366 | return distances;
|
| 367 | }
|
| 368 | {
|
| 369 | CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
|
| 370 | const Byte *pb = cur - delta;
|
| 371 | UInt32 len = (len0 < len1 ? len0 : len1);
|
| 372 | if (pb[len] == cur[len])
|
| 373 | {
|
| 374 | if (++len != lenLimit && pb[len] == cur[len])
|
| 375 | while (++len != lenLimit)
|
| 376 | if (pb[len] != cur[len])
|
| 377 | break;
|
| 378 | if (maxLen < len)
|
| 379 | {
|
| 380 | *distances++ = maxLen = len;
|
| 381 | *distances++ = delta - 1;
|
| 382 | if (len == lenLimit)
|
| 383 | {
|
| 384 | *ptr1 = pair[0];
|
| 385 | *ptr0 = pair[1];
|
| 386 | return distances;
|
| 387 | }
|
| 388 | }
|
| 389 | }
|
| 390 | if (pb[len] < cur[len])
|
| 391 | {
|
| 392 | *ptr1 = curMatch;
|
| 393 | ptr1 = pair + 1;
|
| 394 | curMatch = *ptr1;
|
| 395 | len1 = len;
|
| 396 | }
|
| 397 | else
|
| 398 | {
|
| 399 | *ptr0 = curMatch;
|
| 400 | ptr0 = pair;
|
| 401 | curMatch = *ptr0;
|
| 402 | len0 = len;
|
| 403 | }
|
| 404 | }
|
| 405 | }
|
| 406 | }
|
| 407 | |
| 408 | static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
|
| 409 | UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)
|
| 410 | {
|
| 411 | CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
|
| 412 | CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
|
| 413 | UInt32 len0 = 0, len1 = 0;
|
| 414 | for (;;)
|
| 415 | {
|
| 416 | UInt32 delta = pos - curMatch;
|
| 417 | if (cutValue-- == 0 || delta >= _cyclicBufferSize)
|
| 418 | {
|
| 419 | *ptr0 = *ptr1 = kEmptyHashValue;
|
| 420 | return;
|
| 421 | }
|
| 422 | {
|
| 423 | CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
|
| 424 | const Byte *pb = cur - delta;
|
| 425 | UInt32 len = (len0 < len1 ? len0 : len1);
|
| 426 | if (pb[len] == cur[len])
|
| 427 | {
|
| 428 | while (++len != lenLimit)
|
| 429 | if (pb[len] != cur[len])
|
| 430 | break;
|
| 431 | {
|
| 432 | if (len == lenLimit)
|
| 433 | {
|
| 434 | *ptr1 = pair[0];
|
| 435 | *ptr0 = pair[1];
|
| 436 | return;
|
| 437 | }
|
| 438 | }
|
| 439 | }
|
| 440 | if (pb[len] < cur[len])
|
| 441 | {
|
| 442 | *ptr1 = curMatch;
|
| 443 | ptr1 = pair + 1;
|
| 444 | curMatch = *ptr1;
|
| 445 | len1 = len;
|
| 446 | }
|
| 447 | else
|
| 448 | {
|
| 449 | *ptr0 = curMatch;
|
| 450 | ptr0 = pair;
|
| 451 | curMatch = *ptr0;
|
| 452 | len0 = len;
|
| 453 | }
|
| 454 | }
|
| 455 | }
|
| 456 | }
|
| 457 | |
| 458 | #define MOVE_POS \
|
| 459 | ++p->cyclicBufferPos; \
|
| 460 | p->buffer++; \
|
| 461 | if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);
|
| 462 | |
| 463 | #define MOVE_POS_RET MOVE_POS return offset;
|
| 464 | |
| 465 | static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
|
| 466 | |
| 467 | #define GET_MATCHES_HEADER2(minLen, ret_op) \
|
| 468 | UInt32 lenLimit; UInt32 hashValue; const Byte *cur; UInt32 curMatch; \
|
| 469 | lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \
|
| 470 | cur = p->buffer;
|
| 471 | |
| 472 | #define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)
|
| 473 | #define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)
|
| 474 | |
| 475 | #define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
|
| 476 | |
| 477 | #define GET_MATCHES_FOOTER(offset, maxLen) \
|
| 478 | offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \
|
| 479 | distances + offset, maxLen) - distances); MOVE_POS_RET;
|
| 480 | |
| 481 | #define SKIP_FOOTER \
|
| 482 | SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;
|
| 483 | |
| 484 | static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
|
| 485 | {
|
| 486 | UInt32 offset;
|
| 487 | GET_MATCHES_HEADER(2)
|
| 488 | HASH2_CALC;
|
| 489 | curMatch = p->hash[hashValue];
|
| 490 | p->hash[hashValue] = p->pos;
|
| 491 | offset = 0;
|
| 492 | GET_MATCHES_FOOTER(offset, 1)
|
| 493 | }
|
| 494 | |
| 495 | UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
|
| 496 | {
|
| 497 | UInt32 offset;
|
| 498 | GET_MATCHES_HEADER(3)
|
| 499 | HASH_ZIP_CALC;
|
| 500 | curMatch = p->hash[hashValue];
|
| 501 | p->hash[hashValue] = p->pos;
|
| 502 | offset = 0;
|
| 503 | GET_MATCHES_FOOTER(offset, 2)
|
| 504 | }
|
| 505 | |
| 506 | static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
|
| 507 | {
|
| 508 | UInt32 hash2Value, delta2, maxLen, offset;
|
| 509 | GET_MATCHES_HEADER(3)
|
| 510 | |
| 511 | HASH3_CALC;
|
| 512 | |
| 513 | delta2 = p->pos - p->hash[hash2Value];
|
| 514 | curMatch = p->hash[kFix3HashSize + hashValue];
|
| 515 | |
| 516 | p->hash[hash2Value] =
|
| 517 | p->hash[kFix3HashSize + hashValue] = p->pos;
|
| 518 | |
| 519 | |
| 520 | maxLen = 2;
|
| 521 | offset = 0;
|
| 522 | if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
|
| 523 | {
|
| 524 | for (; maxLen != lenLimit; maxLen++)
|
| 525 | if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
|
| 526 | break;
|
| 527 | distances[0] = maxLen;
|
| 528 | distances[1] = delta2 - 1;
|
| 529 | offset = 2;
|
| 530 | if (maxLen == lenLimit)
|
| 531 | {
|
| 532 | SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
|
| 533 | MOVE_POS_RET;
|
| 534 | }
|
| 535 | }
|
| 536 | GET_MATCHES_FOOTER(offset, maxLen)
|
| 537 | }
|
| 538 | |
| 539 | static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
|
| 540 | {
|
| 541 | UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
|
| 542 | GET_MATCHES_HEADER(4)
|
| 543 | |
| 544 | HASH4_CALC;
|
| 545 | |
| 546 | delta2 = p->pos - p->hash[ hash2Value];
|
| 547 | delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
|
| 548 | curMatch = p->hash[kFix4HashSize + hashValue];
|
| 549 | |
| 550 | p->hash[ hash2Value] =
|
| 551 | p->hash[kFix3HashSize + hash3Value] =
|
| 552 | p->hash[kFix4HashSize + hashValue] = p->pos;
|
| 553 | |
| 554 | maxLen = 1;
|
| 555 | offset = 0;
|
| 556 | if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
|
| 557 | {
|
| 558 | distances[0] = maxLen = 2;
|
| 559 | distances[1] = delta2 - 1;
|
| 560 | offset = 2;
|
| 561 | }
|
| 562 | if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
|
| 563 | {
|
| 564 | maxLen = 3;
|
| 565 | distances[offset + 1] = delta3 - 1;
|
| 566 | offset += 2;
|
| 567 | delta2 = delta3;
|
| 568 | }
|
| 569 | if (offset != 0)
|
| 570 | {
|
| 571 | for (; maxLen != lenLimit; maxLen++)
|
| 572 | if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
|
| 573 | break;
|
| 574 | distances[offset - 2] = maxLen;
|
| 575 | if (maxLen == lenLimit)
|
| 576 | {
|
| 577 | SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
|
| 578 | MOVE_POS_RET;
|
| 579 | }
|
| 580 | }
|
| 581 | if (maxLen < 3)
|
| 582 | maxLen = 3;
|
| 583 | GET_MATCHES_FOOTER(offset, maxLen)
|
| 584 | }
|
| 585 | |
| 586 | static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
|
| 587 | {
|
| 588 | UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
|
| 589 | GET_MATCHES_HEADER(4)
|
| 590 | |
| 591 | HASH4_CALC;
|
| 592 | |
| 593 | delta2 = p->pos - p->hash[ hash2Value];
|
| 594 | delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
|
| 595 | curMatch = p->hash[kFix4HashSize + hashValue];
|
| 596 | |
| 597 | p->hash[ hash2Value] =
|
| 598 | p->hash[kFix3HashSize + hash3Value] =
|
| 599 | p->hash[kFix4HashSize + hashValue] = p->pos;
|
| 600 | |
| 601 | maxLen = 1;
|
| 602 | offset = 0;
|
| 603 | if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
|
| 604 | {
|
| 605 | distances[0] = maxLen = 2;
|
| 606 | distances[1] = delta2 - 1;
|
| 607 | offset = 2;
|
| 608 | }
|
| 609 | if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
|
| 610 | {
|
| 611 | maxLen = 3;
|
| 612 | distances[offset + 1] = delta3 - 1;
|
| 613 | offset += 2;
|
| 614 | delta2 = delta3;
|
| 615 | }
|
| 616 | if (offset != 0)
|
| 617 | {
|
| 618 | for (; maxLen != lenLimit; maxLen++)
|
| 619 | if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
|
| 620 | break;
|
| 621 | distances[offset - 2] = maxLen;
|
| 622 | if (maxLen == lenLimit)
|
| 623 | {
|
| 624 | p->son[p->cyclicBufferPos] = curMatch;
|
| 625 | MOVE_POS_RET;
|
| 626 | }
|
| 627 | }
|
| 628 | if (maxLen < 3)
|
| 629 | maxLen = 3;
|
| 630 | offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
|
| 631 | distances + offset, maxLen) - (distances));
|
| 632 | MOVE_POS_RET
|
| 633 | }
|
| 634 | |
| 635 | UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
|
| 636 | {
|
| 637 | UInt32 offset;
|
| 638 | GET_MATCHES_HEADER(3)
|
| 639 | HASH_ZIP_CALC;
|
| 640 | curMatch = p->hash[hashValue];
|
| 641 | p->hash[hashValue] = p->pos;
|
| 642 | offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
|
| 643 | distances, 2) - (distances));
|
| 644 | MOVE_POS_RET
|
| 645 | }
|
| 646 | |
| 647 | static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
|
| 648 | {
|
| 649 | do
|
| 650 | {
|
| 651 | SKIP_HEADER(2)
|
| 652 | HASH2_CALC;
|
| 653 | curMatch = p->hash[hashValue];
|
| 654 | p->hash[hashValue] = p->pos;
|
| 655 | SKIP_FOOTER
|
| 656 | }
|
| 657 | while (--num != 0);
|
| 658 | }
|
| 659 | |
| 660 | void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
|
| 661 | {
|
| 662 | do
|
| 663 | {
|
| 664 | SKIP_HEADER(3)
|
| 665 | HASH_ZIP_CALC;
|
| 666 | curMatch = p->hash[hashValue];
|
| 667 | p->hash[hashValue] = p->pos;
|
| 668 | SKIP_FOOTER
|
| 669 | }
|
| 670 | while (--num != 0);
|
| 671 | }
|
| 672 | |
| 673 | static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
|
| 674 | {
|
| 675 | do
|
| 676 | {
|
| 677 | UInt32 hash2Value;
|
| 678 | SKIP_HEADER(3)
|
| 679 | HASH3_CALC;
|
| 680 | curMatch = p->hash[kFix3HashSize + hashValue];
|
| 681 | p->hash[hash2Value] =
|
| 682 | p->hash[kFix3HashSize + hashValue] = p->pos;
|
| 683 | SKIP_FOOTER
|
| 684 | }
|
| 685 | while (--num != 0);
|
| 686 | }
|
| 687 | |
| 688 | static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
|
| 689 | {
|
| 690 | do
|
| 691 | {
|
| 692 | UInt32 hash2Value, hash3Value;
|
| 693 | SKIP_HEADER(4)
|
| 694 | HASH4_CALC;
|
| 695 | curMatch = p->hash[kFix4HashSize + hashValue];
|
| 696 | p->hash[ hash2Value] =
|
| 697 | p->hash[kFix3HashSize + hash3Value] = p->pos;
|
| 698 | p->hash[kFix4HashSize + hashValue] = p->pos;
|
| 699 | SKIP_FOOTER
|
| 700 | }
|
| 701 | while (--num != 0);
|
| 702 | }
|
| 703 | |
| 704 | static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
|
| 705 | {
|
| 706 | do
|
| 707 | {
|
| 708 | UInt32 hash2Value, hash3Value;
|
| 709 | SKIP_HEADER(4)
|
| 710 | HASH4_CALC;
|
| 711 | curMatch = p->hash[kFix4HashSize + hashValue];
|
| 712 | p->hash[ hash2Value] =
|
| 713 | p->hash[kFix3HashSize + hash3Value] =
|
| 714 | p->hash[kFix4HashSize + hashValue] = p->pos;
|
| 715 | p->son[p->cyclicBufferPos] = curMatch;
|
| 716 | MOVE_POS
|
| 717 | }
|
| 718 | while (--num != 0);
|
| 719 | }
|
| 720 | |
| 721 | void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
|
| 722 | {
|
| 723 | do
|
| 724 | {
|
| 725 | SKIP_HEADER(3)
|
| 726 | HASH_ZIP_CALC;
|
| 727 | curMatch = p->hash[hashValue];
|
| 728 | p->hash[hashValue] = p->pos;
|
| 729 | p->son[p->cyclicBufferPos] = curMatch;
|
| 730 | MOVE_POS
|
| 731 | }
|
| 732 | while (--num != 0);
|
| 733 | }
|
| 734 | |
| 735 | void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
|
| 736 | {
|
| 737 | vTable->Init = (Mf_Init_Func)MatchFinder_Init;
|
| 738 | vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinder_GetIndexByte;
|
| 739 | vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;
|
| 740 | vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;
|
| 741 | if (!p->btMode)
|
| 742 | {
|
| 743 | vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;
|
| 744 | vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;
|
| 745 | }
|
| 746 | else if (p->numHashBytes == 2)
|
| 747 | {
|
| 748 | vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches;
|
| 749 | vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip;
|
| 750 | }
|
| 751 | else if (p->numHashBytes == 3)
|
| 752 | {
|
| 753 | vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;
|
| 754 | vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;
|
| 755 | }
|
| 756 | else
|
| 757 | {
|
| 758 | vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;
|
| 759 | vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;
|
| 760 | }
|
| 761 | }
|
src/lib/lib7z/Ppmd8.c
| r0 | r14440 | |
| 1 | /* Ppmd8.c -- PPMdI codec
|
| 2 | 2010-03-24 : Igor Pavlov : Public domain
|
| 3 | This code is based on PPMd var.I (2002): Dmitry Shkarin : Public domain */
|
| 4 | |
| 5 | #include <memory.h>
|
| 6 | |
| 7 | #include "Ppmd8.h"
|
| 8 | |
| 9 | const Byte PPMD8_kExpEscape[16] = { 25, 14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2 };
|
| 10 | static const UInt16 kInitBinEsc[] = { 0x3CDD, 0x1F3F, 0x59BF, 0x48F3, 0x64A1, 0x5ABC, 0x6632, 0x6051};
|
| 11 | |
| 12 | #define MAX_FREQ 124
|
| 13 | #define UNIT_SIZE 12
|
| 14 | |
| 15 | #define U2B(nu) ((UInt32)(nu) * UNIT_SIZE)
|
| 16 | #define U2I(nu) (p->Units2Indx[(nu) - 1])
|
| 17 | #define I2U(indx) (p->Indx2Units[indx])
|
| 18 | |
| 19 | #ifdef PPMD_32BIT
|
| 20 | #define REF(ptr) (ptr)
|
| 21 | #else
|
| 22 | #define REF(ptr) ((UInt32)((Byte *)(ptr) - (p)->Base))
|
| 23 | #endif
|
| 24 | |
| 25 | #define STATS_REF(ptr) ((CPpmd_State_Ref)REF(ptr))
|
| 26 | |
| 27 | #define CTX(ref) ((CPpmd8_Context *)Ppmd8_GetContext(p, ref))
|
| 28 | #define STATS(ctx) Ppmd8_GetStats(p, ctx)
|
| 29 | #define ONE_STATE(ctx) Ppmd8Context_OneState(ctx)
|
| 30 | #define SUFFIX(ctx) CTX((ctx)->Suffix)
|
| 31 | |
| 32 | typedef CPpmd8_Context * CTX_PTR;
|
| 33 | |
| 34 | struct CPpmd8_Node_;
|
| 35 | |
| 36 | typedef
|
| 37 | #ifdef PPMD_32BIT
|
| 38 | struct CPpmd8_Node_ *
|
| 39 | #else
|
| 40 | UInt32
|
| 41 | #endif
|
| 42 | CPpmd8_Node_Ref;
|
| 43 | |
| 44 | typedef struct CPpmd8_Node_
|
| 45 | {
|
| 46 | UInt32 Stamp;
|
| 47 | CPpmd8_Node_Ref Next;
|
| 48 | UInt32 NU;
|
| 49 | } CPpmd8_Node;
|
| 50 | |
| 51 | #ifdef PPMD_32BIT
|
| 52 | #define NODE(ptr) (ptr)
|
| 53 | #else
|
| 54 | #define NODE(offs) ((CPpmd8_Node *)(p->Base + (offs)))
|
| 55 | #endif
|
| 56 | |
| 57 | #define EMPTY_NODE 0xFFFFFFFF
|
| 58 | |
| 59 | void Ppmd8_Construct(CPpmd8 *p)
|
| 60 | {
|
| 61 | unsigned i, k, m;
|
| 62 | |
| 63 | p->Base = 0;
|
| 64 | |
| 65 | for (i = 0, k = 0; i < PPMD_NUM_INDEXES; i++)
|
| 66 | {
|
| 67 | unsigned step = (i >= 12 ? 4 : (i >> 2) + 1);
|
| 68 | do { p->Units2Indx[k++] = (Byte)i; } while(--step);
|
| 69 | p->Indx2Units[i] = (Byte)k;
|
| 70 | }
|
| 71 | |
| 72 | p->NS2BSIndx[0] = (0 << 1);
|
| 73 | p->NS2BSIndx[1] = (1 << 1);
|
| 74 | memset(p->NS2BSIndx + 2, (2 << 1), 9);
|
| 75 | memset(p->NS2BSIndx + 11, (3 << 1), 256 - 11);
|
| 76 | |
| 77 | for (i = 0; i < 5; i++)
|
| 78 | p->NS2Indx[i] = (Byte)i;
|
| 79 | for (m = i, k = 1; i < 260; i++)
|
| 80 | {
|
| 81 | p->NS2Indx[i] = (Byte)m;
|
| 82 | if (--k == 0)
|
| 83 | k = (++m) - 4;
|
| 84 | }
|
| 85 | }
|
| 86 | |
| 87 | void Ppmd8_Free(CPpmd8 *p, ISzAlloc *alloc)
|
| 88 | {
|
| 89 | alloc->Free(alloc, p->Base);
|
| 90 | p->Size = 0;
|
| 91 | p->Base = 0;
|
| 92 | }
|
| 93 | |
| 94 | Bool Ppmd8_Alloc(CPpmd8 *p, UInt32 size, ISzAlloc *alloc)
|
| 95 | {
|
| 96 | if (p->Base == 0 || p->Size != size)
|
| 97 | {
|
| 98 | Ppmd8_Free(p, alloc);
|
| 99 | p->AlignOffset =
|
| 100 | #ifdef PPMD_32BIT
|
| 101 | (4 - size) & 3;
|
| 102 | #else
|
| 103 | 4 - (size & 3);
|
| 104 | #endif
|
| 105 | if ((p->Base = (Byte *)alloc->Alloc(alloc, p->AlignOffset + size)) == 0)
|
| 106 | return False;
|
| 107 | p->Size = size;
|
| 108 | }
|
| 109 | return True;
|
| 110 | }
|
| 111 | |
| 112 | static void InsertNode(CPpmd8 *p, void *node, unsigned indx)
|
| 113 | {
|
| 114 | ((CPpmd8_Node *)node)->Stamp = EMPTY_NODE;
|
| 115 | ((CPpmd8_Node *)node)->Next = (CPpmd8_Node_Ref)p->FreeList[indx];
|
| 116 | ((CPpmd8_Node *)node)->NU = I2U(indx);
|
| 117 | p->FreeList[indx] = REF(node);
|
| 118 | p->Stamps[indx]++;
|
| 119 | }
|
| 120 | |
| 121 | static void *RemoveNode(CPpmd8 *p, unsigned indx)
|
| 122 | {
|
| 123 | CPpmd8_Node *node = NODE((CPpmd8_Node_Ref)p->FreeList[indx]);
|
| 124 | p->FreeList[indx] = node->Next;
|
| 125 | p->Stamps[indx]--;
|
| 126 | return node;
|
| 127 | }
|
| 128 | |
| 129 | static void SplitBlock(CPpmd8 *p, void *ptr, unsigned oldIndx, unsigned newIndx)
|
| 130 | {
|
| 131 | unsigned i, nu = I2U(oldIndx) - I2U(newIndx);
|
| 132 | ptr = (Byte *)ptr + U2B(I2U(newIndx));
|
| 133 | if (I2U(i = U2I(nu)) != nu)
|
| 134 | {
|
| 135 | unsigned k = I2U(--i);
|
| 136 | InsertNode(p, ((Byte *)ptr) + U2B(k), nu - k - 1);
|
| 137 | }
|
| 138 | InsertNode(p, ptr, i);
|
| 139 | }
|
| 140 | |
| 141 | static void GlueFreeBlocks(CPpmd8 *p)
|
| 142 | {
|
| 143 | CPpmd8_Node_Ref head = 0;
|
| 144 | CPpmd8_Node_Ref *prev = &head;
|
| 145 | unsigned i;
|
| 146 | |
| 147 | p->GlueCount = 1 << 13;
|
| 148 | memset(p->Stamps, 0, sizeof(p->Stamps));
|
| 149 | |
| 150 | /* Order-0 context is always at top UNIT, so we don't need guard NODE at the end.
|
| 151 | All blocks up to p->LoUnit can be free, so we need guard NODE at LoUnit. */
|
| 152 | if (p->LoUnit != p->HiUnit)
|
| 153 | ((CPpmd8_Node *)p->LoUnit)->Stamp = 0;
|
| 154 | |
| 155 | /* Glue free blocks */
|
| 156 | for (i = 0; i < PPMD_NUM_INDEXES; i++)
|
| 157 | {
|
| 158 | CPpmd8_Node_Ref next = (CPpmd8_Node_Ref)p->FreeList[i];
|
| 159 | p->FreeList[i] = 0;
|
| 160 | while (next != 0)
|
| 161 | {
|
| 162 | CPpmd8_Node *node = NODE(next);
|
| 163 | if (node->NU != 0)
|
| 164 | {
|
| 165 | CPpmd8_Node *node2;
|
| 166 | *prev = next;
|
| 167 | prev = &(node->Next);
|
| 168 | while ((node2 = node + node->NU)->Stamp == EMPTY_NODE)
|
| 169 | {
|
| 170 | node->NU += node2->NU;
|
| 171 | node2->NU = 0;
|
| 172 | }
|
| 173 | }
|
| 174 | next = node->Next;
|
| 175 | }
|
| 176 | }
|
| 177 | *prev = 0;
|
| 178 | |
| 179 | /* Fill lists of free blocks */
|
| 180 | while (head != 0)
|
| 181 | {
|
| 182 | CPpmd8_Node *node = NODE(head);
|
| 183 | unsigned nu;
|
| 184 | head = node->Next;
|
| 185 | nu = node->NU;
|
| 186 | if (nu == 0)
|
| 187 | continue;
|
| 188 | for (; nu > 128; nu -= 128, node += 128)
|
| 189 | InsertNode(p, node, PPMD_NUM_INDEXES - 1);
|
| 190 | if (I2U(i = U2I(nu)) != nu)
|
| 191 | {
|
| 192 | unsigned k = I2U(--i);
|
| 193 | InsertNode(p, node + k, nu - k - 1);
|
| 194 | }
|
| 195 | InsertNode(p, node, i);
|
| 196 | }
|
| 197 | }
|
| 198 | |
| 199 | static void *AllocUnitsRare(CPpmd8 *p, unsigned indx)
|
| 200 | {
|
| 201 | unsigned i;
|
| 202 | void *retVal;
|
| 203 | if (p->GlueCount == 0)
|
| 204 | {
|
| 205 | GlueFreeBlocks(p);
|
| 206 | if (p->FreeList[indx] != 0)
|
| 207 | return RemoveNode(p, indx);
|
| 208 | }
|
| 209 | i = indx;
|
| 210 | do
|
| 211 | {
|
| 212 | if (++i == PPMD_NUM_INDEXES)
|
| 213 | {
|
| 214 | UInt32 numBytes = U2B(I2U(indx));
|
| 215 | p->GlueCount--;
|
| 216 | return ((UInt32)(p->UnitsStart - p->Text) > numBytes) ? (p->UnitsStart -= numBytes) : (NULL);
|
| 217 | }
|
| 218 | }
|
| 219 | while (p->FreeList[i] == 0);
|
| 220 | retVal = RemoveNode(p, i);
|
| 221 | SplitBlock(p, retVal, i, indx);
|
| 222 | return retVal;
|
| 223 | }
|
| 224 | |
| 225 | static void *AllocUnits(CPpmd8 *p, unsigned indx)
|
| 226 | {
|
| 227 | UInt32 numBytes;
|
| 228 | if (p->FreeList[indx] != 0)
|
| 229 | return RemoveNode(p, indx);
|
| 230 | numBytes = U2B(I2U(indx));
|
| 231 | if (numBytes <= (UInt32)(p->HiUnit - p->LoUnit))
|
| 232 | {
|
| 233 | void *retVal = p->LoUnit;
|
| 234 | p->LoUnit += numBytes;
|
| 235 | return retVal;
|
| 236 | }
|
| 237 | return AllocUnitsRare(p, indx);
|
| 238 | }
|
| 239 | |
| 240 | #define MyMem12Cpy(dest, src, num) \
|
| 241 | { UInt32 *d = (UInt32 *)dest; const UInt32 *s = (const UInt32 *)src; UInt32 n = num; \
|
| 242 | do { d[0] = s[0]; d[1] = s[1]; d[2] = s[2]; s += 3; d += 3; } while(--n); }
|
| 243 | |
| 244 | static void *ShrinkUnits(CPpmd8 *p, void *oldPtr, unsigned oldNU, unsigned newNU)
|
| 245 | {
|
| 246 | unsigned i0 = U2I(oldNU);
|
| 247 | unsigned i1 = U2I(newNU);
|
| 248 | if (i0 == i1)
|
| 249 | return oldPtr;
|
| 250 | if (p->FreeList[i1] != 0)
|
| 251 | {
|
| 252 | void *ptr = RemoveNode(p, i1);
|
| 253 | MyMem12Cpy(ptr, oldPtr, newNU);
|
| 254 | InsertNode(p, oldPtr, i0);
|
| 255 | return ptr;
|
| 256 | }
|
| 257 | SplitBlock(p, oldPtr, i0, i1);
|
| 258 | return oldPtr;
|
| 259 | }
|
| 260 | |
| 261 | static void FreeUnits(CPpmd8 *p, void *ptr, unsigned nu)
|
| 262 | {
|
| 263 | InsertNode(p, ptr, U2I(nu));
|
| 264 | }
|
| 265 | |
| 266 | static void SpecialFreeUnit(CPpmd8 *p, void *ptr)
|
| 267 | {
|
| 268 | if ((Byte *)ptr != p->UnitsStart)
|
| 269 | InsertNode(p, ptr, 0);
|
| 270 | else
|
| 271 | {
|
| 272 | #ifdef PPMD8_FREEZE_SUPPORT
|
| 273 | *(UInt32 *)ptr = EMPTY_NODE; /* it's used for (Flags == 0xFF) check in RemoveBinContexts */
|
| 274 | #endif
|
| 275 | p->UnitsStart += UNIT_SIZE;
|
| 276 | }
|
| 277 | }
|
| 278 | |
| 279 | static void *MoveUnitsUp(CPpmd8 *p, void *oldPtr, unsigned nu)
|
| 280 | {
|
| 281 | unsigned indx = U2I(nu);
|
| 282 | void *ptr;
|
| 283 | if ((Byte *)oldPtr > p->UnitsStart + 16 * 1024 || REF(oldPtr) > p->FreeList[indx])
|
| 284 | return oldPtr;
|
| 285 | ptr = RemoveNode(p, indx);
|
| 286 | MyMem12Cpy(ptr, oldPtr, nu);
|
| 287 | if ((Byte*)oldPtr != p->UnitsStart)
|
| 288 | InsertNode(p, oldPtr, indx);
|
| 289 | else
|
| 290 | p->UnitsStart += U2B(I2U(indx));
|
| 291 | return ptr;
|
| 292 | }
|
| 293 | |
| 294 | static void ExpandTextArea(CPpmd8 *p)
|
| 295 | {
|
| 296 | UInt32 count[PPMD_NUM_INDEXES];
|
| 297 | unsigned i;
|
| 298 | memset(count, 0, sizeof(count));
|
| 299 | if (p->LoUnit != p->HiUnit)
|
| 300 | ((CPpmd8_Node *)p->LoUnit)->Stamp = 0;
|
| 301 | |
| 302 | {
|
| 303 | CPpmd8_Node *node = (CPpmd8_Node *)p->UnitsStart;
|
| 304 | for (; node->Stamp == EMPTY_NODE; node += node->NU)
|
| 305 | {
|
| 306 | node->Stamp = 0;
|
| 307 | count[U2I(node->NU)]++;
|
| 308 | }
|
| 309 | p->UnitsStart = (Byte *)node;
|
| 310 | }
|
| 311 | |
| 312 | for (i = 0; i < PPMD_NUM_INDEXES; i++)
|
| 313 | {
|
| 314 | CPpmd8_Node_Ref *next = (CPpmd8_Node_Ref *)&p->FreeList[i];
|
| 315 | while (count[i] != 0)
|
| 316 | {
|
| 317 | CPpmd8_Node *node = NODE(*next);
|
| 318 | while (node->Stamp == 0)
|
| 319 | {
|
| 320 | *next = node->Next;
|
| 321 | node = NODE(*next);
|
| 322 | p->Stamps[i]--;
|
| 323 | if (--count[i] == 0)
|
| 324 | break;
|
| 325 | }
|
| 326 | next = &node->Next;
|
| 327 | }
|
| 328 | }
|
| 329 | }
|
| 330 | |
| 331 | #define SUCCESSOR(p) ((CPpmd_Void_Ref)((p)->SuccessorLow | ((UInt32)(p)->SuccessorHigh << 16)))
|
| 332 | |
| 333 | static void SetSuccessor(CPpmd_State *p, CPpmd_Void_Ref v)
|
| 334 | {
|
| 335 | (p)->SuccessorLow = (UInt16)((UInt32)(v) & 0xFFFF);
|
| 336 | (p)->SuccessorHigh = (UInt16)(((UInt32)(v) >> 16) & 0xFFFF);
|
| 337 | }
|
| 338 | |
| 339 | #define RESET_TEXT(offs) { p->Text = p->Base + p->AlignOffset + (offs); }
|
| 340 | |
| 341 | static void RestartModel(CPpmd8 *p)
|
| 342 | {
|
| 343 | unsigned i, k, m, r;
|
| 344 | |
| 345 | memset(p->FreeList, 0, sizeof(p->FreeList));
|
| 346 | memset(p->Stamps, 0, sizeof(p->Stamps));
|
| 347 | RESET_TEXT(0);
|
| 348 | p->HiUnit = p->Text + p->Size;
|
| 349 | p->LoUnit = p->UnitsStart = p->HiUnit - p->Size / 8 / UNIT_SIZE * 7 * UNIT_SIZE;
|
| 350 | p->GlueCount = 0;
|
| 351 | |
| 352 | p->OrderFall = p->MaxOrder;
|
| 353 | p->RunLength = p->InitRL = -(Int32)((p->MaxOrder < 12) ? p->MaxOrder : 12) - 1;
|
| 354 | p->PrevSuccess = 0;
|
| 355 | |
| 356 | p->MinContext = p->MaxContext = (CTX_PTR)(p->HiUnit -= UNIT_SIZE); /* AllocContext(p); */
|
| 357 | p->MinContext->Suffix = 0;
|
| 358 | p->MinContext->NumStats = 255;
|
| 359 | p->MinContext->Flags = 0;
|
| 360 | p->MinContext->SummFreq = 256 + 1;
|
| 361 | p->FoundState = (CPpmd_State *)p->LoUnit; /* AllocUnits(p, PPMD_NUM_INDEXES - 1); */
|
| 362 | p->LoUnit += U2B(256 / 2);
|
| 363 | p->MinContext->Stats = REF(p->FoundState);
|
| 364 | for (i = 0; i < 256; i++)
|
| 365 | {
|
| 366 | CPpmd_State *s = &p->FoundState[i];
|
| 367 | s->Symbol = (Byte)i;
|
| 368 | s->Freq = 1;
|
| 369 | SetSuccessor(s, 0);
|
| 370 | }
|
| 371 | |
| 372 | for (i = m = 0; m < 25; m++)
|
| 373 | {
|
| 374 | while (p->NS2Indx[i] == m)
|
| 375 | i++;
|
| 376 | for (k = 0; k < 8; k++)
|
| 377 | {
|
| 378 | UInt16 val = (UInt16)(PPMD_BIN_SCALE - kInitBinEsc[k] / (i + 1));
|
| 379 | UInt16 *dest = p->BinSumm[m] + k;
|
| 380 | for (r = 0; r < 64; r += 8)
|
| 381 | dest[r] = val;
|
| 382 | }
|
| 383 | }
|
| 384 | |
| 385 | for (i = m = 0; m < 24; m++)
|
| 386 | {
|
| 387 | while (p->NS2Indx[i + 3] == m + 3)
|
| 388 | i++;
|
| 389 | for (k = 0; k < 32; k++)
|
| 390 | {
|
| 391 | CPpmd_See *s = &p->See[m][k];
|
| 392 | s->Summ = (UInt16)((2 * i + 5) << (s->Shift = PPMD_PERIOD_BITS - 4));
|
| 393 | s->Count = 7;
|
| 394 | }
|
| 395 | }
|
| 396 | }
|
| 397 | |
| 398 | void Ppmd8_Init(CPpmd8 *p, unsigned maxOrder, unsigned restoreMethod)
|
| 399 | {
|
| 400 | p->MaxOrder = maxOrder;
|
| 401 | p->RestoreMethod = restoreMethod;
|
| 402 | RestartModel(p);
|
| 403 | p->DummySee.Shift = PPMD_PERIOD_BITS;
|
| 404 | p->DummySee.Summ = 0; /* unused */
|
| 405 | p->DummySee.Count = 64; /* unused */
|
| 406 | }
|
| 407 | |
| 408 | static void Refresh(CPpmd8 *p, CTX_PTR ctx, unsigned oldNU, unsigned scale)
|
| 409 | {
|
| 410 | unsigned i = ctx->NumStats, escFreq, sumFreq, flags;
|
| 411 | CPpmd_State *s = (CPpmd_State *)ShrinkUnits(p, STATS(ctx), oldNU, (i + 2) >> 1);
|
| 412 | ctx->Stats = REF(s);
|
| 413 | #ifdef PPMD8_FREEZE_SUPPORT
|
| 414 | /* fixed over Shkarin's code. Fixed code is not compatible with original code for some files in FREEZE mode. */
|
| 415 | scale |= (ctx->SummFreq >= ((UInt32)1 << 15));
|
| 416 | #endif
|
| 417 | flags = (ctx->Flags & (0x10 + 0x04 * scale)) + 0x08 * (s->Symbol >= 0x40);
|
| 418 | escFreq = ctx->SummFreq - s->Freq;
|
| 419 | sumFreq = (s->Freq = (Byte)((s->Freq + scale) >> scale));
|
| 420 | do
|
| 421 | {
|
| 422 | escFreq -= (++s)->Freq;
|
| 423 | sumFreq += (s->Freq = (Byte)((s->Freq + scale) >> scale));
|
| 424 | flags |= 0x08 * (s->Symbol >= 0x40);
|
| 425 | }
|
| 426 | while (--i);
|
| 427 | ctx->SummFreq = (UInt16)(sumFreq + ((escFreq + scale) >> scale));
|
| 428 | ctx->Flags = (Byte)flags;
|
| 429 | }
|
| 430 | |
| 431 | static void SwapStates(CPpmd_State *t1, CPpmd_State *t2)
|
| 432 | {
|
| 433 | CPpmd_State tmp = *t1;
|
| 434 | *t1 = *t2;
|
| 435 | *t2 = tmp;
|
| 436 | }
|
| 437 | |
| 438 | static CPpmd_Void_Ref CutOff(CPpmd8 *p, CTX_PTR ctx, unsigned order)
|
| 439 | {
|
| 440 | int i;
|
| 441 | unsigned tmp;
|
| 442 | CPpmd_State *s;
|
| 443 | |
| 444 | if (!ctx->NumStats)
|
| 445 | {
|
| 446 | s = ONE_STATE(ctx);
|
| 447 | if ((Byte *)Ppmd8_GetPtr(p, SUCCESSOR(s)) >= p->UnitsStart)
|
| 448 | {
|
| 449 | if (order < p->MaxOrder)
|
| 450 | SetSuccessor(s, CutOff(p, CTX(SUCCESSOR(s)), order + 1));
|
| 451 | else
|
| 452 | SetSuccessor(s, 0);
|
| 453 | if (SUCCESSOR(s) || order <= 9) /* O_BOUND */
|
| 454 | return REF(ctx);
|
| 455 | }
|
| 456 | SpecialFreeUnit(p, ctx);
|
| 457 | return 0;
|
| 458 | }
|
| 459 | |
| 460 | ctx->Stats = STATS_REF(MoveUnitsUp(p, STATS(ctx), tmp = ((unsigned)ctx->NumStats + 2) >> 1));
|
| 461 | |
| 462 | for (s = STATS(ctx) + (i = ctx->NumStats); s >= STATS(ctx); s--)
|
| 463 | if ((Byte *)Ppmd8_GetPtr(p, SUCCESSOR(s)) < p->UnitsStart)
|
| 464 | {
|
| 465 | CPpmd_State *s2 = STATS(ctx) + (i--);
|
| 466 | SetSuccessor(s, 0);
|
| 467 | SwapStates(s, s2);
|
| 468 | }
|
| 469 | else if (order < p->MaxOrder)
|
| 470 | SetSuccessor(s, CutOff(p, CTX(SUCCESSOR(s)), order + 1));
|
| 471 | else
|
| 472 | SetSuccessor(s, 0);
|
| 473 | |
| 474 | if (i != ctx->NumStats && order)
|
| 475 | {
|
| 476 | ctx->NumStats = (Byte)i;
|
| 477 | s = STATS(ctx);
|
| 478 | if (i < 0)
|
| 479 | {
|
| 480 | FreeUnits(p, s, tmp);
|
| 481 | SpecialFreeUnit(p, ctx);
|
| 482 | return 0;
|
| 483 | }
|
| 484 | if (i == 0)
|
| 485 | {
|
| 486 | ctx->Flags = (ctx->Flags & 0x10) + 0x08 * (s->Symbol >= 0x40);
|
| 487 | *ONE_STATE(ctx) = *s;
|
| 488 | FreeUnits(p, s, tmp);
|
| 489 | ONE_STATE(ctx)->Freq = (Byte)((unsigned)ONE_STATE(ctx)->Freq + 11) >> 3;
|
| 490 | }
|
| 491 | else
|
| 492 | Refresh(p, ctx, tmp, ctx->SummFreq > 16 * i);
|
| 493 | }
|
| 494 | return REF(ctx);
|
| 495 | }
|
| 496 | |
| 497 | #ifdef PPMD8_FREEZE_SUPPORT
|
| 498 | static CPpmd_Void_Ref RemoveBinContexts(CPpmd8 *p, CTX_PTR ctx, unsigned order)
|
| 499 | {
|
| 500 | CPpmd_State *s;
|
| 501 | if (!ctx->NumStats)
|
| 502 | {
|
| 503 | s = ONE_STATE(ctx);
|
| 504 | if ((Byte *)Ppmd8_GetPtr(p, SUCCESSOR(s)) >= p->UnitsStart && order < p->MaxOrder)
|
| 505 | SetSuccessor(s, RemoveBinContexts(p, CTX(SUCCESSOR(s)), order + 1));
|
| 506 | else
|
| 507 | SetSuccessor(s, 0);
|
| 508 | /* Suffix context can be removed already, since different (high-order)
|
| 509 | Successors may refer to same context. So we check Flags == 0xFF (Stamp == EMPTY_NODE) */
|
| 510 | if (!SUCCESSOR(s) && (!SUFFIX(ctx)->NumStats || SUFFIX(ctx)->Flags == 0xFF))
|
| 511 | {
|
| 512 | FreeUnits(p, ctx, 1);
|
| 513 | return 0;
|
| 514 | }
|
| 515 | else
|
| 516 | return REF(ctx);
|
| 517 | }
|
| 518 | |
| 519 | for (s = STATS(ctx) + ctx->NumStats; s >= STATS(ctx); s--)
|
| 520 | if ((Byte *)Ppmd8_GetPtr(p, SUCCESSOR(s)) >= p->UnitsStart && order < p->MaxOrder)
|
| 521 | SetSuccessor(s, RemoveBinContexts(p, CTX(SUCCESSOR(s)), order + 1));
|
| 522 | else
|
| 523 | SetSuccessor(s, 0);
|
| 524 | |
| 525 | return REF(ctx);
|
| 526 | }
|
| 527 | #endif
|
| 528 | |
| 529 | static UInt32 GetUsedMemory(const CPpmd8 *p)
|
| 530 | {
|
| 531 | UInt32 v = 0;
|
| 532 | unsigned i;
|
| 533 | for (i = 0; i < PPMD_NUM_INDEXES; i++)
|
| 534 | v += p->Stamps[i] * I2U(i);
|
| 535 | return p->Size - (UInt32)(p->HiUnit - p->LoUnit) - (UInt32)(p->UnitsStart - p->Text) - U2B(v);
|
| 536 | }
|
| 537 | |
| 538 | #ifdef PPMD8_FREEZE_SUPPORT
|
| 539 | #define RESTORE_MODEL(c1, fSuccessor) RestoreModel(p, c1, fSuccessor)
|
| 540 | #else
|
| 541 | #define RESTORE_MODEL(c1, fSuccessor) RestoreModel(p, c1)
|
| 542 | #endif
|
| 543 | |
| 544 | static void RestoreModel(CPpmd8 *p, CTX_PTR c1
|
| 545 | #ifdef PPMD8_FREEZE_SUPPORT
|
| 546 | , CTX_PTR fSuccessor
|
| 547 | #endif
|
| 548 | )
|
| 549 | {
|
| 550 | CTX_PTR c;
|
| 551 | CPpmd_State *s;
|
| 552 | RESET_TEXT(0);
|
| 553 | for (c = p->MaxContext; c != c1; c = SUFFIX(c))
|
| 554 | if (--(c->NumStats) == 0)
|
| 555 | {
|
| 556 | s = STATS(c);
|
| 557 | c->Flags = (c->Flags & 0x10) + 0x08 * (s->Symbol >= 0x40);
|
| 558 | *ONE_STATE(c) = *s;
|
| 559 | SpecialFreeUnit(p, s);
|
| 560 | ONE_STATE(c)->Freq = (ONE_STATE(c)->Freq + 11) >> 3;
|
| 561 | }
|
| 562 | else
|
| 563 | Refresh(p, c, (c->NumStats+3) >> 1, 0);
|
| 564 |
|
| 565 | for (; c != p->MinContext; c = SUFFIX(c))
|
| 566 | if (!c->NumStats)
|
| 567 | ONE_STATE(c)->Freq -= ONE_STATE(c)->Freq >> 1;
|
| 568 | else if ((c->SummFreq += 4) > 128 + 4 * c->NumStats)
|
| 569 | Refresh(p, c, (c->NumStats + 2) >> 1, 1);
|
| 570 | |
| 571 | #ifdef PPMD8_FREEZE_SUPPORT
|
| 572 | if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE)
|
| 573 | {
|
| 574 | p->MaxContext = fSuccessor;
|
| 575 | p->GlueCount += !(p->Stamps[1] & 1);
|
| 576 | }
|
| 577 | else if (p->RestoreMethod == PPMD8_RESTORE_METHOD_FREEZE)
|
| 578 | {
|
| 579 | while (p->MaxContext->Suffix)
|
| 580 | p->MaxContext = SUFFIX(p->MaxContext);
|
| 581 | RemoveBinContexts(p, p->MaxContext, 0);
|
| 582 | p->RestoreMethod++;
|
| 583 | p->GlueCount = 0;
|
| 584 | p->OrderFall = p->MaxOrder;
|
| 585 | }
|
| 586 | else
|
| 587 | #endif
|
| 588 | if (p->RestoreMethod == PPMD8_RESTORE_METHOD_RESTART || GetUsedMemory(p) < (p->Size >> 1))
|
| 589 | RestartModel(p);
|
| 590 | else
|
| 591 | {
|
| 592 | while (p->MaxContext->Suffix)
|
| 593 | p->MaxContext = SUFFIX(p->MaxContext);
|
| 594 | do
|
| 595 | {
|
| 596 | CutOff(p, p->MaxContext, 0);
|
| 597 | ExpandTextArea(p);
|
| 598 | }
|
| 599 | while (GetUsedMemory(p) > 3 * (p->Size >> 2));
|
| 600 | p->GlueCount = 0;
|
| 601 | p->OrderFall = p->MaxOrder;
|
| 602 | }
|
| 603 | }
|
| 604 | |
| 605 | static CTX_PTR CreateSuccessors(CPpmd8 *p, Bool skip, CPpmd_State *s1, CTX_PTR c)
|
| 606 | {
|
| 607 | CPpmd_State upState;
|
| 608 | Byte flags;
|
| 609 | CPpmd_Byte_Ref upBranch = (CPpmd_Byte_Ref)SUCCESSOR(p->FoundState);
|
| 610 | /* fixed over Shkarin's code. Maybe it could work without + 1 too. */
|
| 611 | CPpmd_State *ps[PPMD8_MAX_ORDER + 1];
|
| 612 | unsigned numPs = 0;
|
| 613 | |
| 614 | if (!skip)
|
| 615 | ps[numPs++] = p->FoundState;
|
| 616 | |
| 617 | while (c->Suffix)
|
| 618 | {
|
| 619 | CPpmd_Void_Ref successor;
|
| 620 | CPpmd_State *s;
|
| 621 | c = SUFFIX(c);
|
| 622 | if (s1)
|
| 623 | {
|
| 624 | s = s1;
|
| 625 | s1 = NULL;
|
| 626 | }
|
| 627 | else if (c->NumStats != 0)
|
| 628 | {
|
| 629 | for (s = STATS(c); s->Symbol != p->FoundState->Symbol; s++);
|
| 630 | if (s->Freq < MAX_FREQ - 9)
|
| 631 | {
|
| 632 | s->Freq++;
|
| 633 | c->SummFreq++;
|
| 634 | }
|
| 635 | }
|
| 636 | else
|
| 637 | {
|
| 638 | s = ONE_STATE(c);
|
| 639 | s->Freq += (!SUFFIX(c)->NumStats & (s->Freq < 24));
|
| 640 | }
|
| 641 | successor = SUCCESSOR(s);
|
| 642 | if (successor != upBranch)
|
| 643 | {
|
| 644 | c = CTX(successor);
|
| 645 | if (numPs == 0)
|
| 646 | return c;
|
| 647 | break;
|
| 648 | }
|
| 649 | ps[numPs++] = s;
|
| 650 | }
|
| 651 | |
| 652 | upState.Symbol = *(const Byte *)Ppmd8_GetPtr(p, upBranch);
|
| 653 | SetSuccessor(&upState, upBranch + 1);
|
| 654 | flags = 0x10 * (p->FoundState->Symbol >= 0x40) + 0x08 * (upState.Symbol >= 0x40);
|
| 655 | |
| 656 | if (c->NumStats == 0)
|
| 657 | upState.Freq = ONE_STATE(c)->Freq;
|
| 658 | else
|
| 659 | {
|
| 660 | UInt32 cf, s0;
|
| 661 | CPpmd_State *s;
|
| 662 | for (s = STATS(c); s->Symbol != upState.Symbol; s++);
|
| 663 | cf = s->Freq - 1;
|
| 664 | s0 = c->SummFreq - c->NumStats - cf;
|
| 665 | upState.Freq = (Byte)(1 + ((2 * cf <= s0) ? (5 * cf > s0) : ((cf + 2 * s0 - 3) / s0)));
|
| 666 | }
|
| 667 | |
| 668 | do
|
| 669 | {
|
| 670 | /* Create Child */
|
| 671 | CTX_PTR c1; /* = AllocContext(p); */
|
| 672 | if (p->HiUnit != p->LoUnit)
|
| 673 | c1 = (CTX_PTR)(p->HiUnit -= UNIT_SIZE);
|
| 674 | else if (p->FreeList[0] != 0)
|
| 675 | c1 = (CTX_PTR)RemoveNode(p, 0);
|
| 676 | else
|
| 677 | {
|
| 678 | c1 = (CTX_PTR)AllocUnitsRare(p, 0);
|
| 679 | if (!c1)
|
| 680 | return NULL;
|
| 681 | }
|
| 682 | c1->NumStats = 0;
|
| 683 | c1->Flags = flags;
|
| 684 | *ONE_STATE(c1) = upState;
|
| 685 | c1->Suffix = REF(c);
|
| 686 | SetSuccessor(ps[--numPs], REF(c1));
|
| 687 | c = c1;
|
| 688 | }
|
| 689 | while (numPs != 0);
|
| 690 | |
| 691 | return c;
|
| 692 | }
|
| 693 | |
| 694 | static CTX_PTR ReduceOrder(CPpmd8 *p, CPpmd_State *s1, CTX_PTR c)
|
| 695 | {
|
| 696 | CPpmd_State *s = NULL;
|
| 697 | CTX_PTR c1 = c;
|
| 698 | CPpmd_Void_Ref upBranch = REF(p->Text);
|
| 699 | |
| 700 | #ifdef PPMD8_FREEZE_SUPPORT
|
| 701 | /* The BUG in Shkarin's code was fixed: ps could overflow in CUT_OFF mode. */
|
| 702 | CPpmd_State *ps[PPMD8_MAX_ORDER + 1];
|
| 703 | unsigned numPs = 0;
|
| 704 | ps[numPs++] = p->FoundState;
|
| 705 | #endif
|
| 706 | |
| 707 | SetSuccessor(p->FoundState, upBranch);
|
| 708 | p->OrderFall++;
|
| 709 | |
| 710 | for (;;)
|
| 711 | {
|
| 712 | if (s1)
|
| 713 | {
|
| 714 | c = SUFFIX(c);
|
| 715 | s = s1;
|
| 716 | s1 = NULL;
|
| 717 | }
|
| 718 | else
|
| 719 | {
|
| 720 | if (!c->Suffix)
|
| 721 | {
|
| 722 | #ifdef PPMD8_FREEZE_SUPPORT
|
| 723 | if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE)
|
| 724 | {
|
| 725 | do { SetSuccessor(ps[--numPs], REF(c)); } while (numPs);
|
| 726 | RESET_TEXT(1);
|
| 727 | p->OrderFall = 1;
|
| 728 | }
|
| 729 | #endif
|
| 730 | return c;
|
| 731 | }
|
| 732 | c = SUFFIX(c);
|
| 733 | if (c->NumStats)
|
| 734 | {
|
| 735 | if ((s = STATS(c))->Symbol != p->FoundState->Symbol)
|
| 736 | do { s++; } while (s->Symbol != p->FoundState->Symbol);
|
| 737 | if (s->Freq < MAX_FREQ - 9)
|
| 738 | {
|
| 739 | s->Freq += 2;
|
| 740 | c->SummFreq += 2;
|
| 741 | }
|
| 742 | }
|
| 743 | else
|
| 744 | {
|
| 745 | s = ONE_STATE(c);
|
| 746 | s->Freq += (s->Freq < 32);
|
| 747 | }
|
| 748 | }
|
| 749 | if (SUCCESSOR(s))
|
| 750 | break;
|
| 751 | #ifdef PPMD8_FREEZE_SUPPORT
|
| 752 | ps[numPs++] = s;
|
| 753 | #endif
|
| 754 | SetSuccessor(s, upBranch);
|
| 755 | p->OrderFall++;
|
| 756 | }
|
| 757 | |
| 758 | #ifdef PPMD8_FREEZE_SUPPORT
|
| 759 | if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE)
|
| 760 | {
|
| 761 | c = CTX(SUCCESSOR(s));
|
| 762 | do { SetSuccessor(ps[--numPs], REF(c)); } while (numPs);
|
| 763 | RESET_TEXT(1);
|
| 764 | p->OrderFall = 1;
|
| 765 | return c;
|
| 766 | }
|
| 767 | else
|
| 768 | #endif
|
| 769 | if (SUCCESSOR(s) <= upBranch)
|
| 770 | {
|
| 771 | CTX_PTR successor;
|
| 772 | CPpmd_State *s1 = p->FoundState;
|
| 773 | p->FoundState = s;
|
| 774 | |
| 775 | successor = CreateSuccessors(p, False, NULL, c);
|
| 776 | if (successor == NULL)
|
| 777 | SetSuccessor(s, 0);
|
| 778 | else
|
| 779 | SetSuccessor(s, REF(successor));
|
| 780 | p->FoundState = s1;
|
| 781 | }
|
| 782 | |
| 783 | if (p->OrderFall == 1 && c1 == p->MaxContext)
|
| 784 | {
|
| 785 | SetSuccessor(p->FoundState, SUCCESSOR(s));
|
| 786 | p->Text--;
|
| 787 | }
|
| 788 | if (SUCCESSOR(s) == 0)
|
| 789 | return NULL;
|
| 790 | return CTX(SUCCESSOR(s));
|
| 791 | }
|
| 792 | |
| 793 | static void UpdateModel(CPpmd8 *p)
|
| 794 | {
|
| 795 | CPpmd_Void_Ref successor, fSuccessor = SUCCESSOR(p->FoundState);
|
| 796 | CTX_PTR c;
|
| 797 | unsigned s0, ns, fFreq = p->FoundState->Freq;
|
| 798 | Byte flag, fSymbol = p->FoundState->Symbol;
|
| 799 | CPpmd_State *s = NULL;
|
| 800 | |
| 801 | if (p->FoundState->Freq < MAX_FREQ / 4 && p->MinContext->Suffix != 0)
|
| 802 | {
|
| 803 | c = SUFFIX(p->MinContext);
|
| 804 | |
| 805 | if (c->NumStats == 0)
|
| 806 | {
|
| 807 | s = ONE_STATE(c);
|
| 808 | if (s->Freq < 32)
|
| 809 | s->Freq++;
|
| 810 | }
|
| 811 | else
|
| 812 | {
|
| 813 | s = STATS(c);
|
| 814 | if (s->Symbol != p->FoundState->Symbol)
|
| 815 | {
|
| 816 | do { s++; } while (s->Symbol != p->FoundState->Symbol);
|
| 817 | if (s[0].Freq >= s[-1].Freq)
|
| 818 | {
|
| 819 | SwapStates(&s[0], &s[-1]);
|
| 820 | s--;
|
| 821 | }
|
| 822 | }
|
| 823 | if (s->Freq < MAX_FREQ - 9)
|
| 824 | {
|
| 825 | s->Freq += 2;
|
| 826 | c->SummFreq += 2;
|
| 827 | }
|
| 828 | }
|
| 829 | }
|
| 830 | |
| 831 | c = p->MaxContext;
|
| 832 | if (p->OrderFall == 0 && fSuccessor)
|
| 833 | {
|
| 834 | CTX_PTR cs = CreateSuccessors(p, True, s, p->MinContext);
|
| 835 | if (cs == 0)
|
| 836 | {
|
| 837 | SetSuccessor(p->FoundState, 0);
|
| 838 | RESTORE_MODEL(c, CTX(fSuccessor));
|
| 839 | }
|
| 840 | else
|
| 841 | {
|
| 842 | SetSuccessor(p->FoundState, REF(cs));
|
| 843 | p->MaxContext = cs;
|
| 844 | }
|
| 845 | return;
|
| 846 | }
|
| 847 | |
| 848 | *p->Text++ = p->FoundState->Symbol;
|
| 849 | successor = REF(p->Text);
|
| 850 | if (p->Text >= p->UnitsStart)
|
| 851 | {
|
| 852 | RESTORE_MODEL(c, CTX(fSuccessor)); /* check it */
|
| 853 | return;
|
| 854 | }
|
| 855 | |
| 856 | if (!fSuccessor)
|
| 857 | {
|
| 858 | CTX_PTR cs = ReduceOrder(p, s, p->MinContext);
|
| 859 | if (cs == NULL)
|
| 860 | {
|
| 861 | RESTORE_MODEL(c, 0);
|
| 862 | return;
|
| 863 | }
|
| 864 | fSuccessor = REF(cs);
|
| 865 | }
|
| 866 | else if ((Byte *)Ppmd8_GetPtr(p, fSuccessor) < p->UnitsStart)
|
| 867 | {
|
| 868 | CTX_PTR cs = CreateSuccessors(p, False, s, p->MinContext);
|
| 869 | if (cs == NULL)
|
| 870 | {
|
| 871 | RESTORE_MODEL(c, 0);
|
| 872 | return;
|
| 873 | }
|
| 874 | fSuccessor = REF(cs);
|
| 875 | }
|
| 876 | |
| 877 | if (--p->OrderFall == 0)
|
| 878 | {
|
| 879 | successor = fSuccessor;
|
| 880 | p->Text -= (p->MaxContext != p->MinContext);
|
| 881 | }
|
| 882 | #ifdef PPMD8_FREEZE_SUPPORT
|
| 883 | else if (p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE)
|
| 884 | {
|
| 885 | successor = fSuccessor;
|
| 886 | RESET_TEXT(0);
|
| 887 | p->OrderFall = 0;
|
| 888 | }
|
| 889 | #endif
|
| 890 | |
| 891 | s0 = p->MinContext->SummFreq - (ns = p->MinContext->NumStats) - fFreq;
|
| 892 | flag = 0x08 * (fSymbol >= 0x40);
|
| 893 | |
| 894 | for (; c != p->MinContext; c = SUFFIX(c))
|
| 895 | {
|
| 896 | unsigned ns1;
|
| 897 | UInt32 cf, sf;
|
| 898 | if ((ns1 = c->NumStats) != 0)
|
| 899 | {
|
| 900 | if ((ns1 & 1) != 0)
|
| 901 | {
|
| 902 | /* Expand for one UNIT */
|
| 903 | unsigned oldNU = (ns1 + 1) >> 1;
|
| 904 | unsigned i = U2I(oldNU);
|
| 905 | if (i != U2I(oldNU + 1))
|
| 906 | {
|
| 907 | void *ptr = AllocUnits(p, i + 1);
|
| 908 | void *oldPtr;
|
| 909 | if (!ptr)
|
| 910 | {
|
| 911 | RESTORE_MODEL(c, CTX(fSuccessor));
|
| 912 | return;
|
| 913 | }
|
| 914 | oldPtr = STATS(c);
|
| 915 | MyMem12Cpy(ptr, oldPtr, oldNU);
|
| 916 | InsertNode(p, oldPtr, i);
|
| 917 | c->Stats = STATS_REF(ptr);
|
| 918 | }
|
| 919 | }
|
| 920 | c->SummFreq = (UInt16)(c->SummFreq + (3 * ns1 + 1 < ns));
|
| 921 | }
|
| 922 | else
|
| 923 | {
|
| 924 | CPpmd_State *s = (CPpmd_State*)AllocUnits(p, 0);
|
| 925 | if (!s)
|
| 926 | {
|
| 927 | RESTORE_MODEL(c, CTX(fSuccessor));
|
| 928 | return;
|
| 929 | }
|
| 930 | *s = *ONE_STATE(c);
|
| 931 | c->Stats = REF(s);
|
| 932 | if (s->Freq < MAX_FREQ / 4 - 1)
|
| 933 | s->Freq <<= 1;
|
| 934 | else
|
| 935 | s->Freq = MAX_FREQ - 4;
|
| 936 | c->SummFreq = (UInt16)(s->Freq + p->InitEsc + (ns > 2));
|
| 937 | }
|
| 938 | cf = 2 * fFreq * (c->SummFreq + 6);
|
| 939 | sf = (UInt32)s0 + c->SummFreq;
|
| 940 | if (cf < 6 * sf)
|
| 941 | {
|
| 942 | cf = 1 + (cf > sf) + (cf >= 4 * sf);
|
| 943 | c->SummFreq += 4;
|
| 944 | }
|
| 945 | else
|
| 946 | {
|
| 947 | cf = 4 + (cf > 9 * sf) + (cf > 12 * sf) + (cf > 15 * sf);
|
| 948 | c->SummFreq = (UInt16)(c->SummFreq + cf);
|
| 949 | }
|
| 950 | {
|
| 951 | CPpmd_State *s = STATS(c) + ns1 + 1;
|
| 952 | SetSuccessor(s, successor);
|
| 953 | s->Symbol = fSymbol;
|
| 954 | s->Freq = (Byte)cf;
|
| 955 | c->Flags |= flag;
|
| 956 | c->NumStats = (Byte)(ns1 + 1);
|
| 957 | }
|
| 958 | }
|
| 959 | p->MaxContext = p->MinContext = CTX(fSuccessor);
|
| 960 | }
|
| 961 | |
| 962 | static void Rescale(CPpmd8 *p)
|
| 963 | {
|
| 964 | unsigned i, adder, sumFreq, escFreq;
|
| 965 | CPpmd_State *stats = STATS(p->MinContext);
|
| 966 | CPpmd_State *s = p->FoundState;
|
| 967 | {
|
| 968 | CPpmd_State tmp = *s;
|
| 969 | for (; s != stats; s--)
|
| 970 | s[0] = s[-1];
|
| 971 | *s = tmp;
|
| 972 | }
|
| 973 | escFreq = p->MinContext->SummFreq - s->Freq;
|
| 974 | s->Freq += 4;
|
| 975 | adder = (p->OrderFall != 0
|
| 976 | #ifdef PPMD8_FREEZE_SUPPORT
|
| 977 | || p->RestoreMethod > PPMD8_RESTORE_METHOD_FREEZE
|
| 978 | #endif
|
| 979 | );
|
| 980 | s->Freq = (Byte)((s->Freq + adder) >> 1);
|
| 981 | sumFreq = s->Freq;
|
| 982 | |
| 983 | i = p->MinContext->NumStats;
|
| 984 | do
|
| 985 | {
|
| 986 | escFreq -= (++s)->Freq;
|
| 987 | s->Freq = (Byte)((s->Freq + adder) >> 1);
|
| 988 | sumFreq += s->Freq;
|
| 989 | if (s[0].Freq > s[-1].Freq)
|
| 990 | {
|
| 991 | CPpmd_State *s1 = s;
|
| 992 | CPpmd_State tmp = *s1;
|
| 993 | do
|
| 994 | s1[0] = s1[-1];
|
| 995 | while (--s1 != stats && tmp.Freq > s1[-1].Freq);
|
| 996 | *s1 = tmp;
|
| 997 | }
|
| 998 | }
|
| 999 | while (--i);
|
| 1000 | |
| 1001 | if (s->Freq == 0)
|
| 1002 | {
|
| 1003 | unsigned numStats = p->MinContext->NumStats;
|
| 1004 | unsigned n0, n1;
|
| 1005 | do { i++; } while ((--s)->Freq == 0);
|
| 1006 | escFreq += i;
|
| 1007 | p->MinContext->NumStats = (Byte)(p->MinContext->NumStats - i);
|
| 1008 | if (p->MinContext->NumStats == 0)
|
| 1009 | {
|
| 1010 | CPpmd_State tmp = *stats;
|
| 1011 | tmp.Freq = (Byte)((2 * tmp.Freq + escFreq - 1) / escFreq);
|
| 1012 | if (tmp.Freq > MAX_FREQ / 3)
|
| 1013 | tmp.Freq = MAX_FREQ / 3;
|
| 1014 | InsertNode(p, stats, U2I((numStats + 2) >> 1));
|
| 1015 | p->MinContext->Flags = (p->MinContext->Flags & 0x10) + 0x08 * (tmp.Symbol >= 0x40);
|
| 1016 | *(p->FoundState = ONE_STATE(p->MinContext)) = tmp;
|
| 1017 | return;
|
| 1018 | }
|
| 1019 | n0 = (numStats + 2) >> 1;
|
| 1020 | n1 = (p->MinContext->NumStats + 2) >> 1;
|
| 1021 | if (n0 != n1)
|
| 1022 | p->MinContext->Stats = STATS_REF(ShrinkUnits(p, stats, n0, n1));
|
| 1023 | p->MinContext->Flags &= ~0x08;
|
| 1024 | p->MinContext->Flags |= 0x08 * ((s = STATS(p->MinContext))->Symbol >= 0x40);
|
| 1025 | i = p->MinContext->NumStats;
|
| 1026 | do { p->MinContext->Flags |= 0x08*((++s)->Symbol >= 0x40); } while (--i);
|
| 1027 | }
|
| 1028 | p->MinContext->SummFreq = (UInt16)(sumFreq + escFreq - (escFreq >> 1));
|
| 1029 | p->MinContext->Flags |= 0x4;
|
| 1030 | p->FoundState = STATS(p->MinContext);
|
| 1031 | }
|
| 1032 | |
| 1033 | CPpmd_See *Ppmd8_MakeEscFreq(CPpmd8 *p, unsigned numMasked1, UInt32 *escFreq)
|
| 1034 | {
|
| 1035 | CPpmd_See *see;
|
| 1036 | if (p->MinContext->NumStats != 0xFF)
|
| 1037 | {
|
| 1038 | see = p->See[p->NS2Indx[p->MinContext->NumStats + 2] - 3] +
|
| 1039 | (p->MinContext->SummFreq > 11 * ((unsigned)p->MinContext->NumStats + 1)) +
|
| 1040 | 2 * (2 * (unsigned)p->MinContext->NumStats <
|
| 1041 | ((unsigned)SUFFIX(p->MinContext)->NumStats + numMasked1)) +
|
| 1042 | p->MinContext->Flags;
|
| 1043 | {
|
| 1044 | unsigned r = (see->Summ >> see->Shift);
|
| 1045 | see->Summ = (UInt16)(see->Summ - r);
|
| 1046 | *escFreq = r + (r == 0);
|
| 1047 | }
|
| 1048 | }
|
| 1049 | else
|
| 1050 | {
|
| 1051 | see = &p->DummySee;
|
| 1052 | *escFreq = 1;
|
| 1053 | }
|
| 1054 | return see;
|
| 1055 | }
|
| 1056 | |
| 1057 | static void NextContext(CPpmd8 *p)
|
| 1058 | {
|
| 1059 | CTX_PTR c = CTX(SUCCESSOR(p->FoundState));
|
| 1060 | if (p->OrderFall == 0 && (Byte *)c >= p->UnitsStart)
|
| 1061 | p->MinContext = p->MaxContext = c;
|
| 1062 | else
|
| 1063 | {
|
| 1064 | UpdateModel(p);
|
| 1065 | p->MinContext = p->MaxContext;
|
| 1066 | }
|
| 1067 | }
|
| 1068 | |
| 1069 | void Ppmd8_Update1(CPpmd8 *p)
|
| 1070 | {
|
| 1071 | CPpmd_State *s = p->FoundState;
|
| 1072 | s->Freq += 4;
|
| 1073 | p->MinContext->SummFreq += 4;
|
| 1074 | if (s[0].Freq > s[-1].Freq)
|
| 1075 | {
|
| 1076 | SwapStates(&s[0], &s[-1]);
|
| 1077 | p->FoundState = --s;
|
| 1078 | if (s->Freq > MAX_FREQ)
|
| 1079 | Rescale(p);
|
| 1080 | }
|
| 1081 | NextContext(p);
|
| 1082 | }
|
| 1083 | |
| 1084 | void Ppmd8_Update1_0(CPpmd8 *p)
|
| 1085 | {
|
| 1086 | p->PrevSuccess = (2 * p->FoundState->Freq >= p->MinContext->SummFreq);
|
| 1087 | p->RunLength += p->PrevSuccess;
|
| 1088 | p->MinContext->SummFreq += 4;
|
| 1089 | if ((p->FoundState->Freq += 4) > MAX_FREQ)
|
| 1090 | Rescale(p);
|
| 1091 | NextContext(p);
|
| 1092 | }
|
| 1093 | |
| 1094 | void Ppmd8_UpdateBin(CPpmd8 *p)
|
| 1095 | {
|
| 1096 | p->FoundState->Freq = (Byte)(p->FoundState->Freq + (p->FoundState->Freq < 196));
|
| 1097 | p->PrevSuccess = 1;
|
| 1098 | p->RunLength++;
|
| 1099 | NextContext(p);
|
| 1100 | }
|
| 1101 | |
| 1102 | void Ppmd8_Update2(CPpmd8 *p)
|
| 1103 | {
|
| 1104 | p->MinContext->SummFreq += 4;
|
| 1105 | if ((p->FoundState->Freq += 4) > MAX_FREQ)
|
| 1106 | Rescale(p);
|
| 1107 | p->RunLength = p->InitRL;
|
| 1108 | UpdateModel(p);
|
| 1109 | p->MinContext = p->MaxContext;
|
| 1110 | }
|
| 1111 | |
| 1112 | /* H->I changes:
|
| 1113 | NS2Indx
|
| 1114 | GlewCount, and Glue method
|
| 1115 | BinSum
|
| 1116 | See / EscFreq
|
| 1117 | CreateSuccessors updates more suffix contexts
|
| 1118 | UpdateModel consts.
|
| 1119 | PrevSuccess Update
|
| 1120 | */
|
src/lib/lib7z/LzmaEnc.c
| r0 | r14440 | |
| 1 | /* LzmaEnc.c -- LZMA Encoder
|
| 2 | 2011-01-27 : Igor Pavlov : Public domain */
|
| 3 | |
| 4 | #include <string.h>
|
| 5 | |
| 6 | /* #define SHOW_STAT */
|
| 7 | /* #define SHOW_STAT2 */
|
| 8 | |
| 9 | #if defined(SHOW_STAT) || defined(SHOW_STAT2)
|
| 10 | #include <stdio.h>
|
| 11 | #endif
|
| 12 | |
| 13 | #include "LzmaEnc.h"
|
| 14 | |
| 15 | #include "LzFind.h"
|
| 16 | #ifndef _7ZIP_ST
|
| 17 | #include "LzFindMt.h"
|
| 18 | #endif
|
| 19 | |
| 20 | #ifdef SHOW_STAT
|
| 21 | static int ttt = 0;
|
| 22 | #endif
|
| 23 | |
| 24 | #define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
|
| 25 | |
| 26 | #define kBlockSize (9 << 10)
|
| 27 | #define kUnpackBlockSize (1 << 18)
|
| 28 | #define kMatchArraySize (1 << 21)
|
| 29 | #define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
|
| 30 | |
| 31 | #define kNumMaxDirectBits (31)
|
| 32 | |
| 33 | #define kNumTopBits 24
|
| 34 | #define kTopValue ((UInt32)1 << kNumTopBits)
|
| 35 | |
| 36 | #define kNumBitModelTotalBits 11
|
| 37 | #define kBitModelTotal (1 << kNumBitModelTotalBits)
|
| 38 | #define kNumMoveBits 5
|
| 39 | #define kProbInitValue (kBitModelTotal >> 1)
|
| 40 | |
| 41 | #define kNumMoveReducingBits 4
|
| 42 | #define kNumBitPriceShiftBits 4
|
| 43 | #define kBitPrice (1 << kNumBitPriceShiftBits)
|
| 44 | |
| 45 | void LzmaEncProps_Init(CLzmaEncProps *p)
|
| 46 | {
|
| 47 | p->level = 5;
|
| 48 | p->dictSize = p->mc = 0;
|
| 49 | p->reduceSize = (UInt32)(Int32)-1;
|
| 50 | p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
|
| 51 | p->writeEndMark = 0;
|
| 52 | }
|
| 53 | |
| 54 | void LzmaEncProps_Normalize(CLzmaEncProps *p)
|
| 55 | {
|
| 56 | int level = p->level;
|
| 57 | if (level < 0) level = 5;
|
| 58 | p->level = level;
|
| 59 | if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
|
| 60 | if (p->dictSize > p->reduceSize)
|
| 61 | {
|
| 62 | unsigned i;
|
| 63 | for (i = 15; i <= 30; i++)
|
| 64 | {
|
| 65 | if (p->reduceSize <= ((UInt32)2 << i)) { p->dictSize = ((UInt32)2 << i); break; }
|
| 66 | if (p->reduceSize <= ((UInt32)3 << i)) { p->dictSize = ((UInt32)3 << i); break; }
|
| 67 | }
|
| 68 | }
|
| 69 | if (p->lc < 0) p->lc = 3;
|
| 70 | if (p->lp < 0) p->lp = 0;
|
| 71 | if (p->pb < 0) p->pb = 2;
|
| 72 | if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
|
| 73 | if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
|
| 74 | if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
|
| 75 | if (p->numHashBytes < 0) p->numHashBytes = 4;
|
| 76 | if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
|
| 77 | if (p->numThreads < 0)
|
| 78 | p->numThreads =
|
| 79 | #ifndef _7ZIP_ST
|
| 80 | ((p->btMode && p->algo) ? 2 : 1);
|
| 81 | #else
|
| 82 | 1;
|
| 83 | #endif
|
| 84 | }
|
| 85 | |
| 86 | UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
|
| 87 | {
|
| 88 | CLzmaEncProps props = *props2;
|
| 89 | LzmaEncProps_Normalize(&props);
|
| 90 | return props.dictSize;
|
| 91 | }
|
| 92 | |
| 93 | /* #define LZMA_LOG_BSR */
|
| 94 | /* Define it for Intel's CPU */
|
| 95 | |
| 96 | |
| 97 | #ifdef LZMA_LOG_BSR
|
| 98 | |
| 99 | #define kDicLogSizeMaxCompress 30
|
| 100 | |
| 101 | #define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
|
| 102 | |
| 103 | UInt32 GetPosSlot1(UInt32 pos)
|
| 104 | {
|
| 105 | UInt32 res;
|
| 106 | BSR2_RET(pos, res);
|
| 107 | return res;
|
| 108 | }
|
| 109 | #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
|
| 110 | #define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
|
| 111 | |
| 112 | #else
|
| 113 | |
| 114 | #define kNumLogBits (9 + (int)sizeof(size_t) / 2)
|
| 115 | #define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
|
| 116 | |
| 117 | void LzmaEnc_FastPosInit(Byte *g_FastPos)
|
| 118 | {
|
| 119 | int c = 2, slotFast;
|
| 120 | g_FastPos[0] = 0;
|
| 121 | g_FastPos[1] = 1;
|
| 122 | |
| 123 | for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
|
| 124 | {
|
| 125 | UInt32 k = (1 << ((slotFast >> 1) - 1));
|
| 126 | UInt32 j;
|
| 127 | for (j = 0; j < k; j++, c++)
|
| 128 | g_FastPos[c] = (Byte)slotFast;
|
| 129 | }
|
| 130 | }
|
| 131 | |
| 132 | #define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
|
| 133 | (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
|
| 134 | res = p->g_FastPos[pos >> i] + (i * 2); }
|
| 135 | /*
|
| 136 | #define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
|
| 137 | p->g_FastPos[pos >> 6] + 12 : \
|
| 138 | p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
|
| 139 | */
|
| 140 | |
| 141 | #define GetPosSlot1(pos) p->g_FastPos[pos]
|
| 142 | #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
|
| 143 | #define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
|
| 144 | |
| 145 | #endif
|
| 146 | |
| 147 | |
| 148 | #define LZMA_NUM_REPS 4
|
| 149 | |
| 150 | typedef unsigned CState;
|
| 151 | |
| 152 | typedef struct
|
| 153 | {
|
| 154 | UInt32 price;
|
| 155 | |
| 156 | CState state;
|
| 157 | int prev1IsChar;
|
| 158 | int prev2;
|
| 159 | |
| 160 | UInt32 posPrev2;
|
| 161 | UInt32 backPrev2;
|
| 162 | |
| 163 | UInt32 posPrev;
|
| 164 | UInt32 backPrev;
|
| 165 | UInt32 backs[LZMA_NUM_REPS];
|
| 166 | } COptimal;
|
| 167 | |
| 168 | #define kNumOpts (1 << 12)
|
| 169 | |
| 170 | #define kNumLenToPosStates 4
|
| 171 | #define kNumPosSlotBits 6
|
| 172 | #define kDicLogSizeMin 0
|
| 173 | #define kDicLogSizeMax 32
|
| 174 | #define kDistTableSizeMax (kDicLogSizeMax * 2)
|
| 175 | |
| 176 | |
| 177 | #define kNumAlignBits 4
|
| 178 | #define kAlignTableSize (1 << kNumAlignBits)
|
| 179 | #define kAlignMask (kAlignTableSize - 1)
|
| 180 | |
| 181 | #define kStartPosModelIndex 4
|
| 182 | #define kEndPosModelIndex 14
|
| 183 | #define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
|
| 184 | |
| 185 | #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
|
| 186 | |
| 187 | #ifdef _LZMA_PROB32
|
| 188 | #define CLzmaProb UInt32
|
| 189 | #else
|
| 190 | #define CLzmaProb UInt16
|
| 191 | #endif
|
| 192 | |
| 193 | #define LZMA_PB_MAX 4
|
| 194 | #define LZMA_LC_MAX 8
|
| 195 | #define LZMA_LP_MAX 4
|
| 196 | |
| 197 | #define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
|
| 198 | |
| 199 | |
| 200 | #define kLenNumLowBits 3
|
| 201 | #define kLenNumLowSymbols (1 << kLenNumLowBits)
|
| 202 | #define kLenNumMidBits 3
|
| 203 | #define kLenNumMidSymbols (1 << kLenNumMidBits)
|
| 204 | #define kLenNumHighBits 8
|
| 205 | #define kLenNumHighSymbols (1 << kLenNumHighBits)
|
| 206 | |
| 207 | #define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
|
| 208 | |
| 209 | #define LZMA_MATCH_LEN_MIN 2
|
| 210 | #define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
|
| 211 | |
| 212 | #define kNumStates 12
|
| 213 | |
| 214 | typedef struct
|
| 215 | {
|
| 216 | CLzmaProb choice;
|
| 217 | CLzmaProb choice2;
|
| 218 | CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
|
| 219 | CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
|
| 220 | CLzmaProb high[kLenNumHighSymbols];
|
| 221 | } CLenEnc;
|
| 222 | |
| 223 | typedef struct
|
| 224 | {
|
| 225 | CLenEnc p;
|
| 226 | UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
|
| 227 | UInt32 tableSize;
|
| 228 | UInt32 counters[LZMA_NUM_PB_STATES_MAX];
|
| 229 | } CLenPriceEnc;
|
| 230 | |
| 231 | typedef struct
|
| 232 | {
|
| 233 | UInt32 range;
|
| 234 | Byte cache;
|
| 235 | UInt64 low;
|
| 236 | UInt64 cacheSize;
|
| 237 | Byte *buf;
|
| 238 | Byte *bufLim;
|
| 239 | Byte *bufBase;
|
| 240 | ISeqOutStream *outStream;
|
| 241 | UInt64 processed;
|
| 242 | SRes res;
|
| 243 | } CRangeEnc;
|
| 244 | |
| 245 | typedef struct
|
| 246 | {
|
| 247 | CLzmaProb *litProbs;
|
| 248 | |
| 249 | CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
|
| 250 | CLzmaProb isRep[kNumStates];
|
| 251 | CLzmaProb isRepG0[kNumStates];
|
| 252 | CLzmaProb isRepG1[kNumStates];
|
| 253 | CLzmaProb isRepG2[kNumStates];
|
| 254 | CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
|
| 255 | |
| 256 | CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
|
| 257 | CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
|
| 258 | CLzmaProb posAlignEncoder[1 << kNumAlignBits];
|
| 259 | |
| 260 | CLenPriceEnc lenEnc;
|
| 261 | CLenPriceEnc repLenEnc;
|
| 262 | |
| 263 | UInt32 reps[LZMA_NUM_REPS];
|
| 264 | UInt32 state;
|
| 265 | } CSaveState;
|
| 266 | |
| 267 | typedef struct
|
| 268 | {
|
| 269 | IMatchFinder matchFinder;
|
| 270 | void *matchFinderObj;
|
| 271 | |
| 272 | #ifndef _7ZIP_ST
|
| 273 | Bool mtMode;
|
| 274 | CMatchFinderMt matchFinderMt;
|
| 275 | #endif
|
| 276 | |
| 277 | CMatchFinder matchFinderBase;
|
| 278 | |
| 279 | #ifndef _7ZIP_ST
|
| 280 | Byte pad[128];
|
| 281 | #endif
|
| 282 | |
| 283 | UInt32 optimumEndIndex;
|
| 284 | UInt32 optimumCurrentIndex;
|
| 285 | |
| 286 | UInt32 longestMatchLength;
|
| 287 | UInt32 numPairs;
|
| 288 | UInt32 numAvail;
|
| 289 | COptimal opt[kNumOpts];
|
| 290 | |
| 291 | #ifndef LZMA_LOG_BSR
|
| 292 | Byte g_FastPos[1 << kNumLogBits];
|
| 293 | #endif
|
| 294 | |
| 295 | UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
|
| 296 | UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
|
| 297 | UInt32 numFastBytes;
|
| 298 | UInt32 additionalOffset;
|
| 299 | UInt32 reps[LZMA_NUM_REPS];
|
| 300 | UInt32 state;
|
| 301 | |
| 302 | UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
|
| 303 | UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
|
| 304 | UInt32 alignPrices[kAlignTableSize];
|
| 305 | UInt32 alignPriceCount;
|
| 306 | |
| 307 | UInt32 distTableSize;
|
| 308 | |
| 309 | unsigned lc, lp, pb;
|
| 310 | unsigned lpMask, pbMask;
|
| 311 | |
| 312 | CLzmaProb *litProbs;
|
| 313 | |
| 314 | CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
|
| 315 | CLzmaProb isRep[kNumStates];
|
| 316 | CLzmaProb isRepG0[kNumStates];
|
| 317 | CLzmaProb isRepG1[kNumStates];
|
| 318 | CLzmaProb isRepG2[kNumStates];
|
| 319 | CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
|
| 320 | |
| 321 | CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
|
| 322 | CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
|
| 323 | CLzmaProb posAlignEncoder[1 << kNumAlignBits];
|
| 324 | |
| 325 | CLenPriceEnc lenEnc;
|
| 326 | CLenPriceEnc repLenEnc;
|
| 327 | |
| 328 | unsigned lclp;
|
| 329 | |
| 330 | Bool fastMode;
|
| 331 | |
| 332 | CRangeEnc rc;
|
| 333 | |
| 334 | Bool writeEndMark;
|
| 335 | UInt64 nowPos64;
|
| 336 | UInt32 matchPriceCount;
|
| 337 | Bool finished;
|
| 338 | Bool multiThread;
|
| 339 | |
| 340 | SRes result;
|
| 341 | UInt32 dictSize;
|
| 342 | |
| 343 | int needInit;
|
| 344 | |
| 345 | CSaveState saveState;
|
| 346 | } CLzmaEnc;
|
| 347 | |
| 348 | void LzmaEnc_SaveState(CLzmaEncHandle pp)
|
| 349 | {
|
| 350 | CLzmaEnc *p = (CLzmaEnc *)pp;
|
| 351 | CSaveState *dest = &p->saveState;
|
| 352 | int i;
|
| 353 | dest->lenEnc = p->lenEnc;
|
| 354 | dest->repLenEnc = p->repLenEnc;
|
| 355 | dest->state = p->state;
|
| 356 | |
| 357 | for (i = 0; i < kNumStates; i++)
|
| 358 | {
|
| 359 | memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
|
| 360 | memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
|
| 361 | }
|
| 362 | for (i = 0; i < kNumLenToPosStates; i++)
|
| 363 | memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
|
| 364 | memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
|
| 365 | memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
|
| 366 | memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
|
| 367 | memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
|
| 368 | memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
|
| 369 | memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
|
| 370 | memcpy(dest->reps, p->reps, sizeof(p->reps));
|
| 371 | memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));
|
| 372 | }
|
| 373 | |
| 374 | void LzmaEnc_RestoreState(CLzmaEncHandle pp)
|
| 375 | {
|
| 376 | CLzmaEnc *dest = (CLzmaEnc *)pp;
|
| 377 | const CSaveState *p = &dest->saveState;
|
| 378 | int i;
|
| 379 | dest->lenEnc = p->lenEnc;
|
| 380 | dest->repLenEnc = p->repLenEnc;
|
| 381 | dest->state = p->state;
|
| 382 | |
| 383 | for (i = 0; i < kNumStates; i++)
|
| 384 | {
|
| 385 | memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
|
| 386 | memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
|
| 387 | }
|
| 388 | for (i = 0; i < kNumLenToPosStates; i++)
|
| 389 | memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
|
| 390 | memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
|
| 391 | memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
|
| 392 | memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
|
| 393 | memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
|
| 394 | memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
|
| 395 | memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
|
| 396 | memcpy(dest->reps, p->reps, sizeof(p->reps));
|
| 397 | memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));
|
| 398 | }
|
| 399 | |
| 400 | SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
|
| 401 | {
|
| 402 | CLzmaEnc *p = (CLzmaEnc *)pp;
|
| 403 | CLzmaEncProps props = *props2;
|
| 404 | LzmaEncProps_Normalize(&props);
|
| 405 | |
| 406 | if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
|
| 407 | props.dictSize > ((UInt32)1 << kDicLogSizeMaxCompress) || props.dictSize > ((UInt32)1 << 30))
|
| 408 | return SZ_ERROR_PARAM;
|
| 409 | p->dictSize = props.dictSize;
|
| 410 | {
|
| 411 | unsigned fb = props.fb;
|
| 412 | if (fb < 5)
|
| 413 | fb = 5;
|
| 414 | if (fb > LZMA_MATCH_LEN_MAX)
|
| 415 | fb = LZMA_MATCH_LEN_MAX;
|
| 416 | p->numFastBytes = fb;
|
| 417 | }
|
| 418 | p->lc = props.lc;
|
| 419 | p->lp = props.lp;
|
| 420 | p->pb = props.pb;
|
| 421 | p->fastMode = (props.algo == 0);
|
| 422 | p->matchFinderBase.btMode = props.btMode;
|
| 423 | {
|
| 424 | UInt32 numHashBytes = 4;
|
| 425 | if (props.btMode)
|
| 426 | {
|
| 427 | if (props.numHashBytes < 2)
|
| 428 | numHashBytes = 2;
|
| 429 | else if (props.numHashBytes < 4)
|
| 430 | numHashBytes = props.numHashBytes;
|
| 431 | }
|
| 432 | p->matchFinderBase.numHashBytes = numHashBytes;
|
| 433 | }
|
| 434 | |
| 435 | p->matchFinderBase.cutValue = props.mc;
|
| 436 | |
| 437 | p->writeEndMark = props.writeEndMark;
|
| 438 | |
| 439 | #ifndef _7ZIP_ST
|
| 440 | /*
|
| 441 | if (newMultiThread != _multiThread)
|
| 442 | {
|
| 443 | ReleaseMatchFinder();
|
| 444 | _multiThread = newMultiThread;
|
| 445 | }
|
| 446 | */
|
| 447 | p->multiThread = (props.numThreads > 1);
|
| 448 | #endif
|
| 449 | |
| 450 | return SZ_OK;
|
| 451 | }
|
| 452 | |
| 453 | static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
|
| 454 | static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
|
| 455 | static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
|
| 456 | static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
|
| 457 | |
| 458 | #define IsCharState(s) ((s) < 7)
|
| 459 | |
| 460 | #define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
|
| 461 | |
| 462 | #define kInfinityPrice (1 << 30)
|
| 463 | |
| 464 | static void RangeEnc_Construct(CRangeEnc *p)
|
| 465 | {
|
| 466 | p->outStream = 0;
|
| 467 | p->bufBase = 0;
|
| 468 | }
|
| 469 | |
| 470 | #define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
|
| 471 | |
| 472 | #define RC_BUF_SIZE (1 << 16)
|
| 473 | static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
|
| 474 | {
|
| 475 | if (p->bufBase == 0)
|
| 476 | {
|
| 477 | p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
|
| 478 | if (p->bufBase == 0)
|
| 479 | return 0;
|
| 480 | p->bufLim = p->bufBase + RC_BUF_SIZE;
|
| 481 | }
|
| 482 | return 1;
|
| 483 | }
|
| 484 | |
| 485 | static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
|
| 486 | {
|
| 487 | alloc->Free(alloc, p->bufBase);
|
| 488 | p->bufBase = 0;
|
| 489 | }
|
| 490 | |
| 491 | static void RangeEnc_Init(CRangeEnc *p)
|
| 492 | {
|
| 493 | /* Stream.Init(); */
|
| 494 | p->low = 0;
|
| 495 | p->range = 0xFFFFFFFF;
|
| 496 | p->cacheSize = 1;
|
| 497 | p->cache = 0;
|
| 498 | |
| 499 | p->buf = p->bufBase;
|
| 500 | |
| 501 | p->processed = 0;
|
| 502 | p->res = SZ_OK;
|
| 503 | }
|
| 504 | |
| 505 | static void RangeEnc_FlushStream(CRangeEnc *p)
|
| 506 | {
|
| 507 | size_t num;
|
| 508 | if (p->res != SZ_OK)
|
| 509 | return;
|
| 510 | num = p->buf - p->bufBase;
|
| 511 | if (num != p->outStream->Write(p->outStream, p->bufBase, num))
|
| 512 | p->res = SZ_ERROR_WRITE;
|
| 513 | p->processed += num;
|
| 514 | p->buf = p->bufBase;
|
| 515 | }
|
| 516 | |
| 517 | static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
|
| 518 | {
|
| 519 | if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
|
| 520 | {
|
| 521 | Byte temp = p->cache;
|
| 522 | do
|
| 523 | {
|
| 524 | Byte *buf = p->buf;
|
| 525 | *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
|
| 526 | p->buf = buf;
|
| 527 | if (buf == p->bufLim)
|
| 528 | RangeEnc_FlushStream(p);
|
| 529 | temp = 0xFF;
|
| 530 | }
|
| 531 | while (--p->cacheSize != 0);
|
| 532 | p->cache = (Byte)((UInt32)p->low >> 24);
|
| 533 | }
|
| 534 | p->cacheSize++;
|
| 535 | p->low = (UInt32)p->low << 8;
|
| 536 | }
|
| 537 | |
| 538 | static void RangeEnc_FlushData(CRangeEnc *p)
|
| 539 | {
|
| 540 | int i;
|
| 541 | for (i = 0; i < 5; i++)
|
| 542 | RangeEnc_ShiftLow(p);
|
| 543 | }
|
| 544 | |
| 545 | static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
|
| 546 | {
|
| 547 | do
|
| 548 | {
|
| 549 | p->range >>= 1;
|
| 550 | p->low += p->range & (0 - ((value >> --numBits) & 1));
|
| 551 | if (p->range < kTopValue)
|
| 552 | {
|
| 553 | p->range <<= 8;
|
| 554 | RangeEnc_ShiftLow(p);
|
| 555 | }
|
| 556 | }
|
| 557 | while (numBits != 0);
|
| 558 | }
|
| 559 | |
| 560 | static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
|
| 561 | {
|
| 562 | UInt32 ttt = *prob;
|
| 563 | UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
|
| 564 | if (symbol == 0)
|
| 565 | {
|
| 566 | p->range = newBound;
|
| 567 | ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
|
| 568 | }
|
| 569 | else
|
| 570 | {
|
| 571 | p->low += newBound;
|
| 572 | p->range -= newBound;
|
| 573 | ttt -= ttt >> kNumMoveBits;
|
| 574 | }
|
| 575 | *prob = (CLzmaProb)ttt;
|
| 576 | if (p->range < kTopValue)
|
| 577 | {
|
| 578 | p->range <<= 8;
|
| 579 | RangeEnc_ShiftLow(p);
|
| 580 | }
|
| 581 | }
|
| 582 | |
| 583 | static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
|
| 584 | {
|
| 585 | symbol |= 0x100;
|
| 586 | do
|
| 587 | {
|
| 588 | RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
|
| 589 | symbol <<= 1;
|
| 590 | }
|
| 591 | while (symbol < 0x10000);
|
| 592 | }
|
| 593 | |
| 594 | static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
|
| 595 | {
|
| 596 | UInt32 offs = 0x100;
|
| 597 | symbol |= 0x100;
|
| 598 | do
|
| 599 | {
|
| 600 | matchByte <<= 1;
|
| 601 | RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
|
| 602 | symbol <<= 1;
|
| 603 | offs &= ~(matchByte ^ symbol);
|
| 604 | }
|
| 605 | while (symbol < 0x10000);
|
| 606 | }
|
| 607 | |
| 608 | void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
|
| 609 | {
|
| 610 | UInt32 i;
|
| 611 | for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
|
| 612 | {
|
| 613 | const int kCyclesBits = kNumBitPriceShiftBits;
|
| 614 | UInt32 w = i;
|
| 615 | UInt32 bitCount = 0;
|
| 616 | int j;
|
| 617 | for (j = 0; j < kCyclesBits; j++)
|
| 618 | {
|
| 619 | w = w * w;
|
| 620 | bitCount <<= 1;
|
| 621 | while (w >= ((UInt32)1 << 16))
|
| 622 | {
|
| 623 | w >>= 1;
|
| 624 | bitCount++;
|
| 625 | }
|
| 626 | }
|
| 627 | ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
|
| 628 | }
|
| 629 | }
|
| 630 | |
| 631 | |
| 632 | #define GET_PRICE(prob, symbol) \
|
| 633 | p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
|
| 634 | |
| 635 | #define GET_PRICEa(prob, symbol) \
|
| 636 | ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
|
| 637 | |
| 638 | #define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
|
| 639 | #define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
|
| 640 | |
| 641 | #define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
|
| 642 | #define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
|
| 643 | |
| 644 | static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
|
| 645 | {
|
| 646 | UInt32 price = 0;
|
| 647 | symbol |= 0x100;
|
| 648 | do
|
| 649 | {
|
| 650 | price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
|
| 651 | symbol <<= 1;
|
| 652 | }
|
| 653 | while (symbol < 0x10000);
|
| 654 | return price;
|
| 655 | }
|
| 656 | |
| 657 | static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
|
| 658 | {
|
| 659 | UInt32 price = 0;
|
| 660 | UInt32 offs = 0x100;
|
| 661 | symbol |= 0x100;
|
| 662 | do
|
| 663 | {
|
| 664 | matchByte <<= 1;
|
| 665 | price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
|
| 666 | symbol <<= 1;
|
| 667 | offs &= ~(matchByte ^ symbol);
|
| 668 | }
|
| 669 | while (symbol < 0x10000);
|
| 670 | return price;
|
| 671 | }
|
| 672 | |
| 673 | |
| 674 | static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
|
| 675 | {
|
| 676 | UInt32 m = 1;
|
| 677 | int i;
|
| 678 | for (i = numBitLevels; i != 0;)
|
| 679 | {
|
| 680 | UInt32 bit;
|
| 681 | i--;
|
| 682 | bit = (symbol >> i) & 1;
|
| 683 | RangeEnc_EncodeBit(rc, probs + m, bit);
|
| 684 | m = (m << 1) | bit;
|
| 685 | }
|
| 686 | }
|
| 687 | |
| 688 | static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
|
| 689 | {
|
| 690 | UInt32 m = 1;
|
| 691 | int i;
|
| 692 | for (i = 0; i < numBitLevels; i++)
|
| 693 | {
|
| 694 | UInt32 bit = symbol & 1;
|
| 695 | RangeEnc_EncodeBit(rc, probs + m, bit);
|
| 696 | m = (m << 1) | bit;
|
| 697 | symbol >>= 1;
|
| 698 | }
|
| 699 | }
|
| 700 | |
| 701 | static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
|
| 702 | {
|
| 703 | UInt32 price = 0;
|
| 704 | symbol |= (1 << numBitLevels);
|
| 705 | while (symbol != 1)
|
| 706 | {
|
| 707 | price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
|
| 708 | symbol >>= 1;
|
| 709 | }
|
| 710 | return price;
|
| 711 | }
|
| 712 | |
| 713 | static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
|
| 714 | {
|
| 715 | UInt32 price = 0;
|
| 716 | UInt32 m = 1;
|
| 717 | int i;
|
| 718 | for (i = numBitLevels; i != 0; i--)
|
| 719 | {
|
| 720 | UInt32 bit = symbol & 1;
|
| 721 | symbol >>= 1;
|
| 722 | price += GET_PRICEa(probs[m], bit);
|
| 723 | m = (m << 1) | bit;
|
| 724 | }
|
| 725 | return price;
|
| 726 | }
|
| 727 | |
| 728 | |
| 729 | static void LenEnc_Init(CLenEnc *p)
|
| 730 | {
|
| 731 | unsigned i;
|
| 732 | p->choice = p->choice2 = kProbInitValue;
|
| 733 | for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
|
| 734 | p->low[i] = kProbInitValue;
|
| 735 | for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
|
| 736 | p->mid[i] = kProbInitValue;
|
| 737 | for (i = 0; i < kLenNumHighSymbols; i++)
|
| 738 | p->high[i] = kProbInitValue;
|
| 739 | }
|
| 740 | |
| 741 | static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
|
| 742 | {
|
| 743 | if (symbol < kLenNumLowSymbols)
|
| 744 | {
|
| 745 | RangeEnc_EncodeBit(rc, &p->choice, 0);
|
| 746 | RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
|
| 747 | }
|
| 748 | else
|
| 749 | {
|
| 750 | RangeEnc_EncodeBit(rc, &p->choice, 1);
|
| 751 | if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
|
| 752 | {
|
| 753 | RangeEnc_EncodeBit(rc, &p->choice2, 0);
|
| 754 | RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
|
| 755 | }
|
| 756 | else
|
| 757 | {
|
| 758 | RangeEnc_EncodeBit(rc, &p->choice2, 1);
|
| 759 | RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
|
| 760 | }
|
| 761 | }
|
| 762 | }
|
| 763 | |
| 764 | static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
|
| 765 | {
|
| 766 | UInt32 a0 = GET_PRICE_0a(p->choice);
|
| 767 | UInt32 a1 = GET_PRICE_1a(p->choice);
|
| 768 | UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
|
| 769 | UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
|
| 770 | UInt32 i = 0;
|
| 771 | for (i = 0; i < kLenNumLowSymbols; i++)
|
| 772 | {
|
| 773 | if (i >= numSymbols)
|
| 774 | return;
|
| 775 | prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
|
| 776 | }
|
| 777 | for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
|
| 778 | {
|
| 779 | if (i >= numSymbols)
|
| 780 | return;
|
| 781 | prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
|
| 782 | }
|
| 783 | for (; i < numSymbols; i++)
|
| 784 | prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
|
| 785 | }
|
| 786 | |
| 787 | static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
|
| 788 | {
|
| 789 | LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
|
| 790 | p->counters[posState] = p->tableSize;
|
| 791 | }
|
| 792 | |
| 793 | static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
|
| 794 | {
|
| 795 | UInt32 posState;
|
| 796 | for (posState = 0; posState < numPosStates; posState++)
|
| 797 | LenPriceEnc_UpdateTable(p, posState, ProbPrices);
|
| 798 | }
|
| 799 | |
| 800 | static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
|
| 801 | {
|
| 802 | LenEnc_Encode(&p->p, rc, symbol, posState);
|
| 803 | if (updatePrice)
|
| 804 | if (--p->counters[posState] == 0)
|
| 805 | LenPriceEnc_UpdateTable(p, posState, ProbPrices);
|
| 806 | }
|
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | static void MovePos(CLzmaEnc *p, UInt32 num)
|
| 812 | {
|
| 813 | #ifdef SHOW_STAT
|
| 814 | ttt += num;
|
| 815 | printf("\n MovePos %d", num);
|
| 816 | #endif
|
| 817 | if (num != 0)
|
| 818 | {
|
| 819 | p->additionalOffset += num;
|
| 820 | p->matchFinder.Skip(p->matchFinderObj, num);
|
| 821 | }
|
| 822 | }
|
| 823 | |
| 824 | static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
|
| 825 | {
|
| 826 | UInt32 lenRes = 0, numPairs;
|
| 827 | p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
|
| 828 | numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);
|
| 829 | #ifdef SHOW_STAT
|
| 830 | printf("\n i = %d numPairs = %d ", ttt, numPairs / 2);
|
| 831 | ttt++;
|
| 832 | {
|
| 833 | UInt32 i;
|
| 834 | for (i = 0; i < numPairs; i += 2)
|
| 835 | printf("%2d %6d | ", p->matches[i], p->matches[i + 1]);
|
| 836 | }
|
| 837 | #endif
|
| 838 | if (numPairs > 0)
|
| 839 | {
|
| 840 | lenRes = p->matches[numPairs - 2];
|
| 841 | if (lenRes == p->numFastBytes)
|
| 842 | {
|
| 843 | const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
|
| 844 | UInt32 distance = p->matches[numPairs - 1] + 1;
|
| 845 | UInt32 numAvail = p->numAvail;
|
| 846 | if (numAvail > LZMA_MATCH_LEN_MAX)
|
| 847 | numAvail = LZMA_MATCH_LEN_MAX;
|
| 848 | {
|
| 849 | const Byte *pby2 = pby - distance;
|
| 850 | for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
|
| 851 | }
|
| 852 | }
|
| 853 | }
|
| 854 | p->additionalOffset++;
|
| 855 | *numDistancePairsRes = numPairs;
|
| 856 | return lenRes;
|
| 857 | }
|
| 858 | |
| 859 | |
| 860 | #define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
|
| 861 | #define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
|
| 862 | #define IsShortRep(p) ((p)->backPrev == 0)
|
| 863 | |
| 864 | static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
|
| 865 | {
|
| 866 | return
|
| 867 | GET_PRICE_0(p->isRepG0[state]) +
|
| 868 | GET_PRICE_0(p->isRep0Long[state][posState]);
|
| 869 | }
|
| 870 | |
| 871 | static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
|
| 872 | {
|
| 873 | UInt32 price;
|
| 874 | if (repIndex == 0)
|
| 875 | {
|
| 876 | price = GET_PRICE_0(p->isRepG0[state]);
|
| 877 | price += GET_PRICE_1(p->isRep0Long[state][posState]);
|
| 878 | }
|
| 879 | else
|
| 880 | {
|
| 881 | price = GET_PRICE_1(p->isRepG0[state]);
|
| 882 | if (repIndex == 1)
|
| 883 | price += GET_PRICE_0(p->isRepG1[state]);
|
| 884 | else
|
| 885 | {
|
| 886 | price += GET_PRICE_1(p->isRepG1[state]);
|
| 887 | price += GET_PRICE(p->isRepG2[state], repIndex - 2);
|
| 888 | }
|
| 889 | }
|
| 890 | return price;
|
| 891 | }
|
| 892 | |
| 893 | static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
|
| 894 | {
|
| 895 | return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
|
| 896 | GetPureRepPrice(p, repIndex, state, posState);
|
| 897 | }
|
| 898 | |
| 899 | static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
|
| 900 | {
|
| 901 | UInt32 posMem = p->opt[cur].posPrev;
|
| 902 | UInt32 backMem = p->opt[cur].backPrev;
|
| 903 | p->optimumEndIndex = cur;
|
| 904 | do
|
| 905 | {
|
| 906 | if (p->opt[cur].prev1IsChar)
|
| 907 | {
|
| 908 | MakeAsChar(&p->opt[posMem])
|
| 909 | p->opt[posMem].posPrev = posMem - 1;
|
| 910 | if (p->opt[cur].prev2)
|
| 911 | {
|
| 912 | p->opt[posMem - 1].prev1IsChar = False;
|
| 913 | p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
|
| 914 | p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
|
| 915 | }
|
| 916 | }
|
| 917 | {
|
| 918 | UInt32 posPrev = posMem;
|
| 919 | UInt32 backCur = backMem;
|
| 920 | |
| 921 | backMem = p->opt[posPrev].backPrev;
|
| 922 | posMem = p->opt[posPrev].posPrev;
|
| 923 | |
| 924 | p->opt[posPrev].backPrev = backCur;
|
| 925 | p->opt[posPrev].posPrev = cur;
|
| 926 | cur = posPrev;
|
| 927 | }
|
| 928 | }
|
| 929 | while (cur != 0);
|
| 930 | *backRes = p->opt[0].backPrev;
|
| 931 | p->optimumCurrentIndex = p->opt[0].posPrev;
|
| 932 | return p->optimumCurrentIndex;
|
| 933 | }
|
| 934 | |
| 935 | #define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
|
| 936 | |
| 937 | static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
|
| 938 | {
|
| 939 | UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur;
|
| 940 | UInt32 matchPrice, repMatchPrice, normalMatchPrice;
|
| 941 | UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS];
|
| 942 | UInt32 *matches;
|
| 943 | const Byte *data;
|
| 944 | Byte curByte, matchByte;
|
| 945 | if (p->optimumEndIndex != p->optimumCurrentIndex)
|
| 946 | {
|
| 947 | const COptimal *opt = &p->opt[p->optimumCurrentIndex];
|
| 948 | UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
|
| 949 | *backRes = opt->backPrev;
|
| 950 | p->optimumCurrentIndex = opt->posPrev;
|
| 951 | return lenRes;
|
| 952 | }
|
| 953 | p->optimumCurrentIndex = p->optimumEndIndex = 0;
|
| 954 | |
| 955 | if (p->additionalOffset == 0)
|
| 956 | mainLen = ReadMatchDistances(p, &numPairs);
|
| 957 | else
|
| 958 | {
|
| 959 | mainLen = p->longestMatchLength;
|
| 960 | numPairs = p->numPairs;
|
| 961 | }
|
| 962 | |
| 963 | numAvail = p->numAvail;
|
| 964 | if (numAvail < 2)
|
| 965 | {
|
| 966 | *backRes = (UInt32)(-1);
|
| 967 | return 1;
|
| 968 | }
|
| 969 | if (numAvail > LZMA_MATCH_LEN_MAX)
|
| 970 | numAvail = LZMA_MATCH_LEN_MAX;
|
| 971 | |
| 972 | data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
|
| 973 | repMaxIndex = 0;
|
| 974 | for (i = 0; i < LZMA_NUM_REPS; i++)
|
| 975 | {
|
| 976 | UInt32 lenTest;
|
| 977 | const Byte *data2;
|
| 978 | reps[i] = p->reps[i];
|
| 979 | data2 = data - (reps[i] + 1);
|
| 980 | if (data[0] != data2[0] || data[1] != data2[1])
|
| 981 | {
|
| 982 | repLens[i] = 0;
|
| 983 | continue;
|
| 984 | }
|
| 985 | for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
|
| 986 | repLens[i] = lenTest;
|
| 987 | if (lenTest > repLens[repMaxIndex])
|
| 988 | repMaxIndex = i;
|
| 989 | }
|
| 990 | if (repLens[repMaxIndex] >= p->numFastBytes)
|
| 991 | {
|
| 992 | UInt32 lenRes;
|
| 993 | *backRes = repMaxIndex;
|
| 994 | lenRes = repLens[repMaxIndex];
|
| 995 | MovePos(p, lenRes - 1);
|
| 996 | return lenRes;
|
| 997 | }
|
| 998 | |
| 999 | matches = p->matches;
|
| 1000 | if (mainLen >= p->numFastBytes)
|
| 1001 | {
|
| 1002 | *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
|
| 1003 | MovePos(p, mainLen - 1);
|
| 1004 | return mainLen;
|
| 1005 | }
|
| 1006 | curByte = *data;
|
| 1007 | matchByte = *(data - (reps[0] + 1));
|
| 1008 | |
| 1009 | if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2)
|
| 1010 | {
|
| 1011 | *backRes = (UInt32)-1;
|
| 1012 | return 1;
|
| 1013 | }
|
| 1014 | |
| 1015 | p->opt[0].state = (CState)p->state;
|
| 1016 | |
| 1017 | posState = (position & p->pbMask);
|
| 1018 | |
| 1019 | {
|
| 1020 | const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
|
| 1021 | p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
|
| 1022 | (!IsCharState(p->state) ?
|
| 1023 | LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
|
| 1024 | LitEnc_GetPrice(probs, curByte, p->ProbPrices));
|
| 1025 | }
|
| 1026 | |
| 1027 | MakeAsChar(&p->opt[1]);
|
| 1028 | |
| 1029 | matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
|
| 1030 | repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
|
| 1031 | |
| 1032 | if (matchByte == curByte)
|
| 1033 | {
|
| 1034 | UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
|
| 1035 | if (shortRepPrice < p->opt[1].price)
|
| 1036 | {
|
| 1037 | p->opt[1].price = shortRepPrice;
|
| 1038 | MakeAsShortRep(&p->opt[1]);
|
| 1039 | }
|
| 1040 | }
|
| 1041 | lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]);
|
| 1042 | |
| 1043 | if (lenEnd < 2)
|
| 1044 | {
|
| 1045 | *backRes = p->opt[1].backPrev;
|
| 1046 | return 1;
|
| 1047 | }
|
| 1048 | |
| 1049 | p->opt[1].posPrev = 0;
|
| 1050 | for (i = 0; i < LZMA_NUM_REPS; i++)
|
| 1051 | p->opt[0].backs[i] = reps[i];
|
| 1052 | |
| 1053 | len = lenEnd;
|
| 1054 | do
|
| 1055 | p->opt[len--].price = kInfinityPrice;
|
| 1056 | while (len >= 2);
|
| 1057 | |
| 1058 | for (i = 0; i < LZMA_NUM_REPS; i++)
|
| 1059 | {
|
| 1060 | UInt32 repLen = repLens[i];
|
| 1061 | UInt32 price;
|
| 1062 | if (repLen < 2)
|
| 1063 | continue;
|
| 1064 | price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
|
| 1065 | do
|
| 1066 | {
|
| 1067 | UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
|
| 1068 | COptimal *opt = &p->opt[repLen];
|
| 1069 | if (curAndLenPrice < opt->price)
|
| 1070 | {
|
| 1071 | opt->price = curAndLenPrice;
|
| 1072 | opt->posPrev = 0;
|
| 1073 | opt->backPrev = i;
|
| 1074 | opt->prev1IsChar = False;
|
| 1075 | }
|
| 1076 | }
|
| 1077 | while (--repLen >= 2);
|
| 1078 | }
|
| 1079 | |
| 1080 | normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
|
| 1081 | |
| 1082 | len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
|
| 1083 | if (len <= mainLen)
|
| 1084 | {
|
| 1085 | UInt32 offs = 0;
|
| 1086 | while (len > matches[offs])
|
| 1087 | offs += 2;
|
| 1088 | for (; ; len++)
|
| 1089 | {
|
| 1090 | COptimal *opt;
|
| 1091 | UInt32 distance = matches[offs + 1];
|
| 1092 | |
| 1093 | UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
|
| 1094 | UInt32 lenToPosState = GetLenToPosState(len);
|
| 1095 | if (distance < kNumFullDistances)
|
| 1096 | curAndLenPrice += p->distancesPrices[lenToPosState][distance];
|
| 1097 | else
|
| 1098 | {
|
| 1099 | UInt32 slot;
|
| 1100 | GetPosSlot2(distance, slot);
|
| 1101 | curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
|
| 1102 | }
|
| 1103 | opt = &p->opt[len];
|
| 1104 | if (curAndLenPrice < opt->price)
|
| 1105 | {
|
| 1106 | opt->price = curAndLenPrice;
|
| 1107 | opt->posPrev = 0;
|
| 1108 | opt->backPrev = distance + LZMA_NUM_REPS;
|
| 1109 | opt->prev1IsChar = False;
|
| 1110 | }
|
| 1111 | if (len == matches[offs])
|
| 1112 | {
|
| 1113 | offs += 2;
|
| 1114 | if (offs == numPairs)
|
| 1115 | break;
|
| 1116 | }
|
| 1117 | }
|
| 1118 | }
|
| 1119 | |
| 1120 | cur = 0;
|
| 1121 | |
| 1122 | #ifdef SHOW_STAT2
|
| 1123 | if (position >= 0)
|
| 1124 | {
|
| 1125 | unsigned i;
|
| 1126 | printf("\n pos = %4X", position);
|
| 1127 | for (i = cur; i <= lenEnd; i++)
|
| 1128 | printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
|
| 1129 | }
|
| 1130 | #endif
|
| 1131 | |
| 1132 | for (;;)
|
| 1133 | {
|
| 1134 | UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen;
|
| 1135 | UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice;
|
| 1136 | Bool nextIsChar;
|
| 1137 | Byte curByte, matchByte;
|
| 1138 | const Byte *data;
|
| 1139 | COptimal *curOpt;
|
| 1140 | COptimal *nextOpt;
|
| 1141 | |
| 1142 | cur++;
|
| 1143 | if (cur == lenEnd)
|
| 1144 | return Backward(p, backRes, cur);
|
| 1145 | |
| 1146 | newLen = ReadMatchDistances(p, &numPairs);
|
| 1147 | if (newLen >= p->numFastBytes)
|
| 1148 | {
|
| 1149 | p->numPairs = numPairs;
|
| 1150 | p->longestMatchLength = newLen;
|
| 1151 | return Backward(p, backRes, cur);
|
| 1152 | }
|
| 1153 | position++;
|
| 1154 | curOpt = &p->opt[cur];
|
| 1155 | posPrev = curOpt->posPrev;
|
| 1156 | if (curOpt->prev1IsChar)
|
| 1157 | {
|
| 1158 | posPrev--;
|
| 1159 | if (curOpt->prev2)
|
| 1160 | {
|
| 1161 | state = p->opt[curOpt->posPrev2].state;
|
| 1162 | if (curOpt->backPrev2 < LZMA_NUM_REPS)
|
| 1163 | state = kRepNextStates[state];
|
| 1164 | else
|
| 1165 | state = kMatchNextStates[state];
|
| 1166 | }
|
| 1167 | else
|
| 1168 | state = p->opt[posPrev].state;
|
| 1169 | state = kLiteralNextStates[state];
|
| 1170 | }
|
| 1171 | else
|
| 1172 | state = p->opt[posPrev].state;
|
| 1173 | if (posPrev == cur - 1)
|
| 1174 | {
|
| 1175 | if (IsShortRep(curOpt))
|
| 1176 | state = kShortRepNextStates[state];
|
| 1177 | else
|
| 1178 | state = kLiteralNextStates[state];
|
| 1179 | }
|
| 1180 | else
|
| 1181 | {
|
| 1182 | UInt32 pos;
|
| 1183 | const COptimal *prevOpt;
|
| 1184 | if (curOpt->prev1IsChar && curOpt->prev2)
|
| 1185 | {
|
| 1186 | posPrev = curOpt->posPrev2;
|
| 1187 | pos = curOpt->backPrev2;
|
| 1188 | state = kRepNextStates[state];
|
| 1189 | }
|
| 1190 | else
|
| 1191 | {
|
| 1192 | pos = curOpt->backPrev;
|
| 1193 | if (pos < LZMA_NUM_REPS)
|
| 1194 | state = kRepNextStates[state];
|
| 1195 | else
|
| 1196 | state = kMatchNextStates[state];
|
| 1197 | }
|
| 1198 | prevOpt = &p->opt[posPrev];
|
| 1199 | if (pos < LZMA_NUM_REPS)
|
| 1200 | {
|
| 1201 | UInt32 i;
|
| 1202 | reps[0] = prevOpt->backs[pos];
|
| 1203 | for (i = 1; i <= pos; i++)
|
| 1204 | reps[i] = prevOpt->backs[i - 1];
|
| 1205 | for (; i < LZMA_NUM_REPS; i++)
|
| 1206 | reps[i] = prevOpt->backs[i];
|
| 1207 | }
|
| 1208 | else
|
| 1209 | {
|
| 1210 | UInt32 i;
|
| 1211 | reps[0] = (pos - LZMA_NUM_REPS);
|
| 1212 | for (i = 1; i < LZMA_NUM_REPS; i++)
|
| 1213 | reps[i] = prevOpt->backs[i - 1];
|
| 1214 | }
|
| 1215 | }
|
| 1216 | curOpt->state = (CState)state;
|
| 1217 | |
| 1218 | curOpt->backs[0] = reps[0];
|
| 1219 | curOpt->backs[1] = reps[1];
|
| 1220 | curOpt->backs[2] = reps[2];
|
| 1221 | curOpt->backs[3] = reps[3];
|
| 1222 | |
| 1223 | curPrice = curOpt->price;
|
| 1224 | nextIsChar = False;
|
| 1225 | data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
|
| 1226 | curByte = *data;
|
| 1227 | matchByte = *(data - (reps[0] + 1));
|
| 1228 | |
| 1229 | posState = (position & p->pbMask);
|
| 1230 | |
| 1231 | curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
|
| 1232 | {
|
| 1233 | const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
|
| 1234 | curAnd1Price +=
|
| 1235 | (!IsCharState(state) ?
|
| 1236 | LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
|
| 1237 | LitEnc_GetPrice(probs, curByte, p->ProbPrices));
|
| 1238 | }
|
| 1239 | |
| 1240 | nextOpt = &p->opt[cur + 1];
|
| 1241 | |
| 1242 | if (curAnd1Price < nextOpt->price)
|
| 1243 | {
|
| 1244 | nextOpt->price = curAnd1Price;
|
| 1245 | nextOpt->posPrev = cur;
|
| 1246 | MakeAsChar(nextOpt);
|
| 1247 | nextIsChar = True;
|
| 1248 | }
|
| 1249 | |
| 1250 | matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
|
| 1251 | repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
|
| 1252 | |
| 1253 | if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
|
| 1254 | {
|
| 1255 | UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
|
| 1256 | if (shortRepPrice <= nextOpt->price)
|
| 1257 | {
|
| 1258 | nextOpt->price = shortRepPrice;
|
| 1259 | nextOpt->posPrev = cur;
|
| 1260 | MakeAsShortRep(nextOpt);
|
| 1261 | nextIsChar = True;
|
| 1262 | }
|
| 1263 | }
|
| 1264 | numAvailFull = p->numAvail;
|
| 1265 | {
|
| 1266 | UInt32 temp = kNumOpts - 1 - cur;
|
| 1267 | if (temp < numAvailFull)
|
| 1268 | numAvailFull = temp;
|
| 1269 | }
|
| 1270 | |
| 1271 | if (numAvailFull < 2)
|
| 1272 | continue;
|
| 1273 | numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);
|
| 1274 | |
| 1275 | if (!nextIsChar && matchByte != curByte) /* speed optimization */
|
| 1276 | {
|
| 1277 | /* try Literal + rep0 */
|
| 1278 | UInt32 temp;
|
| 1279 | UInt32 lenTest2;
|
| 1280 | const Byte *data2 = data - (reps[0] + 1);
|
| 1281 | UInt32 limit = p->numFastBytes + 1;
|
| 1282 | if (limit > numAvailFull)
|
| 1283 | limit = numAvailFull;
|
| 1284 | |
| 1285 | for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
|
| 1286 | lenTest2 = temp - 1;
|
| 1287 | if (lenTest2 >= 2)
|
| 1288 | {
|
| 1289 | UInt32 state2 = kLiteralNextStates[state];
|
| 1290 | UInt32 posStateNext = (position + 1) & p->pbMask;
|
| 1291 | UInt32 nextRepMatchPrice = curAnd1Price +
|
| 1292 | GET_PRICE_1(p->isMatch[state2][posStateNext]) +
|
| 1293 | GET_PRICE_1(p->isRep[state2]);
|
| 1294 | /* for (; lenTest2 >= 2; lenTest2--) */
|
| 1295 | {
|
| 1296 | UInt32 curAndLenPrice;
|
| 1297 | COptimal *opt;
|
| 1298 | UInt32 offset = cur + 1 + lenTest2;
|
| 1299 | while (lenEnd < offset)
|
| 1300 | p->opt[++lenEnd].price = kInfinityPrice;
|
| 1301 | curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
|
| 1302 | opt = &p->opt[offset];
|
| 1303 | if (curAndLenPrice < opt->price)
|
| 1304 | {
|
| 1305 | opt->price = curAndLenPrice;
|
| 1306 | opt->posPrev = cur + 1;
|
| 1307 | opt->backPrev = 0;
|
| 1308 | opt->prev1IsChar = True;
|
| 1309 | opt->prev2 = False;
|
| 1310 | }
|
| 1311 | }
|
| 1312 | }
|
| 1313 | }
|
| 1314 | |
| 1315 | startLen = 2; /* speed optimization */
|
| 1316 | {
|
| 1317 | UInt32 repIndex;
|
| 1318 | for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
|
| 1319 | {
|
| 1320 | UInt32 lenTest;
|
| 1321 | UInt32 lenTestTemp;
|
| 1322 | UInt32 price;
|
| 1323 | const Byte *data2 = data - (reps[repIndex] + 1);
|
| 1324 | if (data[0] != data2[0] || data[1] != data2[1])
|
| 1325 | continue;
|
| 1326 | for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
|
| 1327 | while (lenEnd < cur + lenTest)
|
| 1328 | p->opt[++lenEnd].price = kInfinityPrice;
|
| 1329 | lenTestTemp = lenTest;
|
| 1330 | price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
|
| 1331 | do
|
| 1332 | {
|
| 1333 | UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
|
| 1334 | COptimal *opt = &p->opt[cur + lenTest];
|
| 1335 | if (curAndLenPrice < opt->price)
|
| 1336 | {
|
| 1337 | opt->price = curAndLenPrice;
|
| 1338 | opt->posPrev = cur;
|
| 1339 | opt->backPrev = repIndex;
|
| 1340 | opt->prev1IsChar = False;
|
| 1341 | }
|
| 1342 | }
|
| 1343 | while (--lenTest >= 2);
|
| 1344 | lenTest = lenTestTemp;
|
| 1345 | |
| 1346 | if (repIndex == 0)
|
| 1347 | startLen = lenTest + 1;
|
| 1348 | |
| 1349 | /* if (_maxMode) */
|
| 1350 | {
|
| 1351 | UInt32 lenTest2 = lenTest + 1;
|
| 1352 | UInt32 limit = lenTest2 + p->numFastBytes;
|
| 1353 | UInt32 nextRepMatchPrice;
|
| 1354 | if (limit > numAvailFull)
|
| 1355 | limit = numAvailFull;
|
| 1356 | for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
|
| 1357 | lenTest2 -= lenTest + 1;
|
| 1358 | if (lenTest2 >= 2)
|
| 1359 | {
|
| 1360 | UInt32 state2 = kRepNextStates[state];
|
| 1361 | UInt32 posStateNext = (position + lenTest) & p->pbMask;
|
| 1362 | UInt32 curAndLenCharPrice =
|
| 1363 | price + p->repLenEnc.prices[posState][lenTest - 2] +
|
| 1364 | GET_PRICE_0(p->isMatch[state2][posStateNext]) +
|
| 1365 | LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
|
| 1366 | data[lenTest], data2[lenTest], p->ProbPrices);
|
| 1367 | state2 = kLiteralNextStates[state2];
|
| 1368 | posStateNext = (position + lenTest + 1) & p->pbMask;
|
| 1369 | nextRepMatchPrice = curAndLenCharPrice +
|
| 1370 | GET_PRICE_1(p->isMatch[state2][posStateNext]) +
|
| 1371 | GET_PRICE_1(p->isRep[state2]);
|
| 1372 | |
| 1373 | /* for (; lenTest2 >= 2; lenTest2--) */
|
| 1374 | {
|
| 1375 | UInt32 curAndLenPrice;
|
| 1376 | COptimal *opt;
|
| 1377 | UInt32 offset = cur + lenTest + 1 + lenTest2;
|
| 1378 | while (lenEnd < offset)
|
| 1379 | p->opt[++lenEnd].price = kInfinityPrice;
|
| 1380 | curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
|
| 1381 | opt = &p->opt[offset];
|
| 1382 | if (curAndLenPrice < opt->price)
|
| 1383 | {
|
| 1384 | opt->price = curAndLenPrice;
|
| 1385 | opt->posPrev = cur + lenTest + 1;
|
| 1386 | opt->backPrev = 0;
|
| 1387 | opt->prev1IsChar = True;
|
| 1388 | opt->prev2 = True;
|
| 1389 | opt->posPrev2 = cur;
|
| 1390 | opt->backPrev2 = repIndex;
|
| 1391 | }
|
| 1392 | }
|
| 1393 | }
|
| 1394 | }
|
| 1395 | }
|
| 1396 | }
|
| 1397 | /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
|
| 1398 | if (newLen > numAvail)
|
| 1399 | {
|
| 1400 | newLen = numAvail;
|
| 1401 | for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2);
|
| 1402 | matches[numPairs] = newLen;
|
| 1403 | numPairs += 2;
|
| 1404 | }
|
| 1405 | if (newLen >= startLen)
|
| 1406 | {
|
| 1407 | UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
|
| 1408 | UInt32 offs, curBack, posSlot;
|
| 1409 | UInt32 lenTest;
|
| 1410 | while (lenEnd < cur + newLen)
|
| 1411 | p->opt[++lenEnd].price = kInfinityPrice;
|
| 1412 | |
| 1413 | offs = 0;
|
| 1414 | while (startLen > matches[offs])
|
| 1415 | offs += 2;
|
| 1416 | curBack = matches[offs + 1];
|
| 1417 | GetPosSlot2(curBack, posSlot);
|
| 1418 | for (lenTest = /*2*/ startLen; ; lenTest++)
|
| 1419 | {
|
| 1420 | UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
|
| 1421 | UInt32 lenToPosState = GetLenToPosState(lenTest);
|
| 1422 | COptimal *opt;
|
| 1423 | if (curBack < kNumFullDistances)
|
| 1424 | curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
|
| 1425 | else
|
| 1426 | curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
|
| 1427 | |
| 1428 | opt = &p->opt[cur + lenTest];
|
| 1429 | if (curAndLenPrice < opt->price)
|
| 1430 | {
|
| 1431 | opt->price = curAndLenPrice;
|
| 1432 | opt->posPrev = cur;
|
| 1433 | opt->backPrev = curBack + LZMA_NUM_REPS;
|
| 1434 | opt->prev1IsChar = False;
|
| 1435 | }
|
| 1436 | |
| 1437 | if (/*_maxMode && */lenTest == matches[offs])
|
| 1438 | {
|
| 1439 | /* Try Match + Literal + Rep0 */
|
| 1440 | const Byte *data2 = data - (curBack + 1);
|
| 1441 | UInt32 lenTest2 = lenTest + 1;
|
| 1442 | UInt32 limit = lenTest2 + p->numFastBytes;
|
| 1443 | UInt32 nextRepMatchPrice;
|
| 1444 | if (limit > numAvailFull)
|
| 1445 | limit = numAvailFull;
|
| 1446 | for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
|
| 1447 | lenTest2 -= lenTest + 1;
|
| 1448 | if (lenTest2 >= 2)
|
| 1449 | {
|
| 1450 | UInt32 state2 = kMatchNextStates[state];
|
| 1451 | UInt32 posStateNext = (position + lenTest) & p->pbMask;
|
| 1452 | UInt32 curAndLenCharPrice = curAndLenPrice +
|
| 1453 | GET_PRICE_0(p->isMatch[state2][posStateNext]) +
|
| 1454 | LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
|
| 1455 | data[lenTest], data2[lenTest], p->ProbPrices);
|
| 1456 | state2 = kLiteralNextStates[state2];
|
| 1457 | posStateNext = (posStateNext + 1) & p->pbMask;
|
| 1458 | nextRepMatchPrice = curAndLenCharPrice +
|
| 1459 | GET_PRICE_1(p->isMatch[state2][posStateNext]) +
|
| 1460 | GET_PRICE_1(p->isRep[state2]);
|
| 1461 | |
| 1462 | /* for (; lenTest2 >= 2; lenTest2--) */
|
| 1463 | {
|
| 1464 | UInt32 offset = cur + lenTest + 1 + lenTest2;
|
| 1465 | UInt32 curAndLenPrice;
|
| 1466 | COptimal *opt;
|
| 1467 | while (lenEnd < offset)
|
| 1468 | p->opt[++lenEnd].price = kInfinityPrice;
|
| 1469 | curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
|
| 1470 | opt = &p->opt[offset];
|
| 1471 | if (curAndLenPrice < opt->price)
|
| 1472 | {
|
| 1473 | opt->price = curAndLenPrice;
|
| 1474 | opt->posPrev = cur + lenTest + 1;
|
| 1475 | opt->backPrev = 0;
|
| 1476 | opt->prev1IsChar = True;
|
| 1477 | opt->prev2 = True;
|
| 1478 | opt->posPrev2 = cur;
|
| 1479 | opt->backPrev2 = curBack + LZMA_NUM_REPS;
|
| 1480 | }
|
| 1481 | }
|
| 1482 | }
|
| 1483 | offs += 2;
|
| 1484 | if (offs == numPairs)
|
| 1485 | break;
|
| 1486 | curBack = matches[offs + 1];
|
| 1487 | if (curBack >= kNumFullDistances)
|
| 1488 | GetPosSlot2(curBack, posSlot);
|
| 1489 | }
|
| 1490 | }
|
| 1491 | }
|
| 1492 | }
|
| 1493 | }
|
| 1494 | |
| 1495 | #define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
|
| 1496 | |
| 1497 | static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
|
| 1498 | {
|
| 1499 | UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i;
|
| 1500 | const Byte *data;
|
| 1501 | const UInt32 *matches;
|
| 1502 | |
| 1503 | if (p->additionalOffset == 0)
|
| 1504 | mainLen = ReadMatchDistances(p, &numPairs);
|
| 1505 | else
|
| 1506 | {
|
| 1507 | mainLen = p->longestMatchLength;
|
| 1508 | numPairs = p->numPairs;
|
| 1509 | }
|
| 1510 | |
| 1511 | numAvail = p->numAvail;
|
| 1512 | *backRes = (UInt32)-1;
|
| 1513 | if (numAvail < 2)
|
| 1514 | return 1;
|
| 1515 | if (numAvail > LZMA_MATCH_LEN_MAX)
|
| 1516 | numAvail = LZMA_MATCH_LEN_MAX;
|
| 1517 | data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
|
| 1518 | |
| 1519 | repLen = repIndex = 0;
|
| 1520 | for (i = 0; i < LZMA_NUM_REPS; i++)
|
| 1521 | {
|
| 1522 | UInt32 len;
|
| 1523 | const Byte *data2 = data - (p->reps[i] + 1);
|
| 1524 | if (data[0] != data2[0] || data[1] != data2[1])
|
| 1525 | continue;
|
| 1526 | for (len = 2; len < numAvail && data[len] == data2[len]; len++);
|
| 1527 | if (len >= p->numFastBytes)
|
| 1528 | {
|
| 1529 | *backRes = i;
|
| 1530 | MovePos(p, len - 1);
|
| 1531 | return len;
|
| 1532 | }
|
| 1533 | if (len > repLen)
|
| 1534 | {
|
| 1535 | repIndex = i;
|
| 1536 | repLen = len;
|
| 1537 | }
|
| 1538 | }
|
| 1539 | |
| 1540 | matches = p->matches;
|
| 1541 | if (mainLen >= p->numFastBytes)
|
| 1542 | {
|
| 1543 | *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
|
| 1544 | MovePos(p, mainLen - 1);
|
| 1545 | return mainLen;
|
| 1546 | }
|
| 1547 | |
| 1548 | mainDist = 0; /* for GCC */
|
| 1549 | if (mainLen >= 2)
|
| 1550 | {
|
| 1551 | mainDist = matches[numPairs - 1];
|
| 1552 | while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1)
|
| 1553 | {
|
| 1554 | if (!ChangePair(matches[numPairs - 3], mainDist))
|
| 1555 | break;
|
| 1556 | numPairs -= 2;
|
| 1557 | mainLen = matches[numPairs - 2];
|
| 1558 | mainDist = matches[numPairs - 1];
|
| 1559 | }
|
| 1560 | if (mainLen == 2 && mainDist >= 0x80)
|
| 1561 | mainLen = 1;
|
| 1562 | }
|
| 1563 | |
| 1564 | if (repLen >= 2 && (
|
| 1565 | (repLen + 1 >= mainLen) ||
|
| 1566 | (repLen + 2 >= mainLen && mainDist >= (1 << 9)) ||
|
| 1567 | (repLen + 3 >= mainLen && mainDist >= (1 << 15))))
|
| 1568 | {
|
| 1569 | *backRes = repIndex;
|
| 1570 | MovePos(p, repLen - 1);
|
| 1571 | return repLen;
|
| 1572 | }
|
| 1573 | |
| 1574 | if (mainLen < 2 || numAvail <= 2)
|
| 1575 | return 1;
|
| 1576 | |
| 1577 | p->longestMatchLength = ReadMatchDistances(p, &p->numPairs);
|
| 1578 | if (p->longestMatchLength >= 2)
|
| 1579 | {
|
| 1580 | UInt32 newDistance = matches[p->numPairs - 1];
|
| 1581 | if ((p->longestMatchLength >= mainLen && newDistance < mainDist) ||
|
| 1582 | (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) ||
|
| 1583 | (p->longestMatchLength > mainLen + 1) ||
|
| 1584 | (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist)))
|
| 1585 | return 1;
|
| 1586 | }
|
| 1587 | |
| 1588 | data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
|
| 1589 | for (i = 0; i < LZMA_NUM_REPS; i++)
|
| 1590 | {
|
| 1591 | UInt32 len, limit;
|
| 1592 | const Byte *data2 = data - (p->reps[i] + 1);
|
| 1593 | if (data[0] != data2[0] || data[1] != data2[1])
|
| 1594 | continue;
|
| 1595 | limit = mainLen - 1;
|
| 1596 | for (len = 2; len < limit && data[len] == data2[len]; len++);
|
| 1597 | if (len >= limit)
|
| 1598 | return 1;
|
| 1599 | }
|
| 1600 | *backRes = mainDist + LZMA_NUM_REPS;
|
| 1601 | MovePos(p, mainLen - 2);
|
| 1602 | return mainLen;
|
| 1603 | }
|
| 1604 | |
| 1605 | static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
|
| 1606 | {
|
| 1607 | UInt32 len;
|
| 1608 | RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
|
| 1609 | RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
|
| 1610 | p->state = kMatchNextStates[p->state];
|
| 1611 | len = LZMA_MATCH_LEN_MIN;
|
| 1612 | LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
|
| 1613 | RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
|
| 1614 | RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
|
| 1615 | RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
|
| 1616 | }
|
| 1617 | |
| 1618 | static SRes CheckErrors(CLzmaEnc *p)
|
| 1619 | {
|
| 1620 | if (p->result != SZ_OK)
|
| 1621 | return p->result;
|
| 1622 | if (p->rc.res != SZ_OK)
|
| 1623 | p->result = SZ_ERROR_WRITE;
|
| 1624 | if (p->matchFinderBase.result != SZ_OK)
|
| 1625 | p->result = SZ_ERROR_READ;
|
| 1626 | if (p->result != SZ_OK)
|
| 1627 | p->finished = True;
|
| 1628 | return p->result;
|
| 1629 | }
|
| 1630 | |
| 1631 | static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
|
| 1632 | {
|
| 1633 | /* ReleaseMFStream(); */
|
| 1634 | p->finished = True;
|
| 1635 | if (p->writeEndMark)
|
| 1636 | WriteEndMarker(p, nowPos & p->pbMask);
|
| 1637 | RangeEnc_FlushData(&p->rc);
|
| 1638 | RangeEnc_FlushStream(&p->rc);
|
| 1639 | return CheckErrors(p);
|
| 1640 | }
|
| 1641 | |
| 1642 | static void FillAlignPrices(CLzmaEnc *p)
|
| 1643 | {
|
| 1644 | UInt32 i;
|
| 1645 | for (i = 0; i < kAlignTableSize; i++)
|
| 1646 | p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
|
| 1647 | p->alignPriceCount = 0;
|
| 1648 | }
|
| 1649 | |
| 1650 | static void FillDistancesPrices(CLzmaEnc *p)
|
| 1651 | {
|
| 1652 | UInt32 tempPrices[kNumFullDistances];
|
| 1653 | UInt32 i, lenToPosState;
|
| 1654 | for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
|
| 1655 | {
|
| 1656 | UInt32 posSlot = GetPosSlot1(i);
|
| 1657 | UInt32 footerBits = ((posSlot >> 1) - 1);
|
| 1658 | UInt32 base = ((2 | (posSlot & 1)) << footerBits);
|
| 1659 | tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
|
| 1660 | }
|
| 1661 | |
| 1662 | for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
|
| 1663 | {
|
| 1664 | UInt32 posSlot;
|
| 1665 | const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
|
| 1666 | UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
|
| 1667 | for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
|
| 1668 | posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
|
| 1669 | for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
|
| 1670 | posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
|
| 1671 | |
| 1672 | {
|
| 1673 | UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
|
| 1674 | UInt32 i;
|
| 1675 | for (i = 0; i < kStartPosModelIndex; i++)
|
| 1676 | distancesPrices[i] = posSlotPrices[i];
|
| 1677 | for (; i < kNumFullDistances; i++)
|
| 1678 | distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
|
| 1679 | }
|
| 1680 | }
|
| 1681 | p->matchPriceCount = 0;
|
| 1682 | }
|
| 1683 | |
| 1684 | void LzmaEnc_Construct(CLzmaEnc *p)
|
| 1685 | {
|
| 1686 | RangeEnc_Construct(&p->rc);
|
| 1687 | MatchFinder_Construct(&p->matchFinderBase);
|
| 1688 | #ifndef _7ZIP_ST
|
| 1689 | MatchFinderMt_Construct(&p->matchFinderMt);
|
| 1690 | p->matchFinderMt.MatchFinder = &p->matchFinderBase;
|
| 1691 | #endif
|
| 1692 | |
| 1693 | {
|
| 1694 | CLzmaEncProps props;
|
| 1695 | LzmaEncProps_Init(&props);
|
| 1696 | LzmaEnc_SetProps(p, &props);
|
| 1697 | }
|
| 1698 | |
| 1699 | #ifndef LZMA_LOG_BSR
|
| 1700 | LzmaEnc_FastPosInit(p->g_FastPos);
|
| 1701 | #endif
|
| 1702 | |
| 1703 | LzmaEnc_InitPriceTables(p->ProbPrices);
|
| 1704 | p->litProbs = 0;
|
| 1705 | p->saveState.litProbs = 0;
|
| 1706 | }
|
| 1707 | |
| 1708 | CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
|
| 1709 | {
|
| 1710 | void *p;
|
| 1711 | p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
|
| 1712 | if (p != 0)
|
| 1713 | LzmaEnc_Construct((CLzmaEnc *)p);
|
| 1714 | return p;
|
| 1715 | }
|
| 1716 | |
| 1717 | void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
|
| 1718 | {
|
| 1719 | alloc->Free(alloc, p->litProbs);
|
| 1720 | alloc->Free(alloc, p->saveState.litProbs);
|
| 1721 | p->litProbs = 0;
|
| 1722 | p->saveState.litProbs = 0;
|
| 1723 | }
|
| 1724 | |
| 1725 | void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
|
| 1726 | {
|
| 1727 | #ifndef _7ZIP_ST
|
| 1728 | MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
|
| 1729 | #endif
|
| 1730 | MatchFinder_Free(&p->matchFinderBase, allocBig);
|
| 1731 | LzmaEnc_FreeLits(p, alloc);
|
| 1732 | RangeEnc_Free(&p->rc, alloc);
|
| 1733 | }
|
| 1734 | |
| 1735 | void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
|
| 1736 | {
|
| 1737 | LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
|
| 1738 | alloc->Free(alloc, p);
|
| 1739 | }
|
| 1740 | |
| 1741 | static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
|
| 1742 | {
|
| 1743 | UInt32 nowPos32, startPos32;
|
| 1744 | if (p->needInit)
|
| 1745 | {
|
| 1746 | p->matchFinder.Init(p->matchFinderObj);
|
| 1747 | p->needInit = 0;
|
| 1748 | }
|
| 1749 | |
| 1750 | if (p->finished)
|
| 1751 | return p->result;
|
| 1752 | RINOK(CheckErrors(p));
|
| 1753 | |
| 1754 | nowPos32 = (UInt32)p->nowPos64;
|
| 1755 | startPos32 = nowPos32;
|
| 1756 | |
| 1757 | if (p->nowPos64 == 0)
|
| 1758 | {
|
| 1759 | UInt32 numPairs;
|
| 1760 | Byte curByte;
|
| 1761 | if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
|
| 1762 | return Flush(p, nowPos32);
|
| 1763 | ReadMatchDistances(p, &numPairs);
|
| 1764 | RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
|
| 1765 | p->state = kLiteralNextStates[p->state];
|
| 1766 | curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
|
| 1767 | LitEnc_Encode(&p->rc, p->litProbs, curByte);
|
| 1768 | p->additionalOffset--;
|
| 1769 | nowPos32++;
|
| 1770 | }
|
| 1771 | |
| 1772 | if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
|
| 1773 | for (;;)
|
| 1774 | {
|
| 1775 | UInt32 pos, len, posState;
|
| 1776 | |
| 1777 | if (p->fastMode)
|
| 1778 | len = GetOptimumFast(p, &pos);
|
| 1779 | else
|
| 1780 | len = GetOptimum(p, nowPos32, &pos);
|
| 1781 | |
| 1782 | #ifdef SHOW_STAT2
|
| 1783 | printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos);
|
| 1784 | #endif
|
| 1785 | |
| 1786 | posState = nowPos32 & p->pbMask;
|
| 1787 | if (len == 1 && pos == (UInt32)-1)
|
| 1788 | {
|
| 1789 | Byte curByte;
|
| 1790 | CLzmaProb *probs;
|
| 1791 | const Byte *data;
|
| 1792 | |
| 1793 | RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
|
| 1794 | data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
|
| 1795 | curByte = *data;
|
| 1796 | probs = LIT_PROBS(nowPos32, *(data - 1));
|
| 1797 | if (IsCharState(p->state))
|
| 1798 | LitEnc_Encode(&p->rc, probs, curByte);
|
| 1799 | else
|
| 1800 | LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
|
| 1801 | p->state = kLiteralNextStates[p->state];
|
| 1802 | }
|
| 1803 | else
|
| 1804 | {
|
| 1805 | RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
|
| 1806 | if (pos < LZMA_NUM_REPS)
|
| 1807 | {
|
| 1808 | RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
|
| 1809 | if (pos == 0)
|
| 1810 | {
|
| 1811 | RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
|
| 1812 | RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
|
| 1813 | }
|
| 1814 | else
|
| 1815 | {
|
| 1816 | UInt32 distance = p->reps[pos];
|
| 1817 | RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
|
| 1818 | if (pos == 1)
|
| 1819 | RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
|
| 1820 | else
|
| 1821 | {
|
| 1822 | RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
|
| 1823 | RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
|
| 1824 | if (pos == 3)
|
| 1825 | p->reps[3] = p->reps[2];
|
| 1826 | p->reps[2] = p->reps[1];
|
| 1827 | }
|
| 1828 | p->reps[1] = p->reps[0];
|
| 1829 | p->reps[0] = distance;
|
| 1830 | }
|
| 1831 | if (len == 1)
|
| 1832 | p->state = kShortRepNextStates[p->state];
|
| 1833 | else
|
| 1834 | {
|
| 1835 | LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
|
| 1836 | p->state = kRepNextStates[p->state];
|
| 1837 | }
|
| 1838 | }
|
| 1839 | else
|
| 1840 | {
|
| 1841 | UInt32 posSlot;
|
| 1842 | RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
|
| 1843 | p->state = kMatchNextStates[p->state];
|
| 1844 | LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
|
| 1845 | pos -= LZMA_NUM_REPS;
|
| 1846 | GetPosSlot(pos, posSlot);
|
| 1847 | RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
|
| 1848 | |
| 1849 | if (posSlot >= kStartPosModelIndex)
|
| 1850 | {
|
| 1851 | UInt32 footerBits = ((posSlot >> 1) - 1);
|
| 1852 | UInt32 base = ((2 | (posSlot & 1)) << footerBits);
|
| 1853 | UInt32 posReduced = pos - base;
|
| 1854 | |
| 1855 | if (posSlot < kEndPosModelIndex)
|
| 1856 | RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
|
| 1857 | else
|
| 1858 | {
|
| 1859 | RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
|
| 1860 | RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
|
| 1861 | p->alignPriceCount++;
|
| 1862 | }
|
| 1863 | }
|
| 1864 | p->reps[3] = p->reps[2];
|
| 1865 | p->reps[2] = p->reps[1];
|
| 1866 | p->reps[1] = p->reps[0];
|
| 1867 | p->reps[0] = pos;
|
| 1868 | p->matchPriceCount++;
|
| 1869 | }
|
| 1870 | }
|
| 1871 | p->additionalOffset -= len;
|
| 1872 | nowPos32 += len;
|
| 1873 | if (p->additionalOffset == 0)
|
| 1874 | {
|
| 1875 | UInt32 processed;
|
| 1876 | if (!p->fastMode)
|
| 1877 | {
|
| 1878 | if (p->matchPriceCount >= (1 << 7))
|
| 1879 | FillDistancesPrices(p);
|
| 1880 | if (p->alignPriceCount >= kAlignTableSize)
|
| 1881 | FillAlignPrices(p);
|
| 1882 | }
|
| 1883 | if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
|
| 1884 | break;
|
| 1885 | processed = nowPos32 - startPos32;
|
| 1886 | if (useLimits)
|
| 1887 | {
|
| 1888 | if (processed + kNumOpts + 300 >= maxUnpackSize ||
|
| 1889 | RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
|
| 1890 | break;
|
| 1891 | }
|
| 1892 | else if (processed >= (1 << 15))
|
| 1893 | {
|
| 1894 | p->nowPos64 += nowPos32 - startPos32;
|
| 1895 | return CheckErrors(p);
|
| 1896 | }
|
| 1897 | }
|
| 1898 | }
|
| 1899 | p->nowPos64 += nowPos32 - startPos32;
|
| 1900 | return Flush(p, nowPos32);
|
| 1901 | }
|
| 1902 | |
| 1903 | #define kBigHashDicLimit ((UInt32)1 << 24)
|
| 1904 | |
| 1905 | static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
|
| 1906 | {
|
| 1907 | UInt32 beforeSize = kNumOpts;
|
| 1908 | Bool btMode;
|
| 1909 | if (!RangeEnc_Alloc(&p->rc, alloc))
|
| 1910 | return SZ_ERROR_MEM;
|
| 1911 | btMode = (p->matchFinderBase.btMode != 0);
|
| 1912 | #ifndef _7ZIP_ST
|
| 1913 | p->mtMode = (p->multiThread && !p->fastMode && btMode);
|
| 1914 | #endif
|
| 1915 | |
| 1916 | {
|
| 1917 | unsigned lclp = p->lc + p->lp;
|
| 1918 | if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
|
| 1919 | {
|
| 1920 | LzmaEnc_FreeLits(p, alloc);
|
| 1921 | p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
|
| 1922 | p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
|
| 1923 | if (p->litProbs == 0 || p->saveState.litProbs == 0)
|
| 1924 | {
|
| 1925 | LzmaEnc_FreeLits(p, alloc);
|
| 1926 | return SZ_ERROR_MEM;
|
| 1927 | }
|
| 1928 | p->lclp = lclp;
|
| 1929 | }
|
| 1930 | }
|
| 1931 | |
| 1932 | p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
|
| 1933 | |
| 1934 | if (beforeSize + p->dictSize < keepWindowSize)
|
| 1935 | beforeSize = keepWindowSize - p->dictSize;
|
| 1936 | |
| 1937 | #ifndef _7ZIP_ST
|
| 1938 | if (p->mtMode)
|
| 1939 | {
|
| 1940 | RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
|
| 1941 | p->matchFinderObj = &p->matchFinderMt;
|
| 1942 | MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
|
| 1943 | }
|
| 1944 | else
|
| 1945 | #endif
|
| 1946 | {
|
| 1947 | if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
|
| 1948 | return SZ_ERROR_MEM;
|
| 1949 | p->matchFinderObj = &p->matchFinderBase;
|
| 1950 | MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
|
| 1951 | }
|
| 1952 | return SZ_OK;
|
| 1953 | }
|
| 1954 | |
| 1955 | void LzmaEnc_Init(CLzmaEnc *p)
|
| 1956 | {
|
| 1957 | UInt32 i;
|
| 1958 | p->state = 0;
|
| 1959 | for (i = 0 ; i < LZMA_NUM_REPS; i++)
|
| 1960 | p->reps[i] = 0;
|
| 1961 | |
| 1962 | RangeEnc_Init(&p->rc);
|
| 1963 | |
| 1964 | |
| 1965 | for (i = 0; i < kNumStates; i++)
|
| 1966 | {
|
| 1967 | UInt32 j;
|
| 1968 | for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
|
| 1969 | {
|
| 1970 | p->isMatch[i][j] = kProbInitValue;
|
| 1971 | p->isRep0Long[i][j] = kProbInitValue;
|
| 1972 | }
|
| 1973 | p->isRep[i] = kProbInitValue;
|
| 1974 | p->isRepG0[i] = kProbInitValue;
|
| 1975 | p->isRepG1[i] = kProbInitValue;
|
| 1976 | p->isRepG2[i] = kProbInitValue;
|
| 1977 | }
|
| 1978 | |
| 1979 | {
|
| 1980 | UInt32 num = 0x300 << (p->lp + p->lc);
|
| 1981 | for (i = 0; i < num; i++)
|
| 1982 | p->litProbs[i] = kProbInitValue;
|
| 1983 | }
|
| 1984 | |
| 1985 | {
|
| 1986 | for (i = 0; i < kNumLenToPosStates; i++)
|
| 1987 | {
|
| 1988 | CLzmaProb *probs = p->posSlotEncoder[i];
|
| 1989 | UInt32 j;
|
| 1990 | for (j = 0; j < (1 << kNumPosSlotBits); j++)
|
| 1991 | probs[j] = kProbInitValue;
|
| 1992 | }
|
| 1993 | }
|
| 1994 | {
|
| 1995 | for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
|
| 1996 | p->posEncoders[i] = kProbInitValue;
|
| 1997 | }
|
| 1998 | |
| 1999 | LenEnc_Init(&p->lenEnc.p);
|
| 2000 | LenEnc_Init(&p->repLenEnc.p);
|
| 2001 | |
| 2002 | for (i = 0; i < (1 << kNumAlignBits); i++)
|
| 2003 | p->posAlignEncoder[i] = kProbInitValue;
|
| 2004 | |
| 2005 | p->optimumEndIndex = 0;
|
| 2006 | p->optimumCurrentIndex = 0;
|
| 2007 | p->additionalOffset = 0;
|
| 2008 | |
| 2009 | p->pbMask = (1 << p->pb) - 1;
|
| 2010 | p->lpMask = (1 << p->lp) - 1;
|
| 2011 | }
|
| 2012 | |
| 2013 | void LzmaEnc_InitPrices(CLzmaEnc *p)
|
| 2014 | {
|
| 2015 | if (!p->fastMode)
|
| 2016 | {
|
| 2017 | FillDistancesPrices(p);
|
| 2018 | FillAlignPrices(p);
|
| 2019 | }
|
| 2020 | |
| 2021 | p->lenEnc.tableSize =
|
| 2022 | p->repLenEnc.tableSize =
|
| 2023 | p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
|
| 2024 | LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
|
| 2025 | LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
|
| 2026 | }
|
| 2027 | |
| 2028 | static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
|
| 2029 | {
|
| 2030 | UInt32 i;
|
| 2031 | for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++)
|
| 2032 | if (p->dictSize <= ((UInt32)1 << i))
|
| 2033 | break;
|
| 2034 | p->distTableSize = i * 2;
|
| 2035 | |
| 2036 | p->finished = False;
|
| 2037 | p->result = SZ_OK;
|
| 2038 | RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
|
| 2039 | LzmaEnc_Init(p);
|
| 2040 | LzmaEnc_InitPrices(p);
|
| 2041 | p->nowPos64 = 0;
|
| 2042 | return SZ_OK;
|
| 2043 | }
|
| 2044 | |
| 2045 | static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream,
|
| 2046 | ISzAlloc *alloc, ISzAlloc *allocBig)
|
| 2047 | {
|
| 2048 | CLzmaEnc *p = (CLzmaEnc *)pp;
|
| 2049 | p->matchFinderBase.stream = inStream;
|
| 2050 | p->needInit = 1;
|
| 2051 | p->rc.outStream = outStream;
|
| 2052 | return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
|
| 2053 | }
|
| 2054 | |
| 2055 | SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,
|
| 2056 | ISeqInStream *inStream, UInt32 keepWindowSize,
|
| 2057 | ISzAlloc *alloc, ISzAlloc *allocBig)
|
| 2058 | {
|
| 2059 | CLzmaEnc *p = (CLzmaEnc *)pp;
|
| 2060 | p->matchFinderBase.stream = inStream;
|
| 2061 | p->needInit = 1;
|
| 2062 | return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
|
| 2063 | }
|
| 2064 | |
| 2065 | static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
|
| 2066 | {
|
| 2067 | p->matchFinderBase.directInput = 1;
|
| 2068 | p->matchFinderBase.bufferBase = (Byte *)src;
|
| 2069 | p->matchFinderBase.directInputRem = srcLen;
|
| 2070 | }
|
| 2071 | |
| 2072 | SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
|
| 2073 | UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
|
| 2074 | {
|
| 2075 | CLzmaEnc *p = (CLzmaEnc *)pp;
|
| 2076 | LzmaEnc_SetInputBuf(p, src, srcLen);
|
| 2077 | p->needInit = 1;
|
| 2078 | |
| 2079 | return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
|
| 2080 | }
|
| 2081 | |
| 2082 | void LzmaEnc_Finish(CLzmaEncHandle pp)
|
| 2083 | {
|
| 2084 | #ifndef _7ZIP_ST
|
| 2085 | CLzmaEnc *p = (CLzmaEnc *)pp;
|
| 2086 | if (p->mtMode)
|
| 2087 | MatchFinderMt_ReleaseStream(&p->matchFinderMt);
|
| 2088 | #else
|
| 2089 | pp = pp;
|
| 2090 | #endif
|
| 2091 | }
|
| 2092 | |
| 2093 | typedef struct
|
| 2094 | {
|
| 2095 | ISeqOutStream funcTable;
|
| 2096 | Byte *data;
|
| 2097 | SizeT rem;
|
| 2098 | Bool overflow;
|
| 2099 | } CSeqOutStreamBuf;
|
| 2100 | |
| 2101 | static size_t MyWrite(void *pp, const void *data, size_t size)
|
| 2102 | {
|
| 2103 | CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
|
| 2104 | if (p->rem < size)
|
| 2105 | {
|
| 2106 | size = p->rem;
|
| 2107 | p->overflow = True;
|
| 2108 | }
|
| 2109 | memcpy(p->data, data, size);
|
| 2110 | p->rem -= size;
|
| 2111 | p->data += size;
|
| 2112 | return size;
|
| 2113 | }
|
| 2114 | |
| 2115 | |
| 2116 | UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp)
|
| 2117 | {
|
| 2118 | const CLzmaEnc *p = (CLzmaEnc *)pp;
|
| 2119 | return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
|
| 2120 | }
|
| 2121 | |
| 2122 | const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp)
|
| 2123 | {
|
| 2124 | const CLzmaEnc *p = (CLzmaEnc *)pp;
|
| 2125 | return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
|
| 2126 | }
|
| 2127 | |
| 2128 | SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit,
|
| 2129 | Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)
|
| 2130 | {
|
| 2131 | CLzmaEnc *p = (CLzmaEnc *)pp;
|
| 2132 | UInt64 nowPos64;
|
| 2133 | SRes res;
|
| 2134 | CSeqOutStreamBuf outStream;
|
| 2135 | |
| 2136 | outStream.funcTable.Write = MyWrite;
|
| 2137 | outStream.data = dest;
|
| 2138 | outStream.rem = *destLen;
|
| 2139 | outStream.overflow = False;
|
| 2140 | |
| 2141 | p->writeEndMark = False;
|
| 2142 | p->finished = False;
|
| 2143 | p->result = SZ_OK;
|
| 2144 | |
| 2145 | if (reInit)
|
| 2146 | LzmaEnc_Init(p);
|
| 2147 | LzmaEnc_InitPrices(p);
|
| 2148 | nowPos64 = p->nowPos64;
|
| 2149 | RangeEnc_Init(&p->rc);
|
| 2150 | p->rc.outStream = &outStream.funcTable;
|
| 2151 | |
| 2152 | res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize);
|
| 2153 | |
| 2154 | *unpackSize = (UInt32)(p->nowPos64 - nowPos64);
|
| 2155 | *destLen -= outStream.rem;
|
| 2156 | if (outStream.overflow)
|
| 2157 | return SZ_ERROR_OUTPUT_EOF;
|
| 2158 | |
| 2159 | return res;
|
| 2160 | }
|
| 2161 | |
| 2162 | static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
|
| 2163 | {
|
| 2164 | SRes res = SZ_OK;
|
| 2165 | |
| 2166 | #ifndef _7ZIP_ST
|
| 2167 | Byte allocaDummy[0x300];
|
| 2168 | int i = 0;
|
| 2169 | for (i = 0; i < 16; i++)
|
| 2170 | allocaDummy[i] = (Byte)i;
|
| 2171 | #endif
|
| 2172 | |
| 2173 | for (;;)
|
| 2174 | {
|
| 2175 | res = LzmaEnc_CodeOneBlock(p, False, 0, 0);
|
| 2176 | if (res != SZ_OK || p->finished != 0)
|
| 2177 | break;
|
| 2178 | if (progress != 0)
|
| 2179 | {
|
| 2180 | res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
|
| 2181 | if (res != SZ_OK)
|
| 2182 | {
|
| 2183 | res = SZ_ERROR_PROGRESS;
|
| 2184 | break;
|
| 2185 | }
|
| 2186 | }
|
| 2187 | }
|
| 2188 | LzmaEnc_Finish(p);
|
| 2189 | return res;
|
| 2190 | }
|
| 2191 | |
| 2192 | SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,
|
| 2193 | ISzAlloc *alloc, ISzAlloc *allocBig)
|
| 2194 | {
|
| 2195 | RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig));
|
| 2196 | return LzmaEnc_Encode2((CLzmaEnc *)pp, progress);
|
| 2197 | }
|
| 2198 | |
| 2199 | SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
|
| 2200 | {
|
| 2201 | CLzmaEnc *p = (CLzmaEnc *)pp;
|
| 2202 | int i;
|
| 2203 | UInt32 dictSize = p->dictSize;
|
| 2204 | if (*size < LZMA_PROPS_SIZE)
|
| 2205 | return SZ_ERROR_PARAM;
|
| 2206 | *size = LZMA_PROPS_SIZE;
|
| 2207 | props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
|
| 2208 | |
| 2209 | for (i = 11; i <= 30; i++)
|
| 2210 | {
|
| 2211 | if (dictSize <= ((UInt32)2 << i))
|
| 2212 | {
|
| 2213 | dictSize = (2 << i);
|
| 2214 | break;
|
| 2215 | }
|
| 2216 | if (dictSize <= ((UInt32)3 << i))
|
| 2217 | {
|
| 2218 | dictSize = (3 << i);
|
| 2219 | break;
|
| 2220 | }
|
| 2221 | }
|
| 2222 | |
| 2223 | for (i = 0; i < 4; i++)
|
| 2224 | props[1 + i] = (Byte)(dictSize >> (8 * i));
|
| 2225 | return SZ_OK;
|
| 2226 | }
|
| 2227 | |
| 2228 | SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
|
| 2229 | int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
|
| 2230 | {
|
| 2231 | SRes res;
|
| 2232 | CLzmaEnc *p = (CLzmaEnc *)pp;
|
| 2233 | |
| 2234 | CSeqOutStreamBuf outStream;
|
| 2235 | |
| 2236 | LzmaEnc_SetInputBuf(p, src, srcLen);
|
| 2237 | |
| 2238 | outStream.funcTable.Write = MyWrite;
|
| 2239 | outStream.data = dest;
|
| 2240 | outStream.rem = *destLen;
|
| 2241 | outStream.overflow = False;
|
| 2242 | |
| 2243 | p->writeEndMark = writeEndMark;
|
| 2244 | |
| 2245 | p->rc.outStream = &outStream.funcTable;
|
| 2246 | res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig);
|
| 2247 | if (res == SZ_OK)
|
| 2248 | res = LzmaEnc_Encode2(p, progress);
|
| 2249 | |
| 2250 | *destLen -= outStream.rem;
|
| 2251 | if (outStream.overflow)
|
| 2252 | return SZ_ERROR_OUTPUT_EOF;
|
| 2253 | return res;
|
| 2254 | }
|
| 2255 | |
| 2256 | SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
|
| 2257 | const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
|
| 2258 | ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
|
| 2259 | {
|
| 2260 | CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
|
| 2261 | SRes res;
|
| 2262 | if (p == 0)
|
| 2263 | return SZ_ERROR_MEM;
|
| 2264 | |
| 2265 | res = LzmaEnc_SetProps(p, props);
|
| 2266 | if (res == SZ_OK)
|
| 2267 | {
|
| 2268 | res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
|
| 2269 | if (res == SZ_OK)
|
| 2270 | res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
|
| 2271 | writeEndMark, progress, alloc, allocBig);
|
| 2272 | }
|
| 2273 | |
| 2274 | LzmaEnc_Destroy(p, alloc, allocBig);
|
| 2275 | return res;
|
| 2276 | }
|
src/lib/lib7z/LzmaDec.c
| r0 | r14440 | |
| 1 | /* LzmaDec.c -- LZMA Decoder
|
| 2 | 2010-12-15 : Igor Pavlov : Public domain */
|
| 3 | |
| 4 | #include "LzmaDec.h"
|
| 5 | |
| 6 | #include <string.h>
|
| 7 | |
| 8 | #define kNumTopBits 24
|
| 9 | #define kTopValue ((UInt32)1 << kNumTopBits)
|
| 10 | |
| 11 | #define kNumBitModelTotalBits 11
|
| 12 | #define kBitModelTotal (1 << kNumBitModelTotalBits)
|
| 13 | #define kNumMoveBits 5
|
| 14 | |
| 15 | #define RC_INIT_SIZE 5
|
| 16 | |
| 17 | #define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
|
| 18 | |
| 19 | #define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
|
| 20 | #define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
|
| 21 | #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
|
| 22 | #define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
|
| 23 | { UPDATE_0(p); i = (i + i); A0; } else \
|
| 24 | { UPDATE_1(p); i = (i + i) + 1; A1; }
|
| 25 | #define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
|
| 26 | |
| 27 | #define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
|
| 28 | #define TREE_DECODE(probs, limit, i) \
|
| 29 | { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
|
| 30 | |
| 31 | /* #define _LZMA_SIZE_OPT */
|
| 32 | |
| 33 | #ifdef _LZMA_SIZE_OPT
|
| 34 | #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
|
| 35 | #else
|
| 36 | #define TREE_6_DECODE(probs, i) \
|
| 37 | { i = 1; \
|
| 38 | TREE_GET_BIT(probs, i); \
|
| 39 | TREE_GET_BIT(probs, i); \
|
| 40 | TREE_GET_BIT(probs, i); \
|
| 41 | TREE_GET_BIT(probs, i); \
|
| 42 | TREE_GET_BIT(probs, i); \
|
| 43 | TREE_GET_BIT(probs, i); \
|
| 44 | i -= 0x40; }
|
| 45 | #endif
|
| 46 | |
| 47 | #define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
|
| 48 | |
| 49 | #define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
|
| 50 | #define UPDATE_0_CHECK range = bound;
|
| 51 | #define UPDATE_1_CHECK range -= bound; code -= bound;
|
| 52 | #define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
|
| 53 | { UPDATE_0_CHECK; i = (i + i); A0; } else \
|
| 54 | { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
|
| 55 | #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
|
| 56 | #define TREE_DECODE_CHECK(probs, limit, i) \
|
| 57 | { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
|
| 58 | |
| 59 | |
| 60 | #define kNumPosBitsMax 4
|
| 61 | #define kNumPosStatesMax (1 << kNumPosBitsMax)
|
| 62 | |
| 63 | #define kLenNumLowBits 3
|
| 64 | #define kLenNumLowSymbols (1 << kLenNumLowBits)
|
| 65 | #define kLenNumMidBits 3
|
| 66 | #define kLenNumMidSymbols (1 << kLenNumMidBits)
|
| 67 | #define kLenNumHighBits 8
|
| 68 | #define kLenNumHighSymbols (1 << kLenNumHighBits)
|
| 69 | |
| 70 | #define LenChoice 0
|
| 71 | #define LenChoice2 (LenChoice + 1)
|
| 72 | #define LenLow (LenChoice2 + 1)
|
| 73 | #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
|
| 74 | #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
|
| 75 | #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
|
| 76 | |
| 77 | |
| 78 | #define kNumStates 12
|
| 79 | #define kNumLitStates 7
|
| 80 | |
| 81 | #define kStartPosModelIndex 4
|
| 82 | #define kEndPosModelIndex 14
|
| 83 | #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
|
| 84 | |
| 85 | #define kNumPosSlotBits 6
|
| 86 | #define kNumLenToPosStates 4
|
| 87 | |
| 88 | #define kNumAlignBits 4
|
| 89 | #define kAlignTableSize (1 << kNumAlignBits)
|
| 90 | |
| 91 | #define kMatchMinLen 2
|
| 92 | #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
|
| 93 | |
| 94 | #define IsMatch 0
|
| 95 | #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
|
| 96 | #define IsRepG0 (IsRep + kNumStates)
|
| 97 | #define IsRepG1 (IsRepG0 + kNumStates)
|
| 98 | #define IsRepG2 (IsRepG1 + kNumStates)
|
| 99 | #define IsRep0Long (IsRepG2 + kNumStates)
|
| 100 | #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
|
| 101 | #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
|
| 102 | #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
|
| 103 | #define LenCoder (Align + kAlignTableSize)
|
| 104 | #define RepLenCoder (LenCoder + kNumLenProbs)
|
| 105 | #define Literal (RepLenCoder + kNumLenProbs)
|
| 106 | |
| 107 | #define LZMA_BASE_SIZE 1846
|
| 108 | #define LZMA_LIT_SIZE 768
|
| 109 | |
| 110 | #define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
|
| 111 | |
| 112 | #if Literal != LZMA_BASE_SIZE
|
| 113 | StopCompilingDueBUG
|
| 114 | #endif
|
| 115 | |
| 116 | #define LZMA_DIC_MIN (1 << 12)
|
| 117 | |
| 118 | /* First LZMA-symbol is always decoded.
|
| 119 | And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
|
| 120 | Out:
|
| 121 | Result:
|
| 122 | SZ_OK - OK
|
| 123 | SZ_ERROR_DATA - Error
|
| 124 | p->remainLen:
|
| 125 | < kMatchSpecLenStart : normal remain
|
| 126 | = kMatchSpecLenStart : finished
|
| 127 | = kMatchSpecLenStart + 1 : Flush marker
|
| 128 | = kMatchSpecLenStart + 2 : State Init Marker
|
| 129 | */
|
| 130 | |
| 131 | static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
|
| 132 | {
|
| 133 | CLzmaProb *probs = p->probs;
|
| 134 | |
| 135 | unsigned state = p->state;
|
| 136 | UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
|
| 137 | unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
|
| 138 | unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
|
| 139 | unsigned lc = p->prop.lc;
|
| 140 | |
| 141 | Byte *dic = p->dic;
|
| 142 | SizeT dicBufSize = p->dicBufSize;
|
| 143 | SizeT dicPos = p->dicPos;
|
| 144 | |
| 145 | UInt32 processedPos = p->processedPos;
|
| 146 | UInt32 checkDicSize = p->checkDicSize;
|
| 147 | unsigned len = 0;
|
| 148 | |
| 149 | const Byte *buf = p->buf;
|
| 150 | UInt32 range = p->range;
|
| 151 | UInt32 code = p->code;
|
| 152 | |
| 153 | do
|
| 154 | {
|
| 155 | CLzmaProb *prob;
|
| 156 | UInt32 bound;
|
| 157 | unsigned ttt;
|
| 158 | unsigned posState = processedPos & pbMask;
|
| 159 | |
| 160 | prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
|
| 161 | IF_BIT_0(prob)
|
| 162 | {
|
| 163 | unsigned symbol;
|
| 164 | UPDATE_0(prob);
|
| 165 | prob = probs + Literal;
|
| 166 | if (checkDicSize != 0 || processedPos != 0)
|
| 167 | prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
|
| 168 | (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
|
| 169 | |
| 170 | if (state < kNumLitStates)
|
| 171 | {
|
| 172 | state -= (state < 4) ? state : 3;
|
| 173 | symbol = 1;
|
| 174 | do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100);
|
| 175 | }
|
| 176 | else
|
| 177 | {
|
| 178 | unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
|
| 179 | unsigned offs = 0x100;
|
| 180 | state -= (state < 10) ? 3 : 6;
|
| 181 | symbol = 1;
|
| 182 | do
|
| 183 | {
|
| 184 | unsigned bit;
|
| 185 | CLzmaProb *probLit;
|
| 186 | matchByte <<= 1;
|
| 187 | bit = (matchByte & offs);
|
| 188 | probLit = prob + offs + bit + symbol;
|
| 189 | GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
|
| 190 | }
|
| 191 | while (symbol < 0x100);
|
| 192 | }
|
| 193 | dic[dicPos++] = (Byte)symbol;
|
| 194 | processedPos++;
|
| 195 | continue;
|
| 196 | }
|
| 197 | else
|
| 198 | {
|
| 199 | UPDATE_1(prob);
|
| 200 | prob = probs + IsRep + state;
|
| 201 | IF_BIT_0(prob)
|
| 202 | {
|
| 203 | UPDATE_0(prob);
|
| 204 | state += kNumStates;
|
| 205 | prob = probs + LenCoder;
|
| 206 | }
|
| 207 | else
|
| 208 | {
|
| 209 | UPDATE_1(prob);
|
| 210 | if (checkDicSize == 0 && processedPos == 0)
|
| 211 | return SZ_ERROR_DATA;
|
| 212 | prob = probs + IsRepG0 + state;
|
| 213 | IF_BIT_0(prob)
|
| 214 | {
|
| 215 | UPDATE_0(prob);
|
| 216 | prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
|
| 217 | IF_BIT_0(prob)
|
| 218 | {
|
| 219 | UPDATE_0(prob);
|
| 220 | dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
|
| 221 | dicPos++;
|
| 222 | processedPos++;
|
| 223 | state = state < kNumLitStates ? 9 : 11;
|
| 224 | continue;
|
| 225 | }
|
| 226 | UPDATE_1(prob);
|
| 227 | }
|
| 228 | else
|
| 229 | {
|
| 230 | UInt32 distance;
|
| 231 | UPDATE_1(prob);
|
| 232 | prob = probs + IsRepG1 + state;
|
| 233 | IF_BIT_0(prob)
|
| 234 | {
|
| 235 | UPDATE_0(prob);
|
| 236 | distance = rep1;
|
| 237 | }
|
| 238 | else
|
| 239 | {
|
| 240 | UPDATE_1(prob);
|
| 241 | prob = probs + IsRepG2 + state;
|
| 242 | IF_BIT_0(prob)
|
| 243 | {
|
| 244 | UPDATE_0(prob);
|
| 245 | distance = rep2;
|
| 246 | }
|
| 247 | else
|
| 248 | {
|
| 249 | UPDATE_1(prob);
|
| 250 | distance = rep3;
|
| 251 | rep3 = rep2;
|
| 252 | }
|
| 253 | rep2 = rep1;
|
| 254 | }
|
| 255 | rep1 = rep0;
|
| 256 | rep0 = distance;
|
| 257 | }
|
| 258 | state = state < kNumLitStates ? 8 : 11;
|
| 259 | prob = probs + RepLenCoder;
|
| 260 | }
|
| 261 | {
|
| 262 | unsigned limit, offset;
|
| 263 | CLzmaProb *probLen = prob + LenChoice;
|
| 264 | IF_BIT_0(probLen)
|
| 265 | {
|
| 266 | UPDATE_0(probLen);
|
| 267 | probLen = prob + LenLow + (posState << kLenNumLowBits);
|
| 268 | offset = 0;
|
| 269 | limit = (1 << kLenNumLowBits);
|
| 270 | }
|
| 271 | else
|
| 272 | {
|
| 273 | UPDATE_1(probLen);
|
| 274 | probLen = prob + LenChoice2;
|
| 275 | IF_BIT_0(probLen)
|
| 276 | {
|
| 277 | UPDATE_0(probLen);
|
| 278 | probLen = prob + LenMid + (posState << kLenNumMidBits);
|
| 279 | offset = kLenNumLowSymbols;
|
| 280 | limit = (1 << kLenNumMidBits);
|
| 281 | }
|
| 282 | else
|
| 283 | {
|
| 284 | UPDATE_1(probLen);
|
| 285 | probLen = prob + LenHigh;
|
| 286 | offset = kLenNumLowSymbols + kLenNumMidSymbols;
|
| 287 | limit = (1 << kLenNumHighBits);
|
| 288 | }
|
| 289 | }
|
| 290 | TREE_DECODE(probLen, limit, len);
|
| 291 | len += offset;
|
| 292 | }
|
| 293 | |
| 294 | if (state >= kNumStates)
|
| 295 | {
|
| 296 | UInt32 distance;
|
| 297 | prob = probs + PosSlot +
|
| 298 | ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
|
| 299 | TREE_6_DECODE(prob, distance);
|
| 300 | if (distance >= kStartPosModelIndex)
|
| 301 | {
|
| 302 | unsigned posSlot = (unsigned)distance;
|
| 303 | int numDirectBits = (int)(((distance >> 1) - 1));
|
| 304 | distance = (2 | (distance & 1));
|
| 305 | if (posSlot < kEndPosModelIndex)
|
| 306 | {
|
| 307 | distance <<= numDirectBits;
|
| 308 | prob = probs + SpecPos + distance - posSlot - 1;
|
| 309 | {
|
| 310 | UInt32 mask = 1;
|
| 311 | unsigned i = 1;
|
| 312 | do
|
| 313 | {
|
| 314 | GET_BIT2(prob + i, i, ; , distance |= mask);
|
| 315 | mask <<= 1;
|
| 316 | }
|
| 317 | while (--numDirectBits != 0);
|
| 318 | }
|
| 319 | }
|
| 320 | else
|
| 321 | {
|
| 322 | numDirectBits -= kNumAlignBits;
|
| 323 | do
|
| 324 | {
|
| 325 | NORMALIZE
|
| 326 | range >>= 1;
|
| 327 | |
| 328 | {
|
| 329 | UInt32 t;
|
| 330 | code -= range;
|
| 331 | t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
|
| 332 | distance = (distance << 1) + (t + 1);
|
| 333 | code += range & t;
|
| 334 | }
|
| 335 | /*
|
| 336 | distance <<= 1;
|
| 337 | if (code >= range)
|
| 338 | {
|
| 339 | code -= range;
|
| 340 | distance |= 1;
|
| 341 | }
|
| 342 | */
|
| 343 | }
|
| 344 | while (--numDirectBits != 0);
|
| 345 | prob = probs + Align;
|
| 346 | distance <<= kNumAlignBits;
|
| 347 | {
|
| 348 | unsigned i = 1;
|
| 349 | GET_BIT2(prob + i, i, ; , distance |= 1);
|
| 350 | GET_BIT2(prob + i, i, ; , distance |= 2);
|
| 351 | GET_BIT2(prob + i, i, ; , distance |= 4);
|
| 352 | GET_BIT2(prob + i, i, ; , distance |= 8);
|
| 353 | }
|
| 354 | if (distance == (UInt32)0xFFFFFFFF)
|
| 355 | {
|
| 356 | len += kMatchSpecLenStart;
|
| 357 | state -= kNumStates;
|
| 358 | break;
|
| 359 | }
|
| 360 | }
|
| 361 | }
|
| 362 | rep3 = rep2;
|
| 363 | rep2 = rep1;
|
| 364 | rep1 = rep0;
|
| 365 | rep0 = distance + 1;
|
| 366 | if (checkDicSize == 0)
|
| 367 | {
|
| 368 | if (distance >= processedPos)
|
| 369 | return SZ_ERROR_DATA;
|
| 370 | }
|
| 371 | else if (distance >= checkDicSize)
|
| 372 | return SZ_ERROR_DATA;
|
| 373 | state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
|
| 374 | }
|
| 375 | |
| 376 | len += kMatchMinLen;
|
| 377 | |
| 378 | if (limit == dicPos)
|
| 379 | return SZ_ERROR_DATA;
|
| 380 | {
|
| 381 | SizeT rem = limit - dicPos;
|
| 382 | unsigned curLen = ((rem < len) ? (unsigned)rem : len);
|
| 383 | SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
|
| 384 | |
| 385 | processedPos += curLen;
|
| 386 | |
| 387 | len -= curLen;
|
| 388 | if (pos + curLen <= dicBufSize)
|
| 389 | {
|
| 390 | Byte *dest = dic + dicPos;
|
| 391 | ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
|
| 392 | const Byte *lim = dest + curLen;
|
| 393 | dicPos += curLen;
|
| 394 | do
|
| 395 | *(dest) = (Byte)*(dest + src);
|
| 396 | while (++dest != lim);
|
| 397 | }
|
| 398 | else
|
| 399 | {
|
| 400 | do
|
| 401 | {
|
| 402 | dic[dicPos++] = dic[pos];
|
| 403 | if (++pos == dicBufSize)
|
| 404 | pos = 0;
|
| 405 | }
|
| 406 | while (--curLen != 0);
|
| 407 | }
|
| 408 | }
|
| 409 | }
|
| 410 | }
|
| 411 | while (dicPos < limit && buf < bufLimit);
|
| 412 | NORMALIZE;
|
| 413 | p->buf = buf;
|
| 414 | p->range = range;
|
| 415 | p->code = code;
|
| 416 | p->remainLen = len;
|
| 417 | p->dicPos = dicPos;
|
| 418 | p->processedPos = processedPos;
|
| 419 | p->reps[0] = rep0;
|
| 420 | p->reps[1] = rep1;
|
| 421 | p->reps[2] = rep2;
|
| 422 | p->reps[3] = rep3;
|
| 423 | p->state = state;
|
| 424 | |
| 425 | return SZ_OK;
|
| 426 | }
|
| 427 | |
| 428 | static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
|
| 429 | {
|
| 430 | if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
|
| 431 | {
|
| 432 | Byte *dic = p->dic;
|
| 433 | SizeT dicPos = p->dicPos;
|
| 434 | SizeT dicBufSize = p->dicBufSize;
|
| 435 | unsigned len = p->remainLen;
|
| 436 | UInt32 rep0 = p->reps[0];
|
| 437 | if (limit - dicPos < len)
|
| 438 | len = (unsigned)(limit - dicPos);
|
| 439 | |
| 440 | if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
|
| 441 | p->checkDicSize = p->prop.dicSize;
|
| 442 | |
| 443 | p->processedPos += len;
|
| 444 | p->remainLen -= len;
|
| 445 | while (len != 0)
|
| 446 | {
|
| 447 | len--;
|
| 448 | dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
|
| 449 | dicPos++;
|
| 450 | }
|
| 451 | p->dicPos = dicPos;
|
| 452 | }
|
| 453 | }
|
| 454 | |
| 455 | static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
|
| 456 | {
|
| 457 | do
|
| 458 | {
|
| 459 | SizeT limit2 = limit;
|
| 460 | if (p->checkDicSize == 0)
|
| 461 | {
|
| 462 | UInt32 rem = p->prop.dicSize - p->processedPos;
|
| 463 | if (limit - p->dicPos > rem)
|
| 464 | limit2 = p->dicPos + rem;
|
| 465 | }
|
| 466 | RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
|
| 467 | if (p->processedPos >= p->prop.dicSize)
|
| 468 | p->checkDicSize = p->prop.dicSize;
|
| 469 | LzmaDec_WriteRem(p, limit);
|
| 470 | }
|
| 471 | while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
|
| 472 | |
| 473 | if (p->remainLen > kMatchSpecLenStart)
|
| 474 | {
|
| 475 | p->remainLen = kMatchSpecLenStart;
|
| 476 | }
|
| 477 | return 0;
|
| 478 | }
|
| 479 | |
| 480 | typedef enum
|
| 481 | {
|
| 482 | DUMMY_ERROR, /* unexpected end of input stream */
|
| 483 | DUMMY_LIT,
|
| 484 | DUMMY_MATCH,
|
| 485 | DUMMY_REP
|
| 486 | } ELzmaDummy;
|
| 487 | |
| 488 | static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
|
| 489 | {
|
| 490 | UInt32 range = p->range;
|
| 491 | UInt32 code = p->code;
|
| 492 | const Byte *bufLimit = buf + inSize;
|
| 493 | CLzmaProb *probs = p->probs;
|
| 494 | unsigned state = p->state;
|
| 495 | ELzmaDummy res;
|
| 496 | |
| 497 | {
|
| 498 | CLzmaProb *prob;
|
| 499 | UInt32 bound;
|
| 500 | unsigned ttt;
|
| 501 | unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
|
| 502 | |
| 503 | prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
|
| 504 | IF_BIT_0_CHECK(prob)
|
| 505 | {
|
| 506 | UPDATE_0_CHECK
|
| 507 | |
| 508 | /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
|
| 509 | |
| 510 | prob = probs + Literal;
|
| 511 | if (p->checkDicSize != 0 || p->processedPos != 0)
|
| 512 | prob += (LZMA_LIT_SIZE *
|
| 513 | ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
|
| 514 | (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
|
| 515 | |
| 516 | if (state < kNumLitStates)
|
| 517 | {
|
| 518 | unsigned symbol = 1;
|
| 519 | do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
|
| 520 | }
|
| 521 | else
|
| 522 | {
|
| 523 | unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
|
| 524 | ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];
|
| 525 | unsigned offs = 0x100;
|
| 526 | unsigned symbol = 1;
|
| 527 | do
|
| 528 | {
|
| 529 | unsigned bit;
|
| 530 | CLzmaProb *probLit;
|
| 531 | matchByte <<= 1;
|
| 532 | bit = (matchByte & offs);
|
| 533 | probLit = prob + offs + bit + symbol;
|
| 534 | GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
|
| 535 | }
|
| 536 | while (symbol < 0x100);
|
| 537 | }
|
| 538 | res = DUMMY_LIT;
|
| 539 | }
|
| 540 | else
|
| 541 | {
|
| 542 | unsigned len;
|
| 543 | UPDATE_1_CHECK;
|
| 544 | |
| 545 | prob = probs + IsRep + state;
|
| 546 | IF_BIT_0_CHECK(prob)
|
| 547 | {
|
| 548 | UPDATE_0_CHECK;
|
| 549 | state = 0;
|
| 550 | prob = probs + LenCoder;
|
| 551 | res = DUMMY_MATCH;
|
| 552 | }
|
| 553 | else
|
| 554 | {
|
| 555 | UPDATE_1_CHECK;
|
| 556 | res = DUMMY_REP;
|
| 557 | prob = probs + IsRepG0 + state;
|
| 558 | IF_BIT_0_CHECK(prob)
|
| 559 | {
|
| 560 | UPDATE_0_CHECK;
|
| 561 | prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
|
| 562 | IF_BIT_0_CHECK(prob)
|
| 563 | {
|
| 564 | UPDATE_0_CHECK;
|
| 565 | NORMALIZE_CHECK;
|
| 566 | return DUMMY_REP;
|
| 567 | }
|
| 568 | else
|
| 569 | {
|
| 570 | UPDATE_1_CHECK;
|
| 571 | }
|
| 572 | }
|
| 573 | else
|
| 574 | {
|
| 575 | UPDATE_1_CHECK;
|
| 576 | prob = probs + IsRepG1 + state;
|
| 577 | IF_BIT_0_CHECK(prob)
|
| 578 | {
|
| 579 | UPDATE_0_CHECK;
|
| 580 | }
|
| 581 | else
|
| 582 | {
|
| 583 | UPDATE_1_CHECK;
|
| 584 | prob = probs + IsRepG2 + state;
|
| 585 | IF_BIT_0_CHECK(prob)
|
| 586 | {
|
| 587 | UPDATE_0_CHECK;
|
| 588 | }
|
| 589 | else
|
| 590 | {
|
| 591 | UPDATE_1_CHECK;
|
| 592 | }
|
| 593 | }
|
| 594 | }
|
| 595 | state = kNumStates;
|
| 596 | prob = probs + RepLenCoder;
|
| 597 | }
|
| 598 | {
|
| 599 | unsigned limit, offset;
|
| 600 | CLzmaProb *probLen = prob + LenChoice;
|
| 601 | IF_BIT_0_CHECK(probLen)
|
| 602 | {
|
| 603 | UPDATE_0_CHECK;
|
| 604 | probLen = prob + LenLow + (posState << kLenNumLowBits);
|
| 605 | offset = 0;
|
| 606 | limit = 1 << kLenNumLowBits;
|
| 607 | }
|
| 608 | else
|
| 609 | {
|
| 610 | UPDATE_1_CHECK;
|
| 611 | probLen = prob + LenChoice2;
|
| 612 | IF_BIT_0_CHECK(probLen)
|
| 613 | {
|
| 614 | UPDATE_0_CHECK;
|
| 615 | probLen = prob + LenMid + (posState << kLenNumMidBits);
|
| 616 | offset = kLenNumLowSymbols;
|
| 617 | limit = 1 << kLenNumMidBits;
|
| 618 | }
|
| 619 | else
|
| 620 | {
|
| 621 | UPDATE_1_CHECK;
|
| 622 | probLen = prob + LenHigh;
|
| 623 | offset = kLenNumLowSymbols + kLenNumMidSymbols;
|
| 624 | limit = 1 << kLenNumHighBits;
|
| 625 | }
|
| 626 | }
|
| 627 | TREE_DECODE_CHECK(probLen, limit, len);
|
| 628 | len += offset;
|
| 629 | }
|
| 630 | |
| 631 | if (state < 4)
|
| 632 | {
|
| 633 | unsigned posSlot;
|
| 634 | prob = probs + PosSlot +
|
| 635 | ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
|
| 636 | kNumPosSlotBits);
|
| 637 | TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
|
| 638 | if (posSlot >= kStartPosModelIndex)
|
| 639 | {
|
| 640 | int numDirectBits = ((posSlot >> 1) - 1);
|
| 641 | |
| 642 | /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
|
| 643 | |
| 644 | if (posSlot < kEndPosModelIndex)
|
| 645 | {
|
| 646 | prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
|
| 647 | }
|
| 648 | else
|
| 649 | {
|
| 650 | numDirectBits -= kNumAlignBits;
|
| 651 | do
|
| 652 | {
|
| 653 | NORMALIZE_CHECK
|
| 654 | range >>= 1;
|
| 655 | code -= range & (((code - range) >> 31) - 1);
|
| 656 | /* if (code >= range) code -= range; */
|
| 657 | }
|
| 658 | while (--numDirectBits != 0);
|
| 659 | prob = probs + Align;
|
| 660 | numDirectBits = kNumAlignBits;
|
| 661 | }
|
| 662 | {
|
| 663 | unsigned i = 1;
|
| 664 | do
|
| 665 | {
|
| 666 | GET_BIT_CHECK(prob + i, i);
|
| 667 | }
|
| 668 | while (--numDirectBits != 0);
|
| 669 | }
|
| 670 | }
|
| 671 | }
|
| 672 | }
|
| 673 | }
|
| 674 | NORMALIZE_CHECK;
|
| 675 | return res;
|
| 676 | }
|
| 677 | |
| 678 | |
| 679 | static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
|
| 680 | {
|
| 681 | p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
|
| 682 | p->range = 0xFFFFFFFF;
|
| 683 | p->needFlush = 0;
|
| 684 | }
|
| 685 | |
| 686 | void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
|
| 687 | {
|
| 688 | p->needFlush = 1;
|
| 689 | p->remainLen = 0;
|
| 690 | p->tempBufSize = 0;
|
| 691 | |
| 692 | if (initDic)
|
| 693 | {
|
| 694 | p->processedPos = 0;
|
| 695 | p->checkDicSize = 0;
|
| 696 | p->needInitState = 1;
|
| 697 | }
|
| 698 | if (initState)
|
| 699 | p->needInitState = 1;
|
| 700 | }
|
| 701 | |
| 702 | void LzmaDec_Init(CLzmaDec *p)
|
| 703 | {
|
| 704 | p->dicPos = 0;
|
| 705 | LzmaDec_InitDicAndState(p, True, True);
|
| 706 | }
|
| 707 | |
| 708 | static void LzmaDec_InitStateReal(CLzmaDec *p)
|
| 709 | {
|
| 710 | UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
|
| 711 | UInt32 i;
|
| 712 | CLzmaProb *probs = p->probs;
|
| 713 | for (i = 0; i < numProbs; i++)
|
| 714 | probs[i] = kBitModelTotal >> 1;
|
| 715 | p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
|
| 716 | p->state = 0;
|
| 717 | p->needInitState = 0;
|
| 718 | }
|
| 719 | |
| 720 | SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
|
| 721 | ELzmaFinishMode finishMode, ELzmaStatus *status)
|
| 722 | {
|
| 723 | SizeT inSize = *srcLen;
|
| 724 | (*srcLen) = 0;
|
| 725 | LzmaDec_WriteRem(p, dicLimit);
|
| 726 | |
| 727 | *status = LZMA_STATUS_NOT_SPECIFIED;
|
| 728 | |
| 729 | while (p->remainLen != kMatchSpecLenStart)
|
| 730 | {
|
| 731 | int checkEndMarkNow;
|
| 732 | |
| 733 | if (p->needFlush != 0)
|
| 734 | {
|
| 735 | for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
|
| 736 | p->tempBuf[p->tempBufSize++] = *src++;
|
| 737 | if (p->tempBufSize < RC_INIT_SIZE)
|
| 738 | {
|
| 739 | *status = LZMA_STATUS_NEEDS_MORE_INPUT;
|
| 740 | return SZ_OK;
|
| 741 | }
|
| 742 | if (p->tempBuf[0] != 0)
|
| 743 | return SZ_ERROR_DATA;
|
| 744 | |
| 745 | LzmaDec_InitRc(p, p->tempBuf);
|
| 746 | p->tempBufSize = 0;
|
| 747 | }
|
| 748 | |
| 749 | checkEndMarkNow = 0;
|
| 750 | if (p->dicPos >= dicLimit)
|
| 751 | {
|
| 752 | if (p->remainLen == 0 && p->code == 0)
|
| 753 | {
|
| 754 | *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
|
| 755 | return SZ_OK;
|
| 756 | }
|
| 757 | if (finishMode == LZMA_FINISH_ANY)
|
| 758 | {
|
| 759 | *status = LZMA_STATUS_NOT_FINISHED;
|
| 760 | return SZ_OK;
|
| 761 | }
|
| 762 | if (p->remainLen != 0)
|
| 763 | {
|
| 764 | *status = LZMA_STATUS_NOT_FINISHED;
|
| 765 | return SZ_ERROR_DATA;
|
| 766 | }
|
| 767 | checkEndMarkNow = 1;
|
| 768 | }
|
| 769 | |
| 770 | if (p->needInitState)
|
| 771 | LzmaDec_InitStateReal(p);
|
| 772 | |
| 773 | if (p->tempBufSize == 0)
|
| 774 | {
|
| 775 | SizeT processed;
|
| 776 | const Byte *bufLimit;
|
| 777 | if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
|
| 778 | {
|
| 779 | int dummyRes = LzmaDec_TryDummy(p, src, inSize);
|
| 780 | if (dummyRes == DUMMY_ERROR)
|
| 781 | {
|
| 782 | memcpy(p->tempBuf, src, inSize);
|
| 783 | p->tempBufSize = (unsigned)inSize;
|
| 784 | (*srcLen) += inSize;
|
| 785 | *status = LZMA_STATUS_NEEDS_MORE_INPUT;
|
| 786 | return SZ_OK;
|
| 787 | }
|
| 788 | if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
|
| 789 | {
|
| 790 | *status = LZMA_STATUS_NOT_FINISHED;
|
| 791 | return SZ_ERROR_DATA;
|
| 792 | }
|
| 793 | bufLimit = src;
|
| 794 | }
|
| 795 | else
|
| 796 | bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
|
| 797 | p->buf = src;
|
| 798 | if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
|
| 799 | return SZ_ERROR_DATA;
|
| 800 | processed = (SizeT)(p->buf - src);
|
| 801 | (*srcLen) += processed;
|
| 802 | src += processed;
|
| 803 | inSize -= processed;
|
| 804 | }
|
| 805 | else
|
| 806 | {
|
| 807 | unsigned rem = p->tempBufSize, lookAhead = 0;
|
| 808 | while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
|
| 809 | p->tempBuf[rem++] = src[lookAhead++];
|
| 810 | p->tempBufSize = rem;
|
| 811 | if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
|
| 812 | {
|
| 813 | int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
|
| 814 | if (dummyRes == DUMMY_ERROR)
|
| 815 | {
|
| 816 | (*srcLen) += lookAhead;
|
| 817 | *status = LZMA_STATUS_NEEDS_MORE_INPUT;
|
| 818 | return SZ_OK;
|
| 819 | }
|
| 820 | if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
|
| 821 | {
|
| 822 | *status = LZMA_STATUS_NOT_FINISHED;
|
| 823 | return SZ_ERROR_DATA;
|
| 824 | }
|
| 825 | }
|
| 826 | p->buf = p->tempBuf;
|
| 827 | if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
|
| 828 | return SZ_ERROR_DATA;
|
| 829 | lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));
|
| 830 | (*srcLen) += lookAhead;
|
| 831 | src += lookAhead;
|
| 832 | inSize -= lookAhead;
|
| 833 | p->tempBufSize = 0;
|
| 834 | }
|
| 835 | }
|
| 836 | if (p->code == 0)
|
| 837 | *status = LZMA_STATUS_FINISHED_WITH_MARK;
|
| 838 | return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
|
| 839 | }
|
| 840 | |
| 841 | SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
|
| 842 | {
|
| 843 | SizeT outSize = *destLen;
|
| 844 | SizeT inSize = *srcLen;
|
| 845 | *srcLen = *destLen = 0;
|
| 846 | for (;;)
|
| 847 | {
|
| 848 | SizeT inSizeCur = inSize, outSizeCur, dicPos;
|
| 849 | ELzmaFinishMode curFinishMode;
|
| 850 | SRes res;
|
| 851 | if (p->dicPos == p->dicBufSize)
|
| 852 | p->dicPos = 0;
|
| 853 | dicPos = p->dicPos;
|
| 854 | if (outSize > p->dicBufSize - dicPos)
|
| 855 | {
|
| 856 | outSizeCur = p->dicBufSize;
|
| 857 | curFinishMode = LZMA_FINISH_ANY;
|
| 858 | }
|
| 859 | else
|
| 860 | {
|
| 861 | outSizeCur = dicPos + outSize;
|
| 862 | curFinishMode = finishMode;
|
| 863 | }
|
| 864 | |
| 865 | res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
|
| 866 | src += inSizeCur;
|
| 867 | inSize -= inSizeCur;
|
| 868 | *srcLen += inSizeCur;
|
| 869 | outSizeCur = p->dicPos - dicPos;
|
| 870 | memcpy(dest, p->dic + dicPos, outSizeCur);
|
| 871 | dest += outSizeCur;
|
| 872 | outSize -= outSizeCur;
|
| 873 | *destLen += outSizeCur;
|
| 874 | if (res != 0)
|
| 875 | return res;
|
| 876 | if (outSizeCur == 0 || outSize == 0)
|
| 877 | return SZ_OK;
|
| 878 | }
|
| 879 | }
|
| 880 | |
| 881 | void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
|
| 882 | {
|
| 883 | alloc->Free(alloc, p->probs);
|
| 884 | p->probs = 0;
|
| 885 | }
|
| 886 | |
| 887 | static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)
|
| 888 | {
|
| 889 | alloc->Free(alloc, p->dic);
|
| 890 | p->dic = 0;
|
| 891 | }
|
| 892 | |
| 893 | void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
|
| 894 | {
|
| 895 | LzmaDec_FreeProbs(p, alloc);
|
| 896 | LzmaDec_FreeDict(p, alloc);
|
| 897 | }
|
| 898 | |
| 899 | SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
|
| 900 | {
|
| 901 | UInt32 dicSize;
|
| 902 | Byte d;
|
| 903 | |
| 904 | if (size < LZMA_PROPS_SIZE)
|
| 905 | return SZ_ERROR_UNSUPPORTED;
|
| 906 | else
|
| 907 | dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
|
| 908 |
|
| 909 | if (dicSize < LZMA_DIC_MIN)
|
| 910 | dicSize = LZMA_DIC_MIN;
|
| 911 | p->dicSize = dicSize;
|
| 912 | |
| 913 | d = data[0];
|
| 914 | if (d >= (9 * 5 * 5))
|
| 915 | return SZ_ERROR_UNSUPPORTED;
|
| 916 | |
| 917 | p->lc = d % 9;
|
| 918 | d /= 9;
|
| 919 | p->pb = d / 5;
|
| 920 | p->lp = d % 5;
|
| 921 | |
| 922 | return SZ_OK;
|
| 923 | }
|
| 924 | |
| 925 | static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
|
| 926 | {
|
| 927 | UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
|
| 928 | if (p->probs == 0 || numProbs != p->numProbs)
|
| 929 | {
|
| 930 | LzmaDec_FreeProbs(p, alloc);
|
| 931 | p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
|
| 932 | p->numProbs = numProbs;
|
| 933 | if (p->probs == 0)
|
| 934 | return SZ_ERROR_MEM;
|
| 935 | }
|
| 936 | return SZ_OK;
|
| 937 | }
|
| 938 | |
| 939 | SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
|
| 940 | {
|
| 941 | CLzmaProps propNew;
|
| 942 | RINOK(LzmaProps_Decode(&propNew, props, propsSize));
|
| 943 | RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
|
| 944 | p->prop = propNew;
|
| 945 | return SZ_OK;
|
| 946 | }
|
| 947 | |
| 948 | SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
|
| 949 | {
|
| 950 | CLzmaProps propNew;
|
| 951 | SizeT dicBufSize;
|
| 952 | RINOK(LzmaProps_Decode(&propNew, props, propsSize));
|
| 953 | RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
|
| 954 | dicBufSize = propNew.dicSize;
|
| 955 | if (p->dic == 0 || dicBufSize != p->dicBufSize)
|
| 956 | {
|
| 957 | LzmaDec_FreeDict(p, alloc);
|
| 958 | p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
|
| 959 | if (p->dic == 0)
|
| 960 | {
|
| 961 | LzmaDec_FreeProbs(p, alloc);
|
| 962 | return SZ_ERROR_MEM;
|
| 963 | }
|
| 964 | }
|
| 965 | p->dicBufSize = dicBufSize;
|
| 966 | p->prop = propNew;
|
| 967 | return SZ_OK;
|
| 968 | }
|
| 969 | |
| 970 | SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
|
| 971 | const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
|
| 972 | ELzmaStatus *status, ISzAlloc *alloc)
|
| 973 | {
|
| 974 | CLzmaDec p;
|
| 975 | SRes res;
|
| 976 | SizeT outSize = *destLen, inSize = *srcLen;
|
| 977 | *destLen = *srcLen = 0;
|
| 978 | *status = LZMA_STATUS_NOT_SPECIFIED;
|
| 979 | if (inSize < RC_INIT_SIZE)
|
| 980 | return SZ_ERROR_INPUT_EOF;
|
| 981 | LzmaDec_Construct(&p);
|
| 982 | RINOK(LzmaDec_AllocateProbs(&p, propData, propSize, alloc));
|
| 983 | p.dic = dest;
|
| 984 | p.dicBufSize = outSize;
|
| 985 | LzmaDec_Init(&p);
|
| 986 | *srcLen = inSize;
|
| 987 | res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
|
| 988 | *destLen = p.dicPos;
|
| 989 | if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
|
| 990 | res = SZ_ERROR_INPUT_EOF;
|
| 991 | LzmaDec_FreeProbs(&p, alloc);
|
| 992 | return res;
|
| 993 | }
|
src/lib/lib7z/Ppmd7.c
| r0 | r14440 | |
| 1 | /* Ppmd7.c -- PPMdH codec
|
| 2 | 2010-03-12 : Igor Pavlov : Public domain
|
| 3 | This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
|
| 4 | |
| 5 | #include <memory.h>
|
| 6 | |
| 7 | #include "Ppmd7.h"
|
| 8 | |
| 9 | const Byte PPMD7_kExpEscape[16] = { 25, 14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2 };
|
| 10 | static const UInt16 kInitBinEsc[] = { 0x3CDD, 0x1F3F, 0x59BF, 0x48F3, 0x64A1, 0x5ABC, 0x6632, 0x6051};
|
| 11 | |
| 12 | #define MAX_FREQ 124
|
| 13 | #define UNIT_SIZE 12
|
| 14 | |
| 15 | #define U2B(nu) ((UInt32)(nu) * UNIT_SIZE)
|
| 16 | #define U2I(nu) (p->Units2Indx[(nu) - 1])
|
| 17 | #define I2U(indx) (p->Indx2Units[indx])
|
| 18 | |
| 19 | #ifdef PPMD_32BIT
|
| 20 | #define REF(ptr) (ptr)
|
| 21 | #else
|
| 22 | #define REF(ptr) ((UInt32)((Byte *)(ptr) - (p)->Base))
|
| 23 | #endif
|
| 24 | |
| 25 | #define STATS_REF(ptr) ((CPpmd_State_Ref)REF(ptr))
|
| 26 | |
| 27 | #define CTX(ref) ((CPpmd7_Context *)Ppmd7_GetContext(p, ref))
|
| 28 | #define STATS(ctx) Ppmd7_GetStats(p, ctx)
|
| 29 | #define ONE_STATE(ctx) Ppmd7Context_OneState(ctx)
|
| 30 | #define SUFFIX(ctx) CTX((ctx)->Suffix)
|
| 31 | |
| 32 | typedef CPpmd7_Context * CTX_PTR;
|
| 33 | |
| 34 | struct CPpmd7_Node_;
|
| 35 | |
| 36 | typedef
|
| 37 | #ifdef PPMD_32BIT
|
| 38 | struct CPpmd7_Node_ *
|
| 39 | #else
|
| 40 | UInt32
|
| 41 | #endif
|
| 42 | CPpmd7_Node_Ref;
|
| 43 | |
| 44 | typedef struct CPpmd7_Node_
|
| 45 | {
|
| 46 | UInt16 Stamp; /* must be at offset 0 as CPpmd7_Context::NumStats. Stamp=0 means free */
|
| 47 | UInt16 NU;
|
| 48 | CPpmd7_Node_Ref Next; /* must be at offset >= 4 */
|
| 49 | CPpmd7_Node_Ref Prev;
|
| 50 | } CPpmd7_Node;
|
| 51 | |
| 52 | #ifdef PPMD_32BIT
|
| 53 | #define NODE(ptr) (ptr)
|
| 54 | #else
|
| 55 | #define NODE(offs) ((CPpmd7_Node *)(p->Base + (offs)))
|
| 56 | #endif
|
| 57 | |
| 58 | void Ppmd7_Construct(CPpmd7 *p)
|
| 59 | {
|
| 60 | unsigned i, k, m;
|
| 61 | |
| 62 | p->Base = 0;
|
| 63 | |
| 64 | for (i = 0, k = 0; i < PPMD_NUM_INDEXES; i++)
|
| 65 | {
|
| 66 | unsigned step = (i >= 12 ? 4 : (i >> 2) + 1);
|
| 67 | do { p->Units2Indx[k++] = (Byte)i; } while(--step);
|
| 68 | p->Indx2Units[i] = (Byte)k;
|
| 69 | }
|
| 70 | |
| 71 | p->NS2BSIndx[0] = (0 << 1);
|
| 72 | p->NS2BSIndx[1] = (1 << 1);
|
| 73 | memset(p->NS2BSIndx + 2, (2 << 1), 9);
|
| 74 | memset(p->NS2BSIndx + 11, (3 << 1), 256 - 11);
|
| 75 | |
| 76 | for (i = 0; i < 3; i++)
|
| 77 | p->NS2Indx[i] = (Byte)i;
|
| 78 | for (m = i, k = 1; i < 256; i++)
|
| 79 | {
|
| 80 | p->NS2Indx[i] = (Byte)m;
|
| 81 | if (--k == 0)
|
| 82 | k = (++m) - 2;
|
| 83 | }
|
| 84 | |
| 85 | memset(p->HB2Flag, 0, 0x40);
|
| 86 | memset(p->HB2Flag + 0x40, 8, 0x100 - 0x40);
|
| 87 | }
|
| 88 | |
| 89 | void Ppmd7_Free(CPpmd7 *p, ISzAlloc *alloc)
|
| 90 | {
|
| 91 | alloc->Free(alloc, p->Base);
|
| 92 | p->Size = 0;
|
| 93 | p->Base = 0;
|
| 94 | }
|
| 95 | |
| 96 | Bool Ppmd7_Alloc(CPpmd7 *p, UInt32 size, ISzAlloc *alloc)
|
| 97 | {
|
| 98 | if (p->Base == 0 || p->Size != size)
|
| 99 | {
|
| 100 | Ppmd7_Free(p, alloc);
|
| 101 | p->AlignOffset =
|
| 102 | #ifdef PPMD_32BIT
|
| 103 | (4 - size) & 3;
|
| 104 | #else
|
| 105 | 4 - (size & 3);
|
| 106 | #endif
|
| 107 | if ((p->Base = (Byte *)alloc->Alloc(alloc, p->AlignOffset + size
|
| 108 | #ifndef PPMD_32BIT
|
| 109 | + UNIT_SIZE
|
| 110 | #endif
|
| 111 | )) == 0)
|
| 112 | return False;
|
| 113 | p->Size = size;
|
| 114 | }
|
| 115 | return True;
|
| 116 | }
|
| 117 | |
| 118 | static void InsertNode(CPpmd7 *p, void *node, unsigned indx)
|
| 119 | {
|
| 120 | *((CPpmd_Void_Ref *)node) = p->FreeList[indx];
|
| 121 | p->FreeList[indx] = REF(node);
|
| 122 | }
|
| 123 | |
| 124 | static void *RemoveNode(CPpmd7 *p, unsigned indx)
|
| 125 | {
|
| 126 | CPpmd_Void_Ref *node = (CPpmd_Void_Ref *)Ppmd7_GetPtr(p, p->FreeList[indx]);
|
| 127 | p->FreeList[indx] = *node;
|
| 128 | return node;
|
| 129 | }
|
| 130 | |
| 131 | static void SplitBlock(CPpmd7 *p, void *ptr, unsigned oldIndx, unsigned newIndx)
|
| 132 | {
|
| 133 | unsigned i, nu = I2U(oldIndx) - I2U(newIndx);
|
| 134 | ptr = (Byte *)ptr + U2B(I2U(newIndx));
|
| 135 | if (I2U(i = U2I(nu)) != nu)
|
| 136 | {
|
| 137 | unsigned k = I2U(--i);
|
| 138 | InsertNode(p, ((Byte *)ptr) + U2B(k), nu - k - 1);
|
| 139 | }
|
| 140 | InsertNode(p, ptr, i);
|
| 141 | }
|
| 142 | |
| 143 | static void GlueFreeBlocks(CPpmd7 *p)
|
| 144 | {
|
| 145 | #ifdef PPMD_32BIT
|
| 146 | CPpmd7_Node headItem;
|
| 147 | CPpmd7_Node_Ref head = &headItem;
|
| 148 | #else
|
| 149 | CPpmd7_Node_Ref head = p->AlignOffset + p->Size;
|
| 150 | #endif
|
| 151 | |
| 152 | CPpmd7_Node_Ref n = head;
|
| 153 | unsigned i;
|
| 154 | |
| 155 | p->GlueCount = 255;
|
| 156 | |
| 157 | /* create doubly-linked list of free blocks */
|
| 158 | for (i = 0; i < PPMD_NUM_INDEXES; i++)
|
| 159 | {
|
| 160 | UInt16 nu = I2U(i);
|
| 161 | CPpmd7_Node_Ref next = (CPpmd7_Node_Ref)p->FreeList[i];
|
| 162 | p->FreeList[i] = 0;
|
| 163 | while (next != 0)
|
| 164 | {
|
| 165 | CPpmd7_Node *node = NODE(next);
|
| 166 | node->Next = n;
|
| 167 | n = NODE(n)->Prev = next;
|
| 168 | next = *(const CPpmd7_Node_Ref *)node;
|
| 169 | node->Stamp = 0;
|
| 170 | node->NU = (UInt16)nu;
|
| 171 | }
|
| 172 | }
|
| 173 | NODE(head)->Stamp = 1;
|
| 174 | NODE(head)->Next = n;
|
| 175 | NODE(n)->Prev = head;
|
| 176 | if (p->LoUnit != p->HiUnit)
|
| 177 | ((CPpmd7_Node *)p->LoUnit)->Stamp = 1;
|
| 178 | |
| 179 | /* Glue free blocks */
|
| 180 | while (n != head)
|
| 181 | {
|
| 182 | CPpmd7_Node *node = NODE(n);
|
| 183 | UInt32 nu = (UInt32)node->NU;
|
| 184 | for (;;)
|
| 185 | {
|
| 186 | CPpmd7_Node *node2 = NODE(n) + nu;
|
| 187 | nu += node2->NU;
|
| 188 | if (node2->Stamp != 0 || nu >= 0x10000)
|
| 189 | break;
|
| 190 | NODE(node2->Prev)->Next = node2->Next;
|
| 191 | NODE(node2->Next)->Prev = node2->Prev;
|
| 192 | node->NU = (UInt16)nu;
|
| 193 | }
|
| 194 | n = node->Next;
|
| 195 | }
|
| 196 | |
| 197 | /* Fill lists of free blocks */
|
| 198 | for (n = NODE(head)->Next; n != head;)
|
| 199 | {
|
| 200 | CPpmd7_Node *node = NODE(n);
|
| 201 | unsigned nu;
|
| 202 | CPpmd7_Node_Ref next = node->Next;
|
| 203 | for (nu = node->NU; nu > 128; nu -= 128, node += 128)
|
| 204 | InsertNode(p, node, PPMD_NUM_INDEXES - 1);
|
| 205 | if (I2U(i = U2I(nu)) != nu)
|
| 206 | {
|
| 207 | unsigned k = I2U(--i);
|
| 208 | InsertNode(p, node + k, nu - k - 1);
|
| 209 | }
|
| 210 | InsertNode(p, node, i);
|
| 211 | n = next;
|
| 212 | }
|
| 213 | }
|
| 214 | |
| 215 | static void *AllocUnitsRare(CPpmd7 *p, unsigned indx)
|
| 216 | {
|
| 217 | unsigned i;
|
| 218 | void *retVal;
|
| 219 | if (p->GlueCount == 0)
|
| 220 | {
|
| 221 | GlueFreeBlocks(p);
|
| 222 | if (p->FreeList[indx] != 0)
|
| 223 | return RemoveNode(p, indx);
|
| 224 | }
|
| 225 | i = indx;
|
| 226 | do
|
| 227 | {
|
| 228 | if (++i == PPMD_NUM_INDEXES)
|
| 229 | {
|
| 230 | UInt32 numBytes = U2B(I2U(indx));
|
| 231 | p->GlueCount--;
|
| 232 | return ((UInt32)(p->UnitsStart - p->Text) > numBytes) ? (p->UnitsStart -= numBytes) : (NULL);
|
| 233 | }
|
| 234 | }
|
| 235 | while (p->FreeList[i] == 0);
|
| 236 | retVal = RemoveNode(p, i);
|
| 237 | SplitBlock(p, retVal, i, indx);
|
| 238 | return retVal;
|
| 239 | }
|
| 240 | |
| 241 | static void *AllocUnits(CPpmd7 *p, unsigned indx)
|
| 242 | {
|
| 243 | UInt32 numBytes;
|
| 244 | if (p->FreeList[indx] != 0)
|
| 245 | return RemoveNode(p, indx);
|
| 246 | numBytes = U2B(I2U(indx));
|
| 247 | if (numBytes <= (UInt32)(p->HiUnit - p->LoUnit))
|
| 248 | {
|
| 249 | void *retVal = p->LoUnit;
|
| 250 | p->LoUnit += numBytes;
|
| 251 | return retVal;
|
| 252 | }
|
| 253 | return AllocUnitsRare(p, indx);
|
| 254 | }
|
| 255 | |
| 256 | #define MyMem12Cpy(dest, src, num) \
|
| 257 | { UInt32 *d = (UInt32 *)dest; const UInt32 *s = (const UInt32 *)src; UInt32 n = num; \
|
| 258 | do { d[0] = s[0]; d[1] = s[1]; d[2] = s[2]; s += 3; d += 3; } while(--n); }
|
| 259 | |
| 260 | static void *ShrinkUnits(CPpmd7 *p, void *oldPtr, unsigned oldNU, unsigned newNU)
|
| 261 | {
|
| 262 | unsigned i0 = U2I(oldNU);
|
| 263 | unsigned i1 = U2I(newNU);
|
| 264 | if (i0 == i1)
|
| 265 | return oldPtr;
|
| 266 | if (p->FreeList[i1] != 0)
|
| 267 | {
|
| 268 | void *ptr = RemoveNode(p, i1);
|
| 269 | MyMem12Cpy(ptr, oldPtr, newNU);
|
| 270 | InsertNode(p, oldPtr, i0);
|
| 271 | return ptr;
|
| 272 | }
|
| 273 | SplitBlock(p, oldPtr, i0, i1);
|
| 274 | return oldPtr;
|
| 275 | }
|
| 276 | |
| 277 | #define SUCCESSOR(p) ((CPpmd_Void_Ref)((p)->SuccessorLow | ((UInt32)(p)->SuccessorHigh << 16)))
|
| 278 | |
| 279 | static void SetSuccessor(CPpmd_State *p, CPpmd_Void_Ref v)
|
| 280 | {
|
| 281 | (p)->SuccessorLow = (UInt16)((UInt32)(v) & 0xFFFF);
|
| 282 | (p)->SuccessorHigh = (UInt16)(((UInt32)(v) >> 16) & 0xFFFF);
|
| 283 | }
|
| 284 | |
| 285 | static void RestartModel(CPpmd7 *p)
|
| 286 | {
|
| 287 | unsigned i, k, m;
|
| 288 | |
| 289 | memset(p->FreeList, 0, sizeof(p->FreeList));
|
| 290 | p->Text = p->Base + p->AlignOffset;
|
| 291 | p->HiUnit = p->Text + p->Size;
|
| 292 | p->LoUnit = p->UnitsStart = p->HiUnit - p->Size / 8 / UNIT_SIZE * 7 * UNIT_SIZE;
|
| 293 | p->GlueCount = 0;
|
| 294 | |
| 295 | p->OrderFall = p->MaxOrder;
|
| 296 | p->RunLength = p->InitRL = -(Int32)((p->MaxOrder < 12) ? p->MaxOrder : 12) - 1;
|
| 297 | p->PrevSuccess = 0;
|
| 298 | |
| 299 | p->MinContext = p->MaxContext = (CTX_PTR)(p->HiUnit -= UNIT_SIZE); /* AllocContext(p); */
|
| 300 | p->MinContext->Suffix = 0;
|
| 301 | p->MinContext->NumStats = 256;
|
| 302 | p->MinContext->SummFreq = 256 + 1;
|
| 303 | p->FoundState = (CPpmd_State *)p->LoUnit; /* AllocUnits(p, PPMD_NUM_INDEXES - 1); */
|
| 304 | p->LoUnit += U2B(256 / 2);
|
| 305 | p->MinContext->Stats = REF(p->FoundState);
|
| 306 | for (i = 0; i < 256; i++)
|
| 307 | {
|
| 308 | CPpmd_State *s = &p->FoundState[i];
|
| 309 | s->Symbol = (Byte)i;
|
| 310 | s->Freq = 1;
|
| 311 | SetSuccessor(s, 0);
|
| 312 | }
|
| 313 | |
| 314 | for (i = 0; i < 128; i++)
|
| 315 | for (k = 0; k < 8; k++)
|
| 316 | {
|
| 317 | UInt16 *dest = p->BinSumm[i] + k;
|
| 318 | UInt16 val = (UInt16)(PPMD_BIN_SCALE - kInitBinEsc[k] / (i + 2));
|
| 319 | for (m = 0; m < 64; m += 8)
|
| 320 | dest[m] = val;
|
| 321 | }
|
| 322 | |
| 323 | for (i = 0; i < 25; i++)
|
| 324 | for (k = 0; k < 16; k++)
|
| 325 | {
|
| 326 | CPpmd_See *s = &p->See[i][k];
|
| 327 | s->Summ = (UInt16)((5 * i + 10) << (s->Shift = PPMD_PERIOD_BITS - 4));
|
| 328 | s->Count = 4;
|
| 329 | }
|
| 330 | }
|
| 331 | |
| 332 | void Ppmd7_Init(CPpmd7 *p, unsigned maxOrder)
|
| 333 | {
|
| 334 | p->MaxOrder = maxOrder;
|
| 335 | RestartModel(p);
|
| 336 | p->DummySee.Shift = PPMD_PERIOD_BITS;
|
| 337 | p->DummySee.Summ = 0; /* unused */
|
| 338 | p->DummySee.Count = 64; /* unused */
|
| 339 | }
|
| 340 | |
| 341 | static CTX_PTR CreateSuccessors(CPpmd7 *p, Bool skip)
|
| 342 | {
|
| 343 | CPpmd_State upState;
|
| 344 | CTX_PTR c = p->MinContext;
|
| 345 | CPpmd_Byte_Ref upBranch = (CPpmd_Byte_Ref)SUCCESSOR(p->FoundState);
|
| 346 | CPpmd_State *ps[PPMD7_MAX_ORDER];
|
| 347 | unsigned numPs = 0;
|
| 348 | |
| 349 | if (!skip)
|
| 350 | ps[numPs++] = p->FoundState;
|
| 351 | |
| 352 | while (c->Suffix)
|
| 353 | {
|
| 354 | CPpmd_Void_Ref successor;
|
| 355 | CPpmd_State *s;
|
| 356 | c = SUFFIX(c);
|
| 357 | if (c->NumStats != 1)
|
| 358 | {
|
| 359 | for (s = STATS(c); s->Symbol != p->FoundState->Symbol; s++);
|
| 360 | }
|
| 361 | else
|
| 362 | s = ONE_STATE(c);
|
| 363 | successor = SUCCESSOR(s);
|
| 364 | if (successor != upBranch)
|
| 365 | {
|
| 366 | c = CTX(successor);
|
| 367 | if (numPs == 0)
|
| 368 | return c;
|
| 369 | break;
|
| 370 | }
|
| 371 | ps[numPs++] = s;
|
| 372 | }
|
| 373 | |
| 374 | upState.Symbol = *(const Byte *)Ppmd7_GetPtr(p, upBranch);
|
| 375 | SetSuccessor(&upState, upBranch + 1);
|
| 376 | |
| 377 | if (c->NumStats == 1)
|
| 378 | upState.Freq = ONE_STATE(c)->Freq;
|
| 379 | else
|
| 380 | {
|
| 381 | UInt32 cf, s0;
|
| 382 | CPpmd_State *s;
|
| 383 | for (s = STATS(c); s->Symbol != upState.Symbol; s++);
|
| 384 | cf = s->Freq - 1;
|
| 385 | s0 = c->SummFreq - c->NumStats - cf;
|
| 386 | upState.Freq = (Byte)(1 + ((2 * cf <= s0) ? (5 * cf > s0) : ((2 * cf + 3 * s0 - 1) / (2 * s0))));
|
| 387 | }
|
| 388 | |
| 389 | do
|
| 390 | {
|
| 391 | /* Create Child */
|
| 392 | CTX_PTR c1; /* = AllocContext(p); */
|
| 393 | if (p->HiUnit != p->LoUnit)
|
| 394 | c1 = (CTX_PTR)(p->HiUnit -= UNIT_SIZE);
|
| 395 | else if (p->FreeList[0] != 0)
|
| 396 | c1 = (CTX_PTR)RemoveNode(p, 0);
|
| 397 | else
|
| 398 | {
|
| 399 | c1 = (CTX_PTR)AllocUnitsRare(p, 0);
|
| 400 | if (!c1)
|
| 401 | return NULL;
|
| 402 | }
|
| 403 | c1->NumStats = 1;
|
| 404 | *ONE_STATE(c1) = upState;
|
| 405 | c1->Suffix = REF(c);
|
| 406 | SetSuccessor(ps[--numPs], REF(c1));
|
| 407 | c = c1;
|
| 408 | }
|
| 409 | while (numPs != 0);
|
| 410 | |
| 411 | return c;
|
| 412 | }
|
| 413 | |
| 414 | static void SwapStates(CPpmd_State *t1, CPpmd_State *t2)
|
| 415 | {
|
| 416 | CPpmd_State tmp = *t1;
|
| 417 | *t1 = *t2;
|
| 418 | *t2 = tmp;
|
| 419 | }
|
| 420 | |
| 421 | static void UpdateModel(CPpmd7 *p)
|
| 422 | {
|
| 423 | CPpmd_Void_Ref successor, fSuccessor = SUCCESSOR(p->FoundState);
|
| 424 | CTX_PTR c;
|
| 425 | unsigned s0, ns;
|
| 426 | |
| 427 | if (p->FoundState->Freq < MAX_FREQ / 4 && p->MinContext->Suffix != 0)
|
| 428 | {
|
| 429 | c = SUFFIX(p->MinContext);
|
| 430 | |
| 431 | if (c->NumStats == 1)
|
| 432 | {
|
| 433 | CPpmd_State *s = ONE_STATE(c);
|
| 434 | if (s->Freq < 32)
|
| 435 | s->Freq++;
|
| 436 | }
|
| 437 | else
|
| 438 | {
|
| 439 | CPpmd_State *s = STATS(c);
|
| 440 | if (s->Symbol != p->FoundState->Symbol)
|
| 441 | {
|
| 442 | do { s++; } while (s->Symbol != p->FoundState->Symbol);
|
| 443 | if (s[0].Freq >= s[-1].Freq)
|
| 444 | {
|
| 445 | SwapStates(&s[0], &s[-1]);
|
| 446 | s--;
|
| 447 | }
|
| 448 | }
|
| 449 | if (s->Freq < MAX_FREQ - 9)
|
| 450 | {
|
| 451 | s->Freq += 2;
|
| 452 | c->SummFreq += 2;
|
| 453 | }
|
| 454 | }
|
| 455 | }
|
| 456 | |
| 457 | if (p->OrderFall == 0)
|
| 458 | {
|
| 459 | p->MinContext = p->MaxContext = CreateSuccessors(p, True);
|
| 460 | if (p->MinContext == 0)
|
| 461 | {
|
| 462 | RestartModel(p);
|
| 463 | return;
|
| 464 | }
|
| 465 | SetSuccessor(p->FoundState, REF(p->MinContext));
|
| 466 | return;
|
| 467 | }
|
| 468 | |
| 469 | *p->Text++ = p->FoundState->Symbol;
|
| 470 | successor = REF(p->Text);
|
| 471 | if (p->Text >= p->UnitsStart)
|
| 472 | {
|
| 473 | RestartModel(p);
|
| 474 | return;
|
| 475 | }
|
| 476 | |
| 477 | if (fSuccessor)
|
| 478 | {
|
| 479 | if (fSuccessor <= successor)
|
| 480 | {
|
| 481 | CTX_PTR cs = CreateSuccessors(p, False);
|
| 482 | if (cs == NULL)
|
| 483 | {
|
| 484 | RestartModel(p);
|
| 485 | return;
|
| 486 | }
|
| 487 | fSuccessor = REF(cs);
|
| 488 | }
|
| 489 | if (--p->OrderFall == 0)
|
| 490 | {
|
| 491 | successor = fSuccessor;
|
| 492 | p->Text -= (p->MaxContext != p->MinContext);
|
| 493 | }
|
| 494 | }
|
| 495 | else
|
| 496 | {
|
| 497 | SetSuccessor(p->FoundState, successor);
|
| 498 | fSuccessor = REF(p->MinContext);
|
| 499 | }
|
| 500 | |
| 501 | s0 = p->MinContext->SummFreq - (ns = p->MinContext->NumStats) - (p->FoundState->Freq - 1);
|
| 502 | |
| 503 | for (c = p->MaxContext; c != p->MinContext; c = SUFFIX(c))
|
| 504 | {
|
| 505 | unsigned ns1;
|
| 506 | UInt32 cf, sf;
|
| 507 | if ((ns1 = c->NumStats) != 1)
|
| 508 | {
|
| 509 | if ((ns1 & 1) == 0)
|
| 510 | {
|
| 511 | /* Expand for one UNIT */
|
| 512 | unsigned oldNU = ns1 >> 1;
|
| 513 | unsigned i = U2I(oldNU);
|
| 514 | if (i != U2I(oldNU + 1))
|
| 515 | {
|
| 516 | void *ptr = AllocUnits(p, i + 1);
|
| 517 | void *oldPtr;
|
| 518 | if (!ptr)
|
| 519 | {
|
| 520 | RestartModel(p);
|
| 521 | return;
|
| 522 | }
|
| 523 | oldPtr = STATS(c);
|
| 524 | MyMem12Cpy(ptr, oldPtr, oldNU);
|
| 525 | InsertNode(p, oldPtr, i);
|
| 526 | c->Stats = STATS_REF(ptr);
|
| 527 | }
|
| 528 | }
|
| 529 | c->SummFreq = (UInt16)(c->SummFreq + (2 * ns1 < ns) + 2 * ((4 * ns1 <= ns) & (c->SummFreq <= 8 * ns1)));
|
| 530 | }
|
| 531 | else
|
| 532 | {
|
| 533 | CPpmd_State *s = (CPpmd_State*)AllocUnits(p, 0);
|
| 534 | if (!s)
|
| 535 | {
|
| 536 | RestartModel(p);
|
| 537 | return;
|
| 538 | }
|
| 539 | *s = *ONE_STATE(c);
|
| 540 | c->Stats = REF(s);
|
| 541 | if (s->Freq < MAX_FREQ / 4 - 1)
|
| 542 | s->Freq <<= 1;
|
| 543 | else
|
| 544 | s->Freq = MAX_FREQ - 4;
|
| 545 | c->SummFreq = (UInt16)(s->Freq + p->InitEsc + (ns > 3));
|
| 546 | }
|
| 547 | cf = 2 * (UInt32)p->FoundState->Freq * (c->SummFreq + 6);
|
| 548 | sf = (UInt32)s0 + c->SummFreq;
|
| 549 | if (cf < 6 * sf)
|
| 550 | {
|
| 551 | cf = 1 + (cf > sf) + (cf >= 4 * sf);
|
| 552 | c->SummFreq += 3;
|
| 553 | }
|
| 554 | else
|
| 555 | {
|
| 556 | cf = 4 + (cf >= 9 * sf) + (cf >= 12 * sf) + (cf >= 15 * sf);
|
| 557 | c->SummFreq = (UInt16)(c->SummFreq + cf);
|
| 558 | }
|
| 559 | {
|
| 560 | CPpmd_State *s = STATS(c) + ns1;
|
| 561 | SetSuccessor(s, successor);
|
| 562 | s->Symbol = p->FoundState->Symbol;
|
| 563 | s->Freq = (Byte)cf;
|
| 564 | c->NumStats = (UInt16)(ns1 + 1);
|
| 565 | }
|
| 566 | }
|
| 567 | p->MaxContext = p->MinContext = CTX(fSuccessor);
|
| 568 | }
|
| 569 | |
| 570 | static void Rescale(CPpmd7 *p)
|
| 571 | {
|
| 572 | unsigned i, adder, sumFreq, escFreq;
|
| 573 | CPpmd_State *stats = STATS(p->MinContext);
|
| 574 | CPpmd_State *s = p->FoundState;
|
| 575 | {
|
| 576 | CPpmd_State tmp = *s;
|
| 577 | for (; s != stats; s--)
|
| 578 | s[0] = s[-1];
|
| 579 | *s = tmp;
|
| 580 | }
|
| 581 | escFreq = p->MinContext->SummFreq - s->Freq;
|
| 582 | s->Freq += 4;
|
| 583 | adder = (p->OrderFall != 0);
|
| 584 | s->Freq = (Byte)((s->Freq + adder) >> 1);
|
| 585 | sumFreq = s->Freq;
|
| 586 | |
| 587 | i = p->MinContext->NumStats - 1;
|
| 588 | do
|
| 589 | {
|
| 590 | escFreq -= (++s)->Freq;
|
| 591 | s->Freq = (Byte)((s->Freq + adder) >> 1);
|
| 592 | sumFreq += s->Freq;
|
| 593 | if (s[0].Freq > s[-1].Freq)
|
| 594 | {
|
| 595 | CPpmd_State *s1 = s;
|
| 596 | CPpmd_State tmp = *s1;
|
| 597 | do
|
| 598 | s1[0] = s1[-1];
|
| 599 | while (--s1 != stats && tmp.Freq > s1[-1].Freq);
|
| 600 | *s1 = tmp;
|
| 601 | }
|
| 602 | }
|
| 603 | while (--i);
|
| 604 | |
| 605 | if (s->Freq == 0)
|
| 606 | {
|
| 607 | unsigned numStats = p->MinContext->NumStats;
|
| 608 | unsigned n0, n1;
|
| 609 | do { i++; } while ((--s)->Freq == 0);
|
| 610 | escFreq += i;
|
| 611 | p->MinContext->NumStats = (UInt16)(p->MinContext->NumStats - i);
|
| 612 | if (p->MinContext->NumStats == 1)
|
| 613 | {
|
| 614 | CPpmd_State tmp = *stats;
|
| 615 | do
|
| 616 | {
|
| 617 | tmp.Freq = (Byte)(tmp.Freq - (tmp.Freq >> 1));
|
| 618 | escFreq >>= 1;
|
| 619 | }
|
| 620 | while (escFreq > 1);
|
| 621 | InsertNode(p, stats, U2I(((numStats + 1) >> 1)));
|
| 622 | *(p->FoundState = ONE_STATE(p->MinContext)) = tmp;
|
| 623 | return;
|
| 624 | }
|
| 625 | n0 = (numStats + 1) >> 1;
|
| 626 | n1 = (p->MinContext->NumStats + 1) >> 1;
|
| 627 | if (n0 != n1)
|
| 628 | p->MinContext->Stats = STATS_REF(ShrinkUnits(p, stats, n0, n1));
|
| 629 | }
|
| 630 | p->MinContext->SummFreq = (UInt16)(sumFreq + escFreq - (escFreq >> 1));
|
| 631 | p->FoundState = STATS(p->MinContext);
|
| 632 | }
|
| 633 | |
| 634 | CPpmd_See *Ppmd7_MakeEscFreq(CPpmd7 *p, unsigned numMasked, UInt32 *escFreq)
|
| 635 | {
|
| 636 | CPpmd_See *see;
|
| 637 | unsigned nonMasked = p->MinContext->NumStats - numMasked;
|
| 638 | if (p->MinContext->NumStats != 256)
|
| 639 | {
|
| 640 | see = p->See[p->NS2Indx[nonMasked - 1]] +
|
| 641 | (nonMasked < (unsigned)SUFFIX(p->MinContext)->NumStats - p->MinContext->NumStats) +
|
| 642 | 2 * (p->MinContext->SummFreq < 11 * p->MinContext->NumStats) +
|
| 643 | 4 * (numMasked > nonMasked) +
|
| 644 | p->HiBitsFlag;
|
| 645 | {
|
| 646 | unsigned r = (see->Summ >> see->Shift);
|
| 647 | see->Summ = (UInt16)(see->Summ - r);
|
| 648 | *escFreq = r + (r == 0);
|
| 649 | }
|
| 650 | }
|
| 651 | else
|
| 652 | {
|
| 653 | see = &p->DummySee;
|
| 654 | *escFreq = 1;
|
| 655 | }
|
| 656 | return see;
|
| 657 | }
|
| 658 | |
| 659 | static void NextContext(CPpmd7 *p)
|
| 660 | {
|
| 661 | CTX_PTR c = CTX(SUCCESSOR(p->FoundState));
|
| 662 | if (p->OrderFall == 0 && (Byte *)c > p->Text)
|
| 663 | p->MinContext = p->MaxContext = c;
|
| 664 | else
|
| 665 | UpdateModel(p);
|
| 666 | }
|
| 667 | |
| 668 | void Ppmd7_Update1(CPpmd7 *p)
|
| 669 | {
|
| 670 | CPpmd_State *s = p->FoundState;
|
| 671 | s->Freq += 4;
|
| 672 | p->MinContext->SummFreq += 4;
|
| 673 | if (s[0].Freq > s[-1].Freq)
|
| 674 | {
|
| 675 | SwapStates(&s[0], &s[-1]);
|
| 676 | p->FoundState = --s;
|
| 677 | if (s->Freq > MAX_FREQ)
|
| 678 | Rescale(p);
|
| 679 | }
|
| 680 | NextContext(p);
|
| 681 | }
|
| 682 | |
| 683 | void Ppmd7_Update1_0(CPpmd7 *p)
|
| 684 | {
|
| 685 | p->PrevSuccess = (2 * p->FoundState->Freq > p->MinContext->SummFreq);
|
| 686 | p->RunLength += p->PrevSuccess;
|
| 687 | p->MinContext->SummFreq += 4;
|
| 688 | if ((p->FoundState->Freq += 4) > MAX_FREQ)
|
| 689 | Rescale(p);
|
| 690 | NextContext(p);
|
| 691 | }
|
| 692 | |
| 693 | void Ppmd7_UpdateBin(CPpmd7 *p)
|
| 694 | {
|
| 695 | p->FoundState->Freq = (Byte)(p->FoundState->Freq + (p->FoundState->Freq < 128 ? 1: 0));
|
| 696 | p->PrevSuccess = 1;
|
| 697 | p->RunLength++;
|
| 698 | NextContext(p);
|
| 699 | }
|
| 700 | |
| 701 | void Ppmd7_Update2(CPpmd7 *p)
|
| 702 | {
|
| 703 | p->MinContext->SummFreq += 4;
|
| 704 | if ((p->FoundState->Freq += 4) > MAX_FREQ)
|
| 705 | Rescale(p);
|
| 706 | p->RunLength = p->InitRL;
|
| 707 | UpdateModel(p);
|
| 708 | }
|
src/lib/lib7z/LzFindMt.c
| r0 | r14440 | |
| 1 | /* LzFindMt.c -- multithreaded Match finder for LZ algorithms
|
| 2 | 2009-09-20 : Igor Pavlov : Public domain */
|
| 3 | |
| 4 | #include "LzHash.h"
|
| 5 | |
| 6 | #include "LzFindMt.h"
|
| 7 | |
| 8 | void MtSync_Construct(CMtSync *p)
|
| 9 | {
|
| 10 | p->wasCreated = False;
|
| 11 | p->csWasInitialized = False;
|
| 12 | p->csWasEntered = False;
|
| 13 | Thread_Construct(&p->thread);
|
| 14 | Event_Construct(&p->canStart);
|
| 15 | Event_Construct(&p->wasStarted);
|
| 16 | Event_Construct(&p->wasStopped);
|
| 17 | Semaphore_Construct(&p->freeSemaphore);
|
| 18 | Semaphore_Construct(&p->filledSemaphore);
|
| 19 | }
|
| 20 | |
| 21 | void MtSync_GetNextBlock(CMtSync *p)
|
| 22 | {
|
| 23 | if (p->needStart)
|
| 24 | {
|
| 25 | p->numProcessedBlocks = 1;
|
| 26 | p->needStart = False;
|
| 27 | p->stopWriting = False;
|
| 28 | p->exit = False;
|
| 29 | Event_Reset(&p->wasStarted);
|
| 30 | Event_Reset(&p->wasStopped);
|
| 31 | |
| 32 | Event_Set(&p->canStart);
|
| 33 | Event_Wait(&p->wasStarted);
|
| 34 | }
|
| 35 | else
|
| 36 | {
|
| 37 | CriticalSection_Leave(&p->cs);
|
| 38 | p->csWasEntered = False;
|
| 39 | p->numProcessedBlocks++;
|
| 40 | Semaphore_Release1(&p->freeSemaphore);
|
| 41 | }
|
| 42 | Semaphore_Wait(&p->filledSemaphore);
|
| 43 | CriticalSection_Enter(&p->cs);
|
| 44 | p->csWasEntered = True;
|
| 45 | }
|
| 46 | |
| 47 | /* MtSync_StopWriting must be called if Writing was started */
|
| 48 | |
| 49 | void MtSync_StopWriting(CMtSync *p)
|
| 50 | {
|
| 51 | UInt32 myNumBlocks = p->numProcessedBlocks;
|
| 52 | if (!Thread_WasCreated(&p->thread) || p->needStart)
|
| 53 | return;
|
| 54 | p->stopWriting = True;
|
| 55 | if (p->csWasEntered)
|
| 56 | {
|
| 57 | CriticalSection_Leave(&p->cs);
|
| 58 | p->csWasEntered = False;
|
| 59 | }
|
| 60 | Semaphore_Release1(&p->freeSemaphore);
|
| 61 |
|
| 62 | Event_Wait(&p->wasStopped);
|
| 63 | |
| 64 | while (myNumBlocks++ != p->numProcessedBlocks)
|
| 65 | {
|
| 66 | Semaphore_Wait(&p->filledSemaphore);
|
| 67 | Semaphore_Release1(&p->freeSemaphore);
|
| 68 | }
|
| 69 | p->needStart = True;
|
| 70 | }
|
| 71 | |
| 72 | void MtSync_Destruct(CMtSync *p)
|
| 73 | {
|
| 74 | if (Thread_WasCreated(&p->thread))
|
| 75 | {
|
| 76 | MtSync_StopWriting(p);
|
| 77 | p->exit = True;
|
| 78 | if (p->needStart)
|
| 79 | Event_Set(&p->canStart);
|
| 80 | Thread_Wait(&p->thread);
|
| 81 | Thread_Close(&p->thread);
|
| 82 | }
|
| 83 | if (p->csWasInitialized)
|
| 84 | {
|
| 85 | CriticalSection_Delete(&p->cs);
|
| 86 | p->csWasInitialized = False;
|
| 87 | }
|
| 88 | |
| 89 | Event_Close(&p->canStart);
|
| 90 | Event_Close(&p->wasStarted);
|
| 91 | Event_Close(&p->wasStopped);
|
| 92 | Semaphore_Close(&p->freeSemaphore);
|
| 93 | Semaphore_Close(&p->filledSemaphore);
|
| 94 | |
| 95 | p->wasCreated = False;
|
| 96 | }
|
| 97 | |
| 98 | #define RINOK_THREAD(x) { if ((x) != 0) return SZ_ERROR_THREAD; }
|
| 99 | |
| 100 | static SRes MtSync_Create2(CMtSync *p, unsigned (MY_STD_CALL *startAddress)(void *), void *obj, UInt32 numBlocks)
|
| 101 | {
|
| 102 | if (p->wasCreated)
|
| 103 | return SZ_OK;
|
| 104 | |
| 105 | RINOK_THREAD(CriticalSection_Init(&p->cs));
|
| 106 | p->csWasInitialized = True;
|
| 107 | |
| 108 | RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->canStart));
|
| 109 | RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->wasStarted));
|
| 110 | RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->wasStopped));
|
| 111 | |
| 112 | RINOK_THREAD(Semaphore_Create(&p->freeSemaphore, numBlocks, numBlocks));
|
| 113 | RINOK_THREAD(Semaphore_Create(&p->filledSemaphore, 0, numBlocks));
|
| 114 | |
| 115 | p->needStart = True;
|
| 116 | |
| 117 | RINOK_THREAD(Thread_Create(&p->thread, startAddress, obj));
|
| 118 | p->wasCreated = True;
|
| 119 | return SZ_OK;
|
| 120 | }
|
| 121 | |
| 122 | static SRes MtSync_Create(CMtSync *p, unsigned (MY_STD_CALL *startAddress)(void *), void *obj, UInt32 numBlocks)
|
| 123 | {
|
| 124 | SRes res = MtSync_Create2(p, startAddress, obj, numBlocks);
|
| 125 | if (res != SZ_OK)
|
| 126 | MtSync_Destruct(p);
|
| 127 | return res;
|
| 128 | }
|
| 129 | |
| 130 | void MtSync_Init(CMtSync *p) { p->needStart = True; }
|
| 131 | |
| 132 | #define kMtMaxValForNormalize 0xFFFFFFFF
|
| 133 | |
| 134 | #define DEF_GetHeads2(name, v, action) \
|
| 135 | static void GetHeads ## name(const Byte *p, UInt32 pos, \
|
| 136 | UInt32 *hash, UInt32 hashMask, UInt32 *heads, UInt32 numHeads, const UInt32 *crc) \
|
| 137 | { action; for (; numHeads != 0; numHeads--) { \
|
| 138 | const UInt32 value = (v); p++; *heads++ = pos - hash[value]; hash[value] = pos++; } }
|
| 139 | |
| 140 | #define DEF_GetHeads(name, v) DEF_GetHeads2(name, v, ;)
|
| 141 | |
| 142 | DEF_GetHeads2(2, (p[0] | ((UInt32)p[1] << 8)), hashMask = hashMask; crc = crc; )
|
| 143 | DEF_GetHeads(3, (crc[p[0]] ^ p[1] ^ ((UInt32)p[2] << 8)) & hashMask)
|
| 144 | DEF_GetHeads(4, (crc[p[0]] ^ p[1] ^ ((UInt32)p[2] << 8) ^ (crc[p[3]] << 5)) & hashMask)
|
| 145 | DEF_GetHeads(4b, (crc[p[0]] ^ p[1] ^ ((UInt32)p[2] << 8) ^ ((UInt32)p[3] << 16)) & hashMask)
|
| 146 | /* DEF_GetHeads(5, (crc[p[0]] ^ p[1] ^ ((UInt32)p[2] << 8) ^ (crc[p[3]] << 5) ^ (crc[p[4]] << 3)) & hashMask) */
|
| 147 | |
| 148 | void HashThreadFunc(CMatchFinderMt *mt)
|
| 149 | {
|
| 150 | CMtSync *p = &mt->hashSync;
|
| 151 | for (;;)
|
| 152 | {
|
| 153 | UInt32 numProcessedBlocks = 0;
|
| 154 | Event_Wait(&p->canStart);
|
| 155 | Event_Set(&p->wasStarted);
|
| 156 | for (;;)
|
| 157 | {
|
| 158 | if (p->exit)
|
| 159 | return;
|
| 160 | if (p->stopWriting)
|
| 161 | {
|
| 162 | p->numProcessedBlocks = numProcessedBlocks;
|
| 163 | Event_Set(&p->wasStopped);
|
| 164 | break;
|
| 165 | }
|
| 166 | |
| 167 | {
|
| 168 | CMatchFinder *mf = mt->MatchFinder;
|
| 169 | if (MatchFinder_NeedMove(mf))
|
| 170 | {
|
| 171 | CriticalSection_Enter(&mt->btSync.cs);
|
| 172 | CriticalSection_Enter(&mt->hashSync.cs);
|
| 173 | {
|
| 174 | const Byte *beforePtr = MatchFinder_GetPointerToCurrentPos(mf);
|
| 175 | const Byte *afterPtr;
|
| 176 | MatchFinder_MoveBlock(mf);
|
| 177 | afterPtr = MatchFinder_GetPointerToCurrentPos(mf);
|
| 178 | mt->pointerToCurPos -= beforePtr - afterPtr;
|
| 179 | mt->buffer -= beforePtr - afterPtr;
|
| 180 | }
|
| 181 | CriticalSection_Leave(&mt->btSync.cs);
|
| 182 | CriticalSection_Leave(&mt->hashSync.cs);
|
| 183 | continue;
|
| 184 | }
|
| 185 | |
| 186 | Semaphore_Wait(&p->freeSemaphore);
|
| 187 | |
| 188 | MatchFinder_ReadIfRequired(mf);
|
| 189 | if (mf->pos > (kMtMaxValForNormalize - kMtHashBlockSize))
|
| 190 | {
|
| 191 | UInt32 subValue = (mf->pos - mf->historySize - 1);
|
| 192 | MatchFinder_ReduceOffsets(mf, subValue);
|
| 193 | MatchFinder_Normalize3(subValue, mf->hash + mf->fixedHashSize, mf->hashMask + 1);
|
| 194 | }
|
| 195 | {
|
| 196 | UInt32 *heads = mt->hashBuf + ((numProcessedBlocks++) & kMtHashNumBlocksMask) * kMtHashBlockSize;
|
| 197 | UInt32 num = mf->streamPos - mf->pos;
|
| 198 | heads[0] = 2;
|
| 199 | heads[1] = num;
|
| 200 | if (num >= mf->numHashBytes)
|
| 201 | {
|
| 202 | num = num - mf->numHashBytes + 1;
|
| 203 | if (num > kMtHashBlockSize - 2)
|
| 204 | num = kMtHashBlockSize - 2;
|
| 205 | mt->GetHeadsFunc(mf->buffer, mf->pos, mf->hash + mf->fixedHashSize, mf->hashMask, heads + 2, num, mf->crc);
|
| 206 | heads[0] += num;
|
| 207 | }
|
| 208 | mf->pos += num;
|
| 209 | mf->buffer += num;
|
| 210 | }
|
| 211 | }
|
| 212 | |
| 213 | Semaphore_Release1(&p->filledSemaphore);
|
| 214 | }
|
| 215 | }
|
| 216 | }
|
| 217 | |
| 218 | void MatchFinderMt_GetNextBlock_Hash(CMatchFinderMt *p)
|
| 219 | {
|
| 220 | MtSync_GetNextBlock(&p->hashSync);
|
| 221 | p->hashBufPosLimit = p->hashBufPos = ((p->hashSync.numProcessedBlocks - 1) & kMtHashNumBlocksMask) * kMtHashBlockSize;
|
| 222 | p->hashBufPosLimit += p->hashBuf[p->hashBufPos++];
|
| 223 | p->hashNumAvail = p->hashBuf[p->hashBufPos++];
|
| 224 | }
|
| 225 | |
| 226 | #define kEmptyHashValue 0
|
| 227 | |
| 228 | /* #define MFMT_GM_INLINE */
|
| 229 | |
| 230 | #ifdef MFMT_GM_INLINE
|
| 231 | |
| 232 | #define NO_INLINE MY_FAST_CALL
|
| 233 | |
| 234 | Int32 NO_INLINE GetMatchesSpecN(UInt32 lenLimit, UInt32 pos, const Byte *cur, CLzRef *son,
|
| 235 | UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue,
|
| 236 | UInt32 *_distances, UInt32 _maxLen, const UInt32 *hash, Int32 limit, UInt32 size, UInt32 *posRes)
|
| 237 | {
|
| 238 | do
|
| 239 | {
|
| 240 | UInt32 *distances = _distances + 1;
|
| 241 | UInt32 curMatch = pos - *hash++;
|
| 242 | |
| 243 | CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
|
| 244 | CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
|
| 245 | UInt32 len0 = 0, len1 = 0;
|
| 246 | UInt32 cutValue = _cutValue;
|
| 247 | UInt32 maxLen = _maxLen;
|
| 248 | for (;;)
|
| 249 | {
|
| 250 | UInt32 delta = pos - curMatch;
|
| 251 | if (cutValue-- == 0 || delta >= _cyclicBufferSize)
|
| 252 | {
|
| 253 | *ptr0 = *ptr1 = kEmptyHashValue;
|
| 254 | break;
|
| 255 | }
|
| 256 | {
|
| 257 | CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
|
| 258 | const Byte *pb = cur - delta;
|
| 259 | UInt32 len = (len0 < len1 ? len0 : len1);
|
| 260 | if (pb[len] == cur[len])
|
| 261 | {
|
| 262 | if (++len != lenLimit && pb[len] == cur[len])
|
| 263 | while (++len != lenLimit)
|
| 264 | if (pb[len] != cur[len])
|
| 265 | break;
|
| 266 | if (maxLen < len)
|
| 267 | {
|
| 268 | *distances++ = maxLen = len;
|
| 269 | *distances++ = delta - 1;
|
| 270 | if (len == lenLimit)
|
| 271 | {
|
| 272 | *ptr1 = pair[0];
|
| 273 | *ptr0 = pair[1];
|
| 274 | break;
|
| 275 | }
|
| 276 | }
|
| 277 | }
|
| 278 | if (pb[len] < cur[len])
|
| 279 | {
|
| 280 | *ptr1 = curMatch;
|
| 281 | ptr1 = pair + 1;
|
| 282 | curMatch = *ptr1;
|
| 283 | len1 = len;
|
| 284 | }
|
| 285 | else
|
| 286 | {
|
| 287 | *ptr0 = curMatch;
|
| 288 | ptr0 = pair;
|
| 289 | curMatch = *ptr0;
|
| 290 | len0 = len;
|
| 291 | }
|
| 292 | }
|
| 293 | }
|
| 294 | pos++;
|
| 295 | _cyclicBufferPos++;
|
| 296 | cur++;
|
| 297 | {
|
| 298 | UInt32 num = (UInt32)(distances - _distances);
|
| 299 | *_distances = num - 1;
|
| 300 | _distances += num;
|
| 301 | limit -= num;
|
| 302 | }
|
| 303 | }
|
| 304 | while (limit > 0 && --size != 0);
|
| 305 | *posRes = pos;
|
| 306 | return limit;
|
| 307 | }
|
| 308 | |
| 309 | #endif
|
| 310 | |
| 311 | void BtGetMatches(CMatchFinderMt *p, UInt32 *distances)
|
| 312 | {
|
| 313 | UInt32 numProcessed = 0;
|
| 314 | UInt32 curPos = 2;
|
| 315 | UInt32 limit = kMtBtBlockSize - (p->matchMaxLen * 2);
|
| 316 | distances[1] = p->hashNumAvail;
|
| 317 | while (curPos < limit)
|
| 318 | {
|
| 319 | if (p->hashBufPos == p->hashBufPosLimit)
|
| 320 | {
|
| 321 | MatchFinderMt_GetNextBlock_Hash(p);
|
| 322 | distances[1] = numProcessed + p->hashNumAvail;
|
| 323 | if (p->hashNumAvail >= p->numHashBytes)
|
| 324 | continue;
|
| 325 | for (; p->hashNumAvail != 0; p->hashNumAvail--)
|
| 326 | distances[curPos++] = 0;
|
| 327 | break;
|
| 328 | }
|
| 329 | {
|
| 330 | UInt32 size = p->hashBufPosLimit - p->hashBufPos;
|
| 331 | UInt32 lenLimit = p->matchMaxLen;
|
| 332 | UInt32 pos = p->pos;
|
| 333 | UInt32 cyclicBufferPos = p->cyclicBufferPos;
|
| 334 | if (lenLimit >= p->hashNumAvail)
|
| 335 | lenLimit = p->hashNumAvail;
|
| 336 | {
|
| 337 | UInt32 size2 = p->hashNumAvail - lenLimit + 1;
|
| 338 | if (size2 < size)
|
| 339 | size = size2;
|
| 340 | size2 = p->cyclicBufferSize - cyclicBufferPos;
|
| 341 | if (size2 < size)
|
| 342 | size = size2;
|
| 343 | }
|
| 344 | #ifndef MFMT_GM_INLINE
|
| 345 | while (curPos < limit && size-- != 0)
|
| 346 | {
|
| 347 | UInt32 *startDistances = distances + curPos;
|
| 348 | UInt32 num = (UInt32)(GetMatchesSpec1(lenLimit, pos - p->hashBuf[p->hashBufPos++],
|
| 349 | pos, p->buffer, p->son, cyclicBufferPos, p->cyclicBufferSize, p->cutValue,
|
| 350 | startDistances + 1, p->numHashBytes - 1) - startDistances);
|
| 351 | *startDistances = num - 1;
|
| 352 | curPos += num;
|
| 353 | cyclicBufferPos++;
|
| 354 | pos++;
|
| 355 | p->buffer++;
|
| 356 | }
|
| 357 | #else
|
| 358 | {
|
| 359 | UInt32 posRes;
|
| 360 | curPos = limit - GetMatchesSpecN(lenLimit, pos, p->buffer, p->son, cyclicBufferPos, p->cyclicBufferSize, p->cutValue,
|
| 361 | distances + curPos, p->numHashBytes - 1, p->hashBuf + p->hashBufPos, (Int32)(limit - curPos) , size, &posRes);
|
| 362 | p->hashBufPos += posRes - pos;
|
| 363 | cyclicBufferPos += posRes - pos;
|
| 364 | p->buffer += posRes - pos;
|
| 365 | pos = posRes;
|
| 366 | }
|
| 367 | #endif
|
| 368 | |
| 369 | numProcessed += pos - p->pos;
|
| 370 | p->hashNumAvail -= pos - p->pos;
|
| 371 | p->pos = pos;
|
| 372 | if (cyclicBufferPos == p->cyclicBufferSize)
|
| 373 | cyclicBufferPos = 0;
|
| 374 | p->cyclicBufferPos = cyclicBufferPos;
|
| 375 | }
|
| 376 | }
|
| 377 | distances[0] = curPos;
|
| 378 | }
|
| 379 | |
| 380 | void BtFillBlock(CMatchFinderMt *p, UInt32 globalBlockIndex)
|
| 381 | {
|
| 382 | CMtSync *sync = &p->hashSync;
|
| 383 | if (!sync->needStart)
|
| 384 | {
|
| 385 | CriticalSection_Enter(&sync->cs);
|
| 386 | sync->csWasEntered = True;
|
| 387 | }
|
| 388 | |
| 389 | BtGetMatches(p, p->btBuf + (globalBlockIndex & kMtBtNumBlocksMask) * kMtBtBlockSize);
|
| 390 | |
| 391 | if (p->pos > kMtMaxValForNormalize - kMtBtBlockSize)
|
| 392 | {
|
| 393 | UInt32 subValue = p->pos - p->cyclicBufferSize;
|
| 394 | MatchFinder_Normalize3(subValue, p->son, p->cyclicBufferSize * 2);
|
| 395 | p->pos -= subValue;
|
| 396 | }
|
| 397 | |
| 398 | if (!sync->needStart)
|
| 399 | {
|
| 400 | CriticalSection_Leave(&sync->cs);
|
| 401 | sync->csWasEntered = False;
|
| 402 | }
|
| 403 | }
|
| 404 | |
| 405 | void BtThreadFunc(CMatchFinderMt *mt)
|
| 406 | {
|
| 407 | CMtSync *p = &mt->btSync;
|
| 408 | for (;;)
|
| 409 | {
|
| 410 | UInt32 blockIndex = 0;
|
| 411 | Event_Wait(&p->canStart);
|
| 412 | Event_Set(&p->wasStarted);
|
| 413 | for (;;)
|
| 414 | {
|
| 415 | if (p->exit)
|
| 416 | return;
|
| 417 | if (p->stopWriting)
|
| 418 | {
|
| 419 | p->numProcessedBlocks = blockIndex;
|
| 420 | MtSync_StopWriting(&mt->hashSync);
|
| 421 | Event_Set(&p->wasStopped);
|
| 422 | break;
|
| 423 | }
|
| 424 | Semaphore_Wait(&p->freeSemaphore);
|
| 425 | BtFillBlock(mt, blockIndex++);
|
| 426 | Semaphore_Release1(&p->filledSemaphore);
|
| 427 | }
|
| 428 | }
|
| 429 | }
|
| 430 | |
| 431 | void MatchFinderMt_Construct(CMatchFinderMt *p)
|
| 432 | {
|
| 433 | p->hashBuf = 0;
|
| 434 | MtSync_Construct(&p->hashSync);
|
| 435 | MtSync_Construct(&p->btSync);
|
| 436 | }
|
| 437 | |
| 438 | void MatchFinderMt_FreeMem(CMatchFinderMt *p, ISzAlloc *alloc)
|
| 439 | {
|
| 440 | alloc->Free(alloc, p->hashBuf);
|
| 441 | p->hashBuf = 0;
|
| 442 | }
|
| 443 | |
| 444 | void MatchFinderMt_Destruct(CMatchFinderMt *p, ISzAlloc *alloc)
|
| 445 | {
|
| 446 | MtSync_Destruct(&p->hashSync);
|
| 447 | MtSync_Destruct(&p->btSync);
|
| 448 | MatchFinderMt_FreeMem(p, alloc);
|
| 449 | }
|
| 450 | |
| 451 | #define kHashBufferSize (kMtHashBlockSize * kMtHashNumBlocks)
|
| 452 | #define kBtBufferSize (kMtBtBlockSize * kMtBtNumBlocks)
|
| 453 | |
| 454 | static unsigned MY_STD_CALL HashThreadFunc2(void *p) { HashThreadFunc((CMatchFinderMt *)p); return 0; }
|
| 455 | static unsigned MY_STD_CALL BtThreadFunc2(void *p)
|
| 456 | {
|
| 457 | Byte allocaDummy[0x180];
|
| 458 | int i = 0;
|
| 459 | for (i = 0; i < 16; i++)
|
| 460 | allocaDummy[i] = (Byte)i;
|
| 461 | BtThreadFunc((CMatchFinderMt *)p);
|
| 462 | return 0;
|
| 463 | }
|
| 464 | |
| 465 | SRes MatchFinderMt_Create(CMatchFinderMt *p, UInt32 historySize, UInt32 keepAddBufferBefore,
|
| 466 | UInt32 matchMaxLen, UInt32 keepAddBufferAfter, ISzAlloc *alloc)
|
| 467 | {
|
| 468 | CMatchFinder *mf = p->MatchFinder;
|
| 469 | p->historySize = historySize;
|
| 470 | if (kMtBtBlockSize <= matchMaxLen * 4)
|
| 471 | return SZ_ERROR_PARAM;
|
| 472 | if (p->hashBuf == 0)
|
| 473 | {
|
| 474 | p->hashBuf = (UInt32 *)alloc->Alloc(alloc, (kHashBufferSize + kBtBufferSize) * sizeof(UInt32));
|
| 475 | if (p->hashBuf == 0)
|
| 476 | return SZ_ERROR_MEM;
|
| 477 | p->btBuf = p->hashBuf + kHashBufferSize;
|
| 478 | }
|
| 479 | keepAddBufferBefore += (kHashBufferSize + kBtBufferSize);
|
| 480 | keepAddBufferAfter += kMtHashBlockSize;
|
| 481 | if (!MatchFinder_Create(mf, historySize, keepAddBufferBefore, matchMaxLen, keepAddBufferAfter, alloc))
|
| 482 | return SZ_ERROR_MEM;
|
| 483 | |
| 484 | RINOK(MtSync_Create(&p->hashSync, HashThreadFunc2, p, kMtHashNumBlocks));
|
| 485 | RINOK(MtSync_Create(&p->btSync, BtThreadFunc2, p, kMtBtNumBlocks));
|
| 486 | return SZ_OK;
|
| 487 | }
|
| 488 | |
| 489 | /* Call it after ReleaseStream / SetStream */
|
| 490 | void MatchFinderMt_Init(CMatchFinderMt *p)
|
| 491 | {
|
| 492 | CMatchFinder *mf = p->MatchFinder;
|
| 493 | p->btBufPos = p->btBufPosLimit = 0;
|
| 494 | p->hashBufPos = p->hashBufPosLimit = 0;
|
| 495 | MatchFinder_Init(mf);
|
| 496 | p->pointerToCurPos = MatchFinder_GetPointerToCurrentPos(mf);
|
| 497 | p->btNumAvailBytes = 0;
|
| 498 | p->lzPos = p->historySize + 1;
|
| 499 | |
| 500 | p->hash = mf->hash;
|
| 501 | p->fixedHashSize = mf->fixedHashSize;
|
| 502 | p->crc = mf->crc;
|
| 503 | |
| 504 | p->son = mf->son;
|
| 505 | p->matchMaxLen = mf->matchMaxLen;
|
| 506 | p->numHashBytes = mf->numHashBytes;
|
| 507 | p->pos = mf->pos;
|
| 508 | p->buffer = mf->buffer;
|
| 509 | p->cyclicBufferPos = mf->cyclicBufferPos;
|
| 510 | p->cyclicBufferSize = mf->cyclicBufferSize;
|
| 511 | p->cutValue = mf->cutValue;
|
| 512 | }
|
| 513 | |
| 514 | /* ReleaseStream is required to finish multithreading */
|
| 515 | void MatchFinderMt_ReleaseStream(CMatchFinderMt *p)
|
| 516 | {
|
| 517 | MtSync_StopWriting(&p->btSync);
|
| 518 | /* p->MatchFinder->ReleaseStream(); */
|
| 519 | }
|
| 520 | |
| 521 | void MatchFinderMt_Normalize(CMatchFinderMt *p)
|
| 522 | {
|
| 523 | MatchFinder_Normalize3(p->lzPos - p->historySize - 1, p->hash, p->fixedHashSize);
|
| 524 | p->lzPos = p->historySize + 1;
|
| 525 | }
|
| 526 | |
| 527 | void MatchFinderMt_GetNextBlock_Bt(CMatchFinderMt *p)
|
| 528 | {
|
| 529 | UInt32 blockIndex;
|
| 530 | MtSync_GetNextBlock(&p->btSync);
|
| 531 | blockIndex = ((p->btSync.numProcessedBlocks - 1) & kMtBtNumBlocksMask);
|
| 532 | p->btBufPosLimit = p->btBufPos = blockIndex * kMtBtBlockSize;
|
| 533 | p->btBufPosLimit += p->btBuf[p->btBufPos++];
|
| 534 | p->btNumAvailBytes = p->btBuf[p->btBufPos++];
|
| 535 | if (p->lzPos >= kMtMaxValForNormalize - kMtBtBlockSize)
|
| 536 | MatchFinderMt_Normalize(p);
|
| 537 | }
|
| 538 | |
| 539 | const Byte * MatchFinderMt_GetPointerToCurrentPos(CMatchFinderMt *p)
|
| 540 | {
|
| 541 | return p->pointerToCurPos;
|
| 542 | }
|
| 543 | |
| 544 | #define GET_NEXT_BLOCK_IF_REQUIRED if (p->btBufPos == p->btBufPosLimit) MatchFinderMt_GetNextBlock_Bt(p);
|
| 545 | |
| 546 | UInt32 MatchFinderMt_GetNumAvailableBytes(CMatchFinderMt *p)
|
| 547 | {
|
| 548 | GET_NEXT_BLOCK_IF_REQUIRED;
|
| 549 | return p->btNumAvailBytes;
|
| 550 | }
|
| 551 | |
| 552 | Byte MatchFinderMt_GetIndexByte(CMatchFinderMt *p, Int32 index)
|
| 553 | {
|
| 554 | return p->pointerToCurPos[index];
|
| 555 | }
|
| 556 | |
| 557 | UInt32 * MixMatches2(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *distances)
|
| 558 | {
|
| 559 | UInt32 hash2Value, curMatch2;
|
| 560 | UInt32 *hash = p->hash;
|
| 561 | const Byte *cur = p->pointerToCurPos;
|
| 562 | UInt32 lzPos = p->lzPos;
|
| 563 | MT_HASH2_CALC
|
| 564 | |
| 565 | curMatch2 = hash[hash2Value];
|
| 566 | hash[hash2Value] = lzPos;
|
| 567 | |
| 568 | if (curMatch2 >= matchMinPos)
|
| 569 | if (cur[(ptrdiff_t)curMatch2 - lzPos] == cur[0])
|
| 570 | {
|
| 571 | *distances++ = 2;
|
| 572 | *distances++ = lzPos - curMatch2 - 1;
|
| 573 | }
|
| 574 | return distances;
|
| 575 | }
|
| 576 | |
| 577 | UInt32 * MixMatches3(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *distances)
|
| 578 | {
|
| 579 | UInt32 hash2Value, hash3Value, curMatch2, curMatch3;
|
| 580 | UInt32 *hash = p->hash;
|
| 581 | const Byte *cur = p->pointerToCurPos;
|
| 582 | UInt32 lzPos = p->lzPos;
|
| 583 | MT_HASH3_CALC
|
| 584 | |
| 585 | curMatch2 = hash[ hash2Value];
|
| 586 | curMatch3 = hash[kFix3HashSize + hash3Value];
|
| 587 | |
| 588 | hash[ hash2Value] =
|
| 589 | hash[kFix3HashSize + hash3Value] =
|
| 590 | lzPos;
|
| 591 | |
| 592 | if (curMatch2 >= matchMinPos && cur[(ptrdiff_t)curMatch2 - lzPos] == cur[0])
|
| 593 | {
|
| 594 | distances[1] = lzPos - curMatch2 - 1;
|
| 595 | if (cur[(ptrdiff_t)curMatch2 - lzPos + 2] == cur[2])
|
| 596 | {
|
| 597 | distances[0] = 3;
|
| 598 | return distances + 2;
|
| 599 | }
|
| 600 | distances[0] = 2;
|
| 601 | distances += 2;
|
| 602 | }
|
| 603 | if (curMatch3 >= matchMinPos && cur[(ptrdiff_t)curMatch3 - lzPos] == cur[0])
|
| 604 | {
|
| 605 | *distances++ = 3;
|
| 606 | *distances++ = lzPos - curMatch3 - 1;
|
| 607 | }
|
| 608 | return distances;
|
| 609 | }
|
| 610 | |
| 611 | /*
|
| 612 | UInt32 *MixMatches4(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *distances)
|
| 613 | {
|
| 614 | UInt32 hash2Value, hash3Value, hash4Value, curMatch2, curMatch3, curMatch4;
|
| 615 | UInt32 *hash = p->hash;
|
| 616 | const Byte *cur = p->pointerToCurPos;
|
| 617 | UInt32 lzPos = p->lzPos;
|
| 618 | MT_HASH4_CALC
|
| 619 | |
| 620 | curMatch2 = hash[ hash2Value];
|
| 621 | curMatch3 = hash[kFix3HashSize + hash3Value];
|
| 622 | curMatch4 = hash[kFix4HashSize + hash4Value];
|
| 623 | |
| 624 | hash[ hash2Value] =
|
| 625 | hash[kFix3HashSize + hash3Value] =
|
| 626 | hash[kFix4HashSize + hash4Value] =
|
| 627 | lzPos;
|
| 628 | |
| 629 | if (curMatch2 >= matchMinPos && cur[(ptrdiff_t)curMatch2 - lzPos] == cur[0])
|
| 630 | {
|
| 631 | distances[1] = lzPos - curMatch2 - 1;
|
| 632 | if (cur[(ptrdiff_t)curMatch2 - lzPos + 2] == cur[2])
|
| 633 | {
|
| 634 | distances[0] = (cur[(ptrdiff_t)curMatch2 - lzPos + 3] == cur[3]) ? 4 : 3;
|
| 635 | return distances + 2;
|
| 636 | }
|
| 637 | distances[0] = 2;
|
| 638 | distances += 2;
|
| 639 | }
|
| 640 | if (curMatch3 >= matchMinPos && cur[(ptrdiff_t)curMatch3 - lzPos] == cur[0])
|
| 641 | {
|
| 642 | distances[1] = lzPos - curMatch3 - 1;
|
| 643 | if (cur[(ptrdiff_t)curMatch3 - lzPos + 3] == cur[3])
|
| 644 | {
|
| 645 | distances[0] = 4;
|
| 646 | return distances + 2;
|
| 647 | }
|
| 648 | distances[0] = 3;
|
| 649 | distances += 2;
|
| 650 | }
|
| 651 | |
| 652 | if (curMatch4 >= matchMinPos)
|
| 653 | if (
|
| 654 | cur[(ptrdiff_t)curMatch4 - lzPos] == cur[0] &&
|
| 655 | cur[(ptrdiff_t)curMatch4 - lzPos + 3] == cur[3]
|
| 656 | )
|
| 657 | {
|
| 658 | *distances++ = 4;
|
| 659 | *distances++ = lzPos - curMatch4 - 1;
|
| 660 | }
|
| 661 | return distances;
|
| 662 | }
|
| 663 | */
|
| 664 | |
| 665 | #define INCREASE_LZ_POS p->lzPos++; p->pointerToCurPos++;
|
| 666 | |
| 667 | UInt32 MatchFinderMt2_GetMatches(CMatchFinderMt *p, UInt32 *distances)
|
| 668 | {
|
| 669 | const UInt32 *btBuf = p->btBuf + p->btBufPos;
|
| 670 | UInt32 len = *btBuf++;
|
| 671 | p->btBufPos += 1 + len;
|
| 672 | p->btNumAvailBytes--;
|
| 673 | {
|
| 674 | UInt32 i;
|
| 675 | for (i = 0; i < len; i += 2)
|
| 676 | {
|
| 677 | *distances++ = *btBuf++;
|
| 678 | *distances++ = *btBuf++;
|
| 679 | }
|
| 680 | }
|
| 681 | INCREASE_LZ_POS
|
| 682 | return len;
|
| 683 | }
|
| 684 | |
| 685 | UInt32 MatchFinderMt_GetMatches(CMatchFinderMt *p, UInt32 *distances)
|
| 686 | {
|
| 687 | const UInt32 *btBuf = p->btBuf + p->btBufPos;
|
| 688 | UInt32 len = *btBuf++;
|
| 689 | p->btBufPos += 1 + len;
|
| 690 | |
| 691 | if (len == 0)
|
| 692 | {
|
| 693 | if (p->btNumAvailBytes-- >= 4)
|
| 694 | len = (UInt32)(p->MixMatchesFunc(p, p->lzPos - p->historySize, distances) - (distances));
|
| 695 | }
|
| 696 | else
|
| 697 | {
|
| 698 | /* Condition: there are matches in btBuf with length < p->numHashBytes */
|
| 699 | UInt32 *distances2;
|
| 700 | p->btNumAvailBytes--;
|
| 701 | distances2 = p->MixMatchesFunc(p, p->lzPos - btBuf[1], distances);
|
| 702 | do
|
| 703 | {
|
| 704 | *distances2++ = *btBuf++;
|
| 705 | *distances2++ = *btBuf++;
|
| 706 | }
|
| 707 | while ((len -= 2) != 0);
|
| 708 | len = (UInt32)(distances2 - (distances));
|
| 709 | }
|
| 710 | INCREASE_LZ_POS
|
| 711 | return len;
|
| 712 | }
|
| 713 | |
| 714 | #define SKIP_HEADER2_MT do { GET_NEXT_BLOCK_IF_REQUIRED
|
| 715 | #define SKIP_HEADER_MT(n) SKIP_HEADER2_MT if (p->btNumAvailBytes-- >= (n)) { const Byte *cur = p->pointerToCurPos; UInt32 *hash = p->hash;
|
| 716 | #define SKIP_FOOTER_MT } INCREASE_LZ_POS p->btBufPos += p->btBuf[p->btBufPos] + 1; } while (--num != 0);
|
| 717 | |
| 718 | void MatchFinderMt0_Skip(CMatchFinderMt *p, UInt32 num)
|
| 719 | {
|
| 720 | SKIP_HEADER2_MT { p->btNumAvailBytes--;
|
| 721 | SKIP_FOOTER_MT
|
| 722 | }
|
| 723 | |
| 724 | void MatchFinderMt2_Skip(CMatchFinderMt *p, UInt32 num)
|
| 725 | {
|
| 726 | SKIP_HEADER_MT(2)
|
| 727 | UInt32 hash2Value;
|
| 728 | MT_HASH2_CALC
|
| 729 | hash[hash2Value] = p->lzPos;
|
| 730 | SKIP_FOOTER_MT
|
| 731 | }
|
| 732 | |
| 733 | void MatchFinderMt3_Skip(CMatchFinderMt *p, UInt32 num)
|
| 734 | {
|
| 735 | SKIP_HEADER_MT(3)
|
| 736 | UInt32 hash2Value, hash3Value;
|
| 737 | MT_HASH3_CALC
|
| 738 | hash[kFix3HashSize + hash3Value] =
|
| 739 | hash[ hash2Value] =
|
| 740 | p->lzPos;
|
| 741 | SKIP_FOOTER_MT
|
| 742 | }
|
| 743 | |
| 744 | /*
|
| 745 | void MatchFinderMt4_Skip(CMatchFinderMt *p, UInt32 num)
|
| 746 | {
|
| 747 | SKIP_HEADER_MT(4)
|
| 748 | UInt32 hash2Value, hash3Value, hash4Value;
|
| 749 | MT_HASH4_CALC
|
| 750 | hash[kFix4HashSize + hash4Value] =
|
| 751 | hash[kFix3HashSize + hash3Value] =
|
| 752 | hash[ hash2Value] =
|
| 753 | p->lzPos;
|
| 754 | SKIP_FOOTER_MT
|
| 755 | }
|
| 756 | */
|
| 757 | |
| 758 | void MatchFinderMt_CreateVTable(CMatchFinderMt *p, IMatchFinder *vTable)
|
| 759 | {
|
| 760 | vTable->Init = (Mf_Init_Func)MatchFinderMt_Init;
|
| 761 | vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinderMt_GetIndexByte;
|
| 762 | vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinderMt_GetNumAvailableBytes;
|
| 763 | vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinderMt_GetPointerToCurrentPos;
|
| 764 | vTable->GetMatches = (Mf_GetMatches_Func)MatchFinderMt_GetMatches;
|
| 765 | switch(p->MatchFinder->numHashBytes)
|
| 766 | {
|
| 767 | case 2:
|
| 768 | p->GetHeadsFunc = GetHeads2;
|
| 769 | p->MixMatchesFunc = (Mf_Mix_Matches)0;
|
| 770 | vTable->Skip = (Mf_Skip_Func)MatchFinderMt0_Skip;
|
| 771 | vTable->GetMatches = (Mf_GetMatches_Func)MatchFinderMt2_GetMatches;
|
| 772 | break;
|
| 773 | case 3:
|
| 774 | p->GetHeadsFunc = GetHeads3;
|
| 775 | p->MixMatchesFunc = (Mf_Mix_Matches)MixMatches2;
|
| 776 | vTable->Skip = (Mf_Skip_Func)MatchFinderMt2_Skip;
|
| 777 | break;
|
| 778 | default:
|
| 779 | /* case 4: */
|
| 780 | p->GetHeadsFunc = p->MatchFinder->bigHash ? GetHeads4b : GetHeads4;
|
| 781 | /* p->GetHeadsFunc = GetHeads4; */
|
| 782 | p->MixMatchesFunc = (Mf_Mix_Matches)MixMatches3;
|
| 783 | vTable->Skip = (Mf_Skip_Func)MatchFinderMt3_Skip;
|
| 784 | break;
|
| 785 | /*
|
| 786 | default:
|
| 787 | p->GetHeadsFunc = GetHeads5;
|
| 788 | p->MixMatchesFunc = (Mf_Mix_Matches)MixMatches4;
|
| 789 | vTable->Skip = (Mf_Skip_Func)MatchFinderMt4_Skip;
|
| 790 | break;
|
| 791 | */
|
| 792 | }
|
| 793 | }
|
src/lib/lib7z/7zIn.c
| r0 | r14440 | |
| 1 | /* 7zIn.c -- 7z Input functions
|
| 2 | 2010-10-29 : Igor Pavlov : Public domain */
|
| 3 | |
| 4 | #include <string.h>
|
| 5 | |
| 6 | #include "7z.h"
|
| 7 | #include "7zCrc.h"
|
| 8 | #include "CpuArch.h"
|
| 9 | |
| 10 | Byte k7zSignature[k7zSignatureSize] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C};
|
| 11 | |
| 12 | #define RINOM(x) { if ((x) == 0) return SZ_ERROR_MEM; }
|
| 13 | |
| 14 | #define NUM_FOLDER_CODERS_MAX 32
|
| 15 | #define NUM_CODER_STREAMS_MAX 32
|
| 16 | |
| 17 | void SzCoderInfo_Init(CSzCoderInfo *p)
|
| 18 | {
|
| 19 | Buf_Init(&p->Props);
|
| 20 | }
|
| 21 | |
| 22 | void SzCoderInfo_Free(CSzCoderInfo *p, ISzAlloc *alloc)
|
| 23 | {
|
| 24 | Buf_Free(&p->Props, alloc);
|
| 25 | SzCoderInfo_Init(p);
|
| 26 | }
|
| 27 | |
| 28 | void SzFolder_Init(CSzFolder *p)
|
| 29 | {
|
| 30 | p->Coders = 0;
|
| 31 | p->BindPairs = 0;
|
| 32 | p->PackStreams = 0;
|
| 33 | p->UnpackSizes = 0;
|
| 34 | p->NumCoders = 0;
|
| 35 | p->NumBindPairs = 0;
|
| 36 | p->NumPackStreams = 0;
|
| 37 | p->UnpackCRCDefined = 0;
|
| 38 | p->UnpackCRC = 0;
|
| 39 | p->NumUnpackStreams = 0;
|
| 40 | }
|
| 41 | |
| 42 | void SzFolder_Free(CSzFolder *p, ISzAlloc *alloc)
|
| 43 | {
|
| 44 | UInt32 i;
|
| 45 | if (p->Coders)
|
| 46 | for (i = 0; i < p->NumCoders; i++)
|
| 47 | SzCoderInfo_Free(&p->Coders[i], alloc);
|
| 48 | IAlloc_Free(alloc, p->Coders);
|
| 49 | IAlloc_Free(alloc, p->BindPairs);
|
| 50 | IAlloc_Free(alloc, p->PackStreams);
|
| 51 | IAlloc_Free(alloc, p->UnpackSizes);
|
| 52 | SzFolder_Init(p);
|
| 53 | }
|
| 54 | |
| 55 | UInt32 SzFolder_GetNumOutStreams(CSzFolder *p)
|
| 56 | {
|
| 57 | UInt32 result = 0;
|
| 58 | UInt32 i;
|
| 59 | for (i = 0; i < p->NumCoders; i++)
|
| 60 | result += p->Coders[i].NumOutStreams;
|
| 61 | return result;
|
| 62 | }
|
| 63 | |
| 64 | int SzFolder_FindBindPairForInStream(CSzFolder *p, UInt32 inStreamIndex)
|
| 65 | {
|
| 66 | UInt32 i;
|
| 67 | for (i = 0; i < p->NumBindPairs; i++)
|
| 68 | if (p->BindPairs[i].InIndex == inStreamIndex)
|
| 69 | return i;
|
| 70 | return -1;
|
| 71 | }
|
| 72 | |
| 73 | |
| 74 | int SzFolder_FindBindPairForOutStream(CSzFolder *p, UInt32 outStreamIndex)
|
| 75 | {
|
| 76 | UInt32 i;
|
| 77 | for (i = 0; i < p->NumBindPairs; i++)
|
| 78 | if (p->BindPairs[i].OutIndex == outStreamIndex)
|
| 79 | return i;
|
| 80 | return -1;
|
| 81 | }
|
| 82 | |
| 83 | UInt64 SzFolder_GetUnpackSize(CSzFolder *p)
|
| 84 | {
|
| 85 | int i = (int)SzFolder_GetNumOutStreams(p);
|
| 86 | if (i == 0)
|
| 87 | return 0;
|
| 88 | for (i--; i >= 0; i--)
|
| 89 | if (SzFolder_FindBindPairForOutStream(p, i) < 0)
|
| 90 | return p->UnpackSizes[i];
|
| 91 | /* throw 1; */
|
| 92 | return 0;
|
| 93 | }
|
| 94 | |
| 95 | void SzFile_Init(CSzFileItem *p)
|
| 96 | {
|
| 97 | p->HasStream = 1;
|
| 98 | p->IsDir = 0;
|
| 99 | p->IsAnti = 0;
|
| 100 | p->CrcDefined = 0;
|
| 101 | p->MTimeDefined = 0;
|
| 102 | }
|
| 103 | |
| 104 | void SzAr_Init(CSzAr *p)
|
| 105 | {
|
| 106 | p->PackSizes = 0;
|
| 107 | p->PackCRCsDefined = 0;
|
| 108 | p->PackCRCs = 0;
|
| 109 | p->Folders = 0;
|
| 110 | p->Files = 0;
|
| 111 | p->NumPackStreams = 0;
|
| 112 | p->NumFolders = 0;
|
| 113 | p->NumFiles = 0;
|
| 114 | }
|
| 115 | |
| 116 | void SzAr_Free(CSzAr *p, ISzAlloc *alloc)
|
| 117 | {
|
| 118 | UInt32 i;
|
| 119 | if (p->Folders)
|
| 120 | for (i = 0; i < p->NumFolders; i++)
|
| 121 | SzFolder_Free(&p->Folders[i], alloc);
|
| 122 | |
| 123 | IAlloc_Free(alloc, p->PackSizes);
|
| 124 | IAlloc_Free(alloc, p->PackCRCsDefined);
|
| 125 | IAlloc_Free(alloc, p->PackCRCs);
|
| 126 | IAlloc_Free(alloc, p->Folders);
|
| 127 | IAlloc_Free(alloc, p->Files);
|
| 128 | SzAr_Init(p);
|
| 129 | }
|
| 130 | |
| 131 | |
| 132 | void SzArEx_Init(CSzArEx *p)
|
| 133 | {
|
| 134 | SzAr_Init(&p->db);
|
| 135 | p->FolderStartPackStreamIndex = 0;
|
| 136 | p->PackStreamStartPositions = 0;
|
| 137 | p->FolderStartFileIndex = 0;
|
| 138 | p->FileIndexToFolderIndexMap = 0;
|
| 139 | p->FileNameOffsets = 0;
|
| 140 | Buf_Init(&p->FileNames);
|
| 141 | }
|
| 142 | |
| 143 | void SzArEx_Free(CSzArEx *p, ISzAlloc *alloc)
|
| 144 | {
|
| 145 | IAlloc_Free(alloc, p->FolderStartPackStreamIndex);
|
| 146 | IAlloc_Free(alloc, p->PackStreamStartPositions);
|
| 147 | IAlloc_Free(alloc, p->FolderStartFileIndex);
|
| 148 | IAlloc_Free(alloc, p->FileIndexToFolderIndexMap);
|
| 149 | |
| 150 | IAlloc_Free(alloc, p->FileNameOffsets);
|
| 151 | Buf_Free(&p->FileNames, alloc);
|
| 152 | |
| 153 | SzAr_Free(&p->db, alloc);
|
| 154 | SzArEx_Init(p);
|
| 155 | }
|
| 156 | |
| 157 | /*
|
| 158 | UInt64 GetFolderPackStreamSize(int folderIndex, int streamIndex) const
|
| 159 | {
|
| 160 | return PackSizes[FolderStartPackStreamIndex[folderIndex] + streamIndex];
|
| 161 | }
|
| 162 | |
| 163 | UInt64 GetFilePackSize(int fileIndex) const
|
| 164 | {
|
| 165 | int folderIndex = FileIndexToFolderIndexMap[fileIndex];
|
| 166 | if (folderIndex >= 0)
|
| 167 | {
|
| 168 | const CSzFolder &folderInfo = Folders[folderIndex];
|
| 169 | if (FolderStartFileIndex[folderIndex] == fileIndex)
|
| 170 | return GetFolderFullPackSize(folderIndex);
|
| 171 | }
|
| 172 | return 0;
|
| 173 | }
|
| 174 | */
|
| 175 | |
| 176 | #define MY_ALLOC(T, p, size, alloc) { if ((size) == 0) p = 0; else \
|
| 177 | if ((p = (T *)IAlloc_Alloc(alloc, (size) * sizeof(T))) == 0) return SZ_ERROR_MEM; }
|
| 178 | |
| 179 | static SRes SzArEx_Fill(CSzArEx *p, ISzAlloc *alloc)
|
| 180 | {
|
| 181 | UInt32 startPos = 0;
|
| 182 | UInt64 startPosSize = 0;
|
| 183 | UInt32 i;
|
| 184 | UInt32 folderIndex = 0;
|
| 185 | UInt32 indexInFolder = 0;
|
| 186 | MY_ALLOC(UInt32, p->FolderStartPackStreamIndex, p->db.NumFolders, alloc);
|
| 187 | for (i = 0; i < p->db.NumFolders; i++)
|
| 188 | {
|
| 189 | p->FolderStartPackStreamIndex[i] = startPos;
|
| 190 | startPos += p->db.Folders[i].NumPackStreams;
|
| 191 | }
|
| 192 | |
| 193 | MY_ALLOC(UInt64, p->PackStreamStartPositions, p->db.NumPackStreams, alloc);
|
| 194 | |
| 195 | for (i = 0; i < p->db.NumPackStreams; i++)
|
| 196 | {
|
| 197 | p->PackStreamStartPositions[i] = startPosSize;
|
| 198 | startPosSize += p->db.PackSizes[i];
|
| 199 | }
|
| 200 | |
| 201 | MY_ALLOC(UInt32, p->FolderStartFileIndex, p->db.NumFolders, alloc);
|
| 202 | MY_ALLOC(UInt32, p->FileIndexToFolderIndexMap, p->db.NumFiles, alloc);
|
| 203 | |
| 204 | for (i = 0; i < p->db.NumFiles; i++)
|
| 205 | {
|
| 206 | CSzFileItem *file = p->db.Files + i;
|
| 207 | int emptyStream = !file->HasStream;
|
| 208 | if (emptyStream && indexInFolder == 0)
|
| 209 | {
|
| 210 | p->FileIndexToFolderIndexMap[i] = (UInt32)-1;
|
| 211 | continue;
|
| 212 | }
|
| 213 | if (indexInFolder == 0)
|
| 214 | {
|
| 215 | /*
|
| 216 | v3.13 incorrectly worked with empty folders
|
| 217 | v4.07: Loop for skipping empty folders
|
| 218 | */
|
| 219 | for (;;)
|
| 220 | {
|
| 221 | if (folderIndex >= p->db.NumFolders)
|
| 222 | return SZ_ERROR_ARCHIVE;
|
| 223 | p->FolderStartFileIndex[folderIndex] = i;
|
| 224 | if (p->db.Folders[folderIndex].NumUnpackStreams != 0)
|
| 225 | break;
|
| 226 | folderIndex++;
|
| 227 | }
|
| 228 | }
|
| 229 | p->FileIndexToFolderIndexMap[i] = folderIndex;
|
| 230 | if (emptyStream)
|
| 231 | continue;
|
| 232 | indexInFolder++;
|
| 233 | if (indexInFolder >= p->db.Folders[folderIndex].NumUnpackStreams)
|
| 234 | {
|
| 235 | folderIndex++;
|
| 236 | indexInFolder = 0;
|
| 237 | }
|
| 238 | }
|
| 239 | return SZ_OK;
|
| 240 | }
|
| 241 | |
| 242 | |
| 243 | UInt64 SzArEx_GetFolderStreamPos(const CSzArEx *p, UInt32 folderIndex, UInt32 indexInFolder)
|
| 244 | {
|
| 245 | return p->dataPos +
|
| 246 | p->PackStreamStartPositions[p->FolderStartPackStreamIndex[folderIndex] + indexInFolder];
|
| 247 | }
|
| 248 | |
| 249 | int SzArEx_GetFolderFullPackSize(const CSzArEx *p, UInt32 folderIndex, UInt64 *resSize)
|
| 250 | {
|
| 251 | UInt32 packStreamIndex = p->FolderStartPackStreamIndex[folderIndex];
|
| 252 | CSzFolder *folder = p->db.Folders + folderIndex;
|
| 253 | UInt64 size = 0;
|
| 254 | UInt32 i;
|
| 255 | for (i = 0; i < folder->NumPackStreams; i++)
|
| 256 | {
|
| 257 | UInt64 t = size + p->db.PackSizes[packStreamIndex + i];
|
| 258 | if (t < size) /* check it */
|
| 259 | return SZ_ERROR_FAIL;
|
| 260 | size = t;
|
| 261 | }
|
| 262 | *resSize = size;
|
| 263 | return SZ_OK;
|
| 264 | }
|
| 265 | |
| 266 | |
| 267 | /*
|
| 268 | SRes SzReadTime(const CObjectVector<CBuf> &dataVector,
|
| 269 | CObjectVector<CSzFileItem> &files, UInt64 type)
|
| 270 | {
|
| 271 | CBoolVector boolVector;
|
| 272 | RINOK(ReadBoolVector2(files.Size(), boolVector))
|
| 273 | |
| 274 | CStreamSwitch streamSwitch;
|
| 275 | RINOK(streamSwitch.Set(this, &dataVector));
|
| 276 | |
| 277 | for (int i = 0; i < files.Size(); i++)
|
| 278 | {
|
| 279 | CSzFileItem &file = files[i];
|
| 280 | CArchiveFileTime fileTime;
|
| 281 | bool defined = boolVector[i];
|
| 282 | if (defined)
|
| 283 | {
|
| 284 | UInt32 low, high;
|
| 285 | RINOK(SzReadUInt32(low));
|
| 286 | RINOK(SzReadUInt32(high));
|
| 287 | fileTime.dwLowDateTime = low;
|
| 288 | fileTime.dwHighDateTime = high;
|
| 289 | }
|
| 290 | switch(type)
|
| 291 | {
|
| 292 | case k7zIdCTime: file.IsCTimeDefined = defined; if (defined) file.CTime = fileTime; break;
|
| 293 | case k7zIdATime: file.IsATimeDefined = defined; if (defined) file.ATime = fileTime; break;
|
| 294 | case k7zIdMTime: file.IsMTimeDefined = defined; if (defined) file.MTime = fileTime; break;
|
| 295 | }
|
| 296 | }
|
| 297 | return SZ_OK;
|
| 298 | }
|
| 299 | */
|
| 300 | |
| 301 | static int TestSignatureCandidate(Byte *testBytes)
|
| 302 | {
|
| 303 | size_t i;
|
| 304 | for (i = 0; i < k7zSignatureSize; i++)
|
| 305 | if (testBytes[i] != k7zSignature[i])
|
| 306 | return 0;
|
| 307 | return 1;
|
| 308 | }
|
| 309 | |
| 310 | typedef struct _CSzState
|
| 311 | {
|
| 312 | Byte *Data;
|
| 313 | size_t Size;
|
| 314 | }CSzData;
|
| 315 | |
| 316 | static SRes SzReadByte(CSzData *sd, Byte *b)
|
| 317 | {
|
| 318 | if (sd->Size == 0)
|
| 319 | return SZ_ERROR_ARCHIVE;
|
| 320 | sd->Size--;
|
| 321 | *b = *sd->Data++;
|
| 322 | return SZ_OK;
|
| 323 | }
|
| 324 | |
| 325 | static SRes SzReadBytes(CSzData *sd, Byte *data, size_t size)
|
| 326 | {
|
| 327 | size_t i;
|
| 328 | for (i = 0; i < size; i++)
|
| 329 | {
|
| 330 | RINOK(SzReadByte(sd, data + i));
|
| 331 | }
|
| 332 | return SZ_OK;
|
| 333 | }
|
| 334 | |
| 335 | static SRes SzReadUInt32(CSzData *sd, UInt32 *value)
|
| 336 | {
|
| 337 | int i;
|
| 338 | *value = 0;
|
| 339 | for (i = 0; i < 4; i++)
|
| 340 | {
|
| 341 | Byte b;
|
| 342 | RINOK(SzReadByte(sd, &b));
|
| 343 | *value |= ((UInt32)(b) << (8 * i));
|
| 344 | }
|
| 345 | return SZ_OK;
|
| 346 | }
|
| 347 | |
| 348 | static SRes SzReadNumber(CSzData *sd, UInt64 *value)
|
| 349 | {
|
| 350 | Byte firstByte;
|
| 351 | Byte mask = 0x80;
|
| 352 | int i;
|
| 353 | RINOK(SzReadByte(sd, &firstByte));
|
| 354 | *value = 0;
|
| 355 | for (i = 0; i < 8; i++)
|
| 356 | {
|
| 357 | Byte b;
|
| 358 | if ((firstByte & mask) == 0)
|
| 359 | {
|
| 360 | UInt64 highPart = firstByte & (mask - 1);
|
| 361 | *value += (highPart << (8 * i));
|
| 362 | return SZ_OK;
|
| 363 | }
|
| 364 | RINOK(SzReadByte(sd, &b));
|
| 365 | *value |= ((UInt64)b << (8 * i));
|
| 366 | mask >>= 1;
|
| 367 | }
|
| 368 | return SZ_OK;
|
| 369 | }
|
| 370 | |
| 371 | static SRes SzReadNumber32(CSzData *sd, UInt32 *value)
|
| 372 | {
|
| 373 | UInt64 value64;
|
| 374 | RINOK(SzReadNumber(sd, &value64));
|
| 375 | if (value64 >= 0x80000000)
|
| 376 | return SZ_ERROR_UNSUPPORTED;
|
| 377 | if (value64 >= ((UInt64)(1) << ((sizeof(size_t) - 1) * 8 + 2)))
|
| 378 | return SZ_ERROR_UNSUPPORTED;
|
| 379 | *value = (UInt32)value64;
|
| 380 | return SZ_OK;
|
| 381 | }
|
| 382 | |
| 383 | static SRes SzReadID(CSzData *sd, UInt64 *value)
|
| 384 | {
|
| 385 | return SzReadNumber(sd, value);
|
| 386 | }
|
| 387 | |
| 388 | static SRes SzSkeepDataSize(CSzData *sd, UInt64 size)
|
| 389 | {
|
| 390 | if (size > sd->Size)
|
| 391 | return SZ_ERROR_ARCHIVE;
|
| 392 | sd->Size -= (size_t)size;
|
| 393 | sd->Data += (size_t)size;
|
| 394 | return SZ_OK;
|
| 395 | }
|
| 396 | |
| 397 | static SRes SzSkeepData(CSzData *sd)
|
| 398 | {
|
| 399 | UInt64 size;
|
| 400 | RINOK(SzReadNumber(sd, &size));
|
| 401 | return SzSkeepDataSize(sd, size);
|
| 402 | }
|
| 403 | |
| 404 | static SRes SzReadArchiveProperties(CSzData *sd)
|
| 405 | {
|
| 406 | for (;;)
|
| 407 | {
|
| 408 | UInt64 type;
|
| 409 | RINOK(SzReadID(sd, &type));
|
| 410 | if (type == k7zIdEnd)
|
| 411 | break;
|
| 412 | SzSkeepData(sd);
|
| 413 | }
|
| 414 | return SZ_OK;
|
| 415 | }
|
| 416 | |
| 417 | static SRes SzWaitAttribute(CSzData *sd, UInt64 attribute)
|
| 418 | {
|
| 419 | for (;;)
|
| 420 | {
|
| 421 | UInt64 type;
|
| 422 | RINOK(SzReadID(sd, &type));
|
| 423 | if (type == attribute)
|
| 424 | return SZ_OK;
|
| 425 | if (type == k7zIdEnd)
|
| 426 | return SZ_ERROR_ARCHIVE;
|
| 427 | RINOK(SzSkeepData(sd));
|
| 428 | }
|
| 429 | }
|
| 430 | |
| 431 | static SRes SzReadBoolVector(CSzData *sd, size_t numItems, Byte **v, ISzAlloc *alloc)
|
| 432 | {
|
| 433 | Byte b = 0;
|
| 434 | Byte mask = 0;
|
| 435 | size_t i;
|
| 436 | MY_ALLOC(Byte, *v, numItems, alloc);
|
| 437 | for (i = 0; i < numItems; i++)
|
| 438 | {
|
| 439 | if (mask == 0)
|
| 440 | {
|
| 441 | RINOK(SzReadByte(sd, &b));
|
| 442 | mask = 0x80;
|
| 443 | }
|
| 444 | (*v)[i] = (Byte)(((b & mask) != 0) ? 1 : 0);
|
| 445 | mask >>= 1;
|
| 446 | }
|
| 447 | return SZ_OK;
|
| 448 | }
|
| 449 | |
| 450 | static SRes SzReadBoolVector2(CSzData *sd, size_t numItems, Byte **v, ISzAlloc *alloc)
|
| 451 | {
|
| 452 | Byte allAreDefined;
|
| 453 | size_t i;
|
| 454 | RINOK(SzReadByte(sd, &allAreDefined));
|
| 455 | if (allAreDefined == 0)
|
| 456 | return SzReadBoolVector(sd, numItems, v, alloc);
|
| 457 | MY_ALLOC(Byte, *v, numItems, alloc);
|
| 458 | for (i = 0; i < numItems; i++)
|
| 459 | (*v)[i] = 1;
|
| 460 | return SZ_OK;
|
| 461 | }
|
| 462 | |
| 463 | static SRes SzReadHashDigests(
|
| 464 | CSzData *sd,
|
| 465 | size_t numItems,
|
| 466 | Byte **digestsDefined,
|
| 467 | UInt32 **digests,
|
| 468 | ISzAlloc *alloc)
|
| 469 | {
|
| 470 | size_t i;
|
| 471 | RINOK(SzReadBoolVector2(sd, numItems, digestsDefined, alloc));
|
| 472 | MY_ALLOC(UInt32, *digests, numItems, alloc);
|
| 473 | for (i = 0; i < numItems; i++)
|
| 474 | if ((*digestsDefined)[i])
|
| 475 | {
|
| 476 | RINOK(SzReadUInt32(sd, (*digests) + i));
|
| 477 | }
|
| 478 | return SZ_OK;
|
| 479 | }
|
| 480 | |
| 481 | static SRes SzReadPackInfo(
|
| 482 | CSzData *sd,
|
| 483 | UInt64 *dataOffset,
|
| 484 | UInt32 *numPackStreams,
|
| 485 | UInt64 **packSizes,
|
| 486 | Byte **packCRCsDefined,
|
| 487 | UInt32 **packCRCs,
|
| 488 | ISzAlloc *alloc)
|
| 489 | {
|
| 490 | UInt32 i;
|
| 491 | RINOK(SzReadNumber(sd, dataOffset));
|
| 492 | RINOK(SzReadNumber32(sd, numPackStreams));
|
| 493 | |
| 494 | RINOK(SzWaitAttribute(sd, k7zIdSize));
|
| 495 | |
| 496 | MY_ALLOC(UInt64, *packSizes, (size_t)*numPackStreams, alloc);
|
| 497 | |
| 498 | for (i = 0; i < *numPackStreams; i++)
|
| 499 | {
|
| 500 | RINOK(SzReadNumber(sd, (*packSizes) + i));
|
| 501 | }
|
| 502 | |
| 503 | for (;;)
|
| 504 | {
|
| 505 | UInt64 type;
|
| 506 | RINOK(SzReadID(sd, &type));
|
| 507 | if (type == k7zIdEnd)
|
| 508 | break;
|
| 509 | if (type == k7zIdCRC)
|
| 510 | {
|
| 511 | RINOK(SzReadHashDigests(sd, (size_t)*numPackStreams, packCRCsDefined, packCRCs, alloc));
|
| 512 | continue;
|
| 513 | }
|
| 514 | RINOK(SzSkeepData(sd));
|
| 515 | }
|
| 516 | if (*packCRCsDefined == 0)
|
| 517 | {
|
| 518 | MY_ALLOC(Byte, *packCRCsDefined, (size_t)*numPackStreams, alloc);
|
| 519 | MY_ALLOC(UInt32, *packCRCs, (size_t)*numPackStreams, alloc);
|
| 520 | for (i = 0; i < *numPackStreams; i++)
|
| 521 | {
|
| 522 | (*packCRCsDefined)[i] = 0;
|
| 523 | (*packCRCs)[i] = 0;
|
| 524 | }
|
| 525 | }
|
| 526 | return SZ_OK;
|
| 527 | }
|
| 528 | |
| 529 | static SRes SzReadSwitch(CSzData *sd)
|
| 530 | {
|
| 531 | Byte external;
|
| 532 | RINOK(SzReadByte(sd, &external));
|
| 533 | return (external == 0) ? SZ_OK: SZ_ERROR_UNSUPPORTED;
|
| 534 | }
|
| 535 | |
| 536 | static SRes SzGetNextFolderItem(CSzData *sd, CSzFolder *folder, ISzAlloc *alloc)
|
| 537 | {
|
| 538 | UInt32 numCoders, numBindPairs, numPackStreams, i;
|
| 539 | UInt32 numInStreams = 0, numOutStreams = 0;
|
| 540 | |
| 541 | RINOK(SzReadNumber32(sd, &numCoders));
|
| 542 | if (numCoders > NUM_FOLDER_CODERS_MAX)
|
| 543 | return SZ_ERROR_UNSUPPORTED;
|
| 544 | folder->NumCoders = numCoders;
|
| 545 | |
| 546 | MY_ALLOC(CSzCoderInfo, folder->Coders, (size_t)numCoders, alloc);
|
| 547 | |
| 548 | for (i = 0; i < numCoders; i++)
|
| 549 | SzCoderInfo_Init(folder->Coders + i);
|
| 550 | |
| 551 | for (i = 0; i < numCoders; i++)
|
| 552 | {
|
| 553 | Byte mainByte;
|
| 554 | CSzCoderInfo *coder = folder->Coders + i;
|
| 555 | {
|
| 556 | unsigned idSize, j;
|
| 557 | Byte longID[15];
|
| 558 | RINOK(SzReadByte(sd, &mainByte));
|
| 559 | idSize = (unsigned)(mainByte & 0xF);
|
| 560 | RINOK(SzReadBytes(sd, longID, idSize));
|
| 561 | if (idSize > sizeof(coder->MethodID))
|
| 562 | return SZ_ERROR_UNSUPPORTED;
|
| 563 | coder->MethodID = 0;
|
| 564 | for (j = 0; j < idSize; j++)
|
| 565 | coder->MethodID |= (UInt64)longID[idSize - 1 - j] << (8 * j);
|
| 566 | |
| 567 | if ((mainByte & 0x10) != 0)
|
| 568 | {
|
| 569 | RINOK(SzReadNumber32(sd, &coder->NumInStreams));
|
| 570 | RINOK(SzReadNumber32(sd, &coder->NumOutStreams));
|
| 571 | if (coder->NumInStreams > NUM_CODER_STREAMS_MAX ||
|
| 572 | coder->NumOutStreams > NUM_CODER_STREAMS_MAX)
|
| 573 | return SZ_ERROR_UNSUPPORTED;
|
| 574 | }
|
| 575 | else
|
| 576 | {
|
| 577 | coder->NumInStreams = 1;
|
| 578 | coder->NumOutStreams = 1;
|
| 579 | }
|
| 580 | if ((mainByte & 0x20) != 0)
|
| 581 | {
|
| 582 | UInt64 propertiesSize = 0;
|
| 583 | RINOK(SzReadNumber(sd, &propertiesSize));
|
| 584 | if (!Buf_Create(&coder->Props, (size_t)propertiesSize, alloc))
|
| 585 | return SZ_ERROR_MEM;
|
| 586 | RINOK(SzReadBytes(sd, coder->Props.data, (size_t)propertiesSize));
|
| 587 | }
|
| 588 | }
|
| 589 | while ((mainByte & 0x80) != 0)
|
| 590 | {
|
| 591 | RINOK(SzReadByte(sd, &mainByte));
|
| 592 | RINOK(SzSkeepDataSize(sd, (mainByte & 0xF)));
|
| 593 | if ((mainByte & 0x10) != 0)
|
| 594 | {
|
| 595 | UInt32 n;
|
| 596 | RINOK(SzReadNumber32(sd, &n));
|
| 597 | RINOK(SzReadNumber32(sd, &n));
|
| 598 | }
|
| 599 | if ((mainByte & 0x20) != 0)
|
| 600 | {
|
| 601 | UInt64 propertiesSize = 0;
|
| 602 | RINOK(SzReadNumber(sd, &propertiesSize));
|
| 603 | RINOK(SzSkeepDataSize(sd, propertiesSize));
|
| 604 | }
|
| 605 | }
|
| 606 | numInStreams += coder->NumInStreams;
|
| 607 | numOutStreams += coder->NumOutStreams;
|
| 608 | }
|
| 609 | |
| 610 | if (numOutStreams == 0)
|
| 611 | return SZ_ERROR_UNSUPPORTED;
|
| 612 | |
| 613 | folder->NumBindPairs = numBindPairs = numOutStreams - 1;
|
| 614 | MY_ALLOC(CSzBindPair, folder->BindPairs, (size_t)numBindPairs, alloc);
|
| 615 | |
| 616 | for (i = 0; i < numBindPairs; i++)
|
| 617 | {
|
| 618 | CSzBindPair *bp = folder->BindPairs + i;
|
| 619 | RINOK(SzReadNumber32(sd, &bp->InIndex));
|
| 620 | RINOK(SzReadNumber32(sd, &bp->OutIndex));
|
| 621 | }
|
| 622 | |
| 623 | if (numInStreams < numBindPairs)
|
| 624 | return SZ_ERROR_UNSUPPORTED;
|
| 625 | |
| 626 | folder->NumPackStreams = numPackStreams = numInStreams - numBindPairs;
|
| 627 | MY_ALLOC(UInt32, folder->PackStreams, (size_t)numPackStreams, alloc);
|
| 628 | |
| 629 | if (numPackStreams == 1)
|
| 630 | {
|
| 631 | for (i = 0; i < numInStreams ; i++)
|
| 632 | if (SzFolder_FindBindPairForInStream(folder, i) < 0)
|
| 633 | break;
|
| 634 | if (i == numInStreams)
|
| 635 | return SZ_ERROR_UNSUPPORTED;
|
| 636 | folder->PackStreams[0] = i;
|
| 637 | }
|
| 638 | else
|
| 639 | for (i = 0; i < numPackStreams; i++)
|
| 640 | {
|
| 641 | RINOK(SzReadNumber32(sd, folder->PackStreams + i));
|
| 642 | }
|
| 643 | return SZ_OK;
|
| 644 | }
|
| 645 | |
| 646 | static SRes SzReadUnpackInfo(
|
| 647 | CSzData *sd,
|
| 648 | UInt32 *numFolders,
|
| 649 | CSzFolder **folders, /* for alloc */
|
| 650 | ISzAlloc *alloc,
|
| 651 | ISzAlloc *allocTemp)
|
| 652 | {
|
| 653 | UInt32 i;
|
| 654 | RINOK(SzWaitAttribute(sd, k7zIdFolder));
|
| 655 | RINOK(SzReadNumber32(sd, numFolders));
|
| 656 | {
|
| 657 | RINOK(SzReadSwitch(sd));
|
| 658 | |
| 659 | MY_ALLOC(CSzFolder, *folders, (size_t)*numFolders, alloc);
|
| 660 | |
| 661 | for (i = 0; i < *numFolders; i++)
|
| 662 | SzFolder_Init((*folders) + i);
|
| 663 | |
| 664 | for (i = 0; i < *numFolders; i++)
|
| 665 | {
|
| 666 | RINOK(SzGetNextFolderItem(sd, (*folders) + i, alloc));
|
| 667 | }
|
| 668 | }
|
| 669 | |
| 670 | RINOK(SzWaitAttribute(sd, k7zIdCodersUnpackSize));
|
| 671 | |
| 672 | for (i = 0; i < *numFolders; i++)
|
| 673 | {
|
| 674 | UInt32 j;
|
| 675 | CSzFolder *folder = (*folders) + i;
|
| 676 | UInt32 numOutStreams = SzFolder_GetNumOutStreams(folder);
|
| 677 | |
| 678 | MY_ALLOC(UInt64, folder->UnpackSizes, (size_t)numOutStreams, alloc);
|
| 679 | |
| 680 | for (j = 0; j < numOutStreams; j++)
|
| 681 | {
|
| 682 | RINOK(SzReadNumber(sd, folder->UnpackSizes + j));
|
| 683 | }
|
| 684 | }
|
| 685 | |
| 686 | for (;;)
|
| 687 | {
|
| 688 | UInt64 type;
|
| 689 | RINOK(SzReadID(sd, &type));
|
| 690 | if (type == k7zIdEnd)
|
| 691 | return SZ_OK;
|
| 692 | if (type == k7zIdCRC)
|
| 693 | {
|
| 694 | SRes res;
|
| 695 | Byte *crcsDefined = 0;
|
| 696 | UInt32 *crcs = 0;
|
| 697 | res = SzReadHashDigests(sd, *numFolders, &crcsDefined, &crcs, allocTemp);
|
| 698 | if (res == SZ_OK)
|
| 699 | {
|
| 700 | for (i = 0; i < *numFolders; i++)
|
| 701 | {
|
| 702 | CSzFolder *folder = (*folders) + i;
|
| 703 | folder->UnpackCRCDefined = crcsDefined[i];
|
| 704 | folder->UnpackCRC = crcs[i];
|
| 705 | }
|
| 706 | }
|
| 707 | IAlloc_Free(allocTemp, crcs);
|
| 708 | IAlloc_Free(allocTemp, crcsDefined);
|
| 709 | RINOK(res);
|
| 710 | continue;
|
| 711 | }
|
| 712 | RINOK(SzSkeepData(sd));
|
| 713 | }
|
| 714 | }
|
| 715 | |
| 716 | static SRes SzReadSubStreamsInfo(
|
| 717 | CSzData *sd,
|
| 718 | UInt32 numFolders,
|
| 719 | CSzFolder *folders,
|
| 720 | UInt32 *numUnpackStreams,
|
| 721 | UInt64 **unpackSizes,
|
| 722 | Byte **digestsDefined,
|
| 723 | UInt32 **digests,
|
| 724 | ISzAlloc *allocTemp)
|
| 725 | {
|
| 726 | UInt64 type = 0;
|
| 727 | UInt32 i;
|
| 728 | UInt32 si = 0;
|
| 729 | UInt32 numDigests = 0;
|
| 730 | |
| 731 | for (i = 0; i < numFolders; i++)
|
| 732 | folders[i].NumUnpackStreams = 1;
|
| 733 | *numUnpackStreams = numFolders;
|
| 734 | |
| 735 | for (;;)
|
| 736 | {
|
| 737 | RINOK(SzReadID(sd, &type));
|
| 738 | if (type == k7zIdNumUnpackStream)
|
| 739 | {
|
| 740 | *numUnpackStreams = 0;
|
| 741 | for (i = 0; i < numFolders; i++)
|
| 742 | {
|
| 743 | UInt32 numStreams;
|
| 744 | RINOK(SzReadNumber32(sd, &numStreams));
|
| 745 | folders[i].NumUnpackStreams = numStreams;
|
| 746 | *numUnpackStreams += numStreams;
|
| 747 | }
|
| 748 | continue;
|
| 749 | }
|
| 750 | if (type == k7zIdCRC || type == k7zIdSize)
|
| 751 | break;
|
| 752 | if (type == k7zIdEnd)
|
| 753 | break;
|
| 754 | RINOK(SzSkeepData(sd));
|
| 755 | }
|
| 756 | |
| 757 | if (*numUnpackStreams == 0)
|
| 758 | {
|
| 759 | *unpackSizes = 0;
|
| 760 | *digestsDefined = 0;
|
| 761 | *digests = 0;
|
| 762 | }
|
| 763 | else
|
| 764 | {
|
| 765 | *unpackSizes = (UInt64 *)IAlloc_Alloc(allocTemp, (size_t)*numUnpackStreams * sizeof(UInt64));
|
| 766 | RINOM(*unpackSizes);
|
| 767 | *digestsDefined = (Byte *)IAlloc_Alloc(allocTemp, (size_t)*numUnpackStreams * sizeof(Byte));
|
| 768 | RINOM(*digestsDefined);
|
| 769 | *digests = (UInt32 *)IAlloc_Alloc(allocTemp, (size_t)*numUnpackStreams * sizeof(UInt32));
|
| 770 | RINOM(*digests);
|
| 771 | }
|
| 772 | |
| 773 | for (i = 0; i < numFolders; i++)
|
| 774 | {
|
| 775 | /*
|
| 776 | v3.13 incorrectly worked with empty folders
|
| 777 | v4.07: we check that folder is empty
|
| 778 | */
|
| 779 | UInt64 sum = 0;
|
| 780 | UInt32 j;
|
| 781 | UInt32 numSubstreams = folders[i].NumUnpackStreams;
|
| 782 | if (numSubstreams == 0)
|
| 783 | continue;
|
| 784 | if (type == k7zIdSize)
|
| 785 | for (j = 1; j < numSubstreams; j++)
|
| 786 | {
|
| 787 | UInt64 size;
|
| 788 | RINOK(SzReadNumber(sd, &size));
|
| 789 | (*unpackSizes)[si++] = size;
|
| 790 | sum += size;
|
| 791 | }
|
| 792 | (*unpackSizes)[si++] = SzFolder_GetUnpackSize(folders + i) - sum;
|
| 793 | }
|
| 794 | if (type == k7zIdSize)
|
| 795 | {
|
| 796 | RINOK(SzReadID(sd, &type));
|
| 797 | }
|
| 798 | |
| 799 | for (i = 0; i < *numUnpackStreams; i++)
|
| 800 | {
|
| 801 | (*digestsDefined)[i] = 0;
|
| 802 | (*digests)[i] = 0;
|
| 803 | }
|
| 804 | |
| 805 | |
| 806 | for (i = 0; i < numFolders; i++)
|
| 807 | {
|
| 808 | UInt32 numSubstreams = folders[i].NumUnpackStreams;
|
| 809 | if (numSubstreams != 1 || !folders[i].UnpackCRCDefined)
|
| 810 | numDigests += numSubstreams;
|
| 811 | }
|
| 812 | |
| 813 |
|
| 814 | si = 0;
|
| 815 | for (;;)
|
| 816 | {
|
| 817 | if (type == k7zIdCRC)
|
| 818 | {
|
| 819 | int digestIndex = 0;
|
| 820 | Byte *digestsDefined2 = 0;
|
| 821 | UInt32 *digests2 = 0;
|
| 822 | SRes res = SzReadHashDigests(sd, numDigests, &digestsDefined2, &digests2, allocTemp);
|
| 823 | if (res == SZ_OK)
|
| 824 | {
|
| 825 | for (i = 0; i < numFolders; i++)
|
| 826 | {
|
| 827 | CSzFolder *folder = folders + i;
|
| 828 | UInt32 numSubstreams = folder->NumUnpackStreams;
|
| 829 | if (numSubstreams == 1 && folder->UnpackCRCDefined)
|
| 830 | {
|
| 831 | (*digestsDefined)[si] = 1;
|
| 832 | (*digests)[si] = folder->UnpackCRC;
|
| 833 | si++;
|
| 834 | }
|
| 835 | else
|
| 836 | {
|
| 837 | UInt32 j;
|
| 838 | for (j = 0; j < numSubstreams; j++, digestIndex++)
|
| 839 | {
|
| 840 | (*digestsDefined)[si] = digestsDefined2[digestIndex];
|
| 841 | (*digests)[si] = digests2[digestIndex];
|
| 842 | si++;
|
| 843 | }
|
| 844 | }
|
| 845 | }
|
| 846 | }
|
| 847 | IAlloc_Free(allocTemp, digestsDefined2);
|
| 848 | IAlloc_Free(allocTemp, digests2);
|
| 849 | RINOK(res);
|
| 850 | }
|
| 851 | else if (type == k7zIdEnd)
|
| 852 | return SZ_OK;
|
| 853 | else
|
| 854 | {
|
| 855 | RINOK(SzSkeepData(sd));
|
| 856 | }
|
| 857 | RINOK(SzReadID(sd, &type));
|
| 858 | }
|
| 859 | }
|
| 860 | |
| 861 | |
| 862 | static SRes SzReadStreamsInfo(
|
| 863 | CSzData *sd,
|
| 864 | UInt64 *dataOffset,
|
| 865 | CSzAr *p,
|
| 866 | UInt32 *numUnpackStreams,
|
| 867 | UInt64 **unpackSizes, /* allocTemp */
|
| 868 | Byte **digestsDefined, /* allocTemp */
|
| 869 | UInt32 **digests, /* allocTemp */
|
| 870 | ISzAlloc *alloc,
|
| 871 | ISzAlloc *allocTemp)
|
| 872 | {
|
| 873 | for (;;)
|
| 874 | {
|
| 875 | UInt64 type;
|
| 876 | RINOK(SzReadID(sd, &type));
|
| 877 | if ((UInt64)(int)type != type)
|
| 878 | return SZ_ERROR_UNSUPPORTED;
|
| 879 | switch((int)type)
|
| 880 | {
|
| 881 | case k7zIdEnd:
|
| 882 | return SZ_OK;
|
| 883 | case k7zIdPackInfo:
|
| 884 | {
|
| 885 | RINOK(SzReadPackInfo(sd, dataOffset, &p->NumPackStreams,
|
| 886 | &p->PackSizes, &p->PackCRCsDefined, &p->PackCRCs, alloc));
|
| 887 | break;
|
| 888 | }
|
| 889 | case k7zIdUnpackInfo:
|
| 890 | {
|
| 891 | RINOK(SzReadUnpackInfo(sd, &p->NumFolders, &p->Folders, alloc, allocTemp));
|
| 892 | break;
|
| 893 | }
|
| 894 | case k7zIdSubStreamsInfo:
|
| 895 | {
|
| 896 | RINOK(SzReadSubStreamsInfo(sd, p->NumFolders, p->Folders,
|
| 897 | numUnpackStreams, unpackSizes, digestsDefined, digests, allocTemp));
|
| 898 | break;
|
| 899 | }
|
| 900 | default:
|
| 901 | return SZ_ERROR_UNSUPPORTED;
|
| 902 | }
|
| 903 | }
|
| 904 | }
|
| 905 | |
| 906 | size_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, UInt16 *dest)
|
| 907 | {
|
| 908 | size_t len = p->FileNameOffsets[fileIndex + 1] - p->FileNameOffsets[fileIndex];
|
| 909 | if (dest != 0)
|
| 910 | {
|
| 911 | size_t i;
|
| 912 | const Byte *src = p->FileNames.data + (p->FileNameOffsets[fileIndex] * 2);
|
| 913 | for (i = 0; i < len; i++)
|
| 914 | dest[i] = GetUi16(src + i * 2);
|
| 915 | }
|
| 916 | return len;
|
| 917 | }
|
| 918 | |
| 919 | static SRes SzReadFileNames(const Byte *p, size_t size, UInt32 numFiles, size_t *sizes)
|
| 920 | {
|
| 921 | UInt32 i;
|
| 922 | size_t pos = 0;
|
| 923 | for (i = 0; i < numFiles; i++)
|
| 924 | {
|
| 925 | sizes[i] = pos;
|
| 926 | for (;;)
|
| 927 | {
|
| 928 | if (pos >= size)
|
| 929 | return SZ_ERROR_ARCHIVE;
|
| 930 | if (p[pos * 2] == 0 && p[pos * 2 + 1] == 0)
|
| 931 | break;
|
| 932 | pos++;
|
| 933 | }
|
| 934 | pos++;
|
| 935 | }
|
| 936 | sizes[i] = pos;
|
| 937 | return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE;
|
| 938 | }
|
| 939 | |
| 940 | static SRes SzReadHeader2(
|
| 941 | CSzArEx *p, /* allocMain */
|
| 942 | CSzData *sd,
|
| 943 | UInt64 **unpackSizes, /* allocTemp */
|
| 944 | Byte **digestsDefined, /* allocTemp */
|
| 945 | UInt32 **digests, /* allocTemp */
|
| 946 | Byte **emptyStreamVector, /* allocTemp */
|
| 947 | Byte **emptyFileVector, /* allocTemp */
|
| 948 | Byte **lwtVector, /* allocTemp */
|
| 949 | ISzAlloc *allocMain,
|
| 950 | ISzAlloc *allocTemp)
|
| 951 | {
|
| 952 | UInt64 type;
|
| 953 | UInt32 numUnpackStreams = 0;
|
| 954 | UInt32 numFiles = 0;
|
| 955 | CSzFileItem *files = 0;
|
| 956 | UInt32 numEmptyStreams = 0;
|
| 957 | UInt32 i;
|
| 958 | |
| 959 | RINOK(SzReadID(sd, &type));
|
| 960 | |
| 961 | if (type == k7zIdArchiveProperties)
|
| 962 | {
|
| 963 | RINOK(SzReadArchiveProperties(sd));
|
| 964 | RINOK(SzReadID(sd, &type));
|
| 965 | }
|
| 966 |
|
| 967 |
|
| 968 | if (type == k7zIdMainStreamsInfo)
|
| 969 | {
|
| 970 | RINOK(SzReadStreamsInfo(sd,
|
| 971 | &p->dataPos,
|
| 972 | &p->db,
|
| 973 | &numUnpackStreams,
|
| 974 | unpackSizes,
|
| 975 | digestsDefined,
|
| 976 | digests, allocMain, allocTemp));
|
| 977 | p->dataPos += p->startPosAfterHeader;
|
| 978 | RINOK(SzReadID(sd, &type));
|
| 979 | }
|
| 980 | |
| 981 | if (type == k7zIdEnd)
|
| 982 | return SZ_OK;
|
| 983 | if (type != k7zIdFilesInfo)
|
| 984 | return SZ_ERROR_ARCHIVE;
|
| 985 | |
| 986 | RINOK(SzReadNumber32(sd, &numFiles));
|
| 987 | p->db.NumFiles = numFiles;
|
| 988 | |
| 989 | MY_ALLOC(CSzFileItem, files, (size_t)numFiles, allocMain);
|
| 990 | |
| 991 | p->db.Files = files;
|
| 992 | for (i = 0; i < numFiles; i++)
|
| 993 | SzFile_Init(files + i);
|
| 994 | |
| 995 | for (;;)
|
| 996 | {
|
| 997 | UInt64 type;
|
| 998 | UInt64 size;
|
| 999 | RINOK(SzReadID(sd, &type));
|
| 1000 | if (type == k7zIdEnd)
|
| 1001 | break;
|
| 1002 | RINOK(SzReadNumber(sd, &size));
|
| 1003 | if (size > sd->Size)
|
| 1004 | return SZ_ERROR_ARCHIVE;
|
| 1005 | if ((UInt64)(int)type != type)
|
| 1006 | {
|
| 1007 | RINOK(SzSkeepDataSize(sd, size));
|
| 1008 | }
|
| 1009 | else
|
| 1010 | switch((int)type)
|
| 1011 | {
|
| 1012 | case k7zIdName:
|
| 1013 | {
|
| 1014 | size_t namesSize;
|
| 1015 | RINOK(SzReadSwitch(sd));
|
| 1016 | namesSize = (size_t)size - 1;
|
| 1017 | if ((namesSize & 1) != 0)
|
| 1018 | return SZ_ERROR_ARCHIVE;
|
| 1019 | if (!Buf_Create(&p->FileNames, namesSize, allocMain))
|
| 1020 | return SZ_ERROR_MEM;
|
| 1021 | MY_ALLOC(size_t, p->FileNameOffsets, numFiles + 1, allocMain);
|
| 1022 | memcpy(p->FileNames.data, sd->Data, namesSize);
|
| 1023 | RINOK(SzReadFileNames(sd->Data, namesSize >> 1, numFiles, p->FileNameOffsets))
|
| 1024 | RINOK(SzSkeepDataSize(sd, namesSize));
|
| 1025 | break;
|
| 1026 | }
|
| 1027 | case k7zIdEmptyStream:
|
| 1028 | {
|
| 1029 | RINOK(SzReadBoolVector(sd, numFiles, emptyStreamVector, allocTemp));
|
| 1030 | numEmptyStreams = 0;
|
| 1031 | for (i = 0; i < numFiles; i++)
|
| 1032 | if ((*emptyStreamVector)[i])
|
| 1033 | numEmptyStreams++;
|
| 1034 | break;
|
| 1035 | }
|
| 1036 | case k7zIdEmptyFile:
|
| 1037 | {
|
| 1038 | RINOK(SzReadBoolVector(sd, numEmptyStreams, emptyFileVector, allocTemp));
|
| 1039 | break;
|
| 1040 | }
|
| 1041 | case k7zIdWinAttributes:
|
| 1042 | {
|
| 1043 | RINOK(SzReadBoolVector2(sd, numFiles, lwtVector, allocTemp));
|
| 1044 | RINOK(SzReadSwitch(sd));
|
| 1045 | for (i = 0; i < numFiles; i++)
|
| 1046 | {
|
| 1047 | CSzFileItem *f = &files[i];
|
| 1048 | Byte defined = (*lwtVector)[i];
|
| 1049 | f->AttribDefined = defined;
|
| 1050 | f->Attrib = 0;
|
| 1051 | if (defined)
|
| 1052 | {
|
| 1053 | RINOK(SzReadUInt32(sd, &f->Attrib));
|
| 1054 | }
|
| 1055 | }
|
| 1056 | IAlloc_Free(allocTemp, *lwtVector);
|
| 1057 | *lwtVector = NULL;
|
| 1058 | break;
|
| 1059 | }
|
| 1060 | case k7zIdMTime:
|
| 1061 | {
|
| 1062 | RINOK(SzReadBoolVector2(sd, numFiles, lwtVector, allocTemp));
|
| 1063 | RINOK(SzReadSwitch(sd));
|
| 1064 | for (i = 0; i < numFiles; i++)
|
| 1065 | {
|
| 1066 | CSzFileItem *f = &files[i];
|
| 1067 | Byte defined = (*lwtVector)[i];
|
| 1068 | f->MTimeDefined = defined;
|
| 1069 | f->MTime.Low = f->MTime.High = 0;
|
| 1070 | if (defined)
|
| 1071 | {
|
| 1072 | RINOK(SzReadUInt32(sd, &f->MTime.Low));
|
| 1073 | RINOK(SzReadUInt32(sd, &f->MTime.High));
|
| 1074 | }
|
| 1075 | }
|
| 1076 | IAlloc_Free(allocTemp, *lwtVector);
|
| 1077 | *lwtVector = NULL;
|
| 1078 | break;
|
| 1079 | }
|
| 1080 | default:
|
| 1081 | {
|
| 1082 | RINOK(SzSkeepDataSize(sd, size));
|
| 1083 | }
|
| 1084 | }
|
| 1085 | }
|
| 1086 | |
| 1087 | {
|
| 1088 | UInt32 emptyFileIndex = 0;
|
| 1089 | UInt32 sizeIndex = 0;
|
| 1090 | for (i = 0; i < numFiles; i++)
|
| 1091 | {
|
| 1092 | CSzFileItem *file = files + i;
|
| 1093 | file->IsAnti = 0;
|
| 1094 | if (*emptyStreamVector == 0)
|
| 1095 | file->HasStream = 1;
|
| 1096 | else
|
| 1097 | file->HasStream = (Byte)((*emptyStreamVector)[i] ? 0 : 1);
|
| 1098 | if (file->HasStream)
|
| 1099 | {
|
| 1100 | file->IsDir = 0;
|
| 1101 | file->Size = (*unpackSizes)[sizeIndex];
|
| 1102 | file->Crc = (*digests)[sizeIndex];
|
| 1103 | file->CrcDefined = (Byte)(*digestsDefined)[sizeIndex];
|
| 1104 | sizeIndex++;
|
| 1105 | }
|
| 1106 | else
|
| 1107 | {
|
| 1108 | if (*emptyFileVector == 0)
|
| 1109 | file->IsDir = 1;
|
| 1110 | else
|
| 1111 | file->IsDir = (Byte)((*emptyFileVector)[emptyFileIndex] ? 0 : 1);
|
| 1112 | emptyFileIndex++;
|
| 1113 | file->Size = 0;
|
| 1114 | file->Crc = 0;
|
| 1115 | file->CrcDefined = 0;
|
| 1116 | }
|
| 1117 | }
|
| 1118 | }
|
| 1119 | return SzArEx_Fill(p, allocMain);
|
| 1120 | }
|
| 1121 | |
| 1122 | static SRes SzReadHeader(
|
| 1123 | CSzArEx *p,
|
| 1124 | CSzData *sd,
|
| 1125 | ISzAlloc *allocMain,
|
| 1126 | ISzAlloc *allocTemp)
|
| 1127 | {
|
| 1128 | UInt64 *unpackSizes = 0;
|
| 1129 | Byte *digestsDefined = 0;
|
| 1130 | UInt32 *digests = 0;
|
| 1131 | Byte *emptyStreamVector = 0;
|
| 1132 | Byte *emptyFileVector = 0;
|
| 1133 | Byte *lwtVector = 0;
|
| 1134 | SRes res = SzReadHeader2(p, sd,
|
| 1135 | &unpackSizes, &digestsDefined, &digests,
|
| 1136 | &emptyStreamVector, &emptyFileVector, &lwtVector,
|
| 1137 | allocMain, allocTemp);
|
| 1138 | IAlloc_Free(allocTemp, unpackSizes);
|
| 1139 | IAlloc_Free(allocTemp, digestsDefined);
|
| 1140 | IAlloc_Free(allocTemp, digests);
|
| 1141 | IAlloc_Free(allocTemp, emptyStreamVector);
|
| 1142 | IAlloc_Free(allocTemp, emptyFileVector);
|
| 1143 | IAlloc_Free(allocTemp, lwtVector);
|
| 1144 | return res;
|
| 1145 | }
|
| 1146 | |
| 1147 | static SRes SzReadAndDecodePackedStreams2(
|
| 1148 | ILookInStream *inStream,
|
| 1149 | CSzData *sd,
|
| 1150 | CBuf *outBuffer,
|
| 1151 | UInt64 baseOffset,
|
| 1152 | CSzAr *p,
|
| 1153 | UInt64 **unpackSizes,
|
| 1154 | Byte **digestsDefined,
|
| 1155 | UInt32 **digests,
|
| 1156 | ISzAlloc *allocTemp)
|
| 1157 | {
|
| 1158 | |
| 1159 | UInt32 numUnpackStreams = 0;
|
| 1160 | UInt64 dataStartPos;
|
| 1161 | CSzFolder *folder;
|
| 1162 | UInt64 unpackSize;
|
| 1163 | SRes res;
|
| 1164 | |
| 1165 | RINOK(SzReadStreamsInfo(sd, &dataStartPos, p,
|
| 1166 | &numUnpackStreams, unpackSizes, digestsDefined, digests,
|
| 1167 | allocTemp, allocTemp));
|
| 1168 | |
| 1169 | dataStartPos += baseOffset;
|
| 1170 | if (p->NumFolders != 1)
|
| 1171 | return SZ_ERROR_ARCHIVE;
|
| 1172 | |
| 1173 | folder = p->Folders;
|
| 1174 | unpackSize = SzFolder_GetUnpackSize(folder);
|
| 1175 | |
| 1176 | RINOK(LookInStream_SeekTo(inStream, dataStartPos));
|
| 1177 | |
| 1178 | if (!Buf_Create(outBuffer, (size_t)unpackSize, allocTemp))
|
| 1179 | return SZ_ERROR_MEM;
|
| 1180 | |
| 1181 | res = SzFolder_Decode(folder, p->PackSizes,
|
| 1182 | inStream, dataStartPos,
|
| 1183 | outBuffer->data, (size_t)unpackSize, allocTemp);
|
| 1184 | RINOK(res);
|
| 1185 | if (folder->UnpackCRCDefined)
|
| 1186 | if (CrcCalc(outBuffer->data, (size_t)unpackSize) != folder->UnpackCRC)
|
| 1187 | return SZ_ERROR_CRC;
|
| 1188 | return SZ_OK;
|
| 1189 | }
|
| 1190 | |
| 1191 | static SRes SzReadAndDecodePackedStreams(
|
| 1192 | ILookInStream *inStream,
|
| 1193 | CSzData *sd,
|
| 1194 | CBuf *outBuffer,
|
| 1195 | UInt64 baseOffset,
|
| 1196 | ISzAlloc *allocTemp)
|
| 1197 | {
|
| 1198 | CSzAr p;
|
| 1199 | UInt64 *unpackSizes = 0;
|
| 1200 | Byte *digestsDefined = 0;
|
| 1201 | UInt32 *digests = 0;
|
| 1202 | SRes res;
|
| 1203 | SzAr_Init(&p);
|
| 1204 | res = SzReadAndDecodePackedStreams2(inStream, sd, outBuffer, baseOffset,
|
| 1205 | &p, &unpackSizes, &digestsDefined, &digests,
|
| 1206 | allocTemp);
|
| 1207 | SzAr_Free(&p, allocTemp);
|
| 1208 | IAlloc_Free(allocTemp, unpackSizes);
|
| 1209 | IAlloc_Free(allocTemp, digestsDefined);
|
| 1210 | IAlloc_Free(allocTemp, digests);
|
| 1211 | return res;
|
| 1212 | }
|
| 1213 | |
| 1214 | static SRes SzArEx_Open2(
|
| 1215 | CSzArEx *p,
|
| 1216 | ILookInStream *inStream,
|
| 1217 | ISzAlloc *allocMain,
|
| 1218 | ISzAlloc *allocTemp)
|
| 1219 | {
|
| 1220 | Byte header[k7zStartHeaderSize];
|
| 1221 | Int64 startArcPos;
|
| 1222 | UInt64 nextHeaderOffset, nextHeaderSize;
|
| 1223 | size_t nextHeaderSizeT;
|
| 1224 | UInt32 nextHeaderCRC;
|
| 1225 | CBuf buffer;
|
| 1226 | SRes res;
|
| 1227 | |
| 1228 | startArcPos = 0;
|
| 1229 | RINOK(inStream->Seek(inStream, &startArcPos, SZ_SEEK_CUR));
|
| 1230 | |
| 1231 | RINOK(LookInStream_Read2(inStream, header, k7zStartHeaderSize, SZ_ERROR_NO_ARCHIVE));
|
| 1232 | |
| 1233 | if (!TestSignatureCandidate(header))
|
| 1234 | return SZ_ERROR_NO_ARCHIVE;
|
| 1235 | if (header[6] != k7zMajorVersion)
|
| 1236 | return SZ_ERROR_UNSUPPORTED;
|
| 1237 | |
| 1238 | nextHeaderOffset = GetUi64(header + 12);
|
| 1239 | nextHeaderSize = GetUi64(header + 20);
|
| 1240 | nextHeaderCRC = GetUi32(header + 28);
|
| 1241 | |
| 1242 | p->startPosAfterHeader = startArcPos + k7zStartHeaderSize;
|
| 1243 | |
| 1244 | if (CrcCalc(header + 12, 20) != GetUi32(header + 8))
|
| 1245 | return SZ_ERROR_CRC;
|
| 1246 | |
| 1247 | nextHeaderSizeT = (size_t)nextHeaderSize;
|
| 1248 | if (nextHeaderSizeT != nextHeaderSize)
|
| 1249 | return SZ_ERROR_MEM;
|
| 1250 | if (nextHeaderSizeT == 0)
|
| 1251 | return SZ_OK;
|
| 1252 | if (nextHeaderOffset > nextHeaderOffset + nextHeaderSize ||
|
| 1253 | nextHeaderOffset > nextHeaderOffset + nextHeaderSize + k7zStartHeaderSize)
|
| 1254 | return SZ_ERROR_NO_ARCHIVE;
|
| 1255 | |
| 1256 | {
|
| 1257 | Int64 pos = 0;
|
| 1258 | RINOK(inStream->Seek(inStream, &pos, SZ_SEEK_END));
|
| 1259 | if ((UInt64)pos < startArcPos + nextHeaderOffset ||
|
| 1260 | (UInt64)pos < startArcPos + k7zStartHeaderSize + nextHeaderOffset ||
|
| 1261 | (UInt64)pos < startArcPos + k7zStartHeaderSize + nextHeaderOffset + nextHeaderSize)
|
| 1262 | return SZ_ERROR_INPUT_EOF;
|
| 1263 | }
|
| 1264 | |
| 1265 | RINOK(LookInStream_SeekTo(inStream, startArcPos + k7zStartHeaderSize + nextHeaderOffset));
|
| 1266 | |
| 1267 | if (!Buf_Create(&buffer, nextHeaderSizeT, allocTemp))
|
| 1268 | return SZ_ERROR_MEM;
|
| 1269 | |
| 1270 | res = LookInStream_Read(inStream, buffer.data, nextHeaderSizeT);
|
| 1271 | if (res == SZ_OK)
|
| 1272 | {
|
| 1273 | res = SZ_ERROR_ARCHIVE;
|
| 1274 | if (CrcCalc(buffer.data, nextHeaderSizeT) == nextHeaderCRC)
|
| 1275 | {
|
| 1276 | CSzData sd;
|
| 1277 | UInt64 type;
|
| 1278 | sd.Data = buffer.data;
|
| 1279 | sd.Size = buffer.size;
|
| 1280 | res = SzReadID(&sd, &type);
|
| 1281 | if (res == SZ_OK)
|
| 1282 | {
|
| 1283 | if (type == k7zIdEncodedHeader)
|
| 1284 | {
|
| 1285 | CBuf outBuffer;
|
| 1286 | Buf_Init(&outBuffer);
|
| 1287 | res = SzReadAndDecodePackedStreams(inStream, &sd, &outBuffer, p->startPosAfterHeader, allocTemp);
|
| 1288 | if (res != SZ_OK)
|
| 1289 | Buf_Free(&outBuffer, allocTemp);
|
| 1290 | else
|
| 1291 | {
|
| 1292 | Buf_Free(&buffer, allocTemp);
|
| 1293 | buffer.data = outBuffer.data;
|
| 1294 | buffer.size = outBuffer.size;
|
| 1295 | sd.Data = buffer.data;
|
| 1296 | sd.Size = buffer.size;
|
| 1297 | res = SzReadID(&sd, &type);
|
| 1298 | }
|
| 1299 | }
|
| 1300 | }
|
| 1301 | if (res == SZ_OK)
|
| 1302 | {
|
| 1303 | if (type == k7zIdHeader)
|
| 1304 | res = SzReadHeader(p, &sd, allocMain, allocTemp);
|
| 1305 | else
|
| 1306 | res = SZ_ERROR_UNSUPPORTED;
|
| 1307 | }
|
| 1308 | }
|
| 1309 | }
|
| 1310 | Buf_Free(&buffer, allocTemp);
|
| 1311 | return res;
|
| 1312 | }
|
| 1313 | |
| 1314 | SRes SzArEx_Open(CSzArEx *p, ILookInStream *inStream, ISzAlloc *allocMain, ISzAlloc *allocTemp)
|
| 1315 | {
|
| 1316 | SRes res = SzArEx_Open2(p, inStream, allocMain, allocTemp);
|
| 1317 | if (res != SZ_OK)
|
| 1318 | SzArEx_Free(p, allocMain);
|
| 1319 | return res;
|
| 1320 | }
|
| 1321 | |
| 1322 | SRes SzArEx_Extract(
|
| 1323 | const CSzArEx *p,
|
| 1324 | ILookInStream *inStream,
|
| 1325 | UInt32 fileIndex,
|
| 1326 | UInt32 *blockIndex,
|
| 1327 | Byte **outBuffer,
|
| 1328 | size_t *outBufferSize,
|
| 1329 | size_t *offset,
|
| 1330 | size_t *outSizeProcessed,
|
| 1331 | ISzAlloc *allocMain,
|
| 1332 | ISzAlloc *allocTemp)
|
| 1333 | {
|
| 1334 | UInt32 folderIndex = p->FileIndexToFolderIndexMap[fileIndex];
|
| 1335 | SRes res = SZ_OK;
|
| 1336 | *offset = 0;
|
| 1337 | *outSizeProcessed = 0;
|
| 1338 | if (folderIndex == (UInt32)-1)
|
| 1339 | {
|
| 1340 | IAlloc_Free(allocMain, *outBuffer);
|
| 1341 | *blockIndex = folderIndex;
|
| 1342 | *outBuffer = 0;
|
| 1343 | *outBufferSize = 0;
|
| 1344 | return SZ_OK;
|
| 1345 | }
|
| 1346 | |
| 1347 | if (*outBuffer == 0 || *blockIndex != folderIndex)
|
| 1348 | {
|
| 1349 | CSzFolder *folder = p->db.Folders + folderIndex;
|
| 1350 | UInt64 unpackSizeSpec = SzFolder_GetUnpackSize(folder);
|
| 1351 | size_t unpackSize = (size_t)unpackSizeSpec;
|
| 1352 | UInt64 startOffset = SzArEx_GetFolderStreamPos(p, folderIndex, 0);
|
| 1353 | |
| 1354 | if (unpackSize != unpackSizeSpec)
|
| 1355 | return SZ_ERROR_MEM;
|
| 1356 | *blockIndex = folderIndex;
|
| 1357 | IAlloc_Free(allocMain, *outBuffer);
|
| 1358 | *outBuffer = 0;
|
| 1359 | |
| 1360 | RINOK(LookInStream_SeekTo(inStream, startOffset));
|
| 1361 | |
| 1362 | if (res == SZ_OK)
|
| 1363 | {
|
| 1364 | *outBufferSize = unpackSize;
|
| 1365 | if (unpackSize != 0)
|
| 1366 | {
|
| 1367 | *outBuffer = (Byte *)IAlloc_Alloc(allocMain, unpackSize);
|
| 1368 | if (*outBuffer == 0)
|
| 1369 | res = SZ_ERROR_MEM;
|
| 1370 | }
|
| 1371 | if (res == SZ_OK)
|
| 1372 | {
|
| 1373 | res = SzFolder_Decode(folder,
|
| 1374 | p->db.PackSizes + p->FolderStartPackStreamIndex[folderIndex],
|
| 1375 | inStream, startOffset,
|
| 1376 | *outBuffer, unpackSize, allocTemp);
|
| 1377 | if (res == SZ_OK)
|
| 1378 | {
|
| 1379 | if (folder->UnpackCRCDefined)
|
| 1380 | {
|
| 1381 | if (CrcCalc(*outBuffer, unpackSize) != folder->UnpackCRC)
|
| 1382 | res = SZ_ERROR_CRC;
|
| 1383 | }
|
| 1384 | }
|
| 1385 | }
|
| 1386 | }
|
| 1387 | }
|
| 1388 | if (res == SZ_OK)
|
| 1389 | {
|
| 1390 | UInt32 i;
|
| 1391 | CSzFileItem *fileItem = p->db.Files + fileIndex;
|
| 1392 | *offset = 0;
|
| 1393 | for (i = p->FolderStartFileIndex[folderIndex]; i < fileIndex; i++)
|
| 1394 | *offset += (UInt32)p->db.Files[i].Size;
|
| 1395 | *outSizeProcessed = (size_t)fileItem->Size;
|
| 1396 | if (*offset + *outSizeProcessed > *outBufferSize)
|
| 1397 | return SZ_ERROR_FAIL;
|
| 1398 | if (fileItem->CrcDefined && CrcCalc(*outBuffer + *offset, *outSizeProcessed) != fileItem->Crc)
|
| 1399 | res = SZ_ERROR_CRC;
|
| 1400 | }
|
| 1401 | return res;
|
| 1402 | }
|
src/lib/lib7z/XzEnc.c
| r0 | r14440 | |
| 1 | /* XzEnc.c -- Xz Encode
|
| 2 | 2011-02-07 : Igor Pavlov : Public domain */
|
| 3 | |
| 4 | #include <stdlib.h>
|
| 5 | #include <string.h>
|
| 6 | |
| 7 | #include "7zCrc.h"
|
| 8 | #include "Alloc.h"
|
| 9 | #include "Bra.h"
|
| 10 | #include "CpuArch.h"
|
| 11 | #ifdef USE_SUBBLOCK
|
| 12 | #include "Bcj3Enc.c"
|
| 13 | #include "SbFind.c"
|
| 14 | #include "SbEnc.c"
|
| 15 | #endif
|
| 16 | |
| 17 | #include "XzEnc.h"
|
| 18 | |
| 19 | static void *SzBigAlloc(void *p, size_t size) { p = p; return BigAlloc(size); }
|
| 20 | static void SzBigFree(void *p, void *address) { p = p; BigFree(address); }
|
| 21 | static ISzAlloc g_BigAlloc = { SzBigAlloc, SzBigFree };
|
| 22 | |
| 23 | static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); }
|
| 24 | static void SzFree(void *p, void *address) { p = p; MyFree(address); }
|
| 25 | static ISzAlloc g_Alloc = { SzAlloc, SzFree };
|
| 26 | |
| 27 | #define XzBlock_ClearFlags(p) (p)->flags = 0;
|
| 28 | #define XzBlock_SetNumFilters(p, n) (p)->flags |= ((n) - 1);
|
| 29 | #define XzBlock_SetHasPackSize(p) (p)->flags |= XZ_BF_PACK_SIZE;
|
| 30 | #define XzBlock_SetHasUnpackSize(p) (p)->flags |= XZ_BF_UNPACK_SIZE;
|
| 31 | |
| 32 | static SRes WriteBytes(ISeqOutStream *s, const void *buf, UInt32 size)
|
| 33 | {
|
| 34 | return (s->Write(s, buf, size) == size) ? SZ_OK : SZ_ERROR_WRITE;
|
| 35 | }
|
| 36 | |
| 37 | static SRes WriteBytesAndCrc(ISeqOutStream *s, const void *buf, UInt32 size, UInt32 *crc)
|
| 38 | {
|
| 39 | *crc = CrcUpdate(*crc, buf, size);
|
| 40 | return WriteBytes(s, buf, size);
|
| 41 | }
|
| 42 | |
| 43 | SRes Xz_WriteHeader(CXzStreamFlags f, ISeqOutStream *s)
|
| 44 | {
|
| 45 | UInt32 crc;
|
| 46 | Byte header[XZ_STREAM_HEADER_SIZE];
|
| 47 | memcpy(header, XZ_SIG, XZ_SIG_SIZE);
|
| 48 | header[XZ_SIG_SIZE] = (Byte)(f >> 8);
|
| 49 | header[XZ_SIG_SIZE + 1] = (Byte)(f & 0xFF);
|
| 50 | crc = CrcCalc(header + XZ_SIG_SIZE, XZ_STREAM_FLAGS_SIZE);
|
| 51 | SetUi32(header + XZ_SIG_SIZE + XZ_STREAM_FLAGS_SIZE, crc);
|
| 52 | return WriteBytes(s, header, XZ_STREAM_HEADER_SIZE);
|
| 53 | }
|
| 54 | |
| 55 | SRes XzBlock_WriteHeader(const CXzBlock *p, ISeqOutStream *s)
|
| 56 | {
|
| 57 | Byte header[XZ_BLOCK_HEADER_SIZE_MAX];
|
| 58 | |
| 59 | unsigned pos = 1;
|
| 60 | int numFilters, i;
|
| 61 | header[pos++] = p->flags;
|
| 62 | |
| 63 | if (XzBlock_HasPackSize(p)) pos += Xz_WriteVarInt(header + pos, p->packSize);
|
| 64 | if (XzBlock_HasUnpackSize(p)) pos += Xz_WriteVarInt(header + pos, p->unpackSize);
|
| 65 | numFilters = XzBlock_GetNumFilters(p);
|
| 66 | for (i = 0; i < numFilters; i++)
|
| 67 | {
|
| 68 | const CXzFilter *f = &p->filters[i];
|
| 69 | pos += Xz_WriteVarInt(header + pos, f->id);
|
| 70 | pos += Xz_WriteVarInt(header + pos, f->propsSize);
|
| 71 | memcpy(header + pos, f->props, f->propsSize);
|
| 72 | pos += f->propsSize;
|
| 73 | }
|
| 74 | while((pos & 3) != 0)
|
| 75 | header[pos++] = 0;
|
| 76 | header[0] = (Byte)(pos >> 2);
|
| 77 | SetUi32(header + pos, CrcCalc(header, pos));
|
| 78 | return WriteBytes(s, header, pos + 4);
|
| 79 | }
|
| 80 | |
| 81 | SRes Xz_WriteFooter(CXzStream *p, ISeqOutStream *s)
|
| 82 | {
|
| 83 | Byte buf[32];
|
| 84 | UInt64 globalPos;
|
| 85 | {
|
| 86 | UInt32 crc = CRC_INIT_VAL;
|
| 87 | unsigned pos = 1 + Xz_WriteVarInt(buf + 1, p->numBlocks);
|
| 88 | size_t i;
|
| 89 | |
| 90 | globalPos = pos;
|
| 91 | buf[0] = 0;
|
| 92 | RINOK(WriteBytesAndCrc(s, buf, pos, &crc));
|
| 93 | for (i = 0; i < p->numBlocks; i++)
|
| 94 | {
|
| 95 | const CXzBlockSizes *block = &p->blocks[i];
|
| 96 | pos = Xz_WriteVarInt(buf, block->totalSize);
|
| 97 | pos += Xz_WriteVarInt(buf + pos, block->unpackSize);
|
| 98 | globalPos += pos;
|
| 99 | RINOK(WriteBytesAndCrc(s, buf, pos, &crc));
|
| 100 | }
|
| 101 | pos = ((unsigned)globalPos & 3);
|
| 102 | if (pos != 0)
|
| 103 | {
|
| 104 | buf[0] = buf[1] = buf[2] = 0;
|
| 105 | RINOK(WriteBytesAndCrc(s, buf, 4 - pos, &crc));
|
| 106 | globalPos += 4 - pos;
|
| 107 | }
|
| 108 | {
|
| 109 | SetUi32(buf, CRC_GET_DIGEST(crc));
|
| 110 | RINOK(WriteBytes(s, buf, 4));
|
| 111 | globalPos += 4;
|
| 112 | }
|
| 113 | }
|
| 114 | |
| 115 | {
|
| 116 | UInt32 indexSize = (UInt32)((globalPos >> 2) - 1);
|
| 117 | SetUi32(buf + 4, indexSize);
|
| 118 | buf[8] = (Byte)(p->flags >> 8);
|
| 119 | buf[9] = (Byte)(p->flags & 0xFF);
|
| 120 | SetUi32(buf, CrcCalc(buf + 4, 6));
|
| 121 | memcpy(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE);
|
| 122 | return WriteBytes(s, buf, 12);
|
| 123 | }
|
| 124 | }
|
| 125 | |
| 126 | SRes Xz_AddIndexRecord(CXzStream *p, UInt64 unpackSize, UInt64 totalSize, ISzAlloc *alloc)
|
| 127 | {
|
| 128 | if (p->blocks == 0 || p->numBlocksAllocated == p->numBlocks)
|
| 129 | {
|
| 130 | size_t num = (p->numBlocks + 1) * 2;
|
| 131 | size_t newSize = sizeof(CXzBlockSizes) * num;
|
| 132 | CXzBlockSizes *blocks;
|
| 133 | if (newSize / sizeof(CXzBlockSizes) != num)
|
| 134 | return SZ_ERROR_MEM;
|
| 135 | blocks = alloc->Alloc(alloc, newSize);
|
| 136 | if (blocks == 0)
|
| 137 | return SZ_ERROR_MEM;
|
| 138 | if (p->numBlocks != 0)
|
| 139 | {
|
| 140 | memcpy(blocks, p->blocks, p->numBlocks * sizeof(CXzBlockSizes));
|
| 141 | Xz_Free(p, alloc);
|
| 142 | }
|
| 143 | p->blocks = blocks;
|
| 144 | p->numBlocksAllocated = num;
|
| 145 | }
|
| 146 | {
|
| 147 | CXzBlockSizes *block = &p->blocks[p->numBlocks++];
|
| 148 | block->totalSize = totalSize;
|
| 149 | block->unpackSize = unpackSize;
|
| 150 | }
|
| 151 | return SZ_OK;
|
| 152 | }
|
| 153 | |
| 154 | /* ---------- CSeqCheckInStream ---------- */
|
| 155 | |
| 156 | typedef struct
|
| 157 | {
|
| 158 | ISeqInStream p;
|
| 159 | ISeqInStream *realStream;
|
| 160 | UInt64 processed;
|
| 161 | CXzCheck check;
|
| 162 | } CSeqCheckInStream;
|
| 163 | |
| 164 | void SeqCheckInStream_Init(CSeqCheckInStream *p, int mode)
|
| 165 | {
|
| 166 | p->processed = 0;
|
| 167 | XzCheck_Init(&p->check, mode);
|
| 168 | }
|
| 169 | |
| 170 | void SeqCheckInStream_GetDigest(CSeqCheckInStream *p, Byte *digest)
|
| 171 | {
|
| 172 | XzCheck_Final(&p->check, digest);
|
| 173 | }
|
| 174 | |
| 175 | static SRes SeqCheckInStream_Read(void *pp, void *data, size_t *size)
|
| 176 | {
|
| 177 | CSeqCheckInStream *p = (CSeqCheckInStream *)pp;
|
| 178 | SRes res = p->realStream->Read(p->realStream, data, size);
|
| 179 | XzCheck_Update(&p->check, data, *size);
|
| 180 | p->processed += *size;
|
| 181 | return res;
|
| 182 | }
|
| 183 | |
| 184 | /* ---------- CSeqSizeOutStream ---------- */
|
| 185 | |
| 186 | typedef struct
|
| 187 | {
|
| 188 | ISeqOutStream p;
|
| 189 | ISeqOutStream *realStream;
|
| 190 | UInt64 processed;
|
| 191 | } CSeqSizeOutStream;
|
| 192 | |
| 193 | static size_t MyWrite(void *pp, const void *data, size_t size)
|
| 194 | {
|
| 195 | CSeqSizeOutStream *p = (CSeqSizeOutStream *)pp;
|
| 196 | size = p->realStream->Write(p->realStream, data, size);
|
| 197 | p->processed += size;
|
| 198 | return size;
|
| 199 | }
|
| 200 | |
| 201 | /* ---------- CSeqInFilter ---------- */
|
| 202 | |
| 203 | #define FILTER_BUF_SIZE (1 << 20)
|
| 204 | |
| 205 | typedef struct
|
| 206 | {
|
| 207 | ISeqInStream p;
|
| 208 | ISeqInStream *realStream;
|
| 209 | IStateCoder StateCoder;
|
| 210 | Byte *buf;
|
| 211 | size_t curPos;
|
| 212 | size_t endPos;
|
| 213 | int srcWasFinished;
|
| 214 | } CSeqInFilter;
|
| 215 | |
| 216 | static SRes SeqInFilter_Read(void *pp, void *data, size_t *size)
|
| 217 | {
|
| 218 | CSeqInFilter *p = (CSeqInFilter *)pp;
|
| 219 | size_t sizeOriginal = *size;
|
| 220 | if (sizeOriginal == 0)
|
| 221 | return S_OK;
|
| 222 | *size = 0;
|
| 223 | for (;;)
|
| 224 | {
|
| 225 | if (!p->srcWasFinished && p->curPos == p->endPos)
|
| 226 | {
|
| 227 | p->curPos = 0;
|
| 228 | p->endPos = FILTER_BUF_SIZE;
|
| 229 | RINOK(p->realStream->Read(p->realStream, p->buf, &p->endPos));
|
| 230 | if (p->endPos == 0)
|
| 231 | p->srcWasFinished = 1;
|
| 232 | }
|
| 233 | {
|
| 234 | SizeT srcLen = p->endPos - p->curPos;
|
| 235 | int wasFinished;
|
| 236 | SRes res;
|
| 237 | *size = sizeOriginal;
|
| 238 | res = p->StateCoder.Code(p->StateCoder.p, data, size, p->buf + p->curPos, &srcLen,
|
| 239 | p->srcWasFinished, CODER_FINISH_ANY, &wasFinished);
|
| 240 | p->curPos += srcLen;
|
| 241 | if (*size != 0 || srcLen == 0 || res != 0)
|
| 242 | return res;
|
| 243 | }
|
| 244 | }
|
| 245 | }
|
| 246 | |
| 247 | static void SeqInFilter_Construct(CSeqInFilter *p)
|
| 248 | {
|
| 249 | p->buf = NULL;
|
| 250 | p->p.Read = SeqInFilter_Read;
|
| 251 | }
|
| 252 | |
| 253 | static void SeqInFilter_Free(CSeqInFilter *p)
|
| 254 | {
|
| 255 | if (p->buf)
|
| 256 | {
|
| 257 | g_Alloc.Free(&g_Alloc, p->buf);
|
| 258 | p->buf = NULL;
|
| 259 | }
|
| 260 | }
|
| 261 | |
| 262 | SRes BraState_SetFromMethod(IStateCoder *p, UInt64 id, int encodeMode, ISzAlloc *alloc);
|
| 263 | |
| 264 | static SRes SeqInFilter_Init(CSeqInFilter *p, const CXzFilter *props)
|
| 265 | {
|
| 266 | if (!p->buf)
|
| 267 | {
|
| 268 | p->buf = g_Alloc.Alloc(&g_Alloc, FILTER_BUF_SIZE);
|
| 269 | if (!p->buf)
|
| 270 | return SZ_ERROR_MEM;
|
| 271 | }
|
| 272 | p->curPos = p->endPos = 0;
|
| 273 | p->srcWasFinished = 0;
|
| 274 | RINOK(BraState_SetFromMethod(&p->StateCoder, props->id, 1, &g_Alloc));
|
| 275 | RINOK(p->StateCoder.SetProps(p->StateCoder.p, props->props, props->propsSize, &g_Alloc));
|
| 276 | p->StateCoder.Init(p->StateCoder.p);
|
| 277 | return S_OK;
|
| 278 | }
|
| 279 | |
| 280 | /* ---------- CSbEncInStream ---------- */
|
| 281 | |
| 282 | #ifdef USE_SUBBLOCK
|
| 283 | |
| 284 | typedef struct
|
| 285 | {
|
| 286 | ISeqInStream p;
|
| 287 | ISeqInStream *inStream;
|
| 288 | CSbEnc enc;
|
| 289 | } CSbEncInStream;
|
| 290 | |
| 291 | static SRes SbEncInStream_Read(void *pp, void *data, size_t *size)
|
| 292 | {
|
| 293 | CSbEncInStream *p = (CSbEncInStream *)pp;
|
| 294 | size_t sizeOriginal = *size;
|
| 295 | if (sizeOriginal == 0)
|
| 296 | return S_OK;
|
| 297 | for (;;)
|
| 298 | {
|
| 299 | if (p->enc.needRead && !p->enc.readWasFinished)
|
| 300 | {
|
| 301 | size_t processed = p->enc.needReadSizeMax;
|
| 302 | RINOK(p->inStream->Read(p->inStream, p->enc.buf + p->enc.readPos, &processed));
|
| 303 | p->enc.readPos += processed;
|
| 304 | if (processed == 0)
|
| 305 | {
|
| 306 | p->enc.readWasFinished = True;
|
| 307 | p->enc.isFinalFinished = True;
|
| 308 | }
|
| 309 | p->enc.needRead = False;
|
| 310 | }
|
| 311 | *size = sizeOriginal;
|
| 312 | RINOK(SbEnc_Read(&p->enc, data, size));
|
| 313 | if (*size != 0 || !p->enc.needRead)
|
| 314 | return S_OK;
|
| 315 | }
|
| 316 | }
|
| 317 | |
| 318 | void SbEncInStream_Construct(CSbEncInStream *p, ISzAlloc *alloc)
|
| 319 | {
|
| 320 | SbEnc_Construct(&p->enc, alloc);
|
| 321 | p->p.Read = SbEncInStream_Read;
|
| 322 | }
|
| 323 | |
| 324 | SRes SbEncInStream_Init(CSbEncInStream *p)
|
| 325 | {
|
| 326 | return SbEnc_Init(&p->enc);
|
| 327 | }
|
| 328 | |
| 329 | void SbEncInStream_Free(CSbEncInStream *p)
|
| 330 | {
|
| 331 | SbEnc_Free(&p->enc);
|
| 332 | }
|
| 333 | |
| 334 | #endif
|
| 335 | |
| 336 | |
| 337 | typedef struct
|
| 338 | {
|
| 339 | CLzma2EncHandle lzma2;
|
| 340 | #ifdef USE_SUBBLOCK
|
| 341 | CSbEncInStream sb;
|
| 342 | #endif
|
| 343 | CSeqInFilter filter;
|
| 344 | ISzAlloc *alloc;
|
| 345 | ISzAlloc *bigAlloc;
|
| 346 | } CLzma2WithFilters;
|
| 347 | |
| 348 | |
| 349 | static void Lzma2WithFilters_Construct(CLzma2WithFilters *p, ISzAlloc *alloc, ISzAlloc *bigAlloc)
|
| 350 | {
|
| 351 | p->alloc = alloc;
|
| 352 | p->bigAlloc = bigAlloc;
|
| 353 | p->lzma2 = NULL;
|
| 354 | #ifdef USE_SUBBLOCK
|
| 355 | SbEncInStream_Construct(&p->sb, alloc);
|
| 356 | #endif
|
| 357 | SeqInFilter_Construct(&p->filter);
|
| 358 | }
|
| 359 | |
| 360 | static SRes Lzma2WithFilters_Create(CLzma2WithFilters *p)
|
| 361 | {
|
| 362 | p->lzma2 = Lzma2Enc_Create(p->alloc, p->bigAlloc);
|
| 363 | if (p->lzma2 == 0)
|
| 364 | return SZ_ERROR_MEM;
|
| 365 | return SZ_OK;
|
| 366 | }
|
| 367 | |
| 368 | static void Lzma2WithFilters_Free(CLzma2WithFilters *p)
|
| 369 | {
|
| 370 | SeqInFilter_Free(&p->filter);
|
| 371 | #ifdef USE_SUBBLOCK
|
| 372 | SbEncInStream_Free(&p->sb);
|
| 373 | #endif
|
| 374 | if (p->lzma2)
|
| 375 | {
|
| 376 | Lzma2Enc_Destroy(p->lzma2);
|
| 377 | p->lzma2 = NULL;
|
| 378 | }
|
| 379 | }
|
| 380 | |
| 381 | void XzProps_Init(CXzProps *p)
|
| 382 | {
|
| 383 | p->lzma2Props = 0;
|
| 384 | p->filterProps = 0;
|
| 385 | p->checkId = XZ_CHECK_CRC32;
|
| 386 | }
|
| 387 | |
| 388 | void XzFilterProps_Init(CXzFilterProps *p)
|
| 389 | {
|
| 390 | p->id = 0;
|
| 391 | p->delta = 0;
|
| 392 | p->ip= 0;
|
| 393 | p->ipDefined = False;
|
| 394 | }
|
| 395 | |
| 396 | static SRes Xz_Compress(CXzStream *xz, CLzma2WithFilters *lzmaf,
|
| 397 | ISeqOutStream *outStream, ISeqInStream *inStream,
|
| 398 | const CXzProps *props, ICompressProgress *progress)
|
| 399 | {
|
| 400 | xz->flags = (Byte)props->checkId;
|
| 401 | |
| 402 | RINOK(Lzma2Enc_SetProps(lzmaf->lzma2, props->lzma2Props));
|
| 403 | RINOK(Xz_WriteHeader(xz->flags, outStream));
|
| 404 | |
| 405 | {
|
| 406 | CSeqCheckInStream checkInStream;
|
| 407 | CSeqSizeOutStream seqSizeOutStream;
|
| 408 | CXzBlock block;
|
| 409 | int filterIndex = 0;
|
| 410 | CXzFilter *filter = NULL;
|
| 411 | const CXzFilterProps *fp = props->filterProps;
|
| 412 | |
| 413 | XzBlock_ClearFlags(&block);
|
| 414 | XzBlock_SetNumFilters(&block, 1 + (fp ? 1 : 0));
|
| 415 | |
| 416 | if (fp)
|
| 417 | {
|
| 418 | filter = &block.filters[filterIndex++];
|
| 419 | filter->id = fp->id;
|
| 420 | filter->propsSize = 0;
|
| 421 | if (fp->id == XZ_ID_Delta)
|
| 422 | {
|
| 423 | filter->props[0] = (Byte)(fp->delta - 1);
|
| 424 | filter->propsSize = 1;
|
| 425 | }
|
| 426 | else if (fp->ipDefined)
|
| 427 | {
|
| 428 | SetUi32(filter->props, fp->ip);
|
| 429 | filter->propsSize = 4;
|
| 430 | }
|
| 431 | }
|
| 432 | |
| 433 | {
|
| 434 | CXzFilter *f = &block.filters[filterIndex++];
|
| 435 | f->id = XZ_ID_LZMA2;
|
| 436 | f->propsSize = 1;
|
| 437 | f->props[0] = Lzma2Enc_WriteProperties(lzmaf->lzma2);
|
| 438 | }
|
| 439 | |
| 440 | seqSizeOutStream.p.Write = MyWrite;
|
| 441 | seqSizeOutStream.realStream = outStream;
|
| 442 | seqSizeOutStream.processed = 0;
|
| 443 | |
| 444 | RINOK(XzBlock_WriteHeader(&block, &seqSizeOutStream.p));
|
| 445 | |
| 446 | checkInStream.p.Read = SeqCheckInStream_Read;
|
| 447 | checkInStream.realStream = inStream;
|
| 448 | SeqCheckInStream_Init(&checkInStream, XzFlags_GetCheckType(xz->flags));
|
| 449 | |
| 450 | if (fp)
|
| 451 | {
|
| 452 | #ifdef USE_SUBBLOCK
|
| 453 | if (fp->id == XZ_ID_Subblock)
|
| 454 | {
|
| 455 | lzmaf->sb.inStream = &checkInStream.p;
|
| 456 | RINOK(SbEncInStream_Init(&lzmaf->sb));
|
| 457 | }
|
| 458 | else
|
| 459 | #endif
|
| 460 | {
|
| 461 | lzmaf->filter.realStream = &checkInStream.p;
|
| 462 | RINOK(SeqInFilter_Init(&lzmaf->filter, filter));
|
| 463 | }
|
| 464 | }
|
| 465 | |
| 466 | {
|
| 467 | UInt64 packPos = seqSizeOutStream.processed;
|
| 468 | SRes res = Lzma2Enc_Encode(lzmaf->lzma2, &seqSizeOutStream.p,
|
| 469 | fp ?
|
| 470 | #ifdef USE_SUBBLOCK
|
| 471 | (fp->id == XZ_ID_Subblock) ? &lzmaf->sb.p:
|
| 472 | #endif
|
| 473 | &lzmaf->filter.p:
|
| 474 | &checkInStream.p,
|
| 475 | progress);
|
| 476 | RINOK(res);
|
| 477 | block.unpackSize = checkInStream.processed;
|
| 478 | block.packSize = seqSizeOutStream.processed - packPos;
|
| 479 | }
|
| 480 | |
| 481 | {
|
| 482 | unsigned padSize = 0;
|
| 483 | Byte buf[128];
|
| 484 | while((((unsigned)block.packSize + padSize) & 3) != 0)
|
| 485 | buf[padSize++] = 0;
|
| 486 | SeqCheckInStream_GetDigest(&checkInStream, buf + padSize);
|
| 487 | RINOK(WriteBytes(&seqSizeOutStream.p, buf, padSize + XzFlags_GetCheckSize(xz->flags)));
|
| 488 | RINOK(Xz_AddIndexRecord(xz, block.unpackSize, seqSizeOutStream.processed - padSize, &g_Alloc));
|
| 489 | }
|
| 490 | }
|
| 491 | return Xz_WriteFooter(xz, outStream);
|
| 492 | }
|
| 493 | |
| 494 | SRes Xz_Encode(ISeqOutStream *outStream, ISeqInStream *inStream,
|
| 495 | const CXzProps *props, ICompressProgress *progress)
|
| 496 | {
|
| 497 | SRes res;
|
| 498 | CXzStream xz;
|
| 499 | CLzma2WithFilters lzmaf;
|
| 500 | Xz_Construct(&xz);
|
| 501 | Lzma2WithFilters_Construct(&lzmaf, &g_Alloc, &g_BigAlloc);
|
| 502 | res = Lzma2WithFilters_Create(&lzmaf);
|
| 503 | if (res == SZ_OK)
|
| 504 | res = Xz_Compress(&xz, &lzmaf, outStream, inStream, props, progress);
|
| 505 | Lzma2WithFilters_Free(&lzmaf);
|
| 506 | Xz_Free(&xz, &g_Alloc);
|
| 507 | return res;
|
| 508 | }
|
| 509 | |
| 510 | SRes Xz_EncodeEmpty(ISeqOutStream *outStream)
|
| 511 | {
|
| 512 | SRes res;
|
| 513 | CXzStream xz;
|
| 514 | Xz_Construct(&xz);
|
| 515 | res = Xz_WriteHeader(xz.flags, outStream);
|
| 516 | if (res == SZ_OK)
|
| 517 | res = Xz_WriteFooter(&xz, outStream);
|
| 518 | Xz_Free(&xz, &g_Alloc);
|
| 519 | return res;
|
| 520 | }
|
src/lib/util/un7z.c
| r0 | r14440 | |
| 1 | /***************************************************************************
|
| 2 | |
| 3 | un7z.c
|
| 4 | |
| 5 | Functions to manipulate data within 7z files.
|
| 6 | |
| 7 | ****************************************************************************
|
| 8 | |
| 9 | Copyright Aaron Giles
|
| 10 | All rights reserved.
|
| 11 | |
| 12 | Redistribution and use in source and binary forms, with or without
|
| 13 | modification, are permitted provided that the following conditions are
|
| 14 | met:
|
| 15 | |
| 16 | * Redistributions of source code must retain the above copyright
|
| 17 | notice, this list of conditions and the following disclaimer.
|
| 18 | * Redistributions in binary form must reproduce the above copyright
|
| 19 | notice, this list of conditions and the following disclaimer in
|
| 20 | the documentation and/or other materials provided with the
|
| 21 | distribution.
|
| 22 | * Neither the name 'MAME' nor the names of its contributors may be
|
| 23 | used to endorse or promote products derived from this software
|
| 24 | without specific prior written permission.
|
| 25 | |
| 26 | THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
|
| 27 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
| 28 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 29 | DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
|
| 30 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
| 31 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 32 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
| 33 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
| 34 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
| 35 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
| 36 | POSSIBILITY OF SUCH DAMAGE.
|
| 37 | |
| 38 | ***************************************************************************/
|
| 39 | |
| 40 | // this is based on unzip.c, with modifications needed to use the 7zip library
|
| 41 | |
| 42 | #include "osdcore.h"
|
| 43 | #include "un7z.h"
|
| 44 | |
| 45 | #include <ctype.h>
|
| 46 | #include <stdlib.h>
|
| 47 | #include <zlib.h>
|
| 48 | |
| 49 | /***************************************************************************
|
| 50 | 7Zip Memory / File handling (adapted from 7zfile.c/.h and 7zalloc.c/.h)
|
| 51 | ***************************************************************************/
|
| 52 | |
| 53 | void *SZipAlloc(void *p, size_t size)
|
| 54 | {
|
| 55 | p = p;
|
| 56 | if (size == 0)
|
| 57 | return 0;
|
| 58 | |
| 59 | return malloc(size);
|
| 60 | }
|
| 61 | |
| 62 | void SZipFree(void *p, void *address)
|
| 63 | {
|
| 64 | p = p;
|
| 65 | free(address);
|
| 66 | }
|
| 67 | |
| 68 | |
| 69 | |
| 70 | void File_Construct(CSzFile *p)
|
| 71 | {
|
| 72 | p->_7z_osdfile = NULL;
|
| 73 | }
|
| 74 | |
| 75 | static WRes File_Open(CSzFile *p, const char *name, int writeMode)
|
| 76 | {
|
| 77 | /* we handle this ourselves ... */
|
| 78 | if (!p->_7z_osdfile) return 1;
|
| 79 | else return 0;
|
| 80 | }
|
| 81 | |
| 82 | WRes InFile_Open(CSzFile *p, const char *name) { return File_Open(p, name, 0); }
|
| 83 | WRes OutFile_Open(CSzFile *p, const char *name) { return File_Open(p, name, 1); }
|
| 84 | |
| 85 | |
| 86 | WRes File_Close(CSzFile *p)
|
| 87 | {
|
| 88 | /* we handle this ourselves ... */
|
| 89 | return 0;
|
| 90 | }
|
| 91 | |
| 92 | WRes File_Read(CSzFile *p, void *data, size_t *size)
|
| 93 | {
|
| 94 | // file_error err;
|
| 95 | UINT32 read_length;
|
| 96 | |
| 97 | if (!p->_7z_osdfile)
|
| 98 | {
|
| 99 | printf("un7z.c: called File_Read without file\n");
|
| 100 | return 1;
|
| 101 | }
|
| 102 | |
| 103 | size_t originalSize = *size;
|
| 104 | if (originalSize == 0)
|
| 105 | return 0;
|
| 106 | |
| 107 | // err = |
| 108 | osd_read( p->_7z_osdfile, data, p->_7z_currfpos, originalSize, &read_length );
|
| 109 | *size = read_length;
|
| 110 | p->_7z_currfpos += read_length;
|
| 111 | |
| 112 | if (*size == originalSize)
|
| 113 | return 0;
|
| 114 | |
| 115 | return 0;
|
| 116 | }
|
| 117 | |
| 118 | WRes File_Write(CSzFile *p, const void *data, size_t *size)
|
| 119 | {
|
| 120 | return 0;
|
| 121 | }
|
| 122 | |
| 123 | WRes File_Seek(CSzFile *p, Int64 *pos, ESzSeek origin)
|
| 124 | {
|
| 125 | if (origin==0) p->_7z_currfpos = *pos;
|
| 126 | if (origin==1) p->_7z_currfpos = p->_7z_currfpos + *pos;
|
| 127 | if (origin==2) p->_7z_currfpos = p->_7z_length - *pos;
|
| 128 | |
| 129 | *pos = p->_7z_currfpos;
|
| 130 |
|
| 131 | return 0;
|
| 132 | }
|
| 133 | |
| 134 | WRes File_GetLength(CSzFile *p, UInt64 *length)
|
| 135 | {
|
| 136 | *length = p->_7z_length;
|
| 137 | return 0;
|
| 138 | }
|
| 139 | |
| 140 | /* ---------- FileSeqInStream ---------- */
|
| 141 | |
| 142 | static SRes FileSeqInStream_Read(void *pp, void *buf, size_t *size)
|
| 143 | {
|
| 144 | CFileSeqInStream *p = (CFileSeqInStream *)pp;
|
| 145 | return File_Read(&p->file, buf, size) == 0 ? SZ_OK : SZ_ERROR_READ;
|
| 146 | }
|
| 147 | |
| 148 | void FileSeqInStream_CreateVTable(CFileSeqInStream *p)
|
| 149 | {
|
| 150 | p->s.Read = FileSeqInStream_Read;
|
| 151 | }
|
| 152 | |
| 153 | |
| 154 | /* ---------- FileInStream ---------- */
|
| 155 | |
| 156 | static SRes FileInStream_Read(void *pp, void *buf, size_t *size)
|
| 157 | {
|
| 158 | CFileInStream *p = (CFileInStream *)pp;
|
| 159 | return (File_Read(&p->file, buf, size) == 0) ? SZ_OK : SZ_ERROR_READ;
|
| 160 | }
|
| 161 | |
| 162 | static SRes FileInStream_Seek(void *pp, Int64 *pos, ESzSeek origin)
|
| 163 | {
|
| 164 | CFileInStream *p = (CFileInStream *)pp;
|
| 165 | return File_Seek(&p->file, pos, origin);
|
| 166 | }
|
| 167 | |
| 168 | void FileInStream_CreateVTable(CFileInStream *p)
|
| 169 | {
|
| 170 | p->s.Read = FileInStream_Read;
|
| 171 | p->s.Seek = FileInStream_Seek;
|
| 172 | }
|
| 173 | |
| 174 | /* ---------- FileOutStream ---------- */
|
| 175 | |
| 176 | static size_t FileOutStream_Write(void *pp, const void *data, size_t size)
|
| 177 | {
|
| 178 | CFileOutStream *p = (CFileOutStream *)pp;
|
| 179 | File_Write(&p->file, data, &size);
|
| 180 | return size;
|
| 181 | }
|
| 182 | |
| 183 | void FileOutStream_CreateVTable(CFileOutStream *p)
|
| 184 | {
|
| 185 | p->s.Write = FileOutStream_Write;
|
| 186 | }
|
| 187 | |
| 188 | /***************************************************************************
|
| 189 | CONSTANTS
|
| 190 | ***************************************************************************/
|
| 191 | |
| 192 | /* number of open files to cache */
|
| 193 | #define _7Z_CACHE_SIZE 8
|
| 194 | |
| 195 | |
| 196 | /***************************************************************************
|
| 197 | GLOBAL VARIABLES
|
| 198 | ***************************************************************************/
|
| 199 | |
| 200 | static _7z_file *_7z_cache[_7Z_CACHE_SIZE];
|
| 201 | |
| 202 | /***************************************************************************
|
| 203 | FUNCTION PROTOTYPES
|
| 204 | ***************************************************************************/
|
| 205 | |
| 206 | /* cache management */
|
| 207 | static void free__7z_file(_7z_file *_7z);
|
| 208 | |
| 209 | |
| 210 | /***************************************************************************
|
| 211 | _7Z FILE ACCESS
|
| 212 | ***************************************************************************/
|
| 213 | |
| 214 | /*-------------------------------------------------
|
| 215 | _7z_file_open - opens a _7Z file for reading
|
| 216 | -------------------------------------------------*/
|
| 217 | |
| 218 | int _7z_search_crc_match(_7z_file *new_7z, UINT32 search_crc, const char* search_filename, int search_filename_length, bool matchcrc, bool matchname)
|
| 219 | {
|
| 220 | UInt16 *temp = NULL;
|
| 221 | size_t tempSize = 0;
|
| 222 | |
| 223 | for (int i = 0; i < new_7z->db.db.NumFiles; i++)
|
| 224 | {
|
| 225 | const CSzFileItem *f = new_7z->db.db.Files + i;
|
| 226 | size_t len;
|
| 227 | |
| 228 | len = SzArEx_GetFileNameUtf16(&new_7z->db, i, NULL);
|
| 229 | |
| 230 | // if it's a directory entry we don't care about it..
|
| 231 | if (f->IsDir)
|
| 232 | continue;
|
| 233 | |
| 234 | if (len > tempSize)
|
| 235 | {
|
| 236 | SZipFree(NULL, temp);
|
| 237 | tempSize = len;
|
| 238 | temp = (UInt16 *)SZipAlloc(NULL, tempSize * sizeof(temp[0]));
|
| 239 | if (temp == 0)
|
| 240 | {
|
| 241 | return -1; // memory error
|
| 242 | }
|
| 243 | }
|
| 244 | |
| 245 | bool crcmatch = false;
|
| 246 | bool namematch = false;
|
| 247 | |
| 248 | UINT64 size = f->Size;
|
| 249 | UINT32 crc = f->Crc;
|
| 250 | |
| 251 | /* Check for a name match */
|
| 252 | SzArEx_GetFileNameUtf16(&new_7z->db, i, temp);
|
| 253 | |
| 254 | if (len == search_filename_length+1)
|
| 255 | {
|
| 256 | int j;
|
| 257 | for (j=0;j<search_filename_length;j++)
|
| 258 | {
|
| 259 | UINT8 sn = search_filename[j];
|
| 260 | UINT16 zn = temp[j]; // these are utf16
|
| 261 | |
| 262 | // MAME filenames are always lowercase so be case insensitive
|
| 263 | if ((zn>=0x41) && (zn<=0x5a)) zn+=0x20;
|
| 264 | |
| 265 | if (sn != zn) break;
|
| 266 | } |
| 267 | if (j==search_filename_length) namematch = true;
|
| 268 | }
|
| 269 | |
| 270 | |
| 271 | /* Check for a CRC match */
|
| 272 | if (crc==search_crc) crcmatch = true;
|
| 273 | |
| 274 | bool found = false;
|
| 275 | |
| 276 | if (matchcrc && matchname)
|
| 277 | {
|
| 278 | if (crcmatch && namematch)
|
| 279 | found = true;
|
| 280 | }
|
| 281 | else if (matchcrc)
|
| 282 | {
|
| 283 | if (crcmatch)
|
| 284 | found = true;
|
| 285 | }
|
| 286 | else if (matchname)
|
| 287 | {
|
| 288 | if (namematch)
|
| 289 | found = true;
|
| 290 | }
|
| 291 | |
| 292 | if (found) |
| 293 | {
|
| 294 | // printf("found %S %d %08x %08x %08x %s %d\n", temp, len, crc, search_crc, size, search_filename, search_filename_length);
|
| 295 | new_7z->curr_file_idx = i;
|
| 296 | new_7z->uncompressed_length = size;
|
| 297 | new_7z->crc = crc;
|
| 298 | |
| 299 | SZipFree(NULL, temp);
|
| 300 | return i;
|
| 301 | }
|
| 302 | }
|
| 303 | |
| 304 | SZipFree(NULL, temp);
|
| 305 | return -1;
|
| 306 | }
|
| 307 | |
| 308 | |
| 309 | _7z_error _7z_file_open(const char *filename, _7z_file **_7z)
|
| 310 | {
|
| 311 | file_error err;
|
| 312 | _7z_error _7zerr = _7ZERR_NONE;
|
| 313 | |
| 314 | |
| 315 | _7z_file *new_7z;
|
| 316 | char *string;
|
| 317 | int cachenum;
|
| 318 | |
| 319 | SRes res;
|
| 320 | |
| 321 | /* ensure we start with a NULL result */
|
| 322 | *_7z = NULL;
|
| 323 | |
| 324 | /* see if we are in the cache, and reopen if so */
|
| 325 | for (cachenum = 0; cachenum < ARRAY_LENGTH(_7z_cache); cachenum++)
|
| 326 | {
|
| 327 | _7z_file *cached = _7z_cache[cachenum];
|
| 328 | |
| 329 | /* if we have a valid entry and it matches our filename, use it and remove from the cache */
|
| 330 | if (cached != NULL && cached->filename != NULL && strcmp(filename, cached->filename) == 0)
|
| 331 | {
|
| 332 | *_7z = cached;
|
| 333 | _7z_cache[cachenum] = NULL;
|
| 334 | return _7ZERR_NONE;
|
| 335 | }
|
| 336 | }
|
| 337 | |
| 338 | /* allocate memory for the _7z_file structure */
|
| 339 | new_7z = (_7z_file *)malloc(sizeof(*new_7z));
|
| 340 | if (new_7z == NULL)
|
| 341 | return _7ZERR_OUT_OF_MEMORY;
|
| 342 | memset(new_7z, 0, sizeof(*new_7z));
|
| 343 | |
| 344 | new_7z->inited = false;
|
| 345 | new_7z->archiveStream.file._7z_currfpos = 0;
|
| 346 | err = osd_open(filename, OPEN_FLAG_READ, &new_7z->archiveStream.file._7z_osdfile, &new_7z->archiveStream.file._7z_length);
|
| 347 | if (err != FILERR_NONE)
|
| 348 | {
|
| 349 | _7zerr = _7ZERR_FILE_ERROR;
|
| 350 | goto error;
|
| 351 | }
|
| 352 | |
| 353 | new_7z->allocImp.Alloc = SZipAlloc;
|
| 354 | new_7z->allocImp.Free = SZipFree;
|
| 355 | |
| 356 | new_7z->allocTempImp.Alloc = SZipAlloc;
|
| 357 | new_7z->allocTempImp.Free = SZipFree;
|
| 358 | |
| 359 | if (InFile_Open(&new_7z->archiveStream.file, filename))
|
| 360 | {
|
| 361 | _7zerr = _7ZERR_FILE_ERROR;
|
| 362 | goto error;
|
| 363 | }
|
| 364 | |
| 365 | FileInStream_CreateVTable(&new_7z->archiveStream);
|
| 366 | LookToRead_CreateVTable(&new_7z->lookStream, False);
|
| 367 | |
| 368 | new_7z->lookStream.realStream = &new_7z->archiveStream.s;
|
| 369 | LookToRead_Init(&new_7z->lookStream);
|
| 370 | |
| 371 | CrcGenerateTable();
|
| 372 | |
| 373 | SzArEx_Init(&new_7z->db);
|
| 374 | new_7z->inited = true;
|
| 375 | |
| 376 | res = SzArEx_Open(&new_7z->db, &new_7z->lookStream.s, &new_7z->allocImp, &new_7z->allocTempImp);
|
| 377 | if (res != SZ_OK)
|
| 378 | {
|
| 379 | _7zerr = _7ZERR_FILE_ERROR;
|
| 380 | goto error;
|
| 381 | }
|
| 382 | |
| 383 | new_7z->blockIndex = 0xFFFFFFFF; /* it can have any value before first call (if outBuffer = 0) */
|
| 384 | new_7z->outBuffer = 0; /* it must be 0 before first call for each new archive. */
|
| 385 | new_7z->outBufferSize = 0; /* it can have any value before first call (if outBuffer = 0) */
|
| 386 | |
| 387 | /* make a copy of the filename for caching purposes */
|
| 388 | string = (char *)malloc(strlen(filename) + 1);
|
| 389 | if (string == NULL)
|
| 390 | {
|
| 391 | _7zerr = _7ZERR_OUT_OF_MEMORY;
|
| 392 | goto error;
|
| 393 | }
|
| 394 | strcpy(string, filename);
|
| 395 | new_7z->filename = string;
|
| 396 | *_7z = new_7z;
|
| 397 | return _7ZERR_NONE;
|
| 398 | |
| 399 | error:
|
| 400 | free__7z_file(new_7z);
|
| 401 | return _7zerr;
|
| 402 | }
|
| 403 | |
| 404 | |
| 405 | /*-------------------------------------------------
|
| 406 | _7z_file_close - close a _7Z file and add it
|
| 407 | to the cache
|
| 408 | -------------------------------------------------*/
|
| 409 | |
| 410 | void _7z_file_close(_7z_file *_7z)
|
| 411 | {
|
| 412 | int cachenum;
|
| 413 | |
| 414 | /* close the open files */
|
| 415 | if (_7z->archiveStream.file._7z_osdfile != NULL)
|
| 416 | osd_close(_7z->archiveStream.file._7z_osdfile);
|
| 417 | _7z->archiveStream.file._7z_osdfile = NULL;
|
| 418 | |
| 419 | /* find the first NULL entry in the cache */
|
| 420 | for (cachenum = 0; cachenum < ARRAY_LENGTH(_7z_cache); cachenum++)
|
| 421 | if (_7z_cache[cachenum] == NULL)
|
| 422 | break;
|
| 423 | |
| 424 | /* if no room left in the cache, free the bottommost entry */
|
| 425 | if (cachenum == ARRAY_LENGTH(_7z_cache))
|
| 426 | free__7z_file(_7z_cache[--cachenum]);
|
| 427 | |
| 428 | /* move everyone else down and place us at the top */
|
| 429 | if (cachenum != 0)
|
| 430 | memmove(&_7z_cache[1], &_7z_cache[0], cachenum * sizeof(_7z_cache[0]));
|
| 431 | _7z_cache[0] = _7z;
|
| 432 | }
|
| 433 | |
| 434 | |
| 435 | /*-------------------------------------------------
|
| 436 | _7z_file_cache_clear - clear the _7Z file
|
| 437 | cache and free all memory
|
| 438 | -------------------------------------------------*/
|
| 439 | |
| 440 | void _7z_file_cache_clear(void)
|
| 441 | {
|
| 442 | int cachenum;
|
| 443 | |
| 444 | /* clear call cache entries */
|
| 445 | for (cachenum = 0; cachenum < ARRAY_LENGTH(_7z_cache); cachenum++)
|
| 446 | if (_7z_cache[cachenum] != NULL)
|
| 447 | {
|
| 448 | free__7z_file(_7z_cache[cachenum]);
|
| 449 | _7z_cache[cachenum] = NULL;
|
| 450 | }
|
| 451 | }
|
| 452 | |
| 453 | |
| 454 | /*-------------------------------------------------
|
| 455 | _7z_file_decompress - decompress a file
|
| 456 | from a _7Z into the target buffer
|
| 457 | -------------------------------------------------*/
|
| 458 | |
| 459 | _7z_error _7z_file_decompress(_7z_file *new_7z, void *buffer, UINT32 length)
|
| 460 | {
|
| 461 | file_error err;
|
| 462 | SRes res;
|
| 463 | int index = new_7z->curr_file_idx;
|
| 464 | |
| 465 | /* make sure the file is open.. */
|
| 466 | if (new_7z->archiveStream.file._7z_osdfile==NULL)
|
| 467 | {
|
| 468 | new_7z->archiveStream.file._7z_currfpos = 0;
|
| 469 | err = osd_open(new_7z->filename, OPEN_FLAG_READ, &new_7z->archiveStream.file._7z_osdfile, &new_7z->archiveStream.file._7z_length);
|
| 470 | if (err != FILERR_NONE)
|
| 471 | return _7ZERR_FILE_ERROR;
|
| 472 | }
|
| 473 | |
| 474 | size_t offset = 0;
|
| 475 | size_t outSizeProcessed = 0;
|
| 476 | |
| 477 | res = SzArEx_Extract(&new_7z->db, &new_7z->lookStream.s, index,
|
| 478 | &new_7z->blockIndex, &new_7z->outBuffer, &new_7z->outBufferSize,
|
| 479 | &offset, &outSizeProcessed,
|
| 480 | &new_7z->allocImp, &new_7z->allocTempImp);
|
| 481 |
|
| 482 | if (res != SZ_OK)
|
| 483 | return _7ZERR_FILE_ERROR;
|
| 484 | |
| 485 | memcpy(buffer, new_7z->outBuffer + offset, length);
|
| 486 | |
| 487 | return _7ZERR_NONE;
|
| 488 | }
|
| 489 | |
| 490 | |
| 491 | |
| 492 | /***************************************************************************
|
| 493 | CACHE MANAGEMENT
|
| 494 | ***************************************************************************/
|
| 495 | |
| 496 | /*-------------------------------------------------
|
| 497 | free__7z_file - free all the data for a
|
| 498 | _7z_file
|
| 499 | -------------------------------------------------*/
|
| 500 | |
| 501 | static void free__7z_file(_7z_file *_7z)
|
| 502 | {
|
| 503 | if (_7z != NULL)
|
| 504 | {
|
| 505 | if (_7z->archiveStream.file._7z_osdfile != NULL)
|
| 506 | osd_close(_7z->archiveStream.file._7z_osdfile);
|
| 507 | if (_7z->filename != NULL)
|
| 508 | free((void *)_7z->filename);
|
| 509 | |
| 510 | |
| 511 | if (_7z->outBuffer) IAlloc_Free(&_7z->allocImp, _7z->outBuffer);
|
| 512 | if (_7z->inited) SzArEx_Free(&_7z->db, &_7z->allocImp);
|
| 513 |
|
| 514 | |
| 515 | free(_7z);
|
| 516 | }
|
| 517 | }
|
| 518 | |
| 519 | |