relax ia5 parsing, and combine with ascii

This commit is contained in:
Tammo Spalink
2009-08-02 13:19:52 +08:00
parent 1132200fa1
commit 98aed46992
2 changed files with 29 additions and 48 deletions

View File

@@ -853,7 +853,7 @@ public final class BearerData {
}
}
private static String decodeIa5(byte[] data, int offset, int numFields)
private static String decode7bitAscii(byte[] data, int offset, int numFields)
throws CodingException
{
try {
@@ -868,37 +868,19 @@ public final class BearerData {
inStream.skip(offset);
for (int i = 0; i < numFields; i++) {
int charCode = inStream.read(7);
if ((charCode < UserData.IA5_MAP_BASE_INDEX) ||
(charCode > UserData.IA5_MAP_MAX_INDEX)) {
throw new CodingException("unsupported AI5 character code (" + charCode + ")");
if ((charCode >= UserData.ASCII_MAP_BASE_INDEX) ||
(charCode <= UserData.ASCII_MAP_MAX_INDEX)) {
strBuf.append(UserData.ASCII_MAP[charCode - UserData.ASCII_MAP_BASE_INDEX]);
} else if (charCode == UserData.ASCII_LF_INDEX) {
strBuf.append('\r');
} else if (charCode == UserData.ASCII_CR_INDEX) {
strBuf.append('\n');
} else {
/* For other charCodes, they are unprintable, and so simply use SPACE. */
strBuf.append(' ');
}
strBuf.append(UserData.IA5_MAP[charCode - UserData.IA5_MAP_BASE_INDEX]);
}
return strBuf.toString();
} catch (BitwiseInputStream.AccessException ex) {
throw new CodingException("AI5 decode failed: " + ex);
}
}
private static String decode7bitAscii(byte[] data, int offset, int numFields)
throws CodingException
{
try {
offset *= 8;
BitwiseInputStream inStream = new BitwiseInputStream(data);
int wantedBits = offset + (numFields * 7);
if (inStream.available() < wantedBits) {
throw new CodingException("insufficient data (wanted " + wantedBits +
" bits, but only have " + inStream.available() + ")");
}
inStream.skip(offset);
byte[] expandedData = new byte[numFields];
for (int i = 0; i < numFields; i++) {
expandedData[i] = (byte)inStream.read(7);
}
return new String(expandedData, 0, numFields, "US-ASCII");
} catch (java.io.UnsupportedEncodingException ex) {
throw new CodingException("7bit ASCII decode failed: " + ex);
} catch (BitwiseInputStream.AccessException ex) {
throw new CodingException("7bit ASCII decode failed: " + ex);
}
@@ -956,12 +938,10 @@ public final class BearerData {
// octet encoded.
userData.payloadStr = decodeLatin(userData.payload, offset, userData.numFields);
break;
case UserData.ENCODING_IA5:
case UserData.ENCODING_7BIT_ASCII:
userData.payloadStr = decode7bitAscii(userData.payload, offset, userData.numFields);
break;
case UserData.ENCODING_IA5:
userData.payloadStr = decodeIa5(userData.payload, offset, userData.numFields);
break;
case UserData.ENCODING_UNICODE_16:
userData.payloadStr = decodeUtf16(userData.payload, offset, userData.numFields);
break;
@@ -1003,7 +983,7 @@ public final class BearerData {
try {
StringBuffer strbuf = new StringBuffer(dataLen);
while (inStream.available() >= 6) {
strbuf.append(UserData.IA5_MAP[inStream.read(6)]);
strbuf.append(UserData.ASCII_MAP[inStream.read(6)]);
}
String data = strbuf.toString();
bData.numberOfMessages = Integer.parseInt(data.substring(0, 2));
@@ -1045,7 +1025,7 @@ public final class BearerData {
}
StringBuffer strbuf = new StringBuffer(dataLen);
for (int i = 0; i < numFields; i++) {
strbuf.append(UserData.IA5_MAP[inStream.read(6)]);
strbuf.append(UserData.ASCII_MAP[inStream.read(6)]);
}
bData.userData.payloadStr = strbuf.toString();
}

View File

@@ -49,19 +49,20 @@ public class UserData {
public static final int IS91_MSG_TYPE_SHORT_MESSAGE = 0x85;
/**
* IA5 data encoding character mappings.
* (See CCITT Rec. T.50 Tables 1 and 3)
* US ASCII character mapping table.
*
* Note this mapping is the the same as for printable ASCII
* characters, with a 0x20 offset, meaning that the ASCII SPACE
* character occurs with code 0x20.
* This table contains only the printable ASCII characters, with a
* 0x20 offset, meaning that the ASCII SPACE character is at index
* 0, with the resulting code of 0x20.
*
* Note this mapping is also equivalent to that used by the IS-91
* protocol, except for the latter using only 6 bits, and hence
* mapping only entries up to the '_' character.
* Note this mapping is also equivalent to that used by both the
* IS5 and the IS-91 encodings. For the former this is defined
* using CCITT Rec. T.50 Tables 1 and 3. For the latter IS 637 B,
* Table 4.3.1.4.1-1 -- and note the encoding uses only 6 bits,
* and hence only maps entries up to the '_' character.
*
*/
public static final char[] IA5_MAP = {
public static final char[] ASCII_MAP = {
' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
@@ -85,8 +86,8 @@ public class UserData {
public static final int ASCII_CR_INDEX = 0x0D;
public static final SparseIntArray charToAscii = new SparseIntArray();
static {
for (int i = 0; i < IA5_MAP.length; i++) {
charToAscii.put(IA5_MAP[i], PRINTABLE_ASCII_MIN_INDEX + i);
for (int i = 0; i < ASCII_MAP.length; i++) {
charToAscii.put(ASCII_MAP[i], PRINTABLE_ASCII_MIN_INDEX + i);
}
charToAscii.put('\r', ASCII_LF_INDEX);
charToAscii.put('\n', ASCII_CR_INDEX);
@@ -113,11 +114,11 @@ public class UserData {
}
/**
* Mapping for IA5 values less than 32 are flow control signals
* Mapping for ASCII values less than 32 are flow control signals
* and not used here.
*/
public static final int IA5_MAP_BASE_INDEX = 0x20;
public static final int IA5_MAP_MAX_INDEX = IA5_MAP_BASE_INDEX + IA5_MAP.length - 1;
public static final int ASCII_MAP_BASE_INDEX = 0x20;
public static final int ASCII_MAP_MAX_INDEX = ASCII_MAP_BASE_INDEX + ASCII_MAP.length - 1;
/**
* Contains the data header of the user data