Korean: Use SUBLANG_NEUTRAL in Korean resources.
[wine] / dlls / hhctrl.ocx / chm.c
1 /*
2  * CHM Utility API
3  *
4  * Copyright 2005 James Hawkins
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "winreg.h"
30 #include "ole2.h"
31 #include "htmlhelp.h"
32
33 #include "initguid.h"
34 #include "chm.h"
35
36 static LPWSTR CHM_ANSIToUnicode(LPCSTR ansi)
37 {
38     LPWSTR unicode;
39     int count;
40
41     count = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
42     unicode = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
43     MultiByteToWideChar(CP_ACP, 0, ansi, -1, unicode, count);
44
45     return unicode;
46 }
47
48 /* Reads a string from the #STRINGS section in the CHM file */
49 static LPWSTR CHM_ReadString(CHMInfo *pChmInfo, DWORD dwOffset)
50 {
51     LARGE_INTEGER liOffset;
52     IStorage *pStorage = pChmInfo->pStorage;
53     IStream *pStream;
54     DWORD cbRead;
55     ULONG iPos;
56     DWORD dwSize;
57     LPSTR szString;
58     LPWSTR stringW;
59     
60     const int CB_READ_BLOCK = 64;
61     static const WCHAR stringsW[] = {'#','S','T','R','I','N','G','S',0};
62
63     dwSize = CB_READ_BLOCK;
64     szString = HeapAlloc(GetProcessHeap(), 0, dwSize);
65
66     if (FAILED(IStorage_OpenStream(pStorage, stringsW, NULL, STGM_READ, 0, &pStream)))
67         return NULL;
68
69     liOffset.QuadPart = dwOffset;
70     
71     if (FAILED(IStream_Seek(pStream, liOffset, STREAM_SEEK_SET, NULL)))
72     {
73         IStream_Release(pStream);
74         return NULL;
75     }
76
77     while (SUCCEEDED(IStream_Read(pStream, szString, CB_READ_BLOCK, &cbRead)))
78     {
79         if (!cbRead)
80             return NULL;
81
82         for (iPos = 0; iPos < cbRead; iPos++)
83         {
84             if (!szString[iPos])
85             {
86                 stringW = CHM_ANSIToUnicode(szString);
87                 HeapFree(GetProcessHeap(), 0, szString);
88                 return stringW;
89             }
90         }
91
92         dwSize *= 2;
93         szString = HeapReAlloc(GetProcessHeap(), 0, szString, dwSize);
94         szString += cbRead;
95     }
96
97     /* didn't find a string */
98     return NULL;
99 }
100
101 /* Loads the HH_WINTYPE data from the CHM file
102  *
103  * FIXME: There may be more than one window type in the file, so
104  *        add the ability to choose a certain window type
105  */
106 BOOL CHM_LoadWinTypeFromCHM(CHMInfo *pChmInfo, HH_WINTYPEW *pHHWinType)
107 {
108     LARGE_INTEGER liOffset;
109     IStorage *pStorage = pChmInfo->pStorage;
110     IStream *pStream;
111     HRESULT hr;
112     DWORD cbRead;
113
114     static const WCHAR windowsW[] = {'#','W','I','N','D','O','W','S',0};
115
116     hr = IStorage_OpenStream(pStorage, windowsW, NULL, STGM_READ, 0, &pStream);
117     if (FAILED(hr))
118         return FALSE;
119
120     /* jump past the #WINDOWS header */
121     liOffset.QuadPart = sizeof(DWORD) * 2;
122
123     hr = IStream_Seek(pStream, liOffset, STREAM_SEEK_SET, NULL);
124     if (FAILED(hr)) goto done;
125
126     /* read the HH_WINTYPE struct data */
127     hr = IStream_Read(pStream, pHHWinType, sizeof(*pHHWinType), &cbRead);
128     if (FAILED(hr)) goto done;
129
130     /* convert the #STRINGS offsets to actual strings */
131     pHHWinType->pszType = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszType);
132     pHHWinType->pszCaption = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszCaption);
133     pHHWinType->pszToc = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszToc);
134     pHHWinType->pszIndex = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszIndex);
135     pHHWinType->pszFile = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszFile);
136     pHHWinType->pszHome = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszHome);
137     pHHWinType->pszJump1 = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszJump1);
138     pHHWinType->pszJump2 = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszJump2);
139     pHHWinType->pszUrlJump1 = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszUrlJump1);
140     pHHWinType->pszUrlJump2 = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszUrlJump2);
141     
142     /* FIXME: pszCustomTabs is a list of multiple zero-terminated strings so ReadString won't
143      * work in this case
144      */
145 #if 0
146     pHHWinType->pszCustomTabs = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszCustomTabs);
147 #endif
148
149 done:
150     IStream_Release(pStream);
151
152     return SUCCEEDED(hr);
153 }
154
155 /* Opens the CHM file for reading */
156 BOOL CHM_OpenCHM(CHMInfo *pChmInfo, LPCWSTR szFile)
157 {
158     pChmInfo->szFile = szFile;
159
160     if (FAILED(CoCreateInstance(&CLSID_ITStorage, NULL, CLSCTX_INPROC_SERVER,
161                                 &IID_IITStorage, (void **) &pChmInfo->pITStorage)))
162         return FALSE;
163
164     if (FAILED(IITStorage_StgOpenStorage(pChmInfo->pITStorage, szFile, NULL,
165                                          STGM_READ | STGM_SHARE_DENY_WRITE,
166                                          NULL, 0, &pChmInfo->pStorage)))
167         return FALSE;
168
169     return TRUE;
170 }
171
172 void CHM_CloseCHM(CHMInfo *pCHMInfo)
173 {
174     IITStorage_Release(pCHMInfo->pITStorage);
175     IStorage_Release(pCHMInfo->pStorage);
176 }
177
178 /* Creates a Url of a CHM file that can be used with WB_Navigate */
179 void CHM_CreateITSUrl(CHMInfo *pChmInfo, LPCWSTR szIndex, LPWSTR szUrl)
180 {
181     static const WCHAR formatW[] = {
182         'i','t','s',':','%','s',':',':','%','s',0
183     };
184
185     wsprintfW(szUrl, formatW, pChmInfo->szFile, szIndex);
186 }