SASC
DVBSoftCAM

misc.c

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 #include <ctype.h>
00021 #include <stdlib.h>
00022 #include <stdarg.h>
00023 #include <string.h>
00024 
00025 #include <vdr/tools.h>
00026 #include <vdr/thread.h>
00027 
00028 #include "misc.h"
00029 
00030 // -----------------------------------------------------------------------------
00031 
00032 const char *HexStr(char *str, const unsigned char *mem, int len)
00033 {
00034   for(int i=0 ; i<len ; i++) sprintf(&str[i*2],"%02X",mem[i]);
00035   return str;
00036 }
00037 
00038 static int HexDigit(int asc)
00039 {
00040   if(asc>='A') return toupper(asc)-'A'+10;
00041   return asc-'0';
00042 }
00043 
00044 int GetChar(const char * &line, int *store, int count)
00045 {
00046   line=skipspace(line);
00047   const char *sline=line;
00048   *store=0;
00049   while(count) {
00050     if(line[0]==0 || !isalnum(line[0])) break;
00051     *store<<=8; *store+=line[0];
00052     line++; count--;
00053     }
00054   if(!count && (!*line || isspace(*line) || *line==';')) return sline-line;
00055   *store=0;
00056   line=sline;
00057   return 0;
00058 }
00059 
00060 static int GetHexBase(const char * &line, unsigned char *store, int count, bool fixedLen, bool asc)
00061 {
00062   line=skipspace(line);
00063   const char *sline=line;
00064   unsigned char *sstore=store;
00065   int scount=count;
00066   while(count) {
00067     if(line[0]==0 || line[1]==0 || !isxdigit(line[0]) || !isxdigit(line[1])) break;
00068     if(asc) {
00069        *store++=line[0]; *store++=line[1];
00070        }
00071     else {
00072       int d1=HexDigit(line[0]);
00073       int d0=HexDigit(line[1]);
00074       *store++=(d1<<4) + d0;
00075       }
00076     line+=2; count--;
00077     }
00078   if(asc) *store=0; // make sure strings are NULL terminated
00079   if((!count || (!fixedLen && count!=scount)) &&
00080      (!*line || isspace(*line) || *line==';' || *line==']' || *line=='/') ) return scount-count;
00081   memset(sstore,0,scount);
00082   line=sline;
00083   return 0;
00084 }
00085 
00086 int GetHex(const char * &line, unsigned char *store, int count, bool fixedLen)
00087 {
00088   return GetHexBase(line,store,count,fixedLen,false);
00089 }
00090 
00091 int GetHexAsc(const char * &line, unsigned char *store, int count)
00092 {
00093   return GetHexBase(line,store,count/2,true,true)*2;
00094 }
00095 
00096 unsigned long long Bin2LongLong(unsigned char *mem, int len)
00097 {
00098   unsigned long long k=0;
00099   for(int i=0 ; i<len ; i++) { k<<=8; k+=mem[i]; }
00100   return k;
00101 }
00102 
00103 bool CheckNull(const unsigned char *data, int len)
00104 {
00105   while(--len>=0) if(data[len]) return false;
00106   return true;
00107 }
00108 
00109 bool CheckFF(const unsigned char *data, int len)
00110 {
00111   while(--len>=0) if(data[len]!=0xFF) return false;
00112   return true;
00113 }
00114 
00115 void SetSctLen(unsigned char *data, int len)
00116 {
00117   data[1]=(len>>8) | 0x70;
00118   data[2]=len & 0xFF;
00119 }
00120 
00121 unsigned char XorSum(const unsigned char *mem, int len)
00122 {
00123   unsigned char cs=0;
00124   while(len>0) { cs ^= *mem++; len--; }
00125   return cs;
00126 }
00127 
00128 char *bprintf(const char *fmt, ...)
00129 {
00130   va_list ap;
00131   va_start(ap,fmt);
00132   char *str=0;
00133   if(vasprintf(&str,fmt,ap)<0);
00134   va_end(ap);
00135   return str;
00136 }
00137 
00138 // crc stuff taken from linux-2.6.0/lib/crc32.c
00139 /*
00140  * There are multiple 16-bit CRC polynomials in common use, but this is
00141  * *the* standard CRC-32 polynomial, first popularized by Ethernet.
00142  * x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0
00143  */
00144 #define CRCPOLY_LE 0xedb88320
00145 
00146 unsigned int crc32_le(unsigned int crc, unsigned char const *p, int len)
00147 {
00148   crc^=0xffffffff; // zlib mod
00149   while(len--) {
00150     crc^=*p++;
00151     for(int i=0; i<8; i++)
00152       crc=(crc&1) ? (crc>>1)^CRCPOLY_LE : (crc>>1);
00153     }
00154   crc^=0xffffffff; // zlib mod
00155   return crc;
00156 }
00157 
00158 // -- cLineBuff -----------------------------------------------------------------
00159 
00160 cLineBuff::cLineBuff(int blocksize)
00161 {
00162   blockSize=blocksize;
00163   work=0;
00164   blen=wlen=0;
00165 }
00166 
00167 cLineBuff::~cLineBuff()
00168 {
00169   free(work);
00170 }
00171 
00172 void cLineBuff::Flush(void)
00173 {
00174   blen=0;
00175 }
00176 
00177 char *cLineBuff::Grab(void)
00178 {
00179   char *w=work;
00180   work=0; wlen=0;
00181   Flush();
00182   return w;
00183 }
00184 
00185 bool cLineBuff::Check(int num)
00186 {
00187   if(wlen-blen<num) {
00188     if(num<blockSize) num=blockSize;
00189     char *w=(char *)realloc(work,wlen+num);
00190     if(w) {
00191       wlen+=num;
00192       work=w;
00193       }
00194     }
00195   return work!=0;
00196 }
00197 
00198 void cLineBuff::Printf(const char *fmt, ...)
00199 {
00200   int q=120;
00201   while(Check(q)) {
00202     int s=wlen-blen;
00203     va_list ap;
00204     va_start(ap,fmt);
00205     q=vsnprintf(work+blen,s,fmt,ap);
00206     va_end(ap);
00207     if(q<s) {
00208       if(q>0) blen+=q;
00209       break;
00210       }
00211     // truncated
00212     q+=100;
00213     }
00214 }
00215 
00216 void cLineBuff::Strcat(const char *str)
00217 {
00218   if(str) Printf("%s",str);
00219 }
00220 
00221 void cLineBuff::Back(int n)
00222 {
00223   if(n>blen) blen=0; else blen-=n;
00224   if(work) work[blen]=0;
00225 }
00226 
00227 // -- cSimpleListBase --------------------------------------------------------------
00228 
00229 cSimpleListBase::cSimpleListBase(void)
00230 {
00231   first=last=0; count=0;
00232 }
00233 
00234 cSimpleListBase::~cSimpleListBase()
00235 {
00236   Clear();
00237 }
00238 
00239 void cSimpleListBase::Add(cSimpleItem *Item, cSimpleItem *After)
00240 {
00241   if(After) {
00242     Item->next=After->next;
00243     After->next=Item;
00244     }
00245   else {
00246     Item->next=0;
00247     if(last) last->next=Item;
00248     else first=Item;
00249     }
00250   if(!Item->next) last=Item;
00251   count++;
00252 }
00253 
00254 void cSimpleListBase::Ins(cSimpleItem *Item)
00255 {
00256   Item->next=first;
00257   first=Item;
00258   if(!Item->next) last=Item;
00259   count++;
00260 }
00261 
00262 void cSimpleListBase::Del(cSimpleItem *Item, bool Del)
00263 {
00264   if(first==Item) {
00265     first=Item->next;
00266     if(!first) last=0;
00267     count--;
00268     }
00269   else {
00270     cSimpleItem *item=first;
00271     while(item) {
00272       if(item->next==Item) {
00273         item->next=Item->next;
00274         if(!item->next) last=item;
00275         count--;
00276         break;
00277         }
00278       item=item->next;
00279       }
00280     }
00281   if(Del) delete Item;
00282 }
00283 
00284 void cSimpleListBase::Clear(void)
00285 {
00286   while(first) Del(first);
00287   first=last=0; count=0;
00288 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines