mirror of
https://github.com/amitbet/vncproxy.git
synced 2025-09-24 19:09:13 +00:00
added RfbReaderHelper which wraps the connection reader and allows to add listeners like the recorder or proxy
added fbs recorder, and tested it against vine vnc server 5.0.1 and rfbplayer 1.4 —> works! (there is still some problem with tight VNC server)
This commit is contained in:
@@ -1,26 +1,20 @@
|
||||
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
|
||||
}
|
||||
|
||||
//////////
|
||||
package encodings
|
||||
|
||||
import "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 *common.RfbReadHelper) (common.Encoding, error) {
|
||||
z.copyRectSrcX, _ = r.ReadUint16()
|
||||
z.copyRectSrcY, _ = r.ReadUint16()
|
||||
return z, nil
|
||||
}
|
||||
|
||||
//////////
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package encodings
|
||||
|
||||
import (
|
||||
"io"
|
||||
"vncproxy/common"
|
||||
)
|
||||
|
||||
@@ -13,16 +12,15 @@ 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}
|
||||
func (z *CoRREEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) {
|
||||
bytesPerPixel := int(pixelFmt.BPP / 8)
|
||||
numOfSubrectangles, _ := conn.ReadUint32()
|
||||
numOfSubrectangles, _ := r.ReadUint32()
|
||||
|
||||
//read whole rect background color
|
||||
conn.ReadBytes(bytesPerPixel)
|
||||
r.ReadBytes(bytesPerPixel)
|
||||
|
||||
//read all individual rects (color=BPP + x=16b + y=16b + w=16b + h=16b)
|
||||
_, err := conn.ReadBytes(int(numOfSubrectangles) * (bytesPerPixel + 4))
|
||||
_, err := r.ReadBytes(int(numOfSubrectangles) * (bytesPerPixel + 4))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@@ -1,85 +1,80 @@
|
||||
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
|
||||
}
|
||||
package encodings
|
||||
|
||||
import "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 *common.RfbReadHelper) (common.Encoding, error) {
|
||||
bytesPerPixel := int(pixelFmt.BPP) / 8
|
||||
|
||||
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 := r.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)
|
||||
r.ReadBytes(tw * th * bytesPerPixel)
|
||||
//fmt.Printf("hextile reader: HextileRaw\n")
|
||||
continue
|
||||
}
|
||||
if (subencoding & HextileBackgroundSpecified) != 0 {
|
||||
r.ReadBytes(int(bytesPerPixel))
|
||||
}
|
||||
if (subencoding & HextileForegroundSpecified) != 0 {
|
||||
r.ReadBytes(int(bytesPerPixel))
|
||||
}
|
||||
if (subencoding & HextileAnySubrects) == 0 {
|
||||
//fmt.Printf("hextile reader: no Subrects\n")
|
||||
continue
|
||||
}
|
||||
//fmt.Printf("hextile reader: handling Subrects\n")
|
||||
nSubrects, err := r.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];
|
||||
r.ReadBytes(bufsize)
|
||||
}
|
||||
}
|
||||
|
||||
// len, _ := readUint32(c.c)
|
||||
// _, err := readBytes(c.c, int(len))
|
||||
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
return z, nil
|
||||
}
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package encodings
|
||||
|
||||
import (
|
||||
"io"
|
||||
"vncproxy/common"
|
||||
)
|
||||
|
||||
@@ -16,9 +15,9 @@ func (*RawEncoding) Type() int32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (*RawEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r io.Reader) (common.Encoding, error) {
|
||||
func (*RawEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) (common.Encoding, error) {
|
||||
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
|
||||
conn := common.RfbReadHelper{r}
|
||||
//conn := common.RfbReadHelper{Reader:r}
|
||||
bytesPerPixel := int(pixelFmt.BPP / 8)
|
||||
//pixelBytes := make([]uint8, bytesPerPixel)
|
||||
|
||||
@@ -31,7 +30,7 @@ func (*RawEncoding) Read(pixelFmt *common.PixelFormat, rect *common.Rectangle, r
|
||||
|
||||
for y := uint16(0); y < rect.Height; y++ {
|
||||
for x := uint16(0); x < rect.Width; x++ {
|
||||
if _, err := conn.ReadBytes(bytesPerPixel); err != nil {
|
||||
if _, err := r.ReadBytes(bytesPerPixel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@@ -1,28 +1,27 @@
|
||||
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
|
||||
}
|
||||
package encodings
|
||||
|
||||
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 *common.RfbReadHelper) (common.Encoding, error) {
|
||||
//conn := common.RfbReadHelper{Reader:r}
|
||||
bytesPerPixel := int(pixelFmt.BPP / 8)
|
||||
numOfSubrectangles, _ := r.ReadUint32()
|
||||
|
||||
//read whole rect background color
|
||||
r.ReadBytes(bytesPerPixel)
|
||||
|
||||
//read all individual rects (color=BPP + x=16b + y=16b + w=16b + h=16b)
|
||||
_, err := r.ReadBytes(int(numOfSubrectangles) * (bytesPerPixel + 8))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return z, nil
|
||||
}
|
||||
|
@@ -1,334 +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
|
||||
}
|
||||
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, r *common.RfbReadHelper) (common.Encoding, error) {
|
||||
bytesPixel := calcTightBytePerPixel(pixelFmt)
|
||||
//conn := common.RfbReadHelper{Reader:reader}
|
||||
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
|
||||
|
||||
//var subencoding uint8
|
||||
subencoding, err := r.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
|
||||
r.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 := r.ReadCompactLen()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Printf("reading jpeg size=%d\n", len)
|
||||
r.ReadBytes(len)
|
||||
return t, nil
|
||||
default:
|
||||
|
||||
if compType > TightJpeg {
|
||||
fmt.Println("Compression control byte is incorrect!")
|
||||
}
|
||||
|
||||
handleTightFilters(subencoding, pixelFmt, rect, r)
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
|
||||
func handleTightFilters(subencoding uint8, pixelFmt *common.PixelFormat, rect *common.Rectangle, r *common.RfbReadHelper) {
|
||||
//conn := common.RfbReadHelper{Reader: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 = r.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 := r.ReadUint8()
|
||||
paletteSize := colorCount + 1 // add one more
|
||||
fmt.Printf("----PALETTE_FILTER: paletteSize=%d bytesPixel=%d\n", paletteSize, bytesPixel)
|
||||
//complete palette
|
||||
r.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 = r.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)
|
||||
r.ReadTightData(lengthCurrentbpp)
|
||||
case TightFilterCopy: //BASIC_FILTER
|
||||
fmt.Printf("----BASIC_FILTER: bytesPixel=%d\n", bytesPixel)
|
||||
r.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
|
||||
}
|
||||
|
@@ -1,24 +1,23 @@
|
||||
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
|
||||
}
|
||||
package encodings
|
||||
|
||||
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 *common.RfbReadHelper) (common.Encoding, error) {
|
||||
//conn := common.RfbReadHelper{Reader:r}
|
||||
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
|
||||
//bytesPerPixel := c.PixelFormat.BPP / 8
|
||||
len, _ := r.ReadUint32()
|
||||
_, err := r.ReadBytes(int(len))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return z, nil
|
||||
}
|
||||
|
@@ -1,24 +1,23 @@
|
||||
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
|
||||
}
|
||||
package encodings
|
||||
|
||||
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 *common.RfbReadHelper) (common.Encoding, error) {
|
||||
//conn := common.RfbReadHelper{Reader: r}
|
||||
//conn := &DataSource{conn: conn.c, PixelFormat: conn.PixelFormat}
|
||||
//bytesPerPixel := c.PixelFormat.BPP / 8
|
||||
len, _ := r.ReadUint32()
|
||||
_, err := r.ReadBytes(int(len))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return z, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user