Parse dependencies

andybalholm
Mar 29, 2023, 12:16 AM
4JWXOJIPJAOL6FZMQENB2ZUTWEFXNB7VU2NHYDNT4T45ESSCSUPQC

Dependencies

  • [2] 7VEHGTEY Switch from reading text format to reading binary format.
  • [*] EKAB33DH Start using some parser combinators
  • [*] PHRWK7NK Parse change headers

Change contents

  • edit in combinators.go at line 268
    [2.6658]
    [2.6658]
    }
    func uint32LE(input []byte) ([]byte, uint32, error) {
    if len(input) < 4 {
    return input, 0, fmt.Errorf("need 4 bytes to parse a 32-bit integer; only got %d", len(input))
    }
    return input[4:], binary.LittleEndian.Uint32(input), nil
  • edit in combinators.go at line 361
    [2.8704]
    [2.8704]
    }
    }
    func take(n int) parser[[]byte] {
    return func(data []byte) ([]byte, []byte, error) {
    if n > len(data) {
    return data, nil, io.ErrUnexpectedEOF
    }
    return data[n:], data[:n], nil
  • edit in change_test.go at line 22
    [5.701]
    [5.1094]
    Dependencies: []Hash{Hash{0xe7, 0x82, 0xb1, 0xd7, 0xe4, 0x17, 0x64, 0xe4, 0xfe, 0x45, 0x2d, 0x6f, 0x24, 0x22, 0x40, 0x26, 0x16, 0x12, 0xb7, 0xf, 0x42, 0x70, 0xd9, 0xac, 0xd8, 0x4e, 0x5a, 0x82, 0xea, 0x85, 0xab, 0x57}},
    ExtraKnown: []Hash{},
    Metadata: []byte{},
  • edit in change_test.go at line 34
    [2.9330]
    [5.1402]
    }
    }
    func TestHashBase32(t *testing.T) {
    hs := "CB7A3PP3XC6JY3QYUUNB4WCXGNFJMHTTH54MBAWBUVQL7TTHWBBQC"
    hash, err := HashFromBase32(hs)
    if err != nil {
    t.Fatalf("error parsing hash: %v", err)
  • edit in change_test.go at line 43
    [5.1405]
    [5.1405]
    if hash.String() != hs {
    t.Fatalf("got %s, want %s", hash.String(), hs)
    }
  • edit in change.go at line 5
    [2.774]
    [2.774]
    "encoding/base32"
  • replacement in change.go at line 15
    [2.877][2.877:993]()
    Version uint64
    Message string
    Description string
    Timestamp time.Time
    Authors []map[string]string
    [2.877]
    [2.993]
    Version uint64
    Message string
    Description string
    Timestamp time.Time
    Authors []map[string]string
    Dependencies []Hash
    ExtraKnown []Hash
    Metadata []byte
    }
    type Hash [32]byte
    var base32Encoding = base32.StdEncoding.WithPadding(base32.NoPadding)
    func (h Hash) String() string {
    return base32Encoding.EncodeToString(append(h[:], 1))
  • edit in change.go at line 33
    [2.996]
    [2.996]
    func HashFromBase32(b32 string) (Hash, error) {
    b, err := base32Encoding.DecodeString(b32)
    if err != nil {
    return Hash{}, err
    }
    if len(b) != 33 {
    return Hash{}, fmt.Errorf("expected 33 bytes, got %d", len(b))
    }
    if b[32] != 1 {
    return Hash{}, fmt.Errorf("expected Blake3 hash, got type %d", b[32])
    }
    return Hash(b[:32]), nil
    }
  • replacement in change.go at line 65
    [2.1427][2.1427:1458]()
    zr, err := zstd.NewReader(br)
    [2.1427]
    [2.1458]
    zr, err := zstd.NewReader(bytes.NewReader(data[binary.Size(offsets{}):off.UnhashedOffset]))
  • edit in change.go at line 94
    [2.2118]
    [2.2118]
  • edit in change.go at line 99
    [2.2212]
    [2.2212]
  • edit in change.go at line 107
    [2.2376]
    [2.2376]
  • edit in change.go at line 114
    [2.2601]
    [2.2601]
  • edit in change.go at line 116
    [2.2708]
    [2.2708]
    if err != nil {
    return err
    }
    data, c.Dependencies, err = vec(deserializeHash)(data)
  • replacement in change.go at line 125
    [2.2742][2.2742:2750]()
    //TODO
    [2.2742]
    [2.2750]
    data, c.ExtraKnown, err = vec(deserializeHash)(data)
    if err != nil {
    return err
    }
    data, c.Metadata, err = lengthData(uint64LE)(data)
    if err != nil {
    return err
    }
  • edit in change.go at line 138
    [2.2766]
    [2.2766]
    func deserializeHash(data []byte) ([]byte, Hash, error) {
    data, tag, err := uint32LE(data)
    if err != nil {
    return data, Hash{}, err
    }
    if tag == 0 {
    return data, Hash{}, nil
    }
    if tag != 1 {
    return data, Hash{}, fmt.Errorf("bad tag for Hash, expected 0 or 1, got %d", tag)
    }
    data, b, err := take(32)(data)
    if err != nil {
    return data, Hash{}, err
    }
    return data, Hash(b), nil
    }