@@ -87,9 +87,10 @@ export class Helper {
8787 static decode7Bit ( text : string , inLen ?: number , alignBits ?: number ) {
8888 const ret : number [ ] = [ ] ;
8989 const data = Buffer . from ( text , 'hex' ) ;
90+
9091 let dataPos = 0 ; // Position in the input octets stream
9192 let buf = 0 ; // Bit buffer, used in FIFO manner
92- let bufLen = 0 ; // Ammount of buffered bits
93+ let bufLen = 0 ; // Amount of buffered bits
9394 let inDone = 0 ;
9495 let inExt = false ;
9596
@@ -113,22 +114,18 @@ export class Helper {
113114 }
114115
115116 // Fetch next septet from the FIFO buffer
116- let digit = buf & 0x7f ;
117+ const digit = buf & 0x7f ;
118+
117119 buf >>= 7 ;
118120 bufLen -= 7 ;
119121 inDone ++ ;
120122
121123 if ( digit % 128 === 27 ) {
124+ // Escape character
122125 inExt = true ;
123126 } else {
124- let c ;
125-
126- if ( inExt ) {
127- c = Helper . EXTENDED_TABLE . charCodeAt ( digit ) ;
128- inExt = false ;
129- } else {
130- c = Helper . ALPHABET_7BIT . charCodeAt ( digit ) ;
131- }
127+ let c = inExt ? Helper . EXTENDED_TABLE . charCodeAt ( digit ) || 63 : Helper . ALPHABET_7BIT . charCodeAt ( digit ) ;
128+ inExt = false ;
132129
133130 if ( c < 0x80 ) {
134131 ret . push ( c ) ;
@@ -140,7 +137,7 @@ export class Helper {
140137 ( Helper . EXTENDED_TABLE . charCodeAt ( digit + 1 ) & 0xfc00 ) === 0xdc00
141138 ) {
142139 // Surrogate Pair
143- c = 0x10000 + ( ( c & 0x03ff ) << 10 ) + ( Helper . EXTENDED_TABLE . charCodeAt ( ++ digit ) & 0x03ff ) ;
140+ c = 0x10000 + ( ( c & 0x03ff ) << 10 ) + ( Helper . EXTENDED_TABLE . charCodeAt ( digit + 1 ) & 0x03ff ) ;
144141 ret . push ( 0xf0 | ( c >> 18 ) , 0x80 | ( ( c >> 12 ) & 0x3f ) , 0x80 | ( ( c >> 6 ) & 0x3f ) , 0x80 | ( c & 0x3f ) ) ;
145142 } else {
146143 ret . push ( 0xe0 | ( c >> 12 ) , 0x80 | ( ( c >> 6 ) & 0x3f ) , 0x80 | ( c & 0x3f ) ) ;
@@ -175,7 +172,7 @@ export class Helper {
175172 const buffer = Buffer . from ( text , 'ascii' ) ;
176173
177174 for ( let i = 0 ; i < buffer . length ; i ++ ) {
178- pdu += this . toStringHex ( buffer [ i ] ) ;
175+ pdu += Helper . toStringHex ( buffer [ i ] ) ;
179176 length ++ ;
180177 }
181178
@@ -190,50 +187,57 @@ export class Helper {
190187 *
191188 * @returns An object containing the length of the encoded text in septets and the result as a hexadecimal string
192189 */
193- static encode7Bit ( text : string , alignBits ?: number ) {
194- let ret = '' ;
190+ static encode7Bit ( text : string , alignBits = 0 ) {
191+ let result = '' ;
195192 let buf = 0 ; // Bit buffer, used in FIFO manner
196- let bufLen = 0 ; // Ammount of buffered bits
197- let length = 0 ; // Ammount of produced septets
193+ let bufLen = 0 ; // Amount of buffered bits
194+ let length = 0 ; // Amount of produced septets
198195
199- // Insert leading alignment zero bits if requested
200- if ( alignBits ) {
201- bufLen += alignBits ;
202- }
196+ // Adjust for initial padding if alignBits is specified
197+ bufLen = alignBits ;
203198
204199 for ( const symb of text ) {
205- let code ;
200+ let code : number ;
206201
207202 if ( ( code = Helper . ALPHABET_7BIT . indexOf ( symb ) ) !== - 1 ) {
203+ // Normal character
208204 buf |= code << bufLen ;
209205 bufLen += 7 ;
210206 length ++ ;
211207 } else if ( ( code = Helper . EXTENDED_TABLE . indexOf ( symb ) ) !== - 1 ) {
212- buf |= ( ( code << 7 ) | 27 ) << bufLen ;
213- bufLen += 14 ;
214- length += 2 ;
208+ // ESC character (27), then the actual extended character
209+ buf |= 27 << bufLen ;
210+ bufLen += 7 ;
211+ length ++ ;
212+
213+ // Then add extended character
214+ buf |= code << bufLen ;
215+ bufLen += 7 ;
216+ length ++ ;
215217 } else {
216- buf |= 37 << bufLen ; // Place space symbol
218+ // Replace unknown with space (' '- code 0x20)
219+ buf |= 32 << bufLen ;
217220 bufLen += 7 ;
218221 length ++ ;
219222 }
220223
221224 while ( bufLen >= 8 ) {
222- ret += this . toStringHex ( buf & 0xff ) ;
225+ result += Helper . toStringHex ( buf & 0xff ) ;
223226 buf >>= 8 ;
224227 bufLen -= 8 ;
225228 }
226229 }
227230
228- if ( bufLen ) {
229- ret += this . toStringHex ( buf ) ; // here we have less then 8 bits
231+ // Write out remaining bits if needed
232+ if ( bufLen > 0 ) {
233+ result += Helper . toStringHex ( buf & 0xff ) ;
230234 }
231235
232236 if ( alignBits ) {
233237 length ++ ; // Add 1 to length to account for the padding septet
234238 }
235239
236- return { length, result : ret } ;
240+ return { length, result } ;
237241 }
238242
239243 /**
@@ -248,7 +252,7 @@ export class Helper {
248252
249253 for ( let i = 0 ; i < text . length ; i ++ ) {
250254 const byte = Helper . order ( text . substring ( i , i + 1 ) ) ;
251- pdu += this . toStringHex ( byte , 4 ) ;
255+ pdu += Helper . toStringHex ( byte , 4 ) ;
252256 length += 2 ;
253257 }
254258
@@ -263,12 +267,6 @@ export class Helper {
263267 * @returns The number as a hexadecimal string
264268 */
265269 static toStringHex ( number : number , fill = 2 ) {
266- let str = number . toString ( 16 ) ;
267-
268- while ( str . length < fill ) {
269- str = '0' + str ;
270- }
271-
272- return str . toUpperCase ( ) ;
270+ return number . toString ( 16 ) . padStart ( fill , '0' ) . toUpperCase ( ) ;
273271 }
274272}
0 commit comments