Microsoft MVP성태의 닷넷 이야기
.NET Framework: 481. Mono 내부의 문자열 처리 방식은 UTF-8 [링크 복사], [링크+제목 복사],
조회: 20974
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

Mono 내부의 문자열 처리 방식은 UTF-8

지난번에 소개해 드린 모노 프로파일러로,

Visual Studio에서 Mono 용 Profiler 개발
; https://www.sysnet.pe.kr/2/0/1805

JIT 컴파일 시의 메서드 이름을 알아내는 코드를 다음과 같이 작성할 수 있습니다.

void mono_profiler_jit_compile_enter(MonoProfiler *prof, MonoMethod *method)
{
    const gchar *methodName = mono_method_get_name(method);
}

보시는 바와 같이 반환 값이 gchar *타입이고, gchar 타입은 GLib 헤더 파일에 char 타입으로 정의되어 있으므로 결국 wchar_t 처리에 대한 고려는 전혀 안되어 있습니다.

그렇다면, 한글 처리가 어떻게 될지 궁금해 지는데요. 이런 예제를 만들고,

using System;

class MainClass
{
    public static void Main (string[] args)
    {
        테스트 ();
    }

    public static void 테스트()
    {
        // ...
    }
}

프로파일러 코드에서 methodName을 구해 보면, Visual Studio의 Watch 창에는 "methodName 0x005604f8 <Invalid characters in string.> const char *"이라고 표시됩니다. 혹시나 싶어서 wchar_t * 타입으로 강제 형변환해도 깨진 글자로만 출력됩니다. Watch 창에 출력된 주소 "0x005604f8"의 메모리 값을 조사해 보면,

hangul_in_mono_1

보시는 바와 같이 "ed 85 8c ec 8a a4 ed 8a b8 00"으로 나옵니다. 대충 한글 한 글자당 3바이트가 할당되고 NULL 처리는 단일 '\0' 문자로만 되어 있는 걸로 봐서 UTF16 이상의 인코딩은 아닌 것 같고 UTF-8로 된 듯 한데, 다음과 같이 확인해 볼 수 있습니다.

using System;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        string txt = "테스트";
        Console.WriteLine(BitConverter.ToString(Encoding.UTF8.GetBytes(txt))); // 출력: ED-85-8C-EC-8A-A4-ED-8A-B8
    }
}

아하~~~ 맞군요. ^^

그렇다면 혹시 mono 런타임에 ANSI가 아닌 WIDE 타입의 문자열로 반환하는 메서드가 있지 않을까요? 미리 답변을 내리자면, 없습니다.

모노 소스코드의 "\mono\mono\metadata\class-internals.h" 헤더 파일에 보면 MonoMethod는 다음과 같이 정의되어 있습니다.
struct _MonoMethod {
    guint16 flags;  /* method flags */
    guint16 iflags; /* method implementation flags */
    guint32 token;
    MonoClass *klass;
    MonoMethodSignature *signature;
    const char *name;
    //...[생략]...
}

그리고 "\mono\mono\metadata\loader.c"에 정의된 mono_method_get_name은 그저 MonoMethod 클래스의 name 필드를 반환하는 역할만 합니다.

const char*
mono_method_get_name (MonoMethod *method)
{
    return method->name;
}

대신 GLib를 이용하면 다음과 같이 편하게 대응함수를 만들 수 있습니다.

wchar_t* mono_method_get_nameW(MonoMethod *method)
{
    const gchar *methodName = mono_method_get_name(method);

    wchar_t *pName = (wchar_t *)g_utf8_to_utf16(methodName, strlen(methodName), NULL, NULL, NULL);
    return pName; // mono_method_get_nameW에서는 반드시 g_free를 해야 함.
}

참고로, g_utf8_to_utf16 메서드는 내부적으로 g_malloc으로 메모리를 할당해서 UTF-16 인코딩의 문자열을 담은 버퍼를 반환하기 때문에 사용 후 반드시 g_free 메서드로 해제해야 합니다.

그런데... Mono의 전반적인 문자열 처리가 이렇게 1바이트 연산 기반의 utf-8로 되어 있기 때문에 가능한 그대로 따르는 것이 좋습니다. (예를 들어, strlen과 같은 문자열 연산 함수에서 별탈없이 성공하기 때문에.) 단지, 마지막에 출력할 때나 UTF16으로 변환하는 식의 처리만 하는 것이 코드 작성이 간결해 질 수 있습니다.

