]> Cypherpunks.ru repositories - nncp.git/commitdiff
Simplify Base32-related code
authorSergey Matveev <stargrave@stargrave.org>
Wed, 1 Jan 2020 23:04:35 +0000 (02:04 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 1 Jan 2020 23:04:39 +0000 (02:04 +0300)
15 files changed:
src/base32.go [deleted file]
src/cfg.go
src/check.go
src/cmd/nncp-bundle/main.go
src/cmd/nncp-call/main.go
src/cmd/nncp-cfgmin/main.go
src/cmd/nncp-cfgnew/main.go
src/cmd/nncp-pkt/main.go
src/cmd/nncp-stat/main.go
src/jobs.go
src/nncp.go
src/node.go
src/sp.go
src/tmp.go
src/toss_test.go

diff --git a/src/base32.go b/src/base32.go
deleted file mode 100644 (file)
index 0f7db5b..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-package nncp
-
-import (
-       "encoding/base32"
-       "strings"
-)
-
-func ToBase32(data []byte) string {
-       return strings.TrimRight(base32.StdEncoding.EncodeToString(data), "=")
-}
-
-func FromBase32(data string) ([]byte, error) {
-       padSize := len(data) % 8
-       if padSize != 0 {
-               padSize = 8 - padSize
-               pad := make([]byte, 0, padSize)
-               for i := 0; i < padSize; i++ {
-                       pad = append(pad, '=')
-               }
-               data += string(pad)
-       }
-       return base32.StdEncoding.DecodeString(data)
-}
index f003dcb0bd85dcd2bc31585a69d1a03a515b860d..dabb510a9a3e552b6cf646ce42e5aef9d868c5ac 100644 (file)
@@ -123,7 +123,7 @@ func NewNode(name string, cfg NodeJSON) (*Node, error) {
                return nil, err
        }
 
-       exchPub, err := FromBase32(cfg.ExchPub)
+       exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
        if err != nil {
                return nil, err
        }
@@ -131,7 +131,7 @@ func NewNode(name string, cfg NodeJSON) (*Node, error) {
                return nil, errors.New("Invalid exchPub size")
        }
 
-       signPub, err := FromBase32(cfg.SignPub)
+       signPub, err := Base32Codec.DecodeString(cfg.SignPub)
        if err != nil {
                return nil, err
        }
@@ -141,7 +141,7 @@ func NewNode(name string, cfg NodeJSON) (*Node, error) {
 
        var noisePub []byte
        if cfg.NoisePub != nil {
-               noisePub, err = FromBase32(*cfg.NoisePub)
+               noisePub, err = Base32Codec.DecodeString(*cfg.NoisePub)
                if err != nil {
                        return nil, err
                }
@@ -309,7 +309,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
                return nil, err
        }
 
-       exchPub, err := FromBase32(cfg.ExchPub)
+       exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
        if err != nil {
                return nil, err
        }
@@ -317,7 +317,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
                return nil, errors.New("Invalid exchPub size")
        }
 
-       exchPrv, err := FromBase32(cfg.ExchPrv)
+       exchPrv, err := Base32Codec.DecodeString(cfg.ExchPrv)
        if err != nil {
                return nil, err
        }
@@ -325,7 +325,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
                return nil, errors.New("Invalid exchPrv size")
        }
 
-       signPub, err := FromBase32(cfg.SignPub)
+       signPub, err := Base32Codec.DecodeString(cfg.SignPub)
        if err != nil {
                return nil, err
        }
@@ -333,7 +333,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
                return nil, errors.New("Invalid signPub size")
        }
 
-       signPrv, err := FromBase32(cfg.SignPrv)
+       signPrv, err := Base32Codec.DecodeString(cfg.SignPrv)
        if err != nil {
                return nil, err
        }
@@ -341,7 +341,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
                return nil, errors.New("Invalid signPrv size")
        }
 
-       noisePub, err := FromBase32(cfg.NoisePub)
+       noisePub, err := Base32Codec.DecodeString(cfg.NoisePub)
        if err != nil {
                return nil, err
        }
@@ -349,7 +349,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
                return nil, errors.New("Invalid noisePub size")
        }
 
-       noisePrv, err := FromBase32(cfg.NoisePrv)
+       noisePrv, err := Base32Codec.DecodeString(cfg.NoisePrv)
        if err != nil {
                return nil, err
        }
