2023-05-11 18:05:31 +00:00
|
|
|
|
// Copyright Martin Dosch.
|
2022-04-09 03:00:38 +00:00
|
|
|
|
// Use of this source code is governed by the BSD-2-clause
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2022-04-18 08:16:00 +00:00
|
|
|
|
"bytes"
|
2023-06-04 14:14:32 +00:00
|
|
|
|
"crypto/rand"
|
2022-04-09 03:00:38 +00:00
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
2023-06-04 14:14:32 +00:00
|
|
|
|
"math/big"
|
2023-02-18 14:42:04 +00:00
|
|
|
|
"net/url"
|
2022-04-18 08:16:00 +00:00
|
|
|
|
"os"
|
2022-11-05 11:40:40 +00:00
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
2022-04-09 03:00:38 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-11-05 11:40:40 +00:00
|
|
|
|
func validUTF8(s string) string {
|
|
|
|
|
// Remove invalid code points.
|
2023-08-10 11:42:31 +00:00
|
|
|
|
s = strings.ToValidUTF8(s, "<22>")
|
2022-11-05 11:40:40 +00:00
|
|
|
|
reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`)
|
2023-08-10 11:42:31 +00:00
|
|
|
|
s = reg.ReplaceAllString(s, "<22>")
|
2022-11-05 11:40:40 +00:00
|
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 15:24:46 +00:00
|
|
|
|
func validURI(s string) (*url.URL, error) {
|
2023-02-18 14:42:04 +00:00
|
|
|
|
// Check if URI is valid
|
2023-02-18 15:24:46 +00:00
|
|
|
|
uri, err := url.ParseRequestURI(s)
|
2023-06-06 20:09:59 +00:00
|
|
|
|
return uri, fmt.Errorf("validURI: %w", err)
|
2023-02-18 14:42:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-18 08:16:00 +00:00
|
|
|
|
func readFile(path string) (*bytes.Buffer, error) {
|
|
|
|
|
file, err := os.Open(path)
|
2023-08-16 06:49:43 +00:00
|
|
|
|
if err != nil {
|
2023-06-06 20:09:59 +00:00
|
|
|
|
return nil, fmt.Errorf("readFile: %w", err)
|
2022-04-18 08:16:00 +00:00
|
|
|
|
}
|
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
_, err = buffer.ReadFrom(file)
|
2023-08-16 06:49:43 +00:00
|
|
|
|
if err != nil {
|
2023-06-06 20:09:59 +00:00
|
|
|
|
return nil, fmt.Errorf("readFile: %w", err)
|
2022-04-18 08:16:00 +00:00
|
|
|
|
}
|
2023-06-04 13:15:31 +00:00
|
|
|
|
err = file.Close()
|
2023-08-16 06:49:43 +00:00
|
|
|
|
if err != nil {
|
2023-06-06 20:09:59 +00:00
|
|
|
|
fmt.Println("error while closing file:", err)
|
2023-06-04 13:15:31 +00:00
|
|
|
|
}
|
2022-04-18 08:16:00 +00:00
|
|
|
|
return buffer, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 09:36:41 +00:00
|
|
|
|
func getRpad(messageLength int) string {
|
2023-05-29 09:40:37 +00:00
|
|
|
|
rpadRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
2023-06-06 08:47:41 +00:00
|
|
|
|
length := defaultRpadMultiple - messageLength%defaultRpadMultiple
|
2023-06-04 14:14:32 +00:00
|
|
|
|
max := big.NewInt(int64(len(rpadRunes)))
|
2023-05-29 09:40:37 +00:00
|
|
|
|
rpad := make([]rune, length)
|
|
|
|
|
for i := range rpad {
|
2023-06-04 14:14:32 +00:00
|
|
|
|
randInt, err := rand.Int(rand.Reader, max)
|
2023-08-16 06:49:43 +00:00
|
|
|
|
if err != nil {
|
2023-06-04 14:14:32 +00:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
rpad[i] = rpadRunes[randInt.Int64()]
|
2022-04-17 15:16:29 +00:00
|
|
|
|
}
|
2023-05-29 09:40:37 +00:00
|
|
|
|
return string(rpad)
|
2022-04-17 15:16:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-09 03:00:38 +00:00
|
|
|
|
func getID() string {
|
2023-06-06 08:47:41 +00:00
|
|
|
|
id := make([]byte, defaultIDBytes)
|
2022-05-02 14:13:00 +00:00
|
|
|
|
_, err := rand.Read(id)
|
2023-08-16 06:49:43 +00:00
|
|
|
|
if err != nil {
|
2022-04-09 03:00:38 +00:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2022-05-02 14:13:00 +00:00
|
|
|
|
return fmt.Sprintf("%x-%x-%x", id[0:4], id[4:8], id[8:])
|
2022-04-09 03:00:38 +00:00
|
|
|
|
}
|
2022-04-23 21:40:15 +00:00
|
|
|
|
|
|
|
|
|
func getShortID() string {
|
2023-06-06 08:47:41 +00:00
|
|
|
|
id := make([]byte, defaultShortIDBytes)
|
2022-05-02 14:13:00 +00:00
|
|
|
|
_, err := rand.Read(id)
|
2023-08-16 06:49:43 +00:00
|
|
|
|
if err != nil {
|
2022-04-23 21:40:15 +00:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2022-05-02 14:13:00 +00:00
|
|
|
|
return fmt.Sprintf("%x", id[0:4])
|
2022-04-23 21:40:15 +00:00
|
|
|
|
}
|