VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Common/Language.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common/Language.c')
-rw-r--r--src/Common/Language.c50
1 files changed, 34 insertions, 16 deletions
diff --git a/src/Common/Language.c b/src/Common/Language.c
index b146f820..3ec95dba 100644
--- a/src/Common/Language.c
+++ b/src/Common/Language.c
@@ -9,40 +9,42 @@
#include "Language.h"
#include "Dlgcode.h"
#include "Dictionary.h"
#include "Tcdefs.h"
#include "Xml.h"
#include "../Common/Resource.h"
#ifdef TCMOUNT
#include "../Mount/Resource.h"
#endif
#ifdef VOLFORMAT
#include "../Format/Resource.h"
#endif
#ifdef SETUP
#include "../Setup/Resource.h"
#endif
+#include <Strsafe.h>
+
BOOL LocalizationActive;
int LocalizationSerialNo;
wchar_t UnknownString[1024];
static char *LanguageFileBuffer;
static HANDLE LanguageFileFindHandle = INVALID_HANDLE_VALUE;
static char PreferredLangId[6];
static char *LanguageResource;
static char *HeaderResource[2];
static char ActiveLangPackVersion[6];
static char *MapFirstLanguageFile ()
{
if (LanguageFileFindHandle != INVALID_HANDLE_VALUE)
{
FindClose (LanguageFileFindHandle);
LanguageFileFindHandle = INVALID_HANDLE_VALUE;
}
if (LanguageResource == NULL)
@@ -51,120 +53,136 @@ static char *MapFirstLanguageFile ()
LanguageResource = MapResource ("Xml", IDR_LANGUAGE, &size);
LanguageResource[size - 1] = 0;
}
return LanguageResource;
}
static char *MapNextLanguageFile ()
{
wchar_t f[TC_MAX_PATH*2], *t;
WIN32_FIND_DATAW find;
HANDLE file;
DWORD read;
if (LanguageFileFindHandle == INVALID_HANDLE_VALUE)
{
GetModuleFileNameW (NULL, f, sizeof (f) / sizeof (f[0]));
t = wcsrchr (f, L'\\');
if (t == NULL) return NULL;
-
- wcscpy (t, L"\\Language*.xml");
+
+ *t = 0;
+ StringCbCatW (f, sizeof(f), L"\\Language*.xml");
LanguageFileFindHandle = FindFirstFileW (f, &find);
}
else if (!FindNextFileW (LanguageFileFindHandle, &find))
{
FindClose (LanguageFileFindHandle);
LanguageFileFindHandle = INVALID_HANDLE_VALUE;
return NULL;
}
if (find.nFileSizeHigh != 0) return NULL;
if (LanguageFileBuffer != NULL) free (LanguageFileBuffer);
LanguageFileBuffer = malloc(find.nFileSizeLow);
if (LanguageFileBuffer == NULL) return NULL;
GetModuleFileNameW (NULL, f, sizeof (f) / sizeof(f[0]));
t = wcsrchr (f, L'\\');
- wcscpy (t + 1, find.cFileName);
+ if (t == NULL)
+ {
+ free(LanguageFileBuffer);
+ return NULL;
+ }
+
+ t[1] = 0;
+ StringCbCatW (f, sizeof(f),find.cFileName);
file = CreateFileW (f, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
- if (file == INVALID_HANDLE_VALUE) return NULL;
+ if (file == INVALID_HANDLE_VALUE)
+ {
+ free(LanguageFileBuffer);
+ return NULL;
+ }
ReadFile (file, LanguageFileBuffer, find.nFileSizeLow, &read, NULL);
CloseHandle (file);
- if (read != find.nFileSizeLow) return NULL;
+ if (read != find.nFileSizeLow)
+ {
+ free(LanguageFileBuffer);
+ return NULL;
+ }
return LanguageFileBuffer;
}
BOOL LoadLanguageFile ()
{
DWORD size;
BYTE *res;
char *xml, *header;
char langId[6] = "en", attr[32768], key[128];
BOOL defaultLangParsed = FALSE, langFound = FALSE;
WCHAR wattr[32768];
int i, intKey, len;
char *xmlElements[] = {"control", "string", 0};
#ifdef TCMOUNT
int headers[] = { IDR_COMMON_RSRC_HEADER, IDR_MOUNT_RSRC_HEADER, 0 };
#endif
#ifdef VOLFORMAT
int headers[] = { IDR_COMMON_RSRC_HEADER, IDR_FORMAT_RSRC_HEADER, 0 };
#endif
#ifdef SETUP
int headers[] = { IDR_COMMON_RSRC_HEADER, IDR_SETUP_RSRC_HEADER, 0 };
#endif
LocalizationActive = FALSE;
ActiveLangPackVersion[0] = 0;
ClearDictionaryPool ();
if (PreferredLangId[0] != 0)
- strcpy (langId, PreferredLangId);
+ StringCbCopyA (langId, sizeof(langId), PreferredLangId);
// Parse all available language files until preferred language is found
for (res = MapFirstLanguageFile (); res != NULL; res = MapNextLanguageFile ())
{
xml = (char *) res;
xml = XmlFindElement (xml, "localization");
if (!xml)
continue;
// Required TrueCrypt version
XmlGetAttributeText (xml, "prog-version", attr, sizeof (attr));
// Check version of external language file
if (defaultLangParsed && strcmp (attr, VERSION_STRING) && strcmp (attr, "DEBUG"))
{
wchar_t m[2048];
- swprintf (m, L"The installed language pack is incompatible with this version of VeraCrypt (the language pack is for VeraCrypt %hs). A newer version may be available at www.idrix.fr.\n\nTo prevent this message from being displayed, do any of the following:\n\n- Select 'Settings' > 'Language'; then select 'English' and click 'OK'.\n\n- Remove or replace the language pack with a compatible version (the language pack may reside e.g. in 'C:\\Program Files\\VeraCrypt' or '%%LOCALAPPDATA%%\\VirtualStore\\Program Files\\VeraCrypt', etc.)", attr);
+ StringCbPrintfW (m, sizeof(m), L"The installed language pack is incompatible with this version of VeraCrypt (the language pack is for VeraCrypt %hs). A newer version may be available at www.idrix.fr.\n\nTo prevent this message from being displayed, do any of the following:\n\n- Select 'Settings' > 'Language'; then select 'English' and click 'OK'.\n\n- Remove or replace the language pack with a compatible version (the language pack may reside e.g. in 'C:\\Program Files\\VeraCrypt' or '%%LOCALAPPDATA%%\\VirtualStore\\Program Files\\VeraCrypt', etc.)", attr);
MessageBoxW (NULL, m, L"VeraCrypt", MB_ICONERROR);
continue;
}
// Search language id in language file
if (defaultLangParsed)
{
while (xml = XmlFindElement (xml, "language"))
{
XmlGetAttributeText (xml, "langid", attr, sizeof (attr));
if (strcmp (attr, langId) == 0)
{
XmlGetAttributeText (xml++, "version", ActiveLangPackVersion, sizeof (ActiveLangPackVersion));
langFound = TRUE;
break;
}
xml++;
}
if (!langFound) continue;
@@ -172,41 +190,41 @@ BOOL LoadLanguageFile ()
// Create font dictionary
xml = (char *) res;
while (xml = XmlFindElement (xml, "font"))
{
XmlGetAttributeText (xml, "lang", attr, sizeof (attr));
if (!defaultLangParsed
|| strcmp (attr, langId) == 0)
{
Font font;
memset (&font, 0, sizeof (font));
XmlGetAttributeText (xml, "face", attr, sizeof (attr));
len = MultiByteToWideChar (CP_UTF8, 0, attr, -1, wattr, sizeof (wattr) / sizeof(wattr[0]));
font.FaceName = AddPoolData ((void *) wattr, len * 2);
XmlGetAttributeText (xml, "size", attr, sizeof (attr));
sscanf (attr, "%d", &font.Size);
- strcpy (attr, "font_");
+ StringCbCopyA (attr, sizeof(attr), "font_");
XmlGetAttributeText (xml, "class", attr + 5, sizeof (attr) - 5);
AddDictionaryEntry (
AddPoolData ((void *) attr, strlen (attr) + 1), 0,
AddPoolData ((void *) &font, sizeof(font)));
}
xml++;
}
// Create string and control dictionaries
for (i = 0; xmlElements[i] != 0; i++)
{
xml = (char *) res;
while (xml = XmlFindElement (xml, xmlElements[i]))
{
void *key;
void *text;
XmlGetAttributeText (xml, "lang", attr, sizeof (attr));
if (!defaultLangParsed
@@ -358,158 +376,158 @@ BOOL CALLBACK LanguageDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPa
}
if (!defaultLangFound)
defaultLangFound = TRUE;
SendDlgItemMessage (hwndDlg, IDC_LANGLIST, LB_SETITEMDATA, i, (LPARAM) id);
if (strcmp (attr, PreferredLangId) == 0)
{
char credits [10000];
WCHAR wcredits [10000];
WCHAR wversion [20];
wchar_t szVers [200];
int nLen;
SendDlgItemMessage (hwndDlg, IDC_LANGLIST, LB_SETCURSEL, i, 0);
// Language pack version
if (!ActiveLangPackVersion[0] || memcmp (ActiveLangPackVersion, "0.0.0", 5) == 0)
{
- swprintf (szVers, GetString("LANG_PACK_VERSION"), L"--");
+ StringCbPrintfW (szVers, sizeof(szVers), GetString("LANG_PACK_VERSION"), L"--");
}
else
{
nLen = MultiByteToWideChar (CP_UTF8, 0, ActiveLangPackVersion, -1, wversion, sizeof (wversion) / sizeof(wversion[0]));
if (nLen != 0 && nLen != ERROR_NO_UNICODE_TRANSLATION)
- swprintf (szVers, GetString("LANG_PACK_VERSION"), wversion);
+ StringCbPrintfW (szVers, sizeof(szVers),GetString("LANG_PACK_VERSION"), wversion);
}
SetWindowTextW (GetDlgItem (hwndDlg, IDC_LANGPACK_VERSION), szVers);
// Translator credits
XmlGetAttributeText (xml, "translators", credits, sizeof (credits));
nLen = MultiByteToWideChar (CP_UTF8, 0, credits, -1, wcredits, sizeof (wcredits) / sizeof(wcredits[0]));
if (nLen != 0 && nLen != ERROR_NO_UNICODE_TRANSLATION)
{
SetWindowTextW (GetDlgItem (hwndDlg, IDC_LANGPACK_CREDITS), wcredits);
}
}
- strcpy (lastLangId, attr);
+ StringCbCopyA (lastLangId, sizeof(lastLangId),attr);
langCount++;
}
}
xml++;
}
}
if (lParam == 1)
{
// Auto mode
if (langCount < 2)
EndDialog (hwndDlg, IDCANCEL);
if (langCount == 2)
- strcpy (PreferredLangId, lastLangId);
+ StringCbCopyA (PreferredLangId, sizeof(PreferredLangId), lastLangId);
EndDialog (hwndDlg, IDOK);
}
return 1;
}
case WM_COMMAND:
if (lw == IDOK || hw == LBN_DBLCLK)
{
int i = SendDlgItemMessage (hwndDlg, IDC_LANGLIST, LB_GETCURSEL, 0, 0);
if (i >= 0)
{
int id = SendDlgItemMessage (hwndDlg, IDC_LANGLIST, LB_GETITEMDATA, i, 0);
if (id != LB_ERR)
{
char l[6];
// Decode language id from LPARAM
l[0] = (char) id;
l[1] = (char) (id >> 8);
l[2] = 0;
if ((id & 0xffff0000) != 0)
{
l[2] = '-';
l[3] = (char) (id >> 16);
l[4] = id >> 24;
l[5] = 0;
}
if (SendDlgItemMessage (hwndDlg, IDC_LANGLIST, LB_GETCOUNT, 0, 0) > 1)
- strcpy (PreferredLangId, l);
+ StringCbCopyA (PreferredLangId, sizeof(PreferredLangId), l);
}
}
EndDialog (hwndDlg, IDOK);
return 1;
}
if (lw == IDCANCEL)
{
EndDialog (hwndDlg, lw);
return 1;
}
if (lw == IDC_GET_LANG_PACKS)
{
char tmpstr [256];
if (strlen (ActiveLangPackVersion) > 0 && strlen (GetPreferredLangId()) > 0)
- sprintf (tmpstr, "&langpackversion=%s&lang=%s", ActiveLangPackVersion, GetPreferredLangId());
+ StringCbPrintfA (tmpstr, sizeof(tmpstr), "&langpackversion=%s&lang=%s", ActiveLangPackVersion, GetPreferredLangId());
else
tmpstr[0] = 0;
Applink ("localizations", TRUE, tmpstr);
return 1;
}
return 0;
}
return 0;
}
char *GetPreferredLangId ()
{
return PreferredLangId;
}
void SetPreferredLangId (char *langId)
{
- strncpy (PreferredLangId, langId, 5);
+ StringCbCopyA (PreferredLangId, sizeof(PreferredLangId), langId);
}
char *GetActiveLangPackVersion ()
{
return ActiveLangPackVersion;
}
wchar_t *GetString (const char *stringId)
{
WCHAR *str = (WCHAR *) GetDictionaryValue (stringId);
if (str != NULL) return str;
- wsprintfW (UnknownString, UNKNOWN_STRING_ID L"%hs" UNKNOWN_STRING_ID, stringId);
+ StringCbPrintfW (UnknownString, sizeof(UnknownString), UNKNOWN_STRING_ID L"%hs" UNKNOWN_STRING_ID, stringId);
return UnknownString;
}
Font *GetFont (char *fontType)
{
return (Font *) GetDictionaryValue (fontType);
}