(그나저나, 요새 잠깐 시간날 때마다 Mono를 살펴보고 있는데... 과히 만족스럽진 않군요. ^^ 아무리 무료로 공개되어 자금 지원의 규모면에서 차이가 있다고는 하지만.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 7/10/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 61  62  63  64  65  66  67  68  69  70  [71]  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12161정성태2/26/202017773오류 유형: 597. manifest - The value "x64" of attribute "processorArchitecture" in element "assemblyIdentity" is invalid.
12160정성태2/26/202018221개발 환경 구성: 469. Reg-free COM 개체 사용을 위한 manifest 파일 생성 도구 - COMRegFreeManifest
12159정성태2/26/202015306오류 유형: 596. Visual Studio - The project needs to include ATL support
12158정성태2/25/202017892디버깅 기술: 165. C# - Marshal.GetIUnknownForObject/GetIDispatchForObject 사용 시 메모리 누수(Memory Leak) 발생파일 다운로드1
12157정성태2/25/202017932디버깅 기술: 164. C# - Marshal.GetNativeVariantForObject 사용 시 메모리 누수(Memory Leak) 발생 및 해결 방법파일 다운로드1
12156정성태2/25/202016510오류 유형: 595. LINK : warning LNK4098: defaultlib 'nafxcw.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
12155정성태2/25/202015972오류 유형: 594. Warning NU1701 - This package may not be fully compatible with your project
12154정성태2/25/202015342오류 유형: 593. warning LNK4070: /OUT:... directive in .EXP differs from output filename
12153정성태2/23/202019468.NET Framework: 898. Trampoline을 이용한 후킹의 한계파일 다운로드1
12152정성태2/23/202018554.NET Framework: 897. 실행 시에 메서드 가로채기 - CLR Injection: Runtime Method Replacer 개선 - 세 번째 이야기(Trampoline 후킹)파일 다운로드1
12151정성태2/22/202019331.NET Framework: 896. C# - Win32 API를 Trampoline 기법을 이용해 C# 메서드로 가로채는 방법 - 두 번째 이야기 (원본 함수 호출)파일 다운로드1
12150정성태2/21/202019564.NET Framework: 895. C# - Win32 API를 Trampoline 기법을 이용해 C# 메서드로 가로채는 방법 [1]파일 다운로드1
12149정성태2/20/202018406.NET Framework: 894. eBEST C# XingAPI 래퍼 - 연속 조회 처리 방법 [1]
12148정성태2/19/202020900디버깅 기술: 163. x64 환경에서 구현하는 다양한 Trampoline 기법 [1]
12147정성태2/19/202018595디버깅 기술: 162. x86/x64의 기계어 코드 최대 길이
12146정성태2/18/202019337.NET Framework: 893. eBEST C# XingAPI 래퍼 - 로그인 처리파일 다운로드1
12145정성태2/18/202019194.NET Framework: 892. eBEST C# XingAPI 래퍼 - Sqlite 지원 추가파일 다운로드1
12144정성태2/13/202019322.NET Framework: 891. 실행 시에 메서드 가로채기 - CLR Injection: Runtime Method Replacer 개선 - 두 번째 이야기파일 다운로드1
12143정성태2/13/202015965.NET Framework: 890. 상황별 GetFunctionPointer 반환값 정리 - x64파일 다운로드1
12142정성태2/12/202018309.NET Framework: 889. C# 코드로 접근하는 MethodDesc, MethodTable파일 다운로드1
12141정성태2/10/202017334.NET Framework: 888. C# - ASP.NET Core 웹 응용 프로그램의 출력 가로채기 [2]파일 다운로드1
12140정성태2/10/202017845.NET Framework: 887. C# - ASP.NET 웹 응용 프로그램의 출력 가로채기파일 다운로드1
12139정성태2/9/202019142.NET Framework: 886. C# - Console 응용 프로그램에서 UI 스레드 구현 방법
12138정성태2/9/202023165.NET Framework: 885. C# - 닷넷 응용 프로그램에서 SQLite 사용 [6]파일 다운로드1
12137정성태2/9/202016393오류 유형: 592. [AhnLab] 경고 - 디버거 실행을 탐지했습니다.
12136정성태2/6/202017342Windows: 168. Windows + S(또는 Q)로 뜨는 작업 표시줄의 검색 바가 동작하지 않는 경우
... 61  62  63  64  65  66  67  68  69  70  [71]  72  73  74  75  ...