msg_helpers.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. package dns
  2. import (
  3. "encoding/base32"
  4. "encoding/base64"
  5. "encoding/binary"
  6. "encoding/hex"
  7. "net"
  8. "strconv"
  9. )
  10. // helper functions called from the generated zmsg.go
  11. // These function are named after the tag to help pack/unpack, if there is no tag it is the name
  12. // of the type they pack/unpack (string, int, etc). We prefix all with unpackData or packData, so packDataA or
  13. // packDataDomainName.
  14. func unpackDataA(msg []byte, off int) (net.IP, int, error) {
  15. if off+net.IPv4len > len(msg) {
  16. return nil, len(msg), &Error{err: "overflow unpacking a"}
  17. }
  18. a := append(make(net.IP, 0, net.IPv4len), msg[off:off+net.IPv4len]...)
  19. off += net.IPv4len
  20. return a, off, nil
  21. }
  22. func packDataA(a net.IP, msg []byte, off int) (int, error) {
  23. // It must be a slice of 4, even if it is 16, we encode only the first 4
  24. if off+net.IPv4len > len(msg) {
  25. return len(msg), &Error{err: "overflow packing a"}
  26. }
  27. switch len(a) {
  28. case net.IPv4len, net.IPv6len:
  29. copy(msg[off:], a.To4())
  30. off += net.IPv4len
  31. case 0:
  32. // Allowed, for dynamic updates.
  33. default:
  34. return len(msg), &Error{err: "overflow packing a"}
  35. }
  36. return off, nil
  37. }
  38. func unpackDataAAAA(msg []byte, off int) (net.IP, int, error) {
  39. if off+net.IPv6len > len(msg) {
  40. return nil, len(msg), &Error{err: "overflow unpacking aaaa"}
  41. }
  42. aaaa := append(make(net.IP, 0, net.IPv6len), msg[off:off+net.IPv6len]...)
  43. off += net.IPv6len
  44. return aaaa, off, nil
  45. }
  46. func packDataAAAA(aaaa net.IP, msg []byte, off int) (int, error) {
  47. if off+net.IPv6len > len(msg) {
  48. return len(msg), &Error{err: "overflow packing aaaa"}
  49. }
  50. switch len(aaaa) {
  51. case net.IPv6len:
  52. copy(msg[off:], aaaa)
  53. off += net.IPv6len
  54. case 0:
  55. // Allowed, dynamic updates.
  56. default:
  57. return len(msg), &Error{err: "overflow packing aaaa"}
  58. }
  59. return off, nil
  60. }
  61. // unpackHeader unpacks an RR header, returning the offset to the end of the header and a
  62. // re-sliced msg according to the expected length of the RR.
  63. func unpackHeader(msg []byte, off int) (rr RR_Header, off1 int, truncmsg []byte, err error) {
  64. hdr := RR_Header{}
  65. if off == len(msg) {
  66. return hdr, off, msg, nil
  67. }
  68. hdr.Name, off, err = UnpackDomainName(msg, off)
  69. if err != nil {
  70. return hdr, len(msg), msg, err
  71. }
  72. hdr.Rrtype, off, err = unpackUint16(msg, off)
  73. if err != nil {
  74. return hdr, len(msg), msg, err
  75. }
  76. hdr.Class, off, err = unpackUint16(msg, off)
  77. if err != nil {
  78. return hdr, len(msg), msg, err
  79. }
  80. hdr.Ttl, off, err = unpackUint32(msg, off)
  81. if err != nil {
  82. return hdr, len(msg), msg, err
  83. }
  84. hdr.Rdlength, off, err = unpackUint16(msg, off)
  85. if err != nil {
  86. return hdr, len(msg), msg, err
  87. }
  88. msg, err = truncateMsgFromRdlength(msg, off, hdr.Rdlength)
  89. return hdr, off, msg, err
  90. }
  91. // pack packs an RR header, returning the offset to the end of the header.
  92. // See PackDomainName for documentation about the compression.
  93. func (hdr RR_Header) pack(msg []byte, off int, compression map[string]int, compress bool) (off1 int, err error) {
  94. if off == len(msg) {
  95. return off, nil
  96. }
  97. off, err = PackDomainName(hdr.Name, msg, off, compression, compress)
  98. if err != nil {
  99. return len(msg), err
  100. }
  101. off, err = packUint16(hdr.Rrtype, msg, off)
  102. if err != nil {
  103. return len(msg), err
  104. }
  105. off, err = packUint16(hdr.Class, msg, off)
  106. if err != nil {
  107. return len(msg), err
  108. }
  109. off, err = packUint32(hdr.Ttl, msg, off)
  110. if err != nil {
  111. return len(msg), err
  112. }
  113. off, err = packUint16(hdr.Rdlength, msg, off)
  114. if err != nil {
  115. return len(msg), err
  116. }
  117. return off, nil
  118. }
  119. // helper helper functions.
  120. // truncateMsgFromRdLength truncates msg to match the expected length of the RR.
  121. // Returns an error if msg is smaller than the expected size.
  122. func truncateMsgFromRdlength(msg []byte, off int, rdlength uint16) (truncmsg []byte, err error) {
  123. lenrd := off + int(rdlength)
  124. if lenrd > len(msg) {
  125. return msg, &Error{err: "overflowing header size"}
  126. }
  127. return msg[:lenrd], nil
  128. }
  129. var base32HexNoPadEncoding = base32.HexEncoding.WithPadding(base32.NoPadding)
  130. func fromBase32(s []byte) (buf []byte, err error) {
  131. for i, b := range s {
  132. if b >= 'a' && b <= 'z' {
  133. s[i] = b - 32
  134. }
  135. }
  136. buflen := base32HexNoPadEncoding.DecodedLen(len(s))
  137. buf = make([]byte, buflen)
  138. n, err := base32HexNoPadEncoding.Decode(buf, s)
  139. buf = buf[:n]
  140. return
  141. }
  142. func toBase32(b []byte) string {
  143. return base32HexNoPadEncoding.EncodeToString(b)
  144. }
  145. func fromBase64(s []byte) (buf []byte, err error) {
  146. buflen := base64.StdEncoding.DecodedLen(len(s))
  147. buf = make([]byte, buflen)
  148. n, err := base64.StdEncoding.Decode(buf, s)
  149. buf = buf[:n]
  150. return
  151. }
  152. func toBase64(b []byte) string { return base64.StdEncoding.EncodeToString(b) }
  153. // dynamicUpdate returns true if the Rdlength is zero.
  154. func noRdata(h RR_Header) bool { return h.Rdlength == 0 }
  155. func unpackUint8(msg []byte, off int) (i uint8, off1 int, err error) {
  156. if off+1 > len(msg) {
  157. return 0, len(msg), &Error{err: "overflow unpacking uint8"}
  158. }
  159. return uint8(msg[off]), off + 1, nil
  160. }
  161. func packUint8(i uint8, msg []byte, off int) (off1 int, err error) {
  162. if off+1 > len(msg) {
  163. return len(msg), &Error{err: "overflow packing uint8"}
  164. }
  165. msg[off] = byte(i)
  166. return off + 1, nil
  167. }
  168. func unpackUint16(msg []byte, off int) (i uint16, off1 int, err error) {
  169. if off+2 > len(msg) {
  170. return 0, len(msg), &Error{err: "overflow unpacking uint16"}
  171. }
  172. return binary.BigEndian.Uint16(msg[off:]), off + 2, nil
  173. }
  174. func packUint16(i uint16, msg []byte, off int) (off1 int, err error) {
  175. if off+2 > len(msg) {
  176. return len(msg), &Error{err: "overflow packing uint16"}
  177. }
  178. binary.BigEndian.PutUint16(msg[off:], i)
  179. return off + 2, nil
  180. }
  181. func unpackUint32(msg []byte, off int) (i uint32, off1 int, err error) {
  182. if off+4 > len(msg) {
  183. return 0, len(msg), &Error{err: "overflow unpacking uint32"}
  184. }
  185. return binary.BigEndian.Uint32(msg[off:]), off + 4, nil
  186. }
  187. func packUint32(i uint32, msg []byte, off int) (off1 int, err error) {
  188. if off+4 > len(msg) {
  189. return len(msg), &Error{err: "overflow packing uint32"}
  190. }
  191. binary.BigEndian.PutUint32(msg[off:], i)
  192. return off + 4, nil
  193. }
  194. func unpackUint48(msg []byte, off int) (i uint64, off1 int, err error) {
  195. if off+6 > len(msg) {
  196. return 0, len(msg), &Error{err: "overflow unpacking uint64 as uint48"}
  197. }
  198. // Used in TSIG where the last 48 bits are occupied, so for now, assume a uint48 (6 bytes)
  199. i = (uint64(uint64(msg[off])<<40 | uint64(msg[off+1])<<32 | uint64(msg[off+2])<<24 | uint64(msg[off+3])<<16 |
  200. uint64(msg[off+4])<<8 | uint64(msg[off+5])))
  201. off += 6
  202. return i, off, nil
  203. }
  204. func packUint48(i uint64, msg []byte, off int) (off1 int, err error) {
  205. if off+6 > len(msg) {
  206. return len(msg), &Error{err: "overflow packing uint64 as uint48"}
  207. }
  208. msg[off] = byte(i >> 40)
  209. msg[off+1] = byte(i >> 32)
  210. msg[off+2] = byte(i >> 24)
  211. msg[off+3] = byte(i >> 16)
  212. msg[off+4] = byte(i >> 8)
  213. msg[off+5] = byte(i)
  214. off += 6
  215. return off, nil
  216. }
  217. func unpackUint64(msg []byte, off int) (i uint64, off1 int, err error) {
  218. if off+8 > len(msg) {
  219. return 0, len(msg), &Error{err: "overflow unpacking uint64"}
  220. }
  221. return binary.BigEndian.Uint64(msg[off:]), off + 8, nil
  222. }
  223. func packUint64(i uint64, msg []byte, off int) (off1 int, err error) {
  224. if off+8 > len(msg) {
  225. return len(msg), &Error{err: "overflow packing uint64"}
  226. }
  227. binary.BigEndian.PutUint64(msg[off:], i)
  228. off += 8
  229. return off, nil
  230. }
  231. func unpackString(msg []byte, off int) (string, int, error) {
  232. if off+1 > len(msg) {
  233. return "", off, &Error{err: "overflow unpacking txt"}
  234. }
  235. l := int(msg[off])
  236. if off+l+1 > len(msg) {
  237. return "", off, &Error{err: "overflow unpacking txt"}
  238. }
  239. s := make([]byte, 0, l)
  240. for _, b := range msg[off+1 : off+1+l] {
  241. switch b {
  242. case '"', '\\':
  243. s = append(s, '\\', b)
  244. default:
  245. if b < 32 || b > 127 { // unprintable
  246. var buf [3]byte
  247. bufs := strconv.AppendInt(buf[:0], int64(b), 10)
  248. s = append(s, '\\')
  249. for i := 0; i < 3-len(bufs); i++ {
  250. s = append(s, '0')
  251. }
  252. for _, r := range bufs {
  253. s = append(s, r)
  254. }
  255. } else {
  256. s = append(s, b)
  257. }
  258. }
  259. }
  260. off += 1 + l
  261. return string(s), off, nil
  262. }
  263. func packString(s string, msg []byte, off int) (int, error) {
  264. txtTmp := make([]byte, 256*4+1)
  265. off, err := packTxtString(s, msg, off, txtTmp)
  266. if err != nil {
  267. return len(msg), err
  268. }
  269. return off, nil
  270. }
  271. func unpackStringBase32(msg []byte, off, end int) (string, int, error) {
  272. if end > len(msg) {
  273. return "", len(msg), &Error{err: "overflow unpacking base32"}
  274. }
  275. s := toBase32(msg[off:end])
  276. return s, end, nil
  277. }
  278. func packStringBase32(s string, msg []byte, off int) (int, error) {
  279. b32, err := fromBase32([]byte(s))
  280. if err != nil {
  281. return len(msg), err
  282. }
  283. if off+len(b32) > len(msg) {
  284. return len(msg), &Error{err: "overflow packing base32"}
  285. }
  286. copy(msg[off:off+len(b32)], b32)
  287. off += len(b32)
  288. return off, nil
  289. }
  290. func unpackStringBase64(msg []byte, off, end int) (string, int, error) {
  291. // Rest of the RR is base64 encoded value, so we don't need an explicit length
  292. // to be set. Thus far all RR's that have base64 encoded fields have those as their
  293. // last one. What we do need is the end of the RR!
  294. if end > len(msg) {
  295. return "", len(msg), &Error{err: "overflow unpacking base64"}
  296. }
  297. s := toBase64(msg[off:end])
  298. return s, end, nil
  299. }
  300. func packStringBase64(s string, msg []byte, off int) (int, error) {
  301. b64, err := fromBase64([]byte(s))
  302. if err != nil {
  303. return len(msg), err
  304. }
  305. if off+len(b64) > len(msg) {
  306. return len(msg), &Error{err: "overflow packing base64"}
  307. }
  308. copy(msg[off:off+len(b64)], b64)
  309. off += len(b64)
  310. return off, nil
  311. }
  312. func unpackStringHex(msg []byte, off, end int) (string, int, error) {
  313. // Rest of the RR is hex encoded value, so we don't need an explicit length
  314. // to be set. NSEC and TSIG have hex fields with a length field.
  315. // What we do need is the end of the RR!
  316. if end > len(msg) {
  317. return "", len(msg), &Error{err: "overflow unpacking hex"}
  318. }
  319. s := hex.EncodeToString(msg[off:end])
  320. return s, end, nil
  321. }
  322. func packStringHex(s string, msg []byte, off int) (int, error) {
  323. h, err := hex.DecodeString(s)
  324. if err != nil {
  325. return len(msg), err
  326. }
  327. if off+(len(h)) > len(msg) {
  328. return len(msg), &Error{err: "overflow packing hex"}
  329. }
  330. copy(msg[off:off+len(h)], h)
  331. off += len(h)
  332. return off, nil
  333. }
  334. func unpackStringTxt(msg []byte, off int) ([]string, int, error) {
  335. txt, off, err := unpackTxt(msg, off)
  336. if err != nil {
  337. return nil, len(msg), err
  338. }
  339. return txt, off, nil
  340. }
  341. func packStringTxt(s []string, msg []byte, off int) (int, error) {
  342. txtTmp := make([]byte, 256*4+1) // If the whole string consists out of \DDD we need this many.
  343. off, err := packTxt(s, msg, off, txtTmp)
  344. if err != nil {
  345. return len(msg), err
  346. }
  347. return off, nil
  348. }
  349. func unpackDataOpt(msg []byte, off int) ([]EDNS0, int, error) {
  350. var edns []EDNS0
  351. Option:
  352. code := uint16(0)
  353. if off+4 > len(msg) {
  354. return nil, len(msg), &Error{err: "overflow unpacking opt"}
  355. }
  356. code = binary.BigEndian.Uint16(msg[off:])
  357. off += 2
  358. optlen := binary.BigEndian.Uint16(msg[off:])
  359. off += 2
  360. if off+int(optlen) > len(msg) {
  361. return nil, len(msg), &Error{err: "overflow unpacking opt"}
  362. }
  363. switch code {
  364. case EDNS0NSID:
  365. e := new(EDNS0_NSID)
  366. if err := e.unpack(msg[off : off+int(optlen)]); err != nil {
  367. return nil, len(msg), err
  368. }
  369. edns = append(edns, e)
  370. off += int(optlen)
  371. case EDNS0SUBNET:
  372. e := new(EDNS0_SUBNET)
  373. if err := e.unpack(msg[off : off+int(optlen)]); err != nil {
  374. return nil, len(msg), err
  375. }
  376. edns = append(edns, e)
  377. off += int(optlen)
  378. case EDNS0COOKIE:
  379. e := new(EDNS0_COOKIE)
  380. if err := e.unpack(msg[off : off+int(optlen)]); err != nil {
  381. return nil, len(msg), err
  382. }
  383. edns = append(edns, e)
  384. off += int(optlen)
  385. case EDNS0UL:
  386. e := new(EDNS0_UL)
  387. if err := e.unpack(msg[off : off+int(optlen)]); err != nil {
  388. return nil, len(msg), err
  389. }
  390. edns = append(edns, e)
  391. off += int(optlen)
  392. case EDNS0LLQ:
  393. e := new(EDNS0_LLQ)
  394. if err := e.unpack(msg[off : off+int(optlen)]); err != nil {
  395. return nil, len(msg), err
  396. }
  397. edns = append(edns, e)
  398. off += int(optlen)
  399. case EDNS0DAU:
  400. e := new(EDNS0_DAU)
  401. if err := e.unpack(msg[off : off+int(optlen)]); err != nil {
  402. return nil, len(msg), err
  403. }
  404. edns = append(edns, e)
  405. off += int(optlen)
  406. case EDNS0DHU:
  407. e := new(EDNS0_DHU)
  408. if err := e.unpack(msg[off : off+int(optlen)]); err != nil {
  409. return nil, len(msg), err
  410. }
  411. edns = append(edns, e)
  412. off += int(optlen)
  413. case EDNS0N3U:
  414. e := new(EDNS0_N3U)
  415. if err := e.unpack(msg[off : off+int(optlen)]); err != nil {
  416. return nil, len(msg), err
  417. }
  418. edns = append(edns, e)
  419. off += int(optlen)
  420. case EDNS0PADDING:
  421. e := new(EDNS0_PADDING)
  422. if err := e.unpack(msg[off : off+int(optlen)]); err != nil {
  423. return nil, len(msg), err
  424. }
  425. edns = append(edns, e)
  426. off += int(optlen)
  427. default:
  428. e := new(EDNS0_LOCAL)
  429. e.Code = code
  430. if err := e.unpack(msg[off : off+int(optlen)]); err != nil {
  431. return nil, len(msg), err
  432. }
  433. edns = append(edns, e)
  434. off += int(optlen)
  435. }
  436. if off < len(msg) {
  437. goto Option
  438. }
  439. return edns, off, nil
  440. }
  441. func packDataOpt(options []EDNS0, msg []byte, off int) (int, error) {
  442. for _, el := range options {
  443. b, err := el.pack()
  444. if err != nil || off+3 > len(msg) {
  445. return len(msg), &Error{err: "overflow packing opt"}
  446. }
  447. binary.BigEndian.PutUint16(msg[off:], el.Option()) // Option code
  448. binary.BigEndian.PutUint16(msg[off+2:], uint16(len(b))) // Length
  449. off += 4
  450. if off+len(b) > len(msg) {
  451. copy(msg[off:], b)
  452. off = len(msg)
  453. continue
  454. }
  455. // Actual data
  456. copy(msg[off:off+len(b)], b)
  457. off += len(b)
  458. }
  459. return off, nil
  460. }
  461. func unpackStringOctet(msg []byte, off int) (string, int, error) {
  462. s := string(msg[off:])
  463. return s, len(msg), nil
  464. }
  465. func packStringOctet(s string, msg []byte, off int) (int, error) {
  466. txtTmp := make([]byte, 256*4+1)
  467. off, err := packOctetString(s, msg, off, txtTmp)
  468. if err != nil {
  469. return len(msg), err
  470. }
  471. return off, nil
  472. }
  473. func unpackDataNsec(msg []byte, off int) ([]uint16, int, error) {
  474. var nsec []uint16
  475. length, window, lastwindow := 0, 0, -1
  476. for off < len(msg) {
  477. if off+2 > len(msg) {
  478. return nsec, len(msg), &Error{err: "overflow unpacking nsecx"}
  479. }
  480. window = int(msg[off])
  481. length = int(msg[off+1])
  482. off += 2
  483. if window <= lastwindow {
  484. // RFC 4034: Blocks are present in the NSEC RR RDATA in
  485. // increasing numerical order.
  486. return nsec, len(msg), &Error{err: "out of order NSEC block"}
  487. }
  488. if length == 0 {
  489. // RFC 4034: Blocks with no types present MUST NOT be included.
  490. return nsec, len(msg), &Error{err: "empty NSEC block"}
  491. }
  492. if length > 32 {
  493. return nsec, len(msg), &Error{err: "NSEC block too long"}
  494. }
  495. if off+length > len(msg) {
  496. return nsec, len(msg), &Error{err: "overflowing NSEC block"}
  497. }
  498. // Walk the bytes in the window and extract the type bits
  499. for j := 0; j < length; j++ {
  500. b := msg[off+j]
  501. // Check the bits one by one, and set the type
  502. if b&0x80 == 0x80 {
  503. nsec = append(nsec, uint16(window*256+j*8+0))
  504. }
  505. if b&0x40 == 0x40 {
  506. nsec = append(nsec, uint16(window*256+j*8+1))
  507. }
  508. if b&0x20 == 0x20 {
  509. nsec = append(nsec, uint16(window*256+j*8+2))
  510. }
  511. if b&0x10 == 0x10 {
  512. nsec = append(nsec, uint16(window*256+j*8+3))
  513. }
  514. if b&0x8 == 0x8 {
  515. nsec = append(nsec, uint16(window*256+j*8+4))
  516. }
  517. if b&0x4 == 0x4 {
  518. nsec = append(nsec, uint16(window*256+j*8+5))
  519. }
  520. if b&0x2 == 0x2 {
  521. nsec = append(nsec, uint16(window*256+j*8+6))
  522. }
  523. if b&0x1 == 0x1 {
  524. nsec = append(nsec, uint16(window*256+j*8+7))
  525. }
  526. }
  527. off += length
  528. lastwindow = window
  529. }
  530. return nsec, off, nil
  531. }
  532. func packDataNsec(bitmap []uint16, msg []byte, off int) (int, error) {
  533. if len(bitmap) == 0 {
  534. return off, nil
  535. }
  536. var lastwindow, lastlength uint16
  537. for j := 0; j < len(bitmap); j++ {
  538. t := bitmap[j]
  539. window := t / 256
  540. length := (t-window*256)/8 + 1
  541. if window > lastwindow && lastlength != 0 { // New window, jump to the new offset
  542. off += int(lastlength) + 2
  543. lastlength = 0
  544. }
  545. if window < lastwindow || length < lastlength {
  546. return len(msg), &Error{err: "nsec bits out of order"}
  547. }
  548. if off+2+int(length) > len(msg) {
  549. return len(msg), &Error{err: "overflow packing nsec"}
  550. }
  551. // Setting the window #
  552. msg[off] = byte(window)
  553. // Setting the octets length
  554. msg[off+1] = byte(length)
  555. // Setting the bit value for the type in the right octet
  556. msg[off+1+int(length)] |= byte(1 << (7 - (t % 8)))
  557. lastwindow, lastlength = window, length
  558. }
  559. off += int(lastlength) + 2
  560. return off, nil
  561. }
  562. func unpackDataDomainNames(msg []byte, off, end int) ([]string, int, error) {
  563. var (
  564. servers []string
  565. s string
  566. err error
  567. )
  568. if end > len(msg) {
  569. return nil, len(msg), &Error{err: "overflow unpacking domain names"}
  570. }
  571. for off < end {
  572. s, off, err = UnpackDomainName(msg, off)
  573. if err != nil {
  574. return servers, len(msg), err
  575. }
  576. servers = append(servers, s)
  577. }
  578. return servers, off, nil
  579. }
  580. func packDataDomainNames(names []string, msg []byte, off int, compression map[string]int, compress bool) (int, error) {
  581. var err error
  582. for j := 0; j < len(names); j++ {
  583. off, err = PackDomainName(names[j], msg, off, compression, false && compress)
  584. if err != nil {
  585. return len(msg), err
  586. }
  587. }
  588. return off, nil
  589. }