+static int rdbSaveType(FILE *fp, unsigned char type) {
+ if (fwrite(&type,1,1,fp) == 0) return -1;
+ return 0;
+}
+
+static int rdbSaveTime(FILE *fp, time_t t) {
+ int32_t t32 = (int32_t) t;
+ if (fwrite(&t32,4,1,fp) == 0) return -1;
+ return 0;
+}
+
+/* check rdbLoadLen() comments for more info */
+static int rdbSaveLen(FILE *fp, uint32_t len) {
+ unsigned char buf[2];
+
+ if (len < (1<<6)) {
+ /* Save a 6 bit len */
+ buf[0] = (len&0xFF)|(REDIS_RDB_6BITLEN<<6);
+ if (fwrite(buf,1,1,fp) == 0) return -1;
+ } else if (len < (1<<14)) {
+ /* Save a 14 bit len */
+ buf[0] = ((len>>8)&0xFF)|(REDIS_RDB_14BITLEN<<6);
+ buf[1] = len&0xFF;
+ if (fwrite(buf,2,1,fp) == 0) return -1;
+ } else {
+ /* Save a 32 bit len */
+ buf[0] = (REDIS_RDB_32BITLEN<<6);
+ if (fwrite(buf,1,1,fp) == 0) return -1;
+ len = htonl(len);
+ if (fwrite(&len,4,1,fp) == 0) return -1;
+ }
+ return 0;
+}
+
+/* String objects in the form "2391" "-100" without any space and with a
+ * range of values that can fit in an 8, 16 or 32 bit signed value can be
+ * encoded as integers to save space */
+int rdbTryIntegerEncoding(sds s, unsigned char *enc) {
+ long long value;
+ char *endptr, buf[32];
+
+ /* Check if it's possible to encode this value as a number */
+ value = strtoll(s, &endptr, 10);
+ if (endptr[0] != '\0') return 0;
+ snprintf(buf,32,"%lld",value);
+
+ /* If the number converted back into a string is not identical
+ * then it's not possible to encode the string as integer */
+ if (strlen(buf) != sdslen(s) || memcmp(buf,s,sdslen(s))) return 0;
+
+ /* Finally check if it fits in our ranges */
+ if (value >= -(1<<7) && value <= (1<<7)-1) {
+ enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT8;
+ enc[1] = value&0xFF;
+ return 2;
+ } else if (value >= -(1<<15) && value <= (1<<15)-1) {
+ enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT16;
+ enc[1] = value&0xFF;
+ enc[2] = (value>>8)&0xFF;
+ return 3;
+ } else if (value >= -((long long)1<<31) && value <= ((long long)1<<31)-1) {
+ enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT32;
+ enc[1] = value&0xFF;
+ enc[2] = (value>>8)&0xFF;
+ enc[3] = (value>>16)&0xFF;
+ enc[4] = (value>>24)&0xFF;
+ return 5;
+ } else {
+ return 0;
+ }
+}
+
+static int rdbSaveLzfStringObject(FILE *fp, robj *obj) {
+ unsigned int comprlen, outlen;
+ unsigned char byte;
+ void *out;
+
+ /* We require at least four bytes compression for this to be worth it */
+ outlen = sdslen(obj->ptr)-4;
+ if (outlen <= 0) return 0;
+ if ((out = zmalloc(outlen)) == NULL) return 0;
+ comprlen = lzf_compress(obj->ptr, sdslen(obj->ptr), out, outlen);
+ if (comprlen == 0) {
+ zfree(out);
+ return 0;
+ }
+ /* Data compressed! Let's save it on disk */
+ byte = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_LZF;
+ if (fwrite(&byte,1,1,fp) == 0) goto writeerr;
+ if (rdbSaveLen(fp,comprlen) == -1) goto writeerr;
+ if (rdbSaveLen(fp,sdslen(obj->ptr)) == -1) goto writeerr;
+ if (fwrite(out,comprlen,1,fp) == 0) goto writeerr;
+ zfree(out);
+ return comprlen;
+
+writeerr:
+ zfree(out);
+ return -1;
+}
+
+/* Save a string objet as [len][data] on disk. If the object is a string
+ * representation of an integer value we try to safe it in a special form */
+static int rdbSaveStringObject(FILE *fp, robj *obj) {
+ size_t len = sdslen(obj->ptr);
+ int enclen;
+
+ /* Try integer encoding */
+ if (len <= 11) {
+ unsigned char buf[5];
+ if ((enclen = rdbTryIntegerEncoding(obj->ptr,buf)) > 0) {
+ if (fwrite(buf,enclen,1,fp) == 0) return -1;
+ return 0;
+ }
+ }
+
+ /* Try LZF compression - under 20 bytes it's unable to compress even
+ * aaaaaaaaaaaaaaaaaa so skip it */
+ if (len > 20) {
+ int retval;
+
+ retval = rdbSaveLzfStringObject(fp,obj);
+ if (retval == -1) return -1;
+ if (retval > 0) return 0;
+ /* retval == 0 means data can't be compressed, save the old way */
+ }
+
+ /* Store verbatim */
+ if (rdbSaveLen(fp,len) == -1) return -1;
+ if (len && fwrite(obj->ptr,len,1,fp) == 0) return -1;
+ return 0;
+}
+