コード例 #1
0
        private static void LZ4_compressCtx_FrameHeader(
            byte[] dst,
            ref int dst_p,
            LZ4F_preferences_t prefs)
        {
            /* Magic Number */
            Poke4(dst, dst_p, MAGICNUMBER);
            dst_p += 4;

            /* FLG Byte */
            dst[dst_p++] = (byte)(((1 & _2BITS) << 6)    /* Version('01') */
                                  + (((byte)prefs.frameInfo.blockMode & _1BIT) << 5)
                                  + (((byte)prefs.frameInfo.blockChecksumFlag & _1BIT) << 4)
                                  + ((prefs.frameInfo.contentSize > 0 ? 1 : 0) << 3)
                                  + (((byte)prefs.frameInfo.contentChecksumFlag & _1BIT) << 2)
                                  + (prefs.frameInfo.dictID > 0 ? 1 : 0));

            /* BD Byte */
            dst[dst_p++] = (byte)(((byte)prefs.frameInfo.blockSizeID & _3BITS) << 4);

            /* Optional Frame content size field */
            if (prefs.frameInfo.contentSize != 0)
            {
                Poke8(dst, dst_p, prefs.frameInfo.contentSize);
                dst_p += 8;
                //cctxPtr->totalInSize = 0;
            }

            /* Optional dictionary ID field */
            if (prefs.frameInfo.dictID != 0)
            {
                Poke4(dst, dst_p, prefs.frameInfo.dictID);
                dst_p += 8;
            }

            /* Header CRC Byte */
            dst[dst_p] = LZ4F_headerChecksum(dst, 0, dst_p);
            dst_p++;
        }
