This is extremely inspired by the MESS implementation, written by Nathan * Woods and R. Belmont: * https://github.com/mamedev/mame/blob/4263a71e64377db11392c458b580c5ae83556bc7/src/lib/formats/ap_dsk35.cpp */
| 57 | * https://github.com/mamedev/mame/blob/4263a71e64377db11392c458b580c5ae83556bc7/src/lib/formats/ap_dsk35.cpp |
| 58 | */ |
| 59 | static Bytes encode_crazy_data(const Bytes& input) |
| 60 | { |
| 61 | Bytes output; |
| 62 | ByteWriter bw(output); |
| 63 | ByteReader br(input); |
| 64 | |
| 65 | uint8_t w1, w2, w3, w4; |
| 66 | |
| 67 | static const int LOOKUP_LEN = MAC_SECTOR_LENGTH / 3; |
| 68 | |
| 69 | uint8_t b1[LOOKUP_LEN + 1]; |
| 70 | uint8_t b2[LOOKUP_LEN + 1]; |
| 71 | uint8_t b3[LOOKUP_LEN + 1]; |
| 72 | |
| 73 | uint32_t c1 = 0; |
| 74 | uint32_t c2 = 0; |
| 75 | uint32_t c3 = 0; |
| 76 | for (int j = 0;; j++) |
| 77 | { |
| 78 | c1 = (c1 & 0xff) << 1; |
| 79 | if (c1 & 0x0100) |
| 80 | c1++; |
| 81 | |
| 82 | uint8_t val = br.read_8(); |
| 83 | c3 += val; |
| 84 | if (c1 & 0x0100) |
| 85 | { |
| 86 | c3++; |
| 87 | c1 &= 0xff; |
| 88 | } |
| 89 | b1[j] = (val ^ c1) & 0xff; |
| 90 | |
| 91 | val = br.read_8(); |
| 92 | c2 += val; |
| 93 | if (c3 > 0xff) |
| 94 | { |
| 95 | c2++; |
| 96 | c3 &= 0xff; |
| 97 | } |
| 98 | b2[j] = (val ^ c3) & 0xff; |
| 99 | |
| 100 | if (br.pos == 524) |
| 101 | break; |
| 102 | |
| 103 | val = br.read_8(); |
| 104 | c1 += val; |
| 105 | if (c2 > 0xff) |
| 106 | { |
| 107 | c1++; |
| 108 | c2 &= 0xff; |
| 109 | } |
| 110 | b3[j] = (val ^ c2) & 0xff; |
| 111 | } |
| 112 | uint32_t c4 = ((c1 & 0xc0) >> 6) | ((c2 & 0xc0) >> 4) | ((c3 & 0xc0) >> 2); |
| 113 | b3[LOOKUP_LEN] = 0; |
| 114 | |
| 115 | for (int i = 0; i <= LOOKUP_LEN; i++) |
| 116 | { |