Exemple #1
0
/*! @decl string decode_base64(string encoded_data)
 *!
 *! This function decodes data encoded using the @tt{base64@}
 *! transfer encoding.
 *!
 *! @seealso
 *! @[MIME.encode_base64()], @[MIME.decode()]
 */
static void f_decode_base64( INT32 args )
{
  if(args != 1)
    Pike_error( "Wrong number of arguments to MIME.decode_base64()\n" );
  else if (sp[-1].type != T_STRING)
    Pike_error( "Wrong type of argument to MIME.decode_base64()\n" );
  else if (sp[-1].u.string->size_shift != 0)
    Pike_error( "Char out of range for MIME.decode_base64()\n" );
  else {

    /* Decode the string in sp[-1].u.string.  Any whitespace etc
       must be ignored, so the size of the result can't be exactly
       calculated from the input size.  We'll use a string builder
       instead. */

    struct string_builder buf;
    SIGNED char *src;
    ptrdiff_t cnt;
    INT32 d = 1;
    int pads = 0;

    init_string_builder( &buf, 0 );

    for (src = (SIGNED char *)sp[-1].u.string->str, cnt = sp[-1].u.string->len;
	 cnt--; src++)
      if(*src>=' ' && base64rtab[*src-' ']>=0) {
	/* 6 more bits to put into d */
	if((d=(d<<6)|base64rtab[*src-' '])>=0x1000000) {
	  /* d now contains 24 valid bits.  Put them in the buffer */
	  string_builder_putchar( &buf, (d>>16)&0xff );
	  string_builder_putchar( &buf, (d>>8)&0xff );
	  string_builder_putchar( &buf, d&0xff );
	  d=1;
	}
      } else if (*src=='=') {
Exemple #2
0
static void pipe_read_input_callback(INT32 args)
{
  struct input *i;
  struct pike_string *s;

  if (args<2 || sp[1-args].type!=T_STRING)
    Pike_error("Illegal argument to pipe->read_input_callback\n");
   
  i=THIS->firstinput;

  if (!i)
    Pike_error("Pipe read callback without any inputs left.\n");

  s=sp[1-args].u.string;

  if(append_buffer(s))
  {
    /* THIS DOES NOT WORK */
    push_int(0);
    push_int(0);
    push_callback(offset_input_close_callback);
    apply_low(i->u.obj,i->set_nonblocking_offset,3);
    pop_stack();
    THIS->sleeping=1;
  }

  low_start();
  pop_n_elems(args-1);
}
Exemple #3
0
static void pipe_close_input_callback(INT32 args)
{
   struct input *i;
   i=THIS->firstinput;

   if(!i)
     Pike_error("Input close callback without inputs!\n");

   if(i->type != I_OBJ)
     Pike_error("Premature close callback on pipe!.\n");

   if (i->u.obj->prog)
   {
#ifdef BLOCKING_CLOSE
      apply_low(i->u.obj,i->set_blocking_offset,0);
      pop_stack();
#endif
      apply(i->u.obj,"close",0);
      pop_stack();
   }
   nobjects--;
   free_object(i->u.obj);
   i->type=I_NONE;

   input_finish();
   if(args)
     pop_n_elems(args-1);
}
Exemple #4
0
static void f_parse_html(INT32 args)
{
  xmlDocPtr   doc = NULL;
  char * encoding = "utf-8";
  struct pike_string *encode_data = NULL;
  
  if ( args == 1 ) {
      if ( ARG(1).type != T_STRING ) 
	  Pike_error("Incorrect type for argument 0: expected string (encoding)\n");
      encode_data = ARG(1).u.string;
      encoding = encode_data->str;
  }
  // do nothing
  if ( THIS->input_data->len == 0 )
    push_int(0);

  switch (THIS->parsing_method) {
      case PARSE_PUSH_PARSER:
        Pike_error("Push parser not implemented yet. Please bug [email protected] to implement it.");
        
      case PARSE_MEMORY_PARSER:
        htmlHandleOmittedElem(1);
	doc=htmlSAXParseDoc(THIS->input_data->str, encoding, THIS->sax, NULL);
        break;

      case PARSE_FILE_PARSER:
        htmlHandleOmittedElem(1);
	doc=htmlSAXParseFile(THIS->input_data->str, "utf-8", THIS->sax, NULL);
	break;
  }
  if ( doc != NULL )
    xmlFreeDoc(doc);
  
  push_int(0);
}
Exemple #5
0
/*
**! file: Mhash/mhash.c
**!  File implementing the Mhash.Hash() class.
**! cvs_version: $Id$
**! class: Mhash.Hash
**!  An instance of a normal Mhash object. This object can be used to
**!  calculate various hashes supported by the Mhash library.
**! see_also: Mhash.HMAC
**! method: void create(int|void type)
**!  Called when instantiating a new object. It takes an optional first
**!  argument with the type of hash to use.
**! arg: int|void type
**!  The hash type to use. Can also be set with set_type();
**! name: create - Create a new hash instance.
*/
void f_hash_create(INT32 args)
{
  if(THIS->type != -1 || THIS->hash || THIS->res) {
    Pike_error("Recursive call to create. Use Mhash.Hash()->reset() or \n"
	  "Mhash.Hash()->set_type() to change the hash type or reset\n"
	  "the object.\n");
  }
  switch(args) {
  default:
    Pike_error("Invalid number of arguments to Mhash.Hash(), expected 0 or 1.\n");
    break;
  case 1:
    if(Pike_sp[-args].type != T_INT) {
      Pike_error("Invalid argument 1. Expected integer.\n");
    }
    THIS->type = Pike_sp[-args].u.integer;
    THIS->hash = mhash_init(THIS->type);
    if(THIS->hash == MHASH_FAILED) {
      THIS->hash = NULL;
      Pike_error("Failed to initialize hash.\n");
    }
    break;
  case 0:
    break;
  }
  
  pop_n_elems(args);
}
Exemple #6
0
static void image_ttf_faceinstance_set_height(INT32 args)
{
   struct image_ttf_face_struct *face_s;
   struct image_ttf_faceinstance_struct *face_i=THISi;
   int h=0;

   if (!args)
      Pike_error("Image.TTF.FaceInstance->set_height(): missing arguments\n");

   if (sp[-args].type==T_INT)
      h = sp[-args].u.integer*64;
   else if (sp[-args].type==T_FLOAT)
      h = DOUBLE_TO_INT(sp[-args].u.float_number*64);
   else
      Pike_error("Image.TTF.FaceInstance->set_height(): illegal argument 1\n");
   if (h<1) h=1;

   if (!(face_s=(struct image_ttf_face_struct*)
	 get_storage(THISi->faceobj,image_ttf_face_program)))
      Pike_error("Image.TTF.FaceInstance->write(): lost Face\n");

   ttf_instance_setc(face_s,face_i,h,"Image.TTF.FaceInstance->set_height()");

   pop_n_elems(args);
   ref_push_object(THISOBJ);
}
Exemple #7
0
/*! @decl void create(void|int version, void|int stacksize)
 *!
 *! Creates an instance of the Context class.
 *!
 *! @param version
 *!  This context will be initially made compatible with the specified
 *!  JavaScript version. The following constants are accepted as the value
 *!  of this parameter:
 *!
 *!   @dl
 *!    @item JSVERSION_1_0
 *!     JavaScript v1.0
 *!    @item JSVERSION_1_1
 *!     JavaScript v1.1
 *!    @item JSVERSION_1_2
 *!     JavaScript v1.2
 *!    @item JSVERSION_1_3
 *!     JavaScript v1.3 (ECMA)
 *!    @item JSVERSION_1_4
 *!     JavaScript v1.4 (ECMA)
 *!    @item JSVERSION_1_5
 *!     JavaScript v1.5 (ECMA)
 *!   @enddl
 *!
 *!  The default value is @b{JSVERSION_1_5@}
 *!
 *! @param stacksize
 *!  Sets the size of the private stack for this context. Value given in
 *!  bytes. Defaults to 8192.
 */
static void ctx_create(INT32 args)
{
  INT32      version = JSVERSION_1_5;
  INT32      stacksize = 8192;

  switch(args) {
      case 2:
        get_all_args("create", args, "%i%i", &version, &stacksize);
        break;

      case 1:
        get_all_args("create", args, "%i", &version);
        break;
  }

  THIS->ctx = JS_NewContext(smrt, stacksize);
  if (!THIS->ctx)
    Pike_error("Could not create a new context\n");
    
  if (!init_globals(THIS->ctx))
    Pike_error("Could not initialize the new context.\n");

  if (!JS_DefineFunctions(THIS->ctx, global, output_functions))
    Pike_error("Could not populate the global object with output functions\n");
    
  JS_SetVersion(THIS->ctx, version);

  /* create some privacy for us */
  if (!JS_SetPrivate(THIS->ctx, global, THIS))
    Pike_error("Could not set the private storage for the global object\n");
    
  pop_n_elems(args);
}
Exemple #8
0
/*
**! method: Mhash.hash feed(string data)
**!    alt: Mhash.hash update(string data)
**!  Update the current hash context with data.
**!  update() is here for compatibility reasons with Crypto.md5.
**! arg: string data
**!  The data to update the context with.
**! returns:
**!  The current hash object.
**! name: feed - Update the current hash context.
*/
void f_hash_feed(INT32 args) 
{
  if(THIS->hash == NULL) {
    if(THIS->type != -1) {
      free_hash();
      THIS->hash = mhash_init(THIS->type);
      if(THIS->hash == MHASH_FAILED) {
	THIS->hash = NULL;
	Pike_error("Failed to initialize hash.\n");
      }
    } else
      Pike_error("Hash is uninitialized. Use Mhash.Hash()->set_type() to select hash type.\n");
  }
  if(args == 1) {
    if(Pike_sp[-args].type != T_STRING) {
      Pike_error("Invalid argument 1. Expected string.\n");
    }
    mhash(THIS->hash, Pike_sp[-args].u.string->str,
	  Pike_sp[-args].u.string->len << Pike_sp[-args].u.string->size_shift);
  } else {
    Pike_error("Invalid number of arguments to Mhash.Hash->feed(), expected 1.\n");
  }
  pop_n_elems(args);
  push_object(this_object());
}
Exemple #9
0
/*
 **| method: array(string) explode_dn ( string dn );
 **| alt: array(string) explode_dn ( string dn, int notypes );
 **|  Takes a DN and converts it into an array of its components,
 **|  called RDN (Relative Distinguished Name).
 **
 **| arg: string dn
 **|  The DN to explode.
 **
 **| arg: int notypes
 **|  If != 0 then the types of the DN components will be ignored and
 **|  *not present in the output. Defaults to 1.
 **
 **| returns: an array of RDN entries.
 */
static void
f_ldap_explode_dn(INT32 args)
{
    struct pike_string       *dn;
    char                    **edn;
    int                       notypes = 1;    

    switch (args) {
        case 2:
            if (ARG(2).type != T_INT)
                Pike_error("OpenLDAP.Client->explode_dn(): argument 2 must be an integer\n");
            notypes = ARG(2).u.integer;
            /* fall through */
            
        case 1:
            if (ARG(1).type != T_STRING)
                Pike_error("OpenLDAP.Client->explode_dn(): argument 1 must be an integer\n");
            dn = ARG(1).u.string;
            break;
            
        default:
            Pike_error("OpenLDAP.Client->explode_dn(): expects at most 2 and at least 1 argument\n");
            break;
    }

    pop_n_elems(args);
    edn = ldap_explode_dn(dn->str, notypes);
    if (!edn) {
        push_int(0);
        return;
    }

    push_array(make_pike_array(edn));
    ldap_value_free(edn);
}
Exemple #10
0
/*
 **| method: void create ( string uri );
 **
 **|  Create a new OpenLDAP client connection object.
 **
 **| name: create - create the object
 **
 **| arg: string uri
 **|  An URI pointing to the LDAP host to connect to as described in
 **|  RFC 2255. The LDAP URI has the general format:
 **|
 **|   # ldap://hostport/dn[?attrs[?scope[?filter[?exts]]]]
 **|
 **|  where
 **|    hostport is a host name with an optional ":portnumber"
 **|    dn is the search base
 **|    attrs is a comma separated list of attributes to request
 **|    scope is one of these three strings:
 **|    base one sub (default=base)
 **|    filter is filter
 **|    exts are recognized set of LDAP and/or API extensions.
 **|
 **|  Documentation to this function has been written based on the
 **|  OpenLDAP v2 ldap_url_parse(3) manual page.
 **
 **| see_also: ldap_url_parse(3), RFC 2255
 */
static void
f_create(INT32 args)
{
    if (args != 1)
        Pike_error("OpenLDAP.Client->create():: wrong number of arguments\n");

    if (ARG(1).type != T_STRING || ARG(1).u.string->size_shift > 0)
        Pike_error("OpenLDAP.Client->create():: expecting an 8-bit string as the first argument (%u)\n",
                   ARG(1).u.string->size_shift);

    if ((THIS->lerrno = ldap_url_parse(ARG(1).u.string->str, &THIS->server_url)))
        Pike_error("OpenLDAP.Client->create():: badly formed server URL\n");

    if (!THIS)
        Pike_error("Serious problem - no THIS...\n");
    
    THIS->conn = ldap_init(THIS->server_url->lud_host,
                           THIS->server_url->lud_port);

    if (!THIS->conn)
        Pike_error("OpenLDAP.Client->create():: error initializing OpenLDAP: '%s'\n",
                   strerror(errno));

    pop_n_elems(args);
}
Exemple #11
0
static void mcast_join(INT32 args)
{
   uint32_t mc_addr;
   struct ip_mreq mreq;
   
   if(args!=1)
      Pike_error("mcast->join(): number of arguments invalid.\n");
   if(Pike_sp[-1].type!=T_STRING)
      Pike_error("mcast->join(): expected string.\n");
   
   /* Verifica el estado del socket */
   if( FD < 0 )
     /* El objeto no está inicializado... */
     Pike_error("mcast->join(): Port not bound! (call \"bind\" first)\n");
   
   
   mc_addr = inet_addr(Pike_sp[-1].u.string->str);
   if(mc_addr == -1)
     Pike_error("mcast->join(): Invalid mcast group\n");
   
   mreq.imr_multiaddr.s_addr = mc_addr;
   mreq.imr_interface.s_addr = THIS->if_addr;
   if ( setsockopt(FD,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char *) &mreq,
		   sizeof(mreq)) == -1 )
   {
      UDP->my_errno = errno;
      Pike_error("mcast->join(): error in joining group (%s)\n",strerror(errno));
   }
   
   pop_n_elems(args);
}
Exemple #12
0
/* Do a regular expression match */
void f_pcre_match(INT32 args) 
{
  struct pike_string *data; /* Data to match */
  pcre_extra *extra = NULL;   /* result from study, if enabled */
  pcre *re = NULL;            /* compiled regexp */
  char *pp;                 /* Pointer... */
  int opts = 0;             /* Match options */
  int is_match;             /* Did it match? */

  if(THIS->regexp == NULL)
    Pike_error("PCRE.Regexp not initialized.\n");
  switch(args)
  {
   case 2:
    switch(Pike_sp[-1].type) {
     case T_STRING:
      opts = parse_options(Pike_sp[-1].u.string->str, NULL);
      if(opts < 0)
	Pike_error("PCRE.Regexp->match(): Unknown option modifier '%c'.\n", -opts);
      break;
     case T_INT:
      if(Pike_sp[-1].u.integer == 0) {
	break;
      }
      /* Fallthrough */
     default:
      Pike_error("Bad argument 2 to PCRE.Regexp->match() - expected string.\n");
      break;
    }
    /* Fall through */
   case 1:
    if(Pike_sp[-args].type != T_STRING || Pike_sp[-args].u.string->size_shift > 0) {
      Pike_error("PCRE.Regexp->match(): Invalid argument 1. Expected 8-bit string.\n");
    }
    data = Pike_sp[-args].u.string;
    break;
   default:
    Pike_error("PCRE.Regexp->match(): Invalid number of arguments. Expected 1 or 2.\n");
  }
  re = THIS->regexp;
  extra = THIS->extra;

  /* Do the pattern matching */
  is_match = pcre_exec(re, extra, data->str, data->len, 0,
		       opts, NULL, 0);
  pop_n_elems(args);
  switch(is_match) {
  case PCRE_ERROR_NOMATCH:   push_int(0);  break;
  case PCRE_ERROR_NULL:      Pike_error("Invalid argumens passed to pcre_exec.\n");
  case PCRE_ERROR_BADOPTION: Pike_error("Invalid options sent to pcre_exec.\n");
  case PCRE_ERROR_BADMAGIC:  Pike_error("Invalid magic number.\n");
  case PCRE_ERROR_UNKNOWN_NODE: Pike_error("Unknown node encountered. PCRE bug or memory error.\n");
  case PCRE_ERROR_NOMEMORY:  Pike_error("Out of memory during execution.\n");
  default:
    push_int(1); /* A match! */
    break; 
  }
}
Exemple #13
0
static void image_ilbm___decode(INT32 args)
{
   unsigned char *s;
   ptrdiff_t len;
   struct pike_string *str;
   struct mapping *m;
   int n;
   extern void parse_iff(char *, unsigned char *, ptrdiff_t,
			 struct mapping *, char *);

   get_all_args("__decode", args, "%S", &str);

   s = (unsigned char *)str->str;
   len = str->len;
   pop_n_elems(args-1);

   for(n=0; n<5; n++)
     push_int(0);
   push_mapping(m = allocate_mapping(4));

   parse_iff("ILBM", s, len, m, "BODY");

   mapping_index_no_free(sp-5, m, &string_[string_BMHD]);
   mapping_index_no_free(sp-4, m, &string_[string_CMAP]);
   mapping_index_no_free(sp-3, m, &string_[string_CAMG]);
   mapping_index_no_free(sp-2, m, &string_[string_BODY]);

   map_delete(m, &string_[string_BMHD]);
   map_delete(m, &string_[string_CMAP]);
   map_delete(m, &string_[string_CAMG]);
   map_delete(m, &string_[string_BODY]);

   if(sp[-5].type != T_STRING)
     Pike_error("Missing BMHD chunk\n");
   if(sp[-2].type != T_STRING)
     Pike_error("Missing BODY chunk\n");

   /* Extract image size from BMHD */
   s = (unsigned char *)STR0(sp[-5].u.string);
   len = sp[-5].u.string->len;

   if(len<20)
     Pike_error("Short BMHD chunk\n");

   free_svalue(sp-7);

   sp[-7].u.integer = (s[0]<<8)|s[1];
   sp[-7].type = T_INT;
   sp[-7].subtype = NUMBER_NUMBER;
   sp[-6].u.integer = (s[2]<<8)|s[3];
   sp[-6].type = T_INT;
   sp[-6].subtype = NUMBER_NUMBER;

   f_aggregate(7);
}
Exemple #14
0
PMOD_EXPORT void convert_stack_top_with_base_to_bignum(void)
{
  apply_svalue(&auto_bignum_program, 2);

  if(sp[-1].type != T_OBJECT) {
     if (auto_bignum_program.type!=T_PROGRAM)
	Pike_error("Gmp.mpz conversion failed (Gmp.bignum not loaded).\n");
     else
	Pike_error("Gmp.mpz conversion failed (unknown error).\n");
  }
}
Exemple #15
0
/* void next(void) */
static void f_next(INT32 args)
{
  int n;

  if (args)
    Pike_error("Too many arguments to Count->next()\n");

  n = avs_countnext(PIKE_COUNT->handle);
  if (n != AVS_OK)
    Pike_error("Count->next(): %s\n", avs_errmsg(n));
}
Exemple #16
0
/*
 * Takes an array of mappings, similar to modify above, with the
 * exception that the 'op' field is ignored and not used at all.
 */
static void
f_ldap_add(INT32 args)
{
    struct pike_string     *dn;
    struct array           *arr;
    struct mapping         *m;
    struct svalue          *val;
    LDAPMod               **mods;
    int                     i, ret;

    if (!THIS->bound)
        Pike_error("OpenLDAP.Client: attempting operation on an unbound connection\n");
    
    get_all_args("OpenLDAP.Client->add()", args, "%S%a", &dn, &arr);
    mods = (LDAPMod**)calloc((arr->size + 1), sizeof(LDAPMod*));
    if (!mods)
        Pike_error("OpenLDAP.Client: OUT OF MEMORY!\n");

    for (i = 0; i < arr->size; i++) {
        mods[i] = (LDAPMod*)calloc(1, sizeof(LDAPMod));
        if (!mods[i])
            Pike_error("OpenLDAP.Client: OUT OF MEMORY!\n");
        mods[i]->mod_op = LDAP_MOD_BVALUES;

        if (arr->item[i].type != T_MAPPING)
            Pike_error("OpenLDAP.Client->add(): array member is not a mapping.\n");
        m = arr->item[i].u.mapping;
        
        val = low_mapping_string_lookup(m, modify_type);
        if (!val)
            Pike_error("OpenLDAP.Client->add(): invalid modification mapping. "
                       "Missing the '%s' field\n", "type");
        if (val->type != T_STRING || val->u.string->size_shift > 0)
            Pike_error("OpenLDAP.Client->add(): invalid modification mapping. "
                       "The '%s' field is not an 8-bit string\n", "type");
        mods[i]->mod_type = val->u.string->str;

        val = low_mapping_string_lookup(m, modify_values);
        if (!val)
            Pike_error("OpenLDAP.Client->add(): invalid modification mapping. "
                       "Missing the '%s' field\n", "values");
        if (val->type != T_ARRAY)
            Pike_error("OpenLDAP.Client->add(): invalid modification mapping. "
                       "The '%s' field is not an array\n", "values");
        
        mods[i]->mod_bvalues = make_berval_array(val);
    }
    
    ret = ldap_add_s(THIS->conn, dn->str, mods);
    if (ret != LDAP_SUCCESS)
        Pike_error("OpenLDAP.Client->add(): %s\n",
                   ldap_err2string(ret));
    
    free_mods(mods);
    
    pop_n_elems(args);
}
Exemple #17
0
static void pipe_write_output_callback(INT32 args)
{
   if (args<1 || sp[-args].type!=T_OBJECT)
     Pike_error("Illegal argument to pipe->write_output_callback\n");

   if(!sp[-args].u.object->prog) return;

   if(sp[-args].u.object->prog != output_program)
     Pike_error("Illegal argument to pipe->write_output_callback\n");

   debug_malloc_touch(sp[-args].u.object);
   output_try_write_some(sp[-args].u.object);
   pop_n_elems(args-1);
}
Exemple #18
0
static void f_buf_create( INT32 args )
{
  if(args != 2)
    Pike_error("Wrong number of arguments to create. Expected 2.\n");
  if(sp[-1].type != T_MAPPING)
    Pike_error("Wrong argument 1 to create. Expected mapping.\n");
  if(sp[-2].type != T_MAPPING)
    Pike_error("Wrong argument 2 to create. Expected mapping.\n");
  add_ref(BUF->headers   = sp[-1].u.mapping);
  add_ref(BUF->other     = sp[-2].u.mapping);
  BUF->pos = BUF->data;
  BUF->free = BUFSIZE;
  pop_n_elems(args);
}
Exemple #19
0
/* void create(object(AVS.Index)) */
static void f_create(INT32 args)
{
  struct private_index_data *index;
  if (!args)
    Pike_error("Too few arguments to Search->create()\n");

  if ((Pike_sp[-args].type != T_OBJECT) || 
      (!(index = (struct private_index_data *)
                 get_storage(Pike_sp[-args].u.object, index_program))))
    Pike_error("Bad argument 1 to Search->create()\n");

  memcpy(PIKE_SEARCH, &index->tmp_search, sizeof(struct private_search_data));
  pop_n_elems(args);
}
Exemple #20
0
void image_polyfill(INT32 args)
{
    struct vertex *v;
    double *buf;
    ONERROR err;

    if (!THIS->img)
        Pike_error("Image.Image->polyfill: no image\n");

    buf=xalloc(sizeof(double)*(THIS->xsize+1));
    SET_ONERROR(err, free, buf);

    v=polyfill_begin();

    while (args)
    {
        struct vertex *v_tmp;

        if (sp[-1].type!=T_ARRAY)
        {
            polyfill_free(v);
            SIMPLE_BAD_ARG_ERROR("Image.Image->polyfill", args,
                                 "array(int|float)");
        }
        if ((v_tmp=polyfill_add(&v, sp[-1].u.array, args,
                                "Image.Image->polyfill()"))) {
            v = v_tmp;
        } else {
            polyfill_free(v);
            Pike_error("Image.Image->polyfill: Bad argument %d, bad vertex\n", args);
        }
        args--;
        pop_stack();
    }

    if (!v) {
        free(buf);
        return; /* no vertices */
    }

    polyfill_some(THIS,v,buf);

    polyfill_free(v);

    UNSET_ONERROR(err);
    free(buf);

    ref_push_object(THISOBJ);
}
Exemple #21
0
/* Our nb input read callback */
static void f__input_read_cb(INT32 args)
{
  int avail_size = 0, len;
  struct pike_string *str;
  input *inp = THIS->inputs;
  if(inp == NULL) {
    Pike_error("Input read callback without inputs.");
  }    
  if(args != 2)
    Pike_error("Invalid number of arguments to read callback.");
  if(ARG(2).type != T_STRING) {
    SIMPLE_BAD_ARG_ERROR("_Caudium.nbio()->_input_read_cb", 2, "string");
  }
  str = ARG(2).u.string;
  len = str->len << str->size_shift;
  inp->pos += len;
  if(inp->len != -1 && inp->pos >= inp->len) {
    len -= inp->pos - inp->len; /* Don't "read" too much */
    DERR(fprintf(stderr, "Read all wanted input data.\n"));
    free_input(inp);
  }
  DERR(fprintf(stderr, "Input read callback (got %d bytes).\n", len));
  if(THIS->buf_size) {
    avail_size = THIS->buf_size - (THIS->buf_len + THIS->buf_pos);
  } 
  if(avail_size < len) {
    alloc_data_buf(THIS->buf_size + (len - avail_size));
  }
  DERR(fprintf(stderr, "Copying %d bytes to buf starting at 0x%x (pos %d).\n",
	       len, (int)(THIS->buf + THIS->buf_pos + THIS->buf_len), THIS->buf_pos + THIS->buf_len));
  memcpy(THIS->buf + THIS->buf_pos + THIS->buf_len, str->str, len);
  THIS->buf_len += len;
  if((THIS->buf_len + THIS->buf_pos) > READ_BUFFER_SIZE) {
    DERR(fprintf(stderr, "Read buffer full (%d bytes).\n", THIS->buf_size));
    push_int(0);   push_int(0);  push_int(0);
    apply_low(inp->u.file, inp->set_nb_off, 3);
    pop_stack();
    inp->mode = SLEEPING;
  }
  pop_n_elems(args);
  
  if(THIS->outp->mode == IDLE) {
    DERR(fprintf(stderr, "Waking up output.\n"));
    THIS->outp->mode = ACTIVE;
    f__output_write_cb(0);
  } else {
    DERR(fprintf(stderr, "Output is awake.\n"));
  }
}
Exemple #22
0
/* Inflate class */
static void
f_inflate_create(INT32 args)
{
    if (args == 1) {
	if (ARG(1).type != T_INT)
	    Pike_error("bzip2.inflate->create(): argument must be of type INT\n");
	    
	THIS->blkSize = ARG(1).u.integer != 0;
    } else if (args > 1) {
	Pike_error("bzip2.inflate->create(): expected 1 argument of type INT.\n");
    } else
	THIS->blkSize = 0;
	
    pop_n_elems(args);
}
Exemple #23
0
void f_msg_get_header(INT32 args) {
	HEADER *header=0;
	int idx=THISMSG->msgno;
	
	pop_n_elems(args);
	if( idx >= THISMSG->ctx->msgcount || idx < 0 ) {
		Pike_error("get_header: header index '%d' not present!\n",idx);
	}
		
	header=THISMSG->ctx->hdrs[idx];
	if( !header ) {
		Pike_error("get_header: header pointer is NULL for index '%d'\n",idx);
	}
	push_headers(header);
}
Exemple #24
0
static void f_extension( INT32 args ) {
  int i, found=0;
  struct pike_string *src;
  char *orig, *ptr;

  if(Pike_sp[-1].type != T_STRING)
    SIMPLE_BAD_ARG_ERROR("Caudium.extension", 1, "string");
  src = Pike_sp[-1].u.string;
  if(src->size_shift) {
    Pike_error("Caudium.extension(): Only 8-bit strings allowed.\n");
  }
  orig = src->str;
  for(i = src->len-1; i >= 0; i--) {
    if(orig[i] == 0x2E) {
      found = 1;
      i++;
      break;
    }
  }
  
  if(found) {
    int len = src->len - i;    
    switch(orig[src->len-1]) {
     case '#': case '~':
      /* Remove unix backup extension */
      len--;
    }
    pop_n_elems(args);
    push_string(make_shared_binary_string(orig+i, len));

  } else {
    pop_n_elems(args);
    push_text("");
  }
}
Exemple #25
0
/*! @decl array(string) _xpm_trim_rows(array(string) rows)
 */
void f__xpm_trim_rows( INT32 args )
{
  struct array *a;
  int i,j=0;
  get_all_args("_xpm_trim_rows", args, "%a", &a );
  for(i=0; i<a->size; i++)
  {
    int len,start;
    struct pike_string *s = a->item[i].u.string;
    if(a->item[i].type != T_STRING)
      Pike_error("Array must be array(string).\n");
    if(s->len > 4)
    {
      for(start=0; start<s->len; start++)
        if(s->str[start] == '/' || s->str[start] == '"')
          break;
      if(s->str[start] == '/')
        continue;
      for(len=start+1; len<s->len; len++)
        if(s->str[len] == '"')
          break;
      if(len>=s->len || s->str[len] != '"')
        continue;
      free_string(a->item[j].u.string);
      a->item[j++].u.string=make_shared_binary_string(s->str+start+1,len-start-1);
    }
  }
  pop_n_elems(args-1);
}
Exemple #26
0
static void f_parse_headers( INT32 args )
{
  struct mapping *headermap;
  struct pike_string *headers;
  unsigned char *ptr;
  int len = 0, parsed = 0;
  get_all_args("Caudium.parse_headers", args, "%S", &headers);
  headermap = allocate_mapping(1);
  ptr = (unsigned char *)headers->str;
  len = headers->len;
  /*
   * FIXME:
   * What do we do if memory allocation fails halfway through
   * allocating a new mapping? Should we return that half-finished
   * mapping or rather return NULL? For now it's the former case.
   * /Grendel
   *
   * If memory allocation fails, just bail out with error()
   */
  while(len > 0 &&
        (parsed = get_next_header(ptr, len, headermap)) >= 0 ) {
    ptr += parsed;
    len -= parsed;
  }
  if(parsed == -1) {
    Pike_error("Caudium.parse_headers(): Out of memory while parsing.\n");
  }
  pop_n_elems(args);
  push_mapping(headermap);
}
Exemple #27
0
static void exit_output_struct(struct object *obj)
{
  struct output *o;
  
   debug_malloc_touch(obj);
  o=(struct output *)(Pike_fp->current_storage);

  if (o->obj)
  {
    if(o->obj->prog)
    {
#ifdef BLOCKING_CLOSE
      apply_low(o->obj,o->set_blocking_offset,0);
      pop_stack();
#endif
      push_int(0);
      apply(o->obj,"set_id",1);
      pop_stack();

      apply(o->obj,"close",0);
      pop_stack();

      if(!THISOBJ->prog)
	Pike_error("Pipe done callback destructed pipe.\n");
    }
    free_object(o->obj);
    noutputs--;
    o->obj=0;
    o->fd=-1;
  }
}
Exemple #28
0
void image_hrz_f_encode(INT32 args )
{
  struct object *io;
  struct image *i;
  struct pike_string *s;
  int x,y;
  get_all_args( NULL, args, "%o", &io);

  if(!(i = get_storage( io, image_program)))
    Pike_error("Wrong argument 1 to Image.HRZ.encode\n");

  s = begin_shared_string( 256*240*3 );

  memset(s->str, 0, s->len );
  for(y=0; y<240; y++)
    if(y < i->ysize)
      for(x=0; x<256; x++)
        if(x < i->xsize)
        {
          int in = (x + y*256)*3;
          rgb_group pix = i->img[y*i->xsize+x];
          s->str[in+0] = pix.r >> 2;
          s->str[in+1] = pix.g >> 2;
          s->str[in+2] = pix.b >> 2;
        }
Exemple #29
0
/* string get_version() */
static void f_get_version(INT32 args)
{
  int n;
  char version[AVS_SEARCHVERSION_MAXLEN]; 
  GET_PIKE_SEARCH();
  if (args)
    Pike_error("Too many arguments to Search->get_version()\n");

  THREADS_ALLOW();
  n = avs_getsearchversion(search->handle, version);
  THREADS_DISALLOW();
  if (n != AVS_OK)
    Pike_error("Search->get_version(): %s\n", avs_errmsg(n));

  push_text(version);
}
Exemple #30
0
static void parse_bmhd(struct BMHD *bmhd, unsigned char *s, ptrdiff_t len)
{
  if(len<20)
    Pike_error("Short BMHD chunk\n");

  bmhd->w = (s[0]<<8)|s[1];
  bmhd->h = (s[2]<<8)|s[3];
  bmhd->x = (EXTRACT_CHAR(s+4)<<8)|s[5];
  bmhd->y = (EXTRACT_CHAR(s+6)<<8)|s[7];
  bmhd->nPlanes = s[8];
  bmhd->masking = s[9];
  bmhd->compression = s[10];
  bmhd->pad1 = s[11];
  bmhd->transparentColor = (s[12]<<8)|s[13];
  bmhd->xAspect = s[14];
  bmhd->yAspect = s[15];
  bmhd->pageWidth = (EXTRACT_CHAR(s+16)<<8)|s[17];
  bmhd->pageHeight = (EXTRACT_CHAR(s+18)<<8)|s[19];

#ifdef ILBM_DEBUG
  fprintf(stderr, "w = %d, h = %d, x = %d, y = %d, nPlanes = %d,\n"
	  "masking = %d, compression = %d, pad1 = %d, transparentColor = %d,\n"
	  "xAspect = %d, yAspect = %d, pageWidth = %d, pageHeight = %d\n",
	  bmhd->w, bmhd->h, bmhd->x, bmhd->y, bmhd->nPlanes, bmhd->masking,
	  bmhd->compression, bmhd->pad1, bmhd->transparentColor, bmhd->xAspect,
	  bmhd->yAspect, bmhd->pageWidth, bmhd->pageHeight);
#endif
}