Created
August 25, 2015 22:07
-
-
Save kevinmichaelchen/79948b9d0b448a51c472 to your computer and use it in GitHub Desktop.
Decoding split-up email attachment filenames
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Test { | |
| public static void main( String[] args ) | |
| { | |
| final String contentDisposition = "name: Content-Disposition\n" + | |
| "value: attachment;\n" + | |
| "\tsize=1123785;\n" + | |
| "\tmodification-date=\"Thu, 20 Aug 2015 17:27:44 GMT\";\n" + | |
| "\tfilename*0=\"=?utf-8?B?TmltciAoNCDDlyA0KSBhbmQgKDYgw5cgNikgaGlnaCBtb2JpbGl0eS\";\n" + | |
| "\tfilename*1=\"B2ZWhp?= cles.pdf\";\n" + | |
| "\tcreation-date=\"Thu, 20 Aug 2015 17:27:44 GMT\""; | |
| System.out.println( contentDisposition ); | |
| System.out.println( "" ); | |
| String[] lines = contentDisposition.split( "\n" ); | |
| String encodedString = ""; | |
| String unencodedTail = ""; | |
| Pattern filenamePattern = Pattern.compile( "filename\\*\\d+" ); | |
| Pattern firstFilenamePattern = Pattern.compile( "filename\\*0" ); | |
| for ( String line : lines ) | |
| { | |
| // remove the tabs on each line beginning | |
| line = line.trim(); | |
| // if the pattern matches... | |
| if ( filenamePattern.matcher( line ).find() ) | |
| { | |
| boolean firstFilenamePart = firstFilenamePattern.matcher( line ).find(); | |
| boolean lastFilenamePart = !firstFilenamePart && line.contains( " " ); | |
| // remove the crap before the first quote | |
| // convert | |
| // filename*0="=?utf-8?B?Tmlt" | |
| // to | |
| // =?utf-8?B?Tmlt" | |
| int firstQuoteIndex = line.indexOf( "\"" ); | |
| line = line.substring( firstQuoteIndex + 1 ); | |
| // the first part of the filename is special, since it tells us | |
| // if there's an encoding and also specifies which encoding | |
| // e.g., filename*0="=?utf-8?B?Tmlt | |
| if ( firstFilenamePart ) | |
| { | |
| // remove encoding prefix | |
| final String encodingPrefix = "=?utf-8?"; | |
| int encodingPrefixIndex = line.indexOf( encodingPrefix ); | |
| line = line.substring( encodingPrefixIndex + encodingPrefix.length() ); | |
| // check for base 64 | |
| if ( line.startsWith( "B?" ) ) | |
| { | |
| line = line.substring( 2 ); | |
| } | |
| // check for QP encoding | |
| else if ( line.startsWith( "Q?" ) ) | |
| { | |
| line = line.substring( 2 ); | |
| } | |
| // alert me if I'm missing anything | |
| else | |
| { | |
| throw new RuntimeException( "Unspecified filename encoding for line: " + line ); | |
| } | |
| } | |
| if ( !lastFilenamePart ) | |
| { | |
| // get everything until the next quote | |
| int nextQuoteIndex = line.indexOf( "\"" ); | |
| line = line.substring( 0, nextQuoteIndex ); | |
| encodedString += line; | |
| } | |
| else { | |
| // e.g., B2ZWhp?= cles.pdf" | |
| // get stuff between quote and space, e.g., B2ZWhp?= | |
| int firstSpaceIndex = line.indexOf( " " ); | |
| encodedString += line.substring( 0, firstSpaceIndex ); | |
| // add unencoded string after space | |
| line = line.substring( firstSpaceIndex + 1 ); | |
| int nextQuoteIndex = line.indexOf( "\"" ); | |
| unencodedTail += line.substring( 0, nextQuoteIndex ); | |
| } | |
| } | |
| } | |
| System.out.println( "res: " + encodedString ); | |
| System.out.println( "tail: " + unencodedTail ); | |
| byte[] bytes = Base64.decodeBase64( encodedString ); | |
| String result = new String( bytes ) + unencodedTail; | |
| System.out.println( "result: " + result ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment