1 // netstring -- netstring format serialization library
2 // Copyright (C) 2015-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 "go.cypherpunks.ru/netstring/v2"
29 fmt.Fprintf(os.Stderr, "ns -- Work with netstring encoded files\n\n")
30 fmt.Fprintf(os.Stderr, "Usage: %s list FILE\n", os.Args[0])
31 fmt.Fprintf(os.Stderr, " %s read FILE CHUNK > data\n", os.Args[0])
32 fmt.Fprintf(os.Stderr, " %s write FILE ... > data\n", os.Args[0])
33 fmt.Fprintf(os.Stderr, "FILE can be \"-\" for reading from stdin\n")
37 func fileRead(path string) io.Reader {
41 fd, err := os.Open(path)
56 r := netstring.NewReader(fileRead(os.Args[2]))
65 fmt.Printf("%d\t%d\n", i, size)
69 if len(os.Args) != 4 {
72 chunk, err := strconv.Atoi(os.Args[3])
76 r := netstring.NewReader(fileRead(os.Args[2]))
77 for i := 0; i < chunk; i++ {
88 if _, err = io.Copy(os.Stdout, r); err != nil {
92 w := netstring.NewWriter(os.Stdout)
93 for i, fn := range os.Args[2:] {
94 fd, err := os.Open(fn)
102 size := uint64(fi.Size())
103 if _, err = w.WriteSize(size); err != nil {
106 if _, err = io.Copy(w, fd); err != nil {
109 fmt.Fprintf(os.Stderr, "%d\t%d\t%s\n", i, size, fn)