Skip to content

Commit

Permalink
Use stdint for portability
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviut committed Aug 11, 2022
1 parent 57ffa1f commit 1b985ee
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 231 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ toolsets.
Notes
-----

tinf requires int to be at least 32-bit.

The inflate algorithm and data format are from 'DEFLATE Compressed Data
Format Specification version 1.3' ([RFC 1951][deflate]).

Expand Down
17 changes: 9 additions & 8 deletions examples/tgunzip/tgunzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@
*/

#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

#include "tinf.h"

static unsigned int read_le32(const unsigned char *p)
static uint32_t read_le32(const unsigned char *p)
{
return ((unsigned int) p[0])
| ((unsigned int) p[1] << 8)
| ((unsigned int) p[2] << 16)
| ((unsigned int) p[3] << 24);
return ((uint32_t) p[0])
| ((uint32_t) p[1] << 8)
| ((uint32_t) p[2] << 16)
| ((uint32_t) p[3] << 24);
}

static void printf_error(const char *fmt, ...)
Expand All @@ -56,9 +57,9 @@ int main(int argc, char *argv[])
FILE *fout = NULL;
unsigned char *source = NULL;
unsigned char *dest = NULL;
unsigned int len, dlen, outlen;
int retval = EXIT_FAILURE;
int res;
uint32_t len, dlen, outlen;
int32_t retval = EXIT_FAILURE;
int32_t res;

printf("tgunzip " TINF_VER_STRING " - example from the tiny inflate library (www.ibsensoftware.com)\n\n");

Expand Down
10 changes: 5 additions & 5 deletions src/adler32.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
#define A32_BASE 65521
#define A32_NMAX 5552

