@@ -15,25 +15,26 @@ library SparseCalldataSegmentLib {
1515
1616 /// @notice Splits out a segment of calldata, sparsely-packed.
1717 /// The expected format is:
18- /// [uint32(len(segment0)), segment0, uint32(len(segment1)), segment1, ... uint32(len(segmentN)), segmentN]
18+ /// [uint8(index0), uint32(len(segment0)), segment0, uint8(index1), uint32(len(segment1)), segment1,
19+ /// ... uint8(indexN), uint32(len(segmentN)), segmentN]
1920 /// @param source The calldata to extract the segment from.
2021 /// @return segment The extracted segment. Using the above example, this would be segment0.
2122 /// @return remainder The remaining calldata. Using the above example,
22- /// this would start at uint32(len(segment1) ) and continue to the end at segmentN.
23+ /// this would start at uint8(index1 ) and continue to the end at segmentN.
2324 function getNextSegment (bytes calldata source )
2425 internal
2526 pure
2627 returns (bytes calldata segment , bytes calldata remainder )
2728 {
28- // The first 4 bytes hold the length of the segment, excluding the index.
29- uint32 length = uint32 (bytes4 (source[:4 ]));
29+ // The first byte of the segment is the index.
30+ // The next 4 bytes hold the length of the segment, excluding the index.
31+ uint32 length = uint32 (bytes4 (source[1 :5 ]));
3032
3133 // The offset of the remainder of the calldata.
32- uint256 remainderOffset = 4 + length;
34+ uint256 remainderOffset = 5 + length;
3335
34- // The segment is the next `length` + 1 bytes, to account for the index.
35- // By convention, the first byte of each segment is the index of the segment.
36- segment = source[4 :remainderOffset];
36+ // The segment is the next `length` bytes after the first 5 bytes.
37+ segment = source[5 :remainderOffset];
3738
3839 // The remainder is the rest of the calldata.
3940 remainder = source[remainderOffset:];
@@ -52,7 +53,7 @@ library SparseCalldataSegmentLib {
5253 pure
5354 returns (bytes memory , bytes calldata )
5455 {
55- uint8 nextIndex = peekIndex (source);
56+ uint8 nextIndex = getIndex (source);
5657
5758 if (nextIndex < index) {
5859 revert SegmentOutOfOrder ();
@@ -61,8 +62,6 @@ library SparseCalldataSegmentLib {
6162 if (nextIndex == index) {
6263 (bytes calldata segment , bytes calldata remainder ) = getNextSegment (source);
6364
64- segment = getBody (segment);
65-
6665 if (segment.length == 0 ) {
6766 revert NonCanonicalEncoding ();
6867 }
@@ -73,25 +72,16 @@ library SparseCalldataSegmentLib {
7372 return ("" , source);
7473 }
7574
75+ /// @notice Extracts the final segment from the source.
76+ /// @dev Reverts if the index of the segment is not RESERVED_VALIDATION_DATA_INDEX.
77+ /// @param source The calldata to extract the segment from.
78+ /// @return The final segment.
7679 function getFinalSegment (bytes calldata source ) internal pure returns (bytes calldata ) {
77- (bytes calldata segment , bytes calldata remainder ) = getNextSegment (source);
78-
79- if (getIndex (segment) != RESERVED_VALIDATION_DATA_INDEX) {
80+ if (getIndex (source) != RESERVED_VALIDATION_DATA_INDEX) {
8081 revert ValidationSignatureSegmentMissing ();
8182 }
8283
83- if (remainder.length != 0 ) {
84- revert NonCanonicalEncoding ();
85- }
86-
87- return getBody (segment);
88- }
89-
90- /// @notice Returns the index of the next segment in the source.
91- /// @param source The calldata to extract the index from.
92- /// @return The index of the next segment.
93- function peekIndex (bytes calldata source ) internal pure returns (uint8 ) {
94- return uint8 (source[4 ]);
84+ return source[1 :];
9585 }
9686
9787 /// @notice Extracts the index from a segment.
@@ -101,12 +91,4 @@ library SparseCalldataSegmentLib {
10191 function getIndex (bytes calldata segment ) internal pure returns (uint8 ) {
10292 return uint8 (segment[0 ]);
10393 }
104-
105- /// @notice Extracts the body from a segment.
106- /// @dev The body is the segment without the index.
107- /// @param segment The segment to extract the body from
108- /// @return The body of the segment.
109- function getBody (bytes calldata segment ) internal pure returns (bytes calldata ) {
110- return segment[1 :];
111- }
11294}
0 commit comments