|
Ruby 1.9.2p290(2011-07-09revision32553)
|
00001 /* 00002 * $Id: ossl_bio.c 27437 2010-04-22 08:04:13Z nobu $ 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 = dup(FPTR_TO_FD(fptr))) < 0){ 00029 rb_sys_fail(0); 00030 } 00031 if (!(fp = fdopen(fd, "r"))){ 00032 close(fd); 00033 rb_sys_fail(0); 00034 } 00035 if (!(bio = BIO_new_fp(fp, BIO_CLOSE))){ 00036 fclose(fp); 00037 ossl_raise(eOSSLError, NULL); 00038 } 00039 } 00040 else { 00041 StringValue(obj); 00042 bio = BIO_new_mem_buf(RSTRING_PTR(obj), RSTRING_LEN(obj)); 00043 if (!bio) ossl_raise(eOSSLError, NULL); 00044 } 00045 00046 return bio; 00047 } 00048 00049 BIO * 00050 ossl_protect_obj2bio(VALUE obj, int *status) 00051 { 00052 BIO *ret = NULL; 00053 ret = (BIO*)rb_protect((VALUE(*)_((VALUE)))ossl_obj2bio, obj, status); 00054 return ret; 00055 } 00056 00057 VALUE 00058 ossl_membio2str0(BIO *bio) 00059 { 00060 VALUE ret; 00061 BUF_MEM *buf; 00062 00063 BIO_get_mem_ptr(bio, &buf); 00064 ret = rb_str_new(buf->data, buf->length); 00065 00066 return ret; 00067 } 00068 00069 VALUE 00070 ossl_protect_membio2str(BIO *bio, int *status) 00071 { 00072 return rb_protect((VALUE(*)_((VALUE)))ossl_membio2str0, (VALUE)bio, status); 00073 } 00074 00075 VALUE 00076 ossl_membio2str(BIO *bio) 00077 { 00078 VALUE ret; 00079 int status = 0; 00080 00081 ret = ossl_protect_membio2str(bio, &status); 00082 BIO_free(bio); 00083 if(status) rb_jump_tag(status); 00084 00085 return ret; 00086 } 00087
1.7.3