Esempio n. 1
0
int read_pagemap(char * path_buf, unsigned long virt_addr){
   printf("Big endian? %d\n", is_bigendian());
   f = fopen(path_buf, "rb");
   if(!f){
      printf("Error! Cannot open %s\n", path_buf);
      return -1;
   }
   
   //Shifting by virt-addr-offset number of bytes
   //and multiplying by the size of an address (the size of an entry in pagemap file)
   file_offset = virt_addr / getpagesize() * PAGEMAP_ENTRY;
   printf("Vaddr: 0x%lx, Page_size: %d, Entry_size: %d\n", virt_addr, getpagesize(), PAGEMAP_ENTRY);
   printf("Reading %s at 0x%llx\n", path_buf, (unsigned long long) file_offset);
   status = fseek(f, file_offset, SEEK_SET);
   if(status){
      perror("Failed to do fseek!");
      return -1;
   }
   errno = 0;
   read_val = 0;
   unsigned char c_buf[PAGEMAP_ENTRY];
   for(i=0; i < PAGEMAP_ENTRY; i++){
      c = getc(f);
      if(c==EOF){
         printf("\nReached end of the file\n");
         return 0;
      }
      if(is_bigendian())
           c_buf[i] = c;
      else
           c_buf[PAGEMAP_ENTRY - i - 1] = c;
      printf("[%d]0x%x ", i, c);
   }
   for(i=0; i < PAGEMAP_ENTRY; i++){
      //printf("%d ",c_buf[i]);
      read_val = (read_val << 8) + c_buf[i];
   }
   printf("\n");
   printf("Result: 0x%llx\n", (unsigned long long) read_val);
   //if(GET_BIT(read_val, 63))
   if(GET_BIT(read_val, 63))
      printf("PFN: 0x%llx\n",(unsigned long long) GET_PFN(read_val));
   else
      printf("Page not present\n");
   if(GET_BIT(read_val, 62))
      printf("Page swapped\n");
   fclose(f);
   return 0;
}
Esempio n. 2
0
bool SND_Out_File::write(const vec &v)
{
  if (!good())
    return false;

  int i;

  bool switch_endian = !is_bigendian(); // if LITTLE_ENDIAN than switch
  switch (header.encoding) {
  case enc_linear8 :
    for (i = 0; i < v.size(); i++)
      write_endian<char>(file, double_to_char(v(i) * 128.0), switch_endian);
    break;
  case enc_linear16 :
    for (i = 0; i < v.size(); i++)
      write_endian<short>(file, double_to_short(v(i) * 32768.0),
                          switch_endian);
    break;
  case enc_float :
    for (i = 0; i < v.size(); i++)
      write_endian<float>(file, static_cast<float>(v(i)), switch_endian);
    break;
  case enc_double :
    for (i = 0; i < v.size(); i++)
      write_endian<double>(file, static_cast<double>(v(i)), switch_endian);
    break;
  default :
    it_warning("SND_Out_File::write(): Unsupported encoding!");
    return false;
  }

  return file.good();
}
Esempio n. 3
0
bool SND_In_File::read(vec &v, int n)
{
  if (!good())
    return false;

  int i;

  bool switch_endian = !is_bigendian(); // if LITTLE_ENDIAN than switch
  v.set_size(n, false);
  switch (header.encoding) {
  case enc_linear8 :
    for (i = 0; i < n; i++)
      v(i) = read_endian<char>(file, switch_endian) / 128.0;
    break;
  case enc_linear16 :
    for (i = 0; i < n; i++)
      v(i) = read_endian<short>(file, switch_endian) / 32768.0;
    break;
  case enc_float :
    for (i = 0; i < n; i++)
      v(i) = read_endian<float>(file, switch_endian);
    break;
  case enc_double :
    for (i = 0; i < n; i++)
      v(i) = read_endian<double>(file, switch_endian);
    break;
  default :
    it_warning("SND_In_File::read(): Unsupported encoding!");
    return false;
  }
  return file.good();
}
bool read_n(FILE * file, T * data, size_t n)
{
	if (fread(data, sizeof(*data), n, file) != n)
		return false;
	if (is_bigendian()) {
		for (size_t i = 0; i < n; ++i)
			data[i] = swap_endian(data[i]);
	}
	return true;
}
Esempio n. 5
0
bool raw16be_write(const char *fname, const vec &v, bool append)
{
  ofstream file(fname, (append ? ios::app | ios::ate : ios::out | ios::trunc) | ios::binary);
  if (!file)
    return false;

  bool switch_endian = !is_bigendian(); // if LITTLE_ENDIAN than switch
  for (int i = 0; i < v.size(); i++)
    write_endian<short>(file, double_to_short(v(i) * 32768.0), switch_endian);

  return true;
}
bool write_n(FILE * file, const T * data, size_t n)
{
	if (is_bigendian()) {
		// This is not optimized for speed, however it's only big endian writing....
		bool okay = true;
		for (size_t i = 0; i < n; ++i) {
			T swapped = swap_endian(data[i]);
			okay &= fwrite(&swapped, sizeof(swapped), 1, file) == 1;
		}
		return okay;
	} else {
		return fwrite(data, sizeof(*data), n, file) == n;
	}
}
Esempio n. 7
0
bool raw16le_read(const char *fname, vec &v, int beg, int len)
{
  it_assert_debug(len >= 0, "raw16le_read()");
  ifstream file(fname, ios::in | ios::binary);
  if (!file)
    return false;

  bool switch_endian = is_bigendian(); // if BIG_ENDIAN than switch
  v.set_size(len, false);
  file.seekg(2 * beg);
  for (int i = 0; i < len; i++)
    v(i) = read_endian<short>(file, switch_endian) / 32768.0;

  return true;
}
Esempio n. 8
0
/*-----handle endianness---------------*/    
   int reverseInt(unsigned int i)
 {
    unsigned char c1, c2, c3, c4;
     
    if (is_bigendian())
    {
       return i;
    }else{
          c1=i&255;
          c2=(i>>8)&255;
          c3=(i>>16)&255;
          c4=(i>>24)&255;
    return ((int)c1<<24)+((int)c2<<16)+((int)c3<<8)+c4;
        }
 }
