Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(346)

Delta Between Two Patch Sets: ssh/messages.go

Issue 5728053: code review 5728053: go.crypto/ssh: improve marshal performance (Closed)
Left Patch Set: diff -r 91ccd0bd392e https://code.google.com/p/go.crypto Created 13 years, 1 month ago
Right Patch Set: diff -r 861daa0e6913 https://code.google.com/p/go.crypto Created 13 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « ssh/common.go ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2011 The Go Authors. All rights reserved. 1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package ssh 5 package ssh
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "encoding/binary"
9 "io" 10 "io"
10 "math/big" 11 "math/big"
11 "reflect" 12 "reflect"
12 ) 13 )
13 14
14 // These are SSH message type numbers. They are scattered around several 15 // These are SSH message type numbers. They are scattered around several
15 // documents but many were taken from 16 // documents but many were taken from
16 // http://www.iana.org/assignments/ssh-parameters/ssh-parameters.xml#ssh-paramet ers-1 17 // http://www.iana.org/assignments/ssh-parameters/ssh-parameters.xml#ssh-paramet ers-1
17 const ( 18 const (
18 msgDisconnect = 1 19 msgDisconnect = 1
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 out.uint8(uint8(field.Index(j).Uint())) 304 out.uint8(uint8(field.Index(j).Uint()))
304 } 305 }
305 case reflect.Uint32: 306 case reflect.Uint32:
306 out.uint32(uint32(field.Uint())) 307 out.uint32(uint32(field.Uint()))
307 case reflect.String: 308 case reflect.String:
308 s := field.String() 309 s := field.String()
309 out.int(len(s)) 310 out.int(len(s))
310 out = append(out, s...) 311 out = append(out, s...)
311 case reflect.Slice: 312 case reflect.Slice:
312 switch t.Elem().Kind() { 313 switch t.Elem().Kind() {
313 case reflect.Uint8: 314 case reflect.Uint8:
remyoudompheng 2012/03/03 09:45:18 isn't it a []byte in most cases? it could be type-
dave_cheney.net 2012/03/03 10:51:11 Done.
314 length := field.Len()
315 if v.Type().Field(i).Tag.Get("ssh") != "rest" { 315 if v.Type().Field(i).Tag.Get("ssh") != "rest" {
316 » » » » » out.int(length) 316 » » » » » out.int(field.Len())
317 } 317 }
318 » » » » for j := 0; j < length; j++ { 318 » » » » out = append(out, field.Bytes()...)
319 » » » » » out.uint8(byte(field.Index(j).Uint()))
320 » » » » }
321 case reflect.String: 319 case reflect.String:
322 offset := len(out) 320 offset := len(out)
323 out.uint32(0) 321 out.uint32(0)
324 if n := field.Len(); n > 0 { 322 if n := field.Len(); n > 0 {
325 for j := 0; j < n; j++ { 323 for j := 0; j < n; j++ {
326 f := field.Index(j) 324 f := field.Index(j)
327 if j != 0 { 325 if j != 0 {
328 out.uint8(',') 326 out.uint8(',')
329 } 327 }
330 out = append(out, f.String()...) 328 out = append(out, f.String()...)
331 } 329 }
332 // overwrite length value 330 // overwrite length value
333 » » » » » marshalUint32(out[offset:], uint32(len(o ut)-offset-4)) 331 » » » » » binary.BigEndian.PutUint32(out[offset:], uint32(len(out)-offset-4))
334 } 332 }
335 default: 333 default:
336 panic("slice of unknown type") 334 panic("slice of unknown type")
337 } 335 }
338 case reflect.Ptr: 336 case reflect.Ptr:
339 if t == bigIntType { 337 if t == bigIntType {
340 var n *big.Int 338 var n *big.Int
341 nValue := reflect.ValueOf(&n) 339 nValue := reflect.ValueOf(&n)
342 nValue.Elem().Set(field) 340 nValue.Elem().Set(field)
343 needed := intLength(n) 341 needed := intLength(n)
(...skipping 14 matching lines...) Expand all
358 356
359 return out 357 return out
360 } 358 }
361 359
362 var bigOne = big.NewInt(1) 360 var bigOne = big.NewInt(1)
363 361
364 func parseString(in []byte) (out, rest []byte, ok bool) { 362 func parseString(in []byte) (out, rest []byte, ok bool) {
365 if len(in) < 4 { 363 if len(in) < 4 {
366 return 364 return
367 } 365 }
368 » length := uint32(in[0])<<24 | uint32(in[1])<<16 | uint32(in[2])<<8 | uin t32(in[3]) 366 » length := binary.BigEndian.Uint32(in)
369 if uint32(len(in)) < 4+length { 367 if uint32(len(in)) < 4+length {
370 return 368 return
371 } 369 }
372 out = in[4 : 4+length] 370 out = in[4 : 4+length]
373 rest = in[4+length:] 371 rest = in[4+length:]
374 ok = true 372 ok = true
375 return 373 return
376 } 374 }
377 375
378 var ( 376 var (
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 out.Add(out, bigOne) 412 out.Add(out, bigOne)
415 out.Neg(out) 413 out.Neg(out)
416 } else { 414 } else {
417 // Positive number 415 // Positive number
418 out.SetBytes(contents) 416 out.SetBytes(contents)
419 } 417 }
420 ok = true 418 ok = true
421 return 419 return
422 } 420 }
423 421
424 func parseUint32(in []byte) (out uint32, rest []byte, ok bool) { 422 func parseUint32(in []byte) (uint32, []byte, bool) {
425 if len(in) < 4 { 423 if len(in) < 4 {
426 » » return 424 » » return 0, nil, false
427 » } 425 » }
428 » out = uint32(in[0])<<24 | uint32(in[1])<<16 | uint32(in[2])<<8 | uint32( in[3]) 426 » return binary.BigEndian.Uint32(in), in[4:], true
429 » rest = in[4:] 427 }
430 » ok = true 428
431 » return 429 func parseUint64(in []byte) (uint64, []byte, bool) {
432 }
433
434 func parseUint64(in []byte) (out uint64, rest []byte, ok bool) {
435 if len(in) < 8 { 430 if len(in) < 8 {
436 » » return 431 » » return 0, nil, false
437 » } 432 » }
438 » out = uint64(in[0])<<56 | 433 » return binary.BigEndian.Uint64(in), in[8:], true
remyoudompheng 2012/03/03 09:45:18 this is out = binary.BigEndian.Uint64(in)
dave_cheney.net 2012/03/03 10:51:11 Done.
439 » » uint64(in[1])<<48 |
440 » » uint64(in[2])<<40 |
441 » » uint64(in[3])<<32 |
442 » » uint64(in[4])<<24 |
443 » » uint64(in[5])<<16 |
444 » » uint64(in[6])<<8 |
445 » » uint64(in[7])
446 » rest = in[8:]
447 » ok = true
448 » return
449 } 434 }
450 435
451 func nameListLength(namelist []string) int { 436 func nameListLength(namelist []string) int {
452 length := 4 /* uint32 length prefix */ 437 length := 4 /* uint32 length prefix */
453 for i, name := range namelist { 438 for i, name := range namelist {
454 if i != 0 { 439 if i != 0 {
455 length++ /* comma */ 440 length++ /* comma */
456 } 441 }
457 length += len(name) 442 length += len(name)
458 } 443 }
(...skipping 18 matching lines...) Expand all
477 if bitLen%8 == 0 { 462 if bitLen%8 == 0 {
478 // The number will need 0x00 padding 463 // The number will need 0x00 padding
479 length++ 464 length++
480 } 465 }
481 length += (bitLen + 7) / 8 466 length += (bitLen + 7) / 8
482 } 467 }
483 468
484 return length 469 return length
485 } 470 }
486 471
487 func marshalUint32(to []byte, n uint32) []byte { 472 func marshalUint32(to []byte, n uint32) []byte {
remyoudompheng 2012/03/03 09:45:18 s = marshalUint32(s, n) looks no different from bi
dave_cheney.net 2012/03/03 10:51:11 Done.
488 » to[0] = byte(n >> 24) 473 » binary.BigEndian.PutUint32(to, n)
489 » to[1] = byte(n >> 16)
490 » to[2] = byte(n >> 8)
491 » to[3] = byte(n)
492 return to[4:] 474 return to[4:]
493 } 475 }
494 476
495 func marshalUint64(to []byte, n uint64) []byte { 477 func marshalUint64(to []byte, n uint64) []byte {
496 » to[0] = byte(n >> 56) 478 » binary.BigEndian.PutUint64(to, n)
497 » to[1] = byte(n >> 48)
498 » to[2] = byte(n >> 40)
499 » to[3] = byte(n >> 32)
500 » to[4] = byte(n >> 24)
501 » to[5] = byte(n >> 16)
502 » to[6] = byte(n >> 8)
503 » to[7] = byte(n)
504 return to[8:] 479 return to[8:]
505 } 480 }
506 481
507 func marshalInt(to []byte, n *big.Int) []byte { 482 func marshalInt(to []byte, n *big.Int) []byte {
508 lengthBytes := to 483 lengthBytes := to
509 to = to[4:] 484 to = to[4:]
510 length := 0 485 length := 0
511 486
512 if n.Sign() < 0 { 487 if n.Sign() < 0 {
513 // A negative number has to be converted to two's-complement 488 // A negative number has to be converted to two's-complement
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 case msgChannelFailure: 606 case msgChannelFailure:
632 msg = new(channelRequestFailureMsg) 607 msg = new(channelRequestFailureMsg)
633 default: 608 default:
634 return UnexpectedMessageError{0, packet[0]} 609 return UnexpectedMessageError{0, packet[0]}
635 } 610 }
636 if err := unmarshal(msg, packet, packet[0]); err != nil { 611 if err := unmarshal(msg, packet, packet[0]); err != nil {
637 return err 612 return err
638 } 613 }
639 return msg 614 return msg
640 } 615 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b