Ruby  1.9.3p448(2013-06-27revision41675)
ossl_bio.c
Go to the documentation of this file.
1 /*
2  * $Id: ossl_bio.c 32591 2011-07-20 22:11:56Z 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 *
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);
28  if ((fd = 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);
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
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 }
void rb_io_check_readable(rb_io_t *)
Definition: io.c:657
void rb_update_max_fd(int fd)
Definition: io.c:156
VALUE ossl_membio2str0(BIO *bio)
Definition: ossl_bio.c:59
VALUE ossl_protect_membio2str(BIO *bio, int *status)
Definition: ossl_bio.c:71
Definition: io.h:53
#define FPTR_TO_FD(fptr)
Definition: ruby_missing.h:19
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:704
#define RSTRING_PTR(string)
Definition: generator.h:42
BIO * ossl_protect_obj2bio(VALUE obj, int *status)
Definition: ossl_bio.c:51
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:77
#define GetOpenFile(obj, fp)
Definition: io.h:110
VALUE eOSSLError
Definition: ossl.c:255
#define TYPE(x)
Definition: ruby.h:441
BIO * ossl_obj2bio(VALUE obj)
Definition: ossl_bio.c:17
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:3913
#define NULL
unsigned long VALUE
Definition: ruby.h:88
void rb_sys_fail(const char *mesg)
Definition: error.c:1671
void rb_jump_tag(int tag)
Definition: eval.c:598
#define _(args)
Definition: dln.h:28
#define T_FILE
Definition: ruby.h:424
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:319
#define RSTRING_LENINT(str)
Definition: ruby.h:684
#define StringValue(v)
Definition: ruby.h:466
VALUE rb_str_new(const char *, long)
Definition: string.c:410