refactored encodings to directories.

This commit is contained in:
betzalel
2017-06-14 10:54:15 +03:00
parent 89ff8ac9e3
commit f28d0de0ad
26 changed files with 1200 additions and 966 deletions

View File

@@ -0,0 +1,26 @@
package encodings
import (
"io"
"vncproxy/common"
)
type CopyRectEncoding struct {
//Colors []Color
copyRectSrcX uint16
copyRectSrcY uint16
}
func (z *CopyRectEncoding) Type() int32 {
return 1
}
func (z *CopyRectEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r io.Reader) (common.Encoding, error) {
conn := common.RfbReadHelper{r}
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
//bytesPerPixel := c.PixelFormat.BPP / 8
z.copyRectSrcX, _ = conn.ReadUint16()
z.copyRectSrcY, _ = conn.ReadUint16()
return z, nil
}
//////////

48
encodings/enc-corre.go Normal file
View File

@@ -0,0 +1,48 @@
package encodings
import (
"io"
"vncproxy/common"
)
type CoRREEncoding struct {
//Colors []Color
}
func (z *CoRREEncoding) Type() int32 {
return 4
}
func (z *CoRREEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r io.Reader) (common.Encoding, error) {
conn := common.RfbReadHelper{r}
bytesPerPixel := int(pixelFmt.BPP / 8)
numOfSubrectangles, _ := conn.ReadUint32()
//read whole rect background color
conn.ReadBytes(bytesPerPixel)
//read all individual rects (color=BPP + x=16b + y=16b + w=16b + h=16b)
_, err := conn.ReadBytes(int(numOfSubrectangles) * (bytesPerPixel + 4))
if err != nil {
return nil, err
}
return z, nil
//int nSubrects = rfb.readU32();
//byte[] bg_buf = new byte[bytesPerPixel];
//rfb.readFully(bytesPerPixel);
//Color pixel;
// if (bytesPixel == 1) {
// pixel = colors[bg_buf[0] & 0xFF];
// } else {
// pixel = new Color(bg_buf[2] & 0xFF, bg_buf[1] & 0xFF, bg_buf[0] & 0xFF);
// }
// memGraphics.setColor(pixel);
// memGraphics.fillRect(x, y, w, h);
// byte[] buf = new byte[nSubrects * (bytesPixel + 4)];
// rfb.readFully(buf);
}

85
encodings/enc-hextile.go Normal file
View File

@@ -0,0 +1,85 @@
package encodings
import (
"io"
"vncproxy/common"
)
const (
HextileRaw = 1
HextileBackgroundSpecified = 2
HextileForegroundSpecified = 4
HextileAnySubrects = 8
HextileSubrectsColoured = 16
)
type HextileEncoding struct {
//Colors []Color
}
func (z *HextileEncoding) Type() int32 {
return 5
}
func (z *HextileEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r io.Reader) (common.Encoding, error) {
conn := common.RfbReadHelper{r}
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
bytesPerPixel := int(pixelFmt.BPP) / 8
//buf := make([]byte, bytesPerPixel)
for ty := rect.Y; ty < rect.Y+rect.Height; ty += 16 {
th := 16
if rect.Y+rect.Height-ty < 16 {
th = int(rect.Y) + int(rect.Height) - int(ty)
}
for tx := rect.X; tx < rect.X+rect.Width; tx += 16 {
tw := 16
if rect.X+rect.Width-tx < 16 {
tw = int(rect.X) + int(rect.Width) - int(tx)
}
//handle Hextile Subrect(tx, ty, tw, th):
subencoding, err := conn.ReadUint8()
//fmt.Printf("hextile reader tile: (%d,%d) subenc=%d\n", ty, tx, subencoding)
if err != nil {
//fmt.Printf("error in hextile reader: %v\n", err)
return nil, err
}
if (subencoding & HextileRaw) != 0 {
//ReadRawRect(c, rect, r)
conn.ReadBytes(tw * th * bytesPerPixel)
//fmt.Printf("hextile reader: HextileRaw\n")
continue
}
if (subencoding & HextileBackgroundSpecified) != 0 {
conn.ReadBytes(int(bytesPerPixel))
}
if (subencoding & HextileForegroundSpecified) != 0 {
conn.ReadBytes(int(bytesPerPixel))
}
if (subencoding & HextileAnySubrects) == 0 {
//fmt.Printf("hextile reader: no Subrects\n")
continue
}
//fmt.Printf("hextile reader: handling Subrects\n")
nSubrects, err := conn.ReadUint8()
if err != nil {
return nil, err
}
bufsize := int(nSubrects) * 2
if (subencoding & HextileSubrectsColoured) != 0 {
bufsize += int(nSubrects) * int(bytesPerPixel)
}
//byte[] buf = new byte[bufsize];
conn.ReadBytes(bufsize)
}
}
// len, _ := readUint32(c.c)
// _, err := readBytes(c.c, int(len))
// if err != nil {
// return nil, err
// }
return z, nil
}

