A TXT record holds one or more character-strings, each itself length-prefixed, packed inside the RDATA. Today you parse them into a list, staying strictly within RDLENGTH.
Parse a TXT record's RDATA into a list of length-prefixed strings.
A TXT record carries arbitrary text - used for domain verification, SPF mail
policy, and more - and its RDATA is one or more character-strings, each with
its own length byte, packed one after another. It reuses the length-prefix idea
from labels but with no zero terminator: you keep reading strings until you have
consumed all RDLENGTH bytes. So 05 hello 05 world is the two strings hello and
world.
The bound is what keeps it safe: loop only while there are RDATA bytes left, using
RDLENGTH (which you already captured when framing the record) as the limit. A
length byte of 0 is a legitimate empty string, so a single 00 byte parses
to a list containing one empty string, not an empty list. Watching that edge is the
whole subtlety. One multi-field record remains - the zone’s SOA - and then you can
parse a whole section of records at once.
func parseTXT(rdata []byte) []string {var out []stringfor i := 0; i < len(rdata); {n := int(rdata[i]); i++ // each string is length-prefixedout = append(out, string(rdata[i:i+n]))i += n}return out}