]> Sergey Matveev's repositories - godlighty.git/blob - meta4/parse.go
More known hashes, sync with meta4ra
[godlighty.git] / meta4 / parse.go
1 // godlighty -- highly-customizable HTTP, HTTP/2, HTTPS server
2 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package meta4
17
18 import (
19         "encoding/hex"
20         "encoding/xml"
21 )
22
23 var KnownHashes = map[string]string{
24         "blake3-256":   "BLAKE3-256",
25         "blake2b-512":  "BLAKE2b-512",
26         "blake2b-256":  "BLAKE2b-256",
27         "sha-256":      "SHA-256",
28         "sha-512":      "SHA-512",
29         "shake128":     "SHAKE128",
30         "shake256":     "SHAKE256",
31         "skein-256":    "Skein-256",
32         "skein-512":    "Skein-512",
33         "streebog-256": "Streebog-256",
34         "streebog-512": "Streebog-512",
35         "xxh3-128":     "XXH3-128",
36 }
37
38 type ForHTTP struct {
39         Hashes   map[string][]byte
40         URLs     []string
41         Torrents []string
42 }
43
44 func Parse(fn string, data []byte) (*ForHTTP, error) {
45         var meta Metalink
46         err := xml.Unmarshal(data, &meta)
47         if err != nil {
48                 return nil, err
49         }
50         for _, f := range meta.Files {
51                 if f.Name != fn {
52                         continue
53                 }
54                 forHTTP := ForHTTP{Hashes: make(map[string][]byte)}
55                 for _, h := range f.Hashes {
56                         name := KnownHashes[h.Type]
57                         if name == "" {
58                                 continue
59                         }
60                         digest, err := hex.DecodeString(h.Hash)
61                         if err != nil {
62                                 return nil, err
63                         }
64                         forHTTP.Hashes[name] = digest
65                 }
66                 for _, u := range f.URLs {
67                         forHTTP.URLs = append(forHTTP.URLs, u.URL)
68                 }
69                 for _, m := range f.MetaURLs {
70                         if m.MediaType == "torrent" {
71                                 forHTTP.Torrents = append(forHTTP.Torrents, m.URL)
72                         }
73                 }
74                 return &forHTTP, nil
75         }
76         return nil, nil
77 }