Skip to content

Commit

Permalink
Ported r2454 from ZXing
Browse files Browse the repository at this point in the history
Original commit message: "Issue 1376 Check EAN13 checksum when encoding"
  • Loading branch information
cwalcott committed Feb 14, 2013
1 parent 0a60a7c commit 518aaa5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Aitor Almeida (University of Deusto)
Alasdair Mackintosh (Google)
Alex Dupre
Alexander Martin (Haase & Martin GmbH)
Alexander Schmidt
Anders Borg
Andreas Pillath
Andrew Walbran (Google)
Expand Down Expand Up @@ -37,6 +38,7 @@ Erik Barbara
Fred Lin (Anobiit)
gcstang
Hannes Erven
hosigumayuugi
hypest (Barcorama project)
Isaac Potoczny-Jones
Jacob Haynes (Google)
Expand All @@ -45,6 +47,7 @@ Joan Montané (Softcatalà.cat)
John Connolly (Bug Labs)
Jonas Petersson (Prisjakt)
Joseph Wain (Google)
Juha Kuitunen
Juho Mikkonen
jwicks
Kamil Kaczmarczyk
Expand All @@ -54,6 +57,7 @@ Kevin Xue (NetDragon Websoft Inc., China)
Lachezar Dobrev
Luiz Silva
Luka Finžgar
Malte Starostik
Manuel Kasten
Marcelo
Mateusz Jędrasik
Expand Down Expand Up @@ -84,6 +88,7 @@ Simon Flannery (Ericsson)
Steven Parkes
Suraj Supekar
Sven Klinkhamer
techinstantdx
Thomas Gerbet
Tim Gernat
v.anestis
Expand Down
8 changes: 7 additions & 1 deletion ZXingObjC/oned/ZXEAN13Writer.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format widt

- (BOOL *)encode:(NSString *)contents length:(int *)pLength {
if ([contents length] != 13) {
[NSException raise:NSInvalidArgumentException format:@"Requested contents should be 13 digits long, but got %d", [contents length]];
[NSException raise:NSInvalidArgumentException
format:@"Requested contents should be 13 digits long, but got %d", [contents length]];
}

if (![ZXUPCEANReader checkStandardUPCEANChecksum:contents]) {
[NSException raise:NSInvalidArgumentException
format:@"Contents do not pass checksum"];
}

int firstDigit = [[contents substringToIndex:1] intValue];
Expand Down
1 change: 1 addition & 0 deletions ZXingObjC/oned/ZXUPCEANReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extern const int L_AND_G_PATTERNS[][4];
+ (NSRange)findStartGuardPattern:(ZXBitArray *)row error:(NSError **)error;
- (ZXBarcodeFormat)barcodeFormat;
- (BOOL)checkChecksum:(NSString *)s error:(NSError **)error;
+ (BOOL)checkStandardUPCEANChecksum:(NSString *)s;
+ (int)decodeDigit:(ZXBitArray *)row counters:(int[])counters countersLen:(int)countersLen rowOffset:(int)rowOffset patternType:(UPC_EAN_PATTERNS)patternType error:(NSError **)error;
- (NSRange)decodeEnd:(ZXBitArray *)row endStart:(int)endStart error:(NSError **)error;
- (int)decodeMiddle:(ZXBitArray *)row startRange:(NSRange)startRange result:(NSMutableString *)result error:(NSError **)error;
Expand Down
6 changes: 2 additions & 4 deletions ZXingObjC/oned/ZXUPCEANReader.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ @interface ZXUPCEANReader ()
@property (nonatomic, retain) ZXUPCEANExtensionSupport *extensionReader;
@property (nonatomic, retain) ZXEANManufacturerOrgSupport *eanManSupport;

- (BOOL)checkStandardUPCEANChecksum:(NSString *)s;

@end

@implementation ZXUPCEANReader
Expand Down Expand Up @@ -214,7 +212,7 @@ - (ZXResult *)decodeRow:(int)rowNumber row:(ZXBitArray *)row startGuardRange:(NS
}

- (BOOL)checkChecksum:(NSString *)s error:(NSError **)error {
if ([self checkStandardUPCEANChecksum:s]) {
if ([[self class] checkStandardUPCEANChecksum:s]) {
return YES;
} else {
if (error) *error = FormatErrorInstance();
Expand All @@ -227,7 +225,7 @@ - (BOOL)checkChecksum:(NSString *)s error:(NSError **)error {
* Computes the UPC/EAN checksum on a string of digits, and reports
* whether the checksum is correct or not.
*/
- (BOOL)checkStandardUPCEANChecksum:(NSString *)s {
+ (BOOL)checkStandardUPCEANChecksum:(NSString *)s {
int length = [s length];
if (length == 0) {
return NO;
Expand Down

0 comments on commit 518aaa5

Please sign in to comment.