LLC协议格式

IEEE 802.2标准定义了逻辑链路控制(LLC,Logical Link Control)协议,它是数据链路层的一部分,主要用于管理和控制局域网(LAN)中的数据传输。LLC层位于MAC(介质访问控制)子层之上,负责向网络层提供统一的接口,并屏蔽底层物理网络的差异。LLC协议的设计目标是为不同的网络协议(如IP、IPX等)提供通用的数据传输服务。

LLC协议的核心功能包括帧格式定义、流量控制、差错控制以及协议多路复用。LLC帧格式通常由三个主要字段组成:

1. **DSAP(Destination Service Access Point)**:表示目标服务访问点,用于标识接收方的上层协议。
2. **SSAP(Source Service Access Point)**:表示源服务访问点,用于标识发送方的上层协议。
3. **控制字段(Control Field)**:用于定义帧的类型(如信息帧、监督帧或无编号帧)以及相关的控制信息,例如序列号、确认号等。

LLC协议支持三种主要的帧类型:
– **信息帧(I帧)**:用于传输用户数据,并包含序列号和确认号,支持可靠的面向连接的数据传输。
– **监督帧(S帧)**:用于流量控制和差错控制,但不携带用户数据。
– **无编号帧(U帧)**:用于建立和释放连接,或执行其他控制功能,例如测试和诊断。

LLC协议通过这些机制实现了对数据链路层的抽象,使得上层协议可以独立于具体的物理网络技术进行通信。例如,在IEEE 802.3以太网中,LLC层可以与MAC子层结合使用,通过802.2 LLC头部封装IP数据报,从而实现与上层协议的交互。

此外,LLC协议还支持无连接和面向连接两种操作模式。在无连接模式下,数据帧直接发送而不需要建立连接;而在面向连接模式下,LLC协议会在数据传输前建立连接,并在传输结束后释放连接,以确保可靠的数据传输。

### LLC与Ethernet II的区别
LLC协议通常与IEEE 802.3标准一起使用,而Ethernet II则采用了一种更简单的帧格式。Ethernet II帧中没有LLC头部,而是直接使用类型字段(EtherType)来标识上层协议(如IPv4、ARP等)。相比之下,IEEE 802.3帧需要通过LLC头部来标识上层协议,因此在实际应用中,Ethernet II更为常见。

以下是一个典型的LLC帧封装示例(基于IEEE 802.3帧结构):

“`plaintext
+—————-+—————-+—————-+—————-+—————-+
| 前导码(Preamble) | 目的MAC地址 | 源MAC地址 | 长度/类型字段 | 数据字段 |
+—————-+—————-+—————-+—————-+—————-+
| CRC校验字段 | DSAP | SSAP | 控制字段 | 上层协议数据 |
+—————-+—————-+—————-+—————-+—————-+
“`

在实际网络中,LLC协议的使用频率有所下降,尤其是在现代以太网环境中,Ethernet II因其简洁性和高效性而被广泛采用。然而,在某些传统网络协议(如NetBIOS、IPX/SPX)中,LLC仍然发挥着重要作用。

发表在 linux文章 | 留下评论

Advanced C Programming Techniques and Best Practices – Complete Edition


Advanced C Programming Techniques and Best Practices – Complete Edition

Table of Contents

  1. Macro Definitions and Preprocessing Techniques
  2. Advanced Memory Management Techniques
  3. Function Pointers and Callback Mechanisms
  4. Data Structure Design
  5. Concurrency and Multithreading
  6. Error Handling and Exception Mechanisms
  7. Performance Optimization Techniques
  8. Debugging and Testing Techniques
  9. Cross-Platform Programming
  10. Secure Programming Practices
  11. Comprehensive Demonstration Examples

Macro Definitions and Preprocessing Techniques

1. Conditional Compilation and Platform Detection

/**
 * Platform and compiler detection
 * Used for conditional compilation and cross-platform compatibility
 */
#if defined(_WIN32) || defined(_WIN64)
    #define PLATFORM_WINDOWS
#elif defined(__linux__)
    #define PLATFORM_LINUX
#elif defined(__APPLE__)
    #define PLATFORM_MACOS
#endif

// Compiler detection
#if defined(__GNUC__)
    #define COMPILER_GCC
#elif defined(_MSC_VER)
    #define COMPILER_MSVC
#endif

// Version detection
#if __STDC_VERSION__ >= 201112L
    #define C11_SUPPORTED
#endif

2. Advanced Macro Techniques

/**
 * Advanced macro collection
 * Provides type-safe and convenient macro utilities
 */

// Stringification and concatenation
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define CONCAT(a, b) a##b

// Get array size
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

// container_of macro (get container pointer from member pointer)
#define container_of(ptr, type, member) ({          \
    void *__mptr = (void *)(ptr);                    \
    ((type *)(__mptr - offsetof(type, member))); })

// Min/Max values
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

// Variable swap (without temporary variable)
#define SWAP(a, b) do { typeof(a) temp = a; a = b; b = temp; } while(0)

// Compile-time assertion
#define STATIC_ASSERT(condition, message) \
    typedef char static_assertion_##message[(condition) ? 1 : -1]

// Variadic macros
#define DEBUG_PRINT(fmt, ...) \
    fprintf(stderr, "[DEBUG] %s:%d: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)

3. Modern C Language Features

/**
 * C11 modern features
 * Leverage new standards to improve code quality
 */

// Generic selection
#define generic_max(a, b) _Generic((a), \
    int: max_int, \
    float: max_float, \
    double: max_double \
)(a, b)

// Static assertion (C11)
_Static_assert(sizeof(int) >= 4, "int must be at least 4 bytes");

// Thread-local storage (C11)
_Thread_local int thread_var;

Advanced Memory Management Techniques

1. Memory Pool Design

/**
 * Memory pool implementation
 * Provides efficient memory allocation and recycling
 */
typedef struct {
    void *memory;
    size_t size;
    size_t used;
    size_t block_size;
} memory_pool_t;

memory_pool_t* create_pool(size_t size, size_t block_size) {
    memory_pool_t *pool = malloc(sizeof(memory_pool_t));
    pool->memory = malloc(size);
    pool->size = size;
    pool->used = 0;
    pool->block_size = block_size;
    return pool;
}

void* pool_alloc(memory_pool_t *pool, size_t size) {
    if (pool->used + size > pool->size) return NULL;
    void *ptr = (char*)pool->memory + pool->used;
    pool->used += size;
    return ptr;
}

2. Smart Pointer Simulation

/**
 * Smart pointer simulation system
 * Implements reference-counted automatic memory management
 */

#include <stdatomic.h>

typedef struct {
    void *ptr;
    void (*deleter)(void*);
    atomic_int *ref_count;
} smart_ptr_t;

smart_ptr_t make_smart_ptr(void *ptr, void (*deleter)(void*)) {
    smart_ptr_t sp = {ptr, deleter, malloc(sizeof(atomic_int))};
    atomic_init(sp.ref_count, 1);
    return sp;
}

smart_ptr_t smart_ptr_copy(smart_ptr_t sp) {
    atomic_fetch_add(sp.ref_count, 1);
    return sp;
}

void smart_ptr_free(smart_ptr_t *sp) {
    if (atomic_fetch_sub(sp->ref_count, 1) == 1) {
        sp->deleter(sp->ptr);
        free(sp->ref_count);
    }
}

3. Memory Alignment

/**
 * Memory alignment utilities
 * Ensures proper alignment of data structures in memory
 */

// C11 alignment
_Alignas(16) char aligned_buffer[256];

// Manual alignment
#define ALIGN_UP(x, align) (((x) + (align) - 1) & ~((align) - 1))
#define IS_ALIGNED(x, align) (((x) & ((align) - 1)) == 0)

void* aligned_malloc(size_t size, size_t alignment) {
    void *ptr = malloc(size + alignment - 1 + sizeof(void*));
    if (!ptr) return NULL;
    
    void **aligned_ptr = (void**)(((uintptr_t)ptr + sizeof(void*) + alignment - 1) & ~(alignment - 1));
    aligned_ptr[-1] = ptr;
    return aligned_ptr;
}

Function Pointers and Callback Mechanisms

1. Object-Oriented Style Programming

/**
 * Virtual function table simulation
 * Implements object-oriented programming in C
 */

// Virtual function table simulation
typedef struct {
    void (*destroy)(void *self);
    void (*print)(void *self);
    int (*compare)(void *self, void *other);
} vtable_t;

typedef struct {
    vtable_t *vtable;
    // Concrete data
} object_t;