Esempio n. 9
0
int	inverse_int(int nb)
{
    int ret;

    ret = 0;
    if (!is_bigendian())
    {
        char* src = (char*) &nb;
        char* dst = (char*) &ret;
        int i;
        for (i = 0; i < 4; ++i)
            dst[i] = src[4 - i - 1];
        return (ret);
    }
    return (nb);
}
Esempio n. 10
0
int init_mic_input(void **device) {
  snd_pcm_hw_params_t *hwparams;
  snd_pcm_format_t format;
  unsigned int rate = DEFAULT_SAMPLE_RATE;
  int err;
  
  snd_pcm_hw_params_alloca(&hwparams);
  if (is_bigendian())
    format = SND_PCM_FORMAT_S16_BE;
  else
    format = SND_PCM_FORMAT_S16_LE;
  if ((err = snd_pcm_open((snd_pcm_t **)device, ALSA_AUDIO_DEVICE, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
    cosby_print_err("Uhhh... I don't think this computer has speakers (%d)\n",err);
    return -1;
  }

  if ((err = snd_pcm_hw_params_any((snd_pcm_t *)(*device), hwparams)) < 0) {
    cosby_print_err("Your sounds is really fd up dude. (%d)\n",err);
    return -1;
  }
  if ((err = snd_pcm_hw_params_set_rate_resample((snd_pcm_t *)(*device), hwparams, 0)) < 0) {
    cosby_print_err("Dude! I just said it's fine to resample and your soundscared freaked out on me (%d)\n",err);
    return -1;
  }
  if ((err = snd_pcm_hw_params_set_format((snd_pcm_t *)(*device), hwparams, format)) < 0) {
    cosby_print_err("Hey there. You don't support 16bit sound? Is this 1991? (%d)\n",err);
    return -1;
  }
  if ((err = snd_pcm_hw_params_set_channels((snd_pcm_t *)(*device), hwparams, 2)) < 0) {
    cosby_print_err("Dude! WTF! No stereo sound! (%d)\n",err);
    return -1;
  }
  if ((err = snd_pcm_hw_params_set_access((snd_pcm_t *)(*device), hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
    cosby_print_err("ALSA is seriously braindead. Consider writing a letter to the mailing list complaining. (%d)\n",err);
    return -1;
  }
  if ((err = snd_pcm_hw_params_set_rate_near((snd_pcm_t *)(*device), hwparams, &rate, 0)) < 0) {
    cosby_print_err("I think you have a sound card from 1990 (%d)\n",err);
    return -1;
  }
  if ((err = snd_pcm_hw_params((snd_pcm_t *)(*device), hwparams) < 0)) {
    cosby_print_err("Yur soundscard is br0kn!! (%d)\n",err);
    return -1;
  }
  return 0;
}
Esempio n. 11
0
uint64_t _ntoh64(char* buf)
{
	if(is_bigendian())
	{
		return *((uint64_t*)buf);
	}
	uint64_t v;
	((char*)&v)[7] = buf[0];
	((char*)&v)[6] = buf[1];
	((char*)&v)[7] = buf[2];
	((char*)&v)[7] = buf[3];
	((char*)&v)[7] = buf[4];
	((char*)&v)[7] = buf[5];
	((char*)&v)[7] = buf[6];
	((char*)&v)[7] = buf[7];
	return v;
}
Esempio n. 12
0
bool SND_Format::write_header(std::ostream &f)
{
  f.seekp(0);
  header.magic = SND_MAGIC;
  header.hdr_size = sizeof(header);
  memset(header.info, 0, SND_INFO_LEN);

  bool switch_endian = !is_bigendian(); // if LITTLE_ENDIAN than switch
  write_endian<unsigned int>(f, header.magic, switch_endian);
  write_endian<unsigned int>(f, header.hdr_size, switch_endian);
  write_endian<unsigned int>(f, header.data_size, switch_endian);
  write_endian<unsigned int>(f, header.encoding, switch_endian);
  write_endian<unsigned int>(f, header.sample_rate, switch_endian);
  write_endian<unsigned int>(f, header.channels, switch_endian);
  f.write(reinterpret_cast<char *>(&header.info), SND_INFO_LEN);

  return f.good();
}
Esempio n. 13
0
bool raw16le_read(const char *fname, vec &v)
{
  ifstream file(fname, ios::in | ios::binary);
  if (!file)
    return false;

  // Check size of a file
  file.seekg(0, ios::end);
  int size = int(file.tellg());
  file.seekg(0, ios::beg);

  bool switch_endian = is_bigendian(); // if BIG_ENDIAN than switch
  int n = size / 2; // short vs. byte
  v.set_size(n, false);
  for (int i = 0; i < n; i++)
    v(i) = read_endian<short>(file, switch_endian) / 32768.0;

  return true;
}
Esempio n. 14
0
int _hton64(char* buf,uint64_t v)
{
	if(is_bigendian())
	{
		*((uint64_t*)buf) = v;
		//memcpy(buf,&v,8);
	}
	else
	{
		buf[0] = ((char*)&v)[7];
		buf[1] = ((char*)&v)[6];
		buf[2] = ((char*)&v)[5];
		buf[3] = ((char*)&v)[4];
		buf[4] = ((char*)&v)[3];
		buf[5] = ((char*)&v)[2];
		buf[6] = ((char*)&v)[1];
		buf[7] = ((char*)&v)[0];
	}
	return 8;
}
Esempio n. 15
0
bool SND_Format::read_header(std::istream &f)
{
  bool switch_endian = !is_bigendian(); // if LITTLE_ENDIAN than switch
  f.seekg(0);
  header.magic = read_endian<unsigned int>(f, switch_endian);
  header.hdr_size = read_endian<unsigned int>(f, switch_endian);
  header.data_size = read_endian<unsigned int>(f, switch_endian);
  header.encoding = read_endian<unsigned int>(f, switch_endian);
  header.sample_rate = read_endian<unsigned int>(f, switch_endian);
  header.channels = read_endian<unsigned int>(f, switch_endian);
  f.read(header.info, SND_INFO_LEN);
  if (!f || header.magic != SND_MAGIC) {
    std::cerr << header.magic << " != " << SND_MAGIC << std::endl;
    it_warning("SND_Format::read_header(): This is not a .snd file!");
    return false;
  }
  f.seekg(header.hdr_size);

  return f.good();
}
int fill_table(void) {
	if (is_bigendian()) {
		exit(WRONG_ENDIAN);
	}

	add_instr(0105000, 0105077, clr, clr_disas, SA);
	add_instr(005000, 005077, clrb, clrb_disas, SA);
	add_instr(0110000, 0117777, mov, mov_disas, DA);
	add_instr(010000, 017777, movb, movb_disas, DA);
	add_instr(0105200, 0105277, inc, inc_disas, SA);
	add_instr(005200, 005277, incb, incb_disas, SA);
	add_instr(0120000, 0127777, cmp, cmp_disas, DA);
	add_instr(020000, 027777, cmpb, cmpb_disas, DA);
	add_instr(001000, 001377, bne, bne_disas, BR);
	add_instr(000100, 000177, jmp, jmp_disas, CTR_INT);
	add_instr(060000, 067777, add, add_disas, UN);
	add_instr(070000, 070777, mul, mul_disas, UN);
	add_instr(000000, 000000, halt, halt_disas, CTR);

	return 0;
}
void
check_flattened_format(char *file)
{
	int fd;
	struct makedumpfile_header fh;

	if (flattened_format)
		return;

	fd = open(file, O_RDONLY);
	if (fd < 0) {
		error(INFO, "unable to open dump file %s", file);
		return;
	}
	if (read(fd, &fh, sizeof(fh)) < 0) {
		error(INFO, "unable to read dump file %s", file);
		close(fd);
		return;
	}
	close(fd);

	if (!is_bigendian()){
		fh.type    = bswap_64(fh.type);
		fh.version = bswap_64(fh.version);
	}
	if ((strncmp(fh.signature, MAKEDUMPFILE_SIGNATURE, sizeof(MAKEDUMPFILE_SIGNATURE)) != 0) || 
	    (fh.type != TYPE_FLAT_HEADER))
		return;

	if (!read_all_makedumpfile_data_header(file))
		return;

	if (CRASHDEBUG(1))
		fprintf(fp, "%s: FLAT\n\n", file);

	fh_save = fh;

	flattened_format = TRUE;
}
Esempio n. 18
0
/* opens your speaker for output, and put the handle in device */
int init_speaker_output(void **device) {
  snd_pcm_hw_params_t *hwparams;
  snd_pcm_format_t format;
  unsigned int rate = DEFAULT_SAMPLE_RATE;
  int err;

  /* ALSA is... complicated.

     Writing code to list out all of the sound devices so the user can
     pick one using a sane name (not something like plughw:0,0) and
     then trying to find a mode with direct hardware support would
     probably be as much work as this entire program.

     I can't find an example of someone doing "the right thing."  They
     all start by assuming you want the default device and 44.1kHz
     16bit stereo interleaved audio. So, in true open source fashion,
     I punted and took the easy way out like everyone else.

     I guess I could provide a way to specify an obscure string for an
     alternate sound device, so there's *some* flexibility. But most
     Linux audio programs don't even bother, so I won't.  And, if you
     know what those strings mean, you're certainly smart enough to
     change the value and recompile.

     Speaking of complicated. What's going on that can't I declare a
     snd_pcm_hw_params_t on the stack? 
   */
  snd_pcm_hw_params_alloca(&hwparams);
  if (is_bigendian())
    format = SND_PCM_FORMAT_S16_BE;
  else
    format = SND_PCM_FORMAT_S16_LE;
  if ((err = snd_pcm_open((snd_pcm_t **)device, ALSA_AUDIO_DEVICE, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
    cosby_print_err( "Uhhh... I don't think this computer has speakers (%d)\n",err);
    return -1;
  }

  if ((err = snd_pcm_hw_params_any((snd_pcm_t *)(*device), hwparams)) < 0) {
    cosby_print_err("Your sounds is really fd up dude. (%d)\n",err);
    return -1;
  }
  if ((err = snd_pcm_hw_params_set_rate_resample((snd_pcm_t *)(*device), hwparams, 0)) < 0) {
    cosby_print_err("Dude! I just said it's fine to resample and your soundscared freaked out on me (%d)\n",err);
    return -1;
  }
  if ((err = snd_pcm_hw_params_set_format((snd_pcm_t *)(*device), hwparams, format)) < 0) {
    cosby_print_err("Hey there. You don't support 16bit sound? Is this 1991? (%d)\n",err);
    return -1;
  }
  if ((err = snd_pcm_hw_params_set_channels((snd_pcm_t *)(*device), hwparams, 1)) < 0) {
    cosby_print_err("Dude! WTF! No mono sound! (%d)\n",err);
    return -1;
  }
  if ((err = snd_pcm_hw_params_set_access((snd_pcm_t *)(*device), hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
    cosby_print_err("ALSA is seriously braindead. Consider writing a letter to the mailing list complaining. (%d)\n",err);
    return -1;
  }
  if ((err = snd_pcm_hw_params_set_rate_near((snd_pcm_t *)(*device), hwparams, &rate, 0)) < 0) {
    cosby_print_err("I think you have a sound card from 1990 (%d)\n",err);
    return -1;
  }
  if ((err = snd_pcm_hw_params((snd_pcm_t *)(*device), hwparams) < 0)) {
    cosby_print_err("Yur soundscard is br0kn!! (%d)\n",err);
    return -1;
  }

  return 0;
}
Esempio n. 19
0
template<typename T> inline void EndianConvertReverse(T& val)
{
	if (is_bigendian())
		ByteConverter::apply<T>(&val);
}
Esempio n. 20
0
int main(int argc, char **argv)
{
  int c, i, j, got, args, jsonlen, istart;
  int odim1, odim2, blen, pad;
  int idim1=0, idim2=0, bitpix=0, ncard=0;
  int verbose=0;
  size_t totbytes, dlen;
  char tbuf[SZ_LINE];
  char *buf=NULL;
  char *jsonheader=NULL;
  char *iname=NULL, *oname=NULL;
  FILE *ofp=NULL;
  Optinfo optinfo;
#if HAVE_CFITSIO
  int status = 0;
  int n;
  int hdutype;
  int maxcard, morekeys;
  int dims[2] = {0, 0};
  int block = 1;
  void *dbuf;
  double d1, d2, d3, d4;
  double cens[2] = {0.0, 0.0};
  char *s;
  char *filter=NULL;
  char *evtlist = DEF_EVTLIST;
  char card[81];
  char s1[BUFLEN], s2[BUFLEN], s3[BUFLEN], s4[BUFLEN];
  fitsfile *fptr=NULL, *ofptr=NULL;
#elif HAVE_FUNTOOLS
  char *s=NULL;
  int dtype;
  Fun ifun=NULL, tfun=NULL;
#endif

  /* we want the args in the same order in which they arrived, and
     gnu getopt sometimes changes things without this */
  putenv("POSIXLY_CORRECT=true");

  /* process switch arguments */
  while ((c = getopt(argc, argv, "b:e:f:s:v")) != -1){
    switch(c){
    case 'b':
#if HAVE_CFITSIO
      block = atoi(optarg);
#else
      fprintf(stderr, "warning: -b switch only for cfitsio (ignoring)\n");
#endif
      break;
    case 'e':
#if HAVE_CFITSIO
      evtlist = optarg;
#else
      fprintf(stderr, "warning: -e switch only for cfitsio (ignoring)\n");
#endif
      break;
    case 'f':
#if HAVE_CFITSIO
      filter = optarg;
#else
      fprintf(stderr, "warning: -f switch only for cfitsio (ignoring)\n");
#endif
      break;
    case 's':
#if HAVE_CFITSIO
      s = strdup(optarg);
      if( strlen(s) > BUFLEN ) s[BUFLEN-1] = '\0';
      if( sscanf(s, "%[0-9.*] @ %[-0-9.*] , %[0-9.*] @ %[-0-9.*]%n",
		 s1, s2, s3, s4, &n) == 4){
	dims[0] = atof(s1);
	cens[0] = atof(s2);
	dims[1] = atof(s3);
	cens[1] = atof(s4);
      }  else if(sscanf(s, "%[-0-9.*] : %[-0-9.*] , %[-0-9.*] : %[-0-9.*]%n",
			s1, s2, s3, s4, &n) == 4){
	d1 = atof(s1);
	d2 = atof(s2);
	d3 = atof(s3);
	d4 = atof(s4);
	dims[0] = d2 - d1 + 1;
	cens[0] = dims[0] / 2;
	dims[1] = d4 - d3 + 1;
	cens[1] = dims[1] / 2;
      } else {
	fprintf(stderr, "warning: unknown arg for -s switch (ignoring)\n");
      }
      if( s ) free(s);
#else
      fprintf(stderr, "warning: -s switch only for cfitsio (ignoring)\n");
#endif
     break;
    case 'v':
      verbose++;
      break;
    }
  }

  /* check for required arguments */
  args = argc - optind;
  if( args < 2 ){
    fprintf(stderr, "usage: %s iname oname\n", argv[0]);
    exit(1);
  }
  iname = argv[optind++];
  oname = argv[optind++];

  /* optional info */
  if( !(optinfo = (Optinfo)calloc(sizeof(OptinfoRec), 1)) ){
    fprintf(stderr, "ERROR: can't allocate optional info rec\n");
    exit(1);
  }

  /* open the input FITS file */
#if HAVE_CFITSIO
  fptr = openFITSFile(iname, evtlist, &hdutype, &status);
  errchk(status);
#elif HAVE_FUNTOOLS
  if( !(ifun = FunOpen(iname, "r", NULL)) ){
    fprintf(stderr, "ERROR could not open input FITS file: %s (%s)\n", 
	    iname, strerror(errno));
    exit(1);
  }
#endif

  /* save the input filename in the png file */
  optinfo->fitsname = iname;

  /* open the output PGN file */
  if( !strcmp(oname, "-") || !strcmp(oname, "stdout") ){
    ofp = stdout;
  } else if( !(ofp = fopen(oname, "w")) ){
    fprintf(stderr, "ERROR: could not create output PNG file: %s (%s)\n", 
	    oname, strerror(errno));
    exit(1);
  }

#if HAVE_CFITSIO
  switch(hdutype){
  case IMAGE_HDU:
    // get image array
    dbuf = getImageToArray(fptr, NULL, NULL, &idim1, &idim2, &bitpix, &status);
    errchk(status);
    fits_get_hdrspace(fptr, &maxcard, &morekeys, &status);
    errchk(status);
    ofptr = fptr;
    break;
  default:
    ofptr = filterTableToImage(fptr, filter, NULL, dims, cens, block, &status);
    errchk(status);
    // get image array
    dbuf = getImageToArray(ofptr, NULL, NULL, &idim1, &idim2, &bitpix, &status);
    errchk(status);
    // get number of keys
    fits_get_hdrspace(ofptr, &maxcard, &morekeys, &status);
    errchk(status);
    break;
  }

#elif HAVE_FUNTOOLS
  /* copy the input fits header into a FITS image header */
  if( !(tfun = (Fun)calloc(1, sizeof(FunRec))) ){
      fprintf(stderr, "ERROR: could not create tfun struct\n");
      exit(1);
  }
  _FunCopy2ImageHeader(ifun, tfun);
  /* and save for storage in the png file */
  optinfo->fitsheader = (char *)tfun->header->cards;

  /* get image parameters. its safe to do this before calling image get
     so long as we don't change bitpix before that call */
  FunInfoGet(ifun,
	     FUN_SECT_BITPIX,  &bitpix,
	     FUN_SECT_DIM1,    &idim1,
	     FUN_SECT_DIM2,    &idim2,
	     0);
#endif

  /* convert FITS header into a json string */
  snprintf(tbuf, SZ_LINE-1, "{\"js9Protocol\": %s, ", JS9_PROTOCOL);
  scat(tbuf, &jsonheader);
  snprintf(tbuf, SZ_LINE-1, "\"js9Endian\": \"%s\", ", JS9_ENDIAN);
  scat(tbuf, &jsonheader);
  snprintf(tbuf, SZ_LINE-1, "\"cardstr\": \"");
  scat(tbuf, &jsonheader);
  // concat header cards into a single string
#if HAVE_CFITSIO
  while( ++ncard <= maxcard ){
    fits_read_record(ofptr, ncard, card, &status);
    errchk(status);
    // change " to '
    for(i=0; i<80; i++){
      if( card[i] == '"' ){
	card[i] = '\'';
      }
    }
    snprintf(tbuf, SZ_LINE-1, "%-80s", card);
    scat(tbuf, &jsonheader);
  }
#elif HAVE_FUNTOOLS
  while( (s = FunParamGets(tfun, NULL, ++ncard, NULL, &dtype)) ){
    for(i=0; i<80; i++){
      if( s[i] == '"' ){
	s[i] = '\'';
      }
    }
    scat(s, &jsonheader);
    if( s ) free(s);
  }
#endif
  // end with the number of cards
  snprintf(tbuf, SZ_LINE-1, "\", \"ncard\": %d}", ncard);
  scat(tbuf, &jsonheader);

  /* we want the image buffer to start on an 8-byte boundary, 
     so make jsonheader + null byte end on one */
  pad = 8 - (strlen(jsonheader) % 8) - 1;
  for(i=0; i<pad; i++){
    strcat(jsonheader, " ");
  }
  /* get final length of json header */
  jsonlen = strlen(jsonheader);

  /* total length of the header + null + image we are storing */
  blen = ABS(bitpix/8);
  dlen = (size_t)idim1 * (size_t)idim2 * blen;
  totbytes = jsonlen + 1 + dlen;

  /* all of this should now fit into the png image */
  /* somewhat arbitrarily, we use idim1 for odim1, and adjust odim2 to fit */
  odim1 = idim1;
  odim2 = (int)(((totbytes + odim1 - 1) / odim1) + (COLOR_CHANNELS-1)) / COLOR_CHANNELS;

  /* allocate buf to hold json header + null byte + RGB image */
  if( !(buf=calloc(COLOR_CHANNELS, odim1 * odim2)) ){
    fprintf(stderr, "ERROR: can't allocate image buf\n");
    exit(1);
  }

  /* move the json header into the output buffer */
  memmove(buf, jsonheader, jsonlen);
  /* add a null byte to signify end of json header */
  buf[jsonlen] = '\0';

  /* offset into image buffer where image starts, past header and null byte */
  istart = jsonlen + 1;

  /* debug output */
  if( verbose ){
    fprintf(stderr, 
    "idim=%d,%d (bitpix=%d jsonlen=%d istart=%d endian=%s) [%ld] -> odim=%d,%d [%d]\n", 
	    idim1, idim2, bitpix, jsonlen, istart, JS9_ENDIAN, totbytes, 
	    odim1, odim2, odim1 * odim2 * COLOR_CHANNELS);
  }

#if HAVE_CFITSIO
  /* move the json header into the output buffer */
  memmove(&buf[istart], dbuf, dlen);
#elif HAVE_FUNTOOLS
  /* extract and bin the data section into an image buffer */
  if( !FunImageGet(ifun, &buf[istart], NULL) ){
    fprintf(stderr, "ERROR: could not FunImageGet: %s\n", iname);
    exit(1);
  }
#endif

  /* debugging output to check against javascript input */
  if( verbose > 1 ){
    fprintf(stderr, "jsonheader: %s\n", jsonheader);
    for(j=0; j<idim2; j++){
      fprintf(stderr, "data #%d: ", j);
      for(i=0; i<idim1; i++){
	switch(bitpix){
	case 8:
	  fprintf(stderr, "%d ", 
		  *(unsigned char *)(buf + istart + ((j * idim1) + i) * blen));
	  break;
	case 16:
	  fprintf(stderr, "%d ", 
		  *(short *)(buf + istart + ((j * idim1) + i) * blen));
	  break;
	case -16:
	  fprintf(stderr, "%d ", 
		  *(unsigned short *)(buf + istart + ((j * idim1) + i) * blen));
	  break;
	case 32:
	  fprintf(stderr, "%d ",
		  *(int *)(buf + istart + ((j * idim1) + i) * blen));
	  break;
	case -32:
	  fprintf(stderr, "%.3f ",
		  *(float *)(buf + istart + ((j * idim1) + i) * blen));
	  break;
	case -64:
	  fprintf(stderr, "%.3f ", 
		  *(double *)(buf + istart + ((j * idim1) + i) * blen));
	  break;
	}
      }
      fprintf(stderr, "\n");
    }
    fprintf(stderr, "\n");
  }

  /* might have to swap to preferred endian for png creation */
  if( (!strncmp(JS9_ENDIAN, "l", 1) &&  is_bigendian()) ||
      (!strncmp(JS9_ENDIAN, "b", 1) && !is_bigendian()) ){
    swap_data(&buf[istart], idim1 * idim2, bitpix/8);
  }

  /* write the PNG file */
  got = writePNG(ofp, buf, odim1, odim2, optinfo);

  /* free up space */
  if( buf ) free(buf);
  if( optinfo ) free(optinfo);
  if( jsonheader ) free(jsonheader);

  /* close files */
#if HAVE_CFITSIO
  status = 0;
  if( ofptr && (ofptr != fptr) ) closeFITSFile(ofptr, &status);
  if( fptr ) closeFITSFile(fptr, &status);
  if( dbuf ) free(dbuf);
#elif HAVE_FUNTOOLS
  if( ifun ) FunClose(ifun);
  if( tfun ){
    FunClose(tfun);
    free(tfun);
  }
#endif
  if( ofp) fclose(ofp);

  /* return the news */
  return got;
}
static unsigned long long
store_flat_data_array(char *file, struct flat_data **fda)
{
	int			result = FALSE, fd;
	int64_t			offset_fdh;
	unsigned long long	num_allocated = 0;
	unsigned long long	num_stored    = 0;
	unsigned long long	size_allocated;
	struct flat_data	*ptr = NULL, *cur;
	struct makedumpfile_data_header	fdh;

	fd = open(file, O_RDONLY);
	if (fd < 0) {
		error(INFO, "unable to open dump file %s", file);
		return -1;
	}
	if (lseek(fd, MAX_SIZE_MDF_HEADER, SEEK_SET) < 0) {
		error(INFO, "unable to seek dump file %s", file);
		close(fd);
		return -1;
	}
	while (1) {
		if (num_allocated <= num_stored) {
			num_allocated += 100;
			size_allocated = sizeof(struct flat_data)
					 * num_allocated;
			ptr = realloc(ptr, size_allocated);
			if (ptr == NULL) {
				error(INFO, "unable to allocate");
				break;
			}
		}
		offset_fdh = lseek(fd, 0x0, SEEK_CUR);

		if (read(fd, &fdh, sizeof(fdh)) < 0) {
			error(INFO, "unable to read dump file %s", file);
			break;
		}
		if (!is_bigendian()){
			fdh.offset   = bswap_64(fdh.offset);
			fdh.buf_size = bswap_64(fdh.buf_size);
		}
		if (fdh.offset == END_FLAG_FLAT_HEADER) {
			result = TRUE;
			break;
		}
		cur = ptr + num_stored;
		cur->off_flattened  = offset_fdh + sizeof(fdh);
		cur->off_rearranged = fdh.offset;
		cur->buf_size       = fdh.buf_size;
		num_stored++;

		/* seek for next makedumpfile_data_header. */
		if (lseek(fd, fdh.buf_size, SEEK_CUR) < 0) {
			error(INFO, "unable to seek dump file %s", file);
			break;
		}
	}
	close(fd);
	if (result == FALSE) {
		free(ptr);
		return -1;
	}
	*fda = ptr;

	return num_stored;
}
Esempio n. 22
0
int main()
{
	printf("%d\n", check_cpu_endian());
	printf("%d\n", check_cpu_endian2());
	printf("%d\n", check_cpu_endian3());
	printf("%d\n", is_bigendian());
	

	printf("sizeof(short int)=%lu\n", sizeof(short int));
	printf("sizeof(int)=%lu\n", sizeof(int));
	printf("sizeof(long int)=%lu\n", sizeof(long int));
	printf("sizeof(long long int)=%lu\n", sizeof(long long int));

	printf("~0=0x%X\n", (uint8_t)~0);
	printf("~0 << 8=0x%X\n", (uint8_t)~0 << 8);
	printf("~0 << 56=0x%lX\n", (uint64_t)((uint8_t)~0) << 56);

	int aa = -3;
	unsigned int bb = aa;
	int cc = bb;
	printf("int %d; uint %u int %d\n", aa, bb, cc);
	/*
	uint32_t dd = UINT32_MAX;
	int32_t ee = dd;
	printf("uintmax %u int %d\n", dd, ee);
	*/

	puts("32-------------------");
	int n1 = 0x12345678;
	unsigned char* pc = (unsigned char*)(&n1);
	printf("cpu order:\n");
	print_endian(pc, 32);

	n1 = htonl(n1);
	printf("htonl order:\n");
	print_endian(pc, 32);

	n1 = BigLittleSwap32(n1);
	printf("BigLittleSwap32 order:\n");
	print_endian(pc, 32);

	puts("64-------------------");

	uint64_t n2 = 0x1A2B3C4D12345678;
	pc = (unsigned char*)(&n2);
	printf("cpu order:\n");
	print_endian(pc, 64);

	n2 = BigLittleSwap64(n2);
	printf("BigLittleSwap64 order:\n");
	print_endian(pc, 64);

	n2 = __bswap_64(n2);
	printf("__bswap_64 order:\n");
	print_endian(pc, 64);

	n2 = LITTLE_ENDIAN_CHANGE(n2, 64);
	printf("LITTLE_ENDIAN_CHANGE64 order:\n");
	print_endian(pc, 64);

	n2 = BIG_ENDIAN_CHANGE(n2, 64);
	printf("BIG_ENDIAN_CHANGE64 order:\n");
	print_endian(pc, 64);


	puts("16-------------------");

	uint16_t n3 = 0x1234;
	pc = (unsigned char*)(&n3);
	printf("cpu order:\n");
	print_endian(pc, 16);

	n3 = BigLittleSwap16(n3);
	printf("BigLittleSwap16 order:\n");
	print_endian(pc, 16);


	n3 = little_endian_chg16(n3);
	printf("little_endian_chg16 order:\n");
	print_endian(pc, 16);

	n3 = LITTLE_ENDIAN_CHANGE(n3, 16);
	printf("LITTLE_ENDIAN_CHANGE16 order:\n");
	print_endian(pc, 16);

	n3 = BIG_ENDIAN_CHANGE(n3, 16);
	printf("BIG_ENDIAN_CHANGE16 order:\n");
	print_endian(pc, 16);


	n3 = stream_ltt_bit16(pc, 16);
	printf("stream_ltt_bit16 order:\n");
	print_endian(pc, 16);

	n3 = stream_big_bit16(pc, 16);
	printf("stream_big_bit16 order:\n");
	print_endian(pc, 16);

	puts("16-------------------");
	uint16_t tn = 0x1234;
	print_endian((unsigned char*)(&tn), 16);

	uint64_t n4 = stream_ltt_bit64((unsigned char*)(&tn), 16);
	pc = (unsigned char*)(&n4);
	printf("stream_ltt_bit64 order:\n");
	print_endian(pc, 64);


	n4 = stream_big_bit64((unsigned char*)(&tn), 16);
	pc = (unsigned char*)(&n4);
	printf("stream_big_bit64 order:\n");
	print_endian(pc, 64);


	puts("32-------------------");	
	char buff[4] = {'\x12', '\x34', '\x56', '\x78'};
	print_endian((unsigned char *)buff, 32);
	const char *p = buff;
	printf("p=%p\n", p);
	int m0 = stream_ltt_bit32(&p, 4);
	printf("p=%p m=0x%X\n", p, m0);

	char buff2[4];
	char *p2 = buff2;
	printf("p=%p\n", p2);
	p2 = bit32_ltt_stream(m0, p2, 4);
	print_endian((unsigned char *)buff2, 32);
	printf("p=%p\n", p2);
	


	return 0;

}
Esempio n. 23
0
        out << " + " << i << c;
    } else {
        out << " - " << (-i) << c;
    }
}

static bool is_bigendian() {
    double d = 1.0;

    unsigned char *chr = reinterpret_cast<unsigned char *>( &d );

    return ( chr[0] == 0 );
}

template <typename T>
short Support<T>::bigendian = is_bigendian();

template class Support<double>;

template <> const double Support<double>::POS_INF =  std::numeric_limits<double>::infinity();
template <> const double Support<double>::NEG_INF = -std::numeric_limits<double>::infinity();
template <> const double Support<double>::NaN = std::log(-1.0);
template <> const double Support<double>::PI =  3.1415926535897932385;   // evalf[20](Pi)
template <> const double Support<double>::PI2 = 1.5707963267948966192;   // evalf[20](Pi/2)
template <> const double Support<double>::GAMMA_TAB[171] = {
    1.0, 1.0, 2.0, 6.0, 24.0, 120.0, 720.0, 5040.0, 40320.0,
    362880.0, 3628800.0, 39916800.0, 479001600.0, 6227020800.0,
    87178291200.0, 1307674368000.0, 20922789888000.0,
    355687428096000.0, 6402373705728000.0, 121645100408832000.0,
    2432902008176640000.0, 51090942171709440000.0,
    0.11240007277776076800e22, .25852016738884976640e23,
Esempio n. 24
0
  unsigned int h2 = 0;
  p = (unsigned char *) &c;
  for(size_t j = 0; j < sizeof(c); ++j) {
    h2 *= std::numeric_limits<unsigned char>::max() + 2U;
    h2 += p[j];
  }
  return (h1 + differ++) ^ h2;
}



// ----------------------------------------------------------------------
// ActiveDSFMT (DSFMT_19937_RNG)
// ----------------------------------------------------------------------
template <>
const bool ActiveDSFMT::bigendian = is_bigendian();

#if defined(__SSE2__)
template <>
const __m128i ActiveDSFMT::sse2_param_mask = _mm_set_epi32(ActiveDSFMT::MSK32_3, ActiveDSFMT::MSK32_4, ActiveDSFMT::MSK32_1, ActiveDSFMT::MSK32_2);
#endif // __SSE2__
}

/*
 *Global Seed Provider class definition.
 *
 * Provides unique seeds for thread-safe generators running in each thread.
 */
class GlobalSeedProvider
{
  static const unsigned int default_first_seed = 4257U;