diff options
author | admin <admin@optics-design.com> | 2025-08-05 11:28:41 +0200 |
---|---|---|
committer | admin <admin@optics-design.com> | 2025-08-05 11:28:41 +0200 |
commit | 04b3fcb479f5aaae06d18b315a8bdc8c298f4eae (patch) | |
tree | 92f60caef26f98a83681aa0e1f360df03203bbe4 /include | |
parent | 9496ae0a50e6848121c7e913ca2dc55c8e6c84c1 (diff) |
removed clustering
Diffstat (limited to 'include')
-rw-r--r-- | include/glamac/core/glamac_errors.h (renamed from include/glamac_errors.h) | 11 | ||||
-rw-r--r-- | include/glamac/core/glamacdef.h (renamed from include/glamacdef.h) | 35 | ||||
-rw-r--r-- | include/glamac/core/security.h | 41 | ||||
-rw-r--r-- | include/glamac/data/glass_data.h (renamed from include/glass_data.h) | 4 | ||||
-rw-r--r-- | include/glamac/graphics/glamac_render.h (renamed from include/glamac_render.h) | 2 | ||||
-rw-r--r-- | include/glamac/graphics/glamac_view.h (renamed from include/glamac_view.h) | 68 | ||||
-rw-r--r-- | include/glamac/input/glamac_events.h (renamed from include/glamac_events.h) | 4 | ||||
-rw-r--r-- | include/glautils/fgla.h | 113 |
8 files changed, 196 insertions, 82 deletions
diff --git a/include/glamac_errors.h b/include/glamac/core/glamac_errors.h index 4e7b9d0..2065600 100644 --- a/include/glamac_errors.h +++ b/include/glamac/core/glamac_errors.h @@ -14,6 +14,7 @@ #define GLAMAC_ERRORS_H
#include "glamacdef.h"
+#include "security.h"
// Error codes for GlaMaC operations
typedef enum {
@@ -26,16 +27,12 @@ typedef enum { GLAMAC_ERROR_BUFFER_OVERFLOW = -6,
GLAMAC_ERROR_MANUFACTURER_NOT_FOUND = -7,
GLAMAC_ERROR_NO_GLASSES_FOUND = -8,
- GLAMAC_ERROR_INVALID_ARGUMENT = -9
+ GLAMAC_ERROR_INVALID_ARGUMENT = -9,
+ GLAMAC_ERROR_INVALID_GLASS_DATA = -10,
+ GLAMAC_ERROR_INVALID_MANUFACTURER = -11
} GlamacResult;
// Convert error code to human-readable string
const char* glamac_error_string(GlamacResult error);
-// Security limits
-#define MAX_JSON_FILE_SIZE (10 * 1024 * 1024) // 10MB limit
-#define MAX_JSON_STRING_LEN 1024 // Max string field length
-#define MAX_PATH_LEN 4096 // Max file path length
-#define MAX_MANUFACTURER_NAME_LEN 64 // Max manufacturer name length
-
#endif /* GLAMAC_ERRORS_H */
\ No newline at end of file diff --git a/include/glamacdef.h b/include/glamac/core/glamacdef.h index aeba926..46cbf82 100644 --- a/include/glamacdef.h +++ b/include/glamac/core/glamacdef.h @@ -16,6 +16,7 @@ #include <stdint.h>
#include <uchar.h>
#include <stddef.h>
+#include <stdlib.h>
/* Type definitions for consistent sizing across platforms. Idea taken from https://nullprogram.com/blog/2023/10/08/ (archive link: ) */
typedef uint8_t u8;
typedef char16_t c16;
@@ -37,7 +38,35 @@ typedef size_t usize; /* Utility macros */
#define countof(a) (size)(sizeof(a) / sizeof(*(a)))
#define lengthof(s) (countof(s) - 1)
-// #define new(a, t, n) (t *)alloc(a, sizeof(t), _Alignof(t), n) //From Nullprogram, not using currently
-#define new(t, n) (t *)malloc(n*sizeof(t))
-#define new_arr(t,arr) (t)malloc(sizeof(arr))
+
+/* Safe allocation macros with overflow protection */
+static inline void* safe_malloc(size_t element_size, size_t count) {
+ // Check for integer overflow in multiplication
+ if (count == 0 || element_size == 0) {
+ return NULL;
+ }
+ if (count > SIZE_MAX / element_size) {
+ return NULL; // Would overflow
+ }
+ return malloc(element_size * count);
+}
+
+static inline void* safe_calloc(size_t element_size, size_t count) {
+ // Check for integer overflow - calloc handles this internally but be explicit
+ if (count == 0 || element_size == 0) {
+ return NULL;
+ }
+ if (count > SIZE_MAX / element_size) {
+ return NULL; // Would overflow
+ }
+ return calloc(count, element_size);
+}
+
+// Safe allocation macros - use these instead of malloc directly
+#define new(t, n) ((t*)safe_malloc(sizeof(t), (size_t)(n)))
+#define new_zero(t, n) ((t*)safe_calloc(sizeof(t), (size_t)(n)))
+#define new_arr(t, arr) ((t*)safe_malloc(sizeof(arr), 1))
+
+// Legacy unsafe macro - deprecated, use new() or new_zero() instead
+// #define old_new(t, n) (t *)malloc(n*sizeof(t)) // DEPRECATED
#endif /* OPDECDEF_H */
diff --git a/include/glamac/core/security.h b/include/glamac/core/security.h new file mode 100644 index 0000000..6b8c811 --- /dev/null +++ b/include/glamac/core/security.h @@ -0,0 +1,41 @@ +/**
+ * security.h - Centralized security constants and limits for GlaMaC
+ *
+ * Copyright (C) 2025 https://optics-design.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * See the COPYING file for the full license text.
+ */
+#ifndef GLAMAC_SECURITY_H
+#define GLAMAC_SECURITY_H
+
+// File and data size limits
+#define MAX_JSON_FILE_SIZE (10 * 1024 * 1024) // 10MB limit for JSON file size
+#define MAX_JSON_STRING_LEN 1024 // Max string field length in JSON
+#define MAX_PATH_LEN 4096 // Max file path length
+#define MAX_MANUFACTURER_NAME_LEN 64 // Max manufacturer name length
+
+// Glass search utility limits
+#define FGLA_MAX_SEARCH_TERM_LEN 256 // Max search term length
+#define FGLA_MAX_GLASS_NAME_LEN 256 // Max glass name length
+#define FGLA_MAX_CATALOG_COUNT 10 // Max number of catalogs to search
+#define FGLA_MAX_NORMALIZED_LEN 512 // Max normalized string length
+#define FGLA_GLASS_CODE_LEN 6 // Fixed glass code length
+
+// Buffer limits for input validation
+#define MAX_INPUT_BUFFER_SIZE 8192 // General input buffer limit
+#define MAX_COMMAND_LINE_ARGS 32 // Max command line arguments
+
+// Memory allocation limits
+#define MAX_GLASS_ENTRIES 100000 // Max glass entries in database
+#define MAX_TEXTURE_CACHE_SIZE 1000 // Max cached textures for rendering
+
+// Security validation constants
+#define MIN_GLASS_CODE_DIGITS 6 // Minimum digits in glass code
+#define MAX_GLASS_CODE_DIGITS 6 // Maximum digits in glass code
+
+#endif /* GLAMAC_SECURITY_H */
\ No newline at end of file diff --git a/include/glass_data.h b/include/glamac/data/glass_data.h index 56c2c7f..00f30c0 100644 --- a/include/glass_data.h +++ b/include/glamac/data/glass_data.h @@ -13,8 +13,8 @@ #ifndef GLASS_DATA_H
#define GLASS_DATA_H
-#include "glamacdef.h" // For type definitions
-#include "glamac_errors.h" // For error handling
+#include "../core/glamacdef.h" // For type definitions
+#include "../core/glamac_errors.h" // For error handling
// Structure to represent an optical glass
typedef struct {
diff --git a/include/glamac_render.h b/include/glamac/graphics/glamac_render.h index 899702d..4c20751 100644 --- a/include/glamac_render.h +++ b/include/glamac/graphics/glamac_render.h @@ -6,7 +6,7 @@ #include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
-#include "glamacdef.h"
+#include "../core/glamacdef.h"
#include "glamac_view.h"
// Drawing primitives
diff --git a/include/glamac_view.h b/include/glamac/graphics/glamac_view.h index 6ca4b9b..58de2b1 100644 --- a/include/glamac_view.h +++ b/include/glamac/graphics/glamac_view.h @@ -5,7 +5,7 @@ #define GLAMAC_VIEW_H
#include <SDL3/SDL.h>
-#include "glamacdef.h"
+#include "../core/glamacdef.h"
// Constants for view
#define MIN_PADDING 20 // Minimum padding in pixels
@@ -20,44 +20,6 @@ #define LABEL_OFFSET_Y -8 // Vertical offset from glass point (pixels)
#define LABEL_SIZE_SCALE 1.4f // Scale factor for glass name labels
-// Tight clustering parameters
-#define MAX_CLUSTER_SIZE 8 // Maximum glasses per cluster
-#define DEFAULT_TIGHT_CLUSTER_ND_THRESHOLD 0.0005f // Default nd difference threshold
-#define DEFAULT_TIGHT_CLUSTER_VD_THRESHOLD 0.15f // Default vd difference threshold
-
-// Loose clustering parameters (zoom-dependent)
-#define DEFAULT_LOOSE_CLUSTER_ND_THRESHOLD 0.3f // Default nd base threshold
-#define DEFAULT_LOOSE_CLUSTER_VD_THRESHOLD 0.55f // Default vd base threshold
-#define DEFAULT_LOOSE_CLUSTER_ND_FRACTION 4.9f // Default nd fraction of visible range
-#define DEFAULT_LOOSE_CLUSTER_VD_FRACTION 0.9f // Default vd fraction of visible range
-
-// Global tight cluster thresholds (adjustable parameters)
-extern f32 g_tight_cluster_nd_threshold;
-extern f32 g_tight_cluster_vd_threshold;
-
-// Global loose cluster parameters (adjustable parameters)
-extern f32 g_loose_cluster_nd_threshold; // Base threshold
-extern f32 g_loose_cluster_vd_threshold; // Base threshold
-extern f32 g_loose_cluster_nd_fraction; // Zoom scaling fraction
-extern f32 g_loose_cluster_vd_fraction; // Zoom scaling fraction
-
-// Tight cluster structure
-typedef struct {
- i32 glassIndices[MAX_CLUSTER_SIZE]; // Indices of glasses in this cluster
- i32 count; // Number of glasses in cluster
- i32 representativeIndex; // Index of glass with shortest name
- f32 avgAbbeNumber; // Average position for reference
- f32 avgRefractiveIndex;
-} TightCluster;
-
-// Loose cluster structure (zoom-dependent)
-typedef struct {
- i32 glassIndices[MAX_CLUSTER_SIZE]; // Indices of glasses in this cluster
- i32 count; // Number of glasses in cluster
- i32 representativeIndex; // Index of glass with shortest name
- f32 avgAbbeNumber; // Average position for reference
- f32 avgRefractiveIndex;
-} LooseCluster;
// State for zooming and panning
@@ -75,22 +37,6 @@ typedef struct { b32 gKeyPressed; // Flag to track if 'g' was pressed
u32 gKeyTime; // Time when 'g' was pressed for sequence timing
i32 selectedGlass; // Index of selected glass (-1 if none)
- i32 selectedCluster; // Index of selected tight cluster (-1 if none)
-
- // Tight cluster data (per catalog)
- TightCluster* tightClusters; // Array of tight clusters for current catalog
- i32 tightClusterCount; // Number of tight clusters
-
- // Loose cluster data (per catalog, zoom-dependent)
- LooseCluster* looseClusters; // Array of loose clusters for current catalog
- i32 looseClusterCount; // Number of loose clusters
-
- // Label recalculation tracking
- f32 lastZoomLevel; // Last zoom level for which labels were calculated
- i32 lastWindowWidth; // Last window width for labels
- i32 lastWindowHeight; // Last window height for labels
- f32 lastOffsetX; // Last X offset for labels
- f32 lastOffsetY; // Last Y offset for labels
} ViewState;
// Initialize a view state with default values
@@ -158,18 +104,6 @@ void toggle_fullscreen(SDL_Window* window); // Reset view to default
void reset_view(ViewState* view);
-// Tight clustering functions
-void create_tight_clusters(ViewState* view);
-void free_tight_clusters(ViewState* view);
-i32 find_tight_cluster_for_glass(i32 glassIndex, const ViewState* view);
-
-// Loose clustering functions (zoom-dependent)
-void create_loose_clusters(ViewState* view);
-void free_loose_clusters(ViewState* view);
-i32 find_loose_cluster_for_glass(i32 glassIndex, const ViewState* view);
-
-// Combined clustering logic
-b32 should_show_glass_label(i32 glassIndex, const ViewState* view);
#endif /* GLAMAC_VIEW_H */
diff --git a/include/glamac_events.h b/include/glamac/input/glamac_events.h index c4145c0..f06a703 100644 --- a/include/glamac_events.h +++ b/include/glamac/input/glamac_events.h @@ -5,8 +5,8 @@ #define GLAMAC_EVENTS_H
#include <SDL3/SDL.h>
-#include "glamacdef.h"
-#include "glamac_view.h"
+#include "../core/glamacdef.h"
+#include "../graphics/glamac_view.h"
// Process key event
b32 process_key_event(SDL_KeyboardEvent *key, ViewState *view, SDL_Window *window, b32 *quit);
diff --git a/include/glautils/fgla.h b/include/glautils/fgla.h new file mode 100644 index 0000000..d9a946b --- /dev/null +++ b/include/glautils/fgla.h @@ -0,0 +1,113 @@ +/**
+ * fgla.h - Find Glass utility header
+ *
+ * Copyright (C) 2025 https://optics-design.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * See the COPYING file for the full license text.
+ */
+
+#ifndef FGLA_H
+#define FGLA_H
+
+#include "glamac/core/glamacdef.h"
+#include "glamac/core/security.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Output format types
+typedef enum {
+ FGLA_OUTPUT_CSV = 0,
+ FGLA_OUTPUT_TABLE = 1,
+ FGLA_OUTPUT_JSON = 2
+} FglaOutputFormat;
+
+// Error handling
+typedef enum {
+ FGLA_SUCCESS = 0,
+ FGLA_ERROR_INVALID_ARGS = 1,
+ FGLA_ERROR_INVALID_SEARCH_TERM = 2,
+ FGLA_ERROR_NO_DATABASE = 3,
+ FGLA_ERROR_NO_MATCHES = 4,
+ FGLA_ERROR_MEMORY = 5
+} FglaResult;
+
+/**
+ * @brief Safely converts string to lowercase with length limit
+ * @param str String to convert (modified in place)
+ * @param max_len Maximum length to process
+ */
+void fgla_to_lowercase_safe(char* str, size_t max_len);
+
+/**
+ * @brief Safely normalizes string by removing dashes and converting to lowercase
+ * @param input Input string to normalize
+ * @param output Buffer for normalized output
+ * @param output_size Size of output buffer
+ * @return 0 on success, -1 on error
+ */
+int fgla_normalize_string_safe(const char* input, char* output, size_t output_size);
+
+/**
+ * @brief Safely checks if needle is found in haystack (case-insensitive, dash-insensitive)
+ * @param haystack String to search in
+ * @param needle String to search for
+ * @return 1 if found, 0 if not found or error
+ */
+int fgla_contains_substring_safe(const char* haystack, const char* needle);
+
+/**
+ * @brief Checks if manufacturer matches any of the specified catalogs
+ * @param manufacturer Manufacturer name to check
+ * @param catalog_list Array of catalog names to match against
+ * @param catalog_count Number of catalogs in the list
+ * @return 1 if matches, 0 if no match
+ */
+int fgla_matches_catalog(const char* manufacturer, const char* catalog_list[], int catalog_count);
+
+/**
+ * @brief Validates search term input for security and format
+ * @param term Search term to validate
+ * @return 1 if valid, 0 if invalid
+ */
+int fgla_validate_search_term(const char* term);
+
+/**
+ * @brief Checks if a search term is a glass code pattern
+ * @param term Search term to check
+ * @return 1 if glass code pattern, 0 otherwise
+ */
+int fgla_is_glass_code_pattern(const char* term);
+
+/**
+ * @brief Safely checks if a glass code matches a pattern
+ * @param glass_code Glass code to check
+ * @param pattern Pattern to match (can contain 'x' wildcards)
+ * @return 1 if matches, 0 if no match
+ */
+int fgla_matches_glass_code_pattern_safe(const char* glass_code, const char* pattern);
+
+/**
+ * @brief Prints usage information for fgla utility
+ * @param program_name Name of the program (argv[0])
+ */
+void fgla_print_usage(const char* program_name);
+
+/**
+ * @brief Prints error message with helpful suggestions
+ * @param error Error code from FglaResult enum
+ * @param context Optional context string for the error
+ */
+void fgla_print_error_with_suggestion(FglaResult error, const char* context);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* FGLA_H */
\ No newline at end of file |