Packing.sol 44 KB

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