From ab3c111433229fc79275c4b413b24bec6f5463ce Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Fri, 1 Sep 2017 16:01:44 -0400 Subject: Fix EAN-13 checksum and add unit tests. Parity in EAN-13 is considered counting digits from right to left, starting at 1. Bug: pdfium:882 Change-Id: I3e586499091b8400daf93657eb9878f29d9e6922 Reviewed-on: https://pdfium-review.googlesource.com/12910 Reviewed-by: Tom Sepez Commit-Queue: Henrique Nakashima --- fxbarcode/oned/BC_OnedEAN13Writer.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'fxbarcode/oned/BC_OnedEAN13Writer.cpp') diff --git a/fxbarcode/oned/BC_OnedEAN13Writer.cpp b/fxbarcode/oned/BC_OnedEAN13Writer.cpp index dddb9cc24d..e7642de56d 100644 --- a/fxbarcode/oned/BC_OnedEAN13Writer.cpp +++ b/fxbarcode/oned/BC_OnedEAN13Writer.cpp @@ -79,17 +79,16 @@ CFX_WideString CBC_OnedEAN13Writer::FilterContents( int32_t CBC_OnedEAN13Writer::CalcChecksum(const CFX_ByteString& contents) { int32_t odd = 0; int32_t even = 0; - FX_STRSIZE j = 1; - for (FX_STRSIZE i = 0; i < contents.GetLength(); i++) { - if (j % 2) { - odd += FXSYS_DecimalCharToInt(contents[i]); + 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]); + even += FXSYS_DecimalCharToInt(contents[i - 1]); } - j++; + parity++; } - int32_t checksum = 10 - (odd * 3 + even) % 10; - return checksum; + return (10 - (odd * 3 + even) % 10) % 10; } uint8_t* CBC_OnedEAN13Writer::EncodeWithHint(const CFX_ByteString& contents, -- cgit v1.2.3