1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
/* catdvi - get text from DVI files
Copyright (C) 1999 J.H.M. Dassen (Ray) <jdassen@wi.LeidenUniv.nl>
Copyright (C) 1999 Antti-Juhani Kaijanaho <gaia@iki.fi>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include "bytesex.h"
#include "util.h"
#define SKIPBYTES_BUFLEN 128
void efread(void *ptr, size_t size, size_t nmemb,
FILE *stream, char *errmsg)
{
if (fread(ptr, size, nmemb, stream) != nmemb) {
if (feof(stream)) {
panic("Unexpected end of file\n");
} else {
panic("%s\n", strerror(errno));
}
panic(errmsg);
}
}
byte readbyte(FILE * stream)
{
byte b;
efread(&b, 1, 1, stream, "Failed to read byte\n");
return b;
}
/* A portable reader for unsigned muli-octet big-endian numbers. */
uint32 u_readbigendiannumber(byte count, FILE *stream)
{
uint32 ures = 0;
int i;
assert(count <= 4);
/* Read in an unsigned value. */
for (i = 0; i < count; i++) {
ures = (ures << 8) + readbyte(stream);
}
return ures;
}
/* An extremely portable reader for signed (two's complement)
muli-octet big-endian numbers. */
sint32 s_readbigendiannumber(byte count, FILE *stream)
{
uint32 ures;
const uint32 msb_bitmask = ((uint32) 1) << (8 * count - 1);
/* Beware of overflow! 2 * msb_bitmask may be too much for
uint32. */
const uint32 allset_bitmask = 2 * (msb_bitmask - 1) + 1;
assert(count <= 4);
/* First read the octets, taking care of the bytesex. */
ures = u_readbigendiannumber(count, stream);
assert(ures <= allset_bitmask);
/* We need to handle the sign conversion ourselves, as a
simple cast is implementation-defined. The value in ures
is in two's complement format. */
if (ures >= msb_bitmask) {
/* It is negative. */
/* We must be very careful to keep all values
nonnegative until the result is explicitly
converted. An unexpected negative value may ruin
all of this... Therefore we single out the case
ures == 0, where we'd get a bit pattern we can't
convert back to 0 with the XOR. */
return -((ures == 0) ? 0 :
(sint32) ((ures - 1) ^ allset_bitmask));
}
/* Positive. */
return ures;
}
void readbcblstring(byte * buffer, uint32 slen, FILE * fp)
{
uint32 len;
len = u_readbigendiannumber(1, fp);
if (len > slen - 1) {
warning("invalid BCBL string length");
len = 0;
}
if (len > 0) {
efread(buffer, 1, len, fp, "unable to read BCBL string");
}
buffer[len] = 0;
skipbytes(slen - len - 1, fp);
}
void skipbytes(int count, FILE *stream)
{
byte buf[SKIPBYTES_BUFLEN];
while (count > 0) {
int numbytes = count > SKIPBYTES_BUFLEN
? SKIPBYTES_BUFLEN
: count;
count = count - SKIPBYTES_BUFLEN;
efread(buf, 1, numbytes, stream,
"Error while skipping bytes\n");
}
}
|