duanhanzi8328 2017-07-03 11:47
浏览 274
已采纳

Golang:当输入类型为Reader时,gzip.NewReader()返回nil

I've come across a problem while uncompressing a stream of bytes that are previously compressed. Basically, I'm trying to create a Reader using the function bytes.NewReader() and then to unzip the stream by using gzip.NewReader() function. At last, I'd like to return the real values in either string or byte format.

I know gzip.NewReader requires io.Reader as an input, however, as far as I know, type Reader implements the interface io.Reader. I assume this should not cause any errors but I wonder what might be the problem in this case. I'd really appreciate if you help me out with this!

In case you wonder what this piece of text is,

"amZzRUR2NHVtcVpiZHNROHJiTTNYeGdUSndGTlVDZC9jaElSK1lXcFlJOD0="

it's a sample input sent from my client python script. It compresses by using gzip, does encryption by using AES128 and finally encodes in base64, in this order.

The client code:

import time
import json
import requests
import random
import gzip
import base64

from Crypto.Cipher import AES

baseurl = 'http://0.0.0.0:80'
key = 'TfvY7I358yospfWKcoviZizOShpm5hyH'
iv = 'mb13KcoviZizvYhp'

MODE = AES.MODE_CFB
BLOCK_SIZE = 16
SEGMENT_SIZE = 128

def http_post(url, data):
    print('Going to make a request to {} with the following data: {}'.format(url, data))
    r = requests.post(url,
                  data=data,
                  headers={'Content-type': 'application/json; charset=utf-8',
                           'Connection': 'keep-alive'}, )
    if r.status_code != 200:
        print('Server returned unexpected response code {}, and content: {}'.format(r.status_code, r.content))
        return False
    else:
        data = r.json()
        return data


def post_sample_data(key, iv):
    fake_device_id = "MB88"
    Load_VA_Total_Mean = random.randint(1000, 100000)
    print('Data that should come back: {}'.format(Load_VA_Total_Mean))
    data = {'i': fake_device_id, 'p': [
    {'d': [54.3, 0, 99, 49.35, 3, 99, 51.533, 1, 98, 28964, 7348, 43590, Load_VA_Total_Mean, 10350, 55200, 49.7],
     't': time.time(), 'dt': 'p'}]}
    url = baseurl + '/realtimedata'
    encryption_key_reference = 1
    payload = '{}
{}'.format(convert_pack(data, key, iv), encryption_key_reference)
    return http_post(url, payload)


def convert_pack(inputdict, key, iv):
    jsonpayload = json.dumps(inputdict)  # encode dict to json string
    gzippayload = gzip.compress(jsonpayload.encode('utf-8'))  # compress with gzip
    encryptedpayload = base64.b64encode(encrypt(key, iv, message))
    encoded = base64.b64encode(encryptedpayload)
    print('encoded: {}'.format(encoded))

    return str(encoded, encoding='utf-8')


def _pad_string(value):
    length = len(value)
    pad_size = BLOCK_SIZE - (length % BLOCK_SIZE)
    return value.ljust(length + pad_size, '\x00')


def encrypt(key, iv, plaintext):
    aes = AES.new(key, MODE, iv, segment_size=SEGMENT_SIZE)
    plaintext = _pad_string(plaintext)
    encrypted_text = aes.encrypt(plaintext)
    return encrypted_text


post_sample_data(key, iv)

The server code:

package main

import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"compress/gzip"
"io/ioutil"
"bytes"
)

func main() {
    receivedText := "amZzRUR2NHVtcVpiZHNROHJiTTNYeGdUSndGTlVDZC9jaElSK1lXcFlJOD0="
    fmt.Println("encrypted + encoded + gzipped: ", originalText)
    key := []byte("TfvY7I358yospfWKcoviZizOShpm5hyH")

    text := decrypt(key, originalText)
    fmt.Println("decrypted: ", string(text))
    reader := bytes.NewReader(text)
    gzReader, err1 := gzip.NewReader(reader)
    fmt.Println(gzReader)

    if err1 != nil {
        fmt.Println("error1")
    }

    content, err2 := ioutil.ReadAll(gzReader)

    if err2 != nil {
        fmt.Println("error2")
    }

    fmt.Println(string(content))
}

func decrypt(key []byte, cryptoText string) []byte {
    ciphertext, _ := base64.StdEncoding.DecodeString(cryptoText)
    fmt.Println("decoded: ", string(ciphertext))
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    iv := []byte("mb13KcoviZizvYhp")
    stream := cipher.NewCFBDecrypter(block, iv)
    stream.XORKeyStream(ciphertext, ciphertext)

    return ciphertext
}

The output:

<nil>
error1
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x288 pc=0x10a9167]

goroutine 1 [running]:
io/ioutil.readAll.func1(0xc420037dd0)
    /usr/local/go/src/io/ioutil/ioutil.go:30 +0x119
panic(0x10c4580, 0x1154d00)
    /usr/local/go/src/runtime/panic.go:489 +0x2cf
compress/gzip.(*Reader).Read(0x0, 0xc420092000, 0x200, 0x200, 0x1024ade, 0xc400000008, 0xc4200120c0)
    /usr/local/go/src/compress/gzip/gunzip.go:247 +0x37
bytes.(*Buffer).ReadFrom(0xc420037d28, 0x11451a0, 0x0, 0xc420092000, 0x0, 0x200)
    /usr/local/go/src/bytes/buffer.go:179 +0x160
io/ioutil.readAll(0x11451a0, 0x0, 0x200, 0x0, 0x0, 0x0, 0x0, 0x0)
    /usr/local/go/src/io/ioutil/ioutil.go:33 +0x150
io/ioutil.ReadAll(0x11451a0, 0x0, 0x1, 0x7, 0x0, 0x0, 0x20)
    /usr/local/go/src/io/ioutil/ioutil.go:42 +0x3e
main.main()
    /Users/bkaankuguoglu/Desktop/Go-dev/tool-backend/tester.go:43 
+0x404
exit status 2
  • 写回答

1条回答 默认 最新

  • douyan8267 2017-07-03 11:57
    关注

    The problem is that the "content" produced by the reader you pass to gz.NewReader() is not a valid gzip stream (data). If gzip.NewReader() returns a non-nil error (as it does in your case), the returned gzReader may be nil (and it usually is).

    If you pass a valid gzip stream to gzip.NewReader(), the returned gzReader will not be nil, and decoding will succeed.

    For example the gzip encoded form of the text "hello" is:

    [31 139 8 0 0 0 0 0 0 255 203 72 205 201 201 7 0 134 166 16 54 5 0 0 0]
    

    Using this as the input:

    data := []byte{31, 139, 8, 0, 0, 0, 0, 0, 0,
        255, 203, 72, 205, 201, 201, 7, 0, 134, 166, 16, 54, 5, 0, 0, 0}
    reader := bytes.NewReader(data)
    // The rest is unchanged
    

    It works, and the output is (try it on the Go Playground):

    &{[31 139 8 0 0 0 0 0 0 255 203 72 205 201 201 7 0 134 166 16 54 5 0 0 0] 0 -1}
    &{{ [] 0001-01-01 00:00:00 +0000 UTC  255} 0x10440260 0x10462000 0 0 [31 139 8 0 0 0 0 0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回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器