├── .gitignore ├── CHANGELOG.md ├── README.md ├── bin ├── crunch.exe ├── crunch.pdb ├── crunch_x64.exe └── crunch_x64.pdb ├── crn.2008.sln ├── crn.workspace ├── crn_examples.2008.sln ├── crn_linux.workspace ├── crnlib ├── Makefile ├── crn_arealist.cpp ├── crn_arealist.h ├── crn_assert.cpp ├── crn_assert.h ├── crn_atomics.h ├── crn_buffer_stream.h ├── crn_cfile_stream.h ├── crn_checksum.cpp ├── crn_checksum.h ├── crn_clusterizer.h ├── crn_color.h ├── crn_colorized_console.cpp ├── crn_colorized_console.h ├── crn_command_line_params.cpp ├── crn_command_line_params.h ├── crn_comp.cpp ├── crn_comp.h ├── crn_console.cpp ├── crn_console.h ├── crn_core.cpp ├── crn_core.h ├── crn_data_stream.cpp ├── crn_data_stream.h ├── crn_data_stream_serializer.h ├── crn_dds_comp.cpp ├── crn_dds_comp.h ├── crn_decomp.cpp ├── crn_dxt.cpp ├── crn_dxt.h ├── crn_dxt1.cpp ├── crn_dxt1.h ├── crn_dxt5a.cpp ├── crn_dxt5a.h ├── crn_dxt_endpoint_refiner.cpp ├── crn_dxt_endpoint_refiner.h ├── crn_dxt_fast.cpp ├── crn_dxt_fast.h ├── crn_dxt_hc.cpp ├── crn_dxt_hc.h ├── crn_dxt_hc_common.cpp ├── crn_dxt_hc_common.h ├── crn_dxt_image.cpp ├── crn_dxt_image.h ├── crn_dynamic_stream.h ├── crn_dynamic_string.cpp ├── crn_dynamic_string.h ├── crn_etc.cpp ├── crn_etc.h ├── crn_file_utils.cpp ├── crn_file_utils.h ├── crn_find_files.cpp ├── crn_find_files.h ├── crn_freeimage_image_utils.h ├── crn_hash.cpp ├── crn_hash.h ├── crn_hash_map.cpp ├── crn_hash_map.h ├── crn_helpers.h ├── crn_huffman_codes.cpp ├── crn_huffman_codes.h ├── crn_image.h ├── crn_image_utils.cpp ├── crn_image_utils.h ├── crn_intersect.h ├── crn_jpgd.cpp ├── crn_jpgd.h ├── crn_jpge.cpp ├── crn_jpge.h ├── crn_ktx_texture.cpp ├── crn_ktx_texture.h ├── crn_lzma_codec.cpp ├── crn_lzma_codec.h ├── crn_math.cpp ├── crn_math.h ├── crn_matrix.h ├── crn_mem.cpp ├── crn_mem.h ├── crn_miniz.cpp ├── crn_miniz.h ├── crn_mipmapped_texture.cpp ├── crn_mipmapped_texture.h ├── crn_packed_uint.h ├── crn_pixel_format.cpp ├── crn_pixel_format.h ├── crn_platform.cpp ├── crn_platform.h ├── crn_prefix_coding.cpp ├── crn_prefix_coding.h ├── crn_qdxt1.cpp ├── crn_qdxt1.h ├── crn_qdxt5.cpp ├── crn_qdxt5.h ├── crn_radix_sort.h ├── crn_rand.cpp ├── crn_rand.h ├── crn_ray.h ├── crn_rect.h ├── crn_resample_filters.cpp ├── crn_resample_filters.h ├── crn_resampler.cpp ├── crn_resampler.h ├── crn_rg_etc1.cpp ├── crn_rg_etc1.h ├── crn_ryg_dxt.cpp ├── crn_ryg_dxt.hpp ├── crn_ryg_types.hpp ├── crn_sparse_array.h ├── crn_sparse_bit_array.cpp ├── crn_sparse_bit_array.h ├── crn_stb_image.cpp ├── crn_strutils.cpp ├── crn_strutils.h ├── crn_symbol_codec.cpp ├── crn_symbol_codec.h ├── crn_texture_comp.cpp ├── crn_texture_comp.h ├── crn_texture_conversion.cpp ├── crn_texture_conversion.h ├── crn_texture_file_types.cpp ├── crn_texture_file_types.h ├── crn_threaded_clusterizer.h ├── crn_threaded_resampler.cpp ├── crn_threaded_resampler.h ├── crn_threading.h ├── crn_threading_null.h ├── crn_threading_pthreads.cpp ├── crn_threading_pthreads.h ├── crn_threading_win32.cpp ├── crn_threading_win32.h ├── crn_timer.cpp ├── crn_timer.h ├── crn_traits.h ├── crn_tree_clusterizer.h ├── crn_types.h ├── crn_utils.cpp ├── crn_utils.h ├── crn_value.cpp ├── crn_value.h ├── crn_vec.h ├── crn_vec_interval.h ├── crn_vector.cpp ├── crn_vector.h ├── crn_vector2d.h ├── crn_winhdr.h ├── crn_zeng.cpp ├── crn_zeng.h ├── crnlib.2008.vcproj ├── crnlib.cbp ├── crnlib.cpp ├── crnlib_linux.cbp ├── lzham_timer.cpp ├── lzham_timer.h ├── lzham_win32_threading.cpp ├── lzham_win32_threading.h ├── lzma_7zBuf.cpp ├── lzma_7zBuf.h ├── lzma_7zBuf2.cpp ├── lzma_7zCrc.cpp ├── lzma_7zCrc.h ├── lzma_7zFile.cpp ├── lzma_7zFile.h ├── lzma_7zStream.cpp ├── lzma_7zVersion.h ├── lzma_Alloc.cpp ├── lzma_Alloc.h ├── lzma_Bcj2.cpp ├── lzma_Bcj2.h ├── lzma_Bra.cpp ├── lzma_Bra.h ├── lzma_Bra86.cpp ├── lzma_BraIA64.cpp ├── lzma_CpuArch.h ├── lzma_LzFind.cpp ├── lzma_LzFind.h ├── lzma_LzFindMt.cpp ├── lzma_LzFindMt.h ├── lzma_LzHash.h ├── lzma_LzmaDec.cpp ├── lzma_LzmaDec.h ├── lzma_LzmaEnc.cpp ├── lzma_LzmaEnc.h ├── lzma_LzmaLib.cpp ├── lzma_LzmaLib.h ├── lzma_MyVersion.h ├── lzma_Threads.cpp ├── lzma_Threads.h └── lzma_Types.h ├── crunch ├── corpus_gen.cpp ├── corpus_gen.h ├── corpus_test.cpp ├── corpus_test.h ├── crunch.2008.vcproj ├── crunch.cbp ├── crunch.cpp └── crunch_linux.cbp ├── emscripten └── crunch_lib.cpp ├── example1 ├── example1.2008.vcproj ├── example1.cpp └── stb_image.h ├── example2 ├── example2.2008.vcproj ├── example2.cpp ├── timer.cpp └── timer.h ├── example3 ├── example3.2008.vcproj ├── example3.cpp └── stb_image.h ├── inc ├── crn_decomp.h ├── crnlib.h └── dds_defs.h └── license.txt /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | crnlib/crunch 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## [0.1.4] - 2012-11-24 4 | ### Added 5 | * KTX file format 6 | * Basic ETC1 support 7 | * Simple makefile 8 | 9 | ### Fixed 10 | * Various DDS format fixes 11 | 12 | ## [0.1.3] - 2012-04-26 13 | ### Added 14 | * Ported to Linux (tested on Ubuntu x86 w/Codeblocks). Note that a few features of the cmd line tool don't work yet (eg. -timestamp) 15 | 16 | [0.1.4]: https://github.com/BinomialLLC/crunch 17 | [0.1.3]: https://github.com/BinomialLLC/crunch -------------------------------------------------------------------------------- /bin/crunch.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unity-Technologies/crunch/ea9b8d8c00c8329791256adafa8cf11e4e7942a2/bin/crunch.exe -------------------------------------------------------------------------------- /bin/crunch.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unity-Technologies/crunch/ea9b8d8c00c8329791256adafa8cf11e4e7942a2/bin/crunch.pdb -------------------------------------------------------------------------------- /bin/crunch_x64.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unity-Technologies/crunch/ea9b8d8c00c8329791256adafa8cf11e4e7942a2/bin/crunch_x64.exe -------------------------------------------------------------------------------- /bin/crunch_x64.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unity-Technologies/crunch/ea9b8d8c00c8329791256adafa8cf11e4e7942a2/bin/crunch_x64.pdb -------------------------------------------------------------------------------- /crn.2008.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 10.00 3 | # Visual Studio 2008 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "crunch", "crunch\crunch.2008.vcproj", "{8F645BA1-B996-49EB-859B-970A671DE05D}" 5 | ProjectSection(ProjectDependencies) = postProject 6 | {CF2E70E8-7133-4D96-92C7-68BB406C0664} = {CF2E70E8-7133-4D96-92C7-68BB406C0664} 7 | EndProjectSection 8 | EndProject 9 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "crnlib", "crnlib\crnlib.2008.vcproj", "{CF2E70E8-7133-4D96-92C7-68BB406C0664}" 10 | EndProject 11 | Global 12 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 13 | Debug_DLL|Win32 = Debug_DLL|Win32 14 | Debug_DLL|x64 = Debug_DLL|x64 15 | Debug|Win32 = Debug|Win32 16 | Debug|x64 = Debug|x64 17 | Release_DLL|Win32 = Release_DLL|Win32 18 | Release_DLL|x64 = Release_DLL|x64 19 | Release|Win32 = Release|Win32 20 | Release|x64 = Release|x64 21 | EndGlobalSection 22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 23 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Debug_DLL|Win32.ActiveCfg = Debug|Win32 24 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Debug_DLL|x64.ActiveCfg = Debug|x64 25 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Debug|Win32.ActiveCfg = Debug|Win32 26 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Debug|Win32.Build.0 = Debug|Win32 27 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Debug|x64.ActiveCfg = Debug|x64 28 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Debug|x64.Build.0 = Debug|x64 29 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Release_DLL|Win32.ActiveCfg = Release|Win32 30 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Release_DLL|x64.ActiveCfg = Release|x64 31 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Release|Win32.ActiveCfg = Release|Win32 32 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Release|Win32.Build.0 = Release|Win32 33 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Release|x64.ActiveCfg = Release|x64 34 | {8F645BA1-B996-49EB-859B-970A671DE05D}.Release|x64.Build.0 = Release|x64 35 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug_DLL|Win32.ActiveCfg = Debug_DLL|Win32 36 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug_DLL|Win32.Build.0 = Debug_DLL|Win32 37 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug_DLL|x64.ActiveCfg = Debug_DLL|x64 38 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug_DLL|x64.Build.0 = Debug_DLL|x64 39 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug|Win32.ActiveCfg = Debug|Win32 40 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug|Win32.Build.0 = Debug|Win32 41 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug|x64.ActiveCfg = Debug|x64 42 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Debug|x64.Build.0 = Debug|x64 43 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release_DLL|Win32.ActiveCfg = Release_DLL|Win32 44 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release_DLL|Win32.Build.0 = Release_DLL|Win32 45 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release_DLL|x64.ActiveCfg = Release_DLL|x64 46 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release_DLL|x64.Build.0 = Release_DLL|x64 47 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release|Win32.ActiveCfg = Release|Win32 48 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release|Win32.Build.0 = Release|Win32 49 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release|x64.ActiveCfg = Release|x64 50 | {CF2E70E8-7133-4D96-92C7-68BB406C0664}.Release|x64.Build.0 = Release|x64 51 | EndGlobalSection 52 | GlobalSection(SolutionProperties) = preSolution 53 | HideSolutionNode = FALSE 54 | EndGlobalSection 55 | EndGlobal 56 | -------------------------------------------------------------------------------- /crn.workspace: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /crn_examples.2008.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 10.00 3 | # Visual Studio 2008 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example1", "example1\example1.2008.vcproj", "{8F745B42-F996-49EB-859B-970A671DE05D}" 5 | EndProject 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example2", "example2\example2.2008.vcproj", "{AF745B42-F996-49EB-859B-970A671DEF5E}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example3", "example3\example3.2008.vcproj", "{AF745B42-E296-46EB-859B-970A671DEF5E}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug_DLL|Win32 = Debug_DLL|Win32 13 | Debug_DLL|x64 = Debug_DLL|x64 14 | Debug|Win32 = Debug|Win32 15 | Debug|x64 = Debug|x64 16 | Release_DLL|Win32 = Release_DLL|Win32 17 | Release_DLL|x64 = Release_DLL|x64 18 | Release|Win32 = Release|Win32 19 | Release|x64 = Release|x64 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {8F745B42-F996-49EB-859B-970A671DE05D}.Debug_DLL|Win32.ActiveCfg = Debug_DLL|Win32 23 | {8F745B42-F996-49EB-859B-970A671DE05D}.Debug_DLL|Win32.Build.0 = Debug_DLL|Win32 24 | {8F745B42-F996-49EB-859B-970A671DE05D}.Debug_DLL|x64.ActiveCfg = Debug_DLL|x64 25 | {8F745B42-F996-49EB-859B-970A671DE05D}.Debug_DLL|x64.Build.0 = Debug_DLL|x64 26 | {8F745B42-F996-49EB-859B-970A671DE05D}.Debug|Win32.ActiveCfg = Debug|Win32 27 | {8F745B42-F996-49EB-859B-970A671DE05D}.Debug|Win32.Build.0 = Debug|Win32 28 | {8F745B42-F996-49EB-859B-970A671DE05D}.Debug|x64.ActiveCfg = Debug|x64 29 | {8F745B42-F996-49EB-859B-970A671DE05D}.Debug|x64.Build.0 = Debug|x64 30 | {8F745B42-F996-49EB-859B-970A671DE05D}.Release_DLL|Win32.ActiveCfg = Release_DLL|Win32 31 | {8F745B42-F996-49EB-859B-970A671DE05D}.Release_DLL|Win32.Build.0 = Release_DLL|Win32 32 | {8F745B42-F996-49EB-859B-970A671DE05D}.Release_DLL|x64.ActiveCfg = Release_DLL|x64 33 | {8F745B42-F996-49EB-859B-970A671DE05D}.Release_DLL|x64.Build.0 = Release_DLL|x64 34 | {8F745B42-F996-49EB-859B-970A671DE05D}.Release|Win32.ActiveCfg = Release|Win32 35 | {8F745B42-F996-49EB-859B-970A671DE05D}.Release|Win32.Build.0 = Release|Win32 36 | {8F745B42-F996-49EB-859B-970A671DE05D}.Release|x64.ActiveCfg = Release|x64 37 | {8F745B42-F996-49EB-859B-970A671DE05D}.Release|x64.Build.0 = Release|x64 38 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Debug_DLL|Win32.ActiveCfg = Debug_DLL|Win32 39 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Debug_DLL|Win32.Build.0 = Debug_DLL|Win32 40 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Debug_DLL|x64.ActiveCfg = Debug_DLL|x64 41 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Debug_DLL|x64.Build.0 = Debug_DLL|x64 42 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Debug|Win32.ActiveCfg = Debug|Win32 43 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Debug|Win32.Build.0 = Debug|Win32 44 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Debug|x64.ActiveCfg = Debug|x64 45 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Debug|x64.Build.0 = Debug|x64 46 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Release_DLL|Win32.ActiveCfg = Release_DLL|Win32 47 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Release_DLL|Win32.Build.0 = Release_DLL|Win32 48 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Release_DLL|x64.ActiveCfg = Release_DLL|x64 49 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Release_DLL|x64.Build.0 = Release_DLL|x64 50 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Release|Win32.ActiveCfg = Release|Win32 51 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Release|Win32.Build.0 = Release|Win32 52 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Release|x64.ActiveCfg = Release|x64 53 | {AF745B42-F996-49EB-859B-970A671DEF5E}.Release|x64.Build.0 = Release|x64 54 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Debug_DLL|Win32.ActiveCfg = Debug_DLL|Win32 55 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Debug_DLL|Win32.Build.0 = Debug_DLL|Win32 56 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Debug_DLL|x64.ActiveCfg = Debug_DLL|x64 57 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Debug_DLL|x64.Build.0 = Debug_DLL|x64 58 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Debug|Win32.ActiveCfg = Debug|Win32 59 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Debug|Win32.Build.0 = Debug|Win32 60 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Debug|x64.ActiveCfg = Debug|x64 61 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Debug|x64.Build.0 = Debug|x64 62 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Release_DLL|Win32.ActiveCfg = Release_DLL|Win32 63 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Release_DLL|Win32.Build.0 = Release_DLL|Win32 64 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Release_DLL|x64.ActiveCfg = Release_DLL|x64 65 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Release_DLL|x64.Build.0 = Release_DLL|x64 66 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Release|Win32.ActiveCfg = Release|Win32 67 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Release|Win32.Build.0 = Release|Win32 68 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Release|x64.ActiveCfg = Release|x64 69 | {AF745B42-E296-46EB-859B-970A671DEF5E}.Release|x64.Build.0 = Release|x64 70 | EndGlobalSection 71 | GlobalSection(SolutionProperties) = preSolution 72 | HideSolutionNode = FALSE 73 | EndGlobalSection 74 | EndGlobal 75 | -------------------------------------------------------------------------------- /crn_linux.workspace: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /crnlib/Makefile: -------------------------------------------------------------------------------- 1 | COMPILE_OPTIONS = -O3 -fomit-frame-pointer -ffast-math -fno-math-errno -g -fno-strict-aliasing -Wall -Wno-unused-value -Wno-unused -march=core2 2 | LINKER_OPTIONS = -lpthread -g 3 | 4 | OBJECTS = \ 5 | crn_arealist.o \ 6 | crn_assert.o \ 7 | crn_checksum.o \ 8 | crn_colorized_console.o \ 9 | crn_command_line_params.o \ 10 | crn_comp.o \ 11 | crn_console.o \ 12 | crn_core.o \ 13 | crn_data_stream.o \ 14 | crn_mipmapped_texture.o \ 15 | crn_decomp.o \ 16 | crn_dxt1.o \ 17 | crn_dxt5a.o \ 18 | crn_dxt.o \ 19 | crn_dxt_endpoint_refiner.o \ 20 | crn_dxt_fast.o \ 21 | crn_dxt_hc_common.o \ 22 | crn_dxt_hc.o \ 23 | crn_dxt_image.o \ 24 | crn_dynamic_string.o \ 25 | crn_file_utils.o \ 26 | crn_find_files.o \ 27 | crn_hash.o \ 28 | crn_hash_map.o \ 29 | crn_huffman_codes.o \ 30 | crn_image_utils.o \ 31 | crnlib.o \ 32 | crn_math.o \ 33 | crn_mem.o \ 34 | crn_pixel_format.o \ 35 | crn_platform.o \ 36 | crn_prefix_coding.o \ 37 | crn_qdxt1.o \ 38 | crn_qdxt5.o \ 39 | crn_rand.o \ 40 | crn_resample_filters.o \ 41 | crn_resampler.o \ 42 | crn_ryg_dxt.o \ 43 | crn_sparse_bit_array.o \ 44 | crn_stb_image.o \ 45 | crn_strutils.o \ 46 | crn_symbol_codec.o \ 47 | crn_texture_file_types.o \ 48 | crn_threaded_resampler.o \ 49 | crn_threading_pthreads.o \ 50 | crn_timer.o \ 51 | crn_utils.o \ 52 | crn_value.o \ 53 | crn_vector.o \ 54 | crn_zeng.o \ 55 | crn_texture_comp.o \ 56 | crn_texture_conversion.o \ 57 | crn_dds_comp.o \ 58 | crn_lzma_codec.o \ 59 | crn_ktx_texture.o \ 60 | crn_etc.o \ 61 | crn_rg_etc1.o \ 62 | crn_miniz.o \ 63 | crn_jpge.o \ 64 | crn_jpgd.o \ 65 | lzma_7zBuf2.o \ 66 | lzma_7zBuf.o \ 67 | lzma_7zCrc.o \ 68 | lzma_7zFile.o \ 69 | lzma_7zStream.o \ 70 | lzma_Alloc.o \ 71 | lzma_Bcj2.o \ 72 | lzma_Bra86.o \ 73 | lzma_Bra.o \ 74 | lzma_BraIA64.o \ 75 | lzma_LzFind.o \ 76 | lzma_LzmaDec.o \ 77 | lzma_LzmaEnc.o \ 78 | lzma_LzmaLib.o 79 | 80 | all: crunch 81 | 82 | %.o: %.cpp 83 | g++ $< -o $@ -c $(COMPILE_OPTIONS) 84 | 85 | crunch.o: ../crunch/crunch.cpp 86 | g++ $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS) 87 | 88 | corpus_gen.o: ../crunch/corpus_gen.cpp 89 | g++ $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS) 90 | 91 | corpus_test.o: ../crunch/corpus_test.cpp 92 | g++ $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS) 93 | 94 | crunch: $(OBJECTS) crunch.o corpus_gen.o corpus_test.o 95 | g++ $(OBJECTS) crunch.o corpus_gen.o corpus_test.o -o crunch $(LINKER_OPTIONS) 96 | 97 | -------------------------------------------------------------------------------- /crnlib/crn_arealist.h: -------------------------------------------------------------------------------- 1 | // File: crn_arealist.h - 2D shape algebra 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | struct Area 8 | { 9 | struct Area *Pprev, *Pnext; 10 | 11 | int x1, y1, x2, y2; 12 | 13 | uint get_width() const { return x2 - x1 + 1; } 14 | uint get_height() const { return y2 - y1 + 1; } 15 | uint get_area() const { return get_width() * get_height(); } 16 | }; 17 | 18 | typedef Area * Area_Ptr; 19 | 20 | struct Area_List 21 | { 22 | int total_areas; 23 | int next_free; 24 | 25 | Area *Phead, *Ptail, *Pfree; 26 | }; 27 | 28 | typedef Area_List * Area_List_Ptr; 29 | 30 | Area_List * Area_List_init(int max_areas); 31 | void Area_List_deinit(Area_List* Pobj_base); 32 | 33 | void Area_List_print(Area_List *Plist); 34 | 35 | Area_List * Area_List_dup_new(Area_List *Plist, 36 | int x_ofs, int y_ofs); 37 | 38 | uint Area_List_get_num(Area_List* Plist); 39 | 40 | // src and dst area lists must have the same number of total areas. 41 | void Area_List_dup(Area_List *Psrc_list, 42 | Area_List *Pdst_list, 43 | int x_ofs, int y_ofs); 44 | 45 | void Area_List_copy(Area_List *Psrc_list, 46 | Area_List *Pdst_list, 47 | int x_ofs, int y_ofs); 48 | 49 | void Area_List_clear(Area_List *Plist); 50 | 51 | void Area_List_set(Area_List *Plist, 52 | int x1, int y1, int x2, int y2); 53 | 54 | // logical: x and (not y) 55 | void Area_List_remove(Area_List *Plist, 56 | int x1, int y1, int x2, int y2); 57 | 58 | // logical: x or y 59 | void Area_List_insert(Area_List *Plist, 60 | int x1, int y1, int x2, int y2, 61 | bool combine); 62 | 63 | // logical: x and y 64 | void Area_List_intersect_area(Area_List *Plist, 65 | int x1, int y1, int x2, int y2); 66 | 67 | // logical: x and y 68 | void Area_List_intersect_Area_List(Area_List *Pouter_list, 69 | Area_List *Pinner_list, 70 | Area_List *Pdst_list); 71 | 72 | Area_List_Ptr Area_List_create_optimal(Area_List_Ptr Plist); 73 | 74 | } // namespace crnlib 75 | -------------------------------------------------------------------------------- /crnlib/crn_assert.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_assert.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | #if CRNLIB_USE_WIN32_API 5 | #include "crn_winhdr.h" 6 | #endif 7 | 8 | static bool g_fail_exceptions; 9 | static bool g_exit_on_failure = true; 10 | 11 | void crnlib_enable_fail_exceptions(bool enabled) 12 | { 13 | g_fail_exceptions = enabled; 14 | } 15 | 16 | void crnlib_assert(const char* pExp, const char* pFile, unsigned line) 17 | { 18 | char buf[512]; 19 | 20 | sprintf_s(buf, sizeof(buf), "%s(%u): Assertion failed: \"%s\"\n", pFile, line, pExp); 21 | 22 | crnlib_output_debug_string(buf); 23 | 24 | fputs(buf, stderr); 25 | 26 | if (crnlib_is_debugger_present()) 27 | crnlib_debug_break(); 28 | } 29 | 30 | void crnlib_fail(const char* pExp, const char* pFile, unsigned line) 31 | { 32 | char buf[512]; 33 | 34 | sprintf_s(buf, sizeof(buf), "%s(%u): Failure: \"%s\"\n", pFile, line, pExp); 35 | 36 | crnlib_output_debug_string(buf); 37 | 38 | fputs(buf, stderr); 39 | 40 | if (crnlib_is_debugger_present()) 41 | crnlib_debug_break(); 42 | 43 | #if CRNLIB_USE_WIN32_API 44 | if (g_fail_exceptions) 45 | RaiseException(CRNLIB_FAIL_EXCEPTION_CODE, 0, 0, NULL); 46 | else 47 | #endif 48 | if (g_exit_on_failure) 49 | exit(EXIT_FAILURE); 50 | } 51 | 52 | void trace(const char* pFmt, va_list args) 53 | { 54 | if (crnlib_is_debugger_present()) 55 | { 56 | char buf[512]; 57 | vsprintf_s(buf, sizeof(buf), pFmt, args); 58 | 59 | crnlib_output_debug_string(buf); 60 | } 61 | }; 62 | 63 | void trace(const char* pFmt, ...) 64 | { 65 | va_list args; 66 | va_start(args, pFmt); 67 | trace(pFmt, args); 68 | va_end(args); 69 | }; 70 | -------------------------------------------------------------------------------- /crnlib/crn_assert.h: -------------------------------------------------------------------------------- 1 | // File: crn_assert.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | const unsigned int CRNLIB_FAIL_EXCEPTION_CODE = 256U; 6 | void crnlib_enable_fail_exceptions(bool enabled); 7 | 8 | void crnlib_assert(const char* pExp, const char* pFile, unsigned line); 9 | void crnlib_fail(const char* pExp, const char* pFile, unsigned line); 10 | 11 | #ifdef NDEBUG 12 | #define CRNLIB_ASSERT(x) ((void)0) 13 | #undef CRNLIB_ASSERTS_ENABLED 14 | #else 15 | #define CRNLIB_ASSERT(_exp) (void)( (!!(_exp)) || (crnlib_assert(#_exp, __FILE__, __LINE__), 0) ) 16 | #define CRNLIB_ASSERTS_ENABLED 17 | #endif 18 | 19 | #define CRNLIB_VERIFY(_exp) (void)( (!!(_exp)) || (crnlib_assert(#_exp, __FILE__, __LINE__), 0) ) 20 | 21 | #define CRNLIB_FAIL(msg) do { crnlib_fail(#msg, __FILE__, __LINE__); } while(0) 22 | 23 | #define CRNLIB_ASSERT_OPEN_RANGE(x, l, h) CRNLIB_ASSERT((x >= l) && (x < h)) 24 | #define CRNLIB_ASSERT_CLOSED_RANGE(x, l, h) CRNLIB_ASSERT((x >= l) && (x <= h)) 25 | 26 | void trace(const char* pFmt, va_list args); 27 | void trace(const char* pFmt, ...); 28 | 29 | // Borrowed from boost libraries. 30 | template struct crnlib_assume_failure; 31 | template <> struct crnlib_assume_failure { enum { blah = 1 }; }; 32 | template struct crnlib_assume_try { }; 33 | 34 | #define CRNLIB_JOINER_FINAL(a, b) a##b 35 | #define CRNLIB_JOINER(a, b) CRNLIB_JOINER_FINAL(a, b) 36 | #define CRNLIB_JOIN(a, b) CRNLIB_JOINER(a, b) 37 | #define CRNLIB_ASSUME(p) typedef crnlib_assume_try < sizeof(crnlib_assume_failure< (bool)(p) > ) > CRNLIB_JOIN(crnlib_assume_typedef, __COUNTER__) 38 | 39 | #ifdef NDEBUG 40 | template inline T crnlib_assert_range(T i, T m) 41 | { 42 | m; 43 | return i; 44 | } 45 | template inline T crnlib_assert_range_incl(T i, T m) 46 | { 47 | m; 48 | return i; 49 | } 50 | #else 51 | template inline T crnlib_assert_range(T i, T m) 52 | { 53 | CRNLIB_ASSERT((i >= 0) && (i < m)); 54 | return i; 55 | } 56 | template inline T crnlib_assert_range_incl(T i, T m) 57 | { 58 | CRNLIB_ASSERT((i >= 0) && (i <= m)); 59 | return i; 60 | } 61 | #endif 62 | -------------------------------------------------------------------------------- /crnlib/crn_buffer_stream.h: -------------------------------------------------------------------------------- 1 | // File: crn_buffer_stream.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_data_stream.h" 5 | 6 | namespace crnlib 7 | { 8 | class buffer_stream : public data_stream 9 | { 10 | public: 11 | buffer_stream() : 12 | data_stream(), 13 | m_pBuf(NULL), 14 | m_size(0), 15 | m_ofs(0) 16 | { 17 | } 18 | 19 | buffer_stream(void* p, uint size) : 20 | data_stream(), 21 | m_pBuf(NULL), 22 | m_size(0), 23 | m_ofs(0) 24 | { 25 | open(p, size); 26 | } 27 | 28 | buffer_stream(const void* p, uint size) : 29 | data_stream(), 30 | m_pBuf(NULL), 31 | m_size(0), 32 | m_ofs(0) 33 | { 34 | open(p, size); 35 | } 36 | 37 | virtual ~buffer_stream() 38 | { 39 | } 40 | 41 | bool open(const void* p, uint size) 42 | { 43 | CRNLIB_ASSERT(p); 44 | 45 | close(); 46 | 47 | if ((!p) || (!size)) 48 | return false; 49 | 50 | m_opened = true; 51 | m_pBuf = (uint8*)(p); 52 | m_size = size; 53 | m_ofs = 0; 54 | m_attribs = cDataStreamSeekable | cDataStreamReadable; 55 | return true; 56 | } 57 | 58 | bool open(void* p, uint size) 59 | { 60 | CRNLIB_ASSERT(p); 61 | 62 | close(); 63 | 64 | if ((!p) || (!size)) 65 | return false; 66 | 67 | m_opened = true; 68 | m_pBuf = static_cast(p); 69 | m_size = size; 70 | m_ofs = 0; 71 | m_attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable; 72 | return true; 73 | } 74 | 75 | virtual bool close() 76 | { 77 | if (m_opened) 78 | { 79 | m_opened = false; 80 | m_pBuf = NULL; 81 | m_size = 0; 82 | m_ofs = 0; 83 | return true; 84 | } 85 | 86 | return false; 87 | } 88 | 89 | const void* get_buf() const { return m_pBuf; } 90 | void* get_buf() { return m_pBuf; } 91 | 92 | virtual const void* get_ptr() const { return m_pBuf; } 93 | 94 | virtual uint read(void* pBuf, uint len) 95 | { 96 | CRNLIB_ASSERT(pBuf && (len <= 0x7FFFFFFF)); 97 | 98 | if ((!m_opened) || (!is_readable()) || (!len)) 99 | return 0; 100 | 101 | CRNLIB_ASSERT(m_ofs <= m_size); 102 | 103 | uint bytes_left = m_size - m_ofs; 104 | 105 | len = math::minimum(len, bytes_left); 106 | 107 | if (len) 108 | memcpy(pBuf, &m_pBuf[m_ofs], len); 109 | 110 | m_ofs += len; 111 | 112 | return len; 113 | } 114 | 115 | virtual uint write(const void* pBuf, uint len) 116 | { 117 | CRNLIB_ASSERT(pBuf && (len <= 0x7FFFFFFF)); 118 | 119 | if ((!m_opened) || (!is_writable()) || (!len)) 120 | return 0; 121 | 122 | CRNLIB_ASSERT(m_ofs <= m_size); 123 | 124 | uint bytes_left = m_size - m_ofs; 125 | 126 | len = math::minimum(len, bytes_left); 127 | 128 | if (len) 129 | memcpy(&m_pBuf[m_ofs], pBuf, len); 130 | 131 | m_ofs += len; 132 | 133 | return len; 134 | } 135 | 136 | virtual bool flush() 137 | { 138 | if (!m_opened) 139 | return false; 140 | 141 | return true; 142 | } 143 | 144 | virtual uint64 get_size() 145 | { 146 | if (!m_opened) 147 | return 0; 148 | 149 | return m_size; 150 | } 151 | 152 | virtual uint64 get_remaining() 153 | { 154 | if (!m_opened) 155 | return 0; 156 | 157 | CRNLIB_ASSERT(m_ofs <= m_size); 158 | 159 | return m_size - m_ofs; 160 | } 161 | 162 | virtual uint64 get_ofs() 163 | { 164 | if (!m_opened) 165 | return 0; 166 | 167 | return m_ofs; 168 | } 169 | 170 | virtual bool seek(int64 ofs, bool relative) 171 | { 172 | if ((!m_opened) || (!is_seekable())) 173 | return false; 174 | 175 | int64 new_ofs = relative ? (m_ofs + ofs) : ofs; 176 | 177 | if (new_ofs < 0) 178 | return false; 179 | else if (new_ofs > m_size) 180 | return false; 181 | 182 | m_ofs = static_cast(new_ofs); 183 | 184 | post_seek(); 185 | 186 | return true; 187 | } 188 | 189 | private: 190 | uint8* m_pBuf; 191 | uint m_size; 192 | uint m_ofs; 193 | }; 194 | 195 | } // namespace crnlib 196 | 197 | -------------------------------------------------------------------------------- /crnlib/crn_checksum.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_checksum.cpp 2 | #include "crn_core.h" 3 | 4 | namespace crnlib 5 | { 6 | // From the public domain stb.h header. 7 | uint adler32(const void* pBuf, size_t buflen, uint adler32) 8 | { 9 | const uint8* buffer = static_cast(pBuf); 10 | 11 | const unsigned long ADLER_MOD = 65521; 12 | unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16; 13 | size_t blocklen; 14 | unsigned long i; 15 | 16 | blocklen = buflen % 5552; 17 | while (buflen) { 18 | for (i=0; i + 7 < blocklen; i += 8) { 19 | s1 += buffer[0], s2 += s1; 20 | s1 += buffer[1], s2 += s1; 21 | s1 += buffer[2], s2 += s1; 22 | s1 += buffer[3], s2 += s1; 23 | s1 += buffer[4], s2 += s1; 24 | s1 += buffer[5], s2 += s1; 25 | s1 += buffer[6], s2 += s1; 26 | s1 += buffer[7], s2 += s1; 27 | 28 | buffer += 8; 29 | } 30 | 31 | for (; i < blocklen; ++i) 32 | s1 += *buffer++, s2 += s1; 33 | 34 | s1 %= ADLER_MOD, s2 %= ADLER_MOD; 35 | buflen -= blocklen; 36 | blocklen = 5552; 37 | } 38 | return (s2 << 16) + s1; 39 | } 40 | 41 | uint16 crc16(const void* pBuf, size_t len, uint16 crc) 42 | { 43 | crc = ~crc; 44 | 45 | const uint8* p = reinterpret_cast(pBuf); 46 | while (len) 47 | { 48 | const uint16 q = *p++ ^ (crc >> 8); 49 | crc <<= 8U; 50 | uint16 r = (q >> 4) ^ q; 51 | crc ^= r; 52 | r <<= 5U; 53 | crc ^= r; 54 | r <<= 7U; 55 | crc ^= r; 56 | len--; 57 | } 58 | 59 | return static_cast(~crc); 60 | } 61 | 62 | } // namespace crnlib 63 | 64 | -------------------------------------------------------------------------------- /crnlib/crn_checksum.h: -------------------------------------------------------------------------------- 1 | // File: crn_checksum.h 2 | #pragma once 3 | 4 | namespace crnlib 5 | { 6 | const uint cInitAdler32 = 1U; 7 | uint adler32(const void* pBuf, size_t buflen, uint adler32 = cInitAdler32); 8 | 9 | // crc16() intended for small buffers - doesn't use an acceleration table. 10 | const uint cInitCRC16 = 0; 11 | uint16 crc16(const void* pBuf, size_t len, uint16 crc = cInitCRC16); 12 | 13 | } // namespace crnlib 14 | -------------------------------------------------------------------------------- /crnlib/crn_colorized_console.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_colorized_console.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | #include "crn_colorized_console.h" 5 | #ifdef CRNLIB_USE_WIN32_API 6 | #include "crn_winhdr.h" 7 | #endif 8 | 9 | namespace crnlib 10 | { 11 | void colorized_console::init() 12 | { 13 | console::init(); 14 | console::add_console_output_func(console_output_func, NULL); 15 | } 16 | 17 | void colorized_console::deinit() 18 | { 19 | console::remove_console_output_func(console_output_func); 20 | console::deinit(); 21 | } 22 | 23 | void colorized_console::tick() 24 | { 25 | } 26 | 27 | #ifdef CRNLIB_USE_WIN32_API 28 | bool colorized_console::console_output_func(eConsoleMessageType type, const char* pMsg, void* pData) 29 | { 30 | pData; 31 | 32 | if (console::get_output_disabled()) 33 | return true; 34 | 35 | HANDLE cons = GetStdHandle(STD_OUTPUT_HANDLE); 36 | 37 | DWORD attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; 38 | switch (type) 39 | { 40 | case cDebugConsoleMessage: attr = FOREGROUND_BLUE | FOREGROUND_INTENSITY; break; 41 | case cMessageConsoleMessage: attr = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break; 42 | case cWarningConsoleMessage: attr = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; break; 43 | case cErrorConsoleMessage: attr = FOREGROUND_RED | FOREGROUND_INTENSITY; break; 44 | default: break; 45 | } 46 | 47 | if (INVALID_HANDLE_VALUE != cons) 48 | SetConsoleTextAttribute(cons, (WORD)attr); 49 | 50 | if ((console::get_prefixes()) && (console::get_at_beginning_of_line())) 51 | { 52 | switch (type) 53 | { 54 | case cDebugConsoleMessage: 55 | printf("Debug: %s", pMsg); 56 | break; 57 | case cWarningConsoleMessage: 58 | printf("Warning: %s", pMsg); 59 | break; 60 | case cErrorConsoleMessage: 61 | printf("Error: %s", pMsg); 62 | break; 63 | default: 64 | printf("%s", pMsg); 65 | break; 66 | } 67 | } 68 | else 69 | { 70 | printf("%s", pMsg); 71 | } 72 | 73 | if (console::get_crlf()) 74 | printf("\n"); 75 | 76 | if (INVALID_HANDLE_VALUE != cons) 77 | SetConsoleTextAttribute(cons, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); 78 | 79 | return true; 80 | } 81 | #else 82 | bool colorized_console::console_output_func(eConsoleMessageType type, const char* pMsg, void* pData) 83 | { 84 | pData; 85 | if (console::get_output_disabled()) 86 | return true; 87 | 88 | if ((console::get_prefixes()) && (console::get_at_beginning_of_line())) 89 | { 90 | switch (type) 91 | { 92 | case cDebugConsoleMessage: 93 | printf("Debug: %s", pMsg); 94 | break; 95 | case cWarningConsoleMessage: 96 | printf("Warning: %s", pMsg); 97 | break; 98 | case cErrorConsoleMessage: 99 | printf("Error: %s", pMsg); 100 | break; 101 | default: 102 | printf("%s", pMsg); 103 | break; 104 | } 105 | } 106 | else 107 | { 108 | printf("%s", pMsg); 109 | } 110 | 111 | if (console::get_crlf()) 112 | printf("\n"); 113 | 114 | return true; 115 | } 116 | #endif 117 | 118 | } // namespace crnlib 119 | 120 | -------------------------------------------------------------------------------- /crnlib/crn_colorized_console.h: -------------------------------------------------------------------------------- 1 | // File: crn_colorized_console.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_console.h" 5 | 6 | namespace crnlib 7 | { 8 | class colorized_console 9 | { 10 | public: 11 | static void init(); 12 | static void deinit(); 13 | static void tick(); 14 | 15 | private: 16 | static bool console_output_func(eConsoleMessageType type, const char* pMsg, void* pData); 17 | }; 18 | 19 | } // namespace crnlib 20 | -------------------------------------------------------------------------------- /crnlib/crn_command_line_params.h: -------------------------------------------------------------------------------- 1 | // File: crn_command_line_params.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_value.h" 5 | #include 6 | 7 | namespace crnlib 8 | { 9 | // Returns the command line passed to the app as a string. 10 | // On systems where this isn't trivial, this function combines together the separate arguments, quoting and adding spaces as needed. 11 | void get_command_line_as_single_string(dynamic_string& cmd_line, int argc, char *argv[]); 12 | 13 | class command_line_params 14 | { 15 | public: 16 | struct param_value 17 | { 18 | inline param_value() : m_index(0), m_modifier(0) { } 19 | 20 | dynamic_string_array m_values; 21 | uint m_index; 22 | int8 m_modifier; 23 | }; 24 | 25 | typedef std::multimap param_map; 26 | typedef param_map::const_iterator param_map_const_iterator; 27 | typedef param_map::iterator param_map_iterator; 28 | 29 | command_line_params(); 30 | 31 | void clear(); 32 | 33 | static bool split_params(const char* p, dynamic_string_array& params); 34 | 35 | struct param_desc 36 | { 37 | const char* m_pName; 38 | uint m_num_values; 39 | bool m_support_listing_file; 40 | }; 41 | 42 | bool parse(const dynamic_string_array& params, uint n, const param_desc* pParam_desc); 43 | bool parse(const char* pCmd_line, uint n, const param_desc* pParam_desc, bool skip_first_param = true); 44 | 45 | const dynamic_string_array& get_array() const { return m_params; } 46 | 47 | bool is_param(uint index) const; 48 | 49 | const param_map& get_map() const { return m_param_map; } 50 | 51 | uint get_num_params() const { return static_cast(m_param_map.size()); } 52 | 53 | param_map_const_iterator begin() const { return m_param_map.begin(); } 54 | param_map_const_iterator end() const { return m_param_map.end(); } 55 | 56 | uint find(uint num_keys, const char** ppKeys, crnlib::vector* pIterators, crnlib::vector* pUnmatched_indices) const; 57 | 58 | void find(const char* pKey, param_map_const_iterator& begin, param_map_const_iterator& end) const; 59 | 60 | uint get_count(const char* pKey) const; 61 | 62 | // Returns end() if param cannot be found, or index is out of range. 63 | param_map_const_iterator get_param(const char* pKey, uint index) const; 64 | 65 | bool has_key(const char* pKey) const { return get_param(pKey, 0) != end(); } 66 | 67 | bool has_value(const char* pKey, uint index) const; 68 | uint get_num_values(const char* pKey, uint index) const; 69 | 70 | bool get_value_as_bool(const char* pKey, uint index = 0, bool def = false) const; 71 | 72 | int get_value_as_int(const char* pKey, uint index, int def, int l = INT_MIN, int h = INT_MAX, uint value_index = 0) const; 73 | float get_value_as_float(const char* pKey, uint index, float def = 0.0f, float l = -math::cNearlyInfinite, float h = math::cNearlyInfinite, uint value_index = 0) const; 74 | 75 | bool get_value_as_string(const char* pKey, uint index, dynamic_string& value, uint value_index = 0) const; 76 | const dynamic_string& get_value_as_string_or_empty(const char* pKey, uint index = 0, uint value_index = 0) const; 77 | 78 | private: 79 | dynamic_string_array m_params; 80 | 81 | param_map m_param_map; 82 | 83 | static bool load_string_file(const char* pFilename, dynamic_string_array& strings); 84 | }; 85 | 86 | } // namespace crnlib 87 | -------------------------------------------------------------------------------- /crnlib/crn_console.h: -------------------------------------------------------------------------------- 1 | // File: crn_console.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_dynamic_string.h" 5 | 6 | #ifdef WIN32 7 | #include 8 | #include 9 | #endif 10 | namespace crnlib 11 | { 12 | class dynamic_string; 13 | class data_stream; 14 | class mutex; 15 | 16 | enum eConsoleMessageType 17 | { 18 | cDebugConsoleMessage, // debugging messages 19 | cProgressConsoleMessage, // progress messages 20 | cInfoConsoleMessage, // ordinary messages 21 | cConsoleConsoleMessage, // user console output 22 | cMessageConsoleMessage, // high importance messages 23 | cWarningConsoleMessage, // warnings 24 | cErrorConsoleMessage, // errors 25 | 26 | cCMTTotal 27 | }; 28 | 29 | typedef bool (*console_output_func)(eConsoleMessageType type, const char* pMsg, void* pData); 30 | 31 | class console 32 | { 33 | public: 34 | static void init(); 35 | static void deinit(); 36 | 37 | static bool is_initialized() { return m_pMutex != NULL; } 38 | 39 | static void set_default_category(eConsoleMessageType category); 40 | static eConsoleMessageType get_default_category(); 41 | 42 | static void add_console_output_func(console_output_func pFunc, void* pData); 43 | static void remove_console_output_func(console_output_func pFunc); 44 | 45 | static void printf(const char* p, ...); 46 | 47 | static void vprintf(eConsoleMessageType type, const char* p, va_list args); 48 | static void printf(eConsoleMessageType type, const char* p, ...); 49 | 50 | static void cons(const char* p, ...); 51 | static void debug(const char* p, ...); 52 | static void progress(const char* p, ...); 53 | static void info(const char* p, ...); 54 | static void message(const char* p, ...); 55 | static void warning(const char* p, ...); 56 | static void error(const char* p, ...); 57 | 58 | // FIXME: All console state is currently global! 59 | static void disable_prefixes(); 60 | static void enable_prefixes(); 61 | static bool get_prefixes() { return m_prefixes; } 62 | static bool get_at_beginning_of_line() { return m_at_beginning_of_line; } 63 | 64 | static void disable_crlf(); 65 | static void enable_crlf(); 66 | static bool get_crlf() { return m_crlf; } 67 | 68 | static void disable_output() { m_output_disabled = true; } 69 | static void enable_output() { m_output_disabled = false; } 70 | static bool get_output_disabled() { return m_output_disabled; } 71 | 72 | static void set_log_stream(data_stream* pStream) { m_pLog_stream = pStream; } 73 | static data_stream* get_log_stream() { return m_pLog_stream; } 74 | 75 | static uint get_num_messages(eConsoleMessageType type) { return m_num_messages[type]; } 76 | 77 | private: 78 | static eConsoleMessageType m_default_category; 79 | 80 | struct console_func 81 | { 82 | console_func(console_output_func func = NULL, void* pData = NULL) : m_func(func), m_pData(pData) { } 83 | 84 | console_output_func m_func; 85 | void* m_pData; 86 | }; 87 | static crnlib::vector m_output_funcs; 88 | 89 | static bool m_crlf, m_prefixes, m_output_disabled; 90 | 91 | static data_stream* m_pLog_stream; 92 | 93 | static mutex* m_pMutex; 94 | 95 | static uint m_num_messages[cCMTTotal]; 96 | 97 | static bool m_at_beginning_of_line; 98 | }; 99 | 100 | #if defined(WIN32) 101 | inline int crn_getch() 102 | { 103 | return _getch(); 104 | } 105 | #elif defined(__GNUC__) 106 | #include 107 | #include 108 | inline int crn_getch() 109 | { 110 | struct termios oldt, newt; 111 | int ch; 112 | tcgetattr(STDIN_FILENO, &oldt); 113 | newt = oldt; 114 | newt.c_lflag &= ~(ICANON | ECHO); 115 | tcsetattr(STDIN_FILENO, TCSANOW, &newt); 116 | ch = getchar(); 117 | tcsetattr(STDIN_FILENO, TCSANOW, &oldt); 118 | return ch; 119 | } 120 | #else 121 | inline int crn_getch() 122 | { 123 | printf("crn_getch: Unimplemented"); 124 | return 0; 125 | } 126 | #endif 127 | } // namespace crnlib 128 | 129 | -------------------------------------------------------------------------------- /crnlib/crn_core.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_core.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | 5 | #if CRNLIB_USE_WIN32_API 6 | #include "crn_winhdr.h" 7 | #endif 8 | 9 | namespace crnlib 10 | { 11 | const char *g_copyright_str = "Copyright (c) 2010-2016 Richard Geldreich, Jr. and Binomial LLC"; 12 | const char *g_sig_str = "C8cfRlaorj0wLtnMSxrBJxTC85rho2L9hUZKHcBL"; 13 | 14 | } // namespace crnlib 15 | -------------------------------------------------------------------------------- /crnlib/crn_data_stream.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_data_stream.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | #include "crn_data_stream.h" 5 | 6 | namespace crnlib 7 | { 8 | data_stream::data_stream() : 9 | m_attribs(0), 10 | m_opened(false), m_error(false), m_got_cr(false) 11 | { 12 | } 13 | 14 | data_stream::data_stream(const char* pName, uint attribs) : 15 | m_name(pName), 16 | m_attribs(static_cast(attribs)), 17 | m_opened(false), m_error(false), m_got_cr(false) 18 | { 19 | } 20 | 21 | uint64 data_stream::skip(uint64 len) 22 | { 23 | uint64 total_bytes_read = 0; 24 | 25 | const uint cBufSize = 1024; 26 | uint8 buf[cBufSize]; 27 | 28 | while (len) 29 | { 30 | const uint64 bytes_to_read = math::minimum(sizeof(buf), len); 31 | const uint64 bytes_read = read(buf, static_cast(bytes_to_read)); 32 | total_bytes_read += bytes_read; 33 | 34 | if (bytes_read != bytes_to_read) 35 | break; 36 | 37 | len -= bytes_read; 38 | } 39 | 40 | return total_bytes_read; 41 | } 42 | 43 | bool data_stream::read_line(dynamic_string& str) 44 | { 45 | str.empty(); 46 | 47 | for ( ; ; ) 48 | { 49 | const int c = read_byte(); 50 | 51 | const bool prev_got_cr = m_got_cr; 52 | m_got_cr = false; 53 | 54 | if (c < 0) 55 | { 56 | if (!str.is_empty()) 57 | break; 58 | 59 | return false; 60 | } 61 | else if ((26 == c) || (!c)) 62 | continue; 63 | else if (13 == c) 64 | { 65 | m_got_cr = true; 66 | break; 67 | } 68 | else if (10 == c) 69 | { 70 | if (prev_got_cr) 71 | continue; 72 | 73 | break; 74 | } 75 | 76 | str.append_char(static_cast(c)); 77 | } 78 | 79 | return true; 80 | } 81 | 82 | bool data_stream::printf(const char* p, ...) 83 | { 84 | va_list args; 85 | 86 | va_start(args, p); 87 | dynamic_string buf; 88 | buf.format_args(p, args); 89 | va_end(args); 90 | 91 | return write(buf.get_ptr(), buf.get_len() * sizeof(char)) == buf.get_len() * sizeof(char); 92 | } 93 | 94 | bool data_stream::write_line(const dynamic_string& str) 95 | { 96 | if (!str.is_empty()) 97 | return write(str.get_ptr(), str.get_len()) == str.get_len(); 98 | 99 | return true; 100 | } 101 | 102 | bool data_stream::read_array(vector& buf) 103 | { 104 | if (buf.size() < get_remaining()) 105 | { 106 | if (get_remaining() > 1024U*1024U*1024U) 107 | return false; 108 | 109 | buf.resize((uint)get_remaining()); 110 | } 111 | 112 | if (!get_remaining()) 113 | { 114 | buf.resize(0); 115 | return true; 116 | } 117 | 118 | return read(&buf[0], buf.size()) == buf.size(); 119 | } 120 | 121 | bool data_stream::write_array(const vector& buf) 122 | { 123 | if (!buf.empty()) 124 | return write(&buf[0], buf.size()) == buf.size(); 125 | return true; 126 | } 127 | 128 | } // namespace crnlib 129 | -------------------------------------------------------------------------------- /crnlib/crn_data_stream.h: -------------------------------------------------------------------------------- 1 | // File: crn_data_stream.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | enum data_stream_attribs 8 | { 9 | cDataStreamReadable = 1, 10 | cDataStreamWritable = 2, 11 | cDataStreamSeekable = 4 12 | }; 13 | 14 | const int64 DATA_STREAM_SIZE_UNKNOWN = cINT64_MAX; 15 | const int64 DATA_STREAM_SIZE_INFINITE = cUINT64_MAX; 16 | 17 | class data_stream 18 | { 19 | data_stream(const data_stream&); 20 | data_stream& operator= (const data_stream&); 21 | 22 | public: 23 | data_stream(); 24 | data_stream(const char* pName, uint attribs); 25 | 26 | virtual ~data_stream() { } 27 | 28 | virtual data_stream *get_parent() { return NULL; } 29 | 30 | virtual bool close() { m_opened = false; m_error = false; m_got_cr = false; return true; } 31 | 32 | typedef uint16 attribs_t; 33 | inline attribs_t get_attribs() const { return m_attribs; } 34 | 35 | inline bool is_opened() const { return m_opened; } 36 | 37 | inline bool is_readable() const { return utils::is_bit_set(m_attribs, cDataStreamReadable); } 38 | inline bool is_writable() const { return utils::is_bit_set(m_attribs, cDataStreamWritable); } 39 | inline bool is_seekable() const { return utils::is_bit_set(m_attribs, cDataStreamSeekable); } 40 | 41 | inline bool get_error() const { return m_error; } 42 | 43 | inline const dynamic_string& get_name() const { return m_name; } 44 | inline void set_name(const char* pName) { m_name.set(pName); } 45 | 46 | virtual uint read(void* pBuf, uint len) = 0; 47 | virtual uint64 skip(uint64 len); 48 | 49 | virtual uint write(const void* pBuf, uint len) = 0; 50 | virtual bool flush() = 0; 51 | 52 | virtual bool is_size_known() const { return true; } 53 | 54 | // Returns DATA_STREAM_SIZE_UNKNOWN if size hasn't been determined yet, or DATA_STREAM_SIZE_INFINITE for infinite streams. 55 | virtual uint64 get_size() = 0; 56 | virtual uint64 get_remaining() = 0; 57 | 58 | virtual uint64 get_ofs() = 0; 59 | virtual bool seek(int64 ofs, bool relative) = 0; 60 | 61 | virtual const void* get_ptr() const { return NULL; } 62 | 63 | inline int read_byte() { uint8 c; if (read(&c, 1) != 1) return -1; return c; } 64 | inline bool write_byte(uint8 c) { return write(&c, 1) == 1; } 65 | 66 | bool read_line(dynamic_string& str); 67 | bool printf(const char* p, ...); 68 | bool write_line(const dynamic_string& str); 69 | bool write_bom() { uint16 bom = 0xFEFF; return write(&bom, sizeof(bom)) == sizeof(bom); } 70 | 71 | bool read_array(vector& buf); 72 | bool write_array(const vector& buf); 73 | 74 | protected: 75 | dynamic_string m_name; 76 | 77 | attribs_t m_attribs; 78 | bool m_opened : 1; 79 | bool m_error : 1; 80 | bool m_got_cr : 1; 81 | 82 | inline void set_error() { m_error = true; } 83 | inline void clear_error() { m_error = false; } 84 | 85 | inline void post_seek() { m_got_cr = false; } 86 | }; 87 | 88 | } // namespace crnlib 89 | 90 | -------------------------------------------------------------------------------- /crnlib/crn_dds_comp.h: -------------------------------------------------------------------------------- 1 | // File: crn_comp.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_comp.h" 5 | #include "crn_mipmapped_texture.h" 6 | #include "crn_texture_comp.h" 7 | 8 | namespace crnlib 9 | { 10 | class dds_comp : public itexture_comp 11 | { 12 | CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(dds_comp); 13 | 14 | public: 15 | dds_comp(); 16 | virtual ~dds_comp(); 17 | 18 | virtual const char *get_ext() const { return "DDS"; } 19 | 20 | virtual bool compress_init(const crn_comp_params& params); 21 | virtual bool compress_pass(const crn_comp_params& params, float *pEffective_bitrate); 22 | virtual void compress_deinit(); 23 | 24 | virtual const crnlib::vector& get_comp_data() const { return m_comp_data; } 25 | virtual crnlib::vector& get_comp_data() { return m_comp_data; } 26 | 27 | private: 28 | mipmapped_texture m_src_tex; 29 | mipmapped_texture m_packed_tex; 30 | 31 | crnlib::vector m_comp_data; 32 | 33 | const crn_comp_params* m_pParams; 34 | 35 | pixel_format m_pixel_fmt; 36 | dxt_image::pack_params m_pack_params; 37 | 38 | task_pool m_task_pool; 39 | qdxt1_params m_q1_params; 40 | qdxt5_params m_q5_params; 41 | mipmapped_texture::qdxt_state *m_pQDXT_state; 42 | 43 | void clear(); 44 | bool create_dds_tex(mipmapped_texture &dds_tex); 45 | bool convert_to_dxt(const crn_comp_params& params); 46 | }; 47 | 48 | } // namespace crnlib 49 | -------------------------------------------------------------------------------- /crnlib/crn_decomp.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_decomp.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | 5 | // Include the single-file header library with no defines, which brings in the full CRN decompressor. 6 | #include "../inc/crn_decomp.h" 7 | -------------------------------------------------------------------------------- /crnlib/crn_dxt5a.h: -------------------------------------------------------------------------------- 1 | // File: crn_dxt5a.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_dxt.h" 5 | 6 | namespace crnlib 7 | { 8 | class dxt5_endpoint_optimizer 9 | { 10 | public: 11 | dxt5_endpoint_optimizer(); 12 | 13 | struct params 14 | { 15 | params() : 16 | m_block_index(0), 17 | m_pPixels(NULL), 18 | m_num_pixels(0), 19 | m_comp_index(3), 20 | m_quality(cCRNDXTQualityUber), 21 | m_use_both_block_types(true) 22 | { 23 | } 24 | 25 | uint m_block_index; 26 | 27 | const color_quad_u8* m_pPixels; 28 | uint m_num_pixels; 29 | uint m_comp_index; 30 | 31 | crn_dxt_quality m_quality; 32 | 33 | bool m_use_both_block_types; 34 | }; 35 | 36 | struct results 37 | { 38 | uint8* m_pSelectors; 39 | 40 | uint64 m_error; 41 | 42 | uint8 m_first_endpoint; 43 | uint8 m_second_endpoint; 44 | 45 | uint8 m_block_type; // 1 if 6-alpha, otherwise 8-alpha 46 | }; 47 | 48 | bool compute(const params& p, results& r); 49 | 50 | private: 51 | const params* m_pParams; 52 | results* m_pResults; 53 | 54 | crnlib::vector m_unique_values; 55 | crnlib::vector m_unique_value_weights; 56 | 57 | crnlib::vector m_trial_selectors; 58 | crnlib::vector m_best_selectors; 59 | int m_unique_value_map[256]; 60 | 61 | sparse_bit_array m_flags; 62 | 63 | void evaluate_solution(uint low_endpoint, uint high_endpoint); 64 | }; 65 | 66 | } // namespace crnlib 67 | -------------------------------------------------------------------------------- /crnlib/crn_dxt_endpoint_refiner.h: -------------------------------------------------------------------------------- 1 | // File: crn_dxt_endpoint_refiner.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_dxt.h" 5 | 6 | namespace crnlib 7 | { 8 | // TODO: Experimental/Not fully implemented 9 | class dxt_endpoint_refiner 10 | { 11 | public: 12 | dxt_endpoint_refiner(); 13 | 14 | struct params 15 | { 16 | params() : 17 | m_block_index(0), 18 | m_pPixels(NULL), 19 | m_num_pixels(0), 20 | m_pSelectors(NULL), 21 | m_alpha_comp_index(0), 22 | m_error_to_beat(cUINT64_MAX), 23 | m_dxt1_selectors(true), 24 | m_perceptual(true), 25 | m_highest_quality(true) 26 | { 27 | } 28 | 29 | uint m_block_index; 30 | 31 | const color_quad_u8* m_pPixels; 32 | uint m_num_pixels; 33 | 34 | const uint8* m_pSelectors; 35 | 36 | uint m_alpha_comp_index; 37 | 38 | uint64 m_error_to_beat; 39 | 40 | bool m_dxt1_selectors; 41 | bool m_perceptual; 42 | bool m_highest_quality; 43 | }; 44 | 45 | struct results 46 | { 47 | uint16 m_low_color; 48 | uint16 m_high_color; 49 | uint64 m_error; 50 | }; 51 | 52 | bool refine(const params& p, results& r); 53 | 54 | private: 55 | const params* m_pParams; 56 | results* m_pResults; 57 | 58 | void optimize_dxt1(vec3F low_color, vec3F high_color); 59 | void optimize_dxt5(vec3F low_color, vec3F high_color); 60 | }; 61 | 62 | } // namespace crnlib 63 | -------------------------------------------------------------------------------- /crnlib/crn_dxt_fast.h: -------------------------------------------------------------------------------- 1 | // File: crn_dxt_fast.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_color.h" 5 | #include "crn_dxt.h" 6 | 7 | namespace crnlib 8 | { 9 | namespace dxt_fast 10 | { 11 | void compress_color_block(uint n, const color_quad_u8* block, uint& low16, uint& high16, uint8* pSelectors, bool refine = false); 12 | void compress_color_block(dxt1_block* pDXT1_block, const color_quad_u8* pBlock, bool refine = false); 13 | 14 | void compress_alpha_block(uint n, const color_quad_u8* block, uint& low8, uint& high8, uint8* pSelectors, uint comp_index); 15 | void compress_alpha_block(dxt5_block* pDXT5_block, const color_quad_u8* pBlock, uint comp_index); 16 | 17 | void find_representative_colors(uint n, const color_quad_u8* pBlock, color_quad_u8& lo, color_quad_u8& hi); 18 | 19 | } // namespace dxt_fast 20 | 21 | } // namespace crnlib 22 | -------------------------------------------------------------------------------- /crnlib/crn_dxt_hc_common.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_dxt_hc_common.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | #include "crn_dxt_hc_common.h" 5 | 6 | namespace crnlib 7 | { 8 | chunk_encoding_desc g_chunk_encodings[cNumChunkEncodings] = 9 | { 10 | { 1, { { 0, 0, 8, 8, 0 } } }, 11 | 12 | { 2, { { 0, 0, 8, 4, 1 }, { 0, 4, 8, 4, 2 } } }, 13 | { 2, { { 0, 0, 4, 8, 3 }, { 4, 0, 4, 8, 4 } } }, 14 | 15 | { 3, { { 0, 0, 8, 4, 1 }, { 0, 4, 4, 4, 7 }, { 4, 4, 4, 4, 8 } } }, 16 | { 3, { { 0, 4, 8, 4, 2 }, { 0, 0, 4, 4, 5 }, { 4, 0, 4, 4, 6 } } }, 17 | 18 | { 3, { { 0, 0, 4, 8, 3 }, { 4, 0, 4, 4, 6 }, { 4, 4, 4, 4, 8 } } }, 19 | { 3, { { 4, 0, 4, 8, 4 }, { 0, 0, 4, 4, 5 }, { 0, 4, 4, 4, 7 } } }, 20 | 21 | { 4, { { 0, 0, 4, 4, 5 }, { 4, 0, 4, 4, 6 }, { 0, 4, 4, 4, 7 }, { 4, 4, 4, 4, 8 } } } 22 | }; 23 | 24 | chunk_tile_desc g_chunk_tile_layouts[cNumChunkTileLayouts] = 25 | { 26 | // 2x2 27 | { 0, 0, 8, 8, 0 }, 28 | 29 | // 2x1 30 | { 0, 0, 8, 4, 1 }, 31 | { 0, 4, 8, 4, 2 }, 32 | 33 | // 1x2 34 | { 0, 0, 4, 8, 3 }, 35 | { 4, 0, 4, 8, 4 }, 36 | 37 | // 1x1 38 | { 0, 0, 4, 4, 5 }, 39 | { 4, 0, 4, 4, 6 }, 40 | { 0, 4, 4, 4, 7 }, 41 | { 4, 4, 4, 4, 8 } 42 | }; 43 | 44 | } // namespace crnlib 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /crnlib/crn_dxt_hc_common.h: -------------------------------------------------------------------------------- 1 | // File: crn_dxt_hc_common.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | struct chunk_tile_desc 8 | { 9 | // These values are in pixels, and always a multiple of cBlockPixelWidth/cBlockPixelHeight. 10 | uint m_x_ofs; 11 | uint m_y_ofs; 12 | uint m_width; 13 | uint m_height; 14 | uint m_layout_index; 15 | }; 16 | 17 | struct chunk_encoding_desc 18 | { 19 | uint m_num_tiles; 20 | chunk_tile_desc m_tiles[4]; 21 | }; 22 | 23 | const uint cChunkPixelWidth = 8; 24 | const uint cChunkPixelHeight = 8; 25 | const uint cChunkBlockWidth = 2; 26 | const uint cChunkBlockHeight = 2; 27 | 28 | const uint cChunkMaxTiles = 4; 29 | 30 | const uint cBlockPixelWidthShift = 2; 31 | const uint cBlockPixelHeightShift = 2; 32 | 33 | const uint cBlockPixelWidth = 4; 34 | const uint cBlockPixelHeight = 4; 35 | 36 | const uint cNumChunkEncodings = 8; 37 | extern chunk_encoding_desc g_chunk_encodings[cNumChunkEncodings]; 38 | 39 | const uint cNumChunkTileLayouts = 9; 40 | const uint cFirst4x4ChunkTileLayout = 5; 41 | extern chunk_tile_desc g_chunk_tile_layouts[cNumChunkTileLayouts]; 42 | 43 | } // namespace crnlib 44 | -------------------------------------------------------------------------------- /crnlib/crn_file_utils.h: -------------------------------------------------------------------------------- 1 | // File: crn_file_utils.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | struct file_utils 8 | { 9 | // Returns true if pSrcFilename is older than pDstFilename 10 | static bool is_read_only(const char* pFilename); 11 | static bool disable_read_only(const char* pFilename); 12 | static bool is_older_than(const char *pSrcFilename, const char* pDstFilename); 13 | static bool does_file_exist(const char* pFilename); 14 | static bool does_dir_exist(const char* pDir); 15 | static bool get_file_size(const char* pFilename, uint64& file_size); 16 | static bool get_file_size(const char* pFilename, uint32& file_size); 17 | 18 | static bool is_path_separator(char c); 19 | static bool is_path_or_drive_separator(char c); 20 | static bool is_drive_separator(char c); 21 | 22 | static bool split_path(const char* p, dynamic_string* pDrive, dynamic_string* pDir, dynamic_string* pFilename, dynamic_string* pExt); 23 | static bool split_path(const char* p, dynamic_string& path, dynamic_string& filename); 24 | 25 | static bool get_pathname(const char* p, dynamic_string& path); 26 | static bool get_filename(const char* p, dynamic_string& filename); 27 | 28 | static void combine_path(dynamic_string& dst, const char* pA, const char* pB); 29 | static void combine_path(dynamic_string& dst, const char* pA, const char* pB, const char* pC); 30 | 31 | static bool full_path(dynamic_string& path); 32 | static bool get_extension(dynamic_string& filename); 33 | static bool remove_extension(dynamic_string& filename); 34 | static bool create_path(const dynamic_string& path); 35 | static void trim_trailing_seperator(dynamic_string& path); 36 | 37 | static int wildcmp(const char* pWild, const char* pString); 38 | 39 | static bool write_buf_to_file(const char* pPath, const void* pData, size_t data_size); 40 | 41 | }; // struct file_utils 42 | 43 | } // namespace crnlib 44 | -------------------------------------------------------------------------------- /crnlib/crn_find_files.h: -------------------------------------------------------------------------------- 1 | // File: crn_win32_find_files.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | class find_files 8 | { 9 | public: 10 | struct file_desc 11 | { 12 | inline file_desc() : m_is_dir(false) { } 13 | 14 | dynamic_string m_fullname; 15 | dynamic_string m_base; 16 | dynamic_string m_rel; 17 | dynamic_string m_name; 18 | bool m_is_dir; 19 | 20 | inline bool operator== (const file_desc& other) const { return m_fullname == other.m_fullname; } 21 | inline bool operator< (const file_desc& other) const { return m_fullname < other.m_fullname; } 22 | 23 | inline operator size_t() const { return static_cast(m_fullname); } 24 | }; 25 | 26 | typedef crnlib::vector file_desc_vec; 27 | 28 | inline find_files() 29 | { 30 | m_last_error = 0; // S_OK; 31 | } 32 | 33 | enum flags 34 | { 35 | cFlagRecursive = 1, 36 | cFlagAllowDirs = 2, 37 | cFlagAllowFiles = 4, 38 | cFlagAllowHidden = 8 39 | }; 40 | 41 | bool find(const char* pBasepath, const char* pFilespec, uint flags = cFlagAllowFiles); 42 | 43 | bool find(const char* pSpec, uint flags = cFlagAllowFiles); 44 | 45 | // An HRESULT under Win32. FIXME: Abstract this better? 46 | inline int64 get_last_error() const { return m_last_error; } 47 | 48 | const file_desc_vec& get_files() const { return m_files; } 49 | 50 | private: 51 | file_desc_vec m_files; 52 | 53 | // A HRESULT under Win32 54 | int64 m_last_error; 55 | 56 | bool find_internal(const char* pBasepath, const char* pRelpath, const char* pFilespec, uint flags, int level); 57 | 58 | }; // class find_files 59 | 60 | } // namespace crnlib 61 | -------------------------------------------------------------------------------- /crnlib/crn_hash.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_hash.cpp 2 | // See Paul Hsieh's page at: http://www.azillionmonkeys.com/qed/hash.html 3 | // Also see http://www.concentric.net/~Ttwang/tech/inthash.htm, 4 | // http://burtleburtle.net/bob/hash/integer.html 5 | #include "crn_core.h" 6 | 7 | #undef get16bits 8 | #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ 9 | || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__) 10 | #define get16bits(d) (*((const uint16 *) (d))) 11 | #endif 12 | 13 | #if !defined (get16bits) 14 | #define get16bits(d) ((((uint32)(((const uint8 *)(d))[1])) << 8)\ 15 | +(uint32)(((const uint8 *)(d))[0]) ) 16 | #endif 17 | 18 | namespace crnlib 19 | { 20 | uint32 fast_hash (const void* p, int len) 21 | { 22 | const char * data = static_cast(p); 23 | 24 | uint32 hash = len, tmp; 25 | int rem; 26 | 27 | if (len <= 0 || data == NULL) return 0; 28 | 29 | rem = len & 3; 30 | len >>= 2; 31 | 32 | /* Main loop */ 33 | for (;len > 0; len--) { 34 | hash += get16bits (data); 35 | tmp = (get16bits (data+2) << 11) ^ hash; 36 | hash = (hash << 16) ^ tmp; 37 | data += 2*sizeof (uint16); 38 | hash += hash >> 11; 39 | } 40 | 41 | /* Handle end cases */ 42 | switch (rem) { 43 | case 3: hash += get16bits (data); 44 | hash ^= hash << 16; 45 | hash ^= data[sizeof (uint16)] << 18; 46 | hash += hash >> 11; 47 | break; 48 | case 2: hash += get16bits (data); 49 | hash ^= hash << 11; 50 | hash += hash >> 17; 51 | break; 52 | case 1: hash += *data; 53 | hash ^= hash << 10; 54 | hash += hash >> 1; 55 | } 56 | 57 | /* Force "avalanching" of final 127 bits */ 58 | hash ^= hash << 3; 59 | hash += hash >> 5; 60 | hash ^= hash << 4; 61 | hash += hash >> 17; 62 | hash ^= hash << 25; 63 | hash += hash >> 6; 64 | 65 | return hash; 66 | } 67 | 68 | } // namespace crnlib 69 | -------------------------------------------------------------------------------- /crnlib/crn_hash.h: -------------------------------------------------------------------------------- 1 | // File: crn_hash.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | uint32 fast_hash (const void* p, int len); 8 | 9 | // 4-byte integer hash, full avalanche 10 | inline uint32 bitmix32c(uint32 a) 11 | { 12 | a = (a+0x7ed55d16) + (a<<12); 13 | a = (a^0xc761c23c) ^ (a>>19); 14 | a = (a+0x165667b1) + (a<<5); 15 | a = (a+0xd3a2646c) ^ (a<<9); 16 | a = (a+0xfd7046c5) + (a<<3); 17 | a = (a^0xb55a4f09) ^ (a>>16); 18 | return a; 19 | } 20 | 21 | // 4-byte integer hash, full avalanche, no constants 22 | inline uint32 bitmix32(uint32 a) 23 | { 24 | a -= (a<<6); 25 | a ^= (a>>17); 26 | a -= (a<<9); 27 | a ^= (a<<4); 28 | a -= (a<<3); 29 | a ^= (a<<10); 30 | a ^= (a>>15); 31 | return a; 32 | } 33 | 34 | } // namespace crnlib 35 | -------------------------------------------------------------------------------- /crnlib/crn_hash_map.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_hash_map.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | #include "crn_hash_map.h" 5 | #include "crn_rand.h" 6 | 7 | namespace crnlib 8 | { 9 | #if 0 10 | class counted_obj 11 | { 12 | public: 13 | counted_obj(uint v = 0) : 14 | m_val(v) 15 | { 16 | m_count++; 17 | } 18 | 19 | counted_obj(const counted_obj& obj) : 20 | m_val(obj.m_val) 21 | { 22 | m_count++; 23 | } 24 | 25 | ~counted_obj() 26 | { 27 | CRNLIB_ASSERT(m_count > 0); 28 | m_count--; 29 | } 30 | 31 | static uint m_count; 32 | 33 | uint m_val; 34 | 35 | operator size_t() const { return m_val; } 36 | 37 | bool operator== (const counted_obj& rhs) const { return m_val == rhs.m_val; } 38 | bool operator== (const uint rhs) const { return m_val == rhs; } 39 | 40 | }; 41 | 42 | uint counted_obj::m_count; 43 | 44 | void hash_map_test() 45 | { 46 | random r0, r1; 47 | 48 | uint seed = 0; 49 | for ( ; ; ) 50 | { 51 | seed++; 52 | 53 | typedef crnlib::hash_map my_hash_map; 54 | my_hash_map m; 55 | 56 | const uint n = r0.irand(1, 100000); 57 | 58 | printf("%u\n", n); 59 | 60 | r1.seed(seed); 61 | 62 | crnlib::vector q; 63 | 64 | uint count = 0; 65 | for (uint i = 0; i < n; i++) 66 | { 67 | uint v = r1.urand32() & 0x7FFFFFFF; 68 | my_hash_map::insert_result res = m.insert(counted_obj(v), counted_obj(v ^ 0xdeadbeef)); 69 | if (res.second) 70 | { 71 | count++; 72 | q.push_back(v); 73 | } 74 | } 75 | 76 | CRNLIB_VERIFY(m.size() == count); 77 | 78 | r1.seed(seed); 79 | 80 | my_hash_map cm(m); 81 | m.clear(); 82 | m = cm; 83 | cm.reset(); 84 | 85 | for (uint i = 0; i < n; i++) 86 | { 87 | uint v = r1.urand32() & 0x7FFFFFFF; 88 | my_hash_map::const_iterator it = m.find(counted_obj(v)); 89 | CRNLIB_VERIFY(it != m.end()); 90 | CRNLIB_VERIFY(it->first == v); 91 | CRNLIB_VERIFY(it->second == (v ^ 0xdeadbeef)); 92 | } 93 | 94 | for (uint t = 0; t < 2; t++) 95 | { 96 | const uint nd = r0.irand(1, q.size() + 1); 97 | for (uint i = 0; i < nd; i++) 98 | { 99 | uint p = r0.irand(0, q.size()); 100 | 101 | int k = q[p]; 102 | if (k >= 0) 103 | { 104 | q[p] = -k - 1; 105 | 106 | bool s = m.erase(counted_obj(k)); 107 | CRNLIB_VERIFY(s); 108 | } 109 | } 110 | 111 | typedef crnlib::hash_map uint_hash_set; 112 | uint_hash_set s; 113 | 114 | for (uint i = 0; i < q.size(); i++) 115 | { 116 | int v = q[i]; 117 | 118 | if (v >= 0) 119 | { 120 | my_hash_map::const_iterator it = m.find(counted_obj(v)); 121 | CRNLIB_VERIFY(it != m.end()); 122 | CRNLIB_VERIFY(it->first == (uint)v); 123 | CRNLIB_VERIFY(it->second == ((uint)v ^ 0xdeadbeef)); 124 | 125 | s.insert(v); 126 | } 127 | else 128 | { 129 | my_hash_map::const_iterator it = m.find(counted_obj(-v - 1)); 130 | CRNLIB_VERIFY(it == m.end()); 131 | } 132 | } 133 | 134 | uint found_count = 0; 135 | for (my_hash_map::const_iterator it = m.begin(); it != m.end(); ++it) 136 | { 137 | CRNLIB_VERIFY(it->second == ((uint)it->first ^ 0xdeadbeef)); 138 | 139 | uint_hash_set::const_iterator fit(s.find((uint)it->first)); 140 | CRNLIB_VERIFY(fit != s.end()); 141 | 142 | CRNLIB_VERIFY(fit->first == it->first); 143 | 144 | found_count++; 145 | } 146 | 147 | CRNLIB_VERIFY(found_count == s.size()); 148 | } 149 | 150 | CRNLIB_VERIFY(counted_obj::m_count == m.size() * 2); 151 | } 152 | } 153 | #endif 154 | 155 | } // namespace crnlib 156 | -------------------------------------------------------------------------------- /crnlib/crn_helpers.h: -------------------------------------------------------------------------------- 1 | // File: crn_helpers.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | #define CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(c) c(const c&); c& operator= (const c&); 6 | #define CRNLIB_NO_HEAP_ALLOC() private: static void* operator new(size_t); static void* operator new[](size_t); 7 | 8 | namespace crnlib 9 | { 10 | namespace helpers 11 | { 12 | template struct rel_ops 13 | { 14 | friend bool operator!=(const T& x, const T& y) { return (!(x == y)); } 15 | friend bool operator> (const T& x, const T& y) { return (y < x); } 16 | friend bool operator<=(const T& x, const T& y) { return (!(y < x)); } 17 | friend bool operator>=(const T& x, const T& y) { return (!(x < y)); } 18 | }; 19 | 20 | template 21 | inline T* construct(T* p) 22 | { 23 | return new (static_cast(p)) T; 24 | } 25 | 26 | template 27 | inline T* construct(T* p, const U& init) 28 | { 29 | return new (static_cast(p)) T(init); 30 | } 31 | 32 | template 33 | inline void construct_array(T* p, uint n) 34 | { 35 | T* q = p + n; 36 | for ( ; p != q; ++p) 37 | new (static_cast(p)) T; 38 | } 39 | 40 | template 41 | inline void construct_array(T* p, uint n, const U& init) 42 | { 43 | T* q = p + n; 44 | for ( ; p != q; ++p) 45 | new (static_cast(p)) T(init); 46 | } 47 | 48 | template 49 | inline void destruct(T* p) 50 | { 51 | p; 52 | p->~T(); 53 | } 54 | 55 | template inline void destruct_array(T* p, uint n) 56 | { 57 | T* q = p + n; 58 | for ( ; p != q; ++p) 59 | p->~T(); 60 | } 61 | 62 | } // namespace helpers 63 | 64 | } // namespace crnlib 65 | -------------------------------------------------------------------------------- /crnlib/crn_huffman_codes.h: -------------------------------------------------------------------------------- 1 | // File: crn_huffman_codes.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | const uint cHuffmanMaxSupportedSyms = 8192; 8 | 9 | void* create_generate_huffman_codes_tables(); 10 | void free_generate_huffman_codes_tables(void* p); 11 | 12 | bool generate_huffman_codes(void* pContext, uint num_syms, const uint16* pFreq, uint8* pCodesizes, uint& max_code_size, uint& total_freq_ret); 13 | 14 | } // namespace crnlib 15 | -------------------------------------------------------------------------------- /crnlib/crn_intersect.h: -------------------------------------------------------------------------------- 1 | // File: crn_intersect.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_ray.h" 5 | 6 | namespace crnlib 7 | { 8 | namespace intersection 9 | { 10 | enum result 11 | { 12 | cBackfacing = -1, 13 | cFailure = 0, 14 | cSuccess, 15 | cParallel, 16 | cInside, 17 | }; 18 | 19 | // Returns cInside, cSuccess, or cFailure. 20 | // Algorithm: Graphics Gems 1 21 | template 22 | result ray_aabb(vector_type& coord, scalar_type& t, const ray_type& ray, const aabb_type& box) 23 | { 24 | enum 25 | { 26 | cNumDim = vector_type::num_elements, 27 | cRight = 0, 28 | cLeft = 1, 29 | cMiddle = 2 30 | }; 31 | 32 | bool inside = true; 33 | int quadrant[cNumDim]; 34 | scalar_type candidate_plane[cNumDim]; 35 | 36 | for (int i = 0; i < cNumDim; i++) 37 | { 38 | if (ray.get_origin()[i] < box[0][i]) 39 | { 40 | quadrant[i] = cLeft; 41 | candidate_plane[i] = box[0][i]; 42 | inside = false; 43 | } 44 | else if (ray.get_origin()[i] > box[1][i]) 45 | { 46 | quadrant[i] = cRight; 47 | candidate_plane[i] = box[1][i]; 48 | inside = false; 49 | } 50 | else 51 | { 52 | quadrant[i] = cMiddle; 53 | } 54 | } 55 | 56 | if (inside) 57 | { 58 | coord = ray.get_origin(); 59 | t = 0.0f; 60 | return cInside; 61 | } 62 | 63 | scalar_type max_t[cNumDim]; 64 | for (int i = 0; i < cNumDim; i++) 65 | { 66 | if ((quadrant[i] != cMiddle) && (ray.get_direction()[i] != 0.0f)) 67 | max_t[i] = (candidate_plane[i] - ray.get_origin()[i]) / ray.get_direction()[i]; 68 | else 69 | max_t[i] = -1.0f; 70 | } 71 | 72 | int which_plane = 0; 73 | for (int i = 1; i < cNumDim; i++) 74 | if (max_t[which_plane] < max_t[i]) 75 | which_plane = i; 76 | 77 | if (max_t[which_plane] < 0.0f) 78 | return cFailure; 79 | 80 | for (int i = 0; i < cNumDim; i++) 81 | { 82 | if (i != which_plane) 83 | { 84 | coord[i] = ray.get_origin()[i] + max_t[which_plane] * ray.get_direction()[i]; 85 | 86 | if ( (coord[i] < box[0][i]) || (coord[i] > box[1][i]) ) 87 | return cFailure; 88 | } 89 | else 90 | { 91 | coord[i] = candidate_plane[i]; 92 | } 93 | 94 | CRNLIB_ASSERT(coord[i] >= box[0][i] && coord[i] <= box[1][i]); 95 | } 96 | 97 | t = max_t[which_plane]; 98 | return cSuccess; 99 | } 100 | 101 | template 102 | result ray_aabb(bool& started_within, vector_type& coord, scalar_type& t, const ray_type& ray, const aabb_type& box) 103 | { 104 | if (!box.contains(ray.get_origin())) 105 | { 106 | started_within = false; 107 | return ray_aabb(coord, t, ray, box); 108 | } 109 | 110 | started_within = true; 111 | 112 | float diag_dist = box.diagonal_length() * 1.5f; 113 | ray_type outside_ray(ray.eval(diag_dist), -ray.get_direction()); 114 | 115 | result res(ray_aabb(coord, t, outside_ray, box)); 116 | if (res != cSuccess) 117 | return res; 118 | 119 | t = math::maximum(0.0f, diag_dist - t); 120 | return cSuccess; 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /crnlib/crn_lzma_codec.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_lzma_codec.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | #include "crn_lzma_codec.h" 5 | #include "crn_strutils.h" 6 | #include "crn_checksum.h" 7 | #include "lzma_LzmaLib.h" 8 | #include "crn_threading.h" 9 | 10 | namespace crnlib 11 | { 12 | lzma_codec::lzma_codec() : 13 | m_pCompress(LzmaCompress), 14 | m_pUncompress(LzmaUncompress) 15 | { 16 | CRNLIB_ASSUME(cLZMAPropsSize == LZMA_PROPS_SIZE); 17 | } 18 | 19 | lzma_codec::~lzma_codec() 20 | { 21 | } 22 | 23 | bool lzma_codec::pack(const void* p, uint n, crnlib::vector& buf) 24 | { 25 | if (n > 1024U*1024U*1024U) 26 | return false; 27 | 28 | uint max_comp_size = n + math::maximum(128, n >> 8); 29 | buf.resize(sizeof(header) + max_comp_size); 30 | 31 | header* pHDR = reinterpret_cast(&buf[0]); 32 | uint8* pComp_data = &buf[sizeof(header)]; 33 | 34 | utils::zero_object(*pHDR); 35 | 36 | pHDR->m_uncomp_size = n; 37 | pHDR->m_adler32 = adler32(p, n); 38 | 39 | if (n) 40 | { 41 | size_t destLen = 0; 42 | size_t outPropsSize = 0; 43 | int status = SZ_ERROR_INPUT_EOF; 44 | 45 | for (uint trial = 0; trial < 3; trial++) 46 | { 47 | destLen = max_comp_size; 48 | outPropsSize = cLZMAPropsSize; 49 | 50 | status = (*m_pCompress)(pComp_data, &destLen, reinterpret_cast(p), n, 51 | pHDR->m_lzma_props, &outPropsSize, 52 | -1, /* 0 <= level <= 9, default = 5 */ 53 | 0, /* default = (1 << 24) */ 54 | -1, /* 0 <= lc <= 8, default = 3 */ 55 | -1, /* 0 <= lp <= 4, default = 0 */ 56 | -1, /* 0 <= pb <= 4, default = 2 */ 57 | -1, /* 5 <= fb <= 273, default = 32 */ 58 | #ifdef WIN32 59 | (g_number_of_processors > 1) ? 2 : 1 60 | #else 61 | 1 62 | #endif 63 | ); 64 | 65 | if (status != SZ_ERROR_OUTPUT_EOF) 66 | break; 67 | 68 | max_comp_size += ((n+1)/2); 69 | buf.resize(sizeof(header) + max_comp_size); 70 | pHDR = reinterpret_cast(&buf[0]); 71 | pComp_data = &buf[sizeof(header)]; 72 | } 73 | 74 | if (status != SZ_OK) 75 | { 76 | buf.clear(); 77 | return false; 78 | } 79 | 80 | pHDR->m_comp_size = static_cast(destLen); 81 | 82 | buf.resize(CRNLIB_SIZEOF_U32(header) + static_cast(destLen)); 83 | } 84 | 85 | pHDR->m_sig = header::cSig; 86 | pHDR->m_checksum = static_cast(adler32((uint8*)pHDR + header::cChecksumSkipBytes, sizeof(header) - header::cChecksumSkipBytes)); 87 | 88 | return true; 89 | } 90 | 91 | bool lzma_codec::unpack(const void* p, uint n, crnlib::vector& buf) 92 | { 93 | buf.resize(0); 94 | 95 | if (n < sizeof(header)) 96 | return false; 97 | 98 | const header& hdr = *static_cast(p); 99 | if (hdr.m_sig != header::cSig) 100 | return false; 101 | 102 | if (static_cast(adler32((const uint8*)&hdr + header::cChecksumSkipBytes, sizeof(hdr) - header::cChecksumSkipBytes)) != hdr.m_checksum) 103 | return false; 104 | 105 | if (!hdr.m_uncomp_size) 106 | return true; 107 | 108 | if (!hdr.m_comp_size) 109 | return false; 110 | 111 | if (hdr.m_uncomp_size > 1024U*1024U*1024U) 112 | return false; 113 | 114 | if (!buf.try_resize(hdr.m_uncomp_size)) 115 | return false; 116 | 117 | const uint8* pComp_data = static_cast(p) + sizeof(header); 118 | size_t srcLen = n - sizeof(header); 119 | if (srcLen < hdr.m_comp_size) 120 | return false; 121 | 122 | size_t destLen = hdr.m_uncomp_size; 123 | 124 | int status = (*m_pUncompress)(&buf[0], &destLen, pComp_data, &srcLen, 125 | hdr.m_lzma_props, cLZMAPropsSize); 126 | 127 | if ((status != SZ_OK) || (destLen != hdr.m_uncomp_size)) 128 | { 129 | buf.clear(); 130 | return false; 131 | } 132 | 133 | if (adler32(&buf[0], buf.size()) != hdr.m_adler32) 134 | { 135 | buf.clear(); 136 | return false; 137 | } 138 | 139 | return true; 140 | } 141 | 142 | } // namespace crnlib 143 | -------------------------------------------------------------------------------- /crnlib/crn_lzma_codec.h: -------------------------------------------------------------------------------- 1 | // File: crn_lzma_codec.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_packed_uint.h" 5 | 6 | namespace crnlib 7 | { 8 | class lzma_codec 9 | { 10 | public: 11 | lzma_codec(); 12 | ~lzma_codec(); 13 | 14 | // Always available, because we're statically linking in lzmalib now vs. dynamically loading the DLL. 15 | bool is_initialized() const { return true; } 16 | 17 | bool pack(const void* p, uint n, crnlib::vector& buf); 18 | 19 | bool unpack(const void* p, uint n, crnlib::vector& buf); 20 | 21 | private: 22 | typedef int (CRNLIB_STDCALL *LzmaCompressFuncPtr)(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen, 23 | unsigned char *outProps, size_t *outPropsSize, /* *outPropsSize must be = 5 */ 24 | int level, /* 0 <= level <= 9, default = 5 */ 25 | unsigned dictSize, /* default = (1 << 24) */ 26 | int lc, /* 0 <= lc <= 8, default = 3 */ 27 | int lp, /* 0 <= lp <= 4, default = 0 */ 28 | int pb, /* 0 <= pb <= 4, default = 2 */ 29 | int fb, /* 5 <= fb <= 273, default = 32 */ 30 | int numThreads /* 1 or 2, default = 2 */ 31 | ); 32 | 33 | typedef int (CRNLIB_STDCALL *LzmaUncompressFuncPtr)(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t *srcLen, 34 | const unsigned char *props, size_t propsSize); 35 | 36 | LzmaCompressFuncPtr m_pCompress; 37 | LzmaUncompressFuncPtr m_pUncompress; 38 | 39 | enum { cLZMAPropsSize = 5 }; 40 | 41 | #pragma pack(push) 42 | #pragma pack(1) 43 | struct header 44 | { 45 | enum { cSig = 'L' | ('0' << 8), cChecksumSkipBytes = 3 }; 46 | packed_uint<2> m_sig; 47 | uint8 m_checksum; 48 | 49 | uint8 m_lzma_props[cLZMAPropsSize]; 50 | 51 | packed_uint<4> m_comp_size; 52 | packed_uint<4> m_uncomp_size; 53 | 54 | packed_uint<4> m_adler32; 55 | }; 56 | #pragma pack(pop) 57 | 58 | }; 59 | 60 | } // namespace crnlib 61 | -------------------------------------------------------------------------------- /crnlib/crn_math.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_math.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | 5 | namespace crnlib 6 | { 7 | namespace math 8 | { 9 | uint g_bitmasks[32] = 10 | { 11 | 1U << 0U, 1U << 1U, 1U << 2U, 1U << 3U, 12 | 1U << 4U, 1U << 5U, 1U << 6U, 1U << 7U, 13 | 1U << 8U, 1U << 9U, 1U << 10U, 1U << 11U, 14 | 1U << 12U, 1U << 13U, 1U << 14U, 1U << 15U, 15 | 1U << 16U, 1U << 17U, 1U << 18U, 1U << 19U, 16 | 1U << 20U, 1U << 21U, 1U << 22U, 1U << 23U, 17 | 1U << 24U, 1U << 25U, 1U << 26U, 1U << 27U, 18 | 1U << 28U, 1U << 29U, 1U << 30U, 1U << 31U 19 | }; 20 | 21 | double compute_entropy(const uint8* p, uint n) 22 | { 23 | uint hist[256]; 24 | utils::zero_object(hist); 25 | 26 | for (uint i = 0; i < n; i++) 27 | hist[*p++]++; 28 | 29 | double entropy = 0.0f; 30 | 31 | const double invln2 = 1.0f/log(2.0f); 32 | for (uint i = 0; i < 256; i++) 33 | { 34 | if (!hist[i]) 35 | continue; 36 | 37 | double prob = static_cast(hist[i]) / n; 38 | entropy += (-log(prob) * invln2) * hist[i]; 39 | } 40 | 41 | return entropy; 42 | } 43 | 44 | void compute_lower_pow2_dim(int& width, int& height) 45 | { 46 | const int tex_width = width; 47 | const int tex_height = height; 48 | 49 | width = 1; 50 | for ( ; ; ) 51 | { 52 | if ((width * 2) > tex_width) 53 | break; 54 | width *= 2; 55 | } 56 | 57 | height = 1; 58 | for ( ; ; ) 59 | { 60 | if ((height * 2) > tex_height) 61 | break; 62 | height *= 2; 63 | } 64 | } 65 | 66 | void compute_upper_pow2_dim(int& width, int& height) 67 | { 68 | if (!math::is_power_of_2((uint32)width)) 69 | width = math::next_pow2((uint32)width); 70 | 71 | if (!math::is_power_of_2((uint32)height)) 72 | height = math::next_pow2((uint32)height); 73 | } 74 | 75 | } // namespace math 76 | } // namespace crnlib 77 | -------------------------------------------------------------------------------- /crnlib/crn_packed_uint.h: -------------------------------------------------------------------------------- 1 | // File: crn_packed_uint 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | template 8 | struct packed_uint 9 | { 10 | inline packed_uint() { } 11 | 12 | inline packed_uint(unsigned int val) { *this = val; } 13 | 14 | inline packed_uint(const packed_uint& other) { *this = other; } 15 | 16 | inline packed_uint& operator= (const packed_uint& rhs) 17 | { 18 | if (this != &rhs) 19 | memcpy(m_buf, rhs.m_buf, sizeof(m_buf)); 20 | return *this; 21 | } 22 | 23 | inline packed_uint& operator= (unsigned int val) 24 | { 25 | #ifdef CRNLIB_BUILD_DEBUG 26 | if (N == 1) 27 | { 28 | CRNLIB_ASSERT(val <= 0xFFU); 29 | } 30 | else if (N == 2) 31 | { 32 | CRNLIB_ASSERT(val <= 0xFFFFU); 33 | } 34 | else if (N == 3) 35 | { 36 | CRNLIB_ASSERT(val <= 0xFFFFFFU); 37 | } 38 | #endif 39 | 40 | val <<= (8U * (4U - N)); 41 | 42 | for (unsigned int i = 0; i < N; i++) 43 | { 44 | m_buf[i] = static_cast(val >> 24U); 45 | val <<= 8U; 46 | } 47 | 48 | return *this; 49 | } 50 | 51 | inline operator unsigned int() const 52 | { 53 | switch (N) 54 | { 55 | case 1: return m_buf[0]; 56 | case 2: return (m_buf[0] << 8U) | m_buf[1]; 57 | case 3: return (m_buf[0] << 16U) | (m_buf[1] << 8U) | (m_buf[2]); 58 | default: return (m_buf[0] << 24U) | (m_buf[1] << 16U) | (m_buf[2] << 8U) | (m_buf[3]); 59 | } 60 | } 61 | 62 | unsigned char m_buf[N]; 63 | }; 64 | template 65 | class packed_value 66 | { 67 | public: 68 | packed_value() { } 69 | packed_value(T val) { *this = val; } 70 | 71 | inline operator T() const 72 | { 73 | T result = 0; 74 | for (int i = sizeof(T) - 1; i >= 0; i--) 75 | result = static_cast((result << 8) | m_bytes[i]); 76 | return result; 77 | } 78 | packed_value& operator= (T val) 79 | { 80 | for (int i = 0; i < sizeof(T); i++) 81 | { 82 | m_bytes[i] = static_cast(val); 83 | val >>= 8; 84 | } 85 | return *this; 86 | } 87 | private: 88 | uint8 m_bytes[sizeof(T)]; 89 | }; 90 | } // namespace crnlib 91 | 92 | -------------------------------------------------------------------------------- /crnlib/crn_platform.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_platform.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | 5 | #if CRNLIB_USE_WIN32_API 6 | #include "crn_winhdr.h" 7 | #endif 8 | #ifndef _MSC_VER 9 | int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...) 10 | { 11 | if (!sizeOfBuffer) 12 | return 0; 13 | 14 | va_list args; 15 | va_start(args, format); 16 | int c = vsnprintf(buffer, sizeOfBuffer, format, args); 17 | va_end(args); 18 | 19 | buffer[sizeOfBuffer - 1] = '\0'; 20 | 21 | if (c < 0) 22 | return sizeOfBuffer - 1; 23 | 24 | return CRNLIB_MIN(c, (int)sizeOfBuffer - 1); 25 | } 26 | 27 | int vsprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, va_list args) 28 | { 29 | if (!sizeOfBuffer) 30 | return 0; 31 | 32 | int c = vsnprintf(buffer, sizeOfBuffer, format, args); 33 | 34 | buffer[sizeOfBuffer - 1] = '\0'; 35 | 36 | if (c < 0) 37 | return sizeOfBuffer - 1; 38 | 39 | return CRNLIB_MIN(c, (int)sizeOfBuffer - 1); 40 | } 41 | 42 | char* strlwr(char* p) 43 | { 44 | char *q = p; 45 | while (*q) 46 | { 47 | char c = *q; 48 | *q++ = tolower(c); 49 | } 50 | return p; 51 | } 52 | 53 | char* strupr(char *p) 54 | { 55 | char *q = p; 56 | while (*q) 57 | { 58 | char c = *q; 59 | *q++ = toupper(c); 60 | } 61 | return p; 62 | } 63 | #endif // __GNUC__ 64 | 65 | void crnlib_debug_break(void) 66 | { 67 | CRNLIB_BREAKPOINT 68 | } 69 | 70 | #if CRNLIB_USE_WIN32_API 71 | #include "crn_winhdr.h" 72 | 73 | bool crnlib_is_debugger_present(void) 74 | { 75 | return IsDebuggerPresent() != 0; 76 | } 77 | 78 | void crnlib_output_debug_string(const char* p) 79 | { 80 | OutputDebugStringA(p); 81 | } 82 | #else 83 | bool crnlib_is_debugger_present(void) 84 | { 85 | return false; 86 | } 87 | 88 | void crnlib_output_debug_string(const char* p) 89 | { 90 | puts(p); 91 | } 92 | #endif // CRNLIB_USE_WIN32_API 93 | -------------------------------------------------------------------------------- /crnlib/crn_platform.h: -------------------------------------------------------------------------------- 1 | // File: crn_platform.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | bool crnlib_is_debugger_present(void); 6 | void crnlib_debug_break(void); 7 | void crnlib_output_debug_string(const char* p); 8 | 9 | // actually in crnlib_assert.cpp 10 | void crnlib_assert(const char* pExp, const char* pFile, unsigned line); 11 | void crnlib_fail(const char* pExp, const char* pFile, unsigned line); 12 | 13 | #if CRNLIB_LITTLE_ENDIAN_CPU 14 | const bool c_crnlib_little_endian_platform = true; 15 | #else 16 | const bool c_crnlib_little_endian_platform = false; 17 | #endif 18 | 19 | const bool c_crnlib_big_endian_platform = !c_crnlib_little_endian_platform; 20 | 21 | #ifdef __GNUC__ 22 | #define crn_fopen(pDstFile, f, m) *(pDstFile) = fopen64(f, m) 23 | #define crn_fseek fseeko64 24 | #define crn_ftell ftello64 25 | #elif defined( _MSC_VER ) 26 | #define crn_fopen(pDstFile, f, m) fopen_s(pDstFile, f, m) 27 | #define crn_fseek _fseeki64 28 | #define crn_ftell _ftelli64 29 | #else 30 | #define crn_fopen(pDstFile, f, m) *(pDstFile) = fopen(f, m) 31 | #define crn_fseek(s, o, w) fseek(s, static_cast(o), w) 32 | #define crn_ftell ftell 33 | #endif 34 | 35 | #if CRNLIB_USE_WIN32_API 36 | #define CRNLIB_BREAKPOINT DebugBreak(); 37 | #define CRNLIB_BUILTIN_EXPECT(c, v) c 38 | #elif defined(__GNUC__) 39 | #define CRNLIB_BREAKPOINT asm("int $3"); 40 | #define CRNLIB_BUILTIN_EXPECT(c, v) __builtin_expect(c, v) 41 | #else 42 | #define CRNLIB_BREAKPOINT 43 | #define CRNLIB_BUILTIN_EXPECT(c, v) c 44 | #endif 45 | 46 | #if defined(__GNUC__) 47 | #define CRNLIB_ALIGNED(x) __attribute__((aligned(x))) 48 | #define CRNLIB_NOINLINE __attribute__((noinline)) 49 | #elif defined(_MSC_VER) 50 | #define CRNLIB_ALIGNED(x) __declspec(align(x)) 51 | #define CRNLIB_NOINLINE __declspec(noinline) 52 | #else 53 | #define CRNLIB_ALIGNED(x) 54 | #define CRNLIB_NOINLINE 55 | #endif 56 | 57 | #define CRNLIB_GET_ALIGNMENT(v) ((!sizeof(v)) ? 1 : (__alignof(v) ? __alignof(v) : sizeof(uint32))) 58 | 59 | #ifndef _MSC_VER 60 | int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...); 61 | int vsprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, va_list args); 62 | char* strlwr(char* p); 63 | char* strupr(char *p); 64 | #define _stricmp strcasecmp 65 | #define _strnicmp strncasecmp 66 | #endif 67 | 68 | inline bool crnlib_is_little_endian() { return c_crnlib_little_endian_platform; } 69 | inline bool crnlib_is_big_endian() { return c_crnlib_big_endian_platform; } 70 | 71 | inline bool crnlib_is_pc() 72 | { 73 | #ifdef CRNLIB_PLATFORM_PC 74 | return true; 75 | #else 76 | return false; 77 | #endif 78 | } 79 | 80 | inline bool crnlib_is_x86() 81 | { 82 | #ifdef CRNLIB_PLATFORM_PC_X86 83 | return true; 84 | #else 85 | return false; 86 | #endif 87 | } 88 | 89 | inline bool crnlib_is_x64() 90 | { 91 | #ifdef CRNLIB_PLATFORM_PC_X64 92 | return true; 93 | #else 94 | return false; 95 | #endif 96 | } 97 | -------------------------------------------------------------------------------- /crnlib/crn_prefix_coding.h: -------------------------------------------------------------------------------- 1 | // File: crn_prefix_coding.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | namespace prefix_coding 8 | { 9 | const uint cMaxExpectedCodeSize = 16; 10 | const uint cMaxSupportedSyms = 8192; 11 | const uint cMaxTableBits = 11; 12 | 13 | bool limit_max_code_size(uint num_syms, uint8* pCodesizes, uint max_code_size); 14 | 15 | bool generate_codes(uint num_syms, const uint8* pCodesizes, uint16* pCodes); 16 | 17 | class decoder_tables 18 | { 19 | public: 20 | inline decoder_tables() : 21 | m_table_shift(0), m_table_max_code(0), m_decode_start_code_size(0), m_cur_lookup_size(0), m_lookup(NULL), m_cur_sorted_symbol_order_size(0), m_sorted_symbol_order(NULL) 22 | { 23 | } 24 | 25 | inline decoder_tables(const decoder_tables& other) : 26 | m_table_shift(0), m_table_max_code(0), m_decode_start_code_size(0), m_cur_lookup_size(0), m_lookup(NULL), m_cur_sorted_symbol_order_size(0), m_sorted_symbol_order(NULL) 27 | { 28 | *this = other; 29 | } 30 | 31 | decoder_tables& operator= (const decoder_tables& other) 32 | { 33 | if (this == &other) 34 | return *this; 35 | 36 | clear(); 37 | 38 | memcpy(this, &other, sizeof(*this)); 39 | 40 | if (other.m_lookup) 41 | { 42 | m_lookup = crnlib_new_array(m_cur_lookup_size); 43 | memcpy(m_lookup, other.m_lookup, sizeof(m_lookup[0]) * m_cur_lookup_size); 44 | } 45 | 46 | if (other.m_sorted_symbol_order) 47 | { 48 | m_sorted_symbol_order = crnlib_new_array(m_cur_sorted_symbol_order_size); 49 | memcpy(m_sorted_symbol_order, other.m_sorted_symbol_order, sizeof(m_sorted_symbol_order[0]) * m_cur_sorted_symbol_order_size); 50 | } 51 | 52 | return *this; 53 | } 54 | 55 | inline void clear() 56 | { 57 | if (m_lookup) 58 | { 59 | crnlib_delete_array(m_lookup); 60 | m_lookup = 0; 61 | m_cur_lookup_size = 0; 62 | } 63 | 64 | if (m_sorted_symbol_order) 65 | { 66 | crnlib_delete_array(m_sorted_symbol_order); 67 | m_sorted_symbol_order = NULL; 68 | m_cur_sorted_symbol_order_size = 0; 69 | } 70 | } 71 | 72 | inline ~decoder_tables() 73 | { 74 | if (m_lookup) 75 | crnlib_delete_array(m_lookup); 76 | 77 | if (m_sorted_symbol_order) 78 | crnlib_delete_array(m_sorted_symbol_order); 79 | } 80 | 81 | // DO NOT use any complex classes here - it is bitwise copied. 82 | 83 | uint m_num_syms; 84 | uint m_total_used_syms; 85 | uint m_table_bits; 86 | uint m_table_shift; 87 | uint m_table_max_code; 88 | uint m_decode_start_code_size; 89 | 90 | uint8 m_min_code_size; 91 | uint8 m_max_code_size; 92 | 93 | uint m_max_codes[cMaxExpectedCodeSize + 1]; 94 | int m_val_ptrs[cMaxExpectedCodeSize + 1]; 95 | 96 | uint m_cur_lookup_size; 97 | uint32* m_lookup; 98 | 99 | uint m_cur_sorted_symbol_order_size; 100 | uint16* m_sorted_symbol_order; 101 | 102 | inline uint get_unshifted_max_code(uint len) const 103 | { 104 | CRNLIB_ASSERT( (len >= 1) && (len <= cMaxExpectedCodeSize) ); 105 | uint k = m_max_codes[len - 1]; 106 | if (!k) 107 | return UINT_MAX; 108 | return (k - 1) >> (16 - len); 109 | } 110 | }; 111 | 112 | bool generate_decoder_tables(uint num_syms, const uint8* pCodesizes, decoder_tables* pTables, uint table_bits); 113 | 114 | } // namespace prefix_coding 115 | 116 | } // namespace crnlib 117 | -------------------------------------------------------------------------------- /crnlib/crn_rand.h: -------------------------------------------------------------------------------- 1 | // File: crn_rand.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | class kiss99 8 | { 9 | public: 10 | kiss99(); 11 | 12 | void seed(uint32 i, uint32 j, uint32 k); 13 | 14 | inline uint32 next(); 15 | 16 | private: 17 | uint32 x; 18 | uint32 y; 19 | uint32 z; 20 | uint32 c; 21 | }; 22 | 23 | class well512 24 | { 25 | public: 26 | well512(); 27 | 28 | enum { cStateSize = 16 }; 29 | void seed(uint32 seed[cStateSize]); 30 | void seed(uint32 seed); 31 | void seed(uint32 seed1, uint32 seed2, uint32 seed3); 32 | 33 | inline uint32 next(); 34 | 35 | private: 36 | uint32 m_state[cStateSize]; 37 | uint32 m_index; 38 | }; 39 | 40 | class ranctx 41 | { 42 | public: 43 | ranctx() { seed(0xDE149737); } 44 | 45 | void seed(uint32 seed); 46 | 47 | inline uint32 next(); 48 | 49 | private: 50 | uint32 a; 51 | uint32 b; 52 | uint32 c; 53 | uint32 d; 54 | }; 55 | 56 | class random 57 | { 58 | public: 59 | random(); 60 | random(uint32 i); 61 | 62 | void seed(uint32 i); 63 | void seed(uint32 i1, uint32 i2, uint32 i3); 64 | 65 | uint32 urand32(); 66 | uint64 urand64(); 67 | 68 | // "Fast" variant uses no multiplies. 69 | uint32 fast_urand32(); 70 | 71 | uint32 bit(); 72 | 73 | // Returns random between [0, 1) 74 | double drand(double l, double h); 75 | 76 | float frand(float l, float h); 77 | 78 | // Returns random between [l, h) 79 | int irand(int l, int h); 80 | 81 | // Returns random between [l, h] 82 | int irand_inclusive(int l, int h); 83 | 84 | double gaussian(double mean, double stddev); 85 | 86 | void test(); 87 | 88 | private: 89 | ranctx m_ranctx; 90 | kiss99 m_kiss99; 91 | well512 m_well512; 92 | }; 93 | 94 | // Simpler, minimal state PRNG 95 | class fast_random 96 | { 97 | public: 98 | fast_random(); 99 | fast_random(uint32 i); 100 | fast_random(const fast_random& other); 101 | fast_random& operator=(const fast_random& other); 102 | 103 | void seed(uint32 i); 104 | 105 | uint32 urand32(); 106 | uint64 urand64(); 107 | 108 | int irand(int l, int h); 109 | 110 | double drand(double l, double h); 111 | 112 | float frand(float l, float h); 113 | 114 | private: 115 | uint32 jsr; 116 | uint32 jcong; 117 | }; 118 | 119 | } // namespace crnlib 120 | -------------------------------------------------------------------------------- /crnlib/crn_ray.h: -------------------------------------------------------------------------------- 1 | // File: crn_ray.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_vec.h" 5 | 6 | namespace crnlib 7 | { 8 | template 9 | class ray 10 | { 11 | public: 12 | typedef vector_type vector_t; 13 | typedef typename vector_type::scalar_type scalar_type; 14 | 15 | inline ray() { } 16 | inline ray(eClear) { clear(); } 17 | inline ray(const vector_type& origin, const vector_type& direction) : m_origin(origin), m_direction(direction) { } 18 | 19 | inline void clear() 20 | { 21 | m_origin.clear(); 22 | m_direction.clear(); 23 | } 24 | 25 | inline const vector_type& get_origin(void) const { return m_origin; } 26 | inline void set_origin(const vector_type& origin) { m_origin = origin; } 27 | 28 | inline const vector_type& get_direction(void) const { return m_direction; } 29 | inline void set_direction(const vector_type& direction) { m_direction = direction; } 30 | 31 | inline scalar_type set_endpoints(const vector_type& start, const vector_type& end, const vector_type& def) 32 | { 33 | m_origin = start; 34 | 35 | m_direction = end - start; 36 | return m_direction.normalize(&def); 37 | } 38 | 39 | inline vector_type eval(scalar_type t) const 40 | { 41 | return m_origin + m_direction * t; 42 | } 43 | 44 | private: 45 | vector_type m_origin; 46 | vector_type m_direction; 47 | }; 48 | 49 | typedef ray ray2F; 50 | typedef ray ray3F; 51 | 52 | } // namespace crnlib 53 | -------------------------------------------------------------------------------- /crnlib/crn_resample_filters.h: -------------------------------------------------------------------------------- 1 | // File: crn_resample_filters.h 2 | // RG: This is public domain code, originally derived from Graphics Gems 3, see: http://code.google.com/p/imageresampler/ 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | typedef float (*resample_filter_func)(float t); 8 | 9 | struct resample_filter 10 | { 11 | char name[32]; 12 | resample_filter_func func; 13 | float support; 14 | }; 15 | 16 | extern const resample_filter g_resample_filters[]; 17 | extern const int g_num_resample_filters; 18 | 19 | int find_resample_filter(const char* pName); 20 | 21 | } // namespace crnlib 22 | -------------------------------------------------------------------------------- /crnlib/crn_rg_etc1.h: -------------------------------------------------------------------------------- 1 | // File: rg_etc1.h - Fast, high quality ETC1 block packer/unpacker - Rich Geldreich 2 | // Please see ZLIB license at the end of this file. 3 | #pragma once 4 | 5 | namespace crnlib { 6 | 7 | namespace rg_etc1 8 | { 9 | // Unpacks an 8-byte ETC1 compressed block to a block of 4x4 32bpp RGBA pixels. 10 | // Returns false if the block is invalid. Invalid blocks will still be unpacked with clamping. 11 | // This function is thread safe, and does not dynamically allocate any memory. 12 | // If preserve_alpha is true, the alpha channel of the destination pixels will not be overwritten. Otherwise, alpha will be set to 255. 13 | bool unpack_etc1_block(const void *pETC1_block, unsigned int* pDst_pixels_rgba, bool preserve_alpha = false); 14 | 15 | // Quality setting = the higher the quality, the slower. 16 | // To pack large textures, it is highly recommended to call pack_etc1_block() in parallel, on different blocks, from multiple threads (particularly when using cHighQuality). 17 | enum etc1_quality 18 | { 19 | cLowQuality, 20 | cMediumQuality, 21 | cHighQuality, 22 | }; 23 | 24 | struct etc1_pack_params 25 | { 26 | etc1_quality m_quality; 27 | bool m_dithering; 28 | 29 | inline etc1_pack_params() 30 | { 31 | clear(); 32 | } 33 | 34 | void clear() 35 | { 36 | m_quality = cHighQuality; 37 | m_dithering = false; 38 | } 39 | }; 40 | 41 | // Important: pack_etc1_block_init() must be called before calling pack_etc1_block(). 42 | void pack_etc1_block_init(); 43 | 44 | // Packs a 4x4 block of 32bpp RGBA pixels to an 8-byte ETC1 block. 45 | // 32-bit RGBA pixels must always be arranged as (R,G,B,A) (R first, A last) in memory, independent of platform endianness. A should always be 255. 46 | // Returns squared error of result. 47 | // This function is thread safe, and does not dynamically allocate any memory. 48 | // pack_etc1_block() does not currently support "perceptual" colorspace metrics - it primarily optimizes for RGB RMSE. 49 | unsigned int pack_etc1_block(void* pETC1_block, const unsigned int* pSrc_pixels_rgba, etc1_pack_params& pack_params); 50 | 51 | } // namespace rg_etc1 52 | 53 | } // namespace crnlib 54 | 55 | //------------------------------------------------------------------------------ 56 | // 57 | // rg_etc1 uses the ZLIB license: 58 | // http://opensource.org/licenses/Zlib 59 | // 60 | // Copyright (c) 2012 Rich Geldreich 61 | // 62 | // This software is provided 'as-is', without any express or implied 63 | // warranty. In no event will the authors be held liable for any damages 64 | // arising from the use of this software. 65 | // 66 | // Permission is granted to anyone to use this software for any purpose, 67 | // including commercial applications, and to alter it and redistribute it 68 | // freely, subject to the following restrictions: 69 | // 70 | // 1. The origin of this software must not be misrepresented; you must not 71 | // claim that you wrote the original software. If you use this software 72 | // in a product, an acknowledgment in the product documentation would be 73 | // appreciated but is not required. 74 | // 75 | // 2. Altered source versions must be plainly marked as such, and must not be 76 | // misrepresented as being the original software. 77 | // 78 | // 3. This notice may not be removed or altered from any source distribution. 79 | // 80 | //------------------------------------------------------------------------------ 81 | -------------------------------------------------------------------------------- /crnlib/crn_ryg_dxt.hpp: -------------------------------------------------------------------------------- 1 | // File: ryg_dxt.hpp 2 | #pragma once 3 | 4 | #include "crn_ryg_types.hpp" 5 | 6 | namespace ryg_dxt 7 | { 8 | extern sU8 Expand5[32]; 9 | extern sU8 Expand6[64]; 10 | extern sU8 OMatch5[256][2]; 11 | extern sU8 OMatch6[256][2]; 12 | extern sU8 OMatch5_3[256][2]; 13 | extern sU8 OMatch6_3[256][2]; 14 | extern sU8 QuantRBTab[256+16]; 15 | extern sU8 QuantGTab[256+16]; 16 | 17 | // initialize DXT codec. only needs to be called once. 18 | void sInitDXT(); 19 | 20 | // input: a 4x4 pixel block, A8R8G8B8. you need to handle boundary cases 21 | // yourself. 22 | // alpha=sTRUE => use DXT5 (else use DXT1) 23 | // quality: 0=fastest (no dither), 1=medium (dither) 24 | void sCompressDXTBlock(sU8 *dest,const sU32 *src,sBool alpha,sInt quality); 25 | 26 | void sCompressDXT5ABlock(sU8 *dest,const sU32 *src,sInt quality); 27 | 28 | } // namespace ryg_dxt 29 | 30 | -------------------------------------------------------------------------------- /crnlib/crn_strutils.h: -------------------------------------------------------------------------------- 1 | // File: crn_strutils.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | #ifdef WIN32 6 | #define CRNLIB_PATH_SEPERATOR_CHAR '\\' 7 | #else 8 | #define CRNLIB_PATH_SEPERATOR_CHAR '/' 9 | #endif 10 | 11 | namespace crnlib 12 | { 13 | char* crn_strdup(const char* pStr); 14 | int crn_stricmp(const char *p, const char *q); 15 | 16 | char* strcpy_safe(char* pDst, uint dst_len, const char* pSrc); 17 | 18 | bool int_to_string(int value, char* pDst, uint len); 19 | bool uint_to_string(uint value, char* pDst, uint len); 20 | 21 | bool string_to_int(const char*& pBuf, int& value); 22 | 23 | bool string_to_uint(const char*& pBuf, uint& value); 24 | 25 | bool string_to_int64(const char*& pBuf, int64& value); 26 | bool string_to_uint64(const char*& pBuf, uint64& value); 27 | 28 | bool string_to_bool(const char* p, bool& value); 29 | 30 | bool string_to_float(const char*& p, float& value, uint round_digit = 512U); 31 | 32 | bool string_to_double(const char*& p, double& value, uint round_digit = 512U); 33 | bool string_to_double(const char*& p, const char *pEnd, double& value, uint round_digit = 512U); 34 | 35 | } // namespace crnlib 36 | -------------------------------------------------------------------------------- /crnlib/crn_texture_comp.h: -------------------------------------------------------------------------------- 1 | // File: crn_texture_comp.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | #include "../inc/crnlib.h" 6 | 7 | namespace crnlib 8 | { 9 | class mipmapped_texture; 10 | 11 | class itexture_comp 12 | { 13 | CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(itexture_comp); 14 | 15 | public: 16 | itexture_comp() { } 17 | virtual ~itexture_comp() { } 18 | 19 | virtual const char *get_ext() const = 0; 20 | 21 | virtual bool compress_init(const crn_comp_params& params) = 0; 22 | virtual bool compress_pass(const crn_comp_params& params, float *pEffective_bitrate) = 0; 23 | virtual void compress_deinit() = 0; 24 | 25 | virtual const crnlib::vector& get_comp_data() const = 0; 26 | virtual crnlib::vector& get_comp_data() = 0; 27 | }; 28 | 29 | bool create_compressed_texture(const crn_comp_params ¶ms, crnlib::vector &comp_data, uint32 *pActual_quality_level, float *pActual_bitrate); 30 | bool create_texture_mipmaps(mipmapped_texture &work_tex, const crn_comp_params ¶ms, const crn_mipmap_params &mipmap_params, bool generate_mipmaps); 31 | bool create_compressed_texture(const crn_comp_params ¶ms, const crn_mipmap_params &mipmap_params, crnlib::vector &comp_data, uint32 *pActual_quality_level, float *pActual_bitrate); 32 | 33 | } // namespace crnlib 34 | -------------------------------------------------------------------------------- /crnlib/crn_texture_conversion.h: -------------------------------------------------------------------------------- 1 | // File: crn_texture_conversion.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_dxt_image.h" 5 | #include "crn_mipmapped_texture.h" 6 | #include "crn_rect.h" 7 | #include "crn_lzma_codec.h" 8 | 9 | namespace crnlib 10 | { 11 | namespace texture_conversion 12 | { 13 | class convert_stats 14 | { 15 | public: 16 | convert_stats(); 17 | 18 | bool init( 19 | const char* pSrc_filename, 20 | const char* pDst_filename, 21 | mipmapped_texture& src_tex, 22 | texture_file_types::format dst_file_type, 23 | bool lzma_stats); 24 | 25 | bool print(bool psnr_metrics, bool mip_stats, bool grayscale_sampling, const char *pCSVStatsFile = NULL) const; 26 | 27 | void clear(); 28 | 29 | dynamic_string m_src_filename; 30 | dynamic_string m_dst_filename; 31 | texture_file_types::format m_dst_file_type; 32 | 33 | mipmapped_texture* m_pInput_tex; 34 | mipmapped_texture m_output_tex; 35 | 36 | uint64 m_input_file_size; 37 | uint m_total_input_pixels; 38 | 39 | uint64 m_output_file_size; 40 | uint m_total_output_pixels; 41 | 42 | uint64 m_output_comp_file_size; 43 | }; 44 | 45 | class convert_params 46 | { 47 | public: 48 | convert_params() : 49 | m_pInput_texture(NULL), 50 | m_texture_type(cTextureTypeUnknown), 51 | m_dst_file_type(texture_file_types::cFormatInvalid), 52 | m_dst_format(PIXEL_FMT_INVALID), 53 | m_pProgress_func(NULL), 54 | m_pProgress_user_data(NULL), 55 | m_pIntermediate_texture(NULL), 56 | m_y_flip(false), 57 | m_unflip(false), 58 | m_always_use_source_pixel_format(false), 59 | m_write_mipmaps_to_multiple_files(false), 60 | m_quick(false), 61 | m_debugging(false), 62 | m_param_debugging(false), 63 | m_no_stats(false), 64 | m_lzma_stats(false), 65 | m_status(false), 66 | m_canceled(false) 67 | { 68 | } 69 | 70 | ~convert_params() 71 | { 72 | crnlib_delete(m_pIntermediate_texture); 73 | } 74 | 75 | void print(); 76 | 77 | // Input parameters 78 | mipmapped_texture* m_pInput_texture; 79 | 80 | texture_type m_texture_type; 81 | 82 | dynamic_string m_dst_filename; 83 | texture_file_types::format m_dst_file_type; 84 | pixel_format m_dst_format; 85 | 86 | crn_comp_params m_comp_params; 87 | crn_mipmap_params m_mipmap_params; 88 | 89 | typedef bool (*progress_callback_func_ptr)(uint percentage_complete, void* pUser_data_ptr); 90 | progress_callback_func_ptr m_pProgress_func; 91 | void* m_pProgress_user_data; 92 | 93 | // Return parameters 94 | mipmapped_texture* m_pIntermediate_texture; 95 | mutable dynamic_string m_error_message; 96 | 97 | bool m_y_flip; 98 | bool m_unflip; 99 | bool m_always_use_source_pixel_format; 100 | bool m_write_mipmaps_to_multiple_files; 101 | bool m_quick; 102 | bool m_debugging; 103 | bool m_param_debugging; 104 | bool m_no_stats; 105 | 106 | bool m_lzma_stats; 107 | mutable bool m_status; 108 | mutable bool m_canceled; 109 | }; 110 | 111 | bool process(convert_params& params, convert_stats& stats); 112 | 113 | } // namespace texture_conversion 114 | 115 | } // namespace crnlib 116 | -------------------------------------------------------------------------------- /crnlib/crn_texture_file_types.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_texture_file_types.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | #include "crn_texture_file_types.h" 5 | #include "crn_file_utils.h" 6 | 7 | namespace crnlib 8 | { 9 | const char* texture_file_types::get_extension(format fmt) 10 | { 11 | CRNLIB_ASSERT(fmt < cNumFileFormats); 12 | if (fmt >= cNumFileFormats) 13 | return NULL; 14 | 15 | static const char* extensions[cNumFileFormats] = 16 | { 17 | "dds", 18 | "crn", 19 | "ktx", 20 | 21 | "tga", 22 | "png", 23 | "jpg", 24 | "jpeg", 25 | "bmp", 26 | "gif", 27 | "tif", 28 | "tiff", 29 | "ppm", 30 | "pgm", 31 | "psd", 32 | "jp2", 33 | 34 | "", 35 | "" 36 | }; 37 | return extensions[fmt]; 38 | } 39 | 40 | texture_file_types::format texture_file_types::determine_file_format(const char* pFilename) 41 | { 42 | dynamic_string ext; 43 | if (!file_utils::split_path(pFilename, NULL, NULL, NULL, &ext)) 44 | return cFormatInvalid; 45 | 46 | if (ext.is_empty()) 47 | return cFormatInvalid; 48 | 49 | if (ext[0] == '.') 50 | ext.right(1); 51 | 52 | for (uint i = 0; i < cNumFileFormats; i++) 53 | if (ext == get_extension(static_cast(i))) 54 | return static_cast(i); 55 | 56 | return cFormatInvalid; 57 | } 58 | 59 | bool texture_file_types::supports_mipmaps(format fmt) 60 | { 61 | switch (fmt) 62 | { 63 | case cFormatCRN: 64 | case cFormatDDS: 65 | case cFormatKTX: 66 | return true; 67 | default: break; 68 | } 69 | 70 | return false; 71 | } 72 | 73 | bool texture_file_types::supports_alpha(format fmt) 74 | { 75 | switch (fmt) 76 | { 77 | case cFormatJPG: 78 | case cFormatJPEG: 79 | case cFormatGIF: 80 | case cFormatJP2: 81 | return false; 82 | default: break; 83 | } 84 | 85 | return true; 86 | } 87 | 88 | const char* get_texture_type_desc(texture_type t) 89 | { 90 | switch (t) 91 | { 92 | case cTextureTypeUnknown: return "Unknown"; 93 | case cTextureTypeRegularMap: return "2D map"; 94 | case cTextureTypeNormalMap: return "Normal map"; 95 | case cTextureTypeVerticalCrossCubemap: return "Vertical Cross Cubemap"; 96 | case cTextureTypeCubemap: return "Cubemap"; 97 | default: break; 98 | } 99 | 100 | CRNLIB_ASSERT(false); 101 | 102 | return "?"; 103 | } 104 | 105 | } // namespace crnlib 106 | -------------------------------------------------------------------------------- /crnlib/crn_texture_file_types.h: -------------------------------------------------------------------------------- 1 | // File: crn_texture_file_types.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "../inc/crnlib.h" 5 | #include "crn_vec.h" 6 | #include "crn_pixel_format.h" 7 | 8 | namespace crnlib 9 | { 10 | struct texture_file_types 11 | { 12 | enum format 13 | { 14 | cFormatInvalid = -1, 15 | 16 | cFormatDDS, 17 | cFormatCRN, 18 | cFormatKTX, 19 | 20 | cNumMipmappedFileFormats, 21 | 22 | cFormatTGA = cNumMipmappedFileFormats, 23 | cFormatPNG, 24 | cFormatJPG, 25 | cFormatJPEG, 26 | cFormatBMP, 27 | cFormatGIF, 28 | cFormatTIF, 29 | cFormatTIFF, 30 | cFormatPPM, 31 | cFormatPGM, 32 | cFormatPSD, 33 | cFormatJP2, 34 | 35 | cNumRegularFileFormats, 36 | 37 | cNumImageFileFormats = cNumRegularFileFormats - cNumMipmappedFileFormats, 38 | 39 | // Not really a file format 40 | cFormatClipboard = cNumRegularFileFormats, 41 | cFormatDragDrop, 42 | 43 | cNumFileFormats 44 | }; 45 | 46 | static const char* get_extension(format fmt); 47 | 48 | static format determine_file_format(const char* pFilename); 49 | 50 | static bool supports_mipmaps(format fmt); 51 | static bool supports_alpha(format fmt); 52 | }; 53 | 54 | enum texture_type 55 | { 56 | cTextureTypeUnknown = 0, 57 | cTextureTypeRegularMap, 58 | cTextureTypeNormalMap, 59 | cTextureTypeVerticalCrossCubemap, 60 | cTextureTypeCubemap, 61 | 62 | cNumTextureTypes 63 | }; 64 | 65 | const char* get_texture_type_desc(texture_type t); 66 | 67 | } // namespace crnlib 68 | 69 | -------------------------------------------------------------------------------- /crnlib/crn_threaded_resampler.h: -------------------------------------------------------------------------------- 1 | // File: crn_threaded_resampler.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_resampler.h" 5 | #include "crn_vec.h" 6 | 7 | namespace crnlib 8 | { 9 | class task_pool; 10 | class threaded_resampler 11 | { 12 | CRNLIB_NO_COPY_OR_ASSIGNMENT_OP(threaded_resampler); 13 | 14 | public: 15 | threaded_resampler(task_pool& tp); 16 | ~threaded_resampler(); 17 | 18 | enum pixel_format 19 | { 20 | cPF_Y_F32, 21 | cPF_RGBX_F32, 22 | cPF_RGBA_F32, 23 | 24 | cPF_Total 25 | }; 26 | 27 | struct params 28 | { 29 | params() 30 | { 31 | clear(); 32 | } 33 | 34 | void clear() 35 | { 36 | utils::zero_object(*this); 37 | 38 | m_boundary_op = Resampler::BOUNDARY_CLAMP; 39 | m_sample_low = 0.0f; 40 | m_sample_high = 255.0f; 41 | m_Pfilter_name = CRNLIB_RESAMPLER_DEFAULT_FILTER; 42 | m_filter_x_scale = 1.0f; 43 | m_filter_y_scale = 1.0f; 44 | } 45 | 46 | pixel_format m_fmt; 47 | 48 | const void* m_pSrc_pixels; 49 | uint m_src_width; 50 | uint m_src_height; 51 | uint m_src_pitch; 52 | 53 | void* m_pDst_pixels; 54 | uint m_dst_width; 55 | uint m_dst_height; 56 | uint m_dst_pitch; 57 | 58 | Resampler::Boundary_Op m_boundary_op; 59 | 60 | float m_sample_low; 61 | float m_sample_high; 62 | 63 | const char* m_Pfilter_name; 64 | float m_filter_x_scale; 65 | float m_filter_y_scale; 66 | }; 67 | 68 | bool resample(const params& p); 69 | 70 | private: 71 | task_pool* m_pTask_pool; 72 | 73 | const params* m_pParams; 74 | 75 | Resampler::Contrib_List* m_pX_contribs; 76 | Resampler::Contrib_List* m_pY_contribs; 77 | uint m_bytes_per_pixel; 78 | 79 | crnlib::vector m_tmp_img; 80 | 81 | void free_contrib_lists(); 82 | 83 | void resample_x_task(uint64 data, void* pData_ptr); 84 | void resample_y_task(uint64 data, void* pData_ptr); 85 | }; 86 | 87 | } // namespace crnlib 88 | -------------------------------------------------------------------------------- /crnlib/crn_threading.h: -------------------------------------------------------------------------------- 1 | // File: crn_threading.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | 4 | #if CRNLIB_USE_WIN32_API 5 | #include "crn_threading_win32.h" 6 | #elif CRNLIB_USE_PTHREADS_API 7 | #include "crn_threading_pthreads.h" 8 | #else 9 | #include "crn_threading_null.h" 10 | #endif 11 | -------------------------------------------------------------------------------- /crnlib/crn_timer.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_win32_timer.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | #include "crn_timer.h" 5 | #include 6 | 7 | #include "crn_timer.h" 8 | 9 | #if CRNLIB_USE_WIN32_API 10 | #include "crn_winhdr.h" 11 | #endif 12 | 13 | namespace crnlib 14 | { 15 | unsigned long long timer::g_init_ticks; 16 | unsigned long long timer::g_freq; 17 | double timer::g_inv_freq; 18 | 19 | #if defined(CRNLIB_USE_WIN32_API) 20 | inline void query_counter(timer_ticks *pTicks) 21 | { 22 | QueryPerformanceCounter(reinterpret_cast(pTicks)); 23 | } 24 | inline void query_counter_frequency(timer_ticks *pTicks) 25 | { 26 | QueryPerformanceFrequency(reinterpret_cast(pTicks)); 27 | } 28 | #elif defined(__GNUC__) 29 | #include 30 | inline void query_counter(timer_ticks *pTicks) 31 | { 32 | struct timeval cur_time; 33 | gettimeofday(&cur_time, NULL); 34 | *pTicks = static_cast(cur_time.tv_sec)*1000000ULL + static_cast(cur_time.tv_usec); 35 | } 36 | inline void query_counter_frequency(timer_ticks *pTicks) 37 | { 38 | *pTicks = 1000000; 39 | } 40 | #else 41 | #error Unimplemented 42 | #endif 43 | 44 | timer::timer() : 45 | m_start_time(0), 46 | m_stop_time(0), 47 | m_started(false), 48 | m_stopped(false) 49 | { 50 | if (!g_inv_freq) 51 | init(); 52 | } 53 | 54 | timer::timer(timer_ticks start_ticks) 55 | { 56 | if (!g_inv_freq) 57 | init(); 58 | 59 | m_start_time = start_ticks; 60 | 61 | m_started = true; 62 | m_stopped = false; 63 | } 64 | 65 | void timer::start(timer_ticks start_ticks) 66 | { 67 | m_start_time = start_ticks; 68 | 69 | m_started = true; 70 | m_stopped = false; 71 | } 72 | 73 | void timer::start() 74 | { 75 | query_counter(&m_start_time); 76 | 77 | m_started = true; 78 | m_stopped = false; 79 | } 80 | 81 | void timer::stop() 82 | { 83 | CRNLIB_ASSERT(m_started); 84 | 85 | query_counter(&m_stop_time); 86 | 87 | m_stopped = true; 88 | } 89 | 90 | double timer::get_elapsed_secs() const 91 | { 92 | CRNLIB_ASSERT(m_started); 93 | if (!m_started) 94 | return 0; 95 | 96 | timer_ticks stop_time = m_stop_time; 97 | if (!m_stopped) 98 | query_counter(&stop_time); 99 | 100 | timer_ticks delta = stop_time - m_start_time; 101 | return delta * g_inv_freq; 102 | } 103 | 104 | timer_ticks timer::get_elapsed_us() const 105 | { 106 | CRNLIB_ASSERT(m_started); 107 | if (!m_started) 108 | return 0; 109 | 110 | timer_ticks stop_time = m_stop_time; 111 | if (!m_stopped) 112 | query_counter(&stop_time); 113 | 114 | timer_ticks delta = stop_time - m_start_time; 115 | return (delta * 1000000ULL + (g_freq >> 1U)) / g_freq; 116 | } 117 | 118 | void timer::init() 119 | { 120 | if (!g_inv_freq) 121 | { 122 | query_counter_frequency(&g_freq); 123 | g_inv_freq = 1.0f / g_freq; 124 | 125 | query_counter(&g_init_ticks); 126 | } 127 | } 128 | 129 | timer_ticks timer::get_init_ticks() 130 | { 131 | if (!g_inv_freq) 132 | init(); 133 | 134 | return g_init_ticks; 135 | } 136 | 137 | timer_ticks timer::get_ticks() 138 | { 139 | if (!g_inv_freq) 140 | init(); 141 | 142 | timer_ticks ticks; 143 | query_counter(&ticks); 144 | return ticks - g_init_ticks; 145 | } 146 | 147 | double timer::ticks_to_secs(timer_ticks ticks) 148 | { 149 | if (!g_inv_freq) 150 | init(); 151 | 152 | return ticks * g_inv_freq; 153 | } 154 | 155 | } // namespace crnlib 156 | -------------------------------------------------------------------------------- /crnlib/crn_timer.h: -------------------------------------------------------------------------------- 1 | // File: crn_win32_timer.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | typedef unsigned long long timer_ticks; 8 | 9 | class timer 10 | { 11 | public: 12 | timer(); 13 | timer(timer_ticks start_ticks); 14 | 15 | void start(); 16 | void start(timer_ticks start_ticks); 17 | 18 | void stop(); 19 | 20 | double get_elapsed_secs() const; 21 | inline double get_elapsed_ms() const { return get_elapsed_secs() * 1000.0f; } 22 | timer_ticks get_elapsed_us() const; 23 | 24 | static void init(); 25 | static inline timer_ticks get_ticks_per_sec() { return g_freq; } 26 | static timer_ticks get_init_ticks(); 27 | static timer_ticks get_ticks(); 28 | static double ticks_to_secs(timer_ticks ticks); 29 | static inline double ticks_to_ms(timer_ticks ticks) { return ticks_to_secs(ticks) * 1000.0f; } 30 | static inline double get_secs() { return ticks_to_secs(get_ticks()); } 31 | static inline double get_ms() { return ticks_to_ms(get_ticks()); } 32 | 33 | private: 34 | static timer_ticks g_init_ticks; 35 | static timer_ticks g_freq; 36 | static double g_inv_freq; 37 | 38 | timer_ticks m_start_time; 39 | timer_ticks m_stop_time; 40 | 41 | bool m_started : 1; 42 | bool m_stopped : 1; 43 | }; 44 | 45 | // Prints object's lifetime to stdout 46 | class timed_scope 47 | { 48 | const char* m_pName; 49 | timer m_tm; 50 | 51 | public: 52 | inline timed_scope(char* pName = "timed_scope") : m_pName(pName) { m_tm.start(); } 53 | 54 | inline double get_elapsed_secs() const { return m_tm.get_elapsed_secs(); } 55 | inline double get_elapsed_ms() const { return m_tm.get_elapsed_ms(); } 56 | 57 | const timer &get_timer() const { return m_tm; } 58 | timer &get_timer() { return m_tm; } 59 | 60 | inline ~timed_scope() { double secs = m_tm.get_elapsed_secs(); printf("%s: %f secs, %f ms\n", m_pName, secs, secs * 1000.0f); } 61 | }; 62 | 63 | } // namespace crnlib 64 | -------------------------------------------------------------------------------- /crnlib/crn_types.h: -------------------------------------------------------------------------------- 1 | // File: crn_types.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | 5 | namespace crnlib 6 | { 7 | typedef unsigned char uint8; 8 | typedef signed char int8; 9 | typedef unsigned short uint16; 10 | typedef signed short int16; 11 | typedef unsigned int uint32; 12 | typedef uint32 uint; 13 | typedef signed int int32; 14 | 15 | #ifdef __GNUC__ 16 | typedef unsigned long long uint64; 17 | typedef long long int64; 18 | #else 19 | typedef unsigned __int64 uint64; 20 | typedef signed __int64 int64; 21 | #endif 22 | 23 | const uint8 cUINT8_MIN = 0; 24 | const uint8 cUINT8_MAX = 0xFFU; 25 | const uint16 cUINT16_MIN = 0; 26 | const uint16 cUINT16_MAX = 0xFFFFU; 27 | const uint32 cUINT32_MIN = 0; 28 | const uint32 cUINT32_MAX = 0xFFFFFFFFU; 29 | const uint64 cUINT64_MIN = 0; 30 | const uint64 cUINT64_MAX = 0xFFFFFFFFFFFFFFFFULL; //0xFFFFFFFFFFFFFFFFui64; 31 | 32 | const int8 cINT8_MIN = -128; 33 | const int8 cINT8_MAX = 127; 34 | const int16 cINT16_MIN = -32768; 35 | const int16 cINT16_MAX = 32767; 36 | const int32 cINT32_MIN = (-2147483647 - 1); 37 | const int32 cINT32_MAX = 2147483647; 38 | const int64 cINT64_MIN = (int64)0x8000000000000000ULL; //(-9223372036854775807i64 - 1); 39 | const int64 cINT64_MAX = (int64)0x7FFFFFFFFFFFFFFFULL; // 9223372036854775807i64; 40 | 41 | #if CRNLIB_64BIT_POINTERS 42 | typedef uint64 uint_ptr; 43 | typedef uint64 uint32_ptr; 44 | typedef int64 signed_size_t; 45 | typedef uint64 ptr_bits_t; 46 | #else 47 | typedef unsigned int uint_ptr; 48 | typedef unsigned int uint32_ptr; 49 | typedef signed int signed_size_t; 50 | typedef uint32 ptr_bits_t; 51 | #endif 52 | 53 | enum eVarArg { cVarArg }; 54 | enum eClear { cClear }; 55 | enum eNoClamp { cNoClamp }; 56 | enum { cInvalidIndex = -1 }; 57 | 58 | const uint cIntBits = 32; 59 | 60 | struct empty_type { }; 61 | 62 | } // namespace crnlib 63 | -------------------------------------------------------------------------------- /crnlib/crn_utils.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_utils.cpp 2 | #include "crn_core.h" 3 | #include "crn_utils.h" 4 | 5 | namespace crnlib 6 | { 7 | namespace utils 8 | { 9 | void endian_switch_words(uint16* p, uint num) 10 | { 11 | uint16* p_end = p + num; 12 | while (p != p_end) 13 | { 14 | uint16 k = *p; 15 | *p++ = swap16(k); 16 | } 17 | } 18 | 19 | void endian_switch_dwords(uint32* p, uint num) 20 | { 21 | uint32* p_end = p + num; 22 | while (p != p_end) 23 | { 24 | uint32 k = *p; 25 | *p++ = swap32(k); 26 | } 27 | } 28 | 29 | void copy_words(uint16* pDst, const uint16* pSrc, uint num, bool endian_switch) 30 | { 31 | if (!endian_switch) 32 | memcpy(pDst, pSrc, num << 1U); 33 | else 34 | { 35 | uint16* pDst_end = pDst + num; 36 | while (pDst != pDst_end) 37 | *pDst++ = swap16(*pSrc++); 38 | } 39 | } 40 | 41 | void copy_dwords(uint32* pDst, const uint32* pSrc, uint num, bool endian_switch) 42 | { 43 | if (!endian_switch) 44 | memcpy(pDst, pSrc, num << 2U); 45 | else 46 | { 47 | uint32* pDst_end = pDst + num; 48 | while (pDst != pDst_end) 49 | *pDst++ = swap32(*pSrc++); 50 | } 51 | } 52 | 53 | uint compute_max_mips(uint width, uint height) 54 | { 55 | if ((width | height) == 0) 56 | return 0; 57 | 58 | uint num_mips = 1; 59 | 60 | while ((width > 1U) || (height > 1U)) 61 | { 62 | width >>= 1U; 63 | height >>= 1U; 64 | num_mips++; 65 | } 66 | 67 | return num_mips; 68 | } 69 | 70 | } // namespace utils 71 | 72 | } // namespace crnlib 73 | -------------------------------------------------------------------------------- /crnlib/crn_value.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_value.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | #include "crn_value.h" 5 | 6 | namespace crnlib 7 | { 8 | const char* gValueDataTypeStrings[cDTTotal + 1] = 9 | { 10 | "invalid", 11 | "string", 12 | "bool", 13 | "int", 14 | "uint", 15 | "float", 16 | "vec3f", 17 | "vec3i", 18 | 19 | NULL, 20 | }; 21 | 22 | } // namespace crnlib 23 | -------------------------------------------------------------------------------- /crnlib/crn_vec_interval.h: -------------------------------------------------------------------------------- 1 | // File: crn_vec_interval.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_vec.h" 5 | 6 | namespace crnlib 7 | { 8 | template 9 | class vec_interval 10 | { 11 | public: 12 | enum { N = T::num_elements }; 13 | typedef typename T::scalar_type scalar_type; 14 | 15 | inline vec_interval(const T& v) { m_bounds[0] = v; m_bounds[1] = v; } 16 | inline vec_interval(const T& low, const T& high) { m_bounds[0] = low; m_bounds[1] = high; } 17 | 18 | inline void clear() { m_bounds[0].clear(); m_bounds[1].clear(); } 19 | 20 | inline const T& operator[] (uint i) const { CRNLIB_ASSERT(i < 2); return m_bounds[i]; } 21 | inline T& operator[] (uint i) { CRNLIB_ASSERT(i < 2); return m_bounds[i]; } 22 | 23 | private: 24 | T m_bounds[2]; 25 | }; 26 | 27 | typedef vec_interval vec_interval1F; 28 | typedef vec_interval vec_interval2F; 29 | typedef vec_interval vec_interval3F; 30 | typedef vec_interval vec_interval4F; 31 | 32 | typedef vec_interval2F aabb2F; 33 | typedef vec_interval3F aabb3F; 34 | 35 | } // namespace crnlib 36 | -------------------------------------------------------------------------------- /crnlib/crn_vector.cpp: -------------------------------------------------------------------------------- 1 | // File: crn_vector.cpp 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #include "crn_core.h" 4 | #include "crn_vector.h" 5 | #include "crn_rand.h" 6 | 7 | #include "crn_color.h" 8 | #include "crn_vec.h" 9 | 10 | namespace crnlib 11 | { 12 | bool elemental_vector::increase_capacity(uint min_new_capacity, bool grow_hint, uint element_size, object_mover pMover, bool nofail) 13 | { 14 | CRNLIB_ASSERT(m_size <= m_capacity); 15 | #ifdef CRNLIB_64BIT_POINTERS 16 | CRNLIB_ASSERT(min_new_capacity < (0x400000000ULL / element_size)); 17 | #else 18 | CRNLIB_ASSERT(min_new_capacity < (0x7FFF0000U / element_size)); 19 | #endif 20 | 21 | if (m_capacity >= min_new_capacity) 22 | return true; 23 | 24 | size_t new_capacity = min_new_capacity; 25 | if ((grow_hint) && (!math::is_power_of_2(new_capacity))) 26 | new_capacity = math::next_pow2(new_capacity); 27 | 28 | CRNLIB_ASSERT(new_capacity && (new_capacity > m_capacity)); 29 | 30 | const size_t desired_size = element_size * new_capacity; 31 | size_t actual_size; 32 | if (!pMover) 33 | { 34 | void* new_p = crnlib_realloc(m_p, desired_size, &actual_size, true); 35 | if (!new_p) 36 | { 37 | if (nofail) 38 | return false; 39 | 40 | char buf[256]; 41 | #ifdef _MSC_VER 42 | sprintf_s(buf, sizeof(buf), "vector: crnlib_realloc() failed allocating %u bytes", (uint)desired_size); 43 | #else 44 | sprintf(buf, "vector: crnlib_realloc() failed allocating %u bytes", (uint)desired_size); 45 | #endif 46 | CRNLIB_FAIL(buf); 47 | } 48 | m_p = new_p; 49 | } 50 | else 51 | { 52 | void* new_p = crnlib_malloc(desired_size, &actual_size); 53 | if (!new_p) 54 | { 55 | if (nofail) 56 | return false; 57 | 58 | char buf[256]; 59 | #ifdef _MSC_VER 60 | sprintf_s(buf, sizeof(buf), "vector: crnlib_malloc() failed allocating %u bytes", (uint)desired_size); 61 | #else 62 | sprintf(buf, "vector: crnlib_malloc() failed allocating %u bytes", (uint)desired_size); 63 | #endif 64 | CRNLIB_FAIL(buf); 65 | } 66 | 67 | (*pMover)(new_p, m_p, m_size); 68 | 69 | if (m_p) 70 | crnlib_free(m_p); 71 | 72 | m_p = new_p; 73 | } 74 | 75 | if (actual_size > desired_size) 76 | m_capacity = static_cast(actual_size / element_size); 77 | else 78 | m_capacity = static_cast(new_capacity); 79 | 80 | return true; 81 | } 82 | 83 | } // namespace crnlib 84 | -------------------------------------------------------------------------------- /crnlib/crn_vector2d.h: -------------------------------------------------------------------------------- 1 | // File: crn_vector2d.h 2 | #pragma once 3 | 4 | namespace crnlib 5 | { 6 | template 7 | class vector2D 8 | { 9 | public: 10 | typedef crnlib::vector vector_type; 11 | typedef T value_type; 12 | typedef T& reference; 13 | typedef const T& const_reference; 14 | typedef T* pointer; 15 | typedef const T* const_pointer; 16 | 17 | inline vector2D(uint width = 0, uint height = 0, const T& def = T()) : 18 | m_width(width), 19 | m_height(height), 20 | m_vec(width * height), 21 | m_def(def) 22 | { 23 | } 24 | 25 | inline vector2D(const vector2D& other) : 26 | m_width(other.m_width), 27 | m_height(other.m_height), 28 | m_vec(other.m_vec), 29 | m_def(other.m_def) 30 | { 31 | } 32 | 33 | inline vector2D& operator= (const vector2D& rhs) 34 | { 35 | if (this == &rhs) 36 | return *this; 37 | 38 | m_width = rhs.m_width; 39 | m_height = rhs.m_height; 40 | m_vec = rhs.m_vec; 41 | 42 | return *this; 43 | } 44 | 45 | bool try_resize(uint width, uint height, bool preserve = true) 46 | { 47 | if ((width == m_width) && (height == m_height)) 48 | return true; 49 | 50 | vector_type new_vec; 51 | if (!new_vec.try_resize(width * height)) 52 | return false; 53 | 54 | if (preserve) 55 | { 56 | const uint nx = math::minimum(width, m_width); 57 | const uint ny = math::minimum(height, m_height); 58 | 59 | for (uint y = 0; y < ny; y++) 60 | { 61 | const T* pSrc = &m_vec[y * m_width]; 62 | T* pDst = &new_vec[y * width]; 63 | if (CRNLIB_IS_BITWISE_COPYABLE_OR_MOVABLE(T)) 64 | memcpy(pDst, pSrc, nx * sizeof(T)); 65 | else 66 | { 67 | for (uint x = 0; x < nx; x++) 68 | *pDst++ = *pSrc++; 69 | } 70 | } 71 | } 72 | 73 | m_width = width; 74 | m_height = height; 75 | m_vec.swap(new_vec); 76 | 77 | return true; 78 | } 79 | 80 | void resize(uint width, uint height, bool preserve = true) 81 | { 82 | if (!try_resize(width, height, preserve)) 83 | { 84 | CRNLIB_FAIL("vector2D::resize: Out of memory"); 85 | } 86 | } 87 | 88 | inline void clear() 89 | { 90 | m_vec.clear(); 91 | m_width = 0; 92 | m_height = 0; 93 | } 94 | 95 | inline uint width() const { return m_width; } 96 | inline uint height() const { return m_height; } 97 | inline uint size() const { return m_vec.size(); } 98 | 99 | inline uint size_in_bytes() const { return m_vec.size() * sizeof(T); } 100 | 101 | const vector_type& get_vec() const { return m_vec; } 102 | vector_type& get_vec() { return m_vec; } 103 | 104 | inline const T* get_ptr() const { return m_vec.get_ptr(); } 105 | inline T* get_ptr() { return m_vec.get_ptr(); } 106 | 107 | inline const T& operator[] (uint i) const { return m_vec[i]; } 108 | inline T& operator[] (uint i) { return m_vec[i]; } 109 | 110 | inline const T& operator() (uint x, uint y) const 111 | { 112 | CRNLIB_ASSERT((x < m_width) && (y < m_height)); 113 | return m_vec[x + y * m_width]; 114 | } 115 | 116 | inline T& operator() (uint x, uint y) 117 | { 118 | CRNLIB_ASSERT((x < m_width) && (y < m_height)); 119 | return m_vec[x + y * m_width]; 120 | } 121 | 122 | inline const T& at (uint x, uint y) const 123 | { 124 | if ((x >= m_width) || (y >= m_height)) 125 | return m_def; 126 | return m_vec[x + y * m_width]; 127 | } 128 | 129 | inline T& at (uint x, uint y) 130 | { 131 | if ((x >= m_width) || (y >= m_height)) 132 | return m_def; 133 | return m_vec[x + y * m_width]; 134 | } 135 | 136 | inline void swap(vector2D& other) 137 | { 138 | m_vec.swap(other.m_vec); 139 | anvil::swap(m_width, other.m_width); 140 | anvil::swap(m_height, other.m_height); 141 | } 142 | 143 | inline void set_all(const T& x) 144 | { 145 | m_vec.set_all(x); 146 | } 147 | 148 | private: 149 | vector_type m_vec; 150 | uint m_width; 151 | uint m_height; 152 | T m_def; 153 | }; 154 | 155 | } // namespace anvil 156 | -------------------------------------------------------------------------------- /crnlib/crn_winhdr.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifndef WIN32 4 | #error Should not get here 5 | #endif 6 | 7 | #ifndef _WIN32_WINNT 8 | #define _WIN32_WINNT 0x500 9 | #endif 10 | 11 | #ifndef WIN32_LEAN_AND_MEAN 12 | #define WIN32_LEAN_AND_MEAN 13 | #endif 14 | 15 | #ifndef NOMINMAX 16 | #define NOMINMAX 17 | #endif 18 | 19 | #include "windows.h" 20 | -------------------------------------------------------------------------------- /crnlib/crn_zeng.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unity-Technologies/crunch/ea9b8d8c00c8329791256adafa8cf11e4e7942a2/crnlib/crn_zeng.cpp -------------------------------------------------------------------------------- /crnlib/crn_zeng.h: -------------------------------------------------------------------------------- 1 | // File: crn_zeng.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | 4 | namespace crnlib 5 | { 6 | typedef float (*zeng_similarity_func)(uint index_a, uint index_b, void* pContext); 7 | 8 | void create_zeng_reorder_table(uint n, uint num_indices, const uint* pIndices, crnlib::vector& remap_table, zeng_similarity_func pFunc, void* pContext, float similarity_func_weight); 9 | 10 | } // namespace crnlib 11 | -------------------------------------------------------------------------------- /crnlib/lzham_timer.cpp: -------------------------------------------------------------------------------- 1 | // File: lzham_timer.cpp 2 | // See Copyright Notice and license at the end of include/lzham.h 3 | #include "lzham_core.h" 4 | #include "lzham_timer.h" 5 | 6 | #ifndef LZHAM_USE_WIN32_API 7 | #include 8 | #endif 9 | 10 | namespace lzham 11 | { 12 | unsigned long long lzham_timer::g_init_ticks; 13 | unsigned long long lzham_timer::g_freq; 14 | double lzham_timer::g_inv_freq; 15 | 16 | #if LZHAM_USE_WIN32_API 17 | inline void query_counter(timer_ticks *pTicks) 18 | { 19 | QueryPerformanceCounter(reinterpret_cast(pTicks)); 20 | } 21 | inline void query_counter_frequency(timer_ticks *pTicks) 22 | { 23 | QueryPerformanceFrequency(reinterpret_cast(pTicks)); 24 | } 25 | #else 26 | inline void query_counter(timer_ticks *pTicks) 27 | { 28 | *pTicks = clock(); 29 | } 30 | inline void query_counter_frequency(timer_ticks *pTicks) 31 | { 32 | *pTicks = CLOCKS_PER_SEC; 33 | } 34 | #endif 35 | 36 | lzham_timer::lzham_timer() : 37 | m_start_time(0), 38 | m_stop_time(0), 39 | m_started(false), 40 | m_stopped(false) 41 | { 42 | if (!g_inv_freq) 43 | init(); 44 | } 45 | 46 | lzham_timer::lzham_timer(timer_ticks start_ticks) 47 | { 48 | if (!g_inv_freq) 49 | init(); 50 | 51 | m_start_time = start_ticks; 52 | 53 | m_started = true; 54 | m_stopped = false; 55 | } 56 | 57 | void lzham_timer::start(timer_ticks start_ticks) 58 | { 59 | m_start_time = start_ticks; 60 | 61 | m_started = true; 62 | m_stopped = false; 63 | } 64 | 65 | void lzham_timer::start() 66 | { 67 | query_counter(&m_start_time); 68 | 69 | m_started = true; 70 | m_stopped = false; 71 | } 72 | 73 | void lzham_timer::stop() 74 | { 75 | LZHAM_ASSERT(m_started); 76 | 77 | query_counter(&m_stop_time); 78 | 79 | m_stopped = true; 80 | } 81 | 82 | double lzham_timer::get_elapsed_secs() const 83 | { 84 | LZHAM_ASSERT(m_started); 85 | if (!m_started) 86 | return 0; 87 | 88 | timer_ticks stop_time = m_stop_time; 89 | if (!m_stopped) 90 | query_counter(&stop_time); 91 | 92 | timer_ticks delta = stop_time - m_start_time; 93 | return delta * g_inv_freq; 94 | } 95 | 96 | timer_ticks lzham_timer::get_elapsed_us() const 97 | { 98 | LZHAM_ASSERT(m_started); 99 | if (!m_started) 100 | return 0; 101 | 102 | timer_ticks stop_time = m_stop_time; 103 | if (!m_stopped) 104 | query_counter(&stop_time); 105 | 106 | timer_ticks delta = stop_time - m_start_time; 107 | return (delta * 1000000ULL + (g_freq >> 1U)) / g_freq; 108 | } 109 | 110 | void lzham_timer::init() 111 | { 112 | if (!g_inv_freq) 113 | { 114 | query_counter_frequency(&g_freq); 115 | g_inv_freq = 1.0f / g_freq; 116 | 117 | query_counter(&g_init_ticks); 118 | } 119 | } 120 | 121 | timer_ticks lzham_timer::get_init_ticks() 122 | { 123 | if (!g_inv_freq) 124 | init(); 125 | 126 | return g_init_ticks; 127 | } 128 | 129 | timer_ticks lzham_timer::get_ticks() 130 | { 131 | if (!g_inv_freq) 132 | init(); 133 | 134 | timer_ticks ticks; 135 | query_counter(&ticks); 136 | return ticks - g_init_ticks; 137 | } 138 | 139 | double lzham_timer::ticks_to_secs(timer_ticks ticks) 140 | { 141 | if (!g_inv_freq) 142 | init(); 143 | 144 | return ticks * g_inv_freq; 145 | } 146 | 147 | } // namespace lzham -------------------------------------------------------------------------------- /crnlib/lzham_timer.h: -------------------------------------------------------------------------------- 1 | // File: lzham_timer.h 2 | // See Copyright Notice and license at the end of include/lzham.h 3 | #pragma once 4 | 5 | namespace lzham 6 | { 7 | typedef unsigned long long timer_ticks; 8 | 9 | class lzham_timer 10 | { 11 | public: 12 | lzham_timer(); 13 | lzham_timer(timer_ticks start_ticks); 14 | 15 | void start(); 16 | void start(timer_ticks start_ticks); 17 | 18 | void stop(); 19 | 20 | double get_elapsed_secs() const; 21 | inline double get_elapsed_ms() const { return get_elapsed_secs() * 1000.0f; } 22 | timer_ticks get_elapsed_us() const; 23 | 24 | static void init(); 25 | static inline timer_ticks get_ticks_per_sec() { return g_freq; } 26 | static timer_ticks get_init_ticks(); 27 | static timer_ticks get_ticks(); 28 | static double ticks_to_secs(timer_ticks ticks); 29 | static inline double ticks_to_ms(timer_ticks ticks) { return ticks_to_secs(ticks) * 1000.0f; } 30 | static inline double get_secs() { return ticks_to_secs(get_ticks()); } 31 | static inline double get_ms() { return ticks_to_ms(get_ticks()); } 32 | 33 | private: 34 | static timer_ticks g_init_ticks; 35 | static timer_ticks g_freq; 36 | static double g_inv_freq; 37 | 38 | timer_ticks m_start_time; 39 | timer_ticks m_stop_time; 40 | 41 | bool m_started : 1; 42 | bool m_stopped : 1; 43 | }; 44 | 45 | enum var_args_t { cVarArgs }; 46 | 47 | #if LZHAM_PERF_SECTIONS 48 | class scoped_perf_section 49 | { 50 | public: 51 | inline scoped_perf_section() : 52 | m_start_ticks(lzham_timer::get_ticks()) 53 | { 54 | m_name[0] = '?'; 55 | m_name[1] = '\0'; 56 | } 57 | 58 | inline scoped_perf_section(const char *pName) : 59 | m_start_ticks(lzham_timer::get_ticks()) 60 | { 61 | strcpy_s(m_name, pName); 62 | 63 | lzham_buffered_printf("Thread: 0x%08X, BEGIN Time: %3.3fms, Section: %s\n", GetCurrentThreadId(), lzham_timer::ticks_to_ms(m_start_ticks), m_name); 64 | } 65 | 66 | inline scoped_perf_section(var_args_t, const char *pName, ...) : 67 | m_start_ticks(lzham_timer::get_ticks()) 68 | { 69 | va_list args; 70 | va_start(args, pName); 71 | vsprintf_s(m_name, sizeof(m_name), pName, args); 72 | va_end(args); 73 | 74 | lzham_buffered_printf("Thread: 0x%08X, BEGIN Time: %3.3fms, Section: %s\n", GetCurrentThreadId(), lzham_timer::ticks_to_ms(m_start_ticks), m_name); 75 | } 76 | 77 | inline ~scoped_perf_section() 78 | { 79 | double end_ms = lzham_timer::get_ms(); 80 | double start_ms = lzham_timer::ticks_to_ms(m_start_ticks); 81 | 82 | lzham_buffered_printf("Thread: 0x%08X, END Time: %3.3fms, Total: %3.3fms, Section: %s\n", GetCurrentThreadId(), end_ms, end_ms - start_ms, m_name); 83 | } 84 | 85 | private: 86 | char m_name[64]; 87 | timer_ticks m_start_ticks; 88 | }; 89 | #else 90 | class scoped_perf_section 91 | { 92 | public: 93 | inline scoped_perf_section() { } 94 | inline scoped_perf_section(const char *pName) { (void)pName; } 95 | inline scoped_perf_section(var_args_t, const char *pName, ...) { (void)pName; } 96 | }; 97 | #endif // LZHAM_PERF_SECTIONS 98 | 99 | } // namespace lzham 100 | -------------------------------------------------------------------------------- /crnlib/lzma_7zBuf.cpp: -------------------------------------------------------------------------------- 1 | /* 7zBuf.c -- Byte Buffer 2 | 2008-03-28 3 | Igor Pavlov 4 | Public domain */ 5 | #include "crn_core.h" 6 | 7 | #include "lzma_7zBuf.h" 8 | 9 | namespace crnlib { 10 | 11 | void Buf_Init(CBuf *p) 12 | { 13 | p->data = 0; 14 | p->size = 0; 15 | } 16 | 17 | int Buf_Create(CBuf *p, size_t size, ISzAlloc *alloc) 18 | { 19 | p->size = 0; 20 | if (size == 0) 21 | { 22 | p->data = 0; 23 | return 1; 24 | } 25 | p->data = (Byte *)alloc->Alloc(alloc, size); 26 | if (p->data != 0) 27 | { 28 | p->size = size; 29 | return 1; 30 | } 31 | return 0; 32 | } 33 | 34 | void Buf_Free(CBuf *p, ISzAlloc *alloc) 35 | { 36 | alloc->Free(alloc, p->data); 37 | p->data = 0; 38 | p->size = 0; 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /crnlib/lzma_7zBuf.h: -------------------------------------------------------------------------------- 1 | /* 7zBuf.h -- Byte Buffer 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __7Z_BUF_H 5 | #define __7Z_BUF_H 6 | 7 | #include "lzma_Types.h" 8 | 9 | namespace crnlib { 10 | 11 | typedef struct 12 | { 13 | Byte *data; 14 | size_t size; 15 | } CBuf; 16 | 17 | void Buf_Init(CBuf *p); 18 | int Buf_Create(CBuf *p, size_t size, ISzAlloc *alloc); 19 | void Buf_Free(CBuf *p, ISzAlloc *alloc); 20 | 21 | typedef struct 22 | { 23 | Byte *data; 24 | size_t size; 25 | size_t pos; 26 | } CDynBuf; 27 | 28 | void DynBuf_Construct(CDynBuf *p); 29 | void DynBuf_SeekToBeg(CDynBuf *p); 30 | int DynBuf_Write(CDynBuf *p, const Byte *buf, size_t size, ISzAlloc *alloc); 31 | void DynBuf_Free(CDynBuf *p, ISzAlloc *alloc); 32 | 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /crnlib/lzma_7zBuf2.cpp: -------------------------------------------------------------------------------- 1 | /* 7zBuf2.c -- Byte Buffer 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | 4 | #include "crn_core.h" 5 | #include 6 | #include "lzma_7zBuf.h" 7 | 8 | namespace crnlib { 9 | 10 | void DynBuf_Construct(CDynBuf *p) 11 | { 12 | p->data = 0; 13 | p->size = 0; 14 | p->pos = 0; 15 | } 16 | 17 | void DynBuf_SeekToBeg(CDynBuf *p) 18 | { 19 | p->pos = 0; 20 | } 21 | 22 | int DynBuf_Write(CDynBuf *p, const Byte *buf, size_t size, ISzAlloc *alloc) 23 | { 24 | if (size > p->size - p->pos) 25 | { 26 | size_t newSize = p->pos + size; 27 | Byte *data; 28 | newSize += newSize / 4; 29 | data = (Byte *)alloc->Alloc(alloc, newSize); 30 | if (data == 0) 31 | return 0; 32 | p->size = newSize; 33 | memcpy(data, p->data, p->pos); 34 | alloc->Free(alloc, p->data); 35 | p->data = data; 36 | } 37 | memcpy(p->data + p->pos, buf, size); 38 | p->pos += size; 39 | return 1; 40 | } 41 | 42 | void DynBuf_Free(CDynBuf *p, ISzAlloc *alloc) 43 | { 44 | alloc->Free(alloc, p->data); 45 | p->data = 0; 46 | p->size = 0; 47 | p->pos = 0; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /crnlib/lzma_7zCrc.cpp: -------------------------------------------------------------------------------- 1 | /* 7zCrc.c -- CRC32 calculation 2 | 2008-08-05 3 | Igor Pavlov 4 | Public domain */ 5 | #include "crn_core.h" 6 | 7 | #include "lzma_7zCrc.h" 8 | 9 | namespace crnlib { 10 | 11 | #define kCrcPoly 0xEDB88320 12 | UInt32 g_CrcTable[256]; 13 | 14 | void MY_FAST_CALL CrcGenerateTable(void) 15 | { 16 | UInt32 i; 17 | for (i = 0; i < 256; i++) 18 | { 19 | UInt32 r = i; 20 | int j; 21 | for (j = 0; j < 8; j++) 22 | r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1)); 23 | g_CrcTable[i] = r; 24 | } 25 | } 26 | 27 | UInt32 MY_FAST_CALL CrcUpdate(UInt32 v, const void *data, size_t size) 28 | { 29 | const Byte *p = (const Byte *)data; 30 | for (; size > 0 ; size--, p++) 31 | v = CRC_UPDATE_BYTE(v, *p); 32 | return v; 33 | } 34 | 35 | UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size) 36 | { 37 | return CrcUpdate(CRC_INIT_VAL, data, size) ^ 0xFFFFFFFF; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /crnlib/lzma_7zCrc.h: -------------------------------------------------------------------------------- 1 | /* 7zCrc.h -- CRC32 calculation 2 | 2008-03-13 3 | Igor Pavlov 4 | Public domain */ 5 | 6 | #ifndef __7Z_CRC_H 7 | #define __7Z_CRC_H 8 | 9 | #include 10 | 11 | #include "lzma_Types.h" 12 | 13 | namespace crnlib { 14 | 15 | extern UInt32 g_CrcTable[]; 16 | 17 | void MY_FAST_CALL CrcGenerateTable(void); 18 | 19 | #define CRC_INIT_VAL 0xFFFFFFFF 20 | #define CRC_GET_DIGEST(crc) ((crc) ^ 0xFFFFFFFF) 21 | #define CRC_UPDATE_BYTE(crc, b) (g_CrcTable[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) 22 | 23 | UInt32 MY_FAST_CALL CrcUpdate(UInt32 crc, const void *data, size_t size); 24 | UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size); 25 | 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /crnlib/lzma_7zFile.h: -------------------------------------------------------------------------------- 1 | /* 7zFile.h -- File IO 2 | 2008-11-22 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __7Z_FILE_H 5 | #define __7Z_FILE_H 6 | 7 | #ifdef _WIN32 8 | #define USE_WINDOWS_FILE 9 | #endif 10 | 11 | #ifdef USE_WINDOWS_FILE 12 | #include 13 | #else 14 | #include 15 | #endif 16 | 17 | #include "lzma_Types.h" 18 | 19 | namespace crnlib { 20 | 21 | 22 | /* ---------- File ---------- */ 23 | 24 | typedef struct 25 | { 26 | #ifdef USE_WINDOWS_FILE 27 | HANDLE handle; 28 | #else 29 | FILE *file; 30 | #endif 31 | } CSzFile; 32 | 33 | void File_Construct(CSzFile *p); 34 | WRes InFile_Open(CSzFile *p, const char *name); 35 | WRes OutFile_Open(CSzFile *p, const char *name); 36 | WRes File_Close(CSzFile *p); 37 | 38 | /* reads max(*size, remain file's size) bytes */ 39 | WRes File_Read(CSzFile *p, void *data, size_t *size); 40 | 41 | /* writes *size bytes */ 42 | WRes File_Write(CSzFile *p, const void *data, size_t *size); 43 | 44 | WRes File_Seek(CSzFile *p, Int64 *pos, ESzSeek origin); 45 | WRes File_GetLength(CSzFile *p, UInt64 *length); 46 | 47 | 48 | /* ---------- FileInStream ---------- */ 49 | 50 | typedef struct 51 | { 52 | ISeqInStream s; 53 | CSzFile file; 54 | } CFileSeqInStream; 55 | 56 | void FileSeqInStream_CreateVTable(CFileSeqInStream *p); 57 | 58 | 59 | typedef struct 60 | { 61 | ISeekInStream s; 62 | CSzFile file; 63 | } CFileInStream; 64 | 65 | void FileInStream_CreateVTable(CFileInStream *p); 66 | 67 | 68 | typedef struct 69 | { 70 | ISeqOutStream s; 71 | CSzFile file; 72 | } CFileOutStream; 73 | 74 | void FileOutStream_CreateVTable(CFileOutStream *p); 75 | 76 | } 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /crnlib/lzma_7zStream.cpp: -------------------------------------------------------------------------------- 1 | /* 7zStream.c -- 7z Stream functions 2 | 2008-11-23 : Igor Pavlov : Public domain */ 3 | #include "crn_core.h" 4 | #include 5 | 6 | #include "lzma_Types.h" 7 | 8 | namespace crnlib { 9 | 10 | SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType) 11 | { 12 | while (size != 0) 13 | { 14 | size_t processed = size; 15 | RINOK(stream->Read(stream, buf, &processed)); 16 | if (processed == 0) 17 | return errorType; 18 | buf = (void *)((Byte *)buf + processed); 19 | size -= processed; 20 | } 21 | return SZ_OK; 22 | } 23 | 24 | SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size) 25 | { 26 | return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF); 27 | } 28 | 29 | SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf) 30 | { 31 | size_t processed = 1; 32 | RINOK(stream->Read(stream, buf, &processed)); 33 | return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF; 34 | } 35 | 36 | SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset) 37 | { 38 | Int64 t = offset; 39 | return stream->Seek(stream, &t, SZ_SEEK_SET); 40 | } 41 | 42 | SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size) 43 | { 44 | void *lookBuf; 45 | if (*size == 0) 46 | return SZ_OK; 47 | RINOK(stream->Look(stream, &lookBuf, size)); 48 | memcpy(buf, lookBuf, *size); 49 | return stream->Skip(stream, *size); 50 | } 51 | 52 | SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType) 53 | { 54 | while (size != 0) 55 | { 56 | size_t processed = size; 57 | RINOK(stream->Read(stream, buf, &processed)); 58 | if (processed == 0) 59 | return errorType; 60 | buf = (void *)((Byte *)buf + processed); 61 | size -= processed; 62 | } 63 | return SZ_OK; 64 | } 65 | 66 | SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size) 67 | { 68 | return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF); 69 | } 70 | 71 | static SRes LookToRead_Look_Lookahead(void *pp, void **buf, size_t *size) 72 | { 73 | SRes res = SZ_OK; 74 | CLookToRead *p = (CLookToRead *)pp; 75 | size_t size2 = p->size - p->pos; 76 | if (size2 == 0 && *size > 0) 77 | { 78 | p->pos = 0; 79 | size2 = LookToRead_BUF_SIZE; 80 | res = p->realStream->Read(p->realStream, p->buf, &size2); 81 | p->size = size2; 82 | } 83 | if (size2 < *size) 84 | *size = size2; 85 | *buf = p->buf + p->pos; 86 | return res; 87 | } 88 | 89 | static SRes LookToRead_Look_Exact(void *pp, void **buf, size_t *size) 90 | { 91 | SRes res = SZ_OK; 92 | CLookToRead *p = (CLookToRead *)pp; 93 | size_t size2 = p->size - p->pos; 94 | if (size2 == 0 && *size > 0) 95 | { 96 | p->pos = 0; 97 | if (*size > LookToRead_BUF_SIZE) 98 | *size = LookToRead_BUF_SIZE; 99 | res = p->realStream->Read(p->realStream, p->buf, size); 100 | size2 = p->size = *size; 101 | } 102 | if (size2 < *size) 103 | *size = size2; 104 | *buf = p->buf + p->pos; 105 | return res; 106 | } 107 | 108 | static SRes LookToRead_Skip(void *pp, size_t offset) 109 | { 110 | CLookToRead *p = (CLookToRead *)pp; 111 | p->pos += offset; 112 | return SZ_OK; 113 | } 114 | 115 | static SRes LookToRead_Read(void *pp, void *buf, size_t *size) 116 | { 117 | CLookToRead *p = (CLookToRead *)pp; 118 | size_t rem = p->size - p->pos; 119 | if (rem == 0) 120 | return p->realStream->Read(p->realStream, buf, size); 121 | if (rem > *size) 122 | rem = *size; 123 | memcpy(buf, p->buf + p->pos, rem); 124 | p->pos += rem; 125 | *size = rem; 126 | return SZ_OK; 127 | } 128 | 129 | static SRes LookToRead_Seek(void *pp, Int64 *pos, ESzSeek origin) 130 | { 131 | CLookToRead *p = (CLookToRead *)pp; 132 | p->pos = p->size = 0; 133 | return p->realStream->Seek(p->realStream, pos, origin); 134 | } 135 | 136 | void LookToRead_CreateVTable(CLookToRead *p, int lookahead) 137 | { 138 | p->s.Look = lookahead ? 139 | LookToRead_Look_Lookahead : 140 | LookToRead_Look_Exact; 141 | p->s.Skip = LookToRead_Skip; 142 | p->s.Read = LookToRead_Read; 143 | p->s.Seek = LookToRead_Seek; 144 | } 145 | 146 | void LookToRead_Init(CLookToRead *p) 147 | { 148 | p->pos = p->size = 0; 149 | } 150 | 151 | static SRes SecToLook_Read(void *pp, void *buf, size_t *size) 152 | { 153 | CSecToLook *p = (CSecToLook *)pp; 154 | return LookInStream_LookRead(p->realStream, buf, size); 155 | } 156 | 157 | void SecToLook_CreateVTable(CSecToLook *p) 158 | { 159 | p->s.Read = SecToLook_Read; 160 | } 161 | 162 | static SRes SecToRead_Read(void *pp, void *buf, size_t *size) 163 | { 164 | CSecToRead *p = (CSecToRead *)pp; 165 | return p->realStream->Read(p->realStream, buf, size); 166 | } 167 | 168 | void SecToRead_CreateVTable(CSecToRead *p) 169 | { 170 | p->s.Read = SecToRead_Read; 171 | } 172 | 173 | } 174 | -------------------------------------------------------------------------------- /crnlib/lzma_7zVersion.h: -------------------------------------------------------------------------------- 1 | #define MY_VER_MAJOR 4 2 | #define MY_VER_MINOR 63 3 | #define MY_VER_BUILD 0 4 | #define MY_VERSION "4.63" 5 | #define MY_DATE "2008-12-31" 6 | #define MY_COPYRIGHT ": Igor Pavlov : Public domain" 7 | #define MY_VERSION_COPYRIGHT_DATE MY_VERSION " " MY_COPYRIGHT " : " MY_DATE 8 | -------------------------------------------------------------------------------- /crnlib/lzma_Alloc.cpp: -------------------------------------------------------------------------------- 1 | /* Alloc.c -- Memory allocation functions 2 | 2008-09-24 3 | Igor Pavlov 4 | Public domain */ 5 | #include "crn_core.h" 6 | #ifdef _WIN32 7 | #include 8 | #endif 9 | #include 10 | 11 | #include "lzma_Alloc.h" 12 | 13 | namespace crnlib { 14 | 15 | /* #define _SZ_ALLOC_DEBUG */ 16 | 17 | /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ 18 | #ifdef _SZ_ALLOC_DEBUG 19 | #include 20 | int g_allocCount = 0; 21 | int g_allocCountMid = 0; 22 | int g_allocCountBig = 0; 23 | #endif 24 | 25 | void *MyAlloc(size_t size) 26 | { 27 | if (size == 0) 28 | return 0; 29 | #ifdef _SZ_ALLOC_DEBUG 30 | { 31 | void *p = crnlib::crnlib_malloc(size); 32 | fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_allocCount++, (unsigned)p); 33 | return p; 34 | } 35 | #else 36 | return crnlib::crnlib_malloc(size); 37 | #endif 38 | } 39 | 40 | void MyFree(void *address) 41 | { 42 | #ifdef _SZ_ALLOC_DEBUG 43 | if (address != 0) 44 | fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsigned)address); 45 | #endif 46 | crnlib::crnlib_free(address); 47 | } 48 | 49 | #ifdef _WIN32 50 | 51 | void *MidAlloc(size_t size) 52 | { 53 | if (size == 0) 54 | return 0; 55 | #ifdef _SZ_ALLOC_DEBUG 56 | fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++); 57 | #endif 58 | return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); 59 | } 60 | 61 | void MidFree(void *address) 62 | { 63 | #ifdef _SZ_ALLOC_DEBUG 64 | if (address != 0) 65 | fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid); 66 | #endif 67 | if (address == 0) 68 | return; 69 | VirtualFree(address, 0, MEM_RELEASE); 70 | } 71 | 72 | #ifndef MEM_LARGE_PAGES 73 | #undef _7ZIP_LARGE_PAGES 74 | #endif 75 | 76 | #ifdef _7ZIP_LARGE_PAGES 77 | SIZE_T g_LargePageSize = 0; 78 | typedef SIZE_T (WINAPI *GetLargePageMinimumP)(); 79 | #endif 80 | 81 | void SetLargePageSize() 82 | { 83 | #ifdef _7ZIP_LARGE_PAGES 84 | SIZE_T size = 0; 85 | GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP) 86 | GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum"); 87 | if (largePageMinimum == 0) 88 | return; 89 | size = largePageMinimum(); 90 | if (size == 0 || (size & (size - 1)) != 0) 91 | return; 92 | g_LargePageSize = size; 93 | #endif 94 | } 95 | 96 | 97 | void *BigAlloc(size_t size) 98 | { 99 | if (size == 0) 100 | return 0; 101 | #ifdef _SZ_ALLOC_DEBUG 102 | fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++); 103 | #endif 104 | 105 | #ifdef _7ZIP_LARGE_PAGES 106 | if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18)) 107 | { 108 | void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)), 109 | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); 110 | if (res != 0) 111 | return res; 112 | } 113 | #endif 114 | return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); 115 | } 116 | 117 | void BigFree(void *address) 118 | { 119 | #ifdef _SZ_ALLOC_DEBUG 120 | if (address != 0) 121 | fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig); 122 | #endif 123 | 124 | if (address == 0) 125 | return; 126 | VirtualFree(address, 0, MEM_RELEASE); 127 | } 128 | 129 | #endif 130 | 131 | } 132 | -------------------------------------------------------------------------------- /crnlib/lzma_Alloc.h: -------------------------------------------------------------------------------- 1 | /* Alloc.h -- Memory allocation functions 2 | 2008-03-13 3 | Igor Pavlov 4 | Public domain */ 5 | 6 | #ifndef __COMMON_ALLOC_H 7 | #define __COMMON_ALLOC_H 8 | 9 | #include 10 | 11 | namespace crnlib { 12 | 13 | void *MyAlloc(size_t size); 14 | void MyFree(void *address); 15 | 16 | #ifdef _WIN32 17 | 18 | void SetLargePageSize(); 19 | 20 | void *MidAlloc(size_t size); 21 | void MidFree(void *address); 22 | void *BigAlloc(size_t size); 23 | void BigFree(void *address); 24 | 25 | #else 26 | 27 | #define MidAlloc(size) MyAlloc(size) 28 | #define MidFree(address) MyFree(address) 29 | #define BigAlloc(size) MyAlloc(size) 30 | #define BigFree(address) MyFree(address) 31 | 32 | #endif 33 | 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /crnlib/lzma_Bcj2.cpp: -------------------------------------------------------------------------------- 1 | /* Bcj2.c -- Converter for x86 code (BCJ2) 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | #include "crn_core.h" 4 | #include "lzma_Bcj2.h" 5 | 6 | namespace crnlib { 7 | 8 | #ifdef _LZMA_PROB32 9 | #define CProb UInt32 10 | #else 11 | #define CProb UInt16 12 | #endif 13 | 14 | #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80) 15 | #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1)) 16 | 17 | #define kNumTopBits 24 18 | #define kTopValue ((UInt32)1 << kNumTopBits) 19 | 20 | #define kNumBitModelTotalBits 11 21 | #define kBitModelTotal (1 << kNumBitModelTotalBits) 22 | #define kNumMoveBits 5 23 | 24 | #define RC_READ_BYTE (*buffer++) 25 | #define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; } 26 | #define RC_INIT2 code = 0; range = 0xFFFFFFFF; \ 27 | { int i; for (i = 0; i < 5; i++) { RC_TEST; code = (code << 8) | RC_READ_BYTE; }} 28 | 29 | #define NORMALIZE if (range < kTopValue) { RC_TEST; range <<= 8; code = (code << 8) | RC_READ_BYTE; } 30 | 31 | #define IF_BIT_0(p) ttt = *(p); bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound) 32 | #define UPDATE_0(p) range = bound; *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); NORMALIZE; 33 | #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); NORMALIZE; 34 | 35 | int Bcj2_Decode( 36 | const Byte *buf0, SizeT size0, 37 | const Byte *buf1, SizeT size1, 38 | const Byte *buf2, SizeT size2, 39 | const Byte *buf3, SizeT size3, 40 | Byte *outBuf, SizeT outSize) 41 | { 42 | CProb p[256 + 2]; 43 | SizeT inPos = 0, outPos = 0; 44 | 45 | const Byte *buffer, *bufferLim; 46 | UInt32 range, code; 47 | Byte prevByte = 0; 48 | 49 | unsigned int i; 50 | for (i = 0; i < sizeof(p) / sizeof(p[0]); i++) 51 | p[i] = kBitModelTotal >> 1; 52 | 53 | buffer = buf3; 54 | bufferLim = buffer + size3; 55 | RC_INIT2 56 | 57 | if (outSize == 0) 58 | return SZ_OK; 59 | 60 | for (;;) 61 | { 62 | Byte b; 63 | CProb *prob; 64 | UInt32 bound; 65 | UInt32 ttt; 66 | 67 | SizeT limit = size0 - inPos; 68 | if (outSize - outPos < limit) 69 | limit = outSize - outPos; 70 | while (limit != 0) 71 | { 72 | Byte b = buf0[inPos]; 73 | outBuf[outPos++] = b; 74 | if (IsJ(prevByte, b)) 75 | break; 76 | inPos++; 77 | prevByte = b; 78 | limit--; 79 | } 80 | 81 | if (limit == 0 || outPos == outSize) 82 | break; 83 | 84 | b = buf0[inPos++]; 85 | 86 | if (b == 0xE8) 87 | prob = p + prevByte; 88 | else if (b == 0xE9) 89 | prob = p + 256; 90 | else 91 | prob = p + 257; 92 | 93 | IF_BIT_0(prob) 94 | { 95 | UPDATE_0(prob) 96 | prevByte = b; 97 | } 98 | else 99 | { 100 | UInt32 dest; 101 | const Byte *v; 102 | UPDATE_1(prob) 103 | if (b == 0xE8) 104 | { 105 | v = buf1; 106 | if (size1 < 4) 107 | return SZ_ERROR_DATA; 108 | buf1 += 4; 109 | size1 -= 4; 110 | } 111 | else 112 | { 113 | v = buf2; 114 | if (size2 < 4) 115 | return SZ_ERROR_DATA; 116 | buf2 += 4; 117 | size2 -= 4; 118 | } 119 | dest = (((UInt32)v[0] << 24) | ((UInt32)v[1] << 16) | 120 | ((UInt32)v[2] << 8) | ((UInt32)v[3])) - ((UInt32)outPos + 4); 121 | outBuf[outPos++] = (Byte)dest; 122 | if (outPos == outSize) 123 | break; 124 | outBuf[outPos++] = (Byte)(dest >> 8); 125 | if (outPos == outSize) 126 | break; 127 | outBuf[outPos++] = (Byte)(dest >> 16); 128 | if (outPos == outSize) 129 | break; 130 | outBuf[outPos++] = prevByte = (Byte)(dest >> 24); 131 | } 132 | } 133 | return (outPos == outSize) ? SZ_OK : SZ_ERROR_DATA; 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /crnlib/lzma_Bcj2.h: -------------------------------------------------------------------------------- 1 | /* Bcj2.h -- Converter for x86 code (BCJ2) 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __BCJ2_H 5 | #define __BCJ2_H 6 | 7 | #include "lzma_Types.h" 8 | 9 | namespace crnlib { 10 | 11 | /* 12 | Conditions: 13 | outSize <= FullOutputSize, 14 | where FullOutputSize is full size of output stream of x86_2 filter. 15 | 16 | If buf0 overlaps outBuf, there are two required conditions: 17 | 1) (buf0 >= outBuf) 18 | 2) (buf0 + size0 >= outBuf + FullOutputSize). 19 | 20 | Returns: 21 | SZ_OK 22 | SZ_ERROR_DATA - Data error 23 | */ 24 | 25 | int Bcj2_Decode( 26 | const Byte *buf0, SizeT size0, 27 | const Byte *buf1, SizeT size1, 28 | const Byte *buf2, SizeT size2, 29 | const Byte *buf3, SizeT size3, 30 | Byte *outBuf, SizeT outSize); 31 | 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /crnlib/lzma_Bra.cpp: -------------------------------------------------------------------------------- 1 | /* Bra.c -- Converters for RISC code 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | #include "crn_core.h" 4 | #include "lzma_Bra.h" 5 | 6 | namespace crnlib { 7 | 8 | SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) 9 | { 10 | SizeT i; 11 | if (size < 4) 12 | return 0; 13 | size -= 4; 14 | ip += 8; 15 | for (i = 0; i <= size; i += 4) 16 | { 17 | if (data[i + 3] == 0xEB) 18 | { 19 | UInt32 dest; 20 | UInt32 src = ((UInt32)data[i + 2] << 16) | ((UInt32)data[i + 1] << 8) | (data[i + 0]); 21 | src <<= 2; 22 | if (encoding) 23 | dest = ip + (UInt32)i + src; 24 | else 25 | dest = src - (ip + (UInt32)i); 26 | dest >>= 2; 27 | data[i + 2] = (Byte)(dest >> 16); 28 | data[i + 1] = (Byte)(dest >> 8); 29 | data[i + 0] = (Byte)dest; 30 | } 31 | } 32 | return i; 33 | } 34 | 35 | SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) 36 | { 37 | SizeT i; 38 | if (size < 4) 39 | return 0; 40 | size -= 4; 41 | ip += 4; 42 | for (i = 0; i <= size; i += 2) 43 | { 44 | if ((data[i + 1] & 0xF8) == 0xF0 && 45 | (data[i + 3] & 0xF8) == 0xF8) 46 | { 47 | UInt32 dest; 48 | UInt32 src = 49 | (((UInt32)data[i + 1] & 0x7) << 19) | 50 | ((UInt32)data[i + 0] << 11) | 51 | (((UInt32)data[i + 3] & 0x7) << 8) | 52 | (data[i + 2]); 53 | 54 | src <<= 1; 55 | if (encoding) 56 | dest = ip + (UInt32)i + src; 57 | else 58 | dest = src - (ip + (UInt32)i); 59 | dest >>= 1; 60 | 61 | data[i + 1] = (Byte)(0xF0 | ((dest >> 19) & 0x7)); 62 | data[i + 0] = (Byte)(dest >> 11); 63 | data[i + 3] = (Byte)(0xF8 | ((dest >> 8) & 0x7)); 64 | data[i + 2] = (Byte)dest; 65 | i += 2; 66 | } 67 | } 68 | return i; 69 | } 70 | 71 | SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) 72 | { 73 | SizeT i; 74 | if (size < 4) 75 | return 0; 76 | size -= 4; 77 | for (i = 0; i <= size; i += 4) 78 | { 79 | if ((data[i] >> 2) == 0x12 && (data[i + 3] & 3) == 1) 80 | { 81 | UInt32 src = ((UInt32)(data[i + 0] & 3) << 24) | 82 | ((UInt32)data[i + 1] << 16) | 83 | ((UInt32)data[i + 2] << 8) | 84 | ((UInt32)data[i + 3] & (~3)); 85 | 86 | UInt32 dest; 87 | if (encoding) 88 | dest = ip + (UInt32)i + src; 89 | else 90 | dest = src - (ip + (UInt32)i); 91 | data[i + 0] = (Byte)(0x48 | ((dest >> 24) & 0x3)); 92 | data[i + 1] = (Byte)(dest >> 16); 93 | data[i + 2] = (Byte)(dest >> 8); 94 | data[i + 3] &= 0x3; 95 | data[i + 3] |= dest; 96 | } 97 | } 98 | return i; 99 | } 100 | 101 | SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) 102 | { 103 | UInt32 i; 104 | if (size < 4) 105 | return 0; 106 | size -= 4; 107 | for (i = 0; i <= size; i += 4) 108 | { 109 | if ( ((data[i] == 0x40) && ((data[i + 1] & 0xC0) == 0x00)) || 110 | ((data[i] == 0x7F) && ((data[i + 1] & 0xC0) == 0xC0)) ) 111 | { 112 | UInt32 src = 113 | ((UInt32)data[i + 0] << 24) | 114 | ((UInt32)data[i + 1] << 16) | 115 | ((UInt32)data[i + 2] << 8) | 116 | ((UInt32)data[i + 3]); 117 | UInt32 dest; 118 | 119 | src <<= 2; 120 | if (encoding) 121 | dest = ip + i + src; 122 | else 123 | dest = src - (ip + i); 124 | dest >>= 2; 125 | 126 | dest = (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF) | (dest & 0x3FFFFF) | 0x40000000; 127 | 128 | data[i + 0] = (Byte)(dest >> 24); 129 | data[i + 1] = (Byte)(dest >> 16); 130 | data[i + 2] = (Byte)(dest >> 8); 131 | data[i + 3] = (Byte)dest; 132 | } 133 | } 134 | return i; 135 | } 136 | 137 | } 138 | -------------------------------------------------------------------------------- /crnlib/lzma_Bra.h: -------------------------------------------------------------------------------- 1 | /* Bra.h -- Branch converters for executables 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __BRA_H 5 | #define __BRA_H 6 | 7 | #include "lzma_Types.h" 8 | 9 | namespace crnlib { 10 | 11 | /* 12 | These functions convert relative addresses to absolute addresses 13 | in CALL instructions to increase the compression ratio. 14 | 15 | In: 16 | data - data buffer 17 | size - size of data 18 | ip - current virtual Instruction Pinter (IP) value 19 | state - state variable for x86 converter 20 | encoding - 0 (for decoding), 1 (for encoding) 21 | 22 | Out: 23 | state - state variable for x86 converter 24 | 25 | Returns: 26 | The number of processed bytes. If you call these functions with multiple calls, 27 | you must start next call with first byte after block of processed bytes. 28 | 29 | Type Endian Alignment LookAhead 30 | 31 | x86 little 1 4 32 | ARMT little 2 2 33 | ARM little 4 0 34 | PPC big 4 0 35 | SPARC big 4 0 36 | IA64 little 16 0 37 | 38 | size must be >= Alignment + LookAhead, if it's not last block. 39 | If (size < Alignment + LookAhead), converter returns 0. 40 | 41 | Example: 42 | 43 | UInt32 ip = 0; 44 | for () 45 | { 46 | ; size must be >= Alignment + LookAhead, if it's not last block 47 | SizeT processed = Convert(data, size, ip, 1); 48 | data += processed; 49 | size -= processed; 50 | ip += processed; 51 | } 52 | */ 53 | 54 | #define x86_Convert_Init(state) { state = 0; } 55 | SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding); 56 | SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); 57 | SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); 58 | SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); 59 | SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); 60 | SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); 61 | 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /crnlib/lzma_Bra86.cpp: -------------------------------------------------------------------------------- 1 | /* Bra86.c -- Converter for x86 code (BCJ) 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | #include "crn_core.h" 4 | #include "lzma_Bra.h" 5 | 6 | namespace crnlib { 7 | 8 | #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF) 9 | 10 | const Byte kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0}; 11 | const Byte kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3}; 12 | 13 | SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding) 14 | { 15 | SizeT bufferPos = 0, prevPosT; 16 | UInt32 prevMask = *state & 0x7; 17 | if (size < 5) 18 | return 0; 19 | ip += 5; 20 | prevPosT = (SizeT)0 - 1; 21 | 22 | for (;;) 23 | { 24 | Byte *p = data + bufferPos; 25 | Byte *limit = data + size - 4; 26 | for (; p < limit; p++) 27 | if ((*p & 0xFE) == 0xE8) 28 | break; 29 | bufferPos = (SizeT)(p - data); 30 | if (p >= limit) 31 | break; 32 | prevPosT = bufferPos - prevPosT; 33 | if (prevPosT > 3) 34 | prevMask = 0; 35 | else 36 | { 37 | prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7; 38 | if (prevMask != 0) 39 | { 40 | Byte b = p[4 - kMaskToBitNumber[prevMask]]; 41 | if (!kMaskToAllowedStatus[prevMask] || Test86MSByte(b)) 42 | { 43 | prevPosT = bufferPos; 44 | prevMask = ((prevMask << 1) & 0x7) | 1; 45 | bufferPos++; 46 | continue; 47 | } 48 | } 49 | } 50 | prevPosT = bufferPos; 51 | 52 | if (Test86MSByte(p[4])) 53 | { 54 | UInt32 src = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]); 55 | UInt32 dest; 56 | for (;;) 57 | { 58 | Byte b; 59 | int index; 60 | if (encoding) 61 | dest = (ip + (UInt32)bufferPos) + src; 62 | else 63 | dest = src - (ip + (UInt32)bufferPos); 64 | if (prevMask == 0) 65 | break; 66 | index = kMaskToBitNumber[prevMask] * 8; 67 | b = (Byte)(dest >> (24 - index)); 68 | if (!Test86MSByte(b)) 69 | break; 70 | src = dest ^ ((1 << (32 - index)) - 1); 71 | } 72 | p[4] = (Byte)(~(((dest >> 24) & 1) - 1)); 73 | p[3] = (Byte)(dest >> 16); 74 | p[2] = (Byte)(dest >> 8); 75 | p[1] = (Byte)dest; 76 | bufferPos += 5; 77 | } 78 | else 79 | { 80 | prevMask = ((prevMask << 1) & 0x7) | 1; 81 | bufferPos++; 82 | } 83 | } 84 | prevPosT = bufferPos - prevPosT; 85 | *state = ((prevPosT > 3) ? 0 : ((prevMask << ((int)prevPosT - 1)) & 0x7)); 86 | return bufferPos; 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /crnlib/lzma_BraIA64.cpp: -------------------------------------------------------------------------------- 1 | /* BraIA64.c -- Converter for IA-64 code 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | #include "crn_core.h" 4 | #include "lzma_Bra.h" 5 | 6 | namespace crnlib { 7 | 8 | static const Byte kBranchTable[32] = 9 | { 10 | 0, 0, 0, 0, 0, 0, 0, 0, 11 | 0, 0, 0, 0, 0, 0, 0, 0, 12 | 4, 4, 6, 6, 0, 0, 7, 7, 13 | 4, 4, 0, 0, 4, 4, 0, 0 14 | }; 15 | 16 | SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) 17 | { 18 | SizeT i; 19 | if (size < 16) 20 | return 0; 21 | size -= 16; 22 | for (i = 0; i <= size; i += 16) 23 | { 24 | UInt32 instrTemplate = data[i] & 0x1F; 25 | UInt32 mask = kBranchTable[instrTemplate]; 26 | UInt32 bitPos = 5; 27 | int slot; 28 | for (slot = 0; slot < 3; slot++, bitPos += 41) 29 | { 30 | UInt32 bytePos, bitRes; 31 | UInt64 instruction, instNorm; 32 | int j; 33 | if (((mask >> slot) & 1) == 0) 34 | continue; 35 | bytePos = (bitPos >> 3); 36 | bitRes = bitPos & 0x7; 37 | instruction = 0; 38 | for (j = 0; j < 6; j++) 39 | instruction += (UInt64)data[i + j + bytePos] << (8 * j); 40 | 41 | instNorm = instruction >> bitRes; 42 | if (((instNorm >> 37) & 0xF) == 0x5 && ((instNorm >> 9) & 0x7) == 0) 43 | { 44 | UInt32 src = (UInt32)((instNorm >> 13) & 0xFFFFF); 45 | UInt32 dest; 46 | src |= ((UInt32)(instNorm >> 36) & 1) << 20; 47 | 48 | src <<= 4; 49 | 50 | if (encoding) 51 | dest = ip + (UInt32)i + src; 52 | else 53 | dest = src - (ip + (UInt32)i); 54 | 55 | dest >>= 4; 56 | 57 | instNorm &= ~((UInt64)(0x8FFFFF) << 13); 58 | instNorm |= ((UInt64)(dest & 0xFFFFF) << 13); 59 | instNorm |= ((UInt64)(dest & 0x100000) << (36 - 20)); 60 | 61 | instruction &= (1 << bitRes) - 1; 62 | instruction |= (instNorm << bitRes); 63 | for (j = 0; j < 6; j++) 64 | data[i + j + bytePos] = (Byte)(instruction >> (8 * j)); 65 | } 66 | } 67 | } 68 | return i; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /crnlib/lzma_CpuArch.h: -------------------------------------------------------------------------------- 1 | /* CpuArch.h 2 | 2008-08-05 3 | Igor Pavlov 4 | Public domain */ 5 | 6 | #ifndef __CPUARCH_H 7 | #define __CPUARCH_H 8 | 9 | /* 10 | LITTLE_ENDIAN_UNALIGN means: 11 | 1) CPU is LITTLE_ENDIAN 12 | 2) it's allowed to make unaligned memory accesses 13 | if LITTLE_ENDIAN_UNALIGN is not defined, it means that we don't know 14 | about these properties of platform. 15 | */ 16 | 17 | #if defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) || defined(__i386__) || defined(__x86_64__) 18 | #define LITTLE_ENDIAN_UNALIGN 19 | #endif 20 | 21 | #ifdef LITTLE_ENDIAN_UNALIGN 22 | 23 | #define GetUi16(p) (*(const UInt16 *)(p)) 24 | #define GetUi32(p) (*(const UInt32 *)(p)) 25 | #define GetUi64(p) (*(const UInt64 *)(p)) 26 | #define SetUi32(p, d) *(UInt32 *)(p) = (d); 27 | 28 | #else 29 | 30 | #define GetUi16(p) (((const Byte *)(p))[0] | ((UInt16)((const Byte *)(p))[1] << 8)) 31 | 32 | #define GetUi32(p) ( \ 33 | ((const Byte *)(p))[0] | \ 34 | ((UInt32)((const Byte *)(p))[1] << 8) | \ 35 | ((UInt32)((const Byte *)(p))[2] << 16) | \ 36 | ((UInt32)((const Byte *)(p))[3] << 24)) 37 | 38 | #define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32)) 39 | 40 | #define SetUi32(p, d) { UInt32 _x_ = (d); \ 41 | ((Byte *)(p))[0] = (Byte)_x_; \ 42 | ((Byte *)(p))[1] = (Byte)(_x_ >> 8); \ 43 | ((Byte *)(p))[2] = (Byte)(_x_ >> 16); \ 44 | ((Byte *)(p))[3] = (Byte)(_x_ >> 24); } 45 | 46 | #endif 47 | 48 | #if defined(LITTLE_ENDIAN_UNALIGN) && defined(_WIN64) && (_MSC_VER >= 1300) 49 | 50 | #pragma intrinsic(_byteswap_ulong) 51 | #pragma intrinsic(_byteswap_uint64) 52 | #define GetBe32(p) _byteswap_ulong(*(const UInt32 *)(const Byte *)(p)) 53 | #define GetBe64(p) _byteswap_uint64(*(const UInt64 *)(const Byte *)(p)) 54 | 55 | #else 56 | 57 | #define GetBe32(p) ( \ 58 | ((UInt32)((const Byte *)(p))[0] << 24) | \ 59 | ((UInt32)((const Byte *)(p))[1] << 16) | \ 60 | ((UInt32)((const Byte *)(p))[2] << 8) | \ 61 | ((const Byte *)(p))[3] ) 62 | 63 | #define GetBe64(p) (((UInt64)GetBe32(p) << 32) | GetBe32(((const Byte *)(p)) + 4)) 64 | 65 | #endif 66 | 67 | #define GetBe16(p) (((UInt16)((const Byte *)(p))[0] << 8) | ((const Byte *)(p))[1]) 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /crnlib/lzma_LzFind.h: -------------------------------------------------------------------------------- 1 | /* LzFind.h -- Match finder for LZ algorithms 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZFIND_H 5 | #define __LZFIND_H 6 | 7 | #include "lzma_Types.h" 8 | 9 | namespace crnlib { 10 | 11 | typedef UInt32 CLzRef; 12 | 13 | typedef struct _CMatchFinder 14 | { 15 | Byte *buffer; 16 | UInt32 pos; 17 | UInt32 posLimit; 18 | UInt32 streamPos; 19 | UInt32 lenLimit; 20 | 21 | UInt32 cyclicBufferPos; 22 | UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */ 23 | 24 | UInt32 matchMaxLen; 25 | CLzRef *hash; 26 | CLzRef *son; 27 | UInt32 hashMask; 28 | UInt32 cutValue; 29 | 30 | Byte *bufferBase; 31 | ISeqInStream *stream; 32 | int streamEndWasReached; 33 | 34 | UInt32 blockSize; 35 | UInt32 keepSizeBefore; 36 | UInt32 keepSizeAfter; 37 | 38 | UInt32 numHashBytes; 39 | int directInput; 40 | int btMode; 41 | /* int skipModeBits; */ 42 | int bigHash; 43 | UInt32 historySize; 44 | UInt32 fixedHashSize; 45 | UInt32 hashSizeSum; 46 | UInt32 numSons; 47 | SRes result; 48 | UInt32 crc[256]; 49 | } CMatchFinder; 50 | 51 | #define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer) 52 | #define Inline_MatchFinder_GetIndexByte(p, index) ((p)->buffer[(Int32)(index)]) 53 | 54 | #define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos) 55 | 56 | int MatchFinder_NeedMove(CMatchFinder *p); 57 | Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p); 58 | void MatchFinder_MoveBlock(CMatchFinder *p); 59 | void MatchFinder_ReadIfRequired(CMatchFinder *p); 60 | 61 | void MatchFinder_Construct(CMatchFinder *p); 62 | 63 | /* Conditions: 64 | historySize <= 3 GB 65 | keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB 66 | */ 67 | int MatchFinder_Create(CMatchFinder *p, UInt32 historySize, 68 | UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter, 69 | ISzAlloc *alloc); 70 | void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc); 71 | void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems); 72 | void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue); 73 | 74 | UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *buffer, CLzRef *son, 75 | UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue, 76 | UInt32 *distances, UInt32 maxLen); 77 | 78 | /* 79 | Conditions: 80 | Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func. 81 | Mf_GetPointerToCurrentPos_Func's result must be used only before any other function 82 | */ 83 | 84 | typedef void (*Mf_Init_Func)(void *object); 85 | typedef Byte (*Mf_GetIndexByte_Func)(void *object, Int32 index); 86 | typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object); 87 | typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object); 88 | typedef UInt32 (*Mf_GetMatches_Func)(void *object, UInt32 *distances); 89 | typedef void (*Mf_Skip_Func)(void *object, UInt32); 90 | 91 | typedef struct _IMatchFinder 92 | { 93 | Mf_Init_Func Init; 94 | Mf_GetIndexByte_Func GetIndexByte; 95 | Mf_GetNumAvailableBytes_Func GetNumAvailableBytes; 96 | Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos; 97 | Mf_GetMatches_Func GetMatches; 98 | Mf_Skip_Func Skip; 99 | } IMatchFinder; 100 | 101 | void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable); 102 | 103 | void MatchFinder_Init(CMatchFinder *p); 104 | UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances); 105 | UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances); 106 | void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num); 107 | void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num); 108 | 109 | } 110 | 111 | #endif 112 | -------------------------------------------------------------------------------- /crnlib/lzma_LzFindMt.h: -------------------------------------------------------------------------------- 1 | /* LzFindMt.h -- multithreaded Match finder for LZ algorithms 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZFINDMT_H 5 | #define __LZFINDMT_H 6 | 7 | #include "lzma_Threads.h" 8 | #include "lzma_LzFind.h" 9 | 10 | namespace crnlib { 11 | 12 | #define kMtHashBlockSize (1 << 13) 13 | #define kMtHashNumBlocks (1 << 3) 14 | #define kMtHashNumBlocksMask (kMtHashNumBlocks - 1) 15 | 16 | #define kMtBtBlockSize (1 << 14) 17 | #define kMtBtNumBlocks (1 << 6) 18 | #define kMtBtNumBlocksMask (kMtBtNumBlocks - 1) 19 | 20 | typedef struct _CMtSync 21 | { 22 | Bool wasCreated; 23 | Bool needStart; 24 | Bool exit; 25 | Bool stopWriting; 26 | 27 | CThread thread; 28 | CAutoResetEvent canStart; 29 | CAutoResetEvent wasStarted; 30 | CAutoResetEvent wasStopped; 31 | CSemaphore freeSemaphore; 32 | CSemaphore filledSemaphore; 33 | Bool csWasInitialized; 34 | Bool csWasEntered; 35 | CCriticalSection cs; 36 | UInt32 numProcessedBlocks; 37 | } CMtSync; 38 | 39 | typedef UInt32 * (*Mf_Mix_Matches)(void *p, UInt32 matchMinPos, UInt32 *distances); 40 | 41 | /* kMtCacheLineDummy must be >= size_of_CPU_cache_line */ 42 | #define kMtCacheLineDummy 128 43 | 44 | typedef void (*Mf_GetHeads)(const Byte *buffer, UInt32 pos, 45 | UInt32 *hash, UInt32 hashMask, UInt32 *heads, UInt32 numHeads, const UInt32 *crc); 46 | 47 | typedef struct _CMatchFinderMt 48 | { 49 | /* LZ */ 50 | const Byte *pointerToCurPos; 51 | UInt32 *btBuf; 52 | UInt32 btBufPos; 53 | UInt32 btBufPosLimit; 54 | UInt32 lzPos; 55 | UInt32 btNumAvailBytes; 56 | 57 | UInt32 *hash; 58 | UInt32 fixedHashSize; 59 | UInt32 historySize; 60 | const UInt32 *crc; 61 | 62 | Mf_Mix_Matches MixMatchesFunc; 63 | 64 | /* LZ + BT */ 65 | CMtSync btSync; 66 | Byte btDummy[kMtCacheLineDummy]; 67 | 68 | /* BT */ 69 | UInt32 *hashBuf; 70 | UInt32 hashBufPos; 71 | UInt32 hashBufPosLimit; 72 | UInt32 hashNumAvail; 73 | 74 | CLzRef *son; 75 | UInt32 matchMaxLen; 76 | UInt32 numHashBytes; 77 | UInt32 pos; 78 | Byte *buffer; 79 | UInt32 cyclicBufferPos; 80 | UInt32 cyclicBufferSize; /* it must be historySize + 1 */ 81 | UInt32 cutValue; 82 | 83 | /* BT + Hash */ 84 | CMtSync hashSync; 85 | /* Byte hashDummy[kMtCacheLineDummy]; */ 86 | 87 | /* Hash */ 88 | Mf_GetHeads GetHeadsFunc; 89 | CMatchFinder *MatchFinder; 90 | } CMatchFinderMt; 91 | 92 | void MatchFinderMt_Construct(CMatchFinderMt *p); 93 | void MatchFinderMt_Destruct(CMatchFinderMt *p, ISzAlloc *alloc); 94 | SRes MatchFinderMt_Create(CMatchFinderMt *p, UInt32 historySize, UInt32 keepAddBufferBefore, 95 | UInt32 matchMaxLen, UInt32 keepAddBufferAfter, ISzAlloc *alloc); 96 | void MatchFinderMt_CreateVTable(CMatchFinderMt *p, IMatchFinder *vTable); 97 | void MatchFinderMt_ReleaseStream(CMatchFinderMt *p); 98 | 99 | } 100 | 101 | #endif 102 | -------------------------------------------------------------------------------- /crnlib/lzma_LzHash.h: -------------------------------------------------------------------------------- 1 | /* LzHash.h -- HASH functions for LZ algorithms 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZHASH_H 5 | #define __LZHASH_H 6 | 7 | #define kHash2Size (1 << 10) 8 | #define kHash3Size (1 << 16) 9 | #define kHash4Size (1 << 20) 10 | 11 | #define kFix3HashSize (kHash2Size) 12 | #define kFix4HashSize (kHash2Size + kHash3Size) 13 | #define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size) 14 | 15 | #define HASH2_CALC hashValue = cur[0] | ((UInt32)cur[1] << 8); 16 | 17 | #define HASH3_CALC { \ 18 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 19 | hash2Value = temp & (kHash2Size - 1); \ 20 | hashValue = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; } 21 | 22 | #define HASH4_CALC { \ 23 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 24 | hash2Value = temp & (kHash2Size - 1); \ 25 | hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \ 26 | hashValue = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & p->hashMask; } 27 | 28 | #define HASH5_CALC { \ 29 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 30 | hash2Value = temp & (kHash2Size - 1); \ 31 | hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \ 32 | hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)); \ 33 | hashValue = (hash4Value ^ (p->crc[cur[4]] << 3)) & p->hashMask; \ 34 | hash4Value &= (kHash4Size - 1); } 35 | 36 | /* #define HASH_ZIP_CALC hashValue = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */ 37 | #define HASH_ZIP_CALC hashValue = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF; 38 | 39 | 40 | #define MT_HASH2_CALC \ 41 | hash2Value = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1); 42 | 43 | #define MT_HASH3_CALC { \ 44 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 45 | hash2Value = temp & (kHash2Size - 1); \ 46 | hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); } 47 | 48 | #define MT_HASH4_CALC { \ 49 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 50 | hash2Value = temp & (kHash2Size - 1); \ 51 | hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \ 52 | hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /crnlib/lzma_LzmaEnc.h: -------------------------------------------------------------------------------- 1 | /* LzmaEnc.h -- LZMA Encoder 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZMAENC_H 5 | #define __LZMAENC_H 6 | 7 | #include "lzma_Types.h" 8 | 9 | namespace crnlib { 10 | 11 | #define LZMA_PROPS_SIZE 5 12 | 13 | typedef struct _CLzmaEncProps 14 | { 15 | int level; /* 0 <= level <= 9 */ 16 | UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version 17 | (1 << 12) <= dictSize <= (1 << 30) for 64-bit version 18 | default = (1 << 24) */ 19 | int lc; /* 0 <= lc <= 8, default = 3 */ 20 | int lp; /* 0 <= lp <= 4, default = 0 */ 21 | int pb; /* 0 <= pb <= 4, default = 2 */ 22 | int algo; /* 0 - fast, 1 - normal, default = 1 */ 23 | int fb; /* 5 <= fb <= 273, default = 32 */ 24 | int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */ 25 | int numHashBytes; /* 2, 3 or 4, default = 4 */ 26 | UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */ 27 | unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */ 28 | int numThreads; /* 1 or 2, default = 2 */ 29 | } CLzmaEncProps; 30 | 31 | void LzmaEncProps_Init(CLzmaEncProps *p); 32 | void LzmaEncProps_Normalize(CLzmaEncProps *p); 33 | UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2); 34 | 35 | 36 | /* ---------- CLzmaEncHandle Interface ---------- */ 37 | 38 | /* LzmaEnc_* functions can return the following exit codes: 39 | Returns: 40 | SZ_OK - OK 41 | SZ_ERROR_MEM - Memory allocation error 42 | SZ_ERROR_PARAM - Incorrect paramater in props 43 | SZ_ERROR_WRITE - Write callback error. 44 | SZ_ERROR_PROGRESS - some break from progress callback 45 | SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) 46 | */ 47 | 48 | typedef void * CLzmaEncHandle; 49 | 50 | CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc); 51 | void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig); 52 | SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props); 53 | SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size); 54 | SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream, 55 | ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig); 56 | SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 57 | int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig); 58 | 59 | /* ---------- One Call Interface ---------- */ 60 | 61 | /* LzmaEncode 62 | Return code: 63 | SZ_OK - OK 64 | SZ_ERROR_MEM - Memory allocation error 65 | SZ_ERROR_PARAM - Incorrect paramater 66 | SZ_ERROR_OUTPUT_EOF - output buffer overflow 67 | SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) 68 | */ 69 | 70 | SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 71 | const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, 72 | ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig); 73 | 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /crnlib/lzma_LzmaLib.cpp: -------------------------------------------------------------------------------- 1 | /* LzmaLib.c -- LZMA library wrapper 2 | 2008-08-05 3 | Igor Pavlov 4 | Public domain */ 5 | #include "crn_core.h" 6 | #include "lzma_LzmaEnc.h" 7 | #include "lzma_LzmaDec.h" 8 | #include "lzma_Alloc.h" 9 | #include "lzma_LzmaLib.h" 10 | 11 | namespace crnlib { 12 | 13 | static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); } 14 | static void SzFree(void *p, void *address) { p = p; MyFree(address); } 15 | static ISzAlloc g_Alloc = { SzAlloc, SzFree }; 16 | 17 | MY_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen, 18 | unsigned char *outProps, size_t *outPropsSize, 19 | int level, /* 0 <= level <= 9, default = 5 */ 20 | unsigned dictSize, /* use (1 << N) or (3 << N). 4 KB < dictSize <= 128 MB */ 21 | int lc, /* 0 <= lc <= 8, default = 3 */ 22 | int lp, /* 0 <= lp <= 4, default = 0 */ 23 | int pb, /* 0 <= pb <= 4, default = 2 */ 24 | int fb, /* 5 <= fb <= 273, default = 32 */ 25 | int numThreads /* 1 or 2, default = 2 */ 26 | ) 27 | { 28 | CLzmaEncProps props; 29 | LzmaEncProps_Init(&props); 30 | props.level = level; 31 | props.dictSize = dictSize; 32 | props.lc = lc; 33 | props.lp = lp; 34 | props.pb = pb; 35 | props.fb = fb; 36 | props.numThreads = numThreads; 37 | 38 | return LzmaEncode(dest, destLen, src, srcLen, &props, outProps, outPropsSize, 0, 39 | NULL, &g_Alloc, &g_Alloc); 40 | } 41 | 42 | 43 | MY_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t *srcLen, 44 | const unsigned char *props, size_t propsSize) 45 | { 46 | ELzmaStatus status; 47 | return LzmaDecode(dest, destLen, src, srcLen, props, (unsigned)propsSize, LZMA_FINISH_ANY, &status, &g_Alloc); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /crnlib/lzma_LzmaLib.h: -------------------------------------------------------------------------------- 1 | /* LzmaLib.h -- LZMA library interface 2 | 2008-08-05 3 | Igor Pavlov 4 | Public domain */ 5 | 6 | #ifndef __LZMALIB_H 7 | #define __LZMALIB_H 8 | 9 | #include "lzma_Types.h" 10 | 11 | namespace crnlib { 12 | 13 | #if 0 14 | #ifdef __cplusplus 15 | #define MY_EXTERN_C extern "C" 16 | #else 17 | #define MY_EXTERN_C extern 18 | #endif 19 | 20 | #define MY_STDAPI MY_EXTERN_C int MY_STD_CALL 21 | #else 22 | #define MY_STDAPI int MY_STD_CALL 23 | #endif 24 | 25 | #define LZMA_PROPS_SIZE 5 26 | 27 | /* 28 | RAM requirements for LZMA: 29 | for compression: (dictSize * 11.5 + 6 MB) + state_size 30 | for decompression: dictSize + state_size 31 | state_size = (4 + (1.5 << (lc + lp))) KB 32 | by default (lc=3, lp=0), state_size = 16 KB. 33 | 34 | LZMA properties (5 bytes) format 35 | Offset Size Description 36 | 0 1 lc, lp and pb in encoded form. 37 | 1 4 dictSize (little endian). 38 | */ 39 | 40 | /* 41 | LzmaCompress 42 | ------------ 43 | 44 | outPropsSize - 45 | In: the pointer to the size of outProps buffer; *outPropsSize = LZMA_PROPS_SIZE = 5. 46 | Out: the pointer to the size of written properties in outProps buffer; *outPropsSize = LZMA_PROPS_SIZE = 5. 47 | 48 | LZMA Encoder will use defult values for any parameter, if it is 49 | -1 for any from: level, loc, lp, pb, fb, numThreads 50 | 0 for dictSize 51 | 52 | level - compression level: 0 <= level <= 9; 53 | 54 | level dictSize algo fb 55 | 0: 16 KB 0 32 56 | 1: 64 KB 0 32 57 | 2: 256 KB 0 32 58 | 3: 1 MB 0 32 59 | 4: 4 MB 0 32 60 | 5: 16 MB 1 32 61 | 6: 32 MB 1 32 62 | 7+: 64 MB 1 64 63 | 64 | The default value for "level" is 5. 65 | 66 | algo = 0 means fast method 67 | algo = 1 means normal method 68 | 69 | dictSize - The dictionary size in bytes. The maximum value is 70 | 128 MB = (1 << 27) bytes for 32-bit version 71 | 1 GB = (1 << 30) bytes for 64-bit version 72 | The default value is 16 MB = (1 << 24) bytes. 73 | It's recommended to use the dictionary that is larger than 4 KB and 74 | that can be calculated as (1 << N) or (3 << N) sizes. 75 | 76 | lc - The number of literal context bits (high bits of previous literal). 77 | It can be in the range from 0 to 8. The default value is 3. 78 | Sometimes lc=4 gives the gain for big files. 79 | 80 | lp - The number of literal pos bits (low bits of current position for literals). 81 | It can be in the range from 0 to 4. The default value is 0. 82 | The lp switch is intended for periodical data when the period is equal to 2^lp. 83 | For example, for 32-bit (4 bytes) periodical data you can use lp=2. Often it's 84 | better to set lc=0, if you change lp switch. 85 | 86 | pb - The number of pos bits (low bits of current position). 87 | It can be in the range from 0 to 4. The default value is 2. 88 | The pb switch is intended for periodical data when the period is equal 2^pb. 89 | 90 | fb - Word size (the number of fast bytes). 91 | It can be in the range from 5 to 273. The default value is 32. 92 | Usually, a big number gives a little bit better compression ratio and 93 | slower compression process. 94 | 95 | numThreads - The number of thereads. 1 or 2. The default value is 2. 96 | Fast mode (algo = 0) can use only 1 thread. 97 | 98 | Out: 99 | destLen - processed output size 100 | Returns: 101 | SZ_OK - OK 102 | SZ_ERROR_MEM - Memory allocation error 103 | SZ_ERROR_PARAM - Incorrect paramater 104 | SZ_ERROR_OUTPUT_EOF - output buffer overflow 105 | SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) 106 | */ 107 | 108 | MY_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen, 109 | unsigned char *outProps, size_t *outPropsSize, /* *outPropsSize must be = 5 */ 110 | int level, /* 0 <= level <= 9, default = 5 */ 111 | unsigned dictSize, /* default = (1 << 24) */ 112 | int lc, /* 0 <= lc <= 8, default = 3 */ 113 | int lp, /* 0 <= lp <= 4, default = 0 */ 114 | int pb, /* 0 <= pb <= 4, default = 2 */ 115 | int fb, /* 5 <= fb <= 273, default = 32 */ 116 | int numThreads /* 1 or 2, default = 2 */ 117 | ); 118 | 119 | /* 120 | LzmaUncompress 121 | -------------- 122 | In: 123 | dest - output data 124 | destLen - output data size 125 | src - input data 126 | srcLen - input data size 127 | Out: 128 | destLen - processed output size 129 | srcLen - processed input size 130 | Returns: 131 | SZ_OK - OK 132 | SZ_ERROR_DATA - Data error 133 | SZ_ERROR_MEM - Memory allocation arror 134 | SZ_ERROR_UNSUPPORTED - Unsupported properties 135 | SZ_ERROR_INPUT_EOF - it needs more bytes in input buffer (src) 136 | */ 137 | 138 | MY_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, SizeT *srcLen, 139 | const unsigned char *props, size_t propsSize); 140 | 141 | #define LZMA_COMPRESS_FUNC_EXPORT "LzmaCompress" 142 | #define LZMA_UNCOMPRESS_FUNC_EXPORT "LzmaUncompress" 143 | 144 | } 145 | 146 | #endif 147 | -------------------------------------------------------------------------------- /crnlib/lzma_MyVersion.h: -------------------------------------------------------------------------------- 1 | #define MY_VER_MAJOR 4 2 | #define MY_VER_MINOR 63 3 | #define MY_VER_BUILD 0 4 | #define MY_VERSION "4.63" 5 | #define MY_7ZIP_VERSION "7-Zip 4.63" 6 | #define MY_DATE "2008-12-31" 7 | #define MY_COPYRIGHT "Copyright (c) 1999-2008 Igor Pavlov" 8 | #define MY_VERSION_COPYRIGHT_DATE MY_VERSION " " MY_COPYRIGHT " " MY_DATE 9 | -------------------------------------------------------------------------------- /crnlib/lzma_Threads.cpp: -------------------------------------------------------------------------------- 1 | /* Threads.c -- multithreading library 2 | 2008-08-05 3 | Igor Pavlov 4 | Public domain */ 5 | #include "crn_core.h" 6 | #include "lzma_Threads.h" 7 | #include 8 | 9 | namespace crnlib { 10 | 11 | static WRes GetError() 12 | { 13 | DWORD res = GetLastError(); 14 | return (res) ? (WRes)(res) : 1; 15 | } 16 | 17 | WRes HandleToWRes(HANDLE h) { return (h != 0) ? 0 : GetError(); } 18 | WRes BOOLToWRes(BOOL v) { return v ? 0 : GetError(); } 19 | 20 | static WRes MyCloseHandle(HANDLE *h) 21 | { 22 | if (*h != NULL) 23 | if (!CloseHandle(*h)) 24 | return GetError(); 25 | *h = NULL; 26 | return 0; 27 | } 28 | 29 | WRes Thread_Create(CThread *thread, THREAD_FUNC_RET_TYPE (THREAD_FUNC_CALL_TYPE *startAddress)(void *), LPVOID parameter) 30 | { 31 | unsigned threadId; /* Windows Me/98/95: threadId parameter may not be NULL in _beginthreadex/CreateThread functions */ 32 | thread->handle = 33 | /* CreateThread(0, 0, startAddress, parameter, 0, &threadId); */ 34 | (HANDLE)_beginthreadex(NULL, 0, startAddress, parameter, 0, &threadId); 35 | /* maybe we must use errno here, but probably GetLastError() is also OK. */ 36 | return HandleToWRes(thread->handle); 37 | } 38 | 39 | WRes WaitObject(HANDLE h) 40 | { 41 | return (WRes)WaitForSingleObject(h, INFINITE); 42 | } 43 | 44 | WRes Thread_Wait(CThread *thread) 45 | { 46 | if (thread->handle == NULL) 47 | return 1; 48 | return WaitObject(thread->handle); 49 | } 50 | 51 | WRes Thread_Close(CThread *thread) 52 | { 53 | return MyCloseHandle(&thread->handle); 54 | } 55 | 56 | WRes Event_Create(CEvent *p, BOOL manualReset, int initialSignaled) 57 | { 58 | p->handle = CreateEvent(NULL, manualReset, (initialSignaled ? TRUE : FALSE), NULL); 59 | return HandleToWRes(p->handle); 60 | } 61 | 62 | WRes ManualResetEvent_Create(CManualResetEvent *p, int initialSignaled) 63 | { return Event_Create(p, TRUE, initialSignaled); } 64 | WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *p) 65 | { return ManualResetEvent_Create(p, 0); } 66 | 67 | WRes AutoResetEvent_Create(CAutoResetEvent *p, int initialSignaled) 68 | { return Event_Create(p, FALSE, initialSignaled); } 69 | WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p) 70 | { return AutoResetEvent_Create(p, 0); } 71 | 72 | WRes Event_Set(CEvent *p) { return BOOLToWRes(SetEvent(p->handle)); } 73 | WRes Event_Reset(CEvent *p) { return BOOLToWRes(ResetEvent(p->handle)); } 74 | WRes Event_Wait(CEvent *p) { return WaitObject(p->handle); } 75 | WRes Event_Close(CEvent *p) { return MyCloseHandle(&p->handle); } 76 | 77 | 78 | WRes Semaphore_Create(CSemaphore *p, UInt32 initiallyCount, UInt32 maxCount) 79 | { 80 | p->handle = CreateSemaphore(NULL, (LONG)initiallyCount, (LONG)maxCount, NULL); 81 | return HandleToWRes(p->handle); 82 | } 83 | 84 | WRes Semaphore_Release(CSemaphore *p, LONG releaseCount, LONG *previousCount) 85 | { 86 | return BOOLToWRes(ReleaseSemaphore(p->handle, releaseCount, previousCount)); 87 | } 88 | WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 releaseCount) 89 | { 90 | return Semaphore_Release(p, (LONG)releaseCount, NULL); 91 | } 92 | WRes Semaphore_Release1(CSemaphore *p) 93 | { 94 | return Semaphore_ReleaseN(p, 1); 95 | } 96 | 97 | WRes Semaphore_Wait(CSemaphore *p) { return WaitObject(p->handle); } 98 | WRes Semaphore_Close(CSemaphore *p) { return MyCloseHandle(&p->handle); } 99 | 100 | WRes CriticalSection_Init(CCriticalSection *p) 101 | { 102 | #ifdef _MSC_VER 103 | /* InitializeCriticalSection can raise only STATUS_NO_MEMORY exception */ 104 | __try 105 | { 106 | InitializeCriticalSection(p); 107 | /* InitializeCriticalSectionAndSpinCount(p, 0); */ 108 | } 109 | __except (EXCEPTION_EXECUTE_HANDLER) { return 1; } 110 | #else 111 | InitializeCriticalSection(p); 112 | #endif 113 | return 0; 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /crnlib/lzma_Threads.h: -------------------------------------------------------------------------------- 1 | /* Threads.h -- multithreading library 2 | 2008-11-22 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __7Z_THRESDS_H 5 | #define __7Z_THRESDS_H 6 | 7 | #include "lzma_Types.h" 8 | 9 | namespace crnlib { 10 | 11 | typedef struct _CThread 12 | { 13 | HANDLE handle; 14 | } CThread; 15 | 16 | #define Thread_Construct(thread) (thread)->handle = NULL 17 | #define Thread_WasCreated(thread) ((thread)->handle != NULL) 18 | 19 | typedef unsigned THREAD_FUNC_RET_TYPE; 20 | #define THREAD_FUNC_CALL_TYPE MY_STD_CALL 21 | #define THREAD_FUNC_DECL THREAD_FUNC_RET_TYPE THREAD_FUNC_CALL_TYPE 22 | 23 | WRes Thread_Create(CThread *thread, THREAD_FUNC_RET_TYPE (THREAD_FUNC_CALL_TYPE *startAddress)(void *), LPVOID parameter); 24 | WRes Thread_Wait(CThread *thread); 25 | WRes Thread_Close(CThread *thread); 26 | 27 | typedef struct _CEvent 28 | { 29 | HANDLE handle; 30 | } CEvent; 31 | 32 | typedef CEvent CAutoResetEvent; 33 | typedef CEvent CManualResetEvent; 34 | 35 | #define Event_Construct(event) (event)->handle = NULL 36 | #define Event_IsCreated(event) ((event)->handle != NULL) 37 | 38 | WRes ManualResetEvent_Create(CManualResetEvent *event, int initialSignaled); 39 | WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *event); 40 | WRes AutoResetEvent_Create(CAutoResetEvent *event, int initialSignaled); 41 | WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *event); 42 | WRes Event_Set(CEvent *event); 43 | WRes Event_Reset(CEvent *event); 44 | WRes Event_Wait(CEvent *event); 45 | WRes Event_Close(CEvent *event); 46 | 47 | 48 | typedef struct _CSemaphore 49 | { 50 | HANDLE handle; 51 | } CSemaphore; 52 | 53 | #define Semaphore_Construct(p) (p)->handle = NULL 54 | 55 | WRes Semaphore_Create(CSemaphore *p, UInt32 initiallyCount, UInt32 maxCount); 56 | WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 num); 57 | WRes Semaphore_Release1(CSemaphore *p); 58 | WRes Semaphore_Wait(CSemaphore *p); 59 | WRes Semaphore_Close(CSemaphore *p); 60 | 61 | 62 | typedef CRITICAL_SECTION CCriticalSection; 63 | 64 | WRes CriticalSection_Init(CCriticalSection *p); 65 | #define CriticalSection_Delete(p) DeleteCriticalSection(p) 66 | #define CriticalSection_Enter(p) EnterCriticalSection(p) 67 | #define CriticalSection_Leave(p) LeaveCriticalSection(p) 68 | 69 | } 70 | 71 | #endif 72 | 73 | -------------------------------------------------------------------------------- /crunch/corpus_gen.h: -------------------------------------------------------------------------------- 1 | // File: corpus_gen.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_command_line_params.h" 5 | #include "crn_image.h" 6 | 7 | namespace crnlib 8 | { 9 | class corpus_gen 10 | { 11 | public: 12 | corpus_gen(); 13 | 14 | bool generate(const char* pCmd_line); 15 | 16 | private: 17 | void sort_blocks(image_u8& img); 18 | }; 19 | 20 | } // namespace crnlib 21 | -------------------------------------------------------------------------------- /crunch/corpus_test.h: -------------------------------------------------------------------------------- 1 | // File: corpus_test.h 2 | // See Copyright Notice and license at the end of inc/crnlib.h 3 | #pragma once 4 | #include "crn_command_line_params.h" 5 | #include "crn_image_utils.h" 6 | 7 | namespace crnlib 8 | { 9 | class corpus_tester 10 | { 11 | public: 12 | corpus_tester(); 13 | 14 | bool test(const char* pCmd_line); 15 | 16 | private: 17 | void print_comparative_metric_stats(const command_line_params& params, const crnlib::vector& stats1, const crnlib::vector& stats2, uint num_blocks_x, uint num_blocks_y); 18 | void print_metric_stats(const crnlib::vector& stats, uint num_blocks_x, uint num_blocks_y); 19 | 20 | image_u8 m_bad_block_img; 21 | uint m_next_bad_block_index; 22 | uint m_total_bad_block_files; 23 | 24 | void flush_bad_blocks(); 25 | void add_bad_block(image_u8& block); 26 | }; 27 | 28 | } // namespace crnlib 29 | -------------------------------------------------------------------------------- /crunch/crunch.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 58 | 59 | -------------------------------------------------------------------------------- /crunch/crunch_linux.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 83 | 84 | -------------------------------------------------------------------------------- /example2/timer.cpp: -------------------------------------------------------------------------------- 1 | // File: timer.cpp 2 | // A simple high-precision, platform independent timer class. 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "timer.h" 9 | 10 | #if defined(WIN32) 11 | #include 12 | #elif defined(_XBOX) 13 | #include 14 | #endif 15 | 16 | unsigned long long timer::g_init_ticks; 17 | unsigned long long timer::g_freq; 18 | double timer::g_inv_freq; 19 | 20 | #if defined(WIN32) || defined(_XBOX) 21 | inline void query_counter(timer_ticks *pTicks) 22 | { 23 | QueryPerformanceCounter(reinterpret_cast(pTicks)); 24 | } 25 | inline void query_counter_frequency(timer_ticks *pTicks) 26 | { 27 | QueryPerformanceFrequency(reinterpret_cast(pTicks)); 28 | } 29 | #elif defined(__GNUC__) 30 | #include 31 | inline void query_counter(timer_ticks *pTicks) 32 | { 33 | struct timeval cur_time; 34 | gettimeofday(&cur_time, NULL); 35 | *pTicks = static_cast(cur_time.tv_sec)*1000000ULL + static_cast(cur_time.tv_usec); 36 | } 37 | inline void query_counter_frequency(timer_ticks *pTicks) 38 | { 39 | *pTicks = 1000000; 40 | } 41 | #endif 42 | 43 | timer::timer() : 44 | m_start_time(0), 45 | m_stop_time(0), 46 | m_started(false), 47 | m_stopped(false) 48 | { 49 | if (!g_inv_freq) 50 | init(); 51 | } 52 | 53 | timer::timer(timer_ticks start_ticks) 54 | { 55 | if (!g_inv_freq) 56 | init(); 57 | 58 | m_start_time = start_ticks; 59 | 60 | m_started = true; 61 | m_stopped = false; 62 | } 63 | 64 | void timer::start(timer_ticks start_ticks) 65 | { 66 | m_start_time = start_ticks; 67 | 68 | m_started = true; 69 | m_stopped = false; 70 | } 71 | 72 | void timer::start() 73 | { 74 | query_counter(&m_start_time); 75 | 76 | m_started = true; 77 | m_stopped = false; 78 | } 79 | 80 | void timer::stop() 81 | { 82 | assert(m_started); 83 | 84 | query_counter(&m_stop_time); 85 | 86 | m_stopped = true; 87 | } 88 | 89 | double timer::get_elapsed_secs() const 90 | { 91 | assert(m_started); 92 | if (!m_started) 93 | return 0; 94 | 95 | timer_ticks stop_time = m_stop_time; 96 | if (!m_stopped) 97 | query_counter(&stop_time); 98 | 99 | timer_ticks delta = stop_time - m_start_time; 100 | return delta * g_inv_freq; 101 | } 102 | 103 | timer_ticks timer::get_elapsed_us() const 104 | { 105 | assert(m_started); 106 | if (!m_started) 107 | return 0; 108 | 109 | timer_ticks stop_time = m_stop_time; 110 | if (!m_stopped) 111 | query_counter(&stop_time); 112 | 113 | timer_ticks delta = stop_time - m_start_time; 114 | return (delta * 1000000ULL + (g_freq >> 1U)) / g_freq; 115 | } 116 | 117 | void timer::init() 118 | { 119 | if (!g_inv_freq) 120 | { 121 | query_counter_frequency(&g_freq); 122 | g_inv_freq = 1.0f / g_freq; 123 | 124 | query_counter(&g_init_ticks); 125 | } 126 | } 127 | 128 | timer_ticks timer::get_init_ticks() 129 | { 130 | if (!g_inv_freq) 131 | init(); 132 | 133 | return g_init_ticks; 134 | } 135 | 136 | timer_ticks timer::get_ticks() 137 | { 138 | if (!g_inv_freq) 139 | init(); 140 | 141 | timer_ticks ticks; 142 | query_counter(&ticks); 143 | return ticks - g_init_ticks; 144 | } 145 | 146 | double timer::ticks_to_secs(timer_ticks ticks) 147 | { 148 | if (!g_inv_freq) 149 | init(); 150 | 151 | return ticks * g_inv_freq; 152 | } 153 | 154 | -------------------------------------------------------------------------------- /example2/timer.h: -------------------------------------------------------------------------------- 1 | // File: timer.h 2 | // A simple high-precision, platform independent timer class. 3 | #pragma once 4 | 5 | typedef unsigned long long timer_ticks; 6 | 7 | class timer 8 | { 9 | public: 10 | timer(); 11 | timer(timer_ticks start_ticks); 12 | 13 | void start(); 14 | void start(timer_ticks start_ticks); 15 | 16 | void stop(); 17 | 18 | double get_elapsed_secs() const; 19 | inline double get_elapsed_ms() const { return get_elapsed_secs() * 1000.0f; } 20 | timer_ticks get_elapsed_us() const; 21 | 22 | static void init(); 23 | static inline timer_ticks get_ticks_per_sec() { return g_freq; } 24 | static timer_ticks get_init_ticks(); 25 | static timer_ticks get_ticks(); 26 | static double ticks_to_secs(timer_ticks ticks); 27 | static inline double ticks_to_ms(timer_ticks ticks) { return ticks_to_secs(ticks) * 1000.0f; } 28 | static inline double get_secs() { return ticks_to_secs(get_ticks()); } 29 | static inline double get_ms() { return ticks_to_ms(get_ticks()); } 30 | 31 | private: 32 | static timer_ticks g_init_ticks; 33 | static timer_ticks g_freq; 34 | static double g_inv_freq; 35 | 36 | timer_ticks m_start_time; 37 | timer_ticks m_stop_time; 38 | 39 | bool m_started : 1; 40 | bool m_stopped : 1; 41 | }; 42 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | crunch/crnlib uses the ZLIB license: 2 | http://opensource.org/licenses/Zlib 3 | 4 | Copyright (c) 2010-2016 Richard Geldreich, Jr. and Binomial LLC 5 | 6 | This software is provided 'as-is', without any express or implied 7 | warranty. In no event will the authors be held liable for any damages 8 | arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any purpose, 11 | including commercial applications, and to alter it and redistribute it 12 | freely, subject to the following restrictions: 13 | 14 | 1. The origin of this software must not be misrepresented; you must not 15 | claim that you wrote the original software. If you use this software 16 | in a product, an acknowledgment in the product documentation would be 17 | appreciated but is not required. 18 | 19 | 2. Altered source versions must be plainly marked as such, and must not be 20 | misrepresented as being the original software. 21 | 22 | 3. This notice may not be removed or altered from any source distribution. 23 | --------------------------------------------------------------------------------