index ae5b80f0c4a0bd7d24d89feb2c044ab01a938fe7..9bdc2864051ccd88537c622bba0520265000f13e 100644 (file)
@@ -44,7 +44,7 @@ func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool {
                sds := SDS{
                        "xx":       string(xx),
                        "node":     nodeId,
-                       "pkt":      ToBase32(job.HshValue[:]),
+                       "pkt":      Base32Codec.EncodeToString(job.HshValue[:]),
                        "fullsize": job.Size,
                }
                gut, err := Check(job.Fd, job.HshValue[:], sds, ctx.ShowPrgrs)
index 76854283334c6015a01605472a5fbca877015df6..859fe6f8e5e38f05512d85f9127557e3c76bb690 100644 (file)
@@ -155,7 +155,7 @@ func main() {
                                if _, err = nncp.CopyProgressed(
                                        tarWr, job.Fd, "Tx",
                                        nncp.SdsAdd(sds, nncp.SDS{
-                                               "pkt":      nncp.ToBase32(job.HshValue[:]),
+                                               "pkt":      nncp.Base32Codec.EncodeToString(job.HshValue[:]),
                                                "fullsize": job.Size,
                                        }),
                                        ctx.ShowPrgrs,
@@ -244,7 +244,7 @@ func main() {
                                continue
                        }
                        pktName = filepath.Base(entry.Name)
-                       if _, err = nncp.FromBase32(pktName); err != nil {
+                       if _, err = nncp.Base32Codec.DecodeString(pktName); err != nil {
                                ctx.LogD("nncp-bundle", nncp.SdsAdd(sds, nncp.SDS{"err": "bad packet name"}), "")
                                continue
                        }
@@ -271,7 +271,7 @@ func main() {
                                                continue
                                        }
                                }
-                               nodeId32 := nncp.ToBase32(pktEnc.Recipient[:])
+                               nodeId32 := nncp.Base32Codec.EncodeToString(pktEnc.Recipient[:])
                                sds["xx"] = string(nncp.TTx)
                                sds["node"] = nodeId32
                                sds["pkt"] = pktName
@@ -299,7 +299,7 @@ func main() {
                                ); err != nil {
                                        log.Fatalln("Error during copying:", err)
                                }
-                               if nncp.ToBase32(hsh.Sum(nil)) == pktName {
+                               if nncp.Base32Codec.EncodeToString(hsh.Sum(nil)) == pktName {
                                        ctx.LogI("nncp-bundle", sds, "removed")
                                        if !*dryRun {
                                                os.Remove(dstPath)
@@ -319,7 +319,7 @@ func main() {
                                        continue
                                }
                        }
-                       sds["node"] = nncp.ToBase32(pktEnc.Recipient[:])
+                       sds["node"] = nncp.Base32Codec.EncodeToString(pktEnc.Recipient[:])
                        sds["pkt"] = pktName
                        sds["fullsize"] = entry.Size
                        selfPath = filepath.Join(ctx.Spool, ctx.SelfId.String(), string(nncp.TRx))
@@ -344,7 +344,7 @@ func main() {
                                        if _, err = nncp.CopyProgressed(hsh, tarR, "check", sds, ctx.ShowPrgrs); err != nil {
                                                log.Fatalln("Error during copying:", err)
                                        }
-                                       if nncp.ToBase32(hsh.Sum(nil)) != pktName {
+                                       if nncp.Base32Codec.EncodeToString(hsh.Sum(nil)) != pktName {
                                                ctx.LogE("nncp-bundle", sds, errors.New("bad checksum"), "")
                                                continue
                                        }
@@ -362,7 +362,7 @@ func main() {
                                        if err = tmp.W.Flush(); err != nil {
                                                log.Fatalln("Error during flusing:", err)
                                        }
-                                       if nncp.ToBase32(tmp.Hsh.Sum(nil)) == pktName {
+                                       if nncp.Base32Codec.EncodeToString(tmp.Hsh.Sum(nil)) == pktName {
                                                if err = tmp.Commit(selfPath); err != nil {
                                                        log.Fatalln("Error during commiting:", err)
                                                }
index ae6bffeb39b7f997e3cf5e45d952b3964c28a840..634c003178602963f364d6a38ca8f5802c4b3ce8 100644 (file)
@@ -142,7 +142,7 @@ func main() {
                splitted = strings.Split(*onlyPktsRaw, ",")
                onlyPkts = make(map[[32]byte]bool, len(splitted))
                for _, pktIdRaw := range splitted {
-                       pktId, err := nncp.FromBase32(pktIdRaw)
+                       pktId, err := nncp.Base32Codec.DecodeString(pktIdRaw)
                        if err != nil {
                                log.Fatalln("Invalid packet specified: ", err)
                        }
index b8eae7ef3e29f2e39f0316d4b9411c12936d64dc..20bd14c6f3c894fa6f700b5c13cb885c229c8304 100644 (file)
@@ -65,13 +65,13 @@ func main() {
        for _, node := range ctx.Neigh {
                var noisePub *string
                if node.NoisePub != nil {
-                       np := nncp.ToBase32(node.NoisePub[:])
+                       np := nncp.Base32Codec.EncodeToString(node.NoisePub[:])
                        noisePub = &np
                }
                cfg.Neigh[node.Name] = nncp.NodeJSON{
                        Id:       node.Id.String(),
-                       ExchPub:  nncp.ToBase32(node.ExchPub[:]),
-                       SignPub:  nncp.ToBase32(node.SignPub[:]),
+                       ExchPub:  nncp.Base32Codec.EncodeToString(node.ExchPub[:]),
+                       SignPub:  nncp.Base32Codec.EncodeToString(node.SignPub[:]),
                        NoisePub: noisePub,
                }
        }
index ff638260ce0f945e459f0cf748a9ae02299e59f6..8dad81875a442d3ca86a3ede600459b6003cd7df 100644 (file)
@@ -82,16 +82,16 @@ func main() {
                        nncp.DefaultSpoolPath,
                        nncp.DefaultLogPath,
                        nodeOur.Id.String(),
-                       nncp.ToBase32(nodeOur.ExchPub[:]),
-                       nncp.ToBase32(nodeOur.ExchPrv[:]),
-                       nncp.ToBase32(nodeOur.SignPub[:]),
-                       nncp.ToBase32(nodeOur.SignPrv[:]),
-                       nncp.ToBase32(nodeOur.NoisePrv[:]),
-                       nncp.ToBase32(nodeOur.NoisePub[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.ExchPub[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.ExchPrv[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.SignPub[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.SignPrv[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.NoisePrv[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.NoisePub[:]),
                        nodeOur.Id.String(),
-                       nncp.ToBase32(nodeOur.ExchPub[:]),
-                       nncp.ToBase32(nodeOur.SignPub[:]),
-                       nncp.ToBase32(nodeOur.NoisePub[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.ExchPub[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.SignPub[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.NoisePub[:]),
                        nncp.DefaultSendmailPath,
                )
        } else {
@@ -218,16 +218,16 @@ func main() {
                        nncp.DefaultSpoolPath,
                        nncp.DefaultLogPath,
                        nodeOur.Id.String(),
-                       nncp.ToBase32(nodeOur.ExchPub[:]),
-                       nncp.ToBase32(nodeOur.ExchPrv[:]),
-                       nncp.ToBase32(nodeOur.SignPub[:]),
-                       nncp.ToBase32(nodeOur.SignPrv[:]),
-                       nncp.ToBase32(nodeOur.NoisePrv[:]),
-                       nncp.ToBase32(nodeOur.NoisePub[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.ExchPub[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.ExchPrv[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.SignPub[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.SignPrv[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.NoisePrv[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.NoisePub[:]),
                        nodeOur.Id.String(),
-                       nncp.ToBase32(nodeOur.ExchPub[:]),
-                       nncp.ToBase32(nodeOur.SignPub[:]),
-                       nncp.ToBase32(nodeOur.NoisePub[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.ExchPub[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.SignPub[:]),
+                       nncp.Base32Codec.EncodeToString(nodeOur.NoisePub[:]),
                        nncp.DefaultSendmailPath,
                        nncp.DefaultSendmailPath,
                )
index 3f42cd260e48a25c2f404b94ea769243f4be68db..126497484ddeda0521139430cb6036a5a484be93 100644 (file)
@@ -118,7 +118,7 @@ func main() {
                                -1,
                        ))
                case nncp.PktTypeTrns:
-                       path = nncp.ToBase32(pkt.Path[:pkt.PathLen])
+                       path = nncp.Base32Codec.EncodeToString(pkt.Path[:pkt.PathLen])
                default:
                        path = string(pkt.Path[:pkt.PathLen])
                }
index 09cc63b66e8157ef7e51847b16bf290e21a758d6..d80999595411b14abd6790412636174f0c5142b6 100644 (file)
@@ -40,7 +40,7 @@ func jobPrint(xx nncp.TRxTx, job nncp.Job) {
        fmt.Printf(
                "\t%s %s %s (nice: %s)\n",
                string(xx),
-               nncp.ToBase32(job.HshValue[:]),
+               nncp.Base32Codec.EncodeToString(job.HshValue[:]),
                humanize.IBytes(uint64(job.Size)),
                nncp.NicenessFmt(job.PktEnc.Nice),
        )
index 16c95bfbbe2b7276499c912e0fb28a888c21c19c..bbc9946b38166b97fbe28bbf31bd6c27d887d2a7 100644 (file)
@@ -54,7 +54,7 @@ func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job {
                        return
                }
                for _, fi := range fis {
-                       hshValue, err := FromBase32(fi.Name())
+                       hshValue, err := Base32Codec.DecodeString(fi.Name())
                        if err != nil {
                                continue
                        }
index 93e4446aa6e87425fd09764a719219ef2d84970b..22d75782c2e93a2e22db878b2e81b127c949fc00 100644 (file)
@@ -19,6 +19,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package nncp
 
 import (
+       "encoding/base32"
        "runtime"
 )
 
@@ -38,6 +39,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.`
 
 var (
        Version string = "UNKNOWN"
+
+       Base32Codec *base32.Encoding = base32.StdEncoding.WithPadding(base32.NoPadding)
 )
 
 func VersionGet() string {
index 7bfd0efda5723cb71d164410a4428a2fbf47627e..999af3d9b3bc88e5624669e004809c62bba5b2be 100644 (file)
@@ -32,7 +32,7 @@ import (
 type NodeId [blake2b.Size256]byte
 
 func (id NodeId) String() string {
-       return ToBase32(id[:])
+       return Base32Codec.EncodeToString(id[:])
 }
 
 type Node struct {
@@ -112,7 +112,7 @@ func (nodeOur *NodeOur) Their() *Node {
 }
 
 func NodeIdFromString(raw string) (*NodeId, error) {
-       decoded, err := FromBase32(raw)
+       decoded, err := Base32Codec.DecodeString(raw)
        if err != nil {
                return nil, err
        }
index 37cf265ead11ac3588fcb66eab69f7641599cc9c..dc1d9c4796bff3b80c9571300efd69477d2b7eec 100644 (file)
--- a/src/sp.go
+++ b/src/sp.go
@@ -311,7 +311,7 @@ func (ctx *Ctx) infosOur(nodeId *NodeId, nice uint8, seen *map[[32]byte]uint8) [
                payloads = append(payloads, MarshalSP(SPTypeInfo, info))
                ctx.LogD("sp-info-our", SDS{
                        "node": nodeId,
-                       "name": ToBase32(info.Hash[:]),
+                       "name": Base32Codec.EncodeToString(info.Hash[:]),
                        "size": info.Size,
                }, "")
        }
@@ -464,7 +464,7 @@ func (state *SPState) StartR(conn ConnDeadlined) error {
                }
        }
        if node == nil {
-               peerId := ToBase32(state.hs.PeerStatic())
+               peerId := Base32Codec.EncodeToString(state.hs.PeerStatic())
                state.Ctx.LogE("sp-start", SDS{"peer": peerId}, errors.New("unknown"), "")
                return errors.New("Unknown peer: " + peerId)
        }
@@ -677,7 +677,7 @@ func (state *SPState) StartWorkers(
                                }
                                sdsp := SdsAdd(sds, SDS{
                                        "xx":   string(TTx),
-                                       "pkt":  ToBase32(freq.Hash[:]),
+                                       "pkt":  Base32Codec.EncodeToString(freq.Hash[:]),
                                        "size": int64(freq.Offset),
                                })
                                state.Ctx.LogD("sp-file", sdsp, "queueing")
@@ -685,7 +685,7 @@ func (state *SPState) StartWorkers(
                                        state.Ctx.Spool,
                                        state.Node.Id.String(),
                                        string(TTx),
-                                       ToBase32(freq.Hash[:]),
+                                       Base32Codec.EncodeToString(freq.Hash[:]),
                                ))
                                if err != nil {
                                        state.Ctx.LogE("sp-file", sdsp, err, "")
@@ -872,7 +872,7 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) {
                                return nil, err
                        }
                        sdsp = SdsAdd(sds, SDS{
-                               "pkt":  ToBase32(info.Hash[:]),
+                               "pkt":  Base32Codec.EncodeToString(info.Hash[:]),
                                "size": int64(info.Size),
                                "nice": int(info.Nice),
                        })
@@ -892,7 +892,7 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) {
                                state.Ctx.Spool,
                                state.Node.Id.String(),
                                string(TRx),
-                               ToBase32(info.Hash[:]),
+                               Base32Codec.EncodeToString(info.Hash[:]),
                        )
                        if _, err = os.Stat(pktPath); err == nil {
                                state.Ctx.LogI("sp-info", sdsp, "already done")
@@ -937,14 +937,14 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) {
                                return nil, err
                        }
                        sdsp["xx"] = string(TRx)
-                       sdsp["pkt"] = ToBase32(file.Hash[:])
+                       sdsp["pkt"] = Base32Codec.EncodeToString(file.Hash[:])
                        sdsp["size"] = len(file.Payload)
                        dirToSync := filepath.Join(
                                state.Ctx.Spool,
                                state.Node.Id.String(),
                                string(TRx),
                        )
-                       filePath := filepath.Join(dirToSync, ToBase32(file.Hash[:]))
+                       filePath := filepath.Join(dirToSync, Base32Codec.EncodeToString(file.Hash[:]))
                        state.Ctx.LogD("sp-file", sdsp, "opening part")
                        fd, err := os.OpenFile(
                                filePath+PartSuffix,
@@ -1034,13 +1034,13 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) {
                                state.Ctx.LogE("sp-process", SdsAdd(sds, SDS{"type": "done"}), err, "")
                                return nil, err
                        }
-                       sdsp["pkt"] = ToBase32(done.Hash[:])
+                       sdsp["pkt"] = Base32Codec.EncodeToString(done.Hash[:])
                        state.Ctx.LogD("sp-done", sdsp, "removing")
                        err := os.Remove(filepath.Join(
                                state.Ctx.Spool,
                                state.Node.Id.String(),
                                string(TTx),
-                               ToBase32(done.Hash[:]),
+                               Base32Codec.EncodeToString(done.Hash[:]),
                        ))
                        sdsp["xx"] = string(TTx)
                        if err == nil {
@@ -1056,7 +1056,7 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) {
                                state.Ctx.LogE("sp-process", sdsp, err, "")
                                return nil, err
                        }
-                       sdsp["pkt"] = ToBase32(freq.Hash[:])
+                       sdsp["pkt"] = Base32Codec.EncodeToString(freq.Hash[:])
                        sdsp["offset"] = freq.Offset
                        state.Ctx.LogD("sp-process", sdsp, "queueing")
                        nice, exists := state.infosOurSeen[*freq.Hash]
index 32c3fdba1aed68b16a39477c9f810a73646a7a5e..272fbb3ec4bcac76a4bfc0f7556d2aa8ed1525af 100644 (file)
@@ -106,7 +106,7 @@ func (tmp *TmpFileWHash) Commit(dir string) error {
                return err
        }
        tmp.Fd.Close()
-       checksum := ToBase32(tmp.Hsh.Sum(nil))
+       checksum := Base32Codec.EncodeToString(tmp.Hsh.Sum(nil))
        tmp.ctx.LogD("tmp", SDS{"src": tmp.Fd.Name(), "dst": checksum}, "commit")
        if err = os.Rename(tmp.Fd.Name(), filepath.Join(dir, checksum)); err != nil {
                return err
index b6df326256cf6c0eba277f9ff47b379463f85621..a76615de237f7728d373cc115731290611c4333c 100644 (file)
@@ -189,7 +189,7 @@ func TestTossFile(t *testing.T) {
                incomingPath := filepath.Join(spool, "incoming")
                for _, fileData := range files {
                        checksum := blake2b.Sum256(fileData)
-                       fileName := ToBase32(checksum[:])
+                       fileName := Base32Codec.EncodeToString(checksum[:])
                        src := filepath.Join(spool, fileName)
                        if err := ioutil.WriteFile(src, fileData, os.FileMode(0600)); err != nil {
                                panic(err)
@@ -220,7 +220,7 @@ func TestTossFile(t *testing.T) {
                }
                for _, fileData := range files {
                        checksum := blake2b.Sum256(fileData)
-                       fileName := ToBase32(checksum[:])
+                       fileName := Base32Codec.EncodeToString(checksum[:])
                        data, err := ioutil.ReadFile(filepath.Join(incomingPath, fileName))
                        if err != nil {
                                panic(err)
@@ -468,7 +468,7 @@ func TestTossTrns(t *testing.T) {
                        }
                        checksum := blake2b.Sum256(dst.Bytes())
                        if err := ioutil.WriteFile(
-                               filepath.Join(rxPath, ToBase32(checksum[:])),
+                               filepath.Join(rxPath, Base32Codec.EncodeToString(checksum[:])),
                                dst.Bytes(),
                                os.FileMode(0600),
                        ); err != nil {