Fix bootstrap/PR63632
[official-gcc.git] / libgo / go / mime / type_windows.go
blob180f948d16fe91bc7a98478ef58219927e5d2945
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.
5 package mime
7 import (
8 "syscall"
9 "unsafe"
12 func initMime() {
13 var root syscall.Handle
14 rootpathp, _ := syscall.UTF16PtrFromString(`\`)
15 if syscall.RegOpenKeyEx(syscall.HKEY_CLASSES_ROOT, rootpathp,
16 0, syscall.KEY_READ, &root) != nil {
17 return
19 defer syscall.RegCloseKey(root)
20 var count uint32
21 if syscall.RegQueryInfoKey(root, nil, nil, nil, &count, nil, nil, nil, nil, nil, nil, nil) != nil {
22 return
24 var buf [1 << 10]uint16
25 for i := uint32(0); i < count; i++ {
26 n := uint32(len(buf))
27 if syscall.RegEnumKeyEx(root, i, &buf[0], &n, nil, nil, nil, nil) != nil {
28 continue
30 ext := syscall.UTF16ToString(buf[:])
31 if len(ext) < 2 || ext[0] != '.' { // looking for extensions only
32 continue
34 var h syscall.Handle
35 extpathp, _ := syscall.UTF16PtrFromString(`\` + ext)
36 if syscall.RegOpenKeyEx(
37 syscall.HKEY_CLASSES_ROOT, extpathp,
38 0, syscall.KEY_READ, &h) != nil {
39 continue
41 var typ uint32
42 n = uint32(len(buf) * 2) // api expects array of bytes, not uint16
43 contenttypep, _ := syscall.UTF16PtrFromString("Content Type")
44 if syscall.RegQueryValueEx(
45 h, contenttypep,
46 nil, &typ, (*byte)(unsafe.Pointer(&buf[0])), &n) != nil {
47 syscall.RegCloseKey(h)
48 continue
50 syscall.RegCloseKey(h)
51 if typ != syscall.REG_SZ { // null terminated strings only
52 continue
54 mimeType := syscall.UTF16ToString(buf[:])
55 setExtensionType(ext, mimeType)
59 func initMimeForTests() map[string]string {
60 return map[string]string{
61 ".png": "image/png",