📄 dvbcsa_stream.c
字号:
/* This file is part of libdvbcsa. libdvbcsa 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. libdvbcsa 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 libdvbcsa; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA (c) 2006-2008 Alexandre Becoulet <alexandre.becoulet@free.fr>*/#include "dvbcsa/dvbcsa.h"#include "dvbcsa_pv.h"/*** Get nibble from A and B 40 bits registers*/#define NBGET(r, n) (r >> (n * 4))/*** CSA algorithm is using X, Y, Z, D, E, F 4 bits registers** and P, Q, C 1 bits registers. these registers are stored** in `pqzyx' and `cfed' variables to improve speed.*/#define GETX(x) (x)#define GETY(x) ((x) >> 4)#define GETZ(x) ((x) >> 8)#define TSTP(x) ((x) & 0x1000)#define TSTQ(x) ((x) & 0x2000)#define GETD(x) (x)#define GETE(x) ((x) >> 4)#define GETF(x) ((x) >> 8)#define GETC(x) ((x) >> 12)/*** swap nibbles in byte*/DVBCSA_INLINEstatic inline uint8_t swap_nbl (register uint8_t byte){ return ((byte >> 4) | (byte << 4));}/*** conditional rotation used in stream round*/DVBCSA_INLINEstatic inline uint8_t csa_stream_rotate (register uint32_t pqzyx, register uint32_t x){ return TSTP(pqzyx) ? ((x << 1) | ((x >> 3) & 1)) & 0xf : x;}/*** process bit selection, permutation and sboxes*/DVBCSA_INLINEstatic inline uint32_t csa_stream_sboxes(register uint64_t A){ /* modified sboxes tables */ static const uint16_t sbox[7][32] = { { 0x0001, 0x0000, 0x0400, 0x0400, 0x0001, 0x0401, 0x0401, 0x0000, 0x0401, 0x0001, 0x0001, 0x0000, 0x0400, 0x0400, 0x0000, 0x0401, 0x0000, 0x0401, 0x0401, 0x0000, 0x0001, 0x0001, 0x0400, 0x0400, 0x0001, 0x0001, 0x0000, 0x0401, 0x0400, 0x0400, 0x0401, 0x0000, }, { 0x0802, 0x0800, 0x0002, 0x0800, 0x0802, 0x0002, 0x0002, 0x0800, 0x0800, 0x0802, 0x0000, 0x0002, 0x0000, 0x0000, 0x0000, 0x0802, 0x0802, 0x0800, 0x0800, 0x0002, 0x0002, 0x0802, 0x0800, 0x0802, 0x0000, 0x0000, 0x0000, 0x0802, 0x0800, 0x0002, 0x0002, 0x0000, }, { 0x0010, 0x0000, 0x0010, 0x0010, 0x0010, 0x0014, 0x0000, 0x0004, 0x0004, 0x0014, 0x0014, 0x0014, 0x0014, 0x0000, 0x0004, 0x0000, 0x0014, 0x0000, 0x0004, 0x0010, 0x0004, 0x0014, 0x0014, 0x0004, 0x0000, 0x0004, 0x0000, 0x0004, 0x0010, 0x0000, 0x0010, 0x0010, }, { 0x0028, 0x0008, 0x0020, 0x0028, 0x0020, 0x0000, 0x0020, 0x0008, 0x0008, 0x0020, 0x0000, 0x0008, 0x0000, 0x0028, 0x0028, 0x0000, 0x0008, 0x0000, 0x0028, 0x0008, 0x0028, 0x0020, 0x0028, 0x0000, 0x0000, 0x0028, 0x0020, 0x0000, 0x0020, 0x0008, 0x0008, 0x0020, }, { 0x0100, 0x0000, 0x0000, 0x0040, 0x0140, 0x0100, 0x0140, 0x0100, 0x0140, 0x0140, 0x0000, 0x0040, 0x0100, 0x0040, 0x0040, 0x0000, 0x0140, 0x0100, 0x0000, 0x0100, 0x0140, 0x0000, 0x0040, 0x0040, 0x0100, 0x0140, 0x0000, 0x0040, 0x0100, 0x0000, 0x0040, 0x0140, }, { 0x0000, 0x0080, 0x0200, 0x0280, 0x0080, 0x0200, 0x0200, 0x0000, 0x0000, 0x0080, 0x0280, 0x0000, 0x0200, 0x0280, 0x0080, 0x0280, 0x0200, 0x0280, 0x0000, 0x0200, 0x0280, 0x0000, 0x0080, 0x0080, 0x0200, 0x0080, 0x0080, 0x0200, 0x0000, 0x0280, 0x0280, 0x0000, }, { 0x0000, 0x0000, 0x1000, 0x2000, 0x3000, 0x3000, 0x0000, 0x1000, 0x3000, 0x1000, 0x2000, 0x2000, 0x2000, 0x0000, 0x1000, 0x3000, 0x3000, 0x1000, 0x2000, 0x2000, 0x2000, 0x3000, 0x0000, 0x0000, 0x2000, 0x1000, 0x1000, 0x3000, 0x0000, 0x0000, 0x1000, 0x3000, }, }; uint32_t res; uint64_t t; t = A & 0x2018004200LL; res = sbox[1][ ((t >> 37) ^ (t >> 27) ^ (t >> 25) ^ (t >> 11) ^ (t >> 5)) & 0x1f ]; t = A & 0x4201480000LL; res |= sbox[4][ ((t >> 38) ^ (t >> 32) ^ (t >> 22) ^ (t >> 16) ^ (t >> 18)) & 0x1f ]; t = A & 0x8040122000LL; res |= sbox[5][ ((t >> 39) ^ (t >> 29) ^ (t >> 18) ^ (t >> 14) ^ (t >> 9)) & 0x1f ]; t = A & 0x1082010040LL; res |= sbox[0][ ((t >> 36) ^ (t >> 30) ^ (t >> 23) ^ (t >> 3) ^ (t >> 12)) & 0x1f ]; t = A & 0x0004a00180LL; res |= sbox[2][ ((t >> 26) ^ (t >> 22) ^ (t >> 19) ^ (t >> 5) ^ (t >> 3)) & 0x1f ]; t = A & 0x0100048820LL; res |= sbox[3][ ((t >> 32) ^ (t >> 17) ^ (t >> 9) ^ (t >> 2) ^ (t >> 11)) & 0x1f ]; t = A & 0x0c20001400LL; res |= sbox[6][ ((t >> 35) ^ (t >> 33) ^ (t >> 27) ^ (t >> 9) ^ (t >> 6)) & 0x1f ]; return res;}/*** bit selection and XOR from B*/DVBCSA_INLINEstatic inline uint32_t csa_stream_B_sel (register uint64_t B){ uint32_t t = B >> 9; /* 10000000 01000010000000000001000000000000 */ /* 00000010 00000001000001001000000000000000 */ /* 00000100 00000000101000010000000000000000 */ /* 01000001 00001000000000000010000000000000 */ return (((t ) ^ (t >> 27)) & 0x8) ^ (((t >> 18) ) & 0x9) ^ (( (t >> 22) ^ (t >> 7)) & 0x4) ^ (((t >> 4 ) ) & 0x5) ^ (( (t >> 24) ^ (t >> 6) ^ (t >> 11)) & 0x2) ^ (((t >> 29) ^ (t >> 23)) & 0x1) ^ (((t >> 13) ) & 0xe);}/*** cfed precomputed operations*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -