RHDC Cloud Saves

This commit is contained in:
Matt Pharoah
2025-07-27 19:36:46 -04:00
parent 7741ad7c0a
commit 34fd28af9c
92 changed files with 9687 additions and 1607 deletions

View File

@@ -33,8 +33,8 @@ CONFIG(release, debug|release){
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += src/main.cpp $$files(src/core/*.cpp) $$files(src/db/*.cpp) $$files(src/ui/*.cpp) $$files(src/polyfill/*.cpp) $$files(src/input/*.cpp) $$files(src/plugins/*.cpp) $$files(src/thirdparty/*.cpp) $$files(src/thirdparty/*.c) $$files(src/rhdc/core/*.cpp) $$files(src/rhdc/web/*.cpp) $$files(src/rhdc/ui/*.cpp) $$files(src/input/common/*.cpp)
HEADERS += src/types.hpp $$files(src/core/*.hpp) $$files(src/db/*.hpp) $$files(src/ui/*.hpp) $$files(src/polyfill/*.hpp) $$files(src/input/*.hpp) $$files(src/plugins/*.hpp) $$files(src/thirdparty/*.h) $$files(src/rhdc/core/*.hpp) $$files(src/rhdc/web/*.hpp) $$files(src/rhdc/ui/*.hpp) $$files(src/input/common/*.hpp)
SOURCES += src/main.cpp $$files(src/core/*.cpp) $$files(src/db/*.cpp) $$files(src/ui/*.cpp) $$files(src/polyfill/*.cpp) $$files(src/input/*.cpp) $$files(src/plugins/*.cpp) $$files(src/thirdparty/*.cpp) src/thirdparty/lzlib/lzlib.c $$files(src/thirdparty/*.c) $$files(src/rhdc/core/*.cpp) $$files(src/rhdc/web/*.cpp) $$files(src/rhdc/ui/*.cpp) $$files(src/input/common/*.cpp)
HEADERS += src/types.hpp $$files(src/core/*.hpp) $$files(src/db/*.hpp) $$files(src/ui/*.hpp) $$files(src/polyfill/*.hpp) $$files(src/input/*.hpp) $$files(src/plugins/*.hpp) $$files(src/thirdparty/*.h) src/thirdparty/lzlib/lzlib.h $$files(src/rhdc/core/*.hpp) $$files(src/rhdc/web/*.hpp) $$files(src/rhdc/ui/*.hpp) $$files(src/input/common/*.hpp)
FORMS += $$files(src/ui/designer/*.ui) $$files(src/rhdc/ui/designer/*.ui)
RESOURCES = data/resources.qrc
TRANSLATIONS = lang/parallel-launcher_en_us.ts lang/parallel-launcher_en_gb.ts lang/parallel-launcher_it.ts lang/parallel-launcher_es.ts lang/parallel-launcher_fr.ts lang/parallel-launcher_fr_ca.ts lang/parallel-launcher_de.ts lang/parallel-launcher_ar.ts lang/parallel-launcher_pt.ts lang/parallel-launcher_ja.ts

View File

@@ -0,0 +1,13 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#eff0f1;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 2 3 L 2 11 L 7 11 L 7 12 L 5 12 L 5 13 L 11 13 L 11 12 L 9 12 L 9 11 L 14 11 L 14 3 L 2 3 z M 3 4 L 13 4 L 13 10 L 3 10 L 3 4 z "
class="ColorScheme-Text"/>
</svg>

After

Width:  |  Height:  |  Size: 479 B

View File

@@ -0,0 +1,13 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#eff0f1;
}
</style>
</defs>
<path
style="fill:currentColor"
d="M 7.5 2 A 4.5 4.5 0 0 0 3 6.5 A 4.5 4.5 0 0 0 3 7.1 A 3 3 0 0 0 1 10 A 3 3 0 0 0 4 13 L 11.5 13 A 3.5 3.5 0 0 0 15 9.5 A 3.5 3.5 0 0 0 12 6 A 4.5 4.5 0 0 0 7.5 2 z M 7.5 3 A 3.5 3.5 0 0 1 11 6.5 A 3.5 3.5 0 0 1 11 7 A 2.5 2.5 0 0 1 11.5 7 A 2.5 2.5 0 0 1 14 9.5 A 2.5 2.5 0 0 1 11.5 12 L 4 12 A 2 2 0 0 1 2 10 A 2 2 0 0 1 4 8 A 2 2 0 0 1 4.4 8 A 3.5 3.5 0 0 1 4 6.5 A 3.5 3.5 0 0 1 7.5 3 z "
class="ColorScheme-Text"/>
</svg>

After

Width:  |  Height:  |  Size: 711 B

View File

@@ -0,0 +1,30 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#eff0f1;
}
.ColorScheme-NegativeText {
color:#da4453;
}
</style>
</defs>
<g
transform="translate(0,-1036.3622)">
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 7.5,2 C 5.0147186,2 3,4.0147186 3,6.5 3.0015312,6.7197442 3.0191564,6.939081 3.0527344,7.15625 1.8279505,7.563833 1.0011929,8.7091793 1,10 c 0,1.656854 1.3431458,3 3,3 l 7.5,0 c 1.932997,0 3.321429,-1.477717 3.321429,-3.4107143 -0.343605,-1.177246 -1.384105,-2.7716808 -2.839605,-3.4058395 -0.02988,-0.013019 0.01902,-0.1361103 -0.01112,-0.14829 C 11.732775,3.7441623 9.803314,2.0026739 7.5,2 Z m 0,1 C 9.4329966,3 11,4.5670034 11,6.5 10.998359,6.6892144 10.981376,6.8779841 10.949219,7.0644531 11.129854,7.0226102 11.314584,7.0009929 11.5,7 12.880712,7 14,8.1192881 14,9.5 14,10.880712 12.880712,12 11.5,12 L 4,12 C 2.8954305,12 2,11.104569 2,10 2,8.8954305 2.8954305,8 4,8 4.119895,8.000332 4.2395173,8.0114445 4.3574219,8.0332031 4.1233957,7.5559094 4.0011632,7.031579 4,6.5 4,4.5670034 5.5670034,3 7.5,3 Z"
transform="translate(0,1036.3622)"
id="path4139"
class="ColorScheme-Text" />
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
class="ColorScheme-NegativeText"
d="m 8.0000348,1042.3622 5.9999302,0 c 0.554019,0 1.000035,0.446 1.000035,1 l 0,5.9999 c 0,0.5541 -0.446016,1.0001 -1.000035,1.0001 l -5.9999302,0 C 7.4460155,1050.3622 7,1049.9162 7,1049.3621 l 0,-5.9999 c 0,-0.554 0.4460155,-1 1.0000348,-1 z"
id="rect4153-8" />
<path
style="fill:#ffffff;stroke-opacity:1"
d="m 9,1043.3622 -1,1 2,2 -2,2 1,1 2,-2 2,2 1,-1 -2,-2 2,-2 -1,-1 -2,2 -2,-2 z"
id="path4141" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@@ -0,0 +1,23 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#eff0f1;
}
.ColorScheme-Highlight {
color:#3daee9;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 7.5 2 C 5.0147186 2 3 4.0147186 3 6.5 C 3.0015312 6.7197442 3.0191564 6.939081 3.0527344 7.15625 C 1.8279505 7.563833 1.0011929 8.7091793 1 10 C 1 11.656854 2.3431458 13 4 13 L 11.5 13 C 13.432997 13 15 11.432997 15 9.5 C 14.656395 8.322754 13.437922 6.8177524 11.982422 6.1835938 C 11.952541 6.1705745 12.000846 6.047336 11.970703 6.0351562 C 11.732775 3.7441623 9.803314 2.0026739 7.5 2 z M 7.5 3 C 9.4329966 3 11 4.5670034 11 6.5 C 10.998359 6.6892144 10.981376 6.8779841 10.949219 7.0644531 C 11.129854 7.0226102 11.314584 7.0009929 11.5 7 C 12.880712 7 14 8.1192881 14 9.5 C 14 10.880712 12.880712 12 11.5 12 L 4 12 C 2.8954305 12 2 11.104569 2 10 C 2 8.8954305 2.8954305 8 4 8 C 4.119895 8.000332 4.2395173 8.0114445 4.3574219 8.0332031 C 4.1233957 7.5559094 4.0011632 7.031579 4 6.5 C 4 4.5670034 5.5670034 3 7.5 3 z "
class="ColorScheme-Text"
/>
<path
style="opacity:1;fill:#bdc3c7"
d="M 11 6 A 4 4 0 0 0 7 10 A 4 4 0 0 0 11 14 A 4 4 0 0 0 15 10 A 4 4 0 0 0 11 6 z " />
<path
style="opacity:1;fill:#ffffff"
d="M 9 9 L 9 11 L 10 11 L 10 9 L 9 9 z M 12 9 L 12 11 L 13 11 L 13 9 L 12 9 z " />
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -0,0 +1,27 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#eff0f1;
}
.ColorScheme-PositiveText {
color:#27ae60;
}
</style>
</defs>
<g
transform="translate(0,-1036.3622)">
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 7.5,2 C 5.0147186,2 3,4.0147186 3,6.5 3.0015312,6.7197442 3.0191564,6.939081 3.0527344,7.15625 1.8279505,7.563833 1.0011929,8.7091793 1,10 c 0,1.656854 1.3431458,3 3,3 l 7.5,0 c 1.932997,0 3.321429,-1.477717 3.321429,-3.4107143 -0.343605,-1.177246 -1.384105,-2.7716808 -2.839605,-3.4058395 -0.02988,-0.013019 0.01902,-0.1361103 -0.01112,-0.14829 C 11.732775,3.7441623 9.803314,2.0026739 7.5,2 Z m 0,1 C 9.4329966,3 11,4.5670034 11,6.5 10.998359,6.6892144 10.981376,6.8779841 10.949219,7.0644531 11.129854,7.0226102 11.314584,7.0009929 11.5,7 12.880712,7 14,8.1192881 14,9.5 14,10.880712 12.880712,12 11.5,12 L 4,12 C 2.8954305,12 2,11.104569 2,10 2,8.8954305 2.8954305,8 4,8 4.119895,8.000332 4.2395173,8.0114445 4.3574219,8.0332031 4.1233957,7.5559094 4.0011632,7.031579 4,6.5 4,4.5670034 5.5670034,3 7.5,3 Z"
transform="translate(0,1036.3622)"
class="ColorScheme-Text" />
<path
d="m 11,1042.3622 c 2.216,0 4,1.784 4,4 0,2.216 -1.784,4 -4,4 -2.216,0 -4,-1.784 -4,-4 0,-2.216 1.784,-4 4,-4 z"
style="fill:currentColor;fill-opacity:1;stroke:none"
class="ColorScheme-PositiveText" />
<path
d="m 13,1044.3622 -3,3 -1,-1 -1,1 1,1 1,1 4,-4 -1,-1 z"
style="fill:#ffffff;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@@ -0,0 +1,26 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#eff0f1;
}
.ColorScheme-Highlight {
color:#3daee9;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 7.5 2 C 5.0147186 2 3 4.0147186 3 6.5 C 3.0015312 6.7197442 3.0191564 6.939081 3.0527344 7.15625 C 1.8279505 7.563833 1.0011929 8.7091793 1 10 C 1 11.656854 2.3431458 13 4 13 L 11.5 13 C 13.432997 13 15 11.432997 15 9.5 C 14.656395 8.322754 13.437922 6.8177523 11.982422 6.1835938 C 11.952541 6.1705745 12.000846 6.047336 11.970703 6.0351562 C 11.732775 3.7441623 9.803314 2.0026739 7.5 2 z M 7.5 3 C 9.4329966 3 11 4.5670034 11 6.5 C 10.998359 6.6892144 10.981376 6.8779841 10.949219 7.0644531 C 11.129854 7.0226102 11.314584 7.0009929 11.5 7 C 12.880712 7 14 8.1192881 14 9.5 C 14 10.880712 12.880712 12 11.5 12 L 4 12 C 2.8954305 12 2 11.104569 2 10 C 2 8.8954305 2.8954305 8 4 8 C 4.119895 8.000332 4.2395173 8.0114445 4.3574219 8.0332031 C 4.1233957 7.5559094 4.0011632 7.031579 4 6.5 C 4 4.5670034 5.5670034 3 7.5 3 z "
class="ColorScheme-Text"
/>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 11 6 C 8.784 6 7 7.784 7 10 C 7 12.216 8.784 14 11 14 C 13.216 14 15 12.216 15 10 C 15 7.784 13.216 6 11 6 z "
class="ColorScheme-Highlight"
/>
<path
style="opacity:1;fill:#ffffff"
d="M 11.050781 7.0039062 A 3 3 0 0 0 8.8789062 7.8789062 A 3 3 0 0 0 8.5546875 11.738281 L 9.2792969 11.013672 A 2 2 0 0 1 9.5859375 8.5859375 A 2 2 0 0 1 12.015625 8.2773438 L 12.734375 7.5585938 A 3 3 0 0 0 11.050781 7.0039062 z M 13.445312 8.2617188 L 12.720703 8.9863281 A 2 2 0 0 1 12.414062 11.414062 A 2 2 0 0 1 9.984375 11.722656 L 9.265625 12.441406 A 3 3 0 0 0 13.121094 12.121094 A 3 3 0 0 0 13.445312 8.2617188 z "
/>
</svg>

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@@ -0,0 +1,25 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#eff0f1;
}
.ColorScheme-NeutralText {
color:#f67400;
}
</style>
</defs>
<path style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 7.5 2 A 4.5 4.5 0 0 0 3 6.5 A 4.5 4.5 0 0 0 3.0527344 7.15625 A 3 3 0 0 0 1 10 A 3 3 0 0 0 4 13 L 11.5 13 A 3.5 3.5 0 0 0 15 9.5 A 3.5 3.5 0 0 0 11.970703 6.0351562 A 4.5 4.5 0 0 0 7.5 2 z M 7.5 3 A 3.5 3.5 0 0 1 11 6.5 A 3.5 3.5 0 0 1 10.949219 7.0644531 A 2.5 2.5 0 0 1 11.5 7 A 2.5 2.5 0 0 1 14 9.5 A 2.5 2.5 0 0 1 11.5 12 L 4 12 A 2 2 0 0 1 2 10 A 2 2 0 0 1 4 8 A 2 2 0 0 1 4.3574219 8.0332031 A 3.5 3.5 0 0 1 4 6.5 A 3.5 3.5 0 0 1 7.5 3 z "
class="ColorScheme-Text"
/>
<path
d="M 10.5 6 A 0.5 0.5002376 0 0 0 10.056641 6.2714844 A 0.5 0.5002376 0 0 1 10.054688 6.2714844 L 10.052734 6.2714844 L 6.0664062 13.248047 L 6.0683594 13.248047 A 0.5 0.5002376 0 0 0 6 13.5 A 0.5 0.5002376 0 0 0 6.5 14 L 14.5 14 A 0.5 0.5002376 0 0 0 15 13.5 A 0.5 0.5002376 0 0 0 14.931641 13.248047 L 14.933594 13.248047 L 10.947266 6.2695312 L 10.943359 6.2695312 A 0.5 0.5002376 0 0 0 10.503906 6 L 10.5 6 z "
style="fill:currentColor;fill-opacity:1;stroke:none"
class="ColorScheme-NeutralText"
/>
<path
style="opacity:1;fill:#ffffff"
d="M 10 8.0039062 L 10 11.003906 L 11 11.003906 L 11 8.0039062 L 10 8.0039062 z M 10 12.003906 L 10 12.005859 L 10 13.005859 L 11 13.005859 L 11 12.005859 L 11 12.003906 L 10 12.003906 z "
/>
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@@ -0,0 +1,13 @@
<svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#eff0f1;
}
</style>
<g class="ColorScheme-Text" fill="currentColor">
<path d="m3.5 11a1.5 1.5 0 0 0 -1.5 1.5 1.5 1.5 0 0 0 1.5 1.5 1.5 1.5 0 0 0 1.5-1.5 1.5 1.5 0 0 0 -1.5-1.5zm-.0117188 1a.5.5 0 0 1 .0117188 0 .5.5 0 0 1 .5.5.5.5 0 0 1 -.5.5.5.5 0 0 1 -.5-.5.5.5 0 0 1 .4882812-.5z"/>
<path d="m11.5 11a1.5 1.5 0 0 0 -1.5 1.5 1.5 1.5 0 0 0 1.5 1.5 1.5 1.5 0 0 0 1.5-1.5 1.5 1.5 0 0 0 -1.5-1.5zm-.01172 1a.5.5 0 0 1 .01172 0 .5.5 0 0 1 .5.5.5.5 0 0 1 -.5.5.5.5 0 0 1 -.5-.5.5.5 0 0 1 .488281-.5z"/>
<path d="m7.5 2a1.5 1.5 0 0 0 -1.5 1.5 1.5 1.5 0 0 0 1.5 1.5 1.5 1.5 0 0 0 1.5-1.5 1.5 1.5 0 0 0 -1.5-1.5zm-.0117188 1a.5.5 0 0 1 .0117188 0 .5.5 0 0 1 .5.5.5.5 0 0 1 -.5.5.5.5 0 0 1 -.5-.5.5.5 0 0 1 .4882812-.5z"/>
<path d="m7 4v3h-2a2 2 0 0 0 -.0488281 0 2 2 0 0 0 -1.9511719 2v3h1v-3a1 1 0 0 1 1-1h5a1 1 0 0 1 1 1v3h1v-3a2 2 0 0 0 -1.951172-2 2 2 0 0 0 -.048828 0h-2v-3z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -0,0 +1,13 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#232629;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 2 3 L 2 11 L 7 11 L 7 12 L 5 12 L 5 13 L 11 13 L 11 12 L 9 12 L 9 11 L 14 11 L 14 3 L 2 3 z M 3 4 L 13 4 L 13 10 L 3 10 L 3 4 z "
class="ColorScheme-Text"/>
</svg>

After

Width:  |  Height:  |  Size: 479 B

View File

@@ -0,0 +1,13 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#232629;
}
</style>
</defs>
<path
style="fill:currentColor"
d="M 7.5 2 A 4.5 4.5 0 0 0 3 6.5 A 4.5 4.5 0 0 0 3 7.1 A 3 3 0 0 0 1 10 A 3 3 0 0 0 4 13 L 11.5 13 A 3.5 3.5 0 0 0 15 9.5 A 3.5 3.5 0 0 0 12 6 A 4.5 4.5 0 0 0 7.5 2 z M 7.5 3 A 3.5 3.5 0 0 1 11 6.5 A 3.5 3.5 0 0 1 11 7 A 2.5 2.5 0 0 1 11.5 7 A 2.5 2.5 0 0 1 14 9.5 A 2.5 2.5 0 0 1 11.5 12 L 4 12 A 2 2 0 0 1 2 10 A 2 2 0 0 1 4 8 A 2 2 0 0 1 4.4 8 A 3.5 3.5 0 0 1 4 6.5 A 3.5 3.5 0 0 1 7.5 3 z "
class="ColorScheme-Text"/>
</svg>

After

Width:  |  Height:  |  Size: 711 B

View File

@@ -0,0 +1,30 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#232629;
}
.ColorScheme-NegativeText {
color:#da4453;
}
</style>
</defs>
<g
transform="translate(0,-1036.3622)">
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 7.5,2 C 5.0147186,2 3,4.0147186 3,6.5 3.0015312,6.7197442 3.0191564,6.939081 3.0527344,7.15625 1.8279505,7.563833 1.0011929,8.7091793 1,10 c 0,1.656854 1.3431458,3 3,3 l 7.5,0 c 1.932997,0 3.321429,-1.477717 3.321429,-3.4107143 -0.343605,-1.177246 -1.384105,-2.7716808 -2.839605,-3.4058395 -0.02988,-0.013019 0.01902,-0.1361103 -0.01112,-0.14829 C 11.732775,3.7441623 9.803314,2.0026739 7.5,2 Z m 0,1 C 9.4329966,3 11,4.5670034 11,6.5 10.998359,6.6892144 10.981376,6.8779841 10.949219,7.0644531 11.129854,7.0226102 11.314584,7.0009929 11.5,7 12.880712,7 14,8.1192881 14,9.5 14,10.880712 12.880712,12 11.5,12 L 4,12 C 2.8954305,12 2,11.104569 2,10 2,8.8954305 2.8954305,8 4,8 4.119895,8.000332 4.2395173,8.0114445 4.3574219,8.0332031 4.1233957,7.5559094 4.0011632,7.031579 4,6.5 4,4.5670034 5.5670034,3 7.5,3 Z"
transform="translate(0,1036.3622)"
id="path4139"
class="ColorScheme-Text" />
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
class="ColorScheme-NegativeText"
d="m 8.0000348,1042.3622 5.9999302,0 c 0.554019,0 1.000035,0.446 1.000035,1 l 0,5.9999 c 0,0.5541 -0.446016,1.0001 -1.000035,1.0001 l -5.9999302,0 C 7.4460155,1050.3622 7,1049.9162 7,1049.3621 l 0,-5.9999 c 0,-0.554 0.4460155,-1 1.0000348,-1 z"
id="rect4153-8" />
<path
style="fill:#ffffff;stroke-opacity:1"
d="m 9,1043.3622 -1,1 2,2 -2,2 1,1 2,-2 2,2 1,-1 -2,-2 2,-2 -1,-1 -2,2 -2,-2 z"
id="path4141" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@@ -0,0 +1,23 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#232629;
}
.ColorScheme-Highlight {
color:#3daee9;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 7.5 2 C 5.0147186 2 3 4.0147186 3 6.5 C 3.0015312 6.7197442 3.0191564 6.939081 3.0527344 7.15625 C 1.8279505 7.563833 1.0011929 8.7091793 1 10 C 1 11.656854 2.3431458 13 4 13 L 11.5 13 C 13.432997 13 15 11.432997 15 9.5 C 14.656395 8.322754 13.437922 6.8177524 11.982422 6.1835938 C 11.952541 6.1705745 12.000846 6.047336 11.970703 6.0351562 C 11.732775 3.7441623 9.803314 2.0026739 7.5 2 z M 7.5 3 C 9.4329966 3 11 4.5670034 11 6.5 C 10.998359 6.6892144 10.981376 6.8779841 10.949219 7.0644531 C 11.129854 7.0226102 11.314584 7.0009929 11.5 7 C 12.880712 7 14 8.1192881 14 9.5 C 14 10.880712 12.880712 12 11.5 12 L 4 12 C 2.8954305 12 2 11.104569 2 10 C 2 8.8954305 2.8954305 8 4 8 C 4.119895 8.000332 4.2395173 8.0114445 4.3574219 8.0332031 C 4.1233957 7.5559094 4.0011632 7.031579 4 6.5 C 4 4.5670034 5.5670034 3 7.5 3 z "
class="ColorScheme-Text"
/>
<path
style="opacity:1;fill:#bdc3c7"
d="M 11 6 A 4 4 0 0 0 7 10 A 4 4 0 0 0 11 14 A 4 4 0 0 0 15 10 A 4 4 0 0 0 11 6 z " />
<path
style="opacity:1;fill:#ffffff"
d="M 9 9 L 9 11 L 10 11 L 10 9 L 9 9 z M 12 9 L 12 11 L 13 11 L 13 9 L 12 9 z " />
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -0,0 +1,27 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#232629;
}
.ColorScheme-PositiveText {
color:#27ae60;
}
</style>
</defs>
<g
transform="translate(0,-1036.3622)">
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 7.5,2 C 5.0147186,2 3,4.0147186 3,6.5 3.0015312,6.7197442 3.0191564,6.939081 3.0527344,7.15625 1.8279505,7.563833 1.0011929,8.7091793 1,10 c 0,1.656854 1.3431458,3 3,3 l 7.5,0 c 1.932997,0 3.321429,-1.477717 3.321429,-3.4107143 -0.343605,-1.177246 -1.384105,-2.7716808 -2.839605,-3.4058395 -0.02988,-0.013019 0.01902,-0.1361103 -0.01112,-0.14829 C 11.732775,3.7441623 9.803314,2.0026739 7.5,2 Z m 0,1 C 9.4329966,3 11,4.5670034 11,6.5 10.998359,6.6892144 10.981376,6.8779841 10.949219,7.0644531 11.129854,7.0226102 11.314584,7.0009929 11.5,7 12.880712,7 14,8.1192881 14,9.5 14,10.880712 12.880712,12 11.5,12 L 4,12 C 2.8954305,12 2,11.104569 2,10 2,8.8954305 2.8954305,8 4,8 4.119895,8.000332 4.2395173,8.0114445 4.3574219,8.0332031 4.1233957,7.5559094 4.0011632,7.031579 4,6.5 4,4.5670034 5.5670034,3 7.5,3 Z"
transform="translate(0,1036.3622)"
class="ColorScheme-Text" />
<path
d="m 11,1042.3622 c 2.216,0 4,1.784 4,4 0,2.216 -1.784,4 -4,4 -2.216,0 -4,-1.784 -4,-4 0,-2.216 1.784,-4 4,-4 z"
style="fill:currentColor;fill-opacity:1;stroke:none"
class="ColorScheme-PositiveText" />
<path
d="m 13,1044.3622 -3,3 -1,-1 -1,1 1,1 1,1 4,-4 -1,-1 z"
style="fill:#ffffff;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@@ -0,0 +1,26 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#232629;
}
.ColorScheme-Highlight {
color:#3daee9;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 7.5 2 C 5.0147186 2 3 4.0147186 3 6.5 C 3.0015312 6.7197442 3.0191564 6.939081 3.0527344 7.15625 C 1.8279505 7.563833 1.0011929 8.7091793 1 10 C 1 11.656854 2.3431458 13 4 13 L 11.5 13 C 13.432997 13 15 11.432997 15 9.5 C 14.656395 8.322754 13.437922 6.8177523 11.982422 6.1835938 C 11.952541 6.1705745 12.000846 6.047336 11.970703 6.0351562 C 11.732775 3.7441623 9.803314 2.0026739 7.5 2 z M 7.5 3 C 9.4329966 3 11 4.5670034 11 6.5 C 10.998359 6.6892144 10.981376 6.8779841 10.949219 7.0644531 C 11.129854 7.0226102 11.314584 7.0009929 11.5 7 C 12.880712 7 14 8.1192881 14 9.5 C 14 10.880712 12.880712 12 11.5 12 L 4 12 C 2.8954305 12 2 11.104569 2 10 C 2 8.8954305 2.8954305 8 4 8 C 4.119895 8.000332 4.2395173 8.0114445 4.3574219 8.0332031 C 4.1233957 7.5559094 4.0011632 7.031579 4 6.5 C 4 4.5670034 5.5670034 3 7.5 3 z "
class="ColorScheme-Text"
/>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 11 6 C 8.784 6 7 7.784 7 10 C 7 12.216 8.784 14 11 14 C 13.216 14 15 12.216 15 10 C 15 7.784 13.216 6 11 6 z "
class="ColorScheme-Highlight"
/>
<path
style="opacity:1;fill:#ffffff"
d="M 11.050781 7.0039062 A 3 3 0 0 0 8.8789062 7.8789062 A 3 3 0 0 0 8.5546875 11.738281 L 9.2792969 11.013672 A 2 2 0 0 1 9.5859375 8.5859375 A 2 2 0 0 1 12.015625 8.2773438 L 12.734375 7.5585938 A 3 3 0 0 0 11.050781 7.0039062 z M 13.445312 8.2617188 L 12.720703 8.9863281 A 2 2 0 0 1 12.414062 11.414062 A 2 2 0 0 1 9.984375 11.722656 L 9.265625 12.441406 A 3 3 0 0 0 13.121094 12.121094 A 3 3 0 0 0 13.445312 8.2617188 z "
/>
</svg>

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@@ -0,0 +1,25 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#232629;
}
.ColorScheme-NeutralText {
color:#f67400;
}
</style>
</defs>
<path style="fill:currentColor;fill-opacity:1;stroke:none"
d="M 7.5 2 A 4.5 4.5 0 0 0 3 6.5 A 4.5 4.5 0 0 0 3.0527344 7.15625 A 3 3 0 0 0 1 10 A 3 3 0 0 0 4 13 L 11.5 13 A 3.5 3.5 0 0 0 15 9.5 A 3.5 3.5 0 0 0 11.970703 6.0351562 A 4.5 4.5 0 0 0 7.5 2 z M 7.5 3 A 3.5 3.5 0 0 1 11 6.5 A 3.5 3.5 0 0 1 10.949219 7.0644531 A 2.5 2.5 0 0 1 11.5 7 A 2.5 2.5 0 0 1 14 9.5 A 2.5 2.5 0 0 1 11.5 12 L 4 12 A 2 2 0 0 1 2 10 A 2 2 0 0 1 4 8 A 2 2 0 0 1 4.3574219 8.0332031 A 3.5 3.5 0 0 1 4 6.5 A 3.5 3.5 0 0 1 7.5 3 z "
class="ColorScheme-Text"
/>
<path
d="M 10.5 6 A 0.5 0.5002376 0 0 0 10.056641 6.2714844 A 0.5 0.5002376 0 0 1 10.054688 6.2714844 L 10.052734 6.2714844 L 6.0664062 13.248047 L 6.0683594 13.248047 A 0.5 0.5002376 0 0 0 6 13.5 A 0.5 0.5002376 0 0 0 6.5 14 L 14.5 14 A 0.5 0.5002376 0 0 0 15 13.5 A 0.5 0.5002376 0 0 0 14.931641 13.248047 L 14.933594 13.248047 L 10.947266 6.2695312 L 10.943359 6.2695312 A 0.5 0.5002376 0 0 0 10.503906 6 L 10.5 6 z "
style="fill:currentColor;fill-opacity:1;stroke:none"
class="ColorScheme-NeutralText"
/>
<path
style="opacity:1;fill:#ffffff"
d="M 10 8.0039062 L 10 11.003906 L 11 11.003906 L 11 8.0039062 L 10 8.0039062 z M 10 12.003906 L 10 12.005859 L 10 13.005859 L 11 13.005859 L 11 12.005859 L 11 12.003906 L 10 12.003906 z "
/>
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@@ -0,0 +1,13 @@
<svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#232629;
}
</style>
<g class="ColorScheme-Text" fill="currentColor">
<path d="m3.5 11a1.5 1.5 0 0 0 -1.5 1.5 1.5 1.5 0 0 0 1.5 1.5 1.5 1.5 0 0 0 1.5-1.5 1.5 1.5 0 0 0 -1.5-1.5zm-.0117188 1a.5.5 0 0 1 .0117188 0 .5.5 0 0 1 .5.5.5.5 0 0 1 -.5.5.5.5 0 0 1 -.5-.5.5.5 0 0 1 .4882812-.5z"/>
<path d="m11.5 11a1.5 1.5 0 0 0 -1.5 1.5 1.5 1.5 0 0 0 1.5 1.5 1.5 1.5 0 0 0 1.5-1.5 1.5 1.5 0 0 0 -1.5-1.5zm-.01172 1a.5.5 0 0 1 .01172 0 .5.5 0 0 1 .5.5.5.5 0 0 1 -.5.5.5.5 0 0 1 -.5-.5.5.5 0 0 1 .488281-.5z"/>
<path d="m7.5 2a1.5 1.5 0 0 0 -1.5 1.5 1.5 1.5 0 0 0 1.5 1.5 1.5 1.5 0 0 0 1.5-1.5 1.5 1.5 0 0 0 -1.5-1.5zm-.0117188 1a.5.5 0 0 1 .0117188 0 .5.5 0 0 1 .5.5.5.5 0 0 1 -.5.5.5.5 0 0 1 -.5-.5.5.5 0 0 1 .4882812-.5z"/>
<path d="m7 4v3h-2a2 2 0 0 0 -.0488281 0 2 2 0 0 0 -1.9511719 2v3h1v-3a1 1 0 0 1 1-1h5a1 1 0 0 1 1 1v3h1v-3a2 2 0 0 0 -1.951172-2 2 2 0 0 0 -.048828 0h-2v-3z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -15,6 +15,7 @@
<file>fallback-icons/common/process-stop.svg</file>
<file>fallback-icons/light/application-menu.svg</file>
<file>fallback-icons/light/computer.svg</file>
<file>fallback-icons/light/configure.svg</file>
<file>fallback-icons/light/dialog-cancel.svg</file>
<file>fallback-icons/light/dialog-ok.svg</file>
@@ -28,6 +29,7 @@
<file>fallback-icons/light/edit-copy.svg</file>
<file>fallback-icons/light/edit-find.svg</file>
<file>fallback-icons/light/edit-undo.svg</file>
<file>fallback-icons/light/folder-cloud.svg</file>
<file>fallback-icons/light/go-next-skip.svg</file>
<file>fallback-icons/light/group.svg</file>
<file>fallback-icons/light/input-gamepad.svg</file>
@@ -39,13 +41,20 @@
<file>fallback-icons/light/media-playlist-shuffle.svg</file>
<file>fallback-icons/light/media-seek-forward.svg</file>
<file>fallback-icons/light/plugins.svg</file>
<file>fallback-icons/light/state-error.svg</file>
<file>fallback-icons/light/state-offline.svg</file>
<file>fallback-icons/light/state-ok.svg</file>
<file>fallback-icons/light/state-sync.svg</file>
<file>fallback-icons/light/state-warning.svg</file>
<file>fallback-icons/light/system-shutdown.svg</file>
<file>fallback-icons/light/user.svg</file>
<file>fallback-icons/light/vcs-merge.svg</file>
<file>fallback-icons/light/view-refresh.svg</file>
<file>fallback-icons/light/view-sort-ascending.svg</file>
<file>fallback-icons/light/view-sort-descending.svg</file>
<file>fallback-icons/dark/application-menu.svg</file>
<file>fallback-icons/dark/computer.svg</file>
<file>fallback-icons/dark/configure.svg</file>
<file>fallback-icons/dark/dialog-cancel.svg</file>
<file>fallback-icons/dark/dialog-ok.svg</file>
@@ -59,6 +68,7 @@
<file>fallback-icons/dark/edit-copy.svg</file>
<file>fallback-icons/dark/edit-find.svg</file>
<file>fallback-icons/dark/edit-undo.svg</file>
<file>fallback-icons/dark/folder-cloud.svg</file>
<file>fallback-icons/dark/go-next-skip.svg</file>
<file>fallback-icons/dark/group.svg</file>
<file>fallback-icons/dark/input-gamepad.svg</file>
@@ -70,8 +80,14 @@
<file>fallback-icons/dark/media-playlist-shuffle.svg</file>
<file>fallback-icons/dark/media-seek-forward.svg</file>
<file>fallback-icons/dark/plugins.svg</file>
<file>fallback-icons/dark/state-error.svg</file>
<file>fallback-icons/dark/state-offline.svg</file>
<file>fallback-icons/dark/state-ok.svg</file>
<file>fallback-icons/dark/state-sync.svg</file>
<file>fallback-icons/dark/state-warning.svg</file>
<file>fallback-icons/dark/system-shutdown.svg</file>
<file>fallback-icons/dark/user.svg</file>
<file>fallback-icons/dark/vcs-merge.svg</file>
<file>fallback-icons/dark/view-refresh.svg</file>
<file>fallback-icons/dark/view-sort-ascending.svg</file>
<file>fallback-icons/dark/view-sort-descending.svg</file>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

116
src/core/fast-lzip.cpp Normal file
View File

@@ -0,0 +1,116 @@
#include "src/core/fast-lzip.hpp"
#include "src/thirdparty/lzlib/lzlib.h"
#include "src/core/buffer.hpp"
class LZipCompressorHandle {
private:
LZ_Encoder *const m_encoder;
public:
inline LZipCompressorHandle( LZ_Encoder *encoder ) : m_encoder( encoder ) {}
inline ~LZipCompressorHandle() { LZ_compress_close( m_encoder ); }
};
class LZipDecompressorHandle {
private:
LZ_Decoder *const m_decoder;
public:
inline LZipDecompressorHandle( LZ_Decoder *decoder ) : m_decoder( decoder ) {}
inline ~LZipDecompressorHandle() { LZ_decompress_close( m_decoder ); }
};
static constexpr int BUFFER_SIZE = 4096;
bool LZip::compressFast( QIODevice &input, QIODevice &output ) {
if( !input.isReadable() || !output.isWritable() ) return false;
LZ_Encoder *encoder = LZ_compress_open( 0xFFFF, 16, 2ull << 50 );
if( !encoder ) return false;
LZipCompressorHandle handle( encoder );
if( LZ_compress_errno( encoder ) ) return false;
Buffer inputBuffer( BUFFER_SIZE );
Buffer outputBuffer( BUFFER_SIZE );
while( input.isReadable() && !LZ_compress_errno( encoder ) && !input.atEnd() ) {
int bufferSize = LZ_compress_write_size( encoder );
if( !bufferSize ) return false;
if( bufferSize > BUFFER_SIZE ) {
bufferSize = BUFFER_SIZE;
}
bufferSize = (int)input.read( inputBuffer.data(), bufferSize );
if( bufferSize > 0 ) {
if( LZ_compress_write( encoder, inputBuffer.udata(), bufferSize ) != bufferSize ) return false;
if( input.atEnd() ) LZ_compress_finish( encoder );
} else if( bufferSize == 0 && input.atEnd() ) {
LZ_compress_finish( encoder );
break;
} else {
return false;
}
while( true ) {
const int bytesRead = LZ_compress_read( encoder, outputBuffer.udata(), BUFFER_SIZE );
if( LZ_compress_errno( encoder ) ) return false;
if( bytesRead <= 0 ) break;
if( output.write( outputBuffer.data(), bytesRead ) != (qint64)bytesRead ) return false;
}
}
return LZ_compress_finished( encoder ) && !LZ_compress_errno( encoder );
}
bool LZip::decompress( QIODevice &input, QIODevice &output, uint32_t *crc32 ) {
if( !input.isReadable() || !output.isWritable() ) return false;
LZ_Decoder *decoder = LZ_decompress_open();
if( !decoder ) return false;
LZipDecompressorHandle handle( decoder );
if( LZ_decompress_errno( decoder ) ) return false;
Buffer inputBuffer( BUFFER_SIZE );
Buffer outputBuffer( BUFFER_SIZE );
while( input.isReadable() && !LZ_decompress_errno( decoder ) && !input.atEnd() ) {
int bufferSize = LZ_decompress_write_size( decoder );
if( !bufferSize ) return false;
if( bufferSize > BUFFER_SIZE ) {
bufferSize = BUFFER_SIZE;
}
bufferSize = (int)input.read( inputBuffer.data(), bufferSize );
if( bufferSize > 0 ) {
if( LZ_decompress_write( decoder, inputBuffer.udata(), bufferSize ) != bufferSize ) return false;
if( input.atEnd() ) LZ_decompress_finish( decoder );
} else if( bufferSize == 0 && input.atEnd() ) {
LZ_decompress_finish( decoder );
} else return false;
while( true ) {
const uint64_t head = LZ_decompress_total_in_size( decoder );
const int bytesRead = LZ_decompress_read( decoder, outputBuffer.udata(), BUFFER_SIZE );
if( bytesRead <= 0 ) {
if( LZ_decompress_errno( decoder ) ) return false;
if( head < LZ_decompress_total_in_size( decoder ) ) continue;
break;
}
if( LZ_decompress_errno( decoder ) ) return false;
if( output.write( outputBuffer.data(), bytesRead ) != (qint64)bytesRead ) return false;
}
}
if( crc32 ) *crc32 = LZ_decompress_data_crc( decoder );
return LZ_decompress_finished( decoder ) && !LZ_decompress_errno( decoder );
}

14
src/core/fast-lzip.hpp Normal file
View File

@@ -0,0 +1,14 @@
#ifndef SRC_CORE_FAST_LZIP_HPP_
#define SRC_CORE_FAST_LZIP_HPP_
#include <QIODevice>
#include <cstdint>
namespace LZip {
extern bool compressFast( QIODevice &input, QIODevice &output );
extern bool decompress( QIODevice &input, QIODevice &output, uint32_t *crc32 = nullptr );
}
#endif /* SRC_CORE_FAST_LZIP_HPP_ */

View File

@@ -0,0 +1,23 @@
#include <QBuffer>
class QtMemoryStream : public QBuffer {
private:
const int m_resizeFactor;
public:
inline QtMemoryStream( int initialCapacity = 1023, int resizeFactor = 4 ) : QBuffer(), m_resizeFactor( resizeFactor ) {
buffer().reserve( initialCapacity );
open( QIODevice::ReadWrite );
}
protected:
virtual qint64 writeData( const char *data, qint64 len ) override {
if( len + buffer().size() > (qint64)buffer().capacity() ) {
buffer().reserve( ((buffer().capacity() + 1) * m_resizeFactor) - 1 );
}
return QBuffer::writeData( data, len );
}
};

View File

@@ -10,8 +10,10 @@
#include <QProgressDialog>
#include <QProgressBar>
#include <QCloseEvent>
#include <QCoreApplication>
#include "src/polyfill/base-directory.hpp"
#include "src/polyfill/byteswap.hpp"
#include "src/polyfill/filetime.hpp"
#include "src/polyfill/file.hpp"
#include "src/polyfill/sha1.hpp"
#include "src/db/sqlite.hpp"
@@ -159,7 +161,7 @@ static inline void processRom(
romFiles.push_back({
romPath,
RomUtil::getLastModified( romPath ),
Time::getLastModifiedSec( romPath ),
sha1,
true
});
@@ -1229,6 +1231,21 @@ ROM_PATHS INNER JOIN ROMS USING( sha1 )
fixSaveFileEndianness();
}
settings.migrationState = 44;
if( settings.migrationState < 45 ) {
if(
RhdcCredentials::exists() &&
QMessageBox::question(
nullptr,
QCoreApplication::translate( "Migration", "Enable RHDC Cloud Saves?" ),
QCoreApplication::translate( "Migration", "Parallel Launcher now supports cloud save syncing with Romhacking.com. Would you like to enable cloud saves now?" )
) == QMessageBox::Yes
) {
RhdcSettings rhdcSettings = FileController::loadRhdcSettings();
rhdcSettings.enableCloudSaves = true;
FileController::saveRhdcSettings( rhdcSettings );
}
}
settings.migrationState = 45;
FileController::saveAppSettings( settings );
}

View File

@@ -6,6 +6,7 @@
#include <thread>
#include <cstdlib>
#include "src/polyfill/file-search.hpp"
#include "src/polyfill/filetime.hpp"
#include "src/polyfill/sha1.hpp"
#include "src/core/preset-controllers.hpp"
#include "src/core/file-controller.hpp"
@@ -156,7 +157,7 @@ static void recomputeRomData(
newRomFiles->push_back({
RomFile{
path,
RomUtil::getLastModified( path ),
Time::getLastModifiedSec( path ),
sha1,
true
},
@@ -192,7 +193,7 @@ static void recomputeRomData(
const string sha1 = Sha1::compute( path );
changedRomFiles->push_back({
path,
RomUtil::getLastModified( path ),
Time::getLastModifiedSec( path ),
sha1,
true
});
@@ -225,7 +226,7 @@ static void analyzeChanges(
for( const RomFile &knownFile : DataProvider::fetchLocalRomFiles() ) {
if( foundRoms->count( knownFile.path ) > 0 ) {
const int64 diff = std::abs( RomUtil::getLastModified( knownFile.path ) - knownFile.lastModified );
const int64 diff = std::abs( Time::getLastModifiedSec( knownFile.path ) - knownFile.lastModified );
if( diff > 2 ) {
changedRoms->push_back( knownFile.path );
}

View File

@@ -111,15 +111,6 @@ string RomUtil::getInternalName( const fs::path romPath ) {
return name.substr( 0, n + 1 );
}
int64 RomUtil::getLastModified( const fs::path romPath ) {
fs::error_code err;
#ifdef __APPLE__
return (int64)fs::last_write_time( romPath, err );
#else
return Time::toUnixTime( fs::last_write_time( romPath, err ) );
#endif
}
static const uint s_crcTable[16] = {
0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC, 0x76DC4190, 0x6B6B51F4, 0x4DB26158, 0x5005713C,
0xEDB88320, 0xF00F9344, 0xD6D6A3E8, 0xCB61B38C, 0x9B64C2B0, 0x86D3D2D4, 0xA00AE278, 0xBDBDF21C,

View File

@@ -11,6 +11,7 @@
#include "src/polyfill/windows/unicode.hpp"
#include "src/polyfill/base-directory.hpp"
#include "src/polyfill/fifo-pipe.hpp"
#include "src/polyfill/filetime.hpp"
#include "src/core/numeric-string.hpp"
#include "src/core/version-win.hpp"
#include "src/core/logging.hpp"
@@ -329,8 +330,7 @@ static bool writeVhdFooter( fs::path &filePath, SdCardInfo &info ) {
};
image.write( (const char*)data1, 16 );
const auto lastModified = fs::last_write_time( filePath, err );
const int64 time = (err ? Time::now() : Time::toUnixTime( lastModified )) - 946684800ull;
const int64 time = Time::getLastModifiedSec( filePath ) - 946684800ull;
image.put( (char)(ubyte)((time >> 24) & 0xFFull) );
image.put( (char)(ubyte)((time >> 16) & 0xFFull) );

View File

@@ -33,7 +33,7 @@ const AppSettings AppSettings::Default = {
#endif
/* patchToSameFolder */ true,
/* patchedRomFolder */ fs::to_path( "~/Documents/N64 Roms" ),
/* migrationState */ 44,
/* migrationState */ 45,
/* coreUpdateInterval */ CoreUpdateInterval::Daily,
/* mupenDevBranch */ true,
/* locale */ "",

View File

@@ -2,7 +2,6 @@
#define SRC_CORE_TIME_HPP_
#include <chrono>
#include <type_traits>
#include "src/types.hpp"
namespace Time {
@@ -15,22 +14,6 @@ namespace Time {
return (int64)std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now().time_since_epoch() ).count();
}
template<typename Clock, typename Units> int64 toUnixTime( const std::chrono::time_point<Clock,Units> &timePoint ) {
if constexpr( std::is_same_v<Clock,std::chrono::system_clock> ) {
return (int64)std::chrono::duration_cast<std::chrono::seconds>( timePoint.time_since_epoch() ).count();
} else {
return Time::now() + (int64)std::chrono::duration_cast<std::chrono::seconds>( timePoint - std::chrono::time_point<Clock,Units>::clock::now() ).count();
}
}
template<typename Clock, typename Units> int64 toUnixTimeMs( const std::chrono::time_point<Clock,Units> &timePoint ) {
if constexpr( std::is_same_v<Clock,std::chrono::system_clock> ) {
return (int64)std::chrono::duration_cast<std::chrono::milliseconds>( timePoint.time_since_epoch() ).count();
} else {
return Time::nowMs() + (int64)std::chrono::duration_cast<std::chrono::milliseconds>( timePoint - std::chrono::time_point<Clock,Units>::clock::now() ).count();
}
}
extern int64 tryParseIso( const string &dateString, bool &success );
}

View File

@@ -1660,6 +1660,20 @@ WHERE id = ?1
return paths;
}
bool DataProvider::isRhdcHackDownloaded( const string &hackId ) {
static SqlCommand s_cmd( R"#(
SELECT 1 FROM RHDC_HACK_VERSIONS
INNER JOIN ROM_PATHS USING( sha1 )
WHERE id = ?1
AND local = 1
)#"
);
s_cmd.addParameter( hackId, DATA_REF );
DataRecordIterator iterator = s_cmd.execQuery();
return iterator.moveNext();
}
std::vector<string> DataProvider::fetchRhdcPlaylists() {
static SqlCommand s_cmd( R"#(
SELECT DISTINCT group_name FROM RHDC_HACKS
@@ -1988,6 +2002,41 @@ AND sha1 = ?2
s_cmd.execNonQuery();
}
void DataProvider::enableCloudSave( const string &hackId ) noexcept {
static SqlCommand s_cmd( R"#(
DELETE FROM DISABLED_CLOUD_SAVES
WHERE hack_id = ?1
)#"
);
s_cmd.addParameter( hackId, DATA_REF );
s_cmd.execNonQuery();
}
void DataProvider::disableCloudSave( const string &hackId ) noexcept {
static SqlCommand s_cmd( R"#(
INSERT OR IGNORE INTO DISABLED_CLOUD_SAVES( hack_id )
VALUES( ?1 )
)#"
);
s_cmd.addParameter( hackId, DATA_REF );
s_cmd.execNonQuery();
}
bool DataProvider::cloudSaveEnabled( const string &hackId ) noexcept {
static SqlCommand s_cmd( R"#(
SELECT hack_id FROM GROUP_SAVES
EXCEPT
SELECT hack_id FROM DISABLED_CLOUD_SAVES
)#"
);
s_cmd.addParameter( hackId, DATA_REF );
return s_cmd.execQuery().moveNext();
}
void DataProvider::addCrashLog( int64 timestamp, const string &report ) noexcept {
static SqlCommand s_cmd( R"#(
INSERT OR IGNORE INTO CRASH_LOGS( crash_date, report )

View File

@@ -151,6 +151,7 @@ namespace DataProvider {
extern std::vector<RhdcHackExt> fetchExtRhdcHacksInList( const string &playlist, const string &searchText );
extern std::vector<RhdcHackVersion> fetchRhdcHackVersions( const string &hackId );
extern std::vector<fs::path> fetchRomPathsForRhdcHack( const string &hackId );
extern bool isRhdcHackDownloaded( const string &hackId );
extern std::vector<string> fetchRhdcPlaylists();
extern string getLastPlayedVersion( const string &hackId ) noexcept;
@@ -168,6 +169,9 @@ namespace DataProvider {
extern bool isSaveGroupEmpty( const string &hackId ) noexcept;
extern void addAllVersionsToSaveGroup( const string &hackId ) noexcept;
extern void removeFromSaveGroup( const string &hackId, const string &sha1 ) noexcept;
extern void enableCloudSave( const string &hackId ) noexcept;
extern void disableCloudSave( const string &hackId ) noexcept;
extern bool cloudSaveEnabled( const string &hackId ) noexcept;
/* logging */

View File

@@ -155,6 +155,10 @@ CREATE TABLE IF NOT EXISTS CRASH_LOGS(
);
CREATE INDEX IF NOT EXISTS ix_unreported_crash_logs ON CRASH_LOGS( crash_date ) WHERE sent_at IS NULL;
CREATE TABLE IF NOT EXISTS DISABLED_CLOUD_SAVES(
hack_id TEXT NOT NULL PRIMARY KEY
);
)#";
class DbConnection final {

View File

@@ -6,6 +6,7 @@
#include "src/core/logging.hpp"
#include "src/core/time.hpp"
#include "src/polyfill/backtrace.hpp"
#include "src/polyfill/filetime.hpp"
#include "src/polyfill/file.hpp"
#include "src/db/data-provider.hpp"
#include <exception>
@@ -233,13 +234,8 @@ void storeCrashLog() {
const fs::path crashlogPath = fs::to_path( getAppContext().crashlogFilepath );
if( !fs::exists( crashlogPath, err ) || err ) return;
#ifdef __APPLE__
const int64 timestamp = (int64)fs::last_write_time( crashlogPath, err );
#else
const int64 timestamp = Time::toUnixTime( fs::last_write_time( crashlogPath, err ) );
#endif
if( err ) return;
const int64 timestamp = Time::getLastModifiedSec( crashlogPath );
if( !timestamp ) return;
string report;
report.reserve( 4096 );

73
src/polyfill/filetime.cpp Normal file
View File

@@ -0,0 +1,73 @@
#include "src/polyfill/filetime.hpp"
#include <sys/stat.h>
#ifdef _WIN32
#include <Windows.h>
int64 Time::getLastModifiedSec( const fs::path &path ) {
struct __stat64 fileInfo;
if( _wstat64( path.wstring().c_str(), &fileInfo ) < 0 ) {
return 0ll;
}
return (int64)fileInfo.st_mtime;
}
int64 Time::getLastModifiedMs( const fs::path &path ) {
HANDLE fd = CreateFileW(
path.wstring().c_str(),
GENERIC_READ,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr
);
if( !fd ) {
return 1000ll * getLastModifiedSec( path );
}
FILETIME ts;
if( !GetFileTime( fd, nullptr, nullptr, &ts ) ) {
CloseHandle( fd );
return 1000ll * getLastModifiedSec( path );
}
CloseHandle( fd );
ULARGE_INTEGER ts64;
ts64.LowPart = ts.dwLowDateTime;
ts64.HighPart = ts.dwHighDateTime;
const int64 ticks = (int64)ts64.QuadPart;
return (ticks / 10000ll) - 11644473600000ll;
}
#else
static inline timespec getLastModifiedTs( const fs::path &path ) {
struct stat fileInfo;
if( ::stat( path.c_str(), &fileInfo ) < 0 ) {
return { 0ll, 0ll };
}
#ifdef __APPLE__
return fileInfo.st_mtimespec;
#else
return fileInfo.st_mtim;
#endif
}
int64 Time::getLastModifiedSec( const fs::path &path ) {
return (int64)getLastModifiedTs( path ).tv_sec;
}
int64 Time::getLastModifiedMs( const fs::path &path ) {
const timespec ts = getLastModifiedTs( path );
return (1000ll * (int64)ts.tv_sec) + ((int64)ts.tv_nsec / 1000000ll);
}
#endif

14
src/polyfill/filetime.hpp Normal file
View File

@@ -0,0 +1,14 @@
#ifndef SRC_POLYFILL_FILETIME_HPP_
#define SRC_POLYFILL_FILETIME_HPP_
#include "src/core/filesystem.hpp"
#include "src/types.hpp"
namespace Time {
extern int64 getLastModifiedSec( const fs::path &path );
extern int64 getLastModifiedMs( const fs::path &path );
}
#endif

View File

@@ -0,0 +1,458 @@
#include "src/rhdc/core/cloud-saves.hpp"
#include <QSaveFile>
#include <QTimeZone>
#include <QFile>
#include <unordered_map>
#include <fstream>
#include <chrono>
#include "src/polyfill/file.hpp"
#include "src/polyfill/filetime.hpp"
#include "src/db/data-provider.hpp"
#include "src/core/file-controller.hpp"
#include "src/core/memory-stream.hpp"
#include "src/core/retroarch.hpp"
#include "src/core/fast-lzip.hpp"
#include "src/core/logging.hpp"
#include "src/core/time.hpp"
#include "src/core/sm64.hpp"
#include "src/rhdc/web/api.hpp"
#include "src/rhdc/web/api-helpers.hpp"
#include "src/rhdc/core/layout.hpp"
#include "src/rhdc/core/credentials.hpp"
using namespace __RhdcCloudSave_Private;
static HashMap<string, RhdcCloudSave> g_cloudSaves;
static const RhdcCloudSave g_unknownCloudSave = { CloudSaveStatus::Unknown, 0ll, 0ll, {}, {} };
static bool g_cloudSavesInitialized = false;
void RhdcCloudSave::markInitialized() {
g_cloudSavesInitialized = true;
}
bool RhdcCloudSave::areEnabled() {
return FileController::loadRhdcSettings().enableCloudSaves && RhdcCredentials::exists();
}
bool RhdcCloudSave::isEnabled( const string &hackId ) {
return RhdcCloudSave::areEnabled() && DataProvider::cloudSaveEnabled( hackId );
}
const RhdcCloudSave &RhdcCloudSave::getInfo( const string &hackId ) {
const auto &saveInfo = g_cloudSaves.find( hackId );
return (saveInfo == g_cloudSaves.end()) ? g_unknownCloudSave : saveInfo->second;
}
void RhdcCloudSave::clearAll() {
g_cloudSaves.clear();
CloudSaveInfoEventDispatcher::instance()->fireClearEvent();
}
static inline QString getSaveFilePath( const string &hackId ) {
#ifdef _WIN32
return QString::fromUtf16( RetroArch::getGroupSaveFilePath( hackId ).u16string().c_str() );
#else
return QString::fromUtf8( RetroArch::getGroupSaveFilePath( hackId ).u8string().c_str() );
#endif
}
static bool updateSaveFromCloud( const string &hackId, QIODevice &cloudSave ) {
QSaveFile saveFile( getSaveFilePath( hackId ) );
if( !saveFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) ) {
logError( "Failed to open save file for writing: "s + saveFile.fileName().toStdString() );
return false;
}
if( !LZip::decompress( cloudSave, saveFile ) ) {
logError( "Failed to decompress cloud save file" );
return false;
}
if( !saveFile.flush() ) {
logError( "Failed to write save data to disk: "s + saveFile.fileName().toStdString() );
return false;
}
if( saveFile.pos() != 0x48800ll ) {
logError( "Cloud save data appears to be corrupt (unexpected filesize)" );
return false;
}
return saveFile.commit();
}
static QByteArray tryFetchAndCompressLocalSave( const string &hackId ) {
QFile saveFile( getSaveFilePath( hackId ) );
if( !saveFile.open( QIODevice::ReadOnly | QIODevice::ExistingOnly ) ) {
return QByteArray();
}
QtMemoryStream compressedData( 4095, 6 );
if( !LZip::compressFast( saveFile, compressedData ) || compressedData.size() < 20 ) {
logError( "Failed to compress local save data" );
return QByteArray();
}
return compressedData.buffer();
}
static std::vector<int> getLocalStarCounts( const string &hackId ) {
std::vector<int> starCounts;
const fs::path saveFilePath = RetroArch::getGroupSaveFilePath( hackId );
if( !fs::existsSafe( saveFilePath ) ) return starCounts;
if( !StarLayout::hasLayout( hackId ) ) {
SM64::SaveFile saveFile;
InputFile file( saveFilePath, true );
saveFile.read( file );
if( file.bad() && !file.eof() ) {
return starCounts;
}
starCounts.reserve( 4 );
for( int i = 0; i < 4; i++ ) {
if( Flags::has( saveFile.slot( i ).flags, SM64::SaveFileFlag::Exists ) ) {
starCounts.push_back( saveFile.slot( i ).countStars() );
} else {
starCounts.push_back( 0 );
}
}
return starCounts;
}
StarLayout layout;
if( !StarLayout::tryLoadLayout( hackId, layout ) ) {
return starCounts;
}
return layout.countAllStars( saveFilePath );
}
static inline bool setFileTime( const fs::path filePath, int64 unixTime ) {
const int64 epochDiff = Time::now() - (int64)std::chrono::duration_cast<std::chrono::seconds>( fs::file_time_type::clock::now().time_since_epoch() ).count();
const auto fileTime = fs::file_time_type::clock::time_point() + std::chrono::seconds( unixTime - epochDiff );
fs::error_code err;
fs::last_write_time( filePath, fileTime, err );
return !err;
}
static inline bool countsNotGreater( const std::vector<int> &a, const std::vector<int> &b ) {
if( a.size() != b.size() ) return false;
for( size_t i = 0; i < a.size(); i++ ) {
if( a[i] > b[i] ) return false;
}
return true;
}
static inline void emitChangeEvent( const string &hackId ) {
CloudSaveInfoEventDispatcher::instance()->fireChangeEvent( hackId );
}
static std::optional<int64> getLocalSaveDate( const string &hackId ) {
const fs::path localSavePath = RetroArch::getGroupSaveFilePath( hackId );
fs::error_code err;
if( !fs::is_regular_file( localSavePath, err ) || err ) return std::optional<int64>();
return Time::getLastModifiedSec( localSavePath );
}
void RhdcCloudSave::synchronizeSaveFile(
const string &hackId,
int64 cloudSaveDate,
const std::vector<int> &cloudSaveStars,
const std::function<void(CloudSaveStatus)> &callback
) {
RhdcCloudSave &saveInfo = g_cloudSaves[hackId];
saveInfo.cloudTimestamp = cloudSaveDate;
saveInfo.cloudProgress = cloudSaveStars;
saveInfo.localTimestamp = getLocalSaveDate( hackId );
if( saveInfo.localTimestamp.has_value() && saveInfo.localTimestamp.value() == cloudSaveDate ) {
saveInfo.status = CloudSaveStatus::Synced;
} else {
saveInfo.localProgress = getLocalStarCounts( hackId );
if( ApiUtil::identity().role == RhdcRole::Unverified ) {
saveInfo.status = CloudSaveStatus::Offline;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Offline );
return;
}
saveInfo.status = CloudSaveStatus::Syncing;
if( !saveInfo.localTimestamp.has_value() || (saveInfo.localTimestamp.value() < cloudSaveDate && countsNotGreater( saveInfo.localProgress, cloudSaveStars )) ) {
RhdcApi::fetchCloudSave(
hackId,
saveInfo.localTimestamp.has_value() ? QDateTime::fromSecsSinceEpoch( saveInfo.localTimestamp.value(), QTimeZone::utc() ) : QDateTime(),
[hackId,callback](QIODevice &cloudSave, const QDateTime &cloudSaveDate) {
if( updateSaveFromCloud( hackId, cloudSave ) ) {
RhdcCloudSave &save = g_cloudSaves[hackId];
save.localTimestamp = save.cloudTimestamp = cloudSaveDate.toSecsSinceEpoch();
save.localProgress = getLocalStarCounts( hackId );
setFileTime( RetroArch::getGroupSaveFilePath( hackId ), save.localTimestamp.value() );
save.status = CloudSaveStatus::Synced;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Synced );
} else {
g_cloudSaves[hackId].status = CloudSaveStatus::Errored;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Errored );
}
},
[hackId,callback](const QDateTime &cloudSaveDate) {
setFileTime( RetroArch::getGroupSaveFilePath( hackId ), cloudSaveDate.toSecsSinceEpoch() );
g_cloudSaves[hackId].status = CloudSaveStatus::Synced;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Synced );
},
[hackId,callback](ApiErrorType) {
g_cloudSaves[hackId].status = CloudSaveStatus::Errored;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Errored );
}
);
} else if( saveInfo.localTimestamp.has_value() && saveInfo.localTimestamp.value() > cloudSaveDate && countsNotGreater( cloudSaveStars, saveInfo.localProgress ) ) {
const QByteArray compressedSave = tryFetchAndCompressLocalSave( hackId );
if( compressedSave.isNull() ) {
saveInfo.status = CloudSaveStatus::Errored;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Errored );
return;
}
RhdcApi::pushCloudSave(
hackId,
compressedSave,
QDateTime::fromSecsSinceEpoch( saveInfo.localTimestamp.value(), QTimeZone::utc() ),
saveInfo.localProgress,
[hackId,callback]() {
RhdcCloudSave &save = g_cloudSaves[hackId];
save.cloudTimestamp = save.localTimestamp;
save.cloudProgress = save.localProgress;
save.status = CloudSaveStatus::Synced;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Synced );
},
[hackId,callback](const QDateTime &cloudSaveDate, const std::vector<int> &cloudSaveStars) {
RhdcCloudSave &save = g_cloudSaves[hackId];
save.cloudTimestamp = cloudSaveDate.toSecsSinceEpoch();
save.cloudProgress = cloudSaveStars;
save.status = CloudSaveStatus::Desynced;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Desynced );
},
[hackId,callback](ApiErrorType){
g_cloudSaves[hackId].status = CloudSaveStatus::Errored;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Errored );
}
);
} else {
saveInfo.status = CloudSaveStatus::Desynced;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Desynced );
return;
}
}
emitChangeEvent( hackId );
}
void RhdcCloudSave::synchronizeSaveFile(
const string &hackId,
const std::function<void(CloudSaveStatus)> &callback
) {
if( !g_cloudSavesInitialized ) return;
RhdcCloudSave &saveInfo = g_cloudSaves[hackId];
saveInfo.localTimestamp = getLocalSaveDate( hackId );
if( !saveInfo.localTimestamp.has_value() ) {
saveInfo.status = CloudSaveStatus::NoData;
emitChangeEvent( hackId );
callback( CloudSaveStatus::NoData );
return;
}
if( ApiUtil::identity().role == RhdcRole::Unverified ) {
saveInfo.status = CloudSaveStatus::Offline;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Offline );
return;
}
saveInfo.localProgress = getLocalStarCounts( hackId );
if( saveInfo.cloudTimestamp == saveInfo.localTimestamp && saveInfo.cloudProgress == saveInfo.localProgress ) {
saveInfo.status = CloudSaveStatus::Synced;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Synced );
return;
}
const QByteArray compressedSave = tryFetchAndCompressLocalSave( hackId );
if( compressedSave.isNull() ) {
saveInfo.localProgress.clear();
saveInfo.status = CloudSaveStatus::Errored;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Errored );
return;
}
saveInfo.status = CloudSaveStatus::Syncing;
emitChangeEvent( hackId );
RhdcApi::pushCloudSave(
hackId,
compressedSave,
QDateTime::fromSecsSinceEpoch( saveInfo.localTimestamp.value(), QTimeZone::utc() ),
saveInfo.localProgress,
[hackId, callback](){
RhdcCloudSave &save = g_cloudSaves[hackId];
save.cloudTimestamp = save.localTimestamp;
save.cloudProgress = save.localProgress;
save.status = CloudSaveStatus::Synced;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Synced );
},
[hackId, callback]( const QDateTime &cloudTimestamp, const std::vector<int> cloudProgress ){
RhdcCloudSave &save = g_cloudSaves[hackId];
save.cloudTimestamp = cloudTimestamp.toSecsSinceEpoch();
save.cloudProgress = cloudProgress;
save.status = CloudSaveStatus::Desynced;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Desynced );
},
[hackId, callback](ApiErrorType err ) {
const CloudSaveStatus status = (err == ApiErrorType::Offline) ? CloudSaveStatus::Offline : CloudSaveStatus::Errored;
g_cloudSaves[hackId].status = status;
emitChangeEvent( hackId );
callback( status );
}
);
}
void RhdcCloudSave::forceSyncLocalToCloud(
const string &hackId,
const std::function<void(CloudSaveStatus)> &callback
) {
RhdcCloudSave &saveInfo = g_cloudSaves[hackId];
saveInfo.localTimestamp = getLocalSaveDate( hackId );
if( !saveInfo.localTimestamp.has_value() ) {
saveInfo.localProgress.clear();
saveInfo.cloudProgress.clear();
saveInfo.cloudTimestamp.reset();
saveInfo.status = CloudSaveStatus::Syncing;
emitChangeEvent( hackId );
RhdcApi::deleteCloudSave(
hackId,
[hackId, callback](){
g_cloudSaves[hackId].status = CloudSaveStatus::NoData;
emitChangeEvent( hackId );
callback( CloudSaveStatus::NoData );
},
[hackId, callback](ApiErrorType err) {
const CloudSaveStatus status = (err == ApiErrorType::Offline) ? CloudSaveStatus::Offline : CloudSaveStatus::Errored;
g_cloudSaves[hackId].status = status;
emitChangeEvent( hackId );
callback( status );
}
);
return;
}
saveInfo.localProgress = getLocalStarCounts( hackId );
if( saveInfo.localProgress.empty() ) {
saveInfo.status = CloudSaveStatus::Errored;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Errored );
return;
}
const QByteArray compressedSave = tryFetchAndCompressLocalSave( hackId );
if( compressedSave.isNull() ) {
saveInfo.status = CloudSaveStatus::Errored;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Errored );
return;
}
saveInfo.status = CloudSaveStatus::Syncing;
emitChangeEvent( hackId );
RhdcApi::forcePushCloudSave(
hackId,
compressedSave,
QDateTime::fromSecsSinceEpoch( saveInfo.localTimestamp.value(), QTimeZone::utc() ),
saveInfo.localProgress,
[hackId, callback]() {
RhdcCloudSave &save = g_cloudSaves[hackId];
save.cloudTimestamp = save.localTimestamp;
save.cloudProgress = save.localProgress;
save.status = CloudSaveStatus::Synced;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Synced );
},
[hackId, callback](ApiErrorType err) {
const CloudSaveStatus status = (err == ApiErrorType::Offline) ? CloudSaveStatus::Offline : CloudSaveStatus::Errored;
g_cloudSaves[hackId].status = status;
emitChangeEvent( hackId );
callback( status );
}
);
}
void RhdcCloudSave::forceSyncCloudToLocal(
const string &hackId,
const std::function<void(CloudSaveStatus)> &callback
) {
g_cloudSaves[hackId].status = CloudSaveStatus::Syncing;
emitChangeEvent( hackId );
RhdcApi::fetchCloudSave(
hackId,
QDateTime(),
[hackId,callback](QIODevice &cloudSave, const QDateTime &lastModified) {
if( updateSaveFromCloud( hackId, cloudSave ) ) {
RhdcCloudSave &save = g_cloudSaves[hackId];
save.localTimestamp = save.cloudTimestamp = lastModified.toSecsSinceEpoch();
save.localProgress = getLocalStarCounts( hackId );
setFileTime( RetroArch::getGroupSaveFilePath( hackId ), save.localTimestamp.value() );
save.status = CloudSaveStatus::Synced;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Synced );
} else {
g_cloudSaves[hackId].status = CloudSaveStatus::Errored;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Errored );
}
},
[hackId,callback](const QDateTime&) {
g_cloudSaves[hackId].status = CloudSaveStatus::Errored;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Errored );
},
[hackId,callback](ApiErrorType err) {
fs::error_code fserr;
if( err == ApiErrorType::Offline ) {
g_cloudSaves[hackId].status = CloudSaveStatus::Offline;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Offline );
} else if( err != ApiErrorType::NotFound || fs::is_regular_file( RetroArch::getGroupSaveFilePath( hackId ), fserr ) || fserr ) {
g_cloudSaves[hackId].status = CloudSaveStatus::Errored;
emitChangeEvent( hackId );
callback( CloudSaveStatus::Errored );
} else {
g_cloudSaves[hackId].status = CloudSaveStatus::NoData;
emitChangeEvent( hackId );
callback( CloudSaveStatus::NoData );
}
}
);
}

View File

@@ -0,0 +1,101 @@
#ifndef SRC_RHDC_CORE_CLOUD_SAVES_HPP_
#define SRC_RHDC_CORE_CLOUD_SAVES_HPP_
#include <functional>
#include <optional>
#include <cstdint>
#include <vector>
#include <QString>
#include <QObject>
#include "src/types.hpp"
enum class CloudSaveStatus : int_fast8_t {
Unknown = 0,
Syncing,
Synced,
NoData,
Desynced,
Errored,
Unverified,
Offline
};
namespace __RhdcCloudSave_Private {
class CloudSaveInfoEventDispatcher : public QObject {
Q_OBJECT
private:
inline CloudSaveInfoEventDispatcher() : QObject( nullptr ) {}
inline ~CloudSaveInfoEventDispatcher() {}
public:
inline void fireChangeEvent( const string &hackId ) {
emit statusChanged( QString::fromUtf8( hackId.c_str() ) );
}
inline void fireClearEvent() {
emit statusChanged( QString() );
}
inline static CloudSaveInfoEventDispatcher *instance() {
static CloudSaveInfoEventDispatcher singleton;
return &singleton;
}
signals:
void statusChanged( QString hackId );
};
}
struct RhdcCloudSave {
CloudSaveStatus status;
std::optional<int64> localTimestamp;
std::optional<int64> cloudTimestamp;
std::vector<int> localProgress;
std::vector<int> cloudProgress;
static void markInitialized();
static bool isEnabled( const string &hackId );
static bool areEnabled();
static void synchronizeSaveFile(
const string &hackId,
int64 cloudSaveDate,
const std::vector<int> &cloudSaveStars,
const std::function<void(CloudSaveStatus)> &callback
);
static void synchronizeSaveFile(
const string &hackId,
const std::function<void(CloudSaveStatus)> &callback
);
static void forceSyncLocalToCloud(
const string &hackId,
const std::function<void(CloudSaveStatus)> &callback
);
static void forceSyncCloudToLocal(
const string &hackId,
const std::function<void(CloudSaveStatus)> &callback
);
static const RhdcCloudSave &getInfo( const string &hackId );
static void clearAll();
template<typename T> static inline void registerChangeListener( T *receiver, void(T::*slot)(QString) ) {
QObject::connect(
__RhdcCloudSave_Private::CloudSaveInfoEventDispatcher::instance(),
&__RhdcCloudSave_Private::CloudSaveInfoEventDispatcher::statusChanged,
receiver,
slot
);
}
};
#endif /* SRC_RHDC_CORE_CLOUD_SAVES_HPP_ */

