Commit 768ee7fa authored by jan.koester's avatar jan.koester
Browse files

Initial Commit

parent 6d80a10c
Loading
Loading
Loading
Loading

CMakeLists.txt

0 → 100644
+8 −0
Original line number Diff line number Diff line
project(uuidplus)

set(LIBV "1.0.0")
set(Upstream_VERSION 1.0.0)

SET(CMAKE_INSTALL_LIBDIR lib CACHE PATH "Output directory for libraries")

add_subdirectory(src)

LICENSE.txt

0 → 100644
+29 −0
Original line number Diff line number Diff line
BSD 3-Clause License

Copyright (c) 2025, Jan Koester
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

src/CMakeLists.txt

0 → 100644
+76 −0
Original line number Diff line number Diff line
include(GenerateExportHeader)

add_library(uuidp SHARED uuidp.cpp)

generate_export_header(uuidp)

add_executable(uuid-gen uuid-gen.cpp)

target_link_libraries(uuid-gen uuidp)

install(TARGETS uuidp DESTINATION lib EXPORT uuidpTargets)

include(CMakePackageConfigHelpers)

write_basic_package_version_file(
  "${CMAKE_CURRENT_BINARY_DIR}/libuuidpConfigVersion.cmake"
  VERSION ${Upstream_VERSION}
  COMPATIBILITY AnyNewerVersion
)

export(EXPORT uuidpTargets
  FILE "${CMAKE_CURRENT_BINARY_DIR}/libuuidpTargets.cmake"
  NAMESPACE uuidp::
)

configure_package_config_file(
  "${CMAKE_SOURCE_DIR}/uuid.cmake.in"
  "${CMAKE_CURRENT_BINARY_DIR}/libuuidpConfig.cmake"
     INSTALL_DESTINATION ${ConfigPackageLocation}
  @ONLY
)

set(ConfigPackageLocation lib/cmake/libuuidp)

install(EXPORT uuidpTargets
  FILE
    libuuidpTargets.cmake
  NAMESPACE
    uuidp::
  DESTINATION
    ${ConfigPackageLocation}
)

install(
  FILES
    "${CMAKE_CURRENT_BINARY_DIR}/libuuidpConfig.cmake"
    "${CMAKE_CURRENT_BINARY_DIR}/libuuidpConfigVersion.cmake"
  DESTINATION
    ${ConfigPackageLocation}
  COMPONENT
    Devel
)

install(
    FILES
        uuidp.h
        "${CMAKE_CURRENT_BINARY_DIR}/uuidp_export.h"
    DESTINATION include
)

install(TARGETS uuid-gen DESTINATION bin)

if(${CMAKE_HOST_SYSTEM_NAME} MATCHES "Windows")
    set(CPACK_GENERATOR WIX)
    set(CPACK_WIX_UPGRADE_GUID "fdedd700-d2a1-5dbe-8157-640d83d42577")
    set(CPACK_PACKAGE_NAME "uuidp")
    set(CPACK_PACKAGE_VENDOR "tuxist.de")
    set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE.txt")
    set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "uuidp - A C++ uuid Library")
    set(CPACK_PACKAGE_VERSION "1.0.0")
    set(CPACK_PACKAGE_VERSION_MAJOR "1")
    set(CPACK_PACKAGE_VERSION_MINOR "0")
    set(CPACK_PACKAGE_VERSION_PATCH "0")
    set(CPACK_PACKAGE_INSTALL_DIRECTORY "uuidp")
    INCLUDE(CPack)
endif()

src/uuid-gen.cpp

0 → 100644
+37 −0
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2014, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <iostream>

#include "uuidp.h"

int main(int argc,char *argv[]){
    uuid::uuid uuid;
    uuid.generate();
    std::cout << uuid.c_str() <<std::endl;
    return 0;
}

src/uuidp.cpp

0 → 100644
+198 −0
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2025, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <cstdio>
#include <cstring>
#include <random>
#include <stdexcept>

#include "uuidp.h"

#define CUUIDLEN 40

uuid::uuid::uuid(){
    cstr = new char[CUUIDLEN];
    std::memset(value,0,sizeof(uuid_t));
}

// Corrected copy constructor
uuid::uuid::uuid(const uuid& src){
    cstr = new char[CUUIDLEN];
    std::memcpy(value, src.value, sizeof(uuid_t));
}

