Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/js/libs/bytes/buffer.go
2070 views
1
package bytes
2
3
import (
4
"encoding/hex"
5
6
"github.com/Mzack9999/goja"
7
"github.com/projectdiscovery/nuclei/v3/pkg/js/libs/structs"
8
"github.com/projectdiscovery/nuclei/v3/pkg/js/utils"
9
)
10
11
type (
12
// Buffer is a bytes/Uint8Array type in javascript
13
// @example
14
// ```javascript
15
// const bytes = require('nuclei/bytes');
16
// const bytes = new bytes.Buffer();
17
// ```
18
// @example
19
// ```javascript
20
// const bytes = require('nuclei/bytes');
21
// // optionally it can accept existing byte/Uint8Array as input
22
// const bytes = new bytes.Buffer([1, 2, 3]);
23
// ```
24
Buffer struct {
25
buf []byte
26
}
27
)
28
29
// NewBuffer creates a new buffer from a byte slice.
30
func NewBuffer(call goja.ConstructorCall, runtime *goja.Runtime) *goja.Object {
31
if len(call.Arguments) > 0 {
32
if arg, ok := call.Argument(0).Export().([]byte); ok {
33
return utils.LinkConstructor(call, runtime, &Buffer{buf: arg})
34
} else {
35
utils.NewNucleiJS(runtime).Throw("Invalid argument type. Expected bytes/Uint8Array as input but got %T", call.Argument(0).Export())
36
}
37
}
38
return utils.LinkConstructor(call, runtime, &Buffer{})
39
}
40
41
// Write appends the given data to the buffer.
42
// @example
43
// ```javascript
44
// const bytes = require('nuclei/bytes');
45
// const buffer = new bytes.Buffer();
46
// buffer.Write([1, 2, 3]);
47
// ```
48
func (b *Buffer) Write(data []byte) *Buffer {
49
b.buf = append(b.buf, data...)
50
return b
51
}
52
53
// WriteString appends the given string data to the buffer.
54
// @example
55
// ```javascript
56
// const bytes = require('nuclei/bytes');
57
// const buffer = new bytes.Buffer();
58
// buffer.WriteString('hello');
59
// ```
60
func (b *Buffer) WriteString(data string) *Buffer {
61
b.buf = append(b.buf, []byte(data)...)
62
return b
63
}
64
65
// Bytes returns the byte representation of the buffer.
66
// @example
67
// ```javascript
68
// const bytes = require('nuclei/bytes');
69
// const buffer = new bytes.Buffer();
70
// buffer.WriteString('hello');
71
// log(buffer.Bytes());
72
// ```
73
func (b *Buffer) Bytes() []byte {
74
return b.buf
75
}
76
77
// String returns the string representation of the buffer.
78
// @example
79
// ```javascript
80
// const bytes = require('nuclei/bytes');
81
// const buffer = new bytes.Buffer();
82
// buffer.WriteString('hello');
83
// log(buffer.String());
84
// ```
85
func (b *Buffer) String() string {
86
return string(b.buf)
87
}
88
89
// Len returns the length of the buffer.
90
// @example
91
// ```javascript
92
// const bytes = require('nuclei/bytes');
93
// const buffer = new bytes.Buffer();
94
// buffer.WriteString('hello');
95
// log(buffer.Len());
96
// ```
97
func (b *Buffer) Len() int {
98
return len(b.buf)
99
}
100
101
// Hex returns the hex representation of the buffer.
102
// @example
103
// ```javascript
104
// const bytes = require('nuclei/bytes');
105
// const buffer = new bytes.Buffer();
106
// buffer.WriteString('hello');
107
// log(buffer.Hex());
108
// ```
109
func (b *Buffer) Hex() string {
110
return hex.EncodeToString(b.buf)
111
}
112
113
// Hexdump returns the hexdump representation of the buffer.
114
// @example
115
// ```javascript
116
// const bytes = require('nuclei/bytes');
117
// const buffer = new bytes.Buffer();
118
// buffer.WriteString('hello');
119
// log(buffer.Hexdump());
120
// ```
121
func (b *Buffer) Hexdump() string {
122
return hex.Dump(b.buf)
123
}
124
125
// Pack uses structs.Pack and packs given data and appends it to the buffer.
126
// it packs the data according to the given format.
127
// @example
128
// ```javascript
129
// const bytes = require('nuclei/bytes');
130
// const buffer = new bytes.Buffer();
131
// buffer.Pack('I', 123);
132
// ```
133
func (b *Buffer) Pack(formatStr string, msg any) error {
134
bin, err := structs.Pack(formatStr, msg)
135
if err != nil {
136
return err
137
}
138
b.Write(bin)
139
return nil
140
}
141
142