Packing.sol 44 KB

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