Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#722 Add EBCDIC code pages: cp277 and cp278 #724

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,8 @@ The output looks like this:
| .option("ebcdic_code_page", "common") | Common | (Default) Only characters common across EBCDIC code pages are decoded. |
| .option("ebcdic_code_page", "cp037") | EBCDIC 037 | Australia, Brazil, Canada, New Zealand, Portugal, South Africa, USA. |
| .option("ebcdic_code_page", "cp273") | EBCDIC 273 | Germany, Austria. |
| .option("ebcdic_code_page", "cp277") | EBCDIC 277 | Denmark and Norway. |
| .option("ebcdic_code_page", "cp278") | EBCDIC 278 | Finland and Sweden. |
| .option("ebcdic_code_page", "cp300") | EBCDIC 300 | Double-byte code page with Japanese and Latin characters. |
| .option("ebcdic_code_page", "cp500") | EBCDIC 500 | Belgium, Canada, Switzerland, International. |
| .option("ebcdic_code_page", "cp838") | EBCDIC 838 | Double-byte code page with Thai and Latin characters. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ object CodePage extends Logging {
case "cp037_extended" => new CodePage037Ext
case "cp00300" => new CodePage300 // This is the same as cp300
case "cp273" => new CodePage273
case "cp277" => new CodePage277
case "cp278" => new CodePage278
case "cp300" => new CodePage300
case "cp500" => new CodePage500
case "cp838" => new CodePage838
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2018 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.cobrix.cobol.parser.encoding.codepage

/**
* EBCDIC code page 277 is used to represent characters of Denmark and Norway.
*/
class CodePage277 extends SingleByteCodePage(CodePage277.ebcdicToAsciiMapping) {
override def codePageShortName: String = "cp277"
}

object CodePage277 {
val ebcdicToAsciiMapping: Array[Char] = {
import EbcdicNonPrintable._

/* This is the EBCDIC Code Page 277 to ASCII conversion table
from https://en.wikibooks.org/wiki/Character_Encodings/Code_Tables/EBCDIC/EBCDIC_277 */
val ebcdic2ascii: Array[Char] = {
// Non-printable characters map used: http://www.pacsys.com/asciitab.htm
Array[Char](
c00, c01, c02, c03, spc, c09, spc, del, spc, spc, spc, c0b, c0c, ccr, c0e, c0f, // 0 - 15
c10, c11, c12, c13, spc, nel, c08, spc, c18, c19, spc, spc, c1c, c1d, c1e, c1f, // 16 - 31
spc, spc, spc, spc, spc, clf, c17, c1b, spc, spc, spc, spc, spc, c05, c06, c07, // 32 - 47
spc, spc, c16, spc, spc, spc, spc, c04, spc, spc, spc, spc, c14, c15, spc, c1a, // 48 - 63
' ', rsp, 'â', 'ä', 'à', 'á', 'ã', '}', 'ç', 'ñ', '#', '.', '<', '(', '+', '!', // 64 - 79
'&', 'é', 'ê', 'ë', 'è', 'í', 'î', 'ï', 'ì', 'ß', '¤', 'Å', '*', ')', ';', '^', // 80 - 95
'-', '/', 'Â', 'Ä', 'À', 'Á', 'Ã', '$', 'Ç', 'Ñ', 'ø', ',', '%', '_', '>', '?', // 96 - 111
'¦', 'É', 'Ê', 'Ë', 'È', 'Í', 'Î', 'Ï', 'Ì', '`', ':', 'Æ', 'Ø', qts, '=', qtd, // 112 - 127
'@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', '«', '»', 'ð', 'ý', 'þ', '±', // 128 - 143
'°', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 'ª', 'º', '{', '¸', '[', ']', // 144 - 159
'µ', 'ü', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '¡', '¿', 'Ð', 'Ý', 'Þ', '®', // 160 - 175
'¢', '£', '¥', '·', '©', '§', '¶', '¼', '½', '¾', '¬', '|', '¯', '¨', '´', '×', // 176 - 191
'æ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', shy, 'ô', 'ö', 'ò', 'ó', 'õ', // 192 - 207
'å', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', '¹', 'û', '~', 'ù', 'ú', 'ÿ', // 208 - 223
bsh, '÷', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '²', 'Ô', 'Ö', 'Ò', 'Ó', 'Õ', // 224 - 239
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '³', 'Û', 'Ü', 'Ù', 'Ú', spc) // 240 - 255
}
ebcdic2ascii
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2018 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.cobrix.cobol.parser.encoding.codepage

/**
* EBCDIC code page 278 is used to represent characters of Finland and Sweden.
*/
class CodePage278 extends SingleByteCodePage(CodePage278.ebcdicToAsciiMapping) {
override def codePageShortName: String = "cp278"
}

object CodePage278 {
val ebcdicToAsciiMapping: Array[Char] = {
import EbcdicNonPrintable._

/* This is the EBCDIC Code Page 278 to ASCII conversion table
from https://en.wikibooks.org/wiki/Character_Encodings/Code_Tables/EBCDIC/EBCDIC_278 */
val ebcdic2ascii: Array[Char] = {
// Non-printable characters map used: http://www.pacsys.com/asciitab.htm
Array[Char](
c00, c01, c02, c03, spc, c09, spc, del, spc, spc, spc, c0b, c0c, ccr, c0e, c0f, // 0 - 15
c10, c11, c12, c13, spc, nel, c08, spc, c18, c19, spc, spc, c1c, c1d, c1e, c1f, // 16 - 31
spc, spc, spc, spc, spc, clf, c17, c1b, spc, spc, spc, spc, spc, c05, c06, c07, // 32 - 47
spc, spc, c16, spc, spc, spc, spc, c04, spc, spc, spc, spc, c14, c15, spc, c1a, // 48 - 63
' ', rsp, 'â', '{', 'à', 'á', 'ã', '}', 'ç', 'ñ', '§', '.', '<', '(', '+', '!', // 64 - 79
'&', '`', 'ê', 'ë', 'è', 'í', 'î', 'ï', 'ì', 'ß', '¤', 'Å', '*', ')', ';', '^', // 80 - 95
'-', '/', 'Â', '#', 'À', 'Á', 'Ã', '$', 'Ç', 'Ñ', 'ö', ',', '%', '_', '>', '?', // 96 - 111
'ø', bsh, 'Ê', 'Ë', 'È', 'Í', 'Î', 'Ï', 'Ì', 'é', ':', 'Ä', 'Ö', qts, '=', qtd, // 112 - 127
'Ø', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', '«', '»', 'ð', 'ý', 'þ', '±', // 128 - 143
'°', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 'ª', 'º', 'æ', '¸', 'Æ', ']', // 144 - 159
'µ', 'ü', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '¡', '¿', 'Ð', 'Ý', 'Þ', '®', // 160 - 175
'¢', '£', '¥', '·', '©', '[', '¶', '¼', '½', '¾', '¬', '|', '¯', '¨', '´', '×', // 176 - 191
'ä', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', shy, 'ô', '¦', 'ò', 'ó', 'õ', // 192 - 207
'å', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', '¹', 'û', '~', 'ù', 'ú', 'ÿ', // 208 - 223
'É', '÷', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '²', 'Ô', '@', 'Ò', 'Ó', 'Õ', // 224 - 239
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '³', 'Û', 'Ü', 'Ù', 'Ú', spc) // 240 - 255
}
ebcdic2ascii
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,54 @@ class StringDecodersSpec extends AnyWordSpec {
assert(actual == expected)
}

"decode a CP277 string special characters" in {
val expected = " {Ä!~Ü^[ö¤ß¢§@äåæ¦ü}ÖÆØ$#\\] "
val bytes = Array(0x40, 0x9C, 0x63, 0x4F, 0xDC, 0xFC, 0x5F, 0x9E, 0xCC, 0x5A, 0x59,
0xB0, 0xB5, 0x80, 0x43, 0xD0, 0xC0, 0x70, 0xA1, 0x47, 0xEC, 0x7B, 0x7C, 0x67, 0x4A,
0xE0, 0x9F, 0x40).map(_.toByte)

val actual = decodeEbcdicString(bytes, KeepAll, new CodePage277, improvedNullDetection = false)

assert(actual == expected)
}

"decode a CP277 string example" in {
val expected = "Ægte ørreder svømmer i åen, mens små ællinger leger ved søen."

val bytes = Array(0x7B, 0x87, 0xA3, 0x85, 0x40, 0x6A, 0x99, 0x99, 0x85, 0x84, 0x85, 0x99, 0x40,
0xA2, 0xA5, 0x6A, 0x94, 0x94, 0x85, 0x99, 0x40, 0x89, 0x40, 0xD0, 0x85, 0x95, 0x6B, 0x40, 0x94,
0x85, 0x95, 0xA2, 0x40, 0xA2, 0x94, 0xD0, 0x40, 0xC0, 0x93, 0x93, 0x89, 0x95, 0x87, 0x85, 0x99,
0x40, 0x93, 0x85, 0x87, 0x85, 0x99, 0x40, 0xA5, 0x85, 0x84, 0x40, 0xA2, 0x6A, 0x85, 0x95, 0x4B).map(_.toByte)

val actual = decodeEbcdicString(bytes, KeepAll, new CodePage277, improvedNullDetection = false)

assert(actual == expected)
}

"decode a CP278 string special characters" in {
val expected = " {Ä!~Ü^[ö¤ß¢§@äåæ¦ü}ÖÆØ$#\\] "
val bytes = Array(0x40, 0x43, 0x7B, 0x4F, 0xDC, 0xFC, 0x5F, 0xB5, 0x6A, 0x5A, 0x59,
0xB0, 0x4A, 0xEC, 0xC0, 0xD0, 0x9C, 0xCC, 0xA1, 0x47, 0x7C, 0x9E, 0x80, 0x67, 0x63,
0x71, 0x9F, 0x40).map(_.toByte)

val actual = decodeEbcdicString(bytes, KeepAll, new CodePage278, improvedNullDetection = false)

assert(actual == expected)
}

"decode a CP278 string example" in {
val expected = "Ångbåten är över sjön med färggranna blommor."

val bytes = Array(0x5B, 0x95, 0x87, 0x82, 0xD0, 0xA3, 0x85, 0x95, 0x40, 0xC0, 0x99, 0x40, 0x6A,
0xA5, 0x85, 0x99, 0x40, 0xA2, 0x91, 0x6A, 0x95, 0x40, 0x94, 0x85, 0x84, 0x40, 0x86, 0xC0,
0x99, 0x87, 0x87, 0x99, 0x81, 0x95, 0x95, 0x81, 0x40, 0x82, 0x93, 0x96, 0x94, 0x94, 0x96,
0x99, 0x4B).map(_.toByte)

val actual = decodeEbcdicString(bytes, KeepAll, new CodePage278, improvedNullDetection = false)

assert(actual == expected)
}

"decode a CP500 string special characters" in {
val expected = "âäàáãåçñ[.<(+!&éêëèíîïìß]$*);^-/ÂÄÀÁÃÅÇѦ,%_>?øÉÊËÈÍÎÏÌ`:#@'=\"Øabcdefghi«»ðýþ±°jklmnopqrªºæ¸Æ¤µ~stuvwxyz¡¿ÐÝÞ®¢£¥·©§¶¼½¾¬|¯¨´×{ABCDEFGHI\u00ADôöòóõ}JKLMNOPQR¹ûüùúÿ\\÷STUVWXYZ²ÔÖÒÓÕ0123456789³ÛÜÙÚ"
val bytes = Array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ class CodePageSingleByteSpec extends AnyFunSuite {
assert(codePage.codePageShortName == "cp037_extended")
}

test("Ensure codepage 'cp273' gives the associated CodePage") {
val codePage = CodePage.getCodePageByName("cp273")
assert(codePage.codePageShortName == "cp273")
}

test("Ensure codepage 'cp277' gives the associated CodePage") {
val codePage = CodePage.getCodePageByName("cp277")
assert(codePage.codePageShortName == "cp277")
}

test("Ensure codepage 'cp278' gives the associated CodePage") {
val codePage = CodePage.getCodePageByName("cp278")
assert(codePage.codePageShortName == "cp278")
}

test("Ensure codepage 'cp300' gives the associated CodePage") {
val codePage = CodePage.getCodePageByName("cp300")
assert(codePage.codePageShortName == "cp300")
}

test("Ensure codepage 'cp500' gives the associated CodePage") {
val codePage = CodePage.getCodePageByName("cp500")
assert(codePage.codePageShortName == "cp500")
}

test("Ensure codepage 'cp838' gives the associated CodePage") {
val codePage = CodePage.getCodePageByName("cp838")
assert(codePage.codePageShortName == "cp838")
Expand All @@ -67,6 +92,31 @@ class CodePageSingleByteSpec extends AnyFunSuite {
assert(codePage.codePageShortName == "cp1047")
}

test("Ensure codepage 'cp1140' gives the associated CodePage") {
val codePage = CodePage.getCodePageByName("cp1140")
assert(codePage.codePageShortName == "cp1140")
}

test("Ensure codepage 'cp1141' gives the associated CodePage") {
val codePage = CodePage.getCodePageByName("cp1141")
assert(codePage.codePageShortName == "cp1141")
}

test("Ensure codepage 'cp1148' gives the associated CodePage") {
val codePage = CodePage.getCodePageByName("cp1148")
assert(codePage.codePageShortName == "cp1148")
}

test("Ensure codepage 'cp1364' gives the associated CodePage") {
val codePage = CodePage.getCodePageByName("cp1364")
assert(codePage.codePageShortName == "cp1364")
}

test("Ensure codepage 'cp1388' gives the associated CodePage") {
val codePage = CodePage.getCodePageByName("cp1388")
assert(codePage.codePageShortName == "cp1388")
}

test("Ensure an unknown codepage throws an IllegalArgumentException") {
assert(Try {
CodePage.getCodePageByName("sdw")
Expand Down
Loading