home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Source / ResourceVersionInfo.cpp < prev    next >
C/C++ Source or Header  |  2004-01-30  |  8KB  |  270 lines

  1. // ResourceVersionInfo.cpp: implementation of the CResourceVersionInfo class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "Platform.h"
  6. #include "build.h"
  7.  
  8. #include "ResourceVersionInfo.h"
  9. #ifdef NSIS_SUPPORT_VERSION_INFO
  10. /*
  11. int ValidCodePages[] = {
  12. 437, 708, 709, 710, 720, 737, 775, 850, 852, 855, 85, 86, 86, 86, 86, 864,
  13. 865, 866, 869, 874, 932, 936, 949, 950, 1200, 1250, 1251, 1252, 1253, 1254,
  14. 1255, 1256, 1257, 1258, 20000, 20001, 20002, 20003, 20004, 20005, 20127, 20261,
  15. 20269, 20866, 21027, 21866, 28591, 28592, 28593, 28594, 28595, 28596, 28597, 28598,
  16. 28599, 29001, 1361, 0 };
  17. */
  18. //////////////////////////////////////////////////////////////////////
  19. // Construction/Destruction
  20. //////////////////////////////////////////////////////////////////////
  21. CResourceVersionInfo::CResourceVersionInfo()
  22. {
  23.     memset(&m_FixedInfo, 0, sizeof(VS_FIXEDFILEINFO));
  24.     m_FixedInfo.dwSignature = 0xFEEF04BD;
  25.     m_FixedInfo.dwFileOS = VOS__WINDOWS32;
  26.     m_FixedInfo.dwFileType = VFT_APP;
  27.  
  28.     // Detect local codepage and language
  29.     WORD Lang = GetSystemDefaultLangID();
  30.     WORD CodePage = GetACP();
  31. /*
  32.     SetKeyValue(Lang, CodePage, "Comments", "Portuguese");
  33.     SetKeyValue(Lang, CodePage, "FileVersion", "1.2");
  34.     SetKeyValue(Lang, CodePage, "FileDescription", "Soft");
  35.     SetKeyValue(Lang, CodePage, "LegalCopyright", "My");
  36.     SetKeyValue(Lang, CodePage, "InternalName", "My");
  37.     SetKeyValue(Lang, CodePage, "CompanyName", "rats");
  38.     SetKeyValue(Lang, CodePage, "ProductVersion", "ProductVersion");
  39.  
  40.     Lang = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
  41.     SetKeyValue(Lang, CodePage, "Comments", "English");
  42.     SetKeyValue(Lang, CodePage, "FileVersion", "1.2");
  43.     SetKeyValue(Lang, CodePage, "FileDescription", "Soft");
  44.     SetKeyValue(Lang, CodePage, "LegalCopyright", "My");
  45.     SetKeyValue(Lang, CodePage, "InternalName", "My");
  46.     SetKeyValue(Lang, CodePage, "CompanyName", "rats");
  47.     SetKeyValue(Lang, CodePage, "ProductVersion", "ProductVersion");
  48. */
  49. }
  50.  
  51. CResourceVersionInfo::~CResourceVersionInfo()
  52. {
  53.     
  54. }
  55.  
  56. void CResourceVersionInfo::SetFileFlags(int Value)
  57. {
  58.     m_FixedInfo.dwFileFlags = (m_FixedInfo.dwFileFlags & ~(m_FixedInfo.dwFileFlagsMask)) || Value;    
  59. }
  60.  
  61. void CResourceVersionInfo::SetFileVersion(int HighPart, int LowPart)
  62. {
  63.     m_FixedInfo.dwFileVersionLS = LowPart;
  64.     m_FixedInfo.dwFileVersionMS = HighPart;
  65. }
  66.  
  67. void CResourceVersionInfo::SetProductVersion(int HighPart, int LowPart)
  68. {
  69.     m_FixedInfo.dwProductVersionLS = LowPart;
  70.     m_FixedInfo.dwProductVersionMS = HighPart;
  71. }
  72.  
  73. // Util function - must be freeded
  74. WCHAR* StrToWstrAlloc(const char* istr, int codepage)
  75. {
  76.   int strSize = MultiByteToWideChar(codepage, 0, istr, -1, 0, 0);
  77.   WCHAR* wstr = new WCHAR[strSize];
  78.   MultiByteToWideChar(codepage, 0, istr, -1, wstr, strSize);
  79.   return wstr;
  80. }
  81.  
  82. int GetVersionHeader (LPSTR &p, WORD &wLength, WORD &wValueLength, WORD &wType)
  83. {
  84.     WCHAR *szKey;
  85.     char * baseP;
  86.     
  87.     baseP = p;
  88.     wLength = *(WORD*)p;
  89.     p += sizeof(WORD);
  90.     wValueLength = *(WORD*)p;
  91.     p += sizeof(WORD);
  92.     wType = *(WORD*)p;
  93.     p += sizeof(WORD);
  94.     szKey = (WCHAR*)p;
  95.     p += (wcslen(szKey) + 1) * sizeof (WCHAR);
  96.     while ( ((long)p % 4) != 0 )
  97.         p++;
  98.     return p - baseP;    
  99. }
  100.  
  101. DWORD ZEROS = 0;
  102.  
  103. void PadStream (GrowBuf &strm)
  104. {
  105.     if ( (strm.getlen() % 4) != 0 )
  106.         strm.add (&ZEROS, 4 - (strm.getlen() % 4));
  107. }
  108.  
  109. void SaveVersionHeader (GrowBuf &strm, WORD wLength, WORD wValueLength, WORD wType, const WCHAR *key, void *value)
  110. {
  111.     WORD valueLen;
  112.     WORD keyLen;
  113.     
  114.     strm.add (&wLength, sizeof (wLength));
  115.     
  116.     strm.add (&wValueLength, sizeof (wValueLength));
  117.     strm.add (&wType, sizeof (wType));
  118.     keyLen = (wcslen(key) + 1) * sizeof (WCHAR);
  119.     strm.add ((void*)key, keyLen);
  120.     
  121.     PadStream(strm);
  122.     
  123.     if ( wValueLength > 0 )
  124.     {
  125.         valueLen = wValueLength;
  126.         if ( wType == 1 )
  127.             valueLen = valueLen * sizeof (WCHAR);
  128.         strm.add (value, valueLen);
  129.     }
  130. }
  131.  
  132. void CResourceVersionInfo::ExportToStream(GrowBuf &strm, int Index)
  133. {
  134.     DWORD v;
  135.     WORD wSize;  
  136.     int p, p1;
  137.     WCHAR *KeyName, *KeyValue;
  138.  
  139.     strm.resize(0);
  140.     SaveVersionHeader (strm, 0, sizeof (VS_FIXEDFILEINFO), 0, L"VS_VERSION_INFO", &m_FixedInfo);
  141.     
  142.     DefineList *pChildStrings = m_ChildStringLists.get_strings(Index);
  143.     if ( pChildStrings->getnum() > 0 )
  144.     {
  145.       GrowBuf stringInfoStream;
  146.       int codepage = m_ChildStringLists.get_codepage(Index);
  147.       LANGID langid = m_ChildStringLists.get_lang(Index);
  148.       char Buff[16];
  149.       sprintf(Buff, "%04x%04x", langid, codepage);
  150.       KeyName = StrToWstrAlloc(Buff, CP_ACP);
  151.       SaveVersionHeader (stringInfoStream, 0, 0, 0, KeyName, &ZEROS);
  152.       delete [] KeyName;
  153.       
  154.       for ( int i = 0; i < pChildStrings->getnum(); i++ )
  155.       {
  156.         PadStream (stringInfoStream);
  157.         
  158.         p = stringInfoStream.getlen();
  159.         KeyName = StrToWstrAlloc(pChildStrings->getname(i), codepage);
  160.         KeyValue = StrToWstrAlloc(pChildStrings->getvalue(i), codepage);
  161.         SaveVersionHeader (stringInfoStream, 0, wcslen(KeyValue) + 1, 1, KeyName, (void*)KeyValue);
  162.         delete [] KeyName;
  163.         delete [] KeyValue;
  164.         wSize = stringInfoStream.getlen() - p;
  165.         
  166.         *(WORD*)((PBYTE)stringInfoStream.get()+p)=wSize;
  167.       }
  168.       
  169.       wSize = stringInfoStream.getlen();
  170.       *(WORD*)((PBYTE)stringInfoStream.get())=wSize;
  171.       
  172.       PadStream (strm);
  173.       p = strm.getlen();
  174.       SaveVersionHeader (strm, 0, 0, 0, L"StringFileInfo", &ZEROS);
  175.       strm.add (stringInfoStream.get(), stringInfoStream.getlen());
  176.       wSize = strm.getlen() - p;
  177.       
  178.       *(WORD*)((PBYTE)strm.get()+p)=wSize;
  179.     }
  180.  
  181.     // Show all languages avaiable using Var-Translations
  182.     if ( m_ChildStringLists.getnum() > 0 )
  183.     {
  184.       PadStream (strm);
  185.       p = strm.getlen();
  186.       SaveVersionHeader (strm, 0, 0, 0, L"VarFileInfo", &ZEROS);
  187.       PadStream (strm);
  188.       
  189.       p1 = strm.getlen();
  190.       SaveVersionHeader (strm, 0, 0, 0, L"Translation", &ZEROS);
  191.       
  192.       // First add selected code language translation
  193.       v = MAKELONG(m_ChildStringLists.get_lang(Index), m_ChildStringLists.get_codepage(Index));
  194.       strm.add (&v, sizeof (v));
  195.  
  196.       for ( int k =0; k < m_ChildStringLists.getnum(); k++ )
  197.       {
  198.         if ( k != Index )
  199.         {
  200.           v = MAKELONG(m_ChildStringLists.get_lang(k), m_ChildStringLists.get_codepage(k));
  201.           strm.add (&v, sizeof (v));
  202.         }
  203.       }
  204.       
  205.       wSize = strm.getlen() - p1;
  206.       *(WORD*)((PBYTE)strm.get()+p1)=wSize;
  207.       wSize = sizeof (int) * m_ChildStringLists.getnum();
  208.       p1+=sizeof(WORD);
  209.       *(WORD*)((PBYTE)strm.get()+p1)=wSize;
  210.       
  211.       wSize = strm.getlen() - p;
  212.       *(WORD*)((PBYTE)strm.get()+p)=wSize;
  213.     }
  214.     
  215.     wSize = strm.getlen();
  216.     *(WORD*)((PBYTE)strm.get())=wSize;
  217. }
  218.  
  219. // Returns 0 if success, 1 if already defined
  220. int CResourceVersionInfo::SetKeyValue(LANGID lang_id, int codepage, char* AKeyName, char* AValue)
  221. {
  222.   int pos = m_ChildStringLists.find(lang_id, codepage);
  223.   if ( pos == -1 )
  224.   {
  225.     pos = m_ChildStringLists.add(lang_id, codepage);
  226.   }
  227.   DefineList *pStrings = m_ChildStringLists.get_strings(pos);
  228.   return pStrings->add(AKeyName, AValue);
  229. }
  230.  
  231. int CResourceVersionInfo::GetStringTablesCount()
  232. {
  233.   return m_ChildStringLists.getnum();
  234. }
  235.  
  236. LANGID CResourceVersionInfo::GetLangID(int Index)
  237. {
  238.   return m_ChildStringLists.get_lang(Index);
  239. }
  240.  
  241. int CResourceVersionInfo::GetCodePage(int Index)
  242. {
  243.   return m_ChildStringLists.get_codepage(Index);
  244. }
  245.  
  246. char *CResourceVersionInfo::FindKey(LANGID LangID, int codepage, char *pKeyName)
  247. {
  248.   int pos = m_ChildStringLists.find(LangID, codepage);
  249.   if ( pos == -1 )
  250.   {
  251.     return NULL;
  252.   }
  253.   DefineList *pStrings = m_ChildStringLists.get_strings(pos);
  254.   return pStrings->find(pKeyName);
  255. }
  256. /*
  257. bool CResourceVersionInfo::IsValidCodePage(WORD codePage )
  258. {
  259.   int *pCP = ValidCodePages;
  260.   if ( !codePage )
  261.     return false;
  262.   while ( *pCP++ )
  263.   {
  264.     if ( *pCP == codePage )
  265.       return true;  
  266.   }
  267.   return false;
  268. }
  269. */
  270. #endif