Skip to content

Commit 1155bd2

Browse files
Move rlp utils to dedicated file
1 parent 04d0fde commit 1155bd2

File tree

5 files changed

+151
-99
lines changed

5 files changed

+151
-99
lines changed

src/ethUstream.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <string.h>
2020

2121
#include "ethUstream.h"
22-
#include "ethUtils.h"
22+
#include "rlp_utils.h"
2323
#include "utils.h"
2424

2525
#define MAX_INT256 32

src/ethUtils.c

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -34,89 +34,6 @@
3434
#include "ethUstream.h"
3535
#include "network.h"
3636

37-
bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid) {
38-
if (*buffer <= 0x7f) {
39-
} else if (*buffer <= 0xb7) {
40-
} else if (*buffer <= 0xbf) {
41-
if (bufferLength < (1 + (*buffer - 0xb7))) {
42-
return false;
43-
}
44-
if (*buffer > 0xbb) {
45-
*valid = false; // arbitrary 32 bits length limitation
46-
return true;
47-
}
48-
} else if (*buffer <= 0xf7) {
49-
} else {
50-
if (bufferLength < (1 + (*buffer - 0xf7))) {
51-
return false;
52-
}
53-
if (*buffer > 0xfb) {
54-
*valid = false; // arbitrary 32 bits length limitation
55-
return true;
56-
}
57-
}
58-
*valid = true;
59-
return true;
60-
}
61-
62-
bool rlpDecodeLength(uint8_t *buffer, uint32_t *fieldLength, uint32_t *offset, bool *list) {
63-
if (*buffer <= 0x7f) {
64-
*offset = 0;
65-
*fieldLength = 1;
66-
*list = false;
67-
} else if (*buffer <= 0xb7) {
68-
*offset = 1;
69-
*fieldLength = *buffer - 0x80;
70-
*list = false;
71-
} else if (*buffer <= 0xbf) {
72-
*offset = 1 + (*buffer - 0xb7);
73-
*list = false;
74-
switch (*buffer) {
75-
case 0xb8:
76-
*fieldLength = *(buffer + 1);
77-
break;
78-
case 0xb9:
79-
*fieldLength = (*(buffer + 1) << 8) + *(buffer + 2);
80-
break;
81-
case 0xba:
82-
*fieldLength = (*(buffer + 1) << 16) + (*(buffer + 2) << 8) + *(buffer + 3);
83-
break;
84-
case 0xbb:
85-
*fieldLength = (*(buffer + 1) << 24) + (*(buffer + 2) << 16) +
86-
(*(buffer + 3) << 8) + *(buffer + 4);
87-
break;
88-
default:
89-
return false; // arbitrary 32 bits length limitation
90-
}
91-
} else if (*buffer <= 0xf7) {
92-
*offset = 1;
93-
*fieldLength = *buffer - 0xc0;
94-
*list = true;
95-
} else {
96-
*offset = 1 + (*buffer - 0xf7);
97-
*list = true;
98-
switch (*buffer) {
99-
case 0xf8:
100-
*fieldLength = *(buffer + 1);
101-
break;
102-
case 0xf9:
103-
*fieldLength = (*(buffer + 1) << 8) + *(buffer + 2);
104-
break;
105-
case 0xfa:
106-
*fieldLength = (*(buffer + 1) << 16) + (*(buffer + 2) << 8) + *(buffer + 3);
107-
break;
108-
case 0xfb:
109-
*fieldLength = (*(buffer + 1) << 24) + (*(buffer + 2) << 16) +
110-
(*(buffer + 3) << 8) + *(buffer + 4);
111-
break;
112-
default:
113-
return false; // arbitrary 32 bits length limitation
114-
}
115-
}
116-
117-
return true;
118-
}
119-
12037
bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context) {
12138
uint8_t hashAddress[INT256_LENGTH];
12239

src/ethUtils.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,6 @@
2525

2626
#define KECCAK256_HASH_BYTESIZE 32
2727

28-
/**
29-
* @brief Decode an RLP encoded field - see
30-
* https://github.com/ethereum/wiki/wiki/RLP
31-
* @param [in] buffer buffer containing the RLP encoded field to decode
32-
* @param [out] fieldLength length of the RLP encoded field
33-
* @param [out] offset offset to the beginning of the RLP encoded field from the
34-
* buffer
35-
* @param [out] list true if the field encodes a list, false if it encodes a
36-
* string
37-
* @return true if the RLP header is consistent
38-
*/
39-
bool rlpDecodeLength(uint8_t *buffer, uint32_t *fieldLength, uint32_t *offset, bool *list);
40-
41-
bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid);
42-
4328
bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context);
4429

4530
bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey,

