4

According to RFC 2426 for VCard 3 format, http://www.ietf.org/rfc/rfc2426.txt (section 2.4.1)

Long lines of encoded binary data SHOULD BE folded to 75 characters using the folding method defined in [MIME-DIR].

however every encoder I've seen folds at 76 characters as per mime specs (i.e. commons, migbase64, etc) and and those I've found that allow specifying line length mentions "line length should be a multiple of 4".

Is there a java library out there that allows folding at 75 characters? And honestly, should I worry about a "SHOULD" rather then a "MUST?

  • I'd rather use a stable BASE64 implementation (like from commons) than worry about 76 vs. 75 characters. –  Jul 27 '12 at 09:40
  • Yes, although Tim Roes en/decoder is a potential solution, I too would prefer something very well used and stable. Now if I could have 75 character lines with something tried and true, that would be perfect. – user1557158 Jul 27 '12 at 09:59
  • The Cardme vcard library folds binary encoded lines at variables lengths to cater to various quirkiness of apps. They have VCard utility classes that can fold lines based on custom parameters. Check out the FoldLine method at http://cardme.svn.sourceforge.net/viewvc/cardme/trunk/src/net/sourceforge/cardme/util/VCardUtils.java?revision=278&view=markup – george_h Aug 03 '12 at 15:27
  • [When creating vCards dynamically using Perl](http://stackoverflow.com/questions/19167455/cant-add-a-base64-encoded-image-to-vcard), I had no problems with long lines. They import into iOS Contacts and OS X Contacts just fine. – Blazemonger Oct 03 '13 at 20:40

3 Answers3

1

After looking around at base64 encoders, I went back to the rfc and after a bit of rethinking, I don't think the base64 encoder needs to fold lines.

When it talks about base64 encoding of data it mentions folding:

2.4.1 BINARY
[...]Long lines of encoded binary data SHOULD BE folded to 75 characters
using the folding method defined in [MIME-DIR]

Then in 2.6 it mentions folding again

2.6 Line Delimiting and Folding
[...]   After generating a content line,
lines longer than 75 characters SHOULD be folded according to the
folding procedure described in [MIME DIR].

A content line is defined as

contentline  = [group "."] name *(";" param ) ":" value CRLF

(The importance being that it is the 'full line' not just the value that is folded)

When looking at 2.4.1 it implies the binary encoding should fold lines, however that seems to compete with the content line folding itself (2.6).

If binary data folding and line folding were both applied then 'double folding' would occur which I don't believe is intended. (since I know some heckler will mention the examples are not 75 characters long.. just pretend they are .. thanks) i.e.

PHOTO;ENCODING=b;TYPE=JPEG:MIICajCCAdOgAwIBAgICBEUwDQY <= line folded
MIICajCCAdOgAwI   <=folded at 75 binary chars as per binary folding
kU6NuaJR+E9lJd8ux9JpuV8QS+w3wNThZJ9Tb2HNBr1B+GNO9fi75e  <= line folded
[...]

A more reasonable interpretation is that the intended output is:

PHOTO;ENCODING=b;TYPE=JPEG:MIICajCCAdOgAwIBAgICBEUwDQY <=folded as per line folding
MIICajCCAdOgAwIkU6NuaJR+E9lJd8ux9JpuV8QS+w3wNThZJ9Tb2H <=folded as per line folding
NBr1B+GNO9fi75e[...]

I believe 2.4.1 is basically just reiterating the folder requirement of 2.6, which if correct implies that no base64 encoder of 75 lines is required because basic line folding of the vcard itself will achieve the desired result.

So unless proven wrong, I will go with this interpretation for now.

0

I have written an base64 en/decode for an XML-RPC client. You can find it on github. It's licensed under MIT, so you can just take this class out and use it in your project. To adjust the line width, you just have to edit line 111.

I don't know if you MUST or SHOULD stick to the line length, but since you can stick to it, I would do so :)

Tim Roes
  • 1,406
  • 1
  • 12
  • 22
0

I don't think folded line length is really a big deal. You could probably get away with not folding your lines at all. But the specs recommended it, so it is possible that there are applications out there which expect the lines to be folded.

ez-vcard allows you to manually define the max length of each line if you don't want to use the default 75 chars.

VCard vcard = new VCard();

byte[] photoBytes = ...
PhotoType photo = new PhotoType(photoBytes, ImageTypeParameter.JPEG);
vcard.addPhoto(photo);

Writer writer = ...
VCardVersion version = VCardVersion.V3_0;
FoldingScheme foldingScheme = new FoldingScheme(76, " ");
VCardWriter vcr = new VCardWriter(writer, version, foldingScheme);
vcr.write(vcard);
vcr.close();

About the "double folding" you mentioned, you are correct in your assumption. That is NOT how binary data is folded. First, the binary data is converted to base64, THEN the base64 string is folded.

Also, remember that all folded lines must begin with at least one whitespace character. In the example below, notice how the second and third lines begin with a space.

PHOTO;ENCODING=b;TYPE=JPEG:MIICajCCAdOgAwIBAgICBEUwDQY
 MIICajCCAdOgAwIkU6NuaJR+E9lJd8ux9JpuV8QS+w3wNThZJ9Tb2
 NBr1B+GNO9fi75e
Michael
  • 34,873
  • 17
  • 75
  • 109