File: data.go

package info (click to toggle)
bettercap 2.33.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,668 kB
  • sloc: sh: 154; makefile: 76; python: 52; ansic: 9
file content (90 lines) | stat: -rw-r--r-- 2,273 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package js

import (
	"bytes"
	"compress/gzip"
	"encoding/base64"

	"github.com/robertkrimen/otto"
)

func btoa(call otto.FunctionCall) otto.Value {
	varValue := base64.StdEncoding.EncodeToString([]byte(call.Argument(0).String()))
	v, err := otto.ToValue(varValue)
	if err != nil {
		return ReportError("Could not convert to string: %s", varValue)
	}

	return v
}

func atob(call otto.FunctionCall) otto.Value {
	varValue, err := base64.StdEncoding.DecodeString(call.Argument(0).String())
	if err != nil {
		return ReportError("Could not decode string: %s", call.Argument(0).String())
	}

	v, err := otto.ToValue(string(varValue))
	if err != nil {
		return ReportError("Could not convert to string: %s", varValue)
	}

	return v
}

func gzipCompress(call otto.FunctionCall) otto.Value {
	argv := call.ArgumentList
	argc := len(argv)
	if argc != 1 {
		return ReportError("gzipCompress: expected 1 argument, %d given instead.", argc)
	}

	uncompressedBytes := []byte(argv[0].String())

	var writerBuffer bytes.Buffer
	gzipWriter := gzip.NewWriter(&writerBuffer)
	_, err := gzipWriter.Write(uncompressedBytes)
	if err != nil {
		return ReportError("gzipCompress: could not compress data: %s", err.Error())
	}
	gzipWriter.Close()

	compressedBytes := writerBuffer.Bytes()

	v, err := otto.ToValue(string(compressedBytes))
	if err != nil {
		return ReportError("Could not convert to string: %s", err.Error())
	}

	return v
}

func gzipDecompress(call otto.FunctionCall) otto.Value {
	argv := call.ArgumentList
	argc := len(argv)
	if argc != 1 {
		return ReportError("gzipDecompress: expected 1 argument, %d given instead.", argc)
	}

	compressedBytes := []byte(argv[0].String())
	readerBuffer := bytes.NewBuffer(compressedBytes)

	gzipReader, err := gzip.NewReader(readerBuffer)
	if err != nil {
		return ReportError("gzipDecompress: could not create gzip reader: %s", err.Error())
	}

	var decompressedBuffer bytes.Buffer
	_, err = decompressedBuffer.ReadFrom(gzipReader)
	if err != nil {
		return ReportError("gzipDecompress: could not decompress data: %s", err.Error())
	}

	decompressedBytes := decompressedBuffer.Bytes()
	v, err := otto.ToValue(string(decompressedBytes))
	if err != nil {
		return ReportError("Could not convert to string: %s", err.Error())
	}

	return v
}