Example #1
0
/* Test writing single values of various types to board's global memory.
 * The test passes if we read back what we wrote. */
void test_small_writes  (ssize_t f) {

  unsigned char     uc;
  unsigned short    us;
  unsigned int      ui;
  unsigned long int uli;

  write_uchar     (f, 0, 0, 19);
  uc = read_uchar (f, 0, 0);
  assert (uc == 19);
  
  write_ushort    (f, 0, 0, 13);
  us = read_ushort(f, 0, 0);
  assert (us == 13);

  write_ulong      (f, 0, 0, 0x3037383633352030);
  uli = read_ulong (f, 0, 0);
  assert (uli == 0x3037383633352030);
  
  write_uint     (f, 0, 0, 18987983);
  ui = read_uint (f, 0, 0);
  assert (ui == 18987983);
  
  printf ("test_small_writes PASSED\n");
}
Example #2
0
		size_t TouchEventMessage::deserialize(unsigned char *const read_buffer) {
			size_t len = getSize(); 
			read_uchar(read_buffer, 12, &_touch_event._event_type);
			read_float(read_buffer, 13, &_touch_event._x1);
			read_float(read_buffer, 17, &_touch_event._y1);
			read_float(read_buffer, 21, &_touch_event._x2);
			read_float(read_buffer, 25, &_touch_event._y2);
			return len;
		}
Example #3
0
void test_global_mem_write(ssize_t f) {
  unsigned char     uc;
  write_uchar     (f, 0, 0, 19);
  uc = read_uchar (f, 0, 0);
  if (uc != 19) {
    printf ("Write to global memory did not work!\n");
    printf ("If the board has removable DIMMs, make sure they are inserted fully and are good\n");
  }
}
Example #4
0
MapManager::MAPDATA MapManager::ReadMap( std::string Path )
{
	std::ifstream F;
	try
	{
		F.open(Path.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
		if(!F.is_open())
			throw FILE_NOT_OPENED;

		int /*std::ifstream::pos_type*/ fsize = (int)F.tellg();
		if(fsize <= 0)
			throw INVALID_FILE;
		F.seekg(0, std::ios::beg);

		char* temp = new char[fsize];
		F.read(temp, fsize);
		unsigned char* data = (unsigned char*)temp; //The char* must be converted in order to be interpreted correctly

		int cPos = 0; //To keep track that we don't extend beyond the data length

		unsigned char Version	= read_uchar(data, fsize, cPos);
		unsigned short Year		= read_ushort(data, fsize, cPos);
		unsigned char Month		= read_uchar(data, fsize, cPos);
		unsigned char Day		= read_uchar(data, fsize, cPos);
		std::string LevelName	= read_string(data, fsize, cPos, 25);
		int PlayerPosX			= read_int(data, fsize, cPos);
		int PlayerPosY			= read_int(data, fsize, cPos);

		std::vector<BACKGROUND_STRUCT> BGs;

		unsigned char numBG		= read_uchar(data, fsize, cPos);
		for(unsigned char i = 0; i < numBG; i++)
			BGs.push_back(BACKGROUND_STRUCT(BACKGROUND, read_uchar(data, fsize, cPos)));

		unsigned char numMid	= read_uchar(data, fsize, cPos);
		for(unsigned char i = 0; i < numMid; i++)
			BGs.push_back(BACKGROUND_STRUCT(MIDDLEGROUND, read_uchar(data, fsize, cPos)));

		unsigned char numFG		= read_uchar(data, fsize, cPos);
		for(unsigned char i = 0; i < numFG; i++)
			BGs.push_back(BACKGROUND_STRUCT(FOREGROUND, read_uchar(data, fsize, cPos)));


		unsigned short numObj	= read_ushort(data, fsize, cPos);
		std::vector<GAMEOBJECT_STRUCT> Objects;
		for(unsigned char i = 0; i < numObj; i++)
		{
			unsigned char TypeId = read_uchar(data, fsize, cPos);
			int PosX = read_int(data, fsize, cPos);
			int PosY = read_int(data, fsize, cPos);
			GAMEOBJECT_STRUCT Obj(TypeId, PosX, PosY);
			Objects.push_back(Obj);
		}

		if(cPos != fsize)
			Util::msgWarn(Util::BuildString("Read size does not match file size (%d byte / %dbyte), possibly corrupt file (%s).", cPos, fsize, Path.c_str()));

		Util::msgNote(Util::BuildString("Read map: %s", Path.c_str()));
		return MAPDATA(
			Version,
			LevelName,
			Year,
			Month,
			Day,
			sf::Vector2i(PlayerPosX,PlayerPosY),
			Objects,
			BGs,
			numBG,
			numMid,
			numFG,
			numObj
			);
	}
	catch (ERRORS e)
	{
		F.close();
		return MAPDATA(e);
	}
	F.close();

	return MAPDATA(INVALID_FILE);
}
Example #5
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 #6
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];
                    }