pickle.h 3.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* Copyright 2015-2016 OpenMarket Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef OLM_PICKLE_H_
#define OLM_PICKLE_H_

Denis Kasak's avatar
Denis Kasak committed
18
#include <stddef.h>
19
20
#include <stdint.h>

Denis Kasak's avatar
Denis Kasak committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* Convenience macro for checking the return value of internal unpickling
 * functions and returning early on failure. */
#ifndef UNPICKLE_OK
#define UNPICKLE_OK(x) do { if (!(x)) return NULL; } while(0)
#endif

/* Convenience macro for failing on corrupted pickles from public
 * API unpickling functions. */
#define FAIL_ON_CORRUPTED_PICKLE(pos, session) \
    do { \
        if (!pos) { \
          session->last_error = OLM_CORRUPTED_PICKLE;  \
          return (size_t)-1; \
        } \
    } while(0)

37
38
39
40
#ifdef __cplusplus
extern "C" {
#endif

41
42
43
44
struct _olm_ed25519_public_key;
struct _olm_ed25519_key_pair;


45
46
47
#define _olm_pickle_uint32_length(value) 4
uint8_t * _olm_pickle_uint32(uint8_t * pos, uint32_t value);
uint8_t const * _olm_unpickle_uint32(
48
49
50
51
52
    uint8_t const * pos, uint8_t const * end,
    uint32_t *value
);


53
54
55
#define _olm_pickle_bool_length(value) 1
uint8_t * _olm_pickle_bool(uint8_t * pos, int value);
uint8_t const * _olm_unpickle_bool(
56
57
58
59
    uint8_t const * pos, uint8_t const * end,
    int *value
);

60
61
#define _olm_pickle_bytes_length(bytes, bytes_length) (bytes_length)
uint8_t * _olm_pickle_bytes(uint8_t * pos, uint8_t const * bytes,
62
                           size_t bytes_length);
63
uint8_t const * _olm_unpickle_bytes(uint8_t const * pos, uint8_t const * end,
64
65
66
                                   uint8_t * bytes, size_t bytes_length);


67
68
69
70
71
72
73
74
75
76
77
78
/** Get the number of bytes needed to pickle an ed25519 public key */
size_t _olm_pickle_ed25519_public_key_length(
    const struct _olm_ed25519_public_key * value
);

/** Pickle the ed25519 public key. Returns a pointer to the next free space in
 * the buffer. */
uint8_t * _olm_pickle_ed25519_public_key(
    uint8_t *pos, const struct _olm_ed25519_public_key * value
);

/** Unpickle the ed25519 public key. Returns a pointer to the next item in the
Denis Kasak's avatar
Denis Kasak committed
79
 * buffer on success, NULL on error. */
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const uint8_t * _olm_unpickle_ed25519_public_key(
    const uint8_t *pos, const uint8_t *end,
    struct _olm_ed25519_public_key * value
);

/** Get the number of bytes needed to pickle an ed25519 key pair */
size_t _olm_pickle_ed25519_key_pair_length(
    const struct _olm_ed25519_key_pair * value
);

/** Pickle the ed25519 key pair. Returns a pointer to the next free space in
 * the buffer. */
uint8_t * _olm_pickle_ed25519_key_pair(
    uint8_t *pos, const struct _olm_ed25519_key_pair * value
);

/** Unpickle the ed25519 key pair. Returns a pointer to the next item in the
Denis Kasak's avatar
Denis Kasak committed
97
 * buffer on success, NULL on error. */
98
99
100
101
102
const uint8_t * _olm_unpickle_ed25519_key_pair(
    const uint8_t *pos, const uint8_t *end,
    struct _olm_ed25519_key_pair * value
);

103
104
105
106
107
#ifdef __cplusplus
} // extern "C"
#endif

#endif /* OLM_PICKLE_H */