fix Visual Studio projects
diff --git a/lib/bitstream.h b/lib/bitstream.h
index 20e40ec..e6c2228 100644
--- a/lib/bitstream.h
+++ b/lib/bitstream.h
@@ -150,7 +150,7 @@
 MEM_STATIC unsigned BIT_highbit32 (register U32 val)
 {
 #   if defined(_MSC_VER)   /* Visual */
-    unsigned long r;
+    unsigned long r=0;
     _BitScanReverse ( &r, val );
     return (unsigned) r;
 #   elif defined(__GNUC__) && (__GNUC__ >= 3)   /* Use GCC Intrinsic */
diff --git a/lib/error.h b/lib/error.h
index a6981dd..1e2e062 100644
--- a/lib/error.h
+++ b/lib/error.h
@@ -62,6 +62,7 @@
 
 #define ERROR_LIST(ITEM) \
         ITEM(PREFIX(No_Error)) ITEM(PREFIX(GENERIC)) \
+        ITEM(PREFIX(memory_allocation)) \
         ITEM(PREFIX(dstSize_tooSmall)) ITEM(PREFIX(srcSize_wrong)) \
         ITEM(PREFIX(prefix_unknown)) ITEM(PREFIX(corruption_detected)) \
         ITEM(PREFIX(tableLog_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooSmall)) \
diff --git a/lib/zstd.c b/lib/zstd.c
index 7e69169..43832f0 100644
--- a/lib/zstd.c
+++ b/lib/zstd.c
@@ -39,7 +39,7 @@
 *  Increasing memory usage improves compression ratio
 *  Reduced memory usage can improve speed, due to cache effect
 */
-#define ZSTD_MEMORY_USAGE 17
+#define ZSTD_MEMORY_USAGE 16
 
 /*!
  * HEAPMODE :
@@ -533,7 +533,7 @@
     U32 h = ZSTD_hashPtr(p);
     const BYTE* r;
     r = table[h] + start;
-    ZSTD_addPtr(table, p, start);
+    table[h] = (U32)(p-start);
     return r;
 }
 
@@ -559,23 +559,29 @@
 
 
     /* init */
+    if (ip-base < 4)
+    {
+        ZSTD_addPtr(HashTable, ip+0, base);
+        ZSTD_addPtr(HashTable, ip+1, base);
+        ZSTD_addPtr(HashTable, ip+2, base);
+        ZSTD_addPtr(HashTable, ip+3, base);
+        ip += 4;
+    }
     ZSTD_resetSeqStore(seqStorePtr);
 
     /* Main Search Loop */
-    while (ip < ilimit)
+    while (ip <= ilimit)
     {
-        const BYTE* match = (const BYTE*) ZSTD_updateMatch(HashTable, ip, base);
+        const BYTE* match = ZSTD_updateMatch(HashTable, ip, base);
 
-        if (!ZSTD_checkMatch(match,ip)) { ip += ((ip-anchor) >> g_searchStrength) + 1; continue; }
-        /* catch up */
-        while ((ip>anchor) && (match>base) && (ip[-1] == match[-1])) { ip--; match--; }
+        if (ZSTD_checkMatch(ip-offset_2,ip)) match = ip-offset_2;
+        if (!ZSTD_checkMatch(match,ip)) { ip += ((ip-anchor) >> g_searchStrength) + 1; offset_2 = offset_1; continue; }
+        while ((ip>anchor) && (match>base) && (ip[-1] == match[-1])) { ip--; match--; }  /* catch up */
 
         {
             size_t litLength = ip-anchor;
             size_t matchLength = ZSTD_count(ip+MINMATCH, match+MINMATCH, iend);
-            size_t offsetCode;
-            if (litLength) offset_2 = offset_1;
-            offsetCode = ip-match;
+            size_t offsetCode = ip-match;
             if (offsetCode == offset_2) offsetCode = 0;
             offset_2 = offset_1;
             offset_1 = ip-match;
@@ -584,8 +590,8 @@
             /* Fill Table */
             ZSTD_addPtr(HashTable, ip+1, base);
             ip += matchLength + MINMATCH;
-            if (ip<=iend-8) ZSTD_addPtr(HashTable, ip-2, base);
             anchor = ip;
+            if (ip <= ilimit) ZSTD_addPtr(HashTable, ip-2, base);
         }
     }
 
@@ -1089,7 +1095,7 @@
 
     /* Offset */
     {
-        static const size_t offsetPrefix[MaxOff+1] = {  /* note : size_t faster than U32 */
+        static const U32 offsetPrefix[MaxOff+1] = {
                 1 /*fake*/, 1, 2, 4, 8, 16, 32, 64, 128, 256,
                 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144,
                 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, /*fake*/ 1, 1, 1, 1, 1 };
diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h
index bc03534..09fa192 100644
--- a/lib/zstd_internal.h
+++ b/lib/zstd_internal.h
@@ -52,7 +52,7 @@
 static unsigned ZSTD_highbit(U32 val)
 {
 #   if defined(_MSC_VER)   /* Visual */
-    unsigned long r;
+    unsigned long r=0;
     _BitScanReverse(&r, val);
     return (unsigned)r;
 #   elif defined(__GNUC__) && (__GNUC__ >= 3)   /* GCC Intrinsic */
@@ -91,7 +91,7 @@
         else /* 32 bits */
         {
 #       if defined(_MSC_VER)
-            unsigned long r;
+            unsigned long r=0;
             _BitScanForward( &r, (U32)val );
             return (int)(r>>3);
 #       elif defined(__GNUC__) && (__GNUC__ >= 3)
diff --git a/lib/zstdhc.c b/lib/zstdhc.c
index e74ab25..9c70d79 100644
--- a/lib/zstdhc.c
+++ b/lib/zstdhc.c
@@ -89,8 +89,8 @@
     return 0;
 }
 
-static void ZSTD_HC_resetCCtx_advanced (ZSTD_HC_CCtx* zc,
-                                        ZSTD_HC_parameters params)
+static size_t ZSTD_HC_resetCCtx_advanced (ZSTD_HC_CCtx* zc,
+                                          ZSTD_HC_parameters params)
 {
     /* validate params */
     if (params.windowLog > ZSTD_HC_WINDOWLOG_MAX) params.windowLog = ZSTD_HC_WINDOWLOG_MAX;
@@ -111,6 +111,7 @@
             free(zc->workSpace);
             zc->workSpaceSize = neededSpace;
             zc->workSpace = malloc(neededSpace);
+            if (zc->workSpace == NULL) return ERROR(memory_allocation);
         }
         zc->hashTable = (U32*)zc->workSpace;
         zc->chainTable = zc->hashTable + (1 << params.hashLog);
@@ -132,6 +133,7 @@
     zc->seqStore.matchLengthStart = zc->seqStore.litLengthStart + (BLOCKSIZE>>2);
     zc->seqStore.dumpsStart = zc->seqStore.matchLengthStart + (BLOCKSIZE>>2);
 
+    return 0;
 }
 
 
@@ -144,7 +146,7 @@
 static U32 ZSTD_HC_hashPtr(const void* ptr, U32 h) { return ZSTD_HC_hash(MEM_read32(ptr), h); }
 
 //static const U64 prime5bytes =         889523592379ULL;
-//static U32   ZSTD_HC_hashPtr(const void* p, U32 h) { return ((MEM_read64(p) * prime5bytes) << (64-40)) >> (64-h); }
+//static U32   ZSTD_HC_hashPtr(const void* p, U32 h) { return (U32)((MEM_read64(p) * prime5bytes) << (64-40)) >> (64-h); }
 
 #define NEXT_IN_CHAIN(d)           chainTable[(d) & chainMask]   /* flexible, CHAINSIZE dependent */
 
@@ -258,8 +260,8 @@
     {
         /* repcode */
         if (MEM_read32(ip) == MEM_read32(ip - offset_2))
-        /* store sequence */
         {
+            /* store sequence */
             size_t matchLength = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend);
             size_t litLength = ip-anchor;
             size_t offset = offset_2;
@@ -271,12 +273,13 @@
             continue;
         }
 
+        offset_2 = offset_1;  /* failed once : necessarily offset_1 now */
+
         /* repcode at ip+1 */
         if (MEM_read32(ip+1) == MEM_read32(ip+1 - offset_1))
         {
             size_t matchLength = ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-offset_1, iend);
             size_t litLength = ip+1-anchor;
-            offset_2 = offset_1;
             ZSTD_storeSeq(seqStorePtr, litLength, anchor, 0, matchLength);
             ip += 1+matchLength+MINMATCH;
             anchor = ip;
@@ -287,11 +290,10 @@
         {
             const BYTE* match;
             size_t matchLength = ZSTD_HC_insertAndFindBestMatch(ctx, ip, iend, &match, maxSearches);
-            if (!matchLength) { ip++; offset_2 = offset_1; continue; }
+            if (!matchLength) { ip++; continue; }
             /* store sequence */
             {
                 size_t litLength = ip-anchor;
-                offset_2 = offset_1;
                 offset_1 = ip-match;
                 ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset_1, matchLength-MINMATCH);
                 ip += matchLength;
@@ -381,7 +383,7 @@
     if (ip != ctxPtr->end)
     {
         if (ctxPtr->end != NULL)
-            ZSTD_HC_resetCCtx_advanced(ctxPtr, ctxPtr->params);   /* reset */
+            ZSTD_HC_resetCCtx_advanced(ctxPtr, ctxPtr->params);   /* just reset, but no need to re-alloc */
         ctxPtr->base = ip;
     }
 
@@ -394,8 +396,10 @@
                                       void* dst, size_t maxDstSize,
                                       const ZSTD_HC_parameters params)
 {
+    size_t errorCode;
     if (maxDstSize < 4) return ERROR(dstSize_tooSmall);
-    ZSTD_HC_resetCCtx_advanced(ctx, params);
+    errorCode = ZSTD_HC_resetCCtx_advanced(ctx, params);
+    if (ZSTD_isError(errorCode)) return errorCode;
     MEM_writeLE32(dst, ZSTD_magicNumber); /* Write Header */
     return 4;
 }
@@ -432,6 +436,7 @@
 {
     BYTE* const ostart = (BYTE*)dst;
     BYTE* op = ostart;
+    size_t oSize;
 
     /* correct params, to use less memory */
     U32 srcLog = ZSTD_highbit((U32)srcSize-1) + 1;
@@ -439,7 +444,7 @@
     if (params.chainLog > srcLog) params.chainLog = srcLog;
 
     /* Header */
-    size_t oSize = ZSTD_HC_compressBegin_advanced(ctx, dst, maxDstSize, params);
+    oSize = ZSTD_HC_compressBegin_advanced(ctx, dst, maxDstSize, params);
     if(ZSTD_isError(oSize)) return oSize;
     op += oSize;
     maxDstSize -= oSize;
diff --git a/programs/bench.c b/programs/bench.c
index 3742655..5c410f3 100644
--- a/programs/bench.c
+++ b/programs/bench.c
@@ -419,7 +419,7 @@
     U64    inFileSize;
     size_t benchedSize, readSize;
     void* srcBuffer;
-    int result;
+    int result=0;
 
     /* Check file existence */
     inFile = fopen(inFileName, "rb");
@@ -476,7 +476,7 @@
 {
     size_t benchedSize = 10000000;
     void* srcBuffer = malloc(benchedSize);
-    int result;
+    int result=0;
     char name[20] = {0};
 
     /* Memory allocation */
diff --git a/programs/fileio.c b/programs/fileio.c
index 315a38c..8472033 100644
--- a/programs/fileio.c
+++ b/programs/fileio.c
@@ -520,7 +520,7 @@
         ZSTD_resetDCtx(dctx);
         toRead = ZSTD_nextSrcSizeToDecompress(dctx) - sizeof(ZSTD_magicNumber);
         if (toRead > MAXHEADERSIZE) EXM_THROW(30, "Not enough memory to read header");
-        sizeCheck = fread(header+sizeof(ZSTD_magicNumber), (size_t)1, toRead, finput);
+        sizeCheck = fread(&header[sizeof(ZSTD_magicNumber)], 1, toRead, finput);
         if (sizeCheck != toRead) EXM_THROW(31, "Read error : cannot read header");
         sizeCheck = ZSTD_decompressContinue(dctx, NULL, 0, header, sizeof(ZSTD_magicNumber)+toRead);   // Decode frame header
         if (ZSTD_isError(sizeCheck)) EXM_THROW(32, "Error decoding header");
diff --git a/visual/2012/fuzzer/fuzzer.vcxproj b/visual/2012/fuzzer/fuzzer.vcxproj
index 9cc2a2d..861012d 100644
--- a/visual/2012/fuzzer/fuzzer.vcxproj
+++ b/visual/2012/fuzzer/fuzzer.vcxproj
@@ -163,6 +163,7 @@
     <ClCompile Include="..\..\..\lib\huff0.c" />

     <ClCompile Include="..\..\..\lib\legacy\zstd_v01.c" />

     <ClCompile Include="..\..\..\lib\zstd.c" />

+    <ClCompile Include="..\..\..\lib\zstdhc.c" />

     <ClCompile Include="..\..\..\programs\datagen.c" />

     <ClCompile Include="..\..\..\programs\fuzzer.c" />

     <ClCompile Include="..\..\..\programs\xxhash.c" />

@@ -174,6 +175,9 @@
     <ClInclude Include="..\..\..\lib\huff0_static.h" />

     <ClInclude Include="..\..\..\lib\legacy\zstd_v01.h" />

     <ClInclude Include="..\..\..\lib\zstd.h" />

+    <ClInclude Include="..\..\..\lib\zstdhc.h" />

+    <ClInclude Include="..\..\..\lib\zstdhc_static.h" />

+    <ClInclude Include="..\..\..\lib\zstd_internal.h" />

     <ClInclude Include="..\..\..\lib\zstd_static.h" />

     <ClInclude Include="..\..\..\programs\datagen.h" />

     <ClInclude Include="..\..\..\programs\xxhash.h" />

diff --git a/visual/2012/fuzzer/fuzzer.vcxproj.filters b/visual/2012/fuzzer/fuzzer.vcxproj.filters
index 45e7a6d..bb412fa 100644
--- a/visual/2012/fuzzer/fuzzer.vcxproj.filters
+++ b/visual/2012/fuzzer/fuzzer.vcxproj.filters
@@ -36,6 +36,9 @@
     <ClCompile Include="..\..\..\lib\huff0.c">

       <Filter>Fichiers sources</Filter>

     </ClCompile>

+    <ClCompile Include="..\..\..\lib\zstdhc.c">

+      <Filter>Fichiers sources</Filter>

+    </ClCompile>

   </ItemGroup>

   <ItemGroup>

     <ClInclude Include="..\..\..\lib\fse.h">

@@ -65,5 +68,14 @@
     <ClInclude Include="..\..\..\lib\huff0_static.h">

       <Filter>Fichiers d%27en-tête</Filter>

     </ClInclude>

+    <ClInclude Include="..\..\..\lib\zstd_internal.h">

+      <Filter>Fichiers d%27en-tête</Filter>

+    </ClInclude>

+    <ClInclude Include="..\..\..\lib\zstdhc.h">

+      <Filter>Fichiers d%27en-tête</Filter>

+    </ClInclude>

+    <ClInclude Include="..\..\..\lib\zstdhc_static.h">

+      <Filter>Fichiers d%27en-tête</Filter>

+    </ClInclude>

   </ItemGroup>

 </Project>
\ No newline at end of file
diff --git a/visual/2012/zstd/zstd.vcxproj b/visual/2012/zstd/zstd.vcxproj
index 531c4e8..0a35dd9 100644
--- a/visual/2012/zstd/zstd.vcxproj
+++ b/visual/2012/zstd/zstd.vcxproj
@@ -23,6 +23,7 @@
     <ClCompile Include="..\..\..\lib\huff0.c" />

     <ClCompile Include="..\..\..\lib\legacy\zstd_v01.c" />

     <ClCompile Include="..\..\..\lib\zstd.c" />

+    <ClCompile Include="..\..\..\lib\zstdhc.c" />

     <ClCompile Include="..\..\..\programs\bench.c" />

     <ClCompile Include="..\..\..\programs\fileio.c" />

     <ClCompile Include="..\..\..\programs\xxhash.c" />

@@ -35,6 +36,9 @@
     <ClInclude Include="..\..\..\lib\huff0_static.h" />

     <ClInclude Include="..\..\..\lib\legacy\zstd_v01.h" />

     <ClInclude Include="..\..\..\lib\zstd.h" />

+    <ClInclude Include="..\..\..\lib\zstdhc.h" />

+    <ClInclude Include="..\..\..\lib\zstdhc_static.h" />

+    <ClInclude Include="..\..\..\lib\zstd_internal.h" />

     <ClInclude Include="..\..\..\lib\zstd_static.h" />

     <ClInclude Include="..\..\..\programs\bench.h" />

     <ClInclude Include="..\..\..\programs\fileio.h" />

diff --git a/visual/2012/zstd/zstd.vcxproj.filters b/visual/2012/zstd/zstd.vcxproj.filters
index cc5950b..a9b5393 100644
--- a/visual/2012/zstd/zstd.vcxproj.filters
+++ b/visual/2012/zstd/zstd.vcxproj.filters
@@ -39,6 +39,9 @@
     <ClCompile Include="..\..\..\lib\huff0.c">

       <Filter>Fichiers sources</Filter>

     </ClCompile>

+    <ClCompile Include="..\..\..\lib\zstdhc.c">

+      <Filter>Fichiers sources</Filter>

+    </ClCompile>

   </ItemGroup>

   <ItemGroup>

     <ClInclude Include="..\..\..\lib\fse.h">

@@ -71,5 +74,14 @@
     <ClInclude Include="..\..\..\lib\huff0_static.h">

       <Filter>Fichiers d%27en-tête</Filter>

     </ClInclude>

+    <ClInclude Include="..\..\..\lib\zstd_internal.h">

+      <Filter>Fichiers d%27en-tête</Filter>

+    </ClInclude>

+    <ClInclude Include="..\..\..\lib\zstdhc.h">

+      <Filter>Fichiers d%27en-tête</Filter>

+    </ClInclude>

+    <ClInclude Include="..\..\..\lib\zstdhc_static.h">

+      <Filter>Fichiers d%27en-tête</Filter>

+    </ClInclude>

   </ItemGroup>

 </Project>
\ No newline at end of file