Конвертация C++ в C#

Узнай цену своей работы

Формулировка задачи:

Всем привет, не знаю C++, но очень нужно конвертировать данный код, помогите пожалуйста
//---------------------------------------------------------------------------
 
#ifndef MPPCcompressionHPP
#define MPPCcompressionHPP
 
//---------------------------------------------------------------------------
namespace mppc
{
#define MPPE_HIST_LEN          8192
#define HASH(x)     (((40543*(((((x)[0]<<4)^(x)[1])<<4)^(x)[2]))>>4) & 0x1fff)
 
//---------------------------------------------------------------------------
static __inline void lamecopy(unsigned char* dst, unsigned char* src, unsigned int len)
{
while(len-- > 0)
  *dst++ = *src++;
}
//---------------------------------------------------------------------------
static unsigned __int32 __inline
getbits(const unsigned __int8 *buf, const unsigned __int32 n, unsigned __int32 *i, unsigned __int32 *l)
{
    static const unsigned __int32 m[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
    unsigned __int32 res, ol;
 
    ol = *l;
    if (*l >= n) {
    *l = (*l) - n;
    res = (buf[*i] & m[ol]) >> (*l);
    if (*l == 0) {
        *l = 8;
        (*i)++;
    }
    } else {
    *l = 8 - n + (*l);
    res = (buf[(*i)++] & m[ol]) << 8;
    res = (res | buf[*i]) >> (*l);
    }
 
    return res;
}
//---------------------------------------------------------------------------
unsigned __int32 __inline
getbyte(const unsigned __int8 *buf, const unsigned __int32 i, const unsigned __int32 l)
{
    if (l == 8) {
    return buf[i];
    } else {
    return (((buf[i] << 8) | buf[i+1]) >> l) & 0xff;
    }
}
//---------------------------------------------------------------------------
struct MPPC_decomp_state
 {
 unsigned __int8    hist[2*MPPE_HIST_LEN];    // 8192 * 2 = 16384
 unsigned __int16   histptr;                  // 2
 };
//---------------------------------------------------------------------------
int mppc_decompress(unsigned char *ibuf, unsigned char *obuf, int isize, int osize)
{
if(isize > 9217)
 return -1;
 
MPPC_decomp_state history;
memset(&history, 0, sizeof(history));
MPPC_decomp_state *state = &history;
 
unsigned __int32 olen, off, len, bits, val, sig, i, l;
unsigned __int8 *hist, *s;
unsigned char *isrc = ibuf;
 
hist = state->hist + state->histptr;
olen = len = i = 0;
l = 8;
bits = isize * 8;
while(bits >= 8)
 {
 val = getbyte(isrc, i++, l);
 if(val < 0x80)
  {
  if(state->histptr < 2*MPPE_HIST_LEN)
   (state->hist)[(state->histptr)++] = (unsigned __int8) val;
  else
   return -1;
 
  olen++;
  bits -= 8;
  continue;
  }
 
 sig = val & 0xc0; // c0
 if(sig == 0x80)
  {
  if (state->histptr < 2*MPPE_HIST_LEN)
  (state->hist)[(state->histptr)++] =
     (unsigned __int8) (0x80|((val&0x3f)<<1)|getbits(isrc, 1 , &i ,&l));
  else
   return -1;
 
  olen++;
  bits -= 9;
  continue;
  }
 
    sig = val & 0xf0;
    if (sig == 0xf0) {      //10-bit offset; 0 <= offset < 64
        off = (((val&0x0f)<<2)|getbits(isrc, 2 , &i ,&l));
        bits -= 10;
    } else {
        if (sig == 0xe0) {      // 12-bit offset; 64 <= offset < 320
        off = ((((val&0x0f)<<4)|getbits(isrc, 4 , &i ,&l))+64);
        bits -= 12;
        } else {
        if ((sig&0xe0) == 0xc0) {// 16-bit offset; 320 <= offset < 8192
            off = ((((val&0x1f)<<8)|getbyte(isrc, i++, l))+320);
            bits -= 16;
            if (off > MPPE_HIST_LEN - 1) {
            return -1;
            }
        } else {
            return -1;
        }
        }
    }
    // decode length of match
    val = getbyte(isrc, i, l);
    if ((val & 0x80) == 0x00) {         // len = 3
        len = 3;
        bits--;
        getbits(isrc, 1 , &i ,&l);
    } else if ((val & 0xc0) == 0x80) {      // 4 <= len < 8
        len = 0x04 | ((val>>4) & 0x03);
        bits -= 4;
        getbits(isrc, 4 , &i ,&l);
    } else if ((val & 0xe0) == 0xc0) {      // 8 <= len < 16
        len = 0x08 | ((val>>2) & 0x07);
        bits -= 6;
        getbits(isrc, 6 , &i ,&l);
    } else if ((val & 0xf0) == 0xe0) {      // 16 <= len < 32
        len = 0x10 | (val & 0x0f);
        bits -= 8;
        i++;
    } else {
        bits -= 8;
        val = (val << 8) | getbyte(isrc, ++i, l);
        if ((val & 0xf800) == 0xf000) {     // 32 <= len < 64
        len = 0x0020 | ((val >> 6) & 0x001f);
        bits -= 2;
        getbits(isrc, 2 , &i ,&l);
        } else if ((val & 0xfc00) == 0xf800) {  // 64 <= len < 128
        len = 0x0040 | ((val >> 4) & 0x003f);
        bits -= 4;
        getbits(isrc, 4 , &i ,&l);
        } else if ((val & 0xfe00) == 0xfc00) {  // 128 <= len < 256
        len = 0x0080 | ((val >> 2) & 0x007f);
        bits -= 6;
        getbits(isrc, 6 , &i ,&l);
        } else if ((val & 0xff00) == 0xfe00) {  // 256 <= len < 512
        len = 0x0100 | (val & 0x00ff);
        bits -= 8;
        i++;
        } else {
        bits -= 8;
        val = (val << 8) | getbyte(isrc, ++i, l);
        if ((val & 0xff8000) == 0xff0000) { // 512 <= len < 1024
            len = 0x000200 | ((val >> 6) & 0x0001ff);
            bits -= 2;
            getbits(isrc, 2 , &i ,&l);
        } else if ((val & 0xffc000) == 0xff8000) {// 1024 <= len < 2048
            len = 0x000400 | ((val >> 4) & 0x0003ff);
            bits -= 4;
            getbits(isrc, 4 , &i ,&l);
        } else if ((val & 0xffe000) == 0xffc000) {// 2048 <= len < 4096
            len = 0x000800 | ((val >> 2) & 0x0007ff);
            bits -= 6;
            getbits(isrc, 6 , &i ,&l);
        } else if ((val & 0xfff000) == 0xffe000) {// 4096 <= len < 8192
            len = 0x001000 | (val & 0x000fff);
            bits -= 8;
            i++;
        } else {                // this shouldn't happen
            return -1;
        }
        }
    }
 
 s = state->hist + state->histptr;
 state->histptr += len;
 olen += len;
 
 if(state->histptr < 2*MPPE_HIST_LEN)
  lamecopy(s, s - off, len);
 else
  return -1;
 
 }
 
len = olen;
if(len <= osize)
 {
 memcpy(obuf, hist, olen);
 return len;
 }
 
return -1;
}
//---------------------------------------------------------------------------
bool _export uncompress_small(void *dest, int dest_len, void *source, int source_len)
{
static int tmpx  = 0;
tmpx++;
int res = mppc_decompress((unsigned char*)source, (unsigned char*)dest, source_len, dest_len);
if(res < 0 && dest_len != res)
 return false;
return true;
}
//---------------------------------------------------------------------------
bool _export uncompress_big(char *dest, int dest_len, char *source, int source_len)
{
while(dest_len > 0 && source_len > 2)
 {
 short tmask_len = *(short*)source;
 short t_len = (tmask_len & 0x7FFF);
 if(t_len <= 0 || (t_len + 2) > source_len || t_len > MPPE_HIST_LEN)
  return false;
 
 if(tmask_len >= 0)
  {
  if(t_len > dest_len)return false;
  memcpy(dest, &source[2], t_len);
  dest += t_len;
  dest_len -= t_len;
  }
 else
  {
  int unpcku = (dest_len >= MPPE_HIST_LEN ? MPPE_HIST_LEN : dest_len);
  int res = mppc_decompress((unsigned char*)&source[2], (unsigned char*)dest, t_len, unpcku);
  if(res <= 0 || res > dest_len || res > MPPE_HIST_LEN )
   return false;
  dest += res;
  dest_len -= res;
  }
 
 source += (2 + t_len);
 source_len -= (2 + t_len);
 }
 
if(source_len)
 return false;
return true;
}
//---------------------------------------------------------------------------
unsigned int compressBound(unsigned int sourcelen)
{
return (9 * sourcelen >> 3) + 6;
}
//---------------------------------------------------------------------------
bool _export uncompress(void *dest, int dest_len, void *source, int source_len)
{
if(dest_len <= MPPE_HIST_LEN)
 return uncompress_small(dest,dest_len,source,source_len);
return uncompress_big((char*)dest,dest_len,(char*)source,source_len);
}
//---------------------------------------------------------------------------

Решение задачи: «Конвертация C++ в C#»

textual
Листинг программы
//---------------------------------------------------------------------------
struct MPPC_comp_state
 {
 unsigned __int8    hist[2*MPPE_HIST_LEN];    // 8192 * 2 = 16384
 unsigned __int16   histptr;                  // 2
 unsigned __int16   hash[MPPE_HIST_LEN];      // 8192 * 2 = 16384 [32770]
 };
//---------------------------------------------------------------------------
static void __inline 
putbits8(unsigned __int8 *buf, unsigned __int32 val, const unsigned __int32 n, unsigned __int32 *i, unsigned __int32 *l)
{
    buf += *i;
    if (*l >= n) {
    *l = (*l) - n;
    val <<= *l;
    *buf = *buf | (val & 0xff);
    if (*l == 0) {
        *l = 8;
        (*i)++;
        *(++buf) = 0;
    }
    } else {
    (*i)++;
    *l = 8 - n + (*l);
    val <<= *l;
    *buf = *buf | ((val >> 8) & 0xff);
    *(++buf) = val & 0xff;
    }
}
//---------------------------------------------------------------------------
static void __inline 
putbits16(unsigned __int8 *buf, unsigned __int32 val, const unsigned __int32 n, unsigned __int32 *i, unsigned __int32 *l)
{
    buf += *i;
    if (*l >= n - 8) {
    (*i)++;
    *l = 8 - n + (*l);
    val <<= *l;
    *buf = *buf | ((val >> 8) & 0xff);
    *(++buf) = val & 0xff;
    if (*l == 0) {
        *l = 8;
        (*i)++;
        *(++buf) = 0;
    }
    } else {
    (*i)++; (*i)++;
    *l = 16 - n + (*l);
    val <<= *l;
    *buf = *buf | ((val >> 16) & 0xff);
    *(++buf) = (val >> 8) & 0xff;
    *(++buf) = val & 0xff;
    }
}
//---------------------------------------------------------------------------
static void __inline 
putbits24(unsigned __int8 *buf, unsigned __int32 val, const unsigned __int32 n, unsigned __int32 *i, unsigned __int32 *l)
{
    buf += *i;
    if (*l >= n - 16) {
    (*i)++; (*i)++;
    *l = 16 - n + (*l);
    val <<= *l;
    *buf = *buf | ((val >> 16) & 0xff);
    *(++buf) = (val >> 8) & 0xff;
    *(++buf) = val & 0xff;
    if (*l == 0) {
        *l = 8;
        (*i)++;
        *(++buf) = 0;
    }
    } else {
    (*i)++; (*i)++; (*i)++;
    *l = 24 - n + (*l);
    val <<= *l;
    *buf = *buf | ((val >> 24) & 0xff);
    *(++buf) = (val >> 16) & 0xff;
    *(++buf) = (val >> 8) & 0xff;
    *(++buf) = val & 0xff;
    }
}
//---------------------------------------------------------------------------
int _export MPPC_Compress(unsigned char *src, unsigned char *dst, unsigned long srcCnt, unsigned long dstCnt)
{
MPPC_comp_state history;
memset(&history, 0, sizeof(history));
history.histptr = MPPE_HIST_LEN;
MPPC_comp_state *state = &history;
 
unsigned __int32 olen, off, len, idx, i, l;
unsigned __int8 *hist, *sbuf, *p, *q, *r, *s;
 
 
if(dstCnt < (srcCnt * 9 / 8 + 2))
 return -3;
 
if(srcCnt > MPPE_HIST_LEN)
 return -2;
 
hist = state->hist + MPPE_HIST_LEN;
 
/* check if there is enough room at the end of the history */
if(state->histptr + srcCnt >= 2*MPPE_HIST_LEN)
 {
 state->histptr = MPPE_HIST_LEN;
 memcpy(state->hist, hist, MPPE_HIST_LEN);
 }
 
/* add packet to the history */
sbuf = state->hist + state->histptr;
memcpy(sbuf, src, srcCnt);
state->histptr += srcCnt;
 
    /* compress data */
    r = sbuf + srcCnt;
    *dst = olen = i = 0;
    l = 8;
    while (i < srcCnt - 2) {
    s = q = sbuf + i;
 
    /* Prognose matching position using hash function */
    idx = HASH(s);
    p = hist + state->hash[idx];
    state->hash[idx] = (unsigned __int16) (s - hist);
    if (p > s)  /* It was before MPPC_RESTART_HISTORY */
        p -= MPPE_HIST_LEN; /* try previous history buffer */
    off = s - p;
 
    /* Check our prognosis */
    if (off > MPPE_HIST_LEN - 1 || off < 1 || *p++ != *s++ || *p++ != *s++ ||
        *p++ != *s++) {
        /* no match found; encode literal byte */
        if (src[i] < 0x80) {        /* literal byte < 0x80 */
        putbits8(dst, (unsigned __int32) src[i], 8, &olen, &l);
        } else {                /* literal byte >= 0x80 */
        putbits16(dst, (unsigned __int32) (0x100|(src[i]&0x7f)), 9, &olen, &l);
        }
        ++i;
        continue;
    }
 
    /* Find length of the matching fragment */
#if defined(__amd64__) || defined(__i386__)
    /* Optimization for CPUs without strict data aligning requirements */
    while ((*((unsigned __int32*)p) == *((unsigned __int32*)s)) && (s < (r - 3))) {
        p+=4;
        s+=4;
    }
#endif
    while((*p++ == *s++) && (s <= r));
    len = s - q - 1;
    i += len;
 
    /* at least 3 character match found; code data */
    /* encode offset */
    if (off < 64) {         /* 10-bit offset; 0 <= offset < 64 */
        putbits16(dst, 0x3c0|off, 10, &olen, &l);
    } else if (off < 320) {     /* 12-bit offset; 64 <= offset < 320 */
        putbits16(dst, 0xe00|(off-64), 12, &olen, &l);
    } else if (off < 8192) {    /* 16-bit offset; 320 <= offset < 8192 */
        putbits16(dst, 0xc000|(off-320), 16, &olen, &l);
    } else {
        return -1;
    }
 
    /* encode length of match */
    if (len < 4) {          /* length = 3 */
        putbits8(dst, 0, 1, &olen, &l);
    } else if (len < 8) {       /* 4 <= length < 8 */
        putbits8(dst, 0x08|(len&0x03), 4, &olen, &l);
    } else if (len < 16) {      /* 8 <= length < 16 */
        putbits8(dst, 0x30|(len&0x07), 6, &olen, &l);
    } else if (len < 32) {      /* 16 <= length < 32 */
        putbits8(dst, 0xe0|(len&0x0f), 8, &olen, &l);
    } else if (len < 64) {      /* 32 <= length < 64 */
        putbits16(dst, 0x3c0|(len&0x1f), 10, &olen, &l);
    } else if (len < 128) {     /* 64 <= length < 128 */
        putbits16(dst, 0xf80|(len&0x3f), 12, &olen, &l);
    } else if (len < 256) {     /* 128 <= length < 256 */
        putbits16(dst, 0x3f00|(len&0x7f), 14, &olen, &l);
    } else if (len < 512) {     /* 256 <= length < 512 */
        putbits16(dst, 0xfe00|(len&0xff), 16, &olen, &l);
    } else if (len < 1024) {    /* 512 <= length < 1024 */
        putbits24(dst, 0x3fc00|(len&0x1ff), 18, &olen, &l);
    } else if (len < 2048) {    /* 1024 <= length < 2048 */
        putbits24(dst, 0xff800|(len&0x3ff), 20, &olen, &l);
    } else if (len < 4096) {    /* 2048 <= length < 4096 */
        putbits24(dst, 0x3ff000|(len&0x7ff), 22, &olen, &l);
    } else if (len < 8192) {    /* 4096 <= length < 8192 */
        putbits24(dst, 0xffe000|(len&0xfff), 24, &olen, &l);
    } else {
         return -1;
    }
    }
 
/* Add remaining octets to the output */
while(srcCnt - i > 0)
 {
 if(src[i] < 0x80) {    /* literal byte < 0x80 */
        putbits8(dst, (unsigned __int32)src[i++], 8, &olen, &l);
 } else {       /* literal byte >= 0x80 */
        putbits16(dst, (unsigned __int32) (0x100|(src[i++]&0x7f)), 9, &olen, &l);
    }
 }
 
/* Reset unused bits of the last output octet */
if ((l != 0) && (l != 8))
 putbits8(dst, 0, l, &olen, &l);
 
return olen;
}
//---------------------------------------------------------------------------
bool _export compress(void **dest, int *dest_len, void *source, int source_len)
{
if(!dest || !dest_len)return false;
*dest = 0;
*dest_len = 0;
unsigned int bn_destlen = compressBound(source_len);
if(bn_destlen < 1)return true;
char *buffer = (char*)malloc(bn_destlen);
if(!buffer)return false;
 
if(source_len <= MPPE_HIST_LEN)
 {
 int res = MPPC_Compress((unsigned char*)source, buffer, source_len, bn_destlen);
 if(res < 1)
  {
  free(buffer);
  return false;
  }
 *dest_len = res;
 char *tmp = (char*)malloc(res);
 if(tmp)
  {
  memcpy(tmp,buffer,res);
  free(buffer);
  *dest = tmp;
  }
 else
  {
  *dest = buffer;
  }
 return true;
 }
 
int pos = 0;
while(source_len > 0 && pos < bn_destlen)
 {
 short *pck_size = (short*)&buffer[pos];
 pos += 2;
 
 int src_len = (source_len >= (MPPE_HIST_LEN) ? (MPPE_HIST_LEN) : source_len);
 int res = MPPC_Compress((unsigned char*)source, &buffer[pos], src_len, (bn_destlen - pos));
 if(res < 1)
  {
  free(buffer);
  return false;
  }
 
 if(res >= src_len)
  {
  memcpy(&buffer[pos], source, src_len);
  res = src_len;
  *pck_size = res;
  }
 else
  {
  *pck_size = (res | 0x8000);
  }
 
 pos += res;
 ((char*)source) += src_len;
 source_len -= src_len;
 }
 
*dest = buffer;
*dest_len = pos;
return true;
}
//---------------------------------------------------------------------------
bool _export compress_q(void *dest, int *dest_len, void *source, int source_len)
{
if(!dest || !dest_len)return false;
*dest_len = 0;
unsigned int bn_destlen = compressBound(source_len);
if(bn_destlen < 1)return true;
 
if(source_len <= MPPE_HIST_LEN)
 {
 int res = MPPC_Compress((unsigned char*)source, (unsigned char*)dest, source_len, bn_destlen);
 if(res < 1)return false;
 
 *dest_len = res;
 return true;
 }
 
 
 
return false;
}
//---------------------------------------------------------------------------
}
//---------------------------------------------------------------------------
#endif

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

10   голосов , оценка 3.8 из 5