diff --git a/bytewriter.go b/bytewriter.go index 218c939..436af16 100644 --- a/bytewriter.go +++ b/bytewriter.go @@ -1,37 +1,37 @@ // This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication // license. Its contents can be found at: // http://creativecommons.org/publicdomain/zero/1.0/ - -package main - -import ( - "fmt" - "io" -) - -var newline = []byte{'\n'} - -type ByteWriter struct { - io.Writer - c int -} - -func (w *ByteWriter) Write(p []byte) (n int, err error) { - if len(p) == 0 { - return - } - - for n = range p { - if w.c%12 == 0 { - w.Writer.Write(newline) - w.c = 0 - } - - fmt.Fprintf(w.Writer, "0x%02x,", p[n]) - w.c++ - } - - n++ - - return -} + +package main + +import ( + "fmt" + "io" +) + +var newline = []byte{'\n'} + +type ByteWriter struct { + io.Writer + c int +} + +func (w *ByteWriter) Write(p []byte) (n int, err error) { + if len(p) == 0 { + return + } + + for n = range p { + if w.c%12 == 0 { + w.Writer.Write(newline) + w.c = 0 + } + + fmt.Fprintf(w.Writer, "0x%02x,", p[n]) + w.c++ + } + + n++ + + return +} diff --git a/stringwriter.go b/stringwriter.go index 0e771c7..11e191b 100644 --- a/stringwriter.go +++ b/stringwriter.go @@ -1,37 +1,37 @@ // This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication // license. Its contents can be found at: // http://creativecommons.org/publicdomain/zero/1.0/ - -package main - -import ( - "fmt" - "io" -) - -var line = []byte("\"+\n\"") - -type StringWriter struct { - io.Writer - c int -} - -func (w *StringWriter) Write(p []byte) (n int, err error) { - if len(p) == 0 { - return - } - - for n = range p { - if w.c%16 == 0 { - w.Writer.Write(line) - w.c = 0 - } - - fmt.Fprintf(w.Writer, "\\x%02x", p[n]) - w.c++ - } - - n++ - - return -} + +package main + +import ( + "fmt" + "io" +) + +var line = []byte("\"+\n\"") + +type StringWriter struct { + io.Writer + c int +} + +func (w *StringWriter) Write(p []byte) (n int, err error) { + if len(p) == 0 { + return + } + + for n = range p { + if w.c%16 == 0 { + w.Writer.Write(line) + w.c = 0 + } + + fmt.Fprintf(w.Writer, "\\x%02x", p[n]) + w.c++ + } + + n++ + + return +} diff --git a/translate.go b/translate.go index 2b1de1f..6b1d973 100644 --- a/translate.go +++ b/translate.go @@ -4,7 +4,11 @@ package main -import "io" +import ( + "compress/gzip" + "fmt" + "io" +) // translate translates the input file to go source code. func translate(input io.Reader, output io.Writer, pkgname, funcname string, uncompressed, nomemcpy bool) { @@ -22,3 +26,128 @@ func translate(input io.Reader, output io.Writer, pkgname, funcname string, unco } } } + +// input -> gzip -> gowriter -> output. +func translate_memcpy_comp(input io.Reader, output io.Writer, pkgname, funcname string) { + fmt.Fprintf(output, `package %s + +import ( + "bytes" + "compress/gzip" + "io" +) + +// %s returns raw, uncompressed file data. +func %s() []byte { + gz, err := gzip.NewReader(bytes.NewBuffer([]byte{`, pkgname, funcname, funcname) + + gz := gzip.NewWriter(&ByteWriter{Writer: output}) + io.Copy(gz, input) + gz.Close() + + fmt.Fprint(output, ` + })) + + if err != nil { + panic("Decompression failed: " + err.Error()) + } + + var b bytes.Buffer + io.Copy(&b, gz) + gz.Close() + + return b.Bytes() +}`) +} + +// input -> gzip -> gowriter -> output. +func translate_memcpy_uncomp(input io.Reader, output io.Writer, pkgname, funcname string) { + fmt.Fprintf(output, `package %s + +// %s returns raw file data. +func %s() []byte { + return []byte{`, pkgname, funcname, funcname) + + io.Copy(&ByteWriter{Writer: output}, input) + + fmt.Fprint(output, ` + } +}`) +} + +// input -> gzip -> gowriter -> output. +func translate_nomemcpy_comp(input io.Reader, output io.Writer, pkgname, funcname string) { + fmt.Fprintf(output, `package %s + +import ( + "bytes" + "compress/gzip" + "io" + "reflect" + "unsafe" +) + +var _%s = "`, pkgname, funcname) + + gz := gzip.NewWriter(&StringWriter{Writer: output}) + io.Copy(gz, input) + gz.Close() + + fmt.Fprintf(output, `" + +// %s returns raw, uncompressed file data. +func %s() []byte { + var empty [0]byte + sx := (*reflect.StringHeader)(unsafe.Pointer(&_%s)) + b := empty[:] + bx := (*reflect.SliceHeader)(unsafe.Pointer(&b)) + bx.Data = sx.Data + bx.Len = len(_%s) + bx.Cap = bx.Len + + gz, err := gzip.NewReader(bytes.NewBuffer(b)) + + if err != nil { + panic("Decompression failed: " + err.Error()) + } + + var buf bytes.Buffer + io.Copy(&buf, gz) + gz.Close() + + return buf.Bytes() +} +`, funcname, funcname, funcname, funcname) +} + +// input -> gowriter -> output. +func translate_nomemcpy_uncomp(input io.Reader, output io.Writer, pkgname, funcname string) { + fmt.Fprintf(output, `package %s + +import ( + "reflect" + "unsafe" +) + +var _%s = "`, pkgname, funcname) + + io.Copy(&StringWriter{Writer: output}, input) + + fmt.Fprintf(output, `" + +// %s returns raw file data. +// +// WARNING: The returned byte slice is READ-ONLY. +// Attempting to alter the slice contents will yield a runtime panic. +func %s() []byte { + var empty [0]byte + sx := (*reflect.StringHeader)(unsafe.Pointer(&_%s)) + b := empty[:] + bx := (*reflect.SliceHeader)(unsafe.Pointer(&b)) + bx.Data = sx.Data + bx.Len = len(_%s) + bx.Cap = bx.Len + return b +} +`, funcname, funcname, funcname, funcname) +} diff --git a/translate_memcpy_comp.go b/translate_memcpy_comp.go deleted file mode 100644 index 8f0c5e8..0000000 --- a/translate_memcpy_comp.go +++ /dev/null @@ -1,44 +0,0 @@ -// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication -// license. Its contents can be found at: -// http://creativecommons.org/publicdomain/zero/1.0/ - -package main - -import ( - "compress/gzip" - "fmt" - "io" -) - -// input -> gzip -> gowriter -> output. -func translate_memcpy_comp(input io.Reader, output io.Writer, pkgname, funcname string) { - fmt.Fprintf(output, `package %s - -import ( - "bytes" - "compress/gzip" - "io" -) - -// %s returns raw, uncompressed file data. -func %s() []byte { - gz, err := gzip.NewReader(bytes.NewBuffer([]byte{`, pkgname, funcname, funcname) - - gz := gzip.NewWriter(&ByteWriter{Writer: output}) - io.Copy(gz, input) - gz.Close() - - fmt.Fprint(output, ` - })) - - if err != nil { - panic("Decompression failed: " + err.Error()) - } - - var b bytes.Buffer - io.Copy(&b, gz) - gz.Close() - - return b.Bytes() -}`) -} diff --git a/translate_memcpy_uncomp.go b/translate_memcpy_uncomp.go deleted file mode 100644 index f3aab0f..0000000 --- a/translate_memcpy_uncomp.go +++ /dev/null @@ -1,25 +0,0 @@ -// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication -// license. Its contents can be found at: -// http://creativecommons.org/publicdomain/zero/1.0/ - -package main - -import ( - "fmt" - "io" -) - -// input -> gzip -> gowriter -> output. -func translate_memcpy_uncomp(input io.Reader, output io.Writer, pkgname, funcname string) { - fmt.Fprintf(output, `package %s - -// %s returns raw file data. -func %s() []byte { - return []byte{`, pkgname, funcname, funcname) - - io.Copy(&ByteWriter{Writer: output}, input) - - fmt.Fprint(output, ` - } -}`) -} diff --git a/translate_nomemcpy_comp.go b/translate_nomemcpy_comp.go deleted file mode 100644 index f3c8b77..0000000 --- a/translate_nomemcpy_comp.go +++ /dev/null @@ -1,56 +0,0 @@ -// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication -// license. Its contents can be found at: -// http://creativecommons.org/publicdomain/zero/1.0/ - -package main - -import ( - "compress/gzip" - "fmt" - "io" -) - -// input -> gzip -> gowriter -> output. -func translate_nomemcpy_comp(input io.Reader, output io.Writer, pkgname, funcname string) { - fmt.Fprintf(output, `package %s - -import ( - "bytes" - "compress/gzip" - "io" - "reflect" - "unsafe" -) - -var _%s = "`, pkgname, funcname) - - gz := gzip.NewWriter(&StringWriter{Writer: output}) - io.Copy(gz, input) - gz.Close() - - fmt.Fprintf(output, `" - -// %s returns raw, uncompressed file data. -func %s() []byte { - var empty [0]byte - sx := (*reflect.StringHeader)(unsafe.Pointer(&_%s)) - b := empty[:] - bx := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - bx.Data = sx.Data - bx.Len = len(_%s) - bx.Cap = bx.Len - - gz, err := gzip.NewReader(bytes.NewBuffer(b)) - - if err != nil { - panic("Decompression failed: " + err.Error()) - } - - var buf bytes.Buffer - io.Copy(&buf, gz) - gz.Close() - - return buf.Bytes() -} -`, funcname, funcname, funcname, funcname) -} diff --git a/translate_nomemcpy_uncomp.go b/translate_nomemcpy_uncomp.go deleted file mode 100644 index ac16a11..0000000 --- a/translate_nomemcpy_uncomp.go +++ /dev/null @@ -1,42 +0,0 @@ -// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication -// license. Its contents can be found at: -// http://creativecommons.org/publicdomain/zero/1.0/ - -package main - -import ( - "fmt" - "io" -) - -// input -> gowriter -> output. -func translate_nomemcpy_uncomp(input io.Reader, output io.Writer, pkgname, funcname string) { - fmt.Fprintf(output, `package %s - -import ( - "reflect" - "unsafe" -) - -var _%s = "`, pkgname, funcname) - - io.Copy(&StringWriter{Writer: output}, input) - - fmt.Fprintf(output, `" - -// %s returns raw file data. -// -// WARNING: The returned byte slice is READ-ONLY. -// Attempting to alter the slice contents will yield a runtime panic. -func %s() []byte { - var empty [0]byte - sx := (*reflect.StringHeader)(unsafe.Pointer(&_%s)) - b := empty[:] - bx := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - bx.Data = sx.Data - bx.Len = len(_%s) - bx.Cap = bx.Len - return b -} -`, funcname, funcname, funcname, funcname) -}