1 | /* Copyright 2015-2016 OpenMarket Ltd |
2 | * |
3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | * you may not use this file except in compliance with the License. |
5 | * You may obtain a copy of the License at |
6 | * |
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | * |
9 | * Unless required by applicable law or agreed to in writing, software |
10 | * distributed under the License is distributed on an "AS IS" BASIS, |
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | * See the License for the specific language governing permissions and |
13 | * limitations under the License. |
14 | */ |
15 | #ifndef OLM_PICKLE_H_ |
16 | #define OLM_PICKLE_H_ |
17 | |
18 | #include <stddef.h> |
19 | #include <stdint.h> |
20 | |
21 | /* Convenience macro for checking the return value of internal unpickling |
22 | * functions and returning early on failure. */ |
23 | #ifndef UNPICKLE_OK |
24 | #define UNPICKLE_OK(x) do { if (!(x)) return NULL; } while(0) |
25 | #endif |
26 | |
27 | /* Convenience macro for failing on corrupted pickles from public |
28 | * API unpickling functions. */ |
29 | #define FAIL_ON_CORRUPTED_PICKLE(pos, session) \ |
30 | do { \ |
31 | if (!pos) { \ |
32 | session->last_error = OLM_CORRUPTED_PICKLE; \ |
33 | return (size_t)-1; \ |
34 | } \ |
35 | } while(0) |
36 | |
37 | #ifdef __cplusplus |
38 | extern "C" { |
39 | #endif |
40 | |
41 | struct _olm_ed25519_public_key; |
42 | struct _olm_ed25519_key_pair; |
43 | |
44 | |
45 | #define _olm_pickle_uint32_length(value) 4 |
46 | uint8_t * _olm_pickle_uint32(uint8_t * pos, uint32_t value); |
47 | uint8_t const * _olm_unpickle_uint32( |
48 | uint8_t const * pos, uint8_t const * end, |
49 | uint32_t *value |
50 | ); |
51 | |
52 | |
53 | #define _olm_pickle_bool_length(value) 1 |
54 | uint8_t * _olm_pickle_bool(uint8_t * pos, int value); |
55 | uint8_t const * _olm_unpickle_bool( |
56 | uint8_t const * pos, uint8_t const * end, |
57 | int *value |
58 | ); |
59 | |
60 | #define _olm_pickle_bytes_length(bytes, bytes_length) (bytes_length) |
61 | 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 | uint8_t * bytes, size_t bytes_length); |
65 | |
66 | |
67 | /** Get the number of bytes needed to pickle an ed25519 public key */ |
68 | size_t _olm_pickle_ed25519_public_key_length( |
69 | const struct _olm_ed25519_public_key * value |
70 | ); |
71 | |
72 | /** Pickle the ed25519 public key. Returns a pointer to the next free space in |
73 | * the buffer. */ |
74 | uint8_t * _olm_pickle_ed25519_public_key( |
75 | uint8_t *pos, const struct _olm_ed25519_public_key * value |
76 | ); |
77 | |
78 | /** Unpickle the ed25519 public key. Returns a pointer to the next item in the |
79 | * buffer on success, NULL on error. */ |
80 | const uint8_t * _olm_unpickle_ed25519_public_key( |
81 | const uint8_t *pos, const uint8_t *end, |
82 | struct _olm_ed25519_public_key * value |
83 | ); |
84 | |
85 | /** Get the number of bytes needed to pickle an ed25519 key pair */ |
86 | size_t _olm_pickle_ed25519_key_pair_length( |
87 | const struct _olm_ed25519_key_pair * value |
88 | ); |
89 | |
90 | /** Pickle the ed25519 key pair. Returns a pointer to the next free space in |
91 | * the buffer. */ |
92 | uint8_t * _olm_pickle_ed25519_key_pair( |
93 | uint8_t *pos, const struct _olm_ed25519_key_pair * value |
94 | ); |
95 | |
96 | /** Unpickle the ed25519 key pair. Returns a pointer to the next item in the |
97 | * buffer on success, NULL on error. */ |
98 | const uint8_t * _olm_unpickle_ed25519_key_pair( |
99 | const uint8_t *pos, const uint8_t *end, |
100 | struct _olm_ed25519_key_pair * value |
101 | ); |
102 | |
103 | #ifdef __cplusplus |
104 | } // extern "C" |
105 | #endif |
106 | |
107 | #endif /* OLM_PICKLE_H */ |
108 | |