lo_read (PGconn)

    /*
     * call-seq:
     *    conn.lo_read( lo_desc, len ) -> String
     *
     * Attempts to read _len_ bytes from large object _lo_desc_,
     * returns resulting data.
     */
    static VALUE
    pgconn_loread(VALUE self, VALUE in_lo_desc, VALUE in_len)
    {
            int ret;
            PGconn *conn = get_pgconn(self);
            int len = NUM2INT(in_len);
            int lo_desc = NUM2INT(in_lo_desc);
            VALUE str;
            char *buffer;

            buffer = malloc(len);
            if(buffer == NULL)
                    rb_raise(rb_eNoMemError, "Malloc failed!");

            if (len < 0){
                    rb_raise(rb_ePGError,"nagative length %d given", len);
            }

            if((ret = lo_read(conn, lo_desc, buffer, len)) < 0)
                    rb_raise(rb_ePGError, "lo_read failed");

            if(ret == 0) {
                    free(buffer);
                    return Qnil;
            }

            str = rb_tainted_str_new(buffer, len);
            free(buffer);

            return str;
    }