Example #1
0
static Eina_Bool
_etui_epub_opf_is_valid(Etui_Provider_Data *pd)
{
    char buf[PATH_MAX];
    Eina_File *f;

    snprintf(buf, sizeof(buf), "%s/%s", pd->doc.path, pd->container.content_file);
    buf[sizeof(buf) - 1] = '\0';
    f = eina_file_open(buf, EINA_FALSE);
    if (!f)
        return EINA_FALSE;

    if (!eina_simple_xml_parse((const char *)eina_file_map_all(f, EINA_FILE_POPULATE),
                               eina_file_size_get(f),
                               EINA_TRUE,
                               _etui_epub_opf_parse_cb, pd))
    {
        eina_file_close(f);
        return EINA_FALSE;
    }

    eina_file_close(f);

    return EINA_TRUE;
}
Example #2
0
Eina_Bool
shotgun_emoticon_custom_add(Shotgun_Auth *auth, const char *file, const char *text, const char *type)
{
   Shotgun_Custom_Emoticon *emo;
   char *data_base64,
         tmp_cid[63];
   size_t len,
          len64;
   const char *sha1;
   Eina_File *fh;
   const unsigned char *data;

   EINA_SAFETY_ON_NULL_RETURN_VAL(auth, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(file, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(text, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(type, EINA_FALSE);


   fh = eina_file_open(file, EINA_FALSE);
   if (!fh)
     {
        ERR("Couldnt open file %s", file);
        return EINA_FALSE;
     }

   len = eina_file_size_get(fh);

   data = (const unsigned char *)eina_file_map_all(fh, EINA_FILE_SEQUENTIAL);

   DBG("data[%p] len=%ld", data, len);
   DBG("data[1] = %c", data[1]);
   sha1 = sha1_buffer(data, len);

   data_base64 = shotgun_base64_encode(data, len, &len64);
   eina_file_map_free(fh, (void *)data);
   eina_file_close(fh);

   emo = calloc(1, sizeof(Shotgun_Custom_Emoticon));
   if (!emo) return EINA_FALSE;

   emo->file = eina_stringshare_add(file);
   emo->text = strdup(text);
   emo->type = strdup(type);
   emo->data_base64 = data_base64;
   sprintf(tmp_cid, "cid:sha1+%[email protected]", sha1);
   emo->cid = strdup(tmp_cid);

   auth->custom_emoticons = eina_inlist_append(auth->custom_emoticons, EINA_INLIST_GET(emo));
   return EINA_TRUE;
}
Example #3
0
static Eina_Bool
_etui_epub_ncx_is_valid(Etui_Provider_Data *pd)
{
    char buf[PATH_MAX];
    Etui_Epub_Opf_Manifest_Item *item;
    Eina_File *f;
    char *oebps_dir;

    if (!pd->opf.toc_id)
        return EINA_FALSE;

    item = eina_hash_find(pd->opf.manifest, pd->opf.toc_id);
    if (!item)
        return EINA_FALSE;

    oebps_dir = strdup(pd->container.content_file);
    if (!oebps_dir)
        return EINA_FALSE;

    snprintf(buf, sizeof(buf), "%s/%s/%s", pd->doc.path, dirname(oebps_dir), item->href);
    buf[sizeof(buf) - 1] = '\0';
    free(oebps_dir);
    f = eina_file_open(buf, EINA_FALSE);
    if (!f)
    {
        return EINA_FALSE;
    }

    pd->ncx.current_depth = -1;

    if (!eina_simple_xml_parse((const char *)eina_file_map_all(f, EINA_FILE_POPULATE),
                               eina_file_size_get(f),
                               EINA_TRUE,
                               _etui_epub_ncx_parse_cb, pd))
    {
        eina_file_close(f);
        return EINA_FALSE;
    }

    eina_file_close(f);

    return EINA_TRUE;
}
Example #4
0
File: infra.c Project: tg--/etvdb
/**
 * @brief Function to retrieve supported languages.
 *
 * This function parses a xml file containing tvdb language data,
 * and adds it to a hash table (which it initializes).
 * The xml data will be downloaded, or (optionally) read from a file.
 *
 * If you no longer need the data, you'll have to free the hashtable
 * (with eina_hash_free()), but not the data in the table.
 *
 * @param lang_file_path Path to a XML file containing TVDB's supported languages.
 * This allows to provide a custom XML language file.
 * It is recommended to pass NULL. In this case the languages.xml file provide with
 * etvdb will be used and, if not available, the languages will be retrieved online.
 *
 * @return a pointer to a Eina hashtable on success, NULL on failure
 *
 * @ingroup Infrastructure
 */
EAPI Eina_Hash *etvdb_languages_get(const char *lang_file_path)
{
	char uri[URI_MAX];
	Download xml;
	Eina_File *file = NULL;
	Eina_Hash *hash = NULL;
	Parser_Data pdata;

	hash = eina_hash_string_superfast_new(_hash_free_cb);

	if(!hash) {
		ERR("Hash table not valid or initialized.");
		return NULL;
	}

	/* use path passed to the function or default */
	if (lang_file_path) {
		file = eina_file_open(lang_file_path, EINA_FALSE);
	}
	else {
		file = eina_file_open(DATA_LANG_FILE_XML, EINA_FALSE);
	}

	/* if no local file can be read, download xml data */
	if (file) {
		xml.len = eina_file_size_get(file);
		xml.data = eina_file_map_all(file, EINA_FILE_POPULATE);
		if(!xml.data)
			return NULL;

		DBG("Read %s file with size %d", eina_file_filename_get(file), (int)xml.len);
	}
	else {
		snprintf(uri, URI_MAX, TVDB_API_URI"/%s/languages.xml", etvdb_api_key);
		CURL_XML_DL_MEM(xml, uri) {
			ERR("Couldn't get languages from server.");
			return NULL;
		}
	}
Example #5
0
EAPI Eet_Key *
eet_identity_open(const char               *certificate_file,
                  const char               *private_key_file,
                  Eet_Key_Password_Callback cb)
{
#ifdef HAVE_SIGNATURE
   /* Signature declarations */
   Eet_Key *key = NULL;
# ifdef HAVE_GNUTLS
   /* Gnutls private declarations */
   Eina_File *f = NULL;
   void *data = NULL;
   gnutls_datum_t load_file = { NULL, 0 };
   char pass[1024];

   if (!emile_cipher_init()) return NULL;

   /* Init */
   if (!(key = malloc(sizeof(Eet_Key))))
     goto on_error;

   key->references = 1;

   if (gnutls_x509_crt_init(&(key->certificate)))
     goto on_error;

   if (gnutls_x509_privkey_init(&(key->private_key)))
     goto on_error;

   /* Mmap certificate_file */
   f = eina_file_open(certificate_file, 0);
   if (!f)
     goto on_error;

   /* let's make mmap safe and just get 0 pages for IO erro */
   eina_mmap_safety_enabled_set(EINA_TRUE);

   data = eina_file_map_all(f, EINA_FILE_SEQUENTIAL);
   if (!data) goto on_error;

   /* Import the certificate in Eet_Key structure */
   load_file.data = data;
   load_file.size = eina_file_size_get(f);
   if (gnutls_x509_crt_import(key->certificate, &load_file,
                              GNUTLS_X509_FMT_PEM) < 0)
     goto on_error;

   eina_file_map_free(f, data);

   /* Reset values */
   eina_file_close(f);
   f = NULL;
   data = NULL;
   load_file.data = NULL;
   load_file.size = 0;

   /* Mmap private_key_file */
   f = eina_file_open(private_key_file, 0);
   if (!f)
     goto on_error;

   /* let's make mmap safe and just get 0 pages for IO erro */
   eina_mmap_safety_enabled_set(EINA_TRUE);

   data = eina_file_map_all(f, EINA_FILE_SEQUENTIAL);
   if (!data)
     goto on_error;

   /* Import the private key in Eet_Key structure */
   load_file.data = data;
   load_file.size = eina_file_size_get(f);
   /* Try to directly import the PEM encoded private key */
   if (gnutls_x509_privkey_import(key->private_key, &load_file,
                                  GNUTLS_X509_FMT_PEM) < 0)
     {
        /* Else ask for the private key pass */
         if (cb && cb(pass, 1024, 0, NULL))
           {
     /* If pass then try to decode the pkcs 8 private key */
               if (gnutls_x509_privkey_import_pkcs8(key->private_key, &load_file,
                                                    GNUTLS_X509_FMT_PEM, pass, 0))
                 goto on_error;
           }
         else
         /* Else try to import the pkcs 8 private key without pass */
         if (gnutls_x509_privkey_import_pkcs8(key->private_key, &load_file,
                                              GNUTLS_X509_FMT_PEM, NULL, 1))
           goto on_error;
     }

   eina_file_map_free(f, data);
   eina_file_close(f);

   return key;

on_error:
   if (data) eina_file_map_free(f, data);
   if (f) eina_file_close(f);

   if (key)
     {
        if (key->certificate)
          gnutls_x509_crt_deinit(key->certificate);

        if (key->private_key)
          gnutls_x509_privkey_deinit(key->private_key);

        free(key);
     }

# else /* ifdef HAVE_GNUTLS */
   /* Openssl private declarations */
   FILE *fp;
   EVP_PKEY *pkey = NULL;
   X509 *cert = NULL;

   if (!emile_cipher_init()) return NULL;

   /* Load the X509 certificate in memory. */
   fp = fopen(certificate_file, "rb");
   if (!fp)
     return NULL;

   cert = PEM_read_X509(fp, NULL, NULL, NULL);
   fclose(fp);
   if (!cert)
     goto on_error;

   /* Check the presence of the public key. Just in case. */
   pkey = X509_get_pubkey(cert);
   if (!pkey)
     goto on_error;

   /* Load the private key in memory. */
   fp = fopen(private_key_file, "rb");
   if (!fp)
     goto on_error;

   pkey = PEM_read_PrivateKey(fp, NULL, cb, NULL);
   fclose(fp);
   if (!pkey)
     goto on_error;

   /* Load the certificate and the private key in Eet_Key structure */
   key = malloc(sizeof(Eet_Key));
   if (!key)
     goto on_error;

   key->references = 1;
   key->certificate = cert;
   key->private_key = pkey;

   return key;

on_error:
   if (cert)
     X509_free(cert);

   if (pkey)
     EVP_PKEY_free(pkey);

# endif /* ifdef HAVE_GNUTLS */
#else
   (void) certificate_file;
   (void) private_key_file;
   (void) cb;
#endif /* ifdef HAVE_SIGNATURE */
   return NULL;
}
Example #6
0
static Eina_Bool
evas_image_load_file_head_ico(Evas_Img_Load_Params *ilp, const char *file, const char *key, int *error)
{
   Eina_File *f;
   void *map = NULL;
   size_t position = 0;
   unsigned short word;
   unsigned char byte;
   int wanted_w = 0, wanted_h = 0, w, h, cols, i, planes = 0,
      bpp = 0, pdelta, search = -1, have_choice = 0,
      hasa = 1;
   unsigned int bmoffset, bmsize, fsize;
   unsigned short reserved, type, count;
   struct {
      int pdelta;
      int w, h;
      int cols;
      int bpp, planes;
      int hot_x, hot_y;
      unsigned int bmoffset, bmsize;
   } chosen = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

   f = eina_file_open(file, EINA_FALSE);
   if (!f)
     {
	*error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
	return EINA_FALSE;
     }

   *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
   fsize = eina_file_size_get(f);
   if (fsize < (6 + 16 + 40)) goto close_file;

   map = eina_file_map_all(f, EINA_FILE_SEQUENTIAL);
   if (!map) goto close_file;

   // key:
   //   NULL == highest res
   //   biggest == highest res
   //   smallest == lowest res
   //
   //   smaller == next size SMALLER than load opts WxH (if possible)
   //   bigger == next size BIGGER than load opts WxH (if possible)
   //   more ?

   search = BIGGEST;
   if ((ilp->opts.w > 0) && (ilp->opts.h > 0))
     {
        wanted_w = ilp->opts.w;
        wanted_h = ilp->opts.h;
        search = SMALLER;
     }

   if (!read_ushort(map, fsize, &position, &reserved)) goto close_file;
   if (!read_ushort(map, fsize, &position, &type)) goto close_file;
   if (!read_ushort(map, fsize, &position, &count)) goto close_file;
   if (!((reserved == 0) && 
         ((type == ICON) || (type == CURSOR)) && (count > 0)))
      goto close_file;
   *error = EVAS_LOAD_ERROR_CORRUPT_FILE;

   if (key)
     {
        if (!strcmp(key, "biggest"))
          {
             wanted_w = 0;
             wanted_h = 0;
             search = BIGGEST;
             chosen.pdelta = 0;
          }
        else if (!strcmp(key, "smallest"))
          {
             wanted_w = 1;
             wanted_h = 1;
             search = SMALLEST;
             chosen.pdelta = 0x7fffffff;
          }
        else if (!strcmp(key, "smaller"))
          {
             chosen.pdelta = 0x7fffffff;
             search = SMALLER;
          }
        else if (!strcmp(key, "bigger"))
          {
             chosen.pdelta = 0x7fffffff;
             search = BIGGER;
          }
     }
   for (i = 0; i < count; i++)
     {
        unsigned char tw = 0, th = 0, tcols = 0;
        if (!read_uchar(map, fsize, &position, &tw)) goto close_file;
        w = tw;
        if (w <= 0) w = 256;
        if (!read_uchar(map, fsize, &position, &th)) goto close_file;
        h = th;
        if (h <= 0) h = 256;
        if (!read_uchar(map, fsize, &position, &tcols)) goto close_file;
        cols = tcols;
        if (cols <= 0) cols = 256;
        if (!read_uchar(map, fsize, &position, &byte)) goto close_file;
        if (!read_ushort(map, fsize, &position, &word)) goto close_file;
        if (type == CURSOR) planes = word;
        //else hot_x = word;
        if (!read_ushort(map, fsize, &position, &word)) goto close_file;
        if (type == CURSOR) bpp = word;
        //else hot_y = word;
        if (!read_uint(map, fsize, &position, &bmsize)) goto close_file;
        if (!read_uint(map, fsize, &position, &bmoffset)) goto close_file;
        if ((bmsize <= 0) || (bmoffset <= 0) || (bmoffset >= fsize)) goto close_file;
        if (search == BIGGEST)
          {
             pdelta = w * h;
             if ((!have_choice) ||
                 ((pdelta >= chosen.pdelta) &&
                     (((bpp >= 3) && (bpp >= chosen.bpp)) ||
                         ((bpp < 3) && (cols >= chosen.cols)))))
               {
                  have_choice = 1;
                  chosen.pdelta = pdelta;
                  chosen.w = w;
                  chosen.h = h;
                  chosen.cols = cols;
                  chosen.bpp = bpp;
                  chosen.planes = planes;
                  chosen.bmsize = bmsize;
                  chosen.bmoffset = bmoffset;
               }
          }
        else
          {
             if (search == SMALLEST)
               {
                  pdelta = w * h;
                  if ((!have_choice) ||
                       ((pdelta <= chosen.pdelta) &&
                           (((bpp >= 3) && (bpp >= chosen.bpp)) ||
                               ((bpp < 3) && (cols >= chosen.cols)))))
                    {
                       have_choice = 1;
                       chosen.pdelta = pdelta;
                       chosen.w = w;
                       chosen.h = h;
                       chosen.cols = cols;
                       chosen.bpp = bpp;
                       chosen.planes = planes;
                       chosen.bmsize = bmsize;
                       chosen.bmoffset = bmoffset;
                    }
               }
             else if (search == SMALLER)
               {
                  pdelta = (wanted_w * wanted_h) - (w * h);
                  if ((!have_choice) ||
                      ((w <= wanted_w) && (h <= wanted_h) &&
                          (pdelta <= chosen.pdelta) &&
                          (((bpp >= 3) && (bpp >= chosen.bpp)) ||
                              ((bpp < 3) && (cols >= chosen.cols)))))
                     {
                        have_choice = 1;
                        if (pdelta < 0) pdelta = 0x7fffffff;
                        chosen.pdelta = pdelta;
                        chosen.w = w;
                        chosen.h = h;
                        chosen.cols = cols;
                        chosen.bpp = bpp;
                        chosen.planes = planes;
                        chosen.bmsize = bmsize;
                        chosen.bmoffset = bmoffset;
                    }
               }
             else if (search == BIGGER)
               {
                  pdelta = (w * h) - (wanted_w * wanted_h);
                  if ((!have_choice) ||
                      ((w >= wanted_w) && (h >= wanted_h) &&
                          (pdelta <= chosen.pdelta) &&
                          (((bpp >= 3) && (bpp >= chosen.bpp)) ||
                              ((bpp < 3) && (cols >= chosen.cols)))))
                     {
                        have_choice = 1;
                        if (pdelta < 0) pdelta = 0x7fffffff;
                        chosen.pdelta = pdelta;
                        chosen.w = w;
                        chosen.h = h;
                        chosen.cols = cols;
                        chosen.bpp = bpp;
                        chosen.planes = planes;
                        chosen.bmsize = bmsize;
                        chosen.bmoffset = bmoffset;
                    }
               }
          }
     }
   if (chosen.bmoffset == 0) goto close_file;
   position = chosen.bmoffset;

   w = chosen.w;
   h = chosen.h;
   if ((w > 256) || (h > 256)) goto close_file;
   if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) ||
       IMG_TOO_BIG(w, h))
     {
        if (IMG_TOO_BIG(w, h))
           *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
        else
           *error = EVAS_LOAD_ERROR_GENERIC;
        goto close_file;
     }

   ilp->w = w;
   ilp->h = h;
   if (hasa) ilp->alpha = 1;

   eina_file_map_free(f, map);
   eina_file_close(f);

   *error = EVAS_LOAD_ERROR_NONE;
   return EINA_TRUE;

 close_file:
   if (map) eina_file_map_free(f, map);
   eina_file_close(f);
   return EINA_FALSE;
}
Example #7
0
static Eina_Bool
evas_image_load_file_data_ico(Evas_Img_Load_Params *ilp, const char *file, const char *key, int *error)
{
   Eina_File *f;
   void *map = NULL;
   size_t position = 0;
   unsigned short word;
   unsigned char byte;
   unsigned int dword;
   int wanted_w = 0, wanted_h = 0, w, h, cols, i, planes = 0,
      bpp = 0, pdelta, search = -1, have_choice = 0,
      stride, pstride, j, right_way_up = 0, diff_size = 0, cols2;
   unsigned int bmoffset, bmsize, bitcount, fsize,
      *pal, *surface, *pix, none_zero_alpha = 0;
   unsigned short reserved, type, count;
   unsigned char *maskbuf, *pixbuf, *p;
   struct {
      int pdelta;
      int w, h;
      int cols;
      int bpp, planes;
      int hot_x, hot_y;
      unsigned int bmoffset, bmsize;
   } chosen = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

   f = eina_file_open(file, EINA_FALSE);
   if (!f)
     {
	*error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
	return EINA_FALSE;
     }

   *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
   fsize = eina_file_size_get(f);
   if (fsize < (6 + 16 + 40)) goto close_file;

   map = eina_file_map_all(f, EINA_FILE_SEQUENTIAL);
   if (!map) goto close_file;

   // key:
   //   NULL == highest res
   //   biggest == highest res
   //   smallest == lowest res
   //
   //   smaller == next size SMALLER than load opts WxH (if possible)
   //   bigger == next size BIGGER than load opts WxH (if possible)
   //   more ?

   search = BIGGEST;
   if ((ilp->opts.w > 0) && (ilp->opts.h > 0))
     {
        wanted_w = ilp->opts.w;
        wanted_h = ilp->opts.h;
        search = SMALLER;
     }

   if (!read_ushort(map, fsize, &position, &reserved)) goto close_file;
   if (!read_ushort(map, fsize, &position, &type)) goto close_file;
   if (!read_ushort(map, fsize, &position, &count)) goto close_file;
   if (!((reserved == 0) &&
         ((type == ICON) || (type == CURSOR)) && (count > 0)))
      goto close_file;
   *error = EVAS_LOAD_ERROR_CORRUPT_FILE;

   if (key)
     {
        if (!strcmp(key, "biggest"))
          {
             wanted_w = 0;
             wanted_h = 0;
             search = BIGGEST;
             chosen.pdelta = 0;
          }
        else if (!strcmp(key, "smallest"))
          {
             wanted_w = 1;
             wanted_h = 1;
             search = SMALLEST;
             chosen.pdelta = 0x7fffffff;
          }
        else if (!strcmp(key, "smaller"))
          {
             chosen.pdelta = 0x7fffffff;
             search = SMALLER;
          }
        else if (!strcmp(key, "bigger"))
          {
             chosen.pdelta = 0x7fffffff;
             search = BIGGER;
          }
     }
   for (i = 0; i < count; i++)
     {
        unsigned char tw = 0, th = 0, tcols = 0;
        if (!read_uchar(map, fsize, &position, &tw)) goto close_file;
        w = tw;
        if (w <= 0) w = 256;
        if (!read_uchar(map, fsize, &position, &th)) goto close_file;
        h = th;
        if (h <= 0) h = 256;
        if (!read_uchar(map, fsize, &position, &tcols)) goto close_file;
        cols = tcols;
        if (cols <= 0) cols = 256;
        if (!read_uchar(map, fsize, &position, &byte)) goto close_file;
        if (!read_ushort(map, fsize, &position, &word)) goto close_file;
        if (type == 1) planes = word;
        //else hot_x = word;
        if (!read_ushort(map, fsize, &position, &word)) goto close_file;
        if (type == 1) bpp = word;
        //else hot_y = word;
        if (!read_uint(map, fsize, &position, &bmsize)) goto close_file;
        if (!read_uint(map, fsize, &position, &bmoffset)) goto close_file;
        if ((bmsize <= 0) || (bmoffset <= 0) || (bmoffset >= fsize)) goto close_file;
        if (search == BIGGEST)
          {
             pdelta = w * h;
             if ((!have_choice) ||
                 ((pdelta >= chosen.pdelta) &&
                     (((bpp >= 3) && (bpp >= chosen.bpp)) ||
                         ((bpp < 3) && (cols >= chosen.cols)))))
               {
                  have_choice = 1;
                  chosen.pdelta = pdelta;
                  chosen.w = w;
                  chosen.h = h;
                  chosen.cols = cols;
                  chosen.bpp = bpp;
                  chosen.planes = planes;
                  chosen.bmsize = bmsize;
                  chosen.bmoffset = bmoffset;
               }
          }
        else
          {
             if (search == SMALLEST)
               {
                  pdelta = w * h;
                  if ((!have_choice) ||
                       ((pdelta <= chosen.pdelta) &&
                           (((bpp >= 3) && (bpp >= chosen.bpp)) ||
                               ((bpp < 3) && (cols >= chosen.cols)))))
                    {
                       have_choice = 1;
                       chosen.pdelta = pdelta;
                       chosen.w = w;
                       chosen.h = h;
                       chosen.cols = cols;
                       chosen.bpp = bpp;
                       chosen.planes = planes;
                       chosen.bmsize = bmsize;
                       chosen.bmoffset = bmoffset;
                    }
               }
             else if (search == SMALLER)
               {
                  pdelta = (wanted_w * wanted_h) - (w * h);
                  if ((!have_choice) ||
                      ((w <= wanted_w) && (h <= wanted_h) &&
                          (pdelta <= chosen.pdelta) &&
                          (((bpp >= 3) && (bpp >= chosen.bpp)) ||
                              ((bpp < 3) && (cols >= chosen.cols)))))
                     {
                        have_choice = 1;
                        if (pdelta < 0) pdelta = 0x7fffffff;
                        chosen.pdelta = pdelta;
                        chosen.w = w;
                        chosen.h = h;
                        chosen.cols = cols;
                        chosen.bpp = bpp;
                        chosen.planes = planes;
                        chosen.bmsize = bmsize;
                        chosen.bmoffset = bmoffset;
                    }
               }
             else if (search == BIGGER)
               {
                  pdelta = (w * h) - (wanted_w * wanted_h);
                  if ((!have_choice) ||
                      ((w >= wanted_w) && (h >= wanted_h) &&
                          (pdelta <= chosen.pdelta) &&
                          (((bpp >= 3) && (bpp >= chosen.bpp)) ||
                              ((bpp < 3) && (cols >= chosen.cols)))))
                     {
                        have_choice = 1;
                        if (pdelta < 0) pdelta = 0x7fffffff;
                        chosen.pdelta = pdelta;
                        chosen.w = w;
                        chosen.h = h;
                        chosen.cols = cols;
                        chosen.bpp = bpp;
                        chosen.planes = planes;
                        chosen.bmsize = bmsize;
                        chosen.bmoffset = bmoffset;
                    }
               }
          }
     }
   if (chosen.bmoffset == 0) goto close_file;
   position = chosen.bmoffset;

   w = chosen.w;
   h = chosen.h;
   cols = chosen.cols;
   bpp = chosen.bpp;
   // changed since we loaded header?
   if (((int)ilp->w != w) || ((int)ilp->h != h)) goto close_file;

   // read bmp header time... let's do some checking
   if (!read_uint(map, fsize, &position, &dword)) goto close_file; // headersize - dont care
   if (!read_uint(map, fsize, &position, &dword)) goto close_file; // width
   if (dword > 0)
     {
        if ((int)dword != w)
          {
             w = dword;
             diff_size = 1;
          }
     }
   if (!read_uint(map, fsize, &position, &dword)) goto close_file; // height
   if (dword > 0)
     {
        if ((int)dword != (h * 2))
          {
             h = dword / 2;
             diff_size = 1;
          }
     }
   if (diff_size)
     {
        ERR("Broken ICO file: %s - "
            "  Reporting size of %ix%i in index, but bitmap is %ix%i. "
            "  May be expanded or cropped.",
            file, ilp->w, ilp->h, w, h);
     }
   if (!read_ushort(map, fsize, &position, &word)) goto close_file; // planes
   //planes2 = word;
   if (!read_ushort(map, fsize, &position, &word)) goto close_file; // bitcount
   bitcount = word;
   if (!read_uint(map, fsize, &position, &dword)) goto close_file; // compression
   //compression = dword;
   if (!read_uint(map, fsize, &position, &dword)) goto close_file; // imagesize
   //imagesize = dword;
   if (!read_uint(map, fsize, &position, &dword)) goto close_file; // z pixels per m
   if (!read_uint(map, fsize, &position, &dword)) goto close_file; // y pizels per m
   if (!read_uint(map, fsize, &position, &dword)) goto close_file; // colors used
   //colorsused = dword;
   if (!read_uint(map, fsize, &position, &dword)) goto close_file; // colors important
   //colorsimportant = dword;

   surface = ilp->buffer;
   if (!surface)
     {
        *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
        goto close_file;
     }
   memset(surface, 0, ilp->w * ilp->h * 4);

   if (!((bitcount == 1) || (bitcount == 4) || (bitcount == 8) ||
         (bitcount == 24) || (bitcount == 32)))
      goto close_file;
   if (bitcount <= 8)
     {
        cols2 = 1 << bitcount;
        if (cols == 0) cols = cols2;
        if (cols > cols2) cols = cols2;
        if (cols > 256) cols = 256;
     }
   else
      cols = 0;
   if (bitcount > 8) cols = 0;

   pal = alloca(256 * 4);
   for (i = 0; i < cols; i++)
     {
        unsigned char a, r, g, b;

        if (!read_uchar(map, fsize, &position, &b)) goto close_file;
        if (!read_uchar(map, fsize, &position, &g)) goto close_file;
        if (!read_uchar(map, fsize, &position, &r)) goto close_file;
        if (!read_uchar(map, fsize, &position, &a)) goto close_file;
        a = 0xff;
        pal[i] = ARGB_JOIN(a, r, g, b);
     }
   stride = ((w + 31) / 32);
   maskbuf = alloca(stride * h);
   pixbuf = alloca(stride * 32 * 4); // more than enough
   if (bitcount == 1)
     {
        pstride = stride * 4;
        for (i = 0; i < h; i++)
          {
             pix = surface + (i * ilp->w);
             if (!right_way_up) pix = surface + ((ilp->h - 1 - i) * ilp->w);
             if (!read_mem(map, fsize, &position, pixbuf, pstride)) goto close_file;
             p = pixbuf;
             if (i >= (int)ilp->h) continue;
             for (j = 0; j < w; j++)
               {
                  if (j >= (int)ilp->w) break;
                  if ((j & 0x7) == 0x0)
                    {
                       *pix = pal[*p >> 7];
                    }
                  else if ((j & 0x7) == 0x1)
                    {
                       *pix = pal[(*p >> 6) & 0x1];
                    }