00001
00008 #ifndef DKUTIL_C_BIT_H
00009 #define DKUTIL_C_BIT_H
00010
00011 #include "dkcMemoryStream.h"
00012 #include "dkcSJISFileSystem.h"
00013
00014 #define DKUTIL_C_USE_BIT_TABLE 0
00015
00016
00021 union dkc_NLZ8Byte{
00022 uint32 asINT_[2];
00023 double asDouble_;
00024 };
00028 union dkc_NLZ4Byte{
00029 uint32 asINT_;
00030 float asFloat;
00031 };
00032
00034
00035
00036 DKC_FORCE_INLINE size_t dkcNLZ_IEEE(uint32 arg){
00037 union dkc_NLZ8Byte t;
00038 size_t n;
00039
00040 t.asDouble_ = (double)arg + 0.5;
00041 n = 1054 - (t.asINT_[dkcdLITTLE_ENDIAN] >> 20);
00042 return n;
00043 }
00045 DKC_FORCE_INLINE size_t
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 x = x - ((x >> 1) & 0x55555555);
00083 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
00084 x = (x + (x >> 4)) & 0x0f0f0f0f;
00085 x = x + (x >> 8);
00086 x = x + (x >> 16);
00087 return x & 0x0000003f;
00088 }
00090 #define dkcNumOfBits( bits) dkcPOP(bits)
00091
00092
00094 DKC_FORCE_INLINE size_t dkcNTZ(uint32 x){
00095 return 32 - dkcNTZ(~x&(x-1));
00096
00097 }
00098
00099
00100 #if DKUTIL_C_USE_BIT_TABLE == 1
00101 DKUTIL_EXTERN const uint8 dkccvBitTable8[8];
00102 DKUTIL_EXTERN const uint32 dkccvBitTable16[16];
00103 DKUTIL_EXTERN const uint32 dkccvBitTable32[32];
00104 DKUTIL_EXTERN const uint64 dkccvBitTable64[64]
00105 #endif
00106
00107
00108 enum edkcBitMemoryStream{
00110 edkcBitMemoryStreamSeekCurrent = edkcSeekCurrent,
00112 edkcBitMemoryStreamSeekEnd = edkcSeekEnd,
00114 edkcBitMemoryStreamSeekSet = edkcSeekSet,
00115 };
00116
00117
00119 typedef struct dkc_BitMemoryStream{
00120 int read_count;
00121 size_t read_buff;
00122 int write_count;
00123 size_t write_buff;
00124 DKC_MEMORYSTREAM *pms;
00125 }DKC_BIT_MEMORYSTREAM;
00126
00127 DKC_INLINE DKC_BIT_MEMORYSTREAM *dkcAllocBitMemoryStream(size_t def_size)
00128 {
00129 DKC_BIT_MEMORYSTREAM *p = (DKC_BIT_MEMORYSTREAM *)dkcAllocate(sizeof(DKC_BIT_MEMORYSTREAM));
00130 if(NULL==p) return NULL;
00131 p->pms = dkcAllocMemoryStream(def_size);
00132 if(NULL==p->pms)
00133 goto End;
00134
00135 p->write_count = 8;
00136
00137 return p;
00138 End:
00139 dkcFree((void **)&p);
00140 return NULL;
00141 }
00142
00143 DKC_INLINE int dkcFreeBitMemoryStream(DKC_BIT_MEMORYSTREAM **p)
00144 {
00145 dkcmNOT_ASSERT(NULL==p || NULL==*p || NULL==(*p)->pms);
00146 dkcFreeMemoryStream(&((*p)->pms));
00147 return dkcFree((void **)p);
00148 }
00152 DKC_INLINE int dkcBitMemoryStreamWrite(DKC_BIT_MEMORYSTREAM *p,void *target_arg,int n)
00153 {
00154 int wc = p->write_count;
00155 size_t wb = p->write_buff;
00156
00157 size_t sp = dkcMemoryStreamGetSeekPoint(p->pms);
00158 uint32 *target =(uint32 *) target_arg;
00159 if( n <= 0 || n >= 32 ) return edk_FAILED;
00160 while( --n >= 0 ){
00161 --wc;
00162 #if DKUTIL_C_USE_BIT_TABLE == 1
00163 if( dkccvBitTable32[n] & target ){
00164 wb |= dkccvBitTable8[wc];
00165 }
00166 #else
00167 if( (1<<n) & (*target) ){
00168 wb |= (1 << wc);
00169 }
00170 #endif
00171 if( !wc ){
00172 if( DKUTIL_FAILED(dkcMemoryStreamDynamicWrite(p->pms, &wb,1 )) )
00173 {
00174
00175 dkcMemoryStreamSeek(p->pms,sp,edkcMemoryStreamSeekSet);
00176 return edk_FAILED;
00177 }
00178 wc = 8;
00179 wb = 0;
00180 }
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 p->write_count = wc;
00193 p->write_buff = wb;
00194 return edk_SUCCEEDED;
00195
00196 }
00197
00198 DKC_INLINE int dkcBitMemoryStreamWriteLast(DKC_BIT_MEMORYSTREAM *p){
00199
00200 size_t sp = dkcMemoryStreamGetSeekPoint(p->pms);
00201 int r = dkcMemoryStreamDynamicWrite(p->pms, &p->write_buff,1 );
00202 if( DKUTIL_FAILED(r) )
00203 {
00204
00205 dkcMemoryStreamSeek(p->pms,sp,edkcMemoryStreamSeekSet);
00206 return r;
00207 }
00208 p->write_count = 8;
00209 p->write_buff = 0;
00210 return r;
00211 }
00212
00213 #if 0
00214
00218
00219 DKC_INLINE int dkcBitMemoryStreamRead(DKC_BIT_MEMORYSTREAM *p,void *res_arg,int n)
00220 {
00221 int rc = p->read_count;
00222 size_t rb = p->read_buff;
00223 size_t result = 0;
00224
00225
00226 size_t sp = dkcMemoryStreamGetSeekPoint(p->pms);
00227 uint32 *res = (uint32 *)res_arg;
00228 if( n <= 0 || n >= 32 ) return edk_FAILED;
00229 while( --n >= 0 ){
00230 if( --rc < 0 ){
00231
00232
00233 if( DKUTIL_FAILED(dkcMemoryStreamGet8(p->pms,(uint8 *)&rb))){
00234
00235 dkcMemoryStreamSeek(p->pms,sp,edkcMemoryStreamSeekSet);
00236 return edk_FAILED;
00237 }
00238 rc = 7;
00239 }
00240 #if DKUTIL_C_USE_BIT_TABLE == 1
00241 if( dkccvBitTable8[rc] & rb ){
00242 result |= dkccvBitTable32[n];
00243 }
00244 #else
00245 if( (1 << rc) & rb){
00246 result |= (1 << n);
00247 }
00248 #endif
00249 }
00250 *res = result;
00251 p->read_count = rc;
00252 p->read_buff = rb;
00253 return edk_SUCCEEDED;
00254
00255
00256 }
00257
00258 #else
00259
00260 DKC_INLINE int dkcBitMemoryStreamReadBase(DKC_BIT_MEMORYSTREAM *p,void *res_arg,int n,BOOL as_possible_as_much,int *pcount)
00261 {
00262 int rc = p->read_count;
00263 size_t rb = p->read_buff;
00264 size_t result = 0;
00265 int cc = 0;
00266
00267
00268 size_t sp = dkcMemoryStreamGetSeekPoint(p->pms);
00269 uint32 *res = (uint32 *)res_arg;
00270 if( n <= 0 || n >= 32 ) return edk_FAILED;
00271 {
00272 while( --n >= 0 ){
00273 cc++;
00274 if( --rc < 0 ){
00275
00276
00277 if( DKUTIL_FAILED(dkcMemoryStreamGet8(p->pms,(uint8 *)&rb))){
00278 if(as_possible_as_much)
00279 goto End;
00280
00281 dkcMemoryStreamSeek(p->pms,sp,edkcMemoryStreamSeekSet);
00282 return edk_FAILED;
00283 }
00284 rc = 7;
00285 }
00286 #if DKUTIL_C_USE_BIT_TABLE == 1
00287 if( dkccvBitTable8[rc] & rb ){
00288 result |= dkccvBitTable32[n];
00289 }
00290 #else
00291 if( (1 << rc) & rb){
00292 result |= (1 << n);
00293 }
00294 #endif
00295 }
00296 End:
00297 *res = result;
00298 p->read_count = rc;
00299 p->read_buff = rb;
00300 *pcount = cc;
00301 }
00302 return edk_SUCCEEDED;
00303 }
00304 DKC_INLINE int dkcBitMemoryStreamRead(DKC_BIT_MEMORYSTREAM *p,void *res_arg,int n)
00305 {
00306 int ressize;
00307 return dkcBitMemoryStreamReadBase(p,res_arg,n,FALSE,&ressize);
00308 }
00309
00310 #endif
00311
00313 DKC_INLINE int WINAPI dkcBitMemoryStreamReadLast(DKC_BIT_MEMORYSTREAM *p,uint32 *res,int *ressize)
00314 {
00315 if(p->read_count <= 0){
00317 return dkcBitMemoryStreamReadBase(p,
00318 res,32,TRUE,ressize);
00319 }
00320 *res = p->read_buff;
00321 *ressize = p->read_count;
00322 p->read_count = p->read_buff = 0;
00323 return edk_SUCCEEDED;
00324 }
00325
00326
00327
00328 #define dkcBitMemoryStreamGetMemoryStreamObj(p) p->pms
00329
00330 DKC_INLINE int WINAPI dkcBitMemoryStreamSeekByte(DKC_BIT_MEMORYSTREAM *ptr,int offset,int origin)
00331 {
00332 ptr->read_count = ptr->read_buff = ptr->write_buff = 0;
00333
00334 ptr->write_count = 8;
00335 return dkcMemoryStreamSeek(ptr->pms,offset,origin);
00336 }
00337
00339 #define dkcBitMemoryStreamTellByte(ptr) dkcMemoryStreamTell(ptr->pms)
00340
00341 DKC_INLINE int WINAPI dkcBitMemoryStreamTellBitCheck(DKC_BIT_MEMORYSTREAM *ptr){
00342 size_t t = dkcBitMemoryStreamTellByte(ptr);
00343 if(t > UINT_MAX / 8){
00344 return edk_VariableOverFlow;
00345 }
00346 return edk_SUCCEEDED;
00347 }
00349 DKC_INLINE size_t WINAPI dkcBitMemoryStreamTellBit(DKC_BIT_MEMORYSTREAM *ptr,size_t *bit)
00350 {
00351 size_t t = dkcBitMemoryStreamTellByte(ptr);
00352 dkcmNOT_ASSERT(t > UINT_MAX / 8);
00353 return t * 8;
00354 }
00356 #define dkcBitMemoryStreamWriteToMemory(ptr,dest,size,flag) dkcMemoryStreamWriteToMemory(ptr->pms,dest,size,flag)
00357
00358 #define dkcBitMemoryStreamWriteToMemoryLast(ptr,dest,size,flag) dkcBitMemoryStreamWriteLast(ptr);dkcBitMemoryStreamWriteToMemory(ptr,dest,size,flag)
00359
00360 #define dkcBitMemoryStreamDump(ptr,filename,flag) dkcMemoryStreamDump(ptr->pms,filename,flag)
00361
00362 #define dkcBitMemoryStreamLoadFromFile(ptr,filename,permit_size,flag) dkcMemoryStreamLoadFromFile(ptr->pms,filename,permit_size,flag)
00363
00364 #define dkcBitMemoryStreamLoadFromMemory(ptr,buffer,buffsize) dkcMemoryStreamLoadFromMemory(ptr->pms,buffer,buffsize)
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431 #endif //end of include once