Skip to content

Commit cbbdf9b

Browse files
pavel-alayiText-CI
authored andcommitted
Wrap negative offsets in datamatrix
Add tests for IndexOutOfBounds Update width&height samples in javadocs. DEVSIX-1845 Autoported commit. Original commit hash: [963e9369a]
1 parent d4eca98 commit cbbdf9b

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

itext.tests/itext.barcodes.tests/itext/barcodes/BarcodeDataMatrixTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,32 @@ public virtual void Barcode07Test() {
186186
int result = bc.SetCode(aCode);
187187
NUnit.Framework.Assert.AreEqual(result, BarcodeDataMatrix.DM_ERROR_TEXT_TOO_BIG);
188188
}
189+
190+
[NUnit.Framework.Test]
191+
public virtual void Barcode08Test() {
192+
BarcodeDataMatrix barcodeDataMatrix = new BarcodeDataMatrix();
193+
barcodeDataMatrix.SetWidth(18);
194+
barcodeDataMatrix.SetHeight(18);
195+
int result = barcodeDataMatrix.SetCode("AbcdFFghijklmnopqrstuWXSQ");
196+
NUnit.Framework.Assert.AreEqual(BarcodeDataMatrix.DM_ERROR_TEXT_TOO_BIG, result);
197+
}
198+
199+
[NUnit.Framework.Test]
200+
public virtual void Barcode09Test() {
201+
BarcodeDataMatrix barcodeDataMatrix = new BarcodeDataMatrix();
202+
barcodeDataMatrix.SetWidth(17);
203+
barcodeDataMatrix.SetHeight(17);
204+
int result = barcodeDataMatrix.SetCode("AbcdFFghijklmnopqrstuWXSQ");
205+
NUnit.Framework.Assert.AreEqual(BarcodeDataMatrix.DM_ERROR_INVALID_SQUARE, result);
206+
}
207+
208+
[NUnit.Framework.Test]
209+
public virtual void Barcode10Test() {
210+
BarcodeDataMatrix barcodeDataMatrix = new BarcodeDataMatrix();
211+
barcodeDataMatrix.SetWidth(26);
212+
barcodeDataMatrix.SetHeight(12);
213+
int result = barcodeDataMatrix.SetCode("AbcdFFghijklmnopqrstuWXSQ");
214+
NUnit.Framework.Assert.AreEqual(BarcodeDataMatrix.DM_ERROR_TEXT_TOO_BIG, result);
215+
}
189216
}
190217
}

itext/itext.barcodes/itext/barcodes/BarcodeDataMatrix.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -333,23 +333,24 @@ public virtual int GetHeight() {
333333

334334
/// <summary>Sets the height of the barcode.</summary>
335335
/// <remarks>
336-
/// Sets the height of the barcode. If the height is zero it will be calculated. This height doesn't include the whitespace border, if any.
337-
/// The allowed dimensions are (height, width):<p>
336+
/// Sets the height of the barcode. If the height is zero it will be calculated.
337+
/// This height doesn't include the whitespace border, if any.
338+
/// The allowed dimensions are (width, height):<p>
338339
/// 10, 10<br />
339340
/// 12, 12<br />
340-
/// 8, 18<br />
341+
/// 18, 8<br />
341342
/// 14, 14<br />
342-
/// 8, 32<br />
343+
/// 32, 8<br />
343344
/// 16, 16<br />
344-
/// 12, 26<br />
345+
/// 26, 12<br />
345346
/// 18, 18<br />
346347
/// 20, 20<br />
347-
/// 12, 36<br />
348+
/// 36, 12<br />
348349
/// 22, 22<br />
349-
/// 16, 36<br />
350+
/// 36, 16<br />
350351
/// 24, 24<br />
351352
/// 26, 26<br />
352-
/// 16, 48<br />
353+
/// 48, 16<br />
353354
/// 32, 32<br />
354355
/// 36, 36<br />
355356
/// 40, 40<br />
@@ -383,23 +384,24 @@ public virtual int GetWidth() {
383384

384385
/// <summary>Sets the width of the barcode.</summary>
385386
/// <remarks>
386-
/// Sets the width of the barcode. If the width is zero it will be calculated. This width doesn't include the whitespace border, if any.
387-
/// The allowed dimensions are (height, width):<p>
387+
/// Sets the width of the barcode. If the width is zero it will be calculated.
388+
/// This width doesn't include the whitespace border, if any.
389+
/// The allowed dimensions are (width, height):<p>
388390
/// 10, 10<br />
389391
/// 12, 12<br />
390-
/// 8, 18<br />
392+
/// 18, 8<br />
391393
/// 14, 14<br />
392-
/// 8, 32<br />
394+
/// 32, 8<br />
393395
/// 16, 16<br />
394-
/// 12, 26<br />
396+
/// 26, 12<br />
395397
/// 18, 18<br />
396398
/// 20, 20<br />
397-
/// 12, 36<br />
399+
/// 36, 12<br />
398400
/// 22, 22<br />
399-
/// 16, 36<br />
401+
/// 36, 16<br />
400402
/// 24, 24<br />
401403
/// 26, 26<br />
402-
/// 16, 48<br />
404+
/// 48, 16<br />
403405
/// 32, 32<br />
404406
/// 36, 36<br />
405407
/// 40, 40<br />
@@ -1117,7 +1119,7 @@ private int C40OrTextEncodation(byte[] text, int textOffset, int textLength, byt
11171119
dataOffsetNew = requiredCapacityForASCII;
11181120
}
11191121
}
1120-
addLatch = unlatch < 0 ? true : (dataOffset - requiredCapacityForASCII != unlatch);
1122+
addLatch = (unlatch < 0) || ((dataOffset - requiredCapacityForASCII) != unlatch);
11211123
if (requiredCapacityForC40orText % 3 == 0 && requiredCapacityForC40orText / 3 * 2 + (addLatch ? 2 : 0) < requiredCapacityForASCII
11221124
) {
11231125
usingASCII = false;
@@ -1138,17 +1140,15 @@ private int C40OrTextEncodation(byte[] text, int textOffset, int textLength, byt
11381140
usingASCII = true;
11391141
}
11401142
}
1143+
if (dataOffset < 0) {
1144+
return -1;
1145+
}
11411146
if (usingASCII) {
11421147
return AsciiEncodation(text, textOffset, 1, data, dataOffset, dataLength, prevEnc == mode ? 1 : -1, DM_ASCII
11431148
, origDataOffset);
11441149
}
11451150
if (addLatch) {
1146-
if (c40) {
1147-
data[dataOffset + ptrOut++] = LATCH_C40;
1148-
}
1149-
else {
1150-
data[dataOffset + ptrOut++] = LATCH_TEXT;
1151-
}
1151+
data[dataOffset + ptrOut++] = c40 ? LATCH_C40 : LATCH_TEXT;
11521152
}
11531153
int[] enc = new int[textLength * 4 + 10];
11541154
encPtr = 0;

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
81ee78e4b14d6a1bd6fcc7095e13252b80001b0c
1+
963e9369a70d97773f9dfa873b56d13e1dd5e411

0 commit comments

Comments
 (0)