summaryrefslogtreecommitdiff
path: root/src/util.h
blob: ccb02104a33e3ca1fd3818718484409a5f91ca54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef BOKF_UTIL_H
#define BOKF_UTIL_H

#include <stddef.h>
#include <stdint.h>
#include <time.h>

void *xmalloc(size_t n);
void *xcalloc(size_t n, size_t sz);
void *xrealloc(void *p, size_t n);
char *xstrdup(const char *s);

struct buf {
    unsigned char *p;
    size_t len, cap;
};

void buf_init(struct buf *b);
void buf_free(struct buf *b);
void buf_append(struct buf *b, const void *data, size_t n);
void buf_append_u16be(struct buf *b, uint16_t v);
void buf_append_u32be(struct buf *b, uint32_t v);
void buf_append_u64be(struct buf *b, uint64_t v);

void util_sha256(const void *data, size_t n, unsigned char out[32]);
void util_hex(const unsigned char *in, size_t n, char *out);
char *util_b64url(const unsigned char *in, size_t n);
char *util_b64(const unsigned char *in, size_t n);
int util_random(void *out, size_t n);
char *util_random_id(const char *prefix, size_t nbytes);
int util_const_eq(const void *a, const void *b, size_t n);
int64_t util_now(void);
void util_iso8601(int64_t t, char *buf, size_t n);
char *util_str_trim(char *s);
int util_hex_color_valid(const char *s);
int util_parse_iso_date(const char *s);

/* base64 (standard alphabet, padding required) */
int util_b64_decode(const char *in, size_t in_len, unsigned char **out,
                    size_t *out_len);

/* date helpers, all dates "YYYY-MM-DD" */
int util_date_parse(const char *s, int *y, int *m, int *d);
void util_date_fmt(int y, int m, int d, char *buf, size_t n);
int util_days_in_month(int y, int m);
void util_date_add_months(const char *date, int months, char *out, size_t n);
void util_date_add_days(const char *date, int days, char *out, size_t n);
int util_date_valid(const char *s);

/* Öre rounded to whole kronor (half away from zero), still in öre. The
   annual report and the SRU export round every account with it and sum the
   results, so their lines, totals and fields agree to the krona. */
int64_t util_round_kr(int64_t ore);

/* encoding conversion for SIE files (IBM PC8 / CP437) */
size_t util_utf8_to_cp437(const char *in, unsigned char *out, size_t out_sz);
char *util_cp437_to_utf8(const unsigned char *in, size_t n);

#endif