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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
|
// MethodProps.cpp
#include "StdAfx.h"
#include "../../Common/StringToInt.h"
#include "MethodProps.h"
using namespace NWindows;
UInt64 Calc_From_Val_Percents(UInt64 val, UInt64 percents)
{
// if (percents == 0) return 0;
const UInt64 q = percents / 100;
const UInt32 r = (UInt32)(percents % 100);
UInt64 res = 0;
if (q != 0)
{
if (val > (UInt64)(Int64)-1 / q)
return (UInt64)(Int64)-1;
res = val * q;
}
if (r != 0)
{
UInt64 v2;
if (val <= (UInt64)(Int64)-1 / r)
v2 = val * r / 100;
else
v2 = val / 100 * r;
res += v2;
if (res < v2)
return (UInt64)(Int64)-1;
}
return res;
}
bool StringToBool(const wchar_t *s, bool &res)
{
if (s[0] == 0 || (s[0] == '+' && s[1] == 0) || StringsAreEqualNoCase_Ascii(s, "ON"))
{
res = true;
return true;
}
if ((s[0] == '-' && s[1] == 0) || StringsAreEqualNoCase_Ascii(s, "OFF"))
{
res = false;
return true;
}
return false;
}
HRESULT PROPVARIANT_to_bool(const PROPVARIANT &prop, bool &dest)
{
switch (prop.vt)
{
case VT_EMPTY: dest = true; return S_OK;
case VT_BOOL: dest = (prop.boolVal != VARIANT_FALSE); return S_OK;
case VT_BSTR: return StringToBool(prop.bstrVal, dest) ? S_OK : E_INVALIDARG;
default: break;
}
return E_INVALIDARG;
}
unsigned ParseStringToUInt32(const UString &srcString, UInt32 &number)
{
const wchar_t *start = srcString;
const wchar_t *end;
number = ConvertStringToUInt32(start, &end);
return (unsigned)(end - start);
}
static unsigned ParseStringToUInt64(const UString &srcString, UInt64 &number)
{
const wchar_t *start = srcString;
const wchar_t *end;
number = ConvertStringToUInt64(start, &end);
return (unsigned)(end - start);
}
HRESULT ParsePropToUInt32(const UString &name, const PROPVARIANT &prop, UInt32 &resValue)
{
// =VT_UI4
// =VT_EMPTY : it doesn't change (resValue), and returns S_OK
// {stringUInt32}=VT_EMPTY
if (prop.vt == VT_UI4)
{
if (!name.IsEmpty())
return E_INVALIDARG;
resValue = prop.ulVal;
return S_OK;
}
if (prop.vt != VT_EMPTY)
return E_INVALIDARG;
if (name.IsEmpty())
return S_OK;
UInt32 v;
if (ParseStringToUInt32(name, v) != name.Len())
return E_INVALIDARG;
resValue = v;
return S_OK;
}
HRESULT ParseMtProp2(const UString &name, const PROPVARIANT &prop, UInt32 &numThreads, bool &force)
{
force = false;
UString s;
if (name.IsEmpty())
{
if (prop.vt == VT_UI4)
{
numThreads = prop.ulVal;
force = true;
return S_OK;
}
bool val;
HRESULT res = PROPVARIANT_to_bool(prop, val);
if (res == S_OK)
{
if (!val)
{
numThreads = 1;
force = true;
}
// force = true; for debug
// "(VT_BOOL = VARIANT_TRUE)" set "force = false" and doesn't change numThreads
return S_OK;
}
if (prop.vt != VT_BSTR)
return res;
s.SetFromBstr(prop.bstrVal);
if (s.IsEmpty())
return E_INVALIDARG;
}
else
{
if (prop.vt != VT_EMPTY)
return E_INVALIDARG;
s = name;
}
s.MakeLower_Ascii();
const wchar_t *start = s;
UInt32 v = numThreads;
/* we force up, if threads number specified
only `d` will force it down */
bool force_loc = true;
for (;;)
{
const wchar_t c = *start;
if (!c)
break;
if (c == 'd')
{
force_loc = false; // force down
start++;
continue;
}
if (c == 'u')
{
force_loc = true; // force up
start++;
continue;
}
bool isPercent = false;
if (c == 'p')
{
isPercent = true;
start++;
}
const wchar_t *end;
v = ConvertStringToUInt32(start, &end);
if (end == start)
return E_INVALIDARG;
if (isPercent)
v = numThreads * v / 100;
start = end;
}
numThreads = v;
force = force_loc;
return S_OK;
}
static HRESULT SetLogSizeProp(UInt64 number, NCOM::CPropVariant &destProp)
{
if (number >= 64)
return E_INVALIDARG;
UInt32 val32;
if (number < 32)
val32 = (UInt32)1 << (unsigned)number;
/*
else if (number == 32 && reduce_4GB_to_32bits)
val32 = (UInt32)(Int32)-1;
*/
else
{
destProp = (UInt64)((UInt64)1 << (unsigned)number);
return S_OK;
}
destProp = (UInt32)val32;
return S_OK;
}
static HRESULT StringToDictSize(const UString &s, NCOM::CPropVariant &destProp)
{
/* if (reduce_4GB_to_32bits) we can reduce (4 GiB) property to (4 GiB - 1).
to fit the value to UInt32 for clients that do not support 64-bit values */
const wchar_t *end;
const UInt64 number = ConvertStringToUInt64(s, &end);
const unsigned numDigits = (unsigned)(end - s.Ptr());
if (numDigits == 0 || s.Len() > numDigits + 1)
return E_INVALIDARG;
if (s.Len() == numDigits)
return SetLogSizeProp(number, destProp);
unsigned numBits;
switch (MyCharLower_Ascii(s[numDigits]))
{
case 'b': numBits = 0; break;
case 'k': numBits = 10; break;
case 'm': numBits = 20; break;
case 'g': numBits = 30; break;
default: return E_INVALIDARG;
}
const UInt64 range4g = ((UInt64)1 << (32 - numBits));
if (number < range4g)
destProp = (UInt32)((UInt32)number << numBits);
/*
else if (number == range4g && reduce_4GB_to_32bits)
destProp = (UInt32)(Int32)-1;
*/
else if (numBits == 0)
destProp = (UInt64)number;
else if (number >= ((UInt64)1 << (64 - numBits)))
return E_INVALIDARG;
else
destProp = (UInt64)((UInt64)number << numBits);
return S_OK;
}
static HRESULT PROPVARIANT_to_DictSize(const PROPVARIANT &prop, NCOM::CPropVariant &destProp)
{
if (prop.vt == VT_UI4)
return SetLogSizeProp(prop.ulVal, destProp);
if (prop.vt == VT_BSTR)
{
UString s;
s = prop.bstrVal;
return StringToDictSize(s, destProp);
}
return E_INVALIDARG;
}
void CProps::AddProp32(PROPID propid, UInt32 val)
{
CProp &prop = Props.AddNew();
prop.IsOptional = true;
prop.Id = propid;
prop.Value = (UInt32)val;
}
void CProps::AddPropBool(PROPID propid, bool val)
{
CProp &prop = Props.AddNew();
prop.IsOptional = true;
prop.Id = propid;
prop.Value = val;
}
class CCoderProps
{
PROPID *_propIDs;
NCOM::CPropVariant *_props;
unsigned _numProps;
unsigned _numPropsMax;
public:
CCoderProps(unsigned numPropsMax):
_propIDs(NULL),
_props(NULL),
_numProps(0),
_numPropsMax(numPropsMax)
{
_propIDs = new PROPID[numPropsMax];
_props = new NCOM::CPropVariant[numPropsMax];
}
~CCoderProps()
{
delete []_propIDs;
delete []_props;
}
void AddProp(const CProp &prop);
HRESULT SetProps(ICompressSetCoderProperties *setCoderProperties)
{
return setCoderProperties->SetCoderProperties(_propIDs, _props, _numProps);
}
};
void CCoderProps::AddProp(const CProp &prop)
{
if (_numProps >= _numPropsMax)
throw 1;
_propIDs[_numProps] = prop.Id;
_props[_numProps] = prop.Value;
_numProps++;
}
HRESULT CProps::SetCoderProps(ICompressSetCoderProperties *scp, const UInt64 *dataSizeReduce) const
{
return SetCoderProps_DSReduce_Aff(scp, dataSizeReduce, NULL);
}
HRESULT CProps::SetCoderProps_DSReduce_Aff(
ICompressSetCoderProperties *scp,
const UInt64 *dataSizeReduce,
const UInt64 *affinity) const
{
CCoderProps coderProps(Props.Size() + (dataSizeReduce ? 1 : 0) + (affinity ? 1 : 0) );
FOR_VECTOR (i, Props)
coderProps.AddProp(Props[i]);
if (dataSizeReduce)
{
CProp prop;
prop.Id = NCoderPropID::kReduceSize;
prop.Value = *dataSizeReduce;
coderProps.AddProp(prop);
}
if (affinity)
{
CProp prop;
prop.Id = NCoderPropID::kAffinity;
prop.Value = *affinity;
coderProps.AddProp(prop);
}
return coderProps.SetProps(scp);
}
int CMethodProps::FindProp(PROPID id) const
{
for (unsigned i = Props.Size(); i != 0;)
if (Props[--i].Id == id)
return (int)i;
return -1;
}
unsigned CMethodProps::GetLevel() const
{
int i = FindProp(NCoderPropID::kLevel);
if (i < 0)
return 5;
if (Props[(unsigned)i].Value.vt != VT_UI4)
return 9;
UInt32 level = Props[(unsigned)i].Value.ulVal;
return level > 9 ? 9 : (unsigned)level;
}
struct CNameToPropID
{
VARTYPE VarType;
const char *Name;
};
// the following are related to NCoderPropID::EEnum values
// NCoderPropID::k_NUM_DEFINED
static const CNameToPropID g_NameToPropID[] =
{
{ VT_UI4, "" },
{ VT_UI4, "d" },
{ VT_UI4, "mem" },
{ VT_UI4, "o" },
{ VT_UI8, "c" },
{ VT_UI4, "pb" },
{ VT_UI4, "lc" },
{ VT_UI4, "lp" },
{ VT_UI4, "fb" },
{ VT_BSTR, "mf" },
{ VT_UI4, "mc" },
{ VT_UI4, "pass" },
{ VT_UI4, "a" },
{ VT_UI4, "mt" },
{ VT_BOOL, "eos" },
{ VT_UI4, "x" },
{ VT_UI8, "reduce" },
{ VT_UI8, "expect" },
{ VT_UI8, "cc" }, // "cc" in v23, "b" in v22.01
{ VT_UI4, "check" },
{ VT_BSTR, "filter" },
{ VT_UI8, "memuse" },
{ VT_UI8, "aff" },
{ VT_UI4, "offset" },
{ VT_UI4, "zhb" }
/*
,
// { VT_UI4, "zhc" },
// { VT_UI4, "zhd" },
// { VT_UI4, "zcb" },
{ VT_UI4, "dc" },
{ VT_UI4, "zx" },
{ VT_UI4, "zf" },
{ VT_UI4, "zmml" },
{ VT_UI4, "zov" },
{ VT_BOOL, "zmfr" },
{ VT_BOOL, "zle" }, // long enable
// { VT_UI4, "zldb" },
{ VT_UI4, "zld" },
{ VT_UI4, "zlhb" },
{ VT_UI4, "zlmml" },
{ VT_UI4, "zlbb" },
{ VT_UI4, "zlhrb" },
{ VT_BOOL, "zwus" },
{ VT_BOOL, "zshp" },
{ VT_BOOL, "zshs" },
{ VT_BOOL, "zshe" },
{ VT_BOOL, "zshg" },
{ VT_UI4, "zpsm" }
*/
// { VT_UI4, "mcb" }, // mc log version
// { VT_UI4, "ztlen" }, // fb ?
};
/*
#if defined(static_assert) || (defined(__cplusplus) && __cplusplus >= 200410L) || (defined(_MSC_VER) && _MSC_VER >= 1600)
#if (defined(__cplusplus) && __cplusplus < 201103L) \
&& defined(__clang__) && __clang_major__ >= 4
#pragma GCC diagnostic ignored "-Wc11-extensions"
#endif
static_assert(Z7_ARRAY_SIZE(g_NameToPropID) == NCoderPropID::k_NUM_DEFINED,
"g_NameToPropID doesn't match NCoderPropID enum");
#endif
*/
static int FindPropIdExact(const UString &name)
{
for (unsigned i = 0; i < Z7_ARRAY_SIZE(g_NameToPropID); i++)
if (StringsAreEqualNoCase_Ascii(name, g_NameToPropID[i].Name))
return (int)i;
return -1;
}
static bool ConvertProperty(const PROPVARIANT &srcProp, VARTYPE varType, NCOM::CPropVariant &destProp)
{
if (varType == srcProp.vt)
{
destProp = srcProp;
return true;
}
if (varType == VT_UI8 && srcProp.vt == VT_UI4)
{
destProp = (UInt64)srcProp.ulVal;
return true;
}
if (varType == VT_BOOL)
{
bool res;
if (PROPVARIANT_to_bool(srcProp, res) != S_OK)
return false;
destProp = res;
return true;
}
if (srcProp.vt == VT_EMPTY)
{
destProp = srcProp;
return true;
}
return false;
}
static void SplitParams(const UString &srcString, UStringVector &subStrings)
{
subStrings.Clear();
UString s;
unsigned len = srcString.Len();
if (len == 0)
return;
for (unsigned i = 0; i < len; i++)
{
wchar_t c = srcString[i];
if (c == L':')
{
subStrings.Add(s);
s.Empty();
}
else
s += c;
}
subStrings.Add(s);
}
static void SplitParam(const UString ¶m, UString &name, UString &value)
{
int eqPos = param.Find(L'=');
if (eqPos >= 0)
{
name.SetFrom(param, (unsigned)eqPos);
value = param.Ptr((unsigned)(eqPos + 1));
return;
}
unsigned i;
for (i = 0; i < param.Len(); i++)
{
wchar_t c = param[i];
if (c >= L'0' && c <= L'9')
break;
}
name.SetFrom(param, i);
value = param.Ptr(i);
}
static bool IsLogSizeProp(PROPID propid)
{
switch (propid)
{
case NCoderPropID::kDictionarySize:
case NCoderPropID::kUsedMemorySize:
case NCoderPropID::kBlockSize:
case NCoderPropID::kBlockSize2:
/*
case NCoderPropID::kChainSize:
case NCoderPropID::kLdmWindowSize:
*/
// case NCoderPropID::kReduceSize:
return true;
default: break;
}
return false;
}
HRESULT CMethodProps::SetParam(const UString &name, const UString &value)
{
int index = FindPropIdExact(name);
if (index < 0)
{
// 'b' was used as NCoderPropID::kBlockSize2 before v23
if (!name.IsEqualTo_Ascii_NoCase("b") || value.Find(L':') >= 0)
return E_INVALIDARG;
index = NCoderPropID::kBlockSize2;
}
const CNameToPropID &nameToPropID = g_NameToPropID[(unsigned)index];
CProp prop;
prop.Id = (unsigned)index;
if (IsLogSizeProp(prop.Id))
{
RINOK(StringToDictSize(value, prop.Value))
}
else
{
NCOM::CPropVariant propValue;
if (nameToPropID.VarType == VT_BSTR)
propValue = value;
else if (nameToPropID.VarType == VT_BOOL)
{
bool res;
if (!StringToBool(value, res))
return E_INVALIDARG;
propValue = res;
}
else if (!value.IsEmpty())
{
if (nameToPropID.VarType == VT_UI4)
{
UInt32 number;
if (ParseStringToUInt32(value, number) == value.Len())
propValue = number;
else
propValue = value;
}
else if (nameToPropID.VarType == VT_UI8)
{
UInt64 number;
if (ParseStringToUInt64(value, number) == value.Len())
propValue = number;
else
propValue = value;
}
else
propValue = value;
}
if (!ConvertProperty(propValue, nameToPropID.VarType, prop.Value))
return E_INVALIDARG;
}
Props.Add(prop);
return S_OK;
}
HRESULT CMethodProps::ParseParamsFromString(const UString &srcString)
{
UStringVector params;
SplitParams(srcString, params);
FOR_VECTOR (i, params)
{
const UString ¶m = params[i];
UString name, value;
SplitParam(param, name, value);
RINOK(SetParam(name, value))
}
return S_OK;
}
HRESULT CMethodProps::ParseParamsFromPROPVARIANT(const UString &realName, const PROPVARIANT &value)
{
if (realName.Len() == 0)
{
// [empty]=method
return E_INVALIDARG;
}
if (value.vt == VT_EMPTY)
{
// {realName}=[empty]
UString name, valueStr;
SplitParam(realName, name, valueStr);
return SetParam(name, valueStr);
}
// {realName}=value
const int index = FindPropIdExact(realName);
if (index < 0)
return E_INVALIDARG;
const CNameToPropID &nameToPropID = g_NameToPropID[(unsigned)index];
CProp prop;
prop.Id = (unsigned)index;
if (IsLogSizeProp(prop.Id))
{
RINOK(PROPVARIANT_to_DictSize(value, prop.Value))
}
else
{
if (!ConvertProperty(value, nameToPropID.VarType, prop.Value))
return E_INVALIDARG;
}
Props.Add(prop);
return S_OK;
}
static UInt64 GetMemoryUsage_LZMA(UInt32 dict, bool isBt, UInt32 numThreads)
{
UInt32 hs = dict - 1;
hs |= (hs >> 1);
hs |= (hs >> 2);
hs |= (hs >> 4);
hs |= (hs >> 8);
hs >>= 1;
if (hs >= (1 << 24))
hs >>= 1;
hs |= (1 << 16) - 1;
// if (numHashBytes >= 5)
if (!isBt)
hs |= (256 << 10) - 1;
hs++;
UInt64 size1 = (UInt64)hs * 4;
size1 += (UInt64)dict * 4;
if (isBt)
size1 += (UInt64)dict * 4;
size1 += (2 << 20);
if (numThreads > 1 && isBt)
size1 += (2 << 20) + (4 << 20);
return size1;
}
static const UInt32 kLzmaMaxDictSize = (UInt32)15 << 28;
UInt64 CMethodProps::Get_Lzma_MemUsage(bool addSlidingWindowSize) const
{
const UInt64 dicSize = Get_Lzma_DicSize();
const bool isBt = Get_Lzma_MatchFinder_IsBt();
const UInt32 dict32 = (dicSize >= kLzmaMaxDictSize ? kLzmaMaxDictSize : (UInt32)dicSize);
const UInt32 numThreads = Get_Lzma_NumThreads();
UInt64 size = GetMemoryUsage_LZMA(dict32, isBt, numThreads);
if (addSlidingWindowSize)
{
const UInt32 kBlockSizeMax = (UInt32)0 - (UInt32)(1 << 16);
UInt64 blockSize = (UInt64)dict32 + (1 << 16)
+ (numThreads > 1 ? (1 << 20) : 0);
blockSize += (blockSize >> (blockSize < ((UInt32)1 << 30) ? 1 : 2));
if (blockSize >= kBlockSizeMax)
blockSize = kBlockSizeMax;
size += blockSize;
}
return size;
}
HRESULT COneMethodInfo::ParseMethodFromString(const UString &s)
{
MethodName.Empty();
int splitPos = s.Find(L':');
{
UString temp = s;
if (splitPos >= 0)
temp.DeleteFrom((unsigned)splitPos);
if (!temp.IsAscii())
return E_INVALIDARG;
MethodName.SetFromWStr_if_Ascii(temp);
}
if (splitPos < 0)
return S_OK;
PropsString = s.Ptr((unsigned)(splitPos + 1));
return ParseParamsFromString(PropsString);
}
HRESULT COneMethodInfo::ParseMethodFromPROPVARIANT(const UString &realName, const PROPVARIANT &value)
{
if (!realName.IsEmpty() && !StringsAreEqualNoCase_Ascii(realName, "m"))
return ParseParamsFromPROPVARIANT(realName, value);
// -m{N}=method
if (value.vt != VT_BSTR)
return E_INVALIDARG;
UString s;
s = value.bstrVal;
return ParseMethodFromString(s);
}
|