github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/mime/type_windows.go (about) 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 package mime 6 7 import ( 8 "syscall" 9 "unsafe" 10 ) 11 12 func init() { 13 osInitMime = initMimeWindows 14 } 15 16 func initMimeWindows() { 17 var root syscall.Handle 18 rootpathp, _ := syscall.UTF16PtrFromString(`\`) 19 if syscall.RegOpenKeyEx(syscall.HKEY_CLASSES_ROOT, rootpathp, 20 0, syscall.KEY_READ, &root) != nil { 21 return 22 } 23 defer syscall.RegCloseKey(root) 24 var count uint32 25 if syscall.RegQueryInfoKey(root, nil, nil, nil, &count, nil, nil, nil, nil, nil, nil, nil) != nil { 26 return 27 } 28 contenttypep, _ := syscall.UTF16PtrFromString("Content Type") 29 var buf [1 << 10]uint16 30 for i := uint32(0); i < count; i++ { 31 n := uint32(len(buf)) 32 if syscall.RegEnumKeyEx(root, i, &buf[0], &n, nil, nil, nil, nil) != nil { 33 continue 34 } 35 ext := syscall.UTF16ToString(buf[:]) 36 if len(ext) < 2 || ext[0] != '.' { // looking for extensions only 37 continue 38 } 39 var h syscall.Handle 40 extpathp, _ := syscall.UTF16PtrFromString(`\` + ext) 41 if syscall.RegOpenKeyEx( 42 syscall.HKEY_CLASSES_ROOT, extpathp, 43 0, syscall.KEY_READ, &h) != nil { 44 continue 45 } 46 var typ uint32 47 n = uint32(len(buf) * 2) // api expects array of bytes, not uint16 48 if syscall.RegQueryValueEx( 49 h, contenttypep, 50 nil, &typ, (*byte)(unsafe.Pointer(&buf[0])), &n) != nil { 51 syscall.RegCloseKey(h) 52 continue 53 } 54 syscall.RegCloseKey(h) 55 if typ != syscall.REG_SZ { // null terminated strings only 56 continue 57 } 58 mimeType := syscall.UTF16ToString(buf[:]) 59 setExtensionType(ext, mimeType) 60 } 61 } 62 63 func initMimeForTests() map[string]string { 64 return map[string]string{ 65 ".PnG": "image/png", 66 } 67 }