Packing.sol 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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_2_4(bytes2 left, bytes4 right) internal pure returns (bytes6 result) {
  44. assembly ("memory-safe") {
  45. result := or(left, shr(16, right))
  46. }
  47. }
  48. function pack_2_6(bytes2 left, bytes6 right) internal pure returns (bytes8 result) {
  49. assembly ("memory-safe") {
  50. result := or(left, shr(16, right))
  51. }
  52. }
  53. function pack_4_2(bytes4 left, bytes2 right) internal pure returns (bytes6 result) {
  54. assembly ("memory-safe") {
  55. result := or(left, shr(32, right))
  56. }
  57. }
  58. function pack_4_4(bytes4 left, bytes4 right) internal pure returns (bytes8 result) {
  59. assembly ("memory-safe") {
  60. result := or(left, shr(32, right))
  61. }
  62. }
  63. function pack_4_8(bytes4 left, bytes8 right) internal pure returns (bytes12 result) {
  64. assembly ("memory-safe") {
  65. result := or(left, shr(32, right))
  66. }
  67. }
  68. function pack_4_12(bytes4 left, bytes12 right) internal pure returns (bytes16 result) {
  69. assembly ("memory-safe") {
  70. result := or(left, shr(32, right))
  71. }
  72. }
  73. function pack_4_16(bytes4 left, bytes16 right) internal pure returns (bytes20 result) {
  74. assembly ("memory-safe") {
  75. result := or(left, shr(32, right))
  76. }
  77. }
  78. function pack_4_20(bytes4 left, bytes20 right) internal pure returns (bytes24 result) {
  79. assembly ("memory-safe") {
  80. result := or(left, shr(32, right))
  81. }
  82. }
  83. function pack_4_24(bytes4 left, bytes24 right) internal pure returns (bytes28 result) {
  84. assembly ("memory-safe") {
  85. result := or(left, shr(32, right))
  86. }
  87. }
  88. function pack_4_28(bytes4 left, bytes28 right) internal pure returns (bytes32 result) {
  89. assembly ("memory-safe") {
  90. result := or(left, shr(32, right))
  91. }
  92. }
  93. function pack_6_2(bytes6 left, bytes2 right) internal pure returns (bytes8 result) {
  94. assembly ("memory-safe") {
  95. result := or(left, shr(48, right))
  96. }
  97. }
  98. function pack_6_6(bytes6 left, bytes6 right) internal pure returns (bytes12 result) {
  99. assembly ("memory-safe") {
  100. result := or(left, shr(48, right))
  101. }
  102. }
  103. function pack_8_4(bytes8 left, bytes4 right) internal pure returns (bytes12 result) {
  104. assembly ("memory-safe") {
  105. result := or(left, shr(64, right))
  106. }
  107. }
  108. function pack_8_8(bytes8 left, bytes8 right) internal pure returns (bytes16 result) {
  109. assembly ("memory-safe") {
  110. result := or(left, shr(64, right))
  111. }
  112. }
  113. function pack_8_12(bytes8 left, bytes12 right) internal pure returns (bytes20 result) {
  114. assembly ("memory-safe") {
  115. result := or(left, shr(64, right))
  116. }
  117. }
  118. function pack_8_16(bytes8 left, bytes16 right) internal pure returns (bytes24 result) {
  119. assembly ("memory-safe") {
  120. result := or(left, shr(64, right))
  121. }
  122. }
  123. function pack_8_20(bytes8 left, bytes20 right) internal pure returns (bytes28 result) {
  124. assembly ("memory-safe") {
  125. result := or(left, shr(64, right))
  126. }
  127. }
  128. function pack_8_24(bytes8 left, bytes24 right) internal pure returns (bytes32 result) {
  129. assembly ("memory-safe") {
  130. result := or(left, shr(64, right))
  131. }
  132. }
  133. function pack_12_4(bytes12 left, bytes4 right) internal pure returns (bytes16 result) {
  134. assembly ("memory-safe") {
  135. result := or(left, shr(96, right))
  136. }
  137. }
  138. function pack_12_8(bytes12 left, bytes8 right) internal pure returns (bytes20 result) {
  139. assembly ("memory-safe") {
  140. result := or(left, shr(96, right))
  141. }
  142. }
  143. function pack_12_12(bytes12 left, bytes12 right) internal pure returns (bytes24 result) {
  144. assembly ("memory-safe") {
  145. result := or(left, shr(96, right))
  146. }
  147. }
  148. function pack_12_16(bytes12 left, bytes16 right) internal pure returns (bytes28 result) {
  149. assembly ("memory-safe") {
  150. result := or(left, shr(96, right))
  151. }
  152. }
  153. function pack_12_20(bytes12 left, bytes20 right) internal pure returns (bytes32 result) {
  154. assembly ("memory-safe") {
  155. result := or(left, shr(96, right))
  156. }
  157. }
  158. function pack_16_4(bytes16 left, bytes4 right) internal pure returns (bytes20 result) {
  159. assembly ("memory-safe") {
  160. result := or(left, shr(128, right))
  161. }
  162. }
  163. function pack_16_8(bytes16 left, bytes8 right) internal pure returns (bytes24 result) {
  164. assembly ("memory-safe") {
  165. result := or(left, shr(128, right))
  166. }
  167. }
  168. function pack_16_12(bytes16 left, bytes12 right) internal pure returns (bytes28 result) {
  169. assembly ("memory-safe") {
  170. result := or(left, shr(128, right))
  171. }
  172. }
  173. function pack_16_16(bytes16 left, bytes16 right) internal pure returns (bytes32 result) {
  174. assembly ("memory-safe") {
  175. result := or(left, shr(128, right))
  176. }
  177. }
  178. function pack_20_4(bytes20 left, bytes4 right) internal pure returns (bytes24 result) {
  179. assembly ("memory-safe") {
  180. result := or(left, shr(160, right))
  181. }
  182. }
  183. function pack_20_8(bytes20 left, bytes8 right) internal pure returns (bytes28 result) {
  184. assembly ("memory-safe") {
  185. result := or(left, shr(160, right))
  186. }
  187. }
  188. function pack_20_12(bytes20 left, bytes12 right) internal pure returns (bytes32 result) {
  189. assembly ("memory-safe") {
  190. result := or(left, shr(160, right))
  191. }
  192. }
  193. function pack_24_4(bytes24 left, bytes4 right) internal pure returns (bytes28 result) {
  194. assembly ("memory-safe") {
  195. result := or(left, shr(192, right))
  196. }
  197. }
  198. function pack_24_8(bytes24 left, bytes8 right) internal pure returns (bytes32 result) {
  199. assembly ("memory-safe") {
  200. result := or(left, shr(192, right))
  201. }
  202. }
  203. function pack_28_4(bytes28 left, bytes4 right) internal pure returns (bytes32 result) {
  204. assembly ("memory-safe") {
  205. result := or(left, shr(224, right))
  206. }
  207. }
  208. function extract_2_1(bytes2 self, uint8 offset) internal pure returns (bytes1 result) {
  209. if (offset > 1) revert OutOfRangeAccess();
  210. assembly ("memory-safe") {
  211. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  212. }
  213. }
  214. function replace_2_1(bytes2 self, bytes1 value, uint8 offset) internal pure returns (bytes2 result) {
  215. bytes1 oldValue = extract_2_1(self, offset);
  216. assembly ("memory-safe") {
  217. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  218. }
  219. }
  220. function extract_4_1(bytes4 self, uint8 offset) internal pure returns (bytes1 result) {
  221. if (offset > 3) revert OutOfRangeAccess();
  222. assembly ("memory-safe") {
  223. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  224. }
  225. }
  226. function replace_4_1(bytes4 self, bytes1 value, uint8 offset) internal pure returns (bytes4 result) {
  227. bytes1 oldValue = extract_4_1(self, offset);
  228. assembly ("memory-safe") {
  229. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  230. }
  231. }
  232. function extract_4_2(bytes4 self, uint8 offset) internal pure returns (bytes2 result) {
  233. if (offset > 2) revert OutOfRangeAccess();
  234. assembly ("memory-safe") {
  235. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  236. }
  237. }
  238. function replace_4_2(bytes4 self, bytes2 value, uint8 offset) internal pure returns (bytes4 result) {
  239. bytes2 oldValue = extract_4_2(self, offset);
  240. assembly ("memory-safe") {
  241. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  242. }
  243. }
  244. function extract_6_1(bytes6 self, uint8 offset) internal pure returns (bytes1 result) {
  245. if (offset > 5) revert OutOfRangeAccess();
  246. assembly ("memory-safe") {
  247. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  248. }
  249. }
  250. function replace_6_1(bytes6 self, bytes1 value, uint8 offset) internal pure returns (bytes6 result) {
  251. bytes1 oldValue = extract_6_1(self, offset);
  252. assembly ("memory-safe") {
  253. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  254. }
  255. }
  256. function extract_6_2(bytes6 self, uint8 offset) internal pure returns (bytes2 result) {
  257. if (offset > 4) revert OutOfRangeAccess();
  258. assembly ("memory-safe") {
  259. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  260. }
  261. }
  262. function replace_6_2(bytes6 self, bytes2 value, uint8 offset) internal pure returns (bytes6 result) {
  263. bytes2 oldValue = extract_6_2(self, offset);
  264. assembly ("memory-safe") {
  265. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  266. }
  267. }
  268. function extract_6_4(bytes6 self, uint8 offset) internal pure returns (bytes4 result) {
  269. if (offset > 2) revert OutOfRangeAccess();
  270. assembly ("memory-safe") {
  271. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  272. }
  273. }
  274. function replace_6_4(bytes6 self, bytes4 value, uint8 offset) internal pure returns (bytes6 result) {
  275. bytes4 oldValue = extract_6_4(self, offset);
  276. assembly ("memory-safe") {
  277. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  278. }
  279. }
  280. function extract_8_1(bytes8 self, uint8 offset) internal pure returns (bytes1 result) {
  281. if (offset > 7) revert OutOfRangeAccess();
  282. assembly ("memory-safe") {
  283. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  284. }
  285. }
  286. function replace_8_1(bytes8 self, bytes1 value, uint8 offset) internal pure returns (bytes8 result) {
  287. bytes1 oldValue = extract_8_1(self, offset);
  288. assembly ("memory-safe") {
  289. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  290. }
  291. }
  292. function extract_8_2(bytes8 self, uint8 offset) internal pure returns (bytes2 result) {
  293. if (offset > 6) revert OutOfRangeAccess();
  294. assembly ("memory-safe") {
  295. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  296. }
  297. }
  298. function replace_8_2(bytes8 self, bytes2 value, uint8 offset) internal pure returns (bytes8 result) {
  299. bytes2 oldValue = extract_8_2(self, offset);
  300. assembly ("memory-safe") {
  301. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  302. }
  303. }
  304. function extract_8_4(bytes8 self, uint8 offset) internal pure returns (bytes4 result) {
  305. if (offset > 4) revert OutOfRangeAccess();
  306. assembly ("memory-safe") {
  307. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  308. }
  309. }
  310. function replace_8_4(bytes8 self, bytes4 value, uint8 offset) internal pure returns (bytes8 result) {
  311. bytes4 oldValue = extract_8_4(self, offset);
  312. assembly ("memory-safe") {
  313. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  314. }
  315. }
  316. function extract_8_6(bytes8 self, uint8 offset) internal pure returns (bytes6 result) {
  317. if (offset > 2) revert OutOfRangeAccess();
  318. assembly ("memory-safe") {
  319. result := and(shl(mul(8, offset), self), shl(208, not(0)))
  320. }
  321. }
  322. function replace_8_6(bytes8 self, bytes6 value, uint8 offset) internal pure returns (bytes8 result) {
  323. bytes6 oldValue = extract_8_6(self, offset);
  324. assembly ("memory-safe") {
  325. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  326. }
  327. }
  328. function extract_12_1(bytes12 self, uint8 offset) internal pure returns (bytes1 result) {
  329. if (offset > 11) revert OutOfRangeAccess();
  330. assembly ("memory-safe") {
  331. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  332. }
  333. }
  334. function replace_12_1(bytes12 self, bytes1 value, uint8 offset) internal pure returns (bytes12 result) {
  335. bytes1 oldValue = extract_12_1(self, offset);
  336. assembly ("memory-safe") {
  337. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  338. }
  339. }
  340. function extract_12_2(bytes12 self, uint8 offset) internal pure returns (bytes2 result) {
  341. if (offset > 10) revert OutOfRangeAccess();
  342. assembly ("memory-safe") {
  343. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  344. }
  345. }
  346. function replace_12_2(bytes12 self, bytes2 value, uint8 offset) internal pure returns (bytes12 result) {
  347. bytes2 oldValue = extract_12_2(self, offset);
  348. assembly ("memory-safe") {
  349. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  350. }
  351. }
  352. function extract_12_4(bytes12 self, uint8 offset) internal pure returns (bytes4 result) {
  353. if (offset > 8) revert OutOfRangeAccess();
  354. assembly ("memory-safe") {
  355. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  356. }
  357. }
  358. function replace_12_4(bytes12 self, bytes4 value, uint8 offset) internal pure returns (bytes12 result) {
  359. bytes4 oldValue = extract_12_4(self, offset);
  360. assembly ("memory-safe") {
  361. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  362. }
  363. }
  364. function extract_12_6(bytes12 self, uint8 offset) internal pure returns (bytes6 result) {
  365. if (offset > 6) revert OutOfRangeAccess();
  366. assembly ("memory-safe") {
  367. result := and(shl(mul(8, offset), self), shl(208, not(0)))
  368. }
  369. }
  370. function replace_12_6(bytes12 self, bytes6 value, uint8 offset) internal pure returns (bytes12 result) {
  371. bytes6 oldValue = extract_12_6(self, offset);
  372. assembly ("memory-safe") {
  373. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  374. }
  375. }
  376. function extract_12_8(bytes12 self, uint8 offset) internal pure returns (bytes8 result) {
  377. if (offset > 4) revert OutOfRangeAccess();
  378. assembly ("memory-safe") {
  379. result := and(shl(mul(8, offset), self), shl(192, not(0)))
  380. }
  381. }
  382. function replace_12_8(bytes12 self, bytes8 value, uint8 offset) internal pure returns (bytes12 result) {
  383. bytes8 oldValue = extract_12_8(self, offset);
  384. assembly ("memory-safe") {
  385. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  386. }
  387. }
  388. function extract_16_1(bytes16 self, uint8 offset) internal pure returns (bytes1 result) {
  389. if (offset > 15) revert OutOfRangeAccess();
  390. assembly ("memory-safe") {
  391. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  392. }
  393. }
  394. function replace_16_1(bytes16 self, bytes1 value, uint8 offset) internal pure returns (bytes16 result) {
  395. bytes1 oldValue = extract_16_1(self, offset);
  396. assembly ("memory-safe") {
  397. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  398. }
  399. }
  400. function extract_16_2(bytes16 self, uint8 offset) internal pure returns (bytes2 result) {
  401. if (offset > 14) revert OutOfRangeAccess();
  402. assembly ("memory-safe") {
  403. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  404. }
  405. }
  406. function replace_16_2(bytes16 self, bytes2 value, uint8 offset) internal pure returns (bytes16 result) {
  407. bytes2 oldValue = extract_16_2(self, offset);
  408. assembly ("memory-safe") {
  409. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  410. }
  411. }
  412. function extract_16_4(bytes16 self, uint8 offset) internal pure returns (bytes4 result) {
  413. if (offset > 12) revert OutOfRangeAccess();
  414. assembly ("memory-safe") {
  415. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  416. }
  417. }
  418. function replace_16_4(bytes16 self, bytes4 value, uint8 offset) internal pure returns (bytes16 result) {
  419. bytes4 oldValue = extract_16_4(self, offset);
  420. assembly ("memory-safe") {
  421. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  422. }
  423. }
  424. function extract_16_6(bytes16 self, uint8 offset) internal pure returns (bytes6 result) {
  425. if (offset > 10) revert OutOfRangeAccess();
  426. assembly ("memory-safe") {
  427. result := and(shl(mul(8, offset), self), shl(208, not(0)))
  428. }
  429. }
  430. function replace_16_6(bytes16 self, bytes6 value, uint8 offset) internal pure returns (bytes16 result) {
  431. bytes6 oldValue = extract_16_6(self, offset);
  432. assembly ("memory-safe") {
  433. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  434. }
  435. }
  436. function extract_16_8(bytes16 self, uint8 offset) internal pure returns (bytes8 result) {
  437. if (offset > 8) revert OutOfRangeAccess();
  438. assembly ("memory-safe") {
  439. result := and(shl(mul(8, offset), self), shl(192, not(0)))
  440. }
  441. }
  442. function replace_16_8(bytes16 self, bytes8 value, uint8 offset) internal pure returns (bytes16 result) {
  443. bytes8 oldValue = extract_16_8(self, offset);
  444. assembly ("memory-safe") {
  445. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  446. }
  447. }
  448. function extract_16_12(bytes16 self, uint8 offset) internal pure returns (bytes12 result) {
  449. if (offset > 4) revert OutOfRangeAccess();
  450. assembly ("memory-safe") {
  451. result := and(shl(mul(8, offset), self), shl(160, not(0)))
  452. }
  453. }
  454. function replace_16_12(bytes16 self, bytes12 value, uint8 offset) internal pure returns (bytes16 result) {
  455. bytes12 oldValue = extract_16_12(self, offset);
  456. assembly ("memory-safe") {
  457. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  458. }
  459. }
  460. function extract_20_1(bytes20 self, uint8 offset) internal pure returns (bytes1 result) {
  461. if (offset > 19) revert OutOfRangeAccess();
  462. assembly ("memory-safe") {
  463. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  464. }
  465. }
  466. function replace_20_1(bytes20 self, bytes1 value, uint8 offset) internal pure returns (bytes20 result) {
  467. bytes1 oldValue = extract_20_1(self, offset);
  468. assembly ("memory-safe") {
  469. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  470. }
  471. }
  472. function extract_20_2(bytes20 self, uint8 offset) internal pure returns (bytes2 result) {
  473. if (offset > 18) revert OutOfRangeAccess();
  474. assembly ("memory-safe") {
  475. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  476. }
  477. }
  478. function replace_20_2(bytes20 self, bytes2 value, uint8 offset) internal pure returns (bytes20 result) {
  479. bytes2 oldValue = extract_20_2(self, offset);
  480. assembly ("memory-safe") {
  481. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  482. }
  483. }
  484. function extract_20_4(bytes20 self, uint8 offset) internal pure returns (bytes4 result) {
  485. if (offset > 16) revert OutOfRangeAccess();
  486. assembly ("memory-safe") {
  487. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  488. }
  489. }
  490. function replace_20_4(bytes20 self, bytes4 value, uint8 offset) internal pure returns (bytes20 result) {
  491. bytes4 oldValue = extract_20_4(self, offset);
  492. assembly ("memory-safe") {
  493. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  494. }
  495. }
  496. function extract_20_6(bytes20 self, uint8 offset) internal pure returns (bytes6 result) {
  497. if (offset > 14) revert OutOfRangeAccess();
  498. assembly ("memory-safe") {
  499. result := and(shl(mul(8, offset), self), shl(208, not(0)))
  500. }
  501. }
  502. function replace_20_6(bytes20 self, bytes6 value, uint8 offset) internal pure returns (bytes20 result) {
  503. bytes6 oldValue = extract_20_6(self, offset);
  504. assembly ("memory-safe") {
  505. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  506. }
  507. }
  508. function extract_20_8(bytes20 self, uint8 offset) internal pure returns (bytes8 result) {
  509. if (offset > 12) revert OutOfRangeAccess();
  510. assembly ("memory-safe") {
  511. result := and(shl(mul(8, offset), self), shl(192, not(0)))
  512. }
  513. }
  514. function replace_20_8(bytes20 self, bytes8 value, uint8 offset) internal pure returns (bytes20 result) {
  515. bytes8 oldValue = extract_20_8(self, offset);
  516. assembly ("memory-safe") {
  517. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  518. }
  519. }
  520. function extract_20_12(bytes20 self, uint8 offset) internal pure returns (bytes12 result) {
  521. if (offset > 8) revert OutOfRangeAccess();
  522. assembly ("memory-safe") {
  523. result := and(shl(mul(8, offset), self), shl(160, not(0)))
  524. }
  525. }
  526. function replace_20_12(bytes20 self, bytes12 value, uint8 offset) internal pure returns (bytes20 result) {
  527. bytes12 oldValue = extract_20_12(self, offset);
  528. assembly ("memory-safe") {
  529. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  530. }
  531. }
  532. function extract_20_16(bytes20 self, uint8 offset) internal pure returns (bytes16 result) {
  533. if (offset > 4) revert OutOfRangeAccess();
  534. assembly ("memory-safe") {
  535. result := and(shl(mul(8, offset), self), shl(128, not(0)))
  536. }
  537. }
  538. function replace_20_16(bytes20 self, bytes16 value, uint8 offset) internal pure returns (bytes20 result) {
  539. bytes16 oldValue = extract_20_16(self, offset);
  540. assembly ("memory-safe") {
  541. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  542. }
  543. }
  544. function extract_24_1(bytes24 self, uint8 offset) internal pure returns (bytes1 result) {
  545. if (offset > 23) revert OutOfRangeAccess();
  546. assembly ("memory-safe") {
  547. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  548. }
  549. }
  550. function replace_24_1(bytes24 self, bytes1 value, uint8 offset) internal pure returns (bytes24 result) {
  551. bytes1 oldValue = extract_24_1(self, offset);
  552. assembly ("memory-safe") {
  553. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  554. }
  555. }
  556. function extract_24_2(bytes24 self, uint8 offset) internal pure returns (bytes2 result) {
  557. if (offset > 22) revert OutOfRangeAccess();
  558. assembly ("memory-safe") {
  559. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  560. }
  561. }
  562. function replace_24_2(bytes24 self, bytes2 value, uint8 offset) internal pure returns (bytes24 result) {
  563. bytes2 oldValue = extract_24_2(self, offset);
  564. assembly ("memory-safe") {
  565. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  566. }
  567. }
  568. function extract_24_4(bytes24 self, uint8 offset) internal pure returns (bytes4 result) {
  569. if (offset > 20) revert OutOfRangeAccess();
  570. assembly ("memory-safe") {
  571. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  572. }
  573. }
  574. function replace_24_4(bytes24 self, bytes4 value, uint8 offset) internal pure returns (bytes24 result) {
  575. bytes4 oldValue = extract_24_4(self, offset);
  576. assembly ("memory-safe") {
  577. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  578. }
  579. }
  580. function extract_24_6(bytes24 self, uint8 offset) internal pure returns (bytes6 result) {
  581. if (offset > 18) revert OutOfRangeAccess();
  582. assembly ("memory-safe") {
  583. result := and(shl(mul(8, offset), self), shl(208, not(0)))
  584. }
  585. }
  586. function replace_24_6(bytes24 self, bytes6 value, uint8 offset) internal pure returns (bytes24 result) {
  587. bytes6 oldValue = extract_24_6(self, offset);
  588. assembly ("memory-safe") {
  589. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  590. }
  591. }
  592. function extract_24_8(bytes24 self, uint8 offset) internal pure returns (bytes8 result) {
  593. if (offset > 16) revert OutOfRangeAccess();
  594. assembly ("memory-safe") {
  595. result := and(shl(mul(8, offset), self), shl(192, not(0)))
  596. }
  597. }
  598. function replace_24_8(bytes24 self, bytes8 value, uint8 offset) internal pure returns (bytes24 result) {
  599. bytes8 oldValue = extract_24_8(self, offset);
  600. assembly ("memory-safe") {
  601. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  602. }
  603. }
  604. function extract_24_12(bytes24 self, uint8 offset) internal pure returns (bytes12 result) {
  605. if (offset > 12) revert OutOfRangeAccess();
  606. assembly ("memory-safe") {
  607. result := and(shl(mul(8, offset), self), shl(160, not(0)))
  608. }
  609. }
  610. function replace_24_12(bytes24 self, bytes12 value, uint8 offset) internal pure returns (bytes24 result) {
  611. bytes12 oldValue = extract_24_12(self, offset);
  612. assembly ("memory-safe") {
  613. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  614. }
  615. }
  616. function extract_24_16(bytes24 self, uint8 offset) internal pure returns (bytes16 result) {
  617. if (offset > 8) revert OutOfRangeAccess();
  618. assembly ("memory-safe") {
  619. result := and(shl(mul(8, offset), self), shl(128, not(0)))
  620. }
  621. }
  622. function replace_24_16(bytes24 self, bytes16 value, uint8 offset) internal pure returns (bytes24 result) {
  623. bytes16 oldValue = extract_24_16(self, offset);
  624. assembly ("memory-safe") {
  625. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  626. }
  627. }
  628. function extract_24_20(bytes24 self, uint8 offset) internal pure returns (bytes20 result) {
  629. if (offset > 4) revert OutOfRangeAccess();
  630. assembly ("memory-safe") {
  631. result := and(shl(mul(8, offset), self), shl(96, not(0)))
  632. }
  633. }
  634. function replace_24_20(bytes24 self, bytes20 value, uint8 offset) internal pure returns (bytes24 result) {
  635. bytes20 oldValue = extract_24_20(self, offset);
  636. assembly ("memory-safe") {
  637. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  638. }
  639. }
  640. function extract_28_1(bytes28 self, uint8 offset) internal pure returns (bytes1 result) {
  641. if (offset > 27) revert OutOfRangeAccess();
  642. assembly ("memory-safe") {
  643. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  644. }
  645. }
  646. function replace_28_1(bytes28 self, bytes1 value, uint8 offset) internal pure returns (bytes28 result) {
  647. bytes1 oldValue = extract_28_1(self, offset);
  648. assembly ("memory-safe") {
  649. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  650. }
  651. }
  652. function extract_28_2(bytes28 self, uint8 offset) internal pure returns (bytes2 result) {
  653. if (offset > 26) revert OutOfRangeAccess();
  654. assembly ("memory-safe") {
  655. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  656. }
  657. }
  658. function replace_28_2(bytes28 self, bytes2 value, uint8 offset) internal pure returns (bytes28 result) {
  659. bytes2 oldValue = extract_28_2(self, offset);
  660. assembly ("memory-safe") {
  661. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  662. }
  663. }
  664. function extract_28_4(bytes28 self, uint8 offset) internal pure returns (bytes4 result) {
  665. if (offset > 24) revert OutOfRangeAccess();
  666. assembly ("memory-safe") {
  667. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  668. }
  669. }
  670. function replace_28_4(bytes28 self, bytes4 value, uint8 offset) internal pure returns (bytes28 result) {
  671. bytes4 oldValue = extract_28_4(self, offset);
  672. assembly ("memory-safe") {
  673. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  674. }
  675. }
  676. function extract_28_6(bytes28 self, uint8 offset) internal pure returns (bytes6 result) {
  677. if (offset > 22) revert OutOfRangeAccess();
  678. assembly ("memory-safe") {
  679. result := and(shl(mul(8, offset), self), shl(208, not(0)))
  680. }
  681. }
  682. function replace_28_6(bytes28 self, bytes6 value, uint8 offset) internal pure returns (bytes28 result) {
  683. bytes6 oldValue = extract_28_6(self, offset);
  684. assembly ("memory-safe") {
  685. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  686. }
  687. }
  688. function extract_28_8(bytes28 self, uint8 offset) internal pure returns (bytes8 result) {
  689. if (offset > 20) revert OutOfRangeAccess();
  690. assembly ("memory-safe") {
  691. result := and(shl(mul(8, offset), self), shl(192, not(0)))
  692. }
  693. }
  694. function replace_28_8(bytes28 self, bytes8 value, uint8 offset) internal pure returns (bytes28 result) {
  695. bytes8 oldValue = extract_28_8(self, offset);
  696. assembly ("memory-safe") {
  697. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  698. }
  699. }
  700. function extract_28_12(bytes28 self, uint8 offset) internal pure returns (bytes12 result) {
  701. if (offset > 16) revert OutOfRangeAccess();
  702. assembly ("memory-safe") {
  703. result := and(shl(mul(8, offset), self), shl(160, not(0)))
  704. }
  705. }
  706. function replace_28_12(bytes28 self, bytes12 value, uint8 offset) internal pure returns (bytes28 result) {
  707. bytes12 oldValue = extract_28_12(self, offset);
  708. assembly ("memory-safe") {
  709. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  710. }
  711. }
  712. function extract_28_16(bytes28 self, uint8 offset) internal pure returns (bytes16 result) {
  713. if (offset > 12) revert OutOfRangeAccess();
  714. assembly ("memory-safe") {
  715. result := and(shl(mul(8, offset), self), shl(128, not(0)))
  716. }
  717. }
  718. function replace_28_16(bytes28 self, bytes16 value, uint8 offset) internal pure returns (bytes28 result) {
  719. bytes16 oldValue = extract_28_16(self, offset);
  720. assembly ("memory-safe") {
  721. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  722. }
  723. }
  724. function extract_28_20(bytes28 self, uint8 offset) internal pure returns (bytes20 result) {
  725. if (offset > 8) revert OutOfRangeAccess();
  726. assembly ("memory-safe") {
  727. result := and(shl(mul(8, offset), self), shl(96, not(0)))
  728. }
  729. }
  730. function replace_28_20(bytes28 self, bytes20 value, uint8 offset) internal pure returns (bytes28 result) {
  731. bytes20 oldValue = extract_28_20(self, offset);
  732. assembly ("memory-safe") {
  733. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  734. }
  735. }
  736. function extract_28_24(bytes28 self, uint8 offset) internal pure returns (bytes24 result) {
  737. if (offset > 4) revert OutOfRangeAccess();
  738. assembly ("memory-safe") {
  739. result := and(shl(mul(8, offset), self), shl(64, not(0)))
  740. }
  741. }
  742. function replace_28_24(bytes28 self, bytes24 value, uint8 offset) internal pure returns (bytes28 result) {
  743. bytes24 oldValue = extract_28_24(self, offset);
  744. assembly ("memory-safe") {
  745. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  746. }
  747. }
  748. function extract_32_1(bytes32 self, uint8 offset) internal pure returns (bytes1 result) {
  749. if (offset > 31) revert OutOfRangeAccess();
  750. assembly ("memory-safe") {
  751. result := and(shl(mul(8, offset), self), shl(248, not(0)))
  752. }
  753. }
  754. function replace_32_1(bytes32 self, bytes1 value, uint8 offset) internal pure returns (bytes32 result) {
  755. bytes1 oldValue = extract_32_1(self, offset);
  756. assembly ("memory-safe") {
  757. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  758. }
  759. }
  760. function extract_32_2(bytes32 self, uint8 offset) internal pure returns (bytes2 result) {
  761. if (offset > 30) revert OutOfRangeAccess();
  762. assembly ("memory-safe") {
  763. result := and(shl(mul(8, offset), self), shl(240, not(0)))
  764. }
  765. }
  766. function replace_32_2(bytes32 self, bytes2 value, uint8 offset) internal pure returns (bytes32 result) {
  767. bytes2 oldValue = extract_32_2(self, offset);
  768. assembly ("memory-safe") {
  769. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  770. }
  771. }
  772. function extract_32_4(bytes32 self, uint8 offset) internal pure returns (bytes4 result) {
  773. if (offset > 28) revert OutOfRangeAccess();
  774. assembly ("memory-safe") {
  775. result := and(shl(mul(8, offset), self), shl(224, not(0)))
  776. }
  777. }
  778. function replace_32_4(bytes32 self, bytes4 value, uint8 offset) internal pure returns (bytes32 result) {
  779. bytes4 oldValue = extract_32_4(self, offset);
  780. assembly ("memory-safe") {
  781. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  782. }
  783. }
  784. function extract_32_6(bytes32 self, uint8 offset) internal pure returns (bytes6 result) {
  785. if (offset > 26) revert OutOfRangeAccess();
  786. assembly ("memory-safe") {
  787. result := and(shl(mul(8, offset), self), shl(208, not(0)))
  788. }
  789. }
  790. function replace_32_6(bytes32 self, bytes6 value, uint8 offset) internal pure returns (bytes32 result) {
  791. bytes6 oldValue = extract_32_6(self, offset);
  792. assembly ("memory-safe") {
  793. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  794. }
  795. }
  796. function extract_32_8(bytes32 self, uint8 offset) internal pure returns (bytes8 result) {
  797. if (offset > 24) revert OutOfRangeAccess();
  798. assembly ("memory-safe") {
  799. result := and(shl(mul(8, offset), self), shl(192, not(0)))
  800. }
  801. }
  802. function replace_32_8(bytes32 self, bytes8 value, uint8 offset) internal pure returns (bytes32 result) {
  803. bytes8 oldValue = extract_32_8(self, offset);
  804. assembly ("memory-safe") {
  805. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  806. }
  807. }
  808. function extract_32_12(bytes32 self, uint8 offset) internal pure returns (bytes12 result) {
  809. if (offset > 20) revert OutOfRangeAccess();
  810. assembly ("memory-safe") {
  811. result := and(shl(mul(8, offset), self), shl(160, not(0)))
  812. }
  813. }
  814. function replace_32_12(bytes32 self, bytes12 value, uint8 offset) internal pure returns (bytes32 result) {
  815. bytes12 oldValue = extract_32_12(self, offset);
  816. assembly ("memory-safe") {
  817. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  818. }
  819. }
  820. function extract_32_16(bytes32 self, uint8 offset) internal pure returns (bytes16 result) {
  821. if (offset > 16) revert OutOfRangeAccess();
  822. assembly ("memory-safe") {
  823. result := and(shl(mul(8, offset), self), shl(128, not(0)))
  824. }
  825. }
  826. function replace_32_16(bytes32 self, bytes16 value, uint8 offset) internal pure returns (bytes32 result) {
  827. bytes16 oldValue = extract_32_16(self, offset);
  828. assembly ("memory-safe") {
  829. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  830. }
  831. }
  832. function extract_32_20(bytes32 self, uint8 offset) internal pure returns (bytes20 result) {
  833. if (offset > 12) revert OutOfRangeAccess();
  834. assembly ("memory-safe") {
  835. result := and(shl(mul(8, offset), self), shl(96, not(0)))
  836. }
  837. }
  838. function replace_32_20(bytes32 self, bytes20 value, uint8 offset) internal pure returns (bytes32 result) {
  839. bytes20 oldValue = extract_32_20(self, offset);
  840. assembly ("memory-safe") {
  841. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  842. }
  843. }
  844. function extract_32_24(bytes32 self, uint8 offset) internal pure returns (bytes24 result) {
  845. if (offset > 8) revert OutOfRangeAccess();
  846. assembly ("memory-safe") {
  847. result := and(shl(mul(8, offset), self), shl(64, not(0)))
  848. }
  849. }
  850. function replace_32_24(bytes32 self, bytes24 value, uint8 offset) internal pure returns (bytes32 result) {
  851. bytes24 oldValue = extract_32_24(self, offset);
  852. assembly ("memory-safe") {
  853. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  854. }
  855. }
  856. function extract_32_28(bytes32 self, uint8 offset) internal pure returns (bytes28 result) {
  857. if (offset > 4) revert OutOfRangeAccess();
  858. assembly ("memory-safe") {
  859. result := and(shl(mul(8, offset), self), shl(32, not(0)))
  860. }
  861. }
  862. function replace_32_28(bytes32 self, bytes28 value, uint8 offset) internal pure returns (bytes32 result) {
  863. bytes28 oldValue = extract_32_28(self, offset);
  864. assembly ("memory-safe") {
  865. result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
  866. }
  867. }
  868. }