Просмотр исходного кода

Move generic compression routines to common package

Rod Hynes 9 лет назад
Родитель
Сommit
2fa7c11cc8
3 измененных файлов с 47 добавлено и 28 удалено
  1. 4 28
      psiphon/common/osl/osl.go
  2. 26 0
      psiphon/common/utils.go
  3. 17 0
      psiphon/common/utils_test.go

+ 4 - 28
psiphon/common/osl/osl.go

@@ -30,8 +30,6 @@
 package osl
 
 import (
-	"bytes"
-	"compress/zlib"
 	"crypto/hmac"
 	"crypto/md5"
 	"crypto/sha256"
@@ -41,7 +39,6 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
-	"io/ioutil"
 	"net"
 	"net/url"
 	"path"
@@ -750,7 +747,7 @@ func (config *Config) Pave(
 						return nil, common.ContextError(err)
 					}
 
-					boxedServerEntries, err := box(fileKey, compress(signedServerEntries))
+					boxedServerEntries, err := box(fileKey, common.Compress(signedServerEntries))
 					if err != nil {
 						return nil, common.ContextError(err)
 					}
@@ -793,7 +790,7 @@ func (config *Config) Pave(
 
 	paveFiles = append(paveFiles, &PaveFile{
 		Name:     REGISTRY_FILENAME,
-		Contents: compress(signedRegistry),
+		Contents: common.Compress(signedRegistry),
 	})
 
 	return paveFiles, nil
@@ -991,7 +988,7 @@ func GetOSLFilename(baseDirectory string, oslID []byte) string {
 func UnpackRegistry(
 	compressedRegistry []byte, signingPublicKey string) (*Registry, []byte, error) {
 
-	packagedRegistry, err := uncompress(compressedRegistry)
+	packagedRegistry, err := common.Decompress(compressedRegistry)
 	if err != nil {
 		return nil, nil, common.ContextError(err)
 	}
@@ -1173,7 +1170,7 @@ func (registry *Registry) UnpackOSL(
 		return "", common.ContextError(err)
 	}
 
-	dataPackage, err := uncompress(decryptedContents)
+	dataPackage, err := common.Decompress(decryptedContents)
 	if err != nil {
 		return "", common.ContextError(err)
 	}
@@ -1283,24 +1280,3 @@ func unbox(key, box []byte) ([]byte, error) {
 	}
 	return plaintext, nil
 }
-
-func compress(data []byte) []byte {
-	var compressedData bytes.Buffer
-	writer := zlib.NewWriter(&compressedData)
-	writer.Write(data)
-	writer.Close()
-	return compressedData.Bytes()
-}
-
-func uncompress(data []byte) ([]byte, error) {
-	reader, err := zlib.NewReader(bytes.NewReader(data))
-	if err != nil {
-		return nil, common.ContextError(err)
-	}
-	uncompressedData, err := ioutil.ReadAll(reader)
-	reader.Close()
-	if err != nil {
-		return nil, common.ContextError(err)
-	}
-	return uncompressedData, nil
-}

+ 26 - 0
psiphon/common/utils.go

@@ -20,11 +20,14 @@
 package common
 
 import (
+	"bytes"
+	"compress/zlib"
 	"crypto/rand"
 	"encoding/base64"
 	"encoding/hex"
 	"errors"
 	"fmt"
+	"io/ioutil"
 	"math/big"
 	"runtime"
 	"strings"
@@ -176,3 +179,26 @@ func ContextError(err error) error {
 	pc, _, line, _ := runtime.Caller(1)
 	return fmt.Errorf("%s#%d: %s", getFunctionName(pc), line, err)
 }
+
+// Compress returns zlib compressed data
+func Compress(data []byte) []byte {
+	var compressedData bytes.Buffer
+	writer := zlib.NewWriter(&compressedData)
+	writer.Write(data)
+	writer.Close()
+	return compressedData.Bytes()
+}
+
+// Decompress returns zlib decompressed data
+func Decompress(data []byte) ([]byte, error) {
+	reader, err := zlib.NewReader(bytes.NewReader(data))
+	if err != nil {
+		return nil, ContextError(err)
+	}
+	uncompressedData, err := ioutil.ReadAll(reader)
+	reader.Close()
+	if err != nil {
+		return nil, ContextError(err)
+	}
+	return uncompressedData, nil
+}

+ 17 - 0
psiphon/common/utils_test.go

@@ -20,6 +20,7 @@
 package common
 
 import (
+	"bytes"
 	"testing"
 	"time"
 )
@@ -52,3 +53,19 @@ func TestMakeRandomPeriod(t *testing.T) {
 		t.Error("duration should have randomness difference between calls")
 	}
 }
+
+func TestCompress(t *testing.T) {
+
+	originalData := []byte("test data")
+
+	compressedData := Compress(originalData)
+
+	decompressedData, err := Decompress(compressedData)
+	if err != nil {
+		t.Error("Uncompress failed: %s", err)
+	}
+
+	if bytes.Compare(originalData, decompressedData) != 0 {
+		t.Error("decompressed data doesn't match original data")
+	}
+}