View File

@@ -444,6 +444,8 @@ static inline int bitcount( uint flags ) {
}
static uint countStarsForSlot( const StarLayout &layout, const Buffer &saveData, uint slotIndex ) {
if( (saveData.udata()[layout.activeBit / 8u] & (0x80u >> (layout.activeBit % 8u))) == 0 ) return 0;
uint stars = 0;
for( const StarLayout::Group &group : layout.groups ) {
for( const StarLayout::Course &course : group.courses ) {
@@ -475,6 +477,20 @@ uint StarLayout::countStars( const fs::path &saveFilePath ) const {
}
}
std::vector<int> StarLayout::countAllStars( const fs::path &saveFilePath ) const {
InputFile saveFile( saveFilePath, true );
Buffer saveData = getUsedSaveData( saveFile );
std::vector<int> starCounts;
starCounts.reserve( numSlots );
for( ushort i = 0; i < (ushort)numSlots; i++ ) {
starCounts.push_back( countStarsForSlot( *this, saveData, i ) );
}
return starCounts;
}
StarLayout StarLayout::createDefault() {
return StarLayout{
/* numSlots */ 4u,

View File

@@ -54,6 +54,7 @@ struct StarLayout {
Buffer getUsedSaveData( std::istream &saveFile ) const;
void writeSaveData( std::ostream &saveFile, Buffer &saveData ) const;
uint countStars( const fs::path &saveFilePath ) const;
std::vector<int> countAllStars( const fs::path &saveFilePath ) const;
static bool tryLoadLayout( const string &hackId, StarLayout &layout );
static bool hasLayout( const string &hackId );

View File

@@ -10,7 +10,8 @@ const RhdcSettings &RhdcSettings::Default() {
/* checkAllSaveSlots */ false,
/* preferHle */ false,
/* enableDefaultLayouts */ true,
/* ignoreWidescreenHint */ false
/* ignoreWidescreenHint */ false,
/* enableCloudSaves */ false
};
return s_default;
}
@@ -21,6 +22,7 @@ static constexpr char P_ALL_SAVE_SLOTS[] = "check_all_save_slots";
static constexpr char P_PREFER_HLE[] = "perfer_hle";
static constexpr char P_ENABLE_DEFAULT_LAYOUTS[] = "enable_default_layouts";
static constexpr char P_IGNORE_WIDESCREEN[] = "ignore_widescreen_hint";
static constexpr char P_ENABLE_CLOUD_SAVES[] = "enable_cloud_saves";
template<> void JsonSerializer::serialize<RhdcSettings>( JsonWriter &jw, const RhdcSettings &obj ) {
jw.writeObjectStart();
@@ -30,6 +32,7 @@ template<> void JsonSerializer::serialize<RhdcSettings>( JsonWriter &jw, const R
jw.writeProperty( P_PREFER_HLE, obj.preferHle );
jw.writeProperty( P_ENABLE_DEFAULT_LAYOUTS, obj.enableDefaultLayouts );
jw.writeProperty( P_IGNORE_WIDESCREEN, obj.ignoreWidescreenHint );
jw.writeProperty( P_ENABLE_CLOUD_SAVES, obj.enableCloudSaves );
jw.writeObjectEnd();
}
@@ -41,6 +44,7 @@ template<> RhdcSettings JsonSerializer::parse<RhdcSettings>( const Json &json )
json[P_ALL_SAVE_SLOTS].getOrDefault<bool>( RhdcSettings::Default().checkAllSaveSlots ),
SteamDeck::isSteamDeck() ? false : json[P_PREFER_HLE].getOrDefault<bool>( RhdcSettings::Default().preferHle ),
json[P_ENABLE_DEFAULT_LAYOUTS].getOrDefault<bool>( RhdcSettings::Default().enableDefaultLayouts ),
json[P_IGNORE_WIDESCREEN].getOrDefault<bool>( RhdcSettings::Default().ignoreWidescreenHint )
json[P_IGNORE_WIDESCREEN].getOrDefault<bool>( RhdcSettings::Default().ignoreWidescreenHint ),
json[P_ENABLE_CLOUD_SAVES].getOrDefault<bool>( RhdcSettings::Default().enableCloudSaves )
};
}

View File

@@ -11,6 +11,7 @@ struct RhdcSettings {
bool preferHle;
bool enableDefaultLayouts;
bool ignoreWidescreenHint;
bool enableCloudSaves;
static const RhdcSettings &Default();
};

View File

@@ -4,6 +4,8 @@
#include <QCoreApplication>
#include <QFile>
#include "src/polyfill/base-directory.hpp"
#include "src/polyfill/filetime.hpp"
#include "src/rhdc/core/cloud-saves.hpp"
#include "src/rhdc/core/layout.hpp"
#include "src/rhdc/web/api.hpp"
#include "src/core/preset-controllers.hpp"
@@ -59,7 +61,7 @@ static inline bool shouldDownloadLayout( const FollowedHack &hack ) {
if( hack.layoutUrl.empty() ) return false;
if( !StarLayout::hasLayout( hack.info.hackId ) ) return true;
if( hack.layoutLastModified <= 0 ) return false;
return hack.layoutLastModified > RomUtil::getLastModified( StarLayout::layoutPath( hack.info.hackId ) );
return hack.layoutLastModified > Time::getLastModifiedSec( StarLayout::layoutPath( hack.info.hackId ) );
}
static inline const RhdcHackVersionExt &getLatestVersion( const std::vector<RhdcHackVersionExt> &versions ) {
@@ -76,6 +78,7 @@ void RHDC::sync( const std::function<void(bool)> &callback ) {
[callback](HashMap<string,FollowedHack> &hacks) {
SqlTransaction transaction;
RhdcCloudSave::clearAll();
DataProvider::clearRhdcHacks();
const HashSet<string> knownHashes = DataProvider::fetchAllKnownChecksums();
std::vector<string> undownloadedHacks;
@@ -142,8 +145,17 @@ void RHDC::sync( const std::function<void(bool)> &callback ) {
}, RhdcApi::logApiError( "Failed to download star layout" ) );
}
}
if( RhdcCloudSave::isEnabled( hack.info.hackId ) && DataProvider::isRhdcHackDownloaded( hack.info.hackId ) ) {
if( hack.cloudSaveDate.has_value() && !hack.cloudSaveStars.empty() ) {
RhdcCloudSave::synchronizeSaveFile( hack.info.hackId, hack.cloudSaveDate.value(), hack.cloudSaveStars, [](CloudSaveStatus){} );
} else {
RhdcCloudSave::synchronizeSaveFile( hack.info.hackId, [](CloudSaveStatus){} );
}
}
}
transaction.commit();
RhdcCloudSave::markInitialized();
for( const string &hackId : undownloadedHacks ) {
const FollowedHack &hack = hacks.at( hackId );

View File

@@ -0,0 +1,375 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>RhdcCloudDesyncDialog</class>
<widget class="QDialog" name="RhdcCloudDesyncDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>450</width>
<height>369</height>
</rect>
</property>
<property name="windowTitle">
<string>Cloud Save Desynced</string>
</property>
<property name="sizeGripEnabled">
<bool>true</bool>
</property>
<property name="modal">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Your local save file is out of sync with your RHDC cloud save.</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Local Save</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_2" columnstretch="0,1">
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Last Modified</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="localLastModified">
<property name="text">
<string notr="true">Thu Jan 1 1970 00:00:00</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Stars</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="localStars">
<property name="text">
<string notr="true">0, 0, 0, 0</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="localSaveButton">
<property name="text">
<string>Use Local Save (replaces cloud save)</string>
</property>
<property name="icon">
<iconset theme="computer">
<normaloff>.</normaloff>.</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Cloud Save</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QGridLayout" name="gridLayout" columnstretch="0,1">
<item row="0" column="0">
<widget class="QLabel" name="label_8">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Last Modified</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="cloudLastModified">
<property name="text">
<string notr="true">Thu Jan 1 1970 00:00:00</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_9">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Stars</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="cloudStars">
<property name="text">
<string notr="true">0, 0, 0, 0</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="cloudSaveButton">
<property name="text">
<string>Use Cloud Save (replaces local save)</string>
</property>
<property name="icon">
<iconset theme="folder-cloud">
<normaloff>.</normaloff>.</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="openSaveButton">
<property name="text">
<string>Open Save Folder</string>
</property>
<property name="icon">
<iconset theme="document-open">
<normaloff>.</normaloff>.</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="ignoreButton">
<property name="text">
<string>Continue (do not sync)</string>
</property>
<property name="icon">
<iconset theme="vcs-merge">
<normaloff>.</normaloff>.</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cancelButton">
<property name="text">
<string>Cancel</string>
</property>
<property name="icon">
<iconset theme="dialog-cancel">
<normaloff>.</normaloff>.</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>cancelButton</sender>
<signal>clicked()</signal>
<receiver>RhdcCloudDesyncDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>421</x>
<y>343</y>
</hint>
<hint type="destinationlabel">
<x>447</x>
<y>323</y>
</hint>
</hints>
</connection>
<connection>
<sender>ignoreButton</sender>
<signal>clicked()</signal>
<receiver>RhdcCloudDesyncDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>252</x>
<y>353</y>
</hint>
<hint type="destinationlabel">
<x>447</x>
<y>364</y>
</hint>
</hints>
</connection>
<connection>
<sender>localSaveButton</sender>
<signal>clicked()</signal>
<receiver>RhdcCloudDesyncDialog</receiver>
<slot>chooseLocal()</slot>
<hints>
<hint type="sourcelabel">
<x>160</x>
<y>136</y>
</hint>
<hint type="destinationlabel">
<x>448</x>
<y>28</y>
</hint>
</hints>
</connection>
<connection>
<sender>cloudSaveButton</sender>
<signal>clicked()</signal>
<receiver>RhdcCloudDesyncDialog</receiver>
<slot>chooseCloud()</slot>
<hints>
<hint type="sourcelabel">
<x>228</x>
<y>264</y>
</hint>
<hint type="destinationlabel">
<x>450</x>
<y>75</y>
</hint>
</hints>
</connection>
<connection>
<sender>openSaveButton</sender>
<signal>clicked()</signal>
<receiver>RhdcCloudDesyncDialog</receiver>
<slot>openSaveFolder()</slot>
<hints>
<hint type="sourcelabel">
<x>87</x>
<y>347</y>
</hint>
<hint type="destinationlabel">
<x>141</x>
<y>366</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>chooseLocal()</slot>
<slot>chooseCloud()</slot>
<slot>openSaveFolder()</slot>
</slots>
</ui>

View File

@@ -17,7 +17,7 @@
<item>
<widget class="QStackedWidget" name="pages">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="needsBaseRomPage">
<layout class="QVBoxLayout" name="verticalLayout_3">
@@ -563,8 +563,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>385</width>
<height>693</height>
<width>476</width>
<height>737</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
@@ -646,6 +646,34 @@
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="cloudSaveCheckbox">
<property name="text">
<string>Enable Cloud Saves</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_14">
<property name="font">
<font>
<pointsize>9</pointsize>
</font>
</property>
<property name="text">
<string>If checked, your save files will be backed up on romhacking.com, and will be kept up to date. You must verify your e-mail on your RHDC account to use this feature.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="buddy">
<cstring>cloudSaveCheckbox</cstring>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="enableStarDisplayCheckbox">
<property name="text">
@@ -1000,8 +1028,8 @@
<slot>browseForRom()</slot>
<hints>
<hint type="sourcelabel">
<x>348</x>
<y>339</y>
<x>111</x>
<y>56</y>
</hint>
<hint type="destinationlabel">
<x>525</x>
@@ -1048,12 +1076,12 @@
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>118</x>
<y>204</y>
<x>130</x>
<y>303</y>
</hint>
<hint type="destinationlabel">
<x>74</x>
<y>301</y>
<x>123</x>
<y>402</y>
</hint>
</hints>
</connection>

View File

@@ -0,0 +1,125 @@
#include "src/rhdc/ui/rhdc-cloud-desync-dialog.hpp"
#include "ui_rhdc-cloud-desync-dialog.h"
#include <QMessageBox>
#include <QDateTime>
#include "src/polyfill/file-explorer.hpp"
#include "src/rhdc/core/cloud-saves.hpp"
#include "src/core/numeric-string.hpp"
#include "src/core/retroarch.hpp"
#include "src/ui/icons.hpp"
#include "src/ui/util.hpp"
static inline QString formatStarList( const std::vector<int> &list ) {
if( list.empty() ) return QString();
string str;
for( size_t i = 0; i < list.size() - 1; i++ ) {
str += Number::toString( list[i] );
str += ", ";
}
str += Number::toString( list[list.size() - 1] );
return QString::fromStdString( str );
}
RhdcCloudDesyncDialog::RhdcCloudDesyncDialog( QWidget *parent, const string &hackId ) :
QDialog( parent ),
m_hackId( hackId ),
m_ui( new Ui::RhdcCloudDesyncDialog ),
m_alive( new bool( true ) )
{
m_ui->setupUi( this );
UiUtil::scaleWindow( this );
setWindowIcon( Icon::appIcon() );
setModal( true );
m_ui->localSaveButton->setIcon( Icon::computer() );
m_ui->cloudSaveButton->setIcon( Icon::cloud() );
m_ui->openSaveButton->setIcon( Icon::browse() );
m_ui->ignoreButton->setIcon( Icon::resolveDesync() );
m_ui->cancelButton->setIcon( Icon::cancel() );
const RhdcCloudSave &save = RhdcCloudSave::getInfo( hackId );
m_ui->localLastModified->setText( QDateTime::fromSecsSinceEpoch( save.localTimestamp.value_or( 0ll ) ).toString( Qt::TextDate ) );
m_ui->localStars->setText( formatStarList( save.localProgress ) );
m_ui->cloudLastModified->setText( QDateTime::fromSecsSinceEpoch( save.cloudTimestamp.value_or( 0ll ) ).toString( Qt::TextDate ) );
m_ui->cloudStars->setText( formatStarList( save.cloudProgress ) );
}
RhdcCloudDesyncDialog::~RhdcCloudDesyncDialog() {
*m_alive = false;
delete m_ui;
}
bool RhdcCloudDesyncDialog::run( const string &hackId, QWidget *parent ) {
const RhdcCloudSave &save = RhdcCloudSave::getInfo( hackId );
while( save.status == CloudSaveStatus::Syncing ) {
QCoreApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
if( &save != &RhdcCloudSave::getInfo( hackId ) ) {
return run( hackId, parent );
}
}
if( save.status != CloudSaveStatus::Desynced ) return true;
return RhdcCloudDesyncDialog( parent, hackId ).exec() == (int)QDialog::Accepted;
}
static bool showSyncErrorMessage( QWidget *parent ) {
return QMessageBox::critical(
parent,
RhdcCloudDesyncDialog::tr( "Synchronization Error" ),
RhdcCloudDesyncDialog::tr( "Failed to synchronize your save file with RHDC." ),
QMessageBox::Ok,
QMessageBox::Cancel
) == QMessageBox::Ok;
}
void RhdcCloudDesyncDialog::chooseLocal() {
m_ui->localSaveButton->setEnabled( false );
m_ui->cloudSaveButton->setEnabled( false );
m_ui->ignoreButton->setEnabled( false );
m_ui->cancelButton->setEnabled( false );
const std::shared_ptr<bool> &dialogExists = m_alive;
RhdcCloudSave::forceSyncLocalToCloud( m_hackId, [this,dialogExists](CloudSaveStatus status) {
if( !*dialogExists ) return;
if( status != CloudSaveStatus::Synced ) {
this->hide();
if( !showSyncErrorMessage( this ) ) {
this->reject();
return;
}
}
this->accept();
});
}
void RhdcCloudDesyncDialog::chooseCloud() {
m_ui->localSaveButton->setEnabled( false );
m_ui->cloudSaveButton->setEnabled( false );
m_ui->ignoreButton->setEnabled( false );
m_ui->cancelButton->setEnabled( false );
const std::shared_ptr<bool> &dialogExists = m_alive;
RhdcCloudSave::forceSyncCloudToLocal( m_hackId, [this,dialogExists](CloudSaveStatus status) {
if( !*dialogExists ) return;
if( status != CloudSaveStatus::Synced ) {
this->hide();
if( !showSyncErrorMessage( this ) ) {
this->reject();
return;
}
}
this->accept();
});
}
void RhdcCloudDesyncDialog::openSaveFolder() {
FileExplorer::showFile( RetroArch::getGroupSaveFilePath( m_hackId ) );
}

View File

@@ -0,0 +1,33 @@
#ifndef SRC_RHDC_UI_RHDC_CLOUD_DESYNC_DIALOG_HPP_
#define SRC_RHDC_UI_RHDC_CLOUD_DESYNC_DIALOG_HPP_
#include <QDialog>
#include <memory>
#include "src/types.hpp"
namespace Ui {
class RhdcCloudDesyncDialog;
}
class RhdcCloudDesyncDialog : private QDialog {
Q_OBJECT
private:
const string m_hackId;
Ui::RhdcCloudDesyncDialog *m_ui;
std::shared_ptr<bool> m_alive;
RhdcCloudDesyncDialog( QWidget *parent, const string &hackId );
~RhdcCloudDesyncDialog();
public:
static bool run( const string &hackId, QWidget *parent = nullptr );
private slots:
void chooseLocal();
void chooseCloud();
void openSaveFolder();
};
#endif /* SRC_RHDC_UI_RHDC_CLOUD_DESYNC_DIALOG_HPP_ */

View File

@@ -0,0 +1,96 @@
#include "src/rhdc/ui/rhdc-cloud-status-widget.hpp"
#include <QHBoxLayout>
#include "src/rhdc/web/api-helpers.hpp"
#include "src/rhdc/ui/rhdc-cloud-desync-dialog.hpp"
#include "src/rhdc/core/cloud-saves.hpp"
#include "src/ui/icons.hpp"
#include "src/ui/util.hpp"
#include "src/types.hpp"
RhdcCloudStatusWidget::RhdcCloudStatusWidget( QWidget *parent ) :
QWidget( parent ),
m_icon( new QLabel( this ) ),
m_text( new QLabel( this ) ),
m_button( new QPushButton( this ) )
{
QHBoxLayout *layout = new QHBoxLayout( this );
layout->setContentsMargins( 0, 0, 0, 0 );
setLayout( layout );
m_button->setVisible( false );
layout->addWidget( m_icon );
layout->addWidget( m_text );
layout->addWidget( m_button );
layout->addStretch( 1 );
UiUtil::setIconSize( m_button, 16 );
UiUtil::fixFontSizeOnMac( m_text );
connect( m_button, &QPushButton::clicked, this, &RhdcCloudStatusWidget::onButtonClick );
RhdcCloudSave::registerChangeListener( this, &RhdcCloudStatusWidget::onStatusChange );
}
RhdcCloudStatusWidget::~RhdcCloudStatusWidget() {
UiUtil::clearLayout( layout() );
layout()->deleteLater();
}
void RhdcCloudStatusWidget::setHackId( const std::string &hackId ) {
m_hackId = hackId;
refresh();
}
void RhdcCloudStatusWidget::onStatusChange( QString hackId ) {
if( hackId.isNull() || hackId.toStdString() == m_hackId ) refresh();
}
void RhdcCloudStatusWidget::onButtonClick() {
switch( RhdcCloudSave::getInfo( m_hackId ).status ) {
case CloudSaveStatus::Syncing:
break;
case CloudSaveStatus::Desynced:
RhdcCloudDesyncDialog::run( m_hackId, window() );
break;
default:
RhdcCloudSave::synchronizeSaveFile( m_hackId, [](CloudSaveStatus){} );
break;
}
}
void RhdcCloudStatusWidget::refresh() {
const RhdcCloudSave &save = RhdcCloudSave::getInfo( m_hackId );
m_button->setVisible( save.status == CloudSaveStatus::Errored || save.status == CloudSaveStatus::Desynced );
m_button->setIcon( save.status == CloudSaveStatus::Desynced ? Icon::resolveDesync() : Icon::refresh() );
switch( save.status ) {
case CloudSaveStatus::Syncing:
m_icon->setPixmap( Icon::cloudSyncing().pixmap( 16, 16 ) );
m_text->setText( tr( "Syncing..." ) );
break;
case CloudSaveStatus::Synced:
m_icon->setPixmap( Icon::cloudSynced().pixmap( 16, 16 ) );
m_text->setText( tr( "Up to date" ) );
break;
case CloudSaveStatus::NoData:
m_icon->setPixmap( Icon::cloud().pixmap( 16, 16 ) );
m_text->setText( tr( "No data" ) );
break;
case CloudSaveStatus::Desynced:
m_icon->setPixmap( Icon::cloudDesynced().pixmap( 16, 16 ) );
m_text->setText( tr( "Desynced!" ) );
break;
case CloudSaveStatus::Errored:
m_icon->setPixmap( Icon::cloudError().pixmap( 16, 16 ) );
m_text->setText( tr( "Sync error" ) );
break;
default:
m_icon->setPixmap( Icon::cloudOffline().pixmap( 16, 16 ) );
if( ApiUtil::identity().role == RhdcRole::Unverified ) {
m_text->setText( tr( "Account not verified" ) );
} else {
m_text->setText( tr( "Offline" ) );
}
break;
}
}

View File

@@ -0,0 +1,33 @@
#ifndef SRC_RHDC_UI_RHDC_CLOUD_STATUS_WIDGET_HPP_
#define SRC_RHDC_UI_RHDC_CLOUD_STATUS_WIDGET_HPP_
#include <string>
#include <QPushButton>
#include <QWidget>
#include <QLabel>
class RhdcCloudStatusWidget : public QWidget {
Q_OBJECT
private:
QLabel *const m_icon;
QLabel *const m_text;
QPushButton *const m_button;
std::string m_hackId;
public:
RhdcCloudStatusWidget( QWidget *parent = nullptr );
~RhdcCloudStatusWidget();
void setHackId( const std::string &hackId );
private:
void refresh();
private slots:
void onStatusChange( QString hackId );
void onButtonClick();
};
#endif /* SRC_RHDC_UI_RHDC_CLOUD_STATUS_WIDGET_HPP_ */

View File

@@ -8,6 +8,7 @@
#include "src/ui/toast.hpp"
#include "src/ui/icons.hpp"
#include "src/polyfill/base-directory.hpp"
#include "src/polyfill/filetime.hpp"
#include "src/polyfill/sha1.hpp"
#include "src/rhdc/core/plugin-detect.hpp"
#include "src/rhdc/core/layout.hpp"
@@ -194,7 +195,7 @@ fs::path RhdcDownloadDialog::runForRhdcUrl(
DataProvider::tryFetchRomPathWithSha1( v.sha1, downloadPath )
) {
if( !hack.layoutUrl.empty() ) {
const int64 layoutDownloaded = RomUtil::getLastModified( StarLayout::layoutPath( hack.info.hackId ) );
const int64 layoutDownloaded = Time::getLastModifiedSec( StarLayout::layoutPath( hack.info.hackId ) );
if( layoutDownloaded < hack.layoutLastModified ) {
dialog.downloadLayout(
hack.info.hackId,
@@ -481,7 +482,7 @@ void RhdcDownloadDialog::patchRom(
SqlTransaction transaction;
DataProvider::removeOnlineRom( hackId );
DataProvider::addRomFile({ romPath, RomUtil::getLastModified( romPath ), sha1, true });
DataProvider::addRomFile({ romPath, Time::getLastModifiedSec( romPath ), sha1, true });
DataProvider::updatePluginSettings( sha1, autoPlugin.gfxPlugin, autoPlugin.upscaleTexrects, true, autoPlugin.emulateFramebuffer, autoPlugin.accurateDepthCompare, autoPlugin.emulateRsp );
DataProvider::updateRomHeaderInfo( sha1, internalName, RomUtil::getCrc32( romPath ), RomUtil::readControllerTypes( romPath ) );
transaction.commit();

View File

@@ -3,6 +3,7 @@
#include <QMessageBox>
#include "src/polyfill/file-dialog.hpp"
#include "src/polyfill/filetime.hpp"
#include "src/polyfill/sha1.hpp"
#include "src/core/preset-controllers.hpp"
#include "src/core/file-controller.hpp"
@@ -14,6 +15,7 @@
#include "src/ui/icons.hpp"
#include "src/ui/util.hpp"
#include "src/rhdc/web/api.hpp"
#include "src/rhdc/web/api-helpers.hpp"
RhdcLoginDialog::RhdcLoginDialog( QWidget *parent ) :
QDialog( parent ),
@@ -40,6 +42,7 @@ RhdcLoginDialog::RhdcLoginDialog( QWidget *parent ) :
UiUtil::fixFontSizeOnMac( m_ui->label_9 );
UiUtil::fixFontSizeOnMac( m_ui->label_10 );
UiUtil::fixFontSizeOnMac( m_ui->label_11 );
UiUtil::fixFontSizeOnMac( m_ui->label_14 );
UiUtil::fixFontSizeOnMac( m_ui->preferHleLabel );
#endif
}
@@ -90,7 +93,7 @@ void RhdcLoginDialog::browseForRom() {
DataProvider::addManualRomPath( romPath );
DataProvider::addRomFile({
romPath,
RomUtil::getLastModified( romPath ),
Time::getLastModifiedSec( romPath ),
SM64_SHA1,
true
});
@@ -237,6 +240,8 @@ void RhdcLoginDialog::sendMfaCode() {
this->setGeometry( geo );
this->m_ui->settingsContainer->setVisible( true );
const bool accountVerified = (ApiUtil::identity().role > RhdcRole::Unverified);
const RhdcSettings rhdcSettings = FileController::loadRhdcSettings();
this->m_ui->downloadDirInput->setText( rhdcSettings.downloadDirectory.u8string().c_str() );
this->m_ui->enableStarDisplayCheckbox->setChecked( rhdcSettings.enableStarDisplay );
@@ -245,6 +250,8 @@ void RhdcLoginDialog::sendMfaCode() {
this->m_ui->enableDefaultLayoutsCheckbox->setChecked( rhdcSettings.enableDefaultLayouts );
this->m_ui->starDisplaySubsection->setEnabled( rhdcSettings.enableStarDisplay );
this->m_ui->ignoreWidescreenCheckbox->setChecked( rhdcSettings.ignoreWidescreenHint );
this->m_ui->cloudSaveCheckbox->setEnabled( accountVerified );
this->m_ui->cloudSaveCheckbox->setChecked( accountVerified );
},
[this,dialogExists](ApiErrorType) {
if( !*dialogExists ) return;
@@ -262,7 +269,8 @@ void RhdcLoginDialog::accept() {
m_ui->allSlotsCheckbox->isChecked(),
m_ui->preferHleCheckbox->isChecked(),
m_ui->enableDefaultLayoutsCheckbox->isChecked(),
m_ui->ignoreWidescreenCheckbox->isChecked()
m_ui->ignoreWidescreenCheckbox->isChecked(),
m_ui->cloudSaveCheckbox->isChecked()
});
QDialog::accept();
}

View File

@@ -45,7 +45,7 @@ QNetworkAccessManager *ApiUtil::web() {
}
__Identity &ApiUtil::identity() noexcept {
static __Identity s_identity = { "", "", QByteArray() };
static __Identity s_identity = { "", "", RhdcRole::Unauthenticated, QByteArray() };
return s_identity;
}

View File

@@ -7,6 +7,7 @@
#include <functional>
#include <sstream>
#include <cstdint>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
@@ -14,6 +15,18 @@
#include "src/rhdc/web/api.hpp"
#include "src/core/json.hpp"
enum RhdcRole : int_fast8_t {
Unauthenticated = 0,
Banned = 1,
Unverified = 2,
Restricted = 3,
User = 4,
PowerUser = 5,
TrustedUser = 6,
Moderator = 7,
Staff = 8
};
namespace ApiUtil {
typedef const QByteArray &(*HttpMethodFactory)(void);
}
@@ -32,6 +45,7 @@ namespace ApiUtil {
struct __Identity {
string username;
string userId;
RhdcRole role;
QByteArray authToken;
};

View File

@@ -19,10 +19,13 @@ enum class HttpStatusCode : int {
Accepted = 202,
NoContent = 204,
NotModified = 304,
BadRequest = 400,
Unauthorized = 401,
Forbidden = 403,
NotFound = 404,
Conflict = 409,
Gone = 410,
TooManyRequests = 429,
@@ -55,6 +58,9 @@ struct FollowedHack {
std::vector<string> authors;
string layoutUrl;
int64 layoutLastModified;
std::optional<int64> cloudSaveDate;
std::vector<int> cloudSaveStars;
std::optional<uint> cloudSaveCrc;
};
struct RhdcVersionedHack {
@@ -199,6 +205,41 @@ namespace RhdcApi {
const std::function<void(ApiErrorType)> &onFailure
);
/* Cloud Saves */
extern void fetchCloudSave(
const string &hackId,
const QDateTime &ifModifiedSince,
const std::function<void(QIODevice&, const QDateTime&)> &onModifiedSave,
const std::function<void(const QDateTime&)> &onNotModified,
const std::function<void(ApiErrorType)> &onFailure
);
extern void deleteCloudSave(
const string &hackId,
const std::function<void(void)> &onSuccess,
const std::function<void(ApiErrorType)> &onFailure
);
extern void pushCloudSave(
const string &hackId,
const QByteArray &compressedSave,
const QDateTime &lastModified,
const std::vector<int> &starCounts,
const std::function<void(void)> &onSuccess,
const std::function<void(const QDateTime&, const std::vector<int>&)> &onConflict,
const std::function<void(ApiErrorType)> &onFailure
);
extern void forcePushCloudSave(
const string &hackId,
const QByteArray &compressedSave,
const QDateTime &lastModified,
const std::vector<int> &starCounts,
const std::function<void(void)> &onSuccess,
const std::function<void(ApiErrorType)> &onFailure
);
/* Telemetry */
extern void sendTelemetryDatum();

View File

@@ -160,6 +160,22 @@ static std::vector<RhdcHackVersionExt> parseVersions( const JArray &versionsJson
return versions;
}
static inline std::vector<int> tryParseIntArray( const Json &json ) {
if( !json.isArray() ) return std::vector<int>();
const JArray &jsonArray = json.array();
std::vector<int> result;
result.reserve( jsonArray.size() );
for( const Json &e : jsonArray ) {
const std::optional<int> val = e.tryGet<int>();
if( !val.has_value() ) return std::vector<int>();
result.push_back( val.value() );
}
return result;
}
void RhdcApi::getFollowedHacksAsync(
const std::function<void(HashMap<string,FollowedHack>&)> &onSuccess,
const std::function<void(ApiErrorType)> &onFailure
@@ -215,6 +231,14 @@ void RhdcApi::getFollowedHacksAsync(
starpower.progress.difficulty = starpowerJson["difficulty"].getOrDefault<ubyte>( 0 );
}
std::optional<int64> cloudSaveDate;
const string cloudSaveDateStr = hack["cloudSave"]["lastModified"].getOrDefault<string>( "" );
if( !cloudSaveDateStr.empty() ) {
bool validDate = false;
cloudSaveDate = Time::tryParseIso( cloudSaveDateStr, validDate );
if( !validDate ) cloudSaveDate.reset();
}
hackMap[hackId] = FollowedHack {
RhdcHackInfo {
hackId,
@@ -233,7 +257,10 @@ void RhdcApi::getFollowedHacksAsync(
std::move( groups ),
std::move( authors ),
std::move( layoutUrl ),
layoutUpdated
layoutUpdated,
cloudSaveDate,
tryParseIntArray( hack["cloudSave"]["stars"] ),
hack["cloudSave"]["crc32"].tryGet<uint>()
};
}

View File

@@ -84,6 +84,21 @@ static void refreshTokenAsync() {
);
}
static inline RhdcRole parseRole( const Json &json ) {
const string roleString = json.getOrDefault<string>( "" );
if( roleString == "User"s ) return RhdcRole::User;
if( roleString == "Unverified"s ) return RhdcRole::Unverified;
if( roleString == "Restricted"s ) return RhdcRole::Restricted;
if( roleString == "Trusted User"s ) return RhdcRole::TrustedUser;
if( roleString == "Moderator"s ) return RhdcRole::Moderator;
if( roleString == "Staff"s ) return RhdcRole::Staff;
if( roleString == "Power User"s ) return RhdcRole::PowerUser;
if( roleString == "Banned"s ) return RhdcRole::Banned;
logWarn( "Failed to parse user role. Assuming user role." );
return RhdcRole::User;
}
void RhdcApi::verifyLoginAsync(
const std::function<void(void)> &onSuccess,
const std::function<void(ApiErrorType)> &onFailure
@@ -109,6 +124,7 @@ void RhdcApi::verifyLoginAsync(
} else {
ApiUtil::identity().userId = userId;
ApiUtil::identity().username = json["username"].getOrDefault<string>( "" );
ApiUtil::identity().role = parseRole( json["role"] );
const string dateString = json["tokenExpires"].getOrDefault<string>( "" );
if( !dateString.empty() ) {
@@ -200,5 +216,6 @@ void RhdcApi::logout() noexcept {
ApiUtil::identity().username.clear();
ApiUtil::identity().userId.clear();
ApiUtil::identity().authToken.clear();
ApiUtil::identity().role = RhdcRole::Unauthenticated;
RhdcCredentials::forget();
}

View File

@@ -17,6 +17,7 @@
} else { \
onFailure( ApiErrorType::NotAuthorized ); \
} \
return; \
}
#define OPTIONAL_LOGIN( retryFunc ) { \
@@ -41,6 +42,12 @@
__tryAuthorize = true; \
}
#define REQUIRE_ROLE( minRole, onFailure ) \
if( (int_fast8_t)ApiUtil::identity().role < (int_fast8_t)RhdcRole::minRole ) { \
onFailure( ApiErrorType::NotAuthorized ); \
return; \
}
#define REQUIRE_VERIFIED( onFailure ) REQUIRE_ROLE( Restricted, onFailure )
#endif /* SRC_RHDC_WEB_REQUIRE_LOGIN_HPP_ */

167
src/rhdc/web/saves.cpp Normal file
View File

@@ -0,0 +1,167 @@
#include "src/rhdc/web/api.hpp"
#include "src/rhdc/web/api-helpers.hpp"
#include "src/rhdc/web/require-login.hpp"
#include <QTimeZone>
#include "src/core/numeric-string.hpp"
#include "src/core/logging.hpp"
void RhdcApi::fetchCloudSave(
const string &hackId,
const QDateTime &ifModifiedSince,
const std::function<void(QIODevice&, const QDateTime&)> &onModifiedSave,
const std::function<void(const QDateTime&)> &onNotModified,
const std::function<void(ApiErrorType)> &onFailure
) {
REQUIRE_LOGIN( [=](){ fetchCloudSave( hackId, ifModifiedSince, onModifiedSave, onNotModified, onFailure ); })
REQUIRE_VERIFIED( onFailure )
QNetworkRequest request = ApiUtil::makeRequest(
string( RHDC_API_HOST "/pl/saves/" ) + hackId,
"application/lzip",
true
);
if( !ifModifiedSince.isNull() ) {
request.setHeader( QNetworkRequest::IfModifiedSinceHeader, ifModifiedSince );
}
QNetworkReply *response = ApiUtil::web()->get( request );
QObject::connect( response, &QNetworkReply::finished, response->manager(), [=]() {
const HttpStatusCode statusCode = (HttpStatusCode)ApiUtil::getStatusCode( response );
logDebug( Number::toString( (int)statusCode ) + ' ' + response->url().toString().toStdString() );
switch( statusCode ) {
case HttpStatusCode::OK:
onModifiedSave( *response, response->header( QNetworkRequest::LastModifiedHeader ).toDateTime() );
break;
case HttpStatusCode::NotModified:
onNotModified( response->header( QNetworkRequest::LastModifiedHeader ).toDateTime() );
break;
case HttpStatusCode::NotFound:
onNotModified( QDateTime() );
break;
case HttpStatusCode::Unauthorized:
case HttpStatusCode::Forbidden:
onFailure( ApiErrorType::NotAuthorized );
break;
case HttpStatusCode::TooManyRequests:
case HttpStatusCode::InternalServerError:
case HttpStatusCode::BadGateway:
case HttpStatusCode::ServiceUnavailable:
case HttpStatusCode::GatewayTimeout:
onFailure( ApiErrorType::ServerDown );
break;
case HttpStatusCode::None:
onFailure( ApiUtil::isOnline() ? ApiErrorType::ServerDown : ApiErrorType::Offline );
break;
default:
onFailure( ApiErrorType::UnknownError );
break;
}
response->deleteLater();
}, Qt::QueuedConnection );
}
static inline string pushCloudSaveUrl( const string &hackId, const QDateTime &timestamp, const std::vector<int> &stars ) {
string url = string( RHDC_API_HOST "/pl/saves/" ) + hackId + "?ts=" + Number::toString( timestamp.toSecsSinceEpoch() ) + "&stars=";
if( stars.empty() ) return url;
for( int starCount : stars ) {
url += Number::toString( starCount ) + '.';
}
url.pop_back();
return url;
}
void RhdcApi::pushCloudSave(
const string &hackId,
const QByteArray &compressedSave,
const QDateTime &lastModified,
const std::vector<int> &starCounts,
const std::function<void(void)> &onSuccess,
const std::function<void(const QDateTime&, const std::vector<int>&)> &onConflict,
const std::function<void(ApiErrorType)> &onFailure
) {
REQUIRE_LOGIN( [=](){ pushCloudSave( hackId, compressedSave, lastModified, starCounts, onSuccess, onConflict, onFailure ); })
REQUIRE_VERIFIED( onFailure )
QNetworkReply *response = ApiUtil::send(
HttpMethod::Post,
pushCloudSaveUrl( hackId, lastModified, starCounts ),
"application/lzip",
compressedSave,
true
);
ApiUtil::onResponse( response, onSuccess, [=](ApiErrorType errorType){
if( ApiUtil::getStatusCode( response ) != (int)HttpStatusCode::Conflict ) {
onFailure( errorType );
return;
}
const Json json = ApiUtil::readJson( response );
const std::optional<long> cloudLastModified = json["lastModified"].tryGet<long>();
if( !cloudLastModified.has_value() ) {
onFailure( ApiErrorType::JsonError );
return;
}
if( !json["starCounts"].isArray() ) {
onFailure( ApiErrorType::JsonError );
return;
}
const JArray &starCountsJson = json["starCounts"].array();
std::vector<int> cloudStarCounts;
cloudStarCounts.reserve( starCountsJson.size() );
for( const Json &starCount : starCountsJson ) {
const std::optional<int> parsedCount = starCount.tryGet<int>();
if( !parsedCount.has_value() ) {
onFailure( ApiErrorType::JsonError );
return;
}
cloudStarCounts.push_back( parsedCount.value() );
}
onConflict( QDateTime::fromSecsSinceEpoch( cloudLastModified.value(), QTimeZone::utc() ), cloudStarCounts );
});
}
void RhdcApi::forcePushCloudSave(
const string &hackId,
const QByteArray &compressedSave,
const QDateTime &lastModified,
const std::vector<int> &starCounts,
const std::function<void(void)> &onSuccess,
const std::function<void(ApiErrorType)> &onFailure
) {
REQUIRE_LOGIN( [=](){ forcePushCloudSave( hackId, compressedSave, lastModified, starCounts, onSuccess, onFailure ); })
REQUIRE_VERIFIED( onFailure )
QNetworkReply *response = ApiUtil::send(
HttpMethod::Post,
pushCloudSaveUrl( hackId, lastModified, starCounts ) + "&push=true",
"application/lzip",
compressedSave,
true
);
ApiUtil::onResponse( response, onSuccess, onFailure );
}
void RhdcApi::deleteCloudSave(
const string &hackId,
const std::function<void(void)> &onSuccess,
const std::function<void(ApiErrorType)> &onFailure
) {
REQUIRE_LOGIN( [=](){ deleteCloudSave( hackId, onSuccess, onFailure ); })
REQUIRE_VERIFIED( onFailure )
QNetworkReply *response = ApiUtil::send(
HttpMethod::Delete,
string( RHDC_API_HOST "/pl/saves/" ) + hackId
);
ApiUtil::onResponse( response, onSuccess, onFailure );
}

View File

@@ -112,6 +112,7 @@ void RhdcApi::submitRatingsAsync(
const std::function<void(ApiErrorType)> &onFailure
) {
REQUIRE_LOGIN( [=](){ submitRatingsAsync( hackId, qualityRating, difficultyRating, onSuccess, onFailure ); })
REQUIRE_VERIFIED( onFailure )
QNetworkReply *response = ApiUtil::send<RatingUpdate>( HttpMethod::Patch, string( RHDC_API_HOST "/v3/starpower/hack/" ) + hackId, { qualityRating, difficultyRating } );
ApiUtil::onResponse( response, onSuccess, onFailure );
}
@@ -123,6 +124,7 @@ void RhdcApi::submitStarProgressAsync(
const std::function<void(ApiErrorType)> &onFailure
) {
REQUIRE_LOGIN( [=](){ submitStarProgressAsync( hackId, starsCollected, onSuccess, onFailure ); })
REQUIRE_VERIFIED( onFailure )
QNetworkReply *response = ApiUtil::send<StarsUpdate>( HttpMethod::Patch, string( RHDC_API_HOST "/v3/starpower/hack/" ) + hackId, { starsCollected } );
ApiUtil::onResponse( response, onSuccess, onFailure );
}
@@ -134,6 +136,7 @@ void RhdcApi::submitCompletionAsync(
const std::function<void(ApiErrorType)> &onFailure
) {
REQUIRE_LOGIN( [=](){ submitCompletionAsync( hackId, isComplete, onSuccess, onFailure ); })
REQUIRE_VERIFIED( onFailure )
QNetworkReply *response = ApiUtil::send<CompletionUpdate>( HttpMethod::Patch, string( RHDC_API_HOST "/v3/starpower/hack/" ) + hackId, { isComplete } );
ApiUtil::onResponse( response, onSuccess, onFailure );
}
@@ -145,6 +148,7 @@ void RhdcApi::submitPlayTimeAsync(
const std::function<void(ApiErrorType)> &onFailure
) {
REQUIRE_LOGIN( [=](){ submitPlayTimeAsync( hackId, playTime, onSuccess, onFailure ); })
REQUIRE_VERIFIED( onFailure )
QNetworkReply *response = ApiUtil::send<PlayTimeUpdate>( HttpMethod::Patch, string( RHDC_API_HOST "/v3/starpower/hack/" ) + hackId, { playTime } );
ApiUtil::onResponse( response, onSuccess, onFailure );
}

7
src/thirdparty/lzlib/AUTHORS vendored Normal file
View File

@@ -0,0 +1,7 @@
Lzlib was written by Antonio Diaz Diaz.
The ideas embodied in lzlib are due to (at least) the following people:
Abraham Lempel and Jacob Ziv (for the LZ algorithm), Andrey Markov (for
the definition of Markov chains), G.N.N. Martin (for the definition of
range encoding), Igor Pavlov (for putting all the above together in
LZMA), and Julian Seward (for bzip2's CLI).

17
src/thirdparty/lzlib/COPYING vendored Normal file
View File

@@ -0,0 +1,17 @@
Lzlib - Compression library for the lzip format
Copyright (C) Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

338
src/thirdparty/lzlib/COPYING.GPL vendored Normal file
View File

@@ -0,0 +1,338 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) <year> <name of author>
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.

97
src/thirdparty/lzlib/README vendored Normal file
View File

@@ -0,0 +1,97 @@
Description
Lzlib is a data compression library providing in-memory LZMA compression
and decompression functions, including integrity checking of the
decompressed data. The compressed data format used by the library is the
lzip format. Lzlib is written in C.
The lzip file format is designed for data sharing and long-term
archiving, taking into account both data integrity and decoder
availability:
* The lzip format provides very safe integrity checking and some data
recovery means. The lziprecover program can repair bit-flip errors
(one of the most common forms of data corruption) in lzip files,
and provides data recovery capabilities, including error-checked
merging of damaged copies of a file.
* The lzip format is as simple as possible (but not simpler). The
lzip manual provides the source code of a simple decompressor along
with a detailed explanation of how it works, so that with the only
help of the lzip manual it would be possible for a digital
archaeologist to extract the data from a lzip file long after
quantum computers eventually render LZMA obsolete.
* Additionally the lzip reference implementation is copylefted, which
guarantees that it will remain free forever.
A nice feature of the lzip format is that a corrupt byte is easier to
repair the nearer it is from the beginning of the file. Therefore, with
the help of lziprecover, losing an entire archive just because of a
corrupt byte near the beginning is a thing of the past.
The functions and variables forming the interface of the compression
library are declared in the file 'lzlib.h'. Usage examples of the
library are given in the files 'main.c' and 'bbexample.c' from the
source distribution.
Compression/decompression is done by repeatedly calling a couple of
read/write functions until all the data have been processed by the
library. This interface is safer and less error prone than the
traditional zlib interface.
Compression/decompression is done when the read function is called. This
means the value returned by the position functions will not be updated
until a read call, even if a lot of data are written. If you want the
data to be compressed in advance, just call the read function with a
size equal to 0.
If all the data to be compressed are written in advance, lzlib will
automatically adjust the header of the compressed data to use the
smallest possible dictionary size. This feature reduces the amount of
memory needed for decompression and allows minilzip to produce identical
compressed output as lzip.
Lzlib will correctly decompress a data stream which is the concatenation
of two or more compressed data streams. The result is the concatenation
of the corresponding decompressed data streams. Integrity testing of
concatenated compressed data streams is also supported.
All the library functions are thread safe. The library does not install
any signal handler. The decoder checks the consistency of the compressed
data, so the library should never crash even in case of corrupted input.
In spite of its name (Lempel-Ziv-Markov chain-Algorithm), LZMA is not a
concrete algorithm; it is more like "any algorithm using the LZMA coding
scheme". For example, the option '-0' of lzip uses the scheme in almost
the simplest way possible; issuing the longest match it can find, or a
literal byte if it can't find a match. Inversely, a much more elaborated
way of finding coding sequences of minimum size than the one currently
used by lzip could be developed, and the resulting sequence could also
be coded using the LZMA coding scheme.
Lzlib currently implements two variants of the LZMA algorithm; fast
(used by option '-0' of minilzip) and normal (used by all other
compression levels).
The high compression of LZMA comes from combining two basic, well-proven
compression ideas: sliding dictionaries (LZ77/78) and markov models (the
thing used by every compression algorithm that uses a range encoder or
similar order-0 entropy coder as its last stage) with segregation of
contexts according to what the bits are used for.
The ideas embodied in lzlib are due to (at least) the following people:
Abraham Lempel and Jacob Ziv (for the LZ algorithm), Andrey Markov (for
the definition of Markov chains), G.N.N. Martin (for the definition of
range encoding), Igor Pavlov (for putting all the above together in
LZMA), and Julian Seward (for bzip2's CLI).
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This file is free documentation: you have unlimited permission to copy,
distribute and modify it.
The file Makefile.in is a data file used by configure to produce the
Makefile. It has the same copyright owner and permissions that configure
itself.

139
src/thirdparty/lzlib/cbuffer.c vendored Normal file
View File

@@ -0,0 +1,139 @@
/* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
struct Circular_buffer
{
uint8_t * buffer;
unsigned buffer_size; /* capacity == buffer_size - 1 */
unsigned get; /* buffer is empty when get == put */
unsigned put;
};
static inline void Cb_reset( struct Circular_buffer * const cb )
{ cb->get = 0; cb->put = 0; }
static inline bool Cb_init( struct Circular_buffer * const cb,
const unsigned buf_size )
{
cb->buffer_size = buf_size + 1;
cb->get = 0;
cb->put = 0;
cb->buffer =
( cb->buffer_size > 1 ) ? (uint8_t *)malloc( cb->buffer_size ) : 0;
return ( cb->buffer != 0 );
}
static inline void Cb_free( struct Circular_buffer * const cb )
{ free( cb->buffer ); cb->buffer = 0; }
static inline unsigned Cb_used_bytes( const struct Circular_buffer * const cb )
{ return ( (cb->get <= cb->put) ? 0 : cb->buffer_size ) + cb->put - cb->get; }
static inline unsigned Cb_free_bytes( const struct Circular_buffer * const cb )
{ return ( (cb->get <= cb->put) ? cb->buffer_size : 0 ) - cb->put + cb->get - 1; }
static inline uint8_t Cb_get_byte( struct Circular_buffer * const cb )
{
const uint8_t b = cb->buffer[cb->get];
if( ++cb->get >= cb->buffer_size ) cb->get = 0;
return b;
}
static inline void Cb_put_byte( struct Circular_buffer * const cb,
const uint8_t b )
{
cb->buffer[cb->put] = b;
if( ++cb->put >= cb->buffer_size ) cb->put = 0;
}
static bool Cb_unread_data( struct Circular_buffer * const cb,
const unsigned size )
{
if( size > Cb_free_bytes( cb ) ) return false;
if( cb->get >= size ) cb->get -= size;
else cb->get = cb->buffer_size - size + cb->get;
return true;
}
/* Copies up to 'out_size' bytes to 'out_buffer' and updates 'get'.
Returns the number of bytes copied.
*/
static unsigned Cb_read_data( struct Circular_buffer * const cb,
uint8_t * const out_buffer,
const unsigned out_size )
{
unsigned size = 0;
if( out_size == 0 ) return 0;
if( cb->get > cb->put )
{
size = min( cb->buffer_size - cb->get, out_size );
if( size > 0 )
{
memcpy( out_buffer, cb->buffer + cb->get, size );
cb->get += size;
if( cb->get >= cb->buffer_size ) cb->get = 0;
}
}
if( cb->get < cb->put )
{
const unsigned size2 = min( cb->put - cb->get, out_size - size );
if( size2 > 0 )
{
memcpy( out_buffer + size, cb->buffer + cb->get, size2 );
cb->get += size2;
size += size2;
}
}
return size;
}
/* Copies up to 'in_size' bytes from 'in_buffer' and updates 'put'.
Returns the number of bytes copied.
*/
static unsigned Cb_write_data( struct Circular_buffer * const cb,
const uint8_t * const in_buffer,
const unsigned in_size )
{
unsigned size = 0;
if( in_size == 0 ) return 0;
if( cb->put >= cb->get )
{
size = min( cb->buffer_size - cb->put - (cb->get == 0), in_size );
if( size > 0 )
{
memcpy( cb->buffer + cb->put, in_buffer, size );
cb->put += size;
if( cb->put >= cb->buffer_size ) cb->put = 0;
}
}
if( cb->put < cb->get )
{
const unsigned size2 = min( cb->get - cb->put - 1, in_size - size );
if( size2 > 0 )
{
memcpy( cb->buffer + cb->put, in_buffer + size, size2 );
cb->put += size2;
size += size2;
}
}
return size;
}

147
src/thirdparty/lzlib/decoder.c vendored Normal file
View File

@@ -0,0 +1,147 @@
/* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
static int LZd_try_verify_trailer( struct LZ_decoder * const d )
{
File_trailer trailer;
if( Rd_available_bytes( d->rdec ) < Ft_size && !d->rdec->at_stream_end )
return 0;
d->verify_trailer_pending = false;
d->member_finished = true;
if( Rd_read_data( d->rdec, trailer, Ft_size ) == Ft_size &&
Ft_get_data_crc( trailer ) == LZd_crc( d ) &&
Ft_get_data_size( trailer ) == LZd_data_position( d ) &&
Ft_get_member_size( trailer ) == d->rdec->member_position ) return 0;
return 3;
}
/* Return value: 0 = OK, 1 = decoder error, 2 = unexpected EOF,
3 = trailer error, 4 = unknown marker found,
5 = library error. */
static int LZd_decode_member( struct LZ_decoder * const d )
{
struct Range_decoder * const rdec = d->rdec;
State * const state = &d->state;
/* unsigned old_mpos = d->rdec->member_position; */
if( d->member_finished ) return 0;
if( !Rd_try_reload( rdec, false ) )
{ if( !rdec->at_stream_end ) return 0; else return 2; }
if( d->verify_trailer_pending ) return LZd_try_verify_trailer( d );
while( !Rd_finished( rdec ) )
{
const int pos_state = LZd_data_position( d ) & pos_state_mask;
/* const unsigned mpos = d->rdec->member_position;
if( mpos - old_mpos > rd_min_available_bytes ) return 5;
old_mpos = mpos; */
if( !Rd_enough_available_bytes( rdec ) ) /* check unexpected eof */
{ if( !rdec->at_stream_end ) return 0; else break; }
if( !LZd_enough_free_bytes( d ) ) return 0;
if( Rd_decode_bit( rdec, &d->bm_match[*state][pos_state] ) == 0 ) /* 1st bit */
{
Bit_model * const bm = d->bm_literal[get_lit_state(LZd_peek_prev( d ))];
if( St_is_char( *state ) )
{
*state -= ( *state < 4 ) ? *state : 3;
LZd_put_byte( d, Rd_decode_tree8( rdec, bm ) );
}
else
{
*state -= ( *state < 10 ) ? 3 : 6;
LZd_put_byte( d, Rd_decode_matched( rdec, bm, LZd_peek( d, d->rep0 ) ) );
}
}
else /* match or repeated match */
{
int len;
if( Rd_decode_bit( rdec, &d->bm_rep[*state] ) != 0 ) /* 2nd bit */
{
if( Rd_decode_bit( rdec, &d->bm_rep0[*state] ) == 0 ) /* 3rd bit */
{
if( Rd_decode_bit( rdec, &d->bm_len[*state][pos_state] ) == 0 ) /* 4th bit */
{ *state = St_set_short_rep( *state );
LZd_put_byte( d, LZd_peek( d, d->rep0 ) ); continue; }
}
else
{
unsigned distance;
if( Rd_decode_bit( rdec, &d->bm_rep1[*state] ) == 0 ) /* 4th bit */
distance = d->rep1;
else
{
if( Rd_decode_bit( rdec, &d->bm_rep2[*state] ) == 0 ) /* 5th bit */
distance = d->rep2;
else
{ distance = d->rep3; d->rep3 = d->rep2; }
d->rep2 = d->rep1;
}
d->rep1 = d->rep0;
d->rep0 = distance;
}
*state = St_set_rep( *state );
len = min_match_len + Rd_decode_len( rdec, &d->rep_len_model, pos_state );
}
else /* match */
{
unsigned distance;
len = min_match_len + Rd_decode_len( rdec, &d->match_len_model, pos_state );
distance = Rd_decode_tree6( rdec, d->bm_dis_slot[get_len_state(len)] );
if( distance >= start_dis_model )
{
const unsigned dis_slot = distance;
const int direct_bits = ( dis_slot >> 1 ) - 1;
distance = ( 2 | ( dis_slot & 1 ) ) << direct_bits;
if( dis_slot < end_dis_model )
distance += Rd_decode_tree_reversed( rdec,
d->bm_dis + ( distance - dis_slot ), direct_bits );
else
{
distance +=
Rd_decode( rdec, direct_bits - dis_align_bits ) << dis_align_bits;
distance += Rd_decode_tree_reversed4( rdec, d->bm_align );
if( distance == 0xFFFFFFFFU ) /* marker found */
{
Rd_normalize( rdec );
if( len == min_match_len ) /* End Of Stream marker */
{
d->verify_trailer_pending = true;
return LZd_try_verify_trailer( d );
}
if( len == min_match_len + 1 ) /* Sync Flush marker */
{
if( Rd_try_reload( rdec, true ) ) { /*old_mpos += 5;*/ continue; }
else { if( !rdec->at_stream_end ) return 0; else break; }
}
return 4;
}
}
}
d->rep3 = d->rep2; d->rep2 = d->rep1; d->rep1 = d->rep0; d->rep0 = distance;
*state = St_set_match( *state );
if( d->rep0 >= d->dictionary_size ||
( d->rep0 >= d->cb.put && !d->pos_wrapped ) ) return 1;
}
LZd_copy_block( d, d->rep0, len );
}
}
return 2;
}

420
src/thirdparty/lzlib/decoder.h vendored Normal file
View File

@@ -0,0 +1,420 @@
/* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
enum { rd_min_available_bytes = 8 };
struct Range_decoder
{
struct Circular_buffer cb; /* input buffer */
unsigned long long member_position;
uint32_t code;
uint32_t range;
bool at_stream_end;
bool reload_pending;
};
static inline bool Rd_init( struct Range_decoder * const rdec )
{
if( !Cb_init( &rdec->cb, 65536 + rd_min_available_bytes ) ) return false;
rdec->member_position = 0;
rdec->code = 0;
rdec->range = 0xFFFFFFFFU;
rdec->at_stream_end = false;
rdec->reload_pending = false;
return true;
}
static inline void Rd_free( struct Range_decoder * const rdec )
{ Cb_free( &rdec->cb ); }
static inline bool Rd_finished( const struct Range_decoder * const rdec )
{ return rdec->at_stream_end && !Cb_used_bytes( &rdec->cb ); }
static inline void Rd_finish( struct Range_decoder * const rdec )
{ rdec->at_stream_end = true; }
static inline bool Rd_enough_available_bytes( const struct Range_decoder * const rdec )
{ return ( Cb_used_bytes( &rdec->cb ) >= rd_min_available_bytes ); }
static inline unsigned Rd_available_bytes( const struct Range_decoder * const rdec )
{ return Cb_used_bytes( &rdec->cb ); }
static inline unsigned Rd_free_bytes( const struct Range_decoder * const rdec )
{ if( rdec->at_stream_end ) return 0; return Cb_free_bytes( &rdec->cb ); }
static inline unsigned long long Rd_purge( struct Range_decoder * const rdec )
{
const unsigned long long size =
rdec->member_position + Cb_used_bytes( &rdec->cb );
Cb_reset( &rdec->cb );
rdec->member_position = 0; rdec->at_stream_end = true;
return size;
}
static inline void Rd_reset( struct Range_decoder * const rdec )
{ Cb_reset( &rdec->cb );
rdec->member_position = 0; rdec->at_stream_end = false; }
/* Seeks a member header and updates 'get'. '*skippedp' is set to the
number of bytes skipped. Returns true if it finds a valid header.
*/
static bool Rd_find_header( struct Range_decoder * const rdec,
unsigned * const skippedp )
{
*skippedp = 0;
while( rdec->cb.get != rdec->cb.put )
{
if( rdec->cb.buffer[rdec->cb.get] == magic_string[0] )
{
unsigned get = rdec->cb.get;
int i;
File_header header;
for( i = 0; i < Fh_size; ++i )
{
if( get == rdec->cb.put ) return false; /* not enough data */
header[i] = rdec->cb.buffer[get];
if( ++get >= rdec->cb.buffer_size ) get = 0;
}
if( Fh_verify( header ) ) return true;
}
if( ++rdec->cb.get >= rdec->cb.buffer_size ) rdec->cb.get = 0;
++*skippedp;
}
return false;
}
static inline int Rd_write_data( struct Range_decoder * const rdec,
const uint8_t * const inbuf, const int size )
{
if( rdec->at_stream_end || size <= 0 ) return 0;
return Cb_write_data( &rdec->cb, inbuf, size );
}
static inline uint8_t Rd_get_byte( struct Range_decoder * const rdec )
{
++rdec->member_position;
return Cb_get_byte( &rdec->cb );
}
static inline int Rd_read_data( struct Range_decoder * const rdec,
uint8_t * const outbuf, const int size )
{
const int sz = Cb_read_data( &rdec->cb, outbuf, size );
if( sz > 0 ) rdec->member_position += sz;
return sz;
}
static inline bool Rd_unread_data( struct Range_decoder * const rdec,
const unsigned size )
{
if( size > rdec->member_position || !Cb_unread_data( &rdec->cb, size ) )
return false;
rdec->member_position -= size;
return true;
}
static bool Rd_try_reload( struct Range_decoder * const rdec, const bool force )
{
if( force ) rdec->reload_pending = true;
if( rdec->reload_pending && Rd_available_bytes( rdec ) >= 5 )
{
int i;
rdec->reload_pending = false;
rdec->code = 0;
for( i = 0; i < 5; ++i )
rdec->code = (rdec->code << 8) | Rd_get_byte( rdec );
rdec->range = 0xFFFFFFFFU;
rdec->code &= rdec->range; /* make sure that first byte is discarded */
}
return !rdec->reload_pending;
}
static inline void Rd_normalize( struct Range_decoder * const rdec )
{
if( rdec->range <= 0x00FFFFFFU )
{ rdec->range <<= 8; rdec->code = (rdec->code << 8) | Rd_get_byte( rdec ); }
}
static inline unsigned Rd_decode( struct Range_decoder * const rdec,
const int num_bits )
{
unsigned symbol = 0;
int i;
for( i = num_bits; i > 0; --i )
{
bool bit;
Rd_normalize( rdec );
rdec->range >>= 1;
/* symbol <<= 1; */
/* if( rdec->code >= rdec->range ) { rdec->code -= rdec->range; symbol |= 1; } */
bit = ( rdec->code >= rdec->range );
symbol = ( symbol << 1 ) + bit;
rdec->code -= rdec->range & ( 0U - bit );
}
return symbol;
}
static inline unsigned Rd_decode_bit( struct Range_decoder * const rdec,
Bit_model * const probability )
{
uint32_t bound;
Rd_normalize( rdec );
bound = ( rdec->range >> bit_model_total_bits ) * *probability;
if( rdec->code < bound )
{
rdec->range = bound;
*probability += (bit_model_total - *probability) >> bit_model_move_bits;
return 0;
}
else
{
rdec->range -= bound;
rdec->code -= bound;
*probability -= *probability >> bit_model_move_bits;
return 1;
}
}
static inline unsigned Rd_decode_tree3( struct Range_decoder * const rdec,
Bit_model bm[] )
{
unsigned symbol = 1;
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
return symbol & 7;
}
static inline unsigned Rd_decode_tree6( struct Range_decoder * const rdec,
Bit_model bm[] )
{
unsigned symbol = 1;
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
return symbol & 0x3F;
}
static inline unsigned Rd_decode_tree8( struct Range_decoder * const rdec,
Bit_model bm[] )
{
unsigned symbol = 1;
int i;
for( i = 0; i < 8; ++i )
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
return symbol & 0xFF;
}
static inline unsigned
Rd_decode_tree_reversed( struct Range_decoder * const rdec,
Bit_model bm[], const int num_bits )
{
unsigned model = 1;
unsigned symbol = 0;
int i;
for( i = 0; i < num_bits; ++i )
{
const unsigned bit = Rd_decode_bit( rdec, &bm[model] );
model = ( model << 1 ) + bit;
symbol |= ( bit << i );
}
return symbol;
}
static inline unsigned
Rd_decode_tree_reversed4( struct Range_decoder * const rdec, Bit_model bm[] )
{
unsigned symbol = Rd_decode_bit( rdec, &bm[1] );
unsigned model = 2 + symbol;
unsigned bit = Rd_decode_bit( rdec, &bm[model] );
model = ( model << 1 ) + bit; symbol |= ( bit << 1 );
bit = Rd_decode_bit( rdec, &bm[model] );
model = ( model << 1 ) + bit; symbol |= ( bit << 2 );
symbol |= ( Rd_decode_bit( rdec, &bm[model] ) << 3 );
return symbol;
}
static inline unsigned Rd_decode_matched( struct Range_decoder * const rdec,
Bit_model bm[], unsigned match_byte )
{
unsigned symbol = 1;
unsigned mask = 0x100;
while( true )
{
const unsigned match_bit = ( match_byte <<= 1 ) & mask;
const unsigned bit = Rd_decode_bit( rdec, &bm[symbol+match_bit+mask] );
symbol = ( symbol << 1 ) + bit;
if( symbol > 0xFF ) return symbol & 0xFF;
mask &= ~(match_bit ^ (bit << 8)); /* if( match_bit != bit ) mask = 0; */
}
}
static inline unsigned Rd_decode_len( struct Range_decoder * const rdec,
struct Len_model * const lm,
const int pos_state )
{
if( Rd_decode_bit( rdec, &lm->choice1 ) == 0 )
return Rd_decode_tree3( rdec, lm->bm_low[pos_state] );
if( Rd_decode_bit( rdec, &lm->choice2 ) == 0 )
return len_low_symbols + Rd_decode_tree3( rdec, lm->bm_mid[pos_state] );
return len_low_symbols + len_mid_symbols + Rd_decode_tree8( rdec, lm->bm_high );
}
enum { lzd_min_free_bytes = max_match_len };
struct LZ_decoder
{
struct Circular_buffer cb;
unsigned long long partial_data_pos;
struct Range_decoder * rdec;
unsigned dictionary_size;
uint32_t crc;
bool member_finished;
bool verify_trailer_pending;
bool pos_wrapped;
unsigned rep0; /* rep[0-3] latest four distances */
unsigned rep1; /* used for efficient coding of */
unsigned rep2; /* repeated distances */
unsigned rep3;
State state;
Bit_model bm_literal[1<<literal_context_bits][0x300];
Bit_model bm_match[states][pos_states];
Bit_model bm_rep[states];
Bit_model bm_rep0[states];
Bit_model bm_rep1[states];
Bit_model bm_rep2[states];
Bit_model bm_len[states][pos_states];
Bit_model bm_dis_slot[len_states][1<<dis_slot_bits];
Bit_model bm_dis[modeled_distances-end_dis_model+1];
Bit_model bm_align[dis_align_size];
struct Len_model match_len_model;
struct Len_model rep_len_model;
};
static inline bool LZd_enough_free_bytes( const struct LZ_decoder * const d )
{ return Cb_free_bytes( &d->cb ) >= lzd_min_free_bytes; }
static inline uint8_t LZd_peek_prev( const struct LZ_decoder * const d )
{ return d->cb.buffer[((d->cb.put > 0) ? d->cb.put : d->cb.buffer_size)-1]; }
static inline uint8_t LZd_peek( const struct LZ_decoder * const d,
const unsigned distance )
{
const unsigned i = ( ( d->cb.put > distance ) ? 0 : d->cb.buffer_size ) +
d->cb.put - distance - 1;
return d->cb.buffer[i];
}
static inline void LZd_put_byte( struct LZ_decoder * const d, const uint8_t b )
{
CRC32_update_byte( &d->crc, b );
d->cb.buffer[d->cb.put] = b;
if( ++d->cb.put >= d->cb.buffer_size )
{ d->partial_data_pos += d->cb.put; d->cb.put = 0; d->pos_wrapped = true; }
}
static inline void LZd_copy_block( struct LZ_decoder * const d,
const unsigned distance, unsigned len )
{
unsigned lpos = d->cb.put, i = lpos - distance - 1;
bool fast, fast2;
if( lpos > distance )
{
fast = ( len < d->cb.buffer_size - lpos );
fast2 = ( fast && len <= lpos - i );
}
else
{
i += d->cb.buffer_size;
fast = ( len < d->cb.buffer_size - i ); /* (i == pos) may happen */
fast2 = ( fast && len <= i - lpos );
}
if( fast ) /* no wrap */
{
const unsigned tlen = len;
if( fast2 ) /* no wrap, no overlap */
memcpy( d->cb.buffer + lpos, d->cb.buffer + i, len );
else
for( ; len > 0; --len ) d->cb.buffer[lpos++] = d->cb.buffer[i++];
CRC32_update_buf( &d->crc, d->cb.buffer + d->cb.put, tlen );
d->cb.put += tlen;
}
else for( ; len > 0; --len )
{
LZd_put_byte( d, d->cb.buffer[i] );
if( ++i >= d->cb.buffer_size ) i = 0;
}
}
static inline bool LZd_init( struct LZ_decoder * const d,
struct Range_decoder * const rde,
const unsigned dict_size )
{
if( !Cb_init( &d->cb, max( 65536, dict_size ) + lzd_min_free_bytes ) )
return false;
d->partial_data_pos = 0;
d->rdec = rde;
d->dictionary_size = dict_size;
d->crc = 0xFFFFFFFFU;
d->member_finished = false;
d->verify_trailer_pending = false;
d->pos_wrapped = false;
/* prev_byte of first byte; also for LZd_peek( 0 ) on corrupt file */
d->cb.buffer[d->cb.buffer_size-1] = 0;
d->rep0 = 0;
d->rep1 = 0;
d->rep2 = 0;
d->rep3 = 0;
d->state = 0;
Bm_array_init( d->bm_literal[0], (1 << literal_context_bits) * 0x300 );
Bm_array_init( d->bm_match[0], states * pos_states );
Bm_array_init( d->bm_rep, states );
Bm_array_init( d->bm_rep0, states );
Bm_array_init( d->bm_rep1, states );
Bm_array_init( d->bm_rep2, states );
Bm_array_init( d->bm_len[0], states * pos_states );
Bm_array_init( d->bm_dis_slot[0], len_states * (1 << dis_slot_bits) );
Bm_array_init( d->bm_dis, modeled_distances - end_dis_model + 1 );
Bm_array_init( d->bm_align, dis_align_size );
Lm_init( &d->match_len_model );
Lm_init( &d->rep_len_model );
return true;
}
static inline void LZd_free( struct LZ_decoder * const d )
{ Cb_free( &d->cb ); }
static inline bool LZd_member_finished( const struct LZ_decoder * const d )
{ return ( d->member_finished && !Cb_used_bytes( &d->cb ) ); }
static inline unsigned LZd_crc( const struct LZ_decoder * const d )
{ return d->crc ^ 0xFFFFFFFFU; }
static inline unsigned long long
LZd_data_position( const struct LZ_decoder * const d )
{ return d->partial_data_pos + d->cb.put; }

603
src/thirdparty/lzlib/encoder.c vendored Normal file
View File

@@ -0,0 +1,603 @@
/* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
static int LZe_get_match_pairs( struct LZ_encoder * const e, struct Pair * pairs )
{
int32_t * ptr0 = e->eb.mb.pos_array + ( e->eb.mb.cyclic_pos << 1 );
int32_t * ptr1 = ptr0 + 1;
int32_t * newptr;
int len = 0, len0 = 0, len1 = 0;
int maxlen = 0;
int num_pairs = 0;
const int pos1 = e->eb.mb.pos + 1;
const int min_pos = ( e->eb.mb.pos > e->eb.mb.dictionary_size ) ?
e->eb.mb.pos - e->eb.mb.dictionary_size : 0;
const uint8_t * const data = Mb_ptr_to_current_pos( &e->eb.mb );
int count, key2, key3, key4, newpos1;
unsigned tmp;
int len_limit = e->match_len_limit;
if( len_limit > Mb_available_bytes( &e->eb.mb ) )
{
e->been_flushed = true;
len_limit = Mb_available_bytes( &e->eb.mb );
if( len_limit < 4 ) { *ptr0 = *ptr1 = 0; return 0; }
}
tmp = crc32[data[0]] ^ data[1];
key2 = tmp & ( num_prev_positions2 - 1 );
tmp ^= (unsigned)data[2] << 8;
key3 = num_prev_positions2 + ( tmp & ( num_prev_positions3 - 1 ) );
key4 = num_prev_positions2 + num_prev_positions3 +
( ( tmp ^ ( crc32[data[3]] << 5 ) ) & e->eb.mb.key4_mask );
if( pairs )
{
int np2 = e->eb.mb.prev_positions[key2];
int np3 = e->eb.mb.prev_positions[key3];
if( np2 > min_pos && e->eb.mb.buffer[np2-1] == data[0] )
{
pairs[0].dis = e->eb.mb.pos - np2;
pairs[0].len = maxlen = 2;
num_pairs = 1;
}
if( np2 != np3 && np3 > min_pos && e->eb.mb.buffer[np3-1] == data[0] )
{
maxlen = 3;
np2 = np3;
pairs[num_pairs].dis = e->eb.mb.pos - np2;
++num_pairs;
}
if( num_pairs > 0 )
{
const int delta = pos1 - np2;
while( maxlen < len_limit && data[maxlen-delta] == data[maxlen] )
++maxlen;
pairs[num_pairs-1].len = maxlen;
if( maxlen >= len_limit ) pairs = 0; /* done. now just skip */
}
if( maxlen < 3 ) maxlen = 3;
}
e->eb.mb.prev_positions[key2] = pos1;
e->eb.mb.prev_positions[key3] = pos1;
newpos1 = e->eb.mb.prev_positions[key4];
e->eb.mb.prev_positions[key4] = pos1;
for( count = e->cycles; ; )
{
int delta;
if( newpos1 <= min_pos || --count < 0 ) { *ptr0 = *ptr1 = 0; break; }
if( e->been_flushed ) len = 0;
delta = pos1 - newpos1;
newptr = e->eb.mb.pos_array +
( ( e->eb.mb.cyclic_pos - delta +
( (e->eb.mb.cyclic_pos >= delta) ? 0 : e->eb.mb.dictionary_size + 1 ) ) << 1 );
if( data[len-delta] == data[len] )
{
while( ++len < len_limit && data[len-delta] == data[len] ) {}
if( pairs && maxlen < len )
{
pairs[num_pairs].dis = delta - 1;
pairs[num_pairs].len = maxlen = len;
++num_pairs;
}
if( len >= len_limit )
{
*ptr0 = newptr[0];
*ptr1 = newptr[1];
break;
}
}
if( data[len-delta] < data[len] )
{
*ptr0 = newpos1;
ptr0 = newptr + 1;
newpos1 = *ptr0;
len0 = len; if( len1 < len ) len = len1;
}
else
{
*ptr1 = newpos1;
ptr1 = newptr;
newpos1 = *ptr1;
len1 = len; if( len0 < len ) len = len0;
}
}
return num_pairs;
}
static void LZe_update_distance_prices( struct LZ_encoder * const e )
{
int dis, len_state;
for( dis = start_dis_model; dis < modeled_distances; ++dis )
{
const int dis_slot = dis_slots[dis];
const int direct_bits = ( dis_slot >> 1 ) - 1;
const int base = ( 2 | ( dis_slot & 1 ) ) << direct_bits;
const int price = price_symbol_reversed( e->eb.bm_dis + ( base - dis_slot ),
dis - base, direct_bits );
for( len_state = 0; len_state < len_states; ++len_state )
e->dis_prices[len_state][dis] = price;
}
for( len_state = 0; len_state < len_states; ++len_state )
{
int * const dsp = e->dis_slot_prices[len_state];
int * const dp = e->dis_prices[len_state];
const Bit_model * const bmds = e->eb.bm_dis_slot[len_state];
int slot = 0;
for( ; slot < end_dis_model; ++slot )
dsp[slot] = price_symbol6( bmds, slot );
for( ; slot < e->num_dis_slots; ++slot )
dsp[slot] = price_symbol6( bmds, slot ) +
(((( slot >> 1 ) - 1 ) - dis_align_bits ) << price_shift_bits );
for( dis = 0; dis < start_dis_model; ++dis )
dp[dis] = dsp[dis];
for( ; dis < modeled_distances; ++dis )
dp[dis] += dsp[dis_slots[dis]];
}
}
/* Returns the number of bytes advanced (ahead).
trials[0]..trials[ahead-1] contain the steps to encode.
( trials[0].dis4 == -1 ) means literal.
A match/rep longer or equal than match_len_limit finishes the sequence.
*/
static int LZe_sequence_optimizer( struct LZ_encoder * const e,
const int reps[num_rep_distances],
const State state )
{
int main_len, num_pairs, i, rep, num_trials, len;
int rep_index = 0, cur = 0;
int replens[num_rep_distances];
if( e->pending_num_pairs > 0 ) /* from previous call */
{
num_pairs = e->pending_num_pairs;
e->pending_num_pairs = 0;
}
else
num_pairs = LZe_read_match_distances( e );
main_len = ( num_pairs > 0 ) ? e->pairs[num_pairs-1].len : 0;
for( i = 0; i < num_rep_distances; ++i )
{
replens[i] = Mb_true_match_len( &e->eb.mb, 0, reps[i] + 1 );
if( replens[i] > replens[rep_index] ) rep_index = i;
}
if( replens[rep_index] >= e->match_len_limit )
{
e->trials[0].price = replens[rep_index];
e->trials[0].dis4 = rep_index;
if( !LZe_move_and_update( e, replens[rep_index] ) ) return 0;
return replens[rep_index];
}
if( main_len >= e->match_len_limit )
{
e->trials[0].price = main_len;
e->trials[0].dis4 = e->pairs[num_pairs-1].dis + num_rep_distances;
if( !LZe_move_and_update( e, main_len ) ) return 0;
return main_len;
}
{
const int pos_state = Mb_data_position( &e->eb.mb ) & pos_state_mask;
const int match_price = price1( e->eb.bm_match[state][pos_state] );
const int rep_match_price = match_price + price1( e->eb.bm_rep[state] );
const uint8_t prev_byte = Mb_peek( &e->eb.mb, 1 );
const uint8_t cur_byte = Mb_peek( &e->eb.mb, 0 );
const uint8_t match_byte = Mb_peek( &e->eb.mb, reps[0] + 1 );
e->trials[1].price = price0( e->eb.bm_match[state][pos_state] );
if( St_is_char( state ) )
e->trials[1].price += LZeb_price_literal( &e->eb, prev_byte, cur_byte );
else
e->trials[1].price += LZeb_price_matched( &e->eb, prev_byte, cur_byte, match_byte );
e->trials[1].dis4 = -1; /* literal */
if( match_byte == cur_byte )
Tr_update( &e->trials[1], rep_match_price +
LZeb_price_shortrep( &e->eb, state, pos_state ), 0, 0 );
num_trials = max( main_len, replens[rep_index] );
if( num_trials < min_match_len )
{
e->trials[0].price = 1;
e->trials[0].dis4 = e->trials[1].dis4;
if( !Mb_move_pos( &e->eb.mb ) ) return 0;
return 1;
}
e->trials[0].state = state;
for( i = 0; i < num_rep_distances; ++i )
e->trials[0].reps[i] = reps[i];
for( len = min_match_len; len <= num_trials; ++len )
e->trials[len].price = infinite_price;
for( rep = 0; rep < num_rep_distances; ++rep )
{
int price;
if( replens[rep] < min_match_len ) continue;
price = rep_match_price + LZeb_price_rep( &e->eb, rep, state, pos_state );
for( len = min_match_len; len <= replens[rep]; ++len )
Tr_update( &e->trials[len], price +
Lp_price( &e->rep_len_prices, len, pos_state ), rep, 0 );
}
if( main_len > replens[0] )
{
const int normal_match_price = match_price + price0( e->eb.bm_rep[state] );
int i = 0, len = max( replens[0] + 1, min_match_len );
while( len > e->pairs[i].len ) ++i;
while( true )
{
const int dis = e->pairs[i].dis;
Tr_update( &e->trials[len], normal_match_price +
LZe_price_pair( e, dis, len, pos_state ),
dis + num_rep_distances, 0 );
if( ++len > e->pairs[i].len && ++i >= num_pairs ) break;
}
}
}
while( true ) /* price optimization loop */
{
struct Trial *cur_trial, *next_trial;
int newlen, pos_state, triable_bytes, len_limit;
int start_len = min_match_len;
int next_price, match_price, rep_match_price;
State cur_state;
uint8_t prev_byte, cur_byte, match_byte;
if( !Mb_move_pos( &e->eb.mb ) ) return 0;
if( ++cur >= num_trials ) /* no more initialized trials */
{
LZe_backward( e, cur );
return cur;
}
num_pairs = LZe_read_match_distances( e );
newlen = ( num_pairs > 0 ) ? e->pairs[num_pairs-1].len : 0;
if( newlen >= e->match_len_limit )
{
e->pending_num_pairs = num_pairs;
LZe_backward( e, cur );
return cur;
}
/* give final values to current trial */
cur_trial = &e->trials[cur];
{
const int dis4 = cur_trial->dis4;
int prev_index = cur_trial->prev_index;
const int prev_index2 = cur_trial->prev_index2;
if( prev_index2 == single_step_trial )
{
cur_state = e->trials[prev_index].state;
if( prev_index + 1 == cur ) /* len == 1 */
{
if( dis4 == 0 ) cur_state = St_set_short_rep( cur_state );
else cur_state = St_set_char( cur_state ); /* literal */
}
else if( dis4 < num_rep_distances ) cur_state = St_set_rep( cur_state );
else cur_state = St_set_match( cur_state );
}
else
{
if( prev_index2 == dual_step_trial ) /* dis4 == 0 (rep0) */
--prev_index;
else /* prev_index2 >= 0 */
prev_index = prev_index2;
cur_state = 8; /* St_set_char_rep(); */
}
cur_trial->state = cur_state;
for( i = 0; i < num_rep_distances; ++i )
cur_trial->reps[i] = e->trials[prev_index].reps[i];
mtf_reps( dis4, cur_trial->reps ); /* literal is ignored */
}
pos_state = Mb_data_position( &e->eb.mb ) & pos_state_mask;
prev_byte = Mb_peek( &e->eb.mb, 1 );
cur_byte = Mb_peek( &e->eb.mb, 0 );
match_byte = Mb_peek( &e->eb.mb, cur_trial->reps[0] + 1 );
next_price = cur_trial->price +
price0( e->eb.bm_match[cur_state][pos_state] );
if( St_is_char( cur_state ) )
next_price += LZeb_price_literal( &e->eb, prev_byte, cur_byte );
else
next_price += LZeb_price_matched( &e->eb, prev_byte, cur_byte, match_byte );
/* try last updates to next trial */
next_trial = &e->trials[cur+1];
Tr_update( next_trial, next_price, -1, cur ); /* literal */
match_price = cur_trial->price + price1( e->eb.bm_match[cur_state][pos_state] );
rep_match_price = match_price + price1( e->eb.bm_rep[cur_state] );
if( match_byte == cur_byte && next_trial->dis4 != 0 &&
next_trial->prev_index2 == single_step_trial )
{
const int price = rep_match_price +
LZeb_price_shortrep( &e->eb, cur_state, pos_state );
if( price <= next_trial->price )
{
next_trial->price = price;
next_trial->dis4 = 0; /* rep0 */
next_trial->prev_index = cur;
}
}
triable_bytes =
min( Mb_available_bytes( &e->eb.mb ), max_num_trials - 1 - cur );
if( triable_bytes < min_match_len ) continue;
len_limit = min( e->match_len_limit, triable_bytes );
/* try literal + rep0 */
if( match_byte != cur_byte && next_trial->prev_index != cur )
{
const uint8_t * const data = Mb_ptr_to_current_pos( &e->eb.mb );
const int dis = cur_trial->reps[0] + 1;
const int limit = min( e->match_len_limit + 1, triable_bytes );
int len = 1;
while( len < limit && data[len-dis] == data[len] ) ++len;
if( --len >= min_match_len )
{
const int pos_state2 = ( pos_state + 1 ) & pos_state_mask;
const State state2 = St_set_char( cur_state );
const int price = next_price +
price1( e->eb.bm_match[state2][pos_state2] ) +
price1( e->eb.bm_rep[state2] ) +
LZe_price_rep0_len( e, len, state2, pos_state2 );
while( num_trials < cur + 1 + len )
e->trials[++num_trials].price = infinite_price;
Tr_update2( &e->trials[cur+1+len], price, cur + 1 );
}
}
/* try rep distances */
for( rep = 0; rep < num_rep_distances; ++rep )
{
const uint8_t * const data = Mb_ptr_to_current_pos( &e->eb.mb );
const int dis = cur_trial->reps[rep] + 1;
int price;
if( data[0-dis] != data[0] || data[1-dis] != data[1] ) continue;
for( len = min_match_len; len < len_limit; ++len )
if( data[len-dis] != data[len] ) break;
while( num_trials < cur + len )
e->trials[++num_trials].price = infinite_price;
price = rep_match_price + LZeb_price_rep( &e->eb, rep, cur_state, pos_state );
for( i = min_match_len; i <= len; ++i )
Tr_update( &e->trials[cur+i], price +
Lp_price( &e->rep_len_prices, i, pos_state ), rep, cur );
if( rep == 0 ) start_len = len + 1; /* discard shorter matches */
/* try rep + literal + rep0 */
{
int len2 = len + 1;
const int limit = min( e->match_len_limit + len2, triable_bytes );
int pos_state2;
State state2;
while( len2 < limit && data[len2-dis] == data[len2] ) ++len2;
len2 -= len + 1;
if( len2 < min_match_len ) continue;
pos_state2 = ( pos_state + len ) & pos_state_mask;
state2 = St_set_rep( cur_state );
price += Lp_price( &e->rep_len_prices, len, pos_state ) +
price0( e->eb.bm_match[state2][pos_state2] ) +
LZeb_price_matched( &e->eb, data[len-1], data[len], data[len-dis] );
pos_state2 = ( pos_state2 + 1 ) & pos_state_mask;
state2 = St_set_char( state2 );
price += price1( e->eb.bm_match[state2][pos_state2] ) +
price1( e->eb.bm_rep[state2] ) +
LZe_price_rep0_len( e, len2, state2, pos_state2 );
while( num_trials < cur + len + 1 + len2 )
e->trials[++num_trials].price = infinite_price;
Tr_update3( &e->trials[cur+len+1+len2], price, rep, cur + len + 1, cur );
}
}
/* try matches */
if( newlen >= start_len && newlen <= len_limit )
{
int dis;
const int normal_match_price = match_price +
price0( e->eb.bm_rep[cur_state] );
while( num_trials < cur + newlen )
e->trials[++num_trials].price = infinite_price;
i = 0;
while( e->pairs[i].len < start_len ) ++i;
dis = e->pairs[i].dis;
for( len = start_len; ; ++len )
{
int price = normal_match_price + LZe_price_pair( e, dis, len, pos_state );
Tr_update( &e->trials[cur+len], price, dis + num_rep_distances, cur );
/* try match + literal + rep0 */
if( len == e->pairs[i].len )
{
const uint8_t * const data = Mb_ptr_to_current_pos( &e->eb.mb );
const int dis2 = dis + 1;
int len2 = len + 1;
const int limit = min( e->match_len_limit + len2, triable_bytes );
while( len2 < limit && data[len2-dis2] == data[len2] ) ++len2;
len2 -= len + 1;
if( len2 >= min_match_len )
{
int pos_state2 = ( pos_state + len ) & pos_state_mask;
State state2 = St_set_match( cur_state );
price += price0( e->eb.bm_match[state2][pos_state2] ) +
LZeb_price_matched( &e->eb, data[len-1], data[len], data[len-dis2] );
pos_state2 = ( pos_state2 + 1 ) & pos_state_mask;
state2 = St_set_char( state2 );
price += price1( e->eb.bm_match[state2][pos_state2] ) +
price1( e->eb.bm_rep[state2] ) +
LZe_price_rep0_len( e, len2, state2, pos_state2 );
while( num_trials < cur + len + 1 + len2 )
e->trials[++num_trials].price = infinite_price;
Tr_update3( &e->trials[cur+len+1+len2], price,
dis + num_rep_distances, cur + len + 1, cur );
}
if( ++i >= num_pairs ) break;
dis = e->pairs[i].dis;
}
}
}
}
}
static bool LZe_encode_member( struct LZ_encoder * const e )
{
const bool best = ( e->match_len_limit > 12 );
const int dis_price_count = best ? 1 : 512;
const int align_price_count = best ? 1 : dis_align_size;
const int price_count = ( e->match_len_limit > 36 ) ? 1013 : 4093;
int ahead, i;
State * const state = &e->eb.state;
if( e->eb.member_finished ) return true;
if( Re_member_position( &e->eb.renc ) >= e->eb.member_size_limit )
{ LZeb_try_full_flush( &e->eb ); return true; }
if( Mb_data_position( &e->eb.mb ) == 0 &&
!Mb_data_finished( &e->eb.mb ) ) /* encode first byte */
{
const uint8_t prev_byte = 0;
uint8_t cur_byte;
if( !Mb_enough_available_bytes( &e->eb.mb ) ||
!Re_enough_free_bytes( &e->eb.renc ) ) return true;
cur_byte = Mb_peek( &e->eb.mb, 0 );
Re_encode_bit( &e->eb.renc, &e->eb.bm_match[*state][0], 0 );
LZeb_encode_literal( &e->eb, prev_byte, cur_byte );
CRC32_update_byte( &e->eb.crc, cur_byte );
LZe_get_match_pairs( e, 0 );
if( !Mb_move_pos( &e->eb.mb ) ) return false;
}
while( !Mb_data_finished( &e->eb.mb ) )
{
if( !Mb_enough_available_bytes( &e->eb.mb ) ||
!Re_enough_free_bytes( &e->eb.renc ) ) return true;
if( e->price_counter <= 0 && e->pending_num_pairs == 0 )
{
e->price_counter = price_count; /* recalculate prices every these bytes */
if( e->dis_price_counter <= 0 )
{ e->dis_price_counter = dis_price_count; LZe_update_distance_prices( e ); }
if( e->align_price_counter <= 0 )
{
e->align_price_counter = align_price_count;
for( i = 0; i < dis_align_size; ++i )
e->align_prices[i] = price_symbol_reversed( e->eb.bm_align, i, dis_align_bits );
}
Lp_update_prices( &e->match_len_prices );
Lp_update_prices( &e->rep_len_prices );
}
ahead = LZe_sequence_optimizer( e, e->eb.reps, *state );
e->price_counter -= ahead;
for( i = 0; ahead > 0; )
{
const int pos_state =
( Mb_data_position( &e->eb.mb ) - ahead ) & pos_state_mask;
const int len = e->trials[i].price;
int dis = e->trials[i].dis4;
bool bit = ( dis < 0 );
Re_encode_bit( &e->eb.renc, &e->eb.bm_match[*state][pos_state], !bit );
if( bit ) /* literal byte */
{
const uint8_t prev_byte = Mb_peek( &e->eb.mb, ahead + 1 );
const uint8_t cur_byte = Mb_peek( &e->eb.mb, ahead );
CRC32_update_byte( &e->eb.crc, cur_byte );
if( St_is_char( *state ) )
LZeb_encode_literal( &e->eb, prev_byte, cur_byte );
else
{
const uint8_t match_byte = Mb_peek( &e->eb.mb, ahead + e->eb.reps[0] + 1 );
LZeb_encode_matched( &e->eb, prev_byte, cur_byte, match_byte );
}
*state = St_set_char( *state );
}
else /* match or repeated match */
{
CRC32_update_buf( &e->eb.crc, Mb_ptr_to_current_pos( &e->eb.mb ) - ahead, len );
mtf_reps( dis, e->eb.reps );
bit = ( dis < num_rep_distances );
Re_encode_bit( &e->eb.renc, &e->eb.bm_rep[*state], bit );
if( bit ) /* repeated match */
{
bit = ( dis == 0 );
Re_encode_bit( &e->eb.renc, &e->eb.bm_rep0[*state], !bit );
if( bit )
Re_encode_bit( &e->eb.renc, &e->eb.bm_len[*state][pos_state], len > 1 );
else
{
Re_encode_bit( &e->eb.renc, &e->eb.bm_rep1[*state], dis > 1 );
if( dis > 1 )
Re_encode_bit( &e->eb.renc, &e->eb.bm_rep2[*state], dis > 2 );
}
if( len == 1 ) *state = St_set_short_rep( *state );
else
{
Re_encode_len( &e->eb.renc, &e->eb.rep_len_model, len, pos_state );
Lp_decrement_counter( &e->rep_len_prices, pos_state );
*state = St_set_rep( *state );
}
}
else /* match */
{
dis -= num_rep_distances;
LZeb_encode_pair( &e->eb, dis, len, pos_state );
if( dis >= modeled_distances ) --e->align_price_counter;
--e->dis_price_counter;
Lp_decrement_counter( &e->match_len_prices, pos_state );
*state = St_set_match( *state );
}
}
ahead -= len; i += len;
if( Re_member_position( &e->eb.renc ) >= e->eb.member_size_limit )
{
if( !Mb_dec_pos( &e->eb.mb, ahead ) ) return false;
LZeb_try_full_flush( &e->eb );
return true;
}
}
}
LZeb_try_full_flush( &e->eb );
return true;
}

327
src/thirdparty/lzlib/encoder.h vendored Normal file
View File

@@ -0,0 +1,327 @@
/* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
struct Len_prices
{
const struct Len_model * lm;
int len_symbols;
int count;
int prices[pos_states][max_len_symbols];
int counters[pos_states]; /* may decrement below 0 */
};
static inline void Lp_update_low_mid_prices( struct Len_prices * const lp,
const int pos_state )
{
int * const pps = lp->prices[pos_state];
int tmp = price0( lp->lm->choice1 );
int len = 0;
for( ; len < len_low_symbols && len < lp->len_symbols; ++len )
pps[len] = tmp + price_symbol3( lp->lm->bm_low[pos_state], len );
if( len >= lp->len_symbols ) return;
tmp = price1( lp->lm->choice1 ) + price0( lp->lm->choice2 );
for( ; len < len_low_symbols + len_mid_symbols && len < lp->len_symbols; ++len )
pps[len] = tmp +
price_symbol3( lp->lm->bm_mid[pos_state], len - len_low_symbols );
}
static inline void Lp_update_high_prices( struct Len_prices * const lp )
{
const int tmp = price1( lp->lm->choice1 ) + price1( lp->lm->choice2 );
int len;
for( len = len_low_symbols + len_mid_symbols; len < lp->len_symbols; ++len )
/* using 4 slots per value makes "Lp_price" faster */
lp->prices[3][len] = lp->prices[2][len] =
lp->prices[1][len] = lp->prices[0][len] = tmp +
price_symbol8( lp->lm->bm_high, len - len_low_symbols - len_mid_symbols );
}
static inline void Lp_reset( struct Len_prices * const lp )
{ int i; for( i = 0; i < pos_states; ++i ) lp->counters[i] = 0; }
static inline void Lp_init( struct Len_prices * const lp,
const struct Len_model * const lm,
const int match_len_limit )
{
lp->lm = lm;
lp->len_symbols = match_len_limit + 1 - min_match_len;
lp->count = ( match_len_limit > 12 ) ? 1 : lp->len_symbols;
Lp_reset( lp );
}
static inline void Lp_decrement_counter( struct Len_prices * const lp,
const int pos_state )
{ --lp->counters[pos_state]; }
static inline void Lp_update_prices( struct Len_prices * const lp )
{
int pos_state;
bool high_pending = false;
for( pos_state = 0; pos_state < pos_states; ++pos_state )
if( lp->counters[pos_state] <= 0 )
{ lp->counters[pos_state] = lp->count;
Lp_update_low_mid_prices( lp, pos_state ); high_pending = true; }
if( high_pending && lp->len_symbols > len_low_symbols + len_mid_symbols )
Lp_update_high_prices( lp );
}
static inline int Lp_price( const struct Len_prices * const lp,
const int len, const int pos_state )
{ return lp->prices[pos_state][len - min_match_len]; }
struct Pair /* distance-length pair */
{
int dis;
int len;
};
enum { infinite_price = 0x0FFFFFFF,
max_num_trials = 1 << 13,
single_step_trial = -2,
dual_step_trial = -1 };
struct Trial
{
State state;
int price; /* dual use var; cumulative price, match length */
int dis4; /* -1 for literal, or rep, or match distance + 4 */
int prev_index; /* index of prev trial in trials[] */
int prev_index2; /* -2 trial is single step */
/* -1 literal + rep0 */
/* >= 0 ( rep or match ) + literal + rep0 */
int reps[num_rep_distances];
};
static inline void Tr_update( struct Trial * const trial, const int pr,
const int distance4, const int p_i )
{
if( pr < trial->price )
{ trial->price = pr; trial->dis4 = distance4; trial->prev_index = p_i;
trial->prev_index2 = single_step_trial; }
}
static inline void Tr_update2( struct Trial * const trial, const int pr,
const int p_i )
{
if( pr < trial->price )
{ trial->price = pr; trial->dis4 = 0; trial->prev_index = p_i;
trial->prev_index2 = dual_step_trial; }
}
static inline void Tr_update3( struct Trial * const trial, const int pr,
const int distance4, const int p_i,
const int p_i2 )
{
if( pr < trial->price )
{ trial->price = pr; trial->dis4 = distance4; trial->prev_index = p_i;
trial->prev_index2 = p_i2; }
}
struct LZ_encoder
{
struct LZ_encoder_base eb;
int cycles;
int match_len_limit;
struct Len_prices match_len_prices;
struct Len_prices rep_len_prices;
int pending_num_pairs;
struct Pair pairs[max_match_len+1];
struct Trial trials[max_num_trials];
int dis_slot_prices[len_states][2*max_dictionary_bits];
int dis_prices[len_states][modeled_distances];
int align_prices[dis_align_size];
int num_dis_slots;
int price_counter; /* counters may decrement below 0 */
int dis_price_counter;
int align_price_counter;
bool been_flushed;
};
static inline bool Mb_dec_pos( struct Matchfinder_base * const mb,
const int ahead )
{
if( ahead < 0 || mb->pos < ahead ) return false;
mb->pos -= ahead;
if( mb->cyclic_pos < ahead ) mb->cyclic_pos += mb->dictionary_size + 1;
mb->cyclic_pos -= ahead;
return true;
}
static int LZe_get_match_pairs( struct LZ_encoder * const e, struct Pair * pairs );
/* move-to-front dis in/into reps; do nothing if( dis4 <= 0 ) */
static inline void mtf_reps( const int dis4, int reps[num_rep_distances] )
{
if( dis4 >= num_rep_distances ) /* match */
{
reps[3] = reps[2]; reps[2] = reps[1]; reps[1] = reps[0];
reps[0] = dis4 - num_rep_distances;
}
else if( dis4 > 0 ) /* repeated match */
{
const int distance = reps[dis4];
int i; for( i = dis4; i > 0; --i ) reps[i] = reps[i-1];
reps[0] = distance;
}
}
static inline int LZeb_price_shortrep( const struct LZ_encoder_base * const eb,
const State state, const int pos_state )
{
return price0( eb->bm_rep0[state] ) + price0( eb->bm_len[state][pos_state] );
}
static inline int LZeb_price_rep( const struct LZ_encoder_base * const eb,
const int rep, const State state,
const int pos_state )
{
int price;
if( rep == 0 ) return price0( eb->bm_rep0[state] ) +
price1( eb->bm_len[state][pos_state] );
price = price1( eb->bm_rep0[state] );
if( rep == 1 )
price += price0( eb->bm_rep1[state] );
else
{
price += price1( eb->bm_rep1[state] );
price += price_bit( eb->bm_rep2[state], rep - 2 );
}
return price;
}
static inline int LZe_price_rep0_len( const struct LZ_encoder * const e,
const int len, const State state,
const int pos_state )
{
return LZeb_price_rep( &e->eb, 0, state, pos_state ) +
Lp_price( &e->rep_len_prices, len, pos_state );
}
static inline int LZe_price_pair( const struct LZ_encoder * const e,
const int dis, const int len,
const int pos_state )
{
const int price = Lp_price( &e->match_len_prices, len, pos_state );
const int len_state = get_len_state( len );
if( dis < modeled_distances )
return price + e->dis_prices[len_state][dis];
else
return price + e->dis_slot_prices[len_state][get_slot( dis )] +
e->align_prices[dis & (dis_align_size - 1)];
}
static inline int LZe_read_match_distances( struct LZ_encoder * const e )
{
const int num_pairs = LZe_get_match_pairs( e, e->pairs );
if( num_pairs > 0 )
{
const int len = e->pairs[num_pairs-1].len;
if( len == e->match_len_limit && len < max_match_len )
e->pairs[num_pairs-1].len =
Mb_true_match_len( &e->eb.mb, len, e->pairs[num_pairs-1].dis + 1 );
}
return num_pairs;
}
static inline bool LZe_move_and_update( struct LZ_encoder * const e, int n )
{
while( true )
{
if( !Mb_move_pos( &e->eb.mb ) ) return false;
if( --n <= 0 ) break;
LZe_get_match_pairs( e, 0 );
}
return true;
}
static inline void LZe_backward( struct LZ_encoder * const e, int cur )
{
int dis4 = e->trials[cur].dis4;
while( cur > 0 )
{
const int prev_index = e->trials[cur].prev_index;
struct Trial * const prev_trial = &e->trials[prev_index];
if( e->trials[cur].prev_index2 != single_step_trial )
{
prev_trial->dis4 = -1; /* literal */
prev_trial->prev_index = prev_index - 1;
prev_trial->prev_index2 = single_step_trial;
if( e->trials[cur].prev_index2 >= 0 )
{
struct Trial * const prev_trial2 = &e->trials[prev_index-1];
prev_trial2->dis4 = dis4; dis4 = 0; /* rep0 */
prev_trial2->prev_index = e->trials[cur].prev_index2;
prev_trial2->prev_index2 = single_step_trial;
}
}
prev_trial->price = cur - prev_index; /* len */
cur = dis4; dis4 = prev_trial->dis4; prev_trial->dis4 = cur;
cur = prev_index;
}
}
enum { num_prev_positions3 = 1 << 16,
num_prev_positions2 = 1 << 10 };
static inline bool LZe_init( struct LZ_encoder * const e,
const int dict_size, const int len_limit,
const unsigned long long member_size )
{
enum { before_size = max_num_trials,
/* bytes to keep in buffer after pos */
after_size = max_num_trials + ( 2 * max_match_len ) + 1,
dict_factor = 2,
num_prev_positions23 = num_prev_positions2 + num_prev_positions3,
pos_array_factor = 2,
min_free_bytes = 2 * max_num_trials };
if( !LZeb_init( &e->eb, before_size, dict_size, after_size, dict_factor,
num_prev_positions23, pos_array_factor, min_free_bytes,
member_size ) ) return false;
e->cycles = ( len_limit < max_match_len ) ? 16 + ( len_limit / 2 ) : 256;
e->match_len_limit = len_limit;
Lp_init( &e->match_len_prices, &e->eb.match_len_model, e->match_len_limit );
Lp_init( &e->rep_len_prices, &e->eb.rep_len_model, e->match_len_limit );
e->pending_num_pairs = 0;
e->num_dis_slots = 2 * real_bits( e->eb.mb.dictionary_size - 1 );
e->trials[1].prev_index = 0;
e->trials[1].prev_index2 = single_step_trial;
e->price_counter = 0;
e->dis_price_counter = 0;
e->align_price_counter = 0;
e->been_flushed = false;
return true;
}
static inline void LZe_reset( struct LZ_encoder * const e,
const unsigned long long member_size )
{
LZeb_reset( &e->eb, member_size );
Lp_reset( &e->match_len_prices );
Lp_reset( &e->rep_len_prices );
e->pending_num_pairs = 0;
e->price_counter = 0;
e->dis_price_counter = 0;
e->align_price_counter = 0;
e->been_flushed = false;
}

194
src/thirdparty/lzlib/encoder_base.c vendored Normal file
View File

@@ -0,0 +1,194 @@
/* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
static bool Mb_normalize_pos( struct Matchfinder_base * const mb )
{
if( mb->pos > mb->stream_pos )
{ mb->pos = mb->stream_pos; return false; }
if( !mb->at_stream_end )
{
int i;
const int offset = mb->pos - mb->before_size - mb->dictionary_size;
const int size = mb->stream_pos - offset;
memmove( mb->buffer, mb->buffer + offset, size );
mb->partial_data_pos += offset;
mb->pos -= offset; /* pos = before_size + dictionary_size */
mb->stream_pos -= offset;
for( i = 0; i < mb->num_prev_positions; ++i )
mb->prev_positions[i] -= min( mb->prev_positions[i], offset );
for( i = 0; i < mb->pos_array_size; ++i )
mb->pos_array[i] -= min( mb->pos_array[i], offset );
}
return true;
}
static bool Mb_init( struct Matchfinder_base * const mb, const int before_size,
const int dict_size, const int after_size,
const int dict_factor, const int num_prev_positions23,
const int pos_array_factor )
{
const int buffer_size_limit =
( dict_factor * dict_size ) + before_size + after_size;
unsigned size;
int i;
mb->partial_data_pos = 0;
mb->before_size = before_size;
mb->after_size = after_size;
mb->pos = 0;
mb->cyclic_pos = 0;
mb->stream_pos = 0;
mb->num_prev_positions23 = num_prev_positions23;
mb->at_stream_end = false;
mb->flushing = false;
mb->buffer_size = max( 65536, buffer_size_limit );
mb->buffer = (uint8_t *)malloc( mb->buffer_size );
if( !mb->buffer ) return false;
mb->saved_dictionary_size = dict_size;
mb->dictionary_size = dict_size;
mb->pos_limit = mb->buffer_size - after_size;
size = 1 << max( 16, real_bits( mb->dictionary_size - 1 ) - 2 );
if( mb->dictionary_size > 1 << 26 ) /* 64 MiB */
size >>= 1;
mb->key4_mask = size - 1;
size += num_prev_positions23;
mb->num_prev_positions = size;
mb->pos_array_size = pos_array_factor * ( mb->dictionary_size + 1 );
size += mb->pos_array_size;
if( size * sizeof mb->prev_positions[0] <= size ) mb->prev_positions = 0;
else mb->prev_positions =
(int32_t *)malloc( size * sizeof mb->prev_positions[0] );
if( !mb->prev_positions ) { free( mb->buffer ); return false; }
mb->pos_array = mb->prev_positions + mb->num_prev_positions;
for( i = 0; i < mb->num_prev_positions; ++i ) mb->prev_positions[i] = 0;
return true;
}
static void Mb_adjust_array( struct Matchfinder_base * const mb )
{
int size = 1 << max( 16, real_bits( mb->dictionary_size - 1 ) - 2 );
if( mb->dictionary_size > 1 << 26 ) /* 64 MiB */
size >>= 1;
mb->key4_mask = size - 1;
size += mb->num_prev_positions23;
mb->num_prev_positions = size;
mb->pos_array = mb->prev_positions + mb->num_prev_positions;
}
static void Mb_adjust_dictionary_size( struct Matchfinder_base * const mb )
{
if( mb->stream_pos < mb->dictionary_size )
{
mb->dictionary_size = max( min_dictionary_size, mb->stream_pos );
Mb_adjust_array( mb );
mb->pos_limit = mb->buffer_size;
}
}
static void Mb_reset( struct Matchfinder_base * const mb )
{
int i;
if( mb->stream_pos > mb->pos )
memmove( mb->buffer, mb->buffer + mb->pos, mb->stream_pos - mb->pos );
mb->partial_data_pos = 0;
mb->stream_pos -= mb->pos;
mb->pos = 0;
mb->cyclic_pos = 0;
mb->at_stream_end = false;
mb->flushing = false;
mb->dictionary_size = mb->saved_dictionary_size;
Mb_adjust_array( mb );
mb->pos_limit = mb->buffer_size - mb->after_size;
for( i = 0; i < mb->num_prev_positions; ++i ) mb->prev_positions[i] = 0;
}
/* End Of Stream mark => (dis == 0xFFFFFFFFU, len == min_match_len) */
static void LZeb_try_full_flush( struct LZ_encoder_base * const eb )
{
int i;
const int pos_state = Mb_data_position( &eb->mb ) & pos_state_mask;
const State state = eb->state;
File_trailer trailer;
if( eb->member_finished ||
Cb_free_bytes( &eb->renc.cb ) < max_marker_size + eb->renc.ff_count + Ft_size )
return;
eb->member_finished = true;
Re_encode_bit( &eb->renc, &eb->bm_match[state][pos_state], 1 );
Re_encode_bit( &eb->renc, &eb->bm_rep[state], 0 );
LZeb_encode_pair( eb, 0xFFFFFFFFU, min_match_len, pos_state );
Re_flush( &eb->renc );
Ft_set_data_crc( trailer, LZeb_crc( eb ) );
Ft_set_data_size( trailer, Mb_data_position( &eb->mb ) );
Ft_set_member_size( trailer, Re_member_position( &eb->renc ) + Ft_size );
for( i = 0; i < Ft_size; ++i )
Cb_put_byte( &eb->renc.cb, trailer[i] );
}
/* Sync Flush mark => (dis == 0xFFFFFFFFU, len == min_match_len + 1) */
static bool LZeb_sync_flush( struct LZ_encoder_base * const eb )
{
int i;
const int pos_state = Mb_data_position( &eb->mb ) & pos_state_mask;
const State state = eb->state;
if( eb->member_finished ||
Cb_free_bytes( &eb->renc.cb ) < (2 * max_marker_size) + eb->renc.ff_count )
return false;
for( i = 0; i < 2; ++i ) /* 2 consecutive markers guarantee decoding */
{
Re_encode_bit( &eb->renc, &eb->bm_match[state][pos_state], 1 );
Re_encode_bit( &eb->renc, &eb->bm_rep[state], 0 );
LZeb_encode_pair( eb, 0xFFFFFFFFU, min_match_len + 1, pos_state );
Re_flush( &eb->renc );
}
return true;
}
static void LZeb_reset( struct LZ_encoder_base * const eb,
const unsigned long long member_size )
{
int i;
Mb_reset( &eb->mb );
eb->member_size_limit = member_size - Ft_size - max_marker_size;
eb->crc = 0xFFFFFFFFU;
Bm_array_init( eb->bm_literal[0], (1 << literal_context_bits) * 0x300 );
Bm_array_init( eb->bm_match[0], states * pos_states );
Bm_array_init( eb->bm_rep, states );
Bm_array_init( eb->bm_rep0, states );
Bm_array_init( eb->bm_rep1, states );
Bm_array_init( eb->bm_rep2, states );
Bm_array_init( eb->bm_len[0], states * pos_states );
Bm_array_init( eb->bm_dis_slot[0], len_states * (1 << dis_slot_bits) );
Bm_array_init( eb->bm_dis, modeled_distances - end_dis_model + 1 );
Bm_array_init( eb->bm_align, dis_align_size );
Lm_init( &eb->match_len_model );
Lm_init( &eb->rep_len_model );
Re_reset( &eb->renc, eb->mb.dictionary_size );
for( i = 0; i < num_rep_distances; ++i ) eb->reps[i] = 0;
eb->state = 0;
eb->member_finished = false;
}

615
src/thirdparty/lzlib/encoder_base.h vendored Normal file
View File

@@ -0,0 +1,615 @@
/* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
enum { price_shift_bits = 6,
price_step_bits = 2 };
static const uint8_t dis_slots[1<<10] =
{
0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19 };
static inline uint8_t get_slot( const unsigned dis )
{
if( dis < (1 << 10) ) return dis_slots[dis];
if( dis < (1 << 19) ) return dis_slots[dis>> 9] + 18;
if( dis < (1 << 28) ) return dis_slots[dis>>18] + 36;
return dis_slots[dis>>27] + 54;
}
static const short prob_prices[bit_model_total >> price_step_bits] =
{
640, 539, 492, 461, 438, 419, 404, 390, 379, 369, 359, 351, 343, 336, 330, 323,
318, 312, 307, 302, 298, 293, 289, 285, 281, 277, 274, 270, 267, 264, 261, 258,
255, 252, 250, 247, 244, 242, 239, 237, 235, 232, 230, 228, 226, 224, 222, 220,
218, 216, 214, 213, 211, 209, 207, 206, 204, 202, 201, 199, 198, 196, 195, 193,
192, 190, 189, 188, 186, 185, 184, 182, 181, 180, 178, 177, 176, 175, 174, 172,
171, 170, 169, 168, 167, 166, 165, 164, 163, 162, 161, 159, 158, 157, 157, 156,
155, 154, 153, 152, 151, 150, 149, 148, 147, 146, 145, 145, 144, 143, 142, 141,
140, 140, 139, 138, 137, 136, 136, 135, 134, 133, 133, 132, 131, 130, 130, 129,
128, 127, 127, 126, 125, 125, 124, 123, 123, 122, 121, 121, 120, 119, 119, 118,
117, 117, 116, 115, 115, 114, 114, 113, 112, 112, 111, 111, 110, 109, 109, 108,
108, 107, 106, 106, 105, 105, 104, 104, 103, 103, 102, 101, 101, 100, 100, 99,
99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 92, 91,
91, 90, 90, 89, 89, 88, 88, 88, 87, 87, 86, 86, 85, 85, 84, 84,
83, 83, 83, 82, 82, 81, 81, 80, 80, 80, 79, 79, 78, 78, 77, 77,
77, 76, 76, 75, 75, 75, 74, 74, 73, 73, 73, 72, 72, 71, 71, 71,
70, 70, 70, 69, 69, 68, 68, 68, 67, 67, 67, 66, 66, 65, 65, 65,
64, 64, 64, 63, 63, 63, 62, 62, 61, 61, 61, 60, 60, 60, 59, 59,
59, 58, 58, 58, 57, 57, 57, 56, 56, 56, 55, 55, 55, 54, 54, 54,
53, 53, 53, 53, 52, 52, 52, 51, 51, 51, 50, 50, 50, 49, 49, 49,
48, 48, 48, 48, 47, 47, 47, 46, 46, 46, 45, 45, 45, 45, 44, 44,
44, 43, 43, 43, 43, 42, 42, 42, 41, 41, 41, 41, 40, 40, 40, 40,
39, 39, 39, 38, 38, 38, 38, 37, 37, 37, 37, 36, 36, 36, 35, 35,
35, 35, 34, 34, 34, 34, 33, 33, 33, 33, 32, 32, 32, 32, 31, 31,
31, 31, 30, 30, 30, 30, 29, 29, 29, 29, 28, 28, 28, 28, 27, 27,
27, 27, 26, 26, 26, 26, 26, 25, 25, 25, 25, 24, 24, 24, 24, 23,
23, 23, 23, 22, 22, 22, 22, 22, 21, 21, 21, 21, 20, 20, 20, 20,
20, 19, 19, 19, 19, 18, 18, 18, 18, 18, 17, 17, 17, 17, 17, 16,
16, 16, 16, 15, 15, 15, 15, 15, 14, 14, 14, 14, 14, 13, 13, 13,
13, 13, 12, 12, 12, 12, 12, 11, 11, 11, 11, 10, 10, 10, 10, 10,
9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7,
6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4,
3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1 };
static inline int get_price( const int probability )
{ return prob_prices[probability >> price_step_bits]; }
static inline int price0( const Bit_model probability )
{ return get_price( probability ); }
static inline int price1( const Bit_model probability )
{ return get_price( bit_model_total - probability ); }
static inline int price_bit( const Bit_model bm, const bool bit )
{ return ( bit ? price1( bm ) : price0( bm ) ); }
static inline int price_symbol3( const Bit_model bm[], int symbol )
{
int price;
bool bit = symbol & 1;
symbol |= 8; symbol >>= 1;
price = price_bit( bm[symbol], bit );
bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit );
return price + price_bit( bm[1], symbol & 1 );
}
static inline int price_symbol6( const Bit_model bm[], unsigned symbol )
{
int price;
bool bit = symbol & 1;
symbol |= 64; symbol >>= 1;
price = price_bit( bm[symbol], bit );
bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit );
bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit );
bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit );
bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit );
return price + price_bit( bm[1], symbol & 1 );
}
static inline int price_symbol8( const Bit_model bm[], int symbol )
{
int price;
bool bit = symbol & 1;
symbol |= 0x100; symbol >>= 1;
price = price_bit( bm[symbol], bit );
bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit );
bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit );
bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit );
bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit );
bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit );
bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit );
return price + price_bit( bm[1], symbol & 1 );
}
static inline int price_symbol_reversed( const Bit_model bm[], int symbol,
const int num_bits )
{
int price = 0;
int model = 1;
int i;
for( i = num_bits; i > 0; --i )
{
const bool bit = symbol & 1;
symbol >>= 1;
price += price_bit( bm[model], bit );
model = ( model << 1 ) | bit;
}
return price;
}
static inline int price_matched( const Bit_model bm[], unsigned symbol,
unsigned match_byte )
{
int price = 0;
unsigned mask = 0x100;
symbol |= mask;
while( true )
{
const unsigned match_bit = ( match_byte <<= 1 ) & mask;
const bool bit = ( symbol <<= 1 ) & 0x100;
price += price_bit( bm[(symbol>>9)+match_bit+mask], bit );
if( symbol >= 0x10000 ) return price;
mask &= ~(match_bit ^ symbol); /* if( match_bit != bit ) mask = 0; */
}
}
struct Matchfinder_base
{
unsigned long long partial_data_pos;
uint8_t * buffer; /* input buffer */
int32_t * prev_positions; /* 1 + last seen position of key. else 0 */
int32_t * pos_array; /* may be tree or chain */
int before_size; /* bytes to keep in buffer before dictionary */
int after_size; /* bytes to keep in buffer after pos */
int buffer_size;
int dictionary_size; /* bytes to keep in buffer before pos */
int pos; /* current pos in buffer */
int cyclic_pos; /* cycles through [0, dictionary_size] */
int stream_pos; /* first byte not yet read from file */
int pos_limit; /* when reached, a new block must be read */
int key4_mask;
int num_prev_positions23;
int num_prev_positions; /* size of prev_positions */
int pos_array_size;
int saved_dictionary_size; /* dictionary_size restored by Mb_reset */
bool at_stream_end; /* stream_pos shows real end of file */
bool flushing;
};
static bool Mb_normalize_pos( struct Matchfinder_base * const mb );
static bool Mb_init( struct Matchfinder_base * const mb, const int before_size,
const int dict_size, const int after_size,
const int dict_factor, const int num_prev_positions23,
const int pos_array_factor );
static inline void Mb_free( struct Matchfinder_base * const mb )
{ free( mb->prev_positions ); free( mb->buffer ); }
static inline uint8_t Mb_peek( const struct Matchfinder_base * const mb,
const int distance )
{ return mb->buffer[mb->pos-distance]; }
static inline int Mb_available_bytes( const struct Matchfinder_base * const mb )
{ return mb->stream_pos - mb->pos; }
static inline unsigned long long
Mb_data_position( const struct Matchfinder_base * const mb )
{ return mb->partial_data_pos + mb->pos; }
static inline void Mb_finish( struct Matchfinder_base * const mb )
{ mb->at_stream_end = true; mb->flushing = false; }
static inline bool Mb_data_finished( const struct Matchfinder_base * const mb )
{ return mb->at_stream_end && !mb->flushing && mb->pos >= mb->stream_pos; }
static inline bool Mb_flushing_or_end( const struct Matchfinder_base * const mb )
{ return mb->at_stream_end || mb->flushing; }
static inline int Mb_free_bytes( const struct Matchfinder_base * const mb )
{ if( Mb_flushing_or_end( mb ) ) return 0;
return mb->buffer_size - mb->stream_pos; }
static inline bool Mb_enough_available_bytes( const struct Matchfinder_base * const mb )
{ return ( mb->pos + mb->after_size <= mb->stream_pos ||
( Mb_flushing_or_end( mb ) && mb->pos < mb->stream_pos ) ); }
static inline const uint8_t *
Mb_ptr_to_current_pos( const struct Matchfinder_base * const mb )
{ return mb->buffer + mb->pos; }
static int Mb_write_data( struct Matchfinder_base * const mb,
const uint8_t * const inbuf, const int size )
{
const int sz = min( mb->buffer_size - mb->stream_pos, size );
if( Mb_flushing_or_end( mb ) || sz <= 0 ) return 0;
memcpy( mb->buffer + mb->stream_pos, inbuf, sz );
mb->stream_pos += sz;
return sz;
}
static inline int Mb_true_match_len( const struct Matchfinder_base * const mb,
const int index, const int distance )
{
const uint8_t * const data = mb->buffer + mb->pos;
int i = index;
const int len_limit = min( Mb_available_bytes( mb ), max_match_len );
while( i < len_limit && data[i-distance] == data[i] ) ++i;
return i;
}
static inline bool Mb_move_pos( struct Matchfinder_base * const mb )
{
if( ++mb->cyclic_pos > mb->dictionary_size ) mb->cyclic_pos = 0;
if( ++mb->pos >= mb->pos_limit ) return Mb_normalize_pos( mb );
return true;
}
struct Range_encoder
{
struct Circular_buffer cb;
unsigned min_free_bytes;
uint64_t low;
unsigned long long partial_member_pos;
uint32_t range;
unsigned ff_count;
uint8_t cache;
File_header header;
};
static inline void Re_shift_low( struct Range_encoder * const renc )
{
if( renc->low >> 24 != 0xFF )
{
const bool carry = ( renc->low > 0xFFFFFFFFU );
Cb_put_byte( &renc->cb, renc->cache + carry );
for( ; renc->ff_count > 0; --renc->ff_count )
Cb_put_byte( &renc->cb, 0xFF + carry );
renc->cache = renc->low >> 24;
}
else ++renc->ff_count;
renc->low = ( renc->low & 0x00FFFFFFU ) << 8;
}
static inline void Re_reset( struct Range_encoder * const renc,
const unsigned dictionary_size )
{
int i;
Cb_reset( &renc->cb );
renc->low = 0;
renc->partial_member_pos = 0;
renc->range = 0xFFFFFFFFU;
renc->ff_count = 0;
renc->cache = 0;
Fh_set_dictionary_size( renc->header, dictionary_size );
for( i = 0; i < Fh_size; ++i )
Cb_put_byte( &renc->cb, renc->header[i] );
}
static inline bool Re_init( struct Range_encoder * const renc,
const unsigned dictionary_size,
const unsigned min_free_bytes )
{
if( !Cb_init( &renc->cb, 65536 + min_free_bytes ) ) return false;
renc->min_free_bytes = min_free_bytes;
Fh_set_magic( renc->header );
Re_reset( renc, dictionary_size );
return true;
}
static inline void Re_free( struct Range_encoder * const renc )
{ Cb_free( &renc->cb ); }
static inline unsigned long long
Re_member_position( const struct Range_encoder * const renc )
{ return renc->partial_member_pos + Cb_used_bytes( &renc->cb ) + renc->ff_count; }
static inline bool Re_enough_free_bytes( const struct Range_encoder * const renc )
{ return Cb_free_bytes( &renc->cb ) >= renc->min_free_bytes + renc->ff_count; }
static inline int Re_read_data( struct Range_encoder * const renc,
uint8_t * const out_buffer, const int out_size )
{
const int size = Cb_read_data( &renc->cb, out_buffer, out_size );
if( size > 0 ) renc->partial_member_pos += size;
return size;
}
static inline void Re_flush( struct Range_encoder * const renc )
{
int i; for( i = 0; i < 5; ++i ) Re_shift_low( renc );
renc->low = 0;
renc->range = 0xFFFFFFFFU;
renc->ff_count = 0;
renc->cache = 0;
}
static inline void Re_encode( struct Range_encoder * const renc,
const int symbol, const int num_bits )
{
unsigned mask;
for( mask = 1 << ( num_bits - 1 ); mask > 0; mask >>= 1 )
{
renc->range >>= 1;
if( symbol & mask ) renc->low += renc->range;
if( renc->range <= 0x00FFFFFFU )
{ renc->range <<= 8; Re_shift_low( renc ); }
}
}
static inline void Re_encode_bit( struct Range_encoder * const renc,
Bit_model * const probability, const bool bit )
{
const uint32_t bound = ( renc->range >> bit_model_total_bits ) * *probability;
if( !bit )
{
renc->range = bound;
*probability += (bit_model_total - *probability) >> bit_model_move_bits;
}
else
{
renc->low += bound;
renc->range -= bound;
*probability -= *probability >> bit_model_move_bits;
}
if( renc->range <= 0x00FFFFFFU ) { renc->range <<= 8; Re_shift_low( renc ); }
}
static inline void Re_encode_tree3( struct Range_encoder * const renc,
Bit_model bm[], const int symbol )
{
int model = 1;
bool bit = ( symbol >> 2 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit;
bit = ( symbol >> 1 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit;
Re_encode_bit( renc, &bm[model], symbol & 1 );
}
static inline void Re_encode_tree6( struct Range_encoder * const renc,
Bit_model bm[], const unsigned symbol )
{
int model = 1;
bool bit = ( symbol >> 5 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit;
bit = ( symbol >> 4 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit;
bit = ( symbol >> 3 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit;
bit = ( symbol >> 2 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit;
bit = ( symbol >> 1 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit;
Re_encode_bit( renc, &bm[model], symbol & 1 );
}
static inline void Re_encode_tree8( struct Range_encoder * const renc,
Bit_model bm[], const int symbol )
{
int model = 1;
int i;
for( i = 7; i >= 0; --i )
{
const bool bit = ( symbol >> i ) & 1;
Re_encode_bit( renc, &bm[model], bit );
model = ( model << 1 ) | bit;
}
}
static inline void Re_encode_tree_reversed( struct Range_encoder * const renc,
Bit_model bm[], int symbol, const int num_bits )
{
int model = 1;
int i;
for( i = num_bits; i > 0; --i )
{
const bool bit = symbol & 1;
symbol >>= 1;
Re_encode_bit( renc, &bm[model], bit );
model = ( model << 1 ) | bit;
}
}
static inline void Re_encode_matched( struct Range_encoder * const renc,
Bit_model bm[], unsigned symbol,
unsigned match_byte )
{
unsigned mask = 0x100;
symbol |= mask;
while( true )
{
const unsigned match_bit = ( match_byte <<= 1 ) & mask;
const bool bit = ( symbol <<= 1 ) & 0x100;
Re_encode_bit( renc, &bm[(symbol>>9)+match_bit+mask], bit );
if( symbol >= 0x10000 ) break;
mask &= ~(match_bit ^ symbol); /* if( match_bit != bit ) mask = 0; */
}
}
static inline void Re_encode_len( struct Range_encoder * const renc,
struct Len_model * const lm,
int symbol, const int pos_state )
{
bool bit = ( ( symbol -= min_match_len ) >= len_low_symbols );
Re_encode_bit( renc, &lm->choice1, bit );
if( !bit )
Re_encode_tree3( renc, lm->bm_low[pos_state], symbol );
else
{
bit = ( ( symbol -= len_low_symbols ) >= len_mid_symbols );
Re_encode_bit( renc, &lm->choice2, bit );
if( !bit )
Re_encode_tree3( renc, lm->bm_mid[pos_state], symbol );
else
Re_encode_tree8( renc, lm->bm_high, symbol - len_mid_symbols );
}
}
enum { max_marker_size = 16,
num_rep_distances = 4 }; /* must be 4 */
struct LZ_encoder_base
{
struct Matchfinder_base mb;
unsigned long long member_size_limit;
uint32_t crc;
Bit_model bm_literal[1<<literal_context_bits][0x300];
Bit_model bm_match[states][pos_states];
Bit_model bm_rep[states];
Bit_model bm_rep0[states];
Bit_model bm_rep1[states];
Bit_model bm_rep2[states];
Bit_model bm_len[states][pos_states];
Bit_model bm_dis_slot[len_states][1<<dis_slot_bits];
Bit_model bm_dis[modeled_distances-end_dis_model+1];
Bit_model bm_align[dis_align_size];
struct Len_model match_len_model;
struct Len_model rep_len_model;
struct Range_encoder renc;
int reps[num_rep_distances];
State state;
bool member_finished;
};
static void LZeb_reset( struct LZ_encoder_base * const eb,
const unsigned long long member_size );
static inline bool LZeb_init( struct LZ_encoder_base * const eb,
const int before_size, const int dict_size,
const int after_size, const int dict_factor,
const int num_prev_positions23,
const int pos_array_factor,
const unsigned min_free_bytes,
const unsigned long long member_size )
{
if( !Mb_init( &eb->mb, before_size, dict_size, after_size, dict_factor,
num_prev_positions23, pos_array_factor ) ) return false;
if( !Re_init( &eb->renc, eb->mb.dictionary_size, min_free_bytes ) )
return false;
LZeb_reset( eb, member_size );
return true;
}
static inline bool LZeb_member_finished( const struct LZ_encoder_base * const eb )
{ return ( eb->member_finished && !Cb_used_bytes( &eb->renc.cb ) ); }
static inline void LZeb_free( struct LZ_encoder_base * const eb )
{ Re_free( &eb->renc ); Mb_free( &eb->mb ); }
static inline unsigned LZeb_crc( const struct LZ_encoder_base * const eb )
{ return eb->crc ^ 0xFFFFFFFFU; }
static inline int LZeb_price_literal( const struct LZ_encoder_base * const eb,
const uint8_t prev_byte, const uint8_t symbol )
{ return price_symbol8( eb->bm_literal[get_lit_state(prev_byte)], symbol ); }
static inline int LZeb_price_matched( const struct LZ_encoder_base * const eb,
const uint8_t prev_byte, const uint8_t symbol, const uint8_t match_byte )
{ return price_matched( eb->bm_literal[get_lit_state(prev_byte)], symbol,
match_byte ); }
static inline void LZeb_encode_literal( struct LZ_encoder_base * const eb,
const uint8_t prev_byte, const uint8_t symbol )
{ Re_encode_tree8( &eb->renc, eb->bm_literal[get_lit_state(prev_byte)],
symbol ); }
static inline void LZeb_encode_matched( struct LZ_encoder_base * const eb,
const uint8_t prev_byte, const uint8_t symbol, const uint8_t match_byte )
{ Re_encode_matched( &eb->renc, eb->bm_literal[get_lit_state(prev_byte)],
symbol, match_byte ); }
static inline void LZeb_encode_pair( struct LZ_encoder_base * const eb,
const unsigned dis, const int len,
const int pos_state )
{
const unsigned dis_slot = get_slot( dis );
Re_encode_len( &eb->renc, &eb->match_len_model, len, pos_state );
Re_encode_tree6( &eb->renc, eb->bm_dis_slot[get_len_state(len)], dis_slot );
if( dis_slot >= start_dis_model )
{
const int direct_bits = ( dis_slot >> 1 ) - 1;
const unsigned base = ( 2 | ( dis_slot & 1 ) ) << direct_bits;
const unsigned direct_dis = dis - base;
if( dis_slot < end_dis_model )
Re_encode_tree_reversed( &eb->renc, eb->bm_dis + ( base - dis_slot ),
direct_dis, direct_bits );
else
{
Re_encode( &eb->renc, direct_dis >> dis_align_bits,
direct_bits - dis_align_bits );
Re_encode_tree_reversed( &eb->renc, eb->bm_align, direct_dis, dis_align_bits );
}
}
}

182
src/thirdparty/lzlib/fast_encoder.c vendored Normal file
View File

@@ -0,0 +1,182 @@
/* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
int FLZe_longest_match_len( struct FLZ_encoder * const fe, int * const distance )
{
enum { len_limit = 16 };
const uint8_t * const data = Mb_ptr_to_current_pos( &fe->eb.mb );
int32_t * ptr0 = fe->eb.mb.pos_array + fe->eb.mb.cyclic_pos;
const int pos1 = fe->eb.mb.pos + 1;
int maxlen = 0, newpos1, count;
const int available = min( Mb_available_bytes( &fe->eb.mb ), max_match_len );
if( available < len_limit ) { *ptr0 = 0; return 0; }
fe->key4 = ( ( fe->key4 << 4 ) ^ data[3] ) & fe->eb.mb.key4_mask;
newpos1 = fe->eb.mb.prev_positions[fe->key4];
fe->eb.mb.prev_positions[fe->key4] = pos1;
for( count = 4; ; )
{
int32_t * newptr;
int delta;
if( newpos1 <= 0 || --count < 0 ||
( delta = pos1 - newpos1 ) > fe->eb.mb.dictionary_size )
{ *ptr0 = 0; break; }
newptr = fe->eb.mb.pos_array +
( fe->eb.mb.cyclic_pos - delta +
( ( fe->eb.mb.cyclic_pos >= delta ) ? 0 : fe->eb.mb.dictionary_size + 1 ) );
if( data[maxlen-delta] == data[maxlen] )
{
int len = 0;
while( len < available && data[len-delta] == data[len] ) ++len;
if( maxlen < len )
{ maxlen = len; *distance = delta - 1;
if( maxlen >= len_limit ) { *ptr0 = *newptr; break; } }
}
*ptr0 = newpos1;
ptr0 = newptr;
newpos1 = *ptr0;
}
return maxlen;
}
bool FLZe_encode_member( struct FLZ_encoder * const fe )
{
int rep = 0, i;
State * const state = &fe->eb.state;
if( fe->eb.member_finished ) return true;
if( Re_member_position( &fe->eb.renc ) >= fe->eb.member_size_limit )
{ LZeb_try_full_flush( &fe->eb ); return true; }
if( Mb_data_position( &fe->eb.mb ) == 0 &&
!Mb_data_finished( &fe->eb.mb ) ) /* encode first byte */
{
const uint8_t prev_byte = 0;
uint8_t cur_byte;
if( !Mb_enough_available_bytes( &fe->eb.mb ) ||
!Re_enough_free_bytes( &fe->eb.renc ) ) return true;
cur_byte = Mb_peek( &fe->eb.mb, 0 );
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_match[*state][0], 0 );
LZeb_encode_literal( &fe->eb, prev_byte, cur_byte );
CRC32_update_byte( &fe->eb.crc, cur_byte );
FLZe_reset_key4( fe );
if( !FLZe_update_and_move( fe, 1 ) ) return false;
}
while( !Mb_data_finished( &fe->eb.mb ) &&
Re_member_position( &fe->eb.renc ) < fe->eb.member_size_limit )
{
int match_distance;
int main_len, pos_state;
int len = 0;
if( !Mb_enough_available_bytes( &fe->eb.mb ) ||
!Re_enough_free_bytes( &fe->eb.renc ) ) return true;
main_len = FLZe_longest_match_len( fe, &match_distance );
pos_state = Mb_data_position( &fe->eb.mb ) & pos_state_mask;
for( i = 0; i < num_rep_distances; ++i )
{
const int tlen = Mb_true_match_len( &fe->eb.mb, 0, fe->eb.reps[i] + 1 );
if( tlen > len ) { len = tlen; rep = i; }
}
if( len > min_match_len && len + 3 > main_len )
{
CRC32_update_buf( &fe->eb.crc, Mb_ptr_to_current_pos( &fe->eb.mb ), len );
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_match[*state][pos_state], 1 );
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_rep[*state], 1 );
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_rep0[*state], rep != 0 );
if( rep == 0 )
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_len[*state][pos_state], 1 );
else
{
int distance;
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_rep1[*state], rep > 1 );
if( rep > 1 )
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_rep2[*state], rep > 2 );
distance = fe->eb.reps[rep];
for( i = rep; i > 0; --i ) fe->eb.reps[i] = fe->eb.reps[i-1];
fe->eb.reps[0] = distance;
}
*state = St_set_rep( *state );
Re_encode_len( &fe->eb.renc, &fe->eb.rep_len_model, len, pos_state );
if( !Mb_move_pos( &fe->eb.mb ) ) return false;
if( !FLZe_update_and_move( fe, len - 1 ) ) return false;
continue;
}
if( main_len > min_match_len )
{
CRC32_update_buf( &fe->eb.crc, Mb_ptr_to_current_pos( &fe->eb.mb ), main_len );
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_match[*state][pos_state], 1 );
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_rep[*state], 0 );
*state = St_set_match( *state );
for( i = num_rep_distances - 1; i > 0; --i ) fe->eb.reps[i] = fe->eb.reps[i-1];
fe->eb.reps[0] = match_distance;
LZeb_encode_pair( &fe->eb, match_distance, main_len, pos_state );
if( !Mb_move_pos( &fe->eb.mb ) ) return false;
if( !FLZe_update_and_move( fe, main_len - 1 ) ) return false;
continue;
}
{
const uint8_t prev_byte = Mb_peek( &fe->eb.mb, 1 );
const uint8_t cur_byte = Mb_peek( &fe->eb.mb, 0 );
const uint8_t match_byte = Mb_peek( &fe->eb.mb, fe->eb.reps[0] + 1 );
if( !Mb_move_pos( &fe->eb.mb ) ) return false;
CRC32_update_byte( &fe->eb.crc, cur_byte );
if( match_byte == cur_byte )
{
const int short_rep_price = price1( fe->eb.bm_match[*state][pos_state] ) +
price1( fe->eb.bm_rep[*state] ) +
price0( fe->eb.bm_rep0[*state] ) +
price0( fe->eb.bm_len[*state][pos_state] );
int price = price0( fe->eb.bm_match[*state][pos_state] );
if( St_is_char( *state ) )
price += LZeb_price_literal( &fe->eb, prev_byte, cur_byte );
else
price += LZeb_price_matched( &fe->eb, prev_byte, cur_byte, match_byte );
if( short_rep_price < price )
{
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_match[*state][pos_state], 1 );
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_rep[*state], 1 );
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_rep0[*state], 0 );
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_len[*state][pos_state], 0 );
*state = St_set_short_rep( *state );
continue;
}
}
/* literal byte */
Re_encode_bit( &fe->eb.renc, &fe->eb.bm_match[*state][pos_state], 0 );
if( St_is_char( *state ) )
LZeb_encode_literal( &fe->eb, prev_byte, cur_byte );
else
LZeb_encode_matched( &fe->eb, prev_byte, cur_byte, match_byte );
*state = St_set_char( *state );
}
}
LZeb_try_full_flush( &fe->eb );
return true;
}

72
src/thirdparty/lzlib/fast_encoder.h vendored Normal file
View File

@@ -0,0 +1,72 @@
/* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
struct FLZ_encoder
{
struct LZ_encoder_base eb;
unsigned key4; /* key made from latest 4 bytes */
};
static inline void FLZe_reset_key4( struct FLZ_encoder * const fe )
{
int i;
fe->key4 = 0;
for( i = 0; i < 3 && i < Mb_available_bytes( &fe->eb.mb ); ++i )
fe->key4 = ( fe->key4 << 4 ) ^ fe->eb.mb.buffer[i];
}
int FLZe_longest_match_len( struct FLZ_encoder * const fe, int * const distance );
static inline bool FLZe_update_and_move( struct FLZ_encoder * const fe, int n )
{
while( --n >= 0 )
{
if( Mb_available_bytes( &fe->eb.mb ) >= 4 )
{
fe->key4 = ( ( fe->key4 << 4 ) ^ fe->eb.mb.buffer[fe->eb.mb.pos+3] ) &
fe->eb.mb.key4_mask;
fe->eb.mb.pos_array[fe->eb.mb.cyclic_pos] = fe->eb.mb.prev_positions[fe->key4];
fe->eb.mb.prev_positions[fe->key4] = fe->eb.mb.pos + 1;
}
else fe->eb.mb.pos_array[fe->eb.mb.cyclic_pos] = 0;
if( !Mb_move_pos( &fe->eb.mb ) ) return false;
}
return true;
}
static inline bool FLZe_init( struct FLZ_encoder * const fe,
const unsigned long long member_size )
{
enum { before_size = 0,
dict_size = 65536,
/* bytes to keep in buffer after pos */
after_size = max_match_len,
dict_factor = 16,
num_prev_positions23 = 0,
pos_array_factor = 1,
min_free_bytes = max_marker_size };
return LZeb_init( &fe->eb, before_size, dict_size, after_size, dict_factor,
num_prev_positions23, pos_array_factor, min_free_bytes,
member_size );
}
static inline void FLZe_reset( struct FLZ_encoder * const fe,
const unsigned long long member_size )
{ LZeb_reset( &fe->eb, member_size ); }

297
src/thirdparty/lzlib/lzip.h vendored Normal file
View File

@@ -0,0 +1,297 @@
/* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef max
#define max(x,y) ((x) >= (y) ? (x) : (y))
#endif
#ifndef min
#define min(x,y) ((x) <= (y) ? (x) : (y))
#endif
typedef int State;
enum { states = 12 };
static inline bool St_is_char( const State st ) { return st < 7; }
static inline State St_set_char( const State st )
{
static const State next[states] = { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
return next[st];
}
static inline State St_set_match( const State st )
{ return ( ( st < 7 ) ? 7 : 10 ); }
static inline State St_set_rep( const State st )
{ return ( ( st < 7 ) ? 8 : 11 ); }
static inline State St_set_short_rep( const State st )
{ return ( ( st < 7 ) ? 9 : 11 ); }
enum {
min_dictionary_bits = 12,
min_dictionary_size = 1 << min_dictionary_bits, /* >= modeled_distances */
max_dictionary_bits = 29,
max_dictionary_size = 1 << max_dictionary_bits,
literal_context_bits = 3,
literal_pos_state_bits = 0, /* not used */
pos_state_bits = 2,
pos_states = 1 << pos_state_bits,
pos_state_mask = pos_states - 1,
len_states = 4,
dis_slot_bits = 6,
start_dis_model = 4,
end_dis_model = 14,
modeled_distances = 1 << (end_dis_model / 2), /* 128 */
dis_align_bits = 4,
dis_align_size = 1 << dis_align_bits,
len_low_bits = 3,
len_mid_bits = 3,
len_high_bits = 8,
len_low_symbols = 1 << len_low_bits,
len_mid_symbols = 1 << len_mid_bits,
len_high_symbols = 1 << len_high_bits,
max_len_symbols = len_low_symbols + len_mid_symbols + len_high_symbols,
min_match_len = 2, /* must be 2 */
max_match_len = min_match_len + max_len_symbols - 1, /* 273 */
min_match_len_limit = 5 };
static inline int get_len_state( const int len )
{ return min( len - min_match_len, len_states - 1 ); }
static inline int get_lit_state( const uint8_t prev_byte )
{ return ( prev_byte >> ( 8 - literal_context_bits ) ); }
enum { bit_model_move_bits = 5,
bit_model_total_bits = 11,
bit_model_total = 1 << bit_model_total_bits };
typedef int Bit_model;
static inline void Bm_init( Bit_model * const probability )
{ *probability = bit_model_total / 2; }
static inline void Bm_array_init( Bit_model bm[], const int size )
{ int i; for( i = 0; i < size; ++i ) Bm_init( &bm[i] ); }
struct Len_model
{
Bit_model choice1;
Bit_model choice2;
Bit_model bm_low[pos_states][len_low_symbols];
Bit_model bm_mid[pos_states][len_mid_symbols];
Bit_model bm_high[len_high_symbols];
};
static inline void Lm_init( struct Len_model * const lm )
{
Bm_init( &lm->choice1 );
Bm_init( &lm->choice2 );
Bm_array_init( lm->bm_low[0], pos_states * len_low_symbols );
Bm_array_init( lm->bm_mid[0], pos_states * len_mid_symbols );
Bm_array_init( lm->bm_high, len_high_symbols );
}
/* Table of CRCs of all 8-bit messages. */
static const uint32_t crc32[256] =
{
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106,
0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA,
0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84,
0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E,
0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28,
0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D };
static inline void CRC32_update_byte( uint32_t * const crc, const uint8_t byte )
{ *crc = crc32[(*crc^byte)&0xFF] ^ ( *crc >> 8 ); }
static inline void CRC32_update_buf( uint32_t * const crc,
const uint8_t * const buffer,
const int size )
{
int i;
uint32_t c = *crc;
for( i = 0; i < size; ++i )
c = crc32[(c^buffer[i])&0xFF] ^ ( c >> 8 );
*crc = c;
}
static inline bool isvalid_ds( const unsigned dictionary_size )
{ return ( dictionary_size >= min_dictionary_size &&
dictionary_size <= max_dictionary_size ); }
static inline int real_bits( unsigned value )
{
int bits = 0;
while( value > 0 ) { value >>= 1; ++bits; }
return bits;
}
static const uint8_t magic_string[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */
typedef uint8_t File_header[6]; /* 0-3 magic bytes */
/* 4 version */
/* 5 coded_dict_size */
enum { Fh_size = 6 };
static inline void Fh_set_magic( File_header data )
{ memcpy( data, magic_string, 4 ); data[4] = 1; }
static inline bool Fh_verify_magic( const File_header data )
{ return ( memcmp( data, magic_string, 4 ) == 0 ); }
/* detect (truncated) header */
static inline bool Fh_verify_prefix( const File_header data, const int sz )
{
int i; for( i = 0; i < sz && i < 4; ++i )
if( data[i] != magic_string[i] ) return false;
return ( sz > 0 );
}
/* detect corrupt header */
static inline bool Fh_verify_corrupt( const File_header data )
{
int matches = 0;
int i; for( i = 0; i < 4; ++i )
if( data[i] == magic_string[i] ) ++matches;
return ( matches > 1 && matches < 4 );
}
static inline uint8_t Fh_version( const File_header data )
{ return data[4]; }
static inline bool Fh_verify_version( const File_header data )
{ return ( data[4] == 1 ); }
static inline unsigned Fh_get_dictionary_size( const File_header data )
{
unsigned sz = ( 1 << ( data[5] & 0x1F ) );
if( sz > min_dictionary_size )
sz -= ( sz / 16 ) * ( ( data[5] >> 5 ) & 7 );
return sz;
}
static inline bool Fh_set_dictionary_size( File_header data, const unsigned sz )
{
if( !isvalid_ds( sz ) ) return false;
data[5] = real_bits( sz - 1 );
if( sz > min_dictionary_size )
{
const unsigned base_size = 1 << data[5];
const unsigned fraction = base_size / 16;
unsigned i;
for( i = 7; i >= 1; --i )
if( base_size - ( i * fraction ) >= sz )
{ data[5] |= ( i << 5 ); break; }
}
return true;
}
static inline bool Fh_verify( const File_header data )
{
if( Fh_verify_magic( data ) && Fh_verify_version( data ) )
return isvalid_ds( Fh_get_dictionary_size( data ) );
return false;
}
typedef uint8_t File_trailer[20];
/* 0-3 CRC32 of the uncompressed data */
/* 4-11 size of the uncompressed data */
/* 12-19 member size including header and trailer */
enum { Ft_size = 20 };
static inline unsigned Ft_get_data_crc( const File_trailer data )
{
unsigned tmp = 0;
int i; for( i = 3; i >= 0; --i ) { tmp <<= 8; tmp += data[i]; }
return tmp;
}
static inline void Ft_set_data_crc( File_trailer data, unsigned crc )
{ int i; for( i = 0; i <= 3; ++i ) { data[i] = (uint8_t)crc; crc >>= 8; } }
static inline unsigned long long Ft_get_data_size( const File_trailer data )
{
unsigned long long tmp = 0;
int i; for( i = 11; i >= 4; --i ) { tmp <<= 8; tmp += data[i]; }
return tmp;
}
static inline void Ft_set_data_size( File_trailer data, unsigned long long sz )
{ int i; for( i = 4; i <= 11; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } }
static inline unsigned long long Ft_get_member_size( const File_trailer data )
{
unsigned long long tmp = 0;
int i; for( i = 19; i >= 12; --i ) { tmp <<= 8; tmp += data[i]; }
return tmp;
}
static inline void Ft_set_member_size( File_trailer data, unsigned long long sz )
{ int i; for( i = 12; i <= 19; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } }

595
src/thirdparty/lzlib/lzlib.c vendored Normal file
View File

@@ -0,0 +1,595 @@
/* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "lzlib.h"
#include "lzip.h"
#include "cbuffer.c"
#include "decoder.h"
#include "decoder.c"
#include "encoder_base.h"
#include "encoder_base.c"
#include "encoder.h"
#include "encoder.c"
#include "fast_encoder.h"
#include "fast_encoder.c"
struct LZ_Encoder
{
unsigned long long partial_in_size;
unsigned long long partial_out_size;
struct LZ_encoder_base * lz_encoder_base; /* these 3 pointers make a */
struct LZ_encoder * lz_encoder; /* polymorphic encoder */
struct FLZ_encoder * flz_encoder;
enum LZ_Errno lz_errno;
bool fatal;
};
static void LZ_Encoder_init( struct LZ_Encoder * const e )
{
e->partial_in_size = 0;
e->partial_out_size = 0;
e->lz_encoder_base = 0;
e->lz_encoder = 0;
e->flz_encoder = 0;
e->lz_errno = LZ_ok;
e->fatal = false;
}
struct LZ_Decoder
{
unsigned long long partial_in_size;
unsigned long long partial_out_size;
struct Range_decoder * rdec;
struct LZ_decoder * lz_decoder;
enum LZ_Errno lz_errno;
File_header member_header; /* header of current member */
bool fatal;
bool first_header; /* true until first header is read */
bool seeking;
};
static void LZ_Decoder_init( struct LZ_Decoder * const d )
{
int i;
d->partial_in_size = 0;
d->partial_out_size = 0;
d->rdec = 0;
d->lz_decoder = 0;
d->lz_errno = LZ_ok;
for( i = 0; i < Fh_size; ++i ) d->member_header[i] = 0;
d->fatal = false;
d->first_header = true;
d->seeking = false;
}
static bool verify_encoder( struct LZ_Encoder * const e )
{
if( !e ) return false;
if( !e->lz_encoder_base || ( !e->lz_encoder && !e->flz_encoder ) ||
( e->lz_encoder && e->flz_encoder ) )
{ e->lz_errno = LZ_bad_argument; return false; }
return true;
}
static bool verify_decoder( struct LZ_Decoder * const d )
{
if( !d ) return false;
if( !d->rdec )
{ d->lz_errno = LZ_bad_argument; return false; }
return true;
}
/*------------------------- Misc Functions -------------------------*/
const char * LZ_version( void ) { return LZ_version_string; }
const char * LZ_strerror( const enum LZ_Errno lz_errno )
{
switch( lz_errno )
{
case LZ_ok : return "ok";
case LZ_bad_argument : return "Bad argument";
case LZ_mem_error : return "Not enough memory";
case LZ_sequence_error: return "Sequence error";
case LZ_header_error : return "Header error";
case LZ_unexpected_eof: return "Unexpected eof";
case LZ_data_error : return "Data error";
case LZ_library_error : return "Library error";
}
return "Invalid error code";
}
int LZ_min_dictionary_bits( void ) { return min_dictionary_bits; }
int LZ_min_dictionary_size( void ) { return min_dictionary_size; }
int LZ_max_dictionary_bits( void ) { return max_dictionary_bits; }
int LZ_max_dictionary_size( void ) { return max_dictionary_size; }
int LZ_min_match_len_limit( void ) { return min_match_len_limit; }
int LZ_max_match_len_limit( void ) { return max_match_len; }
/*---------------------- Compression Functions ----------------------*/
struct LZ_Encoder * LZ_compress_open( const int dictionary_size,
const int match_len_limit,
const unsigned long long member_size )
{
File_header header;
struct LZ_Encoder * const e =
(struct LZ_Encoder *)malloc( sizeof (struct LZ_Encoder) );
if( !e ) return 0;
LZ_Encoder_init( e );
if( !Fh_set_dictionary_size( header, dictionary_size ) ||
match_len_limit < min_match_len_limit ||
match_len_limit > max_match_len ||
member_size < min_dictionary_size )
e->lz_errno = LZ_bad_argument;
else
{
if( dictionary_size == 65535 && match_len_limit == 16 )
{
e->flz_encoder = (struct FLZ_encoder *)malloc( sizeof (struct FLZ_encoder) );
if( e->flz_encoder && FLZe_init( e->flz_encoder, member_size ) )
{ e->lz_encoder_base = &e->flz_encoder->eb; return e; }
free( e->flz_encoder ); e->flz_encoder = 0;
}
else
{
e->lz_encoder = (struct LZ_encoder *)malloc( sizeof (struct LZ_encoder) );
if( e->lz_encoder && LZe_init( e->lz_encoder, Fh_get_dictionary_size( header ),
match_len_limit, member_size ) )
{ e->lz_encoder_base = &e->lz_encoder->eb; return e; }
free( e->lz_encoder ); e->lz_encoder = 0;
}
e->lz_errno = LZ_mem_error;
}
e->fatal = true;
return e;
}
int LZ_compress_close( struct LZ_Encoder * const e )
{
if( !e ) return -1;
if( e->lz_encoder_base )
{ LZeb_free( e->lz_encoder_base );
free( e->lz_encoder ); free( e->flz_encoder ); }
free( e );
return 0;
}
int LZ_compress_finish( struct LZ_Encoder * const e )
{
if( !verify_encoder( e ) || e->fatal ) return -1;
Mb_finish( &e->lz_encoder_base->mb );
/* if (open --> write --> finish) use same dictionary size as lzip. */
/* this does not save any memory. */
if( Mb_data_position( &e->lz_encoder_base->mb ) == 0 &&
Re_member_position( &e->lz_encoder_base->renc ) == Fh_size )
{
Mb_adjust_dictionary_size( &e->lz_encoder_base->mb );
Fh_set_dictionary_size( e->lz_encoder_base->renc.header,
e->lz_encoder_base->mb.dictionary_size );
e->lz_encoder_base->renc.cb.buffer[5] = e->lz_encoder_base->renc.header[5];
}
return 0;
}
int LZ_compress_restart_member( struct LZ_Encoder * const e,
const unsigned long long member_size )
{
if( !verify_encoder( e ) || e->fatal ) return -1;
if( !LZeb_member_finished( e->lz_encoder_base ) )
{ e->lz_errno = LZ_sequence_error; return -1; }
if( member_size < min_dictionary_size )
{ e->lz_errno = LZ_bad_argument; return -1; }
e->partial_in_size += Mb_data_position( &e->lz_encoder_base->mb );
e->partial_out_size += Re_member_position( &e->lz_encoder_base->renc );
if( e->lz_encoder ) LZe_reset( e->lz_encoder, member_size );
else FLZe_reset( e->flz_encoder, member_size );
e->lz_errno = LZ_ok;
return 0;
}
int LZ_compress_sync_flush( struct LZ_Encoder * const e )
{
if( !verify_encoder( e ) || e->fatal ) return -1;
if( !Mb_flushing_or_end( &e->lz_encoder_base->mb ) )
e->lz_encoder_base->mb.flushing = true;
return 0;
}
int LZ_compress_read( struct LZ_Encoder * const e,
uint8_t * const buffer, const int size )
{
int out_size = 0;
if( !verify_encoder( e ) || e->fatal ) return -1;
if( size < 0 ) return 0;
do {
if( ( e->flz_encoder && !FLZe_encode_member( e->flz_encoder ) ) ||
( e->lz_encoder && !LZe_encode_member( e->lz_encoder ) ) )
{ e->lz_errno = LZ_library_error; e->fatal = true; return -1; }
if( e->lz_encoder_base->mb.flushing &&
Mb_available_bytes( &e->lz_encoder_base->mb ) <= 0 &&
LZeb_sync_flush( e->lz_encoder_base ) )
e->lz_encoder_base->mb.flushing = false;
out_size += Re_read_data( &e->lz_encoder_base->renc,
buffer + out_size, size - out_size );
}
while( e->lz_encoder_base->mb.flushing && out_size < size &&
Mb_enough_available_bytes( &e->lz_encoder_base->mb ) &&
Re_enough_free_bytes( &e->lz_encoder_base->renc ) );
return out_size;
}
int LZ_compress_write( struct LZ_Encoder * const e,
const uint8_t * const buffer, const int size )
{
if( !verify_encoder( e ) || e->fatal ) return -1;
return Mb_write_data( &e->lz_encoder_base->mb, buffer, size );
}
int LZ_compress_write_size( struct LZ_Encoder * const e )
{
if( !verify_encoder( e ) || e->fatal ) return -1;
return Mb_free_bytes( &e->lz_encoder_base->mb );
}
enum LZ_Errno LZ_compress_errno( struct LZ_Encoder * const e )
{
if( !e ) return LZ_bad_argument;
return e->lz_errno;
}
int LZ_compress_finished( struct LZ_Encoder * const e )
{
if( !verify_encoder( e ) ) return -1;
return ( Mb_data_finished( &e->lz_encoder_base->mb ) &&
LZeb_member_finished( e->lz_encoder_base ) );
}
int LZ_compress_member_finished( struct LZ_Encoder * const e )
{
if( !verify_encoder( e ) ) return -1;
return LZeb_member_finished( e->lz_encoder_base );
}
unsigned long long LZ_compress_data_position( struct LZ_Encoder * const e )
{
if( !verify_encoder( e ) ) return 0;
return Mb_data_position( &e->lz_encoder_base->mb );
}
unsigned long long LZ_compress_member_position( struct LZ_Encoder * const e )
{
if( !verify_encoder( e ) ) return 0;
return Re_member_position( &e->lz_encoder_base->renc );
}
unsigned long long LZ_compress_total_in_size( struct LZ_Encoder * const e )
{
if( !verify_encoder( e ) ) return 0;
return e->partial_in_size + Mb_data_position( &e->lz_encoder_base->mb );
}
unsigned long long LZ_compress_total_out_size( struct LZ_Encoder * const e )
{
if( !verify_encoder( e ) ) return 0;
return e->partial_out_size + Re_member_position( &e->lz_encoder_base->renc );
}
/*--------------------- Decompression Functions ---------------------*/
struct LZ_Decoder * LZ_decompress_open( void )
{
struct LZ_Decoder * const d =
(struct LZ_Decoder *)malloc( sizeof (struct LZ_Decoder) );
if( !d ) return 0;
LZ_Decoder_init( d );
d->rdec = (struct Range_decoder *)malloc( sizeof (struct Range_decoder) );
if( !d->rdec || !Rd_init( d->rdec ) )
{
if( d->rdec ) { Rd_free( d->rdec ); free( d->rdec ); d->rdec = 0; }
d->lz_errno = LZ_mem_error; d->fatal = true;
}
return d;
}
int LZ_decompress_close( struct LZ_Decoder * const d )
{
if( !d ) return -1;
if( d->lz_decoder )
{ LZd_free( d->lz_decoder ); free( d->lz_decoder ); }
if( d->rdec ) { Rd_free( d->rdec ); free( d->rdec ); }
free( d );
return 0;
}
int LZ_decompress_finish( struct LZ_Decoder * const d )
{
if( !verify_decoder( d ) || d->fatal ) return -1;
if( d->seeking )
{ d->seeking = false; d->partial_in_size += Rd_purge( d->rdec ); }
else Rd_finish( d->rdec );
return 0;
}
int LZ_decompress_reset( struct LZ_Decoder * const d )
{
if( !verify_decoder( d ) ) return -1;
if( d->lz_decoder )
{ LZd_free( d->lz_decoder ); free( d->lz_decoder ); d->lz_decoder = 0; }
d->partial_in_size = 0;
d->partial_out_size = 0;
Rd_reset( d->rdec );
d->lz_errno = LZ_ok;
d->fatal = false;
d->first_header = true;
d->seeking = false;
return 0;
}
int LZ_decompress_sync_to_member( struct LZ_Decoder * const d )
{
unsigned skipped = 0;
if( !verify_decoder( d ) ) return -1;
if( d->lz_decoder )
{ LZd_free( d->lz_decoder ); free( d->lz_decoder ); d->lz_decoder = 0; }
if( Rd_find_header( d->rdec, &skipped ) ) d->seeking = false;
else
{
if( !d->rdec->at_stream_end ) d->seeking = true;
else { d->seeking = false; d->partial_in_size += Rd_purge( d->rdec ); }
}
d->partial_in_size += skipped;
d->lz_errno = LZ_ok;
d->fatal = false;
return 0;
}
int LZ_decompress_read( struct LZ_Decoder * const d,
uint8_t * const buffer, const int size )
{
int result;
if( !verify_decoder( d ) || d->fatal ) return -1;
if( d->seeking || size < 0 ) return 0;
if( d->lz_decoder && LZd_member_finished( d->lz_decoder ) )
{
d->partial_out_size += LZd_data_position( d->lz_decoder );
LZd_free( d->lz_decoder ); free( d->lz_decoder ); d->lz_decoder = 0;
}
if( !d->lz_decoder )
{
int rd;
d->partial_in_size += d->rdec->member_position;
d->rdec->member_position = 0;
if( Rd_available_bytes( d->rdec ) < Fh_size + 5 &&
!d->rdec->at_stream_end ) return 0;
if( Rd_finished( d->rdec ) && !d->first_header ) return 0;
rd = Rd_read_data( d->rdec, d->member_header, Fh_size );
if( Rd_finished( d->rdec ) ) /* End Of File */
{
if( rd <= 0 || Fh_verify_prefix( d->member_header, rd ) )
d->lz_errno = LZ_unexpected_eof;
else
d->lz_errno = LZ_header_error;
d->fatal = true;
return -1;
}
if( !Fh_verify_magic( d->member_header ) )
{
/* unreading the header prevents sync_to_member from skipping a member
if leading garbage is shorter than a full header; "lgLZIP\x01\x0C" */
if( Rd_unread_data( d->rdec, rd ) )
{
if( d->first_header || !Fh_verify_corrupt( d->member_header ) )
d->lz_errno = LZ_header_error;
else
d->lz_errno = LZ_data_error; /* corrupt header */
}
else
d->lz_errno = LZ_library_error;
d->fatal = true;
return -1;
}
if( !Fh_verify_version( d->member_header ) ||
!isvalid_ds( Fh_get_dictionary_size( d->member_header ) ) )
{
if( Rd_unread_data( d->rdec, 1 + !Fh_verify_version( d->member_header ) ) )
d->lz_errno = LZ_data_error; /* bad version or bad dict size */
else
d->lz_errno = LZ_library_error;
d->fatal = true;
return -1;
}
d->first_header = false;
if( Rd_available_bytes( d->rdec ) < 5 )
{
/* set position at EOF */
d->rdec->member_position += Cb_used_bytes( &d->rdec->cb );
Cb_reset( &d->rdec->cb );
d->lz_errno = LZ_unexpected_eof;
d->fatal = true;
return -1;
}
d->lz_decoder = (struct LZ_decoder *)malloc( sizeof (struct LZ_decoder) );
if( !d->lz_decoder || !LZd_init( d->lz_decoder, d->rdec,
Fh_get_dictionary_size( d->member_header ) ) )
{ /* not enough free memory */
if( d->lz_decoder )
{ LZd_free( d->lz_decoder ); free( d->lz_decoder ); d->lz_decoder = 0; }
d->lz_errno = LZ_mem_error;
d->fatal = true;
return -1;
}
d->rdec->reload_pending = true;
}
result = LZd_decode_member( d->lz_decoder );
if( result != 0 )
{
if( result == 2 ) /* set position at EOF */
{ d->rdec->member_position += Cb_used_bytes( &d->rdec->cb );
Cb_reset( &d->rdec->cb );
d->lz_errno = LZ_unexpected_eof; }
else if( result == 5 ) d->lz_errno = LZ_library_error;
else d->lz_errno = LZ_data_error;
d->fatal = true;
return -1;
}
return Cb_read_data( &d->lz_decoder->cb, buffer, size );
}
int LZ_decompress_write( struct LZ_Decoder * const d,
const uint8_t * const buffer, const int size )
{
int result;
if( !verify_decoder( d ) || d->fatal ) return -1;
if( size < 0 ) return 0;
result = Rd_write_data( d->rdec, buffer, size );
while( d->seeking )
{
int size2;
unsigned skipped = 0;
if( Rd_find_header( d->rdec, &skipped ) ) d->seeking = false;
d->partial_in_size += skipped;
if( result >= size ) break;
size2 = Rd_write_data( d->rdec, buffer + result, size - result );
if( size2 > 0 ) result += size2;
else break;
}
return result;
}
int LZ_decompress_write_size( struct LZ_Decoder * const d )
{
if( !verify_decoder( d ) || d->fatal ) return -1;
return Rd_free_bytes( d->rdec );
}
enum LZ_Errno LZ_decompress_errno( struct LZ_Decoder * const d )
{
if( !d ) return LZ_bad_argument;
return d->lz_errno;
}
int LZ_decompress_finished( struct LZ_Decoder * const d )
{
if( !verify_decoder( d ) ) return -1;
return ( Rd_finished( d->rdec ) &&
( !d->lz_decoder || LZd_member_finished( d->lz_decoder ) ) );
}
int LZ_decompress_member_finished( struct LZ_Decoder * const d )
{
if( !verify_decoder( d ) ) return -1;
return ( d->lz_decoder && LZd_member_finished( d->lz_decoder ) );
}
int LZ_decompress_member_version( struct LZ_Decoder * const d )
{
if( !verify_decoder( d ) ) return -1;
return Fh_version( d->member_header );
}
int LZ_decompress_dictionary_size( struct LZ_Decoder * const d )
{
if( !verify_decoder( d ) ) return -1;
return Fh_get_dictionary_size( d->member_header );
}
unsigned LZ_decompress_data_crc( struct LZ_Decoder * const d )
{
if( verify_decoder( d ) && d->lz_decoder )
return LZd_crc( d->lz_decoder );
return 0;
}
unsigned long long LZ_decompress_data_position( struct LZ_Decoder * const d )
{
if( verify_decoder( d ) && d->lz_decoder )
return LZd_data_position( d->lz_decoder );
return 0;
}
unsigned long long LZ_decompress_member_position( struct LZ_Decoder * const d )
{
if( !verify_decoder( d ) ) return 0;
return d->rdec->member_position;
}
unsigned long long LZ_decompress_total_in_size( struct LZ_Decoder * const d )
{
if( !verify_decoder( d ) ) return 0;
return d->partial_in_size + d->rdec->member_position;
}
unsigned long long LZ_decompress_total_out_size( struct LZ_Decoder * const d )
{
if( !verify_decoder( d ) ) return 0;
if( d->lz_decoder )
return d->partial_out_size + LZd_data_position( d->lz_decoder );
return d->partial_out_size;
}

