Commit e717dcc8 authored by Jacek Caban's avatar Jacek Caban Committed by Alexandre Julliard

hhctrl.ocx: Rewrite reading strings from #STRINGS section.

parent 07ba4577
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
* CHM Utility API * CHM Utility API
* *
* Copyright 2005 James Hawkins * Copyright 2005 James Hawkins
* Copyright 2007 Jacek Caban
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
...@@ -20,57 +21,55 @@ ...@@ -20,57 +21,55 @@
#include "hhctrl.h" #include "hhctrl.h"
/* Reads a string from the #STRINGS section in the CHM file */ #include "wine/debug.h"
static LPWSTR CHM_ReadString(CHMInfo *pChmInfo, DWORD dwOffset)
{
LARGE_INTEGER liOffset;
IStorage *pStorage = pChmInfo->pStorage;
IStream *pStream;
DWORD cbRead;
ULONG iPos;
DWORD dwSize;
LPSTR szString;
LPWSTR stringW;
const int CB_READ_BLOCK = 64; WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
static const WCHAR stringsW[] = {'#','S','T','R','I','N','G','S',0};
dwSize = CB_READ_BLOCK; #define BLOCK_BITS 12
szString = hhctrl_alloc(dwSize); #define BLOCK_SIZE (1 << BLOCK_BITS)
#define BLOCK_MASK (BLOCK_SIZE-1)
if (FAILED(IStorage_OpenStream(pStorage, stringsW, NULL, STGM_READ, 0, &pStream))) /* Reads a string from the #STRINGS section in the CHM file */
static LPCSTR GetChmString(CHMInfo *chm, DWORD offset)
{
if(!chm->strings_stream)
return NULL; return NULL;
liOffset.QuadPart = dwOffset; if(chm->strings_size <= (offset >> BLOCK_BITS)) {
if(chm->strings)
chm->strings = hhctrl_realloc_zero(chm->strings,
chm->strings_size = ((offset >> BLOCK_BITS)+1)*sizeof(char*));
else
chm->strings = hhctrl_alloc_zero(
chm->strings_size = ((offset >> BLOCK_BITS)+1)*sizeof(char*));
if (FAILED(IStream_Seek(pStream, liOffset, STREAM_SEEK_SET, NULL)))
{
IStream_Release(pStream);
return NULL;
} }
while (SUCCEEDED(IStream_Read(pStream, szString, CB_READ_BLOCK, &cbRead))) if(!chm->strings[offset >> BLOCK_BITS]) {
{ LARGE_INTEGER pos;
if (!cbRead) DWORD read;
return NULL; HRESULT hres;
for (iPos = 0; iPos < cbRead; iPos++) pos.QuadPart = offset & ~BLOCK_MASK;
{ hres = IStream_Seek(chm->strings_stream, pos, STREAM_SEEK_SET, NULL);
if (!szString[iPos]) if(FAILED(hres)) {
{ WARN("Seek failed: %08x\n", hres);
stringW = strdupAtoW(szString); return NULL;
hhctrl_free(szString);
return stringW;
}
} }
dwSize *= 2; chm->strings[offset >> BLOCK_BITS] = hhctrl_alloc(BLOCK_SIZE);
szString = hhctrl_realloc(szString, dwSize);
szString += cbRead;
}
/* didn't find a string */ hres = IStream_Read(chm->strings_stream, chm->strings[offset >> BLOCK_BITS],
BLOCK_SIZE, &read);
if(FAILED(hres)) {
WARN("Read failed: %08x\n", hres);
hhctrl_free(chm->strings[offset >> BLOCK_BITS]);
chm->strings[offset >> BLOCK_BITS] = NULL;
return NULL; return NULL;
}
}
return chm->strings[offset >> BLOCK_BITS] + (offset & BLOCK_MASK);
} }
/* Loads the HH_WINTYPE data from the CHM file /* Loads the HH_WINTYPE data from the CHM file
...@@ -103,16 +102,16 @@ BOOL CHM_LoadWinTypeFromCHM(CHMInfo *pChmInfo, HH_WINTYPEW *pHHWinType) ...@@ -103,16 +102,16 @@ BOOL CHM_LoadWinTypeFromCHM(CHMInfo *pChmInfo, HH_WINTYPEW *pHHWinType)
if (FAILED(hr)) goto done; if (FAILED(hr)) goto done;
/* convert the #STRINGS offsets to actual strings */ /* convert the #STRINGS offsets to actual strings */
pHHWinType->pszType = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszType); pHHWinType->pszType = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszType));
pHHWinType->pszCaption = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszCaption); pHHWinType->pszCaption = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszCaption));
pHHWinType->pszToc = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszToc); pHHWinType->pszToc = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszToc));
pHHWinType->pszIndex = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszIndex); pHHWinType->pszIndex = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszIndex));
pHHWinType->pszFile = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszFile); pHHWinType->pszFile = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszFile));
pHHWinType->pszHome = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszHome); pHHWinType->pszHome = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszHome));
pHHWinType->pszJump1 = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszJump1); pHHWinType->pszJump1 = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszJump1));
pHHWinType->pszJump2 = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszJump2); pHHWinType->pszJump2 = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszJump2));
pHHWinType->pszUrlJump1 = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszUrlJump1); pHHWinType->pszUrlJump1 = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszUrlJump1));
pHHWinType->pszUrlJump2 = CHM_ReadString(pChmInfo, (DWORD)pHHWinType->pszUrlJump2); pHHWinType->pszUrlJump2 = strdupAtoW(GetChmString(pChmInfo, (DWORD)pHHWinType->pszUrlJump2));
/* FIXME: pszCustomTabs is a list of multiple zero-terminated strings so ReadString won't /* FIXME: pszCustomTabs is a list of multiple zero-terminated strings so ReadString won't
* work in this case * work in this case
...@@ -130,6 +129,10 @@ done: ...@@ -130,6 +129,10 @@ done:
/* Opens the CHM file for reading */ /* Opens the CHM file for reading */
BOOL CHM_OpenCHM(CHMInfo *pChmInfo, LPCWSTR szFile) BOOL CHM_OpenCHM(CHMInfo *pChmInfo, LPCWSTR szFile)
{ {
HRESULT hres;
static const WCHAR wszSTRINGS[] = {'#','S','T','R','I','N','G','S',0};
pChmInfo->szFile = szFile; pChmInfo->szFile = szFile;
if (FAILED(CoCreateInstance(&CLSID_ITStorage, NULL, CLSCTX_INPROC_SERVER, if (FAILED(CoCreateInstance(&CLSID_ITStorage, NULL, CLSCTX_INPROC_SERVER,
...@@ -141,6 +144,16 @@ BOOL CHM_OpenCHM(CHMInfo *pChmInfo, LPCWSTR szFile) ...@@ -141,6 +144,16 @@ BOOL CHM_OpenCHM(CHMInfo *pChmInfo, LPCWSTR szFile)
NULL, 0, &pChmInfo->pStorage))) NULL, 0, &pChmInfo->pStorage)))
return FALSE; return FALSE;
hres = IStorage_OpenStream(pChmInfo->pStorage, wszSTRINGS, NULL, STGM_READ, 0,
&pChmInfo->strings_stream);
if(FAILED(hres)) {
WARN("Could not open #STRINGS stream: %08x\n", hres);
return FALSE;
}
pChmInfo->strings = NULL;
pChmInfo->strings_size = 0;
return TRUE; return TRUE;
} }
...@@ -148,4 +161,14 @@ void CHM_CloseCHM(CHMInfo *pCHMInfo) ...@@ -148,4 +161,14 @@ void CHM_CloseCHM(CHMInfo *pCHMInfo)
{ {
IITStorage_Release(pCHMInfo->pITStorage); IITStorage_Release(pCHMInfo->pITStorage);
IStorage_Release(pCHMInfo->pStorage); IStorage_Release(pCHMInfo->pStorage);
IStream_Release(pCHMInfo->strings_stream);
if(pCHMInfo->strings_size) {
int i;
for(i=0; i<pCHMInfo->strings_size; i++)
hhctrl_free(pCHMInfo->strings[i]);
}
hhctrl_free(pCHMInfo->strings);
} }
...@@ -50,6 +50,10 @@ typedef struct CHMInfo ...@@ -50,6 +50,10 @@ typedef struct CHMInfo
IITStorage *pITStorage; IITStorage *pITStorage;
IStorage *pStorage; IStorage *pStorage;
LPCWSTR szFile; LPCWSTR szFile;
IStream *strings_stream;
char **strings;
DWORD strings_size;
} CHMInfo; } CHMInfo;
...@@ -87,6 +91,11 @@ static inline void *hhctrl_realloc(void *mem, size_t len) ...@@ -87,6 +91,11 @@ static inline void *hhctrl_realloc(void *mem, size_t len)
return HeapReAlloc(GetProcessHeap(), 0, mem, len); return HeapReAlloc(GetProcessHeap(), 0, mem, len);
} }
static inline void *hhctrl_realloc_zero(void *mem, size_t len)
{
return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, len);
}
static inline BOOL hhctrl_free(void *mem) static inline BOOL hhctrl_free(void *mem)
{ {
return HeapFree(GetProcessHeap(), 0, mem); return HeapFree(GetProcessHeap(), 0, mem);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment