tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

diplomat_runtime.h (3068B)


      1 #ifndef DIPLOMAT_RUNTIME_C_H
      2 #define DIPLOMAT_RUNTIME_C_H
      3 
      4 #include <assert.h>
      5 #include <stdbool.h>
      6 #include <stddef.h>
      7 #include <stdint.h>
      8 
      9 // These come from `uchar.h`, which is not available on all platforms.
     10 // Redefining them in C is no problem, however in >C++11 they are fundamental
     11 // types, which don't like being redefined.
     12 #if !(__cplusplus >= 201100)
     13 // https://en.cppreference.com/w/c/string/multibyte/char16_t
     14 typedef uint_least16_t char16_t;
     15 // https://en.cppreference.com/w/c/string/multibyte/char32_t
     16 typedef uint_least32_t char32_t;
     17 #endif
     18 
     19 static_assert(sizeof(char) == sizeof(uint8_t), "your architecture's `char` is not 8 bits");
     20 static_assert(sizeof(char16_t) == sizeof(uint16_t), "your architecture's `char16_t` is not 16 bits");
     21 static_assert(sizeof(char32_t) == sizeof(uint32_t), "your architecture's `char32_t` is not 32 bits");
     22 
     23 typedef struct DiplomatWrite {
     24    void* context;
     25    char* buf;
     26    size_t len;
     27    size_t cap;
     28    bool grow_failed;
     29    void (*flush)(struct DiplomatWrite*);
     30    bool (*grow)(struct DiplomatWrite*, size_t);
     31 } DiplomatWrite;
     32 
     33 bool diplomat_is_str(const char* buf, size_t len);
     34 
     35 #define MAKE_SLICES(name, c_ty) \
     36    typedef struct Diplomat##name##View { \
     37        const c_ty* data; \
     38        size_t len; \
     39    } Diplomat##name##View; \
     40    typedef struct Diplomat##name##ViewMut { \
     41        c_ty* data; \
     42        size_t len; \
     43    } Diplomat##name##ViewMut; \
     44    typedef struct Diplomat##name##Array { \
     45        const c_ty* data; \
     46        size_t len; \
     47    } Diplomat##name##Array;
     48 
     49 #define MAKE_SLICES_AND_OPTIONS(name, c_ty) \
     50    MAKE_SLICES(name, c_ty) \
     51    typedef struct Option##name {union { c_ty ok; }; bool is_ok; } Option##name; \
     52    typedef struct Option##name##View {union { Diplomat##name##View ok; }; bool is_ok; } Option##name##View; \
     53    typedef struct Option##name##ViewMut {union { Diplomat##name##ViewMut ok; }; bool is_ok; } Option##name##ViewMut; \
     54    typedef struct Option##name##Array {union { Diplomat##name##Array ok; }; bool is_ok; } Option##name##Array; \
     55 
     56 MAKE_SLICES_AND_OPTIONS(I8, int8_t)
     57 MAKE_SLICES_AND_OPTIONS(U8, uint8_t)
     58 MAKE_SLICES_AND_OPTIONS(I16, int16_t)
     59 MAKE_SLICES_AND_OPTIONS(U16, uint16_t)
     60 MAKE_SLICES_AND_OPTIONS(I32, int32_t)
     61 MAKE_SLICES_AND_OPTIONS(U32, uint32_t)
     62 MAKE_SLICES_AND_OPTIONS(I64, int64_t)
     63 MAKE_SLICES_AND_OPTIONS(U64, uint64_t)
     64 MAKE_SLICES_AND_OPTIONS(Isize, intptr_t)
     65 MAKE_SLICES_AND_OPTIONS(Usize, size_t)
     66 MAKE_SLICES_AND_OPTIONS(F32, float)
     67 MAKE_SLICES_AND_OPTIONS(F64, double)
     68 MAKE_SLICES_AND_OPTIONS(Bool, bool)
     69 MAKE_SLICES_AND_OPTIONS(Char, char32_t)
     70 MAKE_SLICES_AND_OPTIONS(String, char)
     71 MAKE_SLICES_AND_OPTIONS(String16, char16_t)
     72 MAKE_SLICES_AND_OPTIONS(Strings, DiplomatStringView)
     73 MAKE_SLICES_AND_OPTIONS(Strings16, DiplomatString16View)
     74 
     75 DiplomatWrite diplomat_simple_write(char* buf, size_t buf_size);
     76 
     77 DiplomatWrite* diplomat_buffer_write_create(size_t cap);
     78 char* diplomat_buffer_write_get_bytes(DiplomatWrite* t);
     79 size_t diplomat_buffer_write_len(DiplomatWrite* t);
     80 void diplomat_buffer_write_destroy(DiplomatWrite* t);
     81 
     82 #endif