106
src/thirdparty/lzlib/lzlib.h vendored Normal file
View File

@@ -0,0 +1,106 @@
/* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2018 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifdef __cplusplus
extern "C" {
#endif
#define LZ_API_VERSION 1
static const char * const LZ_version_string = "1.10";
enum LZ_Errno { LZ_ok = 0, LZ_bad_argument, LZ_mem_error,
LZ_sequence_error, LZ_header_error, LZ_unexpected_eof,
LZ_data_error, LZ_library_error };
const char * LZ_version( void );
const char * LZ_strerror( const enum LZ_Errno lz_errno );
int LZ_min_dictionary_bits( void );
int LZ_min_dictionary_size( void );
int LZ_max_dictionary_bits( void );
int LZ_max_dictionary_size( void );
int LZ_min_match_len_limit( void );
int LZ_max_match_len_limit( void );
/*---------------------- Compression Functions ----------------------*/
struct LZ_Encoder;
struct LZ_Encoder * LZ_compress_open( const int dictionary_size,
const int match_len_limit,
const unsigned long long member_size );
int LZ_compress_close( struct LZ_Encoder * const encoder );
int LZ_compress_finish( struct LZ_Encoder * const encoder );
int LZ_compress_restart_member( struct LZ_Encoder * const encoder,
const unsigned long long member_size );
int LZ_compress_sync_flush( struct LZ_Encoder * const encoder );
int LZ_compress_read( struct LZ_Encoder * const encoder,
uint8_t * const buffer, const int size );
int LZ_compress_write( struct LZ_Encoder * const encoder,
const uint8_t * const buffer, const int size );
int LZ_compress_write_size( struct LZ_Encoder * const encoder );
enum LZ_Errno LZ_compress_errno( struct LZ_Encoder * const encoder );
int LZ_compress_finished( struct LZ_Encoder * const encoder );
int LZ_compress_member_finished( struct LZ_Encoder * const encoder );
unsigned long long LZ_compress_data_position( struct LZ_Encoder * const encoder );
unsigned long long LZ_compress_member_position( struct LZ_Encoder * const encoder );
unsigned long long LZ_compress_total_in_size( struct LZ_Encoder * const encoder );
unsigned long long LZ_compress_total_out_size( struct LZ_Encoder * const encoder );
/*--------------------- Decompression Functions ---------------------*/
struct LZ_Decoder;
struct LZ_Decoder * LZ_decompress_open( void );
int LZ_decompress_close( struct LZ_Decoder * const decoder );
int LZ_decompress_finish( struct LZ_Decoder * const decoder );
int LZ_decompress_reset( struct LZ_Decoder * const decoder );
int LZ_decompress_sync_to_member( struct LZ_Decoder * const decoder );
int LZ_decompress_read( struct LZ_Decoder * const decoder,
uint8_t * const buffer, const int size );
int LZ_decompress_write( struct LZ_Decoder * const decoder,
const uint8_t * const buffer, const int size );
int LZ_decompress_write_size( struct LZ_Decoder * const decoder );
enum LZ_Errno LZ_decompress_errno( struct LZ_Decoder * const decoder );
int LZ_decompress_finished( struct LZ_Decoder * const decoder );
int LZ_decompress_member_finished( struct LZ_Decoder * const decoder );
int LZ_decompress_member_version( struct LZ_Decoder * const decoder );
int LZ_decompress_dictionary_size( struct LZ_Decoder * const decoder );
unsigned LZ_decompress_data_crc( struct LZ_Decoder * const decoder );
unsigned long long LZ_decompress_data_position( struct LZ_Decoder * const decoder );
unsigned long long LZ_decompress_member_position( struct LZ_Decoder * const decoder );
unsigned long long LZ_decompress_total_in_size( struct LZ_Decoder * const decoder );
unsigned long long LZ_decompress_total_out_size( struct LZ_Decoder * const decoder );
#ifdef __cplusplus
}
#endif

