Unicode port: Support for Unicode license files

git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@6080 212acab6-be3b-0410-9dea-997c60f758d6
This commit is contained in:
wizou 2010-05-04 08:21:49 +00:00
parent f3ac7d9f93
commit 1b5e8adbb8
3 changed files with 36 additions and 12 deletions

View file

@ -269,6 +269,9 @@ typedef DWORDLONG ULONGLONG,*PULONGLONG;
# ifndef SF_RTF # ifndef SF_RTF
# define SF_RTF 0x0002 # define SF_RTF 0x0002
# endif # endif
# ifndef SF_UNICODE
# define SF_UNICODE 0x0010
# endif
#endif #endif
#ifdef __GNUC__ #ifdef __GNUC__

View file

@ -716,11 +716,23 @@ skipPage:
static DWORD dwRead; static DWORD dwRead;
DWORD CALLBACK StreamLicense(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb) DWORD CALLBACK StreamLicense(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{ {
lstrcpynA(pbBuff,(char*)dwCookie+dwRead,cb); lstrcpyn((LPTSTR)pbBuff,(LPTSTR)(dwCookie+dwRead),cb/sizeof(TCHAR));
*pcb=lstrlenA(pbBuff); *pcb=lstrlen((LPTSTR)pbBuff)*sizeof(TCHAR);
dwRead+=*pcb; dwRead+=*pcb;
return 0; return 0;
} }
#ifdef _UNICODE
// on-the-fly conversion of Unicode to ANSI (because Windows don't recognize Unicode RTF data)
DWORD CALLBACK StreamLicenseRTF(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
LONG len = lstrlen(((LPWSTR) dwCookie)+dwRead);
len = min(len, cb/sizeof(WCHAR));
*pcb=WideCharToMultiByte(CP_ACP,0,((LPWSTR) dwCookie)+dwRead,len,pbBuff,cb,NULL,NULL);
// RTF uses only ASCII characters, so we can assume "number of output bytes" = "number of source WChar consumed"
dwRead+=*pcb;
return 0;
}
#endif
static BOOL CALLBACK LicenseProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) static BOOL CALLBACK LicenseProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{ {
@ -733,9 +745,13 @@ static BOOL CALLBACK LicenseProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM
TCHAR *l = (TCHAR *)GetNSISStringNP(GetNSISTab(this_page->parms[1])); TCHAR *l = (TCHAR *)GetNSISStringNP(GetNSISTab(this_page->parms[1]));
int lt = *l; int lt = *l;
EDITSTREAM es = { EDITSTREAM es = {
(DWORD)(++l), (DWORD_PTR)(++l),
0, 0,
#ifdef _UNICODE
lt==SF_RTF?StreamLicenseRTF:StreamLicense
#else
StreamLicense StreamLicense
#endif
}; };
int selected = (this_page->flags & PF_LICENSE_SELECTED) | !(this_page->flags & PF_LICENSE_FORCE_SELECTION); int selected = (this_page->flags & PF_LICENSE_SELECTED) | !(this_page->flags & PF_LICENSE_FORCE_SELECTION);

View file

@ -825,40 +825,45 @@ int CEXEBuild::MacroExists(const TCHAR *macroname)
int CEXEBuild::LoadLicenseFile(TCHAR *file, TCHAR** pdata, LineParser &line) // caller must free *pdata, even on error result int CEXEBuild::LoadLicenseFile(TCHAR *file, TCHAR** pdata, LineParser &line) // caller must free *pdata, even on error result
{ {
unsigned int datalen; FILE *fp=FOPENTEXT(file,"rt");
FILE *fp=FOPEN(file,_T("rb"));
if (!fp) if (!fp)
{ {
ERROR_MSG(_T("%s: open failed \"%s\"\n"),line.gettoken_str(0),file); ERROR_MSG(_T("%s: open failed \"%s\"\n"),line.gettoken_str(0),file);
PRINTHELP() PRINTHELP()
} }
MANAGE_WITH(fp, fclose); MANAGE_WITH(fp, fclose);
unsigned int beginning=ftell(fp); // (we might be positionned after a BOM)
fseek(fp,0,SEEK_END); fseek(fp,0,SEEK_END);
datalen=ftell(fp); unsigned int datalen=ftell(fp)-beginning; // size of file in bytes! not a number of character
if (!datalen) if (!datalen)
{ {
ERROR_MSG(_T("%s: empty license file \"%s\"\n"),line.gettoken_str(0),file); ERROR_MSG(_T("%s: empty license file \"%s\"\n"),line.gettoken_str(0),file);
return PS_ERROR; return PS_ERROR;
} }
rewind(fp); fseek(fp,beginning,SEEK_SET);
TCHAR *data=(TCHAR*)malloc(datalen+2); //TODO: review this for Unicode support TCHAR *data=(TCHAR*)malloc((datalen+2)*sizeof(TCHAR)); // alloc enough for worst-case scenario (ANSI/UTF8 characters read in WCHARs)
if (!data) if (!data)
{ {
ERROR_MSG(_T("Internal compiler error #12345: %s malloc(%d) failed.\n"),line.gettoken_str(0),datalen+2); ERROR_MSG(_T("Internal compiler error #12345: %s malloc(%d) failed.\n"),line.gettoken_str(0),(datalen+2)*sizeof(TCHAR));
return PS_ERROR; return PS_ERROR;
} }
*pdata = data; // memory will be released by caller *pdata = data; // memory will be released by caller
TCHAR *ldata=data+1; TCHAR *ldata=data+1;
if (fread((void*)ldata,1,datalen,fp) != datalen) while (_fgetts(ldata, data+datalen+2-ldata, fp)) // _fgetts translate ANSI/UTF8 characters to TCHAR
ldata += _tcslen(ldata);
if (ferror(fp))
{ {
ERROR_MSG(_T("%s: can't read file.\n"),line.gettoken_str(0)); ERROR_MSG(_T("%s: can't read file.\n"),line.gettoken_str(0));
return PS_ERROR; return PS_ERROR;
} }
ldata[datalen]='\0'; if (!memcmp(data+1,_T("{\\rtf"),5*sizeof(TCHAR)))
if (!memcmp(ldata,"{\\rtf",5))
*data = SF_RTF; *data = SF_RTF;
else else
#ifdef _UNICODE
*data = SF_TEXT|SF_UNICODE;
#else
*data = SF_TEXT; *data = SF_TEXT;
#endif
return PS_OK; return PS_OK;
} }