You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

786 lines
18 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  11. Place, Suite 330, Boston, MA 02111-1307 USA
  12. *****************************************************************************/
  13. /******************************************************************//**
  14. @file include/mach0data.ic
  15. Utilities for converting data from the database file
  16. to the machine format.
  17. Created 11/28/1995 Heikki Tuuri
  18. ***********************************************************************/
  19. #include "ut0mem.h"
  20. /*******************************************************//**
  21. The following function is used to store data in one byte. */
  22. UNIV_INLINE
  23. void
  24. mach_write_to_1(
  25. /*============*/
  26. byte* b, /*!< in: pointer to byte where to store */
  27. ulint n) /*!< in: ulint integer to be stored, >= 0, < 256 */
  28. {
  29. ut_ad(b);
  30. ut_ad(n <= 0xFFUL);
  31. b[0] = (byte)n;
  32. }
  33. /********************************************************//**
  34. The following function is used to fetch data from one byte.
  35. @return ulint integer, >= 0, < 256 */
  36. UNIV_INLINE
  37. ulint
  38. mach_read_from_1(
  39. /*=============*/
  40. const byte* b) /*!< in: pointer to byte */
  41. {
  42. ut_ad(b);
  43. return((ulint)(b[0]));
  44. }
  45. /*******************************************************//**
  46. The following function is used to store data in two consecutive
  47. bytes. We store the most significant byte to the lowest address. */
  48. UNIV_INLINE
  49. void
  50. mach_write_to_2(
  51. /*============*/
  52. byte* b, /*!< in: pointer to two bytes where to store */
  53. ulint n) /*!< in: ulint integer to be stored */
  54. {
  55. ut_ad(b);
  56. ut_ad(n <= 0xFFFFUL);
  57. b[0] = (byte)(n >> 8);
  58. b[1] = (byte)(n);
  59. }
  60. /********************************************************//**
  61. The following function is used to fetch data from 2 consecutive
  62. bytes. The most significant byte is at the lowest address.
  63. @return ulint integer */
  64. UNIV_INLINE
  65. ulint
  66. mach_read_from_2(
  67. /*=============*/
  68. const byte* b) /*!< in: pointer to 2 bytes */
  69. {
  70. ut_ad(b);
  71. return( ((ulint)(b[0]) << 8)
  72. + (ulint)(b[1])
  73. );
  74. }
  75. /********************************************************//**
  76. The following function is used to convert a 16-bit data item
  77. to the canonical format, for fast bytewise equality test
  78. against memory.
  79. @return 16-bit integer in canonical format */
  80. UNIV_INLINE
  81. uint16
  82. mach_encode_2(
  83. /*==========*/
  84. ulint n) /*!< in: integer in machine-dependent format */
  85. {
  86. uint16 ret;
  87. ut_ad(2 == sizeof ret);
  88. mach_write_to_2((byte*) &ret, n);
  89. return(ret);
  90. }
  91. /********************************************************//**
  92. The following function is used to convert a 16-bit data item
  93. from the canonical format, for fast bytewise equality test
  94. against memory.
  95. @return integer in machine-dependent format */
  96. UNIV_INLINE
  97. ulint
  98. mach_decode_2(
  99. /*==========*/
  100. uint16 n) /*!< in: 16-bit integer in canonical format */
  101. {
  102. ut_ad(2 == sizeof n);
  103. return(mach_read_from_2((const byte*) &n));
  104. }
  105. /*******************************************************//**
  106. The following function is used to store data in 3 consecutive
  107. bytes. We store the most significant byte to the lowest address. */
  108. UNIV_INLINE
  109. void
  110. mach_write_to_3(
  111. /*============*/
  112. byte* b, /*!< in: pointer to 3 bytes where to store */
  113. ulint n) /*!< in: ulint integer to be stored */
  114. {
  115. ut_ad(b);
  116. ut_ad(n <= 0xFFFFFFUL);
  117. b[0] = (byte)(n >> 16);
  118. b[1] = (byte)(n >> 8);
  119. b[2] = (byte)(n);
  120. }
  121. /********************************************************//**
  122. The following function is used to fetch data from 3 consecutive
  123. bytes. The most significant byte is at the lowest address.
  124. @return ulint integer */
  125. UNIV_INLINE
  126. ulint
  127. mach_read_from_3(
  128. /*=============*/
  129. const byte* b) /*!< in: pointer to 3 bytes */
  130. {
  131. ut_ad(b);
  132. return( ((ulint)(b[0]) << 16)
  133. + ((ulint)(b[1]) << 8)
  134. + (ulint)(b[2])
  135. );
  136. }
  137. /*******************************************************//**
  138. The following function is used to store data in four consecutive
  139. bytes. We store the most significant byte to the lowest address. */
  140. UNIV_INLINE
  141. void
  142. mach_write_to_4(
  143. /*============*/
  144. byte* b, /*!< in: pointer to four bytes where to store */
  145. ulint n) /*!< in: ulint integer to be stored */
  146. {
  147. ut_ad(b);
  148. b[0] = (byte)(n >> 24);
  149. b[1] = (byte)(n >> 16);
  150. b[2] = (byte)(n >> 8);
  151. b[3] = (byte)n;
  152. }
  153. /********************************************************//**
  154. The following function is used to fetch data from 4 consecutive
  155. bytes. The most significant byte is at the lowest address.
  156. @return ulint integer */
  157. UNIV_INLINE
  158. ulint
  159. mach_read_from_4(
  160. /*=============*/
  161. const byte* b) /*!< in: pointer to four bytes */
  162. {
  163. ut_ad(b);
  164. return( ((ulint)(b[0]) << 24)
  165. + ((ulint)(b[1]) << 16)
  166. + ((ulint)(b[2]) << 8)
  167. + (ulint)(b[3])
  168. );
  169. }
  170. /*********************************************************//**
  171. Writes a ulint in a compressed form where the first byte codes the
  172. length of the stored ulint. We look at the most significant bits of
  173. the byte. If the most significant bit is zero, it means 1-byte storage,
  174. else if the 2nd bit is 0, it means 2-byte storage, else if 3rd is 0,
  175. it means 3-byte storage, else if 4th is 0, it means 4-byte storage,
  176. else the storage is 5-byte.
  177. @return compressed size in bytes */
  178. UNIV_INLINE
  179. ulint
  180. mach_write_compressed(
  181. /*==================*/
  182. byte* b, /*!< in: pointer to memory where to store */
  183. ulint n) /*!< in: ulint integer (< 2^32) to be stored */
  184. {
  185. ut_ad(b);
  186. if (n < 0x80UL) {
  187. mach_write_to_1(b, n);
  188. return(1);
  189. } else if (n < 0x4000UL) {
  190. mach_write_to_2(b, n | 0x8000UL);
  191. return(2);
  192. } else if (n < 0x200000UL) {
  193. mach_write_to_3(b, n | 0xC00000UL);
  194. return(3);
  195. } else if (n < 0x10000000UL) {
  196. mach_write_to_4(b, n | 0xE0000000UL);
  197. return(4);
  198. } else {
  199. mach_write_to_1(b, 0xF0UL);
  200. mach_write_to_4(b + 1, n);
  201. return(5);
  202. }
  203. }
  204. /*********************************************************//**
  205. Returns the size of a ulint when written in the compressed form.
  206. @return compressed size in bytes */
  207. UNIV_INLINE
  208. ulint
  209. mach_get_compressed_size(
  210. /*=====================*/
  211. ulint n) /*!< in: ulint integer (< 2^32) to be stored */
  212. {
  213. if (n < 0x80UL) {
  214. return(1);
  215. } else if (n < 0x4000UL) {
  216. return(2);
  217. } else if (n < 0x200000UL) {
  218. return(3);
  219. } else if (n < 0x10000000UL) {
  220. return(4);
  221. } else {
  222. return(5);
  223. }
  224. }
  225. /*********************************************************//**
  226. Reads a ulint in a compressed form.
  227. @return read integer (< 2^32) */
  228. UNIV_INLINE
  229. ulint
  230. mach_read_compressed(
  231. /*=================*/
  232. const byte* b) /*!< in: pointer to memory from where to read */
  233. {
  234. ulint flag;
  235. ut_ad(b);
  236. flag = mach_read_from_1(b);
  237. if (flag < 0x80UL) {
  238. return(flag);
  239. } else if (flag < 0xC0UL) {
  240. return(mach_read_from_2(b) & 0x7FFFUL);
  241. } else if (flag < 0xE0UL) {
  242. return(mach_read_from_3(b) & 0x3FFFFFUL);
  243. } else if (flag < 0xF0UL) {
  244. return(mach_read_from_4(b) & 0x1FFFFFFFUL);
  245. } else {
  246. ut_ad(flag == 0xF0UL);
  247. return(mach_read_from_4(b + 1));
  248. }
  249. }
  250. /*******************************************************//**
  251. The following function is used to store data in 8 consecutive
  252. bytes. We store the most significant byte to the lowest address. */
  253. UNIV_INLINE
  254. void
  255. mach_write_to_8(
  256. /*============*/
  257. byte* b, /*!< in: pointer to 8 bytes where to store */
  258. dulint n) /*!< in: dulint integer to be stored */
  259. {
  260. ut_ad(b);
  261. mach_write_to_4(b, ut_dulint_get_high(n));
  262. mach_write_to_4(b + 4, ut_dulint_get_low(n));
  263. }
  264. /*******************************************************//**
  265. The following function is used to store data in 8 consecutive
  266. bytes. We store the most significant byte to the lowest address. */
  267. UNIV_INLINE
  268. void
  269. mach_write_ull(
  270. /*===========*/
  271. byte* b, /*!< in: pointer to 8 bytes where to store */
  272. ib_uint64_t n) /*!< in: 64-bit integer to be stored */
  273. {
  274. ut_ad(b);
  275. mach_write_to_4(b, (ulint) (n >> 32));
  276. mach_write_to_4(b + 4, (ulint) n);
  277. }
  278. /********************************************************//**
  279. The following function is used to fetch data from 8 consecutive
  280. bytes. The most significant byte is at the lowest address.
  281. @return dulint integer */
  282. UNIV_INLINE
  283. dulint
  284. mach_read_from_8(
  285. /*=============*/
  286. const byte* b) /*!< in: pointer to 8 bytes */
  287. {
  288. ulint high;
  289. ulint low;
  290. ut_ad(b);
  291. high = mach_read_from_4(b);
  292. low = mach_read_from_4(b + 4);
  293. return(ut_dulint_create(high, low));
  294. }
  295. /********************************************************//**
  296. The following function is used to fetch data from 8 consecutive
  297. bytes. The most significant byte is at the lowest address.
  298. @return 64-bit integer */
  299. UNIV_INLINE
  300. ib_uint64_t
  301. mach_read_ull(
  302. /*==========*/
  303. const byte* b) /*!< in: pointer to 8 bytes */
  304. {
  305. ib_uint64_t ull;
  306. ull = ((ib_uint64_t) mach_read_from_4(b)) << 32;
  307. ull |= (ib_uint64_t) mach_read_from_4(b + 4);
  308. return(ull);
  309. }
  310. /*******************************************************//**
  311. The following function is used to store data in 7 consecutive
  312. bytes. We store the most significant byte to the lowest address. */
  313. UNIV_INLINE
  314. void
  315. mach_write_to_7(
  316. /*============*/
  317. byte* b, /*!< in: pointer to 7 bytes where to store */
  318. dulint n) /*!< in: dulint integer to be stored */
  319. {
  320. ut_ad(b);
  321. mach_write_to_3(b, ut_dulint_get_high(n));
  322. mach_write_to_4(b + 3, ut_dulint_get_low(n));
  323. }
  324. /********************************************************//**
  325. The following function is used to fetch data from 7 consecutive
  326. bytes. The most significant byte is at the lowest address.
  327. @return dulint integer */
  328. UNIV_INLINE
  329. dulint
  330. mach_read_from_7(
  331. /*=============*/
  332. const byte* b) /*!< in: pointer to 7 bytes */
  333. {
  334. ulint high;
  335. ulint low;
  336. ut_ad(b);
  337. high = mach_read_from_3(b);
  338. low = mach_read_from_4(b + 3);
  339. return(ut_dulint_create(high, low));
  340. }
  341. /*******************************************************//**
  342. The following function is used to store data in 6 consecutive
  343. bytes. We store the most significant byte to the lowest address. */
  344. UNIV_INLINE
  345. void
  346. mach_write_to_6(
  347. /*============*/
  348. byte* b, /*!< in: pointer to 6 bytes where to store */
  349. dulint n) /*!< in: dulint integer to be stored */
  350. {
  351. ut_ad(b);
  352. mach_write_to_2(b, ut_dulint_get_high(n));
  353. mach_write_to_4(b + 2, ut_dulint_get_low(n));
  354. }
  355. /********************************************************//**
  356. The following function is used to fetch data from 6 consecutive
  357. bytes. The most significant byte is at the lowest address.
  358. @return dulint integer */
  359. UNIV_INLINE
  360. dulint
  361. mach_read_from_6(
  362. /*=============*/
  363. const byte* b) /*!< in: pointer to 6 bytes */
  364. {
  365. ulint high;
  366. ulint low;
  367. ut_ad(b);
  368. high = mach_read_from_2(b);
  369. low = mach_read_from_4(b + 2);
  370. return(ut_dulint_create(high, low));
  371. }
  372. /*********************************************************//**
  373. Writes a dulint in a compressed form (5..9 bytes).
  374. @return size in bytes */
  375. UNIV_INLINE
  376. ulint
  377. mach_dulint_write_compressed(
  378. /*=========================*/
  379. byte* b, /*!< in: pointer to memory where to store */
  380. dulint n) /*!< in: dulint integer to be stored */
  381. {
  382. ulint size;
  383. ut_ad(b);
  384. size = mach_write_compressed(b, ut_dulint_get_high(n));
  385. mach_write_to_4(b + size, ut_dulint_get_low(n));
  386. return(size + 4);
  387. }
  388. /*********************************************************//**
  389. Returns the size of a dulint when written in the compressed form.
  390. @return compressed size in bytes */
  391. UNIV_INLINE
  392. ulint
  393. mach_dulint_get_compressed_size(
  394. /*============================*/
  395. dulint n) /*!< in: dulint integer to be stored */
  396. {
  397. return(4 + mach_get_compressed_size(ut_dulint_get_high(n)));
  398. }
  399. /*********************************************************//**
  400. Reads a dulint in a compressed form.
  401. @return read dulint */
  402. UNIV_INLINE
  403. dulint
  404. mach_dulint_read_compressed(
  405. /*========================*/
  406. const byte* b) /*!< in: pointer to memory from where to read */
  407. {
  408. ulint high;
  409. ulint low;
  410. ulint size;
  411. ut_ad(b);
  412. high = mach_read_compressed(b);
  413. size = mach_get_compressed_size(high);
  414. low = mach_read_from_4(b + size);
  415. return(ut_dulint_create(high, low));
  416. }
  417. /*********************************************************//**
  418. Writes a dulint in a compressed form (1..11 bytes).
  419. @return size in bytes */
  420. UNIV_INLINE
  421. ulint
  422. mach_dulint_write_much_compressed(
  423. /*==============================*/
  424. byte* b, /*!< in: pointer to memory where to store */
  425. dulint n) /*!< in: dulint integer to be stored */
  426. {
  427. ulint size;
  428. ut_ad(b);
  429. if (ut_dulint_get_high(n) == 0) {
  430. return(mach_write_compressed(b, ut_dulint_get_low(n)));
  431. }
  432. *b = (byte)0xFF;
  433. size = 1 + mach_write_compressed(b + 1, ut_dulint_get_high(n));
  434. size += mach_write_compressed(b + size, ut_dulint_get_low(n));
  435. return(size);
  436. }
  437. /*********************************************************//**
  438. Returns the size of a dulint when written in the compressed form.
  439. @return compressed size in bytes */
  440. UNIV_INLINE
  441. ulint
  442. mach_dulint_get_much_compressed_size(
  443. /*=================================*/
  444. dulint n) /*!< in: dulint integer to be stored */
  445. {
  446. if (0 == ut_dulint_get_high(n)) {
  447. return(mach_get_compressed_size(ut_dulint_get_low(n)));
  448. }
  449. return(1 + mach_get_compressed_size(ut_dulint_get_high(n))
  450. + mach_get_compressed_size(ut_dulint_get_low(n)));
  451. }
  452. /*********************************************************//**
  453. Reads a dulint in a compressed form.
  454. @return read dulint */
  455. UNIV_INLINE
  456. dulint
  457. mach_dulint_read_much_compressed(
  458. /*=============================*/
  459. const byte* b) /*!< in: pointer to memory from where to read */
  460. {
  461. ulint high;
  462. ulint low;
  463. ulint size;
  464. ut_ad(b);
  465. if (*b != (byte)0xFF) {
  466. high = 0;
  467. size = 0;
  468. } else {
  469. high = mach_read_compressed(b + 1);
  470. size = 1 + mach_get_compressed_size(high);
  471. }
  472. low = mach_read_compressed(b + size);
  473. return(ut_dulint_create(high, low));
  474. }
  475. #ifndef UNIV_HOTBACKUP
  476. /*********************************************************//**
  477. Reads a double. It is stored in a little-endian format.
  478. @return double read */
  479. UNIV_INLINE
  480. double
  481. mach_double_read(
  482. /*=============*/
  483. const byte* b) /*!< in: pointer to memory from where to read */
  484. {
  485. double d;
  486. ulint i;
  487. byte* ptr;
  488. ptr = (byte*)&d;
  489. for (i = 0; i < sizeof(double); i++) {
  490. #ifdef WORDS_BIGENDIAN
  491. ptr[sizeof(double) - i - 1] = b[i];
  492. #else
  493. ptr[i] = b[i];
  494. #endif
  495. }
  496. return(d);
  497. }
  498. /*********************************************************//**
  499. Writes a double. It is stored in a little-endian format. */
  500. UNIV_INLINE
  501. void
  502. mach_double_write(
  503. /*==============*/
  504. byte* b, /*!< in: pointer to memory where to write */
  505. double d) /*!< in: double */
  506. {
  507. ulint i;
  508. byte* ptr;
  509. ptr = (byte*)&d;
  510. for (i = 0; i < sizeof(double); i++) {
  511. #ifdef WORDS_BIGENDIAN
  512. b[i] = ptr[sizeof(double) - i - 1];
  513. #else
  514. b[i] = ptr[i];
  515. #endif
  516. }
  517. }
  518. /*********************************************************//**
  519. Reads a float. It is stored in a little-endian format.
  520. @return float read */
  521. UNIV_INLINE
  522. float
  523. mach_float_read(
  524. /*============*/
  525. const byte* b) /*!< in: pointer to memory from where to read */
  526. {
  527. float d;
  528. ulint i;
  529. byte* ptr;
  530. ptr = (byte*)&d;
  531. for (i = 0; i < sizeof(float); i++) {
  532. #ifdef WORDS_BIGENDIAN
  533. ptr[sizeof(float) - i - 1] = b[i];
  534. #else
  535. ptr[i] = b[i];
  536. #endif
  537. }
  538. return(d);
  539. }
  540. /*********************************************************//**
  541. Writes a float. It is stored in a little-endian format. */
  542. UNIV_INLINE
  543. void
  544. mach_float_write(
  545. /*=============*/
  546. byte* b, /*!< in: pointer to memory where to write */
  547. float d) /*!< in: float */
  548. {
  549. ulint i;
  550. byte* ptr;
  551. ptr = (byte*)&d;
  552. for (i = 0; i < sizeof(float); i++) {
  553. #ifdef WORDS_BIGENDIAN
  554. b[i] = ptr[sizeof(float) - i - 1];
  555. #else
  556. b[i] = ptr[i];
  557. #endif
  558. }
  559. }
  560. /*********************************************************//**
  561. Reads a ulint stored in the little-endian format.
  562. @return unsigned long int */
  563. UNIV_INLINE
  564. ulint
  565. mach_read_from_n_little_endian(
  566. /*===========================*/
  567. const byte* buf, /*!< in: from where to read */
  568. ulint buf_size) /*!< in: from how many bytes to read */
  569. {
  570. ulint n = 0;
  571. const byte* ptr;
  572. ut_ad(buf_size <= sizeof(ulint));
  573. ut_ad(buf_size > 0);
  574. ptr = buf + buf_size;
  575. for (;;) {
  576. ptr--;
  577. n = n << 8;
  578. n += (ulint)(*ptr);
  579. if (ptr == buf) {
  580. break;
  581. }
  582. }
  583. return(n);
  584. }
  585. /*********************************************************//**
  586. Writes a ulint in the little-endian format. */
  587. UNIV_INLINE
  588. void
  589. mach_write_to_n_little_endian(
  590. /*==========================*/
  591. byte* dest, /*!< in: where to write */
  592. ulint dest_size, /*!< in: into how many bytes to write */
  593. ulint n) /*!< in: unsigned long int to write */
  594. {
  595. byte* end;
  596. ut_ad(dest_size <= sizeof(ulint));
  597. ut_ad(dest_size > 0);
  598. end = dest + dest_size;
  599. for (;;) {
  600. *dest = (byte)(n & 0xFF);
  601. n = n >> 8;
  602. dest++;
  603. if (dest == end) {
  604. break;
  605. }
  606. }
  607. ut_ad(n == 0);
  608. }
  609. /*********************************************************//**
  610. Reads a ulint stored in the little-endian format.
  611. @return unsigned long int */
  612. UNIV_INLINE
  613. ulint
  614. mach_read_from_2_little_endian(
  615. /*===========================*/
  616. const byte* buf) /*!< in: from where to read */
  617. {
  618. return((ulint)(*buf) + ((ulint)(*(buf + 1))) * 256);
  619. }
  620. /*********************************************************//**
  621. Writes a ulint in the little-endian format. */
  622. UNIV_INLINE
  623. void
  624. mach_write_to_2_little_endian(
  625. /*==========================*/
  626. byte* dest, /*!< in: where to write */
  627. ulint n) /*!< in: unsigned long int to write */
  628. {
  629. ut_ad(n < 256 * 256);
  630. *dest = (byte)(n & 0xFFUL);
  631. n = n >> 8;
  632. dest++;
  633. *dest = (byte)(n & 0xFFUL);
  634. }
  635. /*********************************************************//**
  636. Convert integral type from storage byte order (big endian) to
  637. host byte order.
  638. @return integer value */
  639. UNIV_INLINE
  640. ullint
  641. mach_read_int_type(
  642. /*===============*/
  643. const byte* src, /*!< in: where to read from */
  644. ulint len, /*!< in: length of src */
  645. ibool unsigned_type) /*!< in: signed or unsigned flag */
  646. {
  647. /* XXX this can be optimized on big-endian machines */
  648. ullint ret;
  649. uint i;
  650. if (unsigned_type || (src[0] & 0x80)) {
  651. ret = 0x0000000000000000ULL;
  652. } else {
  653. ret = 0xFFFFFFFFFFFFFF00ULL;
  654. }
  655. if (unsigned_type) {
  656. ret |= src[0];
  657. } else {
  658. ret |= src[0] ^ 0x80;
  659. }
  660. for (i = 1; i < len; i++) {
  661. ret <<= 8;
  662. ret |= src[i];
  663. }
  664. return(ret);
  665. }
  666. #endif /* !UNIV_HOTBACKUP */