@@ -50,6 +50,72 @@ This module is meant to be imported in lieu of `encoding/binary`.
5050Exported variables, types and functions of the standard library package
5151pass through and are available as though that package had been imported.
5252
53+ ### Structs
54+ #### Words
55+ A word is made of one or more fields of up to 64 bits in length.
56+ The length and offset (see [ definition] ( #offset ) ) of a field in number of bits
57+ must be indicated by a struct tag of the format ` bitfield:"<length>,<offset>" ` .
58+ There should be no gaps nor overlaps between fields, and
59+ the sum of the lengths of all fields in a word
60+ must be equal to the word length declared in the [ format struct] ( #formats ) .
61+ Unused or "reserved" fields should nonetheless by defined and tagged
62+ even though they contain all zeroes.
63+
64+ ``` go
65+ package demo
66+
67+ type RFC791InternetHeaderFormatWord0 struct {
68+ Version uint8 ` bitfield:"4,28"`
69+ IHL uint8 ` bitfield:"4,24"`
70+ Precedence uint8 ` bitfield:"3,21"`
71+ Delay bool ` bitfield:"1,20"`
72+ Throughput bool ` bitfield:"1,19"`
73+ Reliability bool ` bitfield:"1,18"`
74+ Reserved uint8 ` bitfield:"2,16"`
75+ TotalLength uint16 ` bitfield:"16,0"`
76+ }
77+ ```
78+
79+ As of Version 1, supported field types are
80+ ` uint ` , ` uint8 ` a.k.a. ` byte ` , ` uint16 ` , ` uint32 ` , ` uint64 ` and ` bool ` .
81+ [ Defined types] ( https://go.dev/ref/spec#Type_definitions )
82+ (e.g. ` type RFC791InternetHeaderPrecedence uint8 ` )
83+ having the above underlying types are compatible.
84+
85+ ##### Offset
86+ ` <offset> ` is an integer
87+ representing the number of places the bit field should be shifted left
88+ from the rightmost section of a word for its position to be appropriate.
89+ It is also the number of places to the right of the rightmost bit of the field.
90+ The offset of every field is the sum of the lengths of all fields to its right.
91+
92+ #### Formats
93+ A "format" is a struct that represents a binary message or file format,
94+ made up of one or more "words" (see [ section] ( #words ) on words below).
95+ A format struct must have one or more fields,
96+ all of which must be structs and bear a tag of the format ` word:"<length>" ` ,
97+ where ` <length> ` is an integer multiple of eight (up to a limit of 64)
98+ indicating the number of bits in the word.
99+
100+ ``` go
101+ package demo
102+
103+ type RFC791InternetHeaderFormatWithoutOptions struct {
104+ RFC791InternetHeaderFormatWord0 ` word:"32"`
105+ RFC791InternetHeaderFormatWord1 ` word:"32"`
106+ RFC791InternetHeaderFormatWord2 ` word:"32"`
107+ RFC791InternetHeaderFormatWord3 ` word:"32"`
108+ RFC791InternetHeaderFormatWord4 ` word:"32"`
109+ }
110+ ```
111+
112+ #### Example
113+ It is highly unlikely that a developer
114+ would ever need to implement the Internet Protocol
115+ (since in Go, the package [ ` net ` ] ( https://pkg.go.dev/net )
116+ supplies types and methods that abstract away low-level details),
117+ but it makes an appropriate illustration of binary message formats.
118+
53119``` go
54120package main
55121
@@ -224,65 +290,6 @@ of struct `demo.RFC791InternetHeaderFormatWithoutOptions`.
224290Values of constants in the struct literal in the example code above
225291are declared in the same file containing the struct definition.
226292
227- ### Structs
228- #### Formats
229- A "format" is a struct that represents a binary message or file format,
230- made up of one or more "words" (see [ section] ( #words ) on words below).
231- A format struct must have one or more fields,
232- all of which must be structs and bear a tag of the format ` word:"<length>" ` ,
233- where ` <length> ` is an integer multiple of eight (up to a limit of 64)
234- indicating the number of bits in the word.
235-
236- ``` go
237- package demo
238-
239- type RFC791InternetHeaderFormatWithoutOptions struct {
240- RFC791InternetHeaderFormatWord0 ` word:"32"`
241- RFC791InternetHeaderFormatWord1 ` word:"32"`
242- RFC791InternetHeaderFormatWord2 ` word:"32"`
243- RFC791InternetHeaderFormatWord3 ` word:"32"`
244- RFC791InternetHeaderFormatWord4 ` word:"32"`
245- }
246- ```
247-
248- #### Words
249- A word is made of one or more fields of up to 64 bits in length.
250- The length and offset (see [ definition] ( #offset ) ) of a field in number of bits
251- must be indicated by a struct tag of the format ` bitfield:"<length>,<offset>" ` .
252- There should be no gaps nor overlaps between fields, and
253- the sum of the lengths of all fields in a word
254- must be equal to the word length declared in the [ format struct] ( #formats ) .
255- Unused or "reserved" fields should nonetheless by defined and tagged
256- even though they contain all zeroes.
257-
258- ``` go
259- package demo
260-
261- type RFC791InternetHeaderFormatWord0 struct {
262- Version uint8 ` bitfield:"4,28"`
263- IHL uint8 ` bitfield:"4,24"`
264- Precedence uint8 ` bitfield:"3,21"`
265- Delay bool ` bitfield:"1,20"`
266- Throughput bool ` bitfield:"1,19"`
267- Reliability bool ` bitfield:"1,18"`
268- Reserved uint8 ` bitfield:"2,16"`
269- TotalLength uint16 ` bitfield:"16,0"`
270- }
271- ```
272-
273- As of Version 1, supported field types are
274- ` uint ` , ` uint8 ` a.k.a. ` byte ` , ` uint16 ` , ` uint32 ` , ` uint64 ` and ` bool ` .
275- [ Defined types] ( https://go.dev/ref/spec#Type_definitions )
276- (e.g. ` type RFC791InternetHeaderPrecedence uint8 ` )
277- having the above underlying types are compatible.
278-
279- ##### Offset
280- ` <offset> ` is an integer
281- representing the number of places the bit field should be shifted left
282- from the rightmost section of a word for its position to be appropriate.
283- It is also the number of places to the right of the rightmost bit of the field.
284- The offset of every field is the sum of the lengths of all fields to its right.
285-
286293### Performance
287294``` bash
288295pkg/encoding/binary$ go test -cpuprofile cpu.prof -memprofile mem.prof -bench . -benchmem
0 commit comments