<commit_before>// Implementation of RFC 6143 §7.7 Encodings.

package vnc

import (
	"bytes"
	"fmt"
)

const (
	Raw               = int32(0)
	CopyRect          = int32(1)
	RRE               = int32(2)
	Hextile           = int32(5)
	TRLE              = int32(15)
	ZRLE              = int32(16)
	ColorPseudo       = int32(-239)
	DesktopSizePseudo = int32(-223)
)

// An Encoding implements a method for encoding pixel data that is
// sent by the server to the client.
type Encoding interface {
	// The number that uniquely identifies this encoding type.
	Type() int32

	// Read the contents of the encoded pixel data from the reader.
	// This should return a new Encoding implementation that contains
	// the proper data.
	Read(*ClientConn, *Rectangle) (Encoding, error)

	// Marshal implements the Marshaler interface.
	Marshal() ([]byte, error)
}

type Encodings []Encoding

func (e Encodings) Marshal() ([]byte, error) {
	buf := NewBuffer(nil)

	for _, enc := range e {
		if err := buf.Write(enc.Type()); err != nil {
			return nil, err
		}
	}

	return buf.Bytes(), nil
}

// RawEncoding is raw pixel data sent by the server.
// See RFC 6143 §7.7.1.
type RawEncoding struct {
	Colors []Color
}

func (*RawEncoding) Type() int32 {
	return Raw
}

func (e *RawEncoding) Marshal() ([]byte, error) {
	buf := NewBuffer(nil)

	for _, c := range e.Colors {
		bytes, err := c.Marshal()
		if err != nil {
			return nil, err
		}
		buf.WriteBytes(bytes)
	}

	return buf.Bytes(), nil
}

func (*RawEncoding) Read(c *ClientConn, rect *Rectangle) (Encoding, error) {
	// if c.debug {
	// 	log.Println("RawEncoding.Read()")
	// 	rect.DebugPrint()
	// }

	var buf bytes.Buffer

	bytesPerPixel := int(c.pixelFormat.BPP / 8)
	n := rect.Area() * bytesPerPixel
	if err := c.receiveN(&buf, n); err != nil {
		return nil, fmt.Errorf("unable to read rectangle with raw encoding: %v", err)<commit_msg>replacing %v with %s where appropriate<commit_after>// Implementation of RFC 6143 §7.7 Encodings.

package vnc

import (
	"bytes"
	"fmt"
)

const (
	Raw               = int32(0)
	CopyRect          = int32(1)
	RRE               = int32(2)
	Hextile           = int32(5)
	TRLE              = int32(15)
	ZRLE              = int32(16)
	ColorPseudo       = int32(-239)
	DesktopSizePseudo = int32(-223)
)

// An Encoding implements a method for encoding pixel data that is
// sent by the server to the client.
type Encoding interface {
	// The number that uniquely identifies this encoding type.
	Type() int32

	// Read the contents of the encoded pixel data from the reader.
	// This should return a new Encoding implementation that contains
	// the proper data.
	Read(*ClientConn, *Rectangle) (Encoding, error)

	// Marshal implements the Marshaler interface.
	Marshal() ([]byte, error)
}

type Encodings []Encoding

func (e Encodings) Marshal() ([]byte, error) {
	buf := NewBuffer(nil)

	for _, enc := range e {
		if err := buf.Write(enc.Type()); err != nil {
			return nil, err
		}
	}

	return buf.Bytes(), nil
}

// RawEncoding is raw pixel data sent by the server.
// See RFC 6143 §7.7.1.
type RawEncoding struct {
	Colors []Color
}

func (*RawEncoding) Type() int32 {
	return Raw
}

func (e *RawEncoding) Marshal() ([]byte, error) {
	buf := NewBuffer(nil)

	for _, c := range e.Colors {
		bytes, err := c.Marshal()
		if err != nil {
			return nil, err
		}
		buf.WriteBytes(bytes)
	}

	return buf.Bytes(), nil
}

func (*RawEncoding) Read(c *ClientConn, rect *Rectangle) (Encoding, error) {
	// if c.debug {
	// 	log.Printf("RawEncoding.Read(): %s", rect)
	// }

	var buf bytes.Buffer

	bytesPerPixel := int(c.pixelFormat.BPP / 8)
	n := rect.Area() * bytesPerPixel
	if err := c.receiveN(&buf, n); err != nil {
		return nil, fmt.Errorf("unable to read rectangle with raw encoding: %s", err)
{'fraction_non_alphanumeric': 0.13180972628222162, 'fraction_numerical': 0.025511559925591284, 'mean_word_length': 2.8844169246646025, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6664226', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1265, 'n_words': 421}