Skip to content

Commit

Permalink
Fix bug in CD_BufferAddString.
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Feb 28, 2011
1 parent 1322192 commit 2e0678c
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 93 deletions.
4 changes: 2 additions & 2 deletions include/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pkginclude_HEADERS = craftd/Arith.h \
pkginclude_HEADERS = craftd/Arithmetic.h \
craftd/common.h \
craftd/config.h \
craftd/Config.h \
craftd/Hash.h \
craftd/Buffer.h \
craftd/Buffer.h \
craftd/Buffers.h \
craftd/javaendian.h \
craftd/Job.h \
Expand Down
71 changes: 49 additions & 22 deletions include/craftd/Arith.h → include/craftd/Arithmetic.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#ifndef CRAFTD_ARITH_H
#define CRAFTD_ARITH_H

/*
* Adapted by Kevin M. Bowling for compiler inlining
*
Expand All @@ -11,73 +8,100 @@
* license.
*/

#ifndef CRAFTD_ARITHMETIC_H
#define CRAFTD_ARITHMETIC_H

/**
* Return the maximum of two values
*
* @param x an integer value
* @param y an integer value
*
* @return greater of the two parameters
*/
static inline int CD_Max(int x, int y)
static inline
int
CD_Max (int x, int y)
{
return x > y ? x : y;
}

/**
* Return the minimum of two values
*
* @param x an integer value
* @param y an integer value
*
* @return smaller of the two parameters
*/
static inline int CD_Min(int x, int y)
static inline
int
CD_Min (int x, int y)
{
return x > y ? y : x;
return x > y ? y : x;
}

/**
* Perform well defined integer division
*
* The C standard has goofy semantics wrt negative numbers and integer division
* This one always truncates toward left on number line and works as expected
*
*
* @param x the dividend
* @param y the divisor
*
* @return the integer quotient
*/
static inline int CD_Div(int x, int y)
static inline
int
CD_Div (int x, int y)
{
if (-13/5 == -2 && (x < 0) != (y < 0) && x%y != 0)
return x/y - 1;
else
return x/y;
if (-13 / 5 == -2 && (x < 0) != (y < 0) && x % y != 0) {
return x / y - 1;
}
else {
return x / y;
}
}

/**
* Perform well defined modulo division
*
* The C standard has goofy semantics wrt negative numbers and modulo division
* This one always truncates toward left on number line and works as expected
*
*
* @param x the dividend
* @param y the divisor
*
* @return the integer quotient
*/
static inline int CD_Mod(int x, int y)
static inline
int
CD_Mod (int x, int y)
{
if (-13/5 == -2 && (x < 0) != (y < 0) && x%y != 0)
return x%y + y;
else
return x%y;
if (-13 / 5 == -2 && (x < 0) != (y < 0) && x % y != 0) {
return x % y + y;
}
else {
return x % y;
}
}

/**
* Perform well defined integer floor
*
* In the x/y, returns the integer to the left on the number line
*
* @param x dividend
* @param y divisior
*
* @return floor of x/y
*/
static inline int CD_Floor(int x, int y)
static inline
int
CD_Floor (int x, int y)
{
return CD_Div(x, y);
return CD_Div(x, y);
}

/**
Expand All @@ -86,11 +110,14 @@ static inline int CD_Floor(int x, int y)
*
* @param x dividend
* @param y divisior
*
* @return ceiling of x/y
*/
static inline int CD_Ceiling(int x, int y)
static inline
int
CD_Ceiling (int x, int y)
{
return CD_Div(x, y) + (x%y != 0);
return CD_Div(x, y) + (x % y != 0);
}

#endif
8 changes: 3 additions & 5 deletions include/craftd/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#ifndef CRAFTD_COMMON_H
#define CRAFTD_COMMON_H

#include <craftd/config.h>

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
Expand All @@ -43,15 +41,15 @@
#include <event2/listener.h>
#include <event2/thread.h>

#include <craftd/config.h>
#include <craftd/memory.h>

#if SIZEOF_FP == 4 && SIZEOF_INTPTR_T == 4
typedef int32_t CDPointer;
#else
typedef int64_t CDPointer;
#endif

#include <craftd/config.h>
#include <craftd/memory.h>

#include <craftd/List.h>
#include <craftd/Map.h>
#include <craftd/Hash.h>
Expand Down
126 changes: 62 additions & 64 deletions plugins/cdnbt/cdnbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <limits.h>
#include <sys/stat.h>

