I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 775047fb authored by zzz's avatar zzz
Browse files

* SusiMail:

   - Fix HeaderLine decoder going past the headers,
     which was causing corruption in forwarded mails
   - More efficient output buffer allocation in HeaderLine decoder
parent 1e4b4331
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,7 @@ import i2p.susi.util.HexTable; ...@@ -28,6 +28,7 @@ import i2p.susi.util.HexTable;
import i2p.susi.util.ReadBuffer; import i2p.susi.util.ReadBuffer;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Locale; import java.util.Locale;
...@@ -150,17 +151,19 @@ public class HeaderLine implements Encoding { ...@@ -150,17 +151,19 @@ public class HeaderLine implements Encoding {
} }
return out.toString(); return out.toString();
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String) * @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/ */
public ReadBuffer decode( byte in[] ) throws DecodingException { public ReadBuffer decode( byte in[] ) throws DecodingException {
return decode( in, 0, in.length ); return decode( in, 0, in.length );
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String) * @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/ */
public ReadBuffer decode( byte in[], int offset, int length ) throws DecodingException { public ReadBuffer decode( byte in[], int offset, int length ) throws DecodingException {
byte[] out = new byte[length]; ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
int written = 0; int written = 0;
int end = offset + length; int end = offset + length;
if( end > in.length ) if( end > in.length )
...@@ -212,7 +215,7 @@ public class HeaderLine implements Encoding { ...@@ -212,7 +215,7 @@ public class HeaderLine implements Encoding {
if (clc.equals("utf-8") || clc.equals("utf8")) { if (clc.equals("utf-8") || clc.equals("utf8")) {
for( int j = 0; j < tmp.length; j++ ) { for( int j = 0; j < tmp.length; j++ ) {
byte d = tmp.content[ tmp.offset + j ]; byte d = tmp.content[ tmp.offset + j ];
out[written++] = ( d == '_' ? 32 : d ); out.write( d == '_' ? 32 : d );
} }
} else { } else {
// decode string // decode string
...@@ -221,7 +224,7 @@ public class HeaderLine implements Encoding { ...@@ -221,7 +224,7 @@ public class HeaderLine implements Encoding {
byte[] utf8 = DataHelper.getUTF8(decoded); byte[] utf8 = DataHelper.getUTF8(decoded);
for( int j = 0; j < utf8.length; j++ ) { for( int j = 0; j < utf8.length; j++ ) {
byte d = utf8[j]; byte d = utf8[j];
out[written++] = ( d == '_' ? 32 : d ); out.write( d == '_' ? 32 : d );
} }
} }
int distance = f4 + 2 - offset; int distance = f4 + 2 - offset;
...@@ -246,6 +249,11 @@ public class HeaderLine implements Encoding { ...@@ -246,6 +249,11 @@ public class HeaderLine implements Encoding {
* delay linebreak in case of long line * delay linebreak in case of long line
*/ */
linebreak = true; linebreak = true;
// The ReadBuffer can contain the body too.
// If we just had a linebreak, we are done...
// don't keep parsing!
if( length > 2 && in[offset+1] == '\r' && in[offset+2] == '\n')
break;
length--; length--;
offset++; offset++;
continue; continue;
...@@ -258,13 +266,13 @@ public class HeaderLine implements Encoding { ...@@ -258,13 +266,13 @@ public class HeaderLine implements Encoding {
* new line does not start with whitespace, so its not a new part of a * new line does not start with whitespace, so its not a new part of a
* long line * long line
*/ */
out[written++] = '\r'; out.write('\r');
out[written++] = '\n'; out.write('\n');
lastSkip = 0; lastSkip = 0;
} }
else { else {
if( !lastCharWasQuoted ) if( !lastCharWasQuoted )
out[written++] = ' '; out.write(' ');
/* /*
* skip whitespace * skip whitespace
*/ */
...@@ -286,16 +294,15 @@ public class HeaderLine implements Encoding { ...@@ -286,16 +294,15 @@ public class HeaderLine implements Encoding {
/* /*
* print out everything else literally * print out everything else literally
*/ */
out[written++] = c; out.write(c);
lastCharWasQuoted = false; lastCharWasQuoted = false;
} }
if( linebreak ) { if( linebreak ) {
out[written++] = '\r'; out.write('\r');
out[written++] = '\n'; out.write('\n');
lastSkip = 0;
} }
return new ReadBuffer(out, 0, written); return new ReadBuffer(out.toByteArray(), 0, out.size());
} }
public ReadBuffer decode(String text) throws DecodingException { public ReadBuffer decode(String text) throws DecodingException {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment