{*******************************************************} { } { ProcessHwp } { } { Copyright (C) 2022 sunk } { } {*******************************************************} // DFRC HWP ¾Æ·¡ÇÑ±Û ¹®¼­ ÇÊÅÍ unit ProcessHwp; interface uses System.Classes, Winapi.Windows, System.SysUtils; const DLL_DFRCFT_HWP = 'kvhvp.dll'; // ±âº»ÀûÀ¸·Î UNICODE·Î ÅØ½ºÆ®¸¦ ¹öÆÛ¿¡ ÀúÀåÇϸç, UTF8, CP949·ÎÀÇ º¯È¯ÀÌ °¡´ÉÇÏ´Ù. DFRCFT_UNICODE = 10; DFRCFT_UTF8 = 20; DFRCFT_CP949 = 30; DFRCFT_HWP = 1000; type PDfrcftBuffer = ^TDfrcftBuffer; TDfrcftBuffer = record dwLen: UINT; // À¯´ÏÄÚµå ±æÀÌ pBuf: UINT16; // À¯´ÏÄÚµå ¹öÆÛ end; Tdfrcft_detect_file = function(pFilePath: PByte): Integer; cdecl; Tdfrcft_hwp_suminfo_file = function(pFilePath: PByte; pDfrcftBuf: PDfrcftBuffer): Integer; cdecl; Tdfrcft_hwp_text_file = function(pFilePath: PByte; pDfrcftBuf: PDfrcftBuffer): Integer; cdecl; Tdfrcft_convert_encoding = function(pDfrcftBuf: PDfrcftBuffer; pBuf: PByte; dwCharEncoding: DWORD): Integer; cdecl; Tdfrcft_convert_encoding_free_mem = procedure(pBuf: PByte); cdecl; Tdfrcft_save_file = procedure(pDfrcftBuf: PDfrcftBuffer; pFilePath: PByte; dwCharEncoding: DWORD); cdecl; Tdfrcft_free_mem = procedure(pDfrcftBuf: PDfrcftBuffer); cdecl; function dfrcft_detect_file(sFilePath: AnsiString): Integer; function dfrcft_hwp_suminfo_file(sFilePath: AnsiString; pDfrcftBuf: PDfrcftBuffer): Integer; function dfrcft_hwp_text_file(sFilePath: AnsiString; pDfrcftBuf: PDfrcftBuffer): Integer; function dfrcft_convert_encoding(pDfrcftBuf: PDfrcftBuffer; pBuf: PByte; dwCharEncoding: DWORD): Integer; procedure dfrcft_convert_encoding_free_mem(pBuf: PByte); procedure dfrcft_save_file(pDfrcftBuf: PDfrcftBuffer; sFilePath: AnsiString; dwCharEncoding: DWORD); procedure dfrcft_free_mem(pDfrcftBuf: PDfrcftBuffer); function FilterHwp(sSrcPath, sDestPath: String): Boolean; implementation var hDfrcftDLL: THandle = 0; _fn_dfrcft_detect_file: Tdfrcft_detect_file = nil; _fn_dfrcft_hwp_suminfo_file: Tdfrcft_hwp_suminfo_file = nil; _fn_dfrcft_hwp_text_file: Tdfrcft_hwp_text_file = nil; _fn_dfrcft_convert_encoding: Tdfrcft_convert_encoding = nil; _fn_dfrcft_convert_encoding_free_mem: Tdfrcft_convert_encoding_free_mem = nil; _fn_dfrcft_save_file: Tdfrcft_save_file = nil; _fn_dfrcft_free_mem: Tdfrcft_free_mem = nil; function InitHwpDfrcftProcedure: Boolean; begin if hDfrcftDLL = 0 then begin hDfrcftDLL := LoadLibrary('.\bin\' + DLL_DFRCFT_HWP); if hDfrcftDLL = 0 then hDfrcftDLL := LoadLibrary(DLL_DFRCFT_HWP); if hDfrcftDLL <> 0 then begin @_fn_dfrcft_detect_file := GetProcAddress(hDfrcftDLL, 'dfrcft_detect_file'); @_fn_dfrcft_hwp_suminfo_file := GetProcAddress(hDfrcftDLL, 'dfrcft_hwp_suminfo_file'); @_fn_dfrcft_hwp_text_file := GetProcAddress(hDfrcftDLL, 'dfrcft_hwp_text_file'); @_fn_dfrcft_convert_encoding := GetProcAddress(hDfrcftDLL, 'dfrcft_convert_encoding'); @_fn_dfrcft_convert_encoding_free_mem := GetProcAddress(hDfrcftDLL, 'dfrcft_convert_encoding_free_mem'); @_fn_dfrcft_save_file := GetProcAddress(hDfrcftDLL, 'dfrcft_save_file'); @_fn_dfrcft_free_mem := GetProcAddress(hDfrcftDLL, 'dfrcft_free_mem'); end; end; Result := hDfrcftDLL <> 0; end; function FilterHwp(sSrcPath, sDestPath: String): Boolean; var DfrcftBuf: TDfrcftBuffer; pBuf: PByte; fs: TFileStream; begin Result := false; try ZeroMemory(@DfrcftBuf, SizeOf(DfrcftBuf)); if (dfrcft_hwp_text_file(sSrcPath, @DfrcftBuf) = 1) then begin fs := nil; pBuf := nil; try dfrcft_convert_encoding(@DfrcftBuf, @pBuf, DFRCFT_UTF8); fs := TFileStream.Create(sDestPath, fmCreate); fs.Write(pBuf^, StrLen(PAnsiChar(pBuf))); Result := true; finally if pBuf <> nil then dfrcft_convert_encoding_free_mem(pBuf); if DfrcftBuf.pBuf <> 0 then dfrcft_free_mem(@DfrcftBuf); if fs <> nil then fs.Free; end; end except // ... end; end; // FilePathÀÇ ÆÄÀÏÀÌ ÇÑ±Û ¹®¼­ ÆÄÀÏÀÎÁö ÆÇ´ÜÇÑ´Ù. // return - DFRCFT_HWP : ÇÑ±Û ¹®¼­ ÆÄÀÏ function dfrcft_detect_file(sFilePath: AnsiString): Integer; begin Result := 0; if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_detect_file) then Result := _fn_dfrcft_detect_file(@sFilePath[1]); end; // ¹®¼­ ¼Ó¼º Á¤º¸¸¦ ÃßÃâÇÑ´Ù. // return - TRUE : ¼º°ø, FALSE : ½ÇÆÐ function dfrcft_hwp_suminfo_file(sFilePath: AnsiString; pDfrcftBuf: PDfrcftBuffer): Integer; begin Result := 0; if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_hwp_suminfo_file) then Result := _fn_dfrcft_hwp_suminfo_file(@sFilePath[1], pDfrcftBuf); end; // º»¹® ÅØ½ºÆ®¸¦ ÃßÃâÇÑ´Ù. // return - TRUE : ¼º°ø, FALSE ½ÇÆÐ function dfrcft_hwp_text_file(sFilePath: AnsiString; pDfrcftBuf: PDfrcftBuffer): Integer; begin Result := 0; if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_hwp_text_file) then Result := _fn_dfrcft_hwp_text_file(@sFilePath[1], pDfrcftBuf); end; // ÀÎÄÚµùÀ» º¯È¯ÇÑ´Ù. (»ç¿ëÀÚ Á¤ÀÇ »ó¼ö ÂüÁ¶) // ÀÎÄÚµù º¯È¯¿¡ »ç¿ëÇÑ ¹öÆÛ´Â dfrcft_convert_encoding_free_mem ÇÔ¼ö¸¦ È£ÃâÇØ¼­ ÇØÁ¦ÇØ¾ß ÇÑ´Ù. // return - º¯È¯µÈ ¹®ÀÚ¿­ÀÇ ±æÀÌ function dfrcft_convert_encoding(pDfrcftBuf: PDfrcftBuffer; pBuf: PByte; dwCharEncoding: DWORD): Integer; begin Result := 0; if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_convert_encoding) then Result := _fn_dfrcft_convert_encoding(pDfrcftBuf, pBuf, dwCharEncoding); end; // ÀÎÄÚµù¿¡ »ç¿ëÇÑ ¹öÆÛ¸¦ ÇØÁ¦ procedure dfrcft_convert_encoding_free_mem(pBuf: PByte); begin if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_convert_encoding_free_mem) then _fn_dfrcft_convert_encoding_free_mem(pBuf); end; // ÇÊÅ͸µµÈ ÅØ½ºÆ®¸¦ ÆÄÀÏ·Î ÀúÀåÇÑ´Ù. procedure dfrcft_save_file(pDfrcftBuf: PDfrcftBuffer; sFilePath: AnsiString; dwCharEncoding: DWORD); begin if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_save_file) then _fn_dfrcft_save_file(pDfrcftBuf, @sFilePath[1], dwCharEncoding); end; // ÅØ½ºÆ® ÇÊÅ͸µ¿¡ »ç¿ëÇÑ PDfrcftBuffer ¸Þ¸ð¸®¸¦ ÇØÁ¦ÇÑ´Ù. procedure dfrcft_free_mem(pDfrcftBuf: PDfrcftBuffer); begin if InitHwpDfrcftProcedure and Assigned(_fn_dfrcft_free_mem) then _fn_dfrcft_free_mem(pDfrcftBuf); end; end.