Increase length of magic id (decs. chance of accidental collision)

This commit is contained in:
Peter Taoussanis 2013-06-13 18:42:49 +07:00
parent 1d482b8893
commit 3e830a4b3a
3 changed files with 17 additions and 12 deletions

View file

@ -2,7 +2,7 @@ Current [semantic](http://semver.org/) version:
```clojure
[com.taoensso/nippy "1.2.1"] ; Stable
[com.taoensso/nippy "2.0.0-alpha2"] ; Development (see notes below)
[com.taoensso/nippy "2.0.0-alpha3"] ; Development (see notes below)
```
2.x adds pluggable compression, crypto support (also pluggable), an improved API (including much better error messages), and hugely improved performance. It **is backwards compatible**, but please note that the `freeze-to-bytes`/`thaw-from-bytes` API has been **deprecated** in favor of `freeze`/`thaw`.

View file

@ -1,4 +1,4 @@
(defproject com.taoensso/nippy "2.0.0-alpha2"
(defproject com.taoensso/nippy "2.0.0-alpha3"
:description "Clojure serialization library"
:url "https://github.com/ptaoussanis/nippy"
:license {:name "Eclipse Public License"

View file

@ -16,13 +16,14 @@
;; TODO Provide ToFreeze, Frozen, Encrypted, etc. tooling helpers
;;;; Header IDs
;; Nippy 2.x+ prefixes frozen data with a 5-byte header:
(def ^:const id-nippy-magic-prefix (byte 17))
(def ^:const id-nippy-header-ver (byte 0))
;; Nippy 2.x+ prefixes frozen data with a header:
(def ^:const header-len 6)
(def ^:const id-nippy-magic-id1 (byte 17))
(def ^:const id-nippy-magic-id2 (byte -42))
(def ^:const id-nippy-header-ver (byte 0))
;; * Compressor id (0 if no compressor)
;; * Encryptor id (0 if no encryptor)
(def ^:const id-nippy-reserved (byte 0))
(def ^:const id-nippy-reserved (byte 0))
;;;; Data type IDs
@ -173,7 +174,8 @@
(defn- wrap-nippy-header [data-ba compressor encryptor password]
(let [header-ba (byte-array
[id-nippy-magic-prefix
[id-nippy-magic-id1
id-nippy-magic-id2
id-nippy-header-ver
(byte (if compressor (compression/header-id compressor) 0))
(byte (if password (encryption/header-id encryptor) 0))
@ -289,8 +291,11 @@
maybe-headers
(fn []
(when-let [[[id-magic* & _ :as headers] data-ba] (utils/ba-split ba 5)]
(when (= id-magic* id-nippy-magic-prefix) ; Not a guarantee of correctness!
(when-let [[[id-mag1* id-mag2* & _ :as headers] data-ba]
(utils/ba-split ba header-len)]
(when (and (= id-mag1* id-nippy-magic-id1)
(= id-mag2* id-nippy-magic-id2))
;; Not a guarantee of correctness!
[headers data-ba])))
legacy-thaw
@ -312,7 +317,7 @@
(if (= legacy-mode true)
(legacy-thaw ba) ; Read as legacy, and only as legacy
(if-let [[[_ id-header* id-comp* id-enc* _] data-ba] (maybe-headers)]
(if-let [[[_ _ id-hver* id-comp* id-enc* _] data-ba] (maybe-headers)]
(let [compressed? (not (zero? id-comp*))
encrypted? (not (zero? id-enc*))]
@ -322,7 +327,7 @@
(catch Exception _ (legacy-thaw ba)))
(cond ; Read as modern, and only as modern
(> id-header* id-nippy-header-ver)
(> id-hver* id-nippy-header-ver)
(ex "Data frozen with newer Nippy version. Please upgrade.")
(and strict? (not encrypted?) password)