Skip to content

Commit

Permalink
windows port of libbson
Browse files Browse the repository at this point in the history
Compatibility
o Migrated internal int types to bson_intN_t from intN_t.  Had to, no
  stdint on windows (but some types existed.  This is cleaner)
o Added a bunch of casts to quiet warnings
o Replaced functions that aren't available or create warnings on windows
o add a cmake build script under build/win32.  Builds and installs under
  win32
o added bson-compat.h, which wraps up a lot of the windows trivia

Bug fixes
o extensive changes to bcon to accommodate windows (some bugs discovered
  due to differences in the varargs calling convention).
  • Loading branch information
hanumantmk committed Jan 23, 2014
1 parent c107136 commit 0d4903f
Show file tree
Hide file tree
Showing 50 changed files with 818 additions and 438 deletions.
2 changes: 2 additions & 0 deletions bson/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ endif

INST_H_FILES = \
bson/bson.h \
bson/bson-compat.h \
bson/bson-config.h \
bson/bson-context.h \
bson/bson-clock.h \
Expand Down Expand Up @@ -54,6 +55,7 @@ libbson_1_0_la_SOURCES = \
$(INST_H_FILES) \
$(NOINST_H_FILES) \
bson/bson.c \
bson/bson-compat.c \
bson/bson-context.c \
bson/bson-clock.c \
bson/bson-error.c \
Expand Down
20 changes: 8 additions & 12 deletions bson/b64_ntop.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,9 @@
* IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
*/

#include <sys/types.h>
#include <sys/param.h>

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bson-compat.h"
#include "bson-macros.h"
#include "bson-types.h"

#define Assert(Cond) if (!(Cond)) abort ()

Expand Down Expand Up @@ -118,14 +114,14 @@ static const char Pad64 = '=';

