Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion backend/dvi/mdvi-lib/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include <config.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -66,10 +67,12 @@ Ulong mugetn(const Uchar *p, size_t n)

char *read_string(FILE *in, int s, char *buffer, size_t len)
{
int n;
Ulong n;
char *str;

n = fugetn(in, s ? s : 1);
if(n >= INT_MAX)
return NULL;
if((str = buffer) == NULL || n + 1 > len)
str = mdvi_malloc(n + 1);
if(fread(str, 1, n, in) != n) {
Expand Down
2 changes: 2 additions & 0 deletions backend/dvi/mdvi-lib/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ static int gf_read_bitmap(FILE *p, DviFontChar *ch)
char *s;

s = read_string(p, op - GF_XXX1 + 1, NULL, 0);
if(s == NULL)
goto error;
DEBUG((DBG_SPECIAL, "(gf) Character %d: Special \"%s\"\n",
ch->code, s));
mdvi_free(s);
Expand Down
64 changes: 44 additions & 20 deletions backend/dvi/mdvi-lib/pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
#define PK_NOOP 246
#define PK_PRE 247

/* more leading zeros in a packed number would overflow an int */
#define PK_MAX_ZEROS 6

#define PK_DYN_F(x) (((x) >> 4) & 0xf)
#define PK_PACKED(x) (PK_DYN_F(x) != 14)

Expand Down Expand Up @@ -167,11 +170,15 @@ static int pk_packed_num(FILE *p, pkread *pkr, int *repeat)
int dyn_f = pkr->dyn_f;

i = pk_get_nyb(p, pkr);
if(feof(p))
return -1;
if(i == 0) {
do {
j = pk_get_nyb(p, pkr);
i++;
} while(j == 0);
} while(j == 0 && i < PK_MAX_ZEROS);
if(j == 0)
return -1;
while(i-- > 0)
j = (j << 4) + pk_get_nyb(p, pkr);
return (j - 15 + ((13 - dyn_f) << 4) +
Expand All @@ -182,9 +189,15 @@ static int pk_packed_num(FILE *p, pkread *pkr, int *repeat)
return ((i - dyn_f - 1) << 4) +
pk_get_nyb(p, pkr) + dyn_f + 1;
else {
/* a repeat count must be followed by a run count */
if(*repeat)
return -1;
*repeat = 1;
if(i == 14)
if(i == 14) {
*repeat = pk_packed_num(p, pkr, repeat);
if(*repeat < 0)
return -1;
}
return pk_packed_num(p, pkr, repeat);
}
}
Expand Down Expand Up @@ -255,6 +268,8 @@ static BITMAP *get_packed(FILE *p, int w, int h, int flags)
int i = 0;

count = pk_packed_num(p, &pkr, &i);
if(count < 0 || i < 0)
goto bad;
if(i > 0) {
if(repeat_count)
fprintf(stderr, "second repeat count for this row (had %d and got %d)\n",
Expand All @@ -270,6 +285,8 @@ static BITMAP *get_packed(FILE *p, int w, int h, int flags)
if(paint)
bitmap_set_row(bm, row, w - inrow, inrow, paint);
/* now copy it as many times as required */
if(repeat_count >= h - row)
goto bad;
r = (Uchar *)bm->data + row * bm->stride;
while(repeat_count-- > 0) {
t = r + bm->stride;
Expand All @@ -292,6 +309,8 @@ static BITMAP *get_packed(FILE *p, int w, int h, int flags)
a = (BmUnit *)(r + bm->stride);
/* deal with entirely with/black rows */
while(count >= w) {
if(row >= h)
goto bad;
/* count number of atoms in a row */
i = ROUND(w, BITMAP_BITS);
while(i-- > 0)
Expand All @@ -301,17 +320,22 @@ static BITMAP *get_packed(FILE *p, int w, int h, int flags)
}
inrow = w;
}
if(count > 0)
if(count > 0) {
if(row >= h)
goto bad;
bitmap_set_row(bm, row, w - inrow, count, paint);
}
inrow -= count;
paint = !paint;
}
if(row != h || inrow != w) {
mdvi_error(_("Bad PK file: More bits than required\n"));
bitmap_destroy(bm);
return NULL;
}
if(row != h || inrow != w)
goto bad;
return bm;

bad:
mdvi_error(_("Bad PK file: More bits than required\n"));
bitmap_destroy(bm);
return NULL;
}

static BITMAP *get_char(FILE *p, int w, int h, int flags)
Expand Down Expand Up @@ -348,6 +372,8 @@ static int pk_load_font(DviParams *unused, DviFont *font)
if(loc != PK_PRE || hic != PK_ID)
goto badpk;
i = fuget1(p);
if(feof(p))
goto badpk;
#ifndef NODEBUG
for(loc = 0; loc < i; loc++)
s[loc] = fuget1(p);
Expand Down Expand Up @@ -387,24 +413,22 @@ static int pk_load_font(DviParams *unused, DviFont *font)
case PK_X3:
case PK_X4: {
#ifndef NODEBUG
char *t;
int n;

/* keep at most the first 255 bytes, for debugging */
i = fugetn(p, flag_byte - PK_X1 + 1);
if(i < 256)
t = &s[0];
else
t = mdvi_malloc(i + 1);
for(n = 0; n < i; n++)
t[n] = fuget1(p);
t[n] = 0;
for(n = 0; n < i && !feof(p); n++) {
int c = fuget1(p);

if(n < (int)sizeof(s) - 1)
s[n] = c;
}
s[Min(n, (int)sizeof(s) - 1)] = 0;
DEBUG((DBG_SPECIAL, "(pk) %s: Special \"%s\"\n",
font->fontname, t));
if(t != &s[0])
mdvi_free(t);
font->fontname, s));
#else
i = fugetn(p, flag_byte - PK_X1 + 1);
while(i-- > 0)
while(i-- > 0 && !feof(p))
fuget1(p);
#endif
break;
Expand Down
6 changes: 3 additions & 3 deletions backend/dvi/mdvi-lib/tfmfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ int tfm_load_file(const char *filename, TFMInfo *info)
if(lh > 12) {
n = msget1(ptr);
if(n > 0) {
i = Max(n, 63);
i = Min(n, 63);
memcpy(info->family, ptr, i);
info->family[i] = 0;
} else
Expand Down Expand Up @@ -344,7 +344,7 @@ static int ofm1_load_file(FILE *in, TFMInfo *info)
if(lh > 12) {
n = fsget1(in);
if(n > 0) {
i = Max(n, 63);
i = Min(n, 63);
fread(info->family, i, 1, in);
info->family[i] = 0;
} else
Expand Down Expand Up @@ -521,7 +521,7 @@ static int ofm_load_file(const char *filename, TFMInfo *info)
if(lh > 12) {
n = fsget1(in);
if(n > 0) {
i = Max(n, 63);
i = Min(n, 63);
fread(info->family, i, 1, in);
info->family[i] = 0;
} else
Expand Down
5 changes: 4 additions & 1 deletion backend/dvi/mdvi-lib/vf.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include <config.h>
#include <limits.h>
#include <string.h>

#include "mdvi.h"
Expand Down Expand Up @@ -165,6 +166,8 @@ static int vf_load_font(DviParams *params, DviFont *font)
cc = fuget1(p);
tfm = fuget3(p);
}
if(pl < 0 || pl > INT_MAX - 256 - mlen)
goto badvf;
if (cc < 0 || cc > 65536) {
/* TeX engines do not support char codes bigger than 65535 */
mdvi_error(_("(vf) %s: unexpected character %d\n"),
Expand Down Expand Up @@ -199,7 +202,7 @@ static int vf_load_font(DviParams *params, DviFont *font)
msize = mlen + pl + 256;
macros = xresize(macros, Uchar, msize);
}
if(pl && fread(macros + mlen, 1, pl, p) != pl)
if(pl && fread(macros + mlen, 1, pl, p) != (size_t)pl)
break;
macros[mlen+pl] = DVI_EOP;
mlen += pl + 1;
Expand Down
Loading