EXIF.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "EXIF.h"
  6. #include "mozilla/EndianUtils.h"
  7. namespace mozilla {
  8. namespace image {
  9. // Section references in this file refer to the EXIF v2.3 standard, also known
  10. // as CIPA DC-008-Translation-2010.
  11. // See Section 4.6.4, Table 4.
  12. // Typesafe enums are intentionally not used here since we're comparing to raw
  13. // integers produced by parsing.
  14. enum EXIFTag
  15. {
  16. OrientationTag = 0x112,
  17. };
  18. // See Section 4.6.2.
  19. enum EXIFType
  20. {
  21. ByteType = 1,
  22. ASCIIType = 2,
  23. ShortType = 3,
  24. LongType = 4,
  25. RationalType = 5,
  26. UndefinedType = 7,
  27. SignedLongType = 9,
  28. SignedRational = 10,
  29. };
  30. static const char* EXIFHeader = "Exif\0\0";
  31. static const uint32_t EXIFHeaderLength = 6;
  32. /////////////////////////////////////////////////////////////
  33. // Parse EXIF data, typically found in a JPEG's APP1 segment.
  34. /////////////////////////////////////////////////////////////
  35. EXIFData
  36. EXIFParser::ParseEXIF(const uint8_t* aData, const uint32_t aLength)
  37. {
  38. if (!Initialize(aData, aLength)) {
  39. return EXIFData();
  40. }
  41. if (!ParseEXIFHeader()) {
  42. return EXIFData();
  43. }
  44. uint32_t offsetIFD;
  45. if (!ParseTIFFHeader(offsetIFD)) {
  46. return EXIFData();
  47. }
  48. JumpTo(offsetIFD);
  49. Orientation orientation;
  50. if (!ParseIFD0(orientation)) {
  51. return EXIFData();
  52. }
  53. // We only care about orientation at this point, so we don't bother with the
  54. // other IFDs. If we got this far we're done.
  55. return EXIFData(orientation);
  56. }
  57. /////////////////////////////////////////////////////////
  58. // Parse the EXIF header. (Section 4.7.2, Figure 30)
  59. /////////////////////////////////////////////////////////
  60. bool
  61. EXIFParser::ParseEXIFHeader()
  62. {
  63. return MatchString(EXIFHeader, EXIFHeaderLength);
  64. }
  65. /////////////////////////////////////////////////////////
  66. // Parse the TIFF header. (Section 4.5.2, Table 1)
  67. /////////////////////////////////////////////////////////
  68. bool
  69. EXIFParser::ParseTIFFHeader(uint32_t& aIFD0OffsetOut)
  70. {
  71. // Determine byte order.
  72. if (MatchString("MM\0*", 4)) {
  73. mByteOrder = ByteOrder::BigEndian;
  74. } else if (MatchString("II*\0", 4)) {
  75. mByteOrder = ByteOrder::LittleEndian;
  76. } else {
  77. return false;
  78. }
  79. // Determine offset of the 0th IFD. (It shouldn't be greater than 64k, which
  80. // is the maximum size of the entry APP1 segment.)
  81. uint32_t ifd0Offset;
  82. if (!ReadUInt32(ifd0Offset) || ifd0Offset > 64 * 1024) {
  83. return false;
  84. }
  85. // The IFD offset is relative to the beginning of the TIFF header, which
  86. // begins after the EXIF header, so we need to increase the offset
  87. // appropriately.
  88. aIFD0OffsetOut = ifd0Offset + EXIFHeaderLength;
  89. return true;
  90. }
  91. /////////////////////////////////////////////////////////
  92. // Parse the entries in IFD0. (Section 4.6.2)
  93. /////////////////////////////////////////////////////////
  94. bool
  95. EXIFParser::ParseIFD0(Orientation& aOrientationOut)
  96. {
  97. uint16_t entryCount;
  98. if (!ReadUInt16(entryCount)) {
  99. return false;
  100. }
  101. for (uint16_t entry = 0 ; entry < entryCount ; ++entry) {
  102. // Read the fields of the entry.
  103. uint16_t tag;
  104. if (!ReadUInt16(tag)) {
  105. return false;
  106. }
  107. // Right now, we only care about orientation, so we immediately skip to the
  108. // next entry if we find anything else.
  109. if (tag != OrientationTag) {
  110. Advance(10);
  111. continue;
  112. }
  113. uint16_t type;
  114. if (!ReadUInt16(type)) {
  115. return false;
  116. }
  117. uint32_t count;
  118. if (!ReadUInt32(count)) {
  119. return false;
  120. }
  121. // We should have an orientation value here; go ahead and parse it.
  122. if (!ParseOrientation(type, count, aOrientationOut)) {
  123. return false;
  124. }
  125. // Since the orientation is all we care about, we're done.
  126. return true;
  127. }
  128. // We didn't find an orientation field in the IFD. That's OK; we assume the
  129. // default orientation in that case.
  130. aOrientationOut = Orientation();
  131. return true;
  132. }
  133. bool
  134. EXIFParser::ParseOrientation(uint16_t aType, uint32_t aCount, Orientation& aOut)
  135. {
  136. // Sanity check the type and count.
  137. if (aType != ShortType || aCount != 1) {
  138. return false;
  139. }
  140. uint16_t value;
  141. if (!ReadUInt16(value)) {
  142. return false;
  143. }
  144. switch (value) {
  145. case 1: aOut = Orientation(Angle::D0, Flip::Unflipped); break;
  146. case 2: aOut = Orientation(Angle::D0, Flip::Horizontal); break;
  147. case 3: aOut = Orientation(Angle::D180, Flip::Unflipped); break;
  148. case 4: aOut = Orientation(Angle::D180, Flip::Horizontal); break;
  149. case 5: aOut = Orientation(Angle::D90, Flip::Horizontal); break;
  150. case 6: aOut = Orientation(Angle::D90, Flip::Unflipped); break;
  151. case 7: aOut = Orientation(Angle::D270, Flip::Horizontal); break;
  152. case 8: aOut = Orientation(Angle::D270, Flip::Unflipped); break;
  153. default: return false;
  154. }
  155. // This is a 32-bit field, but the orientation value only occupies the first
  156. // 16 bits. We need to advance another 16 bits to consume the entire field.
  157. Advance(2);
  158. return true;
  159. }
  160. bool
  161. EXIFParser::Initialize(const uint8_t* aData, const uint32_t aLength)
  162. {
  163. if (aData == nullptr) {
  164. return false;
  165. }
  166. // An APP1 segment larger than 64k violates the JPEG standard.
  167. if (aLength > 64 * 1024) {
  168. return false;
  169. }
  170. mStart = mCurrent = aData;
  171. mLength = mRemainingLength = aLength;
  172. mByteOrder = ByteOrder::Unknown;
  173. return true;
  174. }
  175. void
  176. EXIFParser::Advance(const uint32_t aDistance)
  177. {
  178. if (mRemainingLength >= aDistance) {
  179. mCurrent += aDistance;
  180. mRemainingLength -= aDistance;
  181. } else {
  182. mCurrent = mStart;
  183. mRemainingLength = 0;
  184. }
  185. }
  186. void
  187. EXIFParser::JumpTo(const uint32_t aOffset)
  188. {
  189. if (mLength >= aOffset) {
  190. mCurrent = mStart + aOffset;
  191. mRemainingLength = mLength - aOffset;
  192. } else {
  193. mCurrent = mStart;
  194. mRemainingLength = 0;
  195. }
  196. }
  197. bool
  198. EXIFParser::MatchString(const char* aString, const uint32_t aLength)
  199. {
  200. if (mRemainingLength < aLength) {
  201. return false;
  202. }
  203. for (uint32_t i = 0 ; i < aLength ; ++i) {
  204. if (mCurrent[i] != aString[i]) {
  205. return false;
  206. }
  207. }
  208. Advance(aLength);
  209. return true;
  210. }
  211. bool
  212. EXIFParser::MatchUInt16(const uint16_t aValue)
  213. {
  214. if (mRemainingLength < 2) {
  215. return false;
  216. }
  217. bool matched;
  218. switch (mByteOrder) {
  219. case ByteOrder::LittleEndian:
  220. matched = LittleEndian::readUint16(mCurrent) == aValue;
  221. break;
  222. case ByteOrder::BigEndian:
  223. matched = BigEndian::readUint16(mCurrent) == aValue;
  224. break;
  225. default:
  226. NS_NOTREACHED("Should know the byte order by now");
  227. matched = false;
  228. }
  229. if (matched) {
  230. Advance(2);
  231. }
  232. return matched;
  233. }
  234. bool
  235. EXIFParser::ReadUInt16(uint16_t& aValue)
  236. {
  237. if (mRemainingLength < 2) {
  238. return false;
  239. }
  240. bool matched = true;
  241. switch (mByteOrder) {
  242. case ByteOrder::LittleEndian:
  243. aValue = LittleEndian::readUint16(mCurrent);
  244. break;
  245. case ByteOrder::BigEndian:
  246. aValue = BigEndian::readUint16(mCurrent);
  247. break;
  248. default:
  249. NS_NOTREACHED("Should know the byte order by now");
  250. matched = false;
  251. }
  252. if (matched) {
  253. Advance(2);
  254. }
  255. return matched;
  256. }
  257. bool
  258. EXIFParser::ReadUInt32(uint32_t& aValue)
  259. {
  260. if (mRemainingLength < 4) {
  261. return false;
  262. }
  263. bool matched = true;
  264. switch (mByteOrder) {
  265. case ByteOrder::LittleEndian:
  266. aValue = LittleEndian::readUint32(mCurrent);
  267. break;
  268. case ByteOrder::BigEndian:
  269. aValue = BigEndian::readUint32(mCurrent);
  270. break;
  271. default:
  272. NS_NOTREACHED("Should know the byte order by now");
  273. matched = false;
  274. }
  275. if (matched) {
  276. Advance(4);
  277. }
  278. return matched;
  279. }
  280. } // namespace image
  281. } // namespace mozilla