1 | #ifndef AWS_COMMON_ENCODING_INL |
2 | #define AWS_COMMON_ENCODING_INL |
3 | |
4 | /* |
5 | * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"). |
8 | * You may not use this file except in compliance with the License. |
9 | * A copy of the License is located at |
10 | * |
11 | * http://aws.amazon.com/apache2.0 |
12 | * |
13 | * or in the "license" file accompanying this file. This file is distributed |
14 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
15 | * express or implied. See the License for the specific language governing |
16 | * permissions and limitations under the License. |
17 | */ |
18 | |
19 | #include <aws/common/byte_buf.h> |
20 | #include <aws/common/byte_order.h> |
21 | #include <aws/common/common.h> |
22 | #include <aws/common/encoding.h> |
23 | |
24 | AWS_EXTERN_C_BEGIN |
25 | |
26 | /* Add a 64 bit unsigned integer to the buffer, ensuring network - byte order |
27 | * Assumes the buffer size is at least 8 bytes. |
28 | */ |
29 | AWS_STATIC_IMPL void aws_write_u64(uint64_t value, uint8_t *buffer) { |
30 | value = aws_hton64(value); |
31 | |
32 | memcpy((void *)buffer, &value, sizeof(value)); |
33 | } |
34 | |
35 | /* |
36 | * Extracts a 64 bit unsigned integer from buffer. Ensures conversion from |
37 | * network byte order to host byte order. Assumes buffer size is at least 8 |
38 | * bytes. |
39 | */ |
40 | AWS_STATIC_IMPL uint64_t aws_read_u64(const uint8_t *buffer) { |
41 | uint64_t value = 0; |
42 | memcpy((void *)&value, (void *)buffer, sizeof(value)); |
43 | |
44 | return aws_ntoh64(value); |
45 | } |
46 | |
47 | /* Add a 32 bit unsigned integer to the buffer, ensuring network - byte order |
48 | * Assumes the buffer size is at least 4 bytes. |
49 | */ |
50 | AWS_STATIC_IMPL void aws_write_u32(uint32_t value, uint8_t *buffer) { |
51 | value = aws_hton32(value); |
52 | |
53 | memcpy((void *)buffer, (void *)&value, sizeof(value)); |
54 | } |
55 | |
56 | /* |
57 | * Extracts a 32 bit unsigned integer from buffer. Ensures conversion from |
58 | * network byte order to host byte order. Assumes the buffer size is at least 4 |
59 | * bytes. |
60 | */ |
61 | AWS_STATIC_IMPL uint32_t aws_read_u32(const uint8_t *buffer) { |
62 | uint32_t value = 0; |
63 | memcpy((void *)&value, (void *)buffer, sizeof(value)); |
64 | |
65 | return aws_ntoh32(value); |
66 | } |
67 | |
68 | /* Add a 24 bit unsigned integer to the buffer, ensuring network - byte order |
69 | * return the new position in the buffer for the next operation. |
70 | * Note, since this uses uint32_t for storage, the 3 least significant bytes |
71 | * will be used. Assumes buffer is at least 3 bytes long. |
72 | */ |
73 | AWS_STATIC_IMPL void aws_write_u24(uint32_t value, uint8_t *buffer) { |
74 | value = aws_hton32(value); |
75 | memcpy((void *)buffer, (void *)((uint8_t *)&value + 1), sizeof(value) - 1); |
76 | } |
77 | |
78 | /* |
79 | * Extracts a 24 bit unsigned integer from buffer. Ensures conversion from |
80 | * network byte order to host byte order. Assumes buffer is at least 3 bytes |
81 | * long. |
82 | */ |
83 | AWS_STATIC_IMPL uint32_t aws_read_u24(const uint8_t *buffer) { |
84 | uint32_t value = 0; |
85 | memcpy((void *)((uint8_t *)&value + 1), (void *)buffer, sizeof(value) - 1); |
86 | |
87 | return aws_ntoh32(value); |
88 | } |
89 | |
90 | /* Add a 16 bit unsigned integer to the buffer, ensuring network-byte order |
91 | * return the new position in the buffer for the next operation. |
92 | * Assumes buffer is at least 2 bytes long. |
93 | */ |
94 | AWS_STATIC_IMPL void aws_write_u16(uint16_t value, uint8_t *buffer) { |
95 | value = aws_hton16(value); |
96 | |
97 | memcpy((void *)buffer, (void *)&value, sizeof(value)); |
98 | } |
99 | |
100 | /* |
101 | * Extracts a 16 bit unsigned integer from buffer. Ensures conversion from |
102 | * network byte order to host byte order. Assumes buffer is at least 2 bytes |
103 | * long. |
104 | */ |
105 | AWS_STATIC_IMPL uint16_t aws_read_u16(const uint8_t *buffer) { |
106 | uint16_t value = 0; |
107 | memcpy((void *)&value, (void *)buffer, sizeof(value)); |
108 | |
109 | return aws_ntoh16(value); |
110 | } |
111 | |
112 | AWS_EXTERN_C_END |
113 | |
114 | #endif /* AWS_COMMON_ENCODING_INL */ |
115 | |