main_x32.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //go:build (darwin || freebsd || linux || windows) && (386 || arm || mips || mipsle)
  2. package yu_sys
  3. import (
  4. "unsafe"
  5. )
  6. func (t Address) Call(args ...any) (r1, r2, err uintptr) {
  7. if len(args) == 0 {
  8. return SyscallN(t)
  9. }
  10. j_args := make([]uintptr, 0, len(args))
  11. j_bufs := make([][]byte, 0)
  12. for _, j_arg := range args {
  13. if j_arg == nil {
  14. j_args = append(j_args, 0)
  15. continue
  16. }
  17. switch j_val := j_arg.(type) {
  18. case bool:
  19. if j_val {
  20. j_args = append(j_args, 1)
  21. } else {
  22. j_args = append(j_args, 0)
  23. }
  24. case byte:
  25. j_args = append(j_args, uintptr(j_val))
  26. case int8:
  27. j_args = append(j_args, uintptr(j_val))
  28. case int16:
  29. j_args = append(j_args, uintptr(j_val))
  30. case uint16:
  31. j_args = append(j_args, uintptr(j_val))
  32. case int32:
  33. j_args = append(j_args, uintptr(j_val))
  34. case uint32:
  35. j_args = append(j_args, uintptr(j_val))
  36. case int64:
  37. j_args = append(j_args, uintptr(j_val))
  38. j_args = append(j_args, uintptr(j_val>>32))
  39. case uint64:
  40. j_args = append(j_args, uintptr(j_val))
  41. j_args = append(j_args, uintptr(j_val>>32))
  42. case int:
  43. j_args = append(j_args, uintptr(j_val))
  44. case uint:
  45. j_args = append(j_args, uintptr(j_val))
  46. case uintptr:
  47. j_args = append(j_args, j_val)
  48. case unsafe.Pointer:
  49. j_args = append(j_args, uintptr(j_val))
  50. case *bool:
  51. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  52. case *byte:
  53. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  54. case *int8:
  55. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  56. case *int16:
  57. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  58. case *uint16:
  59. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  60. case *int32:
  61. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  62. case *uint32:
  63. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  64. case *int64:
  65. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  66. case *uint64:
  67. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  68. case *int:
  69. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  70. case *uint:
  71. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  72. case *uintptr:
  73. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  74. case *unsafe.Pointer:
  75. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  76. case []bool:
  77. if len(j_val) == 0 {
  78. j_args = append(j_args, 0)
  79. continue
  80. }
  81. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  82. case []byte:
  83. if len(j_val) == 0 {
  84. j_args = append(j_args, 0)
  85. continue
  86. }
  87. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  88. case []int8:
  89. if len(j_val) == 0 {
  90. j_args = append(j_args, 0)
  91. continue
  92. }
  93. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  94. case []int16:
  95. if len(j_val) == 0 {
  96. j_args = append(j_args, 0)
  97. continue
  98. }
  99. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  100. case []uint16:
  101. if len(j_val) == 0 {
  102. j_args = append(j_args, 0)
  103. continue
  104. }
  105. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  106. case []int32:
  107. if len(j_val) == 0 {
  108. j_args = append(j_args, 0)
  109. continue
  110. }
  111. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  112. case []uint32:
  113. if len(j_val) == 0 {
  114. j_args = append(j_args, 0)
  115. continue
  116. }
  117. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  118. case []int64:
  119. if len(j_val) == 0 {
  120. j_args = append(j_args, 0)
  121. continue
  122. }
  123. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  124. case []uint64:
  125. if len(j_val) == 0 {
  126. j_args = append(j_args, 0)
  127. continue
  128. }
  129. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  130. case []int:
  131. if len(j_val) == 0 {
  132. j_args = append(j_args, 0)
  133. continue
  134. }
  135. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  136. case []uint:
  137. if len(j_val) == 0 {
  138. j_args = append(j_args, 0)
  139. continue
  140. }
  141. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  142. case []uintptr:
  143. if len(j_val) == 0 {
  144. j_args = append(j_args, 0)
  145. continue
  146. }
  147. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  148. case []unsafe.Pointer:
  149. if len(j_val) == 0 {
  150. j_args = append(j_args, 0)
  151. continue
  152. }
  153. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  154. case string:
  155. if j_val == "" {
  156. j_args = append(j_args, 0)
  157. continue
  158. }
  159. j_args = append(j_args, (*[2]uintptr)(unsafe.Pointer(&j_val))[0])
  160. case S:
  161. if j_val.Utf8 == "" {
  162. j_args = append(j_args, 0)
  163. continue
  164. }
  165. switch j_val.Code {
  166. case Gbk:
  167. j_unicode_len := len(j_val.Utf8) * 2
  168. j_unicode := make([]byte, j_unicode_len+2) // 结尾 {0,0}
  169. j_unicode_len = MultiByteToWideChar.CallInt(65001, 0, j_val.Utf8, len(j_val.Utf8), j_unicode, j_unicode_len)
  170. j_gbk_len := j_unicode_len * 2
  171. j_bufs = append(j_bufs, make([]byte, j_gbk_len+2)) // 结尾 {0,0}
  172. j_i := len(j_bufs) - 1
  173. WideCharToMultiByte.Call(936, 0, j_unicode, j_unicode_len, j_bufs[j_i], j_gbk_len, 0, 0)
  174. j_args = append(j_args, uintptr(unsafe.Pointer(&j_bufs[j_i][0])))
  175. case Unicode:
  176. j_utf8_len := len(j_val.Utf8)
  177. j_unicode_len := j_utf8_len * 2
  178. j_bufs = append(j_bufs, make([]byte, j_unicode_len+2)) // 结尾 {0,0}
  179. j_i := len(j_bufs) - 1
  180. MultiByteToWideChar.Call(65001, 0, j_val.Utf8, j_utf8_len, j_bufs[j_i], j_unicode_len)
  181. j_args = append(j_args, uintptr(unsafe.Pointer(&j_bufs[j_i][0])))
  182. }
  183. default:
  184. panic("不支持的数据类型")
  185. }
  186. }
  187. return SyscallN(t, j_args...)
  188. }
  189. func (t Address) CallInt64(args ...any) int64 {
  190. j_i, j_i2, _ := t.Call(args...)
  191. return int64(j_i) | int64(j_i2)<<32
  192. }
  193. func (t Address) CallUint64(args ...any) uint64 {
  194. j_i, j_i2, _ := t.Call(args...)
  195. return uint64(j_i) | uint64(j_i2)<<32
  196. }