static __inline__ void swd_getbyte(ucl_swd_t * s) { int c; if ((c = getbyte(*(s->c))) < 0) { if (s->look > 0) --s->look; #if defined(__UCL_CHECKER) /* initialize memory - value doesn't matter */ s->b[s->ip] = 0; if (s->ip < s->f) s->b_wrap[s->ip] = 0; #endif } else { s->b[s->ip] = UCL_BYTE(c); if (s->ip < s->f) s->b_wrap[s->ip] = UCL_BYTE(c); } if (++s->ip == s->b_size) s->ip = 0; if (++s->bp == s->b_size) s->bp = 0; if (++s->rp == s->b_size) s->rp = 0; }
ucl_memset(ucl_voidp s, int c, ucl_uint len) { #if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMSET) return memset(s,c,len); #else ucl_byte *p = (ucl_byte *) s; if (len > 0) do *p++ = UCL_BYTE(c); while (--len > 0); return s; #endif }
_ucl_config_check(void) { ucl_bool r = 1; int i; union { ucl_uint32 a; unsigned short b; ucl_uint32 aa[4]; unsigned char x[4*sizeof(ucl_align_t)]; } u; #if 0 /* paranoia - the following is guaranteed by definition anyway */ r &= __ucl_assert((const void *)&u == (const void *)&u.a); r &= __ucl_assert((const void *)&u == (const void *)&u.b); r &= __ucl_assert((const void *)&u == (const void *)&u.x[0]); r &= __ucl_assert((const void *)&u == (const void *)&u.aa[0]); #endif r &= basic_integral_check(); r &= basic_ptr_check(); if (r != 1) return UCL_E_ERROR; for (i = 0; i < (int) sizeof(u.x); i++) u.x[i] = UCL_BYTE(i); #if 0 /* check if the compiler correctly casts signed to unsigned */ r &= __ucl_assert( (int) (unsigned char) ((char) -1) == 255); #endif /* check UCL_BYTE_ORDER */ #if defined(UCL_BYTE_ORDER) if (r == 1) { # if (UCL_BYTE_ORDER == UCL_LITTLE_ENDIAN) ucl_uint32 a = (ucl_uint32) (u.a & UCL_0xffffffffL); unsigned short b = (unsigned short) (u.b & 0xffff); r &= __ucl_assert(a == 0x03020100L); r &= __ucl_assert(b == 0x0100); # elif (UCL_BYTE_ORDER == UCL_BIG_ENDIAN) ucl_uint32 a = u.a >> (8 * sizeof(u.a) - 32); unsigned short b = u.b >> (8 * sizeof(u.b) - 16); r &= __ucl_assert(a == 0x00010203L); r &= __ucl_assert(b == 0x0001); # else # error invalid UCL_BYTE_ORDER # endif }
static ucl_bool basic_integral_check(void) { ucl_bool r = 1; ucl_bool sanity; /* paranoia */ r &= __ucl_assert(CHAR_BIT == 8); r &= __ucl_assert(sizeof(char) == 1); r &= __ucl_assert(sizeof(short) >= 2); r &= __ucl_assert(sizeof(long) >= 4); r &= __ucl_assert(sizeof(int) >= sizeof(short)); r &= __ucl_assert(sizeof(long) >= sizeof(int)); r &= __ucl_assert(sizeof(ucl_uint32) >= 4); r &= __ucl_assert(sizeof(ucl_uint32) >= sizeof(unsigned)); #if defined(__UCL_STRICT_16BIT) r &= __ucl_assert(sizeof(ucl_uint) == 2); #else r &= __ucl_assert(sizeof(ucl_uint) >= 4); r &= __ucl_assert(sizeof(ucl_uint) >= sizeof(unsigned)); #endif #if defined(SIZEOF_UNSIGNED) r &= __ucl_assert(SIZEOF_UNSIGNED == sizeof(unsigned)); #endif #if defined(SIZEOF_UNSIGNED_LONG) r &= __ucl_assert(SIZEOF_UNSIGNED_LONG == sizeof(unsigned long)); #endif #if defined(SIZEOF_UNSIGNED_SHORT) r &= __ucl_assert(SIZEOF_UNSIGNED_SHORT == sizeof(unsigned short)); #endif #if !defined(__UCL_IN_MINIUCL) #if defined(SIZEOF_SIZE_T) r &= __ucl_assert(SIZEOF_SIZE_T == sizeof(size_t)); #endif #endif /* assert the signedness of our integral types */ sanity = IS_UNSIGNED(unsigned short) && IS_UNSIGNED(unsigned) && IS_UNSIGNED(unsigned long) && IS_SIGNED(short) && IS_SIGNED(int) && IS_SIGNED(long); if (sanity) { r &= __ucl_assert(IS_UNSIGNED(ucl_uint32)); r &= __ucl_assert(IS_UNSIGNED(ucl_uint)); r &= __ucl_assert(IS_SIGNED(ucl_int32)); r &= __ucl_assert(IS_SIGNED(ucl_int)); r &= __ucl_assert(INT_MAX == UCL_STYPE_MAX(sizeof(int))); r &= __ucl_assert(UINT_MAX == UCL_UTYPE_MAX(sizeof(unsigned))); r &= __ucl_assert(LONG_MAX == UCL_STYPE_MAX(sizeof(long))); r &= __ucl_assert(ULONG_MAX == UCL_UTYPE_MAX(sizeof(unsigned long))); r &= __ucl_assert(SHRT_MAX == UCL_STYPE_MAX(sizeof(short))); r &= __ucl_assert(USHRT_MAX == UCL_UTYPE_MAX(sizeof(unsigned short))); r &= __ucl_assert(UCL_UINT32_MAX == UCL_UTYPE_MAX(sizeof(ucl_uint32))); r &= __ucl_assert(UCL_UINT_MAX == UCL_UTYPE_MAX(sizeof(ucl_uint))); #if !defined(__UCL_IN_MINIUCL) r &= __ucl_assert(SIZE_T_MAX == UCL_UTYPE_MAX(sizeof(size_t))); #endif } #if 0 /* for some reason this fails on a Cray ??? */ r &= __ucl_assert(UCL_BYTE(257) == 1); r &= __ucl_assert(UCL_USHORT(65537L) == 1); #endif return r; }
static ucl_bool ptr_check(void) { ucl_bool r = 1; int i; char _wrkmem[10 * sizeof(ucl_byte *) + sizeof(ucl_align_t)]; ucl_byte *wrkmem; ucl_bytepp dict; unsigned char x[4 * sizeof(ucl_align_t)]; long d; ucl_align_t a; for (i = 0; i < (int) sizeof(x); i++) x[i] = UCL_BYTE(i); wrkmem = (ucl_byte *) UCL_PTR_ALIGN_UP(_wrkmem,sizeof(ucl_align_t)); /* Expect a compiler warning on architectures that * do not allow unaligned access. */ dict = (ucl_bytepp) wrkmem; d = (long) ((const ucl_bytep) dict - (const ucl_bytep) _wrkmem); r &= __ucl_assert(d >= 0); r &= __ucl_assert(d < (long) sizeof(ucl_align_t)); memset(&a,0xff,sizeof(a)); r &= __ucl_assert(a.a_ushort == USHRT_MAX); r &= __ucl_assert(a.a_uint == UINT_MAX); r &= __ucl_assert(a.a_ulong == ULONG_MAX); r &= __ucl_assert(a.a_ucl_uint == UCL_UINT_MAX); /* sanity check of the memory model */ if (r == 1) { for (i = 0; i < 8; i++) r &= __ucl_assert((const ucl_voidp) (&dict[i]) == (const ucl_voidp) (&wrkmem[i * sizeof(ucl_byte *)])); } /* check BZERO8_PTR and that NULL == 0 */ memset(&a,0,sizeof(a)); r &= __ucl_assert(a.a_charp == NULL); r &= __ucl_assert(a.a_ucl_bytep == NULL); r &= __ucl_assert(NULL == 0); if (r == 1) { for (i = 0; i < 10; i++) dict[i] = wrkmem; BZERO8_PTR(dict+1,sizeof(dict[0]),8); r &= __ucl_assert(dict[0] == wrkmem); for (i = 1; i < 9; i++) r &= __ucl_assert(dict[i] == NULL); r &= __ucl_assert(dict[9] == wrkmem); } /* check that the pointer constructs work as expected */ if (r == 1) { unsigned k = 1; const unsigned n = (unsigned) sizeof(ucl_uint32); ucl_byte *p0; ucl_byte *p1; k += __ucl_align_gap(&x[k],n); p0 = (ucl_bytep) &x[k]; #if defined(PTR_LINEAR) r &= __ucl_assert((PTR_LINEAR(p0) & (n-1)) == 0); #else r &= __ucl_assert(n == 4); r &= __ucl_assert(PTR_ALIGNED_4(p0)); #endif r &= __ucl_assert(k >= 1); p1 = (ucl_bytep) &x[1]; r &= __ucl_assert(PTR_GE(p0,p1)); r &= __ucl_assert(k < 1+n); p1 = (ucl_bytep) &x[1+n]; r &= __ucl_assert(PTR_LT(p0,p1)); /* now check that aligned memory access doesn't core dump */ if (r == 1) { /* Expect 2 compiler warnings on architectures that * do not allow unaligned access. */ ucl_uint32 v0 = * (ucl_uint32 *) &x[k]; ucl_uint32 v1 = * (ucl_uint32 *) &x[k+n]; r &= __ucl_assert(v0 > 0); r &= __ucl_assert(v1 > 0); } } return r; }
static int swd_init(ucl_swd_t * s, const ucl_byte * dict, ucl_uint dict_len) { ucl_uint i = 0; int c = 0; if (s->n == 0) s->n = SWD_N; if (s->f == 0) s->f = SWD_F; s->threshold = SWD_THRESHOLD; if (s->n > SWD_N || s->f > SWD_F) return UCL_E_INVALID_ARGUMENT; #if defined(SWD_USE_MALLOC) s->b = (unsigned char *) ucl_alloc(s->n + s->f + s->f, 1); s->head3 = (swd_uint *) ucl_alloc(SWD_HSIZE, sizeof(*s->head3)); s->succ3 = (swd_uint *) ucl_alloc(s->n + s->f, sizeof(*s->succ3)); s->best3 = (swd_uint *) ucl_alloc(s->n + s->f, sizeof(*s->best3)); s->llen3 = (swd_uint *) ucl_alloc(SWD_HSIZE, sizeof(*s->llen3)); if (!s->b || !s->head3 || !s->succ3 || !s->best3 || !s->llen3) return UCL_E_OUT_OF_MEMORY; #ifdef HEAD2 s->head2 = (swd_uint *) ucl_alloc(UCL_UINT32_C(65536), sizeof(*s->head2)); if (!s->head2) return UCL_E_OUT_OF_MEMORY; #endif #endif /* defaults */ s->max_chain = SWD_MAX_CHAIN; s->nice_length = s->f; s->use_best_off = 0; s->lazy_insert = 0; s->b_size = s->n + s->f; if (s->b_size + s->f >= SWD_UINT_MAX) return UCL_E_ERROR; s->b_wrap = s->b + s->b_size; s->node_count = s->n; ucl_memset(s->llen3, 0, sizeof(s->llen3[0]) * SWD_HSIZE); #ifdef HEAD2 #if 1 ucl_memset(s->head2, 0xff, sizeof(s->head2[0]) * UCL_UINT32_C(65536)); assert(s->head2[0] == NIL2); #else for (i = 0; i < UCL_UINT32_C(65536); i++) s->head2[i] = NIL2; #endif #endif s->ip = 0; swd_initdict(s, dict, dict_len); s->bp = s->ip; s->first_rp = s->ip; assert(s->ip + s->f <= s->b_size); #if 1 s->look = (ucl_uint) (s->c->in_end - s->c->ip); if (s->look > 0) { if (s->look > s->f) s->look = s->f; ucl_memcpy(&s->b[s->ip], s->c->ip, s->look); s->c->ip += s->look; s->ip += s->look; } #else s->look = 0; while (s->look < s->f) { if ((c = getbyte(*(s->c))) < 0) break; s->b[s->ip] = UCL_BYTE(c); s->ip++; s->look++; } #endif if (s->ip == s->b_size) s->ip = 0; if (s->look >= 2 && s->dict_len > 0) swd_insertdict(s, 0, s->dict_len); s->rp = s->first_rp; if (s->rp >= s->node_count) s->rp -= s->node_count; else s->rp += s->b_size - s->node_count; #if defined(__UCL_CHECKER) /* initialize memory for the first few HEAD3 (if s->ip is not far * enough ahead to do this job for us). The value doesn't matter. */ if (s->look < 3) ucl_memset(&s->b[s->bp + s->look], 0, 3); #endif UCL_UNUSED(i); UCL_UNUSED(c); return UCL_E_OK; }