Throw exception on integer overflow in dng_ifd.cpp. am: e22575cfb6 am: 70494945fe am: 0d2aa49eab am: 378d7855d4
am: 24a9ab971a

Change-Id: I2d212c5b888b6f2e1cc4c5d2607f1dc039583ba2
diff --git a/Android.bp b/Android.bp
new file mode 100644
index 0000000..65c7c51
--- /dev/null
+++ b/Android.bp
@@ -0,0 +1,149 @@
+cc_defaults {
+    name: "libdng_sdk-defaults",
+    srcs: [
+        "source/dng_1d_function.cpp",
+        "source/dng_1d_table.cpp",
+        "source/dng_abort_sniffer.cpp",
+        "source/dng_area_task.cpp",
+        "source/dng_bad_pixels.cpp",
+        "source/dng_bottlenecks.cpp",
+        "source/dng_camera_profile.cpp",
+        "source/dng_color_space.cpp",
+        "source/dng_color_spec.cpp",
+        "source/dng_date_time.cpp",
+        "source/dng_exceptions.cpp",
+        "source/dng_exif.cpp",
+        "source/dng_file_stream.cpp",
+        "source/dng_filter_task.cpp",
+        "source/dng_fingerprint.cpp",
+        "source/dng_gain_map.cpp",
+        "source/dng_globals.cpp",
+        "source/dng_host.cpp",
+        "source/dng_hue_sat_map.cpp",
+        "source/dng_ifd.cpp",
+        "source/dng_image.cpp",
+        "source/dng_image_writer.cpp",
+        "source/dng_info.cpp",
+        "source/dng_iptc.cpp",
+        "source/dng_jpeg_image.cpp",
+        "source/dng_jpeg_memory_source.cpp",
+        "source/dng_lens_correction.cpp",
+        "source/dng_linearization_info.cpp",
+        "source/dng_lossless_jpeg.cpp",
+        "source/dng_matrix.cpp",
+        "source/dng_memory.cpp",
+        "source/dng_memory_stream.cpp",
+        "source/dng_misc_opcodes.cpp",
+        "source/dng_mosaic_info.cpp",
+        "source/dng_mutex.cpp",
+        "source/dng_negative.cpp",
+        "source/dng_opcode_list.cpp",
+        "source/dng_opcodes.cpp",
+        "source/dng_orientation.cpp",
+        "source/dng_parse_utils.cpp",
+        "source/dng_pixel_buffer.cpp",
+        "source/dng_point.cpp",
+        "source/dng_preview.cpp",
+        "source/dng_pthread.cpp",
+        "source/dng_rational.cpp",
+        "source/dng_read_image.cpp",
+        "source/dng_rect.cpp",
+        "source/dng_ref_counted_block.cpp",
+        "source/dng_reference.cpp",
+        "source/dng_render.cpp",
+        "source/dng_resample.cpp",
+        "source/dng_safe_arithmetic.cpp",
+        "source/dng_shared.cpp",
+        "source/dng_simple_image.cpp",
+        "source/dng_spline.cpp",
+        "source/dng_stream.cpp",
+        "source/dng_string.cpp",
+        "source/dng_string_list.cpp",
+        "source/dng_tag_types.cpp",
+        "source/dng_temperature.cpp",
+        "source/dng_tile_iterator.cpp",
+        "source/dng_tone_curve.cpp",
+        "source/dng_utils.cpp",
+        "source/dng_xy_coord.cpp",
+        "source/dng_xmp.cpp",
+    ],
+    cflags: [
+        "-DUNIX_ENV=1",
+        "-DqDNGBigEndian=0",
+        "-DqDNGThreadSafe=1",
+        "-DqDNGUseLibJPEG=1",
+        "-DqDNGUseXMP=0",
+        "-DqDNGValidateTarget=1",
+        "-DqAndroid=1",
+        "-Wsign-compare",
+        "-Wno-reorder",
+        "-Wframe-larger-than=20000",
+
+        // Ignore unused parameters.
+        "-Wno-unused-parameter",
+
+        // Some integral return types are annotated with "const."
+        "-Wno-ignored-qualifiers",
+    ],
+
+    rtti: true,
+    cppflags: ["-fexceptions"],
+
+    clang: true,
+    sanitize: {
+        misc_undefined: [
+            "unsigned-integer-overflow",
+            "signed-integer-overflow",
+        ],
+    },
+}
+
+cc_library{
+    name: "libdng_sdk",
+    defaults: ["libdng_sdk-defaults"],
+
+    cflags: ["-DqDNGValidate=0"],
+
+    shared_libs: [
+        "libz",
+        "libjpeg",
+    ],
+
+    export_include_dirs: ["source"],
+}
+
+// dng sdk validating version static library
+// This version will print out validation warnings/errors to stderr
+// and is built against the NDK for use with CTS
+// ========================================================
+
+cc_library_static {
+    name: "libdng_sdk_validate",
+    defaults: ["libdng_sdk-defaults"],
+
+    cflags: ["-DqDNGValidate=1"],
+
+    static_libs: ["libjpeg_static_ndk"],
+    shared_libs: ["libz"],
+
+    export_include_dirs: ["source"],
+
+    sdk_version: "24",
+    stl: "c++_static",
+}
+
+// dng sdk unittests for target
+// ========================================================
+
+cc_binary {
+    name: "dng_validate",
+    defaults: ["libdng_sdk-defaults"],
+    srcs: ["source/dng_validate.cpp"],
+
+    cflags: ["-DqDNGValidate=1"],
+
+    shared_libs: [
+        "libz",
+        "libjpeg",
+    ],
+}
diff --git a/Android.mk b/Android.mk
deleted file mode 100644
index 5cb684e..0000000
--- a/Android.mk
+++ /dev/null
@@ -1,155 +0,0 @@
-LOCAL_PATH := $(call my-dir)
-
-# dng sdk shared library for target
-# ========================================================
-
-include $(CLEAR_VARS)
-
-dng_sdk_files := \
-    source/dng_1d_function.cpp \
-    source/dng_1d_table.cpp \
-    source/dng_abort_sniffer.cpp \
-    source/dng_area_task.cpp \
-    source/dng_bad_pixels.cpp \
-    source/dng_bottlenecks.cpp \
-    source/dng_camera_profile.cpp \
-    source/dng_color_space.cpp \
-    source/dng_color_spec.cpp \
-    source/dng_date_time.cpp \
-    source/dng_exceptions.cpp \
-    source/dng_exif.cpp \
-    source/dng_file_stream.cpp \
-    source/dng_filter_task.cpp \
-    source/dng_fingerprint.cpp \
-    source/dng_gain_map.cpp \
-    source/dng_globals.cpp \
-    source/dng_host.cpp \
-    source/dng_hue_sat_map.cpp \
-    source/dng_ifd.cpp \
-    source/dng_image.cpp \
-    source/dng_image_writer.cpp \
-    source/dng_info.cpp \
-    source/dng_iptc.cpp \
-    source/dng_jpeg_image.cpp \
-    source/dng_jpeg_memory_source.cpp \
-    source/dng_lens_correction.cpp \
-    source/dng_linearization_info.cpp \
-    source/dng_lossless_jpeg.cpp \
-    source/dng_matrix.cpp \
-    source/dng_memory.cpp \
-    source/dng_memory_stream.cpp \
-    source/dng_misc_opcodes.cpp \
-    source/dng_mosaic_info.cpp \
-    source/dng_mutex.cpp \
-    source/dng_negative.cpp \
-    source/dng_opcode_list.cpp \
-    source/dng_opcodes.cpp \
-    source/dng_orientation.cpp \
-    source/dng_parse_utils.cpp \
-    source/dng_pixel_buffer.cpp \
-    source/dng_point.cpp \
-    source/dng_preview.cpp \
-    source/dng_pthread.cpp \
-    source/dng_rational.cpp \
-    source/dng_read_image.cpp \
-    source/dng_rect.cpp \
-    source/dng_ref_counted_block.cpp \
-    source/dng_reference.cpp \
-    source/dng_render.cpp \
-    source/dng_resample.cpp \
-    source/dng_safe_arithmetic.cpp \
-    source/dng_shared.cpp \
-    source/dng_simple_image.cpp \
-    source/dng_spline.cpp \
-    source/dng_stream.cpp \
-    source/dng_string.cpp \
-    source/dng_string_list.cpp \
-    source/dng_tag_types.cpp \
-    source/dng_temperature.cpp \
-    source/dng_tile_iterator.cpp \
-    source/dng_tone_curve.cpp \
-    source/dng_utils.cpp \
-    source/dng_xy_coord.cpp \
-    source/dng_xmp.cpp
-
-LOCAL_MODULE := libdng_sdk
-LOCAL_SRC_FILES := $(dng_sdk_files)
-
-LOCAL_CFLAGS := \
-    -DUNIX_ENV=1 -DqDNGBigEndian=0 -DqDNGThreadSafe=1 \
-    -DqDNGUseLibJPEG=1 -DqDNGUseXMP=0 -DqDNGValidate=0 \
-    -DqDNGValidateTarget=1 -DqAndroid=1 \
-    -Wsign-compare -Wno-reorder -Wframe-larger-than=20000
-
-LOCAL_CPPFLAGS := -frtti -fexceptions
-
-# Ignore unused parameters.
-LOCAL_CFLAGS += -Wno-unused-parameter
-# Some integral return types are annotated with "const."
-LOCAL_CFLAGS += -Wno-ignored-qualifiers
-
-LOCAL_CLANG := true
-LOCAL_SANITIZE := unsigned-integer-overflow signed-integer-overflow
-
-LOCAL_SHARED_LIBRARIES := libz libjpeg
-
-LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/source
-
-include $(BUILD_SHARED_LIBRARY)
-
-# dng sdk validating version static library
-# This version will print out validation warnings/errors to stderr
-# and is built against the NDK for use with CTS
-# ========================================================
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libdng_sdk_validate
-LOCAL_SRC_FILES := $(dng_sdk_files)
-
-LOCAL_CFLAGS := \
-    -DUNIX_ENV=1 -DqDNGBigEndian=0 -DqDNGThreadSafe=1 \
-    -DqDNGUseLibJPEG=1 -DqDNGUseXMP=0 -DqDNGValidate=1 \
-    -DqDNGValidateTarget=1 -DqAndroid=1 \
-    -Wsign-compare -Wno-reorder -Wframe-larger-than=20000
-
-LOCAL_CPPFLAGS := -frtti -fexceptions
-
-# Ignore unused parameters.
-LOCAL_CFLAGS += -Wno-unused-parameter
-# Some integral return types are annotated with "const."
-LOCAL_CFLAGS += -Wno-ignored-qualifiers
-
-LOCAL_CLANG := true
-LOCAL_SANITIZE := unsigned-integer-overflow signed-integer-overflow
-
-LOCAL_STATIC_LIBRARIES := libz libjpeg_static
-
-LOCAL_CPP_FEATURES := rtti exceptions
-LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/source
-
-# NDK build, shared C++ runtime
-# LOCAL_SDK_VERSION := current
-# LOCAL_NDK_STL_VARIANT := c++_shared
-
-# Temporary workaround until camera2 NDK is active. See b/27102995.
-LOCAL_CXX_STL := libc++_static
-
-include $(BUILD_STATIC_LIBRARY)
-
-# dng sdk unittests for target
-# ========================================================
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := dng_validate
-LOCAL_MODULE_TAGS := optional
-LOCAL_SRC_FILES := \
-    $(dng_sdk_files) \
-    source/dng_validate.cpp
-
-LOCAL_CFLAGS := -DUNIX_ENV=1 -DqDNGBigEndian=0 -DqDNGThreadSafe=1 -DqDNGUseLibJPEG=1 -DqDNGUseXMP=0 -DqDNGValidate=1 -DqDNGValidateTarget=1 -DqAndroid=1 -fexceptions -Wsign-compare -Wno-reorder -Wframe-larger-than=20000 -frtti
-
-LOCAL_SHARED_LIBRARIES := libz libjpeg
-
-include $(BUILD_EXECUTABLE)
diff --git a/PATENTS b/PATENTS
new file mode 100644
index 0000000..5a1eaf0
--- /dev/null
+++ b/PATENTS
@@ -0,0 +1,73 @@
+Digital Negative (DNG) Specification patent license
+
+Adobe is the publisher of the Digital Negative (DNG) Specification
+describing an image file format for storing camera raw information
+used in a wide range of hardware and software. Adobe provides the DNG
+Specification to the public for the purpose of encouraging
+implementation of this file format in a compliant manner. This
+document is a patent license granted by Adobe to individuals and
+organizations that desire to develop, market, and/or distribute
+hardware and software that reads and/or writes image files compliant
+with the DNG Specification.
+
+Grant of rights
+
+Subject to the terms below and solely to permit the reading and
+writing of image files that comply with the DNG Specification, Adobe
+hereby grants all individuals and organizations the worldwide,
+royalty-free, nontransferable, nonexclusive right under all Essential
+Claims to make, have made, use, sell, import, and distribute Compliant
+Implementations.
+
+“Compliant Implementation” means a portion of a software or hardware
+product that reads or writes computer files compliant with the DNG
+Specification.
+
+“DNG Specification” means any version of the Adobe DNG Specification
+made publicly available by Adobe (for example, version 1.0.0.0 dated
+September 2004).
+
+“Essential Claim” means a claim of a patent, whenever and wherever
+issued, that Adobe has the right to license without payment of royalty
+or other fee that is unavoidably infringed by implementation of the
+DNG Specification. A claim is unavoidably infringed by the DNG
+Specification only when it is not possible to avoid infringing when
+conforming with such specification because there is no technically
+possible noninfringing alternative for achieving such conformity.
+Essential Claim does not include a claim that is infringed by
+implementation of (a) enabling technology that may be necessary to
+make or use any product or portion thereof that complies with the DNG
+Specification but is not itself expressly set forth in the DNG
+Specification (for example, compiler technology and basic operating
+system technology), (b) technology developed elsewhere and merely
+incorporated by reference in the DNG Specification, or (c) the
+implementation of file formats other than DNG.
+
+Revocation
+
+Adobe may revoke the rights granted above to any individual or
+organizational licensee in the event that such licensee or its
+affiliates brings any patent action against Adobe or its affiliates
+related to the reading or writing of files that comply with the DNG
+Specification.
+
+Any Compliant Implementation distributed under this license must
+include the following notice displayed in a prominent manner within
+its source code and documentation: "This product includes DNG
+technology under license by Adobe Systems Incorporated.”
+
+No warranty
+
+The rights granted herein are provided on an as-is basis without
+warranty of any kind, including warranty of title or noninfringement.
+Nothing in this license shall be construed as (a) requiring the
+maintenance of any patent, (b) a warranty or representation as to the
+validity or scope of any patent, (c) a warranty or representation that
+any product or service will be free from infringement of any patent,
+(d) an agreement to bring or prosecute actions against any infringers
+of any patent, or (e) conferring any right or license under any patent
+claim other than Essential Claims.
+
+Reservation of rights
+
+All rights not expressly granted herein are reserved.
diff --git a/README.version b/README.version
index e54deae..6cb2483 100644
--- a/README.version
+++ b/README.version
@@ -2,5 +2,5 @@
 Version: 1.4.0
 License: BSD like License
 License File: LICENSE
