1d6e70f105a9299a781acf551a6cfe85e78f52fe
[platform/upstream/gcc.git] / libgo / go / archive / zip / struct.go
1 // Copyright 2010 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 /*
6 Package zip provides support for reading and writing ZIP archives.
7
8 See: http://www.pkware.com/documents/casestudies/APPNOTE.TXT
9
10 This package does not support ZIP64 or disk spanning.
11 */
12 package zip
13
14 import "os"
15 import "time"
16
17 // Compression methods.
18 const (
19         Store   uint16 = 0
20         Deflate uint16 = 8
21 )
22
23 const (
24         fileHeaderSignature      = 0x04034b50
25         directoryHeaderSignature = 0x02014b50
26         directoryEndSignature    = 0x06054b50
27         fileHeaderLen            = 30 // + filename + extra
28         directoryHeaderLen       = 46 // + filename + extra + comment
29         directoryEndLen          = 22 // + comment
30         dataDescriptorLen        = 12
31 )
32
33 type FileHeader struct {
34         Name             string
35         CreatorVersion   uint16
36         ReaderVersion    uint16
37         Flags            uint16
38         Method           uint16
39         ModifiedTime     uint16 // MS-DOS time
40         ModifiedDate     uint16 // MS-DOS date
41         CRC32            uint32
42         CompressedSize   uint32
43         UncompressedSize uint32
44         Extra            []byte
45         Comment          string
46 }
47
48 type directoryEnd struct {
49         diskNbr            uint16 // unused
50         dirDiskNbr         uint16 // unused
51         dirRecordsThisDisk uint16 // unused
52         directoryRecords   uint16
53         directorySize      uint32
54         directoryOffset    uint32 // relative to file
55         commentLen         uint16
56         comment            string
57 }
58
59 func recoverError(err *os.Error) {
60         if e := recover(); e != nil {
61                 if osErr, ok := e.(os.Error); ok {
62                         *err = osErr
63                         return
64                 }
65                 panic(e)
66         }
67 }
68
69 // msDosTimeToTime converts an MS-DOS date and time into a time.Time.
70 // The resolution is 2s.
71 // See: http://msdn.microsoft.com/en-us/library/ms724247(v=VS.85).aspx
72 func msDosTimeToTime(dosDate, dosTime uint16) time.Time {
73         return time.Time{
74                 // date bits 0-4: day of month; 5-8: month; 9-15: years since 1980
75                 Year:  int64(dosDate>>9 + 1980),
76                 Month: int(dosDate >> 5 & 0xf),
77                 Day:   int(dosDate & 0x1f),
78
79                 // time bits 0-4: second/2; 5-10: minute; 11-15: hour
80                 Hour:   int(dosTime >> 11),
81                 Minute: int(dosTime >> 5 & 0x3f),
82                 Second: int(dosTime & 0x1f * 2),
83         }
84 }
85
86 // Mtime_ns returns the modified time in ns since epoch.
87 // The resolution is 2s.
88 func (h *FileHeader) Mtime_ns() int64 {
89         t := msDosTimeToTime(h.ModifiedDate, h.ModifiedTime)
90         return t.Seconds() * 1e9
91 }