| 1 |
/* |
| 2 |
* strlist.cpp: Implementation of the StringList class. |
| 3 |
* |
| 4 |
* This file is a part of NSIS. |
| 5 |
* |
| 6 |
* Copyright (C) 1999-2026 Nullsoft and Contributors |
| 7 |
* |
| 8 |
* Licensed under the zlib/libpng license (the "License"); |
| 9 |
* you may not use this file except in compliance with the License. |
| 10 |
* |
| 11 |
* Licence details can be found in the file COPYING. |
| 12 |
* |
| 13 |
* This software is provided 'as-is', without any express or implied |
| 14 |
* warranty. |
| 15 |
* |
| 16 |
* Unicode support and Doxygen comments by Jim Park -- 08/01/2007 |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "strlist.h" |
| 20 |
#include "utf.h" |
| 21 |
#include "util.h" // For PrintColorFmtMsg_ERR |
| 22 |
|
| 23 |
#ifdef _UNICODE |
| 24 |
char* convert_processed_string_to_ansi(char *out, const TCHAR *in, WORD codepage); // defined in build.cpp |
| 25 |
#endif |
| 26 |
|
| 27 |
static inline bool byte_rev_match(const void*ptr1, const void*ptr2, size_t cb) |
| 28 |
{ |
| 29 |
char *p1 = (char*) ptr1, *p2 = (char*) ptr2; |
| 30 |
for(; cb--;) if (p1[cb] != p2[cb]) return false; |
| 31 |
return true; |
| 32 |
} |
| 33 |
|
| 34 |
unsigned int ExeHeadStringList::getnum() const |
| 35 |
{ |
| 36 |
char *p = (char*) m_gr.get(); |
| 37 |
if (!p) return 1; // The empty string always exists |
| 38 |
unsigned int num = 1; |
| 39 |
size_t cbList = gettotalsize(), cb = 0, pos; |
| 40 |
pos = 1 + !!m_wide, p += pos; // Skip empty string |
| 41 |
if (m_wide) |
| 42 |
{ |
| 43 |
for(;;) |
| 44 |
{ |
| 45 |
if ((pos+=cb) >= cbList) break; |
| 46 |
cb = (StrLenUTF16(p+=cb) + 1) * 2, ++num; |
| 47 |
} |
| 48 |
} |
| 49 |
else |
| 50 |
{ |
| 51 |
for(;;) |
| 52 |
{ |
| 53 |
if ((pos+=cb) >= cbList) break; |
| 54 |
cb = strlen(p+=cb) + 1, ++num; |
| 55 |
} |
| 56 |
} |
| 57 |
return num; |
| 58 |
} |
| 59 |
|
| 60 |
bool ExeHeadStringList::get(unsigned int offset, tstring&outstr) const |
| 61 |
{ |
| 62 |
if (0 == offset) |
| 63 |
{ |
| 64 |
outstr.assign(_T("")); |
| 65 |
return true; |
| 66 |
} |
| 67 |
char *p = (char*) m_gr.get(); |
| 68 |
unsigned int cbList = gettotalsize(); |
| 69 |
if (p && cbList < offset) |
| 70 |
{ |
| 71 |
if (m_wide) |
| 72 |
StrSetUTF16LE(outstr,&p[offset*WIDEDIV]); |
| 73 |
else |
| 74 |
// BUGBUG: There is no way for us to know the correct codepage |
| 75 |
outstr = CtoTString(&p[offset]); |
| 76 |
return true; |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
* find() finds the offset where the string is stored, returns -1 if not found. |
| 83 |
* It only compares raw byte values, there is no Unicode normalization handling. |
| 84 |
* If ppBufMB is non-null you must delete[] it (Only valid when m_wide is false)! |
| 85 |
*/ |
| 86 |
unsigned int ExeHeadStringList::find(const TCHAR *str, WORD codepage, bool processed, char**ppBufMB) const |
| 87 |
{ |
| 88 |
if (m_wide && *str) |
| 89 |
{ |
| 90 |
WCToUTF16LEHlpr cnv; |
| 91 |
if (!cnv.Create(str)) return -1; |
| 92 |
unsigned int pos = find(cnv.Get(),StrLenUTF16(cnv.Get()),codepage,processed,ppBufMB); |
| 93 |
cnv.Destroy(); |
| 94 |
return pos; |
| 95 |
} |
| 96 |
else |
| 97 |
{ |
| 98 |
return find(str,(unsigned int)_tcslen(str),codepage,processed,ppBufMB); |
| 99 |
} |
| 100 |
} |
| 101 |
unsigned int ExeHeadStringList::find(const void *ptr, unsigned int cchF, WORD codepage, bool processed, char**ppBufMB) const |
| 102 |
{ |
| 103 |
const wchar_t *find = (const wchar_t*) ptr; // Data is: m_wide ? UTF16LE : wchar_t |
| 104 |
if (!*find) return 0; // The empty string is always first (ExeHead uses string block offset 0 to indicate no parameter present in some places). |
| 105 |
|
| 106 |
char *p = (char*) m_gr.get(); |
| 107 |
if (!p) return -1; |
| 108 |
|
| 109 |
unsigned int cbF = ++cchF * 2; // Include \0 as part of cchF, * 2 for UTF16 & DBCS. |
| 110 |
char *bufMB = 0; |
| 111 |
if (!m_wide) |
| 112 |
{ |
| 113 |
unsigned int cbMB; |
| 114 |
bufMB = new char[cbF]; |
| 115 |
if (processed) |
| 116 |
{ |
| 117 |
char *pTmp = convert_processed_string_to_ansi(bufMB,find,codepage); |
| 118 |
cbMB = (int)(pTmp ? pTmp - bufMB : 0); |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
cbMB = WideCharToMultiByte(codepage,0,find,cchF,bufMB,cbF,0,0); |
| 123 |
} |
| 124 |
#ifndef NDEBUG |
| 125 |
if (!cbMB) |
| 126 |
{ |
| 127 |
const TCHAR *fmt = _T("Unable to convert%")NPRIns _T(" string \"%")NPRIs _T("\" to codepage %u\n"); |
| 128 |
PrintColorFmtMsg_ERR(fmt,(processed ? " processed" : ""),find,codepage); |
| 129 |
} |
| 130 |
#endif |
| 131 |
assert(cbMB); |
| 132 |
cbF = cbMB, find = (const wchar_t*) bufMB; |
| 133 |
} |
| 134 |
|
| 135 |
size_t cbList = gettotalsize(), cb = 0, retval = -1, pos; |
| 136 |
pos = 1 + !!m_wide, p += pos; // Skip empty string |
| 137 |
if (m_wide) |
| 138 |
{ |
| 139 |
for(;;) |
| 140 |
{ |
| 141 |
if ((pos+=cb) >= cbList) break; |
| 142 |
cb = (StrLenUTF16(p+=cb) + 1) * 2; |
| 143 |
if (cb < cbF) continue; |
| 144 |
size_t cbOfs = cb - cbF; |
| 145 |
if (byte_rev_match(p + cbOfs,find,cbF)) { retval = (pos + cbOfs) / WIDEDIV; break; } |
| 146 |
} |
| 147 |
} |
| 148 |
else |
| 149 |
{ |
| 150 |
for(;;) |
| 151 |
{ |
| 152 |
if ((pos+=cb) >= cbList) break; |
| 153 |
cb = (unsigned int) strlen(p+=cb) + 1; |
| 154 |
if (cb < cbF) continue; |
| 155 |
size_t cbOfs = cb - cbF; |
| 156 |
if (byte_rev_match(p + cbOfs,find,cbF)) { retval = (pos + cbOfs); break; } |
| 157 |
} |
| 158 |
if (ppBufMB) |
| 159 |
*ppBufMB = bufMB; |
| 160 |
else |
| 161 |
delete[] bufMB; |
| 162 |
} |
| 163 |
// -1 is a valid magic return value but we must avoid the truncation check in truncate_cast |
| 164 |
return retval != (size_t)(-1) ? truncate_cast(unsigned int,retval) : (unsigned int) retval; |
| 165 |
} |
| 166 |
|
| 167 |
int ExeHeadStringList::add(const TCHAR *str, WORD codepage, bool processed) |
| 168 |
{ |
| 169 |
char *p = (char*) m_gr.get(); |
| 170 |
if (!p) |
| 171 |
{ |
| 172 |
if (!*str) return 0; // Delay allocating the empty string |
| 173 |
char *&zero = p, cb = 1 + !!m_wide; |
| 174 |
unsigned int pos = m_gr.add(&zero,cb); |
| 175 |
assert(0 == pos); |
| 176 |
} |
| 177 |
|
| 178 |
char *bufMB = 0; |
| 179 |
unsigned int pos = find(str,codepage,processed,m_wide ? 0 : &bufMB); |
| 180 |
if ((unsigned int)-1 != pos) |
| 181 |
{ |
| 182 |
delete[] bufMB; |
| 183 |
return pos; |
| 184 |
} |
| 185 |
|
| 186 |
if (m_wide) |
| 187 |
{ |
| 188 |
WCToUTF16LEHlpr cnv; |
| 189 |
if (!cnv.Create(str)) throw std::bad_alloc(); |
| 190 |
pos = m_gr.add(cnv.Get(),cnv.GetSize()) / WIDEDIV; |
| 191 |
cnv.Destroy(); |
| 192 |
} |
| 193 |
else |
| 194 |
{ |
| 195 |
unsigned int cbMB = (unsigned int) strlen(bufMB) + 1; |
| 196 |
pos = m_gr.add(bufMB,cbMB); |
| 197 |
delete[] bufMB; |
| 198 |
} |
| 199 |
return pos; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
int StringList::add(const TCHAR *str, int case_sensitive) |
| 204 |
{ |
| 205 |
int a=find(str,case_sensitive); |
| 206 |
if (a >= 0 && case_sensitive!=-1) return a; |
| 207 |
return m_gr.add(str,truncate_cast(int,(_tcslen(str)+1)*sizeof(TCHAR)))/sizeof(TCHAR); |
| 208 |
} |
| 209 |
|
| 210 |
// use 2 for case sensitive end-of-string matches too |
| 211 |
int StringList::find(const TCHAR *str, int case_sensitive, int *idx/*=NULL*/) const // returns -1 if not found |
| 212 |
{ |
| 213 |
const TCHAR *s=get(); |
| 214 |
int ml=getcount(); |
| 215 |
int offs=0; |
| 216 |
|
| 217 |
size_t str_slen = _tcslen(str); |
| 218 |
size_t offs_slen; |
| 219 |
|
| 220 |
if (idx) *idx=0; |
| 221 |
while (offs < ml) |
| 222 |
{ |
| 223 |
// Check if the whole string matches str. |
| 224 |
if ((case_sensitive && !_tcscmp(s+offs,str)) || |
| 225 |
(!case_sensitive && !_tcsicmp(s+offs,str))) |
| 226 |
{ |
| 227 |
return offs; |
| 228 |
} |
| 229 |
|
| 230 |
offs_slen = _tcslen(s+offs); |
| 231 |
|
| 232 |
// Check if just the end of the string matches str. |
| 233 |
if (case_sensitive==2 && |
| 234 |
str_slen < offs_slen && // check for end of string |
| 235 |
!_tcscmp(s + offs + offs_slen - str_slen,str)) |
| 236 |
{ |
| 237 |
return truncate_cast(int,offs + offs_slen - str_slen); |
| 238 |
} |
| 239 |
offs += truncate_cast(int,offs_slen + 1); |
| 240 |
|
| 241 |
if (idx) (*idx)++; |
| 242 |
} |
| 243 |
return -1; |
| 244 |
} |
| 245 |
|
| 246 |
// pos is the position in TCHARs, not bytes. |
| 247 |
void StringList::delbypos(int pos) |
| 248 |
{ |
| 249 |
TCHAR *s=(TCHAR*) m_gr.get(); |
| 250 |
int len=(int)_tcslen(s+pos)+1; |
| 251 |
|
| 252 |
if (pos+len < getcount()) |
| 253 |
{ |
| 254 |
// Move everything after the string position to the current position. |
| 255 |
memcpy(s+pos,s+pos+len, (getcount()-pos+len)*sizeof(TCHAR)); |
| 256 |
} |
| 257 |
m_gr.resize(m_gr.getlen()-len*sizeof(TCHAR)); |
| 258 |
} |
| 259 |
|
| 260 |
// idx corresponds to the nth string in the list. |
| 261 |
int StringList::idx2pos(int idx) const |
| 262 |
{ |
| 263 |
TCHAR *s=(TCHAR*) m_gr.get(); |
| 264 |
int offs=0; |
| 265 |
int cnt=0; |
| 266 |
if (idx>=0) while (offs < getcount()) |
| 267 |
{ |
| 268 |
if (cnt++ == idx) return offs; |
| 269 |
offs+=(int)_tcslen(s+offs)+1; |
| 270 |
} |
| 271 |
return -1; |
| 272 |
} |
| 273 |
|
| 274 |
int StringList::getnum() const |
| 275 |
{ |
| 276 |
TCHAR *s=(TCHAR*) m_gr.get(); |
| 277 |
int ml=getcount(); |
| 278 |
int offs=0; |
| 279 |
int idx=0; |
| 280 |
while (offs < ml) |
| 281 |
{ |
| 282 |
offs+=(int)_tcslen(s+offs)+1; |
| 283 |
idx++; |
| 284 |
} |
| 285 |
return idx; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
// ========== |
| 290 |
// DefineList |
| 291 |
// ========== |
| 292 |
|
| 293 |
/** |
| 294 |
* Since the SortedStringList base class handles the memory for .name values, |
| 295 |
* this destructor handles all the .value values in struct define. |
| 296 |
*/ |
| 297 |
DefineList::~DefineList() |
| 298 |
{ |
| 299 |
struct define *s=(struct define*) m_gr.get(); |
| 300 |
int num=m_gr.getlen()/sizeof(struct define); |
| 301 |
|
| 302 |
for (int i=0; i<num; i++) free(s[i].value); |
| 303 |
} |
| 304 |
|
| 305 |
int DefineList::addn(const TCHAR *name, size_t maxvallen, const TCHAR *value) |
| 306 |
{ |
| 307 |
int pos=SortedStringList<struct define>::add(name); |
| 308 |
if (pos == -1) return 1; |
| 309 |
|
| 310 |
size_t cbVal = ++maxvallen * sizeof(TCHAR); |
| 311 |
TCHAR **newvalue=&(((struct define*) m_gr.get())[pos].value); |
| 312 |
|
| 313 |
*newvalue = (TCHAR*)malloc(cbVal); |
| 314 |
if (!(*newvalue)) |
| 315 |
{ |
| 316 |
extern int g_display_errors; |
| 317 |
extern void quit(); |
| 318 |
if (g_display_errors) |
| 319 |
{ |
| 320 |
PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: DefineList malloc(%lu) failed.\n"), truncate_cast(unsigned long,cbVal)); |
| 321 |
} |
| 322 |
quit(); |
| 323 |
} |
| 324 |
my_strncpy(*newvalue, value, maxvallen); |
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
int DefineList::add(const TCHAR *name, const TCHAR *value/*=_T("")*/) |
| 329 |
{ |
| 330 |
return addn(name, _tcslen(value), value); |
| 331 |
} |
| 332 |
|
| 333 |
int DefineList::set(const TCHAR *name, const TCHAR *value/*=_T("")*/) |
| 334 |
{ |
| 335 |
del(name); |
| 336 |
return add(name, value); |
| 337 |
} |
| 338 |
|
| 339 |
int DefineList::set_si32(const TCHAR *name, long value) |
| 340 |
{ |
| 341 |
TCHAR buf[50]; |
| 342 |
_stprintf(buf, _T("%ld"), value); |
| 343 |
return set(name, buf); |
| 344 |
} |
| 345 |
|
| 346 |
int DefineList::set_ui32(const TCHAR *name, unsigned long value) |
| 347 |
{ |
| 348 |
TCHAR buf[50]; |
| 349 |
_stprintf(buf, _T("%lu"), value); |
| 350 |
return set(name, buf); |
| 351 |
} |
| 352 |
|
| 353 |
TCHAR *DefineList::find(const TCHAR *name) |
| 354 |
{ |
| 355 |
int v=SortedStringList<struct define>::find(name); |
| 356 |
if (v==-1) |
| 357 |
{ |
| 358 |
return NULL; |
| 359 |
} |
| 360 |
return ((struct define*) m_gr.get())[v].value; |
| 361 |
} |
| 362 |
|
| 363 |
// returns 0 on success, 1 otherwise |
| 364 |
int DefineList::del(const TCHAR *str) |
| 365 |
{ |
| 366 |
int pos=SortedStringList<struct define>::find(str); |
| 367 |
if (pos==-1) return 1; |
| 368 |
|
| 369 |
struct define *db=(struct define *) m_gr.get(); |
| 370 |
free(db[pos].value); |
| 371 |
delbypos(pos); |
| 372 |
|
| 373 |
return 0; |
| 374 |
} |
| 375 |
|
| 376 |
int DefineList::getnum() |
| 377 |
{ |
| 378 |
return m_gr.getlen()/sizeof(define); |
| 379 |
} |
| 380 |
|
| 381 |
TCHAR *DefineList::getname(int num) |
| 382 |
{ |
| 383 |
if ((unsigned int)getnum() <= (unsigned int)num) |
| 384 |
return 0; |
| 385 |
return ((struct define*) m_gr.get())[num].name; |
| 386 |
} |
| 387 |
|
| 388 |
TCHAR *DefineList::getvalue(int num) |
| 389 |
{ |
| 390 |
if ((unsigned int)getnum() <= (unsigned int)num) |
| 391 |
return 0; |
| 392 |
return ((struct define*) m_gr.get())[num].value; |
| 393 |
} |
| 394 |
|
| 395 |
// ============== |
| 396 |
// FastStringList |
| 397 |
// ============== |
| 398 |
|
| 399 |
int FastStringList::add(const TCHAR *name, int case_sensitive/*=0*/) |
| 400 |
{ |
| 401 |
int pos = SortedStringListND<struct string_t>::add(name, case_sensitive); |
| 402 |
if (pos == -1) return -1; |
| 403 |
return ((struct string_t*) m_gr.get())[pos].name; |
| 404 |
} |
| 405 |
|
| 406 |
TCHAR *FastStringList::get() const |
| 407 |
{ |
| 408 |
return (TCHAR*)m_strings.get(); |
| 409 |
} |
| 410 |
|
| 411 |
int FastStringList::getcount() const |
| 412 |
{ |
| 413 |
return m_strings.getlen()/sizeof(TCHAR); |
| 414 |
} |
| 415 |
|
| 416 |
int FastStringList::getnum() const |
| 417 |
{ |
| 418 |
return m_gr.getlen()/sizeof(struct string_t); |
| 419 |
} |
| 420 |
|