pike.git / src / post_modules / Nettle / hash.cmod

version» Context lines:

pike.git/src/post_modules/Nettle/hash.cmod:43:    va_start(args, format);    vfprintf(stderr, format, args);    va_end(args);   }   #else   #define werror(x)   #endif      /*! @module Nettle */    - /*! @class HashInfo + /*! @class Hash    *!    *! Represents information about a hash algorithm, such as    *! name, digest size, and internal block size.    */ - PIKECLASS HashInfo + PIKECLASS Hash   {    /*! @decl inherit __builtin.Nettle.Hash    */    INHERIT "__builtin.Nettle.Hash";       CVAR const struct nettle_hash *meta;       /*! @decl string(0..255) name(void)    *!    *! Returns a human readable name for the algorithm.    */    PIKEFUN string(0..255) name()    optflags OPT_TRY_OPTIMIZE;    {    if (!THIS->meta) -  Pike_error("HashInfo not properly initialized.\n"); +  Pike_error("Hash not properly initialized.\n");       push_text(THIS->meta->name);    }       /*! @decl int(0..) digest_size(void)    *!    *! Returns the size of a hash digests.    */    PIKEFUN int(0..) digest_size()    optflags OPT_TRY_OPTIMIZE;    {    if (!THIS->meta) -  Pike_error("HashInfo not properly initialized.\n"); +  Pike_error("Hash not properly initialized.\n");       push_int(THIS->meta->digest_size);    }       /*! @decl int(0..) block_size(void)    *!    *! Returns the internal block size of the hash algorithm.    */    PIKEFUN int(0..) block_size()    optflags OPT_TRY_OPTIMIZE;    {    if (!THIS->meta) -  Pike_error("HashInfo not properly initialized.\n"); +  Pike_error("Hash not properly initialized.\n");       push_int(THIS->meta->block_size);    }          /*! @decl string(0..255) hash(string data)    *!    *! Works as a (faster) shortcut for -  *! @expr{HashState()->update(data)->digest()@}, where HashState is -  *! the hash state class corresponding to this HashInfo. +  *! @expr{State()->update(data)->digest()@}, where State is +  *! the hash state class corresponding to this Hash.    *!    *! @seealso -  *! @[HashState()->update()] and @[HashState()->digest()]. +  *! @[State()->update()] and @[State()->digest()].    */    PIKEFUN string(0..255) hash(string in)    optflags OPT_TRY_OPTIMIZE;    {    void *ctx;    struct pike_string *out;    unsigned digest_length;    const struct nettle_hash *meta = THIS->meta;       if (!meta) -  Pike_error("HashInfo not properly initialized.\n"); +  Pike_error("Hash not properly initialized.\n");    NO_WIDE_STRING(in);       ctx = (void *)alloca(meta->context_size);    if(!ctx)    SIMPLE_OUT_OF_MEMORY_ERROR("hash", meta->context_size);       /* Only thread this block for significant data size */    if (in->len > THREADS_ALLOW_THRESHOLD) {    THREADS_ALLOW();    meta->init(ctx);
pike.git/src/post_modules/Nettle/hash.cmod:156:    if( p->inherits[i].prog->id == PROG_STDIO_FD_ID ||    p->inherits[i].prog->id == PROG_STDIO_FD_REF_ID )    return 1;    }    return 0;    }       /*! @decl string(0..255) hash(Stdio.File file, void|int bytes)    *!    *! Works as a (faster) shortcut for -  *! @expr{HashState()->update(Stdio.read_file(file))->digest()@}, -  *! where HashState is the hash state class corresponding to this -  *! HashInfo. +  *! @expr{State()->update(Stdio.read_file(file))->digest()@}, +  *! where State is the hash state class corresponding to this +  *! Hash.    *!    *! @param bytes    *! The number of bytes of the file object @[file] that should be    *! hashed. Negative numbers are ignored and the whole file is    *! hashed.    *!    *! @seealso -  *! @[Stdio.File], @[HashState()->update()] and -  *! @[HashState()->digest()]. +  *! @[Stdio.File], @[State()->update()] and +  *! @[State()->digest()].    */    PIKEFUN string(0..255) hash(object in, void|int bytes)    optflags OPT_EXTERNAL_DEPEND;    {    void *ctx;    int len, fd;    char *read_buffer;    PIKE_STAT_T st;    struct pike_string *out;    const struct nettle_hash *meta = THIS->meta;
pike.git/src/post_modules/Nettle/hash.cmod:440:       push_string(end_shared_string(res)); /* 22e */       /* Clean intermediate values. */    MEMSET(ctx, 0, meta->context_size);    MEMSET(abcbuf, 0, 3*dsz);    }       INIT    { -  werror("HashInfo->INIT\n"); +  werror("Hash->INIT\n");    THIS->meta = NULL;    } - } +     - /*! @endclass HashInfo */ -  - #define GET_META(o) \ - ( ((struct HashInfo_struct *) get_storage((o), HashInfo_program)) \ -  ->meta) -  - /* The algorithm objects have to be implemented in pike. */ -  - /*! @class HashState +  /*! @class State    *!    *! Base class for hashing contexts.    */ - PIKECLASS HashState +  PIKECLASS State +  program_flags PROGRAM_USES_PARENT|PROGRAM_NEEDS_PARENT;    { -  INHERIT HashInfo; +  +  EXTRA +  { +  /* Perform an inherit of the State class (if any) that our parent +  * may contain via its inherit of __builtin.Nettle.Hash. +  */ +  struct program *parent_prog = Pike_compiler->previous->new_program; +  struct object *parent_obj = Pike_compiler->previous->fake_object; +  int parent_State_fun_num = +  really_low_find_shared_string_identifier(MK_STRING("State"), +  parent_prog, +  SEE_PROTECTED|SEE_PRIVATE); +  if (parent_State_fun_num >= 0) { +  struct program *parent_State_prog = +  low_program_from_function(parent_obj, parent_State_fun_num); +  if (parent_State_prog) { +  /*! @decl inherit Hash::State +  *! +  *! Inherits @[__builtin.Nettle.Hash.State] via +  *! the inherit of @[__builtin.Nettle.Hash] in our +  *! parent (@[Hash]). +  */ +  low_inherit(parent_State_prog, 0, +  parent_State_fun_num + +  parent_prog->inherits[1].identifier_level, +  1 + 42, 0, NULL); +  } +  } +  } +  + #define GET_META(o) \ +  ( ((struct Hash_struct *)parent_storage(1))->meta ) +     CVAR void *ctx;       /* FIXME: Create should copy state from the other object, if    * provided. */    -  /*! @decl HashState update(string data) +  /*! @decl State update(string(0..255) data)    *!    *! Hashes more data. -  +  *! +  *! @returns +  *! Returns @expr{this@} in order to simplify chaining +  *! of function calls.    */ -  PIKEFUN object update(string data) +  PIKEFUN object update(string(0..255) data)    optflags OPT_SIDE_EFFECT; -  +  rawtype tFunc(tStr8, tObjImpl_HASH_STATE_ID);    {    void *ctx = THIS->ctx;    const struct nettle_hash *meta =    GET_META(Pike_fp->current_object);       if (!ctx || !meta) -  Pike_error("HashState not properly initialized.\n"); +  Pike_error("State not properly initialized.\n");       NO_WIDE_STRING(data);       /* Only thread this block for significant data size */    if (data->len > THREADS_ALLOW_THRESHOLD) {    THREADS_ALLOW();    meta->update(ctx, data->len, (const uint8_t *)data->str);    THREADS_DISALLOW();    } else {    meta->update(ctx, data->len, (const uint8_t *)data->str);    }       push_object(this_object());    }       /*! @decl string(0..255) digest(int|void length)    *! -  *! Generates a digests, and resets the hashing contents. +  *! Generates a digest, and resets the hashing contents.    *!    *! @param length    *! If the length argument is provided, the digest is truncated    *! to the given length.    *!    *! @returns    *! The digest.    */    PIKEFUN string(0..255) digest(int|void arg)    {    const struct nettle_hash *meta;    struct pike_string *digest;    unsigned length;    -  if (! THIS->ctx) -  Pike_error("HashState not properly initialized.\n"); -  +     meta = GET_META(Pike_fp->current_object); -  assert(meta); +     -  +  if (!THIS->ctx || !meta) +  Pike_error("State not properly initialized.\n"); +     if (!arg)    length = meta->digest_size;    else    {    if (TYPEOF(*arg) != PIKE_T_INT)    Pike_error("Bad argument type.\n");    if (arg->u.integer < 0)    Pike_error("Invalid length, must be positive.\n");    if ((unsigned)arg->u.integer > meta->digest_size)    Pike_error("Unsupported digest length.\n");
pike.git/src/post_modules/Nettle/hash.cmod:537:    length = arg->u.integer;    }       digest = begin_shared_string(length);    meta->digest(THIS->ctx, length, (uint8_t *)digest->str);    push_string(end_shared_string(digest));    }       INIT    { -  werror("HashState->INIT\n"); +  werror("State->INIT\n");    THIS->ctx = NULL;    }       EXIT -  gc_trivial; +     { -  werror("HashState->EXIT\n"); -  if (THIS->ctx && Pike_fp->current_object->prog) +  werror("State->EXIT\n"); +  if (THIS->ctx)    {    const struct nettle_hash *meta =    GET_META(Pike_fp->current_object); -  assert(meta); +  if (meta) {    memset(THIS->ctx, 0, meta->context_size);    }    }    } -  +  }    - /*! @endclass HashState */ +  /*! @endclass State */    -  + } +  + /*! @endclass Hash */ +  + /* The algorithm objects can be overloaded in pike. */ +    #cmod_define TOSTR(DEF) #DEF      #cmod_define PIKE_NAME MD5   #cmod_define NETTLE_NAME md5   #cmod_include "hash.H"   #cmod_undef PIKE_NAME   #cmod_undef NETTLE_NAME      #ifdef HAVE_NETTLE_MD4_INIT