Packing.sol 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. // SPDX-License-Identifier: MIT
  2. // This file was procedurally generated from scripts/generate/templates/Packing.js.
  3. pragma solidity ^0.8.20;
  4. /**
  5. * @dev Helper library packing and unpacking multiple values into bytesXX.
  6. *
  7. * Example usage:
  8. *
  9. * ```solidity
  10. * library MyPacker {
  11. * type MyType is bytes32;
  12. *
  13. * function _pack(address account, bytes4 selector, uint64 period) external pure returns (MyType) {
  14. * bytes12 subpack = Packing.pack_4_8(selector, bytes8(period));
  15. * bytes32 pack = Packing.pack_20_12(bytes20(account), subpack);
  16. * return MyType.wrap(pack);
  17. * }
  18. *
  19. * function _unpack(MyType self) external pure returns (address, bytes4, uint64) {
  20. * bytes32 pack = MyType.unwrap(self);
  21. * return (
  22. * address(Packing.extract_32_20(pack, 0)),
  23. * Packing.extract_32_4(pack, 20),
  24. * uint64(Packing.extract_32_8(pack, 24))
  25. * );
  26. * }
  27. * }
  28. * ```
  29. */
  30. // solhint-disable func-name-mixedcase
  31. library Packing {
  32. error OutOfRangeAccess();
  33. function pack_1_1(bytes1 left, bytes1 right) internal pure returns (bytes2 result) {
  34. assembly ("memory-safe") {
  35. result := or(left, shr(8, right))
  36. }
  37. }
  38. function pack_2_2(bytes2 left, bytes2 right) internal pure returns (bytes4 result) {
  39. assembly ("memory-safe") {
  40. result := or(left, shr(16, right))
  41. }
  42. }
  43. function pack_4_4(bytes4 left, bytes4 right) internal pure returns (bytes8 result) {
  44. assembly ("memory-safe") {
  45. result := or(left, shr(32, right))
  46. }
  47. }
  48. function pack_4_8(bytes4 left, bytes8 right) internal pure returns (bytes12 result) {
  49. assembly ("memory-safe") {
  50. result := or(left, shr(32, right))
  51. }
  52. }
  53. function pack_4_12(bytes4 left, bytes12 right) internal pure returns (bytes16 result) {
  54. assembly ("memory-safe") {
  55. result := or(left, shr(32, right))
  56. }
  57. }
  58. function pack_4_16(bytes4 left, bytes16 right) internal pure returns (bytes20 result) {
  59. assembly ("memory-safe") {
  60. result := or(left, shr(32, right))
  61. }
  62. }
  63. function pack_4_20(bytes4 left, bytes20 right) internal pure returns (bytes24 result) {
  64. assembly ("memory-safe") {
  65. result := or(left, shr(32, right))
  66. }
  67. }
  68. function pack_4_24(bytes4 left, bytes24 right) internal pure returns (bytes28 result) {
  69. assembly ("memory-safe") {
  70. result := or(left, shr(32, right))
  71. }
  72. }
  73. function pack_4_28(bytes4 left, bytes28 right) internal pure returns (bytes32 result) {
  74. assembly ("memory-safe") {
  75. result := or(left, shr(32, right))
  76. }
  77. }
  78. function pack_8_4(bytes8 left, bytes4 right) internal pure returns (bytes12 result) {
  79. assembly ("memory-safe") {
  80. result := or(left, shr(64, right))
  81. }
  82. }
  83. function pack_8_8(bytes8 left, bytes8 right) internal pure returns (bytes16 result) {
  84. assembly ("memory-safe") {
  85. result := or(left, shr(64, right))
  86. }
  87. }
  88. function pack_8_12(bytes8 left, bytes12 right) internal pure returns (bytes20 result) {
  89. assembly ("memory-safe") {
  90. result := or(left, shr(64, right))
  91. }
  92. }
  93. function pack_8_16(bytes8 left, bytes16 right) internal pure returns (bytes24 result) {
  94. assembly ("memory-safe") {
  95. result := or(left, shr(64, right))
  96. }
  97. }
  98. function pack_8_20(bytes8 left, bytes20 right) internal pure returns (bytes28 result) {
  99. assembly ("memory-safe") {
  100. result := or(left, shr(64, right))
  101. }
  102. }
  103. function pack_8_24(bytes8 left, bytes24 right) internal pure returns (bytes32 result) {
  104. assembly ("memory-safe") {
  105. result := or(left, shr(64, right))
  106. }
  107. }
  108. function pack_12_4(bytes12 left, bytes4 right) internal pure returns (bytes16 result) {
  109. assembly ("memory-safe") {
  110. result := or(left, shr(96, right))
  111. }
  112. }
  113. function pack_12_8(bytes12 left, bytes8 right) internal pure returns (bytes20 result) {
  114. assembly ("memory-safe") {
  115. result := or(left, shr(96, right))
  116. }
  117. }
  118. function pack_12_12(bytes12 left, bytes12 right) internal pure returns (bytes24 result) {
  119. assembly ("memory-safe") {
  120. result := or(left, shr(96, right))
  121. }
  122. }
  123. function pack_12_16(bytes12 left, bytes16 right) internal pure returns (bytes28 result) {
  124. assembly ("memory-safe") {
  125. result := or(left, shr(96, right))
  126. }
  127. }
  128. function pack_12_20(bytes12 left, bytes20 right) internal pure returns (bytes32 result) {
  129. assembly ("memory-safe") {
  130. result := or(left, shr(96, right))
  131. }
  132. }
  133. function pack_16_4(bytes16 left, bytes4 right) internal pure returns (bytes20 result) {
  134. assembly ("memory-safe") {
  135. result := or(left, shr(128, right))
  136. }
  137. }
  138. function pack_16_8(bytes16 left, bytes8 right) internal pure returns (bytes24 result) {
  139. assembly ("memory-safe") {
  140. result := or(left, shr(128, right))
  141. }
  142. }
  143. function pack_16_12(bytes16 left, bytes12 right) internal pure returns (bytes28 result) {
  144. assembly ("memory-safe") {
  145. result := or(left, shr(128, right))
  146. }
  147. }
  148. function pack_16_16(bytes16 left, bytes16 right) internal pure returns (bytes32 result) {
  149. assembly ("memory-safe") {
  150. result := or(left, shr(128, right))
  151. }
  152. }
  153. function pack_20_4(bytes20 left, bytes4 right) internal pure returns (bytes24 result) {
  154. assembly ("memory-safe") {
  155. result := or(left, shr(160, right))
  156. }
  157. }
  158. function pack_20_8(bytes20 left, bytes8 right) internal pure returns (bytes28 result) {
  159. assembly ("memory-safe") {
  160. result := or(left, shr(160, right))
  161. }
  162. }
  163. function pack_20_12(bytes20 left, bytes12 right) internal pure returns (bytes32 result) {
  164. assembly ("memory-safe") {
  165. result := or(left, shr(160, right))
  166. }
  167. }
  168. function pack_24_4(bytes24 left, bytes4 right) internal pure returns (bytes28 result) {
  169. assembly ("memory-safe") {
  170. result := or(left, shr(192, right))
  171. }
  172. }
  173. function pack_24_8(bytes24 left, bytes8 right) internal pure returns (bytes32 result) {
  174. assembly ("memory-safe") {
  175. result := or(left, shr(192, right))
  176. }
  177. }
  178. function pack_28_4(bytes28 left, bytes4 right) internal pure returns (bytes32 result) {
  179. assembly ("memory-safe") {
  180. result := or(left, shr(224, right))
  181. }
  182. }
  183. function extract_2_1(bytes2 self, uint8 offset) internal pure returns (bytes1 result) {
  184. if (offset > 1) revert OutOfRangeAccess();
  185. assembly ("memory-safe") {
  186. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  187. }
  188. }
  189. function replace_2_1(bytes2 self, bytes1 value, uint8 offset) internal pure returns (bytes2 result) {
  190. bytes1 oldValue = extract_2_1(self, offset);
  191. assembly ("memory-safe") {
  192. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  193. }
  194. }
  195. function extract_4_1(bytes4 self, uint8 offset) internal pure returns (bytes1 result) {
  196. if (offset > 3) revert OutOfRangeAccess();
  197. assembly ("memory-safe") {
  198. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  199. }
  200. }
  201. function replace_4_1(bytes4 self, bytes1 value, uint8 offset) internal pure returns (bytes4 result) {
  202. bytes1 oldValue = extract_4_1(self, offset);
  203. assembly ("memory-safe") {
  204. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  205. }
  206. }
  207. function extract_4_2(bytes4 self, uint8 offset) internal pure returns (bytes2 result) {
  208. if (offset > 2) revert OutOfRangeAccess();
  209. assembly ("memory-safe") {
  210. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  211. }
  212. }
  213. function replace_4_2(bytes4 self, bytes2 value, uint8 offset) internal pure returns (bytes4 result) {
  214. bytes2 oldValue = extract_4_2(self, offset);
  215. assembly ("memory-safe") {
  216. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  217. }
  218. }
  219. function extract_8_1(bytes8 self, uint8 offset) internal pure returns (bytes1 result) {
  220. if (offset > 7) revert OutOfRangeAccess();
  221. assembly ("memory-safe") {
  222. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  223. }
  224. }
  225. function replace_8_1(bytes8 self, bytes1 value, uint8 offset) internal pure returns (bytes8 result) {
  226. bytes1 oldValue = extract_8_1(self, offset);
  227. assembly ("memory-safe") {
  228. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  229. }
  230. }
  231. function extract_8_2(bytes8 self, uint8 offset) internal pure returns (bytes2 result) {
  232. if (offset > 6) revert OutOfRangeAccess();
  233. assembly ("memory-safe") {
  234. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  235. }
  236. }
  237. function replace_8_2(bytes8 self, bytes2 value, uint8 offset) internal pure returns (bytes8 result) {
  238. bytes2 oldValue = extract_8_2(self, offset);
  239. assembly ("memory-safe") {
  240. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  241. }
  242. }
  243. function extract_8_4(bytes8 self, uint8 offset) internal pure returns (bytes4 result) {
  244. if (offset > 4) revert OutOfRangeAccess();
  245. assembly ("memory-safe") {
  246. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  247. }
  248. }
  249. function replace_8_4(bytes8 self, bytes4 value, uint8 offset) internal pure returns (bytes8 result) {
  250. bytes4 oldValue = extract_8_4(self, offset);
  251. assembly ("memory-safe") {
  252. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  253. }
  254. }
  255. function extract_12_1(bytes12 self, uint8 offset) internal pure returns (bytes1 result) {
  256. if (offset > 11) revert OutOfRangeAccess();
  257. assembly ("memory-safe") {
  258. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  259. }
  260. }
  261. function replace_12_1(bytes12 self, bytes1 value, uint8 offset) internal pure returns (bytes12 result) {
  262. bytes1 oldValue = extract_12_1(self, offset);
  263. assembly ("memory-safe") {
  264. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  265. }
  266. }
  267. function extract_12_2(bytes12 self, uint8 offset) internal pure returns (bytes2 result) {
  268. if (offset > 10) revert OutOfRangeAccess();
  269. assembly ("memory-safe") {
  270. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  271. }
  272. }
  273. function replace_12_2(bytes12 self, bytes2 value, uint8 offset) internal pure returns (bytes12 result) {
  274. bytes2 oldValue = extract_12_2(self, offset);
  275. assembly ("memory-safe") {
  276. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  277. }
  278. }
  279. function extract_12_4(bytes12 self, uint8 offset) internal pure returns (bytes4 result) {
  280. if (offset > 8) revert OutOfRangeAccess();
  281. assembly ("memory-safe") {
  282. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  283. }
  284. }
  285. function replace_12_4(bytes12 self, bytes4 value, uint8 offset) internal pure returns (bytes12 result) {
  286. bytes4 oldValue = extract_12_4(self, offset);
  287. assembly ("memory-safe") {
  288. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  289. }
  290. }
  291. function extract_12_8(bytes12 self, uint8 offset) internal pure returns (bytes8 result) {
  292. if (offset > 4) revert OutOfRangeAccess();
  293. assembly ("memory-safe") {
  294. result := and(shl(mul(8, offset), self), shl(192, not(0)))
  295. }
  296. }
  297. function replace_12_8(bytes12 self, bytes8 value, uint8 offset) internal pure returns (bytes12 result) {
  298. bytes8 oldValue = extract_12_8(self, offset);
  299. assembly ("memory-safe") {
  300. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  301. }
  302. }
  303. function extract_16_1(bytes16 self, uint8 offset) internal pure returns (bytes1 result) {
  304. if (offset > 15) revert OutOfRangeAccess();
  305. assembly ("memory-safe") {
  306. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  307. }
  308. }
  309. function replace_16_1(bytes16 self, bytes1 value, uint8 offset) internal pure returns (bytes16 result) {
  310. bytes1 oldValue = extract_16_1(self, offset);
  311. assembly ("memory-safe") {
  312. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  313. }
  314. }
  315. function extract_16_2(bytes16 self, uint8 offset) internal pure returns (bytes2 result) {
  316. if (offset > 14) revert OutOfRangeAccess();
  317. assembly ("memory-safe") {
  318. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  319. }
  320. }
  321. function replace_16_2(bytes16 self, bytes2 value, uint8 offset) internal pure returns (bytes16 result) {
  322. bytes2 oldValue = extract_16_2(self, offset);
  323. assembly ("memory-safe") {
  324. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  325. }
  326. }
  327. function extract_16_4(bytes16 self, uint8 offset) internal pure returns (bytes4 result) {
  328. if (offset > 12) revert OutOfRangeAccess();
  329. assembly ("memory-safe") {
  330. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  331. }
  332. }
  333. function replace_16_4(bytes16 self, bytes4 value, uint8 offset) internal pure returns (bytes16 result) {
  334. bytes4 oldValue = extract_16_4(self, offset);
  335. assembly ("memory-safe") {
  336. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  337. }
  338. }
  339. function extract_16_8(bytes16 self, uint8 offset) internal pure returns (bytes8 result) {
  340. if (offset > 8) revert OutOfRangeAccess();
  341. assembly ("memory-safe") {
  342. result := and(shl(mul(8, offset), self), shl(192, not(0)))
  343. }
  344. }
  345. function replace_16_8(bytes16 self, bytes8 value, uint8 offset) internal pure returns (bytes16 result) {
  346. bytes8 oldValue = extract_16_8(self, offset);
  347. assembly ("memory-safe") {
  348. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  349. }
  350. }
  351. function extract_16_12(bytes16 self, uint8 offset) internal pure returns (bytes12 result) {
  352. if (offset > 4) revert OutOfRangeAccess();
  353. assembly ("memory-safe") {
  354. result := and(shl(mul(8, offset), self), shl(160, not(0)))
  355. }
  356. }
  357. function replace_16_12(bytes16 self, bytes12 value, uint8 offset) internal pure returns (bytes16 result) {
  358. bytes12 oldValue = extract_16_12(self, offset);
  359. assembly ("memory-safe") {
  360. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  361. }
  362. }
  363. function extract_20_1(bytes20 self, uint8 offset) internal pure returns (bytes1 result) {
  364. if (offset > 19) revert OutOfRangeAccess();
  365. assembly ("memory-safe") {
  366. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  367. }
  368. }
  369. function replace_20_1(bytes20 self, bytes1 value, uint8 offset) internal pure returns (bytes20 result) {
  370. bytes1 oldValue = extract_20_1(self, offset);
  371. assembly ("memory-safe") {
  372. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  373. }
  374. }
  375. function extract_20_2(bytes20 self, uint8 offset) internal pure returns (bytes2 result) {
  376. if (offset > 18) revert OutOfRangeAccess();
  377. assembly ("memory-safe") {
  378. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  379. }
  380. }
  381. function replace_20_2(bytes20 self, bytes2 value, uint8 offset) internal pure returns (bytes20 result) {
  382. bytes2 oldValue = extract_20_2(self, offset);
  383. assembly ("memory-safe") {
  384. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  385. }
  386. }
  387. function extract_20_4(bytes20 self, uint8 offset) internal pure returns (bytes4 result) {
  388. if (offset > 16) revert OutOfRangeAccess();
  389. assembly ("memory-safe") {
  390. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  391. }
  392. }
  393. function replace_20_4(bytes20 self, bytes4 value, uint8 offset) internal pure returns (bytes20 result) {
  394. bytes4 oldValue = extract_20_4(self, offset);
  395. assembly ("memory-safe") {
  396. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  397. }
  398. }
  399. function extract_20_8(bytes20 self, uint8 offset) internal pure returns (bytes8 result) {
  400. if (offset > 12) revert OutOfRangeAccess();
  401. assembly ("memory-safe") {
  402. result := and(shl(mul(8, offset), self), shl(192, not(0)))
  403. }
  404. }
  405. function replace_20_8(bytes20 self, bytes8 value, uint8 offset) internal pure returns (bytes20 result) {
  406. bytes8 oldValue = extract_20_8(self, offset);
  407. assembly ("memory-safe") {
  408. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  409. }
  410. }
  411. function extract_20_12(bytes20 self, uint8 offset) internal pure returns (bytes12 result) {
  412. if (offset > 8) revert OutOfRangeAccess();
  413. assembly ("memory-safe") {
  414. result := and(shl(mul(8, offset), self), shl(160, not(0)))
  415. }
  416. }
  417. function replace_20_12(bytes20 self, bytes12 value, uint8 offset) internal pure returns (bytes20 result) {
  418. bytes12 oldValue = extract_20_12(self, offset);
  419. assembly ("memory-safe") {
  420. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  421. }
  422. }
  423. function extract_20_16(bytes20 self, uint8 offset) internal pure returns (bytes16 result) {
  424. if (offset > 4) revert OutOfRangeAccess();
  425. assembly ("memory-safe") {
  426. result := and(shl(mul(8, offset), self), shl(128, not(0)))
  427. }
  428. }
  429. function replace_20_16(bytes20 self, bytes16 value, uint8 offset) internal pure returns (bytes20 result) {
  430. bytes16 oldValue = extract_20_16(self, offset);
  431. assembly ("memory-safe") {
  432. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  433. }
  434. }
  435. function extract_24_1(bytes24 self, uint8 offset) internal pure returns (bytes1 result) {
  436. if (offset > 23) revert OutOfRangeAccess();
  437. assembly ("memory-safe") {
  438. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  439. }
  440. }
  441. function replace_24_1(bytes24 self, bytes1 value, uint8 offset) internal pure returns (bytes24 result) {
  442. bytes1 oldValue = extract_24_1(self, offset);
  443. assembly ("memory-safe") {
  444. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  445. }
  446. }
  447. function extract_24_2(bytes24 self, uint8 offset) internal pure returns (bytes2 result) {
  448. if (offset > 22) revert OutOfRangeAccess();
  449. assembly ("memory-safe") {
  450. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  451. }
  452. }
  453. function replace_24_2(bytes24 self, bytes2 value, uint8 offset) internal pure returns (bytes24 result) {
  454. bytes2 oldValue = extract_24_2(self, offset);
  455. assembly ("memory-safe") {
  456. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  457. }
  458. }
  459. function extract_24_4(bytes24 self, uint8 offset) internal pure returns (bytes4 result) {
  460. if (offset > 20) revert OutOfRangeAccess();
  461. assembly ("memory-safe") {
  462. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  463. }
  464. }
  465. function replace_24_4(bytes24 self, bytes4 value, uint8 offset) internal pure returns (bytes24 result) {
  466. bytes4 oldValue = extract_24_4(self, offset);
  467. assembly ("memory-safe") {
  468. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  469. }
  470. }
  471. function extract_24_8(bytes24 self, uint8 offset) internal pure returns (bytes8 result) {
  472. if (offset > 16) revert OutOfRangeAccess();
  473. assembly ("memory-safe") {
  474. result := and(shl(mul(8, offset), self), shl(192, not(0)))
  475. }
  476. }
  477. function replace_24_8(bytes24 self, bytes8 value, uint8 offset) internal pure returns (bytes24 result) {
  478. bytes8 oldValue = extract_24_8(self, offset);
  479. assembly ("memory-safe") {
  480. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  481. }
  482. }
  483. function extract_24_12(bytes24 self, uint8 offset) internal pure returns (bytes12 result) {
  484. if (offset > 12) revert OutOfRangeAccess();
  485. assembly ("memory-safe") {
  486. result := and(shl(mul(8, offset), self), shl(160, not(0)))
  487. }
  488. }
  489. function replace_24_12(bytes24 self, bytes12 value, uint8 offset) internal pure returns (bytes24 result) {
  490. bytes12 oldValue = extract_24_12(self, offset);
  491. assembly ("memory-safe") {
  492. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  493. }
  494. }
  495. function extract_24_16(bytes24 self, uint8 offset) internal pure returns (bytes16 result) {
  496. if (offset > 8) revert OutOfRangeAccess();
  497. assembly ("memory-safe") {
  498. result := and(shl(mul(8, offset), self), shl(128, not(0)))
  499. }
  500. }
  501. function replace_24_16(bytes24 self, bytes16 value, uint8 offset) internal pure returns (bytes24 result) {
  502. bytes16 oldValue = extract_24_16(self, offset);
  503. assembly ("memory-safe") {
  504. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  505. }
  506. }
  507. function extract_24_20(bytes24 self, uint8 offset) internal pure returns (bytes20 result) {
  508. if (offset > 4) revert OutOfRangeAccess();
  509. assembly ("memory-safe") {
  510. result := and(shl(mul(8, offset), self), shl(96, not(0)))
  511. }
  512. }
  513. function replace_24_20(bytes24 self, bytes20 value, uint8 offset) internal pure returns (bytes24 result) {
  514. bytes20 oldValue = extract_24_20(self, offset);
  515. assembly ("memory-safe") {
  516. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  517. }
  518. }
  519. function extract_28_1(bytes28 self, uint8 offset) internal pure returns (bytes1 result) {
  520. if (offset > 27) revert OutOfRangeAccess();
  521. assembly ("memory-safe") {
  522. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  523. }
  524. }
  525. function replace_28_1(bytes28 self, bytes1 value, uint8 offset) internal pure returns (bytes28 result) {
  526. bytes1 oldValue = extract_28_1(self, offset);
  527. assembly ("memory-safe") {
  528. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  529. }
  530. }
  531. function extract_28_2(bytes28 self, uint8 offset) internal pure returns (bytes2 result) {
  532. if (offset > 26) revert OutOfRangeAccess();
  533. assembly ("memory-safe") {
  534. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  535. }
  536. }
  537. function replace_28_2(bytes28 self, bytes2 value, uint8 offset) internal pure returns (bytes28 result) {
  538. bytes2 oldValue = extract_28_2(self, offset);
  539. assembly ("memory-safe") {
  540. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  541. }
  542. }
  543. function extract_28_4(bytes28 self, uint8 offset) internal pure returns (bytes4 result) {
  544. if (offset > 24) revert OutOfRangeAccess();
  545. assembly ("memory-safe") {
  546. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  547. }
  548. }
  549. function replace_28_4(bytes28 self, bytes4 value, uint8 offset) internal pure returns (bytes28 result) {
  550. bytes4 oldValue = extract_28_4(self, offset);
  551. assembly ("memory-safe") {
  552. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  553. }
  554. }
  555. function extract_28_8(bytes28 self, uint8 offset) internal pure returns (bytes8 result) {
  556. if (offset > 20) revert OutOfRangeAccess();
  557. assembly ("memory-safe") {
  558. result := and(shl(mul(8, offset), self), shl(192, not(0)))
  559. }
  560. }
  561. function replace_28_8(bytes28 self, bytes8 value, uint8 offset) internal pure returns (bytes28 result) {
  562. bytes8 oldValue = extract_28_8(self, offset);
  563. assembly ("memory-safe") {
  564. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  565. }
  566. }
  567. function extract_28_12(bytes28 self, uint8 offset) internal pure returns (bytes12 result) {
  568. if (offset > 16) revert OutOfRangeAccess();
  569. assembly ("memory-safe") {
  570. result := and(shl(mul(8, offset), self), shl(160, not(0)))
  571. }
  572. }
  573. function replace_28_12(bytes28 self, bytes12 value, uint8 offset) internal pure returns (bytes28 result) {
  574. bytes12 oldValue = extract_28_12(self, offset);
  575. assembly ("memory-safe") {
  576. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  577. }
  578. }
  579. function extract_28_16(bytes28 self, uint8 offset) internal pure returns (bytes16 result) {
  580. if (offset > 12) revert OutOfRangeAccess();
  581. assembly ("memory-safe") {
  582. result := and(shl(mul(8, offset), self), shl(128, not(0)))
  583. }
  584. }
  585. function replace_28_16(bytes28 self, bytes16 value, uint8 offset) internal pure returns (bytes28 result) {
  586. bytes16 oldValue = extract_28_16(self, offset);
  587. assembly ("memory-safe") {
  588. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  589. }
  590. }
  591. function extract_28_20(bytes28 self, uint8 offset) internal pure returns (bytes20 result) {
  592. if (offset > 8) revert OutOfRangeAccess();
  593. assembly ("memory-safe") {
  594. result := and(shl(mul(8, offset), self), shl(96, not(0)))
  595. }
  596. }
  597. function replace_28_20(bytes28 self, bytes20 value, uint8 offset) internal pure returns (bytes28 result) {
  598. bytes20 oldValue = extract_28_20(self, offset);
  599. assembly ("memory-safe") {
  600. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  601. }
  602. }
  603. function extract_28_24(bytes28 self, uint8 offset) internal pure returns (bytes24 result) {
  604. if (offset > 4) revert OutOfRangeAccess();
  605. assembly ("memory-safe") {
  606. result := and(shl(mul(8, offset), self), shl(64, not(0)))
  607. }
  608. }
  609. function replace_28_24(bytes28 self, bytes24 value, uint8 offset) internal pure returns (bytes28 result) {
  610. bytes24 oldValue = extract_28_24(self, offset);
  611. assembly ("memory-safe") {
  612. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  613. }
  614. }
  615. function extract_32_1(bytes32 self, uint8 offset) internal pure returns (bytes1 result) {
  616. if (offset > 31) revert OutOfRangeAccess();
  617. assembly ("memory-safe") {
  618. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  619. }
  620. }
  621. function replace_32_1(bytes32 self, bytes1 value, uint8 offset) internal pure returns (bytes32 result) {
  622. bytes1 oldValue = extract_32_1(self, offset);
  623. assembly ("memory-safe") {
  624. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  625. }
  626. }
  627. function extract_32_2(bytes32 self, uint8 offset) internal pure returns (bytes2 result) {
  628. if (offset > 30) revert OutOfRangeAccess();
  629. assembly ("memory-safe") {
  630. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  631. }
  632. }
  633. function replace_32_2(bytes32 self, bytes2 value, uint8 offset) internal pure returns (bytes32 result) {
  634. bytes2 oldValue = extract_32_2(self, offset);
  635. assembly ("memory-safe") {
  636. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  637. }
  638. }
  639. function extract_32_4(bytes32 self, uint8 offset) internal pure returns (bytes4 result) {
  640. if (offset > 28) revert OutOfRangeAccess();
  641. assembly ("memory-safe") {
  642. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  643. }
  644. }
  645. function replace_32_4(bytes32 self, bytes4 value, uint8 offset) internal pure returns (bytes32 result) {
  646. bytes4 oldValue = extract_32_4(self, offset);
  647. assembly ("memory-safe") {
  648. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  649. }
  650. }
  651. function extract_32_8(bytes32 self, uint8 offset) internal pure returns (bytes8 result) {
  652. if (offset > 24) revert OutOfRangeAccess();
  653. assembly ("memory-safe") {
  654. result := and(shl(mul(8, offset), self), shl(192, not(0)))
  655. }
  656. }
  657. function replace_32_8(bytes32 self, bytes8 value, uint8 offset) internal pure returns (bytes32 result) {
  658. bytes8 oldValue = extract_32_8(self, offset);
  659. assembly ("memory-safe") {
  660. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  661. }
  662. }
  663. function extract_32_12(bytes32 self, uint8 offset) internal pure returns (bytes12 result) {
  664. if (offset > 20) revert OutOfRangeAccess();
  665. assembly ("memory-safe") {
  666. result := and(shl(mul(8, offset), self), shl(160, not(0)))
  667. }
  668. }
  669. function replace_32_12(bytes32 self, bytes12 value, uint8 offset) internal pure returns (bytes32 result) {
  670. bytes12 oldValue = extract_32_12(self, offset);
  671. assembly ("memory-safe") {
  672. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  673. }
  674. }
  675. function extract_32_16(bytes32 self, uint8 offset) internal pure returns (bytes16 result) {
  676. if (offset > 16) revert OutOfRangeAccess();
  677. assembly ("memory-safe") {
  678. result := and(shl(mul(8, offset), self), shl(128, not(0)))
  679. }
  680. }
  681. function replace_32_16(bytes32 self, bytes16 value, uint8 offset) internal pure returns (bytes32 result) {
  682. bytes16 oldValue = extract_32_16(self, offset);
  683. assembly ("memory-safe") {
  684. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  685. }
  686. }
  687. function extract_32_20(bytes32 self, uint8 offset) internal pure returns (bytes20 result) {
  688. if (offset > 12) revert OutOfRangeAccess();
  689. assembly ("memory-safe") {
  690. result := and(shl(mul(8, offset), self), shl(96, not(0)))
  691. }
  692. }
  693. function replace_32_20(bytes32 self, bytes20 value, uint8 offset) internal pure returns (bytes32 result) {
  694. bytes20 oldValue = extract_32_20(self, offset);
  695. assembly ("memory-safe") {
  696. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  697. }
  698. }
  699. function extract_32_24(bytes32 self, uint8 offset) internal pure returns (bytes24 result) {
  700. if (offset > 8) revert OutOfRangeAccess();
  701. assembly ("memory-safe") {
  702. result := and(shl(mul(8, offset), self), shl(64, not(0)))
  703. }
  704. }
  705. function replace_32_24(bytes32 self, bytes24 value, uint8 offset) internal pure returns (bytes32 result) {
  706. bytes24 oldValue = extract_32_24(self, offset);
  707. assembly ("memory-safe") {
  708. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  709. }
  710. }
  711. function extract_32_28(bytes32 self, uint8 offset) internal pure returns (bytes28 result) {
  712. if (offset > 4) revert OutOfRangeAccess();
  713. assembly ("memory-safe") {
  714. result := and(shl(mul(8, offset), self), shl(32, not(0)))
  715. }
  716. }
  717. function replace_32_28(bytes32 self, bytes28 value, uint8 offset) internal pure returns (bytes32 result) {
  718. bytes28 oldValue = extract_32_28(self, offset);
  719. assembly ("memory-safe") {
  720. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  721. }
  722. }
  723. }