Unify checksum calculation of EAN-8 and EAN-13.

The algorithm is the same.

Change-Id: Ia5713f6b1602aafac546047b8d398048d6532686
Reviewed-on: https://pdfium-review.googlesource.com/13290
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 03285bf..deaa706 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1422,6 +1422,8 @@
       "fxbarcode/oned/BC_OnedEAN13Writer.h",
       "fxbarcode/oned/BC_OnedEAN8Writer.cpp",
       "fxbarcode/oned/BC_OnedEAN8Writer.h",
+      "fxbarcode/oned/BC_OnedEANChecksum.cpp",
+      "fxbarcode/oned/BC_OnedEANChecksum.h",
       "fxbarcode/oned/BC_OnedUPCAWriter.cpp",
       "fxbarcode/oned/BC_OnedUPCAWriter.h",
       "fxbarcode/pdf417/BC_PDF417.cpp",
diff --git a/fxbarcode/oned/BC_OnedEAN13Writer.cpp b/fxbarcode/oned/BC_OnedEAN13Writer.cpp
index e7642de..2d9759b 100644
--- a/fxbarcode/oned/BC_OnedEAN13Writer.cpp
+++ b/fxbarcode/oned/BC_OnedEAN13Writer.cpp
@@ -31,6 +31,7 @@
 #include "core/fxge/cfx_defaultrenderdevice.h"
 #include "fxbarcode/BC_Writer.h"
 #include "fxbarcode/oned/BC_OneDimWriter.h"
+#include "fxbarcode/oned/BC_OnedEANChecksum.h"
 
 namespace {
 
@@ -76,19 +77,9 @@
   }
   return filtercontents;
 }
+
 int32_t CBC_OnedEAN13Writer::CalcChecksum(const CFX_ByteString& contents) {
-  int32_t odd = 0;
-  int32_t even = 0;
-  FX_STRSIZE parity = 1;
-  for (FX_STRSIZE i = contents.GetLength(); i > 0; i--) {
-    if (parity % 2) {
-      odd += FXSYS_DecimalCharToInt(contents[i - 1]);
-    } else {
-      even += FXSYS_DecimalCharToInt(contents[i - 1]);
-    }
-    parity++;
-  }
-  return (10 - (odd * 3 + even) % 10) % 10;
+  return EANCalcChecksum(contents);
 }
 
 uint8_t* CBC_OnedEAN13Writer::EncodeWithHint(const CFX_ByteString& contents,
diff --git a/fxbarcode/oned/BC_OnedEAN8Writer.cpp b/fxbarcode/oned/BC_OnedEAN8Writer.cpp
index c3becc1a..d4d438f 100644
--- a/fxbarcode/oned/BC_OnedEAN8Writer.cpp
+++ b/fxbarcode/oned/BC_OnedEAN8Writer.cpp
@@ -32,6 +32,7 @@
 #include "fxbarcode/BC_Writer.h"
 #include "fxbarcode/common/BC_CommonBitMatrix.h"
 #include "fxbarcode/oned/BC_OneDimWriter.h"
+#include "fxbarcode/oned/BC_OnedEANChecksum.h"
 
 namespace {
 
@@ -84,18 +85,7 @@
 }
 
 int32_t CBC_OnedEAN8Writer::CalcChecksum(const CFX_ByteString& contents) {
-  int32_t odd = 0;
-  int32_t even = 0;
-  FX_STRSIZE parity = 1;
-  for (FX_STRSIZE i = contents.GetLength(); i > 0; i--) {
-    if (i % 2) {
-      odd += FXSYS_DecimalCharToInt(contents[i - 1]);
-    } else {
-      even += FXSYS_DecimalCharToInt(contents[i - 1]);
-    }
-    parity++;
-  }
-  return (10 - (odd * 3 + even) % 10) % 10;
+  return EANCalcChecksum(contents);
 }
 
 uint8_t* CBC_OnedEAN8Writer::EncodeWithHint(const CFX_ByteString& contents,
diff --git a/fxbarcode/oned/BC_OnedEANChecksum.cpp b/fxbarcode/oned/BC_OnedEANChecksum.cpp
new file mode 100644
index 0000000..744a608
--- /dev/null
+++ b/fxbarcode/oned/BC_OnedEANChecksum.cpp
@@ -0,0 +1,21 @@
+// Copyright 2017 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "fxbarcode/oned/BC_OnedEANChecksum.h"
+
+#include "core/fxcrt/fx_extension.h"
+
+int32_t EANCalcChecksum(const CFX_ByteString& contents) {
+  int32_t odd = 0;
+  int32_t even = 0;
+  FX_STRSIZE parity = 1;
+  for (FX_STRSIZE i = contents.GetLength(); i > 0; i--) {
+    if (parity % 2)
+      odd += FXSYS_DecimalCharToInt(contents[i - 1]);
+    else
+      even += FXSYS_DecimalCharToInt(contents[i - 1]);
+    parity++;
+  }
+  return (10 - (odd * 3 + even) % 10) % 10;
+}
diff --git a/fxbarcode/oned/BC_OnedEANChecksum.h b/fxbarcode/oned/BC_OnedEANChecksum.h
new file mode 100644
index 0000000..47d2d6b
--- /dev/null
+++ b/fxbarcode/oned/BC_OnedEANChecksum.h
@@ -0,0 +1,12 @@
+// Copyright 2017 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef FXBARCODE_ONED_BC_ONEDEANCHECKSUM_H_
+#define FXBARCODE_ONED_BC_ONEDEANCHECKSUM_H_
+
+#include "core/fxcrt/fx_string.h"
+
+int32_t EANCalcChecksum(const CFX_ByteString& contents);
+
+#endif  // FXBARCODE_ONED_BC_ONEDEANCHECKSUM_H_