Skip to content

Commit

Permalink
ctb: Encryption seed must be non-zero for the printer to accept a file
Browse files Browse the repository at this point in the history
Summary:
- Fixes #41
- Force encryption seed to be non-zero
- Allow specifying ecryption seed on the command line
- Initialize the 'rand' package in the command's init
- Use the correct image type code (0x07, not 0x0f)
  • Loading branch information
ezrec committed May 17, 2020
1 parent 01f4c82 commit d13b431
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ The command line tool is designed to be used in a 'pipeline' style, for example:

Options for '.ctb':

-e, --encryption-seed uint32 Specify a specific encryption seed

Options for '.photon':

Expand Down
6 changes: 6 additions & 0 deletions cmd/uv3dp/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
package main

import (
"math/rand"
"time"

"github.com/ezrec/uv3dp"
)

func init() {
// Initialize the rand package
rand.Seed(time.Now().UnixNano())

newEmptyFormatter := func(suffix string) uv3dp.Formatter { return NewEmptyFormatter() }

uv3dp.RegisterFormatter("empty", newEmptyFormatter)
Expand Down
33 changes: 23 additions & 10 deletions ctb/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"image"
"io/ioutil"
"math"
"math/rand"
"sort"
"time"

Expand Down Expand Up @@ -120,7 +121,7 @@ type ctbLayerDef struct {
_ [4]uint32 // 14:
}

type CbdDlp struct {
type Ctb struct {
properties uv3dp.Properties
layerDef []ctbLayerDef

Expand All @@ -142,6 +143,8 @@ func durationToFloat32(time_ns time.Duration) float32 {

type CtbFormatter struct {
*pflag.FlagSet

EncryptionSeed uint32
}

func NewCtbFormatter(suffix string) (cf *CtbFormatter) {
Expand All @@ -152,10 +155,12 @@ func NewCtbFormatter(suffix string) (cf *CtbFormatter) {
FlagSet: flagSet,
}

cf.Uint32VarP(&cf.EncryptionSeed, "encryption-seed", "e", 0, "Specify a specific encryption seed")

return
}

// Save a uv3dp.Printable in CBD DLP format
// Save a uv3dp.Printable in CTB format
func (cf *CtbFormatter) Encode(writer uv3dp.Writer, p uv3dp.Printable) (err error) {
properties := p.Properties()

Expand All @@ -170,11 +175,18 @@ func (cf *CtbFormatter) Encode(writer uv3dp.Writer, p uv3dp.Printable) (err erro
}
rleHash := map[uint64]rleInfo{}

// Select an encryption seed
// A zero encryption seed is rejected by the printer, so check for that
seed := cf.EncryptionSeed
for seed == 0 {
seed = rand.Uint32()
}

headerBase := uint32(0)
header := ctbHeader{
Magic: defaultHeaderMagic,
Version: 2,
EncryptionSeed: 0, // Force encryption off, so we can de-duplicate layers
EncryptionSeed: seed,
}
headerSize, _ := restruct.SizeOf(&header)

Expand Down Expand Up @@ -340,17 +352,18 @@ func (cf *CtbFormatter) Encode(writer uv3dp.Writer, p uv3dp.Printable) (err erro
if param.RetractSpeed < 0 {
param.RetractSpeed = defaultRetractSpeed
}
param.Unknown38 = 0x1234

// ctbSlicer
param.Unknown38 = 0x1234
slicer.MachineOffset = machineBase
slicer.MachineSize = uint32(machineSize)
slicer.EncryptionMode = 0xf // Magic!
slicer.EncryptionMode = 7 // Magic!
slicer.TimeSeconds = 0x12345678
slicer.ChiTuBoxVersion[0] = 1 // Magic!
slicer.ChiTuBoxVersion[1] = 6
slicer.ChiTuBoxVersion[2] = 3
slicer.Unknown34 = 0x200 // Magic?
slicer.Unknown2C = 1 // Magic?
slicer.Unknown34 = 0 // Magic?

// Compute total cubic millimeters (== milliliters) of all the on pixels
bedArea := float64(header.BedSizeMM[0] * header.BedSizeMM[1])
Expand Down Expand Up @@ -555,7 +568,7 @@ func (cf *CtbFormatter) Decode(file uv3dp.Reader, filesize int64) (printable uv3
exp.RetractHeight = defaultRetractHeight
}

cbd := &CbdDlp{
cbd := &Ctb{
properties: prop,
layerDef: layerDef,
rleMap: rleMap,
Expand All @@ -566,15 +579,15 @@ func (cf *CtbFormatter) Decode(file uv3dp.Reader, filesize int64) (printable uv3
return
}

// Properties get the properties of the CbdDlp Printable
func (cbd *CbdDlp) Properties() (prop uv3dp.Properties) {
// Properties get the properties of the Ctb Printable
func (cbd *Ctb) Properties() (prop uv3dp.Properties) {
prop = cbd.properties

return
}

// Layer gets a layer - we decode from the RLE on-the fly
func (cbd *CbdDlp) Layer(index int) (layer uv3dp.Layer) {
func (cbd *Ctb) Layer(index int) (layer uv3dp.Layer) {
if index < 0 || index >= len(cbd.layerDef) {
return
}
Expand Down
4 changes: 2 additions & 2 deletions ctb/format_empty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ var (
},
})

emptyRaw = []byte{
0x86, 0x0, 0xfd, 0x12, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa0, 0x41, 0x0, 0x0, 0x20, 0x42, 0x0, 0x0, 0x1b, 0x43, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xcd, 0xcc, 0x4c, 0x3e, 0xcd, 0xcc, 0x4c, 0x3d, 0x0, 0x0, 0x84, 0x41, 0x0, 0x0, 0x84, 0x41, 0x0, 0x0, 0x10, 0x40, 0x2, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x14, 0x0, 0x0, 0x0, 0x70, 0x0, 0x0, 0x0, 0x47, 0x1, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x94, 0x0, 0x0, 0x0, 0x4b, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xb8, 0x0, 0x0, 0x0, 0x3c, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xff, 0x0, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf4, 0x0, 0x0, 0x0, 0x4c, 0x0, 0x0, 0x0, 0x14, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x90, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0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 0x0, 0x0, 0x7e, 0x35, 0x46, 0x0, 0xfd, 0xa, 0xf9, 0x0, 0x7c, 0xde, 0x1c}
)

type bufferMap struct {
Expand Down Expand Up @@ -100,6 +99,7 @@ func TestEmptyToRaw(t *testing.T) {

emptyRaw := item.Raw
if !bytes.Equal(encoded_buff, emptyRaw) {
t.Logf("%+#v\n", encoded_buff)
t.Errorf("%v: expected [%d byte encoding], got [%d byte encoding]", item.Format, len(emptyRaw), len(encoded_buff))
if len(encoded_buff) == len(emptyRaw) {
for i := 0; i < len(emptyRaw)-3; i += 4 {
Expand Down

0 comments on commit d13b431

Please sign in to comment.