Ruby
2.0.0p247(2013-06-27revision41674)
|
00001 /* 00002 * $Id: ossl_bio.c 33553 2011-10-29 11:02:32Z akr $ 00003 * 'OpenSSL for Ruby' team members 00004 * Copyright (C) 2003 00005 * All rights reserved. 00006 */ 00007 /* 00008 * This program is licenced under the same licence as Ruby. 00009 * (See the file 'LICENCE'.) 00010 */ 00011 #include "ossl.h" 00012 #ifdef HAVE_UNISTD_H 00013 #include <unistd.h> 00014 #endif 00015 00016 BIO * 00017 ossl_obj2bio(VALUE obj) 00018 { 00019 BIO *bio; 00020 00021 if (TYPE(obj) == T_FILE) { 00022 rb_io_t *fptr; 00023 FILE *fp; 00024 int fd; 00025 00026 GetOpenFile(obj, fptr); 00027 rb_io_check_readable(fptr); 00028 if ((fd = rb_cloexec_dup(FPTR_TO_FD(fptr))) < 0){ 00029 rb_sys_fail(0); 00030 } 00031 rb_update_max_fd(fd); 00032 if (!(fp = fdopen(fd, "r"))){ 00033 close(fd); 00034 rb_sys_fail(0); 00035 } 00036 if (!(bio = BIO_new_fp(fp, BIO_CLOSE))){ 00037 fclose(fp); 00038 ossl_raise(eOSSLError, NULL); 00039 } 00040 } 00041 else { 00042 StringValue(obj); 00043 bio = BIO_new_mem_buf(RSTRING_PTR(obj), RSTRING_LENINT(obj)); 00044 if (!bio) ossl_raise(eOSSLError, NULL); 00045 } 00046 00047 return bio; 00048 } 00049 00050 BIO * 00051 ossl_protect_obj2bio(VALUE obj, int *status) 00052 { 00053 BIO *ret = NULL; 00054 ret = (BIO*)rb_protect((VALUE(*)_((VALUE)))ossl_obj2bio, obj, status); 00055 return ret; 00056 } 00057 00058 VALUE 00059 ossl_membio2str0(BIO *bio) 00060 { 00061 VALUE ret; 00062 BUF_MEM *buf; 00063 00064 BIO_get_mem_ptr(bio, &buf); 00065 ret = rb_str_new(buf->data, buf->length); 00066 00067 return ret; 00068 } 00069 00070 VALUE 00071 ossl_protect_membio2str(BIO *bio, int *status) 00072 { 00073 return rb_protect((VALUE(*)_((VALUE)))ossl_membio2str0, (VALUE)bio, status); 00074 } 00075 00076 VALUE 00077 ossl_membio2str(BIO *bio) 00078 { 00079 VALUE ret; 00080 int status = 0; 00081 00082 ret = ossl_protect_membio2str(bio, &status); 00083 BIO_free(bio); 00084 if(status) rb_jump_tag(status); 00085 00086 return ret; 00087 } 00088