Add cli function for decoding RNC files. (#2191)

Co-authored-by: Alberth289346 <alberth289346@gmail.com>
Co-authored-by: Stephen E. Baker <baker.stephen.e@gmail.com>
This commit is contained in:
Alberth289346
2022-07-03 22:40:15 +02:00
committed by GitHub
parent 94ac71cae9
commit 073ec51da9
7 changed files with 167 additions and 2 deletions

View File

@@ -34,7 +34,7 @@ SOFTWARE.
#include <stdexcept>
#include <vector>
#include "../libs/rnc/rnc.h"
#include "rnc.h"
namespace {

View File

@@ -17,6 +17,7 @@
# - ENABLE_UNIT_TESTS : Enable Unit Testing Target (requires Catch2) (no)
# - BUILD_ANIMVIEW : Generate AnimViewer build files (no)
# - BUILD_TOOLS : Generate cli tools
cmake_minimum_required(VERSION 3.10)
@@ -72,6 +73,7 @@ endif()
option(ENABLE_UNIT_TESTS "Enables Unit Testing Targets" OFF)
option(BUILD_ANIMVIEW "Build the animation viewer as part of the build process" OFF)
option(BUILD_TOOLS "Build additional CLI tools (rnc_decode)" OFF)
if(WITH_AUDIO)
set(CORSIX_TH_USE_SDL_MIXER ON)
@@ -139,6 +141,12 @@ if(BUILD_ANIMVIEW)
message("Building AnimView")
add_subdirectory(AnimView)
endif()
if(BUILD_TOOLS)
message("Building Tools")
add_subdirectory("tools")
endif()
message("")
# Documentation generation, construct 'doc' target

View File

@@ -2,7 +2,7 @@
#include <array>
#include "../../libs/rnc/rnc.h"
#include "rnc.h"
#include "th_lua.h"
//! Provides lua function to decompress RNC data

View File

@@ -5,6 +5,8 @@ add_library(RncLib STATIC
${CMAKE_CURRENT_SOURCE_DIR}/rnc.h
)
target_include_directories(RncLib INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
set_property(TARGET RncLib PROPERTY CXX_STANDARD 14)
set_property(TARGET RncLib PROPERTY CXX_EXTENSIONS OFF)
set_property(TARGET RncLib PROPERTY CXX_STANDARD_REQUIRED ON)

1
tools/CMakeLists.txt Normal file
View File

@@ -0,0 +1 @@
add_subdirectory(rnc)

27
tools/rnc/CMakeLists.txt Normal file
View File

@@ -0,0 +1,27 @@
# Sanity check
if(CORSIX_TH_DONE_TOP_LEVEL_CMAKE)
else()
message(FATAL_ERROR "Please run CMake from the top-level directory instead of here.")
endif()
# Project Declaration
project(RncDecodeCli)
add_executable(
RncDecodeCli
rnc_decode_cli.cpp
)
set_target_properties(RncDecodeCli PROPERTIES OUTPUT_NAME "rnc_decode")
target_link_libraries(RncDecodeCli RncLib)
# Set language standard
set_property(TARGET RncDecodeCli PROPERTY CXX_STANDARD 14)
set_property(TARGET RncDecodeCli PROPERTY CXX_EXTENSIONS OFF)
set_property(TARGET RncDecodeCli PROPERTY CXX_STANDARD_REQUIRED ON)
if(MSVC)
# We want to bind against the very latest versions of the MSVC runtimes
add_definitions(/D "_BIND_TO_CURRENT_VCLIBS_VERSION=1")
endif()

View File

@@ -0,0 +1,127 @@
/*
Copyright (c) 2022 Albert "Alberth" Hofkamp
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "rnc.h"
static std::uint8_t* decompress(const std::uint8_t* in, std::size_t inlen,
std::size_t* outlen) {
if (inlen < rnc_header_size) {
fprintf(stderr, "Input is not RNC compressed data\n");
exit(2);
}
*outlen = rnc_output_size(in);
if (*outlen > 0xFFFFFF) {
fprintf(stderr, "Output file too long (> 16MB).\n");
exit(1);
}
std::uint8_t* outbuf = new std::uint8_t[*outlen];
if (outbuf == nullptr) {
fprintf(stderr, "Cannot allocate memory for output file.\n");
exit(1);
}
switch (rnc_unpack(in, outbuf)) {
case rnc_status::ok:
return outbuf;
case rnc_status::file_is_not_rnc:
fprintf(stderr, "Input is not RNC compressed data\n");
exit(2);
case rnc_status::huf_decode_error:
fprintf(stderr, "Huffman decoding error\n");
exit(2);
case rnc_status::file_size_mismatch:
fprintf(stderr, "Size mismatch\n");
exit(2);
case rnc_status::packed_crc_error:
fprintf(stderr, "Incorrect packed CRC\n");
exit(2);
case rnc_status::unpacked_crc_error:
fprintf(stderr, "Incorrect unpacked CRC\n");
exit(2);
default:
fprintf(stderr, "Unknown error decompressing RNC data\n");
exit(2);
}
}
static bool ishelp(const char* txt) {
if (!strcmp(txt, "-h")) return true;
if (!strcmp(txt, "--help")) return true;
return false;
}
int main(int argc, char* argv[]) {
if (argc != 3 || ishelp(argv[1])) {
fprintf(stderr, "Usage: rnc_decode <input> <output>\n");
exit(1);
}
FILE* inhandle = fopen(argv[1], "rb");
if (inhandle == nullptr) {
fprintf(stderr, "Cannot open input file.\n");
exit(1);
}
fseek(inhandle, 0, SEEK_END);
std::size_t insize = ftell(inhandle);
fseek(inhandle, 0, SEEK_SET);
printf("Input file is %lu bytes.\n", insize);
std::uint8_t* indata = new std::uint8_t[insize];
if (indata == nullptr) {
fprintf(stderr, "Cannot allocate memory for input file.\n");
exit(1);
}
std::size_t count = fread(indata, 1, insize, inhandle);
if (count != insize) {
fprintf(stderr, "Cannot read input file.\n");
exit(1);
}
fclose(inhandle);
std::size_t outlen;
std::uint8_t* outdata = decompress(indata, insize, &outlen);
FILE* outhandle = fopen(argv[2], "wb");
count = fwrite(outdata, 1, outlen, outhandle);
if (count != outlen) {
fprintf(stderr, "Cannot write output file.\n");
exit(1);
}
fclose(outhandle);
printf("Wrote %lu bytes output file.\n", outlen);
delete[] outdata;
return 0;
}