modcompact.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package modcompact
  20. // By Rensmt
  21. import (
  22. "bytes"
  23. "encoding/hex"
  24. "encoding/json"
  25. "fmt"
  26. "strconv"
  27. "github.com/bashery/linethrift"
  28. )
  29. type TMoreCompactProtocol struct {
  30. __a []byte
  31. __b [][]string
  32. __c []byte
  33. __d []byte
  34. __e []string
  35. __f []byte
  36. __h []byte
  37. __last_fid int
  38. __last_pos int
  39. __last_sid int
  40. data []byte
  41. res interface{}
  42. err bool
  43. }
  44. type ExceptionMod struct {
  45. Code int64 `json:"1"`
  46. Reason string `json:"2"`
  47. }
  48. func TMoreCompactProtocolGoods(decodedByte []byte) *TMoreCompactProtocol {
  49. A := new(TMoreCompactProtocol)
  50. A.func_A()
  51. A.funcD(decodedByte)
  52. return A
  53. }
  54. func (p *TMoreCompactProtocol) readVarint(data []byte) []int {
  55. var (
  56. result = 0
  57. shift = 0
  58. i = 0
  59. )
  60. for {
  61. byteh := int(data[i])
  62. i++
  63. result |= (byteh & 0x7f) << shift
  64. if byteh>>7 == 0 {
  65. return []int{result, i}
  66. }
  67. shift += 7
  68. }
  69. }
  70. func (p *TMoreCompactProtocol) readCollectionBegin(x []byte) (typea int, size int, length int) {
  71. size_type := int(x[0])
  72. size = size_type >> 4
  73. typea = size_type & 0x0f
  74. length = 0
  75. if size == 15 {
  76. owsh := p.readVarint(x[1:])
  77. size, length = owsh[0], owsh[1]
  78. }
  79. return typea, size, length + 1
  80. }
  81. func (p *TMoreCompactProtocol) funcB() int {
  82. var (
  83. i2 = 0
  84. i3 = 0
  85. )
  86. for {
  87. //l2, _ := strconv.Atoi(string(p.data[p.__last_pos]))
  88. l2 := int(p.data[p.__last_pos])
  89. p.__last_pos++
  90. i2 |= (l2 & 127) << i3
  91. if (l2 & 128) != 128 {
  92. return i2
  93. }
  94. i3 += 7
  95. }
  96. }
  97. func (p *TMoreCompactProtocol) funcC(pX int, i2 int) []byte {
  98. if i2 == 0 {
  99. return []byte{}
  100. }
  101. bArr := p.data[pX : pX+i2]
  102. return bArr
  103. }
  104. func (p *TMoreCompactProtocol) funcD(d []byte) {
  105. p.data = d
  106. p.funcT()
  107. }
  108. func (p *TMoreCompactProtocol) funcE() {
  109. var a interface{}
  110. fid := p.funcY()
  111. if fid == 0 {
  112. return
  113. } else if fid == 1 {
  114. a = p.funcG(p.funcW())
  115. p.res = a
  116. } else if fid == 2 {
  117. a = p.funcG(p.funcW())
  118. p.res = a
  119. p.err = true
  120. } else if fid == 6 { //exception
  121. return
  122. } else { //EOF
  123. return
  124. }
  125. }
  126. func (p *TMoreCompactProtocol) funcF(n int) int {
  127. return (n >> 1) ^ -(n & 1)
  128. }
  129. func (p *TMoreCompactProtocol) T2() bool {
  130. b := p.funcB()
  131. if b == 0 {
  132. return false
  133. }
  134. return true
  135. }
  136. func (p *TMoreCompactProtocol) funcG(t int) interface{} {
  137. var (
  138. a interface{}
  139. b = 0
  140. c = 0
  141. )
  142. if t == 2 {
  143. b = p.funcB()
  144. if b == 0 {
  145. a = false
  146. } else {
  147. a = true
  148. }
  149. } else if t == 3 {
  150. bx := bytes.NewReader(p.data[p.__last_pos:])
  151. x, _ := bx.ReadByte()
  152. a = int(x)
  153. p.__last_pos++
  154. } else if t == 4 {
  155. p.__last_pos += 8
  156. a = ""
  157. } else if t == 8 {
  158. a = p.funcF(p.funcX(p.data[p.__last_pos:]))
  159. } else if t == 10 {
  160. a = p.funcF(p.funcB())
  161. } else if t == 11 {
  162. a = p.funcS()
  163. return a
  164. } else if t == 12 {
  165. an := make(map[string]interface{})
  166. b := p.funcB()
  167. c := p.funcN(b)
  168. for _, d := range c { //d = int of operation type
  169. an[strconv.Itoa(d)] = p.funcG(p.funcW())
  170. }
  171. a = an
  172. } else if t == 13 {
  173. var an = make(map[string]string)
  174. c := p.funcB()
  175. if c != 0 {
  176. d := p.funcY()
  177. t1, t2 := p.funcQ(int(d))
  178. for i := 0; i < c; i++ {
  179. k := fmt.Sprint(p.funcG(t1))
  180. v := fmt.Sprint(p.funcG(t2))
  181. an[k] = v
  182. }
  183. }
  184. a = an
  185. } else if t == 14 || t == 15 {
  186. var an []interface{}
  187. ftype, count, offset := p.readCollectionBegin(p.data[p.__last_pos:])
  188. p.__last_pos += offset
  189. for i := 0; i < count; i++ {
  190. b := p.funcG(p.func_D(ftype)) // GODD!!! THIS OPERATION
  191. an = append(an, b)
  192. }
  193. a = an
  194. } else if t == 16 {
  195. b = p.funcB()
  196. c = -(b & 1) ^ p.func_E(b, 1)
  197. p.__last_sid += c
  198. a = strconv.Itoa(p.__last_sid)
  199. } else if t == 17 {
  200. b = p.funcB()
  201. if len(p.__e) > b {
  202. a = p.__e[b]
  203. }
  204. }
  205. return a
  206. }
  207. func (p *TMoreCompactProtocol) funcM() {
  208. a := p.funcB()
  209. for _a := 0; _a < a; _a++ {
  210. bArr := []byte{p.data[p.__last_pos]}
  211. bArr = append(bArr, p.func_C(p.data[p.__last_pos+1:p.__last_pos+17])...)
  212. p.__e = append(p.__e, string(bArr))
  213. p.__last_pos += 17
  214. }
  215. p.funcE()
  216. }
  217. func (p *TMoreCompactProtocol) funcN(d int) []int {
  218. var (
  219. a = []int{}
  220. i = 0
  221. )
  222. for {
  223. b := 1 << i
  224. if b > d {
  225. break
  226. } else if d&b != 0 {
  227. a = append(a, i)
  228. }
  229. i++
  230. }
  231. return a
  232. }
  233. func (p *TMoreCompactProtocol) funcQ(d int) (int, int) {
  234. return p.func_D(d >> 4), p.func_D(d & 15)
  235. }
  236. func (p *TMoreCompactProtocol) funcS() string {
  237. var (
  238. a = p.funcB()
  239. b = p.data[p.__last_pos : p.__last_pos+a]
  240. )
  241. p.__last_pos += a
  242. return string(b)
  243. }
  244. func (p *TMoreCompactProtocol) funcT() {
  245. p.__last_pos = 3
  246. if len(p.data) == 4 {
  247. fmt.Println("Data (error: 20):", p.data)
  248. } else {
  249. var (
  250. a = p.funcB()
  251. b = p.funcC(p.__last_pos, a)
  252. d = 0
  253. f = 0
  254. g = 0
  255. )
  256. p.__d = make([]byte, a<<1)
  257. for _, h := range b {
  258. var (
  259. _a = 0
  260. _b = 128
  261. )
  262. for _a < 8 {
  263. //hInt, err := strconv.Atoi(string(h))
  264. //fmt.Println(err)
  265. //fmt.Println("H =>", hInt)
  266. hInt := int(h)
  267. if hInt&_b == 0 {
  268. d = (g << 1) + 1
  269. } else {
  270. d = (g << 1) + 2
  271. }
  272. if p.__a[d] != 0 {
  273. if f >= len(p.__d) {
  274. cD := make([]byte, 4)
  275. for z := 0; z < 4; z++ {
  276. cD[z] = byte(len(p.__d))
  277. }
  278. p.__d = append(p.__d, cD...)
  279. }
  280. p.__d[f] = p.__a[d]
  281. f++
  282. g = 0
  283. } else {
  284. g = d
  285. }
  286. _b >>= 1
  287. _a++
  288. }
  289. }
  290. p.__last_pos += a
  291. p.funcM()
  292. }
  293. }
  294. func (p *TMoreCompactProtocol) funcW() int {
  295. a := p.__d[p.__last_fid]
  296. p.__last_fid++
  297. return int(a)
  298. }
  299. func (p *TMoreCompactProtocol) funcX(a []byte) int {
  300. var (
  301. c = 0
  302. d = 0
  303. i = 0
  304. )
  305. for {
  306. e := a[i]
  307. i++
  308. //ee, _ := strconv.Atoi(string(e))
  309. ee := int(e)
  310. c |= (ee & 0x7f) << d
  311. if e>>7 == 0 {
  312. p.__last_pos += i
  313. return c
  314. }
  315. d += 7
  316. }
  317. }
  318. func (p *TMoreCompactProtocol) funcY() byte {
  319. a := p.data[p.__last_pos]
  320. p.__last_pos++
  321. return a
  322. }
  323. func (p *TMoreCompactProtocol) funcZ() bool {
  324. if len(p.data) > p.__last_pos {
  325. return true
  326. }
  327. return false
  328. }
  329. func (p *TMoreCompactProtocol) func_A() {
  330. p.__a = make([]byte, 512)
  331. p.__b = make([][]string, 18)
  332. p.func_B([]string{"1", "0", "1", "1"}, 2)
  333. p.func_B([]string{"1", "0", "1", "0", "1", "0", "0", "1"}, 3)
  334. p.func_B([]string{"1", "0", "1", "0", "1", "0", "0", "0"}, 4)
  335. p.func_B([]string{"1", "0", "1", "0", "1", "1", "1"}, 6)
  336. p.func_B([]string{"0", "1"}, 8)
  337. p.func_B([]string{"0", "0"}, 10)
  338. p.func_B([]string{"1", "0", "1", "0", "0"}, 11)
  339. p.func_B([]string{"1", "1", "0", "1"}, 12)
  340. p.func_B([]string{"1", "0", "1", "0", "1", "1", "0"}, 13)
  341. p.func_B([]string{"1", "0", "1", "0", "1", "0", "1"}, 14)
  342. p.func_B([]string{"1", "1", "0", "0"}, 15)
  343. p.func_B([]string{"1", "1", "1"}, 16)
  344. p.func_B([]string{"1", "0", "0"}, 17)
  345. }
  346. func (p *TMoreCompactProtocol) func_B(cArr []string, b2 int) {
  347. p.__b[b2] = cArr
  348. i2 := 0
  349. for _, c2 := range cArr {
  350. if c2 == "0" {
  351. i2 = (i2 << 1) + 1
  352. } else if c2 == "1" {
  353. i2 = (i2 << 1) + 2
  354. }
  355. }
  356. p.__a[i2] = byte(b2)
  357. }
  358. func (p *TMoreCompactProtocol) func_C(val []byte) []byte {
  359. x := hex.EncodeToString(val)
  360. return []byte(x)
  361. }
  362. func (p *TMoreCompactProtocol) func_D(val int) int {
  363. if val == 0 {
  364. return 0
  365. }
  366. if val == 1 || val == 2 {
  367. return 2
  368. }
  369. if val == 3 {
  370. return 3
  371. }
  372. if val == 4 {
  373. return 6
  374. }
  375. if val == 5 {
  376. return 8
  377. }
  378. if val == 6 {
  379. return 10
  380. }
  381. if val == 7 {
  382. return 4
  383. }
  384. if val == 8 {
  385. return 11
  386. }
  387. if val == 9 {
  388. return 15
  389. }
  390. if val == 10 {
  391. return 14
  392. }
  393. if val == 11 {
  394. return 13
  395. }
  396. if val == 12 {
  397. return 12
  398. }
  399. return 404
  400. }
  401. func (p *TMoreCompactProtocol) func_E(val int, n int) int {
  402. if val >= 0 {
  403. val >>= n
  404. } else {
  405. val = ((val + 0x1000000000000000) >> n)
  406. }
  407. return val
  408. }
  409. func (p *TMoreCompactProtocol) GETOPS() (res []*linethrift.Operation, err *ExceptionMod) {
  410. if p.err == true {
  411. jsonStr, _ := json.Marshal(p.res)
  412. json.Unmarshal(jsonStr, &err)
  413. } else {
  414. jsonStr, _ := json.Marshal(p.res)
  415. json.Unmarshal(jsonStr, &res)
  416. }
  417. return res, err
  418. }