summaryrefslogtreecommitdiff
blob: 45c911643bcb0991034f153343266a26a2ee6c66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
From eeb09032068baed6d81cff01cdfcccd6d55a8152 Mon Sep 17 00:00:00 2001
From: Alexandre Rostovtsev <tetromino@gmail.com>
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 <stdarg.h>
 #include <stdlib.h>
 #include <stddef.h>
+#include <stdint.h>
 
 #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