-
Notifications
You must be signed in to change notification settings - Fork 3
feat: implement HostResolver using CRT #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
b2a2625
11572b5
88c61fe
4ba7c5b
ca1b218
0ccd5d3
450cbc6
4f4f2d3
326ddca
186cd26
c72ec66
5eddfaa
11ab0c2
bfcf314
f82711e
31f1dcf
873d3b1
bd99c72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package aws.sdk.kotlin.crt.util | ||
|
|
||
| import aws.sdk.kotlin.crt.winver.* | ||
| import kotlinx.cinterop.* | ||
| import platform.posix.memcpy | ||
|
|
||
| // The functions below are adapted from C++ SDK: | ||
| // https://github.com/aws/aws-sdk-cpp/blob/0e6085bf0dd9a1cb1f27d101c4cf2db6ade6f307/src/aws-cpp-sdk-core/source/platform/windows/OSVersionInfo.cpp#L49-L106 | ||
|
|
||
| private val wordHexFormat = HexFormat { | ||
| upperCase = false | ||
| number { | ||
| removeLeadingZeros = true | ||
| minLength = 4 | ||
| } | ||
| } | ||
|
|
||
| private data class LangCodePage( | ||
| val language: UShort, | ||
| val codePage: UShort, | ||
| ) | ||
|
|
||
| public fun osVersionFromKernel(): String? = memScoped { | ||
| withFileVersionInfo("Kernel32.dll") { versionInfoPtr -> | ||
| getLangCodePage(versionInfoPtr)?.let { langCodePage -> | ||
| getProductVersion(versionInfoPtr, langCodePage) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private inline fun <R> withFileVersionInfo(fileName: String, block: (CPointer<ByteVarOf<Byte>>) -> R?): R? { | ||
| val blobSize = GetFileVersionInfoSizeW(fileName, null) | ||
| val blob = ByteArray(blobSize.convert()) | ||
| blob.usePinned { pinned -> | ||
| val result = GetFileVersionInfoW(fileName, 0u, blobSize, pinned.addressOf(0)) | ||
| return if (result == 0) { | ||
| null | ||
| } else { | ||
| block(pinned.addressOf(0)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun MemScope.getLangCodePage(versionInfoPtr: CPointer<ByteVarOf<Byte>>): LangCodePage? { | ||
| // Get _any_ language pack and codepage since they should all have the same version | ||
| val langAndCodePagePtr = alloc<COpaquePointerVar>() | ||
| val codePageSize = alloc<UIntVar>() | ||
| val result = VerQueryValueW( | ||
| versionInfoPtr, | ||
| """\VarFileInfo\Translation""", | ||
| langAndCodePagePtr.ptr, | ||
| codePageSize.ptr, | ||
| ) | ||
|
|
||
| return if (result == 0) { | ||
| null | ||
| } else { | ||
| val langAndCodePage = langAndCodePagePtr.value!!.reinterpret<UIntVar>().pointed.value | ||
| val language = (langAndCodePage and 0x0000ffffu).toUShort() // low WORD | ||
| val codePage = (langAndCodePage and 0xffff0000u shr 16).toUShort() // high WORD | ||
| LangCodePage(language, codePage) | ||
| } | ||
| } | ||
|
|
||
| private fun MemScope.getProductVersion(versionInfoPtr: CPointer<ByteVarOf<Byte>>, langCodePage: LangCodePage): String? { | ||
| val versionId = buildString { | ||
| // Something like: \StringFileInfo\04090fb0\ProductVersion | ||
| append("""\StringFileInfo\""") | ||
| append(langCodePage.language.toHexString(wordHexFormat)) | ||
| append(langCodePage.codePage.toHexString(wordHexFormat)) | ||
| append("""\ProductVersion""") | ||
| } | ||
|
|
||
| // Get the block corresponding to versionId | ||
| val block = alloc<COpaquePointerVar>() | ||
| val blockSize = alloc<UIntVar>() | ||
| val result = VerQueryValueW(versionInfoPtr, versionId, block.ptr, blockSize.ptr) | ||
|
|
||
| return if (result == 0) { | ||
| null | ||
| } else { | ||
| // Copy the bytes into a Kotlin byte array | ||
| val blockBytes = ByteArray(blockSize.value.convert()) | ||
| blockBytes.usePinned { pinned -> | ||
| memcpy(pinned.addressOf(0), block.value!!.reinterpret<ByteVar>(), blockSize.value.convert()) | ||
| } | ||
| blockBytes.decodeToString() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package aws.sdk.kotlin.crt.util | ||
|
|
||
| import kotlinx.coroutines.test.runTest | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertNotNull | ||
|
|
||
| class OsVersionTest { | ||
| @Test | ||
| fun testOsInfo() = runTest { | ||
| val version = osVersionFromKernel() | ||
| assertNotNull(version) | ||
| } | ||
|
Comment on lines
+13
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: This only runs on Windows so we should verify the correct OS family is returned: val version = osVersionFromKernel()
assertEquals(OsFamily.Windows, version.family)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. osVersionFromKernel just returns the string version, not the family |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Let's add a comment explaining why this is here so that Future We are not confused.