| 1 |
// =========================================================================== |
| 2 |
// |
| 3 |
// PUBLIC DOMAIN NOTICE |
| 4 |
// National Center for Biotechnology Information (NCBI) |
| 5 |
// |
| 6 |
// This software/database is a "United States Government Work" under the |
| 7 |
// terms of the United States Copyright Act. It was written as part of |
| 8 |
// the author's official duties as a United States Government employee and |
| 9 |
// thus cannot be copyrighted. This software/database is freely available |
| 10 |
// to the public for use. The National Library of Medicine and the U.S. |
| 11 |
// Government do not place any restriction on its use or reproduction. |
| 12 |
// We would, however, appreciate having the NCBI and the author cited in |
| 13 |
// any work or product based on this material. |
| 14 |
// |
| 15 |
// Although all reasonable efforts have been taken to ensure the accuracy |
| 16 |
// and reliability of the software and data, the NLM and the U.S. |
| 17 |
// Government do not and cannot warrant the performance or results that |
| 18 |
// may be obtained by using this software or data. The NLM and the U.S. |
| 19 |
// Government disclaim all warranties, express or implied, including |
| 20 |
// warranties of performance, merchantability or fitness for any particular |
| 21 |
// purpose. |
| 22 |
// |
| 23 |
// =========================================================================== |
| 24 |
// |
| 25 |
// File Name: asn.go |
| 26 |
// |
| 27 |
// Author: Jonathan Kans |
| 28 |
// |
| 29 |
// ========================================================================== |
| 30 |
|
| 31 |
package eutils |
| 32 |
|
| 33 |
import ( |
| 34 |
"bufio" |
| 35 |
"html" |
| 36 |
"io" |
| 37 |
"os" |
| 38 |
"runtime" |
| 39 |
"strings" |
| 40 |
"unicode" |
| 41 |
) |
| 42 |
|
| 43 |
// ASN1Converter parses text ASN.1 records into XML objects |
| 44 |
func ASN1Converter(inp io.Reader, set, rec string) <-chan string { |
| 45 |
|
| 46 |
if inp == nil { |
| 47 |
return nil |
| 48 |
} |
| 49 |
|
| 50 |
tks := make(chan string, chanDepth) |
| 51 |
out := make(chan string, chanDepth) |
| 52 |
if tks == nil || out == nil { |
| 53 |
DisplayError("Unable to create ASN1 converter channels") |
| 54 |
os.Exit(1) |
| 55 |
} |
| 56 |
|
| 57 |
// tokenizeASN1 sends ASN1 tokens down a channel |
| 58 |
tokenizeASN1 := func(inp io.Reader, tks chan<- string) { |
| 59 |
|
| 60 |
// close channel when all tokens have been sent |
| 61 |
defer close(tks) |
| 62 |
|
| 63 |
var buf strings.Builder |
| 64 |
|
| 65 |
scanr := bufio.NewScanner(inp) |
| 66 |
|
| 67 |
row := 0 |
| 68 |
idx := 0 |
| 69 |
line := "" |
| 70 |
|
| 71 |
sentinel := string(rune(0)) |
| 72 |
|
| 73 |
nextLine := func() string { |
| 74 |
|
| 75 |
for scanr.Scan() { |
| 76 |
// read line |
| 77 |
line := scanr.Text() |
| 78 |
row++ |
| 79 |
if line == "" { |
| 80 |
// ignore blank lines |
| 81 |
continue |
| 82 |
} |
| 83 |
|
| 84 |
// https://stackoverflow.com/questions/58994146/how-to-remove-non-printable-characters |
| 85 |
line = strings.Map(func(r rune) rune { |
| 86 |
if unicode.IsPrint(r) { |
| 87 |
return r |
| 88 |
} |
| 89 |
return -1 |
| 90 |
}, line) |
| 91 |
|
| 92 |
// add sentinel |
| 93 |
line += sentinel |
| 94 |
return line |
| 95 |
} |
| 96 |
|
| 97 |
// end of data |
| 98 |
return sentinel |
| 99 |
} |
| 100 |
|
| 101 |
readRestOfAsnString := func() string { |
| 102 |
|
| 103 |
for { |
| 104 |
|
| 105 |
line = nextLine() |
| 106 |
if line == "" || line == sentinel { |
| 107 |
break |
| 108 |
} |
| 109 |
idx = 0 |
| 110 |
|
| 111 |
ch := line[idx] |
| 112 |
|
| 113 |
for inAsnString[ch] { |
| 114 |
idx++ |
| 115 |
ch = line[idx] |
| 116 |
} |
| 117 |
str := line[:idx] |
| 118 |
line = line[idx:] |
| 119 |
idx = 0 |
| 120 |
|
| 121 |
buf.WriteString(str) |
| 122 |
|
| 123 |
if strings.HasPrefix(line, "\"") { |
| 124 |
// " |
| 125 |
// skip past closing quote |
| 126 |
line = line[1:] |
| 127 |
idx = 0 |
| 128 |
|
| 129 |
// break out of continuation loop |
| 130 |
return line |
| 131 |
} |
| 132 |
|
| 133 |
// string continues on additional lines |
| 134 |
} |
| 135 |
|
| 136 |
return line |
| 137 |
} |
| 138 |
|
| 139 |
readRestOfAsnBits := func() string { |
| 140 |
|
| 141 |
for { |
| 142 |
|
| 143 |
line = nextLine() |
| 144 |
if line == "" || line == sentinel { |
| 145 |
break |
| 146 |
} |
| 147 |
idx = 0 |
| 148 |
|
| 149 |
ch := line[idx] |
| 150 |
|
| 151 |
for inAsnBits[ch] { |
| 152 |
idx++ |
| 153 |
ch = line[idx] |
| 154 |
} |
| 155 |
str := line[:idx] |
| 156 |
line = line[idx:] |
| 157 |
idx = 0 |
| 158 |
|
| 159 |
buf.WriteString(str) |
| 160 |
|
| 161 |
if strings.HasPrefix(line, "'") { |
| 162 |
// skip past closing apostrophe |
| 163 |
line = line[1:] |
| 164 |
idx = 0 |
| 165 |
|
| 166 |
// break out of continuation loop |
| 167 |
return line |
| 168 |
} |
| 169 |
|
| 170 |
// bit string continues on additional lines |
| 171 |
} |
| 172 |
|
| 173 |
return line |
| 174 |
} |
| 175 |
|
| 176 |
for { |
| 177 |
|
| 178 |
line = nextLine() |
| 179 |
if line == "" || line == sentinel { |
| 180 |
break |
| 181 |
} |
| 182 |
|
| 183 |
for { |
| 184 |
|
| 185 |
if line == "" || line == sentinel { |
| 186 |
break |
| 187 |
} |
| 188 |
idx = 0 |
| 189 |
|
| 190 |
// trim leading blanks |
| 191 |
ch := line[idx] |
| 192 |
for inBlank[ch] { |
| 193 |
idx++ |
| 194 |
ch = line[idx] |
| 195 |
} |
| 196 |
line = line[idx:] |
| 197 |
idx = 0 |
| 198 |
|
| 199 |
if ch == ',' { |
| 200 |
tks <- string(ch) |
| 201 |
line = line[1:] |
| 202 |
continue |
| 203 |
} |
| 204 |
|
| 205 |
if ch == '{' { |
| 206 |
// start structure |
| 207 |
tks <- string(ch) |
| 208 |
line = line[1:] |
| 209 |
continue |
| 210 |
} |
| 211 |
|
| 212 |
if ch == '}' { |
| 213 |
// end structure |
| 214 |
tks <- string(ch) |
| 215 |
line = line[1:] |
| 216 |
continue |
| 217 |
} |
| 218 |
|
| 219 |
if ch == '"' { |
| 220 |
// " |
| 221 |
// start of string |
| 222 |
buf.Reset() |
| 223 |
|
| 224 |
// skip past opening quote |
| 225 |
line = line[1:] |
| 226 |
idx = 0 |
| 227 |
ch = line[idx] |
| 228 |
|
| 229 |
// read to closing quote or sentinel at end of line |
| 230 |
for inAsnString[ch] { |
| 231 |
idx++ |
| 232 |
ch = line[idx] |
| 233 |
} |
| 234 |
str := line[:idx] |
| 235 |
line = line[idx:] |
| 236 |
idx = 0 |
| 237 |
|
| 238 |
buf.WriteString(str) |
| 239 |
|
| 240 |
if strings.HasPrefix(line, "\"") { |
| 241 |
// " |
| 242 |
// skip past closing quote |
| 243 |
line = line[1:] |
| 244 |
idx = 0 |
| 245 |
|
| 246 |
} else { |
| 247 |
|
| 248 |
// continue reading additional lines of string |
| 249 |
line = readRestOfAsnString() |
| 250 |
idx = 0 |
| 251 |
} |
| 252 |
|
| 253 |
tmp := buf.String() |
| 254 |
if tmp == "" { |
| 255 |
// encode empty string |
| 256 |
tmp = "\"\"" |
| 257 |
} |
| 258 |
|
| 259 |
tks <- tmp |
| 260 |
buf.Reset() |
| 261 |
|
| 262 |
continue |
| 263 |
} |
| 264 |
|
| 265 |
if ch == '\'' { |
| 266 |
// start of bit string |
| 267 |
buf.Reset() |
| 268 |
|
| 269 |
// skip past opening apostrophe |
| 270 |
line = line[1:] |
| 271 |
idx = 0 |
| 272 |
ch = line[idx] |
| 273 |
|
| 274 |
// read to closing apostrophe or sentinel at end of line |
| 275 |
for inAsnBits[ch] { |
| 276 |
idx++ |
| 277 |
ch = line[idx] |
| 278 |
} |
| 279 |
str := line[:idx] |
| 280 |
line = line[idx:] |
| 281 |
idx = 0 |
| 282 |
|
| 283 |
buf.WriteString(str) |
| 284 |
|
| 285 |
if strings.HasPrefix(line, "'") { |
| 286 |
// skip past closing apostrophe |
| 287 |
line = line[1:] |
| 288 |
idx = 0 |
| 289 |
|
| 290 |
} else { |
| 291 |
|
| 292 |
// continue reading additional lines of bit string |
| 293 |
line = readRestOfAsnBits() |
| 294 |
idx = 0 |
| 295 |
} |
| 296 |
|
| 297 |
// if apostrophe is at end of line, read next line |
| 298 |
if line == "" || line == sentinel { |
| 299 |
line = nextLine() |
| 300 |
} |
| 301 |
|
| 302 |
// then skip past trailing hex or binary indicator |
| 303 |
if strings.HasPrefix(line, "H") || strings.HasPrefix(line, "B") { |
| 304 |
line = line[1:] |
| 305 |
idx = 0 |
| 306 |
} |
| 307 |
|
| 308 |
tks <- buf.String() |
| 309 |
buf.Reset() |
| 310 |
|
| 311 |
continue |
| 312 |
} |
| 313 |
|
| 314 |
if ch == ':' && strings.HasPrefix(line, "::=") { |
| 315 |
// start of record contents |
| 316 |
tks <- "::=" |
| 317 |
line = line[3:] |
| 318 |
idx = 0 |
| 319 |
continue |
| 320 |
} |
| 321 |
|
| 322 |
if ch == '-' && strings.HasPrefix(line, "--") { |
| 323 |
// skip comments |
| 324 |
break |
| 325 |
} |
| 326 |
if ch == ';' { |
| 327 |
// skip comments |
| 328 |
break |
| 329 |
} |
| 330 |
|
| 331 |
// read token or unquoted numeric value |
| 332 |
idx = 0 |
| 333 |
for inAsnTag[ch] { |
| 334 |
idx++ |
| 335 |
ch = line[idx] |
| 336 |
} |
| 337 |
tkn := line[:idx] |
| 338 |
line = line[idx:] |
| 339 |
idx = 0 |
| 340 |
|
| 341 |
tks <- tkn |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
// convertASN1 sends XML records down a channel |
| 347 |
convertASN1 := func(inp <-chan string, out chan<- string) { |
| 348 |
|
| 349 |
// close channel when all tokens have been processed |
| 350 |
defer close(out) |
| 351 |
|
| 352 |
// ensure that XML tags are legal |
| 353 |
fixTag := func(tag string) string { |
| 354 |
|
| 355 |
if tag == "" { |
| 356 |
return tag |
| 357 |
} |
| 358 |
|
| 359 |
okay := true |
| 360 |
for _, ch := range tag { |
| 361 |
if !inElement[ch] { |
| 362 |
okay = false |
| 363 |
} |
| 364 |
} |
| 365 |
if okay { |
| 366 |
return tag |
| 367 |
} |
| 368 |
|
| 369 |
var temp strings.Builder |
| 370 |
|
| 371 |
// replace illegal characters with underscore |
| 372 |
for _, ch := range tag { |
| 373 |
if inElement[ch] { |
| 374 |
temp.WriteRune(ch) |
| 375 |
} else { |
| 376 |
temp.WriteRune('_') |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
return temp.String() |
| 381 |
} |
| 382 |
|
| 383 |
nextToken := func() string { |
| 384 |
|
| 385 |
for { |
| 386 |
tkn, ok := <-inp |
| 387 |
if !ok { |
| 388 |
break |
| 389 |
} |
| 390 |
if tkn == "" { |
| 391 |
// ignore blank tokens |
| 392 |
continue |
| 393 |
} |
| 394 |
|
| 395 |
return tkn |
| 396 |
} |
| 397 |
|
| 398 |
// end of data |
| 399 |
return "" |
| 400 |
} |
| 401 |
|
| 402 |
// collects tags until next brace or comma |
| 403 |
var arry []string |
| 404 |
|
| 405 |
// builds XML output for current record |
| 406 |
var buffer strings.Builder |
| 407 |
|
| 408 |
// array to speed up indentation |
| 409 |
indentSpaces := []string{ |
| 410 |
"", |
| 411 |
" ", |
| 412 |
" ", |
| 413 |
" ", |
| 414 |
" ", |
| 415 |
" ", |
| 416 |
" ", |
| 417 |
" ", |
| 418 |
" ", |
| 419 |
" ", |
| 420 |
} |
| 421 |
|
| 422 |
count := 0 |
| 423 |
|
| 424 |
indent := 0 |
| 425 |
if set != "" { |
| 426 |
indent = 1 |
| 427 |
} |
| 428 |
|
| 429 |
// indent a specified number of spaces |
| 430 |
doIndent := func(indt int) { |
| 431 |
i := indt |
| 432 |
for i > 9 { |
| 433 |
buffer.WriteString(" ") |
| 434 |
i -= 10 |
| 435 |
} |
| 436 |
if i < 0 { |
| 437 |
return |
| 438 |
} |
| 439 |
buffer.WriteString(indentSpaces[i]) |
| 440 |
} |
| 441 |
|
| 442 |
printOpeningTag := func(tag string) { |
| 443 |
|
| 444 |
tag = fixTag(tag) |
| 445 |
doIndent(indent) |
| 446 |
indent++ |
| 447 |
buffer.WriteString("<") |
| 448 |
buffer.WriteString(tag) |
| 449 |
buffer.WriteString(">\n") |
| 450 |
} |
| 451 |
|
| 452 |
printClosingTag := func(tag string) { |
| 453 |
|
| 454 |
tag = fixTag(tag) |
| 455 |
indent-- |
| 456 |
doIndent(indent) |
| 457 |
buffer.WriteString("</") |
| 458 |
buffer.WriteString(tag) |
| 459 |
buffer.WriteString(">\n") |
| 460 |
} |
| 461 |
|
| 462 |
printContent := func(tag, tkn string) { |
| 463 |
|
| 464 |
if tkn == "\"\"" { |
| 465 |
return |
| 466 |
} |
| 467 |
tag = fixTag(tag) |
| 468 |
doIndent(indent) |
| 469 |
buffer.WriteString("<") |
| 470 |
buffer.WriteString(tag) |
| 471 |
buffer.WriteString(">") |
| 472 |
tkn = strings.TrimSpace(tkn) |
| 473 |
tkn = html.EscapeString(tkn) |
| 474 |
buffer.WriteString(tkn) |
| 475 |
buffer.WriteString("</") |
| 476 |
buffer.WriteString(tag) |
| 477 |
buffer.WriteString(">\n") |
| 478 |
} |
| 479 |
|
| 480 |
popFromArry := func() (string, string, string) { |
| 481 |
|
| 482 |
fst, sec, trd := "", "", "" |
| 483 |
switch len(arry) { |
| 484 |
case 1: |
| 485 |
fst = arry[0] |
| 486 |
case 2: |
| 487 |
fst, sec = arry[0], arry[1] |
| 488 |
case 3: |
| 489 |
fst, sec, trd = arry[0], arry[1], arry[2] |
| 490 |
} |
| 491 |
arry = nil |
| 492 |
return fst, sec, trd |
| 493 |
} |
| 494 |
|
| 495 |
// recursive function definition |
| 496 |
var parseAsnObject func(prnt string, lvl int) |
| 497 |
|
| 498 |
parseAsnObject = func(prnt string, lvl int) { |
| 499 |
|
| 500 |
for { |
| 501 |
tkn := nextToken() |
| 502 |
if tkn == "" { |
| 503 |
return |
| 504 |
} |
| 505 |
|
| 506 |
switch tkn { |
| 507 |
case "{": |
| 508 |
fst, sec, trd := popFromArry() |
| 509 |
tag := fst |
| 510 |
if fst == "" { |
| 511 |
fst = prnt |
| 512 |
} |
| 513 |
if fst == "" { |
| 514 |
return |
| 515 |
} |
| 516 |
printOpeningTag(fst) |
| 517 |
tag = fst + "_E" |
| 518 |
if sec != "" { |
| 519 |
printOpeningTag(sec) |
| 520 |
tag = sec + "_E" |
| 521 |
} |
| 522 |
if trd != "" { |
| 523 |
printOpeningTag(trd) |
| 524 |
tag = trd |
| 525 |
} |
| 526 |
parseAsnObject(tag, lvl+1) |
| 527 |
if trd != "" { |
| 528 |
printClosingTag(trd) |
| 529 |
} |
| 530 |
if sec != "" { |
| 531 |
printClosingTag(sec) |
| 532 |
} |
| 533 |
printClosingTag(fst) |
| 534 |
if lvl == 0 { |
| 535 |
return |
| 536 |
} |
| 537 |
case ",": |
| 538 |
fst, sec, trd := popFromArry() |
| 539 |
if trd != "" { |
| 540 |
printOpeningTag(fst) |
| 541 |
printContent(sec, trd) |
| 542 |
printClosingTag(fst) |
| 543 |
} else if sec != "" { |
| 544 |
printContent(fst, sec) |
| 545 |
} else if fst != "" { |
| 546 |
printContent(prnt, fst) |
| 547 |
} |
| 548 |
case "}": |
| 549 |
fst, sec, trd := popFromArry() |
| 550 |
if fst == "" { |
| 551 |
return |
| 552 |
} |
| 553 |
if trd != "" { |
| 554 |
printOpeningTag(fst) |
| 555 |
printContent(sec, trd) |
| 556 |
printClosingTag(fst) |
| 557 |
} else if sec != "" { |
| 558 |
printContent(fst, sec) |
| 559 |
} else if fst != "" { |
| 560 |
printContent(prnt, fst) |
| 561 |
} |
| 562 |
return |
| 563 |
case "::=": |
| 564 |
DisplayError("Unexpected ::= token found") |
| 565 |
os.Exit(1) |
| 566 |
default: |
| 567 |
arry = append(arry, tkn) |
| 568 |
} |
| 569 |
|
| 570 |
count++ |
| 571 |
if count > 1000 { |
| 572 |
count = 0 |
| 573 |
txt := buffer.String() |
| 574 |
if txt != "" { |
| 575 |
// send current result through output channel |
| 576 |
out <- txt |
| 577 |
} |
| 578 |
buffer.Reset() |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
if set != "" { |
| 584 |
out <- "<" + set + ">" |
| 585 |
} |
| 586 |
|
| 587 |
// process stream of catenated top-level ASN1 records |
| 588 |
for { |
| 589 |
arry = nil |
| 590 |
|
| 591 |
top := nextToken() |
| 592 |
if top == "" { |
| 593 |
break |
| 594 |
} |
| 595 |
|
| 596 |
if rec != "" { |
| 597 |
top = rec |
| 598 |
} |
| 599 |
|
| 600 |
arry = append(arry, top) |
| 601 |
|
| 602 |
tkn := nextToken() |
| 603 |
if tkn == "" { |
| 604 |
DisplayError("Incomplete ASN1 starting with '%s'", top) |
| 605 |
os.Exit(1) |
| 606 |
} |
| 607 |
if tkn != "::=" { |
| 608 |
DisplayError("ASN1 message missing expected ::= token, found '%s'", tkn) |
| 609 |
os.Exit(1) |
| 610 |
} |
| 611 |
|
| 612 |
parseAsnObject(top, 0) |
| 613 |
|
| 614 |
txt := buffer.String() |
| 615 |
if txt != "" { |
| 616 |
// send remaining result through output channel |
| 617 |
out <- txt |
| 618 |
} |
| 619 |
|
| 620 |
buffer.Reset() |
| 621 |
|
| 622 |
runtime.Gosched() |
| 623 |
} |
| 624 |
|
| 625 |
if set != "" { |
| 626 |
out <- "</" + set + ">" |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
// launch single tokenizer goroutine |
| 631 |
go tokenizeASN1(inp, tks) |
| 632 |
|
| 633 |
// launch single converter goroutine |
| 634 |
go convertASN1(tks, out) |
| 635 |
|
| 636 |
return out |
| 637 |
} |
| 638 |
|
| 639 |
// ASNtoXML converts ASN.1 to an XML string |
| 640 |
func ASNtoXML(asn, set, rec string) string { |
| 641 |
|
| 642 |
return StringToXML(asn, func(inp io.Reader) <-chan string { |
| 643 |
return ASN1Converter(inp, set, rec) |
| 644 |
}) |
| 645 |
} |