2016-02-06 19:20:54 +01:00
|
|
|
package contentenc
|
2015-11-01 01:32:33 +01:00
|
|
|
|
|
|
|
// Per-file header
|
|
|
|
//
|
|
|
|
// Format: [ "Version" uint16 big endian ] [ "Id" 16 random bytes ]
|
|
|
|
|
|
|
|
import (
|
2017-05-25 14:20:17 +02:00
|
|
|
"bytes"
|
2015-11-01 01:32:33 +01:00
|
|
|
"encoding/binary"
|
2019-02-17 17:13:20 +01:00
|
|
|
"encoding/hex"
|
2018-04-02 18:32:30 +02:00
|
|
|
"fmt"
|
2016-12-10 11:50:16 +01:00
|
|
|
"log"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/cryptocore"
|
2015-11-01 01:32:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-10-02 06:14:18 +02:00
|
|
|
// CurrentVersion is the current On-Disk-Format version
|
2016-02-06 19:20:54 +01:00
|
|
|
CurrentVersion = 2
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
headerVersionLen = 2 // uint16
|
|
|
|
headerIDLen = 16 // 128 bit random file id
|
|
|
|
// HeaderLen is the total header length
|
|
|
|
HeaderLen = headerVersionLen + headerIDLen
|
2015-11-01 01:32:33 +01:00
|
|
|
)
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// FileHeader represents the header stored on each non-empty file.
|
2015-11-01 01:32:33 +01:00
|
|
|
type FileHeader struct {
|
|
|
|
Version uint16
|
2016-10-02 06:14:18 +02:00
|
|
|
ID []byte
|
2015-11-01 01:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pack - serialize fileHeader object
|
|
|
|
func (h *FileHeader) Pack() []byte {
|
2016-10-02 06:14:18 +02:00
|
|
|
if len(h.ID) != headerIDLen || h.Version != CurrentVersion {
|
2016-12-10 11:50:16 +01:00
|
|
|
log.Panic("FileHeader object not properly initialized")
|
2015-11-01 01:32:33 +01:00
|
|
|
}
|
2016-10-02 06:14:18 +02:00
|
|
|
buf := make([]byte, HeaderLen)
|
|
|
|
binary.BigEndian.PutUint16(buf[0:headerVersionLen], h.Version)
|
|
|
|
copy(buf[headerVersionLen:], h.ID)
|
2015-11-01 01:32:33 +01:00
|
|
|
return buf
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-25 14:20:17 +02:00
|
|
|
// allZeroFileID is preallocated to quickly check if the data read from disk is all zero
|
|
|
|
var allZeroFileID = make([]byte, headerIDLen)
|
2019-10-06 19:34:32 +02:00
|
|
|
var allZeroHeader = make([]byte, HeaderLen)
|
2017-05-25 14:20:17 +02:00
|
|
|
|
2015-11-01 01:32:33 +01:00
|
|
|
// ParseHeader - parse "buf" into fileHeader object
|
|
|
|
func ParseHeader(buf []byte) (*FileHeader, error) {
|
2016-10-02 06:14:18 +02:00
|
|
|
if len(buf) != HeaderLen {
|
2018-04-02 18:32:30 +02:00
|
|
|
return nil, fmt.Errorf("ParseHeader: invalid length, want=%d have=%d", HeaderLen, len(buf))
|
2015-11-01 01:32:33 +01:00
|
|
|
}
|
2019-10-06 19:34:32 +02:00
|
|
|
if bytes.Equal(buf, allZeroHeader) {
|
|
|
|
return nil, fmt.Errorf("ParseHeader: header is all-zero. Header hexdump: %s", hex.EncodeToString(buf))
|
|
|
|
}
|
2015-11-01 01:32:33 +01:00
|
|
|
var h FileHeader
|
2016-10-02 06:14:18 +02:00
|
|
|
h.Version = binary.BigEndian.Uint16(buf[0:headerVersionLen])
|
2016-02-06 19:20:54 +01:00
|
|
|
if h.Version != CurrentVersion {
|
2019-10-06 19:34:32 +02:00
|
|
|
return nil, fmt.Errorf("ParseHeader: invalid version, want=%d have=%d. Header hexdump: %s",
|
2019-02-17 17:13:20 +01:00
|
|
|
CurrentVersion, h.Version, hex.EncodeToString(buf))
|
2015-11-01 01:32:33 +01:00
|
|
|
}
|
2016-10-02 06:14:18 +02:00
|
|
|
h.ID = buf[headerVersionLen:]
|
2017-05-25 14:20:17 +02:00
|
|
|
if bytes.Equal(h.ID, allZeroFileID) {
|
2019-10-06 19:34:32 +02:00
|
|
|
return nil, fmt.Errorf("ParseHeader: file id is all-zero. Header hexdump: %s",
|
2019-02-17 17:13:20 +01:00
|
|
|
hex.EncodeToString(buf))
|
2017-05-25 14:20:17 +02:00
|
|
|
}
|
2015-11-01 01:32:33 +01:00
|
|
|
return &h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RandomHeader - create new fileHeader object with random Id
|
|
|
|
func RandomHeader() *FileHeader {
|
|
|
|
var h FileHeader
|
2016-02-06 19:20:54 +01:00
|
|
|
h.Version = CurrentVersion
|
2016-10-02 06:14:18 +02:00
|
|
|
h.ID = cryptocore.RandBytes(headerIDLen)
|
2015-11-01 01:32:33 +01:00
|
|
|
return &h
|
|
|
|
}
|