diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000000000000000000000000000000000000..45b897e3ec13904950dc27184091cfba48153540_Q01ha2VMaXN0cy50eHQ=
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,16 @@
+# Minimal CMake project for building a static library under Windows.
+
+cmake_minimum_required (VERSION 2.8)
+project (yaml C)
+
+set (YAML_VERSION_MAJOR 0)
+set (YAML_VERSION_MINOR 1)
+set (YAML_VERSION_PATCH 6)
+set (YAML_VERSION_STRING "${YAML_VERSION_MAJOR}.${YAML_VERSION_MINOR}.${YAML_VERSION_PATCH}")
+
+file (GLOB SRC src/*.c)
+
+include_directories (include win32)
+add_definitions (-DHAVE_CONFIG_H -DYAML_DECLARE_STATIC)
+add_library (yaml STATIC ${SRC})
+
diff --git a/Makefile.am b/Makefile.am
index c61564cac13466b1f384cd98bf2942bbda82b663_TWFrZWZpbGUuYW0=..45b897e3ec13904950dc27184091cfba48153540_TWFrZWZpbGUuYW0= 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,7 +3,7 @@
 
 SUBDIRS = include src . tests win32
 
-EXTRA_DIST = README LICENSE doc/doxygen.cfg
+EXTRA_DIST = README LICENSE CMakeLists.txt doc/doxygen.cfg
 
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = yaml-0.1.pc
@@ -16,5 +16,3 @@
 bootstrap: maintainer-clean
 	./bootstrap
 
-test:
-	make -C tests check-TESTS
diff --git a/configure.ac b/configure.ac
index c61564cac13466b1f384cd98bf2942bbda82b663_Y29uZmlndXJlLmFj..45b897e3ec13904950dc27184091cfba48153540_Y29uZmlndXJlLmFj 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3,7 +3,7 @@
 # Define the package version numbers and the bug reporting link.
 m4_define([YAML_MAJOR], 0)
 m4_define([YAML_MINOR], 1)
-m4_define([YAML_PATCH], 4)
+m4_define([YAML_PATCH], 6)
 m4_define([YAML_BUGS], [http://pyyaml.org/newticket?component=libyaml])
 
 # Define the libtool version numbers; check the Autobook, Section 11.4.
@@ -19,7 +19,7 @@
 #           YAML_AGE = 0
 m4_define([YAML_RELEASE], 0)
 m4_define([YAML_CURRENT], 2)
-m4_define([YAML_REVISION], 2)
+m4_define([YAML_REVISION], 4)
 m4_define([YAML_AGE], 0)
 
 # Initialize autoconf & automake.
diff --git a/src/api.c b/src/api.c
index c61564cac13466b1f384cd98bf2942bbda82b663_c3JjL2FwaS5j..45b897e3ec13904950dc27184091cfba48153540_c3JjL2FwaS5j 100644
--- a/src/api.c
+++ b/src/api.c
@@ -117,12 +117,7 @@
 YAML_DECLARE(int)
 yaml_stack_extend(void **start, void **top, void **end)
 {
-    void *new_start;
-
-    if ((char *)*end - (char *)*start >= INT_MAX / 2)
-	return 0;
-
-    new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2);
+    void *new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2);
 
     if (!new_start) return 0;
 
diff --git a/src/loader.c b/src/loader.c
index c61564cac13466b1f384cd98bf2942bbda82b663_c3JjL2xvYWRlci5j..45b897e3ec13904950dc27184091cfba48153540_c3JjL2xvYWRlci5j 100644
--- a/src/loader.c
+++ b/src/loader.c
@@ -286,6 +286,8 @@
     int index;
     yaml_char_t *tag = first_event->data.scalar.tag;
 
+    if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) goto error;
+
     if (!tag || strcmp((char *)tag, "!") == 0) {
         yaml_free(tag);
         tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_SCALAR_TAG);
@@ -329,6 +331,8 @@
     int index, item_index;
     yaml_char_t *tag = first_event->data.sequence_start.tag;
 
+    if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) goto error;
+
     if (!tag || strcmp((char *)tag, "!") == 0) {
         yaml_free(tag);
         tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG);
@@ -351,6 +355,9 @@
     if (!yaml_parser_parse(parser, &event)) return 0;
 
     while (event.type != YAML_SEQUENCE_END_EVENT) {
+        if (!STACK_LIMIT(parser,
+                    parser->document->nodes.start[index-1].data.sequence.items,
+                    INT_MAX-1)) return 0;
         item_index = yaml_parser_load_node(parser, &event);
         if (!item_index) return 0;
         if (!PUSH(parser,
@@ -387,6 +394,8 @@
     yaml_node_pair_t pair;
     yaml_char_t *tag = first_event->data.mapping_start.tag;
 
+    if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) goto error;
+
     if (!tag || strcmp((char *)tag, "!") == 0) {
         yaml_free(tag);
         tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_MAPPING_TAG);
@@ -409,6 +418,9 @@
     if (!yaml_parser_parse(parser, &event)) return 0;
 
     while (event.type != YAML_MAPPING_END_EVENT) {
+        if (!STACK_LIMIT(parser,
+                    parser->document->nodes.start[index-1].data.mapping.pairs,
+                    INT_MAX-1)) return 0;
         pair.key = yaml_parser_load_node(parser, &event);
         if (!pair.key) return 0;
         if (!yaml_parser_parse(parser, &event)) return 0;
diff --git a/src/reader.c b/src/reader.c
index c61564cac13466b1f384cd98bf2942bbda82b663_c3JjL3JlYWRlci5j..45b897e3ec13904950dc27184091cfba48153540_c3JjL3JlYWRlci5j 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -460,6 +460,10 @@
 
     }
 
+    if (parser->offset >= PTRDIFF_MAX)
+        return yaml_parser_set_reader_error(parser, "input is too long",
+                PTRDIFF_MAX, -1);
+
     return 1;
 }
 
diff --git a/src/scanner.c b/src/scanner.c
index c61564cac13466b1f384cd98bf2942bbda82b663_c3JjL3NjYW5uZXIuYw==..45b897e3ec13904950dc27184091cfba48153540_c3JjL3NjYW5uZXIuYw== 100644
--- a/src/scanner.c
+++ b/src/scanner.c
@@ -615,7 +615,7 @@
  */
 
 static int
-yaml_parser_roll_indent(yaml_parser_t *parser, size_t column,
-        int number, yaml_token_type_t type, yaml_mark_t mark);
+yaml_parser_roll_indent(yaml_parser_t *parser, ptrdiff_t column,
+        ptrdiff_t number, yaml_token_type_t type, yaml_mark_t mark);
 
 static int
@@ -620,9 +620,6 @@
 
 static int
-yaml_parser_unroll_indent(yaml_parser_t *parser, size_t column);
-
-static int
-yaml_parser_reset_indent(yaml_parser_t *parser);
+yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column);
 
 /*
  * Token fetchers.
@@ -1106,7 +1103,7 @@
      */
 
     int required = (!parser->flow_level
-            && parser->indent == (int)parser->mark.column);
+            && parser->indent == (ptrdiff_t)parser->mark.column);
 
     /*
      * A simple key is required only when it is the first token in the current
@@ -1179,6 +1176,11 @@
 
     /* Increase the flow level. */
 
