summaryrefslogtreecommitdiff
path: root/lib/yazedc.cpp
blob: 6350ef73118de6eded9e3b9c7718d42683af928a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* 
 *  PSX-Tools Bundle Pack 
 *  Copyright (C) 1998 Heiko Eissfeldt
 *  portions used& Chris Smith
 *  Modified by Yazoo, then by
 *  Nicolas "Pixel" Noble
 *  
 *  This program is free software; you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or 
 *  (at your option) any later version. 
 * 
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 * 
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

/* $Id: yazedc.cpp,v 1.3 2003-12-04 01:47:37 pixel Exp $ */

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "yazedc.h"

yazedc::yazedc() : minute(0), second(2), frame(0), sectortype(0) {}

/* ------------- tables generated by gen_encodes --------------*/

#include "crctables"

static int encode_L2_Q(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P + L2_Q])
{
  unsigned char *Q;
  int i,j;

  Q = inout + 4 + L2_RAW + 4 + 8 + L2_P;
  memset(Q, 0, L2_Q);
  for (j = 0; j < 26; j++) {
     for (i = 0; i < 43; i++) {
	unsigned char data;

        /* LSB */
        data = inout[(j*43*2+i*2*44) % (4 + L2_RAW + 4 + 8 + L2_P)];
	if (data != 0) {
		unsigned int base = rs_l12_log[data];

		unsigned int sum = base + DQ[0][i];
		if (sum >= ((1 << RS_L12_BITS)-1))
		  sum -= (1 << RS_L12_BITS)-1;
		
		Q[0]    ^= rs_l12_alog[sum];

		sum = base + DQ[1][i];
		if (sum >= ((1 << RS_L12_BITS)-1))
		  sum -= (1 << RS_L12_BITS)-1;
		
		Q[26*2] ^= rs_l12_alog[sum];
	}
        /* MSB */
        data = inout[(j*43*2+i*2*44+1) % (4 + L2_RAW + 4 + 8 + L2_P)];
	if (data != 0) {
		unsigned int base = rs_l12_log[data];

		unsigned int sum = base+DQ[0][i];
		if (sum >= ((1 << RS_L12_BITS)-1))
		  sum -= (1 << RS_L12_BITS)-1;
		
		Q[1]      ^= rs_l12_alog[sum];

		sum = base + DQ[1][i];
		if (sum >= ((1 << RS_L12_BITS)-1))
		  sum -= (1 << RS_L12_BITS)-1;
		
		Q[26*2+1] ^= rs_l12_alog[sum];
	}
     }
     Q += 2;
  }
  return 0;
}

static int encode_L2_P(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P])
{
  unsigned char *P;
  int i,j;

  P = inout + 4 + L2_RAW + 4 + 8;
  memset(P, 0, L2_P);
  for (j = 0; j < 43; j++) {
     for (i = 0; i < 24; i++) {
	unsigned char data;

        /* LSB */
        data = inout[i*2*43];
	if (data != 0) {
		unsigned int base = rs_l12_log[data];
		
		unsigned int sum = base + DP[0][i];
		if (sum >= ((1 << RS_L12_BITS)-1))
		  sum -= (1 << RS_L12_BITS)-1;

		P[0]    ^= rs_l12_alog[sum];

		sum = base + DP[1][i];
		if (sum >= ((1 << RS_L12_BITS)-1))
		  sum -= (1 << RS_L12_BITS)-1;
		
		P[43*2] ^= rs_l12_alog[sum];
	}
        /* MSB */
        data = inout[i*2*43+1];
	if (data != 0) {
		unsigned int base = rs_l12_log[data];

		unsigned int sum = base + DP[0][i];
		if (sum >= ((1 << RS_L12_BITS)-1))
		  sum -= (1 << RS_L12_BITS)-1;
		
		P[1]      ^= rs_l12_alog[sum];

		sum = base + DP[1][i];
		if (sum >= ((1 << RS_L12_BITS)-1))
		  sum -= (1 << RS_L12_BITS)-1;
		
		P[43*2+1] ^= rs_l12_alog[sum];
	}
     }
     P += 2;
     inout += 2;
  }
  return 0;
}

