diff options
-rw-r--r-- | src/base64.cpp | 228 | ||||
-rw-r--r-- | src/base64.h | 61 | ||||
-rw-r--r-- | src/game.cpp | 4 | ||||
-rw-r--r-- | src/resources/mapreader.cpp | 65 | ||||
-rw-r--r-- | src/resources/mapreader.h | 7 |
5 files changed, 239 insertions, 126 deletions
diff --git a/src/base64.cpp b/src/base64.cpp index 57f2c9bb..e2df07de 100644 --- a/src/base64.cpp +++ b/src/base64.cpp @@ -1,104 +1,152 @@ /* - * This file is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This file is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this file; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ - */ + +----------------------------------------------------------------------+ + | PHP version 4.0 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 2.02 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available at through the world-wide-web at | + | http://www.php.net/license/2_02.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Jim Winstead (jimw@php.net) | + +----------------------------------------------------------------------+ + */ +/* $Id$ */ + +#include <string.h> +#include <stdlib.h> #include "base64.h" -#include <ctype.h> - -static const char base64digits[] = -"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -#define BAD -1 - -static const char base64val[] = { - BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, - BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, - BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,BAD,BAD, BAD,BAD,BAD,BAD, - BAD, 0, 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,BAD, BAD,BAD,BAD,BAD, - BAD, 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,BAD, BAD,BAD,BAD,BAD + +static char base64_table[] = +{ + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' }; +static char base64_pad = '='; -#define DECODE64(c) (isascii(c) ? base64val[c] : BAD) +unsigned char *php_base64_encode(const unsigned char *str, int length, int *ret_length) { + const unsigned char *current = str; + int i = 0; + unsigned char *result = (unsigned char *)malloc(((length + 3 - length % 3) * 4 / 3 + 1) * sizeof(char)); -void to64frombits(unsigned char *out, const unsigned char *in, int inlen) -{ - for (; inlen >= 3; inlen -= 3) - { - *out++ = base64digits[in[0] >> 2]; - *out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)]; - *out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)]; - *out++ = base64digits[in[2] & 0x3f]; - in += 3; + while (length > 2) { /* keep going until we have less than 24 bits */ + result[i++] = base64_table[current[0] >> 2]; + result[i++] = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; + result[i++] = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)]; + result[i++] = base64_table[current[2] & 0x3f]; + + current += 3; + length -= 3; /* we just handle 3 octets of data */ } - if (inlen > 0) - { - unsigned char fragment; - - *out++ = base64digits[in[0] >> 2]; - fragment = (in[0] << 4) & 0x30; - if (inlen > 1) - fragment |= in[1] >> 4; - *out++ = base64digits[fragment]; - *out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c]; - *out++ = '='; + + /* now deal with the tail end of things */ + if (length != 0) { + result[i++] = base64_table[current[0] >> 2]; + if (length > 1) { + result[i++] = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; + result[i++] = base64_table[(current[1] & 0x0f) << 2]; + result[i++] = base64_pad; + } + else { + result[i++] = base64_table[(current[0] & 0x03) << 4]; + result[i++] = base64_pad; + result[i++] = base64_pad; + } + } + if(ret_length) { + *ret_length = i; } - *out = '\0'; + result[i] = '\0'; + return result; } -int from64tobits(char *out, const char *in) -{ - int len = 0; - register unsigned char digit1, digit2, digit3, digit4; - - if (in[0] == '+' && in[1] == ' ') - in += 2; - if (*in == '\r') - return(0); - - do { - digit1 = in[0]; - if (DECODE64(digit1) == BAD) - return(-1); - digit2 = in[1]; - if (DECODE64(digit2) == BAD) - return(-1); - digit3 = in[2]; - if (digit3 != '=' && DECODE64(digit3) == BAD) - return(-1); - digit4 = in[3]; - if (digit4 != '=' && DECODE64(digit4) == BAD) - return(-1); - in += 4; - *out++ = (DECODE64(digit1) << 2) | (DECODE64(digit2) >> 4); - ++len; - if (digit3 != '=') - { - *out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2); - ++len; - if (digit4 != '=') - { - *out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4); - ++len; +/* as above, but backwards. :) */ +unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_length) { + const unsigned char *current = str; + int ch, i = 0, j = 0, k; + /* this sucks for threaded environments */ + static short reverse_table[256]; + static int table_built; + unsigned char *result; + + if (++table_built == 1) { + char *chp; + for(ch = 0; ch < 256; ch++) { + chp = strchr(base64_table, ch); + if(chp) { + reverse_table[ch] = chp - base64_table; + } else { + reverse_table[ch] = -1; } } - } while (*in && *in != '\r' && digit4 != '='); + } - return (len); + result = (unsigned char *)malloc(length + 1); + if (result == NULL) { + return NULL; + } + + /* run through the whole string, converting as we go */ + while ((ch = *current++) != '\0') { + if (ch == base64_pad) break; + + /* When Base64 gets POSTed, all pluses are interpreted as spaces. + This line changes them back. It's not exactly the Base64 spec, + but it is completely compatible with it (the spec says that + spaces are invalid). This will also save many people considerable + headache. - Turadg Aleahmad <turadg@wise.berkeley.edu> + */ + + if (ch == ' ') ch = '+'; + + ch = reverse_table[ch]; + if (ch < 0) continue; + + switch(i % 4) { + case 0: + result[j] = ch << 2; + break; + case 1: + result[j++] |= ch >> 4; + result[j] = (ch & 0x0f) << 4; + break; + case 2: + result[j++] |= ch >>2; + result[j] = (ch & 0x03) << 6; + break; + case 3: + result[j++] |= ch; + break; + } + i++; + } + + k = j; + /* mop things up if we ended on a boundary */ + if (ch == base64_pad) { + switch(i % 4) { + case 0: + case 1: + free(result); + return NULL; + case 2: + k++; + case 3: + result[k++] = 0; + } + } + if(ret_length) { + *ret_length = j; + } + result[k] = '\0'; + return result; } + diff --git a/src/base64.h b/src/base64.h index 2bf93a41..2e5bd69e 100644 --- a/src/base64.h +++ b/src/base64.h @@ -1,31 +1,44 @@ /* - * This file is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This file is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this file; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ + +----------------------------------------------------------------------+ + | PHP HTML Embedded Scripting Language Version 3.0 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997,1998 PHP Development Team (See Credits file) | + +----------------------------------------------------------------------+ + | This program is free software; you can redistribute it and/or modify | + | it under the terms of one of the following licenses: | + | | + | A) the GNU General Public License as published by the Free Software | + | Foundation; either version 2 of the License, or (at your option) | + | any later version. | + | | + | B) the PHP License as published by the PHP Development Team and | + | included in the distribution in the file: LICENSE | + | | + | This program is distributed in the hope that it will be useful, | + | but WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | + | GNU General Public License for more details. | + | | + | You should have received a copy of both licenses referred to here. | + | If you did not, or have any questions about PHP licensing, please | + | contact core@php.net. | + +----------------------------------------------------------------------+ + | Author: Jim Winstead (jimw@php.net) | + +----------------------------------------------------------------------+ */ +/* $Id$ */ -#ifndef _TMW_BASE64_H +#ifndef _BASE64_h +#define _BASE64_h -/* - * Converts binary data to a base64 string. - */ -void to64frombits(unsigned char *out, const unsigned char *in, int inlen); +extern unsigned char *php_base64_encode(const unsigned char *, int, int *); +extern unsigned char *php_base64_decode(const unsigned char *, int, int *); + +#endif /* _BASE64_h */ /* - * Converts a base64 string to binary data. + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: */ -int from64tobits(char *out, const char *in); - -#endif diff --git a/src/game.cpp b/src/game.cpp index 4d89bd67..5bcb821f 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -125,8 +125,8 @@ void game() { void do_init() { - tiledMap = Map::load(map_path); - //tiledMap = MapReader::readMap("core/maps/desert.tmx"); + //tiledMap = Map::load(map_path); + tiledMap = MapReader::readMap("core/maps/desert.tmx"); if (!tiledMap) { error("Could not find map file"); } diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 20438c9d..c5f3b837 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -25,6 +25,7 @@ #include "../log.h" #include "resourcemanager.h" #include "../graphic/spriteset.h" +#include "../base64.h" #include <iostream> @@ -165,7 +166,46 @@ void MapReader::readLayer(xmlNodePtr node, Map *map, int layer) } // Read base64 encoded map file - + xmlNodePtr dataChild = node->xmlChildrenNode; + if (!dataChild) continue; + + int len = strlen((const char*)dataChild->content) + 1; + unsigned char *charData = new unsigned char[len + 1]; + const char *charStart = (const char*)dataChild->content; + unsigned char *charIndex = charData; + + while (*charStart) { + if (*charStart != ' ' && *charStart != '\t' && + *charStart != '\n') + { + *charIndex = *charStart; + charIndex++; + } + charStart++; + } + *charIndex = '\0'; + + int binLen; + unsigned char *binData = + php_base64_decode(charData, strlen((char*)charData), + &binLen); + + delete[] charData; + + if (binData) { + for (int i = 0; i < binLen - 3; i += 4) { + int gid = binData[i] | + binData[i + 1] << 8 | + binData[i + 2] << 16 | + binData[i + 3] << 24; + + setTileWithGid(map, x, y, layer, gid); + + x++; + if (x == w) {x = 0; y++;} + } + free(binData); + } } else { // Read plain XML map file @@ -176,15 +216,7 @@ void MapReader::readLayer(xmlNodePtr node, Map *map, int layer) if (xmlStrEqual(n2->name, BAD_CAST "tile") && y < h) { int gid = getProperty(n2, "gid", -1); - if (layer == 3) - { - Tileset *set = getTilesetWithGid(gid); - map->setWalk(x, y, - !set || (gid - set->getFirstGid() == 0)); - } - else if (layer < 3) { - map->setTile(x, y, layer, getTileWithGid(gid)); - } + setTileWithGid(map, x, y, layer, gid); x++; if (x == w) {x = 0; y++;} @@ -296,3 +328,16 @@ Tileset *MapReader::getTilesetWithGid(int gid) return NULL; } + +void MapReader::setTileWithGid(Map *map, int x, int y, int layer, int gid) +{ + if (layer == 3) + { + Tileset *set = getTilesetWithGid(gid); + map->setWalk(x, y, + !set || (gid - set->getFirstGid() == 0)); + } + else if (layer < 3) { + map->setTile(x, y, layer, getTileWithGid(gid)); + } +} diff --git a/src/resources/mapreader.h b/src/resources/mapreader.h index 59270f25..241945f1 100644 --- a/src/resources/mapreader.h +++ b/src/resources/mapreader.h @@ -94,6 +94,13 @@ class MapReader */ static Tileset *MapReader::getTilesetWithGid(int gid); + /** + * Sets a tile using a global tile id. Used by the layer loading + * routine. + */ + static void MapReader::setTileWithGid( + Map *map, int x, int y, int layer, int gid); + static std::vector<Tileset*> tilesets; }; |