unsigned int tinf_adler32(const void *data, unsigned int length)
uint32_t tinf_adler32(const void *data, uint32_t length)
{
const unsigned char *buf = (const unsigned char *) data;

unsigned int s1 = 1;
unsigned int s2 = 0;
uint32_t s1 = 1;
uint32_t s2 = 0;

while (length > 0) {
int k = length < A32_NMAX ? length : A32_NMAX;
int i;
int32_t k = length < A32_NMAX ? length : A32_NMAX;
int32_t i;

for (i = k / 16; i; --i, buf += 16) {
s1 += buf[0];
Expand Down
8 changes: 4 additions & 4 deletions src/crc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@

#include "tinf.h"

static const unsigned int tinf_crc32tab[16] = {
static const uint32_t tinf_crc32tab[16] = {
0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC, 0x76DC4190,
0x6B6B51F4, 0x4DB26158, 0x5005713C, 0xEDB88320, 0xF00F9344,
0xD6D6A3E8, 0xCB61B38C, 0x9B64C2B0, 0x86D3D2D4, 0xA00AE278,
0xBDBDF21C
};

unsigned int tinf_crc32(const void *data, unsigned int length)
uint32_t tinf_crc32(const void *data, uint32_t length)
{
const unsigned char *buf = (const unsigned char *) data;
unsigned int crc = 0xFFFFFFFF;
unsigned int i;
uint32_t crc = 0xFFFFFFFF;
uint32_t i;

if (length == 0) {
return 0;
Expand Down
18 changes: 10 additions & 8 deletions src/tinf.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#ifndef TINF_H_INCLUDED
#define TINF_H_INCLUDED

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -76,8 +78,8 @@ void TINFCC tinf_init(void);
* @param sourceLen size of compressed data
* @return `TINF_OK` on success, error code on error
*/
int TINFCC tinf_uncompress(void *dest, unsigned int *destLen,
const void *source, unsigned int sourceLen);
int32_t TINFCC tinf_uncompress(void *dest, uint32_t *destLen,
const void *source, uint32_t sourceLen);

/**
* Decompress `sourceLen` bytes of gzip data from `source` to `dest`.
Expand All @@ -94,8 +96,8 @@ int TINFCC tinf_uncompress(void *dest, unsigned int *destLen,
* @param sourceLen size of compressed data
* @return `TINF_OK` on success, error code on error
*/
int TINFCC tinf_gzip_uncompress(void *dest, unsigned int *destLen,
const void *source, unsigned int sourceLen);
int32_t TINFCC tinf_gzip_uncompress(void *dest, uint32_t *destLen,
const void *source, uint32_t sourceLen);

/**
* Decompress `sourceLen` bytes of zlib data from `source` to `dest`.
Expand All @@ -112,8 +114,8 @@ int TINFCC tinf_gzip_uncompress(void *dest, unsigned int *destLen,
* @param sourceLen size of compressed data
* @return `TINF_OK` on success, error code on error
*/
int TINFCC tinf_zlib_uncompress(void *dest, unsigned int *destLen,
const void *source, unsigned int sourceLen);
int32_t TINFCC tinf_zlib_uncompress(void *dest, uint32_t *destLen,
const void *source, uint32_t sourceLen);

/**
* Compute Adler-32 checksum of `length` bytes starting at `data`.
Expand All @@ -122,7 +124,7 @@ int TINFCC tinf_zlib_uncompress(void *dest, unsigned int *destLen,
* @param length size of data
* @return Adler-32 checksum
*/
unsigned int TINFCC tinf_adler32(const void *data, unsigned int length);
uint32_t TINFCC tinf_adler32(const void *data, uint32_t length);

/**
* Compute CRC32 checksum of `length` bytes starting at `data`.
Expand All @@ -131,7 +133,7 @@ unsigned int TINFCC tinf_adler32(const void *data, unsigned int length);
* @param length size of data
* @return CRC32 checksum
*/
unsigned int TINFCC tinf_crc32(const void *data, unsigned int length);
uint32_t TINFCC tinf_crc32(const void *data, uint32_t length);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
28 changes: 14 additions & 14 deletions src/tinfgzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ typedef enum {
FCOMMENT = 16
} tinf_gzip_flag;

static unsigned int read_le16(const unsigned char *p)
static uint32_t read_le16(const unsigned char *p)
{
return ((unsigned int) p[0])
| ((unsigned int) p[1] << 8);
return ((uint32_t) p[0])
| ((uint32_t) p[1] << 8);
}

static unsigned int read_le32(const unsigned char *p)
static uint32_t read_le32(const unsigned char *p)
{
return ((unsigned int) p[0])
| ((unsigned int) p[1] << 8)
| ((unsigned int) p[2] << 16)
| ((unsigned int) p[3] << 24);
return ((uint32_t) p[0])
| ((uint32_t) p[1] << 8)
| ((uint32_t) p[2] << 16)
| ((uint32_t) p[3] << 24);
}

int tinf_gzip_uncompress(void *dest, unsigned int *destLen,
const void *source, unsigned int sourceLen)
int32_t tinf_gzip_uncompress(void *dest, uint32_t *destLen,
const void *source, uint32_t sourceLen)
{
const unsigned char *src = (const unsigned char *) source;
unsigned char *dst = (unsigned char *) dest;
const unsigned char *start;
unsigned int dlen, crc32;
int res;
uint32_t dlen, crc32;
int32_t res;
unsigned char flg;

/* -- Check header -- */
Expand Down Expand Up @@ -89,7 +89,7 @@ int tinf_gzip_uncompress(void *dest, unsigned int *destLen,

/* Skip extra data if present */
if (flg & FEXTRA) {
unsigned int xlen = read_le16(start);
uint32_t xlen = read_le16(start);

if (xlen > sourceLen - 12) {
return TINF_DATA_ERROR;
Expand Down Expand Up @@ -118,7 +118,7 @@ int tinf_gzip_uncompress(void *dest, unsigned int *destLen,

/* Check header crc if present */
if (flg & FHCRC) {
unsigned int hcrc;
uint32_t hcrc;

if (start - src > sourceLen - 2) {
return TINF_DATA_ERROR;
Expand Down
Loading

0 comments on commit 1b985ee

Please sign in to comment.