Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package jar |
| 16 | |
| 17 | import ( |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 18 | "bytes" |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 19 | "fmt" |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 20 | "io" |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 21 | "os" |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 22 | "strings" |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 23 | "text/scanner" |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 24 | "time" |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 25 | "unicode" |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 26 | |
| 27 | "android/soong/third_party/zip" |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
Colin Cross | 3454031 | 2017-09-06 12:52:37 -0700 | [diff] [blame] | 30 | const ( |
| 31 | MetaDir = "META-INF/" |
| 32 | ManifestFile = MetaDir + "MANIFEST.MF" |
| 33 | ModuleInfoClass = "module-info.class" |
| 34 | ) |
| 35 | |
Colin Cross | bddcf13 | 2017-10-04 17:02:23 -0700 | [diff] [blame] | 36 | var DefaultTime = time.Date(2008, 1, 1, 0, 0, 0, 0, time.UTC) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 37 | |
| 38 | var MetaDirExtra = [2]byte{0xca, 0xfe} |
| 39 | |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 40 | // EntryNamesLess tells whether <filepathA> should precede <filepathB> in |
| 41 | // the order of files with a .jar |
| 42 | func EntryNamesLess(filepathA string, filepathB string) (less bool) { |
| 43 | diff := index(filepathA) - index(filepathB) |
| 44 | if diff == 0 { |
| 45 | return filepathA < filepathB |
| 46 | } |
| 47 | return diff < 0 |
| 48 | } |
| 49 | |
| 50 | // Treats trailing * as a prefix match |
| 51 | func patternMatch(pattern, name string) bool { |
| 52 | if strings.HasSuffix(pattern, "*") { |
| 53 | return strings.HasPrefix(name, strings.TrimSuffix(pattern, "*")) |
| 54 | } else { |
| 55 | return name == pattern |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | var jarOrder = []string{ |
Colin Cross | 3454031 | 2017-09-06 12:52:37 -0700 | [diff] [blame] | 60 | MetaDir, |
| 61 | ManifestFile, |
| 62 | MetaDir + "*", |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 63 | "*", |
| 64 | } |
| 65 | |
| 66 | func index(name string) int { |
| 67 | for i, pattern := range jarOrder { |
| 68 | if patternMatch(pattern, name) { |
| 69 | return i |
| 70 | } |
| 71 | } |
| 72 | panic(fmt.Errorf("file %q did not match any pattern", name)) |
| 73 | } |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 74 | |
| 75 | func MetaDirFileHeader() *zip.FileHeader { |
| 76 | dirHeader := &zip.FileHeader{ |
| 77 | Name: MetaDir, |
| 78 | Extra: []byte{MetaDirExtra[1], MetaDirExtra[0], 0, 0}, |
| 79 | } |
Chris Gross | fa5b4e9 | 2021-06-02 12:56:08 -0700 | [diff] [blame] | 80 | dirHeader.SetMode(0755 | os.ModeDir) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 81 | dirHeader.SetModTime(DefaultTime) |
| 82 | |
| 83 | return dirHeader |
| 84 | } |
| 85 | |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 86 | // Create a manifest zip header and contents using the provided contents if any. |
| 87 | func ManifestFileContents(contents []byte) (*zip.FileHeader, []byte, error) { |
| 88 | b, err := manifestContents(contents) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 89 | if err != nil { |
| 90 | return nil, nil, err |
| 91 | } |
| 92 | |
| 93 | fh := &zip.FileHeader{ |
| 94 | Name: ManifestFile, |
| 95 | Method: zip.Store, |
| 96 | UncompressedSize64: uint64(len(b)), |
| 97 | } |
Chris Gross | fa5b4e9 | 2021-06-02 12:56:08 -0700 | [diff] [blame] | 98 | fh.SetMode(0644) |
Colin Cross | 2825cb3 | 2017-09-29 13:57:10 -0700 | [diff] [blame] | 99 | fh.SetModTime(DefaultTime) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 100 | |
| 101 | return fh, b, nil |
| 102 | } |
| 103 | |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 104 | // Create manifest contents, using the provided contents if any. |
| 105 | func manifestContents(contents []byte) ([]byte, error) { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 106 | manifestMarker := []byte("Manifest-Version:") |
| 107 | header := append(manifestMarker, []byte(" 1.0\nCreated-By: soong_zip\n")...) |
| 108 | |
| 109 | var finalBytes []byte |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 110 | if !bytes.Contains(contents, manifestMarker) { |
| 111 | finalBytes = append(append(header, contents...), byte('\n')) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 112 | } else { |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 113 | finalBytes = contents |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return finalBytes, nil |
| 117 | } |
Colin Cross | 9cb51db | 2019-06-17 14:12:41 -0700 | [diff] [blame] | 118 | |
| 119 | var javaIgnorableIdentifier = &unicode.RangeTable{ |
| 120 | R16: []unicode.Range16{ |
| 121 | {0x00, 0x08, 1}, |
| 122 | {0x0e, 0x1b, 1}, |
| 123 | {0x7f, 0x9f, 1}, |
| 124 | }, |
| 125 | LatinOffset: 3, |
| 126 | } |
| 127 | |
| 128 | func javaIdentRune(ch rune, i int) bool { |
| 129 | if unicode.IsLetter(ch) { |
| 130 | return true |
| 131 | } |
| 132 | if unicode.IsDigit(ch) && i > 0 { |
| 133 | return true |
| 134 | } |
| 135 | |
| 136 | if unicode.In(ch, |
| 137 | unicode.Nl, // letter number |
| 138 | unicode.Sc, // currency symbol |
| 139 | unicode.Pc, // connecting punctuation |
| 140 | ) { |
| 141 | return true |
| 142 | } |
| 143 | |
| 144 | if unicode.In(ch, |
| 145 | unicode.Cf, // format |
| 146 | unicode.Mc, // combining mark |
| 147 | unicode.Mn, // non-spacing mark |
| 148 | javaIgnorableIdentifier, |
| 149 | ) && i > 0 { |
| 150 | return true |
| 151 | } |
| 152 | |
| 153 | return false |
| 154 | } |
| 155 | |
| 156 | // JavaPackage parses the package out of a java source file by looking for the package statement, or the first valid |
| 157 | // non-package statement, in which case it returns an empty string for the package. |
| 158 | func JavaPackage(r io.Reader, src string) (string, error) { |
| 159 | var s scanner.Scanner |
| 160 | var sErr error |
| 161 | |
| 162 | s.Init(r) |
| 163 | s.Filename = src |
| 164 | s.Error = func(s *scanner.Scanner, msg string) { |
| 165 | sErr = fmt.Errorf("error parsing %q: %s", src, msg) |
| 166 | } |
| 167 | s.IsIdentRune = javaIdentRune |
| 168 | |
| 169 | tok := s.Scan() |
| 170 | if sErr != nil { |
| 171 | return "", sErr |
| 172 | } |
| 173 | if tok == scanner.Ident { |
| 174 | switch s.TokenText() { |
| 175 | case "package": |
| 176 | // Nothing |
| 177 | case "import": |
| 178 | // File has no package statement, first keyword is an import |
| 179 | return "", nil |
| 180 | case "class", "enum", "interface": |
| 181 | // File has no package statement, first keyword is a type declaration |
| 182 | return "", nil |
| 183 | case "public", "protected", "private", "abstract", "static", "final", "strictfp": |
| 184 | // File has no package statement, first keyword is a modifier |
| 185 | return "", nil |
| 186 | case "module", "open": |
| 187 | // File has no package statement, first keyword is a module declaration |
| 188 | return "", nil |
| 189 | default: |
| 190 | return "", fmt.Errorf(`expected first token of java file to be "package", got %q`, s.TokenText()) |
| 191 | } |
| 192 | } else if tok == '@' { |
| 193 | // File has no package statement, first token is an annotation |
| 194 | return "", nil |
| 195 | } else if tok == scanner.EOF { |
| 196 | // File no package statement, it has no non-whitespace non-comment tokens |
| 197 | return "", nil |
| 198 | } else { |
| 199 | return "", fmt.Errorf(`expected first token of java file to be "package", got %q`, s.TokenText()) |
| 200 | } |
| 201 | |
| 202 | var pkg string |
| 203 | for { |
| 204 | tok = s.Scan() |
| 205 | if sErr != nil { |
| 206 | return "", sErr |
| 207 | } |
| 208 | if tok != scanner.Ident { |
| 209 | return "", fmt.Errorf(`expected "package <package>;", got "package %s%s"`, pkg, s.TokenText()) |
| 210 | } |
| 211 | pkg += s.TokenText() |
| 212 | |
| 213 | tok = s.Scan() |
| 214 | if sErr != nil { |
| 215 | return "", sErr |
| 216 | } |
| 217 | if tok == ';' { |
| 218 | return pkg, nil |
| 219 | } else if tok == '.' { |
| 220 | pkg += "." |
| 221 | } else { |
| 222 | return "", fmt.Errorf(`expected "package <package>;", got "package %s%s"`, pkg, s.TokenText()) |
| 223 | } |
| 224 | } |
| 225 | } |