//
// This file was generated via github.com/skx/implant/
//
// Local edits will be lost.
//
package staticfiles

import (
	"bytes"
	"compress/gzip"
	"encoding/base64"
	"fmt"
	"io/ioutil"
)

//
// EmbeddedResource is the structure which is used to record details of
// each embedded resource in your binary.
//
// The resource contains the (original) filename, relative to the input
// directory `implant` was generated with, along with the original size
// and the compressed/encoded data.
//
type EmbeddedResource struct {
	Filename string
	Contents string
	Length   int
}

//
// RESOURCES is a map containing all embedded resources. The map key is the
// file name.
//
// It is exposed to callers via the `getResources()` function.
//
var RESOURCES = map[string]EmbeddedResource{

	"files/tftp/arm64.efi": {
		Filename: "files/tftp/arm64.efi",
		Contents: "[encoded data, 139840 characters]",
		Length:   184960,
	},

	"files/tftp/i386.efi": {
		Filename: "files/tftp/i386.efi",
		Contents: "[encoded data, 128896 characters]",
		Length:   151744,
	},

	"files/tftp/i386.kpxe": {
		Filename: "files/tftp/i386.kpxe",
		Contents: "[encoded data, 89476 characters]",
		Length:   67443,
	},

	"files/tftp/x86.efi": {
		Filename: "files/tftp/x86.efi",
		Contents: "[encoded data, 142928 characters]",
		Length:   183232,
	},
}

//
// Return the contents of a resource.
//
func getResource(path string) ([]byte, error) {
	if entry, ok := RESOURCES[path]; ok {
		var raw bytes.Buffer
		var err error

		// Decode the data.
		in, err := base64.StdEncoding.DecodeString(entry.Contents)
		if err != nil {
			return nil, err
		}

		// Gunzip the data to the client
		gr, err := gzip.NewReader(bytes.NewBuffer(in))
		if err != nil {
			return nil, err
		}
		defer gr.Close()
		data, err := ioutil.ReadAll(gr)
		if err != nil {
			return nil, err
		}
		_, err = raw.Write(data)
		if err != nil {
			return nil, err
		}

		// Return it.
		return raw.Bytes(), nil
	}
	return nil, fmt.Errorf("failed to find resource '%s'", path)
}

//
// Return the available resources in a slice.
//
func getResources() []EmbeddedResource {
	i := 0
	ret := make([]EmbeddedResource, len(RESOURCES))
	for _, v := range RESOURCES {
		ret[i] = v
		i++
	}
	return ret
}
