class_tag_map2.diff

Martin Bosslet, 01/24/2011 10:26 AM

Download (7.59 KB)

View differences:

ruby/ext/openssl/ossl_asn1.c (working copy)
454 454
} ossl_asn1_info_t;
455 455

  
456 456
static ossl_asn1_info_t ossl_asn1_info[] = {
457
    { "EOC",               &cASN1EndOfContent,    },  /*  0 */
457
    { "END_OF_CONTENT",    &cASN1EndOfContent,    },  /*  0 */
458 458
    { "BOOLEAN",           &cASN1Boolean,         },  /*  1 */
459 459
    { "INTEGER",           &cASN1Integer,         },  /*  2 */
460 460
    { "BIT_STRING",        &cASN1BitString,       },  /*  3 */
......
487 487
    { "BMPSTRING",         &cASN1BMPString,       },  /* 30 */
488 488
};
489 489

  
490

  
491

  
490 492
int ossl_asn1_info_size = (sizeof(ossl_asn1_info)/sizeof(ossl_asn1_info[0]));
491 493

  
494
static VALUE class_tag_map;
495

  
492 496
static int ossl_asn1_default_tag(VALUE obj);
493 497

  
498
static VALUE
499
ossl_asn1_default_tag_class(VALUE self, VALUE klass)
500
{
501
    VALUE tag = rb_hash_lookup(class_tag_map, klass);
502
    if (tag != Qnil) {
503
        return tag;
504
    }
505

  
506
    ossl_raise(eASN1Error, "universal tag for %s not found",
507
	       rb_class2name(klass));
508

  
509
    return Qnil; /* dummy */
510
}
511

  
512
static VALUE
513
ossl_asn1_default_tag_public(VALUE self, VALUE obj)
514
{
515
  return INT2NUM(ossl_asn1_default_tag(obj));
516
}
517

  
494 518
ASN1_TYPE*
495 519
ossl_asn1_get_asn1type(VALUE obj)
496 520
{
......
570 594
static int
571 595
ossl_asn1_default_tag(VALUE obj)
572 596
{
573
    int i;
597
    VALUE tmp_class = CLASS_OF(obj);
598
    while (tmp_class) {
599
        VALUE tag = rb_hash_lookup(class_tag_map, tmp_class);
600
        if (tag != Qnil) {
601
            return NUM2INT(tag);
602
        }
603
        tmp_class = RCLASS_SUPER(tmp_class);
604
    }
574 605

  
575
    for(i = 0; i < ossl_asn1_info_size; i++){
576
	if(ossl_asn1_info[i].klass &&
577
	   rb_obj_is_kind_of(obj, *ossl_asn1_info[i].klass)){
578
	    return i;
579
	}
580
    }
581 606
    ossl_raise(eASN1Error, "universal tag for %s not found",
582 607
	       rb_class2name(CLASS_OF(obj)));
583 608

  
......
841 866
            ossl_asn1_set_infinite_length(asn1data, Qtrue);
842 867
        else
843 868
            ossl_asn1_set_infinite_length(asn1data, Qfalse);
844
        
869

  
845 870
	rb_ary_push(ary, asn1data);
846 871
	length -= len;
847 872
        if(once) break;
......
1190 1215
    rb_define_module_function(mASN1, "traverse", ossl_asn1_traverse, 1);
1191 1216
    rb_define_module_function(mASN1, "decode", ossl_asn1_decode, 1);
1192 1217
    rb_define_module_function(mASN1, "decode_all", ossl_asn1_decode_all, 1);
1218
    rb_define_module_function(mASN1, "default_tag_of_class", ossl_asn1_default_tag_class, 1);
1219
    rb_define_module_function(mASN1, "default_tag", ossl_asn1_default_tag_public, 1);
1193 1220
    ary = rb_ary_new();
1194 1221
    rb_define_const(mASN1, "UNIVERSAL_TAG_NAME", ary);
1195 1222
    for(i = 0; i < ossl_asn1_info_size; i++){
......
1260 1287
    rb_attr(cASN1BitString, rb_intern("unused_bits"), 1, 1, 0);
1261 1288

  
1262 1289
    rb_define_method(cASN1EndOfContent, "initialize", ossl_asn1eoc_initialize, 0);
1290

  
1291
    class_tag_map = rb_hash_new();
1292
    rb_hash_aset(class_tag_map, cASN1EndOfContent, INT2NUM(0));
1293
    rb_hash_aset(class_tag_map, cASN1Boolean, INT2NUM(1));
1294
    rb_hash_aset(class_tag_map, cASN1Integer, INT2NUM(2));
1295
    rb_hash_aset(class_tag_map, cASN1BitString, INT2NUM(3));
1296
    rb_hash_aset(class_tag_map, cASN1OctetString, INT2NUM(4));
1297
    rb_hash_aset(class_tag_map, cASN1Null, INT2NUM(5));
1298
    rb_hash_aset(class_tag_map, cASN1ObjectId, INT2NUM(6));
1299
    rb_hash_aset(class_tag_map, cASN1Enumerated, INT2NUM(10));
1300
    rb_hash_aset(class_tag_map, cASN1UTF8String, INT2NUM(12));
1301
    rb_hash_aset(class_tag_map, cASN1Sequence, INT2NUM(16));
1302
    rb_hash_aset(class_tag_map, cASN1Set, INT2NUM(17));
1303
    rb_hash_aset(class_tag_map, cASN1NumericString, INT2NUM(18));
1304
    rb_hash_aset(class_tag_map, cASN1PrintableString, INT2NUM(19));
1305
    rb_hash_aset(class_tag_map, cASN1T61String, INT2NUM(20));
1306
    rb_hash_aset(class_tag_map, cASN1VideotexString, INT2NUM(21));
1307
    rb_hash_aset(class_tag_map, cASN1IA5String, INT2NUM(22));
1308
    rb_hash_aset(class_tag_map, cASN1UTCTime, INT2NUM(23));
1309
    rb_hash_aset(class_tag_map, cASN1GeneralizedTime, INT2NUM(24));
1310
    rb_hash_aset(class_tag_map, cASN1GraphicString, INT2NUM(25));
1311
    rb_hash_aset(class_tag_map, cASN1ISO64String, INT2NUM(26));
1312
    rb_hash_aset(class_tag_map, cASN1GeneralString, INT2NUM(27));
1313
    rb_hash_aset(class_tag_map, cASN1UniversalString, INT2NUM(28));
1314
    rb_hash_aset(class_tag_map, cASN1BMPString, INT2NUM(30));
1315

  
1316
    rb_define_const(mASN1, "CLASS_TAG_MAP", class_tag_map);
1263 1317
}
ruby/test/openssl/test_asn1.rb (working copy)
430 430
    end
431 431
  end
432 432
  
433
  def test_default_tag_of_class
434
    OpenSSL::ASN1::CLASS_TAG_MAP.each do |klass, tag|
435
      assert_equal(tag, OpenSSL::ASN1.default_tag_of_class(klass))
436
    end
437
  end
438
  
439
  def test_default_tag
440
    assert_equal(0, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::EndOfContent.new))    
441
    assert_equal(1, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::Boolean.new(true)))
442
    assert_equal(2, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::Integer.new(1)))
443
    assert_equal(3, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::BitString.new("\x01")))
444
    assert_equal(4, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::OctetString.new("\x01")))    
445
    assert_equal(5, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::Null.new(nil)))    
446
    assert_equal(6, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::ObjectId.new("1.2.3.4.5")))    
447
    assert_equal(10, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::Enumerated.new(1)))    
448
    assert_equal(12, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::UTF8String.new("a"))) 
449
    assert_equal(16, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::Sequence.new([])))    
450
    assert_equal(17, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::Set.new([])))    
451
    assert_equal(18, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::NumericString.new("a")))
452
    assert_equal(19, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::PrintableString.new("a")))
453
    assert_equal(20, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::T61String.new("a")))
454
    assert_equal(21, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::VideotexString.new("a")))
455
    assert_equal(22, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::IA5String.new("a")))
456
    assert_equal(23, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::UTCTime.new(Time.now)))
457
    assert_equal(24, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::GeneralizedTime.new(Time.now)))
458
    assert_equal(25, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::GraphicString.new("a")))
459
    assert_equal(26, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::ISO64String.new("a")))
460
    assert_equal(27, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::GeneralString.new("a")))
461
    assert_equal(28, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::UniversalString.new("a")))
462
    assert_equal(30, OpenSSL::ASN1.default_tag(OpenSSL::ASN1::BMPString.new("a")))
463
  end
464
  
465
  def test_default_tag_sub_class
466
    asn1_class = Class.new(OpenSSL::ASN1::Integer)
467
    asn1 = asn1_class.new(8)
468
    assert_equal(2, OpenSSL::ASN1.default_tag(asn1))
469
  end
470
  
471
  def test_default_tag_of_class_negative
472
    assert_raises(OpenSSL::ASN1::ASN1Error) do
473
      OpenSSL::ASN1.default_tag_of_class(Array)
474
    end
475
  end
476
    
477
  def test_default_tag_negative
478
    assert_raises(OpenSSL::ASN1::ASN1Error) do
479
      OpenSSL::ASN1.default_tag(OpenSSL::ASN1::ASN1Data.new(1, 2, nil))
480
    end
481
  end
482
  
433 483
end if defined?(OpenSSL)