.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.1.1 / internal / go / gcimporter / exportdata.go
1 // Copyright 2011 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 // This file is a copy of $GOROOT/src/go/internal/gcimporter/exportdata.go.
6
7 // This file implements FindExportData.
8
9 package gcimporter
10
11 import (
12         "bufio"
13         "fmt"
14         "io"
15         "io/ioutil"
16         "strconv"
17         "strings"
18 )
19
20 func readGopackHeader(r io.Reader) (name string, size int, err error) {
21         // See $GOROOT/include/ar.h.
22         hdr := make([]byte, 16+12+6+6+8+10+2)
23         _, err = io.ReadFull(r, hdr)
24         if err != nil {
25                 return
26         }
27         // leave for debugging
28         if false {
29                 fmt.Printf("header: %s", hdr)
30         }
31         s := strings.TrimSpace(string(hdr[16+12+6+6+8:][:10]))
32         size, err = strconv.Atoi(s)
33         if err != nil || hdr[len(hdr)-2] != '`' || hdr[len(hdr)-1] != '\n' {
34                 err = fmt.Errorf("invalid archive header")
35                 return
36         }
37         name = strings.TrimSpace(string(hdr[:16]))
38         return
39 }
40
41 // findExportData positions the reader r at the beginning of the
42 // export data section of an underlying GC-created object/archive
43 // file by reading from it. The reader must be positioned at the
44 // start of the file before calling this function. The hdr result
45 // is the string before the export data, either "$$" or "$$B".
46 //
47 func findExportData(r *bufio.Reader) (hdr string, length int, err error) {
48         // Read first line to make sure this is an object file.
49         line, err := r.ReadSlice('\n')
50         if err != nil {
51                 err = fmt.Errorf("can't find export data (%v)", err)
52                 return
53         }
54
55         if string(line) == "!<arch>\n" {
56                 // Archive file. Scan to __.PKGDEF.
57                 var name string
58                 if name, length, err = readGopackHeader(r); err != nil {
59                         return
60                 }
61
62                 // First entry should be __.PKGDEF.
63                 if name != "__.PKGDEF" {
64                         err = fmt.Errorf("go archive is missing __.PKGDEF")
65                         return
66                 }
67
68                 // Read first line of __.PKGDEF data, so that line
69                 // is once again the first line of the input.
70                 if line, err = r.ReadSlice('\n'); err != nil {
71                         err = fmt.Errorf("can't find export data (%v)", err)
72                         return
73                 }
74                 length -= len(line)
75         }
76
77         // Now at __.PKGDEF in archive or still at beginning of file.
78         // Either way, line should begin with "go object ".
79         if !strings.HasPrefix(string(line), "go object ") {
80                 err = fmt.Errorf("not a Go object file")
81                 return
82         }
83
84         // Skip over object header to export data.
85         // Begins after first line starting with $$.
86         for line[0] != '$' {
87                 if line, err = r.ReadSlice('\n'); err != nil {
88                         err = fmt.Errorf("can't find export data (%v)", err)
89                         return
90                 }
91                 length -= len(line)
92         }
93         hdr = string(line)
94
95         return
96 }
97
98 func GetExportData(r io.ReadSeeker, b []byte) ([]byte, error) {
99         br := bufio.NewReader(r)
100         _, length, err := findExportData(br)
101         if err != nil {
102                 return nil, err
103         }
104         if _, err := r.Seek(-int64(br.Buffered()), io.SeekCurrent); err != nil {
105                 return nil, err
106         }
107         if length > 0 {
108                 if cap(b) >= length {
109                         b = b[:length]
110                 } else {
111                         b = make([]byte, length)
112                 }
113                 _, err := io.ReadFull(r, b)
114                 return b, err
115         } else {
116                 return ioutil.ReadAll(r)
117         }
118 }