#include <craftd/Server.h>
Expand All @@ -39,95 +38,94 @@ static int spawnX, spawnY, spawnZ;
* Run at plugin startup to initialize Config.SpawnPos struct with default spawn
* coordinates and make sure the world directory exists.
*
* @return 0 on success, -1 otherwise
* @return true on success, false otherwise
*/
static int
cdnbt_LoadLevelDat(CDPlugin* self)
static
bool
cdnbt_LoadLevelDat (CDPlugin* self)
{
struct stat buf;
int worldstat = stat(world_dir, &buf);

if (worldstat == -1)
{
LOG(LOG_ERR, "World directory %s does not exist", world_dir);
return -1;
}

char leveldatpath[PATH_MAX];
nbt_file *nf;
nbt_tag *data;

if (nbt_init(&nf) != NBT_OK)
{
LOG(LOG_ERR, "Cannot initialize level.dat structure");
return -1;
}

evutil_snprintf(leveldatpath, PATH_MAX, "%s/level.dat", world_dir);

if (nbt_parse(nf, leveldatpath) != NBT_OK)
{
LOG(LOG_ERR, "Cannot parse level.dat (%d).", leveldatpath);
return -1;
}

data = nbt_find_tag_by_name("Data", nbt_cast_compound(nf->root));

nbt_tag *t_gametime = nbt_find_tag_by_name("Time", nbt_cast_compound(data));
nbt_tag *t_spawnX = nbt_find_tag_by_name("SpawnX", nbt_cast_compound(data));
nbt_tag *t_spawnY = nbt_find_tag_by_name("SpawnY", nbt_cast_compound(data));
nbt_tag *t_spawnZ = nbt_find_tag_by_name("SpawnZ", nbt_cast_compound(data));

CD_ServerSetTime(self->server, *(nbt_cast_int(t_gametime)));
spawnX = *(nbt_cast_int(t_spawnX));
spawnY = *(nbt_cast_int(t_spawnY));
spawnZ = *(nbt_cast_int(t_spawnZ));

nbt_free(nf);
return 0;
struct stat buf;
int worldstat = stat(world_dir, &buf);

if (worldstat == -1) {
ERR("World directory %s does not exist", world_dir);
return false;
}

CDString* leveldatPath;
nbt_file* nf;
nbt_tag* data;

if (nbt_init(&nf) != NBT_OK) {
ERR("Cannot initialize level.dat structure");
return false;
}

leveldatPath = CD_CreateStringFromFormat("%s/level.dat", world_dir);

if (nbt_parse(nf, CD_StringContent(leveldatPath)) != NBT_OK) {
ERR("Cannot parse level.dat (%s).", CD_StringContent(leveldatPath));
return false;
}

data = nbt_find_tag_by_name("Data", nbt_cast_compound(nf->root));

nbt_tag* t_gametime = nbt_find_tag_by_name("Time", nbt_cast_compound(data));
nbt_tag* t_spawnX = nbt_find_tag_by_name("SpawnX", nbt_cast_compound(data));
nbt_tag* t_spawnY = nbt_find_tag_by_name("SpawnY", nbt_cast_compound(data));
nbt_tag* t_spawnZ = nbt_find_tag_by_name("SpawnZ", nbt_cast_compound(data));

CD_ServerSetTime(self->server, *(nbt_cast_int(t_gametime)));

spawnX = *(nbt_cast_int(t_spawnX));
spawnY = *(nbt_cast_int(t_spawnY));
spawnZ = *(nbt_cast_int(t_spawnZ));

nbt_free(nf);

CD_DestroyString(leveldatPath);

return true;
}

int valid_chunk(nbt_tag *nbtroot)
bool
valid_chunk (nbt_tag *nbtroot)
{
/* Check valid root element */
nbt_tag *root = nbt_find_tag_by_name("Level", nbt_cast_compound(nbtroot));
nbt_tag* root = nbt_find_tag_by_name("Level", nbt_cast_compound(nbtroot));

if ((root != NULL) &&
(strcmp(root->name, "Level") == 0) && (root->type == TAG_COMPOUND))
{
nbt_tag *blocks = nbt_find_tag_by_name("Blocks", nbt_cast_compound(root));
if ((root != NULL) && (strcmp(root->name, "Level") == 0) && (root->type == TAG_COMPOUND)) {
nbt_tag* blocks = nbt_find_tag_by_name("Blocks", nbt_cast_compound(root));

if ((blocks != NULL) && (blocks->type == TAG_BYTE_ARRAY))
{
nbt_byte_array *arr = (nbt_byte_array *)blocks->value;
if ((blocks != NULL) && (blocks->type == TAG_BYTE_ARRAY)) {
nbt_byte_array* arr = (nbt_byte_array*) blocks->value;

if (arr->length == 32768)
{
return 0; /* Valid at last. */
if (arr->length == 32768) {
return true; /* Valid at last. */
}
}
}
return 1;

return false;
}

static
bool
cdnbt_LoadChunk()
{
// zomg
return true;
// zomg
return true;
}

extern
bool
CD_PluginInitialize (CDPlugin* self)
{
cdnbt_LoadLevelDat(self);
cdnbt_LoadLevelDat(self);

CD_EventRegister(self->server, "Chunk.load", cdnbt_LoadChunk);
CD_EventRegister(self->server, "Chunk.load", cdnbt_LoadChunk);

return true;
return true;
}

extern
Expand Down
3 changes: 3 additions & 0 deletions src/Buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ CD_BufferAddDouble (CDBuffer* self, MCDouble data)
void
CD_BufferAddString (CDBuffer* self, CDString* data)
{
MCShort size = htons(CD_StringSize(data));

evbuffer_add(self->raw, &size, MCShortSize);
evbuffer_add(self->raw, CD_StringContent(data), CD_StringSize(data));
}

Expand Down

0 comments on commit 2e0678c

Please sign in to comment.