ObjFW  Diff

Differences From Artifact [e62baac3c8]:

To Artifact [42f8137e1c]:


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
#include <string.h>

#include <time.h>

#include "macros.h"

const char*
of_strptime(const char *buffer, const char *format, struct tm *tm)
{
	enum {
		SEARCH_CONVERSION_SPECIFIER,
		IN_CONVERSION_SPECIFIER
	} state = SEARCH_CONVERSION_SPECIFIER;
	size_t j, buffer_len, format_len;

	buffer_len = strlen(buffer);
	format_len = strlen(format);

	j = 0;
	for (size_t i = 0; i < format_len; i++) {
		if (j >= buffer_len)
			return NULL;

		switch (state) {
		case SEARCH_CONVERSION_SPECIFIER:
			if (format[i] == '%')
				state = IN_CONVERSION_SPECIFIER;
			else if (format[i] != buffer[j++])
				return NULL;

			break;

		case IN_CONVERSION_SPECIFIER:;
			int k, max_len, number = 0;

			switch (format[i]) {
			case 'd':
			case 'e':
			case 'H':
			case 'm':
			case 'M':
			case 'S':
			case 'y':
				max_len = 2;
				break;
			case 'Y':
				max_len = 4;
				break;
			case '%':


			case 'n':
			case 't':

				max_len = 0;
				break;
			default:
				return NULL;
			}

			if (max_len > 0 && (buffer[j] < '0' || buffer[j] > '9'))
				return NULL;

			for (k = 0; k < max_len && j < buffer_len &&
			    buffer[j] >= '0' && buffer[j] <= '9'; k++, j++) {
				number *= 10;
				number += buffer[j] - '0';
			}

			switch (format[i]) {
























































			case 'd':
			case 'e':
				tm->tm_mday = number;
				break;
			case 'H':
				tm->tm_hour = number;
				break;







|





|

|
|


|
|












|









|


|


>
>


>
|





|


|






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
#include <string.h>

#include <time.h>

#include "macros.h"

const char*
of_strptime(const char *buffer, const char *format, struct tm *tm, int16_t *tz)
{
	enum {
		SEARCH_CONVERSION_SPECIFIER,
		IN_CONVERSION_SPECIFIER
	} state = SEARCH_CONVERSION_SPECIFIER;
	size_t j, bufferLen, formatLen;

	bufferLen = strlen(buffer);
	formatLen = strlen(format);

	j = 0;
	for (size_t i = 0; i < formatLen; i++) {
		if (j >= bufferLen)
			return NULL;

		switch (state) {
		case SEARCH_CONVERSION_SPECIFIER:
			if (format[i] == '%')
				state = IN_CONVERSION_SPECIFIER;
			else if (format[i] != buffer[j++])
				return NULL;

			break;

		case IN_CONVERSION_SPECIFIER:;
			int k, maxLen, number = 0;

			switch (format[i]) {
			case 'd':
			case 'e':
			case 'H':
			case 'm':
			case 'M':
			case 'S':
			case 'y':
				maxLen = 2;
				break;
			case 'Y':
				maxLen = 4;
				break;
			case '%':
			case 'a':
			case 'b':
			case 'n':
			case 't':
			case 'z':
				maxLen = 0;
				break;
			default:
				return NULL;
			}

			if (maxLen > 0 && (buffer[j] < '0' || buffer[j] > '9'))
				return NULL;

			for (k = 0; k < maxLen && j < bufferLen &&
			    buffer[j] >= '0' && buffer[j] <= '9'; k++, j++) {
				number *= 10;
				number += buffer[j] - '0';
			}

			switch (format[i]) {
			case 'a':
				if (bufferLen < j + 3)
					return NULL;

				if (memcmp(buffer + j, "Sun", 3) == 0)
					tm->tm_wday = 0;
				else if (memcmp(buffer + j, "Mon", 3) == 0)
					tm->tm_wday = 1;
				else if (memcmp(buffer + j, "Tue", 3) == 0)
					tm->tm_wday = 2;
				else if (memcmp(buffer + j, "Wed", 3) == 0)
					tm->tm_wday = 3;
				else if (memcmp(buffer + j, "Thu", 3) == 0)
					tm->tm_wday = 4;
				else if (memcmp(buffer + j, "Fri", 3) == 0)
					tm->tm_wday = 5;
				else if (memcmp(buffer + j, "Sat", 3) == 0)
					tm->tm_wday = 6;
				else
					return NULL;

				j += 3;
				break;
			case 'b':
				if (bufferLen < j + 3)
					return NULL;

				if (memcmp(buffer + j, "Jan", 3) == 0)
					tm->tm_mon = 1;
				else if (memcmp(buffer + j, "Feb", 3) == 0)
					tm->tm_mon = 2;
				else if (memcmp(buffer + j, "Mar", 3) == 0)
					tm->tm_mon = 3;
				else if (memcmp(buffer + j, "Apr", 3) == 0)
					tm->tm_mon = 4;
				else if (memcmp(buffer + j, "May", 3) == 0)
					tm->tm_mon = 5;
				else if (memcmp(buffer + j, "Jun", 3) == 0)
					tm->tm_mon = 6;
				else if (memcmp(buffer + j, "Jul", 3) == 0)
					tm->tm_mon = 7;
				else if (memcmp(buffer + j, "Aug", 3) == 0)
					tm->tm_mon = 8;
				else if (memcmp(buffer + j, "Sep", 3) == 0)
					tm->tm_mon = 9;
				else if (memcmp(buffer + j, "Oct", 3) == 0)
					tm->tm_mon = 10;
				else if (memcmp(buffer + j, "Nov", 3) == 0)
					tm->tm_mon = 11;
				else if (memcmp(buffer + j, "Dec", 3) == 0)
					tm->tm_mon = 12;
				else
					return NULL;

				j += 3;
				break;
			case 'd':
			case 'e':
				tm->tm_mday = number;
				break;
			case 'H':
				tm->tm_hour = number;
				break;
106
107
108
109
110
111
112






































113
114
115
116
117
118
119
				tm->tm_year = number;
				break;
			case 'Y':
				if (number < 1900)
					return NULL;

				tm->tm_year = number - 1900;






































				break;
			case '%':
				if (buffer[j++] != '%')
					return NULL;
				break;
			case 'n':
			case 't':







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
				tm->tm_year = number;
				break;
			case 'Y':
				if (number < 1900)
					return NULL;

				tm->tm_year = number - 1900;
				break;
			case 'z':
				if (buffer[j] == '-' || buffer[j] == '+') {
					const char *b = buffer + j;

					if (bufferLen < j + 5)
						return NULL;

					if (tz == NULL)
						break;

					*tz = (((int16_t)b[1] - '0') * 600 +
					    ((int16_t)b[2] - '0') * 60 +
					    ((int16_t)b[3] - '0') * 10 +
					    ((int16_t)b[4] - '0')) *
					    (b[0] == '-' ? -1 : 1);

					j += 5;
				} else if (buffer[j] == 'Z') {
					if (tz != NULL)
						*tz = 0;

					j++;
				} else if (buffer[j] == 'G') {
					if (bufferLen < j + 3)
						return NULL;

					if (buffer[j + 1] != 'M' ||
					    buffer[j + 2] != 'T')
						return NULL;

					if (tz != NULL)
						*tz = 0;

					j += 3;
				} else
					return NULL;

				break;
			case '%':
				if (buffer[j++] != '%')
					return NULL;
				break;
			case 'n':
			case 't':