fix #1241
Ensure that first input position is valid for a match
even during first usage of context
by starting reference at 1
(avoiding the problematic 0).
It appears that since commit 9d65a5c, compressing the same input data twice in a row while using the same compression context - even if the context is reset between compressions - results in different outputs when using certain compression levels. We were relying on the guarantees that @Cyan4973 described in #999 and assuming that zstd would output binary identical compressed bitstreams in this scenario.

Are we misunderstanding `ZSTD_compressCCtx` and thinking that it wouldn't reuse _any_ state in between invocations when that's not guaranteed?

### Steps to repro

We're only able to repro this easily on macOS (10.13), but when we discovered the problem the data had been compressed by Windows and Linux versions of zstd, so the problem doesn't appear to be platform-specific.

Save the following code to a file called `test.c`:

```c
#define ZSTD_STATIC_LINKING_ONLY
#include <zstd.h>
#include <stdio.h>
#include <string.h>

unsigned char data[] = 
{
  0x74, 0x75, 0x72, 0x70, 0x69, 0x73, 0x20, 0x65, 0x67, 0x65, 0x73, 0x74,
  0x61, 0x73, 0x20, 0x70, 0x6f, 0x72, 0x74, 0x74, 0x69, 0x74, 0x6f, 0x72,
  0x20, 0x71, 0x75, 0x69, 0x73, 0x20, 0x74, 0x69, 0x6e, 0x63, 0x69, 0x64,
  0x75, 0x6e, 0x74, 0x20, 0x6c, 0x65, 0x6f, 0x2e, 0x20, 0x44, 0x6f, 0x6e,
  0x65, 0x63, 0x20, 0x6c, 0x75, 0x63, 0x74, 0x75, 0x73, 0x20, 0x65, 0x67,
  0x65, 0x74, 0x20, 0x73, 0x61, 0x70, 0x69, 0x65, 0x6e, 0x20, 0x66, 0x72,
  0x69, 0x6e, 0x67, 0x69, 0x6c, 0x6c, 0x61, 0x20, 0x73, 0x65, 0x6d, 0x70,
  0x65, 0x72, 0x2e, 0x20, 0x46, 0x75, 0x73, 0x63, 0x65, 0x20, 0x66, 0x72,
  0x69, 0x6e, 0x67, 0x69, 0x6c, 0x6c, 0x61, 0x20, 0x6c, 0x69, 0x62, 0x65,
  0x72, 0x6f, 0x20, 0x71, 0x75, 0x69, 0x73, 0x20, 0x76, 0x65, 0x6e, 0x65,
  0x6e, 0x61, 0x74, 0x69, 0x73, 0x20, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x72,
  0x61, 0x74, 0x2e, 0x20, 0x53, 0x65, 0x64, 0x20, 0x65, 0x6c, 0x65, 0x69,
  0x66, 0x65, 0x6e, 0x64, 0x20, 0x75, 0x6c, 0x74, 0x72, 0x69, 0x63, 0x65,
  0x73, 0x20, 0x6c, 0x61, 0x63, 0x75, 0x73, 0x2c, 0x20, 0x71, 0x75, 0x69,
  0x73, 0x20, 0x66, 0x65, 0x72, 0x6d, 0x65, 0x6e, 0x74, 0x75, 0x6d, 0x20,
  0x74, 0x75, 0x72, 0x70, 0x69, 0x73, 0x20, 0x62, 0x6c, 0x61, 0x6e, 0x64,
  0x69, 0x74, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74, 0x2e,
  0x20, 0x43, 0x75, 0x72, 0x61, 0x62, 0x69, 0x74, 0x75, 0x72, 0x20, 0x67,
  0x72, 0x61, 0x76, 0x69, 0x64, 0x61, 0x20, 0x74, 0x65, 0x6c, 0x6c, 0x75,
  0x73, 0x20, 0x76, 0x65, 0x6c, 0x69, 0x74, 0x2e, 0x20, 0x41, 0x6c, 0x69,
  0x71, 0x75, 0x61, 0x6d, 0x20, 0x65, 0x72, 0x61, 0x74, 0x20, 0x76, 0x6f,
  0x6c, 0x75, 0x74, 0x70, 0x61, 0x74, 0x2e, 0x20, 0x53, 0x75, 0x73, 0x70,
  0x65, 0x6e, 0x64, 0x69, 0x73, 0x73, 0x65, 0x20, 0x76, 0x65, 0x6c, 0x20,
  0x6d, 0x6f, 0x6c, 0x65, 0x73, 0x74, 0x69, 0x65, 0x20, 0x6d, 0x69, 0x2e,
  0x0a, 0x0a, 0x50, 0x65, 0x6c, 0x6c, 0x65, 0x6e, 0x74, 0x65, 0x73, 0x71,
  0x75, 0x65, 0x20, 0x68, 0x61, 0x62, 0x69, 0x74, 0x61, 0x6e, 0x74, 0x20,
  0x6d, 0x6f, 0x72, 0x62, 0x69, 0x20, 0x74, 0x72, 0x69, 0x73, 0x74, 0x69,
  0x71, 0x75, 0x65, 0x20, 0x73, 0x65, 0x6e, 0x65, 0x63, 0x74, 0x75, 0x73,
  0x20, 0x65, 0x74, 0x20, 0x6e, 0x65, 0x74, 0x75, 0x73, 0x20, 0x65, 0x74,
  0x20, 0x6d, 0x61, 0x6c, 0x65, 0x73, 0x75, 0x61, 0x64, 0x61, 0x20, 0x66,
  0x61, 0x6d, 0x65, 0x73, 0x20, 0x61, 0x63, 0x20, 0x74, 0x75, 0x72, 0x70,
  0x69, 0x73, 0x20, 0x65, 0x67, 0x65, 0x73, 0x74, 0x61, 0x73, 0x2e, 0x20,
  0x51, 0x75, 0x69, 0x73, 0x71, 0x75, 0x65, 0x20, 0x76, 0x69, 0x76, 0x65,
  0x72, 0x72, 0x61, 0x20, 0x76, 0x65, 0x6c, 0x20, 0x6a, 0x75, 0x73, 0x74,
  0x6f, 0x20, 0x61, 0x63, 0x20, 0x61, 0x75, 0x63, 0x74, 0x6f, 0x72, 0x2e,
  0x20, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x64, 0x75, 0x6d, 0x20, 0x65, 0x74,
  0x20, 0x6d, 0x61, 0x6c, 0x65, 0x73, 0x75, 0x61, 0x64, 0x61, 0x20, 0x66,
  0x61, 0x6d, 0x65, 0x73, 0x20, 0x61, 0x63, 0x20, 0x61, 0x6e, 0x74, 0x65,
  0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x70, 0x72, 0x69, 0x6d, 0x69,
  0x73, 0x20, 0x69, 0x6e, 0x20, 0x66, 0x61, 0x75, 0x63, 0x69, 0x62, 0x75,
  0x73, 0x2e, 0x20, 0x50, 0x65, 0x6c, 0x6c, 0x65, 0x6e, 0x74, 0x65, 0x73,
  0x71, 0x75, 0x65, 0x20, 0x6e, 0x6f, 0x6e, 0x20, 0x61, 0x63, 0x63, 0x75,
  0x6d, 0x73, 0x61, 0x6e, 0x20, 0x6e, 0x69, 0x73, 0x69, 0x2e, 0x20, 0x49,
  0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61,
  0x6d, 0x65, 0x74, 0x20, 0x6d, 0x69, 0x20, 0x65, 0x72, 0x6f, 0x73, 0x2e,
  0x20, 0x56, 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6c, 0x75, 0x6d, 0x20,
};

int main(int argc, char** argv)
{
    for (int level = 1; level <= 19; ++level)
    {
        char buffer[1024];        
        ZSTD_CCtx* zstd = ZSTD_createCCtx();

        size_t size1 = ZSTD_compressCCtx(zstd, buffer, sizeof(buffer), data, sizeof(data), level);
        ZSTD_CCtx_reset(zstd);
        size_t size2 = ZSTD_compressCCtx(zstd, buffer, sizeof(buffer), data, sizeof(data), level);

        printf("Level %d: %zu bytes / %zu bytes%s\n", level, size1, size2, size1 != size2 ? " (*)" : "");
        ZSTD_freeCCtx(zstd);
    }
}
```

