|
1 | 1 | package convex.core.data;
|
2 | 2 |
|
| 3 | +import convex.core.data.prim.AByteFlag; |
| 4 | +import convex.core.data.prim.ANumeric; |
| 5 | +import convex.core.data.prim.CVMBigInteger; |
| 6 | +import convex.core.data.prim.CVMChar; |
| 7 | +import convex.core.data.prim.CVMDouble; |
| 8 | +import convex.core.data.prim.CVMLong; |
3 | 9 | import convex.core.exceptions.BadFormatException;
|
| 10 | +import convex.core.util.ErrorMessages; |
4 | 11 |
|
5 | 12 | /**
|
6 |
| - * Base Encoder for CAD3 data / stores |
| 13 | + * Base Encoder for CAD3 data / stores. |
| 14 | + * |
| 15 | + * Does NOT directly decode custom CVM value types. Use the derived CVMEncoder if you need that behaviour. |
7 | 16 | */
|
8 | 17 | public class CAD3Encoder extends AEncoder<ACell> {
|
9 | 18 |
|
10 | 19 | public Blob encode(ACell a) {
|
11 | 20 | return Cells.encode(a);
|
12 | 21 | }
|
13 | 22 |
|
| 23 | + @Override |
14 | 24 | public ACell decode(Blob encoding) throws BadFormatException {
|
15 |
| - return Format.read(encoding); |
| 25 | + if (encoding.count()<1) throw new BadFormatException("Empty encoding"); |
| 26 | + return read(encoding,0); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public ACell read(Blob encoding, int offset) throws BadFormatException { |
| 31 | + byte tag = encoding.byteAt(offset); |
| 32 | + ACell result= read(tag,encoding,offset); |
| 33 | + return result; |
| 34 | + } |
| 35 | + |
| 36 | + protected ACell read(byte tag, Blob encoding, int offset) throws BadFormatException { |
| 37 | + switch (tag>>4) { |
| 38 | + case 0: // 0x00-0x0F : Only null is valid |
| 39 | + if (tag==Tag.NULL) return null; |
| 40 | + break; |
| 41 | + |
| 42 | + case 1: // 0x10-0x1F : Numeric values |
| 43 | + return readNumeric(tag,encoding,offset); |
| 44 | + |
| 45 | + case 2: // 0x20-0x2F : Addresses and references |
| 46 | + if (tag == Tag.ADDRESS) return Address.read(encoding,offset); |
| 47 | + // Note: 0x20 reference is invalid as a top level encoding |
| 48 | + break; |
| 49 | + |
| 50 | + case 3: // 0x30-0x3F : BAsic string / blob-like objects |
| 51 | + return readBasicObject(tag, encoding, offset); |
| 52 | + |
| 53 | + case 4: case 5: case 6: case 7: // 0x40-0x7F currently reserved |
| 54 | + break; |
| 55 | + |
| 56 | + case 8: // 0x80-0x8F : BAsic string / blob-like objects |
| 57 | + return readDataStructure(tag, encoding, offset); |
| 58 | + |
| 59 | + case 9: // 0x90-0x9F : Crypto / signature objects |
| 60 | + return readSignedData(tag,encoding, offset); |
| 61 | + |
| 62 | + case 10: // 0xA0-0xAF : Sparse records. A is for Airy. |
| 63 | + return readSparseRecord(tag,encoding,offset); |
| 64 | + |
| 65 | + case 11: // 0xB0-0xBF : Byte flags including booleans |
| 66 | + return AByteFlag.read(tag); |
| 67 | + |
| 68 | + case 12: // 0xC0-0xCF : Coded data objects |
| 69 | + return readCodedData(tag,encoding, offset); |
| 70 | + |
| 71 | + case 13: // 0xD0-0xDF : Dense records |
| 72 | + return readDenseRecord(tag,encoding, offset); |
| 73 | + |
| 74 | + case 14: // 0xE0-0xEF : Extension values |
| 75 | + return readExtension(tag,encoding, offset); |
| 76 | + |
| 77 | + case 15: // 0xF0-0xFF : Reserved / failure Values |
| 78 | + break; |
| 79 | + } |
| 80 | + |
| 81 | + return Format.read(tag,encoding,offset); |
| 82 | + // throw new BadFormatException(ErrorMessages.badTagMessage(tag)); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + protected ANumeric readNumeric(byte tag, Blob blob, int offset) throws BadFormatException { |
| 87 | + if (tag<0x19) return CVMLong.read(tag,blob,offset); |
| 88 | + if (tag == 0x19) return CVMBigInteger.read(blob,offset); |
| 89 | + if (tag == Tag.DOUBLE) return CVMDouble.read(tag,blob,offset); |
| 90 | + |
| 91 | + throw new BadFormatException(ErrorMessages.badTagMessage(tag)); |
| 92 | + } |
| 93 | + |
| 94 | + protected ACell readBasicObject(byte tag, Blob blob, int offset) throws BadFormatException{ |
| 95 | + switch (tag) { |
| 96 | + case Tag.SYMBOL: return Symbol.read(blob,offset); |
| 97 | + case Tag.KEYWORD: return Keyword.read(blob,offset); |
| 98 | + case Tag.BLOB: return Blobs.read(blob,offset); |
| 99 | + case Tag.STRING: return Strings.read(blob,offset); |
| 100 | + } |
| 101 | + |
| 102 | + if ((tag&Tag.CHAR_MASK)==Tag.CHAR_BASE) { |
| 103 | + int len=CVMChar.byteCountFromTag(tag); |
| 104 | + if (len>4) throw new BadFormatException("Can't read char type with length: " + len); |
| 105 | + return CVMChar.read(len, blob,offset); // skip tag byte |
| 106 | + } |
| 107 | + |
| 108 | + throw new BadFormatException(ErrorMessages.badTagMessage(tag)); |
| 109 | + } |
| 110 | + |
| 111 | + protected ACell readDataStructure(byte tag, Blob b, int pos) throws BadFormatException { |
| 112 | + if (tag == Tag.VECTOR) return Vectors.read(b,pos); |
| 113 | + |
| 114 | + if (tag == Tag.MAP) return Maps.read(b,pos); |
| 115 | + |
| 116 | + if (tag == Tag.SYNTAX) return Syntax.read(b,pos); |
| 117 | + |
| 118 | + if (tag == Tag.SET) return Sets.read(b,pos); |
| 119 | + |
| 120 | + if (tag == Tag.LIST) return List.read(b,pos); |
| 121 | + |
| 122 | + if (tag == Tag.INDEX) return Index.read(b,pos); |
| 123 | + |
| 124 | + throw new BadFormatException("Can't read data structure with tag byte: " + tag); |
| 125 | + } |
| 126 | + |
| 127 | + protected SignedData<?> readSignedData(byte tag,Blob blob, int offset) throws BadFormatException { |
| 128 | + if (tag==Tag.SIGNED_DATA) return SignedData.read(blob,offset,true); |
| 129 | + if (tag==Tag.SIGNED_DATA_SHORT) return SignedData.read(blob,offset,false); |
| 130 | + throw new BadFormatException(ErrorMessages.badTagMessage(tag)); |
| 131 | + } |
| 132 | + |
| 133 | + protected ARecord readSparseRecord(byte tag, Blob encoding, int offset) throws BadFormatException { |
| 134 | + // TODO spare records |
| 135 | + throw new BadFormatException(ErrorMessages.TODO); |
16 | 136 | }
|
17 | 137 |
|
| 138 | + protected ACell readCodedData(byte tag, Blob encoding, int offset) throws BadFormatException { |
| 139 | + // TODO Change delegation to proper read |
| 140 | + return Format.read(tag,encoding,offset); |
| 141 | + } |
| 142 | + |
| 143 | + protected ACell readDenseRecord(byte tag, Blob encoding, int offset) throws BadFormatException { |
| 144 | + // TODO Change delegation to proper read |
| 145 | + return Format.read(tag,encoding,offset); |
| 146 | + } |
| 147 | + |
| 148 | + protected ACell readExtension(byte tag, Blob blob, int offset) throws BadFormatException { |
| 149 | + // We expect a VLQ Count following the tag |
| 150 | + long code=Format.readVLQCount(blob,offset+1); |
| 151 | + return ExtensionValue.create(tag, code); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
18 | 157 | /**
|
19 | 158 | * Reads a cell value from a Blob of data, allowing for non-embedded branches following the first cell
|
20 | 159 | * @param data Data to decode
|
|
0 commit comments