wired
libc-free QUIC/HTTP3 SDK in C for x86_64-linux
Loading...
Searching...
No Matches
bytes.h
Go to the documentation of this file.
1#ifndef UTIL_BYTES_H
2#define UTIL_BYTES_H
3
6
12
25static inline int bytes_put(wired_mspan buf, usz* off, wired_span src) {
26 if (*off + src.n > buf.n) return 0;
27 for (usz i = 0; i < src.n; i++) buf.p[*off + i] = src.p[i];
28 *off += src.n;
29 return 1;
30}
31
43static inline int bytes_take(wired_span buf, usz* off, wired_mspan dst) {
44 if (*off + dst.n > buf.n) return 0;
45 for (usz i = 0; i < dst.n; i++) dst.p[i] = buf.p[*off + i];
46 *off += dst.n;
47 return 1;
48}
49
65static inline void* bytes_memcpy(void* dst, const void* src, usz n) {
66 u8* d = dst;
67 const u8* s = src;
68 for (usz i = 0; i < n; i++) d[i] = s[i];
69 return dst;
70}
71
85static inline void* bytes_move_down(void* dst, const void* src, usz n) {
86 u8* d = dst;
87 const u8* s = src;
88 for (usz i = 0; i < n; i++) d[i] = s[i];
89 return dst;
90}
91
100static inline void* bytes_memset(void* dst, int c, usz n) {
101 u8* d = dst;
102 for (usz i = 0; i < n; i++) d[i] = (u8)c;
103 return dst;
104}
105
112static inline usz wired_cstr_len(const char* s) {
113 usz n = 0;
114 while (s[n]) n++;
115 return n;
116}
117
124static inline u8 ascii_lower(u8 c) {
125 return (c >= 'A' && c <= 'Z') ? (u8)(c | 0x20) : c;
126}
127
137static inline int ascii_dns_eq(wired_span a, wired_span b) {
138 usz diff = 0;
139 if (a.n != b.n) return 0;
140 for (usz i = 0; i < a.n; i++)
141 diff |= (usz)(ascii_lower(a.p[i]) ^ ascii_lower(b.p[i]));
142 return diff == 0;
143}
144
145#endif
static void * bytes_memcpy(void *dst, const void *src, usz n)
Freestanding byte copy.
Definition bytes.h:65
static int bytes_put(wired_mspan buf, usz *off, wired_span src)
Append src into buf at *off.
Definition bytes.h:25
static void * bytes_memset(void *dst, int c, usz n)
Freestanding byte fill.
Definition bytes.h:100
static int bytes_take(wired_span buf, usz *off, wired_mspan dst)
Copy dst.n bytes into dst from *off (buf total).
Definition bytes.h:43
static usz wired_cstr_len(const char *s)
Freestanding NUL-terminated string length.
Definition bytes.h:112
static u8 ascii_lower(u8 c)
ASCII lowercase of one octet (A-Z -> a-z, everything else unchanged).
Definition bytes.h:124
static int ascii_dns_eq(wired_span a, wired_span b)
RFC 6125 6.4.1: ASCII case-insensitive equality of two DNS-name-shaped byte spans (host names,...
Definition bytes.h:137
static void * bytes_move_down(void *dst, const void *src, usz n)
Byte copy toward lower addresses: dst may overlap src as long as dst <= src (a forward copy then neve...
Definition bytes.h:85
Value-type views that fold the SDK's recurring argument shapes into single parameters,...
Mutable view of a byte range.
Definition span.h:42
usz n
number of writable bytes at p
Definition span.h:44
u8 * p
first byte of the writable range (not owned)
Definition span.h:43
Read-only view of a byte range.
Definition span.h:30
usz n
number of readable bytes at p
Definition span.h:32
const u8 * p
first byte of the viewed range (not owned)
Definition span.h:31
Linux direct syscalls.
u64 usz
unsigned size (size_t equivalent)
Definition types.h:20
unsigned char u8
unsigned 8-bit integer / byte
Definition types.h:18