2021-03-08 02:19:22 +08:00
|
|
|
package txtutil
|
|
|
|
|
2023-05-21 01:21:45 +08:00
|
|
|
import "github.com/StackExchange/dnscontrol/v4/models"
|
2021-03-08 02:19:22 +08:00
|
|
|
|
|
|
|
// SplitSingleLongTxt finds TXT records with a single long string and splits it
|
|
|
|
// into 255-octet chunks. This is used by providers that, when a user specifies
|
|
|
|
// one long TXT string, split it into smaller strings behind the scenes.
|
2023-04-15 03:22:23 +08:00
|
|
|
// This should be called from GetZoneRecordsCorrections().
|
2021-03-08 02:19:22 +08:00
|
|
|
func SplitSingleLongTxt(records []*models.RecordConfig) {
|
|
|
|
for _, rc := range records {
|
|
|
|
if rc.HasFormatIdenticalToTXT() {
|
|
|
|
s := rc.TxtStrings[0]
|
|
|
|
if len(rc.TxtStrings) == 1 && len(s) > 255 {
|
|
|
|
rc.SetTargetTXTs(splitChunks(s, 255))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 23:37:46 +08:00
|
|
|
// Segment returns the string as 255-octet segments, the last being the remainder.
|
|
|
|
func Segment(s string) []string {
|
|
|
|
return splitChunks(s, 255)
|
|
|
|
}
|
|
|
|
|
2021-03-08 02:19:22 +08:00
|
|
|
func splitChunks(buf string, lim int) []string {
|
|
|
|
var chunk string
|
|
|
|
chunks := make([]string, 0, len(buf)/lim+1)
|
|
|
|
for len(buf) >= lim {
|
|
|
|
chunk, buf = buf[:lim], buf[lim:]
|
|
|
|
chunks = append(chunks, chunk)
|
|
|
|
}
|
|
|
|
if len(buf) > 0 {
|
|
|
|
chunks = append(chunks, buf[:])
|
|
|
|
}
|
|
|
|
return chunks
|
|
|
|
}
|