Now build against zstd v1.3.5:

```
$ git clone https://github.com/facebook/zstd.git
$ cd zstd
$ git checkout v1.3.5
$ make
$ cc -o test -Ilib test.c lib/libzstd.a
```

The following output will be seen:

```
Level 1: 323 bytes / 323 bytes
Level 2: 324 bytes / 324 bytes
Level 3: 325 bytes / 325 bytes
Level 4: 324 bytes / 324 bytes
Level 5: 324 bytes / 324 bytes
Level 6: 322 bytes / 322 bytes
Level 7: 322 bytes / 322 bytes
Level 8: 322 bytes / 322 bytes
Level 9: 324 bytes / 322 bytes (*)
Level 10: 324 bytes / 322 bytes (*)
Level 11: 322 bytes / 321 bytes (*)
Level 12: 322 bytes / 321 bytes (*)
Level 13: 322 bytes / 321 bytes (*)
Level 14: 322 bytes / 321 bytes (*)
Level 15: 322 bytes / 321 bytes (*)
Level 16: 322 bytes / 321 bytes (*)
Level 17: 322 bytes / 321 bytes (*)
Level 18: 322 bytes / 321 bytes (*)
Level 19: 322 bytes / 320 bytes (*)
```

As you can see, every level from 9 onwards results in different compressed output the second time.