int
b64_ntop (uint8_t const *src,
size_t srclength,
bson_size_t srclength,
char *target,
size_t targsize)
bson_size_t targsize)
{
size_t datalength = 0;
bson_size_t datalength = 0;
uint8_t input[3];
uint8_t output[4];
size_t i;
bson_size_t i;

while (2 < srclength) {
input[0] = *src++;
Expand Down Expand Up @@ -184,5 +180,5 @@ b64_ntop (uint8_t const *src,
return -1;
}
target[datalength] = '\0'; /* Returned value doesn't count \0. */
return datalength;
return (int)datalength;
}
13 changes: 6 additions & 7 deletions bson/bcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

#include "bcon.h"
#include <error.h>

/* These stack manipulation macros are used to manage append recursion in
* bcon_append_ctx_va(). They take care of some awkward dereference rules (the
Expand Down Expand Up @@ -101,7 +100,7 @@ typedef union bcon_append {

bson_oid_t *OID;
bson_bool_t BOOL;
struct timeval DATE_TIME;
bson_int64_t DATE_TIME;

struct
{
Expand Down Expand Up @@ -157,7 +156,7 @@ typedef union bcon_extract {

const bson_oid_t **OID;
bson_bool_t *BOOL;
struct timeval *DATE_TIME;
bson_int64_t *DATE_TIME;

struct
{
Expand Down Expand Up @@ -230,7 +229,7 @@ _bcon_append_single (bson_t *bson,
bson_append_bool (bson, key, -1, val->BOOL);
break;
case BCON_TYPE_DATE_TIME:
bson_append_timeval (bson, key, -1, &val->DATE_TIME);
bson_append_date_time (bson, key, -1, val->DATE_TIME);
break;
case BCON_TYPE_NULL:
bson_append_null (bson, key, -1);
Expand Down Expand Up @@ -338,7 +337,7 @@ _bcon_extract_single (const bson_iter_t *iter,
break;
case BCON_TYPE_DATE_TIME:
CHECK_TYPE (BSON_TYPE_DATE_TIME);
bson_iter_timeval (iter, val->DATE_TIME);
*val->DATE_TIME = bson_iter_date_time (iter);
break;
case BCON_TYPE_NULL:
CHECK_TYPE (BSON_TYPE_NULL);
Expand Down Expand Up @@ -485,7 +484,7 @@ _bcon_append_tokenize (va_list *ap,
u->BOOL = va_arg (*ap, bson_bool_t);
break;
case BCON_TYPE_DATE_TIME:
u->DATE_TIME = va_arg (*ap, struct timeval);
u->DATE_TIME = va_arg (*ap, bson_int64_t);
break;
case BCON_TYPE_NULL:
break;
Expand Down Expand Up @@ -615,7 +614,7 @@ _bcon_extract_tokenize (va_list *ap,
u->BOOL = va_arg (*ap, bson_bool_t *);
break;
case BCON_TYPE_DATE_TIME:
u->DATE_TIME = va_arg (*ap, struct timeval *);
u->DATE_TIME = va_arg (*ap, bson_int64_t *);
break;
case BCON_TYPE_NULL:
break;
Expand Down
68 changes: 37 additions & 31 deletions bson/bcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,36 @@
#define BCON_STACK_MAX 100

#define BCON_ENSURE_DECLARE(fun, type) \
static BSON_INLINE bson_bool_t bcon_ensure_##fun (type _t) { return FALSE; }
static BSON_INLINE type bcon_ensure_##fun (type _t) { return _t; }

#define BCON_ENSURE(fun, val) \
((FALSE) ? bcon_ensure_##fun (val) ? (val) : (val) : (val))
bcon_ensure_##fun (val)

#define BCON_ENSURE_STORAGE(fun, val) \
((FALSE) ? bcon_ensure_##fun (val) ? &(val) : &(val) : &(val))
bcon_ensure_##fun (&(val))

BCON_ENSURE_DECLARE (const_char_ptr, const char *)
BCON_ENSURE_DECLARE (const_char_ptr_ptr, const char **)
BCON_ENSURE_DECLARE (double, double)
BCON_ENSURE_DECLARE (double_ptr, double *)
BCON_ENSURE_DECLARE (const_bson_ptr, const bson_t *)
BCON_ENSURE_DECLARE (bson_ptr, bson_t *)
BCON_ENSURE_DECLARE (bson, bson_t)
BCON_ENSURE_DECLARE (subtype, bson_subtype_t)
BCON_ENSURE_DECLARE (subtype_ptr, bson_subtype_t *)
BCON_ENSURE_DECLARE (const_uint8_ptr, const bson_uint8_t *)
BCON_ENSURE_DECLARE (const_uint8_ptr_ptr, const bson_uint8_t **)
BCON_ENSURE_DECLARE (uint32, bson_uint32_t)
BCON_ENSURE_DECLARE (timeval, struct timeval)
BCON_ENSURE_DECLARE (uint32_ptr, bson_uint32_t *)
BCON_ENSURE_DECLARE (const_oid_ptr, const bson_oid_t *)
BCON_ENSURE_DECLARE (oid_ptr, bson_oid_t *)
BCON_ENSURE_DECLARE (const_oid_ptr_ptr, const bson_oid_t **)
BCON_ENSURE_DECLARE (int32, bson_int32_t)
BCON_ENSURE_DECLARE (int32_ptr, bson_int32_t *)
BCON_ENSURE_DECLARE (int64, bson_int64_t)
BCON_ENSURE_DECLARE (int64_ptr, bson_int64_t *)
BCON_ENSURE_DECLARE (bool, bson_bool_t)
BCON_ENSURE_DECLARE (bool_ptr, bson_bool_t *)
BCON_ENSURE_DECLARE (bson_type, bson_type_t)
BCON_ENSURE_DECLARE (bson_iter, bson_iter_t)
BCON_ENSURE_DECLARE (bson_iter_ptr, bson_iter_t *)
BCON_ENSURE_DECLARE (const_bson_iter_ptr, const bson_iter_t *)

#define BCON_UTF8(_val) \
Expand All @@ -71,7 +77,7 @@ BCON_ENSURE_DECLARE (const_bson_iter_ptr, const bson_iter_t *)
#define BCON_BOOL(_val) \
BCON_MAGIC, BCON_TYPE_BOOL, BCON_ENSURE (bool, (_val))
#define BCON_DATE_TIME(_val) \
BCON_MAGIC, BCON_TYPE_DATE_TIME, BCON_ENSURE (timeval, (_val))
BCON_MAGIC, BCON_TYPE_DATE_TIME, BCON_ENSURE (int64, (_val))
#define BCON_NULL BCON_MAGIC, BCON_TYPE_NULL
#define BCON_REGEX(_regex, _flags) \
BCON_MAGIC, BCON_TYPE_REGEX, \
Expand Down Expand Up @@ -105,56 +111,56 @@ BCON_ENSURE_DECLARE (const_bson_iter_ptr, const bson_iter_t *)
BCON_MAGIC, BCON_TYPE_ITER, BCON_ENSURE (const_bson_iter_ptr, (_val))

#define BCONE_UTF8(_val) BCONE_MAGIC, BCON_TYPE_UTF8, \
BCON_ENSURE_STORAGE (const_char_ptr, (_val))
BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_val))
#define BCONE_DOUBLE(_val) BCONE_MAGIC, BCON_TYPE_DOUBLE, \
BCON_ENSURE_STORAGE (double, (_val))
BCON_ENSURE_STORAGE (double_ptr, (_val))
#define BCONE_DOCUMENT(_val) BCONE_MAGIC, BCON_TYPE_DOCUMENT, \
BCON_ENSURE_STORAGE (bson, (_val))
BCON_ENSURE_STORAGE (bson_ptr, (_val))
#define BCONE_ARRAY(_val) BCONE_MAGIC, BCON_TYPE_ARRAY, \
BCON_ENSURE_STORAGE (bson, (_val))
BCON_ENSURE_STORAGE (bson_ptr, (_val))
#define BCONE_BIN(subtype, binary, length) \
BCONE_MAGIC, BCON_TYPE_BIN, \
BCON_ENSURE_STORAGE (subtype, (subtype)), \
BCON_ENSURE_STORAGE (const_uint8_ptr, (binary)), \
BCON_ENSURE_STORAGE (uint32, (length))
BCON_ENSURE_STORAGE (subtype_ptr, (subtype)), \
BCON_ENSURE_STORAGE (const_uint8_ptr_ptr, (binary)), \
BCON_ENSURE_STORAGE (uint32_ptr, (length))
#define BCONE_UNDEFINED BCONE_MAGIC, BCON_TYPE_UNDEFINED
#define BCONE_OID(_val) BCONE_MAGIC, BCON_TYPE_OID, \
BCON_ENSURE_STORAGE (const_oid_ptr, (_val))
BCON_ENSURE_STORAGE (const_oid_ptr_ptr, (_val))
#define BCONE_BOOL(_val) BCONE_MAGIC, BCON_TYPE_BOOL, \
BCON_ENSURE_STORAGE (bool, (_val))
BCON_ENSURE_STORAGE (bool_ptr, (_val))
#define BCONE_DATE_TIME(_val) BCONE_MAGIC, BCON_TYPE_DATE_TIME, \
BCON_ENSURE_STORAGE (timeval, (_val))
BCON_ENSURE_STORAGE (int64_ptr, (_val))
#define BCONE_NULL BCONE_MAGIC, BCON_TYPE_NULL
#define BCONE_REGEX(_regex, _flags) \
BCONE_MAGIC, BCON_TYPE_REGEX, \
BCON_ENSURE_STORAGE (const_char_ptr, (_regex)), \
BCON_ENSURE_STORAGE (const_char_ptr, (_flags))
BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_regex)), \
BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_flags))
#define BCONE_DBPOINTER(_collection, _oid) \
BCONE_MAGIC, BCON_TYPE_DBPOINTER, \
BCON_ENSURE_STORAGE (const_char_ptr, (_collection)), \
BCON_ENSURE_STORAGE (const_oid_ptr, (_oid))
BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_collection)), \
BCON_ENSURE_STORAGE (const_oid_ptr_ptr, (_oid))
#define BCONE_CODE(_val) BCONE_MAGIC, BCON_TYPE_CODE, \
BCON_ENSURE_STORAGE (const_char_ptr, (_val))
BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_val))
#define BCONE_SYMBOL(_val) BCONE_MAGIC, BCON_TYPE_SYMBOL, \
BCON_ENSURE_STORAGE (const_char_ptr, (_val))
BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_val))
#define BCONE_CODEWSCOPE(_js, _scope) \
BCONE_MAGIC, BCON_TYPE_CODEWSCOPE, \
BCON_ENSURE_STORAGE (const_char_ptr, (_js)), \
BCON_ENSURE_STORAGE (bson, (_scope))
BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_js)), \
BCON_ENSURE_STORAGE (bson_ptr, (_scope))
#define BCONE_INT32(_val) BCONE_MAGIC, BCON_TYPE_INT32, \
BCON_ENSURE_STORAGE (int32, (_val))
BCON_ENSURE_STORAGE (int32_ptr, (_val))
#define BCONE_TIMESTAMP(_timestamp, _increment) \
BCONE_MAGIC, BCON_TYPE_TIMESTAMP, \
BCON_ENSURE_STORAGE (int32, (_timestamp)), \
BCON_ENSURE_STORAGE (int32, (_increment))
BCON_ENSURE_STORAGE (int32_ptr, (_timestamp)), \
BCON_ENSURE_STORAGE (int32_ptr, (_increment))
#define BCONE_INT64(_val) BCONE_MAGIC, BCON_TYPE_INT64, \
BCON_ENSURE_STORAGE (int64, (_val))
BCON_ENSURE_STORAGE (int64_ptr, (_val))
#define BCONE_MAXKEY BCONE_MAGIC, BCON_TYPE_MAXKEY
#define BCONE_MINKEY BCONE_MAGIC, BCON_TYPE_MINKEY
#define BCONE_SKIP(_val) BCONE_MAGIC, BCON_TYPE_SKIP, \
BCON_ENSURE (bson_type, (_val))
#define BCONE_ITER(_val) BCONE_MAGIC, BCON_TYPE_ITER, \
BCON_ENSURE_STORAGE (bson_iter, (_val))
BCON_ENSURE_STORAGE (bson_iter_ptr, (_val))

extern char *BCON_MAGIC;
extern char *BCONE_MAGIC;
Expand Down
52 changes: 49 additions & 3 deletions bson/bson-clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
#endif

#include <time.h>

#include "bson.h"
#include "bson-clock.h"


/**
* bson_get_monotonic_time:
*
Expand Down Expand Up @@ -59,8 +58,55 @@ bson_get_monotonic_time (void)
{
struct timeval tv;

gettimeofday (&tv, NULL);
bson_gettimeofday (&tv, NULL);
return (tv.tv_sec * 1000000UL) + tv.tv_usec;
}
#endif
}


/* The const value is shamelessy stolen from
* http://www.boost.org/doc/libs/1_55_0/boost/chrono/detail/inlined/win/chrono.hpp
*
* File times are the number of 100 nanosecond intervals elapsed since 12:00 am
* Jan 1, 1601 UTC. I haven't check the math particularly hard
*
* ... good luck
*/
int
bson_gettimeofday(struct timeval *tv, struct timezone *tz)
{
#ifdef BSON_OS_WIN32
#define DELTA_EPOCH_IN_MICROSEC 11644473600000000LL

FILETIME ft;
bson_uint64_t tmp = 0;

if (NULL != tv)
{
GetSystemTimeAsFileTime(&ft);

/* pull out of the filetime into a 64 bit uint */
tmp = ft.dwHighDateTime;
tmp <<= 32;
tmp |= ft.dwLowDateTime;

/* adjust to unix epoch */
tmp -= DELTA_EPOCH_IN_MICROSEC;

/* convert from 100's of nanosecs to microsecs */
tmp /= 10;

tv->tv_sec = (long)(tmp / 1000000UL);
tv->tv_usec = (long)(tmp % 1000000UL);
}

assert (NULL == tz);

return 0;
#else
return gettimeofday(tv, tz);
#endif
}


2 changes: 2 additions & 0 deletions bson/bson-clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ BSON_BEGIN_DECLS
bson_int64_t
bson_get_monotonic_time (void);

int
bson_gettimeofday(struct timeval *tv, struct timezone *tz);

BSON_END_DECLS
#endif /* BSON_CLOCK_H */
17 changes: 17 additions & 0 deletions bson/bson-compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2013 MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "bson-compat.h"
Loading

0 comments on commit 0d4903f

Please sign in to comment.