Replace GetWord_LSBFirst with FXWORD_GET_LSBFIRST

The existing implementation of a LSB first word method was incorrect,
in addition it was implemented to the BMP code, but also used in the
GIF code. Thus is should be moved to a common location.

Also added in an implementation for FXWORD_GET_MSBFIRST, since the
GIF code will need this.

BUG=pdfium:914

Change-Id: I0e84813356fbd456b293a190da3c2cde01a6580b
Reviewed-on: https://pdfium-review.googlesource.com/15210
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
diff --git a/core/fxcodec/gif/cfx_gifcontext.cpp b/core/fxcodec/gif/cfx_gifcontext.cpp
index a882a58..09e8753 100644
--- a/core/fxcodec/gif/cfx_gifcontext.cpp
+++ b/core/fxcodec/gif/cfx_gifcontext.cpp
@@ -11,7 +11,6 @@
 
 #include "core/fxcodec/codec/ccodec_gifmodule.h"
 #include "core/fxcodec/gif/cfx_gif.h"
-#include "core/fxcodec/lbmp/fx_bmp.h"
 #include "third_party/base/ptr_util.h"
 #include "third_party/base/stl_util.h"
 
@@ -99,9 +98,9 @@
   }
 
   width_ = static_cast<int>(
-      GetWord_LSBFirst(reinterpret_cast<uint8_t*>(&gif_lsd->width)));
+      FXWORD_GET_LSBFIRST(reinterpret_cast<uint8_t*>(&gif_lsd->width)));
   height_ = static_cast<int>(
-      GetWord_LSBFirst(reinterpret_cast<uint8_t*>(&gif_lsd->height)));
+      FXWORD_GET_LSBFIRST(reinterpret_cast<uint8_t*>(&gif_lsd->height)));
   bc_index_ = gif_lsd->bc_index;
   pixel_aspect_ = gif_lsd->pixel_aspect;
   return CFX_GifDecodeStatus::Success;
@@ -209,7 +208,7 @@
   uint8_t* img_data_size = nullptr;
   uint8_t* img_data = nullptr;
   uint32_t skip_size_org = skip_size_;
-  CFX_GifImage* gif_image = images_[frame_num].get();
+  CFX_GifImage* gif_image = images_[static_cast<size_t>(frame_num)].get();
   uint32_t gif_img_row_bytes = gif_image->image_info.width;
   if (gif_img_row_bytes == 0)
     return CFX_GifDecodeStatus::Error;
@@ -452,7 +451,7 @@
       graphic_control_extension_->block_size = gif_gce->block_size;
       graphic_control_extension_->gce_flags = gif_gce->gce_flags;
       graphic_control_extension_->delay_time =
-          GetWord_LSBFirst(reinterpret_cast<uint8_t*>(&gif_gce->delay_time));
+          FXWORD_GET_LSBFIRST(reinterpret_cast<uint8_t*>(&gif_gce->delay_time));
       graphic_control_extension_->trans_index = gif_gce->trans_index;
       break;
     }
@@ -485,13 +484,13 @@
 
   auto gif_image = pdfium::MakeUnique<CFX_GifImage>();
   gif_image->image_info.left =
-      GetWord_LSBFirst(reinterpret_cast<uint8_t*>(&img_info->left));
+      FXWORD_GET_LSBFIRST(reinterpret_cast<uint8_t*>(&img_info->left));
   gif_image->image_info.top =
-      GetWord_LSBFirst(reinterpret_cast<uint8_t*>(&img_info->top));
+      FXWORD_GET_LSBFIRST(reinterpret_cast<uint8_t*>(&img_info->top));
   gif_image->image_info.width =
-      GetWord_LSBFirst(reinterpret_cast<uint8_t*>(&img_info->width));
+      FXWORD_GET_LSBFIRST(reinterpret_cast<uint8_t*>(&img_info->width));
   gif_image->image_info.height =
-      GetWord_LSBFirst(reinterpret_cast<uint8_t*>(&img_info->height));
+      FXWORD_GET_LSBFIRST(reinterpret_cast<uint8_t*>(&img_info->height));
   gif_image->image_info.local_flags = img_info->local_flags;
   if (gif_image->image_info.left + gif_image->image_info.width > width_ ||
       gif_image->image_info.top + gif_image->image_info.height > height_)
diff --git a/core/fxcodec/lbmp/fx_bmp.cpp b/core/fxcodec/lbmp/fx_bmp.cpp
index e705555..73be66e 100644
--- a/core/fxcodec/lbmp/fx_bmp.cpp
+++ b/core/fxcodec/lbmp/fx_bmp.cpp
@@ -28,10 +28,6 @@
 
 }  // namespace
 
-uint16_t GetWord_LSBFirst(uint8_t* p) {
-  return p[0] | (p[1] << 8);
-}
-
 BMPDecompressor::BMPDecompressor()
     : context_ptr(nullptr),
       next_in(nullptr),
@@ -88,7 +84,7 @@
     }
 
     pBmp_header->bfType =