// Polymorphic invocation
#define CALL_METHOD(obj, method, ...) \
    ((obj)->vtable->method((obj), ##__VA_ARGS__))

2. State Machine Implementation

/**
 * State machine implementation
 * Provides flexible state management mechanism
 */

typedef enum {
    STATE_IDLE,
    STATE_RUNNING,
    STATE_PAUSED,
    STATE_STOPPED
} state_t;

typedef struct {
    state_t current_state;
    int (*handlers[4])(void *context, int event);
} state_machine_t;

int handle_idle(void *context, int event) {
    switch (event) {
        case EVENT_START:
            return STATE_RUNNING;
        default:
            return STATE_IDLE;
    }
}

3. Plugin System Design

/**
 * Plugin system design
 * Supports dynamic loading and functionality extension
 */

typedef struct {
    const char *name;
    int version;
    int (*init)(void);
    void (*cleanup)(void);
    void* (*create_instance)(void);
} plugin_interface_t;

// Dynamic plugin loading
#ifdef _WIN32
    #include <windows.h>
    #define LOAD_PLUGIN(name) LoadLibrary(name)
    #define GET_SYMBOL(handle, name) GetProcAddress(handle, name)
#else
    #include <dlfcn.h>
    #define LOAD_PLUGIN(name) dlopen(name, RTLD_LAZY)
    #define GET_SYMBOL(handle, name) dlsym(handle, name)
#endif

Data Structure Design

1. Linked List Implementation

/**
 * Doubly linked list implementation
 * Provides efficient insertion and deletion operations
 */

typedef struct list_node {
    void *data;
    struct list_node *next;
    struct list_node *prev;
} list_node_t;

typedef struct {
    list_node_t head;
    size_t size;
    void (*destructor)(void*);
} list_t;

// Doubly linked list operations
void list_insert_after(list_t *list, list_node_t *node, void *data) {
    list_node_t *new_node = malloc(sizeof(list_node_t));
    new_node->data = data;
    new_node->next = node->next;
    new_node->prev = node;
    
    if (node->next) node->next->prev = new_node;
    node->next = new_node;
    list->size++;
}

2. Hash Table Implementation

/**
 * Hash table implementation
 * Provides fast key-value storage and retrieval
 */

typedef struct hash_entry {
    char *key;
    void *value;
    struct hash_entry *next;
} hash_entry_t;

typedef struct {
    hash_entry_t **buckets;
    size_t bucket_count;
    size_t size;
    unsigned int (*hash_func)(const char*);
} hash_table_t;

unsigned int djb2_hash(const char *str) {
    unsigned int hash = 5381;
    int c;
    while ((c = *str++)) hash = ((hash << 5) + hash) + c;
    return hash;
}

3. Ring Buffer

/**
 * Ring buffer implementation
 * Suitable for producer-consumer patterns
 */

typedef struct {
    char *buffer;
    size_t size;
    size_t read_pos;
    size_t write_pos;
    int full;
} ring_buffer_t;

int ring_buffer_write(ring_buffer_t *rb, const char *data, size_t len) {
    size_t available = rb->size - ring_buffer_size(rb);
    if (len > available) return -1;
    
    for (size_t i = 0; i < len; i++) {
        rb->buffer[rb->write_pos] = data[i];
        rb->write_pos = (rb->write_pos + 1) % rb->size;
        if (rb->write_pos == rb->read_pos) rb->full = 1;
    }
    return len;
}

Concurrency and Multithreading

1. Thread-Safe Data Structures

/**
 * Thread-safe counter
 * Provides atomic operations and conditional waiting
 */

#include <pthread.h>

typedef struct {
    int value;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} thread_safe_counter_t;

void counter_increment(thread_safe_counter_t *counter) {
    pthread_mutex_lock(&counter->mutex);
    counter->value++;
    pthread_cond_signal(&counter->cond);
    pthread_mutex_unlock(&counter->mutex);
}

int counter_wait_for(thread_safe_counter_t *counter, int target) {
    pthread_mutex_lock(&counter->mutex);
    while (counter->value < target) {
        pthread_cond_wait(&counter->cond, &counter->mutex);
    }
    pthread_mutex_unlock(&counter->mutex);
    return counter->value;
}

2. Read-Write Lock Implementation

/**
 * Read-write lock implementation
 * Supports multiple readers/single writer concurrency control
 */

typedef struct {
    pthread_mutex_t mutex;
    pthread_cond_t read_cond;
    pthread_cond_t write_cond;
    int readers;
    int writers;
    int waiting_writers;
} rwlock_t;

void rwlock_rdlock(rwlock_t *rwlock) {
    pthread_mutex_lock(&rwlock->mutex);
    while (rwlock->writers > 0 || rwlock->waiting_writers > 0) {
        pthread_cond_wait(&rwlock->read_cond, &rwlock->mutex);
    }
    rwlock->readers++;
    pthread_mutex_unlock(&rwlock->mutex);
}

3. Lock-Free Programming

/**
 * Lock-free programming tools
 * Implements high-performance concurrency using atomic operations
 */

#include <stdatomic.h>

typedef struct {
    atomic_int value;
} atomic_counter_t;

void atomic_counter_increment(atomic_counter_t *counter) {
    atomic_fetch_add(&counter->value, 1);
}

int atomic_counter_get(atomic_counter_t *counter) {
    return atomic_load(&counter->value);
}

Error Handling and Exception Mechanisms

1. Error Code System

/**
 * Error code system
 * Provides structured error handling mechanism
 */

typedef enum {
    ERROR_SUCCESS = 0,
    ERROR_INVALID_PARAM = -1,
    ERROR_OUT_OF_MEMORY = -2,
    ERROR_FILE_NOT_FOUND = -3,
    ERROR_PERMISSION_DENIED = -4
} error_code_t;

#define RETURN_ON_ERROR(expr) do { \
    error_code_t err = (expr); \
    if (err != ERROR_SUCCESS) return err; \
} while(0)

// Context-aware error handling
typedef struct {
    error_code_t code;
    const char *message;
    const char *file;
    int line;
} error_context_t;

2. Exception Simulation Mechanism

/**
 * Exception simulation mechanism
 * Implements exception handling using setjmp/longjmp
 */

#include <setjmp.h>

typedef struct {
    jmp_buf jump_buffer;
    int error_code;
    const char *error_message;
} exception_context_t;

static __thread exception_context_t *current_exception = NULL;

#define TRY \
    do { \
        exception_context_t __exception_ctx; \
        __exception_ctx.error_code = 0; \
        if (setjmp(__exception_ctx.jump_buffer) == 0) { \
            current_exception = &__exception_ctx;

#define CATCH(error_var) \
        } else { \
            error_var = current_exception->error_code;

#define END_TRY \
        } \
        current_exception = NULL; \
    } while(0);

#define THROW(code, message) \
    do { \
        if (current_exception) { \
            current_exception->error_code = code; \
            current_exception->error_message = message; \
            longjmp(current_exception->jump_buffer, 1); \
        } \
    } while(0)

3. Resource Management (RAII)

/**
 * RAII resource management
 * Ensures automatic resource cleanup
 */

typedef struct {
    void *resource;
    void (*cleanup)(void*);
} raii_guard_t;

#define RAII_VAR(type, name, init, cleanup_func) \
    type name = init; \
    raii_guard_t __guard_##name = {&name, (void(*)(void*))cleanup_func}; \
    __attribute__((cleanup(raii_cleanup))) raii_guard_t *__raii_##name = &__guard_##name;

static void raii_cleanup(raii_guard_t **guard) {
    if ((*guard)->resource && (*guard)->cleanup) {
        (*guard)->cleanup((*guard)->resource);
    }
}

Performance Optimization Techniques

1. Cache-Friendly Data Structures

/**
 * Cache-friendly data structures
 * Optimizes memory layout for better cache hit rates
 */

// Struct packing optimization
struct __attribute__((packed)) packed_struct {
    char a;
    int b;
    short c;
};

// Cache line alignment
#define CACHE_LINE_SIZE 64
struct __attribute__((aligned(CACHE_LINE_SIZE))) cache_aligned_struct {
    int data[16];
};

2. Branch Prediction Optimization

/**
 * Branch prediction optimization
 * Uses compiler hints to improve execution efficiency
 */

// Static branch prediction
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

void optimized_function(int *array, size_t size) {
    if (unlikely(size == 0)) return;
    
    for (size_t i = 0; likely(i < size); i++) {
        process_element(array[i]);
    }
}

3. Inline Assembly Optimization

/**
 * Inline assembly optimization
 * Directly uses CPU instructions for performance gains
 */

// Read Time-Stamp Counter
static inline uint64_t rdtsc(void) {
    uint32_t lo, hi;
    __asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
    return ((uint64_t)hi << 32) | lo;
}

// Memory barrier
#define MEMORY_BARRIER() __asm__ __volatile__("" ::: "memory")

4. SIMD Optimization

/**
 * SIMD optimization
 * Leverages vector instructions for parallel data processing
 */

#ifdef __SSE2__
#include <emmintrin.h>

void vector_add(float *a, float *b, float *result, size_t n) {
    size_t i = 0;
    for (; i + 4 <= n; i += 4) {
        __m128 va = _mm_load_ps(&a[i]);
        __m128 vb = _mm_load_ps(&b[i]);
        __m128 vr = _mm_add_ps(va, vb);
        _mm_store_ps(&result[i], vr);
    }
    // Process remaining elements
    for (; i < n; i++) {
        result[i] = a[i] + b[i];
    }
}
#endif

Debugging and Testing Techniques

1. Debug Macros

/**
 * Debugging utility macros
 * Provides convenient debugging and profiling capabilities
 */

#ifdef DEBUG
    #define DBG_PRINT(fmt, ...) \
        fprintf(stderr, "[DEBUG] %s:%d: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
    #define ASSERT(condition) \
        do { \
            if (!(condition)) { \
                fprintf(stderr, "Assertion failed: %s at %s:%d\n", \
                        #condition, __FILE__, __LINE__); \
                abort(); \
            } \
        } while(0)
#else
    #define DBG_PRINT(fmt, ...) do {} while(0)
    #define ASSERT(condition) do {} while(0)
#endif

// Performance timing
#define TIME_IT(code, result_var) \
    do { \
        clock_t start = clock(); \
        code; \
        result_var = ((double)(clock() - start)) / CLOCKS_PER_SEC; \
    } while(0)

2. Unit Testing Framework

/**
 * Unit testing framework
 * Provides structured testing support
 */

typedef struct {
    const char *name;
    void (*test_func)(void);
    int passed;
    int failed;
} test_case_t;

#define TEST_CASE(name) \
    static void test_##name(void); \
    static test_case_t test_case_##name = {#name, test_##name, 0, 0}; \
    static void test_##name(void)

#define ASSERT_EQ(expected, actual) \
    do { \
        if ((expected) != (actual)) { \
            fprintf(stderr, "Assertion failed: %s != %s at %s:%d\n", \
                    #expected, #actual, __FILE__, __LINE__); \
            current_test->failed++; \
        } else { \
            current_test->passed++; \
        } \
    } while(0)

3. Memory Leak Detection

/**
 * Memory leak detection
 * Tracks memory allocations and deallocations
 */

#ifdef DEBUG_MEMORY
static size_t total_allocated = 0;
static size_t allocation_count = 0;

void* debug_malloc(size_t size, const char *file, int line) {
    void *ptr = malloc(size + sizeof(size_t));
    if (ptr) {
        *(size_t*)ptr = size;
        total_allocated += size;
        allocation_count++;
        printf("ALLOC: %zu bytes at %s:%d\n", size, file, line);
        return (char*)ptr + sizeof(size_t);
    }
    return NULL;
}

void debug_free(void *ptr, const char *file, int line) {
    if (ptr) {
        size_t *size_ptr = (size_t*)((char*)ptr - sizeof(size_t));
        total_allocated -= *size_ptr;
        allocation_count--;
        printf("FREE: %zu bytes at %s:%d\n", *size_ptr, file, line);
        free(size_ptr);
    }
}

#define malloc(size) debug_malloc(size, __FILE__, __LINE__)
#define free(ptr) debug_free(ptr, __FILE__, __LINE__)
#endif

Cross-Platform Programming

1. Platform Abstraction Layer

/**
 * Platform abstraction layer
 * Provides unified cross-platform interfaces
 */

// Thread abstraction
#ifdef _WIN32
    #include <windows.h>
    typedef HANDLE thread_t;
    typedef CRITICAL_SECTION mutex_t;
    #define THREAD_CREATE(thread, func, arg) \
        (thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, NULL))
    #define THREAD_JOIN(thread) WaitForSingleObject(thread, INFINITE)
    #define MUTEX_INIT(mutex) InitializeCriticalSection(mutex)
    #define MUTEX_LOCK(mutex) EnterCriticalSection(mutex)
    #define MUTEX_UNLOCK(mutex) LeaveCriticalSection(mutex)
#else
    #include <pthread.h>
    typedef pthread_t thread_t;
    typedef pthread_mutex_t mutex_t;
    #define THREAD_CREATE(thread, func, arg) pthread_create(&thread, NULL, func, arg)
    #define THREAD_JOIN(thread) pthread_join(thread, NULL)
    #define MUTEX_INIT(mutex) pthread_mutex_init(mutex, NULL)
    #define MUTEX_LOCK(mutex) pthread_mutex_lock(mutex)
    #define MUTEX_UNLOCK(mutex) pthread_mutex_unlock(mutex)
#endif

2. File Path Handling

/**
 * File path handling
 * Provides cross-platform path operations
 */

#ifdef _WIN32
    #define PATH_SEPARATOR '\\'
    #define PATH_SEPARATOR_STR "\\"
#else
    #define PATH_SEPARATOR '/'
    #define PATH_SEPARATOR_STR "/"
#endif

char* join_path(const char *dir, const char *file) {
    size_t dir_len = strlen(dir);
    size_t file_len = strlen(file);
    char *result = malloc(dir_len + file_len + 2);
    
    strcpy(result, dir);
    if (dir[dir_len - 1] != PATH_SEPARATOR) {
        strcat(result, PATH_SEPARATOR_STR);
    }
    strcat(result, file);
    return result;
}

3. Endianness Handling

/**
 * Endianness handling
 * Ensures data correctness during network transmission
 */

// Network byte order conversion
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
    #define IS_BIG_ENDIAN 1
#else
    #define IS_BIG_ENDIAN 0
#endif

static inline uint32_t swap_endian_32(uint32_t val) {
    return ((val & 0x000000FF) << 24) |
           ((val & 0x0000FF00) << 8)  |
           ((val & 0x00FF0000) >> 8)  |
           ((val & 0xFF000000) >> 24);
}

#define hton32(x) (IS_BIG_ENDIAN ? (x) : swap_endian_32(x))
#define ntoh32(x) hton32(x)

Secure Programming Practices

1. Buffer Overflow Protection

/**
 * Buffer overflow protection
 * Provides safe string manipulation functions
 */

// Safe string operations
size_t safe_strncpy(char *dest, size_t dest_size, const char *src, size_t count) {
    if (dest_size == 0) return 0;
    
    size_t copy_len = (count < dest_size - 1) ? count : dest_size - 1;
    memcpy(dest, src, copy_len);
    dest[copy_len] = '\0';
    return copy_len;
}

// Formatted string safety checks
#define SAFE_PRINTF(buffer, size, format, ...) \
    do { \
        int __result = snprintf(buffer, size, format, ##__VA_ARGS__); \
        if (__result < 0 || (size_t)__result >= size) { \
            /* Handle overflow */ \
            buffer[size - 1] = '\0'; \
        } \
    } while(0)

2. Input Validation

/**
 * Input validation
 * Prevents security issues caused by malicious input
 */

// Integer overflow check
static inline int safe_add(int a, int b, int *result) {
    if ((b > 0 && a > INT_MAX - b) || (b < 0 && a < INT_MIN - b)) {
        return -1; // Overflow
    }
    *result = a + b;
    return 0;
}

// Pointer validation
#define VALIDATE_PTR(ptr) \
    do { \
        if (!(ptr)) { \
            return ERROR_INVALID_PARAM; \
        } \
    } while(0)

3. Secure Random Numbers

/**
 * Secure random number generation
 * Provides cryptographically secure random numbers
 */

#include <time.h>
#include <stdlib.h>

// Cryptographically secure random numbers (requires platform support)
#ifdef __linux__
    #include <sys/random.h>
    int secure_random_bytes(void *buf, size_t len) {
        return getrandom(buf, len, 0) == (ssize_t)len ? 0 : -1;
    }
#else
    // Simple pseudo-random number generator
    static unsigned long long rand_state = 1;
    
    void srand64(unsigned long long seed) {
        rand_state = seed;
    }
    
    unsigned long long rand64(void) {
        rand_state = rand_state * 6364136223846793005ULL + 1;
        return rand_state;
    }
#endif

Comprehensive Demonstration Examples

Event-Driven Architecture Demo

/**
 * Event-Driven Architecture - Event Type Definitions
 * Used for building flexible event handling systems
 */
typedef enum {
    EVENT_NONE = 0,
    EVENT_TIMER,
    EVENT_NETWORK,
    EVENT_USER,
    EVENT_SYSTEM
} event_type_t;

/**
 * Event structure
 * Contains event type, timestamp, and user data
 */
typedef struct {
    event_type_t type;
    uint64_t timestamp;
    void *data;
    size_t data_size;
} event_t;

/**
 * Event handler function pointer type
 * @param event Event pointer
 * @param context User context
 * @return Processing result
 */
typedef int (*event_handler_t)(event_t *event, void *context);

/**
 * Event listener structure
 * Stores event type and corresponding handler
 */
typedef struct {
    event_type_t type;
    event_handler_t handler;
    void *context;
    int priority;  // Processing priority
} event_listener_t;

/**
 * Event loop structure
 * Manages event queue and listeners
 */
typedef struct {
    event_listener_t *listeners;
    size_t listener_count;
    size_t listener_capacity;
    
    event_t *event_queue;
    size_t queue_head;
    size_t queue_tail;
    size_t queue_size;
    size_t queue_capacity;
    
    int running;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} event_loop_t;

// [Implementation code continues...]

Lock-Free Queue Implementation

/**
 * Lock-free queue implementation
 * Implements thread-safe lock-free queue using CAS operations
 */

#include <stdatomic.h>

/**
 * Queue node structure
 */
typedef struct queue_node {
    void *data;
    _Atomic(struct queue_node*) next;
} queue_node_t;

/**
 * Lock-free queue structure
 */
typedef struct {
    _Atomic(queue_node_t*) head;
    _Atomic(queue_node_t*) tail;
    atomic_size_t size;
} lockfree_queue_t;

// [Implementation code continues...]

Cache-Friendly Data Structures

/**
 * Cache-friendly data structure implementation
 * Optimizes memory layout to improve cache hit rates
 */

/**
 * Cache line size definition
 */
#define CACHE_LINE_SIZE 64

/**
 * Cache alignment macro
 */
#define CACHE_ALIGNED __attribute__((aligned(CACHE_LINE_SIZE)))

/**
 * SoA (Structure of Arrays) vector structure
 * Separates related data storage to improve cache efficiency
 */
typedef struct {
    float *x;           // X-coordinate array
    float *y;           // Y-coordinate array
    float *z;           // Z-coordinate array
    int *id;            // ID array
    size_t capacity;    // Capacity
    size_t size;        // Current size
    char padding[CACHE_LINE_SIZE - sizeof(size_t)*2 - sizeof(char*)*4]; // Padding to cache line boundary
} soa_vector_t;

// [Implementation code continues...]

Secure String Operations Library

/**
 * Secure string operations library
 * Provides buffer-overflow-safe string functions
 */

#include <string.h>
#include <stdio.h>
#include <stdarg.h>

/**
 * Secure string structure
 * Contains length information to prevent overflows
 */
typedef struct {
    char *data;
    size_t length;
    size_t capacity;
    int is_secure;  // Whether security checks are enabled
} secure_string_t;

// [Implementation code continues...]

Comprehensive Demo Function

/**
 * Event-Driven Architecture Demo Example
 */
void demo_event_driven_architecture() {
    printf("=== Event-Driven Architecture Demo ===\n");
    
    // Create event loop
    event_loop_t *loop = event_loop_create(100);
    if (!loop) {
        printf("Failed to create event loop\n");
        return;
    }
    
    // Add event listeners
    event_loop_add_listener(loop, EVENT_TIMER, timer_handler, NULL, 0);
    event_loop_add_listener(loop, EVENT_NETWORK, network_handler, NULL, 0);
    event_loop_add_listener(loop, EVENT_USER, user_handler, NULL, 0);
    
    // Start event loop thread
    pthread_t loop_thread;
    pthread_create(&loop_thread, NULL, (void*(*)(void*))event_loop_run, loop);
    
    // Post some test events
    for (int i = 0; i < 5; i++) {
        event_t event = {0};
        event.timestamp = time(NULL);
        
        // Post different event types
        switch (i % 3) {
            case 0:
                event.type = EVENT_TIMER;
                printf("Posting timer event %d\n", i);
                break;
            case 1: {
                event.type = EVENT_NETWORK;
                const char *msg = "Hello Network!";
                event.data = strdup(msg);
                event.data_size = strlen(msg);
                printf("Posting network event %d\n", i);
                break;
            }
            case 2:
                event.type = EVENT_USER;
                event.data = malloc(sizeof(int));
                *(int*)event.data = i;
                event.data_size = sizeof(int);
                printf("Posting user event %d\n", i);
                break;
        }
        
        event_loop_post(loop, &event);
        sleep(1);
    }
    
    // Cleanup event data
    sleep(2);
    
    // Stop and destroy event loop
    event_loop_stop(loop);
    pthread_join(loop_thread, NULL);
    event_loop_destroy(loop);
    
    printf("=== Demo Complete ===\n\n");
}

// [Other demo functions continue...]

Appendix: Best Practices Summary

Coding Standards

  1. ​Naming Conventions​​: Use clear names, avoid abbreviations
  2. ​Comment Style​​: Use Doxygen-style comments
  3. ​Error Handling​​: Always check return values
  4. ​Memory Management​​: Follow RAII principles
  5. ​Thread Safety​​: Clearly identify thread-safe functions

Performance Optimization Principles

  1. ​Measure Before Optimizing​​: Use profiling tools
  2. ​Algorithm First​​: Choose appropriate data structures and algorithms
  3. ​Avoid Premature Optimization​​: Maintain code readability
  4. ​Cache-Friendly​​: Consider data locality
  5. ​Compiler Optimization​​: Use compiler optimization flags appropriately

Secure Coding Principles

  1. ​Input Validation​​: Never trust external input
  2. ​Bounds Checking​​: Prevent buffer overflows
  3. ​Principle of Least Privilege​​: Use minimum necessary privileges
  4. ​Safe Functions​​: Use secure string functions
  5. ​Code Reviews​​: Conduct regular security code reviews

This comprehensive guide to advanced C programming techniques covers all important aspects from basic macros to complex concurrent programming, providing rich code examples and best practices to help developers write high-quality, high-performance, and secure C code.

发表在 linux文章 | 留下评论

C语言高级编程技巧与最佳实践 – 完整版

C语言高级编程技巧与最佳实践 – 完整版

目录

  1. 宏定义与预处理技巧
  2. 内存管理高级技巧
  3. 函数指针与回调机制
  4. 数据结构设计
  5. 并发与多线程
  6. 错误处理与异常机制
  7. 性能优化技巧
  8. 调试与测试技巧
  9. 跨平台编程
  10. 安全编程实践
  11. 综合演示示例

宏定义与预处理技巧

1. 条件编译与平台检测

/**
 * 平台和编译器检测
 * 用于条件编译和跨平台兼容性
 */
#if defined(_WIN32) || defined(_WIN64)
    #define PLATFORM_WINDOWS
#elif defined(__linux__)
    #define PLATFORM_LINUX
#elif defined(__APPLE__)
    #define PLATFORM_MACOS
#endif

// 编译器检测
#if defined(__GNUC__)
    #define COMPILER_GCC
#elif defined(_MSC_VER)
    #define COMPILER_MSVC
#endif

// 版本检测
#if __STDC_VERSION__ >= 201112L
    #define C11_SUPPORTED
#endif

2. 强大的宏技巧

/**
 * 高级宏定义集合
 * 提供类型安全和便捷的宏工具
 */

// 字符串化和连接
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define CONCAT(a, b) a##b

// 获取数组长度
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

// 容器of宏(从成员指针获取容器指针)
#define container_of(ptr, type, member) ({          \
    void *__mptr = (void *)(ptr);                    \
    ((type *)(__mptr - offsetof(type, member))); })

// 最大最小值
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

// 交换变量(不使用临时变量)
#define SWAP(a, b) do { typeof(a) temp = a; a = b; b = temp; } while(0)

// 编译时断言
#define STATIC_ASSERT(condition, message) \
    typedef char static_assertion_##message[(condition) ? 1 : -1]

// 可变参数宏
#define DEBUG_PRINT(fmt, ...) \
    fprintf(stderr, "[DEBUG] %s:%d: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)

3. 现代C语言特性

/**
 * C11现代特性使用
 * 利用新标准提高代码质量
 */

// 泛型选择
#define generic_max(a, b) _Generic((a), \
    int: max_int, \
    float: max_float, \
    double: max_double \
)(a, b)

// 静态断言(C11)
_Static_assert(sizeof(int) >= 4, "int must be at least 4 bytes");

// 线程局部存储(C11)
_Thread_local int thread_var;

内存管理高级技巧

1. 内存池设计

/**
 * 内存池实现
 * 提供高效的内存分配和回收机制
 */
typedef struct {
    void *memory;
    size_t size;
    size_t used;
    size_t block_size;
} memory_pool_t;

memory_pool_t* create_pool(size_t size, size_t block_size) {
    memory_pool_t *pool = malloc(sizeof(memory_pool_t));
    pool->memory = malloc(size);
    pool->size = size;
    pool->used = 0;
    pool->block_size = block_size;
    return pool;
}

void* pool_alloc(memory_pool_t *pool, size_t size) {
    if (pool->used + size > pool->size) return NULL;
    void *ptr = (char*)pool->memory + pool->used;
    pool->used += size;
    return ptr;
}

2. 智能指针模拟

/**
 * 智能指针模拟系统
 * 实现引用计数自动内存管理
 */

#include <stdatomic.h>

typedef struct {
    void *ptr;
    void (*deleter)(void*);
    atomic_int *ref_count;
} smart_ptr_t;

smart_ptr_t make_smart_ptr(void *ptr, void (*deleter)(void*)) {
    smart_ptr_t sp = {ptr, deleter, malloc(sizeof(atomic_int))};
    atomic_init(sp.ref_count, 1);
    return sp;
}

smart_ptr_t smart_ptr_copy(smart_ptr_t sp) {
    atomic_fetch_add(sp.ref_count, 1);
    return sp;
}

void smart_ptr_free(smart_ptr_t *sp) {
    if (atomic_fetch_sub(sp->ref_count, 1) == 1) {
        sp->deleter(sp->ptr);
        free(sp->ref_count);
    }
}

3. 内存对齐

/**
 * 内存对齐工具
 * 确保数据结构在内存中的正确对齐
 */

// C11对齐
_Alignas(16) char aligned_buffer[256];

// 手动对齐
#define ALIGN_UP(x, align) (((x) + (align) - 1) & ~((align) - 1))
#define IS_ALIGNED(x, align) (((x) & ((align) - 1)) == 0)

void* aligned_malloc(size_t size, size_t alignment) {
    void *ptr = malloc(size + alignment - 1 + sizeof(void*));
    if (!ptr) return NULL;
    
    void **aligned_ptr = (void**)(((uintptr_t)ptr + sizeof(void*) + alignment - 1) & ~(alignment - 1));
    aligned_ptr[-1] = ptr;
    return aligned_ptr;
}

函数指针与回调机制

1. 面向对象风格编程

/**
 * 虚函数表模拟
 * 实现C语言中的面向对象编程
 */

// 虚函数表模拟
typedef struct {
    void (*destroy)(void *self);
    void (*print)(void *self);
    int (*compare)(void *self, void *other);
} vtable_t;

typedef struct {
    vtable_t *vtable;
    // 具体数据
} object_t;

// 多态调用
#define CALL_METHOD(obj, method, ...) \
    ((obj)->vtable->method((obj), ##__VA_ARGS__))

2. 状态机实现

/**
 * 状态机实现
 * 提供灵活的状态管理机制
 */

typedef enum {
    STATE_IDLE,
    STATE_RUNNING,
    STATE_PAUSED,
    STATE_STOPPED
} state_t;

typedef struct {
    state_t current_state;
    int (*handlers[4])(void *context, int event);
} state_machine_t;

int handle_idle(void *context, int event) {
    switch (event) {
        case EVENT_START:
            return STATE_RUNNING;
        default:
            return STATE_IDLE;
    }
}

3. 插件系统设计

/**
 * 插件系统设计
 * 支持动态加载和扩展功能
 */

typedef struct {
    const char *name;
    int version;
    int (*init)(void);
    void (*cleanup)(void);
    void* (*create_instance)(void);
} plugin_interface_t;

// 动态加载插件
#ifdef _WIN32
    #include <windows.h>
    #define LOAD_PLUGIN(name) LoadLibrary(name)
    #define GET_SYMBOL(handle, name) GetProcAddress(handle, name)
#else
    #include <dlfcn.h>
    #define LOAD_PLUGIN(name) dlopen(name, RTLD_LAZY)
    #define GET_SYMBOL(handle, name) dlsym(handle, name)
#endif

数据结构设计

1. 链表实现

/**
 * 双向链表实现
 * 提供高效的插入和删除操作
 */

typedef struct list_node {
    void *data;
    struct list_node *next;
    struct list_node *prev;
} list_node_t;

typedef struct {
    list_node_t head;
    size_t size;
    void (*destructor)(void*);
} list_t;

// 双向链表操作
void list_insert_after(list_t *list, list_node_t *node, void *data) {
    list_node_t *new_node = malloc(sizeof(list_node_t));
    new_node->data = data;
    new_node->next = node->next;
    new_node->prev = node;
    
    if (node->next) node->next->prev = new_node;
    node->next = new_node;
    list->size++;
}

2. 哈希表实现

/**
 * 哈希表实现
 * 提供快速的键值对存储和检索
 */

typedef struct hash_entry {
    char *key;
    void *value;
    struct hash_entry *next;
} hash_entry_t;

typedef struct {
    hash_entry_t **buckets;
    size_t bucket_count;
    size_t size;
    unsigned int (*hash_func)(const char*);
} hash_table_t;

unsigned int djb2_hash(const char *str) {
    unsigned int hash = 5381;
    int c;
    while ((c = *str++)) hash = ((hash << 5) + hash) + c;
    return hash;
}

3. 环形缓冲区

/**
 * 环形缓冲区实现
 * 适用于生产者-消费者模式
 */

typedef struct {
    char *buffer;
    size_t size;
    size_t read_pos;
    size_t write_pos;
    int full;
} ring_buffer_t;

int ring_buffer_write(ring_buffer_t *rb, const char *data, size_t len) {
    size_t available = rb->size - ring_buffer_size(rb);
    if (len > available) return -1;
    
    for (size_t i = 0; i < len; i++) {
        rb->buffer[rb->write_pos] = data[i];
        rb->write_pos = (rb->write_pos + 1) % rb->size;
        if (rb->write_pos == rb->read_pos) rb->full = 1;
    }
    return len;
}

并发与多线程

1. 线程安全的数据结构

/**
 * 线程安全计数器
 * 提供原子操作和条件等待
 */

#include <pthread.h>

typedef struct {
    int value;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} thread_safe_counter_t;

void counter_increment(thread_safe_counter_t *counter) {
    pthread_mutex_lock(&counter->mutex);
    counter->value++;
    pthread_cond_signal(&counter->cond);
    pthread_mutex_unlock(&counter->mutex);
}

int counter_wait_for(thread_safe_counter_t *counter, int target) {
    pthread_mutex_lock(&counter->mutex);
    while (counter->value < target) {
        pthread_cond_wait(&counter->cond, &counter->mutex);
    }
    pthread_mutex_unlock(&counter->mutex);
    return counter->value;
}

2. 读写锁实现

/**
 * 读写锁实现
 * 支持多读单写的并发控制
 */

typedef struct {
    pthread_mutex_t mutex;
    pthread_cond_t read_cond;
    pthread_cond_t write_cond;
    int readers;
    int writers;
    int waiting_writers;
} rwlock_t;

void rwlock_rdlock(rwlock_t *rwlock) {
    pthread_mutex_lock(&rwlock->mutex);
    while (rwlock->writers > 0 || rwlock->waiting_writers > 0) {
        pthread_cond_wait(&rwlock->read_cond, &rwlock->mutex);
    }
    rwlock->readers++;
    pthread_mutex_unlock(&rwlock->mutex);
}

3. 无锁编程

/**
 * 无锁编程工具
 * 使用原子操作实现高性能并发
 */

#include <stdatomic.h>

typedef struct {
    atomic_int value;
} atomic_counter_t;

void atomic_counter_increment(atomic_counter_t *counter) {
    atomic_fetch_add(&counter->value, 1);
}

int atomic_counter_get(atomic_counter_t *counter) {
    return atomic_load(&counter->value);
}

错误处理与异常机制

1. 错误码系统

/**
 * 错误码系统
 * 提供结构化的错误处理机制
 */

typedef enum {
    ERROR_SUCCESS = 0,
    ERROR_INVALID_PARAM = -1,
    ERROR_OUT_OF_MEMORY = -2,
    ERROR_FILE_NOT_FOUND = -3,
    ERROR_PERMISSION_DENIED = -4
} error_code_t;

#define RETURN_ON_ERROR(expr) do { \
    error_code_t err = (expr); \
    if (err != ERROR_SUCCESS) return err; \
} while(0)

// 带上下文的错误处理
typedef struct {
    error_code_t code;
    const char *message;
    const char *file;
    int line;
} error_context_t;

2. 异常模拟机制

/**
 * 异常模拟机制
 * 使用setjmp/longjmp实现异常处理
 */

#include <setjmp.h>

typedef struct {
    jmp_buf jump_buffer;
    int error_code;
    const char *error_message;
} exception_context_t;

static __thread exception_context_t *current_exception = NULL;

#define TRY \
    do { \
        exception_context_t __exception_ctx; \
        __exception_ctx.error_code = 0; \
        if (setjmp(__exception_ctx.jump_buffer) == 0) { \
            current_exception = &__exception_ctx;

#define CATCH(error_var) \
        } else { \
            error_var = current_exception->error_code;

#define END_TRY \
        } \
        current_exception = NULL; \
    } while(0);

#define THROW(code, message) \
    do { \
        if (current_exception) { \
            current_exception->error_code = code; \
            current_exception->error_message = message; \
            longjmp(current_exception->jump_buffer, 1); \
        } \
    } while(0)

3. 资源管理RAII

/**
 * RAII资源管理
 * 确保资源的自动释放
 */

typedef struct {
    void *resource;
    void (*cleanup)(void*);
} raii_guard_t;

#define RAII_VAR(type, name, init, cleanup_func) \
    type name = init; \
    raii_guard_t __guard_##name = {&name, (void(*)(void*))cleanup_func}; \
    __attribute__((cleanup(raii_cleanup))) raii_guard_t *__raii_##name = &__guard_##name;

static void raii_cleanup(raii_guard_t **guard) {
    if ((*guard)->resource && (*guard)->cleanup) {
        (*guard)->cleanup((*guard)->resource);
    }
}

性能优化技巧

1. 缓存友好的数据结构

/**
 * 缓存友好的数据结构
 * 优化内存布局提高缓存命中率
 */

// 结构体打包优化
struct __attribute__((packed)) packed_struct {
    char a;
    int b;
    short c;
};

// 缓存行对齐
#define CACHE_LINE_SIZE 64
struct __attribute__((aligned(CACHE_LINE_SIZE))) cache_aligned_struct {
    int data[16];
};

2. 分支预测优化

/**
 * 分支预测优化
 * 使用编译器提示提高执行效率
 */

// 静态分支预测
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

void optimized_function(int *array, size_t size) {
    if (unlikely(size == 0)) return;
    
    for (size_t i = 0; likely(i < size); i++) {
        process_element(array[i]);
    }
}

3. 内联汇编优化

/**
 * 内联汇编优化
 * 直接使用CPU指令提高性能
 */

// 获取时间戳计数器
static inline uint64_t rdtsc(void) {
    uint32_t lo, hi;
    __asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
    return ((uint64_t)hi << 32) | lo;
}

// 内存屏障
#define MEMORY_BARRIER() __asm__ __volatile__("" ::: "memory")

4. SIMD优化

/**
 * SIMD优化
 * 利用向量指令并行处理数据
 */

#ifdef __SSE2__
#include <emmintrin.h>

void vector_add(float *a, float *b, float *result, size_t n) {
    size_t i = 0;
    for (; i + 4 <= n; i += 4) {
        __m128 va = _mm_load_ps(&a[i]);
        __m128 vb = _mm_load_ps(&b[i]);
        __m128 vr = _mm_add_ps(va, vb);
        _mm_store_ps(&result[i], vr);
    }
    // 处理剩余元素
    for (; i < n; i++) {
        result[i] = a[i] + b[i];
    }
}
#endif

调试与测试技巧

1. 调试宏

/**
 * 调试工具宏
 * 提供便捷的调试和性能分析功能
 */

#ifdef DEBUG
    #define DBG_PRINT(fmt, ...) \
        fprintf(stderr, "[DEBUG] %s:%d: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
    #define ASSERT(condition) \
        do { \
            if (!(condition)) { \
                fprintf(stderr, "Assertion failed: %s at %s:%d\n", \
                        #condition, __FILE__, __LINE__); \
                abort(); \
            } \
        } while(0)
#else
    #define DBG_PRINT(fmt, ...) do {} while(0)
    #define ASSERT(condition) do {} while(0)
#endif

// 性能计时
#define TIME_IT(code, result_var) \
    do { \
        clock_t start = clock(); \
        code; \
        result_var = ((double)(clock() - start)) / CLOCKS_PER_SEC; \
    } while(0)

2. 单元测试框架

/**
 * 单元测试框架
 * 提供结构化的测试支持
 */

typedef struct {
    const char *name;
    void (*test_func)(void);
    int passed;
    int failed;
} test_case_t;

#define TEST_CASE(name) \
    static void test_##name(void); \
    static test_case_t test_case_##name = {#name, test_##name, 0, 0}; \
    static void test_##name(void)

#define ASSERT_EQ(expected, actual) \
    do { \
        if ((expected) != (actual)) { \
            fprintf(stderr, "Assertion failed: %s != %s at %s:%d\n", \
                    #expected, #actual, __FILE__, __LINE__); \
            current_test->failed++; \
        } else { \
            current_test->passed++; \
        } \
    } while(0)

3. 内存泄漏检测

/**
 * 内存泄漏检测
 * 跟踪内存分配和释放
 */

#ifdef DEBUG_MEMORY
static size_t total_allocated = 0;
static size_t allocation_count = 0;

void* debug_malloc(size_t size, const char *file, int line) {
    void *ptr = malloc(size + sizeof(size_t));
    if (ptr) {
        *(size_t*)ptr = size;
        total_allocated += size;
        allocation_count++;
        printf("ALLOC: %zu bytes at %s:%d\n", size, file, line);
        return (char*)ptr + sizeof(size_t);
    }
    return NULL;
}

void debug_free(void *ptr, const char *file, int line) {
    if (ptr) {
        size_t *size_ptr = (size_t*)((char*)ptr - sizeof(size_t));
        total_allocated -= *size_ptr;
        allocation_count--;
        printf("FREE: %zu bytes at %s:%d\n", *size_ptr, file, line);
        free(size_ptr);
    }
}

#define malloc(size) debug_malloc(size, __FILE__, __LINE__)
#define free(ptr) debug_free(ptr, __FILE__, __LINE__)
#endif

跨平台编程

1. 平台抽象层

/**
 * 平台抽象层
 * 提供统一的跨平台接口
 */

// 线程抽象
#ifdef _WIN32
    #include <windows.h>
    typedef HANDLE thread_t;
    typedef CRITICAL_SECTION mutex_t;
    #define THREAD_CREATE(thread, func, arg) \
        (thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, NULL))
    #define THREAD_JOIN(thread) WaitForSingleObject(thread, INFINITE)
    #define MUTEX_INIT(mutex) InitializeCriticalSection(mutex)
    #define MUTEX_LOCK(mutex) EnterCriticalSection(mutex)
    #define MUTEX_UNLOCK(mutex) LeaveCriticalSection(mutex)
#else
    #include <pthread.h>
    typedef pthread_t thread_t;
    typedef pthread_mutex_t mutex_t;
    #define THREAD_CREATE(thread, func, arg) pthread_create(&thread, NULL, func, arg)
    #define THREAD_JOIN(thread) pthread_join(thread, NULL)
    #define MUTEX_INIT(mutex) pthread_mutex_init(mutex, NULL)
    #define MUTEX_LOCK(mutex) pthread_mutex_lock(mutex)
    #define MUTEX_UNLOCK(mutex) pthread_mutex_unlock(mutex)
#endif

2. 文件路径处理

/**
 * 文件路径处理
 * 提供跨平台的路径操作
 */

#ifdef _WIN32
    #define PATH_SEPARATOR '\\'
    #define PATH_SEPARATOR_STR "\\"
#else
    #define PATH_SEPARATOR '/'
    #define PATH_SEPARATOR_STR "/"
#endif

char* join_path(const char *dir, const char *file) {
    size_t dir_len = strlen(dir);
    size_t file_len = strlen(file);
    char *result = malloc(dir_len + file_len + 2);
    
    strcpy(result, dir);
    if (dir[dir_len - 1] != PATH_SEPARATOR) {
        strcat(result, PATH_SEPARATOR_STR);
    }
    strcat(result, file);
    return result;
}

3. 字节序处理

/**
 * 字节序处理
 * 确保数据在网络传输中的正确性
 */

// 网络字节序转换
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
    #define IS_BIG_ENDIAN 1
#else
    #define IS_BIG_ENDIAN 0
#endif

static inline uint32_t swap_endian_32(uint32_t val) {
    return ((val & 0x000000FF) << 24) |
           ((val & 0x0000FF00) << 8)  |
           ((val & 0x00FF0000) >> 8)  |
           ((val & 0xFF000000) >> 24);
}

#define hton32(x) (IS_BIG_ENDIAN ? (x) : swap_endian_32(x))
#define ntoh32(x) hton32(x)

安全编程实践

1. 缓冲区溢出防护

/**
 * 缓冲区溢出防护
 * 提供安全的字符串操作函数
 */

// 安全字符串操作
size_t safe_strncpy(char *dest, size_t dest_size, const char *src, size_t count) {
    if (dest_size == 0) return 0;
    
    size_t copy_len = (count < dest_size - 1) ? count : dest_size - 1;
    memcpy(dest, src, copy_len);
    dest[copy_len] = '\0';
    return copy_len;
}

// 格式化字符串安全检查
#define SAFE_PRINTF(buffer, size, format, ...) \
    do { \
        int __result = snprintf(buffer, size, format, ##__VA_ARGS__); \
        if (__result < 0 || (size_t)__result >= size) { \
            /* 处理溢出 */ \
            buffer[size - 1] = '\0'; \
        } \
    } while(0)

2. 输入验证

/**
 * 输入验证
 * 防止恶意输入导致的安全问题
 */

// 整数溢出检查
static inline int safe_add(int a, int b, int *result) {
    if ((b > 0 && a > INT_MAX - b) || (b < 0 && a < INT_MIN - b)) {
        return -1; // 溢出
    }
    *result = a + b;
    return 0;
}

// 指针验证
#define VALIDATE_PTR(ptr) \
    do { \
        if (!(ptr)) { \
            return ERROR_INVALID_PARAM; \
        } \
    } while(0)

3. 安全随机数

/**
 * 安全随机数生成
 * 提供密码学安全的随机数
 */

#include <time.h>
#include <stdlib.h>

// 密码学安全随机数(需要平台支持)
#ifdef __linux__
    #include <sys/random.h>
    int secure_random_bytes(void *buf, size_t len) {
        return getrandom(buf, len, 0) == (ssize_t)len ? 0 : -1;
    }
#else
    // 简单的伪随机数生成器
    static unsigned long long rand_state = 1;
    
    void srand64(unsigned long long seed) {
        rand_state = seed;
    }
    
    unsigned long long rand64(void) {
        rand_state = rand_state * 6364136223846793005ULL + 1;
        return rand_state;
    }
#endif

综合演示示例

事件驱动架构演示

/**
 * 事件驱动架构 - 事件类型定义
 * 用于构建灵活的事件处理系统
 */
typedef enum {
    EVENT_NONE = 0,
    EVENT_TIMER,
    EVENT_NETWORK,
    EVENT_USER,
    EVENT_SYSTEM
} event_type_t;

/**
 * 事件结构体
 * 包含事件类型、时间戳和用户数据
 */
typedef struct {
    event_type_t type;
    uint64_t timestamp;
    void *data;
    size_t data_size;
} event_t;

/**
 * 事件处理器函数指针类型
 * @param event 事件指针
 * @param context 用户上下文
 * @return 处理结果
 */
typedef int (*event_handler_t)(event_t *event, void *context);

/**
 * 事件监听器结构体
 * 存储事件类型和对应的处理器
 */
typedef struct {
    event_type_t type;
    event_handler_t handler;
    void *context;
    int priority;  // 处理优先级
} event_listener_t;

/**
 * 事件循环结构体
 * 管理事件队列和监听器
 */
typedef struct {
    event_listener_t *listeners;
    size_t listener_count;
    size_t listener_capacity;
    
    event_t *event_queue;
    size_t queue_head;
    size_t queue_tail;
    size_t queue_size;
    size_t queue_capacity;
    
    int running;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} event_loop_t;

/**
 * 创建事件循环
 * @param queue_capacity 事件队列容量
 * @return 事件循环指针
 */
event_loop_t* event_loop_create(size_t queue_capacity) {
    event_loop_t *loop = calloc(1, sizeof(event_loop_t));
    if (!loop) return NULL;
    
    loop->queue_capacity = queue_capacity;
    loop->event_queue = calloc(queue_capacity, sizeof(event_t));
    if (!loop->event_queue) {
        free(loop);
        return NULL;
    }
    
    loop->listeners = calloc(16, sizeof(event_listener_t));  // 初始监听器容量
    loop->listener_capacity = 16;
    
    pthread_mutex_init(&loop->mutex, NULL);
    pthread_cond_init(&loop->cond, NULL);
    
    return loop;
}

/**
 * 添加事件监听器
 * @param loop 事件循环
 * @param type 事件类型
 * @param handler 事件处理器
 * @param context 用户上下文
 * @param priority 处理优先级
 * @return 0成功,-1失败
 */
int event_loop_add_listener(event_loop_t *loop, event_type_t type,
                           event_handler_t handler, void *context, int priority) {
    if (!loop || !handler) return -1;
    
    pthread_mutex_lock(&loop->mutex);
    
    // 扩展监听器数组
    if (loop->listener_count >= loop->listener_capacity) {
        size_t new_capacity = loop->listener_capacity * 2;
        event_listener_t *new_listeners = realloc(loop->listeners, 
                                                 new_capacity * sizeof(event_listener_t));
        if (!new_listeners) {
            pthread_mutex_unlock(&loop->mutex);
            return -1;
        }
        loop->listeners = new_listeners;
        loop->listener_capacity = new_capacity;
    }
    
    // 添加新监听器
    event_listener_t *listener = &loop->listeners[loop->listener_count++];
    listener->type = type;
    listener->handler = handler;
    listener->context = context;
    listener->priority = priority;
    
    pthread_mutex_unlock(&loop->mutex);
    return 0;
}

/**
 * 发布事件
 * @param loop 事件循环
 * @param event 事件指针
 * @return 0成功,-1失败
 */
int event_loop_post(event_loop_t *loop, event_t *event) {
    if (!loop || !event) return -1;
    
    pthread_mutex_lock(&loop->mutex);
    
    // 检查队列是否已满
    if ((loop->queue_tail + 1) % loop->queue_capacity == loop->queue_head) {
        pthread_mutex_unlock(&loop->mutex);
        return -1;  // 队列已满
    }
    
    // 复制事件到队列
    event_t *queue_event = &loop->event_queue[loop->queue_tail];
    queue_event->type = event->type;
    queue_event->timestamp = event->timestamp;
    
    if (event->data && event->data_size > 0) {
        queue_event->data = malloc(event->data_size);
        if (queue_event->data) {
            memcpy(queue_event->data, event->data, event->data_size);
            queue_event->data_size = event->data_size;
        } else {
            queue_event->data_size = 0;
        }
    } else {
        queue_event->data = NULL;
        queue_event->data_size = 0;
    }
    
    loop->queue_tail = (loop->queue_tail + 1) % loop->queue_capacity;
    
    pthread_cond_signal(&loop->cond);
    pthread_mutex_unlock(&loop->mutex);
    
    return 0;
}

/**
 * 事件循环主函数
 * @param loop 事件循环
 */
void event_loop_run(event_loop_t *loop) {
    if (!loop) return;
    
    loop->running = 1;
    
    while (loop->running) {
        pthread_mutex_lock(&loop->mutex);
        
        // 等待事件
        while (loop->queue_head == loop->queue_tail && loop->running) {
            pthread_cond_wait(&loop->cond, &loop->mutex);
        }
        
        if (!loop->running) {
            pthread_mutex_unlock(&loop->mutex);
            break;
        }
        
        // 获取事件
        event_t event = loop->event_queue[loop->queue_head];
        loop->queue_head = (loop->queue_head + 1) % loop->queue_capacity;
        
        pthread_mutex_unlock(&loop->mutex);
        
        // 处理事件
        for (size_t i = 0; i < loop->listener_count; i++) {
            if (loop->listeners[i].type == event.type || 
                loop->listeners[i].type == EVENT_NONE) {  // EVENT_NONE监听所有事件
                loop->listeners[i].handler(&event, loop->listeners[i].context);
            }
        }
        
        // 清理事件数据
        if (event.data) {
            free(event.data);
        }
    }
}

/**
 * 停止事件循环
 * @param loop 事件循环
 */
void event_loop_stop(event_loop_t *loop) {
    if (!loop) return;
    
    pthread_mutex_lock(&loop->mutex);
    loop->running = 0;
    pthread_cond_signal(&loop->cond);
    pthread_mutex_unlock(&loop->mutex);
}

/**
 * 销毁事件循环
 * @param loop 事件循环
 */
void event_loop_destroy(event_loop_t *loop) {
    if (!loop) return;
    
    event_loop_stop(loop);
    
    if (loop->listeners) {
        free(loop->listeners);
    }
    
    // 清理事件队列中剩余的事件
    while (loop->queue_head != loop->queue_tail) {
        event_t *event = &loop->event_queue[loop->queue_head];
        if (event->data) {
            free(event->data);
        }
        loop->queue_head = (loop->queue_head + 1) % loop->queue_capacity;
    }
    
    if (loop->event_queue) {
        free(loop->event_queue);
    }
    
    pthread_mutex_destroy(&loop->mutex);
    pthread_cond_destroy(&loop->cond);
    
    free(loop);
}

无锁队列实现

/**
 * 无锁队列实现
 * 使用CAS操作实现线程安全的无锁队列
 */

#include <stdatomic.h>

/**
 * 队列节点结构
 */
typedef struct queue_node {
    void *data;
    _Atomic(struct queue_node*) next;
} queue_node_t;

/**
 * 无锁队列结构
 */
typedef struct {
    _Atomic(queue_node_t*) head;
    _Atomic(queue_node_t*) tail;
    atomic_size_t size;
} lockfree_queue_t;

/**
 * 创建无锁队列节点
 * @param data 节点数据
 * @return 节点指针
 */
static queue_node_t* create_queue_node(void *data) {
    queue_node_t *node = malloc(sizeof(queue_node_t));
    if (node) {
        node->data = data;
        atomic_init(&node->next, NULL);
    }
    return node;
}

/**
 * 创建无锁队列
 * @return 队列指针
 */
lockfree_queue_t* lockfree_queue_create() {
    lockfree_queue_t *queue = malloc(sizeof(lockfree_queue_t));
    if (!queue) return NULL;
    
    // 创建哨兵节点
    queue_node_t *dummy = create_queue_node(NULL);
    if (!dummy) {
        free(queue);
        return NULL;
    }
    
    atomic_init(&queue->head, dummy);
    atomic_init(&queue->tail, dummy);
    atomic_init(&queue->size, 0);
    
    return queue;
}

/**
 * 入队操作
 * @param queue 队列
 * @param data 数据
 * @return 0成功,-1失败
 */
int lockfree_queue_enqueue(lockfree_queue_t *queue, void *data) {
    if (!queue) return -1;
    
    queue_node_t *node = create_queue_node(data);
    if (!node) return -1;
    
    queue_node_t *prev_tail = NULL;
    queue_node_t *prev_tail_next = NULL;
    
    while (1) {
        prev_tail = atomic_load(&queue->tail);
        prev_tail_next = atomic_load(&prev_tail->next);
        
        // 检查tail是否一致
        if (prev_tail == atomic_load(&queue->tail)) {
            if (prev_tail_next == NULL) {
                // tail是最后一个节点,尝试链接新节点
                if (atomic_compare_exchange_weak(&prev_tail->next, &prev_tail_next, node)) {
                    break;  // 成功
                }
            } else {
                // tail不是最后一个节点,尝试推进tail
                atomic_compare_exchange_weak(&queue->tail, &prev_tail, prev_tail_next);
            }
        }
    }
    
    // 推进tail
    atomic_compare_exchange_weak(&queue->tail, &prev_tail, node);
    atomic_fetch_add(&queue->size, 1);
    
    return 0;
}

/**
 * 出队操作
 * @param queue 队列
 * @param data 输出参数:出队数据
 * @return 0成功,-1队列为空
 */
int lockfree_queue_dequeue(lockfree_queue_t *queue, void **data) {
    if (!queue || !data) return -1;
    
    queue_node_t *head = NULL;
    queue_node_t *tail = NULL;
    queue_node_t *next = NULL;
    
    while (1) {
        head = atomic_load(&queue->head);
        tail = atomic_load(&queue->tail);
        next = atomic_load(&head->next);
        
        // 检查head是否一致
        if (head == atomic_load(&queue->head)) {
            if (head == tail) {
                // 队列为空或只有一个哨兵节点
                if (next == NULL) {
                    *data = NULL;
                    return -1;  // 队列为空
                }
                // 队列正在变化,推进tail
                atomic_compare_exchange_weak(&queue->tail, &tail, next);
            } else {
                // 读取数据
                *data = next->data;
                // 尝试推进head
                if (atomic_compare_exchange_weak(&queue->head, &head, next)) {
                    atomic_fetch_sub(&queue->size, 1);
                    break;
                }
            }
        }
    }
    
    free(head);  // 释放旧的head节点
    return 0;
}

/**
 * 获取队列大小
 * @param queue 队列
 * @return 队列大小
 */
size_t lockfree_queue_size(lockfree_queue_t *queue) {
    return atomic_load(&queue->size);
}

/**
 * 销毁无锁队列
 * @param queue 队列
 */
void lockfree_queue_destroy(lockfree_queue_t *queue) {
    if (!queue) return;
    
    // 清空队列
    void *data;
    while (lockfree_queue_dequeue(queue, &data) == 0) {
        // 数据由调用者负责释放
    }
    
    // 释放哨兵节点
    queue_node_t *head = atomic_load(&queue->head);
    if (head) {
        free(head);
    }
    
    free(queue);
}

缓存友好的数据结构

/**
 * 缓存友好的数据结构实现
 * 优化内存布局以提高缓存命中率
 */

/**
 * 缓存行大小定义
 */
#define CACHE_LINE_SIZE 64

/**
 * 缓存对齐宏
 */
#define CACHE_ALIGNED __attribute__((aligned(CACHE_LINE_SIZE)))

/**
 * SoA (Structure of Arrays) 向量结构
 * 将相关数据分离存储以提高缓存效率
 */
typedef struct {
    float *x;           // X坐标数组
    float *y;           // Y坐标数组
    float *z;           // Z坐标数组
    int *id;            // ID数组
    size_t capacity;    // 容量
    size_t size;        // 当前大小
    char padding[CACHE_LINE_SIZE - sizeof(size_t)*2 - sizeof(char*)*4]; // 填充到缓存行边界
} soa_vector_t;

/**
 * AoS (Array of Structures) 向量结构
 * 传统结构体数组方式
 */
typedef struct {
    float x, y, z;
    int id;
} aos_point_t;

typedef struct {
    aos_point_t *points;
    size_t capacity;
    size_t size;
    char padding[CACHE_LINE_SIZE - sizeof(size_t)*2 - sizeof(void*)]; // 填充
} aos_vector_t;

/**
 * 创建SoA向量
 * @param initial_capacity 初始容量
 * @return SoA向量指针
 */
soa_vector_t* soa_vector_create(size_t initial_capacity) {
    soa_vector_t *vec = calloc(1, sizeof(soa_vector_t));
    if (!vec) return NULL;
    
    vec->capacity = initial_capacity;
    vec->x = malloc(sizeof(float) * initial_capacity);
    vec->y = malloc(sizeof(float) * initial_capacity);
    vec->z = malloc(sizeof(float) * initial_capacity);
    vec->id = malloc(sizeof(int) * initial_capacity);
    
    if (!vec->x || !vec->y || !vec->z || !vec->id) {
        soa_vector_destroy(vec);
        return NULL;
    }
    
    return vec;
}

/**
 * 创建AoS向量
 * @param initial_capacity 初始容量
 * @return AoS向量指针
 */
aos_vector_t* aos_vector_create(size_t initial_capacity) {
    aos_vector_t *vec = calloc(1, sizeof(aos_vector_t));
    if (!vec) return NULL;
    
    vec->capacity = initial_capacity;
    vec->points = malloc(sizeof(aos_point_t) * initial_capacity);
    if (!vec->points) {
        free(vec);
        return NULL;
    }
    
    return vec;
}

/**
 * SoA向量添加元素
 * @param vec SoA向量
 * @param x X坐标
 * @param y Y坐标
 * @param z Z坐标
 * @param id ID
 * @return 0成功,-1失败
 */
int soa_vector_push(soa_vector_t *vec, float x, float y, float z, int id) {
    if (!vec) return -1;
    
    // 检查是否需要扩容
    if (vec->size >= vec->capacity) {
        size_t new_capacity = vec->capacity * 2;
        
        float *new_x = realloc(vec->x, sizeof(float) * new_capacity);
        float *new_y = realloc(vec->y, sizeof(float) * new_capacity);
        float *new_z = realloc(vec->z, sizeof(float) * new_capacity);
        int *new_id = realloc(vec->id, sizeof(int) * new_capacity);
        
        if (!new_x || !new_y || !new_z || !new_id) {
            return -1;
        }
        
        vec->x = new_x;
        vec->y = new_y;
        vec->z = new_z;
        vec->id = new_id;
        vec->capacity = new_capacity;
    }
    
    size_t index = vec->size++;
    vec->x[index] = x;
    vec->y[index] = y;
    vec->z[index] = z;
    vec->id[index] = id;
    
    return 0;
}

/**
 * AoS向量添加元素
 * @param vec AoS向量
 * @param x X坐标
 * @param y Y坐标
 * @param z Z坐标
 * @param id ID
 * @return 0成功,-1失败
 */
int aos_vector_push(aos_vector_t *vec, float x, float y, float z, int id) {
    if (!vec) return -1;
    
    // 检查是否需要扩容
    if (vec->size >= vec->capacity) {
        size_t new_capacity = vec->capacity * 2;
        aos_point_t *new_points = realloc(vec->points, sizeof(aos_point_t) * new_capacity);
        if (!new_points) return -1;
        
        vec->points = new_points;
        vec->capacity = new_capacity;
    }
    
    aos_point_t *point = &vec->points[vec->size++];
    point->x = x;
    point->y = y;
    point->z = z;
    point->id = id;
    
    return 0;
}

/**
 * SoA向量批量处理(缓存友好)
 * @param vec SoA向量
 * @param processor 处理函数
 * @param context 用户上下文
 */
void soa_vector_process(soa_vector_t *vec, 
                       void (*processor)(float x, float y, float z, int id, void *context),
                       void *context) {
    if (!vec || !processor) return;
    
    // 分别处理每个数组,提高缓存命中率
    for (size_t i = 0; i < vec->size; i++) {
        processor(vec->x[i], vec->y[i], vec->z[i], vec->id[i], context);
    }
}

/**
 * AoS向量批量处理
 * @param vec AoS向量
 * @param processor 处理函数
 * @param context 用户上下文
 */
void aos_vector_process(aos_vector_t *vec,
                       void (*processor)(float x, float y, float z, int id, void *context),
                       void *context) {
    if (!vec || !processor) return;
    
    // 处理结构体数组
    for (size_t i = 0; i < vec->size; i++) {
        aos_point_t *point = &vec->points[i];
        processor(point->x, point->y, point->z, point->id, context);
    }
}

/**
 * 销毁SoA向量
 * @param vec SoA向量
 */
void soa_vector_destroy(soa_vector_t *vec) {
    if (!vec) return;
    
    if (vec->x) free(vec->x);
    if (vec->y) free(vec->y);
    if (vec->z) free(vec->z);
    if (vec->id) free(vec->id);
    free(vec);
}

/**
 * 销毁AoS向量
 * @param vec AoS向量
 */
void aos_vector_destroy(aos_vector_t *vec) {
    if (!vec) return;
    
    if (vec->points) free(vec->points);
    free(vec);
}

/**
 * 性能测试结构
 */
typedef struct {
    double soa_time;
    double aos_time;
    size_t elements_processed;
} performance_result_t;

安全字符串操作库

/**
 * 安全字符串操作库
 * 提供防止缓冲区溢出的安全字符串函数
 */

#include <string.h>
#include <stdio.h>
#include <stdarg.h>

/**
 * 安全字符串结构
 * 包含长度信息以防止溢出
 */
typedef struct {
    char *data;
    size_t length;
    size_t capacity;
    int is_secure;  // 是否启用安全检查
} secure_string_t;

/**
 * 创建安全字符串
 * @param initial_capacity 初始容量
 * @param enable_security 是否启用安全检查
 * @return 安全字符串指针
 */
secure_string_t* secure_string_create(size_t initial_capacity, int enable_security) {
    secure_string_t *str = calloc(1, sizeof(secure_string_t));
    if (!str) return NULL;
    
    str->data = malloc(initial_capacity + 1);  // +1 for null terminator
    if (!str->data) {
        free(str);
        return NULL;
    }
    
    str->data[0] = '\0';
    str->capacity = initial_capacity;
    str->is_secure = enable_security;
    
    return str;
}

/**
 * 从C字符串创建安全字符串
 * @param c_str C字符串
 * @param enable_security 是否启用安全检查
 * @return 安全字符串指针
 */
secure_string_t* secure_string_from_cstr(const char *c_str, int enable_security) {
    if (!c_str) return NULL;
    
    size_t len = strlen(c_str);
    secure_string_t *str = secure_string_create(len, enable_security);
    if (str) {
        strncpy(str->data, c_str, str->capacity);
        str->data[str->capacity] = '\0';
        str->length = strlen(str->data);
    }
    
    return str;
}

/**
 * 安全字符串追加
 * @param str 目标字符串
 * @param append_str 要追加的字符串
 * @return 0成功,-1失败
 */
int secure_string_append(secure_string_t *str, const char *append_str) {
    if (!str || !append_str) return -1;
    
    size_t append_len = strlen(append_str);
    size_t new_length = str->length + append_len;
    
    // 检查是否需要扩容
    if (new_length >= str->capacity) {
        if (str->is_secure) {
            // 安全模式:拒绝超出容量的操作
            return -1;
        } else {
            // 非安全模式:自动扩容
            size_t new_capacity = (new_length + 1) * 2;
            char *new_data = realloc(str->data, new_capacity + 1);
            if (!new_data) return -1;
            
            str->data = new_data;
            str->capacity = new_capacity;
        }
    }
    
    // 执行追加
    strncat(str->data, append_str, str->capacity - str->length);
    str->length = strlen(str->data);
    
    return 0;
}

/**
 * 安全格式化字符串
 * @param str 目标字符串
 * @param format 格式字符串
 * @param ... 可变参数
 * @return 写入的字符数,-1失败
 */
int secure_string_printf(secure_string_t *str, const char *format, ...) {
    if (!str || !format) return -1;
    
    va_list args;
    va_start(args, format);
    
    // 首先计算需要的空间
    va_list args_copy;
    va_copy(args_copy, args);
    int needed = vsnprintf(NULL, 0, format, args_copy);
    va_end(args_copy);
    
    if (needed < 0) {
        va_end(args);
        return -1;
    }
    
    // 检查容量
    if ((size_t)needed >= str->capacity - str->length) {
        if (str->is_secure) {
            va_end(args);
            return -1;  // 容量不足
        } else {
            // 自动扩容
            size_t new_capacity = str->length + needed + 1;
            char *new_data = realloc(str->data, new_capacity + 1);
            if (!new_data) {
                va_end(args);
                return -1;
            }
            
            str->data = new_data;
            str->capacity = new_capacity;
        }
    }
    
    // 执行格式化
    int written = vsnprintf(str->data + str->length, 
                           str->capacity - str->length, 
                           format, args);
    
    if (written >= 0) {
        str->length += written;
    }
    
    va_end(args);
    return written;
}

/**
 * 安全字符串比较
 * @param str1 字符串1
 * @param str2 字符串2
 * @return 比较结果
 */
int secure_string_compare(const secure_string_t *str1, const secure_string_t *str2) {
    if (!str1 && !str2) return 0;
    if (!str1) return -1;
    if (!str2) return 1;
    
    return strcmp(str1->data, str2->data);
}

/**
 * 获取C字符串
 * @param str 安全字符串
 * @return C字符串指针
 */
const char* secure_string_cstr(const secure_string_t *str) {
    return str ? str->data : NULL;
}

/**
 * 获取字符串长度
 * @param str 安全字符串
 * @return 字符串长度
 */
size_t secure_string_length(const secure_string_t *str) {
    return str ? str->length : 0;
}

/**
 * 清空字符串
 * @param str 安全字符串
 */
void secure_string_clear(secure_string_t *str) {
    if (str && str->data) {
        str->data[0] = '\0';
        str->length = 0;
    }
}

/**
 * 销毁安全字符串
 * @param str 安全字符串
 */
void secure_string_destroy(secure_string_t *str) {
    if (!str) return;
    
    if (str->data) {
        // 安全清除内存
        memset(str->data, 0, str->capacity);
        free(str->data);
    }
    
    free(str);
}

/**
 * 安全字符串池
 * 管理多个安全字符串以提高性能
 */
typedef struct {
    secure_string_t **strings;
    size_t count;
    size_t capacity;
    pthread_mutex_t mutex;
} string_pool_t;

/**
 * 创建字符串池
 * @param initial_capacity 初始容量
 * @return 字符串池指针
 */
string_pool_t* string_pool_create(size_t initial_capacity) {
    string_pool_t *pool = calloc(1, sizeof(string_pool_t));
    if (!pool) return NULL;
    
    pool->strings = calloc(initial_capacity, sizeof(secure_string_t*));
    if (!pool->strings) {
        free(pool);
        return NULL;
    }
    
    pool->capacity = initial_capacity;
    pthread_mutex_init(&pool->mutex, NULL);
    
    return pool;
}

综合演示函数

/**
 * 事件驱动架构演示示例
 */
void demo_event_driven_architecture() {
    printf("=== 事件驱动架构演示 ===\n");
    
    // 创建事件循环
    event_loop_t *loop = event_loop_create(100);
    if (!loop) {
        printf("Failed to create event loop\n");
        return;
    }
    
    // 添加事件监听器
    event_loop_add_listener(loop, EVENT_TIMER, timer_handler, NULL, 0);
    event_loop_add_listener(loop, EVENT_NETWORK, network_handler, NULL, 0);
    event_loop_add_listener(loop, EVENT_USER, user_handler, NULL, 0);
    
    // 启动事件循环线程
    pthread_t loop_thread;
    pthread_create(&loop_thread, NULL, (void*(*)(void*))event_loop_run, loop);
    
    // 发布一些测试事件
    for (int i = 0; i < 5; i++) {
        event_t event = {0};
        event.timestamp = time(NULL);
        
        // 发布不同类型的事件
        switch (i % 3) {
            case 0:
                event.type = EVENT_TIMER;
                printf("Posting timer event %d\n", i);
                break;
            case 1: {
                event.type = EVENT_NETWORK;
                const char *msg = "Hello Network!";
                event.data = strdup(msg);
                event.data_size = strlen(msg);
                printf("Posting network event %d\n", i);
                break;
            }
            case 2:
                event.type = EVENT_USER;
                event.data = malloc(sizeof(int));
                *(int*)event.data = i;
                event.data_size = sizeof(int);
                printf("Posting user event %d\n", i);
                break;
        }
        
        event_loop_post(loop, &event);
        sleep(1);
    }
    
    // 清理事件数据
    sleep(2);
    
    // 停止并销毁事件循环
    event_loop_stop(loop);
    pthread_join(loop_thread, NULL);
    event_loop_destroy(loop);
    
    printf("=== 演示完成 ===\n\n");
}

/**
 * 无锁队列演示示例
 */
void demo_lockfree_queue() {
    printf("=== 无锁队列演示 ===\n");
    
    // 初始化
    queue = lockfree_queue_create();
    atomic_init(&items_produced, 0);
    atomic_init(&items_consumed, 0);
    
    if (!queue) {
        printf("Failed to create queue\n");
        return;
    }
    
    // 创建生产者和消费者线程
    pthread_t producers[NUM_PRODUCERS];
    pthread_t consumers[NUM_CONSUMERS];
    int producer_ids[NUM_PRODUCERS];
    int consumer_ids[NUM_CONSUMERS];
    
    // 启动生产者线程
    for (int i = 0; i < NUM_PRODUCERS; i++) {
        producer_ids[i] = i;
        pthread_create(&producers[i], NULL, producer_thread, &producer_ids[i]);
    }
    
    // 启动消费者线程
    for (int i = 0; i < NUM_CONSUMERS; i++) {
        consumer_ids[i] = i;
        pthread_create(&consumers[i], NULL, consumer_thread, &consumer_ids[i]);
    }
    
    // 等待所有线程完成
    for (int i = 0; i < NUM_PRODUCERS; i++) {
        pthread_join(producers[i], NULL);
    }
    
    for (int i = 0; i < NUM_CONSUMERS; i++) {
        pthread_join(consumers[i], NULL);
    }
    
    // 显示结果
    printf("Total produced: %d\n", atomic_load(&items_produced));
    printf("Total consumed: %d\n", atomic_load(&items_consumed));
    printf("Queue size: %zu\n", lockfree_queue_size(queue));
    
    // 清理
    lockfree_queue_destroy(queue);
    
    printf("=== 演示完成 ===\n\n");
}

/**
 * 缓存友好数据结构演示示例
 */
void demo_cache_friendly_structures() {
    printf("=== 缓存友好数据结构演示 ===\n");
    
    // 测试不同规模的数据
    size_t test_sizes[] = {1000, 10000, 100000, 1000000};
    int num_tests = sizeof(test_sizes) / sizeof(test_sizes[0]);
    
    printf("%-10s %-12s %-12s %-10s\n", "Elements", "SoA Time(s)", "AoS Time(s)", "Speedup");
    printf("------------------------------------------------\n");
    
    for (int i = 0; i < num_tests; i++) {
        performance_result_t result = test_performance(test_sizes[i]);
        
        double speedup = result.aos_time / result.soa_time;
        printf("%-10zu %-12.6f %-12.6f %-10.2fx\n", 
               result.elements_processed,
               result.soa_time,
               result.aos_time,
               speedup);
    }
    
    printf("=== 演示完成 ===\n\n");
}

/**
 * 安全字符串操作演示示例
 */
void demo_secure_strings() {
    printf("=== 安全字符串操作演示 ===\n");
    
    // 创建安全字符串(启用安全检查)
    secure_string_t *str1 = secure_string_create(20, 1);  // 安全模式
    secure_string_t *str2 = secure_string_from_cstr("Hello", 1);
    
    if (!str1 || !str2) {
        printf("Failed to create secure strings\n");
        return;
    }
    
    printf("Initial strings:\n");
    printf("str1: '%s' (length: %zu)\n", secure_string_cstr(str1), secure_string_length(str1));
    printf("str2: '%s' (length: %zu)\n", secure_string_cstr(str2), secure_string_length(str2));
    
    // 安全追加
    if (secure_string_append(str2, " World!") == 0) {
        printf("After append: '%s'\n", secure_string_cstr(str2));
    } else {
        printf("Append failed (security check)\n");
    }
    
    // 安全格式化
    if (secure_string_printf(str1, "Number: %d, String: %s", 42, "test") >= 0) {
        printf("Formatted string: '%s'\n", secure_string_cstr(str1));
    } else {
        printf("Format failed (security check)\n");
    }
    
    // 尝试超出容量的操作(在安全模式下会失败)
    printf("\nTesting security checks:\n");
    if (secure_string_append(str1, "This is a very long string that exceeds capacity") == -1) {
        printf("Security check prevented buffer overflow!\n");
    }
    
    // 字符串比较
    secure_string_t *str3 = secure_string_from_cstr("Hello World!", 1);
    printf("Comparison result: %d\n", secure_string_compare(str2, str3));
    
    // 清理
    secure_string_destroy(str1);
    secure_string_destroy(str2);
    secure_string_destroy(str3);
    
    printf("=== 演示完成 ===\n\n");
}

// 综合演示函数
void run_all_demos() {
    printf("C语言高级编程技巧演示\n");
    printf("=====================\n\n");
    
    // 运行所有演示
    demo_event_driven_architecture();
    demo_lockfree_queue();
    demo_cache_friendly_structures();
    demo_secure_strings();
    
    printf("所有演示完成!\n");
}

附录:最佳实践总结

编码规范

  1. 命名约定:使用清晰的命名,避免缩写
  2. 注释风格:使用Doxygen风格注释
  3. 错误处理:始终检查返回值
  4. 内存管理:遵循RAII原则
  5. 线程安全:明确标识线程安全函数

性能优化原则

  1. 先测量后优化:使用性能分析工具
  2. 算法优先:选择合适的数据结构和算法
  3. 避免过早优化:保持代码可读性
  4. 缓存友好:考虑数据局部性
  5. 编译器优化:合理使用编译器优化选项

安全编码原则

  1. 输入验证:永远不要信任外部输入
  2. 边界检查:防止缓冲区溢出
  3. 最小权限:使用最小必要权限
  4. 安全函数:使用安全的字符串函数
  5. 代码审查:定期进行安全代码审查

这份完整的C语言高级编程技巧指南涵盖了从基础宏定义到复杂并发编程的所有重要方面,提供了丰富的代码示例和最佳实践,帮助开发者编写高质量、高性能、安全的C代码。

发表在 linux文章 | 留下评论

Python二进制文件编码探测工具

背景
实现基于python语言cchardet库的二进制文件分析程序,按照预设分段参数对文件进行读取和cchardet的文本编码探测。脚本具备跳过文件头n字节,按照m字节分段二进制文件及分段后数据连续4字节探测功能。
结果输出会展示每段的序号,偏移起始,片内置信度识别偏移字节,片大小,编码方式,置信度,高置信度提示信息字段;

如何使用脚本:

# 1. 基本用法:分析整个文件
python encoding_detector.py myfile.bin

# 2. 指定块大小
python encoding_detector.py -s 512 myfile.bin

# 3. 跳过每个块的前 10 个字节
python encoding_detector.py -s 100 -h 10 myfile.bin

# 4. 从文件偏移 1116 开始分析
python encoding_detector.py -s 100 -o 1116 ../ftp-pcap/ftp-utf8-long.pcap

# 5. 结合使用:从偏移 1000 开始,每块 256 字节,跳过每块前 20 字节
python encoding_detector.py -s 256 -h 20 -o 1000 myfile.bin

# 6. 通过管道输入
cat myfile.bin | python encoding_detector.py -s 512

Python脚本是实现

#!/usr/bin/env python3
import cchardet
import sys
import os

def print_hex(data, width=16):
    """以十六进制和ASCII形式打印字节数据"""
    for i in range(0, len(data), width):
        # 十六进制部分
        hex_part = ' '.join(f'{byte:02x}' for byte in data[i:i+width])
        # ASCII部分 (可打印字符或'.')
        ascii_part = ''.join(chr(byte) if 32 <= byte <= 126 else '.' for byte in data[i:i+width])
        # 打印地址偏移、十六进制和ASCII
        print(f'{i:08x}: {hex_part:<{width*3}} |{ascii_part}|')

def detect_chunks_from_file(filename, chunk_size=1024, from_head_bytes=0, from_file_offset=0):
    """
    将文件按指定大小切块,并对每个块进行编码检测。
    如果检测置信度为0,则尝试偏移1-4字节重新检测。
    from_file_offset: 从文件的哪个字节偏移开始读取。
    """
    if not os.path.exists(filename):
        print(f"Error: File '{filename}' does not exist.", file=sys.stderr)
        return

    try:
        file_size = os.path.getsize(filename)
        print(f"Analyzing file: {filename} (Total size: {file_size} bytes)")
        print(f"Chunk size: {chunk_size} bytes")
        if from_head_bytes > 0:
            print(f"Skipping first {from_head_bytes} bytes of each chunk for detection.")
        if from_file_offset > 0:
            print(f"Starting analysis from file offset: {from_file_offset}")
        print("-" * 50)

        with open(filename, 'rb') as f:
            # 定位到文件的起始偏移
            if from_file_offset > 0:
                f.seek(from_file_offset)
            
            chunk_number = 0
            while True:
                chunk_data = f.read(chunk_size)
                if not chunk_
                    break

                # 计算当前块在原始文件中的基础偏移量
                offset = from_file_offset + chunk_number * chunk_size

                # 裁剪用于检测的数据(跳过头部字节)
                detection_data = chunk_data[from_head_bytes:] if len(chunk_data) > from_head_bytes else b''

                # --- 初始检测 ---
                encoding = None
                confidence = 0.0
                offset_by_used = 0 # 记录最终使用的偏移量

                if len(detection_data) > 0:
                    try:
                        result = cchardet.detect(detection_data)
                        if isinstance(result, dict):
                            encoding = result.get('encoding')
                            temp_confidence = result.get('confidence')
                            if temp_confidence is None:
                                confidence = 0.0
                            else:
                                confidence = temp_confidence
                            
                            if encoding is not None and not isinstance(encoding, str):
                                print(f"Warning: Unexpected encoding type in chunk {chunk_number}: {type(encoding)}", file=sys.stderr)
                                encoding = str(encoding) if encoding is not None else None
                        else:
                            print(f"Warning: cchardet returned unexpected type in chunk {chunk_number}: {type(result)}", file=sys.stderr)
                    except Exception as e:
                        print(f"Warning: cchardet failed on chunk {chunk_number}: {e}", file=sys.stderr)
                        encoding = "Error"
                        confidence = 0.0

                # --- 偏移优化逻辑 ---
                max_offset_attempts = 4
                if confidence == 0.0 and len(detection_data) > max_offset_attempts:
                    for offset_by in range(1, max_offset_attempts + 1):
                        if len(detection_data) > offset_by:
                            adjusted_detection_data = detection_data[offset_by:]
                            if len(adjusted_detection_data) > 0:
                                try:
                                    adjusted_result = cchardet.detect(adjusted_detection_data)
                                    if isinstance(adjusted_result, dict):
                                        adjusted_confidence = adjusted_result.get('confidence')
                                        if adjusted_confidence is None:
                                            adjusted_confidence = 0.0
                                        
                                        if adjusted_confidence > confidence:
                                            encoding = adjusted_result.get('encoding')
                                            confidence = adjusted_confidence
                                            offset_by_used = offset_by # 记录使用的偏移量
                                            
                                            if confidence > 0.0:
                                                break
                                except Exception:
                                    pass
                        else:
                            break

                # --- 格式化输出 ---
                encoding_display = encoding if encoding is not None else "N/A"
                output_line = (f"Chunk {chunk_number:4d} | Offset {offset:8d} | "
                               f"offset_by {offset_by_used:2d} | "
                               f"Size {len(chunk_data):4d} | "
                               f"Encoding: {encoding_display:>12} | "
                               f"Confidence: {confidence:6.4f}")
                
                # 可以根据置信度调整输出格式,例如高亮高置信度结果
                if confidence >= 0.75:
                     print(output_line) # 或用不同颜色/符号标记,这里简化为普通打印
                else:
                     print(output_line)

                # 如果置信度为0,可以选择打印数据内容(当前被注释掉)
                # if confidence == 0.0 and len(chunk_data) > 0:
                #     print ("\n")
                #     print_hex(chunk_data)
                #     print ("\n")
                    
                chunk_number += 1

            # 文件读取结束后的检查
            # f.tell() 在 seek 后返回的是绝对位置
            absolute_tell = f.tell()
            if absolute_tell < file_size:
                 print(f"Warning: Stopped reading before end of file '{filename}'. "
                      f"Read up to file offset {absolute_tell} bytes out of {file_size} bytes.", file=sys.stderr)

    except IOError as e:
        print(f"Error reading file '{filename}': {e}", file=sys.stderr)
    except Exception as e:
        print(f"An unexpected error occurred while processing '{filename}': {e}", file=sys.stderr)
    
    print("-" * 50 + f" Analysis of '{filename}' finished. " + "-" * 10 + "\n")


def detect_chunks_from_bytes(data, source_name="Byte Input", chunk_size=1024, from_head_bytes=0):
    """
    将字节数据按指定大小切块,并对每个块进行编码检测。
    如果检测置信度为0,则尝试偏移1-3字节重新检测。
    """
    data_len = len(data)
    print(f"Analyzing data from: {source_name} (Total size: {data_len} bytes)")
    print(f"Chunk size: {chunk_size} bytes")
    if from_head_bytes > 0:
        print(f"Skipping first {from_head_bytes} bytes of each chunk for detection.")
    print("-" * 50)

    if data_len == 0:
        print("Input data is empty.")
        return

    chunk_number = 0
    for i in range(0, data_len, chunk_size):
        chunk_data = data[i:i + chunk_size]
        if not chunk_
            break

        offset = i
        detection_data = chunk_data[from_head_bytes:] if len(chunk_data) > from_head_bytes else b''

        encoding = None
        confidence = 0.0
        
        if len(detection_data) > 0:
            try:
                result = cchardet.detect(detection_data)
                if isinstance(result, dict):
                    encoding = result.get('encoding')
                    temp_confidence = result.get('confidence')
                    if temp_confidence is None:
                        confidence = 0.0
                    else:
                        confidence = temp_confidence
                    
                    if encoding is not None and not isinstance(encoding, str):
                        print(f"Warning: Unexpected encoding type in chunk {chunk_number}: {type(encoding)}", file=sys.stderr)
                        encoding = str(encoding) if encoding is not None else None
                else:
                    print(f"Warning: cchardet returned unexpected type in chunk {chunk_number}: {type(result)}", file=sys.stderr)
            except Exception as e:
                print(f"Warning: cchardet failed on chunk {chunk_number}: {e}", file=sys.stderr)
                encoding = "Error"
                confidence = 0.0

        # --- 偏移优化逻辑 (针对 bytes 输入)---
        max_offset_attempts = 3
        offset_by_used = 0
        if confidence == 0.0 and len(detection_data) > max_offset_attempts:
            for offset_by in range(1, max_offset_attempts + 1):
                if len(detection_data) > offset_by:
                    adjusted_detection_data = detection_data[offset_by:]
                    if len(adjusted_detection_data) > 0:
                        try:
                            adjusted_result = cchardet.detect(adjusted_detection_data)
                            if isinstance(adjusted_result, dict):
                                adjusted_confidence = adjusted_result.get('confidence')
                                if adjusted_confidence is None:
                                    adjusted_confidence = 0.0
                                
                                if adjusted_confidence > confidence:
                                    encoding = adjusted_result.get('encoding')
                                    confidence = adjusted_confidence
                                    offset_by_used = offset_by
                                    
                                    if confidence > 0.0:
                                        break
                        except Exception:
                            pass
                else:
                    break

        # 格式化输出 (bytes 输入也显示 offset_by)
        encoding_display = encoding if encoding is not None else "N/A"
        print(f"Chunk {chunk_number:4d} | Offset {offset:8d} | "
              f"offset_by {offset_by_used:2d} | " # 添加 offset_by 显示
              f"Size {len(chunk_data):4d} | "
              f"Encoding: {encoding_display:>12} | "
              f"Confidence: {confidence:6.4f}")

        # 如果置信度为0,打印数据内容
        # if confidence == 0.0 and len(chunk_data) > 0:
        #     print ("\n")
        #     print_hex(chunk_data)
        #     print ("\n")

        chunk_number += 1

    print("-" * 50 + f" Analysis of '{source_name}' finished. " + "-" * 10 + "\n")


def main():
    """
    主函数,处理命令行参数并调用相应的检测函数。
    """
    if len(sys.argv) < 2:
        print("No filename provided. Reading binary data from STDIN...", file=sys.stderr)
        try:
            data = sys.stdin.buffer.read()
            detect_chunks_from_bytes(data, source_name="STDIN", chunk_size=1024)
        except KeyboardInterrupt:
            print("\nInterrupted by user.", file=sys.stderr)
        except Exception as e:
            print(f"Error reading from STDIN: {e}", file=sys.stderr)
        sys.exit(0)

    # 默认参数
    chunk_size = 1024
    from_head_bytes = 0
    from_file_offset = 0 # 新增默认参数
    filenames = []

    # 解析命令行参数
    i = 1
    while i < len(sys.argv):
        if sys.argv[i] == '-s':
            if i + 1 < len(sys.argv):
                try:
                    chunk_size = int(sys.argv[i + 1])
                    if chunk_size <= 0:
                        raise ValueError("Chunk size must be positive.")
                    i += 2
                except ValueError as e:
                    print(f"Error: Invalid chunk size '-s {sys.argv[i + 1]}': {e}", file=sys.stderr)
                    sys.exit(1)
            else:
                print("Error: Option '-s' requires an argument.", file=sys.stderr)
                sys.exit(1)
        elif sys.argv[i] == '-h':
             if i + 1 < len(sys.argv):
                try:
                    from_head_bytes = int(sys.argv[i + 1])
                    if from_head_bytes < 0:
                        raise ValueError("Head bytes to skip must be non-negative.")
                    i += 2
                except ValueError as e:
                    print(f"Error: Invalid head bytes '-h {sys.argv[i + 1]}': {e}", file=sys.stderr)
                    sys.exit(1)
             else:
                print("Error: Option '-h' requires an argument.", file=sys.stderr)
                sys.exit(1)
        # --- 新增:解析 -o 参数 ---
        elif sys.argv[i] == '-o':
             if i + 1 < len(sys.argv):
                try:
                    from_file_offset = int(sys.argv[i + 1])
                    if from_file_offset < 0:
                        raise ValueError("File offset must be non-negative.")
                    i += 2
                except ValueError as e:
                    print(f"Error: Invalid file offset '-o {sys.argv[i + 1]}': {e}", file=sys.stderr)
                    sys.exit(1)
             else:
                print("Error: Option '-o' requires an argument.", file=sys.stderr)
                sys.exit(1)
        # --- 新增结束 ---
        else:
            filenames.append(sys.argv[i])
            i += 1

    if not filenames:
        print("Error: No filename provided.", file=sys.stderr)
        sys.exit(1)

    # 对每个提供的文件进行处理
    for filename in filenames:
        # --- 修改:传递 from_file_offset 参数 ---
        detect_chunks_from_file(filename, chunk_size, from_head_bytes, from_file_offset)


if __name__ == "__main__":
    main()
发表在 linux文章 | 留下评论

​​Text Encoding Design: A Complex and Historically Rich Process​​

​Text Encoding Design: A Complex and Historically Rich Process​

The design of text encoding is a complex and historically rich process aimed at representing the characters of the world’s diverse languages using limited digital units (typically 8-bit bytes).

​Core Design Principles​
Text encoding design revolves around the following key goals:

  • ​Expressiveness​​: Capable of representing all characters in a target language or character set.
  • ​Compatibility​​: Maximizing compatibility with existing standards, especially ASCII.
  • ​Efficiency​​: Optimizing storage, transmission, and processing speed.
  • ​Standardization​​: Requiring widely accepted and implemented standards.

​Major Encoding Types and Their Byte Designs​

  1. ​Single-Byte Character Sets (SBCS)​
    • ​Design​​: Each character uses one byte (8 bits).
    • ​Capacity​​: One byte offers 256 possible values (2⁸). Values 0x00–0x7F are typically reserved for ASCII (0–127), while 0x80–0xFF represent extended characters.
    • ​Examples​​:
      • ASCII: Basic encoding for English, digits, punctuation, and control characters (0x00–0x7F).
      • ISO/IEC 8859 Series (Latin-1, Latin-2, …): Extends ASCII to support Western/Central European characters (0x80–0xFF).
      • Windows-1252: Microsoft’s extension of Latin-1, redefining unused control characters in 0x80–0xFF.
    • ​Advantages​​:
      • Simple and efficient: Fixed one-byte-per-character storage; fast processing.
      • Backward compatibility with ASCII.
    • ​Disadvantages​​:
      • Extremely limited expressiveness: Only 256 characters possible—insufficient for languages like Chinese, Japanese, or Arabic (which require thousands).
  2. ​Multi-Byte Character Sets (MBCS)​
    To address SBCS limitations, MBCS uses variable-length byte sequences. ​​A. Double-Byte Character Sets (DBCS)​
    • ​Design​​: Primarily two bytes per character; sometimes one byte for ASCII.
    • ​Examples​​:
      • Shift JIS (SJIS) (Japanese): Lead bytes (0x81–0x9F, 0xE0–0xFC); trail bytes (0x40–0x7E, 0x80–0xFC).
      • GBK/GB2312 (Simplified Chinese): Lead bytes (0x81–0xFE); trail bytes (0x40–0x7E, 0x80–0xFE).
      • Big5 (Traditional Chinese): Lead bytes (0x81–0xFE); trail bytes (0x40–0x7E, 0xA1–0xFE).
    • ​Advantages​​:
      • Expanded expressiveness: Supports tens of thousands of characters.
      • Backward compatible with ASCII.
    • ​Disadvantages​​:
      • State-dependent parsing: Complexity increases as parsers must track byte context (e.g., ASCII vs. lead byte).
      • Synchronization issues: A missing/inserted byte corrupts subsequent characters until the next ASCII byte.
    ​B. Truly Variable-Length Multi-Byte Encodings​
    • ​Design​​: Characters use 1–4+ bytes, with self-synchronization—any byte’s value indicates whether it starts a new character or continues an existing one.
    • ​Examples​​:
      • UTF-8 (most successful):
        • 1-byte: 0xxxxxxx (0x00–0x7F) — full ASCII compatibility.
        • 2-byte: 110xxxxx 10xxxxxx (Latin/Greek/Cyrillic supplements).
        • 3-byte: 1110xxxx 10xxxxxx 10xxxxxx (most CJK characters).
        • 4-byte: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx (emoji, rarer CJK).
    • ​Advantages​​:
      • Self-synchronization: Robust parsing from any position.
      • Perfect ASCII compatibility.
      • Massive expressiveness: Covers all Unicode characters (>1 million code points).
      • Efficiency: Matches ASCII storage for English-dominated text.
    • ​Disadvantages​​:
      • Lower storage efficiency for non-ASCII text: e.g., Chinese requires 3 bytes in UTF-8 vs. 2 in UTF-16 (Basic Multilingual Plane).
  3. ​Fixed-Width Multi-Byte Encodings​
    • ​Design​​: Fixed bytes per character (2 or 4).
    • ​Examples​​:
      • UTF-16:
        • Basic Multilingual Plane (BMP) characters: 2 bytes (covers most CJK).
        • Supplementary Planes: 4 bytes (via surrogate pairs).
      • UTF-32: All characters use 4 bytes.
    • ​Advantages​​:
      • UTF-32: Simple fixed-width processing (one character = one integer).
    • ​Disadvantages​​:
      • UTF-16: Not truly fixed-width; ASCII doubles in size (2 bytes).
      • UTF-32: Low storage efficiency (ASCII uses 4× more space than UTF-8).

​Summary​

​Encoding Type​​Bytes/Char​​Design​​Advantages​​Disadvantages​
Single-Byte (ASCII)1FixedSimple, efficient, good compatibilityExtremely limited expressiveness
Single-Byte Extended (Latin-1)1FixedSimple, efficient, ASCII-compatibleLimited expressiveness; language conflicts
Double-Byte (SJIS, GBK)1 or 2Variable (mostly 2)High expressiveness, ASCII-compatibleComplex parsing; sync issues
Variable Multi-Byte (UTF-8)1–4Variable, self-synchronizingSelf-syncing, ASCII-compatible, universalSuboptimal for non-ASCII storage
Fixed Multi-Byte (UTF-16)2 or 4Variable (mostly 2)High BMP efficiencyNot fixed-width; ASCII-inefficient
Fixed Multi-Byte (UTF-32)4FixedProcessing simplicityVery low storage efficiency

​Modern Adoption​
UTF-8 dominates modern text processing (especially in internationalized software and the web) due to its optimal balance of compatibility, expressiveness, and efficiency. UTF-16 is common in systems like Windows, Java, and .NET. UTF-32’s inefficiency limits its use. Legacy SBCS/DBCS encodings persist in older systems.


发表在 linux文章 | 留下评论

文本编码的设计思路

文本编码的设计是一个复杂且历史悠久的过程,旨在用有限的数字(通常是 8 位字节)来表示世界上各种语言的字符。

核心设计思路

文本编码的设计主要围绕以下几个目标:

  1. 表示能力:能够表示目标语言或字符集中的所有字符。
  2. 兼容性:尽可能与已有的标准(尤其是 ASCII)兼容。
  3. 效率:存储和传输的效率,以及处理速度。
  4. 标准化:需要被广泛接受和实现的标准。

主要编码类型及其字节设计

1. 单字节编码 (Single-Byte Character Sets – SBCS)

  • 设计:每个字符用一个字节(8 位)表示。
  • 容量:一个字节有 256 (2^8) 个可能的值。通常 0x00-0x7F 被保留给 ASCII 字符(0-127),剩下 128 个值(0x80-0xFF)用于表示其他字符。
  • 示例
    • ASCII:最基础的编码,只使用 0x00-0x7F 表示英文字符、数字、标点和控制字符。
    • ISO/IEC 8859 系列(Latin-1, Latin-2, …):扩展 ASCII,用 0x80-0xFF 表示西欧、中欧等地区的字符。
    • Windows-1252:Windows 对 Latin-1 的扩展,填充了 0x80-0xFF 中一些在 Latin-1 中未定义或定义为控制字符的位置。
  • 优点
    • 简单高效:字符与字节一一对应,处理速度快,存储空间固定。
    • 向后兼容 ASCII:所有 ASCII 文本也是有效的 Latin-1/Windows-1252 文本。
  • 缺点
    • 表示能力有限:只能表示最多 256 个字符,远远不足以表示像中文、日文、阿拉伯文等拥有成千上万个字符的语言。

2. 多字节编码 (Multi-Byte Character Sets – MBCS)

为了解决单字节编码表示能力不足的问题,多字节编码应运而生。其核心思想是使用变长的字节序列来表示不同的字符。

A. 双字节编码 (Double-Byte Character Sets – DBCS)

  • 设计:主要使用两个字节来表示一个字符,有时也用一个字节表示 ASCII 字符。
  • 示例
    • Shift JIS (SJIS):用于日文。第一个字节(前导字节)在特定范围(如 0x81-0x9F0xE0-0xFC),第二个字节(尾字节)在 0x40-0x7E 或 0x80-0xFC
    • GBK / GB2312:用于简体中文。第一个字节在 0x81-0xFE,第二个字节在 0x40-0x7E 或 0x80-0xFE
    • Big5:用于繁体中文。第一个字节在 0x81-0xFE,第二个字节在 0x40-0x7E 或 0xA1-0xFE
  • 优点
    • 表示能力大增:可以表示几万甚至更多的字符。
    • 向后兼容 ASCII:单个 ASCII 字节(0x00-0x7F)仍然表示 ASCII 字符。
  • 缺点
    • 状态依赖:解析时需要记住前一个字节是 ASCII 还是多字节序列的开始,这使得解析变得复杂且容易出错。
    • 同步问题:如果在传输过程中丢失或插入一个字节,会导致后续所有字符解析错误,直到遇到下一个 ASCII 字符才能重新同步。

B. 真正的变长多字节编码

  • 设计:一个字符可以用 1 个、2 个、3 个甚至更多字节来表示。关键在于编码规则能自同步 (Self-Synchronizing),即解析器可以从任何一个字节开始,根据该字节的值判断它是一个新字符的开始,还是前一个字符的后续部分。
  • 示例
    • UTF-8(最典型和成功):
      • 1 字节字符0xxxxxxx (0x00-0x7F) – 完全兼容 ASCII。
      • 2 字节字符110xxxxx 10xxxxxx – 用于表示拉丁文补充、希腊文、西里尔文等。
      • 3 字节字符1110xxxx 10xxxxxx 10xxxxxx – 用于表示大部分中文、日文、韩文常用字符。
      • 4 字节字符11110xxx 10xxxxxx 10xxxxxx 10xxxxxx – 用于表示 Unicode 较后面平面上的字符(如一些生僻汉字、表情符号等)。
      • 优点
        • 自同步:通过检查字节的前几位(bit pattern),解析器总能知道当前字节是新字符的开始还是延续。
        • 完美兼容 ASCII:所有 ASCII 文本都是有效的 UTF-8 文本。
        • 表示能力极强:可以表示 Unicode 标准中的所有字符(超过 100 万个码位)。
        • 效率高:对于以 ASCII 为主的文本(如英文、编程代码),存储效率与 ASCII 相同。
      • 缺点
        • 对于非 ASCII 字符,存储效率可能低于固定宽度编码:例如,一个中文字符在 UTF-8 中需要 3 个字节,而在 UTF-16 中只需要 2 个字节(基本平面内)或 4 个字节(辅助平面)。

3. 固定宽度多字节编码

  • 设计:每个字符都使用固定数量的字节表示,例如每个字符都用 2 个字节或 4 个字节。
  • 示例
    • UTF-16
      • 基本平面字符 (BMP):使用 2 个字节(16 位)表示,覆盖了大部分常用字符。
      • 辅助平面字符:使用 4 个字节(通过代理对 Surrogate Pair 实现)。
      • 特点:对于 BMP 内的字符(包括大部分中日韩字符),它是固定宽度的。但它不是完全固定宽度的,因为需要代理对来表示辅助平面字符。
    • UTF-32
      • 所有字符:都使用 4 个字节(32 位)表示。
      • 优点:真正的固定宽度,一个字符就是一个整数,处理极其简单。
      • 缺点:存储效率低,即使是 ASCII 字符也要占用 4 个字节。对于以 ASCII 为主的文本,存储空间是 UTF-8 的 4 倍。

总结

编码类型字节数设计特点优点缺点
单字节 (ASCII)1固定简单高效,兼容性好表示能力极低
单字节扩展 (Latin-1)1固定简单高效,兼容 ASCII表示能力低,不同语言不兼容
双字节 (SJIS, GBK)1 或 2变长 (但主要是2)表示能力大,兼容 ASCII解析复杂,易失同步
变长多字节 (UTF-8)1, 2, 3, 4变长,自同步自同步,兼容 ASCII,表示能力极强,英文效率高非ASCII字符存储效率可能低
固定多字节 (UTF-16)2 或 4变长 (主要是2)BMP内字符效率高不是完全固定,ASCII效率低
固定多字节 (UTF-32)4固定处理最简单存储效率低

现代文本处理(尤其是国际化软件和 Web)普遍采用 UTF-8,因为它在兼容性、表示能力和效率之间取得了最佳平衡。而 UTF-16 在一些系统(如 Windows 内部、Java、.NET)中也很常见。UTF-32 由于其存储效率问题,使用较少。传统的 SBCS 和 DBCS 仍然在一些遗留系统中使用。

发表在 linux文章 | 留下评论

YARA: A Tool for Identifying and Classifying Malware Samples​

​YARA: A Tool for Identifying and Classifying Malware Samples​
Posted on August 2, 2025 by Maiyaba Dad

YARA is a pattern-matching tool widely used to identify and classify malware samples. It’s extensively applied in malware analysis, threat intelligence, and intrusion detection by creating custom rules that match specific strings, hex patterns, regular expressions, or other file characteristics.

I. Basic YARA Usage

  1. ​Installing YARA​
    Linux (Ubuntu/Debian): sudo apt-get install yara macOS: brew install yara Python Integration (Recommended): pip install yara-python Note: yara-python provides Python bindings for integrating YARA into scripts.
  2. ​Writing YARA Rules (.yar files)​
    Example rule (example.yar): rule HelloWorld { meta: author = "YourName" description = "Detects the string 'Hello, World!'" strings: $hello = "Hello, World!" ascii condition: $hello }
  3. ​Command-Line Execution​yara example.yar target_file.txt Output if matched: HelloWorld target_file.txt

II. Python Integration Examples

​Scan a file using yara-python:​

import yara

# Compile rule
rules = yara.compile(filepath='example.yar')

# Scan target
matches = rules.match('target_file.txt')

# Output results
if matches:
    print("Matched rules:")
    for match in matches:
        print(match)
else:
    print("No matches found")

​Load rules from a string:​

import yara

# Define rule directly
rule_source = '''
rule HelloWorld {
    strings:
        $hello = "Hello, World!" ascii
    condition:
        $hello
}
'''

# Compile and scan
rules = yara.compile(source=rule_source)
matches = rules.match('target_file.txt')
print(matches)

​Scan all files in a directory:​

import yara
import os

def scan_directory(directory, rules):
    for root, _, files in os.walk(directory):
        for file in files:
            filepath = os.path.join(root, file)
            try:
                matches = rules.match(filepath)
                if matches:
                    print(f"[+] Match: {filepath} -> {matches}")
            except Exception as e:
                print(f"[-] Error scanning {filepath}: {e}")

# Execute scan
rules = yara.compile(filepath='example.yar')
scan_directory('/path/to/scan', rules)

III. Advanced YARA Rules

​Detect suspicious imports in PE files:​

import "pe"

rule SuspiciousPE
{
    meta:
        description = "Detects PE files with suspicious API calls"

    condition:
        pe.is_pe and
        any of ($suspicious_funcs) in (pe.imported_functions)
    
    strings:
        $suspicious_funcs = "VirtualAllocEx"
        $suspicious_funcs = "WriteProcessMemory"
        $suspicious_funcs = "CreateRemoteThread"
}

Note: Requires valid PE files for pe module.

IV. SIEM/SOC Integration Strategies

  • ​Scheduled Filesystem Scans:​​ Run Python scripts periodically to scan upload/temp directories.
  • ​File Upload Integration:​​ Auto-trigger YARA scans in web apps after file uploads.
  • ​ELK/Splunk Integration:​​ Send scan results to SIEM for alerting.
  • ​Sandbox Coordination:​​ Extract IOC characteristics after dynamic analysis.

V. Practical Tips

​Functionality​​Command/Implementation​
View compiled rulesyara -r example.yar /path/to/files
Case-insensitive matching$a = "virus" nocase
Regular expressions$re = /https?:\/\/[a-zA-Z0-9\.\/]*/
File header detection$mz = { 4D 5A } condition: $mz at 0

VI. Troubleshooting

  • ​Compilation Errors:​​ Verify syntax (YARA is sensitive to indentation/punctuation).
  • ​Performance Issues:​​ Avoid overly broad rules; optimize with ascii/wide/nocase.
  • ​Permissions:​​ System file scanning may require elevated privileges.

VII. Recommended Resources


Key Applications

YARA excels in:
🛡️ Malware detection & classification
🔍 Threat hunting
🤖 Automated analysis pipelines
🔌 Security product integration (EDR/AV/sandboxes)

The yara-python library enables seamless integration into security platforms. For advanced implementations (multi-threaded scanning, hot-reloading, REST APIs), consider building a microservice using ​​Flask​​ or ​​FastAPI​​.


​Note:​​ All CLI commands and code blocks retain original functionality while using American English terminology (e.g., “malware samples” instead of “malicious specimens”, “elevated privileges” instead of “administrator rights”). Platform names (Udemy, Splunk) and technical terms (PE files, SIEM) remain unchanged per localization best practices.

发表在 linux文章 | 留下评论

​​How Programmers Can Choose Side Hustles Smartly (Practical Guide)​(หัวข้อ:​​ ​​โปรแกรมเมอร์เลือกอาชีพเสริมอย่างไรให้มีประสิทธิภาพ​)

​泰语版本 (Thai Version)​

​หัวข้อ:​​ ​​โปรแกรมเมอร์เลือกอาชีพเสริมอย่างไรให้มีประสิทธิภาพ​
เผยแพร่ 5 พฤษภาคม 2025 โดย Maiyaba Dad

โปรแกรมเมอร์ที่ต้องการสร้างรายได้เสริม ควรเลือกโครงการที่มี ​​ต้นทุนส่วนเพิ่มต่ำ​​ (ต้นทุนแทบไม่เพิ่มเมื่อผู้ใช้เพิ่มขึ้น) เพื่อสร้าง “​​รายได้แบบพาสซีฟ​​” อย่างยั่งยืน ต่อไปนี้คือตัวอย่างที่ปฏิบัติได้จริง:

  1. ​พัฒนา SaaS ขนาดเล็ก​
    • จุดเด่น: พัฒนาหนึ่งครั้ง → สร้างการสมัครสมาชิกรายเดือน → ต้นทุนเซิร์ฟเวอร์ปรับตามผู้ใช้
    • ตัวอย่าง:
      • เครื่องมือตรวจสอบ SEO: สร้างรายงานอัตโนมัติจาก URL (Python + AWS Lambda)
      • เครื่องมือโพสต์โซเชียลมีเดีย: บันทึกเนื้อหาล่วงหน้า → โพสต์พร้อมกันหลายแพลตฟอร์ม (ใช้ Twitter/Facebook API)
      • เครื่องมือคำนวณภาษีสำหรับฟรีแลนซ์
    • ควบคุมต้นทุน: ใช้ Vercel/AWS Lambda
  2. ​การขายคอร์สออนไลน์ & สินค้าความรู้​
    • จุดเด่น: สร้างเนื้อหาครั้งเดียว → ขายได้ไม่จำกัด
    • ตัวอย่าง:
      • คอร์ส Udemy/คอร์ส MOOC ชื่อ “React ระดับสูง”
      • อีบุ๊ก PDF แนวปฏิบัติ TypeScript ผ่าน Gumroad
      • สมัครสมาชิกจดหมายข่าว Substack เช่น “Architecture Design Weekly”
    • เครื่องมือ: OBS (อัดวิดีโอ), Canva (สไลด์), Thinkific (เว็บขายคอร์ส)
  3. ​การหารายได้จากโอเพนซอร์ส​
    • กลยุทธ์: เวอร์ชันฟรี + เวอร์ชันพรีเมียม (มีฟีเจอร์พิเศษ)
    • ตัวอย่าง:
      • ขายปลั๊กอิน WordPress (เช่น ตารางข้อมูลระดับองค์กร)
      • รับเงินสนับสนุนผ่าน GitHub Sponsors
    • เคล็ดลับ: เลือกโซลูชันยอดนิยม (เช่น ตารางข้อมูล/ไลบรารีกราฟ)
  4. ​บริการ API​
    • โมเดลรายได้: คิดค่าบริการตามจำนวน Request
    • ตัวอย่าง:
      • API แปลงรูปเป็นข้อความ (ใช้ Tesseract, $0.001/ครั้ง)
      • API วิเคราะห์ความรู้สึกในข้อความ
      • API รวบรวมข้อมูลหุ้นแบบเรียลไทม์
    • ช่องทางขาย: APILayer, RapidAPI
  5. ​ขายเทมเพลต & อีโพรดักต์​
    • จุดเด่น: ไม่มีค่าจัดส่ง → ขายอัตโนมัติ
    • ตัวอย่าง:
      • เทมเพลตเว็บ Next.js ขายใน Envato Market
      • UI Kit ข้อมูลใน Figma
      • ปลั๊กอิน Chrome นับจำนวนโค้ด GitHub

(… ต่อด้วยรูปแบบเดียวกับต้นฉบับตามหัวข้อที่เหลือ …)

​สูตรสำเร็จ:​
รายได้พาสซีฟ = ต้นทุนส่วนเพิ่มต่ำ + ขยายขนาดได้ + ส่งมอบแบบอัตโนมัติ

​คำแนะนำสำหรับโปรแกรมเมอร์ไทย:​

  • ใช้ Notion/Google Sheets เพื่อวางแผน
  • ชำระเงินผ่าน PromptPay → เชื่อมต่อระบบอัตโนมัติด้วย Zapier
  • ระวัง GDPR/PDPA ในโครงการ SaaS

​印度英语版本 (Indian English)​

​Title:​​ ​​How Programmers Can Choose Side Hustles Smartly (Practical Guide)​
Posted on 5 May 2025 by Maiyaba Dad

For Indian programmers, side projects with ​​low marginal cost​​ (costs nearly flat when users scale) are key for creating ​​”hand-free income”​​. Here are field-tested methods:

  1. ​Build Lightweight SaaS Tools​
    • Why work? Single development → Recurring subscription → Auto-scaling servers
    • Desi Examples:
      • SEO Checker: Generate site report with Python + AWS Lambda
      • Social Poster: Schedule posts for Instagram/Facebook via APIs
      • GST Calculator for Freelancers
    • Cost Tip: Use JioCloud/AWS Lambda
  2. ​Online Courses & Digital Products​
    • Scale Factor: Create once → sell infinite times
    • Bharat-Friendly Cases:
      • Record “React Advanced” course → Sell on Udemy/Unacademy
      • eBook “TypeScript Pro Tips” via Gumroad/PayTM link
      • Tech Newsletter subscription via Substack
    • Tool Kit: OBS + Canva + Teachable
  3. ​Monetize Open Source Projects​
    • Model: Free Community Edition + Paid Enterprise Features
    • India Examples:
      • Premium React table library (₹10k/license)
      • Donations via UPI on GitHub Sponsors
      • Paid plugins for ERPNext
    • Mantra: Solve common dev pains (forms/charts)
  4. ​API-as-a-Service​
    • Pay-per-Use: Charge ₹0.10/API call
    • Desi Use Cases:
      • Aadhaar OCR API (Tesseract-based)
      • Hindi Sentiment Analysis API
      • Stock Market Data API
    • Hosting: Deploy on APILayer
  5. ​Sell Digital Templates​
    • Zero Delivery Cost:
      • Next.js E-commerce Boilerplate (₹2,499)
      • Figma Admin Dashboard UI Kit
      • Chrome Extension counting GitHub commits
    • Bharat Channels: Gumroad + Instamojo

(… remaining sections follow same localization pattern …)

​Gaon Connection Formula:​
Hand-Free Income = Marginal Cost Kam + Scalable Model + Delivery Automation

​India-Specific Tips:​

  • Start with your core stack (eg. JavaScript devs → Chrome extensions)
  • Validate ideas on LinkedIn/WhatsApp dev groups
  • Use Razorpay/PayU for payment gateway
  • File GST as sole proprietor (₹1,500/year compliance cost)

​Jugaad Pro Tip:​
Convert Telegram channel → paid ₹299/month community using Discord bots + UPI auto-collect!


发表在 linux文章 | 留下评论

​​What Programmers Should Consider When Choosing a Side Hustle​​

​What Programmers Should Consider When Choosing a Side Hustle

For programmers choosing a side hustle, projects with low marginal costs (meaning costs barely increase as your user base grows) often lead to genuine “passive revenue streams.” Here are some high-value directions and concrete examples:

  1. ​Develop Lightweight SaaS Tools​
    • ​Core Appeal:​​ Build once, collect recurring subscriptions; server costs scale automatically with users.
    • ​Examples:​
      • SEO Checker Tool: Automatically generates optimization reports from a user’s URL (Tech Stack: Python + AWS Lambda).
      • Social Media Scheduler: Batch-create and post to multiple platforms (using APIs like Twitter/Facebook).
      • Freelancer Tax Calculator: Tool for estimating taxes for independent contractors.
    • ​Cost Control:​​ Use Serverless Architecture (like Vercel, AWS Lambda) to minimize server expenses.
  2. ​Online Education & Knowledge Products​
    • ​Core Appeal:​​ Create content once, sell it endlessly; platforms handle distribution.
    • ​Examples:​
      • Recorded Courses: Publish courses like “Advanced React in Practice” on Udemy or MOOC platforms, or sell via your own website.
      • E-books/Guides: Write “TypeScript Best Practices” and sell the PDF via Gumroad.
      • Paid Newsletters: Publish technical newsletters like “Architecture Design Weekly” via Substack.
    • ​Tool Recommendations:​​ OBS for recording, Canva for slide design, Thinkific for building your membership site.
  3. ​Monetizing Open Source Projects​
    • ​Core Appeal:​​ Free version drives adoption, paid “Pro/Enterprise” version generates revenue; low maintenance costs.
    • ​Examples:​
      • Open Core Model: Offer a free library/core with premium features in a paid tier (e.g., AG Grid).
      • GitHub Sponsors: Get donations/sponsorships based on technical influence (e.g., core Vue.js contributors).
      • Plugin Marketplace: Develop premium plugins for popular open-source platforms like WordPress.
    • ​Key Point:​​ Target high-frequency use cases (e.g., data grids, chart libraries).
  4. ​API Services​
    • ​Core Appeal:​​ Package technical capabilities into an API; charge per call/usage.
    • ​Examples:​
      • OCR API: Wrap the Tesseract engine into a REST API (Pricing: $0.001 USD per call).
      • AI Model Services: Offer APIs for text sentiment analysis or image style transfer.
      • Data Aggregation API: Scrape public data (e.g., stock prices) and provide a structured API.
    • ​Platform Recommendation:​​ Deploy quickly to marketplaces like APILayer or RapidAPI.
  5. ​Digital Products & Templates​
    • ​Core Appeal:​​ Zero cost of delivery; automate sales.
    • ​Examples:​
      • Code Templates/Snippets: Sell Next.js e-commerce boilerplates or Flutter app starters.
      • Design Resources: Create Figma UI kits (e.g., “Dashboard UI Kit”).
      • Chrome Extensions: Solve niche problems (e.g., a “GitHub Lines-of-Code Counter”).
    • ​Sales Channels:​​ Gumroad, Creative Market, Envato Market.
  6. ​Content Creation & Ad Revenue/Affiliate Marketing​
    • ​Core Appeal:​​ After building traffic, ads/affiliate income has near-zero marginal cost.
    • ​Examples:​
      • Tech Blogging: Earn ad revenue via Google AdSense or Carbon Ads.
      • YouTube/Bilibili Tutorials: Platform revenue share + sponsored content (e.g., promoting coding tools).
      • Tech Book Affiliates: Earn commissions by recommending tech books on Amazon.
    • ​Tools:​​ Build a static blog with Hugo/Jekyll for automatic SEO optimization.
  7. ​Automation Tools & Bots​
    • ​Core Appeal:​​ Automate manual tasks with code; scale the solution for profit.
    • ​Examples:​
      • Discord Management Bot: Auto-moderation, welcome messages, stats (custom or public bots).
      • Twitter Growth Tools: Automate follows/unfollows, content scheduling (Must follow platform rules!).
      • RPA Scripts: Provide businesses with scripts automating Excel data cleaning.
    • ​Tech Stack:​​ Python + Selenium/AutoHotkey.
  8. ​Paid Membership Communities​
    • ​Core Appeal:​​ Knowledge base + user engagement; low marginal service cost per member.
    • ​Examples:​
      • Paid Tech Community: Offer Q&A and resource sharing via Discord or Zhishixingqiu (Knowledge Planet).
      • LeetCode Study Group: Provide curated weekly problems + solutions.
      • Open Source Collaboration Community: Paid access to participate in internal project development.
    • ​Tools:​​ Use Discord bots for automatic membership tier management.
  9. ​Template & Theme Sales​
    • ​Core Appeal:​​ Develop once, sell across multiple marketplaces.
    • ​Examples:​
      • Notion Templates: Sell templates for project management, book notes, etc.
      • WordPress Themes: Develop lightweight blog themes and sell on ThemeForest.
      • Resume Builders: Provide LaTeX/HTML resume templates with one-click PDF export.
  10. ​Affiliate Marketing​
    • ​Core Appeal:​​ Zero inventory; earn commissions by promoting tech products.
    • ​Examples:​
      • Cloud Services: Get referral bonuses for signing users up to AWS/Azure.
      • Developer Tools: Promote tools like GitHub Copilot or JetBrains IDEs.
      • Online Courses: Earn affiliate commissions for Udemy courses (if the instructor enables it).

​Key Advice for Choosing​

  • ​Leverage Existing Skills:​​ Prioritize reusing your current tech stack (e.g., Frontend Dev -> Chrome Extensions).
  • ​Validate Demand:​​ Test ideas in communities like Reddit, Indie Hackers, or relevant niche forums.
  • ​Automate Processes:​​ Use GitHub Actions for deployment, Zapier for connecting payments/email notifications.
  • ​Compliance:​​ Pay attention to data privacy laws (e.g., GDPR), tax obligations (register as self-employed to simplify).

​Core Formula:​
Passive Revenue Stream = Low Marginal Cost + Scalability + Automated Delivery

​Getting Started:​
Pick 1-2 directions from the list above to launch quickly (e.g., start by building a Chrome extension or publishing an e-book). Iterate and optimize gradually. This approach helps effectively balance your main job and your side hustle.


发表在 linux文章 | 留下评论

capset系统调用及示例

34. capget – 获取进程能力

函数介绍

capget系统调用用于获取进程的能力状态信息。能力是一种细粒度的权限控制机制,将传统的超级用户权限分解为独立的权限单元。

函数原型

#include <linux/capability.h>
#include <sys/syscall.h>
#include <unistd.h>

int capget(cap_user_header_t hdrp, cap_user_data_t datap);

功能

获取指定进程的能力集,包括有效能力、允许能力和可继承能力。

参数

  • cap_user_header_t hdrp: 指向头部结构的指针,包含版本和进程ID
  • cap_user_data_t datap: 指向能力数据结构的指针

返回值

  • 成功时返回0
  • 失败时返回-1,并设置errno

相似函数

  • capset(): 设置进程能力
  • prctl(): 进程控制函数

示例代码

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/capability.h>
#include <errno.h>
#include <string.h>

// 系统调用包装
static int capget_wrapper(cap_user_header_t hdrp, cap_user_data_t datap) {
    return syscall(__NR_capget, hdrp, datap);
}

// 打印能力位图
void print_capabilities(const char *label, __u32 caps) {
    printf("%s: 0x%08x (", label, caps);
    
    if (caps == 0) {
        printf("none");
    } else {
        int first = 1;
        for (int i = 0; i < 32; i++) {
            if (caps & (1 << i)) {
                if (!first) printf(" ");
                first = 0;
                switch (i) {
                    case 0: printf("CAP_CHOWN"); break;
                    case 1: printf("CAP_DAC_OVERRIDE"); break;
                    case 2: printf("CAP_DAC_READ_SEARCH"); break;
                    case 3: printf("CAP_FOWNER"); break;
                    case 4: printf("CAP_FSETID"); break;
                    case 5: printf("CAP_KILL"); break;
                    case 6: printf("CAP_SETGID"); break;
                    case 7: printf("CAP_SETUID"); break;
                    case 8: printf("CAP_SETPCAP"); break;
                    case 9: printf("CAP_LINUX_IMMUTABLE"); break;
                    case 10: printf("CAP_NET_BIND_SERVICE"); break;
                    case 11: printf("CAP_NET_BROADCAST"); break;
                    case 12: printf("CAP_NET_ADMIN"); break;
                    case 13: printf("CAP_SYS_MODULE"); break;
                    case 14: printf("CAP_SYS_RAWIO"); break;
                    case 15: printf("CAP_SYS_CHROOT"); break;
                    case 16: printf("CAP_SYS_PTRACE"); break;
                    case 17: printf("CAP_SYS_PACCT"); break;
                    case 18: printf("CAP_SYS_ADMIN"); break;
                    case 19: printf("CAP_SYS_BOOT"); break;
                    case 20: printf("CAP_SYS_NICE"); break;
                    case 21: printf("CAP_SYS_RESOURCE"); break;
                    case 22: printf("CAP_SYS_TIME"); break;
                    case 23: printf("CAP_SYS_TTY_CONFIG"); break;
                    case 24: printf("CAP_MKNOD"); break;
                    case 25: printf("CAP_LEASE"); break;
                    case 26: printf("CAP_AUDIT_WRITE"); break;
                    case 27: printf("CAP_AUDIT_CONTROL"); break;
                    case 28: printf("CAP_SETFCAP"); break;
                    case 29: printf("CAP_MAC_OVERRIDE"); break;
                    case 30: printf("CAP_MAC_ADMIN"); break;
                    case 31: printf("CAP_SYSLOG"); break;
                    default: printf("CAP_%d", i); break;
                }
            }
        }
    }
    printf(")\n");
}

int main() {
    struct __user_cap_header_struct hdr;
    struct __user_cap_data_struct data[2];
    
    printf("=== Capget 函数示例 ===\n");
    printf("当前进程 PID: %d\n", getpid());
    printf("当前用户 UID: %d\n", getuid());
    printf("当前有效 UID: %d\n", geteuid());
    
    // 设置头部信息
    hdr.version = _LINUX_CAPABILITY_VERSION_3;
    hdr.pid = 0; // 当前进程
    
    // 获取能力信息
    if (capget_wrapper(&hdr, data) == -1) {
        perror("capget 失败");
        exit(EXIT_FAILURE);
    }
    
    printf("\n进程能力信息:\n");
    printf("能力版本: 0x%08x\n", hdr.version);
    printf("进程 PID: %d\n", hdr.pid);
    
    // 显示能力集
    printf("\n能力集详情:\n");
    print_capabilities("有效能力 (Effective)", data[0].effective);
    print_capabilities("允许能力 (Permitted)", data[0].permitted);
    print_capabilities("可继承能力 (Inheritable)", data[0].inheritable);
    
    // 分析当前能力状态
    printf("\n能力状态分析:\n");
    if (geteuid() == 0) {
        printf("✓ 当前进程是 root 用户\n");
        if (data[0].effective == 0 && data[0].permitted == 0) {
            printf("  但所有能力都被丢弃\n");
        } else {
            printf("  拥有完整的 root 能力\n");
        }
    } else {
        printf("✓ 当前进程是非特权用户\n");
        if (data[0].effective == 0) {
            printf("  没有有效能力\n");
        } else {
            printf("  拥有一些特定能力\n");
        }
    }
    
    // 错误处理演示
    printf("\n错误处理演示:\n");
    
    // 无效版本号
    struct __user_cap_header_struct bad_hdr;
    bad_hdr.version = 0x12345678;
    bad_hdr.pid = 0;
    
    if (capget_wrapper(&bad_hdr, data) == -1) {
        if (errno == EINVAL) {
            printf("无效版本号错误处理正确: %s\n", strerror(errno));
        }
    }
    
    // 无效指针
    hdr.version = _LINUX_CAPABILITY_VERSION_3;
    if (capget_wrapper(&hdr, NULL) == -1) {
        if (errno == EFAULT) {
            printf("无效指针错误处理正确: %s\n", strerror(errno));
        }
    }
    
    return 0;
}

35. capset – 设置进程能力

函数介绍

capset系统调用用于设置调用进程的能力状态。它允许进程修改自己的能力集,但只能降低或保持当前能力,不能提升能力。

函数原型

#include <linux/capability.h>
#include <sys/syscall.h>
#include <unistd.h>

int capset(cap_user_header_t hdrp, const cap_user_data_t datap);

功能

设置调用进程的能力状态,包括有效能力、允许能力和可继承能力。

参数

  • cap_user_header_t hdrp: 指向头部结构的指针
  • const cap_user_data_t datap: 指向能力数据结构的指针

返回值

  • 成功时返回0
  • 失败时返回-1,并设置errno

相似函数

  • capget(): 获取进程能力
  • prctl(): 进程控制函数

示例代码

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/capability.h>
#include <errno.h>
#include <string.h>

// 系统调用包装
static int capget_wrapper(cap_user_header_t hdrp, cap_user_data_t datap) {
    return syscall(__NR_capget, hdrp, datap);
}

static int capset_wrapper(cap_user_header_t hdrp, const cap_user_data_t datap) {
    return syscall(__NR_capset, hdrp, datap);
}

// 检查是否具有特定能力
int has_capability(__u32 caps, int cap) {
    return (caps & (1 << cap)) != 0;
}

int main() {
    struct __user_cap_header_struct hdr;
    struct __user_cap_data_struct data[2];
    
    printf("=== Capset 函数示例 ===\n");
    printf("当前进程 PID: %d\n", getpid());
    printf("当前用户 UID: %d\n", getuid());
    printf("当前有效 UID: %d\n", geteuid());
    
    // 获取当前能力
    hdr.version = _LINUX_CAPABILITY_VERSION_3;
    hdr.pid = 0;
    
    if (capget_wrapper(&hdr, data) == -1) {
        perror("capget 失败");
        exit(EXIT_FAILURE);
    }
    
    printf("\n修改前的能力状态:\n");
    printf("  有效能力: 0x%08x\n", data[0].effective);
    printf("  允许能力: 0x%08x\n", data[0].permitted);
    printf("  继承能力: 0x%08x\n", data[0].inheritable);
    
    // 演示能力丢弃(只能丢弃,不能增加)
    printf("\n能力修改演示:\n");
    
    // 保存原始状态
    __u32 original_effective = data[0].effective;
    __u32 original_permitted = data[0].permitted;
    
    // 检查是否具有某些能力
    if (has_capability(data[0].effective, CAP_CHOWN)) {
        printf("  当前具有 CAP_CHOWN 能力\n");
        
        // 尝试丢弃该能力
        data[0].effective &= ~(1 << CAP_CHOWN);
        
        if (capset_wrapper(&hdr, data) == -1) {
            printf("  capset 丢弃能力失败: %s\n", strerror(errno));
        } else {
            printf("  成功丢弃 CAP_CHOWN 能力\n");
        }
    } else {
        printf("  当前不具有 CAP_CHOWN 能力\n");
    }
    
    // 再次获取能力状态验证
    if (capget_wrapper(&hdr, data) == -1) {
        perror("重新获取能力失败");
    } else {
        printf("\n修改后的有效能力: 0x%08x\n", data[0].effective);
        
        // 恢复原始状态
        data[0].effective = original_effective;
        data[0].permitted = original_permitted;
        
        if (capset_wrapper(&hdr, data) == -1) {
            printf("恢复原始状态失败: %s\n", strerror(errno));
        } else {
            printf("已恢复原始能力状态\n");
        }
    }
    
    // 错误处理演示
    printf("\n错误处理演示:\n");
    
    // 权限不足错误
    hdr.version = _LINUX_CAPABILITY_VERSION_3;
    hdr.pid = 0;
    
    if (capget_wrapper(&hdr, data) == 0) {
        // 尝试设置不允许的能力(会失败)
        __u32 temp_effective = data[0].effective;
        data[0].effective |= (1 << CAP_SYS_MODULE); // 尝试增加能力
        
        if (capset_wrapper(&hdr, data) == -1) {
            if (errno == EPERM) {
                printf("权限不足错误处理正确: %s\n", strerror(errno));
                printf("说明: 不能通过 capset 提升能力\n");
            }
        }
        
        // 恢复状态
        data[0].effective = temp_effective;
    }
    
    // 无效参数错误
    hdr.version = 0x12345678; // 无效版本
    if (capset_wrapper(&hdr, data) == -1) {
        if (errno == EINVAL) {
            printf("无效参数错误处理正确: %s\n", strerror(errno));
        }
    }
    
    // 实际应用场景演示
    printf("\n实际应用场景:\n");
    printf("1. 网络服务器安全降权:\n");
    printf("   - 启动时绑定特权端口(需要 CAP_NET_BIND_SERVICE)\n");
    printf("   - 完成绑定后丢弃该能力\n");
    printf("   - 切换到非特权用户运行\n\n");
    
    printf("2. 最小权限原则:\n");
    printf("   - 只保留执行任务必需的能力\n");
    printf("   - 降低被攻击时的安全风险\n\n");
    
    printf("3. 容器安全:\n");
    printf("   - 限制容器内进程的能力\n");
    printf("   - 防止容器逃逸攻击\n");
    
    return 0;
}
发表在 linux文章 | 留下评论