Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujiaqing committed Aug 24, 2017
0 parents commit b6fcdad
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Snappy\nSnappy library for D.
6 changes: 6 additions & 0 deletions dub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "snappy",
"license": "Apache-2.0",
"copyright": "Copyright (C) 2015-2017 Shanghai Putao Technology Co., Ltd",
"description": "Snappy library for dlang."
}
4 changes: 4 additions & 0 deletions source/snappy/package.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module snappy;

public import snappy.snappy;

76 changes: 76 additions & 0 deletions source/snappy/snappy.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module snappy.snappy;

import std.conv;

extern (C) {
enum snappy_status {
SNAPPY_OK = 0,
SNAPPY_INVALID_INPUT = 1,
SNAPPY_BUFFER_TOO_SMALL = 2,
};

snappy_status snappy_uncompressed_length(const byte* compressed,
size_t compressed_length,
size_t* result);

snappy_status snappy_uncompress(const byte* compressed,
size_t compressed_length,
byte* uncompressed,
size_t* uncompressed_length);
snappy_status snappy_compress(const byte* input,
size_t input_length,
byte* compressed,
size_t* compressed_length);
size_t snappy_max_compressed_length(size_t source_length);
}

class Snappy {

static byte[] uncompress(byte[] compressed) {
size_t uncompressedPrediction;
snappy_status ok = snappy_uncompressed_length(compressed.ptr, compressed.length, &uncompressedPrediction);
if (ok != snappy_status.SNAPPY_OK) {
throw new Exception(to!(string)(ok));
}
auto res = new byte[uncompressedPrediction];
size_t uncompressed = uncompressedPrediction;
ok = snappy_uncompress(compressed.ptr, compressed.length, res.ptr, &uncompressed);
if (ok != snappy_status.SNAPPY_OK) {
throw new Exception(to!(string)(ok));
}
if (uncompressed != uncompressedPrediction) {
throw new Exception("uncompressedPrediction " ~ to!(string)(uncompressedPrediction) ~ " != " ~ "uncompressed " ~ to!(string)(uncompressed));
}
return res;
}



static byte[] compress(byte[] uncompressed) {
size_t maxCompressedSize = snappy_max_compressed_length(uncompressed.length);
byte[] res = new byte[maxCompressedSize];
size_t compressedSize = maxCompressedSize;
snappy_status ok = snappy_compress(uncompressed.ptr, uncompressed.length, res.ptr, &compressedSize);
if (ok != snappy_status.SNAPPY_OK) {
throw new Exception(to!(string)(ok));
}
return res[0..compressedSize];
}

}


unittest{
import std.stdio;
import util.snappy;

byte[] data = cast(byte[])"ffdsffffffffffffffffaaaaaaaaaaaaaaaaaaccccccccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeeeeeeeeee";
writeln("-------------------------------------------------");
writefln("start test compress data, length:%s", data.length);

byte[] cprData = Snappy.compress(data);
writefln("compress data, length:%s, data:%s", cprData.length, cprData);

byte[] unData = Snappy.uncompress(cprData);
writefln("uncompress data, length:%s, data:%s", unData.length, unData);
}

0 comments on commit b6fcdad

Please sign in to comment.