Ejemplo n.º 1
0
void
evermore80(Place pl, int baud)
{
	char buf[32], *s;
	long now, seconds, week;

	fprint(2, "Evermore80");

	time(&now);
	seconds = now - 315964800;
	week = (seconds / (7*24*3600));
	seconds = seconds %  (7*24*3600);
	s = buf;

	s = putbyte(s, 0x80);		/* message ID */
	s = putshort(s, week);		/* week number */
	s = putlong(s, seconds*100);	/* seconds */
	s = putshort(s, pl.lat*10.0);	/* latitude tenths degree */
	s = putshort(s, pl.lon*10.0);	/* longitude tenths degree */
	s = putshort(s, 100);		/* altitude meters */
	s = putshort(s, 0);		/* datumn ID */
	s = putbyte(s, 2);		/* warm start */
	s = putbyte(s, GGAon|GSAon|GSVon|RMCon|CRCon);
	switch(baud){
	case 4800:	s = putbyte(s, 0);	break;
	case 9600:	s = putbyte(s, 1);	break;
	case 19200:	s = putbyte(s, 2);	break;
	case 38400:	s = putbyte(s, 3);	break;
	default:
		sysfatal("Illegal baud rate");
	}

	evermoresend(buf, s - buf);
	fprint(2, "\n");
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
	FILE		*ifp, *ofp;
	int		flen;
	unsigned short	i;
	unsigned char	buf[65535];

	if (argc != 3)
		usage(*argv);

	if ((ifp = fopen(*(argv+1), "rb")) == NULL) {
		fprintf(stderr, "can't open input file for reading\n");
		exit(1);
	}

	flen = filelength(fileno(ifp));
	if (flen > 65535) {
		fprintf(stderr, "fatal: input file has more than 65535 samples\n");
		exit(1);
	} else if (flen < 50) {
		fprintf(stderr, "warning: input file is very short, really a sound sample?\n");
	}

	if(strcmp(argv[2], "-") == 0) {
		ofp = stdout;
	} else if ((ofp = fopen(*(argv+2), "wb")) == NULL) {
		fprintf(stderr, "can't open output file for writing\n");
		exit(1);
	}

	/* magic number for id's sfx format probably */
	i = 3;
	putshort(i, ofp);
	/* 11kHz sample rate */
	i = 11025;
	putshort(i, ofp);
	/* number of samples */
	i = (unsigned short) flen;
	putshort(i, ofp);
	/* separator, whatever */
	i = 0;
	putshort(i, ofp);

	fread(&buf[0], flen, 1, ifp);
	if(flen >= 4) {
		if(buf[0] == 'R' && buf[1] == 'I' && buf[2] == 'F' && buf[3] == 'F') {
			fprintf(stderr, "warning: input looks like a WAV file, you sure it's raw?\n");
		} else if(buf[0] == '\x03' && buf[1] == '\x00' && buf[2] == '\x11' && buf[3] == '\x2b') {
			fprintf(stderr, "warning: input already a sfx file?\n");
		}
	}
	fwrite(&buf[0], flen, 1, ofp);

	fclose(ifp);
	fclose(ofp);
	return 0;
}
Ejemplo n.º 3
0
static void ttf_dumpcvar(struct alltabs *at,MMSet *mm) {
    int16_t **deltas;
    int ptcnt, cnt, pcnt;
    int i, j, rj, big;
    int tuple_size;
    uint32_t start, end;
    uint16_t *pts;

    deltas=CvtFindDeltas(mm, &ptcnt);
    for (i=cnt=0; i < mm->instance_count; ++i)
        if (deltas[i] != NULL)
            ++cnt;
    if (cnt==0) {
        free(deltas);
        return;
    }

    tuple_size=4 + 2 * mm->axis_count;
    at->cvar=atmpfile();
    putlong(at->cvar, 0x00010000);	/* Format */
    putshort(at->cvar, cnt);	/* Number of instances with cvt tables (tuple count of interesting tuples) */
    putshort(at->cvar, 8 + cnt * tuple_size);	/* Offset to data */

    for (i=0; i < mm->instance_count; ++i)
        if (deltas[i] != NULL) {
            putshort(at->cvar, 0);	/* tuple data size, figure out later */
            putshort(at->cvar, 0xa000);	/* tuple coords follow, private points in data */
            for (j=0; j < mm->axis_count; ++j)
                putshort(at->cvar,
                         rint(16384 * mm->positions[i * mm->axis_count + j]));
        }
    if (aftell(at->cvar) != 8 + cnt * tuple_size)
        ErrorMsg(2,"Data offset wrong\n");

    for (i=cnt=0; i < mm->instance_count; ++i)
        if (deltas[i] != NULL) {
            start=aftell(at->cvar);
            for (j=pcnt=0; j < ptcnt; ++j)
                if (deltas[i][j] != 0)
                    ++pcnt;
            pts=malloc(pcnt * sizeof(uint16_t));
            for (j=pcnt=0; j < ptcnt; ++j)
                if (deltas[i][j] != 0)
                    pts[pcnt++]=j;

            if (pcnt > 0x7f) {
                aputc(0x80 | (pcnt >> 8), at->cvar);
                aputc(pcnt & 0xff, at->cvar);
            } else
Ejemplo n.º 4
0
void
CUTRDTTCPTransportTestDriver::testWritePacketEnoughData ()
{
    static BYTE buffer[] = "xxxxAbCdEfG";
    const int nBufSize = sizeof(buffer);
    const int nHdrSize = 4;
    const int nResSize = nBufSize - nHdrSize;
    BYTE *pResultBuffer = new BYTE[nResSize];

    buffer[0] = '$';
    buffer[1] = m_spTransport->m_tcpInterleave;
    putshort(&buffer[2], (UINT16)nResSize);
    memcpy (pResultBuffer, buffer + nHdrSize, nResSize);

    m_pContFact->CreateBuffer (m_spBuffer, (char*)pResultBuffer, nResSize);
    m_spBuffer->AddRef();       // writePacket assumes it can release the buffer

    m_spSocket->setExpectedMethod(CUTMockSocket::FTC_WRITE);
    m_spSocket->setExpectedArg(&buffer[0]);
    m_spSocket->setExpectedArg(nBufSize);

    CPPUNIT_ASSERT( SUCCEEDED( m_spTransport->writePacket(m_spBuffer.Ptr()) ) );

    CPPUNIT_ASSERT( m_spSocket->Validate() );

    delete[] pResultBuffer;
}
Ejemplo n.º 5
0
int main(int argc, char **argv)
{
	FILE		*ifp, *ofp;
	int		flen;
	unsigned short	i;
	unsigned char	buf[65535];

	if (argc != 3)
		usage(*argv);

	if ((ifp = fopen(*(argv+1), "rb")) == NULL) {
		fprintf(stderr, "can't open input file for reading\n");
		exit(1);
	}

	flen = filelength(fileno(ifp));
	if (flen > 65535) {
		fprintf(stderr, "input file has more than 65535 samples\n");
		exit(1);
	}

	if ((ofp = fopen(*(argv+2), "wb")) == NULL) {
		fprintf(stderr, "can't open output file for writing\n");
		exit(1);
	}

	/* magic number for id's sfx format probably */
	i = 3;
	putshort(i, ofp);
	/* 11kHz sample rate */
	i = 11025;
	putshort(i, ofp);
	/* number of samples */
	i = (unsigned short) flen;
	putshort(i, ofp);
	/* separator, whatever */
	i = 0;
	putshort(i, ofp);
	
	fread(&buf[0], flen, 1, ifp);
	fwrite(&buf[0], flen, 1, ofp);

	fclose(ifp);
	fclose(ofp);
	return 0;
}
Ejemplo n.º 6
0
void 
NetDifficulty::Packetize(uint8* buf, uint16& size)
{
	buf[0] = 'G';
	buf[1] = 'D';

	putshort(&buf[2], (uint16)m_difficulty->m_owner);
	memcpy(&buf[4], m_difficulty, sizeof(*m_difficulty));
	size = 4 + sizeof(*m_difficulty);
}
Ejemplo n.º 7
0
void
MBR_make(struct mbr *mbr, struct dos_mbr *dos_mbr)
{
	int i;

	memcpy(dos_mbr->dmbr_boot, mbr->code, sizeof(dos_mbr->dmbr_boot));
	putshort(&dos_mbr->dmbr_sign, DOSMBR_SIGNATURE);

	for (i = 0; i < NDOSPART; i++)
		PRT_make(&mbr->part[i], mbr->offset, mbr->reloffset,
		    &dos_mbr->dmbr_parts[i]);
}
Ejemplo n.º 8
0
Archivo: dns.c Proyecto: frekky/iodine
int
dns_encode_a_response(char *buf, size_t buflen, struct query *q)
/* Only used when iodined gets an A type query for ns.topdomain or www.topdomain */
/* Mostly same as dns_encode_ns_response() above */
{
	HEADER *header;
	int len;
	short name;
	char *ipp;
	char *p;

	if (buflen < sizeof(HEADER))
		return 0;

	memset(buf, 0, buflen);

	header = (HEADER*)buf;

	header->id = htons(q->id);
	header->qr = 1;
	header->opcode = 0;
	header->aa = 1;
	header->tc = 0;
	header->rd = 0;
	header->ra = 0;

	p = buf + sizeof(HEADER);

	header->qdcount = htons(1);
	header->ancount = htons(1);

	/* pointer to start of name */
	name = 0xc000 | ((p - buf) & 0x3fff);

	/* Query section */
	putname(&p, buflen - (p - buf), q->name);	/* Name */
	CHECKLEN(4);
	putshort(&p, q->type);			/* Type */
	putshort(&p, C_IN);			/* Class */

	/* Answer section */
	CHECKLEN(12);
	putshort(&p, name);			/* Name */
	putshort(&p, q->type);			/* Type */
	putshort(&p, C_IN);			/* Class */
	putlong(&p, 3600);			/* TTL */
	putshort(&p, 4);			/* Data length */

	/* ugly hack to output IP address */
	ipp = (char *) &q->destination;
	CHECKLEN(4);
	putbyte(&p, *(ipp++));
	putbyte(&p, *(ipp++));
	putbyte(&p, *(ipp++));
	putbyte(&p, *ipp);

	len = p - buf;
	return len;
}
Ejemplo n.º 9
0
static int
DumpArrayInt32(int dumpfd, char tag, register afs_uint32 * array,
	       register int nelem)
{
    char tbuffer[4];
    register afs_uint32 v;
    int code = 0;
    register byte *p = (unsigned char *)tbuffer;
    *p++ = tag;
    putshort(p, nelem);
    code = write(dumpfd, tbuffer, 3);
    if (code != 3)
	return VOLSERDUMPERROR;
    while (nelem--) {
	p = (unsigned char *)tbuffer;
	v = *array++;		/*this was register */

	putint32(p, v);
	code = write(dumpfd, tbuffer, 4);
	if (code != 4)
	    return VOLSERDUMPERROR;
    }
    return 0;
}
Ejemplo n.º 10
0
int SpriteCache::saveToFile(const char *filnam, int lastElement, bool compressOutput)
{
  FILE *output = fopen(filnam, "wb");
  if (output == NULL)
    return -1;

  if (compressOutput) {
    // re-open the file so that it can be seeked
    fclose(output);
    output = fopen(filnam, "r+b");
    if (output == NULL)
      return -1;
  }

  int spriteFileIDCheck = (int)time(NULL);

  // version 6
  putshort(6, output);

  fwrite(spriteFileSig, strlen(spriteFileSig), 1, output);

  fputc(compressOutput ? 1 : 0, output);
  putw(spriteFileIDCheck, output);

  int i, lastslot = 0;
  if (elements < lastElement)
    lastElement = elements;

  for (i = 1; i < lastElement; i++) {
    // slot empty
    if ((images[i] != NULL) || ((offsets[i] != 0) && (offsets[i] != sprite0InitialOffset)))
      lastslot = i;
  }

  putshort(lastslot, output);

  // allocate buffers to store the indexing info
  int numsprits = lastslot + 1;
  short *spritewidths = (short*)malloc(numsprits * sizeof(short));
  short *spriteheights = (short*)malloc(numsprits * sizeof(short));
  long *spriteoffs = (long*)malloc(numsprits * sizeof(long));

  const int memBufferSize = 100000;
  char *memBuffer = (char*)malloc(memBufferSize);

  for (i = 0; i <= lastslot; i++) {

    spriteoffs[i] = ftell(output);

    // if compressing uncompressed sprites, load the sprite into memory
    if ((images[i] == NULL) && (this->spritesAreCompressed != compressOutput))
      (*this)[i];

    if (images[i] != NULL) {
      // image in memory -- write it out
      pre_save_sprite(i);
      int bpss = bitmap_color_depth(images[i]) / 8;
      spritewidths[i] = images[i]->w;
      spriteheights[i] = images[i]->h;
      putshort(bpss, output);
      putshort(spritewidths[i], output);
      putshort(spriteheights[i], output);

      if (compressOutput) {
        long lenloc = ftell(output);
        // write some space for the length data
        putw(0, output);

        compressSprite(images[i], output);

        long fileSizeSoFar = ftell(output);
        // write the length of the compressed data
        fseek(output, lenloc, SEEK_SET);
        putw((fileSizeSoFar - lenloc) - 4, output);
        fseek(output, 0, SEEK_END);
      }
      else
        fwrite(&images[i]->line[0][0], spritewidths[i] * bpss, spriteheights[i], output);

      continue;
    }

    if ((offsets[i] == 0) || ((offsets[i] == sprite0InitialOffset) && (i > 0))) {
      // sprite doesn't exist
      putshort(0, output);
      spritewidths[i] = 0;
      spriteheights[i] = 0;
      spriteoffs[i] = 0;
      continue;
    }

    // not in memory -- seek to it in the source file
    seekToSprite(i);
    lastLoad = i;

    short colDepth = getshort(ff);
    putshort(colDepth, output);

    if (colDepth == 0) {
      continue;
    }

    if (this->spritesAreCompressed != compressOutput) {
      // shouldn't be able to get here
      free(memBuffer);
      fclose(output);
      return -2;
    }

    // and copy the data across
    int width = getshort(ff);
    int height = getshort(ff);

    spritewidths[i] = width;
    spriteheights[i] = height;

    putshort(width, output);
    putshort(height, output);

    int sizeToCopy;
    if (this->spritesAreCompressed) {
      sizeToCopy = getw(ff);
      putw(sizeToCopy, output);
    }
    else {
      sizeToCopy = width * height * (int)colDepth;
    }

    while (sizeToCopy > memBufferSize) {
      fread(memBuffer, memBufferSize, 1, ff);
      fwrite(memBuffer, memBufferSize, 1, output);
      sizeToCopy -= memBufferSize;
    }

    fread(memBuffer, sizeToCopy, 1, ff);
    fwrite(memBuffer, sizeToCopy, 1, output);
  }

  free(memBuffer);

  fclose(output);

  // write the sprite index file
  FILE *ooo = fopen(spindexfilename, "wb");
  // write "SPRINDEX" id
  fwrite(&spindexid[0], strlen(spindexid), 1, ooo);
  // write version (1)
  putw(2, ooo);
  putw(spriteFileIDCheck, ooo);
  // write last sprite number and num sprites, to verify that
  // it matches the spr file
  putw(lastslot, ooo);
  putw(numsprits, ooo);
  fwrite(&spritewidths[0], sizeof(short), numsprits, ooo);
  fwrite(&spriteheights[0], sizeof(short), numsprits, ooo);
  fwrite(&spriteoffs[0], sizeof(long), numsprits, ooo);
  fclose(ooo);

  free(spritewidths);
  free(spriteheights);
  free(spriteoffs);

  return 0;
}
Ejemplo n.º 11
0
Archivo: sgilib.c Proyecto: Minoos/gimp
sgi_t *
sgiOpenFile(FILE *file,	/* I - File to open */
            int  mode,	/* I - Open mode (SGI_READ or SGI_WRITE) */
            int  comp,	/* I - Type of compression */
            int  bpp,	/* I - Bytes per pixel */
            int  xsize,	/* I - Width of image in pixels */
            int  ysize,	/* I - Height of image in pixels */
            int  zsize)	/* I - Number of channels */
{
  int	i, j;		/* Looping var */
  char	name[80];	/* Name of file in image header */
  short	magic;		/* Magic number */
  sgi_t	*sgip;		/* New image pointer */


  if ((sgip = calloc(sizeof(sgi_t), 1)) == NULL)
    return (NULL);

  sgip->file = file;
  sgip->swapBytes = 0;

  switch (mode)
  {
    case SGI_READ :
        sgip->mode = SGI_READ;

        magic = getshort(sgip);
        if (magic != SGI_MAGIC)
        {
	  /* try little endian format */
	  magic = ((magic >> 8) & 0x00ff) | ((magic << 8) & 0xff00);
	  if(magic != SGI_MAGIC) {
            free(sgip);
            return (NULL);
          } else {
	    sgip->swapBytes = 1;
	  }
	}

        sgip->comp  = getc(sgip->file);
        sgip->bpp   = getc(sgip->file);
        getshort(sgip);		/* Dimensions */
        sgip->xsize = getshort(sgip);
        sgip->ysize = getshort(sgip);
        sgip->zsize = getshort(sgip);
        getlong(sgip);		/* Minimum pixel */
        getlong(sgip);		/* Maximum pixel */

        if (sgip->comp)
        {
         /*
          * This file is compressed; read the scanline tables...
          */

          fseek(sgip->file, 512, SEEK_SET);

          sgip->table    = calloc(sgip->zsize, sizeof(long *));
          sgip->table[0] = calloc(sgip->ysize * sgip->zsize, sizeof(long));
          for (i = 1; i < sgip->zsize; i ++)
            sgip->table[i] = sgip->table[0] + i * sgip->ysize;

          for (i = 0; i < sgip->zsize; i ++)
            for (j = 0; j < sgip->ysize; j ++)
              sgip->table[i][j] = getlong(sgip);
        };
        break;

    case SGI_WRITE :
	if (xsize < 1 ||
	    ysize < 1 ||
	    zsize < 1 ||
	    bpp < 1 || bpp > 2 ||
	    comp < SGI_COMP_NONE || comp > SGI_COMP_ARLE)
        {
          free(sgip);
          return (NULL);
        };

        sgip->mode = SGI_WRITE;

        putshort(SGI_MAGIC, sgip);
        putc((sgip->comp = comp) != 0, sgip->file);
        putc(sgip->bpp = bpp, sgip->file);
        putshort(3, sgip);		/* Dimensions */
        putshort(sgip->xsize = xsize, sgip);
        putshort(sgip->ysize = ysize, sgip);
        putshort(sgip->zsize = zsize, sgip);
        if (bpp == 1)
        {
          putlong(0, sgip);	/* Minimum pixel */
          putlong(255, sgip);	/* Maximum pixel */
        }
        else
        {
          putlong(-32768, sgip);	/* Minimum pixel */
          putlong(32767, sgip);	/* Maximum pixel */
        };
        putlong(0, sgip);		/* Reserved */

        memset(name, 0, sizeof(name));
        fwrite(name, sizeof(name), 1, sgip->file);

        for (i = 0; i < 102; i ++)
          putlong(0, sgip);

        switch (comp)
        {
          case SGI_COMP_NONE : /* No compression */
             /*
              * This file is uncompressed.  To avoid problems with sparse files,
              * we need to write blank pixels for the entire image...
              */

              if (bpp == 1)
              {
        	for (i = xsize * ysize * zsize; i > 0; i --)
        	  putc(0, sgip->file);
              }
              else
              {
        	for (i = xsize * ysize * zsize; i > 0; i --)
        	  putshort(0, sgip);
              };
              break;

          case SGI_COMP_ARLE : /* Aggressive RLE */
              sgip->arle_row    = (unsigned short *)calloc(xsize, sizeof(unsigned short));
              sgip->arle_offset = 0;

          case SGI_COMP_RLE : /* Run-Length Encoding */
             /*
              * This file is compressed; write the (blank) scanline tables...
              */

              for (i = 2 * ysize * zsize; i > 0; i --)
        	putlong(0, sgip);

              sgip->firstrow = ftell(sgip->file);
              sgip->nextrow  = ftell(sgip->file);
              sgip->table    = calloc(sgip->zsize, sizeof(long *));
              sgip->table[0] = calloc(sgip->ysize * sgip->zsize, sizeof(long));
              for (i = 1; i < sgip->zsize; i ++)
        	sgip->table[i] = sgip->table[0] + i * sgip->ysize;
              sgip->length    = calloc(sgip->zsize, sizeof(long *));
              sgip->length[0] = calloc(sgip->ysize * sgip->zsize, sizeof(long));
              for (i = 1; i < sgip->zsize; i ++)
        	sgip->length[i] = sgip->length[0] + i * sgip->ysize;
              break;
        };
        break;

    default :
        free(sgip);
        return (NULL);
  };
Ejemplo n.º 12
0
Archivo: dns.c Proyecto: frekky/iodine
int
dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr, char *data, size_t datalen)
{
	HEADER *header;
	short name;
	char *p;
	int len;
	int ancnt;

	if (buflen < sizeof(HEADER))
		return 0;

	memset(buf, 0, buflen);

	header = (HEADER*)buf;

	header->id = htons(q->id);
	header->qr = (qr == QR_ANSWER);
	header->opcode = 0;
	header->aa = (qr == QR_ANSWER);
	header->tc = 0;
	header->rd = (qr == QR_QUERY);
	header->ra = 0;

	p = buf + sizeof(HEADER);

	switch (qr) {
	case QR_ANSWER:
		header->qdcount = htons(1);

		name = 0xc000 | ((p - buf) & 0x3fff);

		/* Question section */
		putname(&p, buflen - (p - buf), q->name);

		CHECKLEN(4);
		putshort(&p, q->type);
		putshort(&p, C_IN);

		/* Answer section */

		if (q->type == T_CNAME || q->type == T_A ||
			q->type == T_PTR || q->type == T_AAAA ||
			q->type == T_A6 || q->type == T_DNAME) {
			/* data is expected to be like "Hblabla.host.name.com\0" */

			char *startp;
			int namelen;

			CHECKLEN(10);
			putshort(&p, name);
			if (q->type == T_A || q->type == T_AAAA)
				/* answer CNAME to A question */
				putshort(&p, T_CNAME);
			else
				putshort(&p, q->type);
			putshort(&p, C_IN);
			putlong(&p, 0);		/* TTL */

			startp = p;
			p += 2;			/* skip 2 bytes length */
			if (q->type == T_A6) {
				CHECKLEN(1);
				putbyte(&p, 128);
			}
			putname(&p, buflen - (p - buf), data);
			CHECKLEN(0);
			namelen = p - startp;
			namelen -= 2;
			putshort(&startp, namelen);
			ancnt = 1;
		} else if (q->type == T_MX || q->type == T_SRV) {
			/* Data is expected to be like
			   "Hblabla.host.name.com\0Hanother.com\0\0"
			   For SRV, see RFC2782.
			 */

			char *mxdata = data;
			char *startp;
			int namelen;

			ancnt = 1;
			while (1) {
				CHECKLEN(10);
				putshort(&p, name);
				putshort(&p, q->type);
				putshort(&p, C_IN);
				putlong(&p, 0);		/* TTL */

				startp = p;
				p += 2;			/* skip 2 bytes length */
				CHECKLEN(2);
				putshort(&p, 10 * ancnt);	/* preference */

				if (q->type == T_SRV) {
					/* weight, port (5060 = SIP) */
					CHECKLEN(4);
					putshort(&p, 10);
					putshort(&p, 5060);
				}

				putname(&p, buflen - (p - buf), mxdata);
				CHECKLEN(0);
				namelen = p - startp;
				namelen -= 2;
				putshort(&startp, namelen);

				mxdata = mxdata + strlen(mxdata) + 1;
				if (*mxdata == '\0')
					break;

				ancnt++;
			}
		} else if (q->type == T_TXT) {
			/* TXT has binary or base-X data */
			char *startp;
			int txtlen;

			CHECKLEN(10);
			putshort(&p, name);
			putshort(&p, q->type);
			putshort(&p, C_IN);
			putlong(&p, 0);		/* TTL */

			startp = p;
			p += 2;			/* skip 2 bytes length */
			puttxtbin(&p, buflen - (p - buf), data, datalen);
			CHECKLEN(0);
			txtlen = p - startp;
			txtlen -= 2;
			putshort(&startp, txtlen);
			ancnt = 1;
		} else {
			/* NULL has raw binary data */

			CHECKLEN(10);
			putshort(&p, name);
			putshort(&p, q->type);
			putshort(&p, C_IN);
			putlong(&p, 0);		/* TTL */

			datalen = MIN(datalen, buflen - (p - buf));
			CHECKLEN(2);
			putshort(&p, datalen);
			CHECKLEN(datalen);
			putdata(&p, data, datalen);
			CHECKLEN(0);
			ancnt = 1;
		}
		header->ancount = htons(ancnt);
		break;
	case QR_QUERY:
		/* Note that iodined also uses this for forward queries */

		header->qdcount = htons(1);

		datalen = MIN(datalen, buflen - (p - buf));
		putname(&p, datalen, data);

		CHECKLEN(4);
		putshort(&p, q->type);
		putshort(&p, C_IN);

		/* EDNS0 to advertise maximum response length
		   (even CNAME/A/MX, 255+255+header would be >512) */
		if (dnsc_use_edns0) {
			header->arcount = htons(1);
			CHECKLEN(11);
			putbyte(&p, 0x00);    /* Root */
			putshort(&p, 0x0029); /* OPT */
			putshort(&p, 0x1000); /* Payload size: 4096 */
			putshort(&p, 0x0000); /* Higher bits/edns version */
			putshort(&p, 0x8000); /* Z */
			putshort(&p, 0x0000); /* Data length */
		}

		break;
	}

	len = p - buf;

	return len;
}
Ejemplo n.º 13
0
Archivo: dns.c Proyecto: frekky/iodine
int
dns_encode_ns_response(char *buf, size_t buflen, struct query *q, char *topdomain)
/* Only used when iodined gets an NS type query */
/* Mostly same as dns_encode_a_response() below */
{
	HEADER *header;
	int len;
	short name;
	short topname;
	short nsname;
	char *ipp;
	int domain_len;
	char *p;

	if (buflen < sizeof(HEADER))
		return 0;

	memset(buf, 0, buflen);

	header = (HEADER*)buf;

	header->id = htons(q->id);
	header->qr = 1;
	header->opcode = 0;
	header->aa = 1;
	header->tc = 0;
	header->rd = 0;
	header->ra = 0;

	p = buf + sizeof(HEADER);

	header->qdcount = htons(1);
	header->ancount = htons(1);
	header->arcount = htons(1);

	/* pointer to start of name */
	name = 0xc000 | ((p - buf) & 0x3fff);

	domain_len = strlen(q->name) - strlen(topdomain);
	if (domain_len < 0 || domain_len == 1)
		return -1;
	if (strcasecmp(q->name + domain_len, topdomain))
		return -1;
	if (domain_len >= 1 && q->name[domain_len - 1] != '.')
		return -1;

	/* pointer to start of topdomain; instead of dots at the end
	   we have length-bytes in front, so total length is the same */
	topname = 0xc000 | ((p - buf + domain_len) & 0x3fff);

	/* Query section */
	putname(&p, buflen - (p - buf), q->name);	/* Name */
	CHECKLEN(4);
	putshort(&p, q->type);			/* Type */
	putshort(&p, C_IN);			/* Class */

	/* Answer section */
	CHECKLEN(12);
	putshort(&p, name);			/* Name */
	putshort(&p, q->type);			/* Type */
	putshort(&p, C_IN);			/* Class */
	putlong(&p, 3600);			/* TTL */
	putshort(&p, 5);			/* Data length */

	/* pointer to ns.topdomain */
	nsname = 0xc000 | ((p - buf) & 0x3fff);
	CHECKLEN(5);
	putbyte(&p, 2);
	putbyte(&p, 'n');
	putbyte(&p, 's');
	putshort(&p, topname);			/* Name Server */

	/* Additional data (A-record of NS server) */
	CHECKLEN(12);
	putshort(&p, nsname);			/* Name Server */
	putshort(&p, T_A);			/* Type */
	putshort(&p, C_IN);			/* Class */
	putlong(&p, 3600);			/* TTL */
	putshort(&p, 4);			/* Data length */

	/* ugly hack to output IP address */
	ipp = (char *) &q->destination;
	CHECKLEN(4);
	putbyte(&p, *(ipp++));
	putbyte(&p, *(ipp++));
	putbyte(&p, *(ipp++));
	putbyte(&p, *ipp);

	len = p - buf;
	return len;
}
Ejemplo n.º 14
0
int main(int argc, char *argv[]) {
  FILE      *infile, *wadfile;
  char      keyword[20];
  char      vertexname[20], sectorname[20];
  char      fromvertexname[20], tovertexname[20];
  int      ox, oy, x, y, z, xf, yf;
  int      numentries, reslength;
  int      i;
  int      episode, level, maptype = 0;
  char      gametype[20];
  int      lastitem;
  int      floorh, ceilingh, brightness, special, tag, type, attrs, angle, flags;
  int      arg1, arg2, arg3, arg4, arg5;
  int       floort, ceilingt;
  int      uppert, lowert, normalt;
  char      thingname[20];
  int      tid;
  int      fromvertexindex, tovertexindex;
  int      rightsidedefindex, leftsidedefindex;
  int      sectorindex;
  char      behavior[16] = { 'A', 'C', 'S', '\0', 0x08, '\0', '\0', '\0',
             '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
                };

  w_vertex64_t   vertices[32768];
  w_sector64_t   sectors[32768];
  sidedef64_t  sidedefs[32768];
  linedef64_t  linedefs[32768];
  things64_t   things[32768];
  char lights[100000], macros[100000];
  int lightbytes = 0, macrobytes = 0;
  int      numvertices = 0, numsectors = 0, numsidedefs = 0,
			  numlinedefs = 0, numthings = 0;
  int      vertexshortcut = 1;
  int      sectorshortcut = 1;

  char      entryname[9];
  int      entrypos;

  if (argc != 3) {
    fprintf(stderr,"Usage: %s infile outfile\n", argv[0]);
    exit(-1);
  }
  if (!strcmp(argv[1], "-"))
    infile = stdin;
  else
    infile = fopen(argv[1], "r");
  if (infile == NULL) {
    fprintf(stderr,"Unable to open input file %s\n", argv[1]);
    exit(-1);
  }
  if (!strcmp(argv[2], "-"))
    wadfile = stdout;
  else
    wadfile = fopen(argv[2], "wb");
  if (wadfile == NULL) {
    fprintf(stderr,"Unable to create output file %s\n", argv[2]);
    exit(-1);
  }
  if (wadfile == stdout)
    quiet = 1;
  allowcomment(infile);
  fscanf(infile, " %s ", keyword);

  if (!strcmp(keyword, "LEVEL_START")) {
    fscanf(infile, " %d %d %d %s ", &episode, &level, &maptype, &gametype[0]);
    if(maptype != 64) {
       fprintf(stderr, "map file is not for Doom64EX\n");
       exit(-1);
    }
    allowcomment(infile);
    fscanf(infile, " %s ", keyword);
  } else {
    fprintf(stderr,"LEVEL_START marker missing!\n");
    exit(-1);
  }

  if (!strcmp(keyword, "VERTEXES_START") |
		  !strcmp(keyword, "VERTICES_START"))
  {
	  /* Process vertices until VERTEXES_END is found */
	  lastitem = 0;
	  numvertices = 0;
	  while (!lastitem) {
		  allowcomment(infile);
		  fscanf(infile, " %s ", vertexname);
		  if (!strcmp(vertexname, "VERTEXES_END") |
				  !strcmp(vertexname, "VERTICES_END"))
			  lastitem = 1;
		  else {
			  /* KLUDGE to speed things up, notice when vertex names contain
				* the vertex number and avoid the long scan of vertices[].
				*/
			  {
				  const char *p;
				  long num = 0;
				  int digits = 0;

				  /* Skip over the non-numeric prefix */
				  for (p = vertexname; *p != '\0' && (*p < '0' || *p > '9'); p++)
					  ;
				  for (; *p >= '0' && *p <= '9'; p++) {
					  num = 10 * num + *p - '0';
					  digits++;
				  }
				  if (digits == 0 || num != numvertices)
					  vertexshortcut = 0;
			  }
			  fscanf(infile, " : %d %d %d %d ", &x, &xf, &y, &yf);
			  allowcomment(infile);
			  vertices[numvertices].x = x;
			  vertices[numvertices].y = y;
			  vertices[numvertices].x_frac = xf;
			  vertices[numvertices].y_frac = yf;
			  strncpy(vertices[numvertices].name, vertexname, 8);
			  numvertices++;
		  }
	  }
	  fscanf(infile, " %s ", keyword);
  } else {
	  fprintf(stderr,"VERTEXES_START marker missing!\n");
	  exit(-1);
  }

  if (!strcmp(keyword, "SECTORS_START")) {
	  int i;
	  uint32_t flags;
	  uint16_t sflags;
	  /* Process sectors until SECTORS_END is found */
	  lastitem = 0;
	  numsectors = 0;
	  while (!lastitem) {
		  allowcomment(infile);
		  fscanf(infile, " %s ", sectorname);
		  if (!strcmp(sectorname, "SECTORS_END"))
			  lastitem = 1;
		  else {
			  fscanf(infile, " : %d %d %d %d %d %d %d ", &floorh, &ceilingh,
					  &floort, &ceilingt, &type, &tag, &flags);
			  /* KLUDGE to speed things up, notice when sector names contain
				* the sector number and avoid the long scan of sectors[].
				*/
			  {
				  const char *p;
				  long num = 0;
				  int digits = 0;

				  /* Skip over the non-numeric prefix */
				  for (p = sectorname; *p != '\0' && (*p < '0' || *p > '9'); p++)
					  ;
				  for (; *p >= '0' && *p <= '9'; p++)
				  {
					  num = 10 * num + *p - '0';
					  digits++;
				  }
				  if (digits == 0 || num != numsectors)
					  sectorshortcut = 0;
			  }
			  allowcomment(infile);
			  sectors[numsectors].s.f_height = floorh;
			  sectors[numsectors].s.c_height = ceilingh;
			  sectors[numsectors].s.type = type;
			  sectors[numsectors].s.flags = flags;
			  sectors[numsectors].s.tag = tag;
			  strncpy(sectors[numsectors].name, sectorname, 8);
			  sectors[numsectors].s.f_index = floort;
			  sectors[numsectors].s.c_index = ceilingt;

			  for(i=0; i<10; i++) {
				  int got;
				  fscanf(infile, " %x ", &got);
				  sectors[numsectors].s.color_index[i] = got & 0xff;
			  }

			  numsectors++;
		  }
	  }
	  fscanf(infile, " %s ", keyword);
  } else {
	  fprintf(stderr,"SECTORS_START marker missing!\n");
	  exit(-1);
  }

  if (!strcmp(keyword, "LINEDEFS_START")) {
	  uint32_t flags;
	  uint16_t sflags;

	  /* Process lines until LINEDEFS_END is found */
	  lastitem = 0;
	  numlinedefs = 0;
	  while (!lastitem) {
		  allowcomment(infile);
		  fscanf(infile, " %s ", fromvertexname);
		  if (!strcmp(fromvertexname, "LINEDEFS_END"))
			  lastitem = 1;
		  else {
			  fscanf(infile, " %s : %u %hu %d ", tovertexname, &flags, &type, &tag);
			  allowcomment(infile);
			  fromvertexindex = findmatch(fromvertexname, (char*)vertices,
					  vertexshortcut, numvertices,
					  sizeof(w_vertex64_t));
			  tovertexindex = findmatch(tovertexname, (char*)vertices,
					  vertexshortcut, numvertices,
					  sizeof(w_vertex64_t));

			  /* Read one or two SIDEDEFS */
			  fscanf(infile, " %s %d %d %d %d %d ", sectorname, &ox, &oy,
					  &uppert, &lowert, &normalt);
			  allowcomment(infile);
			  sectorindex = findmatch(sectorname, (char*)sectors, sectorshortcut,
					  numsectors, sizeof(w_sector64_t));
			  sidedefs[numsidedefs].sector = sectorindex;
			  sidedefs[numsidedefs].x = ox;
			  sidedefs[numsidedefs].y = oy;
			  sidedefs[numsidedefs].u_texture = uppert;
			  sidedefs[numsidedefs].l_texture = lowert;
			  sidedefs[numsidedefs].m_texture = normalt;
			  rightsidedefindex = numsidedefs;
			  numsidedefs++;
			  fscanf(infile, "%s", sectorname);
			  if (!strcmp(sectorname, "-")) {
				  allowcomment(infile);
				  leftsidedefindex = -1;
			  } else {
				  fscanf(infile, " %d %d %u %u %u ", &ox, &oy, &uppert, &lowert, &normalt);
				  allowcomment(infile);
				  sectorindex = findmatch(sectorname, (char*)sectors, sectorshortcut,
						  numsectors, sizeof(w_sector64_t));
				  sidedefs[numsidedefs].sector = sectorindex;
				  sidedefs[numsidedefs].x = ox;
				  sidedefs[numsidedefs].y = oy;
				  sidedefs[numsidedefs].u_texture = uppert;
				  sidedefs[numsidedefs].l_texture = lowert;
				  sidedefs[numsidedefs].m_texture = normalt;
				  leftsidedefindex = numsidedefs;
				  numsidedefs++;
			  }
			  linedefs[numlinedefs].v_from = fromvertexindex;
			  linedefs[numlinedefs].v_to = tovertexindex;
			  linedefs[numlinedefs].flags = flags;
			  linedefs[numlinedefs].type = sflags;
			  linedefs[numlinedefs].tag = tag;
			  linedefs[numlinedefs].r_side = rightsidedefindex;
			  linedefs[numlinedefs].l_side = leftsidedefindex;
			  numlinedefs++;
		  }
	  }
	  fscanf(infile, " %s ", keyword);
  } else {
    fprintf(stderr,"LINEDEFS_START marker missing!\n");
    exit(-1);
  }

  if (!strcmp(keyword, "THINGS_START")) {
	  /* Process things until THINGS_END is found */
	  lastitem = 0;
	  numthings = 0;
	  while (!lastitem) {
		  allowcomment(infile);
		  fscanf(infile, " %s ", thingname);
		  if (!strcmp(thingname, "THINGS_END"))
			  lastitem = 1;
		  else {
			  sscanf(thingname, "%hd", &(things[numthings].type));
			  fscanf(infile, " : %hd %hd %hd %hd %hd %hd",
					  &(things[numthings].x),
					  &(things[numthings].y),
					  &(things[numthings].z),
					  &(things[numthings].angle),
					  &(things[numthings].flags),
					  &(things[numthings].tid));
			  allowcomment(infile);
			  numthings++;
		  }
	  }
	  fscanf(infile, " %s ", keyword);
  } else {
	  fprintf(stderr,"THINGS_START marker missing!\n");
	  exit(-1);
  }

  if (!strcmp(keyword, "LIGHTS_START")) {
	  lightbytes = 0;
	  fscanf(infile, " %s ", keyword);
	  while(strcmp(keyword, "LIGHTS_END")) {
		  unsigned int got;
		  sscanf(keyword, " %d ", &got);
		  lights[lightbytes++] = (unsigned char)(got & 0xff);
		  fscanf(infile, " %s ", keyword);
	  }
  } else {
	  fprintf(stderr,"LIGHTS_START marker missing!\n");
	  exit(-1);
  }

  fscanf(infile, " %s ", keyword);
  if (!strcmp(keyword, "MACROS_START")) {
	  macrobytes = 0;
	  fscanf(infile, " %s ", keyword);
	  while(strcmp(keyword, "MACROS_END")) {
		  unsigned int got;
		  sscanf(keyword, " %d ", &got);
		  macros[macrobytes++] = (unsigned char)(got & 0xff);
		  fscanf(infile, " %s ", keyword);
	  }
  } else {
	  fprintf(stderr,"MACROS_START marker missing!\n");
	  exit(-1);
  }


  if (infile != stdin)
	  fclose(infile);

  /* Concatenate all the data into the final PWAD file */

  /* Header */
  fprintf(wadfile,"PWAD");

  /* Doom64 map type */
  numentries = 14;
  putlong(numentries, wadfile);
  reslength = numthings * sizeof(things64_t) +
	  numlinedefs * sizeof(linedef64_t) +
	  numsidedefs * sizeof(sidedef64_t) +
	  numvertices * sizeof(vertex64_t) +
	  numsectors * sizeof(sector64_t) +
	  lightbytes +
	  macrobytes +
	  sizeof(wadinfo_t);
  putlong(reslength, wadfile);

  /* ExMy - start at position 12 (zero length entry) */

  /* THINGS - start at position 12 */
  for (i = 0; i < numthings; i++) {
      /* Doom map type */
      putshort(things[i].x, wadfile);
      putshort(things[i].y, wadfile);
      putshort(things[i].z, wadfile);
      putshort(things[i].angle, wadfile);
      putshort(things[i].type, wadfile);
      putshort(things[i].flags, wadfile);
      putshort(things[i].tid, wadfile);
  }

  /* LINEDEFS - start at position 12 + numthings * sizeof(things_t) */
  for (i = 0; i < numlinedefs; i++) {
	  /* Doom map type */
	  putshort(linedefs[i].v_from, wadfile);
	  putshort(linedefs[i].v_to, wadfile);
	  putlong(linedefs[i].flags, wadfile);
	  putshort(linedefs[i].type, wadfile);
	  putshort(linedefs[i].tag, wadfile);
	  putshort(linedefs[i].r_side, wadfile);
	  putshort(linedefs[i].l_side, wadfile);
  }

  /* SIDEDEFS - start at position 12 + numthings * sizeof(things_t) +
   * numlinedefs * sizeof(linedef_t)
   */
  for (i = 0; i < numsidedefs; i++) {
    putshort(sidedefs[i].x, wadfile);
    putshort(sidedefs[i].y, wadfile);
    putshort(sidedefs[i].u_texture, wadfile);
    putshort(sidedefs[i].l_texture, wadfile);
    putshort(sidedefs[i].m_texture, wadfile);
    putshort(sidedefs[i].sector, wadfile);
  }

  /* VERTEXES - start at position 12 + numthings * sizeof(things_t) +
   * numlinedefs * sizeof(linedef_t) + numsidedefs * sizeof(sidedef_t)
   */
  for (i = 0; i < numvertices; i++) {
    putshort(vertices[i].x_frac, wadfile);
    putshort(vertices[i].x, wadfile);
    putshort(vertices[i].y_frac, wadfile);
    putshort(vertices[i].y, wadfile);
  }

  /* SECTORS - start at position 12 + numthings * sizeof(things_t) +
   * numlinedefs * sizeof(linedef_t) + numsidedefs * sizeof(sidedef_t) +
   * numvertices * sizeof(vertex_t)
   */
  for (i = 0; i < numsectors; i++) {
    putshort(sectors[i].s.f_height, wadfile);
    putshort(sectors[i].s.c_height, wadfile);
    putshort(sectors[i].s.f_index, wadfile);
    putshort(sectors[i].s.c_index, wadfile);
	 fwrite(sectors[i].s.color_index, 10, 1, wadfile);
    putshort(sectors[i].s.type, wadfile);
    putshort(sectors[i].s.tag, wadfile);
    putshort(sectors[i].s.flags, wadfile);
  }

  fwrite(lights, lightbytes, 1, wadfile);
  fwrite(macros, macrobytes, 1, wadfile);

  /* Now, write the main directory */
  entrypos = 12;
  if (episode > 0)
      sprintf(entryname, "E%1dM%1d", episode, level);
  else
      sprintf(entryname, "MAP%02d", level);
  addentry(&entrypos, 0,            entryname,  wadfile);
    addentry(&entrypos, numthings*sizeof(things64_t),   "THINGS",   wadfile);
    addentry(&entrypos, numlinedefs*sizeof(linedef64_t),   "LINEDEFS", wadfile);
  addentry(&entrypos, numsidedefs*sizeof(sidedef64_t),   "SIDEDEFS", wadfile);
  addentry(&entrypos, numvertices*sizeof(vertex64_t),   "VERTEXES", wadfile);
  addentry(&entrypos, 0,                       "SEGS",     wadfile);
  addentry(&entrypos, 0,                       "SSECTORS", wadfile);
  addentry(&entrypos, 0,                       "NODES",    wadfile);
  addentry(&entrypos, numsectors*sizeof(sector64_t),   "SECTORS",  wadfile);
  addentry(&entrypos, 0,                       "REJECT",   wadfile);
  addentry(&entrypos, 0,                       "BLOCKMAP", wadfile);
  addentry(&entrypos, 0,                       "LEAFS", wadfile);
  addentry(&entrypos, lightbytes,                       "LIGHTS", wadfile);
  addentry(&entrypos, macrobytes,                       "MACROS", wadfile);

  /* DONE! */
  if (wadfile != stdout)
    fclose(wadfile);
  return 0;
}