Refactor code structure

Move X.509 and Openpgp operations into custom packages. Implement more
robust input reading. Do not convert []byte to string unnecessarily.

Finish implementation of X.509 CRL creation.
This commit is contained in:
Jan Dittberner 2021-01-05 19:59:43 +01:00
parent 2de9771472
commit 9f0916b14a
9 changed files with 715 additions and 493 deletions

View file

@ -1,6 +1,7 @@
package shared
import (
"bytes"
"fmt"
"io"
"time"
@ -13,31 +14,36 @@ func ReceiveBytes(port io.Reader, count int, timeout time.Duration) ([]byte, err
readCh := make(chan []byte, 1)
errCh := make(chan error, 1)
go func() {
sumRead := 0
for {
data := make([]byte, count)
buffer := bytes.NewBuffer([]byte{})
for remainder := count; remainder > 0; {
data := make([]byte, remainder)
if readBytes, err := port.Read(data); err != nil {
errCh <- err
return
} else if readBytes > 0 {
log.Tracef("%d bytes read", readBytes)
sumRead += readBytes
readCh <- data[0:readBytes]
} else {
readCh <- make([]byte, 0)
}
if sumRead >= count {
break
buffer.Write(data[0:readBytes])
remainder -= readBytes
log.Tracef("%d bytes read, remaining %d", readBytes, remainder)
}
}
readCh <- buffer.Bytes()
close(readCh)
}()
buffer := bytes.NewBuffer([]byte{})
select {
case <-time.After(timeout):
return nil, fmt.Errorf("timeout passed %v", timeout)
case err := <-errCh:
return nil, err
case data := <-readCh:
return data, nil
log.Tracef("received %d bytes from channel", len(data))
if data == nil {
break
}
buffer.Write(data)
}
return buffer.Bytes(), nil
}
func SendBytes(port io.Writer, data []byte) error {