Merge branch 'xcrush' of github.com:awakecoding/FreeRDP

This commit is contained in:
Marc-André Moreau 2014-05-08 16:11:55 -04:00
commit 1c081c965f
9 changed files with 495 additions and 4 deletions

View File

@ -0,0 +1,40 @@
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* Bulk Data Compression
*
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FREERDP_CODEC_BULK_H
#define FREERDP_CODEC_BULK_H
#include <freerdp/api.h>
#include <freerdp/types.h>
/* Level-2 Compression Flags */
#define PACKET_COMPRESSED 0x20
#define PACKET_AT_FRONT 0x40
#define PACKET_FLUSHED 0x80
/* Level-1 Compression Flags */
#define L1_PACKET_AT_FRONT 0x04
#define L1_NO_COMPRESSION 0x02
#define L1_COMPRESSED 0x01
#define L1_INNER_COMPRESSION 0x10
#endif /* FREERDP_CODEC_BULK_H */

View File

@ -25,9 +25,7 @@
#include <winpr/bitstream.h>
#define PACKET_COMPRESSED 0x20
#define PACKET_AT_FRONT 0x40
#define PACKET_FLUSHED 0x80
#include <freerdp/codec/bulk.h>
struct _MPPC_CONTEXT
{

View File

@ -0,0 +1,75 @@
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* XCrush (RDP6.1) Bulk Data Compression
*
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FREERDP_CODEC_XCRUSH_H
#define FREERDP_CODEC_XCRUSH_H
#include <freerdp/api.h>
#include <freerdp/types.h>
#include <freerdp/codec/mppc.h>
struct _RDP61_MATCH_DETAILS
{
UINT16 MatchLength;
UINT16 MatchOutputOffset;
UINT32 MatchHistoryOffset;
};
typedef struct _RDP61_MATCH_DETAILS RDP61_MATCH_DETAILS;
struct _RDP61_COMPRESSED_DATA
{
BYTE Level1ComprFlags;
BYTE Level2ComprFlags;
UINT16 MatchCount;
RDP61_MATCH_DETAILS* MatchDetails;
BYTE* Literals;
};
typedef struct _RDP61_COMPRESSED_DATA RDP61_COMPRESSED_DATA;
struct _XCRUSH_CONTEXT
{
BOOL Compressor;
MPPC_CONTEXT* mppc;
BYTE* HistoryPtr;
UINT32 HistoryOffset;
UINT32 HistoryBufferSize;
BYTE HistoryBuffer[2000000];
BYTE BlockBuffer[16384];
};
typedef struct _XCRUSH_CONTEXT XCRUSH_CONTEXT;
#ifdef __cplusplus
extern "C" {
#endif
FREERDP_API int xcrush_compress(XCRUSH_CONTEXT* xcrush, BYTE* pSrcData, UINT32 SrcSize, BYTE** ppDstData, UINT32* pDstSize, UINT32* pFlags);
FREERDP_API int xcrush_decompress(XCRUSH_CONTEXT* xcrush, BYTE* pSrcData, UINT32 SrcSize, BYTE** ppDstData, UINT32* pDstSize, UINT32 flags);
FREERDP_API void xcrush_context_reset(XCRUSH_CONTEXT* xcrush);
FREERDP_API XCRUSH_CONTEXT* xcrush_context_new(BOOL Compressor);
FREERDP_API void xcrush_context_free(XCRUSH_CONTEXT* xcrush);
#ifdef __cplusplus
}
#endif
#endif /* FREERDP_CODEC_XCRUSH_H */

View File

@ -48,6 +48,7 @@ set(${MODULE_PREFIX}_SRCS
nsc_encode.h
nsc_types.h
ncrush.c
xcrush.c
mppc.c
jpeg.c)

View File

@ -8,6 +8,7 @@ set(${MODULE_PREFIX}_TESTS
TestFreeRDPRegion.c
TestFreeRDPCodecMppc.c
TestFreeRDPCodecNCrush.c
TestFreeRDPCodecXCrush.c
TestFreeRDPCodecPlanar.c
TestFreeRDPCodecRemoteFX.c)

View File

@ -0,0 +1,10 @@
#include <winpr/crt.h>
#include <winpr/print.h>
#include <freerdp/codec/xcrush.h>
int TestFreeRDPCodecXCrush(int argc, char* argv[])
{
return 0;
}

353
libfreerdp/codec/xcrush.c Normal file
View File

@ -0,0 +1,353 @@
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* XCrush (RDP6.1) Bulk Data Compression
*
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <winpr/crt.h>
#include <winpr/print.h>
#include <winpr/bitstream.h>
#include <freerdp/codec/xcrush.h>
int xcrush_copy_bytes(BYTE* dst, BYTE* src, int num)
{
int index;
for (index = 0; index < num; index++)
{
dst[index] = src[index];
}
return num;
}
int xcrush_decompress_l1(XCRUSH_CONTEXT* xcrush, BYTE* pSrcData, UINT32 SrcSize, BYTE** ppDstData, UINT32* pDstSize, UINT32 flags)
{
BYTE* pSrcEnd = NULL;
BYTE* Literals = NULL;
UINT16 MatchCount = 0;
UINT16 MatchIndex = 0;
BYTE* OutputPtr = NULL;
int OutputLength = 0;
UINT32 OutputOffset = 0;
BYTE* HistoryPtr = NULL;
BYTE* HistoryBuffer = NULL;
BYTE* HistoryBufferEnd = NULL;
UINT32 HistoryBufferSize = 0;
UINT16 MatchLength = 0;
UINT16 MatchOutputOffset = 0;
UINT32 MatchHistoryOffset = 0;
RDP61_MATCH_DETAILS* MatchDetails = NULL;
if (SrcSize < 1)
return -1001;
if (flags & L1_PACKET_AT_FRONT)
xcrush->HistoryOffset = 0;
pSrcEnd = &pSrcData[SrcSize];
HistoryBuffer = xcrush->HistoryBuffer;
HistoryBufferSize = xcrush->HistoryBufferSize;
HistoryBufferEnd = &(HistoryBuffer[HistoryBufferSize]);
xcrush->HistoryPtr = HistoryPtr = &(HistoryBuffer[xcrush->HistoryOffset]);
if (flags & L1_NO_COMPRESSION)
{
Literals = pSrcData;
}
else
{
if (!(flags & L1_COMPRESSED))
return -1002;
if ((pSrcData + 2) > pSrcEnd)
return -1003;
MatchCount = *((UINT16*) pSrcData);
MatchDetails = (RDP61_MATCH_DETAILS*) &pSrcData[2];
Literals = (BYTE*) &MatchDetails[MatchCount];
OutputOffset = 0;
if (Literals > pSrcEnd)
return -1004;
for (MatchIndex = 0; MatchIndex < MatchCount; MatchIndex++)
{
MatchLength = MatchDetails[MatchIndex].MatchLength;
MatchOutputOffset = MatchDetails[MatchIndex].MatchOutputOffset;
MatchHistoryOffset = MatchDetails[MatchIndex].MatchHistoryOffset;
if (MatchOutputOffset < OutputOffset)
return -1005;
if (MatchLength > HistoryBufferSize)
return -1006;
if (MatchHistoryOffset > HistoryBufferSize)
return -1007;
OutputLength = MatchOutputOffset - OutputOffset;
if ((MatchOutputOffset - OutputOffset) > HistoryBufferSize)
return -1008;
if (OutputLength > 0)
{
if ((&HistoryPtr[OutputLength] >= HistoryBufferEnd) || (Literals >= pSrcEnd) || (&Literals[OutputLength] > pSrcEnd))
return -1009;
xcrush_copy_bytes(HistoryPtr, Literals, OutputLength);
HistoryPtr += OutputLength;
Literals += OutputLength;
OutputOffset += OutputLength;
if (Literals > pSrcEnd)
return -1010;
}
OutputPtr = &xcrush->HistoryBuffer[MatchHistoryOffset];
if ((&HistoryPtr[MatchLength] >= HistoryBufferEnd) || (&OutputPtr[MatchLength] >= HistoryBufferEnd))
return -1011;
xcrush_copy_bytes(HistoryPtr, OutputPtr, MatchLength);
OutputOffset += MatchLength;
HistoryPtr += MatchLength;
}
}
if (Literals < pSrcEnd)
{
OutputLength = pSrcEnd - Literals;
if ((&HistoryPtr[OutputLength] >= HistoryBufferEnd) || (&Literals[OutputLength] > pSrcEnd))
return -1012;
xcrush_copy_bytes(HistoryPtr, Literals, OutputLength);
HistoryPtr += OutputLength;
}
xcrush->HistoryOffset = HistoryPtr - HistoryBuffer;
*pDstSize = HistoryPtr - xcrush->HistoryPtr;
*ppDstData = xcrush->HistoryPtr;
return 1;
}
int xcrush_decompress(XCRUSH_CONTEXT* xcrush, BYTE* pSrcData, UINT32 SrcSize, BYTE** ppDstData, UINT32* pDstSize, UINT32 flags)
{
int status = 0;
UINT32 DstSize = 0;
BYTE* pDstData = NULL;
BYTE Level1ComprFlags;
BYTE Level2ComprFlags;
if (SrcSize < 2)
return -1;
Level1ComprFlags = pSrcData[0];
Level2ComprFlags = pSrcData[1];
pSrcData += 2;
SrcSize -= 2;
if (Level2ComprFlags & PACKET_COMPRESSED)
{
status = mppc_decompress(xcrush->mppc, pSrcData, SrcSize, &pDstData, &DstSize, Level2ComprFlags);
if (status < 0)
return status;
}
else
{
pDstData = pSrcData;
DstSize = SrcSize;
}
status = xcrush_decompress_l1(xcrush, pDstData, DstSize, ppDstData, pDstSize, Level1ComprFlags);
return status;
}
int xcrush_compress_l1(XCRUSH_CONTEXT* xcrush, BYTE* pSrcData, UINT32 SrcSize, BYTE** ppDstData, UINT32* pDstSize, UINT32* pFlags)
{
UINT32 Flags = 0;
UINT32 HistoryOffset = 0;
BYTE* HistoryPtr = NULL;
BYTE* HistoryBuffer = NULL;
HistoryOffset = xcrush->HistoryOffset;
HistoryBuffer = xcrush->HistoryBuffer;
HistoryPtr = &HistoryBuffer[HistoryOffset];
CopyMemory(HistoryPtr, pSrcData, SrcSize);
xcrush->HistoryOffset += SrcSize;
if (SrcSize <= 50)
{
Flags |= L1_NO_COMPRESSION;
}
else
{
}
*pFlags = Flags;
return 1;
}
int xcrush_compress(XCRUSH_CONTEXT* xcrush, BYTE* pSrcData, UINT32 SrcSize, BYTE** ppDstData, UINT32* pDstSize, UINT32* pFlags)
{
int status = 0;
UINT32 DstSize = 0;
BYTE* pDstData = NULL;
BYTE* CompressedData = NULL;
UINT32 CompressedDataSize = 0;
BYTE* OriginalData = NULL;
UINT32 OriginalDataSize = 0;
UINT32 Level1ComprFlags = 0;
UINT32 Level2ComprFlags = 0;
UINT32 CompressionLevel = 3;
if (SrcSize > 16384)
return -1;
if ((SrcSize + 2) > *pDstSize)
return -1;
OriginalData = pDstData;
OriginalDataSize = *pDstSize;
pDstData = xcrush->BlockBuffer;
DstSize = sizeof(xcrush->BlockBuffer);
status = xcrush_compress_l1(xcrush, pSrcData, SrcSize, &pDstData, &DstSize, &Level1ComprFlags);
if (status < 0)
return status;
if (Level1ComprFlags & L1_COMPRESSED)
{
CompressedData = pDstData;
CompressedDataSize = DstSize;
if (CompressedDataSize > SrcSize)
return -1;
}
else
{
CompressedData = pSrcData;
CompressedDataSize = DstSize;
if (CompressedDataSize != SrcSize)
return -1;
}
status = 1;
pDstData = OriginalData + 2;
DstSize = OriginalDataSize - 2;
if (DstSize > 50)
{
status = mppc_compress(xcrush->mppc, CompressedData, CompressedDataSize, &pDstData, &DstSize, &Level2ComprFlags);
}
if (status < 0)
return status;
if (!(Level2ComprFlags & PACKET_COMPRESSED) || (Level2ComprFlags & PACKET_FLUSHED))
{
if (CompressedDataSize > DstSize)
return -1;
DstSize = CompressedDataSize;
CopyMemory(pDstData, CompressedData, CompressedDataSize);
}
if (Level2ComprFlags & PACKET_COMPRESSED)
{
}
else
{
if (Level2ComprFlags & PACKET_FLUSHED)
{
}
else
{
}
}
Level1ComprFlags |= L1_INNER_COMPRESSION;
OriginalData[0] = (BYTE) Level1ComprFlags;
OriginalData[1] = (BYTE) Level2ComprFlags;
if (*pDstSize < (DstSize + 2))
return -1;
*pDstSize = DstSize + 2;
*pFlags = PACKET_COMPRESSED | CompressionLevel;
return 1;
}
void xcrush_context_reset(XCRUSH_CONTEXT* xcrush)
{
}
XCRUSH_CONTEXT* xcrush_context_new(BOOL Compressor)
{
XCRUSH_CONTEXT* xcrush;
xcrush = (XCRUSH_CONTEXT*) calloc(1, sizeof(XCRUSH_CONTEXT));
if (xcrush)
{
xcrush->Compressor = Compressor;
xcrush->mppc = mppc_context_new(1, Compressor);
xcrush->HistoryOffset = 0;
xcrush->HistoryBufferSize = 2000000;
xcrush_context_reset(xcrush);
}
return xcrush;
}
void xcrush_context_free(XCRUSH_CONTEXT* xcrush)
{
if (xcrush)
{
mppc_context_free(xcrush->mppc);
free(xcrush);
}
}

View File

@ -137,7 +137,7 @@ int bulk_decompress(rdpBulk* bulk, BYTE* pSrcData, UINT32 SrcSize, BYTE** ppDstD
break;
case PACKET_COMPR_TYPE_RDP61:
status = -1;
status = xcrush_decompress(bulk->xcrushRecv, pSrcData, SrcSize, ppDstData, pDstSize, flags);
break;
case PACKET_COMPR_TYPE_RDP8:
@ -251,8 +251,12 @@ void bulk_reset(rdpBulk* bulk)
{
mppc_context_reset(bulk->mppcSend);
mppc_context_reset(bulk->mppcRecv);
ncrush_context_reset(bulk->ncrushRecv);
ncrush_context_reset(bulk->ncrushSend);
xcrush_context_reset(bulk->xcrushRecv);
xcrush_context_reset(bulk->xcrushSend);
}
rdpBulk* bulk_new(rdpContext* context)
@ -271,6 +275,9 @@ rdpBulk* bulk_new(rdpContext* context)
bulk->ncrushRecv = ncrush_context_new(FALSE);
bulk->ncrushSend = ncrush_context_new(TRUE);
bulk->xcrushRecv = xcrush_context_new(FALSE);
bulk->xcrushSend = xcrush_context_new(TRUE);
bulk->CompressionLevel = context->settings->CompressionLevel;
bulk->TotalCompressedBytes = 0;
@ -291,5 +298,8 @@ void bulk_free(rdpBulk* bulk)
ncrush_context_free(bulk->ncrushRecv);
ncrush_context_free(bulk->ncrushSend);
xcrush_context_free(bulk->xcrushRecv);
xcrush_context_free(bulk->xcrushSend);
free(bulk);
}

View File

@ -26,6 +26,7 @@ typedef struct rdp_bulk rdpBulk;
#include <freerdp/codec/mppc.h>
#include <freerdp/codec/ncrush.h>
#include <freerdp/codec/xcrush.h>
struct rdp_bulk
{
@ -36,6 +37,8 @@ struct rdp_bulk
MPPC_CONTEXT* mppcRecv;
NCRUSH_CONTEXT* ncrushRecv;
NCRUSH_CONTEXT* ncrushSend;
XCRUSH_CONTEXT* xcrushRecv;
XCRUSH_CONTEXT* xcrushSend;
BYTE OutputBuffer[65536];
UINT64 TotalCompressedBytes;
UINT64 TotalUncompressedBytes;