/*
Copyright (C) 2008, The Perl Foundation.
$Id: /mirror/trunk/config/gen/crypto/digest_pmc.in 29985 2008-08-02T23:02:50.721172Z chromatic  $

=head1 NAME

src/dynpmc/@md_file@.pmc - @md_name@ wrapper of libcrypto (libssl)

=head1 DESCRIPTION

See L<www.openssl.org>

=head2 Methods

=over 4

=cut

*/

#include <openssl/@md_inc@.h>

pmclass @md_name@
    dynpmc
    need_ext
    group digest_group {

/*

=item C<void init()>

=cut

*/
    void init() {
@md_guard@
        @md_ctx@ *c    = mem_allocate_zeroed_typed(@md_ctx@);
        PMC_data(SELF) = c;
        PObj_active_destroy_SET(SELF);
#else
        Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_ILL_INHERIT,
            "@md_name@ is disabled");
#endif
    }

/*

=item C<void destroy()>

=cut

*/
    void destroy() {
@md_guard@
        @md_ctx@ *c = PMC_data_typed(SELF, @md_ctx@ *);
        if (c) {
            mem_sys_free(c);
            PMC_data(SELF) = NULL;
        }
#endif
    }

/*

=item C<PMC* clone()>

=cut

*/
    PMC* clone() {
@md_guard@
        PMC     *retval  = pmc_new_noinit(INTERP, SELF->vtable->base_type);
        @md_ctx@ *c       = mem_allocate_zeroed_typed(@md_ctx@);

        memcpy(c, PMC_data(SELF), sizeof (@md_ctx@));
        PMC_data(retval) = c;

        PObj_active_destroy_SET(retval);
        return retval;
#else
        return NULL;
#endif
    }


/*

=back

=head2 Specific Methods

=over 4

=item C<void Init()>

=cut

*/
    METHOD void Init() {
@md_guard@
        @md_ctx@ *c = PMC_data_typed(SELF, @md_ctx@ *);
        (void)@md_name@_Init(c);
#endif
    }

/*

=item C<void Update(STRING *buf)>

=cut

*/
    METHOD void Update(STRING *buf) {
@md_guard@
        @md_ctx@ *c = PMC_data_typed(SELF, @md_ctx@ *);
        (void)@md_name@_Update(c, (unsigned char *)buf->strstart, buf->bufused);
#endif
    }

/*

=item C<STRING* Final()>

=cut

*/
    METHOD STRING* Final() {
@md_guard@
        unsigned char digest[@md_digest@_LENGTH];
        @md_ctx@      *c = PMC_data_typed(SELF, @md_ctx@ *);
        STRING       *retval;

        (void)@md_name@_Final(digest, c);
        retval = string_from_cstring(INTERP, (char *)digest, @md_digest@_LENGTH);

        RETURN(STRING *retval);
#else
        return;
#endif
    }

}

/*

=back

=head1 AUTHORS

Francois Perrad

=cut

*/

/*
 * Local variables:
 *   c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4:
 */
