#include "CompressionStream.h"
#include "Accessor.h"
#include "MemoryWrapperStream.h"
namespace
{
namespace
{
using
namespace
collections
;
using
namespace
lzw
;
void
::
()
{
if
(
>=
2
&& (
& (
-
1
)) ==
0
)
{
++;
}
}
lzw
::
*
::
(
lzw
::
*
,
)
{
if
(
<
MaxDictionarySize
)
{
*
=
.
();
code
->
=
;
code
->
=
;
code
->
=
;
code
->
=
->
+
1
;
->
.
(
,
code
,
);
++;
return
code
;
}
else
{
return
0
;
}
}
::
()
:codeAllocator(
65536
)
, mapAllocator(
1048576
)
{
=
.
();
for
(
=
0
;
i
<
256
;
i
++)
{
();
(
, (
)
i
);
}
}
::
(
bool
(&
)[
256
])
{
=
.
();
for
(
=
0
;
i
<
256
;
i
++)
{
if
(
[
i
])
{
();
(
, (
)
i
);
}
}
if
(
<
8
)
{
=
++;
}
}
::
()
{
}
void
::
()
{
=
0
;
=
/
8
;
if
(
%
8
!=
0
)
{
bufferUsedSize
++;
}
while
(
written
<
bufferUsedSize
)
{
=
->
(
+
written
,
bufferUsedSize
-
written
);
CHECK_ERROR(size != 0, L"LzwEncoder::Flush()#Failed to flush the lzw buffer.");
do
{
if
(!(
size
!=
0
))
throw
(
L"LzwEncoder::Flush()#Failed to flush the lzw buffer."
);}
while
(
0
);
written
+=
size
;
}
=
0
;
}
[
9
] = {
0x00
,
0x80
,
0xC0
,
0xE0
,
0xF0
,
0xF8
,
0xFC
,
0xFE
,
0xFF
};
[
9
] = {
0x00
,
0x01
,
0x03
,
0x07
,
0x0F
,
0x1F
,
0x3F
,
0x7F
,
0xFF
};
void
::
(
,
)
{
=
0
;
=
8
-
%
8
;
if
(
bitStep
>
)
{
bitStep
=
;
}
while
(
bitStart
<
)
{
if
(
==
BufferSize
*
8
)
{
();
}
=
%
8
;
=
/
8
;
=
[
byteIndex
];
byte
&=
highMarks
[
writeStart
];
= (
)((
>>
bitStart
)&
lowMarks
[
bitStep
]) << (
8
-
writeStart
-
bitStep
);
byte
|=
content
;
[
byteIndex
] =
byte
;
+=
bitStep
;
bitStart
+=
bitStep
;
=
-
bitStart
;
bitStep
=
remain
<
8
?
remain
:
8
;
}
}
::
()
{
=
;
}
::
(
bool
(&
)[
256
])
:
(
)
{
=
;
}
::
()
{
}
void
::
(
*
)
{
=
;
}
void
::
()
{
if
(
!=
)
{
(
->
,
);
=
;
}
=
8
-
%
8
;
if
(
remain
!=
8
&&
remain
>=
)
{
CHECK_ERROR(eofIndex != -1, L"LzwEncoder::Close()#Internal error.");
do
{
if
(!(
!= -
1
))
throw
(
L"LzwEncoder::Close()#Internal error."
);}
while
(
0
);
(
,
);
}
();
}
::
(
void
*
,
)
{
*
= (
*)
;
for
(
=
0
;
i
<
;
i
++)
{
=
bytes
[
i
];
*
=
->
.
(
byte
);
if
(
next
)
{
=
next
;
}
else
{
(
->
,
);
if
(
<
MaxDictionarySize
)
{
();
(
,
byte
);
}
=
->
.
(
byte
);
}
}
return
;
}
bool
::
(
&
,
)
{
=
0
;
if
(
== -
1
)
{
return
false
;
}
=
*
8
-
;
=
0
;
=
8
-
%
8
;
if
(
bitStep
>
)
{
bitStep
=
;
}
while
(
writtenBits
<
)
{
if
(
remainBits
==
0
)
{
=
->
(
,
BufferSize
);
if
(
==
0
)
{
= -
1
;
return
false
;
}
remainBits
=
*
8
;
=
0
;
}
=
[
/
8
];
byte
>>= (
8
-
%
8
-
bitStep
);
byte
&=
lowMarks
[
bitStep
];
|=
byte
<<
writtenBits
;
+=
bitStep
;
remainBits
-=
bitStep
;
writtenBits
+=
bitStep
;
=
-
writtenBits
;
bitStep
=
remain
<
8
?
remain
:
8
;
}
return
true
;
}
void
::
(
)
{
if
(
.
() <
)
{
.
(
);
}
=
;
}
void
::
(
lzw
::
*
)
{
*
= &
0
] +
->
;
*
=
;
while
(
current
!=
)
{
*(--
outputByte
) =
current
->
;
current
=
current
->
;
}
=
0
;
}
::
()
{
for
(
=
0
;
i
<
256
;
i
++)
{
.
(
->
.
((
)
i
));
}
}
::
(
bool
(&
)[
256
])
:
(
)
{
for
(
=
0
;
i
<
256
;
i
++)
{
if
(
[
i
])
{
.
(
->
.
((
)
i
));
}
}
if
(
!= -
1
)
{
.
(
0
);
}
}
::
()
{
}
void
::
(
*
)
{
=
;
}
void
::
()
{
}
::
(
void
*
,
)
{
=
0
;
*
= (
*)
;
while
(
written
<
)
{
=
-
written
;
=
-
;
if
(
remain
==
0
)
{
=
0
;
if
(!
(
index
,
) ||
index
==
)
{
break
;
}
*
=
0
;
if
(
index
==
.
())
{
prefix
=
;
(
prefix
->
+
1
);
(
prefix
);
-
1
] =
0
];
}
else
{
prefix
=
index
];
(
prefix
->
);
(
prefix
);
}
if
(
<
MaxDictionarySize
)
{
if
(
)
{
.
(
(
,
0
]));
}
();
}
=
index
];
}
else
{
if
(
remain
>
expect
)
{
remain
=
expect
;
}
(
bytes
+
written
, &
],
remain
);
+=
remain
;
written
+=
remain
;
}
}
return
written
;
}
(
stream
::
&
,
stream
::
&
)
{
=
0
;
while
(
true
)
{
char
[
1024
];
=
.
(
buffer
, (
)
sizeof
(
buffer
));
if
(
copied
==
0
)
{
break
;
}
totalSize
+=
.
(
buffer
,
copied
);
}
return
totalSize
;
}
const
=
1048576
;
void
(
stream
::
&
,
stream
::
&
)
{
<
char
>
(
CompressionFragmentSize
);
while
(
true
)
{
=
.
(&
buffer
0
],
buffer
.
());
if
(
size
==
0
)
break
;
;
{
;
(
compressedStream
,
encoder
);
encoderStream
.
(&
buffer
0
],
size
);
}
compressedStream
.
(
0
);
{
{
= (
)
size
;
.
(&
bufferSize
, (
)
sizeof
(
bufferSize
));
}
{
= (
)
compressedStream
.
();
.
(&
compressedSize
, (
)
sizeof
(
compressedSize
));
}
(
compressedStream
,
);
}
}
}
void
(
stream
::
&
,
stream
::
&
)
{
=
0
;
=
0
;
while
(
true
)
{
=
0
;
if
(
.
(&
bufferSize
, (
)
sizeof
(
bufferSize
)) !=
sizeof
(
bufferSize
))
{
break
;
}
=
0
;
CHECK_ERROR(inputStream.Read(&compressedSize, (vint)sizeof(compressedSize)) == sizeof(compressedSize), L"vl::stream::DecompressStream(MemoryStream&, MemoryStream&)#Incomplete input");
do
{
if
(!(
.
(&
compressedSize
, (
)
sizeof
(
compressedSize
)) ==
sizeof
(
compressedSize
)))
throw
(
L"vl::stream::DecompressStream(MemoryStream&, MemoryStream&)#Incomplete input"
);}
while
(
0
);
<
char
>
(
compressedSize
);
CHECK_ERROR(inputStream.Read(&buffer[0], compressedSize) == compressedSize, L"vl::stream::DecompressStream(MemoryStream&, MemoryStream&)#Incomplete input");
do
{
if
(!(
.
(&
buffer
0
],
compressedSize
) ==
compressedSize
))
throw
(
L"vl::stream::DecompressStream(MemoryStream&, MemoryStream&)#Incomplete input"
);}
while
(
0
);
(&
buffer
0
],
compressedSize
);
;
(
compressedStream
,
decoder
);
totalWritten
+=
(
decoderStream
,
);
totalSize
+=
bufferSize
;
}
CHECK_ERROR(outputStream.Size() == totalSize, L"vl::stream::DecompressStream(MemoryStream&, MemoryStream&)#Incomplete input");
do
{
if
(!(
.
() ==
totalSize
))
throw
(
L"vl::stream::DecompressStream(MemoryStream&, MemoryStream&)#Incomplete input"
);}
while
(
0
);
}
}
}