src/mess/formats/td0_dsk.c
| r5827 | r5828 | |
| 1 | 1 | /********************************************************************* |
| 2 | 2 | |
| 3 | | formats/td0_dsk.c |
| 3 | formats/td0_dsk.c |
| 4 | 4 | |
| 5 | | TD0 disk images |
| 5 | TD0 disk images |
| 6 | 6 | |
| 7 | 7 | *********************************************************************/ |
| 8 | /* |
| 9 | * Based on Japanese version 29-NOV-1988 |
| 10 | * LZSS coded by Haruhiko OKUMURA |
| 11 | * Adaptive Huffman Coding coded by Haruyasu YOSHIZAKI |
| 12 | * Edited and translated to English by Kenji RIKITAKE |
| 13 | */ |
| 8 | 14 | |
| 9 | 15 | #include <string.h> |
| 10 | 16 | #include "driver.h" |
| r5827 | r5828 | |
| 19 | 25 | int tracks; |
| 20 | 26 | int sector_size; |
| 21 | 27 | UINT64 track_offsets[84*2]; /* offset within data for each track */ |
| 28 | UINT8 *data; |
| 22 | 29 | }; |
| 23 | 30 | |
| 24 | 31 | |
| r5827 | r5828 | |
| 38 | 45 | floppy_image_read(floppy, header, 0, 2); |
| 39 | 46 | if (header[0]=='T' && header[1]=='D') { |
| 40 | 47 | *vote = 100; |
| 48 | } else if (header[0]=='t' && header[1]=='d') { |
| 49 | *vote = 100; |
| 41 | 50 | } else { |
| 42 | 51 | *vote = 0; |
| 43 | 52 | } |
| r5827 | r5828 | |
| 62 | 71 | static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset) |
| 63 | 72 | { |
| 64 | 73 | UINT64 offs; |
| 65 | | UINT8 header[8]; |
| 66 | | UINT8 sectors_per_track; |
| 74 | UINT8 *header; |
| 75 | UINT8 sectors_per_track; |
| 67 | 76 | int i; |
| 68 | 77 | |
| 69 | 78 | if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks) |
| r5827 | r5828 | |
| 74 | 83 | offs = td0_get_track_offset(floppy, head, track); |
| 75 | 84 | |
| 76 | 85 | // read track header |
| 77 | | floppy_image_read(floppy, header, offs - 4, 4); |
| 78 | | |
| 86 | header = get_tag(floppy)->data + offs - 4; |
| 87 | |
| 79 | 88 | // take number of sectors per track |
| 80 | 89 | sectors_per_track = header[0]; |
| 81 | | |
| 90 | |
| 82 | 91 | if (!sector_is_index) { |
| 83 | 92 | // when taking ID's return seek error if number is over counter |
| 84 | 93 | if (sector > sectors_per_track) { |
| 85 | 94 | return FLOPPY_ERROR_SEEKERROR; |
| 86 | 95 | } |
| 87 | 96 | } |
| 88 | | |
| 97 | |
| 89 | 98 | // move trought sectors |
| 90 | 99 | for(i=0;i < sector-1;i++) { |
| 91 | | floppy_image_read(floppy, header, offs, 6); |
| 100 | header = get_tag(floppy)->data + offs; |
| 92 | 101 | offs+= 6; |
| 93 | 102 | if ((header[4] & 0x30)==0) { |
| 94 | | floppy_image_read(floppy, header+6, offs, 2); |
| 95 | | offs+= 2; |
| 96 | | offs+= header[6] + (header[7]<<8); |
| 103 | offs+= 2; |
| 104 | offs+= header[6] + (header[7]<<8); |
| 97 | 105 | } |
| 98 | | } |
| 106 | } |
| 99 | 107 | // read size of sector |
| 100 | | floppy_image_read(floppy, header, offs, 6); |
| 108 | header = get_tag(floppy)->data + offs; |
| 101 | 109 | get_tag(floppy)->sector_size = 1 << (header[3] + 7); |
| 102 | 110 | |
| 103 | 111 | if (offset) |
| r5827 | r5828 | |
| 111 | 119 | { |
| 112 | 120 | UINT64 offset; |
| 113 | 121 | floperr_t err; |
| 114 | | UINT8 header[9]; |
| 122 | UINT8 *header; |
| 115 | 123 | int size,realsize,i; |
| 116 | 124 | int buff_pos; |
| 117 | 125 | int data_pos; |
| 118 | 126 | UINT8 *data; |
| 119 | 127 | UINT8 *buf; |
| 120 | | |
| 121 | | buf = (UINT8*)buffer; |
| 128 | |
| 129 | buf = (UINT8*)buffer; |
| 122 | 130 | // take sector offset |
| 123 | 131 | err = get_offset(floppy, head, track, sector, sector_is_index, &offset); |
| 124 | 132 | if (err) |
| 125 | 133 | return err; |
| 126 | | |
| 134 | |
| 127 | 135 | // read sector header |
| 128 | | floppy_image_read(floppy, header, offset, 6); |
| 136 | header = get_tag(floppy)->data + offset; |
| 129 | 137 | offset+=6; |
| 130 | 138 | // if there is no date just jump out |
| 131 | 139 | if ((header[4] & 0x30)!=0) return FLOPPY_ERROR_SUCCESS; |
| 132 | | |
| 133 | | floppy_image_read(floppy, header+6, offset, 3); |
| 140 | |
| 134 | 141 | offset+=3; |
| 135 | 142 | // take data size |
| 136 | 143 | size = header[6] + (header[7]<<8)-1; |
| 137 | 144 | // take real sector size |
| 138 | 145 | realsize = 1 << (header[3] + 7); |
| 139 | | |
| 140 | | data = malloc(size); |
| 146 | |
| 141 | 147 | // read sector data |
| 142 | | floppy_image_read(floppy, data, offset, size); |
| 143 | | |
| 148 | data = get_tag(floppy)->data + offset; |
| 144 | 149 | buff_pos = 0; |
| 145 | 150 | data_pos = 0; |
| 146 | | |
| 151 | |
| 147 | 152 | switch(header[8]) { |
| 148 | | case 0: |
| 153 | case 0: |
| 149 | 154 | // encoding type 0 |
| 150 | 155 | // - plain data |
| 151 | 156 | memcpy(buffer,data,size); |
| 152 | | break; |
| 153 | | case 1: |
| 157 | break; |
| 158 | case 1: |
| 154 | 159 | // encoding type 1 |
| 155 | | // - 2 bytes size |
| 160 | // - 2 bytes size |
| 156 | 161 | // - 2 bytes of data |
| 157 | 162 | // data is reapeted specified number of times |
| 158 | 163 | while(buff_pos<realsize) { |
| r5827 | r5828 | |
| 163 | 168 | data_pos+=4; |
| 164 | 169 | } |
| 165 | 170 | break; |
| 166 | | case 2: |
| 171 | case 2: |
| 167 | 172 | // encoding type 2 |
| 168 | | // - if first byte is zero next byte represent size of |
| 169 | | // plain data after it |
| 170 | | // - if different then zero when multiply by 2 represent |
| 171 | | // size of data that should be reapeted next byte times |
| 173 | // - if first byte is zero next byte represent size of |
| 174 | // plain data after it |
| 175 | // - if different then zero when multiply by 2 represent |
| 176 | // size of data that should be reapeted next byte times |
| 172 | 177 | while(buff_pos<realsize) { |
| 173 | 178 | if (data[data_pos]==0x00) { |
| 174 | | int size = data[data_pos+1]; |
| 179 | int size = data[data_pos+1]; |
| 175 | 180 | memcpy(buf+buff_pos,data + data_pos + 2,size); |
| 176 | 181 | data_pos += 2 + size; |
| 177 | 182 | buff_pos += size; |
| r5827 | r5828 | |
| 179 | 184 | int size = 2*data[data_pos]; |
| 180 | 185 | int repeat = data[data_pos+1]; |
| 181 | 186 | data_pos+=2; |
| 182 | | |
| 183 | | for (i=0;i<repeat;i++) { |
| 187 | |
| 188 | for (i=0;i<repeat;i++) { |
| 184 | 189 | memcpy(buf + buff_pos,data + data_pos,size); |
| 185 | 190 | buff_pos += size; |
| 186 | 191 | } |
| r5827 | r5828 | |
| 188 | 193 | } |
| 189 | 194 | } |
| 190 | 195 | break; |
| 191 | | default: |
| 192 | | free(data); |
| 196 | default: |
| 193 | 197 | return FLOPPY_ERROR_INTERNAL; |
| 194 | 198 | break; |
| 195 | 199 | } |
| 196 | | free(data); |
| 197 | 200 | return FLOPPY_ERROR_SUCCESS; |
| 198 | 201 | } |
| 199 | 202 | |
| r5827 | r5828 | |
| 215 | 218 | if (err) |
| 216 | 219 | return err; |
| 217 | 220 | |
| 218 | | if (sector_length) { |
| 221 | if (sector_length) { |
| 219 | 222 | *sector_length = get_tag(floppy)->sector_size; |
| 220 | 223 | } |
| 221 | 224 | return FLOPPY_ERROR_SUCCESS; |
| r5827 | r5828 | |
| 225 | 228 | { |
| 226 | 229 | floperr_t retVal; |
| 227 | 230 | UINT64 offset; |
| 228 | | UINT8 sector_info[9]; |
| 231 | UINT8 *sector_info; |
| 229 | 232 | |
| 230 | 233 | retVal = get_offset(floppy, head, track, sector_index, FALSE, &offset); |
| 231 | 234 | |
| 232 | | floppy_image_read(floppy, sector_info, offset, 9); |
| 235 | sector_info = get_tag(floppy)->data + offset; |
| 233 | 236 | if (cylinder) |
| 234 | 237 | *cylinder = sector_info[0]; |
| 235 | 238 | if (side) |
| r5827 | r5828 | |
| 244 | 247 | if (sector_info[4] & 0x02) *flags |= ID_FLAG_CRC_ERROR_IN_DATA_FIELD; |
| 245 | 248 | if (sector_info[4] & 0x04) *flags |= ID_FLAG_DELETED_DATA; |
| 246 | 249 | } |
| 247 | | |
| 250 | |
| 248 | 251 | return retVal; |
| 249 | 252 | } |
| 250 | 253 | |
| 254 | static floppy_image *floppy_file; |
| 255 | static UINT64 floppy_file_offset; |
| 251 | 256 | |
| 257 | int data_read(UINT8 *buf,UINT16 size) |
| 258 | { |
| 259 | if (floppy_file_offset + size > floppy_image_size(floppy_file) ) { |
| 260 | size = floppy_image_size(floppy_file)- floppy_file_offset; |
| 261 | } |
| 262 | floppy_image_read(floppy_file,buf,floppy_file_offset,size); |
| 263 | floppy_file_offset += size; |
| 264 | return size; |
| 265 | } |
| 266 | |
| 267 | #define BUFSZ 512 // new input buffer |
| 268 | |
| 269 | static struct tdlzhuf { |
| 270 | UINT16 r, |
| 271 | bufcnt,bufndx,bufpos, // string buffer |
| 272 | // the following to allow block reads from input in next_word() |
| 273 | ibufcnt,ibufndx; // input buffer counters |
| 274 | UINT8 inbuf[BUFSZ]; // input buffer |
| 275 | } tdctl; |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | /* LZSS Parameters */ |
| 281 | |
| 282 | #define N 4096 /* Size of string buffer */ |
| 283 | #define F 60 /* Size of look-ahead buffer */ |
| 284 | #define THRESHOLD 2 |
| 285 | #define NIL N /* End of tree's node */ |
| 286 | |
| 287 | static UINT8 text_buf[N + F - 1]; |
| 288 | |
| 289 | /* Huffman coding parameters */ |
| 290 | |
| 291 | #define N_CHAR (256 - THRESHOLD + F) |
| 292 | /* character code (= 0..N_CHAR-1) */ |
| 293 | #define T (N_CHAR * 2 - 1) /* Size of table */ |
| 294 | #define R (T - 1) /* root position */ |
| 295 | #define MAX_FREQ 0x8000 |
| 296 | /* update when cumulative frequency */ |
| 297 | /* reaches to this value */ |
| 298 | |
| 299 | /* |
| 300 | * Tables for encoding/decoding upper 6 bits of |
| 301 | * sliding dictionary pointer |
| 302 | */ |
| 303 | |
| 304 | /* decoder table */ |
| 305 | static UINT8 d_code[256] = { |
| 306 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 307 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 308 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 309 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 310 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, |
| 311 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, |
| 312 | 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, |
| 313 | 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, |
| 314 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 315 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 316 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 317 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 318 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 319 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 320 | 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, |
| 321 | 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, |
| 322 | 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, |
| 323 | 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, |
| 324 | 0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D, |
| 325 | 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F, |
| 326 | 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11, |
| 327 | 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13, |
| 328 | 0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15, |
| 329 | 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17, |
| 330 | 0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B, |
| 331 | 0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F, |
| 332 | 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23, |
| 333 | 0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27, |
| 334 | 0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B, |
| 335 | 0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F, |
| 336 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 337 | 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, |
| 338 | }; |
| 339 | |
| 340 | static UINT8 d_len[256] = { |
| 341 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 342 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 343 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 344 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 345 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 346 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 347 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 348 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 349 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 350 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 351 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 352 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 353 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 354 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 355 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 356 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 357 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 358 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 359 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 360 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 361 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 362 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 363 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 364 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 365 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 366 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 367 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 368 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 369 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 370 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 371 | 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, |
| 372 | 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, |
| 373 | }; |
| 374 | |
| 375 | static UINT16 freq[T + 1]; /* cumulative freq table */ |
| 376 | |
| 377 | /* |
| 378 | * pointing parent nodes. |
| 379 | * area [T..(T + N_CHAR - 1)] are pointers for leaves |
| 380 | */ |
| 381 | INT16 prnt[T + N_CHAR]; |
| 382 | |
| 383 | /* pointing children nodes (son[], son[] + 1)*/ |
| 384 | INT16 son[T]; |
| 385 | |
| 386 | static UINT16 getbuf; |
| 387 | static UINT8 getlen; |
| 388 | |
| 389 | static int next_word(void) |
| 390 | { |
| 391 | if(tdctl.ibufndx >= tdctl.ibufcnt) |
| 392 | { |
| 393 | tdctl.ibufndx = 0; |
| 394 | tdctl.ibufcnt = data_read(tdctl.inbuf,BUFSZ); |
| 395 | if(tdctl.ibufcnt <= 0) |
| 396 | return(-1); |
| 397 | } |
| 398 | while (getlen <= 8) { // typically reads a word at a time |
| 399 | getbuf |= tdctl.inbuf[tdctl.ibufndx++] << (8 - getlen); |
| 400 | getlen += 8; |
| 401 | } |
| 402 | return(0); |
| 403 | } |
| 404 | |
| 405 | |
| 406 | static int GetBit(void) /* get one bit */ |
| 407 | { |
| 408 | INT16 i; |
| 409 | if(next_word() < 0) |
| 410 | return(-1); |
| 411 | i = getbuf; |
| 412 | getbuf <<= 1; |
| 413 | getlen--; |
| 414 | if(i < 0) |
| 415 | return(1); |
| 416 | else |
| 417 | return(0); |
| 418 | } |
| 419 | |
| 420 | static int GetByte(void) /* get a byte */ |
| 421 | { |
| 422 | UINT16 i; |
| 423 | if(next_word() != 0) |
| 424 | return(-1); |
| 425 | i = getbuf; |
| 426 | getbuf <<= 8; |
| 427 | getlen -= 8; |
| 428 | i = i >> 8; |
| 429 | return((int) i); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | |
| 434 | /* initialize freq tree */ |
| 435 | |
| 436 | static void StartHuff(void) |
| 437 | { |
| 438 | int i, j; |
| 439 | |
| 440 | for (i = 0; i < N_CHAR; i++) { |
| 441 | freq[i] = 1; |
| 442 | son[i] = i + T; |
| 443 | prnt[i + T] = i; |
| 444 | } |
| 445 | i = 0; j = N_CHAR; |
| 446 | while (j <= R) { |
| 447 | freq[j] = freq[i] + freq[i + 1]; |
| 448 | son[j] = i; |
| 449 | prnt[i] = prnt[i + 1] = j; |
| 450 | i += 2; j++; |
| 451 | } |
| 452 | freq[T] = 0xffff; |
| 453 | prnt[R] = 0; |
| 454 | } |
| 455 | |
| 456 | |
| 457 | /* reconstruct freq tree */ |
| 458 | |
| 459 | static void reconst(void) |
| 460 | { |
| 461 | INT16 i, j, k; |
| 462 | UINT16 f, l; |
| 463 | |
| 464 | /* halven cumulative freq for leaf nodes */ |
| 465 | j = 0; |
| 466 | for (i = 0; i < T; i++) { |
| 467 | if (son[i] >= T) { |
| 468 | freq[j] = (freq[i] + 1) / 2; |
| 469 | son[j] = son[i]; |
| 470 | j++; |
| 471 | } |
| 472 | } |
| 473 | /* make a tree : first, connect children nodes */ |
| 474 | for (i = 0, j = N_CHAR; j < T; i += 2, j++) { |
| 475 | k = i + 1; |
| 476 | f = freq[j] = freq[i] + freq[k]; |
| 477 | for (k = j - 1; f < freq[k]; k--); |
| 478 | k++; |
| 479 | l = (j - k) * 2; |
| 480 | |
| 481 | /* movmem() is Turbo-C dependent |
| 482 | rewritten to memmove() by Kenji */ |
| 483 | |
| 484 | /* movmem(&freq[k], &freq[k + 1], l); */ |
| 485 | (void)memmove(&freq[k + 1], &freq[k], l); |
| 486 | freq[k] = f; |
| 487 | /* movmem(&son[k], &son[k + 1], l); */ |
| 488 | (void)memmove(&son[k + 1], &son[k], l); |
| 489 | son[k] = i; |
| 490 | } |
| 491 | /* connect parent nodes */ |
| 492 | for (i = 0; i < T; i++) { |
| 493 | if ((k = son[i]) >= T) { |
| 494 | prnt[k] = i; |
| 495 | } else { |
| 496 | prnt[k] = prnt[k + 1] = i; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | |
| 502 | /* update freq tree */ |
| 503 | |
| 504 | static void update(int c) |
| 505 | { |
| 506 | int i, j, k, l; |
| 507 | |
| 508 | if (freq[R] == MAX_FREQ) { |
| 509 | reconst(); |
| 510 | } |
| 511 | c = prnt[c + T]; |
| 512 | do { |
| 513 | k = ++freq[c]; |
| 514 | |
| 515 | /* swap nodes to keep the tree freq-ordered */ |
| 516 | if (k > freq[l = c + 1]) { |
| 517 | while (k > freq[++l]); |
| 518 | l--; |
| 519 | freq[c] = freq[l]; |
| 520 | freq[l] = k; |
| 521 | |
| 522 | i = son[c]; |
| 523 | prnt[i] = l; |
| 524 | if (i < T) prnt[i + 1] = l; |
| 525 | |
| 526 | j = son[l]; |
| 527 | son[l] = i; |
| 528 | |
| 529 | prnt[j] = c; |
| 530 | if (j < T) prnt[j + 1] = c; |
| 531 | son[c] = j; |
| 532 | |
| 533 | c = l; |
| 534 | } |
| 535 | } while ((c = prnt[c]) != 0); /* do it until reaching the root */ |
| 536 | } |
| 537 | |
| 538 | |
| 539 | static INT16 DecodeChar(void) |
| 540 | { |
| 541 | int ret; |
| 542 | UINT16 c; |
| 543 | |
| 544 | c = son[R]; |
| 545 | |
| 546 | /* |
| 547 | * start searching tree from the root to leaves. |
| 548 | * choose node #(son[]) if input bit == 0 |
| 549 | * else choose #(son[]+1) (input bit == 1) |
| 550 | */ |
| 551 | while (c < T) { |
| 552 | if((ret = GetBit()) < 0) |
| 553 | return(-1); |
| 554 | c += (unsigned) ret; |
| 555 | c = son[c]; |
| 556 | } |
| 557 | c -= T; |
| 558 | update(c); |
| 559 | return c; |
| 560 | } |
| 561 | |
| 562 | static INT16 DecodePosition(void) |
| 563 | { |
| 564 | INT16 bit; |
| 565 | UINT16 i, j, c; |
| 566 | |
| 567 | /* decode upper 6 bits from given table */ |
| 568 | if((bit=GetByte()) < 0) |
| 569 | return(-1); |
| 570 | i = (UINT16) bit; |
| 571 | c = (UINT16)d_code[i] << 6; |
| 572 | j = d_len[i]; |
| 573 | |
| 574 | /* input lower 6 bits directly */ |
| 575 | j -= 2; |
| 576 | while (j--) { |
| 577 | if((bit = GetBit()) < 0) |
| 578 | return(-1); |
| 579 | i = (i << 1) + bit; |
| 580 | } |
| 581 | return(c | (i & 0x3f)); |
| 582 | } |
| 583 | |
| 584 | /* DeCompression |
| 585 | |
| 586 | split out initialization code to init_Decode() |
| 587 | |
| 588 | */ |
| 589 | |
| 590 | static void init_Decode(void) |
| 591 | { |
| 592 | int i; |
| 593 | getbuf = 0; |
| 594 | getlen = 0; |
| 595 | tdctl.ibufcnt= tdctl.ibufndx = 0; // input buffer is empty |
| 596 | tdctl.bufcnt = 0; |
| 597 | StartHuff(); |
| 598 | for (i = 0; i < N - F; i++) |
| 599 | text_buf[i] = ' '; |
| 600 | tdctl.r = N - F; |
| 601 | } |
| 602 | |
| 603 | |
| 604 | static int Decode(UINT8 *buf, int len) /* Decoding/Uncompressing */ |
| 605 | { |
| 606 | INT16 c,pos; |
| 607 | int count; // was an unsigned long, seems unnecessary |
| 608 | for (count = 0; count < len; ) { |
| 609 | if(tdctl.bufcnt == 0) { |
| 610 | if((c = DecodeChar()) < 0) |
| 611 | return(count); // fatal error |
| 612 | if (c < 256) { |
| 613 | *(buf++) = c; |
| 614 | text_buf[tdctl.r++] = c; |
| 615 | tdctl.r &= (N - 1); |
| 616 | count++; |
| 617 | } |
| 618 | else { |
| 619 | if((pos = DecodePosition()) < 0) |
| 620 | return(count); // fatal error |
| 621 | tdctl.bufpos = (tdctl.r - pos - 1) & (N - 1); |
| 622 | tdctl.bufcnt = c - 255 + THRESHOLD; |
| 623 | tdctl.bufndx = 0; |
| 624 | } |
| 625 | } |
| 626 | else { // still chars from last string |
| 627 | while( tdctl.bufndx < tdctl.bufcnt && count < len ) { |
| 628 | c = text_buf[(tdctl.bufpos + tdctl.bufndx) & (N - 1)]; |
| 629 | *(buf++) = c; |
| 630 | tdctl.bufndx++; |
| 631 | text_buf[tdctl.r++] = c; |
| 632 | tdctl.r &= (N - 1); |
| 633 | count++; |
| 634 | } |
| 635 | // reset bufcnt after copy string from text_buf[] |
| 636 | if(tdctl.bufndx >= tdctl.bufcnt) |
| 637 | tdctl.bufndx = tdctl.bufcnt = 0; |
| 638 | } |
| 639 | } |
| 640 | return(count); // count == len, success |
| 641 | } |
| 642 | |
| 643 | |
| 252 | 644 | FLOPPY_CONSTRUCT( td0_dsk_construct ) |
| 253 | 645 | { |
| 254 | 646 | struct FloppyCallbacks *callbacks; |
| 255 | 647 | struct td0dsk_tag *tag; |
| 256 | | UINT8 header[16]; |
| 648 | UINT8 *header; |
| 257 | 649 | int number_of_sectors; |
| 258 | 650 | int position; |
| 259 | 651 | int i; |
| r5827 | r5828 | |
| 265 | 657 | return FLOPPY_ERROR_UNSUPPORTED; |
| 266 | 658 | } |
| 267 | 659 | |
| 268 | | floppy_image_read(floppy, header, 0, 16); |
| 269 | | |
| 270 | 660 | tag = (struct td0dsk_tag *) floppy_create_tag(floppy, TD0_DSK_TAG, sizeof(struct td0dsk_tag)); |
| 271 | 661 | if (!tag) |
| 272 | 662 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 273 | 663 | |
| 664 | tag->data = (UINT8*)malloc(floppy_image_size(floppy)); |
| 665 | if (tag->data==NULL) { |
| 666 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 667 | } |
| 668 | floppy_image_read(floppy, tag->data, 0, floppy_image_size(floppy)); |
| 669 | header = tag->data; |
| 670 | |
| 671 | if (header[0]=='t') { |
| 672 | UINT8 obuf[BUFSZ]; |
| 673 | int rd; |
| 674 | int off = 12; |
| 675 | int size = 0; |
| 676 | floppy_file = floppy; |
| 677 | init_Decode(); |
| 678 | floppy_file_offset =12; |
| 679 | do |
| 680 | { |
| 681 | if((rd = Decode(obuf,BUFSZ)) > 0) size += rd; |
| 682 | } while(rd == BUFSZ); |
| 683 | memcpy(obuf,tag->data,12); |
| 684 | free(tag->data); |
| 685 | tag->data = (UINT8*)malloc(size+12); |
| 686 | if (tag->data==NULL) { |
| 687 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 688 | } |
| 689 | memcpy(tag->data,obuf,12); |
| 690 | floppy_file_offset =12; |
| 691 | init_Decode(); |
| 692 | do |
| 693 | { |
| 694 | if((rd = Decode(obuf,BUFSZ)) > 0) { |
| 695 | memcpy(tag->data+off,obuf,rd); |
| 696 | off += rd; |
| 697 | } |
| 698 | } while(rd == BUFSZ); |
| 699 | } |
| 700 | header = tag->data; |
| 274 | 701 | tag->heads = header[9]; |
| 275 | 702 | if (tag->heads > 1) { |
| 276 | 703 | tag->heads = 2; |
| 277 | | } |
| 278 | | |
| 704 | } |
| 705 | |
| 279 | 706 | // header len + comment header + comment len |
| 280 | 707 | position = 12; |
| 281 | 708 | if (header[7] & 0x80) { |
| 282 | 709 | position += 10 + header[14] + (header[15]<<8); |
| 283 | 710 | } |
| 284 | 711 | tag->tracks = 0; |
| 285 | | |
| 286 | | do { |
| 712 | do { |
| 287 | 713 | // read track header |
| 288 | | floppy_image_read(floppy, header, position, 4); |
| 714 | header = tag->data + position; |
| 289 | 715 | track = header[1]; |
| 290 | 716 | number_of_sectors = header[0]; |
| 291 | | if (number_of_sectors!=0xff){ |
| 717 | if (number_of_sectors!=0xff){ |
| 292 | 718 | position+=4; |
| 293 | 719 | tag->track_offsets[(track<<1) + (header[2] & 1)] = position; |
| 294 | 720 | for(i=0;i<number_of_sectors;i++) { |
| 295 | 721 | // read sector header |
| 296 | | floppy_image_read(floppy, header, position, 6); |
| 722 | header = tag->data + position; |
| 297 | 723 | position+=6; |
| 298 | 724 | // read sector size |
| 299 | 725 | if ((header[4] & 0x30)==0) { |
| 300 | 726 | // if there is sector data |
| 301 | | floppy_image_read(floppy, header, position, 2); |
| 727 | header = tag->data + position; |
| 302 | 728 | position+=2; |
| 303 | 729 | // skip sector data |
| 304 | 730 | position+= header[0] + (header[1]<<8); |
| 305 | 731 | } |
| 306 | 732 | } |
| 307 | 733 | tag->tracks++; |
| 308 | | } |
| 734 | } |
| 309 | 735 | } while(number_of_sectors!=0xff); |
| 310 | 736 | tag->tracks++; |
| 311 | 737 | |