59
encodings/enc-raw.go Normal file
View File

@@ -0,0 +1,59 @@
package encodings
import (
"io"
"vncproxy/common"
)
// RawEncoding is raw pixel data sent by the server.
//
// See RFC 6143 Section 7.7.1
type RawEncoding struct {
//Colors []Color
}
func (*RawEncoding) Type() int32 {
return 0
}
func (*RawEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r io.Reader) (common.Encoding, error) {
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
conn := common.RfbReadHelper{r}
bytesPerPixel := int(pixelFmt.BPP / 8)
//pixelBytes := make([]uint8, bytesPerPixel)
// var byteOrder binary.ByteOrder = binary.LittleEndian
// if conn.PixelFormat.BigEndian {
// byteOrder = binary.BigEndian
// }
//colors := make([]vnc.Color, int(rect.Height)*int(rect.Width))
for y := uint16(0); y < rect.Height; y++ {
for x := uint16(0); x < rect.Width; x++ {
if _, err := conn.ReadBytes(bytesPerPixel); err != nil {
return nil, err
}
// var rawPixel uint32
// if conn.PixelFormat.BPP == 8 {
// rawPixel = uint32(pixelBytes[0])
// } else if conn.PixelFormat.BPP == 16 {
// rawPixel = uint32(byteOrder.Uint16(pixelBytes))
// } else if conn.PixelFormat.BPP == 32 {
// rawPixel = byteOrder.Uint32(pixelBytes)
// }
// color := &colors[int(y)*int(rect.Width)+int(x)]
// if conn.PixelFormat.TrueColor {
// color.R = uint16((rawPixel >> conn.PixelFormat.RedShift) & uint32(conn.PixelFormat.RedMax))
// color.G = uint16((rawPixel >> conn.PixelFormat.GreenShift) & uint32(conn.PixelFormat.GreenMax))
// color.B = uint16((rawPixel >> conn.PixelFormat.BlueShift) & uint32(conn.PixelFormat.BlueMax))
// } else {
// *color = conn.ColorMap[rawPixel]
// }
}
}
return &RawEncoding{}, nil
}

28
encodings/enc-rre.go Normal file
View File

@@ -0,0 +1,28 @@
package encodings
import "io"
import "vncproxy/common"
type RREEncoding struct {
//Colors []Color
}
func (z *RREEncoding) Type() int32 {
return 2
}
func (z *RREEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r io.Reader) (common.Encoding, error) {
conn := common.RfbReadHelper{r}
bytesPerPixel := int(pixelFmt.BPP / 8)
numOfSubrectangles, _ := conn.ReadUint32()
//read whole rect background color
conn.ReadBytes(bytesPerPixel)
//read all individual rects (color=BPP + x=16b + y=16b + w=16b + h=16b)
_, err := conn.ReadBytes(int(numOfSubrectangles) * (bytesPerPixel + 8))
if err != nil {
return nil, err
}
return z, nil
}

334
encodings/enc-tight.go Normal file
View File

