#include "blob.h" const blob_t BLOB_NULL = { NULL, 0 }; static const char *xd = "0123456789abcdefghijklmnopqrstuvwxyz"; static inline int dx(int c) { if (likely(c >= '0' && c <= '9')) return c - '0'; if (likely(c >= 'a' && c <= 'f')) return c - 'a' + 0xa; if (c >= 'A' && c <= 'F') return c - 'A' + 0xa; return -1; } char *blob_cstr_dup(blob_t b) { char *p; if (blob_is_null(b)) return NULL; p = malloc(b.len+1); if (p != NULL) { memcpy(p, b.ptr, b.len); p[b.len] = 0; } return p; } blob_t blob_dup(blob_t b) { blob_t p; if (blob_is_null(b)) return BLOB_NULL; p.ptr = malloc(b.len); if (p.ptr != NULL) { memcpy(p.ptr, b.ptr, b.len); p.len = b.len; } else { p.len = 0; } return p; } int blob_cmp(blob_t a, blob_t b) { if (a.len != b.len) return a.len - b.len; return memcmp(a.ptr, b.ptr, a.len); } blob_t blob_pushed(blob_t buffer, blob_t left) { if (buffer.ptr + buffer.len != left.ptr + left.len) return BLOB_NULL; return BLOB_PTR_LEN(buffer.ptr, left.ptr - buffer.ptr); } void blob_push(blob_t *b, blob_t d) { if (b->len >= d.len) { memcpy(b->ptr, d.ptr, d.len); b->ptr += d.len; b->len -= d.len; } else { *b = BLOB_NULL; } } void blob_push_uint(blob_t *to, unsigned int value, int radix) { char buf[64]; char *ptr = &buf[sizeof(buf)-1]; if (value == 0) { blob_push(to, BLOB_STR("0")); return; } while (value != 0) { *(ptr--) = xd[value % radix]; value /= radix; } blob_push(to, BLOB_PTR_PTR(ptr+1, &buf[sizeof(buf)-1])); } void blob_push_hexdump(blob_t *to, blob_t binary) { static const char *xd = "0123456789abcdef"; char *d; int i; if (blob_is_null(*to)) return; if (to->len < binary.len * 2) { *to = BLOB_NULL; return; } for (i = 0, d = to->ptr; i < binary.len; i++) { *(d++) = xd[(binary.ptr[i] >> 4) & 0xf]; *(d++) = xd[binary.ptr[i] & 0xf]; } to->ptr = d; to->len -= binary.len * 2; } blob_t blob_pull(blob_t *b, int len) { blob_t r; if (b->len >= len) { r = BLOB_PTR_LEN(b->ptr, len); b->ptr += len; b->len -= len; return r; } *b = BLOB_NULL; return BLOB_NULL; } void blob_pull_skip(blob_t *b, int len) { if (b->len >= len) { b->ptr += len; b->len -= len; } else { *b = BLOB_NULL; } } int blob_pull_matching(blob_t *b, blob_t e) { if (b->len < e.len) return 0; if (memcmp(b->ptr, e.ptr, e.len) != 0) return 0; b->ptr += e.len; b->len -= e.len; return 1; } unsigned int blob_pull_uint(blob_t *b, int radix) { unsigned int val; int ch; val = 0; while (b->len && b->ptr[0] != 0) { ch = dx(b->ptr[0]); if (ch < 0 || ch >= radix) break; val *= radix; val += ch; b->ptr++; b->len--; } return val; } blob_t blob_pull_cspn(blob_t *b, const blob_t reject) { blob_t t = *b; int i; for (i = 0; i < t.len; i++) { if (memchr(reject.ptr, t.ptr[i], reject.len) != NULL) { *b = BLOB_PTR_LEN(t.ptr + i, t.len - i); return BLOB_PTR_LEN(t.ptr, i); } } *b = BLOB_NULL; return t; } blob_t blob_expand_head(blob_t *b, blob_t limits, unsigned char sep) { blob_t t = *b; blob_t r; if (t.ptr <= limits.ptr || t.ptr+t.len > limits.ptr+limits.len) return BLOB_NULL; while (t.ptr > limits.ptr && t.ptr[-1] == sep) t.ptr--, t.len++; r.ptr = t.ptr; r.len = 0; while (t.ptr > limits.ptr && t.ptr[-1] != sep) { t.ptr--, t.len++; r.ptr--, r.len++; } *b = t; return r; }