Move string validation into own helper function.

v0.5
Martin Dosch 2 years ago
parent 679740d516
commit dd9566eaee
No known key found for this signature in database
GPG Key ID: 52A57CFCE13D657D

@ -10,8 +10,19 @@ import (
"fmt"
"log"
"os"
"regexp"
"strings"
)
func validUTF8(s string) string {
// Remove invalid code points.
s = strings.ToValidUTF8(s, "")
reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`)
s = reg.ReplaceAllString(s, "")
return s
}
func readFile(path string) (*bytes.Buffer, error) {
file, err := os.Open(path)
if err != nil {

@ -12,7 +12,6 @@ import (
"log"
"net"
"os"
"regexp"
"strings"
"time"
@ -290,11 +289,7 @@ func main() {
}
}
// Remove invalid code points.
message = strings.ToValidUTF8(message, "")
reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`)
message = reg.ReplaceAllString(message, "")
message = validUTF8(message)
// Exit if message is empty.
if message == "" && !*flagInteractive && !*flagListen && *flagHTTPUpload == "" {
os.Exit(0)
@ -428,9 +423,7 @@ func main() {
}
// Remove invalid code points.
message = strings.ToValidUTF8(message, "")
reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`)
message = reg.ReplaceAllString(message, "")
message = validUTF8(message)
if message == "" {
continue
}

@ -9,7 +9,6 @@ import (
"errors"
"log"
"os"
"regexp"
"runtime"
"strings"
"time"
@ -99,9 +98,7 @@ func oxDecrypt(m xmpp.Chat, client *xmpp.Client, iqc chan xmpp.IQ,
return "error", time.Now(), err
}
// Remove invalid code points.
message := strings.ToValidUTF8(string(decryptMsg.Data), "")
reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`)
message = reg.ReplaceAllString(message, "")
message := validUTF8(string(decryptMsg.Data))
doc := etree.NewDocument()
err = doc.ReadFromString(message)
if err != nil {

Loading…
Cancel
Save