From 665e433697247c4a43e055830dee7a72afdb810f Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 1 Jan 2026 12:52:28 +0100 Subject: feat: FIX Checksum API for OCL.FIX. Signed-off-by: Amlal El Mahrouss --- include/ocl/fix/checksum.hpp | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/ocl/fix/checksum.hpp b/include/ocl/fix/checksum.hpp index c075b13..31aee75 100644 --- a/include/ocl/fix/checksum.hpp +++ b/include/ocl/fix/checksum.hpp @@ -9,9 +9,50 @@ #define __OCL_FIX_CHECKSUM #include +#include +#include namespace ocl::fix { -} + + /// \brief Returns the checksum index of a FIX message. + /// \param range the range_buffer containing the message. + /// \throws runtime_error if the FIX message is invalid (missing tag "8"). + inline std::string try_index_checksum(range_buffer& fix) + { + if (fix.is_valid()) + return fix["10"]; + else + detail::throw_runtime_error(); + } + + /// \brief FIX message operators namespace. + namespace operators::fix + { + using checksum_type = long long; + + /// \brief Calculates the FIX protocol checksum for a message. + /// \param in_ Pointer to the message buffer. + /// \param len Length of the message in bytes. + /// \return The checksum value (sum of all bytes modulo 256). + inline checksum_type + checksum(const char* in_, + const long len) + { + if (len < 1) + return 0L; + + long long cks{}; + + for (long idx{}; idx < len; ++idx) + { + cks += static_cast(in_[idx]); + } + + return cks % 256; + } + } // namespace operators::fix + +} // namespace ocl::fix #endif -- cgit v1.2.3