Fix buffer possible overflow when writings tags
[xboard.git] / pgntags.c
1 /*
2  * pgntags.c -- Functions to manage PGN tags
3  *
4  * Copyright 1995, 2009, 2010, 2011 Free Software Foundation, Inc.
5  *
6  * Enhancements Copyright 2005 Alessandro Scotti
7  *
8  * ------------------------------------------------------------------------
9  *
10  * GNU XBoard is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or (at
13  * your option) any later version.
14  *
15  * GNU XBoard is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see http://www.gnu.org/licenses/.  *
22  *
23  * ------------------------------------------------------------------------
24  *
25  * This file could well be a part of backend.c, but I prefer it this
26  * way.
27  */
28
29 #include "config.h"
30
31 #include <stdio.h>
32 #include <errno.h>
33 #include <ctype.h>
34 #if STDC_HEADERS
35 # include <stdlib.h>
36 # include <string.h>
37 #else /* not STDC_HEADERS */
38 # if HAVE_STRING_H
39 #  include <string.h>
40 # else /* not HAVE_STRING_H */
41 #  include <strings.h>
42 # endif /* not HAVE_STRING_H */
43 #endif /* not STDC_HEADERS */
44
45 #include "common.h"
46 #include "frontend.h"
47 #include "backend.h"
48 #include "parser.h"
49
50
51 /* Parse PGN tags; returns 0 for success or error number
52  */
53 int ParsePGNTag(tag, gameInfo)
54     char *tag;
55     GameInfo *gameInfo;
56 {
57     char *name, *value, *p, *oldTags;
58     int len;
59     int success;
60
61     name = tag;
62     while (!isalpha(*name) && !isdigit(*name)) {
63         name++;
64     }
65     p = name;
66     while (*p != ' ' && *p != '\t' && *p != '\n') {
67         p++;
68     }
69     *p = NULLCHAR;
70     value = strchr(p + 1, '"') + 1;
71     p = strrchr(value, '"');
72     *p = NULLCHAR;
73
74     if (StrCaseCmp(name, "Event") == 0) {
75         success = StrSavePtr(value, &gameInfo->event) != NULL;
76     } else if (StrCaseCmp(name, "Site") == 0) {
77         success = StrSavePtr(value, &gameInfo->site) != NULL;
78     } else if (StrCaseCmp(name, "Date") == 0) {
79         success = StrSavePtr(value, &gameInfo->date) != NULL;
80     } else if (StrCaseCmp(name, "Round") == 0) {
81         success = StrSavePtr(value, &gameInfo->round) != NULL;
82     } else if (StrCaseCmp(name, "White") == 0) {
83         success = StrSavePtr(value, &gameInfo->white) != NULL;
84     } else if (StrCaseCmp(name, "Black") == 0) {
85         success = StrSavePtr(value, &gameInfo->black) != NULL;
86     }
87     /* Fold together the various ways of denoting White/Black rating */
88     else if ((StrCaseCmp(name, "WhiteElo")==0) ||
89              (StrCaseCmp(name, "WhiteUSCF")==0) ) {
90       success = TRUE;
91       gameInfo->whiteRating = atoi( value );
92     } else if ((StrCaseCmp(name, "BlackElo")==0) ||
93                (StrCaseCmp(name, "BlackUSCF")==0)) {
94       success = TRUE;
95       gameInfo->blackRating = atoi( value );
96     }
97     else if (StrCaseCmp(name, "Result") == 0) {
98         if (strcmp(value, "1-0") == 0)
99             gameInfo->result = WhiteWins;
100         else if (strcmp(value, "0-1") == 0)
101             gameInfo->result = BlackWins;
102         else if (strcmp(value, "1/2-1/2") == 0)
103             gameInfo->result = GameIsDrawn;
104         else
105             gameInfo->result = GameUnfinished;
106         success = TRUE;
107     } else if (StrCaseCmp(name, "TimeControl") == 0) {
108 //      int tc, mps, inc = -1;
109 //      if(sscanf(value, "%d/%d", &mps, &tc) == 2 || )
110         success = StrSavePtr(value, &gameInfo->timeControl) != NULL;
111     } else if (StrCaseCmp(name, "FEN") == 0) {
112         success = StrSavePtr(value, &gameInfo->fen) != NULL;
113     } else if (StrCaseCmp(name, "SetUp") == 0) {
114         /* ignore on input; presence of FEN governs */
115         success = TRUE;
116     } else if (StrCaseCmp(name, "Variant") == 0) {
117         /* xboard-defined extension */
118         gameInfo->variant = StringToVariant(value);
119         success = TRUE;
120     } else if (StrCaseCmp(name, PGN_OUT_OF_BOOK) == 0) {
121         /* [AS] Out of book annotation */
122         success = StrSavePtr(value, &gameInfo->outOfBook) != NULL;
123     } else {
124         if (gameInfo->extraTags == NULL) {
125             oldTags = "";
126         } else {
127             oldTags = gameInfo->extraTags;
128         }
129         /* Buffer size includes 7 bytes of space for [ ""]\n\0 */
130         len = strlen(oldTags) + strlen(value) + strlen(name) + 7;
131         if ((p = (char *) malloc(len))  !=  NULL) {
132             sprintf(p, "%s[%s \"%s\"]\n", oldTags, name, value);
133             if (gameInfo->extraTags != NULL) free(gameInfo->extraTags);
134             gameInfo->extraTags = p;
135             success = TRUE;
136         } else {
137             success = FALSE;
138         }
139     }
140     return(success ? 0 : ENOMEM);
141 }
142
143
144 /* Print game info
145  */
146 void PrintPGNTags(fp, gameInfo)
147      FILE *fp;
148      GameInfo *gameInfo;
149 {
150     fprintf(fp, "[Event \"%s\"]\n", gameInfo->event ? gameInfo->event : "?");
151     fprintf(fp, "[Site \"%s\"]\n", gameInfo->site ? gameInfo->site : "?");
152     fprintf(fp, "[Date \"%s\"]\n", gameInfo->date ? gameInfo->date : "?");
153     fprintf(fp, "[Round \"%s\"]\n", gameInfo->round ? gameInfo->round : "-");
154     fprintf(fp, "[White \"%s\"]\n", gameInfo->white ? gameInfo->white : "?");
155     fprintf(fp, "[Black \"%s\"]\n", gameInfo->black ? gameInfo->black : "?");
156     fprintf(fp, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
157     if (gameInfo->whiteRating >= 0)
158         fprintf(fp, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating);
159     if (gameInfo->blackRating >= 0)
160         fprintf(fp, "[BlackElo \"%d\"]\n", gameInfo->blackRating);
161     if (gameInfo->timeControl)
162         fprintf(fp, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
163     if (gameInfo->variant != VariantNormal)
164         fprintf(fp, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
165     if (gameInfo->extraTags)
166         fputs(gameInfo->extraTags, fp);
167 }
168
169
170 /* Return a non-static buffer with a games info.
171  */
172 char *PGNTags(gameInfo)
173     GameInfo *gameInfo;
174 {
175     size_t len;
176     char *buf;
177     char *p;
178
179     // First calculate the needed buffer size.
180     // Then we don't have to check the buffer size later.
181     len = 12 + 11 + 11 + 12 + 12 + 12 + 25 + 1; // The first 7 tags
182     if (gameInfo->event) len += strlen(gameInfo->event);
183     if (gameInfo->site)  len += strlen(gameInfo->site);
184     if (gameInfo->date)  len += strlen(gameInfo->date);
185     if (gameInfo->round) len += strlen(gameInfo->round);
186     if (gameInfo->white) len += strlen(gameInfo->white);
187     if (gameInfo->black) len += strlen(gameInfo->black);
188     if (gameInfo->whiteRating >= 0) len += 40;
189     if (gameInfo->blackRating >= 0) len += 40;
190     if (gameInfo->timeControl) len += strlen(gameInfo->timeControl) + 20;
191     if (gameInfo->variant != VariantNormal) len += 50;
192     if (gameInfo->extraTags) len += strlen(gameInfo->extraTags);
193
194     buf = malloc(len);
195     if (!buf)
196         return 0;
197
198     p = buf;
199     p += sprintf(p, "[Event \"%s\"]\n", gameInfo->event ? gameInfo->event : "?");
200     p += sprintf(p, "[Site \"%s\"]\n", gameInfo->site ? gameInfo->site : "?");
201     p += sprintf(p, "[Date \"%s\"]\n", gameInfo->date ? gameInfo->date : "?");
202     p += sprintf(p, "[Round \"%s\"]\n", gameInfo->round ? gameInfo->round : "-");
203     p += sprintf(p, "[White \"%s\"]\n", gameInfo->white ? gameInfo->white : "?");
204     p += sprintf(p, "[Black \"%s\"]\n", gameInfo->black ? gameInfo->black : "?");
205     p += sprintf(p, "[Result \"%s\"]\n", PGNResult(gameInfo->result));
206     if (gameInfo->whiteRating >= 0)
207         p += sprintf(p, "[WhiteElo \"%d\"]\n", gameInfo->whiteRating);
208     if (gameInfo->blackRating >= 0)
209         p += sprintf(p, "[BlackElo \"%d\"]\n", gameInfo->blackRating);
210     if (gameInfo->timeControl)
211         p += sprintf(p, "[TimeControl \"%s\"]\n", gameInfo->timeControl);
212     if (gameInfo->variant != VariantNormal)
213         p += sprintf(p, "[Variant \"%s\"]\n", VariantName(gameInfo->variant));
214     if (gameInfo->extraTags)
215         strcpy(p, gameInfo->extraTags);
216     return buf;
217 }
218
219
220 /* Returns pointer to a static string with a result.
221  */
222 char *PGNResult(result)
223      ChessMove result;
224 {
225     switch (result) {
226       case GameUnfinished:
227       default:
228         return "*";
229       case WhiteWins:
230         return "1-0";
231       case BlackWins:
232         return "0-1";
233       case GameIsDrawn:
234         return "1/2-1/2";
235     }
236 }
237
238 /* Returns 0 for success, nonzero for error */
239 int
240 ReplaceTags(tags, gameInfo)
241      char *tags;
242      GameInfo *gameInfo;
243 {
244     ChessMove moveType;
245     int err;
246
247     ClearGameInfo(gameInfo);
248     yynewstr(tags);
249     for (;;) {
250         yyboardindex = 0;
251         moveType = (ChessMove) Myylex();
252         if (moveType == (ChessMove) 0) {
253             break;
254         } else if (moveType == PGNTag) {
255             err = ParsePGNTag(yy_text, gameInfo);
256             if (err != 0) return err;
257         }
258     }
259     /* just one problem...if there is a result in the new tags,
260      * DisplayMove() won't ever show it because ClearGameInfo() set
261      * gameInfo->resultDetails to NULL. So we must plug something in if there
262      * is a result.
263      */
264     if (gameInfo->result != GameUnfinished) {
265       if (gameInfo->resultDetails) free(gameInfo->resultDetails);
266       gameInfo->resultDetails = strdup("");
267     }
268     return 0;
269 }