コード例 #2
0
        private static int LZ4_compress64kCtx_safe32(
            ushort[] hash_table,
            byte[] src,
            byte[] dst,
            int src_0,
            int dst_0,
            int src_len,
            int dst_maxlen,
            ref LZ4F_preferences_t prefs)
        {
            unchecked
            {
                var debruijn32 = DEBRUIJN_TABLE_32;
                int _i;

                // ---- preprocessed source start here ----
                // r93
                var src_p       = src_0;
                var src_anchor  = src_p;
                var src_base    = src_p;
                var src_end     = src_p + src_len;
                var src_mflimit = src_end - MFLIMIT;

                var dst_p   = dst_0;
                var dst_end = dst_p + dst_maxlen;

                var src_LASTLITERALS   = src_end - LASTLITERALS;
                var src_LASTLITERALS_1 = src_LASTLITERALS - 1;

                var src_LASTLITERALS_STEPSIZE_1 = src_LASTLITERALS - (STEPSIZE_32 - 1);
                var dst_LASTLITERALS_1          = dst_end - (1 + LASTLITERALS);
                var dst_LASTLITERALS_3          = dst_end - (2 + 1 + LASTLITERALS);

                int len, length;

                uint h, h_fwd;

                // Init
                if (src_len < MINLENGTH)
                {
                    goto _last_literals;
                }

                // First Byte
                src_p++;
                h_fwd = (((Peek4(src, src_p)) * 2654435761u) >> HASH64K_ADJUST);

                // Main Loop
                while (true)
                {
                    var findMatchAttempts = (1 << SKIPSTRENGTH) + 3;
                    var src_p_fwd         = src_p;
                    int src_ref;
                    int dst_token;

                    // Find a match
                    do
                    {
                        h = h_fwd;
                        var step = findMatchAttempts++ >> SKIPSTRENGTH;
                        src_p     = src_p_fwd;
                        src_p_fwd = src_p + step;

                        if (src_p_fwd > src_mflimit)
                        {
                            goto _last_literals;
                        }

                        h_fwd         = (((Peek4(src, src_p_fwd)) * 2654435761u) >> HASH64K_ADJUST);
                        src_ref       = src_base + hash_table[h];
                        hash_table[h] = (ushort)(src_p - src_base);
                    } while (!Equal4(src, src_ref, src_p));

                    // Catch up
                    while ((src_p > src_anchor) && (src_ref > src_0) && (src[src_p - 1] == src[src_ref - 1]))
                    {
                        src_p--;
                        src_ref--;
                    }

                    // Encode Literal length
                    length    = (src_p - src_anchor);
                    dst_token = dst_p++;

                    if (dst_p + length + (length >> 8) > dst_LASTLITERALS_3)
                    {
                        return(0);                                                     // Check output limit
                    }
                    if (length >= RUN_MASK)
                    {
                        len            = length - RUN_MASK;
                        dst[dst_token] = (RUN_MASK << ML_BITS);
                        if (len > 254)
                        {
                            do
                            {
                                dst[dst_p++] = 255;
                                len         -= 255;
                            } while (len > 254);
                            dst[dst_p++] = (byte)len;
                            BlockCopy(src, src_anchor, dst, dst_p, length);
                            dst_p += length;
                            goto _next_match;
                        }
                        else
                        {
                            dst[dst_p++] = (byte)len;
                        }
                    }
                    else
                    {
                        dst[dst_token] = (byte)(length << ML_BITS);
                    }

                    // Copy Literals
                    if (length > 0)
                    {
                        _i = dst_p + length;
                        WildCopy(src, src_anchor, dst, dst_p, _i);
                        dst_p = _i;
                    }

_next_match:
                    // Encode Offset
                    Poke2(dst, dst_p, (ushort)(src_p - src_ref));
                    dst_p += 2;

                    // Start Counting
                    src_p     += MINMATCH;
                    src_ref   += MINMATCH; // MinMatch verified
                    src_anchor = src_p;

                    while (src_p < src_LASTLITERALS_STEPSIZE_1)
                    {
                        var diff = (int)Xor4(src, src_ref, src_p);
                        if (diff == 0)
                        {
                            src_p   += STEPSIZE_32;
                            src_ref += STEPSIZE_32;
                            continue;
                        }
                        src_p += debruijn32[((uint)((diff) & -(diff)) * 0x077CB531u) >> 27];
                        goto _endCount;
                    }

                    if ((src_p < src_LASTLITERALS_1) && (Equal2(src, src_ref, src_p)))
                    {
                        src_p   += 2;
                        src_ref += 2;
                    }
                    if ((src_p < src_LASTLITERALS) && (src[src_ref] == src[src_p]))
                    {
                        src_p++;
                    }

_endCount:

                    // Encode MatchLength
                    len = (src_p - src_anchor);

                    if (dst_p + (len >> 8) > dst_LASTLITERALS_1)
                    {
                        return(0);                                         // Check output limit
                    }
                    if (len >= ML_MASK)
                    {
                        dst[dst_token] += ML_MASK;
                        len            -= ML_MASK;
                        for (; len > 509; len -= 510)
                        {
                            dst[dst_p++] = 255;
                            dst[dst_p++] = 255;
                        }
                        if (len > 254)
                        {
                            len         -= 255;
                            dst[dst_p++] = 255;
                        }
                        dst[dst_p++] = (byte)len;
                    }
                    else
                    {
                        dst[dst_token] += (byte)len;
                    }

                    // Test end of chunk
                    if (src_p > src_mflimit)
                    {
                        src_anchor = src_p;
                        break;
                    }

                    // Fill table
                    hash_table[(((Peek4(src, src_p - 2)) * 2654435761u) >> HASH64K_ADJUST)] = (ushort)(src_p - 2 - src_base);

                    // Test next position

                    h             = (((Peek4(src, src_p)) * 2654435761u) >> HASH64K_ADJUST);
                    src_ref       = src_base + hash_table[h];
                    hash_table[h] = (ushort)(src_p - src_base);

                    if (Equal4(src, src_ref, src_p))
                    {
                        dst_token      = dst_p++;
                        dst[dst_token] = 0;
                        goto _next_match;
                    }

                    // Prepare next loop
                    src_anchor = src_p++;
                    h_fwd      = (((Peek4(src, src_p)) * 2654435761u) >> HASH64K_ADJUST);
                }

_last_literals:
                // Encode Last Literals
                var lastRun = (src_end - src_anchor);
                if (dst_p + lastRun + 1 + (lastRun - RUN_MASK + 255) / 255 > dst_end)
                {
                    return(0);
                }
                if (lastRun >= RUN_MASK)
                {
                    dst[dst_p++] = (RUN_MASK << ML_BITS);
                    lastRun     -= RUN_MASK;
                    for (; lastRun > 254; lastRun -= 255)
                    {
                        dst[dst_p++] = 255;
                    }
                    dst[dst_p++] = (byte)lastRun;
                }
                else
                {
                    dst[dst_p++] = (byte)(lastRun << ML_BITS);
                }
                BlockCopy(src, src_anchor, dst, dst_p, src_end - src_anchor);
                dst_p += src_end - src_anchor;

                // End
                return((dst_p) - dst_0);
            }
        }
コード例 #3
0
 private static void LZ4_compressCtx_FrameFooter(
     byte[] dst,
     ref int dst_p,
     LZ4F_preferences_t prefs)
 {
 }