-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module droid.gateway.compression; | ||
|
||
import droid.exception; | ||
import std.zlib, | ||
std.stdio, | ||
std.conv : to; | ||
|
||
enum CompressionType : string { | ||
NONE = "", | ||
ZLIB = "zlib", | ||
ZLIB_STREAM = "zlib-stream" | ||
} | ||
class Decompressor { | ||
string read(ubyte[] data) { | ||
throw new DroidException("Compression type not supported!"); | ||
} | ||
} | ||
|
||
class ZLibStream : Decompressor { | ||
const ulong[] ZLIB_SUFFIX = [0x0, 0x0, 0xFF, 0xFF]; | ||
UnCompress decompressor; | ||
|
||
this() { | ||
decompressor = new UnCompress(HeaderFormat.deflate); | ||
} | ||
|
||
override string read(ubyte[] data) { | ||
if (data[$-4..$] != ZLIB_SUFFIX) { | ||
throw new DroidException("ZLib-Stream compression enabled but invalid data was recieved!"); | ||
} | ||
|
||
string decompressed = to!string(decompressor.uncompress(data)); | ||
decompressor.flush(); | ||
|
||
return decompressed; | ||
} | ||
} |