int yazedc::scramble_L2(unsigned char *inout)
{
  unsigned char *r = inout + 12;
  const unsigned char *s = yellowbook_scrambler;
  unsigned int i;
  unsigned int *f = (unsigned int *)inout;

  for (i = (L2_RAW + L2_Q + L2_P +16)/sizeof(unsigned char); i; i--) {
    *r++ ^= *s++;
  }

  /* generate F1 frames */
  for (i = (2352/sizeof(unsigned int)); i; i--) {
    *f++ = ((*f & 0xff00ff00UL) >> 8) | ((*f & 0x00ff00ffUL) << 8);
  }

  return 0;
}

int yazedc::build_address(unsigned char inout[], int sectortype, unsigned address)
{
  inout[12] = minute;
  inout[13] = second;
  inout[14] = frame;
  if (sectortype == MODE_0)
	inout[15] = 0;
  else if (sectortype == MODE_1)
	inout[15] = 1;
  else if (sectortype == MODE_2)
	inout[15] = 2;
  else if (sectortype == MODE_2_FORM_1)
	inout[15] = 2;
  else if (sectortype == MODE_2_FORM_2)
	inout[15] = 2;
  else
	return -1;
  return 0;
}

#include "crctable.out"
unsigned long int build_edc(unsigned char inout[], int from, int upto)
{
  unsigned char *p = inout+from;
  unsigned long result = 0;

  for (; from <= upto; from++)
    result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);

  return result;
}

/* Layer 2 Product code en/decoder */
int yazedc::do_encode_L2(unsigned char inout[(12 + 4 + L2_RAW+4+8+L2_Q+L2_P)], int sectortype, unsigned address)
{
  unsigned long int result;

#define SYNCPATTERN "\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"

  /* supply initial sync pattern */
  memcpy(inout, SYNCPATTERN, sizeof(SYNCPATTERN));

  if (sectortype == MODE_0) {
	memset(inout + sizeof(SYNCPATTERN), 0, 4 + L2_RAW + 12 + L2_P + L2_Q);
        build_address(inout, sectortype, address);
	return 0;
  }
  
  switch (sectortype) {
    case MODE_1:
        build_address(inout, sectortype, address);
	result = build_edc(inout, 0, 16+2048-1);
	inout[2064+0] = result >> 0L;
	inout[2064+1] = result >> 8L;
	inout[2064+2] = result >> 16L;
	inout[2064+3] = result >> 24L;
	memset(inout+2064+4, 0, 8);
  	encode_L2_P(inout+12);
	encode_L2_Q(inout+12);
	break;
    case MODE_2:
	build_address(inout, sectortype, address);
	break;
    case MODE_2_FORM_1:
	result = build_edc(inout, 16, 16+8+2048-1);
	inout[2072+0] = result >> 0L;
	inout[2072+1] = result >> 8L;
	inout[2072+2] = result >> 16L;
	inout[2072+3] = result >> 24L;

	/* clear header for P/Q parity calculation */
	inout[12] = 0;
	inout[12+1] = 0;
	inout[12+2] = 0;
	inout[12+3] = 0;
  	encode_L2_P(inout+12);
 	encode_L2_Q(inout+12);
	build_address(inout, sectortype, address);
	break;
    case MODE_2_FORM_2:
        build_address(inout, sectortype, address);
	result = build_edc(inout, 16, 16+8+2324-1);
	inout[2348+0] = result >> 0L;
	inout[2348+1] = result >> 8L;
	inout[2348+2] = result >> 16L;
	inout[2348+3] = result >> 24L;
	break;
    default:
	return -1;
  }

  return 0;
}

int yazedc::get_sector_type(void)
{
  return sectortype;
}

int yazedc::set_sector_type(int st)
{
  switch(st) {
    case MODE_0:
    case MODE_1:
    case MODE_2:
    case MODE_2_FORM_1:
    case MODE_2_FORM_2:
      sectortype = st;
    default:
      return -1;
  }
  return 0;
}