From 5b0d6f6f3eb395154b332ad57cb630d04feb569a Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 19 Oct 2017 18:09:14 +1100 Subject: [PATCH] Add a skeleton of an Adler checksummer. --- gen/c/std/flate.c | 41 +++++++++++++++++++++++++++++ gen/h/std/flate.h | 18 +++++++++++++ lang/check/bounds.go | 52 ++++++++++++++++++++++++++++++------- std/flate/decode_zlib.puffs | 6 +++++ 4 files changed, 108 insertions(+), 9 deletions(-) diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 8be87d2a..93f2c945 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -234,6 +234,23 @@ typedef struct { } private_impl; } puffs_flate_decoder; +typedef struct { + // Do not access the private_impl's fields directly. There is no API/ABI + // compatibility or safety guarantee if you do so. Instead, use the + // puffs_flate_adler_etc functions. + // + // In C++, these fields would be "private", but C does not support that. + // + // It is a struct, not a struct*, so that it can be stack allocated. + struct { + puffs_flate_status status; + uint32_t magic; + + uint32_t f_state; + + } private_impl; +} puffs_flate_adler; + typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. Instead, use the @@ -247,6 +264,7 @@ typedef struct { uint32_t magic; puffs_flate_decoder f_dec; + puffs_flate_adler f_adler; struct { uint32_t coro_susp_point; @@ -647,6 +665,10 @@ static const uint32_t puffs_flate_dcode_magic_numbers[32] = { // ---------------- Private Initializer Prototypes +void puffs_flate_adler_initialize(puffs_flate_adler* self, + uint32_t puffs_version, + uint32_t for_internal_use_only); + // ---------------- Private Function Prototypes puffs_flate_status puffs_flate_decoder_decode_blocks(puffs_flate_decoder* self, @@ -707,6 +729,23 @@ void puffs_flate_decoder_initialize(puffs_flate_decoder* self, self->private_impl.magic = PUFFS_MAGIC; } +void puffs_flate_adler_initialize(puffs_flate_adler* self, + uint32_t puffs_version, + uint32_t for_internal_use_only) { + if (!self) { + return; + } + if (puffs_version != PUFFS_VERSION) { + self->private_impl.status = PUFFS_FLATE_ERROR_BAD_PUFFS_VERSION; + return; + } + if (for_internal_use_only != PUFFS_ALREADY_ZEROED) { + memset(self, 0, sizeof(*self)); + } + self->private_impl.magic = PUFFS_MAGIC; + self->private_impl.f_state = 1; +} + void puffs_flate_zlib_decoder_initialize(puffs_flate_zlib_decoder* self, uint32_t puffs_version, uint32_t for_internal_use_only) { @@ -723,6 +762,8 @@ void puffs_flate_zlib_decoder_initialize(puffs_flate_zlib_decoder* self, self->private_impl.magic = PUFFS_MAGIC; puffs_flate_decoder_initialize(&self->private_impl.f_dec, PUFFS_VERSION, PUFFS_ALREADY_ZEROED); + puffs_flate_adler_initialize(&self->private_impl.f_adler, PUFFS_VERSION, + PUFFS_ALREADY_ZEROED); } // ---------------- Function Implementations diff --git a/gen/h/std/flate.h b/gen/h/std/flate.h index 9b6448e8..d99caad6 100644 --- a/gen/h/std/flate.h +++ b/gen/h/std/flate.h @@ -234,6 +234,23 @@ typedef struct { } private_impl; } puffs_flate_decoder; +typedef struct { + // Do not access the private_impl's fields directly. There is no API/ABI + // compatibility or safety guarantee if you do so. Instead, use the + // puffs_flate_adler_etc functions. + // + // In C++, these fields would be "private", but C does not support that. + // + // It is a struct, not a struct*, so that it can be stack allocated. + struct { + puffs_flate_status status; + uint32_t magic; + + uint32_t f_state; + + } private_impl; +} puffs_flate_adler; + typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. Instead, use the @@ -247,6 +264,7 @@ typedef struct { uint32_t magic; puffs_flate_decoder f_dec; + puffs_flate_adler f_adler; struct { uint32_t coro_susp_point; diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 9cc3dda5..0576ff15 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -165,19 +165,48 @@ func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) { return o, nil } +func typeBounds(tm *t.Map, typ *a.TypeExpr) (*big.Int, *big.Int, error) { + b := [2]*big.Int{} + if key := typ.Name().Key(); typ.Decorator() == 0 && key < t.Key(len(numTypeBounds)) { + b = numTypeBounds[key] + } + if b[0] == nil || b[1] == nil { + return nil, nil, nil + } + if o := typ.Min(); o != nil { + cv := o.ConstValue() + if cv.Cmp(b[0]) < 0 { + return nil, nil, fmt.Errorf("check: type refinement %v for %q is out of bounds", cv, typ.String(tm)) + } + b[0] = cv + } + if o := typ.Max(); o != nil { + cv := o.ConstValue() + if cv.Cmp(b[1]) > 0 { + return nil, nil, fmt.Errorf("check: type refinement %v for %q is out of bounds", cv, typ.String(tm)) + } + b[1] = cv + } + return b[0], b[1], nil +} + func bcheckField(tm *t.Map, n *a.Field) error { - // TODO: check if x is a numeric type? What if n.XType() is "ptr T"? - x := n.XType().Innermost() - dv, nMin, nMax := zero, zero, zero + innTyp := n.XType().Innermost() + nMin, nMax, err := typeBounds(tm, innTyp) + if err != nil { + return err + } + if nMin == nil { + if n.DefaultValue() != nil { + return fmt.Errorf("check: explicit default value %v for field %q of non-numeric innermost type %q", + n.DefaultValue().ConstValue(), n.Name().String(tm), innTyp.String(tm)) + } + return nil + } + dv := zero if o := n.DefaultValue(); o != nil { dv = o.ConstValue() } - if o := x.Min(); o != nil { - nMin = o.ConstValue() - } - if o := x.Max(); o != nil { - nMax = o.ConstValue() - } if dv.Cmp(nMin) < 0 || dv.Cmp(nMax) > 0 { return fmt.Errorf("check: default value %v is not within bounds [%v..%v] for field %q", dv, nMin, nMax, n.Name().String(tm)) @@ -237,6 +266,11 @@ func (q *checker) bcheckStatement(n *a.Node) error { case a.KVar: n := n.Var() + if innTyp := n.XType().Innermost(); innTyp.IsRefined() { + if _, _, err := typeBounds(q.tm, innTyp); err != nil { + return err + } + } lhs := a.NewExpr(a.FlagsTypeChecked, 0, n.Name(), nil, nil, nil, nil) lhs.SetMType(n.XType()) rhs := n.Value() diff --git a/std/flate/decode_zlib.puffs b/std/flate/decode_zlib.puffs index ab97b9ee..8d38a870 100644 --- a/std/flate/decode_zlib.puffs +++ b/std/flate/decode_zlib.puffs @@ -20,6 +20,7 @@ pub error "TODO: unsupported zlib preset dictionary" pub struct zlib_decoder?( dec decoder, + adler adler, ) pub func zlib_decoder.decode?(dst writer1, src reader1)() { @@ -40,3 +41,8 @@ pub func zlib_decoder.decode?(dst writer1, src reader1)() { var checksum u32 = in.src.read_u32be?() // TODO: verify the checksum. } + +// TODO: drop the '?' but still generate puffs_flate_adler_initialize? +pri struct adler?( + state u32 = 1, +)