summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/luapplib/util
diff options
context:
space:
mode:
Diffstat (limited to 'Build/source/texk/web2c/luatexdir/luapplib/util')
-rw-r--r--Build/source/texk/web2c/luatexdir/luapplib/util/utilarm.h10
-rw-r--r--Build/source/texk/web2c/luatexdir/luapplib/util/utilfpred.c104
-rw-r--r--Build/source/texk/web2c/luatexdir/luapplib/util/utiliof.c4
-rw-r--r--Build/source/texk/web2c/luatexdir/luapplib/util/utilsha.c53
4 files changed, 84 insertions, 87 deletions
diff --git a/Build/source/texk/web2c/luatexdir/luapplib/util/utilarm.h b/Build/source/texk/web2c/luatexdir/luapplib/util/utilarm.h
new file mode 100644
index 00000000000..546c6d4dd1a
--- /dev/null
+++ b/Build/source/texk/web2c/luatexdir/luapplib/util/utilarm.h
@@ -0,0 +1,10 @@
+#ifndef UTIL_ARM_H
+#define UTIL_ARM_H
+
+#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__ ||( defined(__sun) && defined(__SVR4))
+# define ARM_COMPLIANT 1
+#else
+# define ARM_COMPLIANT 0
+#endif
+
+#endif \ No newline at end of file
diff --git a/Build/source/texk/web2c/luatexdir/luapplib/util/utilfpred.c b/Build/source/texk/web2c/luatexdir/luapplib/util/utilfpred.c
index ce9c129e249..4416161eab3 100644
--- a/Build/source/texk/web2c/luatexdir/luapplib/util/utilfpred.c
+++ b/Build/source/texk/web2c/luatexdir/luapplib/util/utilfpred.c
@@ -6,7 +6,7 @@
/*
Here we implement predictor filters used with flate and lzw compressions in PDF streams. The main idea of data prediction
-is to compute and output the differences between data records instead of this records. Adjacent pixels in images are usually
+is to compute and output the differences between data records instead of those records. Adjacent pixels in images are usually
similar, so differences between pixel values tends to be zero. And both Flate and LZW performs better when the input
is rather smooth. Although a preliminary use of predictors is related to bitmap data, The actual need for predictor filter
came from the fact that xref streams may also be predicted (usually with PNG up-predictor).
@@ -24,7 +24,7 @@ PDF specification allows to use several predictor algorithms, specified by /Pred
All PNG predictors works on bytes, regardless the image color-depth. While encoding, every input data byte is decreased
by the appropriate byte of the previous pixel. Even if the pixel does not fit a full byte, PNG predictors use an artificial
pixel size rounded up to a full byte. PNG predictors utilizes previous (left) pixel, pixel above and previous to above
-pixel. In case of PNG, the type of the predictor is written on a dedicated byte on the beginning of every scanline. It
+pixel. In case of PNG, the type of the predictor is written on a dedicated byte at the beginning of every scanline. It
means all predictor functions must maintain and information about left, above and left-above pixels.
Despite the same differencing idea, TIFF predictors are different. The prediction process bases on pixel components,
@@ -38,20 +38,13 @@ input value (or 0 at the beginning of the scanline) and output a difference betw
previously decoded value (or zero) and outputs their sum. When encoding, the result is cast to the proper unsigned integer,
when decoding, modulo 256 (or appropriate) is used, which makes encoding and decoding looseless.
-Some extra bits trickery is involved in TIFF predictor function, when components doesn't fit bytes boundary. In that case,
-an input is treated as an bits stream. Every input byte is "buffered" in a larger integer, as its lower bits (from right).
+Some extra bits trickery is involved in TIFF predictor function, when components don't fit bytes boundary. In that case,
+an input is treated as a bits stream. Every input byte is "buffered" in a larger integer, as its lower bits (from right).
Every output value is taken from its higher (left) bits. In a special case of bits-per-component equal 1, we buffer all
pixel bits and use XOR to compute bits difference between pixels. I've excerpted that trick from poppler, but I'm not
really sure if it works any better, especially when the number of components per pixel is 1. In that case we do a hard
bit-by-bit work anyway.
-Predictor codecs state keeps a notion of surrounding pixels. PNG predictors uses left, up and upleft
-pixel data, while TIFF predictor (2) only needs the previous (left) pixel. Important to note that PNG
-predictors always work on bytes, no matter of color-depth (bits per component), while TIFF predictor
-works on pixel components, which not necessarily fits into a complete byte. However, for PNG predictor
-the size of a pixel still matters, because 'left' and 'upleft' refers to a corresponding pixel byte,
-not necessarily previous byte.
-
In PNG prediction, we record every pixel byte (in decoded form) in state->rowsave. At the end of a scanline
we copy state->rowsave to state->rowup, so that in the next scanline we can access up-pixel byte.
Left pixel byte is accessed as state->rowsave (the byte recently stored or virtual left edge byte \0).
@@ -60,8 +53,10 @@ at the left edge of the row). Both state->rowup and state->rowsave has a safe sp
that are permanently \0.
*/
-#define predictor_component_t unsigned short
-#define predictor_pixel1b_t unsigned int
+#define predictor_component_t uint16_t
+#define predictor_pixel1b_t uint32_t
+
+#define MAX_COMPONENTS 8
typedef struct predictor_state {
int default_predictor; /* default predictor indicator */
@@ -70,7 +65,7 @@ typedef struct predictor_state {
int compbits; /* number of bits per component (/DecodeParms << /BitsPerComponent ... >>) */
int components; /* number of components (/DecodeParms << /Colors ... >>) */
uint8_t *buffer; /* temporary private buffer area */
- uint8_t *rowin; /* an input row buffer position */
+ uint8_t *rowin; /* an input row buffer position */
int rowsize; /* size of a current scanline in bytes (rounded up) */
int rowend; /* an input buffer end position */
int rowindex; /* an output buffer position */
@@ -81,8 +76,9 @@ typedef struct predictor_state {
int pixelsize; /* number of bytes per pixel (rounded up) */
};
struct { /* used by TIFF predictor codecs */
+ predictor_component_t compbuffer[MAX_COMPONENTS];
union {
- predictor_component_t *prevcomp; /* an array of left pixel components */
+ predictor_component_t *prevcomp; /* an array of left pixel components, typically eq ->compbuffer */
predictor_pixel1b_t *prevpixel; /* left pixel value stored on a single integer (for 1bit color-depth) */
};
int compin, compout; /* bit stream buffers */
@@ -98,7 +94,23 @@ typedef struct predictor_state {
enum {
STATUS_LAST = 0,
- STATUS_CONTINUE = 1 // any value different then IOFEOF, IOFERR, ...
+ STATUS_CONTINUE = 1 // any value different then IOFEOF, IOFERR, ... which are < 0
+};
+
+/*
+Predictor type identifiers (pdf spec 76). lpdf doesn't hire the codec if predictor is 1. Predictor 15 indicates
+that the type of PNG prediction algorithm may change in subsequent lines. We always check algorithm marker anyway.
+*/
+
+enum predictor_code {
+ NONE_PREDICTOR = 1,
+ TIFF_PREDICTOR = 2,
+ PNG_NONE_PREDICTOR = 10,
+ PNG_SUB_PREDICTOR = 11,
+ PNG_UP_PREDICTOR = 12,
+ PNG_AVERAGE_PREDICTOR = 13,
+ PNG_PAETH_PREDICTOR = 14,
+ PNG_OPTIMUM_PREDICTOR = 15
};
predictor_state * predictor_decoder_init (predictor_state *state, int predictor, int rowsamples, int components, int compbits)
@@ -116,31 +128,19 @@ predictor_state * predictor_decoder_init (predictor_state *state, int predictor,
state->components = components;
state->compbits = compbits;
- if (predictor == 2)
+ if (predictor == TIFF_PREDICTOR)
{ /* tiff predictor */
size_t compbuf, pixbuf;
- compbuf = state->components * sizeof(predictor_component_t);
+ compbuf = components * sizeof(predictor_component_t);
pixbuf = 1 * sizeof(predictor_pixel1b_t);
state->pixbufsize = (int)(compbuf > pixbuf ? compbuf : pixbuf);
- buffersize = rowsize + state->pixbufsize;
+ buffersize = rowsize * sizeof(uint8_t);
buffer = (uint8_t *)util_calloc(buffersize, 1);
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
- { /*memory leak */
- predictor_component_t *c;
- if (state->pixbufsize%(sizeof(predictor_component_t))) {
- c = malloc(state->pixbufsize - state->pixbufsize%(sizeof(predictor_component_t)) + sizeof(predictor_component_t) );
- } else {
- c = malloc(state->pixbufsize);
- }
- memcpy(c,(state->rowin + rowsize),state->pixbufsize);
- if (state->prevcomp){
- free(state->prevcomp);
- }
- state->prevcomp = c;
- }
-#else
- state->prevcomp = (predictor_component_t *)(state->rowin + rowsize);
-#endif
+ if (state->pixbufsize > sizeof(state->compbuffer)) // components > MAX_COMPONENTS
+ state->prevcomp = (predictor_component_t *)util_calloc(state->pixbufsize, 1);
+ else
+ state->prevcomp = state->compbuffer;
+ // &state->prevcomp == &state->prevpixel
state->sampleindex = state->compindex = 0;
state->bitsin = state->bitsout = 0;
state->compin = state->compout = 0;
@@ -173,30 +173,16 @@ predictor_state * predictor_encoder_init (predictor_state *state, int predictor,
void predictor_decoder_close (predictor_state *state)
{
util_free(state->buffer);
+ if (state->default_predictor == TIFF_PREDICTOR && state->prevcomp != NULL && state->prevcomp != state->compbuffer)
+ util_free(state->prevcomp);
}
void predictor_encoder_close (predictor_state *state)
{
- util_free(state->buffer);
+ predictor_decoder_close(state);
}
/*
-Predictor type identifiers (pdf spec 76). lpdf doesn't hire the codec if predictor is 1. Predictor 15 indicates
-that the type of PNG prediction algorithm may change in subsequent lines. We always check algorithm marker anyway.
-*/
-
-enum predictor_code {
- NONE_PREDICTOR = 1,
- TIFF_PREDICTOR = 2,
- PNG_NONE_PREDICTOR = 10,
- PNG_SUB_PREDICTOR = 11,
- PNG_UP_PREDICTOR = 12,
- PNG_AVERAGE_PREDICTOR = 13,
- PNG_PAETH_PREDICTOR = 14,
- PNG_OPTIMUM_PREDICTOR = 15
-};
-
-/*
All predoctor codecs first read the entire data row into a buffer. This is not crucial for the process,
but allows to separate read/write states. In particular, there is one place in which codec functions
may return on EOD.
@@ -254,20 +240,22 @@ static int read_scanline (predictor_state *state, iof *I, int size)
#define ensure_output_bytes(O, n) if (!iof_ensure(O, n)) return IOFFULL
-#define tobyte(c) ((unsigned char)(c))
-#define tocomp(c) ((unsigned short)(c))
+#define tobyte(c) ((uint8_t)(c))
+#define tocomp(c) ((uint16_t)(c))
#define row_byte(state) (state->rowin[state->rowindex])
+/* png predictor macros; on bytes */
+
#define up_pixel_byte(state) (state->rowup[state->rowindex])
#define upleft_pixel_byte(state) (state->rowup[state->rowindex - state->pixelsize])
#define left_pixel_byte(state) (state->rowsave[state->rowindex - state->pixelsize])
-
#define save_pixel_byte(state, c) (state->rowsave[state->rowindex] = c)
+/* tiff predictor macros; on components */
+
#define left_pixel_component(state) (state->prevcomp[state->compindex]) // tiff predictor with 2, 4, 8, 16 components
#define left_pixel_value(state) (state->prevpixel[0]) // tiff predictor with 1bit components
-
#define save_pixel_component(state, c) ((void)\
((state->prevcomp[state->compindex] = c), \
(++state->compindex < state->components || (state->compindex = 0))))
@@ -418,7 +406,7 @@ iof_status predictor_decode_state (iof *I, iof *O, predictor_state *state)
d = row_byte(state) << 8;
++state->rowindex;
d |= row_byte(state);
- c = (d + left_pixel_component(state)) & 0xff;
+ c = (d + left_pixel_component(state)) & 0xffff;
save_pixel_component(state, c);
iof_set2(O, c >> 8, c & 0xff);
}
diff --git a/Build/source/texk/web2c/luatexdir/luapplib/util/utiliof.c b/Build/source/texk/web2c/luatexdir/luapplib/util/utiliof.c
index 7d07cf12d81..2a1c6c7eeb8 100644
--- a/Build/source/texk/web2c/luatexdir/luapplib/util/utiliof.c
+++ b/Build/source/texk/web2c/luatexdir/luapplib/util/utiliof.c
@@ -8,6 +8,8 @@
#include "utillog.h"
#include "utiliof.h"
+#include "utilarm.h"
+
/* commons */
void * iof_copy_data (const void *data, size_t size)
@@ -2001,7 +2003,7 @@ void iof_filters_free (void)
iof_filters_heap = NULL;
}
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
#define iof_heap_get(hp, ghost, data, siz) \
(ghost = (iof_heap_ghost *)((void*)((hp)->pos)), \
ghost->heap = hp, \
diff --git a/Build/source/texk/web2c/luatexdir/luapplib/util/utilsha.c b/Build/source/texk/web2c/luatexdir/luapplib/util/utilsha.c
index 66532073023..aa640ec24f9 100644
--- a/Build/source/texk/web2c/luatexdir/luapplib/util/utilsha.c
+++ b/Build/source/texk/web2c/luatexdir/luapplib/util/utilsha.c
@@ -1,6 +1,8 @@
/* sha2 implementation by Aaron D. Gifford (http://www.aarongifford.com) */
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#include "utilarm.h"
+
+#if ARM_COMPLIANT
#include <stdlib.h>
#endif
@@ -44,9 +46,6 @@
*/
#include <string.h> /* memcpy()/memset() or bcopy()/bzero() */
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
-#include <stdlib.h>
-#endif
#include <assert.h> /* assert() */
//#include "sha2.h"
#include "utilsha.h"
@@ -450,7 +449,7 @@ static void SHA256_Transform(SHA256_CTX* context, const uint32_t* data) {
uint32_t T1, T2, *W256;
int j;
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
W256 = (uint32_t*)((void *)context->buffer);
#else
W256 = (uint32_t*)context->buffer;
@@ -552,7 +551,7 @@ static void SHA256_Update(SHA256_CTX* context, const uint8_t *data, size_t len)
context->bitcount += freespace << 3;
len -= freespace;
data += freespace;
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
SHA256_Transform(context, (uint32_t*)((void *)context->buffer));
#else
SHA256_Transform(context, (uint32_t*)context->buffer);
@@ -569,8 +568,8 @@ static void SHA256_Update(SHA256_CTX* context, const uint8_t *data, size_t len)
}
while (len >= SHA256_BLOCK_LENGTH) {
/* Process as many complete blocks as we can */
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
- SHA256_Transform(context, (const uint32_t*)(const void *)(data));
+#if ARM_COMPLIANT
+ SHA256_Transform(context, (const uint32_t*)(const void *)(data));
#else
SHA256_Transform(context, (const uint32_t*)data);
#endif
@@ -589,7 +588,7 @@ static void SHA256_Update(SHA256_CTX* context, const uint8_t *data, size_t len)
}
static void SHA256_Final(uint8_t digest[], SHA256_CTX* context) {
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
uint32_t *d ;
#else
uint32_t *d = (uint32_t*)digest;
@@ -599,7 +598,7 @@ static void SHA256_Final(uint8_t digest[], SHA256_CTX* context) {
/* Sanity check: */
assert(context != (SHA256_CTX*)0);
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
d = malloc(sizeof(uint32_t)*8); /* why 8 ? see below for loop */
assert(d);
#endif
@@ -623,7 +622,7 @@ static void SHA256_Final(uint8_t digest[], SHA256_CTX* context) {
MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
}
/* Do second-to-last transform: */
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
SHA256_Transform(context, (uint32_t*)(void *)(context->buffer));
#else
SHA256_Transform(context, (uint32_t*)context->buffer);
@@ -645,9 +644,8 @@ static void SHA256_Final(uint8_t digest[], SHA256_CTX* context) {
context->buffer64[SHA256_SHORT_BLOCK_LENGTH / sizeof(uint64_t)] = context->bitcount;
/* Final transform: */
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
- SHA256_Transform(context, (uint32_t*)((void *)(context->buffer)));
-
+#if ARM_COMPLIANT
+ SHA256_Transform(context, (uint32_t*)((void *)(context->buffer)));
#else
SHA256_Transform(context, (uint32_t*)context->buffer);
@@ -667,7 +665,7 @@ static void SHA256_Final(uint8_t digest[], SHA256_CTX* context) {
#endif
}
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
memcpy(digest,d,SHA256_DIGEST_LENGTH);
free(d);
#endif
@@ -912,7 +910,7 @@ static void SHA512_Update(SHA512_CTX* context, const uint8_t *data, size_t len)
ADDINC128(context->bitcount, freespace << 3);
len -= freespace;
data += freespace;
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
SHA512_Transform(context, (uint64_t*)((void *)context->buffer));
#else
SHA512_Transform(context, (uint64_t*)context->buffer);
@@ -928,10 +926,10 @@ static void SHA512_Update(SHA512_CTX* context, const uint8_t *data, size_t len)
}
while (len >= SHA512_BLOCK_LENGTH) {
/* Process as many complete blocks as we can */
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
- SHA512_Transform(context, (uint64_t*)((void *)context->buffer));
+#if ARM_COMPLIANT
+ SHA512_Transform(context, (uint64_t*)((void *)context->buffer));
#else
- SHA512_Transform(context, (uint64_t*)context->buffer);
+ SHA512_Transform(context, (uint64_t*)context->buffer);
#endif
ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
len -= SHA512_BLOCK_LENGTH;
@@ -967,7 +965,7 @@ static void SHA512_Last(SHA512_CTX* context) {
MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
}
/* Do second-to-last transform: */
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
SHA512_Transform(context, (uint64_t*)((void *)context->buffer));
#else
SHA512_Transform(context, (uint64_t*)context->buffer);
@@ -991,7 +989,7 @@ static void SHA512_Last(SHA512_CTX* context) {
context->buffer64[SHA512_SHORT_BLOCK_LENGTH / sizeof(uint64_t) + 1] = context->bitcount[0];
/* Final transform: */
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
SHA512_Transform(context, (uint64_t*)((void *)context->buffer));
#else
SHA512_Transform(context, (uint64_t*)context->buffer);
@@ -1000,16 +998,15 @@ static void SHA512_Last(SHA512_CTX* context) {
}
static void SHA512_Final(uint8_t digest[], SHA512_CTX* context) {
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
uint64_t *d ;
#else
uint64_t *d = (uint64_t*)digest;
-
#endif
/* Sanity check: */
assert(context != (SHA512_CTX*)0);
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
d = malloc(sizeof(uint64_t)*8); /* why 8 ? see below for loop */
assert(d);
#endif
@@ -1032,7 +1029,7 @@ static void SHA512_Final(uint8_t digest[], SHA512_CTX* context) {
MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);
#endif
}
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
memcpy(digest,d,SHA512_DIGEST_LENGTH);
free(d);
#endif
@@ -1089,7 +1086,7 @@ static void SHA384_Update(SHA384_CTX* context, const uint8_t* data, size_t len)
}
static void SHA384_Final(uint8_t digest[], SHA384_CTX* context) {
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
uint64_t *d;
#else
uint64_t *d = (uint64_t*)digest;
@@ -1099,7 +1096,7 @@ static void SHA384_Final(uint8_t digest[], SHA384_CTX* context) {
/* Sanity check: */
assert(context != (SHA384_CTX*)0);
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
d = malloc(sizeof(uint64_t)*6); /* why 6 ? see below for loop */
assert(d);
#endif
@@ -1122,7 +1119,7 @@ static void SHA384_Final(uint8_t digest[], SHA384_CTX* context) {
MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH);
#endif
}
-#if defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm || defined __ARM_ARCH ||defined __aarch64__
+#if ARM_COMPLIANT
memcpy(digest,d,SHA384_DIGEST_LENGTH);
free(d);
#endif