tif_swab.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 1988-1997 Sam Leffler
  3. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10. * publicity relating to the software without the specific, prior written
  11. * permission of Sam Leffler and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. /*
  25. * TIFF Library Bit & Byte Swapping Support.
  26. *
  27. * XXX We assume short = 16-bits and long = 32-bits XXX
  28. */
  29. #include "tiffiop.h"
  30. #if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabShort)
  31. void TIFFSwabShort(uint16_t *wp)
  32. {
  33. register unsigned char *cp = (unsigned char *)wp;
  34. unsigned char t;
  35. assert(sizeof(uint16_t) == 2);
  36. t = cp[1];
  37. cp[1] = cp[0];
  38. cp[0] = t;
  39. }
  40. #endif
  41. #if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabLong)
  42. void TIFFSwabLong(uint32_t *lp)
  43. {
  44. register unsigned char *cp = (unsigned char *)lp;
  45. unsigned char t;
  46. assert(sizeof(uint32_t) == 4);
  47. t = cp[3];
  48. cp[3] = cp[0];
  49. cp[0] = t;
  50. t = cp[2];
  51. cp[2] = cp[1];
  52. cp[1] = t;
  53. }
  54. #endif
  55. #if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabLong8)
  56. void TIFFSwabLong8(uint64_t *lp)
  57. {
  58. register unsigned char *cp = (unsigned char *)lp;
  59. unsigned char t;
  60. assert(sizeof(uint64_t) == 8);
  61. t = cp[7];
  62. cp[7] = cp[0];
  63. cp[0] = t;
  64. t = cp[6];
  65. cp[6] = cp[1];
  66. cp[1] = t;
  67. t = cp[5];
  68. cp[5] = cp[2];
  69. cp[2] = t;
  70. t = cp[4];
  71. cp[4] = cp[3];
  72. cp[3] = t;
  73. }
  74. #endif
  75. #if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfShort)
  76. void TIFFSwabArrayOfShort(register uint16_t *wp, tmsize_t n)
  77. {
  78. register unsigned char *cp;
  79. register unsigned char t;
  80. assert(sizeof(uint16_t) == 2);
  81. /* XXX unroll loop some */
  82. while (n-- > 0)
  83. {
  84. cp = (unsigned char *)wp;
  85. t = cp[1];
  86. cp[1] = cp[0];
  87. cp[0] = t;
  88. wp++;
  89. }
  90. }
  91. #endif
  92. #if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfTriples)
  93. void TIFFSwabArrayOfTriples(register uint8_t *tp, tmsize_t n)
  94. {
  95. unsigned char *cp;
  96. unsigned char t;
  97. /* XXX unroll loop some */
  98. while (n-- > 0)
  99. {
  100. cp = (unsigned char *)tp;
  101. t = cp[2];
  102. cp[2] = cp[0];
  103. cp[0] = t;
  104. tp += 3;
  105. }
  106. }
  107. #endif
  108. #if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfLong)
  109. void TIFFSwabArrayOfLong(register uint32_t *lp, tmsize_t n)
  110. {
  111. register unsigned char *cp;
  112. register unsigned char t;
  113. assert(sizeof(uint32_t) == 4);
  114. /* XXX unroll loop some */
  115. while (n-- > 0)
  116. {
  117. cp = (unsigned char *)lp;
  118. t = cp[3];
  119. cp[3] = cp[0];
  120. cp[0] = t;
  121. t = cp[2];
  122. cp[2] = cp[1];
  123. cp[1] = t;
  124. lp++;
  125. }
  126. }
  127. #endif
  128. #if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfLong8)
  129. void TIFFSwabArrayOfLong8(register uint64_t *lp, tmsize_t n)
  130. {
  131. register unsigned char *cp;
  132. register unsigned char t;
  133. assert(sizeof(uint64_t) == 8);
  134. /* XXX unroll loop some */
  135. while (n-- > 0)
  136. {
  137. cp = (unsigned char *)lp;
  138. t = cp[7];
  139. cp[7] = cp[0];
  140. cp[0] = t;
  141. t = cp[6];
  142. cp[6] = cp[1];
  143. cp[1] = t;
  144. t = cp[5];
  145. cp[5] = cp[2];
  146. cp[2] = t;
  147. t = cp[4];
  148. cp[4] = cp[3];
  149. cp[3] = t;
  150. lp++;
  151. }
  152. }
  153. #endif
  154. #if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabFloat)
  155. void TIFFSwabFloat(float *fp)
  156. {
  157. register unsigned char *cp = (unsigned char *)fp;
  158. unsigned char t;
  159. assert(sizeof(float) == 4);
  160. t = cp[3];
  161. cp[3] = cp[0];
  162. cp[0] = t;
  163. t = cp[2];
  164. cp[2] = cp[1];
  165. cp[1] = t;
  166. }
  167. #endif
  168. #if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfFloat)
  169. void TIFFSwabArrayOfFloat(register float *fp, tmsize_t n)
  170. {
  171. register unsigned char *cp;
  172. register unsigned char t;
  173. assert(sizeof(float) == 4);
  174. /* XXX unroll loop some */
  175. while (n-- > 0)
  176. {
  177. cp = (unsigned char *)fp;
  178. t = cp[3];
  179. cp[3] = cp[0];
  180. cp[0] = t;
  181. t = cp[2];
  182. cp[2] = cp[1];
  183. cp[1] = t;
  184. fp++;
  185. }
  186. }
  187. #endif
  188. #if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabDouble)
  189. void TIFFSwabDouble(double *dp)
  190. {
  191. register unsigned char *cp = (unsigned char *)dp;
  192. unsigned char t;
  193. assert(sizeof(double) == 8);
  194. t = cp[7];
  195. cp[7] = cp[0];
  196. cp[0] = t;
  197. t = cp[6];
  198. cp[6] = cp[1];
  199. cp[1] = t;
  200. t = cp[5];
  201. cp[5] = cp[2];
  202. cp[2] = t;
  203. t = cp[4];
  204. cp[4] = cp[3];
  205. cp[3] = t;
  206. }
  207. #endif
  208. #if defined(DISABLE_CHECK_TIFFSWABMACROS) || !defined(TIFFSwabArrayOfDouble)
  209. void TIFFSwabArrayOfDouble(double *dp, tmsize_t n)
  210. {
  211. register unsigned char *cp;
  212. register unsigned char t;
  213. assert(sizeof(double) == 8);
  214. /* XXX unroll loop some */
  215. while (n-- > 0)
  216. {
  217. cp = (unsigned char *)dp;
  218. t = cp[7];
  219. cp[7] = cp[0];
  220. cp[0] = t;
  221. t = cp[6];
  222. cp[6] = cp[1];
  223. cp[1] = t;
  224. t = cp[5];
  225. cp[5] = cp[2];
  226. cp[2] = t;
  227. t = cp[4];
  228. cp[4] = cp[3];
  229. cp[3] = t;
  230. dp++;
  231. }
  232. }
  233. #endif
  234. /*
  235. * Bit reversal tables. TIFFBitRevTable[<byte>] gives
  236. * the bit reversed value of <byte>. Used in various
  237. * places in the library when the FillOrder requires
  238. * bit reversal of byte values (e.g. CCITT Fax 3
  239. * encoding/decoding). TIFFNoBitRevTable is provided
  240. * for algorithms that want an equivalent table that
  241. * do not reverse bit values.
  242. */
  243. static const unsigned char TIFFBitRevTable[256] = {
  244. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0,
  245. 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  246. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4,
  247. 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  248. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc,
  249. 0x3c, 0xbc, 0x7c, 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  250. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a, 0x8a, 0x4a, 0xca,
  251. 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  252. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6,
  253. 0x36, 0xb6, 0x76, 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  254. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x01, 0x81, 0x41, 0xc1,
  255. 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  256. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9,
  257. 0x39, 0xb9, 0x79, 0xf9, 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  258. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 0x0d, 0x8d, 0x4d, 0xcd,
  259. 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  260. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3,
  261. 0x33, 0xb3, 0x73, 0xf3, 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  262. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7,
  263. 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  264. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf,
  265. 0x3f, 0xbf, 0x7f, 0xff};
  266. static const unsigned char TIFFNoBitRevTable[256] = {
  267. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  268. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  269. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  270. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  271. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  272. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  273. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53,
  274. 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  275. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  276. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  277. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
  278. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  279. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  280. 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  281. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
  282. 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  283. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
  284. 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  285. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
  286. 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  287. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  288. 0xfc, 0xfd, 0xfe, 0xff,
  289. };
  290. const unsigned char *TIFFGetBitRevTable(int reversed)
  291. {
  292. return (reversed ? TIFFBitRevTable : TIFFNoBitRevTable);
  293. }
  294. void TIFFReverseBits(uint8_t *cp, tmsize_t n)
  295. {
  296. for (; n > 8; n -= 8)
  297. {
  298. cp[0] = TIFFBitRevTable[cp[0]];
  299. cp[1] = TIFFBitRevTable[cp[1]];
  300. cp[2] = TIFFBitRevTable[cp[2]];
  301. cp[3] = TIFFBitRevTable[cp[3]];
  302. cp[4] = TIFFBitRevTable[cp[4]];
  303. cp[5] = TIFFBitRevTable[cp[5]];
  304. cp[6] = TIFFBitRevTable[cp[6]];
  305. cp[7] = TIFFBitRevTable[cp[7]];
  306. cp += 8;
  307. }
  308. while (n-- > 0)
  309. {
  310. *cp = TIFFBitRevTable[*cp];
  311. cp++;
  312. }
  313. }