-BugComponent: 25788014
-Owners: ebrauer, kinan, mboehme
+BugComponent: 174430
+Owners: adaubert, floriank, yujieqin 
diff --git a/source/dng_image.cpp b/source/dng_image.cpp
index 41b54f0..2026b9a 100644
--- a/source/dng_image.cpp
+++ b/source/dng_image.cpp
@@ -553,8 +553,8 @@
 					 edgeOption,
 					 dng_rect (fBounds.t,
 					 		   fBounds.l,
-					 		   fBounds.t + repeatV,
-					 		   fBounds.l + repeatH),
+					 		   fBounds.t + (int32)repeatV,
+					 		   fBounds.l + (int32)repeatH),
 					 areaTL);
 			
 			}
@@ -570,7 +570,7 @@
 					 edgeOption,
 					 dng_rect (fBounds.t,
 					 		   areaTM.l,
-					 		   fBounds.t + repeatV,
+					 		   fBounds.t + (int32)repeatV,
 					 		   areaTM.r),
 					 areaTM);
 			
@@ -586,8 +586,8 @@
 			GetEdge (buffer,
 					 edgeOption,
 					 dng_rect (fBounds.t,
-					 		   fBounds.r - repeatH,
-					 		   fBounds.t + repeatV,
+					 		   fBounds.r - (int32)repeatH,
+					 		   fBounds.t + (int32)repeatV,
 					 		   fBounds.r),
 					 areaTR);
 			