-        GetWord_LSBFirst(reinterpret_cast<uint8_t*>(&pBmp_header->bfType));
+        FXWORD_GET_LSBFIRST(reinterpret_cast<uint8_t*>(&pBmp_header->bfType));
     pBmp_header->bfOffBits = FXDWORD_GET_LSBFIRST(
         reinterpret_cast<uint8_t*>(&pBmp_header->bfOffBits));
     data_size =
@@ -117,11 +113,11 @@
           skip_size = skip_size_org;
           return 2;
         }
-        width = GetWord_LSBFirst(
+        width = FXWORD_GET_LSBFIRST(
             reinterpret_cast<uint8_t*>(&pBmp_core_header->bcWidth));
-        height = GetWord_LSBFirst(
+        height = FXWORD_GET_LSBFIRST(
             reinterpret_cast<uint8_t*>(&pBmp_core_header->bcHeight));
-        bitCounts = GetWord_LSBFirst(
+        bitCounts = FXWORD_GET_LSBFIRST(
             reinterpret_cast<uint8_t*>(&pBmp_core_header->bcBitCount));
         compress_flag = BMP_RGB;
         imgTB_flag = false;
@@ -137,7 +133,7 @@
             reinterpret_cast<uint8_t*>(&pBmp_info_header->biWidth));
         int32_t signed_height = FXDWORD_GET_LSBFIRST(
             reinterpret_cast<uint8_t*>(&pBmp_info_header->biHeight));
-        bitCounts = GetWord_LSBFirst(
+        bitCounts = FXWORD_GET_LSBFIRST(
             reinterpret_cast<uint8_t*>(&pBmp_info_header->biBitCount));
         compress_flag = FXDWORD_GET_LSBFIRST(
             reinterpret_cast<uint8_t*>(&pBmp_info_header->biCompression));
@@ -163,13 +159,13 @@
               reinterpret_cast<uint8_t*>(&pBmp_info_header->biWidth));
           int32_t signed_height = FXDWORD_GET_LSBFIRST(
               reinterpret_cast<uint8_t*>(&pBmp_info_header->biHeight));
-          bitCounts = GetWord_LSBFirst(
+          bitCounts = FXWORD_GET_LSBFIRST(
               reinterpret_cast<uint8_t*>(&pBmp_info_header->biBitCount));
           compress_flag = FXDWORD_GET_LSBFIRST(
               reinterpret_cast<uint8_t*>(&pBmp_info_header->biCompression));
           color_used = FXDWORD_GET_LSBFIRST(
               reinterpret_cast<uint8_t*>(&pBmp_info_header->biClrUsed));
-          biPlanes = GetWord_LSBFirst(
+          biPlanes = FXWORD_GET_LSBFIRST(
               reinterpret_cast<uint8_t*>(&pBmp_info_header->biPlanes));
           dpi_x = FXDWORD_GET_LSBFIRST(
               reinterpret_cast<uint8_t*>(&pBmp_info_header->biXPelsPerMeter));
@@ -385,7 +381,7 @@
         green_bits -= 8;
         red_bits -= 8;
         for (uint32_t col = 0; col < width; ++col) {
-          *buf = GetWord_LSBFirst(reinterpret_cast<uint8_t*>(buf));
+          *buf = FXWORD_GET_LSBFIRST(reinterpret_cast<uint8_t*>(buf));
           out_row_buffer[idx++] =
               static_cast<uint8_t>((*buf & mask_blue) << blue_bits);
           out_row_buffer[idx++] =
diff --git a/core/fxcodec/lbmp/fx_bmp.h b/core/fxcodec/lbmp/fx_bmp.h
index 1dfbcc1..9dfa849 100644
--- a/core/fxcodec/lbmp/fx_bmp.h
+++ b/core/fxcodec/lbmp/fx_bmp.h
@@ -133,6 +133,4 @@
   UnownedPtr<CCodec_BmpModule::Delegate> const m_pDelegate;
 };
 
-uint16_t GetWord_LSBFirst(uint8_t* p);
-
 #endif  // CORE_FXCODEC_LBMP_FX_BMP_H_
diff --git a/core/fxcrt/fx_system.h b/core/fxcrt/fx_system.h
index 8e2957b..eb07ebd 100644
--- a/core/fxcrt/fx_system.h
+++ b/core/fxcrt/fx_system.h
@@ -199,6 +199,12 @@
 #define FXSYS_wcsftime wcsftime
 #endif  // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
 
+#define FXWORD_GET_LSBFIRST(p)                                \
+  (static_cast<uint16_t>((static_cast<uint16_t>(p[1]) << 8) | \
+                         (static_cast<uint16_t>(p[0]))))
+#define FXWORD_GET_MSBFIRST(p)                                \
+  (static_cast<uint16_t>((static_cast<uint16_t>(p[0]) << 8) | \
+                         (static_cast<uint16_t>(p[1]))))
 #define FXDWORD_GET_LSBFIRST(p)                                                \
   ((static_cast<uint32_t>(p[3]) << 24) | (static_cast<uint32_t>(p[2]) << 16) | \
    (static_cast<uint32_t>(p[1]) << 8) | (static_cast<uint32_t>(p[0])))