Skip to content

Commit 4185131

Browse files
fix: string literals containing mutiple bytes
1 parent 0a13f17 commit 4185131

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

cobj/codegen.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,48 +420,56 @@ static void joutput_indent(const char *str) {
420420
static void joutput_string(const unsigned char *s, int size) {
421421
int i;
422422
int c;
423+
int output_multibyte = 0;
423424
int printable = 1;
424425

425426
for (i = 0; i < size;) {
426427
c = s[i];
427-
if ((0x00 <= c && c <= 0x1F) || (c == 0x7F) || (c == 0x80) || (0xf0 <= c)) {
428-
printable = 0;
429-
break;
430-
} else if (i < size - 1 &&
431-
((0x81 <= c && c <= 0x9f) || (0xE0 <= c && c <= 0xEF))) {
428+
if (0x20 <= c && c <= 0x7e) {
429+
i += 1;
430+
} else if ((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xef)) {
432431
i += 2;
433432
} else {
434-
i += 1;
433+
printable = 0;
434+
break;
435435
}
436436
}
437437

438438
if (printable) {
439439
if (param_wrap_string_flag) {
440440
joutput("new CobolDataStorage(");
441441
}
442+
442443
joutput("\"");
444+
443445
for (i = 0; i < size; i++) {
444446
c = s[i];
445-
if (c == '\"' || c == '\\') {
447+
if (!output_multibyte && (c == '\"' || c == '\\')) {
446448
joutput("\\%c", c);
447-
} else if (c == '\n') {
449+
} else if (!output_multibyte && (c == '\n')) {
448450
joutput("\\n");
449451
} else {
450452
joutput("%c", c);
451453
}
454+
output_multibyte = !output_multibyte &&
455+
((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xef));
452456
}
457+
453458
joutput("\"");
459+
454460
if (param_wrap_string_flag) {
455461
joutput(")");
456462
}
457463
} else {
458464
joutput("makeCobolDataStorage(");
465+
459466
for (i = 0; i < size; i++) {
460467
joutput("(byte)0x%02x", s[i]);
461468
if (i < size - 1) {
462469
joutput(", ");
463470
}
464471
}
472+
465473
joutput(")");
466474
}
467475
}

libcobj/src/jp/osscons/opensourcecobol/libcobj/data/CobolDataStorage.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package jp.osscons.opensourcecobol.libcobj.data;
2020

21+
import java.io.UnsupportedEncodingException;
2122
import java.nio.ByteBuffer;
2223
import java.nio.ByteOrder;
2324
import jp.osscons.opensourcecobol.libcobj.common.CobolModule;
@@ -180,7 +181,11 @@ public void memcpy(byte[] buf, int size) {
180181
* @param size
181182
*/
182183
public void memcpy(String str, int size) {
183-
this.memcpy(str.getBytes(), size);
184+
try {
185+
this.memcpy(str.getBytes("SJIS"), size);
186+
} catch (UnsupportedEncodingException e) {
187+
this.memcpy(str.getBytes(), size);
188+
}
184189
}
185190

186191
/**

0 commit comments

Comments
 (0)