marshal.odin 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. package encoding_cbor
  2. import "base:intrinsics"
  3. import "base:runtime"
  4. import "core:bytes"
  5. import "core:io"
  6. import "core:mem"
  7. import "core:reflect"
  8. import "core:slice"
  9. import "core:strconv"
  10. import "core:strings"
  11. import "core:unicode/utf8"
  12. /*
  13. Marshal a value into binary CBOR.
  14. Flags can be used to control the output (mainly determinism, which coincidently affects size).
  15. The default flags `ENCODE_SMALL` (`.Deterministic_Int_Size`, `.Deterministic_Float_Size`) will try
  16. to put ints and floats into their smallest possible byte size without losing equality.
  17. Adding the `.Self_Described_CBOR` flag will wrap the value in a tag that lets generic decoders know
  18. the contents are CBOR from just reading the first byte.
  19. Adding the `.Deterministic_Map_Sorting` flag will sort the encoded maps by the byte content of the
  20. encoded key. This flag has a cost on performance and memory efficiency because all keys in a map
  21. have to be precomputed, sorted and only then written to the output.
  22. Empty flags will do nothing extra to the value.
  23. The allocations for the `.Deterministic_Map_Sorting` flag are done using the given `temp_allocator`.
  24. but are followed by the necessary `delete` and `free` calls if the allocator supports them.
  25. This is helpful when the CBOR size is so big that you don't want to collect all the temporary
  26. allocations until the end.
  27. */
  28. marshal_into :: proc {
  29. marshal_into_bytes,
  30. marshal_into_builder,
  31. marshal_into_writer,
  32. marshal_into_encoder,
  33. }
  34. marshal :: marshal_into
  35. // Marshals the given value into a CBOR byte stream (allocated using the given allocator).
  36. // See docs on the `marshal_into` proc group for more info.
  37. marshal_into_bytes :: proc(v: any, flags := ENCODE_SMALL, allocator := context.allocator, temp_allocator := context.temp_allocator) -> (bytes: []byte, err: Marshal_Error) {
  38. b, alloc_err := strings.builder_make(allocator)
  39. // The builder as a stream also returns .EOF if it ran out of memory so this is consistent.
  40. if alloc_err != nil {
  41. return nil, .EOF
  42. }
  43. defer if err != nil { strings.builder_destroy(&b) }
  44. if err = marshal_into_builder(&b, v, flags, temp_allocator); err != nil {
  45. return
  46. }
  47. return b.buf[:], nil
  48. }
  49. // Marshals the given value into a CBOR byte stream written to the given builder.
  50. // See docs on the `marshal_into` proc group for more info.
  51. marshal_into_builder :: proc(b: ^strings.Builder, v: any, flags := ENCODE_SMALL, temp_allocator := context.temp_allocator) -> Marshal_Error {
  52. return marshal_into_writer(strings.to_writer(b), v, flags, temp_allocator)
  53. }
  54. // Marshals the given value into a CBOR byte stream written to the given writer.
  55. // See docs on the `marshal_into` proc group for more info.
  56. marshal_into_writer :: proc(w: io.Writer, v: any, flags := ENCODE_SMALL, temp_allocator := context.temp_allocator) -> Marshal_Error {
  57. encoder := Encoder{flags, w, temp_allocator}
  58. return marshal_into_encoder(encoder, v)
  59. }
  60. // Marshals the given value into a CBOR byte stream written to the given encoder.
  61. // See docs on the `marshal_into` proc group for more info.
  62. marshal_into_encoder :: proc(e: Encoder, v: any) -> (err: Marshal_Error) {
  63. e := e
  64. if e.temp_allocator.procedure == nil {
  65. e.temp_allocator = context.temp_allocator
  66. }
  67. if .Self_Described_CBOR in e.flags {
  68. err_conv(_encode_u64(e, TAG_SELF_DESCRIBED_CBOR, .Tag)) or_return
  69. e.flags &~= { .Self_Described_CBOR }
  70. }
  71. if v == nil {
  72. return _encode_nil(e.writer)
  73. }
  74. // Check if type has a tag implementation to use.
  75. if impl, ok := _tag_implementations_type[v.id]; ok {
  76. return impl->marshal(e, v)
  77. }
  78. ti := runtime.type_info_base(type_info_of(v.id))
  79. a := any{v.data, ti.id}
  80. #partial switch info in ti.variant {
  81. case runtime.Type_Info_Named:
  82. unreachable()
  83. case runtime.Type_Info_Pointer:
  84. switch vv in v {
  85. case Undefined: return _encode_undefined(e.writer)
  86. case Nil: return _encode_nil(e.writer)
  87. }
  88. case runtime.Type_Info_Integer:
  89. switch vv in v {
  90. case Simple: return err_conv(_encode_simple(e.writer, vv))
  91. case Negative_U8: return _encode_u8(e.writer, u8(vv), .Negative)
  92. case Negative_U16: return err_conv(_encode_u16(e, u16(vv), .Negative))
  93. case Negative_U32: return err_conv(_encode_u32(e, u32(vv), .Negative))
  94. case Negative_U64: return err_conv(_encode_u64(e, u64(vv), .Negative))
  95. }
  96. switch i in a {
  97. case i8: return _encode_uint(e.writer, _int_to_uint(i))
  98. case i16: return err_conv(_encode_uint(e, _int_to_uint(i)))
  99. case i32: return err_conv(_encode_uint(e, _int_to_uint(i)))
  100. case i64: return err_conv(_encode_uint(e, _int_to_uint(i)))
  101. case i128: return err_conv(_encode_uint(e, _int_to_uint(i128(i)) or_return))
  102. case int: return err_conv(_encode_uint(e, _int_to_uint(i64(i))))
  103. case u8: return _encode_uint(e.writer, i)
  104. case u16: return err_conv(_encode_uint(e, i))
  105. case u32: return err_conv(_encode_uint(e, i))
  106. case u64: return err_conv(_encode_uint(e, i))
  107. case u128: return err_conv(_encode_uint(e, _u128_to_u64(u128(i)) or_return))
  108. case uint: return err_conv(_encode_uint(e, u64(i)))
  109. case uintptr: return err_conv(_encode_uint(e, u64(i)))
  110. case i16le: return err_conv(_encode_uint(e, _int_to_uint(i16(i))))
  111. case i32le: return err_conv(_encode_uint(e, _int_to_uint(i32(i))))
  112. case i64le: return err_conv(_encode_uint(e, _int_to_uint(i64(i))))
  113. case i128le: return err_conv(_encode_uint(e, _int_to_uint(i128(i)) or_return))
  114. case u16le: return err_conv(_encode_uint(e, u16(i)))
  115. case u32le: return err_conv(_encode_uint(e, u32(i)))
  116. case u64le: return err_conv(_encode_uint(e, u64(i)))
  117. case u128le: return err_conv(_encode_uint(e, _u128_to_u64(u128(i)) or_return))
  118. case i16be: return err_conv(_encode_uint(e, _int_to_uint(i16(i))))
  119. case i32be: return err_conv(_encode_uint(e, _int_to_uint(i32(i))))
  120. case i64be: return err_conv(_encode_uint(e, _int_to_uint(i64(i))))
  121. case i128be: return err_conv(_encode_uint(e, _int_to_uint(i128(i)) or_return))
  122. case u16be: return err_conv(_encode_uint(e, u16(i)))
  123. case u32be: return err_conv(_encode_uint(e, u32(i)))
  124. case u64be: return err_conv(_encode_uint(e, u64(i)))
  125. case u128be: return err_conv(_encode_uint(e, _u128_to_u64(u128(i)) or_return))
  126. }
  127. case runtime.Type_Info_Rune:
  128. buf, w := utf8.encode_rune(a.(rune))
  129. return err_conv(_encode_text(e, string(buf[:w])))
  130. case runtime.Type_Info_Float:
  131. switch f in a {
  132. case f16: return _encode_f16(e.writer, f)
  133. case f32: return _encode_f32(e, f)
  134. case f64: return _encode_f64(e, f)
  135. case f16le: return _encode_f16(e.writer, f16(f))
  136. case f32le: return _encode_f32(e, f32(f))
  137. case f64le: return _encode_f64(e, f64(f))
  138. case f16be: return _encode_f16(e.writer, f16(f))
  139. case f32be: return _encode_f32(e, f32(f))
  140. case f64be: return _encode_f64(e, f64(f))
  141. }
  142. case runtime.Type_Info_Complex:
  143. switch z in a {
  144. case complex32:
  145. arr: [2]Value = {real(z), imag(z)}
  146. return err_conv(_encode_array(e, arr[:]))
  147. case complex64:
  148. arr: [2]Value = {real(z), imag(z)}
  149. return err_conv(_encode_array(e, arr[:]))
  150. case complex128:
  151. arr: [2]Value = {real(z), imag(z)}
  152. return err_conv(_encode_array(e, arr[:]))
  153. }
  154. case runtime.Type_Info_Quaternion:
  155. switch q in a {
  156. case quaternion64:
  157. arr: [4]Value = {imag(q), jmag(q), kmag(q), real(q)}
  158. return err_conv(_encode_array(e, arr[:]))
  159. case quaternion128:
  160. arr: [4]Value = {imag(q), jmag(q), kmag(q), real(q)}
  161. return err_conv(_encode_array(e, arr[:]))
  162. case quaternion256:
  163. arr: [4]Value = {imag(q), jmag(q), kmag(q), real(q)}
  164. return err_conv(_encode_array(e, arr[:]))
  165. }
  166. case runtime.Type_Info_String:
  167. switch s in a {
  168. case string: return err_conv(_encode_text(e, s))
  169. case cstring: return err_conv(_encode_text(e, string(s)))
  170. }
  171. case runtime.Type_Info_Boolean:
  172. switch b in a {
  173. case bool: return _encode_bool(e.writer, b)
  174. case b8: return _encode_bool(e.writer, bool(b))
  175. case b16: return _encode_bool(e.writer, bool(b))
  176. case b32: return _encode_bool(e.writer, bool(b))
  177. case b64: return _encode_bool(e.writer, bool(b))
  178. }
  179. case runtime.Type_Info_Array:
  180. if info.elem.id == byte {
  181. raw := ([^]byte)(v.data)
  182. return err_conv(_encode_bytes(e, raw[:info.count]))
  183. }
  184. err_conv(_encode_u64(e, u64(info.count), .Array)) or_return
  185. for i in 0..<info.count {
  186. data := uintptr(v.data) + uintptr(i*info.elem_size)
  187. marshal_into(e, any{rawptr(data), info.elem.id}) or_return
  188. }
  189. return
  190. case runtime.Type_Info_Enumerated_Array:
  191. // index := runtime.type_info_base(info.index).variant.(runtime.Type_Info_Enum)
  192. err_conv(_encode_u64(e, u64(info.count), .Array)) or_return
  193. for i in 0..<info.count {
  194. data := uintptr(v.data) + uintptr(i*info.elem_size)
  195. marshal_into(e, any{rawptr(data), info.elem.id}) or_return
  196. }
  197. return
  198. case runtime.Type_Info_Dynamic_Array:
  199. if info.elem.id == byte {
  200. raw := (^[dynamic]byte)(v.data)
  201. return err_conv(_encode_bytes(e, raw[:]))
  202. }
  203. array := (^mem.Raw_Dynamic_Array)(v.data)
  204. err_conv(_encode_u64(e, u64(array.len), .Array)) or_return
  205. for i in 0..<array.len {
  206. data := uintptr(array.data) + uintptr(i*info.elem_size)
  207. marshal_into(e, any{rawptr(data), info.elem.id}) or_return
  208. }
  209. return
  210. case runtime.Type_Info_Slice:
  211. if info.elem.id == byte {
  212. raw := (^[]byte)(v.data)
  213. return err_conv(_encode_bytes(e, raw^))
  214. }
  215. array := (^mem.Raw_Slice)(v.data)
  216. err_conv(_encode_u64(e, u64(array.len), .Array)) or_return
  217. for i in 0..<array.len {
  218. data := uintptr(array.data) + uintptr(i*info.elem_size)
  219. marshal_into(e, any{rawptr(data), info.elem.id}) or_return
  220. }
  221. return
  222. case runtime.Type_Info_Map:
  223. m := (^mem.Raw_Map)(v.data)
  224. err_conv(_encode_u64(e, u64(runtime.map_len(m^)), .Map)) or_return
  225. if m != nil {
  226. if info.map_info == nil {
  227. return _unsupported(v.id, nil)
  228. }
  229. map_cap := uintptr(runtime.map_cap(m^))
  230. ks, vs, hs, _, _ := runtime.map_kvh_data_dynamic(m^, info.map_info)
  231. if .Deterministic_Map_Sorting not_in e.flags {
  232. for bucket_index in 0..<map_cap {
  233. runtime.map_hash_is_valid(hs[bucket_index]) or_continue
  234. key := rawptr(runtime.map_cell_index_dynamic(ks, info.map_info.ks, bucket_index))
  235. value := rawptr(runtime.map_cell_index_dynamic(vs, info.map_info.vs, bucket_index))
  236. marshal_into(e, any{ key, info.key.id }) or_return
  237. marshal_into(e, any{ value, info.value.id }) or_return
  238. }
  239. return
  240. }
  241. // Deterministic_Map_Sorting needs us to sort the entries by the byte contents of the
  242. // encoded key.
  243. //
  244. // This means we have to store and sort them before writing incurring extra (temporary) allocations.
  245. //
  246. // If the map key is a `string` or `cstring` we only allocate space for a dynamic array of entries
  247. // we sort.
  248. //
  249. // If the map key is of another type we also allocate space for encoding the key into.
  250. // To sort a string/cstring we need to first sort by their encoded header/length.
  251. // This fits in 9 bytes at most.
  252. pre_key :: #force_inline proc(e: Encoder, str: string) -> (res: [10]byte) {
  253. e := e
  254. builder := strings.builder_from_slice(res[:])
  255. e.writer = strings.to_stream(&builder)
  256. assert(_encode_u64(e, u64(len(str)), .Text) == nil)
  257. res[9] = u8(len(builder.buf))
  258. assert(res[9] < 10)
  259. return
  260. }
  261. Encoded_Entry_Fast :: struct($T: typeid) {
  262. pre_key: [10]byte,
  263. key: T,
  264. val_idx: uintptr,
  265. }
  266. Encoded_Entry :: struct {
  267. key: ^[dynamic]byte,
  268. val_idx: uintptr,
  269. }
  270. switch info.key.id {
  271. case string:
  272. entries := make([dynamic]Encoded_Entry_Fast(^[]byte), 0, map_cap, e.temp_allocator) or_return
  273. defer delete(entries)
  274. for bucket_index in 0..<map_cap {
  275. runtime.map_hash_is_valid(hs[bucket_index]) or_continue
  276. key := (^[]byte)(runtime.map_cell_index_dynamic(ks, info.map_info.ks, bucket_index))
  277. append(&entries, Encoded_Entry_Fast(^[]byte){
  278. pre_key = pre_key(e, string(key^)),
  279. key = key,
  280. val_idx = bucket_index,
  281. })
  282. }
  283. slice.sort_by_cmp(entries[:], proc(a, b: Encoded_Entry_Fast(^[]byte)) -> slice.Ordering {
  284. a, b := a, b
  285. pre_cmp := slice.Ordering(bytes.compare(a.pre_key[:a.pre_key[9]], b.pre_key[:b.pre_key[9]]))
  286. if pre_cmp != .Equal {
  287. return pre_cmp
  288. }
  289. return slice.Ordering(bytes.compare(a.key^, b.key^))
  290. })
  291. for &entry in entries {
  292. io.write_full(e.writer, entry.pre_key[:entry.pre_key[9]]) or_return
  293. io.write_full(e.writer, entry.key^) or_return
  294. value := rawptr(runtime.map_cell_index_dynamic(vs, info.map_info.vs, entry.val_idx))
  295. marshal_into(e, any{ value, info.value.id }) or_return
  296. }
  297. return
  298. case cstring:
  299. entries := make([dynamic]Encoded_Entry_Fast(^cstring), 0, map_cap, e.temp_allocator) or_return
  300. defer delete(entries)
  301. for bucket_index in 0..<map_cap {
  302. runtime.map_hash_is_valid(hs[bucket_index]) or_continue
  303. key := (^cstring)(runtime.map_cell_index_dynamic(ks, info.map_info.ks, bucket_index))
  304. append(&entries, Encoded_Entry_Fast(^cstring){
  305. pre_key = pre_key(e, string(key^)),
  306. key = key,
  307. val_idx = bucket_index,
  308. })
  309. }
  310. slice.sort_by_cmp(entries[:], proc(a, b: Encoded_Entry_Fast(^cstring)) -> slice.Ordering {
  311. a, b := a, b
  312. pre_cmp := slice.Ordering(bytes.compare(a.pre_key[:a.pre_key[9]], b.pre_key[:b.pre_key[9]]))
  313. if pre_cmp != .Equal {
  314. return pre_cmp
  315. }
  316. ab := transmute([]byte)string(a.key^)
  317. bb := transmute([]byte)string(b.key^)
  318. return slice.Ordering(bytes.compare(ab, bb))
  319. })
  320. for &entry in entries {
  321. io.write_full(e.writer, entry.pre_key[:entry.pre_key[9]]) or_return
  322. io.write_full(e.writer, transmute([]byte)string(entry.key^)) or_return
  323. value := rawptr(runtime.map_cell_index_dynamic(vs, info.map_info.vs, entry.val_idx))
  324. marshal_into(e, any{ value, info.value.id }) or_return
  325. }
  326. return
  327. case:
  328. entries := make([dynamic]Encoded_Entry, 0, map_cap, e.temp_allocator) or_return
  329. defer delete(entries)
  330. for bucket_index in 0..<map_cap {
  331. runtime.map_hash_is_valid(hs[bucket_index]) or_continue
  332. key := rawptr(runtime.map_cell_index_dynamic(ks, info.map_info.ks, bucket_index))
  333. key_builder := strings.builder_make(0, 8, e.temp_allocator) or_return
  334. marshal_into(Encoder{e.flags, strings.to_stream(&key_builder), e.temp_allocator}, any{ key, info.key.id }) or_return
  335. append(&entries, Encoded_Entry{ &key_builder.buf, bucket_index }) or_return
  336. }
  337. slice.sort_by_cmp(entries[:], proc(a, b: Encoded_Entry) -> slice.Ordering {
  338. return slice.Ordering(bytes.compare(a.key[:], b.key[:]))
  339. })
  340. for entry in entries {
  341. io.write_full(e.writer, entry.key[:]) or_return
  342. delete(entry.key^)
  343. value := rawptr(runtime.map_cell_index_dynamic(vs, info.map_info.vs, entry.val_idx))
  344. marshal_into(e, any{ value, info.value.id }) or_return
  345. }
  346. return
  347. }
  348. }
  349. case runtime.Type_Info_Struct:
  350. switch vv in v {
  351. case Tag: return err_conv(_encode_tag(e, vv))
  352. }
  353. field_name :: #force_inline proc(info: runtime.Type_Info_Struct, i: int) -> string {
  354. if cbor_name := string(reflect.struct_tag_get(reflect.Struct_Tag(info.tags[i]), "cbor")); cbor_name != "" {
  355. return cbor_name
  356. } else {
  357. return info.names[i]
  358. }
  359. }
  360. marshal_entry :: #force_inline proc(e: Encoder, info: runtime.Type_Info_Struct, v: any, name: string, i: int) -> Marshal_Error {
  361. err_conv(_encode_text(e, name)) or_return
  362. id := info.types[i].id
  363. data := rawptr(uintptr(v.data) + info.offsets[i])
  364. field_any := any{data, id}
  365. if tag := string(reflect.struct_tag_get(reflect.Struct_Tag(info.tags[i]), "cbor_tag")); tag != "" {
  366. if impl, ok := _tag_implementations_id[tag]; ok {
  367. return impl->marshal(e, field_any)
  368. }
  369. nr, ok := strconv.parse_u64_of_base(tag, 10)
  370. if !ok { return .Invalid_CBOR_Tag }
  371. if impl, nok := _tag_implementations_nr[nr]; nok {
  372. return impl->marshal(e, field_any)
  373. }
  374. err_conv(_encode_u64(e, nr, .Tag)) or_return
  375. }
  376. return marshal_into(e, field_any)
  377. }
  378. n: u64; {
  379. for _, i in info.names {
  380. if field_name(info, i) != "-" {
  381. n += 1
  382. }
  383. }
  384. err_conv(_encode_u64(e, n, .Map)) or_return
  385. }
  386. if .Deterministic_Map_Sorting in e.flags {
  387. Name :: struct {
  388. name: string,
  389. field: int,
  390. }
  391. entries := make([dynamic]Name, 0, n, e.temp_allocator) or_return
  392. defer delete(entries)
  393. for _, i in info.names {
  394. fname := field_name(info, i)
  395. if fname == "-" {
  396. continue
  397. }
  398. append(&entries, Name{fname, i}) or_return
  399. }
  400. // Sort lexicographic on the bytes of the key.
  401. slice.sort_by_cmp(entries[:], proc(a, b: Name) -> slice.Ordering {
  402. return slice.Ordering(bytes.compare(transmute([]byte)a.name, transmute([]byte)b.name))
  403. })
  404. for entry in entries {
  405. marshal_entry(e, info, v, entry.name, entry.field) or_return
  406. }
  407. } else {
  408. for _, i in info.names {
  409. fname := field_name(info, i)
  410. if fname == "-" {
  411. continue
  412. }
  413. marshal_entry(e, info, v, fname, i) or_return
  414. }
  415. }
  416. return
  417. case runtime.Type_Info_Union:
  418. switch vv in v {
  419. case Value: return err_conv(encode(e, vv))
  420. }
  421. id := reflect.union_variant_typeid(v)
  422. if v.data == nil || id == nil {
  423. return _encode_nil(e.writer)
  424. }
  425. if len(info.variants) == 1 {
  426. return marshal_into(e, any{v.data, id})
  427. }
  428. // Encode a non-nil multi-variant union as the `TAG_OBJECT_TYPE`.
  429. // Which is a tag of an array, where the first element is the textual id/type of the object
  430. // that follows it.
  431. err_conv(_encode_u16(e, TAG_OBJECT_TYPE, .Tag)) or_return
  432. _encode_u8(e.writer, 2, .Array) or_return
  433. vti := reflect.union_variant_type_info(v)
  434. #partial switch vt in vti.variant {
  435. case reflect.Type_Info_Named:
  436. err_conv(_encode_text(e, vt.name)) or_return
  437. case:
  438. builder := strings.builder_make(e.temp_allocator) or_return
  439. defer strings.builder_destroy(&builder)
  440. reflect.write_type(&builder, vti)
  441. err_conv(_encode_text(e, strings.to_string(builder))) or_return
  442. }
  443. return marshal_into(e, any{v.data, vti.id})
  444. case runtime.Type_Info_Enum:
  445. return marshal_into(e, any{v.data, info.base.id})
  446. case runtime.Type_Info_Bit_Set:
  447. // Store bit_set as big endian just like the protocol.
  448. do_byte_swap := !reflect.bit_set_is_big_endian(v)
  449. switch ti.size * 8 {
  450. case 0:
  451. return _encode_u8(e.writer, 0)
  452. case 8:
  453. x := (^u8)(v.data)^
  454. return _encode_u8(e.writer, x)
  455. case 16:
  456. x := (^u16)(v.data)^
  457. if do_byte_swap { x = intrinsics.byte_swap(x) }
  458. return err_conv(_encode_u16(e, x))
  459. case 32:
  460. x := (^u32)(v.data)^
  461. if do_byte_swap { x = intrinsics.byte_swap(x) }
  462. return err_conv(_encode_u32(e, x))
  463. case 64:
  464. x := (^u64)(v.data)^
  465. if do_byte_swap { x = intrinsics.byte_swap(x) }
  466. return err_conv(_encode_u64(e, x))
  467. case:
  468. panic("unknown bit_size size")
  469. }
  470. }
  471. return _unsupported(v.id, nil)
  472. }