revert breaks some stupid old compilers
[oscam.git] / module-gbox-helper.c
blobfe59848b5a711cd1d558c188cc9abf7ac2ae7ce1
1 #define MODULE_LOG_PREFIX "gbox"
3 #include "globals.h"
5 #ifdef MODULE_GBOX
6 #include "minilzo/minilzo.h"
7 #include "oscam-string.h"
10 uint16_t gbox_get_caid(uint32_t caprovid)
12 if ((caprovid >> 24) == 0x05)
13 { return 0x0500; }
14 else
15 { return caprovid >> 16; }
18 uint32_t gbox_get_provid(uint32_t caprovid)
20 uint32_t provid = 0;
22 switch(caprovid >> 24)
24 //ViXS
25 case 0x05:
26 provid = caprovid & 0xFFFFFF;
27 break;
28 //Cryptoworx
29 case 0x0D:
30 provid = (caprovid >> 8) & 0xFF;
31 break;
32 default:
33 provid = caprovid & 0xFFFF;
34 break;
36 return provid;
39 uint32_t gbox_get_caprovid(uint16_t caid, uint32_t prid)
41 uint32_t caprovid = 0;
43 switch(caid >> 8)
45 // ViXS
46 case 0x05:
47 caprovid = (caid >> 8) << 24 | (prid & 0xFFFFFF);
48 break;
49 // Cryptoworx
50 case 0x0D:
51 caprovid = (caid >> 8) << 24 | (caid & 0xFF) << 16 |
52 ((prid << 8) & 0xFF00);
53 break;
54 // N@gr@
55 case 0x18:
56 caprovid = (caid >> 8) << 24 | (caid & 0xFF) << 16;
57 break;
58 default:
59 caprovid = (caid >> 8) << 24 | (caid & 0xFF) << 16 |
60 (prid & 0xFFFF);
61 break;
63 return caprovid;
66 static void gbox_convert_pw(uchar *password, uint32_t pw)
68 int32_t i;
69 for(i = 3; i >= 0; i--)
71 password[3 - i] = (pw >> (8 * i)) & 0xff;
75 uint32_t gbox_get_checksum(uchar *buf, uint16_t buflen)
77 uint8_t checksum[4];
78 int32_t counter;
80 checksum[3] = buf[0];
81 checksum[2] = buf[1];
82 checksum[1] = buf[2];
83 checksum[0] = buf[3];
85 for(counter = 1; counter < (buflen / 4) - 4; counter++)
87 checksum[3] ^= buf[counter * 4];
88 checksum[2] ^= buf[counter * 4 + 1];
89 checksum[1] ^= buf[counter * 4 + 2];
90 checksum[0] ^= buf[counter * 4 + 3];
93 return checksum[3] << 24 | checksum[2] << 16 | checksum[1] << 8 | checksum[0];
96 ////////////////////////////////////////////////////////////////////////////////
97 // GBOX BUFFER ENCRYPTION/DECRYPTION (thanks to dvbcrypt@gmail.com)
98 ////////////////////////////////////////////////////////////////////////////////
99 static unsigned char Lookup_Table[0x40] =
101 0x25, 0x38, 0xD4, 0xCD, 0x17, 0x7A, 0x5E, 0x6C, 0x52, 0x42, 0xFE, 0x68, 0xAB, 0x3F, 0xF7, 0xBE,
102 0x47, 0x57, 0x71, 0xB0, 0x23, 0xC1, 0x26, 0x6C, 0x41, 0xCE, 0x94, 0x37, 0x45, 0x04, 0xA2, 0xEA,
103 0x07, 0x58, 0x35, 0x55, 0x08, 0x2A, 0x0F, 0xE7, 0xAC, 0x76, 0xF0, 0xC1, 0xE6, 0x09, 0x10, 0xDD,
104 0xC5, 0x8D, 0x2E, 0xD9, 0x03, 0x9C, 0x3D, 0x2C, 0x4D, 0x41, 0x0C, 0x5E, 0xDE, 0xE4, 0x90, 0xAE
107 static void gbox_encrypt8(unsigned char *buffer, unsigned char *pass)
109 int passcounter;
110 int bufcounter;
111 unsigned char temp;
112 for(passcounter = 0; passcounter < 4; passcounter++)
113 for(bufcounter = 7; bufcounter >= 0; bufcounter--)
115 temp = pass[3];
116 pass[3] = (pass[3] / 2) + (pass[2] & 1) * 0x80;
117 pass[2] = (pass[2] / 2) + (pass[1] & 1) * 0x80;
118 pass[1] = (pass[1] / 2) + (pass[0] & 1) * 0x80;
119 pass[0] = (pass[0] / 2) + (temp & 1) * 0x80;
120 buffer[(bufcounter + 1) & 7] = buffer[(bufcounter + 1) & 7 ] - Lookup_Table[(buffer[bufcounter] >> 2) & 0x3F ];
121 buffer[(bufcounter + 1) & 7] = Lookup_Table[(buffer[bufcounter] - pass[(bufcounter + 1) & 3]) & 0x3F ] ^ buffer[(bufcounter + 1) & 7 ];
122 buffer[(bufcounter + 1) & 7] = buffer[(bufcounter + 1) & 7 ] - pass[(bufcounter & 3)];
126 static void gbox_decrypt8(unsigned char *buffer, unsigned char *pass)
128 unsigned char temp;
129 int bufcounter;
130 int passcounter;
131 for(passcounter = 3; passcounter >= 0; passcounter--)
132 for(bufcounter = 0; bufcounter <= 7; bufcounter++)
134 buffer[(bufcounter + 1) & 7] = pass[bufcounter & 3] + buffer[(bufcounter + 1) & 7];
135 temp = buffer[bufcounter] - pass[(bufcounter + 1) & 3];
136 buffer[(bufcounter + 1) & 7] = Lookup_Table[temp & 0x3F] ^ buffer[(bufcounter + 1) & 7];
137 temp = buffer[bufcounter] >> 2;
138 buffer[(bufcounter + 1) & 7] = Lookup_Table[temp & 0x3F] + buffer[(bufcounter + 1) & 7];
139 temp = pass[0] & 0x80;
140 pass[0] = ((pass[1] & 0x80) >> 7) + (pass[0] << 1);
141 pass[1] = ((pass[2] & 0x80) >> 7) + (pass[1] << 1);
142 pass[2] = ((pass[3] & 0x80) >> 7) + (pass[2] << 1);
143 pass[3] = (temp >> 7) + (pass[3] << 1);
147 static void gbox_decryptB(unsigned char *buffer, int bufsize, uchar *localkey)
149 int counter;
150 gbox_encrypt8(&buffer[bufsize - 9], localkey);
151 gbox_decrypt8(buffer, localkey);
152 for(counter = bufsize - 2; counter >= 0; counter--)
153 { buffer[counter] = buffer[counter + 1] ^ buffer[counter]; }
156 static void gbox_encryptB(unsigned char *buffer, int bufsize, uchar *key)
158 int counter;
159 for(counter = 0; counter < (bufsize - 1); counter++)
160 { buffer[counter] = buffer[counter + 1] ^ buffer[counter]; }
161 gbox_encrypt8(buffer, key);
162 gbox_decrypt8(&buffer[bufsize - 9], key);
165 static void gbox_encryptA(unsigned char *buffer, unsigned char *pass)
167 int counter;
168 unsigned char temp;
169 for(counter = 0x1F; counter >= 0; counter--)
171 temp = pass[3] & 1;
172 pass[3] = ((pass[2] & 1) << 7) + (pass[3] >> 1);
173 pass[2] = ((pass[1] & 1) << 7) + (pass[2] >> 1);
174 pass[1] = ((pass[0] & 1) << 7) + (pass[1] >> 1);
175 pass[0] = (temp << 7) + (pass[0] >> 1);
176 temp = (pass[(counter + 1) & 3] ^ buffer[counter & 7]) >> 2;
177 buffer[(counter + 1) & 7] = Lookup_Table[temp & 0x3F] * 2 + buffer[(counter + 1) & 7 ];
178 temp = buffer[counter & 7] - pass[(counter + 1) & 3];
179 buffer[(counter + 1) & 7] = Lookup_Table[temp & 0x3F] ^ buffer[(counter + 1) & 7];
180 buffer[(counter + 1) & 7] = pass[counter & 3] + buffer[(counter + 1) & 7];
184 static void gbox_decryptA(unsigned char *buffer, unsigned char *pass)
186 int counter;
187 unsigned char temp;
188 for(counter = 0; counter <= 0x1F; counter++)
190 buffer[(counter + 1) & 7] = buffer[(counter + 1) & 7] - pass[counter & 3];
191 temp = buffer[counter & 7] - pass[(counter + 1) & 3];
192 buffer[(counter + 1) & 7] = Lookup_Table[temp & 0x3F] ^ buffer[(counter + 1) & 7];
193 temp = (pass[(counter + 1) & 3] ^ buffer[counter & 7]) >> 2;
194 buffer[(counter + 1) & 7] = buffer[(counter + 1) & 7] - Lookup_Table[temp & 0x3F] * 2;
195 temp = pass[0] & 0x80;
196 pass[0] = ((pass[1] & 0x80) >> 7) + (pass[0] << 1);
197 pass[1] = ((pass[2] & 0x80) >> 7) + (pass[1] << 1);
198 pass[2] = ((pass[3] & 0x80) >> 7) + (pass[2] << 1);
199 pass[3] = (temp >> 7) + (pass[3] << 1);
203 void gbox_encrypt(uchar *buffer, int bufsize, uint32_t key)
205 uchar pass[4];
206 gbox_convert_pw(&pass[0], key);
207 gbox_encryptA(buffer, &pass[0]);
208 gbox_encryptB(buffer, bufsize, &pass[0]);
211 void gbox_decrypt(uchar *buffer, int bufsize, uint32_t localkey)
213 uchar pass[4];
214 gbox_convert_pw(&pass[0], localkey);
215 gbox_decryptB(buffer, bufsize, &pass[0]);
216 gbox_decryptA(buffer, &pass[0]);
219 void gbox_compress(uchar *buf, int32_t unpacked_len, int32_t *packed_len)
221 unsigned char *tmp, *tmp2;
222 lzo_voidp wrkmem;
223 if(!cs_malloc(&tmp, 0x40000))
225 return;
227 if(!cs_malloc(&tmp2, 0x40000))
229 NULLFREE(tmp);
230 return;
232 if(!cs_malloc(&wrkmem, unpacked_len * 0x1000))
234 NULLFREE(tmp);
235 NULLFREE(tmp2);
236 return;
238 unpacked_len -= 12;
239 memcpy(tmp2, buf + 12, unpacked_len);
240 lzo_init();
241 lzo_uint pl = 0;
242 if(lzo1x_1_compress(tmp2, unpacked_len, tmp, &pl, wrkmem) != LZO_E_OK)
243 { cs_log("compression failed!"); }
244 memcpy(buf + 12, tmp, pl);
245 pl += 12;
246 NULLFREE(tmp);
247 NULLFREE(tmp2);
248 NULLFREE(wrkmem);
249 *packed_len = pl;
252 void gbox_decompress(uchar *buf, int32_t *unpacked_len)
254 uchar *tmp;
255 if(!cs_malloc(&tmp, 0x40000))
256 { return; }
257 int err;
258 int len = *unpacked_len - 12;
259 *unpacked_len = 0x40000;
260 lzo_init();
261 cs_log_dbg(D_READER, "-> data decompressing %d bytes", len);
262 if((err = lzo1x_decompress_safe(buf + 12, len, tmp, (lzo_uint *)unpacked_len, NULL)) != LZO_E_OK)
263 { cs_log_dbg(D_READER, "gbox: decompression failed! errno=%d", err); }
264 memcpy(buf + 12, tmp, *unpacked_len);
265 *unpacked_len += 12;
266 NULLFREE(tmp);
268 #endif