SASC
DVBSoftCAM

helper.h

Go to the documentation of this file.
00001 /*
00002  * Softcam plugin to VDR (C++)
00003  *
00004  * This code is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This code is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
00018  */
00019 
00020 #ifndef ___HELPER_H
00021 #define ___HELPER_H
00022 
00023 #include <byteswap.h>
00024 
00025 #if defined __i386__
00026 #define get_misalign(_a)    *(_a)
00027 #define put_misalign(_b,_a) *(_a)=(_b)
00028 #else
00029 template<class T> inline T get_misalign(T *p)
00030 { struct s { T v; } __attribute__((packed)); return ((s *)p)->v; }
00031 
00032 template<class T> inline void put_misalign(unsigned int v, T* p)
00033 { struct s { T v; } __attribute__((packed)); ((s *)p)->v = v; }
00034 #endif
00035 
00036 #define UINT32_BE(a) bswap_32(get_misalign((unsigned int *)(a)))
00037 #define UINT16_BE(a) bswap_16(get_misalign((unsigned short *)(a)))
00038 #define UINT8_BE(a)  *((unsigned char *)a)
00039 #define BYTE4_BE(a,b) put_misalign(bswap_32(b),(unsigned int *)(a))
00040 #define BYTE2_BE(a,b) put_misalign(bswap_16(b),(unsigned short *)(a))
00041 #define BYTE1_BE(a,b) *(unsigned char *)(a)=(b)
00042 
00043 #define UINT32_LE(a) get_misalign((unsigned int *)(a))
00044 #define UINT16_LE(a) get_misalign((unsigned short *)(a))
00045 #define UINT8_LE(a)  *((unsigned char *)a)
00046 #define BYTE4_LE(a,b) put_misalign(b,(unsigned int *)(a))
00047 #define BYTE2_LE(a,b) put_misalign(b,(unsigned short *)(a))
00048 #define BYTE1_LE(a,b) *(unsigned char *)(a)=(b)
00049 
00050 #define __ror16_const(x,xx) ((((x)&0xFFFF)>>(xx)) | ((x)<<(16-(xx))))
00051 #define __rol16_const(x,xx) (((x)<<(xx)) | (((x)&0xFFFF)>>(16-(xx))))
00052 #define __sn8_const(b)      ((((b)>>4)&0xf) + (((b)&0xf)<<4))
00053 
00054 #if defined __GNUC__ && __GNUC__ >= 2 && defined __i386__
00055 
00056 #define ror16(x,xx) ({ unsigned int v; \
00057                      __asm__ ("rorw %2, %w0" : "=qm" (v) : "0" (x), "i" (xx) : "cc"); \
00058                      v; })
00059 #define rol16(x,xx) ({ unsigned int v; \
00060                      __asm__ ("rolw %2, %w0" : "=qm" (v) : "0" (x), "i" (xx) : "cc"); \
00061                      v; })
00062 #define sn8(b)      ({ unsigned char v; \
00063                      if(__builtin_constant_p(b)) \
00064                        v=__sn8_const(b); \
00065                      else \
00066                        __asm__ ("rorb $4, %b0" : "=qm" (v) : "0" (b) : "cc"); \
00067                      v; })
00068 
00069 #else //__GNUC__
00070 
00071 #define ror16(x,xx) __ror16_const(x,xx)
00072 #define rol16(x,xx) __rol16_const(x,xx)
00073 #define sn8(b)      __sn8_const(b)
00074 
00075 #endif //__GNUC__
00076 
00077 static inline void __xxor(unsigned char *data, int len, const unsigned char *v1, const unsigned char *v2)
00078 {
00079   switch(len) { // looks ugly, but the compiler can optimize it very well ;)
00080     case 16:
00081       *((unsigned int *)data+3) = *((unsigned int *)v1+3) ^ *((unsigned int *)v2+3);
00082       *((unsigned int *)data+2) = *((unsigned int *)v1+2) ^ *((unsigned int *)v2+2);
00083     case 8:
00084       *((unsigned int *)data+1) = *((unsigned int *)v1+1) ^ *((unsigned int *)v2+1);
00085     case 4:
00086       *((unsigned int *)data+0) = *((unsigned int *)v1+0) ^ *((unsigned int *)v2+0);
00087       break;
00088     default:
00089       while(len--) *data++ = *v1++ ^ *v2++;
00090       break;
00091     }
00092 }
00093 #define xxor(d,l,v1,v2) __xxor(d,l,v1,v2)
00094 
00095 #endif // ___HELPER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines