Skip to content

Commit

Permalink
Add support for CBOR stringref extension.
Browse files Browse the repository at this point in the history
See http://cbor.schmorp.de/stringref for stringref definition.
Repeated binary and text strings are assigned an index on write and a
tagged integer is written when repeated. Parser respects the stringref IDs
when reading data back in.

Nested tags are now supported. The previous getCurrentTag() function is
still supported for backwards compatibility, and returns the first tag.
Otherwise a new TagList type is used to efficiently handle multiple tags.
  • Loading branch information
here-abarany committed Dec 10, 2022
1 parent 965f508 commit 99494af
Show file tree
Hide file tree
Showing 8 changed files with 1,535 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* Constants used by {@link CBORGenerator} and {@link CBORParser}
*
*
* @author Tatu Saloranta
*/
public final class CBORConstants
Expand Down Expand Up @@ -36,7 +36,7 @@ public final class CBORConstants
/* Other marker values
/**********************************************************
*/

public final static int SUFFIX_INDEFINITE = 0x1F;
public final static int SUFFIX_UINT8_ELEMENTS = 0x18;
public final static int SUFFIX_UINT16_ELEMENTS = 0x19;
Expand All @@ -56,6 +56,16 @@ public final class CBORConstants
* for the very first root-level data item.
*/
public final static int TAG_ID_SELF_DESCRIBE = 55799;

/**
* Tag denoting a namespace for string references in the following value.
*/
public final static int TAG_ID_STRINGREF_NAMESPACE = 256;

/**
* Tag denoting the next integer value should be an index for a previous string.
*/
public final static int TAG_ID_STRINGREF = 25;

/*
/**********************************************************
Expand All @@ -68,13 +78,13 @@ public final class CBORConstants
public final static byte BYTE_ARRAY_2_ELEMENTS = (byte) (PREFIX_TYPE_ARRAY + 2);

public final static byte BYTE_OBJECT_INDEFINITE = (byte) (PREFIX_TYPE_OBJECT + SUFFIX_INDEFINITE);

public final static byte BYTE_FALSE = (byte) (PREFIX_TYPE_MISC + 20);
public final static byte BYTE_TRUE = (byte) (PREFIX_TYPE_MISC + 21);
public final static byte BYTE_NULL = (byte) (PREFIX_TYPE_MISC + 22);

public final static byte BYTE_EMPTY_STRING = (byte) (PREFIX_TYPE_TEXT);

/**
* String that is chunked
*/
Expand All @@ -91,12 +101,12 @@ public final class CBORConstants
public final static int TAG_BIGNUM_NEG = 3;
public final static int TAG_DECIMAL_FRACTION = 4;
public final static int TAG_BIGFLOAT = 5;

public final static byte BYTE_TAG_BIGNUM_POS = (byte) (PREFIX_TYPE_TAG + TAG_BIGNUM_POS);
public final static byte BYTE_TAG_BIGNUM_NEG = (byte) (PREFIX_TYPE_TAG + TAG_BIGNUM_NEG);
public final static byte BYTE_TAG_DECIMAL_FRACTION = (byte) (PREFIX_TYPE_TAG + TAG_DECIMAL_FRACTION);
public final static byte BYTE_TAG_BIGFLOAT = (byte) (PREFIX_TYPE_TAG + TAG_BIGFLOAT);

public final static byte BYTE_BREAK = (byte) 0xFF;

public final static int INT_BREAK = 0xFF;
Expand Down Expand Up @@ -141,4 +151,13 @@ public static boolean hasMajorType(int expType, byte encoded) {
int actual = (encoded & MASK_MAJOR_TYPE) >> 5;
return (actual == expType);
}

public static boolean shouldReferenceString(int index, int stringBytes) {
// See table in specification: http://cbor.schmorp.de/stringref
// Only support 32-bit indices.
return (index >= 0 && index <= 23 && stringBytes >= 3) ||
(index >= 24 && index <= 255 && stringBytes >= 4) ||
(index >= 256 && index <= 65535 && stringBytes >= 5) ||
(index >= 65536 && stringBytes >= 7);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@ private final CBORGenerator _createCBORGenerator(IOContext ctxt,
if (CBORGenerator.Feature.WRITE_TYPE_HEADER.enabledIn(formatFeat)) {
gen.writeTag(CBORConstants.TAG_ID_SELF_DESCRIBE);
}
if (CBORGenerator.Feature.STRINGREF.enabledIn(formatFeat)) {
gen.writeTag(CBORConstants.TAG_ID_STRINGREF_NAMESPACE);
}
return gen;
}

Expand Down
Loading

0 comments on commit 99494af

Please sign in to comment.