Ruby 2.0.0p247(2013-06-27revision41674)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

ossl_bio.c
Go to the documentation of this file.
1 /*
2 * $Id: ossl_bio.c 33553 2011-10-29 11:02:32Z akr $
3 * 'OpenSSL for Ruby' team members
4 * Copyright (C) 2003
5 * All rights reserved.
6 */
7 /*
8 * This program is licenced under the same licence as Ruby.
9 * (See the file 'LICENCE'.)
10 */
11 #include "ossl.h"
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15
16 BIO *
17 ossl_obj2bio(VALUE obj)
18 {
19 BIO *bio;
20
21 if (TYPE(obj) == T_FILE) {
22 rb_io_t *fptr;
23 FILE *fp;
24 int fd;
25
26 GetOpenFile(obj, fptr);
27 rb_io_check_readable(fptr);
28 if ((fd = rb_cloexec_dup(FPTR_TO_FD(fptr))) < 0){
29 rb_sys_fail(0);
30 }
31 rb_update_max_fd(fd);
32 if (!(fp = fdopen(fd, "r"))){
33 close(fd);
34 rb_sys_fail(0);
35 }
36 if (!(bio = BIO_new_fp(fp, BIO_CLOSE))){
37 fclose(fp);
38 ossl_raise(eOSSLError, NULL);
39 }
40 }
41 else {
42 StringValue(obj);
43 bio = BIO_new_mem_buf(RSTRING_PTR(obj), RSTRING_LENINT(obj));
44 if (!bio) ossl_raise(eOSSLError, NULL);
45 }
46
47 return bio;
48 }
49
50 BIO *
51 ossl_protect_obj2bio(VALUE obj, int *status)
52 {
53 BIO *ret = NULL;
54 ret = (BIO*)rb_protect((VALUE(*)_((VALUE)))ossl_obj2bio, obj, status);
55 return ret;
56 }
57
58 VALUE
59 ossl_membio2str0(BIO *bio)
60 {
61 VALUE ret;
62 BUF_MEM *buf;
63
64 BIO_get_mem_ptr(bio, &buf);
65 ret = rb_str_new(buf->data, buf->length);
66
67 return ret;
68 }
69
70 VALUE
71 ossl_protect_membio2str(BIO *bio, int *status)
72 {
73 return rb_protect((VALUE(*)_((VALUE)))ossl_membio2str0, (VALUE)bio, status);
74 }
75
76 VALUE
77 ossl_membio2str(BIO *bio)
78 {
79 VALUE ret;
80 int status = 0;
81
82 ret = ossl_protect_membio2str(bio, &status);
83 BIO_free(bio);
84 if(status) rb_jump_tag(status);
85
86 return ret;
87 }
88