@@ -0,0 +1,334 @@
package encodings
import (
"errors"
"fmt"
"io"
"vncproxy/common"
)
var TightMinToCompress int = 12
const (
TightExplicitFilter = 0x04
TightFill = 0x08
TightJpeg = 0x09
TightMaxSubencoding = 0x09
TightFilterCopy = 0x00
TightFilterPalette = 0x01
TightFilterGradient = 0x02
)
type TightEncoding struct {
output io.Writer
logger common.Logger
}
func (t *TightEncoding) SetOutput(output io.Writer) {
t.output = output
}
func (*TightEncoding) Type() int32 {
return 7
}
// func ReadAndRecBytes(conn io.Reader, rec io.Writer, count int) ([]byte, error) {
// buf, err := readBytes(conn, count)
// rec.Write(buf)
// return buf, err
// }
// func ReadAndRecUint8(conn io.Reader, rec io.Writer) (uint8, error) {
// myUint, err := readUint8(conn)
// buf := make([]byte, 1)
// buf[0] = byte(myUint) // cast int8 to byte
// rec.Write(buf)
// return myUint, err
// }
// func ReadAndRecUint16(conn io.Reader, rec io.Writer) (uint16, error) {
// myUint, err := readUint16(conn)
// buf := make([]byte, 2)
// //buf[0] = byte(myUint) // cast int8 to byte
// //var i int16 = 41
// //b := make([]byte, 2)
// binary.LittleEndian.PutUint16(buf, uint16(myUint))
// rec.Write(buf)
// return myUint, err
// }
func calcTightBytePerPixel(pf *common.PixelFormat) int {
bytesPerPixel := int(pf.BPP / 8)
var bytesPerPixelTight int
if 24 == pf.Depth && 32 == pf.BPP {
bytesPerPixelTight = 3
} else {
bytesPerPixelTight = bytesPerPixel
}
return bytesPerPixelTight
}
func (t *TightEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, reader io.Reader) (common.Encoding, error) {
bytesPixel := calcTightBytePerPixel(pixelFmt)
conn := common.RfbReadHelper{reader}
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
//var subencoding uint8
subencoding, err := conn.ReadUint8()
if err != nil {
fmt.Printf("error in handling tight encoding: %v\n", err)
return nil, err
}
fmt.Printf("bytesPixel= %d, subencoding= %d\n", bytesPixel, subencoding)
// if err := binary.Read(conn.c, binary.BigEndian, &subencoding); err != nil {
// return t, err
// }
//move it to position (remove zlib flush commands)
compType := subencoding >> 4 & 0x0F
// for stream_id := 0; stream_id < 4; stream_id++ {
// // if ((comp_ctl & 1) != 0 && tightInflaters[stream_id] != null) {
// // tightInflaters[stream_id] = null;
// // }
// subencoding >>= 1
// }
fmt.Printf("afterSHL:%d\n", compType)
switch compType {
case TightFill:
fmt.Printf("reading fill size=%d\n", bytesPixel)
//read color
conn.ReadBytes(int(bytesPixel))
return t, nil
case TightJpeg:
if pixelFmt.BPP == 8 {
return nil, errors.New("Tight encoding: JPEG is not supported in 8 bpp mode")
}
len, err := conn.ReadCompactLen()
if err != nil {
return nil, err
}
fmt.Printf("reading jpeg size=%d\n", len)
conn.ReadBytes(len)
return t, nil
default:
if compType > TightJpeg {
fmt.Println("Compression control byte is incorrect!")
}
handleTightFilters(subencoding, pixelFmt, rect, reader)
return t, nil
}
}
func handleTightFilters(subencoding uint8, pixelFmt *common.PixelFormat, rect *common.Rectangle, reader io.Reader) {
conn := common.RfbReadHelper{reader}
var FILTER_ID_MASK uint8 = 0x40
//var STREAM_ID_MASK uint8 = 0x30
//decoderId := (subencoding & STREAM_ID_MASK) >> 4
var filterid uint8
var err error
if (subencoding & FILTER_ID_MASK) > 0 { // filter byte presence
filterid, err = conn.ReadUint8()
if err != nil {
fmt.Printf("error in handling tight encoding, reading filterid: %v\n", err)
return
}
fmt.Printf("read filter: %d\n", filterid)
}
//var numColors uint8
bytesPixel := calcTightBytePerPixel(pixelFmt)
fmt.Printf("filter: %d\n", filterid)
// if rfb.rec != null {
// rfb.rec.writeByte(filter_id)
// }
lengthCurrentbpp := int(bytesPixel) * int(rect.Width) * int(rect.Height)
switch filterid {
case TightFilterPalette: //PALETTE_FILTER
colorCount, err := conn.ReadUint8()
paletteSize := colorCount + 1 // add one more
fmt.Printf("----PALETTE_FILTER: paletteSize=%d bytesPixel=%d\n", paletteSize, bytesPixel)
//complete palette
conn.ReadBytes(int(paletteSize) * bytesPixel)
var dataLength int
if paletteSize == 2 {
dataLength = int(rect.Height) * ((int(rect.Width) + 7) / 8)
} else {
dataLength = int(rect.Width * rect.Height)
}
_, err = conn.ReadTightData(dataLength)
if err != nil {
fmt.Printf("error in handling tight encoding, Reading Palette: %v\n", err)
return
}
case TightFilterGradient: //GRADIENT_FILTER
fmt.Printf("----GRADIENT_FILTER: bytesPixel=%d\n", bytesPixel)
//useGradient = true
fmt.Printf("usegrad: %d\n", filterid)
conn.ReadTightData(lengthCurrentbpp)
case TightFilterCopy: //BASIC_FILTER
fmt.Printf("----BASIC_FILTER: bytesPixel=%d\n", bytesPixel)
conn.ReadTightData(lengthCurrentbpp)
default:
fmt.Printf("Bad tight filter id: %d\n", filterid)
return
}
////////////
// if numColors == 0 && bytesPixel == 4 {
// rowSize1 *= 3
// }
// rowSize := (int(rect.Width)*bitsPixel + 7) / 8
// dataSize := int(rect.Height) * rowSize
// dataSize1 := rect.Height * rowSize1
// fmt.Printf("datasize: %d, origDatasize: %d", dataSize, dataSize1)
// // Read, optionally uncompress and decode data.
// if int(dataSize1) < TightMinToCompress {
// // Data size is small - not compressed with zlib.
// if numColors != 0 {
// // Indexed colors.
// //indexedData := make([]byte, dataSize)
// readBytes(conn.c, int(dataSize1))
// //readFully(indexedData);
// // if (rfb.rec != null) {
// // rfb.rec.write(indexedData);
// // }
// // if (numColors == 2) {
// // // Two colors.
// // if (bytesPixel == 1) {
// // decodeMonoData(x, y, w, h, indexedData, palette8);
// // } else {
// // decodeMonoData(x, y, w, h, indexedData, palette24);
// // }
// // } else {
// // // 3..255 colors (assuming bytesPixel == 4).
// // int i = 0;
// // for (int dy = y; dy < y + h; dy++) {
// // for (int dx = x; dx < x + w; dx++) {
// // pixels24[dy * rfb.framebufferWidth + dx] = palette24[indexedData[i++] & 0xFF];
// // }
// // }
// // }
// } else if useGradient {
// // "Gradient"-processed data
// //buf := make ( []byte,w * h * 3);
// dataByteCount := int(3) * int(rect.Width) * int(rect.Height)
// readBytes(conn.c, dataByteCount)
// // rfb.readFully(buf);
// // if (rfb.rec != null) {
// // rfb.rec.write(buf);
// // }
// // decodeGradientData(x, y, w, h, buf);
// } else {
// // Raw truecolor data.
// dataByteCount := int(bytesPixel) * int(rect.Width) * int(rect.Height)
// readBytes(conn.c, dataByteCount)
// // if (bytesPixel == 1) {
// // for (int dy = y; dy < y + h; dy++) {
// // rfb.readFully(pixels8, dy * rfb.framebufferWidth + x, w);
// // if (rfb.rec != null) {
// // rfb.rec.write(pixels8, dy * rfb.framebufferWidth + x, w);
// // }
// // }
// // } else {
// // byte[] buf = new byte[w * 3];
// // int i, offset;
// // for (int dy = y; dy < y + h; dy++) {
// // rfb.readFully(buf);
// // if (rfb.rec != null) {
// // rfb.rec.write(buf);
// // }
// // offset = dy * rfb.framebufferWidth + x;
// // for (i = 0; i < w; i++) {
// // pixels24[offset + i] = (buf[i * 3] & 0xFF) << 16 | (buf[i * 3 + 1] & 0xFF) << 8 | (buf[i * 3 + 2] & 0xFF);
// // }
// // }
// // }
// }
// } else {
// // Data was compressed with zlib.
// zlibDataLen, err := readCompactLen(conn.c)
// fmt.Printf("compactlen=%d\n", zlibDataLen)
// if err != nil {
// return nil, err
// }
// //byte[] zlibData = new byte[zlibDataLen];
// //rfb.readFully(zlibData);
// readBytes(conn.c, zlibDataLen)
// // if (rfb.rec != null) {
// // rfb.rec.write(zlibData);
// // }
// // int stream_id = comp_ctl & 0x03;
// // if (tightInflaters[stream_id] == null) {
// // tightInflaters[stream_id] = new Inflater();
// // }
// // Inflater myInflater = tightInflaters[stream_id];
// // myInflater.setInput(zlibData);
// // byte[] buf = new byte[dataSize];
// // myInflater.inflate(buf);
// // if (rfb.rec != null && !rfb.recordFromBeginning) {
// // rfb.recordCompressedData(buf);
// // }
// // if (numColors != 0) {
// // // Indexed colors.
// // if (numColors == 2) {
// // // Two colors.
// // if (bytesPixel == 1) {
// // decodeMonoData(x, y, w, h, buf, palette8);
// // } else {
// // decodeMonoData(x, y, w, h, buf, palette24);
// // }
// // } else {
// // // More than two colors (assuming bytesPixel == 4).
// // int i = 0;
// // for (int dy = y; dy < y + h; dy++) {
// // for (int dx = x; dx < x + w; dx++) {
// // pixels24[dy * rfb.framebufferWidth + dx] = palette24[buf[i++] & 0xFF];
// // }
// // }
// // }
// // } else if (useGradient) {
// // // Compressed "Gradient"-filtered data (assuming bytesPixel == 4).
// // decodeGradientData(x, y, w, h, buf);
// // } else {
// // // Compressed truecolor data.
// // if (bytesPixel == 1) {
// // int destOffset = y * rfb.framebufferWidth + x;
// // for (int dy = 0; dy < h; dy++) {
// // System.arraycopy(buf, dy * w, pixels8, destOffset, w);
// // destOffset += rfb.framebufferWidth;
// // }
// // } else {
// // int srcOffset = 0;
// // int destOffset, i;
// // for (int dy = 0; dy < h; dy++) {
// // myInflater.inflate(buf);
// // destOffset = (y + dy) * rfb.framebufferWidth + x;
// // for (i = 0; i < w; i++) {
// // pixels24[destOffset + i] = (buf[srcOffset] & 0xFF) << 16 | (buf[srcOffset + 1] & 0xFF) << 8
// // | (buf[srcOffset + 2] & 0xFF);
// // srcOffset += 3;
// // }
// // }
// // }
// // }
// }
return
}

24
encodings/enc-zlib.go Normal file
View File

@@ -0,0 +1,24 @@
package encodings
import "io"
import "vncproxy/common"
type ZLibEncoding struct {
//Colors []Color
}
func (z *ZLibEncoding) Type() int32 {
return 6
}
func (z *ZLibEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r io.Reader) (common.Encoding, error) {
conn := common.RfbReadHelper{r}
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
//bytesPerPixel := c.PixelFormat.BPP / 8
len, _ := conn.ReadUint32()
_, err := conn.ReadBytes(int(len))
if err != nil {
return nil, err
}
return z, nil
}

24
encodings/enc-zrle.go Normal file
View File

@@ -0,0 +1,24 @@
package encodings
import "io"
import "vncproxy/common"
type ZRLEEncoding struct {
//Colors []Color
}
func (z *ZRLEEncoding) Type() int32 {
return 16
}
func (z *ZRLEEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r io.Reader) (common.Encoding, error) {
conn := common.RfbReadHelper{r}
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
//bytesPerPixel := c.PixelFormat.BPP / 8
len, _ := conn.ReadUint32()
_, err := conn.ReadBytes(int(len))
if err != nil {
return nil, err
}
return z, nil
}