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