// Corrected copy constructor
uuid::uuid::uuid(const uuid_t& src){
    cstr = new char[CUUIDLEN];
    std::memcpy(value, src, sizeof(uuid_t));
}

uuid::uuid::uuid(const char* src) {
    cstr = new char[CUUIDLEN];
    parse(src);
}

uuid::uuid::uuid(const std::string &src){
    cstr = new char[CUUIDLEN];
    parse(src);
}

uuid::uuid::~uuid(){
    delete[] cstr;
}

int uuid::uuid::parse(const char* src){
    if (src == nullptr) {
        return -1;
    }

    if (std::strlen(src) != 36) {
        return -1;
    }

    // A temporary buffer to hold the 16 bytes.
    uint8_t temp_value[16];
    int byte_index = 0;

    for (size_t i = 0; i < 36; ++i) {
        if (i == 8 || i == 13 || i == 18 || i == 23) {
            if (src[i] != '-') {
                return -1; // Missing or misplaced hyphen
            }
        } else {
            // Read two hexadecimal characters
            char hex_chars[3] = {src[i], src[i+1], '\0'};
            unsigned int byte_val;
            if (std::sscanf(hex_chars, "%2x", &byte_val) != 1) {
                return -1; // Invalid hex characters
            }
            temp_value[byte_index++] = static_cast<uint8_t>(byte_val);
            i++; // Move to the next hex character
        }
    }

    // A UUID has 16 bytes.
    if (byte_index != 16) {
        return -1;
    }

    // Copy the temporary values into the class's member variable.
    std::memcpy(value, temp_value, sizeof(uuid_t));
    return 0; // Success
}

int uuid::uuid::parse(const std::string &src){
     return parse(src.c_str());
}

// Corrected empty() logic
bool uuid::uuid::empty() const{
    for(size_t i=0; i<sizeof(uuid_t); ++i){
        if(value[i]!=0){
            return false;
        }
    }
    return true; // Return true if all bytes are zero
}

void uuid::uuid::clear(){
    std::memset(value,0,sizeof(uuid_t));
}

char uuid::uuid::at(size_t pos){
    return value[pos];
}

void uuid::uuid::insert(size_t pos, char src){
    value[pos]=src;
}

bool uuid::uuid::operator==(const uuid_t &src) const{
    // Use std::memcmp for a correct and efficient comparison
    return std::memcmp(value, src, sizeof(uuid_t)) == 0;
}

bool uuid::uuid::operator==(const uuid &src) const{
    // Use std::memcmp for a correct and efficient comparison
    return std::memcmp(value, src.value, sizeof(uuid_t)) == 0;
}

bool uuid::uuid::operator!=(const uuid &src) const{
    // Simply negate the equality operator
    return !(*this == src);
}

// Corrected assignment operator
uuid::uuid& uuid::uuid::operator =(const uuid& rval) {
    if (this != &rval) { // Prevent self-assignment
        std::memcpy(value, rval.value, sizeof(uuid_t));
    }
    return *this;
}

uuid::uuid& uuid::uuid::operator=(const uuid_t& src){
    std::memcpy(value, src, sizeof(uuid_t));
    return *this;
}

// Corrected c_str() format string
const char *uuid::uuid::c_str() const{
    if (cstr == nullptr) {
        throw std::runtime_error("Buffer for C string is not allocated.");
    }

    std::snprintf(cstr,CUUIDLEN,
                  "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
                  value[0], value[1], value[2], value[3],
                  value[4], value[5],
                  value[6], value[7],
                  value[8], value[9],
                  value[10], value[11], value[12], value[13], value[14], value[15]);

    return cstr;
}

// Corrected generate() for a UUID v4
void uuid::uuid::generate(){
    // Use a high-quality random number generator
    std::random_device rd;
    std::mt19937_64 gen(rd());
    std::uniform_int_distribution<uint64_t> distrib;

    // Generate two 64-bit random numbers
    uint64_t high_bits = distrib(gen);
    uint64_t low_bits = distrib(gen);

    // Copy the random numbers to the value array
    std::memcpy(value, &high_bits, sizeof(uint64_t));
    std::memcpy(value + 8, &low_bits, sizeof(uint64_t));

    // Set the UUID version to 4 (for random UUIDs)
    value[6] = (value[6] & 0x0F) | 0x40; // Version 4 is 0100
    // Set the UUID variant to 10 (for RFC 4122)
    value[8] = (value[8] & 0x3F) | 0x80; // Variant 10 is 10xx
}
Loading