Fix.
[gsasl.git] / lib / gs2 / gs2parser.c
blob13f52290223f486a847cb7ea0cc52f05f91344fa
1 /* gs2parser.h --- GS2 parser.
2 * Copyright (C) 2006 Simon Josefsson
4 * This file is part of GNU SASL Library.
6 * GNU SASL Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * GNU SASL Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with GNU SASL Library; if not, write to the Free
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include "gs2parser.h"
25 int
26 gs2_parser (const char *token, size_t toklen, struct gs2_token *out)
28 uint32_t ctxlen;
30 /* Packets shorter than 4 octets are invalid. */
31 if (toklen < 4)
32 return 1;
34 ctxlen = token[0] << 24 | token[1] << 16 | token[2] << 8 | token[3];
36 /* If the length field is longer than the entire packet size, minus
37 4 octets, the packet is invalid. */
38 if (ctxlen > toklen - 4)
39 return 1;
41 out->context_length = ctxlen;
42 out->context_token = token + 4;
44 out->wrap_length = toklen - ctxlen - 4;
45 out->wrap_token = out->wrap_length > 0 ? token + 4 + out->wrap_length : NULL;
47 return 0;