aboutsummaryrefslogtreecommitdiffstats
path: root/weaver/testdata/megatraceNoweave.jar
blob: 8a62739958f60833c968af3908fdcbdf7990f1a9 (plain)
ofshex dumpascii
0000 50 4b 03 04 14 00 08 00 08 00 48 8f 2e 2e 00 00 00 00 00 00 00 00 00 00 00 00 15 00 00 00 74 72 PK........H...................tr
0020 61 63 65 2f 4d 65 67 61 54 72 61 63 65 2e 63 6c 61 73 73 ad 56 df 53 1b 55 14 fe 6e 12 48 48 53 ace/MegaTrace.class.V.S.U..n.HHS
0040 b0 b4 85 16 8a 05 85 36 84 1f 29 a5 ad 15 da da 96 d2 5f 02 45 83 68 ad 16 2f e1 12 96 86 dd cc .......6..)......._.E.h../......
0060 66 03 c5 71 74 1c 1f 7c f1 49 67 fc 1b 9c f1 a5 3a 23 6d 75 74 7c f6 49 c7 d1 71 1c ff 00 1f f4 f..qt..|.Ig.....:#mut|.I..q.....
0080 c9 17 9f d4 ef ee 86 04 92 85 29 4e 33 73 72 ef ee 3d f7 9c f3 7d e7 dc 73 f7 bb 7f be fc 06 41 ..........)N3sr..=...}..s......A
00a0 f4 61 49 a0 c1 b1 65 5a 25 c7 55 46 4e e9 59 18 42 e0 89 45 b9 2c 93 59 69 66 92 37 66 17 55 da .aI...eZ%.UFN.Y.B..E.,.Yif.7f.U.
00c0 09 23 28 d0 54 a1 da 39 2e ed 3b ca 0e a3 46 a0 f6 8c 61 1a ce 39 81 60 bc 7b 5a 20 34 62 cd a9 .#(.T..9..;...F...a..9.`.{Z.4b..
00e0 18 c2 88 44 11 42 94 6e c6 0c 53 4d 14 96 66 95 3d 25 67 b3 4a a0 71 cc 4a cb ec b4 b4 0d fd 5c ...D.B.n..SM..f.=%g.J.q.J......\
0100 7c 19 72 16 8c bc c0 9e b1 0a 57 c3 02 6d 72 31 dd 29 6d ab 60 ce 75 ba ab 33 e5 40 4e 72 6b 26 |.r.......W..mr1.)m.`.u..3.@Nrk&
0120 3e 66 d9 99 a4 cc e7 18 ef 62 d2 2e 98 8e b1 a4 92 86 e9 28 db 94 d9 e4 05 77 eb 48 d6 ca 17 6c >f.......b.........(.....w.H...l
0140 35 bc 49 d7 05 7a dd 32 cc 49 8a d3 99 72 a4 63 a4 27 a5 ed 0c 77 8f 55 32 c1 48 0e 72 6f 7f 71 5.I..z.2.I...r.c.'...w.U2.H.ro.q
0160 6f ff 8a 92 cb ca ee bf 30 b7 6c a4 d5 26 e6 52 ab 79 47 2d 85 b1 97 a4 58 05 47 60 bf 67 cc b0 o.......0.l..&.R.yG-....X.G`.g..
0180 92 93 36 fd a4 1c 5b c9 a5 e1 18 f6 a3 a9 0e fb d0 4c 86 37 ec 76 a8 93 b9 58 98 9f d7 0c 1f 14 ..6...[..........L.7.v...X......
01a0 a8 f7 b0 b7 2b 0d 68 a8 3d 82 56 5a 8c 8f 55 ee 18 ee 9e d6 bc b7 45 d1 82 27 99 17 99 cb 29 73 ....+.h.=.VZ..U.......E..'....)s
01c0 4e a0 37 5e 0d a5 bb 6a b7 e7 8f 31 b5 a3 43 5b 78 4a 20 e2 58 de 9a c0 be 78 f5 0e ea 76 a2 4b N.7^...j...1..C[xJ..X....x...v.K
01e0 eb 1e 11 d8 eb 03 30 8c b8 40 38 a7 5f 64 cd 18 12 3a b4 6e f4 08 74 6e 9b d0 9c 6d a5 95 62 e0 ......0..@8._d...:.n..tn...m..b.
0200 e7 77 94 57 9f 84 c5 58 e8 fd 51 04 90 14 d8 bd 4e e2 5d c3 d1 1c 0e b0 d6 18 c6 8c dc 68 84 64 .w.W...X..Q.....N.]..........h.d
0220 ed c4 a5 40 b3 ae da 52 fd 94 cb 47 e0 e8 23 d6 19 4b c4 56 8e 3e 14 3e f5 16 1d bd 9b 56 39 c7 ...@...R...G..#..K.V.>.>.....V9.
0240 b0 cc fc 3a c5 ae c2 d4 82 6d ad e8 73 13 c6 90 c0 93 7e 55 b9 98 5a 35 9d 05 45 37 02 89 47 c7 ...:.....m..s.....~U..Z5..E7..G.
0260 14 c6 59 1d 51 c1 24 82 f8 ad ed 0b a7 c4 f2 73 38 1f c5 39 5c 60 28 3a b3 b3 6a de b2 55 55 66 ..Y.Q.$........s8..9\`(:..j..UUf
0280 07 8e 4b 86 12 7f d4 f3 c7 5e 12 2e 55 fc e8 7a 17 98 e7 8b 6a cb a7 07 59 ef eb 99 bd 2a 70 4e ..K......^..U..z....j...Y....*pN
02a0 eb ba 00 2f 1b 2a 3b 77 8d 0d aa 6a 53 e5 b3 d7 d2 3a 4d b9 c4 32 68 8d 57 f6 a1 e2 ba 17 d7 bc .../.*;w...jS....:M..2h.W.......
02c0 65 f5 cd 4a 3b 82 31 81 b3 9b 7d a5 d4 8e 5c c5 30 81 b6 5d a8 c5 0d 81 3a 6d 49 17 d4 8c c0 81 e..J;.1...}...\.0..]....:mI.....
02e0 ad fc 0b 8c 6e 76 78 45 39 97 8c 7c 4e 3a e9 85 9d 61 ec dd 1a 63 f5 59 17 e8 f2 a9 b2 51 f6 8c ....nvxE9..|N:...a...c.Y.....Q..
0300 b4 63 2c ab 94 91 31 a5 e3 9e a0 b3 55 e1 ed 8c 8f 69 74 69 3e 5e ae 02 9a fa bf 40 fb b6 04 ea .c,...1.....U....iti>^.....@....
0320 d7 43 8b c9 a8 c9 cb d5 ab 86 40 60 c1 88 e0 75 e6 3c a3 9c 09 37 c0 14 a6 74 4f 79 43 40 f0 16 .C........@`...u.<...7...tOyC@..
0340 8d a6 ac 82 9d 56 97 0d 7d 89 d5 97 3c f4 6b cb 02 b1 6b a6 a9 ec 91 ac cc e7 15 8f 70 ad e7 57 .....V..}...<.k...k.........p..W
0360 e0 88 0f 99 ee 11 48 17 9c 4b 2a 9d 95 b6 d4 a7 5e a0 c5 47 d1 53 a0 bb 36 9f c5 a9 d5 9c 1a 2f ......H..K*.....^..G.S..6....../
0380 98 19 ed c5 f7 ba 72 1f fd f7 b2 77 2f 13 47 46 37 df 76 9f 75 0f ea 88 c5 94 dc 75 42 1d 24 21 ......r....w/.GF7.v.u......uB.$!
03a0 04 82 02 10 e5 c8 2b 9f ff 75 7c 4a 72 14 1c 6b 12 f7 b1 eb 33 4e 02 88 f1 bf d6 7d 19 c4 6e fe ......+..u|Jr..k....3N.....}..n.
03c0 c7 3c 05 d4 a3 81 23 6f 4f ec a1 56 23 e7 c7 6a 22 5c 58 59 50 b6 02 fe 2d fe f8 7e 81 62 d6 f0 .<....#oO..V#..j"\XYP...-..~.b..
03e0 ef b6 9e 60 c3 4f bb 7c 9f a1 84 38 0e 7e 8e 03 5f a1 e5 66 e3 a1 fb 38 dc bb 86 a7 d7 70 74 0d ...`.O.|...8.~.._..f...8.....pt.
0400 bd 3d 0f 70 6c 62 7d e9 f8 a6 a5 be 7b a5 00 1b 5d 13 61 34 11 49 2b cd 0e 10 97 0e 36 e1 99 2e .=.plb}.....{...].a4.I+.....6...
0420 06 ab 67 83 38 e1 02 18 c4 49 9c e2 ee 56 1c c0 33 38 4d 70 11 7d e7 70 7c 96 ab 9a 9d 61 9c 29 ..g.8....I...V..38Mp.}.p|....a.)
0440 05 99 a0 66 50 83 4f 04 bf 46 68 0d 17 bf 45 e8 9e 0b bf 4c 49 ac 44 c9 08 2e 15 29 e9 12 be 94 ...fP.O..Fh...E....LI.D....)....
0460 88 13 94 21 86 26 12 7a e2 ba b8 41 a7 01 8e cd eb 60 2f 13 6c 4f 09 6c 39 19 51 57 ad 9e 58 1b ...!.&.z...A.....`/.lO.l9.QW..X.
0480 5c ef 4d de b6 12 c6 66 17 99 ce e3 95 72 1c 01 ff 38 3e a0 7c a8 e3 78 4f 4f fc e3 b8 b6 6d 1c \.M....f.....r...8>.|..xOO....m.
04a0 8d f4 bf 77 db 38 ea 70 1d cf 17 eb 6b 98 3a 9a ab ba 44 e3 f8 43 4c 06 50 36 57 ef 12 ac c3 6e ...w.8.p....k.:...D..CL.P6W....n
04c0 e1 6c 23 b1 75 78 01 2f ba 23 0f 30 b5 5f e2 7c 34 b0 75 a3 75 4d 86 74 07 d9 74 37 17 fb 84 0e .l#.ux./.#.0._.|4.u.uM.t..t7....
04e0 2d b0 48 59 aa f1 72 db 5a ac f9 70 e2 21 5e 11 28 a7 35 e0 fa bc 89 57 1f a3 cf 5a cf e7 21 6a -.HY..r.Z..p.!^.(.5....W...Z..!j
0500 6b f3 91 44 cf 43 dc f2 68 28 3b 15 78 0d 5d 45 c6 fa 8a d1 05 1b 6f 97 cb dd 3b 8f 87 36 70 14 k..D.C..h(;.x.]E......o...;..6p.
0520 2c d2 5e 87 19 72 54 79 98 1f 40 56 6e 3e bc e9 30 cf ba 04 47 90 76 b5 e6 a0 8a 9d a1 96 4f f3 ,.^..rTy..@Vn>..0...G.v.......O.
0540 b5 fc 50 e7 73 87 cf 27 be 3e 29 a5 ba da e7 9e f4 8f d8 81 fd 8b ed 4f ca 5f 22 28 5c 36 20 a6 ..P.s..'.>)............O._"(\6..
0560 f5 40 b8 81 9a 1a 84 96 a4 61 42 84 ca da 7e 33 57 bb 9e 56 fe e6 d8 e0 59 e4 78 50 47 45 9a f9 .@.......aB...~3W..V....Y.xPGE..
0580 85 a8 d8 85 d9 7d db ad f9 76 d7 22 c4 6f 54 49 c2 a0 af ab e2 31 e4 cf ad ec 45 4d b4 28 c5 75 .....}...v.".oTI.....1....EM.(.u
05a0 c7 6d 18 fa 97 a5 fc 88 f1 91 a1 24 1b bd 91 cb ab e4 8a 65 df e1 cd 47 97 1b bb b2 5c 9c 73 fa .m.........$.......e...G....\.s.
05c0 d3 fc d4 4a 3a 2a ef cc 49 47 26 f3 76 7a 20 59 11 9c 7b 15 d1 64 3b 45 27 77 0f e5 24 45 e7 4a ...J:*..IG&.vz.Y..{..d;E'w..$E.J
05e0 17 a4 4d f9 82 f2 3d 71 f2 94 8b 0e 4a 17 25 4e 39 47 79 93 f2 36 e5 5d ca c7 94 1f 28 3f 51 7e ..M...=q....J.%N9Gy..6.]....(?Q~
0600 a1 fc 4a 34 dc 13 38 4e 39 45 a1 cd c0 5b 94 77 28 9f 50 3e a5 fc 4c f9 9d f2 c7 7f 50 4b 07 08 ..J4..8N9E...[.w(.P>..L.....PK..
0620 b5 57 8b d5 e9 05 00 00 7d 0e 00 00 50 4b 03 04 14 00 08 00 08 00 48 8f 2e 2e 00 00 00 00 00 00 .W......}...PK........H.........
0640 00 00 00 00 00 00 1c 00 00 00 74 72 61 63 65 2f 4d 65 67 61 54 72 61 63 65 24 4d 61 72 6b 65 72 ..........trace/MegaTrace$Marker
0660 2e 63 6c 61 73 73 3b f5 6f d7 3e 06 66 06 5d 06 6e 46 06 b1 92 a2 c4 e4 54 7d df d4 f4 c4 10 10 .class;.o.>.f.].nF......T}......
0680 4b c5 37 b1 28 3b b5 88 9d 81 91 91 41 20 2b b1 2c 51 3f 27 31 2f 5d df 3f 29 2b 35 b9 84 9d 81 K.7.(;......A.+.,Q?'1/].?)+5....
06a0 99 91 81 2b 38 bf b4 28 39 d5 2d 33 27 95 91 81 0f ae 51 0f a4 98 91 81 c7 33 2f 2f b5 c8 39 27 ...+8..(9.-3'.....Q......3//..9'
06c0 b1 b8 38 b5 98 91 81 1f cd 78 76 06 0e 46 06 36 88 1d 6c 8a 0c 4c 0c 2c 0c 10 c0 c4 c0 0a 26 d9 ..8......xv..F.6..l..L.,......&.
06e0 18 d8 81 34 17 03 23 90 cd c9 c0 c5 c6 08 00 50 4b 07 08 9b ca 1c 02 89 00 00 00 ad 00 00 00 50 ...4..#........PK..............P
0700 4b 03 04 14 00 08 00 08 00 48 8f 2e 2e 00 00 00 00 00 00 00 00 00 00 00 00 15 00 00 00 74 72 61 K........H...................tra
0720 63 65 2f 45 78 65 63 54 72 61 63 65 2e 63 6c 61 73 73 6d 53 5d 4f 13 41 14 3d d3 ee 5a ba 54 a0 ce/ExecTrace.classmS]O.A.=..Z.T.
0740 a0 c8 87 22 28 42 21 b1 1b df 8c 12 12 44 4c 4c 0a 1a 21 24 a8 2f c3 74 6c b7 94 dd 66 76 0a f8 ..."(B!......DLL..!$./.tl...fv..
0760 2f 7c 31 f1 47 f0 20 f8 80 91 44 df 78 f0 47 a9 77 a6 9b 0a cd 6e 72 67 f7 de 39 f7 9c 7b 67 ee /|1.G.....D.x.G.w....nrg..9..{g.
0780 fe fe f3 e3 27 b2 78 88 12 c3 a0 56 5c 48 7f ed 48 8a 2d f3 95 03 eb 06 d7 65 8d 27 c1 2c c3 68 ....'.x....V\H..H.-......e.'.,.h
07a0 4f 70 76 9d ab 3d a9 72 70 19 c6 78 43 cc b6 a4 da 0c c2 5a 53 ea 28 7c 19 c6 9a 87 42 32 14 2b Opv..=.rp..xC......ZS.(|....B2.+
07c0 3d 0a 4f 19 fa 96 44 33 08 03 bd cc 90 2d 2d 6c 33 38 ab 51 95 b0 9e a1 e9 6c 15 50 80 e7 21 83 =.O...D3.....--l38.Q.....l.P..!.
07e0 eb 54 4e 25 08 e5 46 7b 7f 57 aa 2d be db 24 e0 70 25 12 bc b9 cd 55 60 fc 24 78 6d c9 72 16 50 .TN%..F{.W.-..$.p%....U`.$xm.r.P
0800 34 99 0e 86 89 57 d7 83 98 f4 78 dc 92 42 bf fa c0 30 52 5a 48 29 68 2a 52 b5 72 07 d4 28 1f 4a 4....W....x..B...0RZH)h*R.r..(.J
0820 7e 20 55 79 a5 b1 f9 31 d4 75 a9 03 51 40 0e 7d 79 2a 66 8c 61 9e a0 7e 02 f5 9b 3c ac f9 1b d1 ~.Uy...1.u..Q@.}y*f.a..~...<....
0840 8a 75 9f 45 ed b0 ba 76 24 64 4b 07 51 98 c3 84 87 49 53 44 be ce e3 0e c2 b6 fb d6 12 99 d6 86 .u.E...v$dK.Q....ISD............
0860 13 4a 6f 33 6a 2b 21 5f 04 a6 8b 81 6e 5d e5 06 3f e0 0c 73 29 b5 bd 8e 82 50 8b b6 7e 2e 45 93 .Jo3j+!_....n]..?..s)....P..~.E.
0880 2b 6e f4 18 26 52 80 1d 00 d1 8e a7 75 98 14 35 9d b2 d7 29 69 35 0a b5 3c d2 98 a1 42 1d d0 11 +n..&R......u..5...)i5..<...B...
08a0 93 8d 9b c3 00 e0 d2 9a 87 47 91 7e f2 66 d1 79 9c ef 18 38 a1 77 06 83 b4 1a 3c 68 d4 86 6c 04 .........G.~.f.y...8.w....<h..l.
08c0 e4 15 bb 19 3e bd cd ae bb 78 86 91 f4 94 42 07 80 1b 56 30 8f 9b 18 25 d4 2d 2b 64 28 ca 96 14 ....>....x....B...V0...%.-+d(...
08e0 18 3a c5 f8 ce 05 9c af e7 98 dc 39 c3 ed 5f 56 ea 3f c3 50 97 e1 0e a6 ae 30 94 ec 3e e0 9d e2 .:.........9.._V.?.P.....0..>...
0900 ee 05 5c e7 38 7b dc 93 eb 25 b9 9e 19 c8 2b b9 0b 89 7a ff 39 32 a4 3a fd 0d 33 27 3d c9 fd 49 ..\.8{...%....+...z.92.:..3'=..I
0920 b2 8b 7b b6 c1 fb f6 9c de a5 fc 14 96 c9 3d ac 4b 25 89 22 6b 8f c6 75 c1 16 c1 9c bf c9 03 a4 ..{...........=.K%."k..u........
0940 7d e1 3d 59 40 f6 86 ac 81 07 b4 3e ce 50 26 8d 56 25 fd 97 b5 62 9f c8 3e 5f 62 51 c6 c7 9c e9 }.=Y@......>.P&.V%...b..>_bQ....
0960 d4 61 dd f8 3c f9 7b 58 5f 7d e2 d3 20 05 ad 58 fa 87 91 da 8b 5b 86 f6 f2 d4 f0 46 55 97 45 a4 .a..<.{X_}.....X.....[.....FU.E.
0980 a4 af 65 ac ab 5c 73 3f 56 e2 91 df d3 a6 1d 69 7b c9 b0 97 5d 24 5b 26 db 27 6b 91 7d f9 07 50 ..e..\s?V......i{...]$[&.'k.}..P
09a0 4b 07 08 4c 02 94 c2 6d 02 00 00 9b 04 00 00 50 4b 01 02 14 00 14 00 08 00 08 00 48 8f 2e 2e b5 K..L...m.......PK..........H....
09c0 57 8b d5 e9 05 00 00 7d 0e 00 00 15 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 74 72 61 W......}.....................tra
09e0 63 65 2f 4d 65 67 61 54 72 61 63 65 2e 63 6c 61 73 73 50 4b 01 02 14 00 14 00 08 00 08 00 48 8f ce/MegaTrace.classPK..........H.
0a00 2e 2e 9b ca 1c 02 89 00 00 00 ad 00 00 00 1c 00 00 00 00 00 00 00 00 00 00 00 00 00 2c 06 00 00 ............................,...
0a20 74 72 61 63 65 2f 4d 65 67 61 54 72 61 63 65 24 4d 61 72 6b 65 72 2e 63 6c 61 73 73 50 4b 01 02 trace/MegaTrace$Marker.classPK..
0a40 14 00 14 00 08 00 08 00 48 8f 2e 2e 4c 02 94 c2 6d 02 00 00 9b 04 00 00 15 00 00 00 00 00 00 00 ........H...L...m...............
0a60 00 00 00 00 00 00 ff 06 00 00 74 72 61 63 65 2f 45 78 65 63 54 72 61 63 65 2e 63 6c 61 73 73 50 ..........trace/ExecTrace.classP
0a80 4b 05 06 00 00 00 00 03 00 03 00 d0 00 00 00 af 09 00 00 00 00 K....................
terpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */
#include <stdio.h>
#include <string.h>
#include <rfb/Exception.h>
#include <rfb/secTypes.h>
#include <rfb/CapsList.h>
#include <rfb/SMsgReaderV3.h>
#include <rfb/SMsgWriterV3.h>
#include <rfb/SConnection.h>
#include <rfb/ServerCore.h>

#include <rfb/LogWriter.h>

using namespace rfb;

static LogWriter vlog("SConnection");

// AccessRights values
const SConnection::AccessRights SConnection::AccessView       = 0x0001;
const SConnection::AccessRights SConnection::AccessKeyEvents  = 0x0002;
const SConnection::AccessRights SConnection::AccessPtrEvents  = 0x0004;
const SConnection::AccessRights SConnection::AccessCutText    = 0x0008;
const SConnection::AccessRights SConnection::AccessDefault    = 0x03ff;
const SConnection::AccessRights SConnection::AccessNoQuery    = 0x0400;
const SConnection::AccessRights SConnection::AccessFull       = 0xffff;


SConnection::SConnection(SSecurityFactory* secFact, bool reverseConnection_)
  : readyForSetColourMapEntries(false),
    is(0), os(0), reader_(0), writer_(0),
    security(0), securityFactory(secFact), state_(RFBSTATE_UNINITIALISED),
    reverseConnection(reverseConnection_)
{
  defaultMajorVersion = 3;
  defaultMinorVersion = 8;
  if (rfb::Server::protocol3_3)
    defaultMinorVersion = 3;

  cp.setVersion(defaultMajorVersion, defaultMinorVersion);
}

SConnection::~SConnection()
{
  if (security) security->destroy();
  deleteReaderAndWriter();
}

void SConnection::deleteReaderAndWriter()
{
  delete reader_;
  reader_ = 0;
  delete writer_;
  writer_ = 0;
}

void SConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
{
  is = is_;
  os = os_;
}

void SConnection::initialiseProtocol()
{
  cp.writeVersion(os);
  state_ = RFBSTATE_PROTOCOL_VERSION;
}

void SConnection::processMsg()
{
  switch (state_) {
  case RFBSTATE_PROTOCOL_VERSION: processVersionMsg();      break;
  case RFBSTATE_SECURITY_TYPE:    processSecurityTypeMsg(); break;
  case RFBSTATE_TIGHT_TUNN_TYPE:  processTunnelTypeMsg();   break;
  case RFBSTATE_TIGHT_AUTH_TYPE:  processAuthTypeMsg();     break;
  case RFBSTATE_SECURITY:         processSecurityMsg();     break;
  case RFBSTATE_INITIALISATION:   processInitMsg();         break;
  case RFBSTATE_NORMAL:           reader_->readMsg();       break;
  case RFBSTATE_QUERYING:
    throw Exception("SConnection::processMsg: bogus data from client while "
                    "querying");
  case RFBSTATE_UNINITIALISED:
    throw Exception("SConnection::processMsg: not initialised yet?");
  default:
    throw Exception("SConnection::processMsg: invalid state");
  }
}

void SConnection::processVersionMsg()
{
  vlog.debug("reading protocol version");
  bool done;
  if (!cp.readVersion(is, &done)) {
    state_ = RFBSTATE_INVALID;
    throw Exception("reading version failed: not an RFB client?");
  }
  if (!done) return;

  vlog.info("Client needs protocol version %d.%d",
            cp.majorVersion, cp.minorVersion);

  if (cp.majorVersion != 3) {
    // unknown protocol version
    char msg[256];
    sprintf(msg,"Error: client needs protocol version %d.%d, server has %d.%d",
            cp.majorVersion, cp.minorVersion,
            defaultMajorVersion, defaultMinorVersion);
    throwConnFailedException(msg);
  }

  if (cp.minorVersion != 3 && cp.minorVersion != 7 && cp.minorVersion != 8) {
    vlog.error("Client uses unofficial protocol version %d.%d",
               cp.majorVersion,cp.minorVersion);
    if (cp.minorVersion >= 8)
      cp.minorVersion = 8;
    else if (cp.minorVersion == 7)
      cp.minorVersion = 7;
    else
      cp.minorVersion = 3;
    vlog.error("Assuming compatibility with version %d.%d",
               cp.majorVersion,cp.minorVersion);
  }

  versionReceived();

  std::list<rdr::U8> secTypes;
  std::list<rdr::U8>::iterator i;
  securityFactory->getSecTypes(&secTypes, reverseConnection);

  if (cp.isVersion(3,3)) {

    // cope with legacy 3.3 client only if "no authentication" or "vnc
    // authentication" is supported.
    for (i=secTypes.begin(); i!=secTypes.end(); i++) {
      if (*i == secTypeNone || *i == secTypeVncAuth) break;
    }
    if (i == secTypes.end()) {
      char msg[256];
      sprintf(msg,"No supported security type for %d.%d client",
              cp.majorVersion, cp.minorVersion);
      throwConnFailedException(msg);
    }

    os->writeU32(*i);
    if (*i == secTypeNone) os->flush();
    state_ = RFBSTATE_SECURITY;
    security = securityFactory->getSSecurity(*i, reverseConnection);
    processSecurityMsg();
    return;
  }

  // Add a special security type to advertise TightVNC protocol extensions.
  secTypes.push_back(secTypeTight);

  // list supported security types for >=3.7 clients

  if (secTypes.empty())
    throwConnFailedException("No supported security types");

  os->writeU8(secTypes.size());
  for (i=secTypes.begin(); i!=secTypes.end(); i++)
    os->writeU8(*i);
  os->flush();
  state_ = RFBSTATE_SECURITY_TYPE;
}


void SConnection::processSecurityTypeMsg()
{
  vlog.debug("processing security type message");
  int secType = is->readU8();

  if (secType == secTypeTight) {
    vlog.info("Enabling TightVNC protocol extensions");
    cp.tightExtensionsEnabled = true;
    offerTunneling();
  } else {
    processSecurityType(secType);
  }
}

//
// TightVNC-specific protocol initialization (tunneling, authentication)
//

void SConnection::offerTunneling()
{
  vlog.debug("offering list of tunneling methods");
  int nTypes = 0;

  // Advertise our tunneling capabilities (currently, nothing to advertise).
  os->writeU32(nTypes);
  os->flush();

  if (nTypes) {
    // NOTE: Never executed in current version.
    state_ = RFBSTATE_TIGHT_TUNN_TYPE;
  } else {
    offerAuthentication();
  }
}

// NOTE: This function is never called in current version.
void SConnection::processTunnelTypeMsg()
{
  vlog.debug("processing tunneling type message (TightVNC extension)");
  int tunnelType = is->readU32();
  vlog.error("unsupported tunneling type %d requested, ignoring", tunnelType);
  offerAuthentication();
}

void SConnection::offerAuthentication()
{
  vlog.debug("offering list of authentication methods");

  // See processVersionMsg(), the code below is similar.

  std::list<rdr::U8> secTypes;
  std::list<rdr::U8>::iterator i;

  // NOTE: In addition to standard security types, we might want to offer
  //       TightVNC-specific authentication types. But currently we support
  //       only the standard security types: secTypeNone and secTypeVncAuth.
  securityFactory->getSecTypes(&secTypes, reverseConnection);

  CapsList caps;
  for (i = secTypes.begin(); i != secTypes.end(); i++) {
    // FIXME: Capability info should be provided by SSecurity objects.
    switch (*i) {
    case secTypeNone:     caps.addStandard(*i, "NOAUTH__"); break;
    case secTypeVncAuth:  caps.addStandard(*i, "VNCAUTH_"); break;
    default:
      // This should not ever happen.
      vlog.error("not offering unknown security type %d", (int)*i);
    }
  }

  if (caps.getSize() < 1)
    throwConnFailedException("No supported security types");

  if (caps.includesOnly(secTypeNone)) {
    // Special case - if caps includes nothing else than secTypeNone, we send
    // an empty capability list and do not expect security type selection from
    // the client.
    os->writeU32(0);
    os->flush();
    processSecurityType(secTypeNone);
  } else {
    // Normal case - sending the list of authentication capabilities.
    os->writeU32(caps.getSize());
    caps.write(os);
    os->flush();
    state_ = RFBSTATE_TIGHT_AUTH_TYPE;
  }
}

void SConnection::processAuthTypeMsg()
{
  vlog.debug("processing authentication type message (TightVNC extension)");

  // NOTE: Currently, we support only the standard security types, so we
  //       just pass TightVNC authentication type for standard processing,
  //       just as it was usual RFB security type.
  int secType = is->readU32();
  processSecurityType(secType);
}

//
// End of TightVNC-specific code
//

void SConnection::processSecurityType(int secType)
{
  // Verify that the requested security type should be offered
  std::list<rdr::U8> secTypes;
  std::list<rdr::U8>::iterator i;
  securityFactory->getSecTypes(&secTypes, reverseConnection);
  for (i=secTypes.begin(); i!=secTypes.end(); i++)
    if (*i == secType) break;
  if (i == secTypes.end())
    throw Exception("Requested security type not available");

  vlog.info("Client requests security type %s(%d)",
            secTypeName(secType),secType);

  try {
    state_ = RFBSTATE_SECURITY;
    security = securityFactory->getSSecurity(secType, reverseConnection);
  } catch (rdr::Exception& e) {
    throwConnFailedException(e.str());
  }

  processSecurityMsg();
}

void SConnection::processSecurityMsg()
{
  vlog.debug("processing security message");
  try {
    bool done = security->processMsg(this);
    if (done) {
      state_ = RFBSTATE_QUERYING;
      queryConnection(security->getUserName());
    }
  } catch (AuthFailureException& e) {
    vlog.error("AuthFailureException: %s", e.str());
    os->writeU32(secResultFailed);
    if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message
      os->writeString(e.str());
    os->flush();
    throw;
  }
}

void SConnection::processInitMsg()
{
  vlog.debug("reading client initialisation");
  reader_->readClientInit();
}

void SConnection::throwConnFailedException(const char* msg)
{
  vlog.info(msg);
  if (state_ == RFBSTATE_PROTOCOL_VERSION) {
    if (cp.majorVersion == 3 && cp.minorVersion == 3) {
      os->writeU32(0);
      os->writeString(msg);
      os->flush();
    } else {
      os->writeU8(0);
      os->writeString(msg);
      os->flush();
    }
  }
  state_ = RFBSTATE_INVALID;
  throw ConnFailedException(msg);
}

void SConnection::writeConnFailedFromScratch(const char* msg,
                                             rdr::OutStream* os)
{
  os->writeBytes("RFB 003.003\n", 12);
  os->writeU32(0);
  os->writeString(msg);
  os->flush();
}

void SConnection::versionReceived()
{
}

void SConnection::authSuccess()
{
}

void SConnection::queryConnection(const char* userName)
{
  approveConnection(true);
}

void SConnection::approveConnection(bool accept, const char* reason)
{
  if (state_ != RFBSTATE_QUERYING)
    throw Exception("SConnection::approveConnection: invalid state");

  if (!reason) reason = "Authentication failure";

  if (!cp.beforeVersion(3,8) || security->getType() != secTypeNone) {
    if (accept) {
      os->writeU32(secResultOK);
    } else {
      os->writeU32(secResultFailed);
      if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message
        os->writeString(reason);
    }
    os->flush();
  }

  if (accept) {
    state_ = RFBSTATE_INITIALISATION;
    reader_ = new SMsgReaderV3(this, is);
    writer_ = new SMsgWriterV3(&cp, os);
    authSuccess();
  } else {
    state_ = RFBSTATE_INVALID;
    throw AuthFailureException(reason);
  }
}

void SConnection::setInitialColourMap()
{
}

void SConnection::clientInit(bool shared)
{
  writer_->writeServerInit();

  // FIXME: Send interaction capabilities via writer_?
  if (cp.tightExtensionsEnabled)
    sendInteractionCaps();

  state_ = RFBSTATE_NORMAL;
}

// FIXME: Move sendInteractionCaps() to a class derived from SMsgWriterV3?
void SConnection::sendInteractionCaps()
{
  // Advertise support for non-standard server-to-client messages
  // (this version has nothing to advertise).
  CapsList scaps;

  // Advertise support for non-standard client-to-server messages
  // (this version has nothing to advertise).
  CapsList ccaps;

  // Advertise all supported encoding types (except raw encoding).
  CapsList ecaps;

  // First, add true encodings.
  for (unsigned int i = 1; i <= encodingMax; i++) {
    if (Encoder::supported(i)) {
      // FIXME: Capability info should be provided by Encoder objects.
      switch (i) {
      case encodingRRE:      ecaps.addStandard(i, "RRE_____"); break;
      case encodingCoRRE:    ecaps.addStandard(i, "CORRE___"); break;
      case encodingHextile:  ecaps.addStandard(i, "HEXTILE_"); break;
      case encodingZRLE:     ecaps.addStandard(i, "ZRLE____"); break;
      case encodingTight:    ecaps.addTightExt(i, "TIGHT___"); break;
      default:
        // This should not ever happen.
        vlog.error("not advertising unknown encoding type %d", (int)i);
      }
    }
  }

  // CopyRect is special - Encoder::supported() returns 0 for it,
  // that's why we add it here explicitly.
  ecaps.addStandard(encodingCopyRect,             "COPYRECT");

  // Add supported pseudo encodings as well.
  ecaps.addTightExt(pseudoEncodingCompressLevel0, "COMPRLVL");
  ecaps.addTightExt(pseudoEncodingQualityLevel0,  "JPEGQLVL");
  ecaps.addTightExt(pseudoEncodingXCursor,        "X11CURSR");
  ecaps.addTightExt(pseudoEncodingCursor,         "RCHCURSR");
  ecaps.addTightExt(pseudoEncodingLastRect,       "LASTRECT");
  ecaps.addStandard(pseudoEncodingDesktopSize,    "NEWFBSIZ");

  os->writeU16(scaps.getSize());
  os->writeU16(ccaps.getSize());
  os->writeU16(ecaps.getSize());
  os->writeU16(0);
  if (scaps.getSize())
    scaps.write(os);
  if (ccaps.getSize())
    ccaps.write(os);
  if (ecaps.getSize())
    ecaps.write(os);
  os->flush();
}

void SConnection::setPixelFormat(const PixelFormat& pf)
{
  SMsgHandler::setPixelFormat(pf);
  readyForSetColourMapEntries = true;
}

void SConnection::framebufferUpdateRequest(const Rect& r, bool incremental)
{
  if (!readyForSetColourMapEntries) {
    readyForSetColourMapEntries = true;
    if (!cp.pf().trueColour) {
      setInitialColourMap();
    }
  }
}