unsafe.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // unsafe.go is based on qtls/unsafe.go
  2. // https://github.com/quic-go/qtls-go1-20/blob/49f389c17d984e5b248f0a57cbae10dd4198a3bf/unsafe.go
  3. /* Copyright (c) 2009 The Go Authors. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above
  10. copyright notice, this list of conditions and the following disclaimer
  11. in the documentation and/or other materials provided with the
  12. distribution.
  13. * Neither the name of Google Inc. nor the names of its
  14. contributors may be used to endorse or promote products derived from
  15. this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. package tls
  29. import (
  30. "crypto/tls"
  31. "reflect"
  32. "unsafe"
  33. )
  34. func init() {
  35. if !structsEqual(&tls.ConnectionState{}, &ConnectionState{}) {
  36. panic("tls.ConnectionState doesn't match")
  37. }
  38. }
  39. func UnsafeFromConnectionState(ss *ConnectionState) *tls.ConnectionState {
  40. return (*tls.ConnectionState)(unsafe.Pointer(ss))
  41. }
  42. func structsEqual(a, b interface{}) bool {
  43. return compare(reflect.TypeOf(a), reflect.TypeOf(b))
  44. }
  45. // compare compares two types and returns true if and only if
  46. // they can be casted to each other safely.
  47. // compare does not currently support Maps, Chan, UnsafePointer if reflect.DeepEqual fails.
  48. // Support for these types can be added if needed.
  49. // note that field names are still compared.
  50. func compare(a, b reflect.Type) bool {
  51. if reflect.DeepEqual(a, b) {
  52. return true
  53. }
  54. if a.Kind() != b.Kind() {
  55. return false
  56. }
  57. if a.Kind() == reflect.Pointer || a.Kind() == reflect.Slice {
  58. return compare(a.Elem(), b.Elem())
  59. }
  60. if a.Kind() == reflect.Func {
  61. if a.NumIn() != b.NumIn() || a.NumOut() != b.NumOut() {
  62. return false
  63. }
  64. for i_in := 0; i_in < a.NumIn(); i_in++ {
  65. if !compare(a.In(i_in), b.In(i_in)) {
  66. return false
  67. }
  68. }
  69. for i_out := 0; i_out < a.NumOut(); i_out++ {
  70. if !compare(a.Out(i_out), b.Out(i_out)) {
  71. return false
  72. }
  73. }
  74. return true
  75. }
  76. if a.Kind() == reflect.Struct {
  77. if a.NumField() != b.NumField() {
  78. return false
  79. }
  80. for i := 0; i < a.NumField(); i++ {
  81. fa := a.Field(i)
  82. fb := b.Field(i)
  83. if !reflect.DeepEqual(fa.Index, fb.Index) || fa.Name != fb.Name ||
  84. fa.Anonymous != fb.Anonymous || fa.Offset != fb.Offset {
  85. return false
  86. }
  87. if !reflect.DeepEqual(fa.Type, fb.Type) {
  88. if !compare(fa.Type, fb.Type) {
  89. return false
  90. }
  91. }
  92. }
  93. return true
  94. }
  95. // TODO: add support for missing types
  96. return false
  97. }