/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Single-file source-code for debase64. To compile a smaller-sized command-line app, you can use the `go` command as follows: go build -ldflags "-s -w" -trimpath debase64.go */ package main import ( "bytes" "encoding/base64" "errors" "io" "os" "strings" ) const info = ` debase64 [file/data-URI...] Decode base64-encoded files and/or data-URIs. ` func main() { if len(os.Args) > 2 { os.Stderr.WriteString(info[1:]) os.Exit(1) } if len(os.Args) > 1 { switch os.Args[1] { case `-h`, `--h`, `-help`, `--help`: os.Stderr.WriteString(info[1:]) return } } name := `-` if len(os.Args) > 1 { name = os.Args[1] } if err := run(name); err != nil { os.Stderr.WriteString("\x1b[31m") os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\x1b[0m\n") os.Exit(1) } } func run(s string) error { if s == `-` { return debase64(os.Stdout, os.Stdin) } if seemsDataURI(s) { return debase64(os.Stdout, strings.NewReader(s)) } f, err := os.Open(s) if err != nil { return err } defer f.Close() return debase64(os.Stdout, f) } // debase64 decodes base64 chunks explicitly, so decoding errors can be told // apart from output-writing ones func debase64(w io.Writer, r io.Reader) error { var buf [16 * 1024]byte n, err := r.Read(buf[:]) if n < 1 && err == io.EOF { return nil } if err != nil { return err } start, err := skipIntroDataURI(buf[:n]) if err != nil { return err } start = append([]byte(nil), start...) r = io.MultiReader(bytes.NewReader(start), r) dec := base64.NewDecoder(base64.StdEncoding, r) for { n, err := dec.Read(buf[:]) if n < 1 && err == io.EOF { return nil } if err != nil { return err } if _, err := w.Write(buf[:n]); err != nil { // assume write-errors are always due to deliberately-closed // stdout pipes: ignore those errors and quit right away return nil } } } func skipIntroDataURI(chunk []byte) ([]byte, error) { if !bytes.HasPrefix(chunk, []byte(`data:`)) { return chunk, nil } start := chunk if len(start) > 64 { start = start[:64] } i := bytes.Index(start, []byte(`;base64,`)) if i < 0 { return chunk, errors.New(`invalid data URI`) } return chunk[i+len(`;base64,`):], nil } func seemsDataURI(s string) bool { start := s if len(s) > 64 { start = s[:64] } return strings.HasPrefix(s, `data:`) && strings.Contains(start, `;base64,`) }