From eeb09032068baed6d81cff01cdfcccd6d55a8152 Mon Sep 17 00:00:00 2001 From: Alexandre Rostovtsev Date: Mon, 26 Sep 2011 04:45:49 -0400 Subject: [PATCH 1/2] Use stdint.h types Use stdint.h types (int16_t and int32_t) instead of assuming that short and long must always a specific number of bytes. Also, use strtoul for reading uint32_t values. --- rply.c | 61 ++++++++++++++++++++++++++++--------------------------------- 1 files changed, 28 insertions(+), 33 deletions(-) diff --git a/rply.c b/rply.c index 042244f..9eaa77f 100644 --- a/rply.c +++ b/rply.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "rply.h" @@ -1183,18 +1184,12 @@ static e_ply_storage_mode ply_arch_endian(void) { static int ply_type_check(void) { assert(sizeof(char) == 1); assert(sizeof(unsigned char) == 1); - assert(sizeof(short) == 2); - assert(sizeof(unsigned short) == 2); - assert(sizeof(long) == 4); - assert(sizeof(unsigned long) == 4); + assert(sizeof(long) >= 4); assert(sizeof(float) == 4); assert(sizeof(double) == 8); if (sizeof(char) != 1) return 0; if (sizeof(unsigned char) != 1) return 0; - if (sizeof(short) != 2) return 0; - if (sizeof(unsigned short) != 2) return 0; - if (sizeof(long) != 4) return 0; - if (sizeof(unsigned long) != 4) return 0; + if (sizeof(long) < 4) return 0; if (sizeof(float) != 4) return 0; if (sizeof(double) != 8) return 0; return 1; @@ -1214,23 +1209,23 @@ static int oascii_uint8(p_ply ply, double value) { } static int oascii_int16(p_ply ply, double value) { - if (value > SHRT_MAX || value < SHRT_MIN) return 0; - return fprintf(ply->fp, "%d ", (short) value) > 0; + if (value > INT16_MAX || value < INT16_MIN) return 0; + return fprintf(ply->fp, "%d ", (int16_t) value) > 0; } static int oascii_uint16(p_ply ply, double value) { - if (value > USHRT_MAX || value < 0) return 0; - return fprintf(ply->fp, "%d ", (unsigned short) value) > 0; + if (value > UINT16_MAX || value < 0) return 0; + return fprintf(ply->fp, "%d ", (uint16_t) value) > 0; } static int oascii_int32(p_ply ply, double value) { - if (value > LONG_MAX || value < LONG_MIN) return 0; - return fprintf(ply->fp, "%d ", (int) value) > 0; + if (value > INT32_MAX || value < INT32_MIN) return 0; + return fprintf(ply->fp, "%d ", (int32_t) value) > 0; } static int oascii_uint32(p_ply ply, double value) { - if (value > ULONG_MAX || value < 0) return 0; - return fprintf(ply->fp, "%d ", (unsigned int) value) > 0; + if (value > UINT32_MAX || value < 0) return 0; + return fprintf(ply->fp, "%d ", (uint32_t) value) > 0; } static int oascii_float32(p_ply ply, double value) { @@ -1256,26 +1251,26 @@ static int obinary_uint8(p_ply ply, double value) { } static int obinary_int16(p_ply ply, double value) { - short int16 = (short) value; - if (value > SHRT_MAX || value < SHRT_MIN) return 0; + int16_t int16 = value; + if (value > INT16_MAX || value < INT16_MIN) return 0; return ply->odriver->ochunk(ply, &int16, sizeof(int16)); } static int obinary_uint16(p_ply ply, double value) { - unsigned short uint16 = (unsigned short) value; - if (value > USHRT_MAX || value < 0) return 0; + uint16_t uint16 = value; + if (value > UINT16_MAX || value < 0) return 0; return ply->odriver->ochunk(ply, &uint16, sizeof(uint16)); } static int obinary_int32(p_ply ply, double value) { - long int32 = (long) value; - if (value > LONG_MAX || value < LONG_MIN) return 0; + int32_t int32 = value; + if (value > INT32_MAX || value < INT32_MIN) return 0; return ply->odriver->ochunk(ply, &int32, sizeof(int32)); } static int obinary_uint32(p_ply ply, double value) { - unsigned long uint32 = (unsigned long) value; - if (value > ULONG_MAX || value < 0) return 0; + uint32_t uint32 = value; + if (value > UINT32_MAX || value < 0) return 0; return ply->odriver->ochunk(ply, &uint32, sizeof(uint32)); } @@ -1312,7 +1307,7 @@ static int iascii_int16(p_ply ply, double *value) { char *end; if (!ply_read_word(ply)) return 0; *value = strtol(BWORD(ply), &end, 10); - if (*end || *value > SHRT_MAX || *value < SHRT_MIN) return 0; + if (*end || *value > INT16_MAX || *value < INT16_MIN) return 0; return 1; } @@ -1320,7 +1315,7 @@ static int iascii_uint16(p_ply ply, double *value) { char *end; if (!ply_read_word(ply)) return 0; *value = strtol(BWORD(ply), &end, 10); - if (*end || *value > USHRT_MAX || *value < 0) return 0; + if (*end || *value > UINT16_MAX || *value < 0) return 0; return 1; } @@ -1328,15 +1323,15 @@ static int iascii_int32(p_ply ply, double *value) { char *end; if (!ply_read_word(ply)) return 0; *value = strtol(BWORD(ply), &end, 10); - if (*end || *value > LONG_MAX || *value < LONG_MIN) return 0; + if (*end || *value > INT32_MAX || *value < INT32_MIN) return 0; return 1; } static int iascii_uint32(p_ply ply, double *value) { char *end; if (!ply_read_word(ply)) return 0; - *value = strtol(BWORD(ply), &end, 10); - if (*end || *value < 0) return 0; + *value = strtoul(BWORD(ply), &end, 10); + if (*end || *value > UINT32_MAX || *value < 0) return 0; return 1; } @@ -1371,28 +1366,28 @@ static int ibinary_uint8(p_ply ply, double *value) { } static int ibinary_int16(p_ply ply, double *value) { - short int16; + int16_t int16; if (!ply->idriver->ichunk(ply, &int16, sizeof(int16))) return 0; *value = int16; return 1; } static int ibinary_uint16(p_ply ply, double *value) { - unsigned short uint16; + uint16_t uint16; if (!ply->idriver->ichunk(ply, &uint16, sizeof(uint16))) return 0; *value = uint16; return 1; } static int ibinary_int32(p_ply ply, double *value) { - long int32; + int32_t int32; if (!ply->idriver->ichunk(ply, &int32, sizeof(int32))) return 0; *value = int32; return 1; } static int ibinary_uint32(p_ply ply, double *value) { - unsigned long uint32; + uint32_t uint32; if (!ply->idriver->ichunk(ply, &uint32, sizeof(uint32))) return 0; *value = uint32; return 1; -- 1.7.6.1