This didn't happen back in v1.3.3:

```
Level 1: 322 bytes / 322 bytes
Level 2: 325 bytes / 325 bytes
Level 3: 325 bytes / 325 bytes
Level 4: 324 bytes / 324 bytes
Level 5: 324 bytes / 324 bytes
Level 6: 322 bytes / 322 bytes
Level 7: 322 bytes / 322 bytes
Level 8: 322 bytes / 322 bytes
Level 9: 322 bytes / 322 bytes
Level 10: 325 bytes / 325 bytes
Level 11: 322 bytes / 322 bytes
Level 12: 322 bytes / 322 bytes
Level 13: 322 bytes / 322 bytes
Level 14: 322 bytes / 322 bytes
Level 15: 322 bytes / 322 bytes
Level 16: 322 bytes / 322 bytes
Level 17: 322 bytes / 322 bytes
Level 18: 322 bytes / 322 bytes
Level 19: 322 bytes / 322 bytes
```

It started happening to some extent with commit 9d65a5c:

```
Level 1: 322 bytes / 322 bytes
Level 2: 325 bytes / 325 bytes
Level 3: 325 bytes / 325 bytes
Level 4: 324 bytes / 324 bytes
Level 5: 324 bytes / 324 bytes
Level 6: 322 bytes / 322 bytes
Level 7: 322 bytes / 322 bytes
Level 8: 322 bytes / 322 bytes
Level 9: 324 bytes / 322 bytes (*)
Level 10: 325 bytes / 325 bytes
Level 11: 322 bytes / 322 bytes
Level 12: 322 bytes / 322 bytes
Level 13: 322 bytes / 322 bytes
Level 14: 322 bytes / 322 bytes
Level 15: 322 bytes / 322 bytes
Level 16: 322 bytes / 322 bytes
Level 17: 322 bytes / 322 bytes
Level 18: 322 bytes / 322 bytes
Level 19: 322 bytes / 322 bytes
```

### Workaround

For now, we've switched to using `ZSTD_compress`, which does result in deterministic outputs in this scenario.
Nondeterministic compression with `ZSTD_compressCCtx`
diff --git a/appveyor.yml b/appveyor.yml
index 742f612069d..2b674ce3ca1 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -181,15 +181,15 @@
     - COMPILER: "gcc"
       HOST:     "mingw"
       PLATFORM: "x64"
-      SCRIPT:   "make allzstd"
+      SCRIPT:   "CPPFLAGS=-DDEBUGLEVEL=2 CFLAGS=-Werror make -j allzstd DEBUGLEVEL=2"
     - COMPILER: "gcc"
       HOST:     "mingw"
       PLATFORM: "x86"
-      SCRIPT:   "make allzstd"
+      SCRIPT:   "CFLAGS=-Werror make -j allzstd"
     - COMPILER: "clang"
       HOST:     "mingw"
       PLATFORM: "x64"
-      SCRIPT:   "MOREFLAGS='--target=x86_64-w64-mingw32 -Werror -Wconversion -Wno-sign-conversion' make allzstd"
+      SCRIPT:   "CFLAGS='--target=x86_64-w64-mingw32 -Werror -Wconversion -Wno-sign-conversion' make -j allzstd"
 
     - COMPILER: "visual"
       HOST:     "visual"
