2023-09-16 16:02:17 +05:30
|
|
|
package bytes
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
|
2025-07-09 14:47:26 -05:00
|
|
|
"github.com/Mzack9999/goja"
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/js/libs/structs"
|
2024-02-07 21:45:40 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/js/utils"
|
2023-09-16 16:02:17 +05:30
|
|
|
)
|
|
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
type (
|
|
|
|
|
// Buffer is a bytes/Uint8Array type in javascript
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const bytes = require('nuclei/bytes');
|
|
|
|
|
// const bytes = new bytes.Buffer();
|
|
|
|
|
// ```
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const bytes = require('nuclei/bytes');
|
|
|
|
|
// // optionally it can accept existing byte/Uint8Array as input
|
|
|
|
|
// const bytes = new bytes.Buffer([1, 2, 3]);
|
|
|
|
|
// ```
|
|
|
|
|
Buffer struct {
|
|
|
|
|
buf []byte
|
|
|
|
|
}
|
|
|
|
|
)
|
2023-09-16 16:02:17 +05:30
|
|
|
|
|
|
|
|
// NewBuffer creates a new buffer from a byte slice.
|
2024-02-07 21:45:40 +05:30
|
|
|
func NewBuffer(call goja.ConstructorCall, runtime *goja.Runtime) *goja.Object {
|
|
|
|
|
if len(call.Arguments) > 0 {
|
|
|
|
|
if arg, ok := call.Argument(0).Export().([]byte); ok {
|
|
|
|
|
return utils.LinkConstructor(call, runtime, &Buffer{buf: arg})
|
|
|
|
|
} else {
|
|
|
|
|
utils.NewNucleiJS(runtime).Throw("Invalid argument type. Expected bytes/Uint8Array as input but got %T", call.Argument(0).Export())
|
|
|
|
|
}
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
2024-02-07 21:45:40 +05:30
|
|
|
return utils.LinkConstructor(call, runtime, &Buffer{})
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
|
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
// Write appends the given data to the buffer.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const bytes = require('nuclei/bytes');
|
|
|
|
|
// const buffer = new bytes.Buffer();
|
|
|
|
|
// buffer.Write([1, 2, 3]);
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (b *Buffer) Write(data []byte) *Buffer {
|
|
|
|
|
b.buf = append(b.buf, data...)
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
// WriteString appends the given string data to the buffer.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const bytes = require('nuclei/bytes');
|
|
|
|
|
// const buffer = new bytes.Buffer();
|
|
|
|
|
// buffer.WriteString('hello');
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (b *Buffer) WriteString(data string) *Buffer {
|
|
|
|
|
b.buf = append(b.buf, []byte(data)...)
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
// Bytes returns the byte representation of the buffer.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const bytes = require('nuclei/bytes');
|
|
|
|
|
// const buffer = new bytes.Buffer();
|
|
|
|
|
// buffer.WriteString('hello');
|
|
|
|
|
// log(buffer.Bytes());
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (b *Buffer) Bytes() []byte {
|
|
|
|
|
return b.buf
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// String returns the string representation of the buffer.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const bytes = require('nuclei/bytes');
|
|
|
|
|
// const buffer = new bytes.Buffer();
|
|
|
|
|
// buffer.WriteString('hello');
|
|
|
|
|
// log(buffer.String());
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (b *Buffer) String() string {
|
|
|
|
|
return string(b.buf)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Len returns the length of the buffer.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const bytes = require('nuclei/bytes');
|
|
|
|
|
// const buffer = new bytes.Buffer();
|
|
|
|
|
// buffer.WriteString('hello');
|
|
|
|
|
// log(buffer.Len());
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (b *Buffer) Len() int {
|
|
|
|
|
return len(b.buf)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hex returns the hex representation of the buffer.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const bytes = require('nuclei/bytes');
|
|
|
|
|
// const buffer = new bytes.Buffer();
|
|
|
|
|
// buffer.WriteString('hello');
|
|
|
|
|
// log(buffer.Hex());
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (b *Buffer) Hex() string {
|
|
|
|
|
return hex.EncodeToString(b.buf)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hexdump returns the hexdump representation of the buffer.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const bytes = require('nuclei/bytes');
|
|
|
|
|
// const buffer = new bytes.Buffer();
|
|
|
|
|
// buffer.WriteString('hello');
|
|
|
|
|
// log(buffer.Hexdump());
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (b *Buffer) Hexdump() string {
|
|
|
|
|
return hex.Dump(b.buf)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pack uses structs.Pack and packs given data and appends it to the buffer.
|
|
|
|
|
// it packs the data according to the given format.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const bytes = require('nuclei/bytes');
|
|
|
|
|
// const buffer = new bytes.Buffer();
|
|
|
|
|
// buffer.Pack('I', 123);
|
|
|
|
|
// ```
|
|
|
|
|
func (b *Buffer) Pack(formatStr string, msg any) error {
|
2023-09-16 16:02:17 +05:30
|
|
|
bin, err := structs.Pack(formatStr, msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
b.Write(bin)
|
|
|
|
|
return nil
|
|
|
|
|
}
|