aboutsummaryrefslogtreecommitdiff
path: root/fxbarcode/oned/BC_OnedEANChecksum.cpp
blob: c1fbab495b77a7edb5113349270a605ce3a75d5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Copyright 2017 The PDFium Authors
// 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 ByteString& contents) {
  int32_t odd = 0;
  int32_t even = 0;
  size_t parity = 1;
  for (size_t 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;
}