github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/mobile/internal/binres/package.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package binres 6 7 type TableHeader struct { 8 chunkHeader 9 packageCount uint32 10 } 11 12 func (hdr *TableHeader) UnmarshalBinary(bin []byte) error { 13 if err := (&hdr.chunkHeader).UnmarshalBinary(bin); err != nil { 14 return err 15 } 16 hdr.packageCount = btou32(bin[8:]) 17 return nil 18 } 19 20 func (hdr TableHeader) MarshalBinary() ([]byte, error) { 21 bin := make([]byte, 12) 22 b, err := hdr.chunkHeader.MarshalBinary() 23 if err != nil { 24 return nil, err 25 } 26 copy(bin, b) 27 putu32(b[8:], hdr.packageCount) 28 return bin, nil 29 } 30 31 // TODO next up: package chunk 32 // https://justanapplication.wordpress.com/2011/09/16/ 33 type Table struct { 34 TableHeader 35 pool *Pool 36 pkg *Package 37 } 38 39 func (tbl *Table) UnmarshalBinary(bin []byte) error { 40 buf := bin 41 if err := (&tbl.TableHeader).UnmarshalBinary(buf); err != nil { 42 return err 43 } 44 buf = buf[tbl.headerByteSize:] 45 tbl.pool = new(Pool) 46 if err := tbl.pool.UnmarshalBinary(buf); err != nil { 47 return err 48 } 49 buf = buf[tbl.pool.size():] 50 tbl.pkg = new(Package) 51 if err := tbl.pkg.UnmarshalBinary(buf); err != nil { 52 return err 53 } 54 return nil 55 } 56 57 type Package struct { 58 chunkHeader 59 60 // If this is a base package, its ID. Package IDs start 61 // at 1 (corresponding to the value of the package bits in a 62 // resource identifier). 0 means this is not a base package. 63 id uint32 64 65 // name of package, zero terminated 66 name [128]uint16 67 68 // Offset to a ResStringPool_header defining the resource 69 // type symbol table. If zero, this package is inheriting from 70 // another base package (overriding specific values in it). 71 typeStrings uint32 72 73 // Last index into typeStrings that is for public use by others. 74 lastPublicType uint32 75 76 // Offset to a ResStringPool_header defining the resource 77 // key symbol table. If zero, this package is inheriting from 78 // another base package (overriding specific values in it). 79 keyStrings uint32 80 81 // Last index into keyStrings that is for public use by others. 82 lastPublicKey uint32 83 84 typeIdOffset uint32 85 } 86 87 func (pkg *Package) UnmarshalBinary(bin []byte) error { 88 if err := (&pkg.chunkHeader).UnmarshalBinary(bin); err != nil { 89 return err 90 } 91 pkg.id = btou32(bin[8:]) 92 for i := range pkg.name { 93 pkg.name[i] = btou16(bin[12+i*2:]) 94 } 95 pkg.typeStrings = btou32(bin[140:]) 96 pkg.lastPublicType = btou32(bin[144:]) 97 pkg.keyStrings = btou32(bin[148:]) 98 pkg.lastPublicKey = btou32(bin[152:]) 99 pkg.typeIdOffset = btou32(bin[156:]) 100 return nil 101 }