View File

@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>326</width>
<height>622</height>
<height>662</height>
</rect>
</property>
<property name="windowTitle">
@@ -21,6 +21,63 @@
</property>
</widget>
</item>
<item>
<widget class="QFrame" name="cloudSyncSettings">
<layout class="QHBoxLayout" name="horizontalLayout_5" stretch="0,0,0,1">
<property name="spacing">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>5</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="cloudSyncCheckbox">
<property name="text">
<string>Cloud Sync</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>5</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="RhdcCloudStatusWidget" name="cloudSyncStatus" native="true"/>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1">
<item>
@@ -514,6 +571,12 @@ color: #ffffff;</string>
<signal>valueChanged()</signal>
</slots>
</customwidget>
<customwidget>
<class>RhdcCloudStatusWidget</class>
<extends>QWidget</extends>
<header>src/rhdc/ui/rhdc-cloud-status-widget.hpp</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>syncSavesCheckbox</tabstop>
@@ -546,7 +609,7 @@ color: #ffffff;</string>
<y>16</y>
</hint>
<hint type="destinationlabel">
<x>484</x>
<x>325</x>
<y>42</y>
</hint>
</hints>
@@ -558,11 +621,11 @@ color: #ffffff;</string>
<slot>pluginChanged()</slot>
<hints>
<hint type="sourcelabel">
<x>249</x>
<y>51</y>
<x>318</x>
<y>126</y>
</hint>
<hint type="destinationlabel">
<x>485</x>
<x>325</x>
<y>80</y>
</hint>
</hints>
@@ -574,11 +637,11 @@ color: #ffffff;</string>
<slot>overclockCpuToggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>89</x>
<y>98</y>
<x>95</x>
<y>172</y>
</hint>
<hint type="destinationlabel">
<x>486</x>
<x>325</x>
<y>140</y>
</hint>
</hints>
@@ -590,11 +653,11 @@ color: #ffffff;</string>
<slot>overclockViToggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>289</x>
<y>90</y>
<x>295</x>
<y>172</y>
</hint>
<hint type="destinationlabel">
<x>486</x>
<x>325</x>
<y>174</y>
</hint>
</hints>
@@ -606,11 +669,11 @@ color: #ffffff;</string>
<slot>emulateFramebufferToggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>137</x>
<y>123</y>
<x>143</x>
<y>202</y>
</hint>
<hint type="destinationlabel">
<x>485</x>
<x>325</x>
<y>213</y>
</hint>
</hints>
@@ -622,11 +685,11 @@ color: #ffffff;</string>
<slot>accurateDepthCompareToggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>254</x>
<y>116</y>
<x>260</x>
<y>192</y>
</hint>
<hint type="destinationlabel">
<x>486</x>
<x>325</x>
<y>249</y>
</hint>
</hints>
@@ -638,11 +701,11 @@ color: #ffffff;</string>
<slot>upscaleTexRectsToggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>263</x>
<y>141</y>
<x>269</x>
<y>212</y>
</hint>
<hint type="destinationlabel">
<x>486</x>
<x>325</x>
<y>286</y>
</hint>
</hints>
@@ -654,11 +717,11 @@ color: #ffffff;</string>
<slot>removeBordersToggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>419</x>
<y>95</y>
<x>425</x>
<y>172</y>
</hint>
<hint type="destinationlabel">
<x>486</x>
<x>325</x>
<y>324</y>
</hint>
</hints>
@@ -670,11 +733,11 @@ color: #ffffff;</string>
<slot>trueWidescreenToggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>388</x>
<y>134</y>
<x>394</x>
<y>212</y>
</hint>
<hint type="destinationlabel">
<x>485</x>
<x>325</x>
<y>323</y>
</hint>
</hints>
@@ -686,11 +749,11 @@ color: #ffffff;</string>
<slot>advancedOptionsToggled()</slot>
<hints>
<hint type="sourcelabel">
<x>107</x>
<y>343</y>
<x>114</x>
<y>548</y>
</hint>
<hint type="destinationlabel">
<x>484</x>
<x>325</x>
<y>554</y>
</hint>
</hints>
@@ -702,11 +765,11 @@ color: #ffffff;</string>
<slot>inputModeSelectionChanged()</slot>
<hints>
<hint type="sourcelabel">
<x>237</x>
<y>411</y>
<x>198</x>
<y>594</y>
</hint>
<hint type="destinationlabel">
<x>486</x>
<x>325</x>
<y>613</y>
</hint>
</hints>
@@ -718,12 +781,12 @@ color: #ffffff;</string>
<slot>sdCardChanged(int)</slot>
<hints>
<hint type="sourcelabel">
<x>202</x>
<y>532</y>
<x>268</x>
<y>633</y>
</hint>
<hint type="destinationlabel">
<x>486</x>
<y>680</y>
<x>325</x>
<y>661</y>
</hint>
</hints>
</connection>
@@ -734,11 +797,11 @@ color: #ffffff;</string>
<slot>manageSdCards()</slot>
<hints>
<hint type="sourcelabel">
<x>366</x>
<y>522</y>
<x>305</x>
<y>633</y>
</hint>
<hint type="destinationlabel">
<x>485</x>
<x>325</x>
<y>661</y>
</hint>
</hints>
@@ -750,11 +813,11 @@ color: #ffffff;</string>
<slot>widescreenToggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>74</x>
<y>615</y>
<x>86</x>
<y>671</y>
</hint>
<hint type="destinationlabel">
<x>485</x>
<x>325</x>
<y>347</y>
</hint>
</hints>
@@ -766,11 +829,11 @@ color: #ffffff;</string>
<slot>useInterpreterToggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>410</x>
<y>609</y>
<x>422</x>
<y>671</y>
</hint>
<hint type="destinationlabel">
<x>482</x>
<x>325</x>
<y>395</y>
</hint>
</hints>
@@ -782,11 +845,11 @@ color: #ffffff;</string>
<slot>bigEepromToggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>209</x>
<y>618</y>
<x>221</x>
<y>671</y>
</hint>
<hint type="destinationlabel">
<x>481</x>
<x>325</x>
<y>450</y>
</hint>
</hints>
@@ -798,15 +861,31 @@ color: #ffffff;</string>
<slot>rspEmulationToggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>72</x>
<y>653</y>
<x>84</x>
<y>711</y>
</hint>
<hint type="destinationlabel">
<x>483</x>
<x>325</x>
<y>498</y>
</hint>
</hints>
</connection>
<connection>
<sender>cloudSyncCheckbox</sender>
<signal>toggled(bool)</signal>
<receiver>RomSettingsWidget</receiver>
<slot>cloudSyncToggled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>85</x>
<y>54</y>
</hint>
<hint type="destinationlabel">
<x>328</x>
<y>26</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>saveSyncingToggled(bool)</slot>
@@ -826,5 +905,6 @@ color: #ffffff;</string>
<slot>useInterpreterToggled(bool)</slot>
<slot>bigEepromToggled(bool)</slot>
<slot>rspEmulationToggled(bool)</slot>
<slot>cloudSyncToggled(bool)</slot>
</slots>
</ui>