@@ -605,7 +605,7 @@
 					 dng_rect (areaLM.t,
 					 		   fBounds.l,
 					 		   areaLM.b,
-					 		   fBounds.l + repeatH),
+					 		   fBounds.l + (int32)repeatH),
 					 areaLM);
 			
 			}
@@ -620,7 +620,7 @@
 			GetEdge (buffer,
 					 edgeOption,
 					 dng_rect (areaRM.t,
-					 		   fBounds.r - repeatH,
+					 		   fBounds.r - (int32)repeatH,
 					 		   areaRM.b,
 					 		   fBounds.r),
 					 areaRM);
@@ -636,10 +636,10 @@
 			
 			GetEdge (buffer,
 					 edgeOption,
-					 dng_rect (fBounds.b - repeatV,
+					 dng_rect (fBounds.b - (int32)repeatV,
 					 		   fBounds.l,
 					 		   fBounds.b,
-					 		   fBounds.l + repeatH),
+					 		   fBounds.l + (int32)repeatH),
 					 areaBL);
 			
 			}
@@ -653,7 +653,7 @@
 			
 			GetEdge (buffer,
 					 edgeOption,
-					 dng_rect (fBounds.b - repeatV,
+					 dng_rect (fBounds.b - (int32)repeatV,
 					 		   areaBM.l,
 					 		   fBounds.b,
 					 		   areaBM.r),
@@ -670,8 +670,8 @@
 			
 			GetEdge (buffer,
 					 edgeOption,
-					 dng_rect (fBounds.b - repeatV,
-					 		   fBounds.r - repeatH,
+					 dng_rect (fBounds.b - (int32)repeatV,
+					 		   fBounds.r - (int32)repeatH,
 					 		   fBounds.b,
 					 		   fBounds.r),
 					 areaBR);
diff --git a/source/dng_pthread.h b/source/dng_pthread.h
index 0a28a1b..4572e31 100644
--- a/source/dng_pthread.h
+++ b/source/dng_pthread.h
@@ -53,6 +53,19 @@
 
 #endif
 
+// Use the standard "timespec" struct as "dng_timespec" for VS2015 and above
+// from <time.h>. Define "dng_timespec" as "timespec" otherwise.
+#if _MSC_VER >= 1900
+#include <time.h>
+#define dng_timespec timespec
+#else
+struct dng_timespec {
+	long tv_sec;
+	long tv_nsec;
+};
+#define timespec dng_timespec
+#endif
+
 #ifdef __cplusplus
 extern "C"
 {
@@ -62,12 +75,6 @@
 
 #define DNG_ETIMEDOUT       60              /* Operation timed out */
 
-struct dng_timespec {
-	long tv_sec;
-	long tv_nsec;
-};
-
-
 typedef unsigned long dng_pthread_t;
 
 typedef struct dng_pthread_mutex_impl *dng_pthread_mutex_t;
@@ -179,8 +186,6 @@
 #undef PTHREAD_ONCE_INIT
 #define PTHREAD_ONCE_INIT DNG_PTHREAD_ONCE_INIT
 
-#define timespec dng_timespec
-
 /* If it is defined on Windows, it probably has the wrong value... */
 #if defined(WIN32) || !defined(ETIMEDOUT)
 #undef ETIMEDOUT