Skip to content

Instantly share code, notes, and snippets.

@k-takata
Created December 15, 2012 10:03
Show Gist options
  • Save k-takata/4292446 to your computer and use it in GitHub Desktop.
Save k-takata/4292446 to your computer and use it in GitHub Desktop.
Using errno which is actually linked with iconv.dll
# HG changeset patch
# Parent b57883f4cb4b0fe42b70fbf34fed933ab9bd7440
diff --git a/src/mbyte.c b/src/mbyte.c
--- a/src/mbyte.c
+++ b/src/mbyte.c
@@ -4307,6 +4307,44 @@
# endif
/*
+ * Get the address of 'funcname' which is imported by 'hInst' DLL.
+ */
+ static void *
+get_iconv_import_func(HINSTANCE hInst, const char *funcname)
+{
+ PBYTE pImage = (PBYTE)hInst;
+ PIMAGE_DOS_HEADER pDOS = (PIMAGE_DOS_HEADER)hInst;
+ PIMAGE_NT_HEADERS pPE;
+ PIMAGE_IMPORT_DESCRIPTOR pImpDesc;
+ PIMAGE_THUNK_DATA pIAT; /* Import Address Table */
+ PIMAGE_THUNK_DATA pINT; /* Import Name Table */
+ PIMAGE_IMPORT_BY_NAME pImpName;
+
+ if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
+ return NULL;
+ pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
+ if (pPE->Signature != IMAGE_NT_SIGNATURE)
+ return NULL;
+ pImpDesc = (PIMAGE_IMPORT_DESCRIPTOR)(pImage
+ + pPE->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
+ .VirtualAddress);
+ for (; pImpDesc->FirstThunk; ++pImpDesc)
+ {
+ pIAT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->FirstThunk);
+ pINT = (PIMAGE_THUNK_DATA)(pImage + pImpDesc->OriginalFirstThunk);
+ for (; pIAT->u1.Function; ++pIAT, ++pINT)
+ {
+ if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
+ continue;
+ pImpName = (PIMAGE_IMPORT_BY_NAME)(pImage + pINT->u1.AddressOfData);
+ if (strcmp(pImpName->Name, funcname) == 0)
+ return (void*)pIAT->u1.Function;
+ }
+ }
+ return NULL;
+}
+
+/*
* Try opening the iconv.dll and return TRUE if iconv() can be used.
*/
int
@@ -4339,7 +4377,9 @@
iconv_open = (void *)GetProcAddress(hIconvDLL, "libiconv_open");
iconv_close = (void *)GetProcAddress(hIconvDLL, "libiconv_close");
iconvctl = (void *)GetProcAddress(hIconvDLL, "libiconvctl");
- iconv_errno = (void *)GetProcAddress(hMsvcrtDLL, "_errno");
+ iconv_errno = get_iconv_import_func(hIconvDLL, "_errno");
+ if (iconv_errno == NULL)
+ iconv_errno = (void *)GetProcAddress(hMsvcrtDLL, "_errno");
if (iconv == NULL || iconv_open == NULL || iconv_close == NULL
|| iconvctl == NULL || iconv_errno == NULL)
{
@koron
Copy link

koron commented Dec 15, 2012

πŸ‘ εΏ…θ¦γ¨γ•γ‚Œγ‚‹γ‹γ―γ‚γ‹γ‚ŠγΎγ›γ‚“γŒε‡Ίγ—γ¦γΏγ‚‹δΎ‘ε€€γ―γ‚γ‚‹γ¨ζ€γ„γΎγ™γ€‚

@mattn
Copy link

mattn commented Dec 15, 2012

γŸγ—γ‹γ€iconv_errno γ¨γ„γ†γ‚¨γƒ³γƒˆγƒͺγƒγ‚€γƒ³γƒˆγŒγ‚γ£γŸγ¨ζ€γ†γ€‚

@k-takata
Copy link
Author

γŸγ—γ‹γ€iconv_errno γ¨γ„γ†γ‚¨γƒ³γƒˆγƒͺγƒγ‚€γƒ³γƒˆγŒγ‚γ£γŸγ¨ζ€γ†γ€‚

ε΄δΈ‹γ•γ‚ŒγŸγ¨θžγγΎγ—γŸγŒγ€‚
https://twitter.com/nu774_qaac/status/278761156695449600
bug #28084: Please add a export function to provide an errno in libiconv.

@mattn
Copy link

mattn commented Dec 17, 2012

γΎγƒΌγ˜γƒΌγ§γƒΌw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment