1 // MethodProps.cpp
2
3 #include "StdAfx.h"
4
5 #include "../../Common/StringToInt.h"
6
7 #include "MethodProps.h"
8
9 using namespace NWindows;
10
Calc_From_Val_Percents(UInt64 val,UInt64 percents)11 UInt64 Calc_From_Val_Percents(UInt64 val, UInt64 percents)
12 {
13 // if (percents == 0) return 0;
14 const UInt64 q = percents / 100;
15 const UInt32 r = (UInt32)(percents % 100);
16 UInt64 res = 0;
17
18 if (q != 0)
19 {
20 if (val > (UInt64)(Int64)-1 / q)
21 return (UInt64)(Int64)-1;
22 res = val * q;
23 }
24
25 if (r != 0)
26 {
27 UInt64 v2;
28 if (val <= (UInt64)(Int64)-1 / r)
29 v2 = val * r / 100;
30 else
31 v2 = val / 100 * r;
32 res += v2;
33 if (res < v2)
34 return (UInt64)(Int64)-1;
35 }
36
37 return res;
38 }
39
40
StringToBool(const wchar_t * s,bool & res)41 bool StringToBool(const wchar_t *s, bool &res)
42 {
43 if (s[0] == 0 || (s[0] == '+' && s[1] == 0) || StringsAreEqualNoCase_Ascii(s, "ON"))
44 {
45 res = true;
46 return true;
47 }
48 if ((s[0] == '-' && s[1] == 0) || StringsAreEqualNoCase_Ascii(s, "OFF"))
49 {
50 res = false;
51 return true;
52 }
53 return false;
54 }
55
PROPVARIANT_to_bool(const PROPVARIANT & prop,bool & dest)56 HRESULT PROPVARIANT_to_bool(const PROPVARIANT &prop, bool &dest)
57 {
58 switch (prop.vt)
59 {
60 case VT_EMPTY: dest = true; return S_OK;
61 case VT_BOOL: dest = (prop.boolVal != VARIANT_FALSE); return S_OK;
62 case VT_BSTR: return StringToBool(prop.bstrVal, dest) ? S_OK : E_INVALIDARG;
63 }
64 return E_INVALIDARG;
65 }
66
ParseStringToUInt32(const UString & srcString,UInt32 & number)67 unsigned ParseStringToUInt32(const UString &srcString, UInt32 &number)
68 {
69 const wchar_t *start = srcString;
70 const wchar_t *end;
71 number = ConvertStringToUInt32(start, &end);
72 return (unsigned)(end - start);
73 }
74
ParseStringToUInt64(const UString & srcString,UInt64 & number)75 static unsigned ParseStringToUInt64(const UString &srcString, UInt64 &number)
76 {
77 const wchar_t *start = srcString;
78 const wchar_t *end;
79 number = ConvertStringToUInt64(start, &end);
80 return (unsigned)(end - start);
81 }
82
ParsePropToUInt32(const UString & name,const PROPVARIANT & prop,UInt32 & resValue)83 HRESULT ParsePropToUInt32(const UString &name, const PROPVARIANT &prop, UInt32 &resValue)
84 {
85 // =VT_UI4
86 // =VT_EMPTY : it doesn't change (resValue), and returns S_OK
87 // {stringUInt32}=VT_EMPTY
88
89 if (prop.vt == VT_UI4)
90 {
91 if (!name.IsEmpty())
92 return E_INVALIDARG;
93 resValue = prop.ulVal;
94 return S_OK;
95 }
96 if (prop.vt != VT_EMPTY)
97 return E_INVALIDARG;
98 if (name.IsEmpty())
99 return S_OK;
100 UInt32 v;
101 if (ParseStringToUInt32(name, v) != name.Len())
102 return E_INVALIDARG;
103 resValue = v;
104 return S_OK;
105 }
106
107
108
ParseMtProp2(const UString & name,const PROPVARIANT & prop,UInt32 & numThreads,bool & force)109 HRESULT ParseMtProp2(const UString &name, const PROPVARIANT &prop, UInt32 &numThreads, bool &force)
110 {
111 force = false;
112 UString s;
113 if (name.IsEmpty())
114 {
115 if (prop.vt == VT_UI4)
116 {
117 numThreads = prop.ulVal;
118 force = true;
119 return S_OK;
120 }
121 bool val;
122 HRESULT res = PROPVARIANT_to_bool(prop, val);
123 if (res == S_OK)
124 {
125 if (!val)
126 {
127 numThreads = 1;
128 force = true;
129 }
130 // force = true; for debug
131 // "(VT_BOOL = VARIANT_TRUE)" set "force = false" and doesn't change numThreads
132 return S_OK;
133 }
134 if (prop.vt != VT_BSTR)
135 return res;
136 s.SetFromBstr(prop.bstrVal);
137 if (s.IsEmpty())
138 return E_INVALIDARG;
139 }
140 else
141 {
142 if (prop.vt != VT_EMPTY)
143 return E_INVALIDARG;
144 s = name;
145 }
146
147 s.MakeLower_Ascii();
148 const wchar_t *start = s;
149 UInt32 v = numThreads;
150
151 /* we force up, if threads number specified
152 only `d` will force it down */
153 bool force_loc = true;
154 for (;;)
155 {
156 const wchar_t c = *start;
157 if (!c)
158 break;
159 if (c == 'd')
160 {
161 force_loc = false; // force down
162 start++;
163 continue;
164 }
165 if (c == 'u')
166 {
167 force_loc = true; // force up
168 start++;
169 continue;
170 }
171 bool isPercent = false;
172 if (c == 'p')
173 {
174 isPercent = true;
175 start++;
176 }
177 const wchar_t *end;
178 v = ConvertStringToUInt32(start, &end);
179 if (end == start)
180 return E_INVALIDARG;
181 if (isPercent)
182 v = numThreads * v / 100;
183 start = end;
184 }
185
186 numThreads = v;
187 force = force_loc;
188 return S_OK;
189 }
190
191
192
SetLogSizeProp(UInt64 number,NCOM::CPropVariant & destProp)193 static HRESULT SetLogSizeProp(UInt64 number, NCOM::CPropVariant &destProp)
194 {
195 if (number >= 64)
196 return E_INVALIDARG;
197 UInt32 val32;
198 if (number < 32)
199 val32 = (UInt32)1 << (unsigned)number;
200 /*
201 else if (number == 32 && reduce_4GB_to_32bits)
202 val32 = (UInt32)(Int32)-1;
203 */
204 else
205 {
206 destProp = (UInt64)((UInt64)1 << (unsigned)number);
207 return S_OK;
208 }
209 destProp = (UInt32)val32;
210 return S_OK;
211 }
212
213
StringToDictSize(const UString & s,NCOM::CPropVariant & destProp)214 static HRESULT StringToDictSize(const UString &s, NCOM::CPropVariant &destProp)
215 {
216 /* if (reduce_4GB_to_32bits) we can reduce (4 GiB) property to (4 GiB - 1).
217 to fit the value to UInt32 for clients that do not support 64-bit values */
218
219 const wchar_t *end;
220 const UInt64 number = ConvertStringToUInt64(s, &end);
221 const unsigned numDigits = (unsigned)(end - s.Ptr());
222 if (numDigits == 0 || s.Len() > numDigits + 1)
223 return E_INVALIDARG;
224
225 if (s.Len() == numDigits)
226 return SetLogSizeProp(number, destProp);
227
228 unsigned numBits;
229
230 switch (MyCharLower_Ascii(s[numDigits]))
231 {
232 case 'b': numBits = 0; break;
233 case 'k': numBits = 10; break;
234 case 'm': numBits = 20; break;
235 case 'g': numBits = 30; break;
236 default: return E_INVALIDARG;
237 }
238
239 const UInt64 range4g = ((UInt64)1 << (32 - numBits));
240 if (number < range4g)
241 destProp = (UInt32)((UInt32)number << numBits);
242 /*
243 else if (number == range4g && reduce_4GB_to_32bits)
244 destProp = (UInt32)(Int32)-1;
245 */
246 else if (numBits == 0)
247 destProp = (UInt64)number;
248 else if (number >= ((UInt64)1 << (64 - numBits)))
249 return E_INVALIDARG;
250 else
251 destProp = (UInt64)((UInt64)number << numBits);
252 return S_OK;
253 }
254
255
PROPVARIANT_to_DictSize(const PROPVARIANT & prop,NCOM::CPropVariant & destProp)256 static HRESULT PROPVARIANT_to_DictSize(const PROPVARIANT &prop, NCOM::CPropVariant &destProp)
257 {
258 if (prop.vt == VT_UI4)
259 return SetLogSizeProp(prop.ulVal, destProp);
260
261 if (prop.vt == VT_BSTR)
262 {
263 UString s;
264 s = prop.bstrVal;
265 return StringToDictSize(s, destProp);
266 }
267 return E_INVALIDARG;
268 }
269
270
AddProp32(PROPID propid,UInt32 val)271 void CProps::AddProp32(PROPID propid, UInt32 val)
272 {
273 CProp &prop = Props.AddNew();
274 prop.IsOptional = true;
275 prop.Id = propid;
276 prop.Value = (UInt32)val;
277 }
278
AddPropBool(PROPID propid,bool val)279 void CProps::AddPropBool(PROPID propid, bool val)
280 {
281 CProp &prop = Props.AddNew();
282 prop.IsOptional = true;
283 prop.Id = propid;
284 prop.Value = val;
285 }
286
287 class CCoderProps
288 {
289 PROPID *_propIDs;
290 NCOM::CPropVariant *_props;
291 unsigned _numProps;
292 unsigned _numPropsMax;
293 public:
CCoderProps(unsigned numPropsMax)294 CCoderProps(unsigned numPropsMax):
295 _propIDs(NULL),
296 _props(NULL),
297 _numProps(0),
298 _numPropsMax(numPropsMax)
299 {
300 _propIDs = new PROPID[numPropsMax];
301 _props = new NCOM::CPropVariant[numPropsMax];
302 }
~CCoderProps()303 ~CCoderProps()
304 {
305 delete []_propIDs;
306 delete []_props;
307 }
308 void AddProp(const CProp &prop);
SetProps(ICompressSetCoderProperties * setCoderProperties)309 HRESULT SetProps(ICompressSetCoderProperties *setCoderProperties)
310 {
311 return setCoderProperties->SetCoderProperties(_propIDs, _props, _numProps);
312 }
313 };
314
AddProp(const CProp & prop)315 void CCoderProps::AddProp(const CProp &prop)
316 {
317 if (_numProps >= _numPropsMax)
318 throw 1;
319 _propIDs[_numProps] = prop.Id;
320 _props[_numProps] = prop.Value;
321 _numProps++;
322 }
323
SetCoderProps(ICompressSetCoderProperties * scp,const UInt64 * dataSizeReduce) const324 HRESULT CProps::SetCoderProps(ICompressSetCoderProperties *scp, const UInt64 *dataSizeReduce) const
325 {
326 return SetCoderProps_DSReduce_Aff(scp, dataSizeReduce, NULL);
327 }
328
SetCoderProps_DSReduce_Aff(ICompressSetCoderProperties * scp,const UInt64 * dataSizeReduce,const UInt64 * affinity) const329 HRESULT CProps::SetCoderProps_DSReduce_Aff(
330 ICompressSetCoderProperties *scp,
331 const UInt64 *dataSizeReduce,
332 const UInt64 *affinity) const
333 {
334 CCoderProps coderProps(Props.Size() + (dataSizeReduce ? 1 : 0) + (affinity ? 1 : 0) );
335 FOR_VECTOR (i, Props)
336 coderProps.AddProp(Props[i]);
337 if (dataSizeReduce)
338 {
339 CProp prop;
340 prop.Id = NCoderPropID::kReduceSize;
341 prop.Value = *dataSizeReduce;
342 coderProps.AddProp(prop);
343 }
344 if (affinity)
345 {
346 CProp prop;
347 prop.Id = NCoderPropID::kAffinity;
348 prop.Value = *affinity;
349 coderProps.AddProp(prop);
350 }
351 return coderProps.SetProps(scp);
352 }
353
354
FindProp(PROPID id) const355 int CMethodProps::FindProp(PROPID id) const
356 {
357 for (unsigned i = Props.Size(); i != 0;)
358 if (Props[--i].Id == id)
359 return (int)i;
360 return -1;
361 }
362
GetLevel() const363 unsigned CMethodProps::GetLevel() const
364 {
365 int i = FindProp(NCoderPropID::kLevel);
366 if (i < 0)
367 return 5;
368 if (Props[(unsigned)i].Value.vt != VT_UI4)
369 return 9;
370 UInt32 level = Props[(unsigned)i].Value.ulVal;
371 return level > 9 ? 9 : (unsigned)level;
372 }
373
374 struct CNameToPropID
375 {
376 VARTYPE VarType;
377 const char *Name;
378 };
379
380
381 // the following are related to NCoderPropID::EEnum values
382
383 static const CNameToPropID g_NameToPropID[] =
384 {
385 { VT_UI4, "" },
386 { VT_UI4, "d" },
387 { VT_UI4, "mem" },
388 { VT_UI4, "o" },
389 { VT_UI4, "c" },
390 { VT_UI4, "pb" },
391 { VT_UI4, "lc" },
392 { VT_UI4, "lp" },
393 { VT_UI4, "fb" },
394 { VT_BSTR, "mf" },
395 { VT_UI4, "mc" },
396 { VT_UI4, "pass" },
397 { VT_UI4, "a" },
398 { VT_UI4, "mt" },
399 { VT_BOOL, "eos" },
400 { VT_UI4, "x" },
401 { VT_UI8, "reduce" },
402 { VT_UI8, "expect" },
403 { VT_UI4, "b" },
404 { VT_UI4, "check" },
405 { VT_BSTR, "filter" },
406 { VT_UI8, "memuse" }
407 };
408
FindPropIdExact(const UString & name)409 static int FindPropIdExact(const UString &name)
410 {
411 for (unsigned i = 0; i < ARRAY_SIZE(g_NameToPropID); i++)
412 if (StringsAreEqualNoCase_Ascii(name, g_NameToPropID[i].Name))
413 return (int)i;
414 return -1;
415 }
416
ConvertProperty(const PROPVARIANT & srcProp,VARTYPE varType,NCOM::CPropVariant & destProp)417 static bool ConvertProperty(const PROPVARIANT &srcProp, VARTYPE varType, NCOM::CPropVariant &destProp)
418 {
419 if (varType == srcProp.vt)
420 {
421 destProp = srcProp;
422 return true;
423 }
424
425 if (varType == VT_UI8 && srcProp.vt == VT_UI4)
426 {
427 destProp = (UInt64)srcProp.ulVal;
428 return true;
429 }
430
431 if (varType == VT_BOOL)
432 {
433 bool res;
434 if (PROPVARIANT_to_bool(srcProp, res) != S_OK)
435 return false;
436 destProp = res;
437 return true;
438 }
439 if (srcProp.vt == VT_EMPTY)
440 {
441 destProp = srcProp;
442 return true;
443 }
444 return false;
445 }
446
SplitParams(const UString & srcString,UStringVector & subStrings)447 static void SplitParams(const UString &srcString, UStringVector &subStrings)
448 {
449 subStrings.Clear();
450 UString s;
451 unsigned len = srcString.Len();
452 if (len == 0)
453 return;
454 for (unsigned i = 0; i < len; i++)
455 {
456 wchar_t c = srcString[i];
457 if (c == L':')
458 {
459 subStrings.Add(s);
460 s.Empty();
461 }
462 else
463 s += c;
464 }
465 subStrings.Add(s);
466 }
467
SplitParam(const UString & param,UString & name,UString & value)468 static void SplitParam(const UString ¶m, UString &name, UString &value)
469 {
470 int eqPos = param.Find(L'=');
471 if (eqPos >= 0)
472 {
473 name.SetFrom(param, (unsigned)eqPos);
474 value = param.Ptr((unsigned)(eqPos + 1));
475 return;
476 }
477 unsigned i;
478 for (i = 0; i < param.Len(); i++)
479 {
480 wchar_t c = param[i];
481 if (c >= L'0' && c <= L'9')
482 break;
483 }
484 name.SetFrom(param, i);
485 value = param.Ptr(i);
486 }
487
IsLogSizeProp(PROPID propid)488 static bool IsLogSizeProp(PROPID propid)
489 {
490 switch (propid)
491 {
492 case NCoderPropID::kDictionarySize:
493 case NCoderPropID::kUsedMemorySize:
494 case NCoderPropID::kBlockSize:
495 case NCoderPropID::kBlockSize2:
496 // case NCoderPropID::kReduceSize:
497 return true;
498 }
499 return false;
500 }
501
SetParam(const UString & name,const UString & value)502 HRESULT CMethodProps::SetParam(const UString &name, const UString &value)
503 {
504 int index = FindPropIdExact(name);
505 if (index < 0)
506 return E_INVALIDARG;
507 const CNameToPropID &nameToPropID = g_NameToPropID[(unsigned)index];
508 CProp prop;
509 prop.Id = (unsigned)index;
510
511 if (IsLogSizeProp(prop.Id))
512 {
513 RINOK(StringToDictSize(value, prop.Value));
514 }
515 else
516 {
517 NCOM::CPropVariant propValue;
518 if (nameToPropID.VarType == VT_BSTR)
519 propValue = value;
520 else if (nameToPropID.VarType == VT_BOOL)
521 {
522 bool res;
523 if (!StringToBool(value, res))
524 return E_INVALIDARG;
525 propValue = res;
526 }
527 else if (!value.IsEmpty())
528 {
529 if (nameToPropID.VarType == VT_UI4)
530 {
531 UInt32 number;
532 if (ParseStringToUInt32(value, number) == value.Len())
533 propValue = number;
534 else
535 propValue = value;
536 }
537 else if (nameToPropID.VarType == VT_UI8)
538 {
539 UInt64 number;
540 if (ParseStringToUInt64(value, number) == value.Len())
541 propValue = number;
542 else
543 propValue = value;
544 }
545 else
546 propValue = value;
547 }
548 if (!ConvertProperty(propValue, nameToPropID.VarType, prop.Value))
549 return E_INVALIDARG;
550 }
551 Props.Add(prop);
552 return S_OK;
553 }
554
ParseParamsFromString(const UString & srcString)555 HRESULT CMethodProps::ParseParamsFromString(const UString &srcString)
556 {
557 UStringVector params;
558 SplitParams(srcString, params);
559 FOR_VECTOR (i, params)
560 {
561 const UString ¶m = params[i];
562 UString name, value;
563 SplitParam(param, name, value);
564 RINOK(SetParam(name, value));
565 }
566 return S_OK;
567 }
568
ParseParamsFromPROPVARIANT(const UString & realName,const PROPVARIANT & value)569 HRESULT CMethodProps::ParseParamsFromPROPVARIANT(const UString &realName, const PROPVARIANT &value)
570 {
571 if (realName.Len() == 0)
572 {
573 // [empty]=method
574 return E_INVALIDARG;
575 }
576 if (value.vt == VT_EMPTY)
577 {
578 // {realName}=[empty]
579 UString name, valueStr;
580 SplitParam(realName, name, valueStr);
581 return SetParam(name, valueStr);
582 }
583
584 // {realName}=value
585 int index = FindPropIdExact(realName);
586 if (index < 0)
587 return E_INVALIDARG;
588 const CNameToPropID &nameToPropID = g_NameToPropID[(unsigned)index];
589 CProp prop;
590 prop.Id = (unsigned)index;
591
592 if (IsLogSizeProp(prop.Id))
593 {
594 RINOK(PROPVARIANT_to_DictSize(value, prop.Value));
595 }
596 else
597 {
598 if (!ConvertProperty(value, nameToPropID.VarType, prop.Value))
599 return E_INVALIDARG;
600 }
601 Props.Add(prop);
602 return S_OK;
603 }
604
605
GetMemoryUsage_LZMA(UInt32 dict,bool isBt,UInt32 numThreads)606 static UInt64 GetMemoryUsage_LZMA(UInt32 dict, bool isBt, UInt32 numThreads)
607 {
608 UInt32 hs = dict - 1;
609 hs |= (hs >> 1);
610 hs |= (hs >> 2);
611 hs |= (hs >> 4);
612 hs |= (hs >> 8);
613 hs >>= 1;
614 if (hs >= (1 << 24))
615 hs >>= 1;
616 hs |= (1 << 16) - 1;
617 // if (numHashBytes >= 5)
618 if (!isBt)
619 hs |= (256 << 10) - 1;
620 hs++;
621 UInt64 size1 = (UInt64)hs * 4;
622 size1 += (UInt64)dict * 4;
623 if (isBt)
624 size1 += (UInt64)dict * 4;
625 size1 += (2 << 20);
626
627 if (numThreads > 1 && isBt)
628 size1 += (2 << 20) + (4 << 20);
629 return size1;
630 }
631
632 static const UInt32 kLzmaMaxDictSize = (UInt32)15 << 28;
633
Get_Lzma_MemUsage(bool addSlidingWindowSize) const634 UInt64 CMethodProps::Get_Lzma_MemUsage(bool addSlidingWindowSize) const
635 {
636 const UInt64 dicSize = Get_Lzma_DicSize();
637 const bool isBt = Get_Lzma_MatchFinder_IsBt();
638 const UInt32 dict32 = (dicSize >= kLzmaMaxDictSize ? kLzmaMaxDictSize : (UInt32)dicSize);
639 const UInt32 numThreads = Get_Lzma_NumThreads();
640 UInt64 size = GetMemoryUsage_LZMA(dict32, isBt, numThreads);
641
642 if (addSlidingWindowSize)
643 {
644 const UInt32 kBlockSizeMax = (UInt32)0 - (UInt32)(1 << 16);
645 UInt64 blockSize = (UInt64)dict32 + (1 << 16)
646 + (numThreads > 1 ? (1 << 20) : 0);
647 blockSize += (blockSize >> (blockSize < ((UInt32)1 << 30) ? 1 : 2));
648 if (blockSize >= kBlockSizeMax)
649 blockSize = kBlockSizeMax;
650 size += blockSize;
651 }
652 return size;
653 }
654
655
656
657
ParseMethodFromString(const UString & s)658 HRESULT COneMethodInfo::ParseMethodFromString(const UString &s)
659 {
660 MethodName.Empty();
661 int splitPos = s.Find(L':');
662 {
663 UString temp = s;
664 if (splitPos >= 0)
665 temp.DeleteFrom((unsigned)splitPos);
666 if (!temp.IsAscii())
667 return E_INVALIDARG;
668 MethodName.SetFromWStr_if_Ascii(temp);
669 }
670 if (splitPos < 0)
671 return S_OK;
672 PropsString = s.Ptr((unsigned)(splitPos + 1));
673 return ParseParamsFromString(PropsString);
674 }
675
ParseMethodFromPROPVARIANT(const UString & realName,const PROPVARIANT & value)676 HRESULT COneMethodInfo::ParseMethodFromPROPVARIANT(const UString &realName, const PROPVARIANT &value)
677 {
678 if (!realName.IsEmpty() && !StringsAreEqualNoCase_Ascii(realName, "m"))
679 return ParseParamsFromPROPVARIANT(realName, value);
680 // -m{N}=method
681 if (value.vt != VT_BSTR)
682 return E_INVALIDARG;
683 UString s;
684 s = value.bstrVal;
685 return ParseMethodFromString(s);
686 }
687