@@ -77,6 +77,20 @@ private void encode(short obj) {
7777 push (obj );
7878 }
7979
80+ // Use this function, when the max len
81+ public short encode (short object , byte [] buffer , short startOff , short maxLength ) {
82+ scratchBuf [STACK_PTR_OFFSET ] = 0 ;
83+ bufferRef [0 ] = buffer ;
84+ scratchBuf [START_OFFSET ] = startOff ;
85+ if ((short ) (startOff + maxLength ) > buffer .length ) {
86+ ISOException .throwIt (ISO7816 .SW_CONDITIONS_NOT_SATISFIED );
87+ }
88+ scratchBuf [LEN_OFFSET ] = (short ) (startOff + maxLength );
89+ push (object );
90+ encode ();
91+ return (short ) (scratchBuf [START_OFFSET ] - startOff );
92+ }
93+
8094 public short encode (short object , byte [] buffer , short startOff ) {
8195 scratchBuf [STACK_PTR_OFFSET ] = 0 ;
8296 bufferRef [0 ] = buffer ;
@@ -87,60 +101,32 @@ public short encode(short object, byte[] buffer, short startOff) {
87101 } else {
88102 scratchBuf [LEN_OFFSET ] = (short ) buffer .length ;
89103 }
90- //this.length = (short)(startOff + length);
91104 push (object );
92105 encode ();
93106 return (short ) (scratchBuf [START_OFFSET ] - startOff );
94107 }
95108
96- // array{KMError.OK,Array{KMByteBlobs}}
97- public void encodeCertChain (byte [] buffer , short offset , short length , short errInt32Ptr ) {
98- bufferRef [0 ] = buffer ;
99- scratchBuf [START_OFFSET ] = offset ;
100- scratchBuf [LEN_OFFSET ] = (short ) (offset + 1 );
101- //Total length is ArrayHeader + [UIntHeader + length(errInt32Ptr)]
102- scratchBuf [LEN_OFFSET ] += (short ) (1 + getEncodedIntegerLength (errInt32Ptr ));
103-
104- writeMajorTypeWithLength (ARRAY_TYPE , (short ) 2 ); // Array of 2 elements
105- encodeUnsignedInteger (errInt32Ptr );
106- }
107-
108109 //array{KMError.OK,Array{KMByteBlobs}}
109- public short encodeCert (byte [] certBuffer , short bufferStart , short certStart , short certLength , short errInt32Ptr ) {
110+ public short encodeCert (byte [] certBuffer , short bufferStart , short certStart , short certLength ) {
111+ if (bufferStart > certStart ) {
112+ ISOException .throwIt (ISO7816 .SW_DATA_INVALID );
113+ }
110114 bufferRef [0 ] = certBuffer ;
111115 scratchBuf [START_OFFSET ] = certStart ;
112116 scratchBuf [LEN_OFFSET ] = (short ) (certStart + 1 );
113- //Array header - 2 elements i.e. 1 byte
114- scratchBuf [START_OFFSET ]--;
115- // errInt32Ptr - PowerResetStatus + ErrorCode - 4 bytes
116- // Integer header - 1 byte
117- scratchBuf [START_OFFSET ] -= getEncodedIntegerLength (errInt32Ptr );
118- //Array header - 2 elements i.e. 1 byte
117+ // Byte Header + cert length
118+ scratchBuf [START_OFFSET ] -= getEncodedBytesLength (certLength );
119+ //Array header - 1 elements i.e. 1 byte
119120 scratchBuf [START_OFFSET ]--;
120- // Cert Byte blob - typically 2 bytes length i.e. 3 bytes header
121- scratchBuf [START_OFFSET ] -= 2 ;
122- if (certLength >= SHORT_PAYLOAD ) {
123- scratchBuf [START_OFFSET ]--;
124- }
125121 if (scratchBuf [START_OFFSET ] < bufferStart ) {
126122 ISOException .throwIt (ISO7816 .SW_WRONG_LENGTH );
127123 }
128124 bufferStart = scratchBuf [START_OFFSET ];
129- writeMajorTypeWithLength (ARRAY_TYPE , (short ) 2 ); // Array of 2 elements
130- encodeUnsignedInteger (errInt32Ptr ); //PowerResetStatus + ErrorCode
131- writeMajorTypeWithLength (ARRAY_TYPE , (short ) 1 ); // Array of 1 element
125+ writeMajorTypeWithLength (ARRAY_TYPE , (short ) 1 ); // Array of 1 elements
132126 writeMajorTypeWithLength (BYTES_TYPE , certLength ); // Cert Byte Blob of length
133127 return bufferStart ;
134128 }
135129
136- public short encodeError (short errInt32Ptr , byte [] buffer , short startOff , short length ) {
137- bufferRef [0 ] = buffer ;
138- scratchBuf [START_OFFSET ] = startOff ;
139- scratchBuf [LEN_OFFSET ] = (short ) (startOff + length + 1 );
140- encodeUnsignedInteger (errInt32Ptr );
141- return (short ) (scratchBuf [START_OFFSET ] - startOff );
142- }
143-
144130 private void encode () {
145131 while (scratchBuf [STACK_PTR_OFFSET ] > 0 ) {
146132 short exp = pop ();
@@ -637,7 +623,7 @@ private short getEncodedArrayLen(short obj) {
637623 return len ;
638624 }
639625
640- private short getEncodedBytesLength (short len ) {
626+ public short getEncodedBytesLength (short len ) {
641627 short ret = 0 ;
642628 if (len < KMEncoder .UINT8_LENGTH && len >= 0 ) {
643629 ret = 1 ;
0 commit comments