| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "asterisk.h" |
| 18 |
#include "asterisk/lock.h" |
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
#include "ooasn1.h" |
| 22 |
|
| 23 |
static int encode16BitConstrainedString |
| 24 |
(OOCTXT* pctxt, Asn116BitCharString value, Asn116BitCharSet* pCharSet); |
| 25 |
|
| 26 |
static int encode2sCompBinInt (OOCTXT* pctxt, ASN1INT value); |
| 27 |
static int encodeNonNegBinInt (OOCTXT* pctxt, ASN1UINT value); |
| 28 |
static int encodeUnconsLength (OOCTXT* pctxt, ASN1UINT value); |
| 29 |
static int getIdentByteCount (ASN1UINT ident); |
| 30 |
|
| 31 |
int encodeBitsFromOctet (OOCTXT* pctxt, ASN1OCTET value, ASN1UINT nbits); |
| 32 |
int encodeGetMsgBitCnt (OOCTXT* pctxt); |
| 33 |
int encodeIdent (OOCTXT* pctxt, ASN1UINT ident); |
| 34 |
|
| 35 |
|
| 36 |
int encodeBit (OOCTXT* pctxt, ASN1BOOL value) |
| 37 |
{ |
| 38 |
int stat = ASN_OK; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
if (pctxt->buffer.bitOffset == 8) { |
| 43 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
if (--pctxt->buffer.bitOffset < 0) { |
| 49 |
if (++pctxt->buffer.byteIndex >= pctxt->buffer.size) { |
| 50 |
if ((stat = encodeExpandBuffer (pctxt, 1)) != ASN_OK) { |
| 51 |
return stat; |
| 52 |
} |
| 53 |
} |
| 54 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; |
| 55 |
pctxt->buffer.bitOffset = 7; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
if (value) { |
| 61 |
pctxt->buffer.data[pctxt->buffer.byteIndex] |= |
| 62 |
( 1 << pctxt->buffer.bitOffset ); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
if (pctxt->buffer.bitOffset == 0) { |
| 68 |
pctxt->buffer.bitOffset = 8; |
| 69 |
pctxt->buffer.byteIndex++; |
| 70 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; |
| 71 |
} |
| 72 |
|
| 73 |
return stat; |
| 74 |
} |
| 75 |
|
| 76 |
int encodeBits (OOCTXT* pctxt, ASN1UINT value, ASN1UINT nbits) |
| 77 |
{ |
| 78 |
int nbytes = (nbits + 7)/ 8, stat = ASN_OK; |
| 79 |
|
| 80 |
if (nbits == 0) return stat; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
if (pctxt->buffer.bitOffset == 8) { |
| 85 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
if (nbits < (sizeof(ASN1UINT) * 8)) |
| 91 |
value &= ((1 << nbits) - 1); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if (nbits < (unsigned)pctxt->buffer.bitOffset) { |
| 96 |
pctxt->buffer.bitOffset -= nbits; |
| 97 |
pctxt->buffer.data[pctxt->buffer.byteIndex] |= |
| 98 |
( value << pctxt->buffer.bitOffset ); |
| 99 |
return stat; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
stat = encodeCheckBuffer (pctxt, nbytes); |
| 105 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
nbits -= pctxt->buffer.bitOffset; |
| 111 |
pctxt->buffer.data[pctxt->buffer.byteIndex++] |= |
| 112 |
(ASN1OCTET)( value >> nbits ); |
| 113 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; |
| 114 |
|
| 115 |
while (nbits >= 8) { |
| 116 |
nbits -= 8; |
| 117 |
pctxt->buffer.data[pctxt->buffer.byteIndex++] = |
| 118 |
(ASN1OCTET)( value >> nbits ); |
| 119 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
pctxt->buffer.bitOffset = 8 - nbits; |
| 125 |
if (nbits > 0) { |
| 126 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = |
| 127 |
(ASN1OCTET)((value & ((1 << nbits)-1)) << pctxt->buffer.bitOffset); |
| 128 |
} |
| 129 |
else |
| 130 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; |
| 131 |
|
| 132 |
return stat; |
| 133 |
} |
| 134 |
|
| 135 |
int encodeBitsFromOctet (OOCTXT* pctxt, ASN1OCTET value, ASN1UINT nbits) |
| 136 |
{ |
| 137 |
int lshift = pctxt->buffer.bitOffset; |
| 138 |
int rshift = 8 - pctxt->buffer.bitOffset; |
| 139 |
int stat = ASN_OK; |
| 140 |
ASN1OCTET mask = 0x0; |
| 141 |
|
| 142 |
if (nbits == 0) return ASN_OK; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
if (nbits < 8) { |
| 147 |
switch (nbits) { |
| 148 |
case 1: mask = 0x80; break; |
| 149 |
case 2: mask = 0xC0; break; |
| 150 |
case 3: mask = 0xE0; break; |
| 151 |
case 4: mask = 0xF0; break; |
| 152 |
case 5: mask = 0xF8; break; |
| 153 |
case 6: mask = 0xFC; break; |
| 154 |
case 7: mask = 0xFE; break; |
| 155 |
default:; |
| 156 |
} |
| 157 |
value &= mask; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
if (pctxt->buffer.bitOffset == 8) { |
| 163 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = value; |
| 164 |
if (nbits == 8) { |
| 165 |
pctxt->buffer.byteIndex++; |
| 166 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; |
| 167 |
} |
| 168 |
else |
| 169 |
pctxt->buffer.bitOffset -= nbits; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
else { |
| 176 |
pctxt->buffer.data[pctxt->buffer.byteIndex] |= |
| 177 |
(ASN1OCTET)(value >> rshift); |
| 178 |
|
| 179 |
pctxt->buffer.bitOffset -= nbits; |
| 180 |
|
| 181 |
if (pctxt->buffer.bitOffset < 0) { |
| 182 |
pctxt->buffer.byteIndex++; |
| 183 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = |
| 184 |
(ASN1OCTET)(value << lshift); |
| 185 |
pctxt->buffer.bitOffset += 8; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
return stat; |
| 190 |
} |
| 191 |
|
| 192 |
int encodeBitString (OOCTXT* pctxt, ASN1UINT numbits, const ASN1OCTET* data) |
| 193 |
{ |
| 194 |
int enclen, octidx = 0, stat; |
| 195 |
Asn1SizeCnst* pSizeList = pctxt->pSizeConstraint; |
| 196 |
|
| 197 |
for (;;) { |
| 198 |
if ((enclen = encodeLength (pctxt, numbits)) < 0) { |
| 199 |
return LOG_ASN1ERR (pctxt, enclen); |
| 200 |
} |
| 201 |
|
| 202 |
if (enclen > 0) { |
| 203 |
ASN1BOOL doAlign; |
| 204 |
|
| 205 |
stat = bitAndOctetStringAlignmentTest |
| 206 |
(pSizeList, numbits, TRUE, &doAlign); |
| 207 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 208 |
|
| 209 |
if (doAlign) { |
| 210 |
stat = encodeByteAlign (pctxt); |
| 211 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 212 |
} |
| 213 |
|
| 214 |
stat = encodeOctets (pctxt, &data[octidx], enclen); |
| 215 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 216 |
} |
| 217 |
|
| 218 |
if (enclen < (int)numbits) { |
| 219 |
numbits -= enclen; |
| 220 |
octidx += (enclen/8); |
| 221 |
} |
| 222 |
else break; |
| 223 |
} |
| 224 |
|
| 225 |
return ASN_OK; |
| 226 |
} |
| 227 |
|
| 228 |
int encodeBMPString |
| 229 |
(OOCTXT* pctxt, ASN1BMPString value, Asn116BitCharSet* permCharSet) |
| 230 |
{ |
| 231 |
Asn116BitCharSet charSet; |
| 232 |
int stat; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
init16BitCharSet (&charSet, BMP_FIRST, BMP_LAST, BMP_ABITS, BMP_UBITS); |
| 237 |
|
| 238 |
if (permCharSet) { |
| 239 |
set16BitCharSet (pctxt, &charSet, permCharSet); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
stat = encode16BitConstrainedString (pctxt, value, &charSet); |
| 245 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 246 |
|
| 247 |
return stat; |
| 248 |
} |
| 249 |
|
| 250 |
int encodeByteAlign (OOCTXT* pctxt) |
| 251 |
{ |
| 252 |
if (pctxt->buffer.bitOffset != 8) { |
| 253 |
if ((pctxt->buffer.byteIndex + 1) >= pctxt->buffer.size) { |
| 254 |
int stat = encodeExpandBuffer (pctxt, 1); |
| 255 |
if (stat != ASN_OK) return (stat); |
| 256 |
} |
| 257 |
pctxt->buffer.byteIndex++; |
| 258 |
pctxt->buffer.bitOffset = 8; |
| 259 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; |
| 260 |
} |
| 261 |
|
| 262 |
return ASN_OK; |
| 263 |
} |
| 264 |
|
| 265 |
int encodeCheckBuffer (OOCTXT* pctxt, ASN1UINT nbytes) |
| 266 |
{ |
| 267 |
int stat = ASN_OK; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
if ( ( pctxt->buffer.byteIndex + nbytes + 1 ) >= pctxt->buffer.size ) { |
| 273 |
if ((stat = encodeExpandBuffer (pctxt, nbytes+1)) != ASN_OK) { |
| 274 |
return LOG_ASN1ERR (pctxt, stat); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
return (stat); |
| 279 |
} |
| 280 |
|
| 281 |
int encodeConsInteger |
| 282 |
(OOCTXT* pctxt, ASN1INT value, ASN1INT lower, ASN1INT upper) |
| 283 |
{ |
| 284 |
ASN1UINT range_value; |
| 285 |
ASN1UINT adjusted_value; |
| 286 |
int stat; |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
if (value < lower || value > upper) { |
| 291 |
return ASN_E_CONSVIO; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
if ((upper > 0 && lower >= 0) || (upper <= 0 && lower < 0)) { |
| 298 |
range_value = upper - lower; |
| 299 |
adjusted_value = value - lower; |
| 300 |
} |
| 301 |
else { |
| 302 |
range_value = upper + abs(lower); |
| 303 |
adjusted_value = value + abs(lower); |
| 304 |
} |
| 305 |
|
| 306 |
if (range_value != ASN1UINT_MAX) { range_value += 1; } |
| 307 |
|
| 308 |
if (range_value == 0 || lower > upper) |
| 309 |
stat = ASN_E_RANGERR; |
| 310 |
else if (lower != upper) { |
| 311 |
stat = encodeConsWholeNumber (pctxt, adjusted_value, range_value); |
| 312 |
} |
| 313 |
else |
| 314 |
stat = ASN_OK; |
| 315 |
|
| 316 |
return stat; |
| 317 |
} |
| 318 |
|
| 319 |
int encodeConsUnsigned |
| 320 |
(OOCTXT* pctxt, ASN1UINT value, ASN1UINT lower, ASN1UINT upper) |
| 321 |
{ |
| 322 |
ASN1UINT range_value; |
| 323 |
ASN1UINT adjusted_value; |
| 324 |
int stat; |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
range_value = (lower == 0 && upper == ASN1UINT_MAX) ? |
| 330 |
ASN1UINT_MAX : upper - lower + 1; |
| 331 |
|
| 332 |
adjusted_value = value - lower; |
| 333 |
|
| 334 |
if (lower != upper) { |
| 335 |
stat = encodeConsWholeNumber (pctxt, adjusted_value, range_value); |
| 336 |
} |
| 337 |
else |
| 338 |
stat = ASN_OK; |
| 339 |
|
| 340 |
return stat; |
| 341 |
} |
| 342 |
|
| 343 |
int encodeConsWholeNumber |
| 344 |
(OOCTXT* pctxt, ASN1UINT adjusted_value, ASN1UINT range_value) |
| 345 |
{ |
| 346 |
ASN1UINT nocts, range_bitcnt = getUIntBitCount (range_value - 1); |
| 347 |
int stat; |
| 348 |
|
| 349 |
if (adjusted_value >= range_value && range_value != ASN1UINT_MAX) { |
| 350 |
return LOG_ASN1ERR (pctxt, ASN_E_RANGERR); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
if (range_value <= 255) { |
| 356 |
return encodeBits (pctxt, adjusted_value, range_bitcnt); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
else if (range_value == 256) { |
| 362 |
stat = encodeByteAlign (pctxt); |
| 363 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 364 |
|
| 365 |
return encodeBits (pctxt, adjusted_value, 8); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
else if (range_value <= 65536) { |
| 371 |
stat = encodeByteAlign (pctxt); |
| 372 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 373 |
|
| 374 |
return encodeBits (pctxt, adjusted_value, 16); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
else { |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
if (adjusted_value < 256) nocts = 1; |
| 385 |
else if (adjusted_value < 65536) nocts = 2; |
| 386 |
else if (adjusted_value < 0x1000000) nocts = 3; |
| 387 |
else nocts = 4; |
| 388 |
|
| 389 |
stat = encodeBits (pctxt, nocts - 1, 2); |
| 390 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 391 |
|
| 392 |
stat = encodeByteAlign (pctxt); |
| 393 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 394 |
|
| 395 |
return encodeNonNegBinInt (pctxt, adjusted_value); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
int encodeConstrainedStringEx (OOCTXT* pctxt, |
| 400 |
const char* string, |
| 401 |
const char* charSet, |
| 402 |
ASN1UINT abits, |
| 403 |
ASN1UINT ubits, |
| 404 |
ASN1UINT canSetBits) |
| 405 |
{ |
| 406 |
ASN1UINT i, len = strlen(string); |
| 407 |
int stat; |
| 408 |
|
| 409 |
|
| 410 |
Asn1SizeCnst* psize = pctxt->pSizeConstraint; |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
stat = encodeLength (pctxt, len); |
| 415 |
if (stat < 0) return LOG_ASN1ERR (pctxt, stat); |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
if (alignCharStr (pctxt, len, abits, psize)) { |
| 420 |
stat = encodeByteAlign (pctxt); |
| 421 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
if (abits >= canSetBits && canSetBits > 4) { |
| 427 |
for (i = 0; i < len; i++) { |
| 428 |
if ((stat = encodeBits (pctxt, string[i], abits)) != ASN_OK) |
| 429 |
return LOG_ASN1ERR (pctxt, stat); |
| 430 |
} |
| 431 |
} |
| 432 |
else if (0 != charSet) { |
| 433 |
ASN1UINT nchars = strlen(charSet), pos; |
| 434 |
const char* ptr; |
| 435 |
for (i = 0; i < len; i++) { |
| 436 |
ptr = memchr (charSet, string[i], nchars); |
| 437 |
|
| 438 |
if (0 == ptr) |
| 439 |
return LOG_ASN1ERR (pctxt, ASN_E_CONSVIO); |
| 440 |
else |
| 441 |
pos = ptr - charSet; |
| 442 |
|
| 443 |
if ((stat = encodeBits (pctxt, pos, abits)) != ASN_OK) |
| 444 |
return LOG_ASN1ERR (pctxt, stat); |
| 445 |
} |
| 446 |
} |
| 447 |
else return LOG_ASN1ERR (pctxt, ASN_E_INVPARAM); |
| 448 |
|
| 449 |
return stat; |
| 450 |
} |
| 451 |
|
| 452 |
int encodeExpandBuffer (OOCTXT* pctxt, ASN1UINT nbytes) |
| 453 |
{ |
| 454 |
if (pctxt->buffer.dynamic) |
| 455 |
{ |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
pctxt->buffer.size += ASN1MAX (ASN_K_ENCBUFSIZ, nbytes); |
| 460 |
|
| 461 |
pctxt->buffer.data = (ASN1OCTET*) memHeapRealloc |
| 462 |
(&pctxt->pMsgMemHeap, pctxt->buffer.data, pctxt->buffer.size); |
| 463 |
|
| 464 |
if (!pctxt->buffer.data) return (ASN_E_NOMEM); |
| 465 |
|
| 466 |
return (ASN_OK); |
| 467 |
} |
| 468 |
|
| 469 |
return (ASN_E_BUFOVFLW); |
| 470 |
} |
| 471 |
|
| 472 |
int encodeGetMsgBitCnt (OOCTXT* pctxt) |
| 473 |
{ |
| 474 |
int numBitsInLastByte = 8 - pctxt->buffer.bitOffset; |
| 475 |
return ((pctxt->buffer.byteIndex * 8) + numBitsInLastByte); |
| 476 |
} |
| 477 |
|
| 478 |
ASN1OCTET* encodeGetMsgPtr (OOCTXT* pctxt, int* pLength) |
| 479 |
{ |
| 480 |
if (pLength) *pLength = getPERMsgLen (pctxt); |
| 481 |
return pctxt->buffer.data; |
| 482 |
} |
| 483 |
|
| 484 |
int encodeIdent (OOCTXT* pctxt, ASN1UINT ident) |
| 485 |
{ |
| 486 |
ASN1UINT mask; |
| 487 |
int nshifts = 0, stat; |
| 488 |
|
| 489 |
if (ident !=0) { |
| 490 |
ASN1UINT lv; |
| 491 |
nshifts = getIdentByteCount (ident); |
| 492 |
while (nshifts > 0) { |
| 493 |
mask = ((ASN1UINT)0x7f) << (7 * (nshifts - 1)); |
| 494 |
nshifts--; |
| 495 |
lv = (ASN1UINT)((ident & mask) >> (nshifts * 7)); |
| 496 |
if (nshifts != 0) { lv |= 0x80; } |
| 497 |
if ((stat = encodeBits (pctxt, lv, 8)) != ASN_OK) |
| 498 |
return LOG_ASN1ERR (pctxt, stat); |
| 499 |
} |
| 500 |
} |
| 501 |
else { |
| 502 |
|
| 503 |
if ((stat = encodeBits (pctxt, 0, 8)) != ASN_OK) |
| 504 |
return LOG_ASN1ERR (pctxt, stat); |
| 505 |
} |
| 506 |
|
| 507 |
return ASN_OK; |
| 508 |
} |
| 509 |
|
| 510 |
int encodeLength (OOCTXT* pctxt, ASN1UINT value) |
| 511 |
{ |
| 512 |
ASN1BOOL extendable; |
| 513 |
Asn1SizeCnst* pSize = |
| 514 |
checkSize (pctxt->pSizeConstraint, value, &extendable); |
| 515 |
ASN1UINT lower = (pSize) ? pSize->lower : 0; |
| 516 |
ASN1UINT upper = (pSize) ? pSize->upper : ASN1UINT_MAX; |
| 517 |
int enclen, stat; |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
if (pctxt->pSizeConstraint && !pSize) |
| 524 |
return LOG_ASN1ERR (pctxt, ASN_E_CONSVIO); |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
pctxt->pSizeConstraint = 0; |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
if (extendable) { |
| 534 |
stat = (pSize) ? |
| 535 |
encodeBit (pctxt, pSize->extended) : encodeBit (pctxt, 1); |
| 536 |
|
| 537 |
if (stat != ASN_OK) return (stat); |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
if (upper < 65536) { |
| 543 |
stat = (lower == upper) ? ASN_OK : |
| 544 |
encodeConsWholeNumber (pctxt, value - lower, upper - lower + 1); |
| 545 |
enclen = (stat == ASN_OK) ? value : stat; |
| 546 |
} |
| 547 |
else { |
| 548 |
|
| 549 |
enclen = encodeUnconsLength (pctxt, value); |
| 550 |
} |
| 551 |
|
| 552 |
return enclen; |
| 553 |
|
| 554 |
} |
| 555 |
|
| 556 |
int encodeObjectIdentifier (OOCTXT* pctxt, ASN1OBJID* pvalue) |
| 557 |
{ |
| 558 |
int len, stat; |
| 559 |
ASN1UINT temp; |
| 560 |
register int numids, i; |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
len = 1; |
| 565 |
numids = pvalue->numids; |
| 566 |
for (i = 2; i < numids; i++) { |
| 567 |
len += getIdentByteCount (pvalue->subid[i]); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
if ((stat = encodeLength (pctxt, (ASN1UINT)len)) < 0) { |
| 573 |
return LOG_ASN1ERR (pctxt, stat); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
if (0 == pvalue) return LOG_ASN1ERR (pctxt, ASN_E_INVOBJID); |
| 579 |
if (numids < 2) return LOG_ASN1ERR (pctxt, ASN_E_INVOBJID); |
| 580 |
if (pvalue->subid[0] > 2) return LOG_ASN1ERR (pctxt, ASN_E_INVOBJID); |
| 581 |
if (pvalue->subid[0] != 2 && pvalue->subid[1] > 39) |
| 582 |
return LOG_ASN1ERR (pctxt, ASN_E_INVOBJID); |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
temp = ((pvalue->subid[0] * 40) + pvalue->subid[1]); |
| 589 |
if ((stat = encodeIdent (pctxt, temp)) != ASN_OK) |
| 590 |
return LOG_ASN1ERR (pctxt, stat); |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
for (i = 2; i < numids; i++) { |
| 595 |
if ((stat = encodeIdent (pctxt, pvalue->subid[i])) != ASN_OK) |
| 596 |
return LOG_ASN1ERR (pctxt, stat); |
| 597 |
} |
| 598 |
|
| 599 |
return ASN_OK; |
| 600 |
} |
| 601 |
|
| 602 |
int encodebitsFromOctet (OOCTXT* pctxt, ASN1OCTET value, ASN1UINT nbits) |
| 603 |
{ |
| 604 |
int lshift = pctxt->buffer.bitOffset; |
| 605 |
int rshift = 8 - pctxt->buffer.bitOffset; |
| 606 |
int stat = ASN_OK; |
| 607 |
ASN1OCTET mask = 0x0; |
| 608 |
|
| 609 |
if (nbits == 0) return ASN_OK; |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
if (nbits < 8) { |
| 614 |
switch (nbits) { |
| 615 |
case 1: mask = 0x80; break; |
| 616 |
case 2: mask = 0xC0; break; |
| 617 |
case 3: mask = 0xE0; break; |
| 618 |
case 4: mask = 0xF0; break; |
| 619 |
case 5: mask = 0xF8; break; |
| 620 |
case 6: mask = 0xFC; break; |
| 621 |
case 7: mask = 0xFE; break; |
| 622 |
default:; |
| 623 |
} |
| 624 |
value &= mask; |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
if (pctxt->buffer.bitOffset == 8) { |
| 630 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = value; |
| 631 |
if (nbits == 8) { |
| 632 |
pctxt->buffer.byteIndex++; |
| 633 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; |
| 634 |
} |
| 635 |
else |
| 636 |
pctxt->buffer.bitOffset -= nbits; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
else { |
| 643 |
pctxt->buffer.data[pctxt->buffer.byteIndex] |= |
| 644 |
(ASN1OCTET)(value >> rshift); |
| 645 |
|
| 646 |
pctxt->buffer.bitOffset -= nbits; |
| 647 |
|
| 648 |
if (pctxt->buffer.bitOffset < 0) { |
| 649 |
pctxt->buffer.byteIndex++; |
| 650 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = |
| 651 |
(ASN1OCTET)(value << lshift); |
| 652 |
pctxt->buffer.bitOffset += 8; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
return stat; |
| 657 |
} |
| 658 |
|
| 659 |
int encodeOctets (OOCTXT* pctxt, const ASN1OCTET* pvalue, ASN1UINT nbits) |
| 660 |
{ |
| 661 |
int i = 0, stat; |
| 662 |
int numFullOcts = nbits / 8; |
| 663 |
|
| 664 |
if (nbits == 0) return 0; |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
stat = encodeCheckBuffer (pctxt, numFullOcts + 1); |
| 669 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 670 |
|
| 671 |
if (numFullOcts > 0) { |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
if (pctxt->buffer.bitOffset == 8) { |
| 678 |
memcpy (&pctxt->buffer.data[pctxt->buffer.byteIndex], pvalue, |
| 679 |
numFullOcts); |
| 680 |
pctxt->buffer.byteIndex += numFullOcts; |
| 681 |
pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; |
| 682 |
i = numFullOcts; |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
else { |
| 688 |
for (i = 0; i < numFullOcts; i++) { |
| 689 |
stat = encodeBitsFromOctet (pctxt, pvalue[i], 8); |
| 690 |
if (stat != ASN_OK) return stat; |
| 691 |
} |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
if (nbits % 8 != 0) { |
| 698 |
stat = encodeBitsFromOctet (pctxt, pvalue[i], nbits % 8); |
| 699 |
} |
| 700 |
|
| 701 |
return stat; |
| 702 |
} |
| 703 |
|
| 704 |
int encodeOctetString (OOCTXT* pctxt, ASN1UINT numocts, const ASN1OCTET* data) |
| 705 |
{ |
| 706 |
int enclen, octidx = 0, stat; |
| 707 |
Asn1SizeCnst* pSizeList = pctxt->pSizeConstraint; |
| 708 |
|
| 709 |
for (;;) { |
| 710 |
if ((enclen = encodeLength (pctxt, numocts)) < 0) { |
| 711 |
return LOG_ASN1ERR (pctxt, enclen); |
| 712 |
} |
| 713 |
|
| 714 |
if (enclen > 0) { |
| 715 |
ASN1BOOL doAlign; |
| 716 |
|
| 717 |
stat = bitAndOctetStringAlignmentTest |
| 718 |
(pSizeList, numocts, FALSE, &doAlign); |
| 719 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 720 |
|
| 721 |
if (doAlign) { |
| 722 |
stat = encodeByteAlign (pctxt); |
| 723 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 724 |
} |
| 725 |
|
| 726 |
stat = encodeOctets (pctxt, &data[octidx], enclen * 8); |
| 727 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 728 |
} |
| 729 |
|
| 730 |
if (enclen < (int)numocts) { |
| 731 |
numocts -= enclen; |
| 732 |
octidx += enclen; |
| 733 |
} |
| 734 |
else break; |
| 735 |
} |
| 736 |
|
| 737 |
return ASN_OK; |
| 738 |
} |
| 739 |
|
| 740 |
int encodeOpenType (OOCTXT* pctxt, ASN1UINT numocts, const ASN1OCTET* data) |
| 741 |
{ |
| 742 |
int enclen, octidx = 0, stat; |
| 743 |
ASN1OCTET zeroByte = 0x00; |
| 744 |
ASN1OpenType openType; |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
if (numocts == 0) { |
| 749 |
openType.numocts = 1; |
| 750 |
openType.data = &zeroByte; |
| 751 |
} |
| 752 |
else { |
| 753 |
openType.numocts = numocts; |
| 754 |
openType.data = data; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
for (;;) { |
| 760 |
if ((enclen = encodeLength (pctxt, openType.numocts)) < 0) { |
| 761 |
return LOG_ASN1ERR (pctxt, enclen); |
| 762 |
} |
| 763 |
|
| 764 |
stat = encodeByteAlign (pctxt); |
| 765 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 766 |
|
| 767 |
stat = encodeOctets (pctxt, &openType.data[octidx], enclen * 8); |
| 768 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 769 |
|
| 770 |
if (enclen < (int)openType.numocts) { |
| 771 |
openType.numocts -= enclen; |
| 772 |
octidx += enclen; |
| 773 |
} |
| 774 |
else break; |
| 775 |
} |
| 776 |
|
| 777 |
return ASN_OK; |
| 778 |
} |
| 779 |
|
| 780 |
int encodeOpenTypeExt (OOCTXT* pctxt, DList* pElemList) |
| 781 |
{ |
| 782 |
DListNode* pnode; |
| 783 |
ASN1OpenType* pOpenType; |
| 784 |
int stat; |
| 785 |
|
| 786 |
if (0 != pElemList) { |
| 787 |
pnode = pElemList->head; |
| 788 |
while (0 != pnode) { |
| 789 |
if (0 != pnode->data) { |
| 790 |
pOpenType = (ASN1OpenType*)pnode->data; |
| 791 |
|
| 792 |
if (pOpenType->numocts > 0) { |
| 793 |
stat = encodeByteAlign (pctxt); |
| 794 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 795 |
|
| 796 |
stat = encodeOpenType |
| 797 |
(pctxt, pOpenType->numocts, pOpenType->data); |
| 798 |
|
| 799 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 800 |
} |
| 801 |
} |
| 802 |
pnode = pnode->next; |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
return ASN_OK; |
| 807 |
} |
| 808 |
|
| 809 |
int encodeOpenTypeExtBits (OOCTXT* pctxt, DList* pElemList) |
| 810 |
{ |
| 811 |
DListNode* pnode; |
| 812 |
int stat; |
| 813 |
|
| 814 |
if (0 != pElemList) { |
| 815 |
pnode = pElemList->head; |
| 816 |
|
| 817 |
while (0 != pnode) { |
| 818 |
stat = encodeBit (pctxt, (ASN1BOOL)(0 != pnode->data)); |
| 819 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 820 |
|
| 821 |
pnode = pnode->next; |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
return ASN_OK; |
| 826 |
} |
| 827 |
|
| 828 |
int encodeSemiConsInteger (OOCTXT* pctxt, ASN1INT value, ASN1INT lower) |
| 829 |
{ |
| 830 |
int nbytes, stat; |
| 831 |
int shift = ((sizeof(value) - 1) * 8) - 1; |
| 832 |
ASN1UINT tempValue; |
| 833 |
|
| 834 |
if (lower > ASN1INT_MIN) |
| 835 |
value -= lower; |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
for ( ; shift > 0; shift -= 8) { |
| 840 |
tempValue = (value >> shift) & 0x1ff; |
| 841 |
if (tempValue == 0 || tempValue == 0x1ff) continue; |
| 842 |
else break; |
| 843 |
} |
| 844 |
|
| 845 |
nbytes = (shift + 9) / 8; |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
if ((stat = encodeLength (pctxt, nbytes)) < 0) { |
| 850 |
return stat; |
| 851 |
} |
| 852 |
|
| 853 |
if ((stat = encodeByteAlign (pctxt)) != ASN_OK) |
| 854 |
return stat; |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
stat = encode2sCompBinInt (pctxt, value); |
| 859 |
|
| 860 |
return stat; |
| 861 |
} |
| 862 |
|
| 863 |
int encodeSemiConsUnsigned (OOCTXT* pctxt, ASN1UINT value, ASN1UINT lower) |
| 864 |
{ |
| 865 |
int nbytes, stat; |
| 866 |
int shift = ((sizeof(value) - 1) * 8) - 1; |
| 867 |
ASN1UINT mask = 1UL << ((sizeof(value) * 8) - 1); |
| 868 |
ASN1UINT tempValue; |
| 869 |
|
| 870 |
value -= lower; |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
for ( ; shift > 0; shift -= 8) { |
| 875 |
tempValue = (value >> shift) & 0x1ff; |
| 876 |
|
| 877 |
if (tempValue == 0) continue; |
| 878 |
else break; |
| 879 |
} |
| 880 |
|
| 881 |
nbytes = (shift + 9) / 8; |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
if ((value & mask) != 0) nbytes++; |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
if ((stat = encodeLength (pctxt, nbytes)) < 0) { |
| 890 |
return stat; |
| 891 |
} |
| 892 |
|
| 893 |
if ((stat = encodeByteAlign (pctxt)) != ASN_OK) |
| 894 |
return stat; |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
if (nbytes > sizeof(value)) { |
| 899 |
stat = encodebitsFromOctet (pctxt, 0, 8); |
| 900 |
if (stat != ASN_OK) return (stat); |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
stat = encodeNonNegBinInt (pctxt, value); |
| 906 |
|
| 907 |
return stat; |
| 908 |
} |
| 909 |
|
| 910 |
int encodeSmallNonNegWholeNumber (OOCTXT* pctxt, ASN1UINT value) |
| 911 |
{ |
| 912 |
int stat; |
| 913 |
|
| 914 |
if (value < 64) { |
| 915 |
stat = encodeBits (pctxt, value, 7); |
| 916 |
} |
| 917 |
else { |
| 918 |
ASN1UINT len; |
| 919 |
|
| 920 |
|
| 921 |
if (value < 256) len = 1; |
| 922 |
else if (value < 65536) len = 2; |
| 923 |
else if (value < 0x1000000) len = 3; |
| 924 |
else len = 4; |
| 925 |
|
| 926 |
stat = encodeBits (pctxt, len, 8); |
| 927 |
|
| 928 |
|
| 929 |
if (stat == ASN_OK) { |
| 930 |
if ((stat = encodeByteAlign (pctxt)) == ASN_OK) { |
| 931 |
stat = encodeBits (pctxt, value, len*8); |
| 932 |
} |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
return stat; |
| 937 |
} |
| 938 |
|
| 939 |
int encodeVarWidthCharString (OOCTXT* pctxt, const char* value) |
| 940 |
{ |
| 941 |
int stat; |
| 942 |
ASN1UINT len = strlen (value); |
| 943 |
|
| 944 |
|
| 945 |
Asn1SizeCnst* psize = pctxt->pSizeConstraint; |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
stat = encodeLength (pctxt, len); |
| 950 |
if (stat < 0) return LOG_ASN1ERR (pctxt, stat); |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
if (alignCharStr (pctxt, len, 8, psize)) { |
| 955 |
stat = encodeByteAlign (pctxt); |
| 956 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
stat = encodeOctets (pctxt, (const ASN1OCTET*)value, len * 8); |
| 962 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 963 |
|
| 964 |
return ASN_OK; |
| 965 |
} |
| 966 |
|
| 967 |
static int encode16BitConstrainedString |
| 968 |
(OOCTXT* pctxt, Asn116BitCharString value, Asn116BitCharSet* pCharSet) |
| 969 |
{ |
| 970 |
ASN1UINT i, pos; |
| 971 |
ASN1UINT nbits = pCharSet->alignedBits; |
| 972 |
int stat; |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
stat = encodeLength (pctxt, value.nchars); |
| 977 |
if (stat < 0) return LOG_ASN1ERR (pctxt, stat); |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
stat = encodeByteAlign (pctxt); |
| 982 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
for (i = 0; i < value.nchars; i++) { |
| 987 |
if (pCharSet->charSet.data == 0) { |
| 988 |
stat = encodeBits |
| 989 |
(pctxt, value.data[i] - pCharSet->firstChar, nbits); |
| 990 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 991 |
} |
| 992 |
else { |
| 993 |
for (pos = 0; pos < pCharSet->charSet.nchars; pos++) { |
| 994 |
if (value.data[i] == pCharSet->charSet.data[pos]) { |
| 995 |
stat = encodeBits (pctxt, pos, nbits); |
| 996 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 997 |
break; |
| 998 |
} |
| 999 |
} |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
return stat; |
| 1004 |
} |
| 1005 |
|
| 1006 |
int encode2sCompBinInt (OOCTXT* pctxt, ASN1INT value) |
| 1007 |
{ |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
ASN1OCTET lbuf[8], lb; |
| 1016 |
ASN1INT i = sizeof(lbuf), temp = value; |
| 1017 |
|
| 1018 |
memset (lbuf, 0, sizeof(lbuf)); |
| 1019 |
do { |
| 1020 |
lb = temp % 256; |
| 1021 |
temp /= 256; |
| 1022 |
if (temp < 0 && lb != 0) temp--; |
| 1023 |
lbuf[--i] = lb; |
| 1024 |
} while (temp != 0 && temp != -1); |
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
if (value > 0 && (lb & 0x80) != 0) { |
| 1030 |
i--; |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
else if (value < 0 && ((lb & 0x80) == 0)) { |
| 1038 |
lbuf[--i] = 0xff; |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
return encodeOctets (pctxt, &lbuf[i], (sizeof(lbuf) - i) * 8); |
| 1044 |
} |
| 1045 |
|
| 1046 |
static int encodeNonNegBinInt (OOCTXT* pctxt, ASN1UINT value) |
| 1047 |
{ |
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
ASN1UINT bitcnt = (value == 0) ? 1 : getUIntBitCount (value); |
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
bitcnt = (bitcnt + 7) & (~7); |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
return encodeBits (pctxt, value, bitcnt); |
| 1064 |
} |
| 1065 |
|
| 1066 |
static int encodeUnconsLength (OOCTXT* pctxt, ASN1UINT value) |
| 1067 |
{ |
| 1068 |
int enclen, stat; |
| 1069 |
|
| 1070 |
stat = encodeByteAlign (pctxt); |
| 1071 |
if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); |
| 1072 |
|
| 1073 |
|
| 1074 |
if (value < 128) { |
| 1075 |
stat = encodeBits (pctxt, value, 8); |
| 1076 |
enclen = (stat == ASN_OK) ? value : stat; |
| 1077 |
} |
| 1078 |
|
| 1079 |
else if (value < 16384) { |
| 1080 |
if ((stat = encodeBit (pctxt, 1)) == ASN_OK) |
| 1081 |
stat = encodeBits (pctxt, value, 15); |
| 1082 |
enclen = (stat == ASN_OK) ? value : stat; |
| 1083 |
} |
| 1084 |
|
| 1085 |
else { |
| 1086 |
int multiplier = ASN1MIN (value/16384, 4); |
| 1087 |
encodeBit (pctxt, 1); |
| 1088 |
encodeBit (pctxt, 1); |
| 1089 |
stat = encodeBits (pctxt, multiplier, 6); |
| 1090 |
enclen = (stat == ASN_OK) ? 16384 * multiplier : stat; |
| 1091 |
} |
| 1092 |
|
| 1093 |
return enclen; |
| 1094 |
} |
| 1095 |
|
| 1096 |
static int getIdentByteCount (ASN1UINT ident) |
| 1097 |
{ |
| 1098 |
if (ident < (1u << 7)) { |
| 1099 |
return 1; |
| 1100 |
} |
| 1101 |
else if (ident < (1u << 14)) { |
| 1102 |
return 2; |
| 1103 |
} |
| 1104 |
else if (ident < (1u << 21)) { |
| 1105 |
return 3; |
| 1106 |
} |
| 1107 |
else if (ident < (1u << 28)) { |
| 1108 |
return 4; |
| 1109 |
} |
| 1110 |
return 5; |
| 1111 |
} |