diff --git a/build/cmake/lib/CMakeLists.txt b/build/cmake/lib/CMakeLists.txt
index c4c2f81e6b9..e84e0630173 100644
--- a/build/cmake/lib/CMakeLists.txt
+++ b/build/cmake/lib/CMakeLists.txt
@@ -14,7 +14,7 @@ OPTION(ZSTD_BUILD_STATIC "BUILD STATIC LIBRARIES" ON)
 OPTION(ZSTD_BUILD_SHARED "BUILD SHARED LIBRARIES" ON)
 
 IF(NOT ZSTD_BUILD_SHARED AND NOT ZSTD_BUILD_STATIC)
-    MESSAGE(SEND_ERROR "You need to build at least one flavor of libstd")
+    MESSAGE(SEND_ERROR "You need to build at least one flavor of libzstd")
 ENDIF()
 
 # Define library directory, where sources and header files are located
diff --git a/lib/Makefile b/lib/Makefile
index 9cedd53b721..01689c6d533 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -19,6 +19,9 @@ LIBVER := $(shell echo $(LIBVER_SCRIPT))
 VERSION?= $(LIBVER)
 
 CPPFLAGS+= -I. -I./common -DXXH_NAMESPACE=ZSTD_
+ifeq ($(OS),Windows_NT)   # MinGW assumed
+CPPFLAGS   += -D__USE_MINGW_ANSI_STDIO   # compatibility with %zu formatting
+endif
 CFLAGS  ?= -O3
 DEBUGFLAGS = -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
             -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
@@ -52,11 +55,11 @@ ifeq ($(ZSTD_LIB_DECOMPRESSION), 0)
 endif
 
 ifneq ($(ZSTD_LIB_COMPRESSION), 0)
-	ZSTD_FILES += $(ZSTDCOMP_FILES) 
+	ZSTD_FILES += $(ZSTDCOMP_FILES)
 endif
 
 ifneq ($(ZSTD_LIB_DECOMPRESSION), 0)
-	ZSTD_FILES += $(ZSTDDECOMP_FILES) 
+	ZSTD_FILES += $(ZSTDDECOMP_FILES)
 endif
 
 ifneq ($(ZSTD_LIB_DEPRECATED), 0)
diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c
index d659baf1245..ed3aab871b9 100644
--- a/lib/compress/zstd_compress.c
+++ b/lib/compress/zstd_compress.c
@@ -1032,6 +1032,9 @@ ZSTD_reset_matchState(ZSTD_matchState_t* ms,
 
     ms->hashLog3 = hashLog3;
     memset(&ms->window, 0, sizeof(ms->window));
+    ms->window.dictLimit = 1;    /* start from 1, so that 1st position is valid */
+    ms->window.lowLimit = 1;     /* it ensures first and later CCtx usages compress the same */
+    ms->window.nextSrc = ms->window.base + 1;   /* see issue #1241 */
     ZSTD_invalidateMatchState(ms);
 
     /* opt parser space */
@@ -1281,8 +1284,9 @@ static size_t ZSTD_resetCCtx_usingCDict(ZSTD_CCtx* cctx,
     }
 
     if (attachDict) {
-        const U32 cdictLen = (U32)( cdict->matchState.window.nextSrc
+        const U32 cdictEnd = (U32)( cdict->matchState.window.nextSrc
                                   - cdict->matchState.window.base);
+        const U32 cdictLen = cdictEnd - cdict->matchState.window.dictLimit;
         if (cdictLen == 0) {
             /* don't even attach dictionaries with no contents */
             DEBUGLOG(4, "skipping attaching empty dictionary");
@@ -1292,9 +1296,9 @@ static size_t ZSTD_resetCCtx_usingCDict(ZSTD_CCtx* cctx,
 
             /* prep working match state so dict matches never have negative indices
              * when they are translated to the working context's index space. */
-            if (cctx->blockState.matchState.window.dictLimit < cdictLen) {
+            if (cctx->blockState.matchState.window.dictLimit < cdictEnd) {
                 cctx->blockState.matchState.window.nextSrc =
-                    cctx->blockState.matchState.window.base + cdictLen;
+                    cctx->blockState.matchState.window.base + cdictEnd;
                 ZSTD_window_clear(&cctx->blockState.matchState.window);
             }
             cctx->blockState.matchState.loadedDictEnd = cctx->blockState.matchState.window.dictLimit;
diff --git a/programs/Makefile b/programs/Makefile
index 4202764c2e0..912f9eff06e 100644
--- a/programs/Makefile
+++ b/programs/Makefile
@@ -38,6 +38,9 @@ endif
 CPPFLAGS+= -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(ZSTDDIR)/compress \
            -I$(ZSTDDIR)/dictBuilder \
            -DXXH_NAMESPACE=ZSTD_
+ifeq ($(OS),Windows_NT)   # MinGW assumed
+CPPFLAGS   += -D__USE_MINGW_ANSI_STDIO   # compatibility with %zu formatting
+endif
 CFLAGS  ?= -O3
 DEBUGFLAGS+=-Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
             -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
@@ -158,7 +161,7 @@ zstd-release: DEBUGFLAGS :=
 zstd-release: zstd
 
 zstd32 : CPPFLAGS += $(THREAD_CPP)
-zstd32 : LDFLAGS += $(THREAD_LD) 
+zstd32 : LDFLAGS += $(THREAD_LD)
 zstd32 : CPPFLAGS += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT)
 zstd32 : $(ZSTDLIB_FILES) zstdcli.c fileio.c bench.c datagen.c dibio.c
 ifneq (,$(filter Windows%,$(OS)))

diff --git a/tests/Makefile b/tests/Makefile
index 813380cc2e1..81e6857802d 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -27,6 +27,9 @@ DEBUGLEVEL ?= 1
 DEBUGFLAGS  = -g -DDEBUGLEVEL=$(DEBUGLEVEL)
 CPPFLAGS   += -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(ZSTDDIR)/compress \
               -I$(ZSTDDIR)/dictBuilder -I$(ZSTDDIR)/deprecated -I$(PRGDIR)
+ifeq ($(OS),Windows_NT)   # MinGW assumed
+CPPFLAGS   += -D__USE_MINGW_ANSI_STDIO   # compatibility with %zu formatting
+endif
 CFLAGS     ?= -O3
 CFLAGS     += -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow                 \
               -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
diff --git a/tests/fuzzer.c b/tests/fuzzer.c
index 8856a504a22..6d57afa1621 100644
--- a/tests/fuzzer.c
+++ b/tests/fuzzer.c
@@ -412,6 +412,26 @@ static int basicUnitTests(U32 seed, double compressibility)
     }
     DISPLAYLEVEL(3, "OK \n");
 
+    DISPLAYLEVEL(3, "test%3d : re-using a CCtx should compress the same : ", testNb++);
+    {   int i;
+        for (i=0; i<20; i++)
+            ((char*)CNBuffer)[i] = (char)i;   /* ensure no match during initial section */
+        memcpy((char*)CNBuffer + 20, CNBuffer, 10);   /* create one match, starting from beginning of sample, which is the difficult case (see #1241) */
+        for (i=1; i<=19; i++) {
+            ZSTD_CCtx* const cctx = ZSTD_createCCtx();
+            size_t size1, size2;
+            DISPLAYLEVEL(5, "l%i ", i);
+            size1 = ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, CNBuffer, 30, i);
+            CHECK_Z(size1);
+            size2 = ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, CNBuffer, 30, i);
+            CHECK_Z(size2);
+            CHECK_EQ(size1, size2);
+
+            ZSTD_freeCCtx(cctx);
+        }
+    }
+    DISPLAYLEVEL(3, "OK \n");
+
     DISPLAYLEVEL(3, "test%3d : ZSTD_CCtx_getParameter() : ", testNb++);
     {   ZSTD_CCtx* const cctx = ZSTD_createCCtx();
         ZSTD_outBuffer out = {NULL, 0, 0};

{"all tests": {"run": "PASS", "test": "FAIL", "fix": "PASS"}}
{"all tests": {"run": "PASS", "test": "FAIL", "fix": "PASS"}}
{"passed_count": 1, "failed_count": 0, "skipped_count": 0, "passed_tests": ["all tests"], "failed_tests": [], "skipped_tests": []}
{"passed_count": 0, "failed_count": 1, "skipped_count": 0, "passed_tests": [], "failed_tests": ["all tests"], "skipped_tests": []}
{"passed_count": 1, "failed_count": 0, "skipped_count": 0, "passed_tests": ["all tests"], "failed_tests": [], "skipped_tests": []}