View File

@@ -1309,8 +1309,24 @@
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="cloudSaveCheckbox">
<property name="toolTip">
<string>If checked, your save files will be backed up on romhacking.com, and will be kept up to date. You must verify your e-mail on your RHDC account to use this feature.</string>
</property>
<property name="text">
<string>Enable Cloud Saves</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="enableStarDisplayCheckbox">
<property name="toolTip">
<string>If checked, when a rom is running that has a supported layout file from RHDC, a layout of all the stars in the hack will be shown. This layout updates automatically after the game is saved.</string>
</property>
<property name="text">
<string>Enable Star Display</string>
</property>

View File

@@ -32,6 +32,11 @@ static inline QIcon getFallbackIcon( const char *filename ) {
return s_icon; \
}
#define LOAD_FIXED_ICON( varName, iconName ) const QIcon &Icon::varName() { \
static const QIcon s_icon = getFallbackIcon( iconName ".svg" ); \
return s_icon; \
}
const QIcon &Icon::appIcon() {
static const QIcon s_icon = QIcon( ":/appicon.svg" );
return s_icon;
@@ -85,5 +90,13 @@ LOAD_SYMBOL_ICON( ratingLovedFilled, "rating-loved-filled" )
LOAD_SYMBOL_ICON( rhdcLight, "rhdc-logo-light" )
LOAD_SYMBOL_ICON( rhdcDark, "rhdc-logo-dark" )
LOAD_SYMBOL_ICON( rhdcLink, "rhdc-link" )
LOAD_FIXED_ICON( cloud, "folder-cloud" )
LOAD_FIXED_ICON( cloudSynced, "state-ok" )
LOAD_FIXED_ICON( cloudSyncing, "state-sync" )
LOAD_FIXED_ICON( cloudDesynced, "state-warning" )
LOAD_FIXED_ICON( cloudOffline, "state-offline" )
LOAD_FIXED_ICON( cloudError, "state-error" )
LOAD_FIXED_ICON( resolveDesync, "vcs-merge" )
LOAD_FIXED_ICON( computer, "computer" )
#undef LOAD_ICON

View File

@@ -53,6 +53,14 @@ namespace Icon {
extern const QIcon &ratingNeutralFilled();
extern const QIcon &ratingLikedFilled();
extern const QIcon &ratingLovedFilled();
extern const QIcon &cloud();
extern const QIcon &cloudSynced();
extern const QIcon &cloudSyncing();
extern const QIcon &cloudDesynced();
extern const QIcon &cloudOffline();
extern const QIcon &cloudError();
extern const QIcon &resolveDesync();
extern const QIcon &computer();
}
#endif /* SRC_UI_ICONS_HPP_ */

View File

@@ -8,7 +8,6 @@
#include <QRegularExpression>
#include "src/core/file-controller.hpp"
#include "src/ui/icons.hpp"
#include "src/ui/util.hpp"
IsViewerWindow::IsViewerWindow() : QMainWindow(), m_escapeBuffer(), m_oddLine(false), m_jp(false) {
const UiState uiState = FileController::loadUiState();

View File

@@ -23,6 +23,7 @@
#include "src/ui/toast.hpp"
#include "src/ui/play.hpp"
#include "src/ui/util.hpp"
#include "src/polyfill/filetime.hpp"
#include "src/polyfill/file-dialog.hpp"
#include "src/polyfill/file-explorer.hpp"
#include "src/polyfill/base-directory.hpp"
@@ -325,7 +326,8 @@ void MainWindow::romSelectionChanged() {
!Flags::has( hints.hackFlags, RhdcHackFlag::NoOverclock ),
romInfo.inputTypes,
hackId.empty() ? std::nullopt : std::optional<bool>( DataProvider::groupSaveEnabled( hackId ) ),
(Flags::has( hints.hackFlags, RhdcHackFlag::SupportsSD ) && romInfo.sdCard.empty()) ? "?"s : romInfo.sdCard
(Flags::has( hints.hackFlags, RhdcHackFlag::SupportsSD ) && romInfo.sdCard.empty()) ? "?"s : romInfo.sdCard,
hackId
);
const bool isDownloaded = DataProvider::romWithSha1Exists( sha1 );
@@ -368,7 +370,8 @@ void MainWindow::romSelectionChanged() {
!Flags::has( hints.hackFlags, RhdcHackFlag::NoOverclock ),
rom.info->inputTypes,
(rom.rhdc != nullptr) ? std::optional<bool>( DataProvider::groupSaveEnabled( rom.rhdc->info.hackId ) ) : std::nullopt,
(Flags::has( hints.hackFlags, RhdcHackFlag::SupportsSD ) && rom.info->sdCard.empty()) ? "?"s : rom.info->sdCard
(Flags::has( hints.hackFlags, RhdcHackFlag::SupportsSD ) && rom.info->sdCard.empty()) ? "?"s : rom.info->sdCard,
(rom.rhdc != nullptr) ? rom.rhdc->info.hackId : ""s
);
}
@@ -660,7 +663,7 @@ void MainWindow::addRom() {
DataProvider::addRomFile( RomFile{
romPath,
RomUtil::getLastModified( romPath ),
Time::getLastModifiedSec( romPath ),
sha1,
true
});

View File

@@ -8,6 +8,7 @@
#include <QCheckBox>
#include <QFile>
#include "src/polyfill/base-directory.hpp"
#include "src/polyfill/filetime.hpp"
#include "src/polyfill/file.hpp"
#include "src/polyfill/sha1.hpp"
#include "src/polyfill/env.hpp"
@@ -32,7 +33,9 @@
#include "src/core/libpl.hpp"
#include "src/core/uuid.hpp"
#include "src/db/data-provider.hpp"
#include "src/rhdc/ui/rhdc-cloud-desync-dialog.hpp"
#include "src/rhdc/ui/rhdc-save-sync-dialog.hpp"
#include "src/rhdc/core/cloud-saves.hpp"
#include "src/rhdc/core/layout.hpp"
#include "src/rhdc/web/api.hpp"
@@ -125,7 +128,8 @@ static bool playGame(
const std::function<void(void)> &callback,
const StarLayout *testLayout,
bool waitForRhdcSync,
bool inputBindingError
bool inputBindingError,
bool skipInitialCloudSync
) {
string groupId;
bool syncSaves = false;
@@ -134,6 +138,33 @@ static bool playGame(
DataProvider::tryFetchHackIdByChecksum( romInfo.sha1, groupId ) &&
DataProvider::groupSaveEnabled( groupId )
) {
if( !skipInitialCloudSync && RhdcCloudSave::isEnabled( groupId ) ) {
switch( RhdcCloudSave::getInfo( groupId ).status ) {
case CloudSaveStatus::Desynced:
RhdcCloudDesyncDialog::run( groupId );
break;
case CloudSaveStatus::Synced:
case CloudSaveStatus::NoData:
break;
default: {
//TODO: show splash screen?
bool doneSyncing = false;
RhdcCloudSave::synchronizeSaveFile(
groupId,
[&](CloudSaveStatus syncStatus) {
if( syncStatus == CloudSaveStatus::Desynced ) {
RhdcCloudDesyncDialog::run( groupId );
}
doneSyncing = true;
}
);
while( !doneSyncing ) QCoreApplication::processEvents( QEventLoop::WaitForMoreEvents | QEventLoop::ExcludeUserInputEvents );
break;
}
}
}
string storedGroupId;
if( DataProvider::tryGetSaveGroup( romInfo.sha1, storedGroupId ) && groupId == storedGroupId ) {
syncSaves = true;
@@ -181,7 +212,7 @@ static bool playGame(
saveFileInfos.push_back({
saveFile,
versionSource,
RomUtil::getLastModified( saveFile ),
Time::getLastModifiedSec( saveFile ),
getStarCountFromSaveFile( saveFile, groupId )
});
}
@@ -363,9 +394,10 @@ static bool playGame(
}
const bool hackHasStars = hack.info.starCount > 0;
ushort saveFileStarCount = 0;
if( hackHasStars ) {
starProgress = getStarCount( romFile, hack.info.hackId );
starProgress = starProgress > hack.info.starCount ? hack.info.starCount : starProgress;
saveFileStarCount = getStarCount( romFile, hack.info.hackId );
starProgress = saveFileStarCount > hack.info.starCount ? hack.info.starCount : saveFileStarCount;
DataProvider::updateRhdcStarProgress( hackId, starProgress, starProgress == hack.info.starCount );
if( newPlayTime >= 300000 || starProgress > 0 ) {
@@ -379,6 +411,10 @@ static bool playGame(
}
}
if( RhdcCloudSave::isEnabled( hackId ) ) {
RhdcCloudSave::synchronizeSaveFile( hackId, [](CloudSaveStatus){} );
}
std::shared_ptr<bool> rhdcPlayTimeUpdated( new bool( false ) );
std::shared_ptr<bool> rhdcProgressUpdated( new bool( false ) );
@@ -454,7 +490,7 @@ static bool playGame(
if( DataProvider::tryFetchRomByPath( romFile.path, true, &newRomInfo, &newRomFile ) ) {
fs::error_code err;
fs::remove( validationFile, err );
playGame( newRomFile, newRomInfo, players, bindSavestate, callback, testLayout, waitForRhdcSync, inputBindingError );
playGame( newRomFile, newRomInfo, players, bindSavestate, callback, testLayout, waitForRhdcSync, inputBindingError, true );
return;
}
}
@@ -660,7 +696,7 @@ bool Game::play(
players.push_back({ DefaultProfile::get( inputDriver ), Uuid() });
}
return playGame( romFile, romInfo, players, bindSavestate, callback, testLayout, waitForRhdcSync, inputBindingError );
return playGame( romFile, romInfo, players, bindSavestate, callback, testLayout, waitForRhdcSync, inputBindingError, false );
}
static inline bool hasBpsExtension( const fs::path &path ) {
@@ -729,7 +765,7 @@ bool Game::tryLoadRom(
romFile = RomFile{
romPath,
RomUtil::getLastModified( romPath ),
Time::getLastModifiedSec( romPath ),
Sha1::compute( romPath ),
true
};

View File

@@ -10,6 +10,8 @@
#include "src/ui/flow-layout.hpp"
#include "src/ui/icons.hpp"
#include "src/input/keyboard.hpp"
#include "src/db/data-provider.hpp"
#include "src/rhdc/core/cloud-saves.hpp"
RomSettingsWidget::RomSettingsWidget( QWidget *parent ) :
QWidget( parent ),
@@ -79,7 +81,8 @@ RomSettingsWidget::RomSettingsWidget( QWidget *parent ) :
true,
{ N64InputType::Unknown, N64InputType::Unknown, N64InputType::Unknown, N64InputType::Unknown },
std::nullopt,
""
""s,
""s
);
updateWarningVisibility();
@@ -132,10 +135,12 @@ void RomSettingsWidget::setSettings(
bool suggestOverclock,
const N64InputTypes &inputTypes,
const std::optional<bool> &syncSaves,
const std::string &sdCard
const std::string &sdCard,
const std::string &hackId
) {
m_accurateDepthCompare = accurateDepthCompare;
m_suggestOverclock = suggestOverclock;
m_hackId = hackId;
if( gfxPlugin == GfxPlugin::UseDefault ) {
gfxPlugin = FileController::loadAppSettings().defaultGraphicsPlugin;
@@ -147,14 +152,29 @@ void RomSettingsWidget::setSettings(
suggestOverclock ? tr( "Overclock CPU (Recommended)" ) : tr( "Overclock CPU" )
);
m_suppressEvents = true;
if( syncSaves.has_value() ) {
const bool showCloudSaves = !hackId.empty() && RhdcCloudSave::areEnabled() && DataProvider::isRhdcHackDownloaded( hackId );
const bool enableCloudSaves = showCloudSaves && syncSaves.value() && DataProvider::cloudSaveEnabled( hackId );
m_ui->syncSavesCheckbox->setVisible( true );
m_ui->cloudSyncSettings->setVisible( showCloudSaves );
m_ui->syncSavesCheckbox->setChecked( syncSaves.value() );
m_ui->cloudSyncSettings->setEnabled( syncSaves.value() );
m_ui->cloudSyncCheckbox->setChecked( enableCloudSaves );
m_ui->cloudSyncStatus->setVisible( enableCloudSaves );
if( showCloudSaves ) {
m_ui->cloudSyncStatus->setHackId( hackId );
}
if( enableCloudSaves && RhdcCloudSave::getInfo( hackId ).status == CloudSaveStatus::Unknown ) {
RhdcCloudSave::synchronizeSaveFile( hackId, [](CloudSaveStatus){} );
}
} else {
m_ui->syncSavesCheckbox->setVisible( false );
m_ui->cloudSyncSettings->setVisible( false );
}
m_suppressEvents = true;
if( hasSpecialInputType( inputTypes ) ) {
m_ui->inputModeSelect->setEnabled( false );
m_ui->inputModeSelect->setSelected( DefaultInputModes::Normal.id );
@@ -404,6 +424,30 @@ void RomSettingsWidget::pluginChanged() {
void RomSettingsWidget::saveSyncingToggled( bool checked ) {
if( m_suppressEvents || !m_ui->syncSavesCheckbox->isVisible() ) return;
emit saveSyncingChanged( checked );
m_suppressEvents = true;
if( !m_hackId.empty() && RhdcCloudSave::areEnabled() ) {
m_ui->cloudSyncSettings->setEnabled( true );
const bool enableCloudSaves = checked && DataProvider::cloudSaveEnabled( m_hackId );
m_ui->cloudSyncCheckbox->setChecked( enableCloudSaves );
m_ui->cloudSyncStatus->setVisible( enableCloudSaves );
if( enableCloudSaves ) RhdcCloudSave::synchronizeSaveFile( m_hackId, [](CloudSaveStatus){} );
} else {
m_ui->cloudSyncSettings->setEnabled( false );
}
m_suppressEvents = false;
}
void RomSettingsWidget::cloudSyncToggled( bool checked ) {
if( m_suppressEvents || !m_ui->cloudSyncCheckbox->isVisible() || m_hackId.empty() ) return;
m_ui->cloudSyncStatus->setVisible( checked );
if( checked ) {
DataProvider::enableCloudSave( m_hackId );
RhdcCloudSave::synchronizeSaveFile( m_hackId, [](CloudSaveStatus){} );
} else {
DataProvider::disableCloudSave( m_hackId );
}
}
void RomSettingsWidget::advancedOptionsToggled() {

View File

@@ -22,6 +22,7 @@ class RomSettingsWidget : public QWidget {
bool m_supportsMouse;
bool m_showAdvancedOptions;
std::vector<SdCardInfo> m_sdCards;
std::string m_hackId;
public:
explicit RomSettingsWidget( QWidget *parent = nullptr );
@@ -45,7 +46,8 @@ class RomSettingsWidget : public QWidget {
bool suggestOverclock,
const N64InputTypes &inputTypes,
const std::optional<bool> &syncSaves,
const std::string &sdCard
const std::string &sdCard,
const std::string &hackId
);
const Uuid &getInputMode() const;
@@ -88,6 +90,7 @@ class RomSettingsWidget : public QWidget {
void accurateDepthCompareToggled( bool checked );
void rspEmulationToggled( bool checked );
void saveSyncingToggled( bool checked );
void cloudSyncToggled( bool checked );
void pluginChanged();
void advancedOptionsToggled();
void sdCardChanged( int index );

View File

@@ -201,6 +201,7 @@ void SettingsDialog::showEvent( QShowEvent *event ) {
m_ui->enableDefaultLayoutsCheckbox->setChecked( rhdcSettings.enableDefaultLayouts );
m_ui->starDisplaySubsection->setEnabled( rhdcSettings.enableStarDisplay );
m_ui->ignoreWidescreenCheckbox->setChecked( rhdcSettings.ignoreWidescreenHint );
m_ui->cloudSaveCheckbox->setChecked( rhdcSettings.enableCloudSaves );
#ifdef __APPLE__
m_ui->glidenAntiAliasingCheckbox->setChecked( false );
@@ -315,6 +316,7 @@ void SettingsDialog::save() {
rhdcSettings.preferHle = m_ui->preferHleCheckbox->isChecked();
rhdcSettings.enableDefaultLayouts = m_ui->enableDefaultLayoutsCheckbox->isChecked();
rhdcSettings.ignoreWidescreenHint = m_ui->ignoreWidescreenCheckbox->isChecked();
rhdcSettings.enableCloudSaves = m_ui->cloudSaveCheckbox->isChecked();
#if defined(__linux__) && defined(_LP64) && defined(DISCORD_LAZY_LINKING)
if( settings.discordIntegration && !Discord::pluginInstalled() ) {