+    if (parser->flow_level == INT_MAX) {
+        parser->error = YAML_MEMORY_ERROR;
+        return 0;
+    }
+
     parser->flow_level++;
 
     return 1;
@@ -1209,8 +1211,8 @@
  */
 
 static int
-yaml_parser_roll_indent(yaml_parser_t *parser, size_t column,
-        int number, yaml_token_type_t type, yaml_mark_t mark)
+yaml_parser_roll_indent(yaml_parser_t *parser, ptrdiff_t column,
+        ptrdiff_t number, yaml_token_type_t type, yaml_mark_t mark)
 {
     yaml_token_t token;
 
@@ -1219,7 +1221,7 @@
     if (parser->flow_level)
         return 1;
 
-    if (parser->indent == -1 || parser->indent < column)
+    if (parser->indent < column)
     {
         /*
          * Push the current indentation level to the stack and set the new
@@ -1229,6 +1231,11 @@
         if (!PUSH(parser, parser->indents, parser->indent))
             return 0;
 
+        if (column > INT_MAX) {
+            parser->error = YAML_MEMORY_ERROR;
+            return 0;
+        }
+
         parser->indent = column;
 
         /* Create a token and insert it into the queue. */
@@ -1257,51 +1264,7 @@
 
 
 static int
-yaml_parser_unroll_indent(yaml_parser_t *parser, size_t column)
-{
-    yaml_token_t token;
-
-    /* In the flow context, do nothing. */
-
-    if (parser->flow_level)
-        return 1;
-
-    /*
-     * column is unsigned and parser->indent is signed, so if
-     * parser->indent is less than zero the conditional in the while
-     * loop below is incorrect.  Guard against that.
-     */
-    
-    if (parser->indent < 0)
-        return 1;
-
-    /* Loop through the intendation levels in the stack. */
-
-    while (parser->indent > column)
-    {
-        /* Create a token and append it to the queue. */
-
-        TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark);
-
-        if (!ENQUEUE(parser, parser->tokens, token))
-            return 0;
-
-        /* Pop the indentation level. */
-
-        parser->indent = POP(parser, parser->indents);
-    }
-
-    return 1;
-}
-
-/*
- * Pop indentation levels from the indents stack until the current
- * level resets to -1.  For each intendation level, append the
- * BLOCK-END token.
- */
-
-static int
-yaml_parser_reset_indent(yaml_parser_t *parser)
+yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column)
 {
     yaml_token_t token;
 
@@ -1312,7 +1275,7 @@
 
     /* Loop through the intendation levels in the stack. */
 
-    while (parser->indent > -1)
+    while (parser->indent > column)
     {
         /* Create a token and append it to the queue. */
 
@@ -1385,7 +1348,7 @@
 
     /* Reset the indentation level. */
 
-    if (!yaml_parser_reset_indent(parser))
+    if (!yaml_parser_unroll_indent(parser, -1))
         return 0;
 
     /* Reset simple keys. */
@@ -1416,7 +1379,7 @@
 
     /* Reset the indentation level. */
 
-    if (!yaml_parser_reset_indent(parser))
+    if (!yaml_parser_unroll_indent(parser, -1))
         return 0;
 
     /* Reset simple keys. */
@@ -1454,7 +1417,7 @@
 
     /* Reset the indentation level. */
 
-    if (!yaml_parser_reset_indent(parser))
+    if (!yaml_parser_unroll_indent(parser, -1))
         return 0;
 
     /* Reset simple keys. */
@@ -2666,6 +2629,9 @@
         /* Check if it is a URI-escape sequence. */
 
         if (CHECK(parser->buffer, '%')) {
+            if (!STRING_EXTEND(parser, string))
+                goto error;
+
             if (!yaml_parser_scan_uri_escapes(parser,
                         directive, start_mark, &string)) goto error;
         }
diff --git a/src/yaml_private.h b/src/yaml_private.h
index c61564cac13466b1f384cd98bf2942bbda82b663_c3JjL3lhbWxfcHJpdmF0ZS5o..45b897e3ec13904950dc27184091cfba48153540_c3JjL3lhbWxfcHJpdmF0ZS5o 100644
--- a/src/yaml_private.h
+++ b/src/yaml_private.h
@@ -7,6 +7,17 @@
 
 #include <assert.h>
 #include <limits.h>
+#include <stddef.h>
+
+#ifndef _MSC_VER
+#include <stdint.h>
+#else
+#ifdef _WIN64
+#define PTRDIFF_MAX _I64_MAX
+#else
+#define PTRDIFF_MAX INT_MAX
+#endif
+#endif
 
 /*
  * Memory management.
@@ -132,5 +143,5 @@
      (string).start = (string).pointer = (string).end = 0)
 
 #define STRING_EXTEND(context,string)                                           \
-    (((string).pointer+5 < (string).end)                                        \
+    ((((string).pointer+5 < (string).end)                                       \
         || yaml_string_extend(&(string).start,                                  \
@@ -136,5 +147,8 @@
         || yaml_string_extend(&(string).start,                                  \
-            &(string).pointer, &(string).end))
+            &(string).pointer, &(string).end)) ?                                \
+         1 :                                                                    \
+        ((context)->error = YAML_MEMORY_ERROR,                                  \
+         0))
 
 #define CLEAR(context,string)                                                   \
     ((string).pointer = (string).start,                                         \
@@ -421,6 +435,12 @@
 #define STACK_EMPTY(context,stack)                                              \
     ((stack).start == (stack).top)
 
+#define STACK_LIMIT(context,stack,size)                                         \
+    ((stack).top - (stack).start < (size) ?                                     \
+        1 :                                                                     \
+        ((context)->error = YAML_MEMORY_ERROR,                                  \
+         0))
+
 #define PUSH(context,stack,value)                                               \
     (((stack).top != (stack).end                                                \
       || yaml_stack_extend((void **)&(stack).start,                             \
diff --git a/win32/config.h b/win32/config.h
index c61564cac13466b1f384cd98bf2942bbda82b663_d2luMzIvY29uZmlnLmg=..45b897e3ec13904950dc27184091cfba48153540_d2luMzIvY29uZmlnLmg= 100644
--- a/win32/config.h
+++ b/win32/config.h
@@ -1,4 +1,4 @@
 #define YAML_VERSION_MAJOR 0
 #define YAML_VERSION_MINOR 1
-#define YAML_VERSION_PATCH 4
-#define YAML_VERSION_STRING "0.1.4"
+#define YAML_VERSION_PATCH 6
+#define YAML_VERSION_STRING "0.1.6"