src/rlp_utils.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*******************************************************************************
2+
* Ledger Ethereum App
3+
* (c) 2016-2019 Ledger
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
********************************************************************************/
17+
18+
/**
19+
* @brief Utilities for an Ethereum Hardware Wallet logic
20+
* @file ethUtils.h
21+
* @author Ledger Firmware Team <[email protected]>
22+
* @version 1.0
23+
* @date 8th of March 2016
24+
*/
25+
26+
#include <stdbool.h>
27+
#include <stdint.h>
28+
#include <string.h>
29+
30+
#include "rlp_utils.h"
31+
32+
bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid) {
33+
if (*buffer <= 0x7f) {
34+
} else if (*buffer <= 0xb7) {
35+
} else if (*buffer <= 0xbf) {
36+
if (bufferLength < (1 + (*buffer - 0xb7))) {
37+
return false;
38+
}
39+
if (*buffer > 0xbb) {
40+
*valid = false; // arbitrary 32 bits length limitation
41+
return true;
42+
}
43+
} else if (*buffer <= 0xf7) {
44+
} else {
45+
if (bufferLength < (1 + (*buffer - 0xf7))) {
46+
return false;
47+
}
48+
if (*buffer > 0xfb) {
49+
*valid = false; // arbitrary 32 bits length limitation
50+
return true;
51+
}
52+
}
53+
*valid = true;
54+
return true;
55+
}
56+
57+
bool rlpDecodeLength(uint8_t *buffer, uint32_t *fieldLength, uint32_t *offset, bool *list) {
58+
if (*buffer <= 0x7f) {
59+
*offset = 0;
60+
*fieldLength = 1;
61+
*list = false;
62+
} else if (*buffer <= 0xb7) {
63+
*offset = 1;
64+
*fieldLength = *buffer - 0x80;
65+
*list = false;
66+
} else if (*buffer <= 0xbf) {
67+
*offset = 1 + (*buffer - 0xb7);
68+
*list = false;
69+
switch (*buffer) {
70+
case 0xb8:
71+
*fieldLength = *(buffer + 1);
72+
break;
73+
case 0xb9:
74+
*fieldLength = (*(buffer + 1) << 8) + *(buffer + 2);
75+
break;
76+
case 0xba:
77+
*fieldLength = (*(buffer + 1) << 16) + (*(buffer + 2) << 8) + *(buffer + 3);
78+
break;
79+
case 0xbb:
80+
*fieldLength = (*(buffer + 1) << 24) + (*(buffer + 2) << 16) +
81+
(*(buffer + 3) << 8) + *(buffer + 4);
82+
break;
83+
default:
84+
return false; // arbitrary 32 bits length limitation
85+
}
86+
} else if (*buffer <= 0xf7) {
87+
*offset = 1;
88+
*fieldLength = *buffer - 0xc0;
89+
*list = true;
90+
} else {
91+
*offset = 1 + (*buffer - 0xf7);
92+
*list = true;
93+
switch (*buffer) {
94+
case 0xf8:
95+
*fieldLength = *(buffer + 1);
96+
break;
97+
case 0xf9:
98+
*fieldLength = (*(buffer + 1) << 8) + *(buffer + 2);
99+
break;
100+
case 0xfa:
101+
*fieldLength = (*(buffer + 1) << 16) + (*(buffer + 2) << 8) + *(buffer + 3);
102+
break;
103+
case 0xfb:
104+
*fieldLength = (*(buffer + 1) << 24) + (*(buffer + 2) << 16) +
105+
(*(buffer + 3) << 8) + *(buffer + 4);
106+
break;
107+
default:
108+
return false; // arbitrary 32 bits length limitation
109+
}
110+
}
111+
112+
return true;
113+
}

src/rlp_utils.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*******************************************************************************
2+
* Ledger Ethereum App
3+
* (c) 2016-2019 Ledger
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
********************************************************************************/
17+
18+
#pragma once
19+
20+
#include <stdbool.h>
21+
#include <stddef.h>
22+
#include <stdint.h>
23+
24+
/**
25+
* @brief Decode an RLP encoded field - see
26+
* https://github.com/ethereum/wiki/wiki/RLP
27+
* @param [in] buffer buffer containing the RLP encoded field to decode
28+
* @param [out] fieldLength length of the RLP encoded field
29+
* @param [out] offset offset to the beginning of the RLP encoded field from the
30+
* buffer
31+
* @param [out] list true if the field encodes a list, false if it encodes a
32+
* string
33+
* @return true if the RLP header is consistent
34+
*/
35+
bool rlpDecodeLength(uint8_t *buffer, uint32_t *fieldLength, uint32_t *offset, bool *list);
36+
37+
bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid);

0 commit comments

Comments
 (0)