diff options
| author | William Ahern <william@Williams-MacBook-Air.local> | 2012-10-06 19:33:09 -0700 |
|---|---|---|
| committer | William Ahern <william@Williams-MacBook-Air.local> | 2012-10-06 19:33:09 -0700 |
| commit | ee4377fd20153de610f77bc2a355bba4834c1097 (patch) | |
| tree | 0d68a8a057b90efb9a7d6e7e0116eef999d08c02 | |
| parent | 7b0c50d597493f0baf5f1384bedbecf130610fce (diff) | |
| download | luaossl-ee4377fd20153de610f77bc2a355bba4834c1097.tar.gz luaossl-ee4377fd20153de610f77bc2a355bba4834c1097.tar.bz2 luaossl-ee4377fd20153de610f77bc2a355bba4834c1097.zip | |
-n
add __pairs iterator to X509_NAME binding
| -rw-r--r-- | openssl.c | 52 |
1 files changed, 52 insertions, 0 deletions
| @@ -659,6 +659,57 @@ static int xn_all(lua_State *L) { | |||
| 659 | } /* xn_all() */ | 659 | } /* xn_all() */ |
| 660 | 660 | ||
| 661 | 661 | ||
| 662 | static int xn__next(lua_State *L) { | ||
| 663 | X509_NAME *name = checksimple(L, lua_upvalueindex(1), X509_NAME_CLASS); | ||
| 664 | X509_NAME_ENTRY *entry; | ||
| 665 | ASN1_OBJECT *obj; | ||
| 666 | const char *id; | ||
| 667 | char txt[256]; | ||
| 668 | int i, n, nid, len; | ||
| 669 | |||
| 670 | lua_settop(L, 0); | ||
| 671 | |||
| 672 | i = lua_tointeger(L, lua_upvalueindex(2)); | ||
| 673 | n = X509_NAME_entry_count(name); | ||
| 674 | |||
| 675 | while (i < n) { | ||
| 676 | if (!(entry = X509_NAME_get_entry(name, i++))) | ||
| 677 | continue; | ||
| 678 | |||
| 679 | obj = X509_NAME_ENTRY_get_object(entry); | ||
| 680 | nid = OBJ_obj2nid(obj); | ||
| 681 | |||
| 682 | if (nid != NID_undef && ((id = OBJ_nid2sn(nid)) || (id = OBJ_nid2ln(nid)))) { | ||
| 683 | lua_pushstring(L, id); | ||
| 684 | } else { | ||
| 685 | if (0 > (len = OBJ_obj2txt(txt, sizeof txt, obj, 1))) | ||
| 686 | return throwssl(L, "x509.name:__pairs"); | ||
| 687 | |||
| 688 | lua_pushlstring(L, txt, len); | ||
| 689 | } | ||
| 690 | |||
| 691 | len = ASN1_STRING_length(X509_NAME_ENTRY_get_data(entry)); | ||
| 692 | lua_pushlstring(L, (char *)ASN1_STRING_data(X509_NAME_ENTRY_get_data(entry)), len); | ||
| 693 | |||
| 694 | break; | ||
| 695 | } | ||
| 696 | |||
| 697 | lua_pushinteger(L, i); | ||
| 698 | lua_replace(L, lua_upvalueindex(2)); | ||
| 699 | |||
| 700 | return lua_gettop(L); | ||
| 701 | } /* xn__next() */ | ||
| 702 | |||
| 703 | static int xn__pairs(lua_State *L) { | ||
| 704 | lua_settop(L, 1); | ||
| 705 | lua_pushinteger(L, 0); | ||
| 706 | |||
| 707 | lua_pushcclosure(L, &xn__next, 2); | ||
| 708 | |||
| 709 | return 1; | ||
| 710 | } /* xn__pairs() */ | ||
| 711 | |||
| 712 | |||
| 662 | static int xn__gc(lua_State *L) { | 713 | static int xn__gc(lua_State *L) { |
| 663 | X509_NAME **ud = luaL_checkudata(L, 1, X509_NAME_CLASS); | 714 | X509_NAME **ud = luaL_checkudata(L, 1, X509_NAME_CLASS); |
| 664 | 715 | ||
| @@ -689,6 +740,7 @@ static const luaL_Reg xn_methods[] = { | |||
| 689 | }; | 740 | }; |
| 690 | 741 | ||
| 691 | static const luaL_Reg xn_metatable[] = { | 742 | static const luaL_Reg xn_metatable[] = { |
| 743 | { "__pairs", &xn__pairs }, | ||
| 692 | { "__gc", &xn__gc }, | 744 | { "__gc", &xn__gc }, |
| 693 | { "__tostring", &xn__tostring }, | 745 | { "__tostring", &xn__tostring }, |
| 694 | { NULL, NULL }, | 746 | { NULL, NULL }, |
