+ (*len) = 0;
+ while (n > 127)
+ {
+ buf[*len] = 128 + (n & 127);
+ n = n >> 7;
+ (*len)++;
+ }
+ buf[*len] = n;
+ (*len)++;
+}
+
+static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
+{
+ unsigned n = 0;
+ unsigned w = 1;
+ (*len) = 0;
+
+ while (buf[*len] > 127)
+ {
+ n += w*(buf[*len] & 127);
+ w = w << 7;
+ (*len)++;
+ }
+ n += w * buf[*len];
+ (*len)++;
+ *np = n;
+}
+
+static void rec_encode_zint(zint n, unsigned char *buf, int *len)
+{
+ (*len) = 0;
+ while (n > 127)
+ {
+ buf[*len] = (unsigned) (128 + (n & 127));
+ n = n >> 7;
+ (*len)++;
+ }
+ buf[*len] = (unsigned) n;
+ (*len)++;
+}
+
+static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
+{
+ zint n = 0;
+ zint w = 1;
+ (*len) = 0;
+
+ while (buf[*len] > 127)
+ {
+ n += w*(buf[*len] & 127);
+ w = w << 7;
+ (*len)++;
+ }
+ n += w * buf[*len];
+ (*len)++;
+ *np = n;
+}
+
+static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
+ char **out_buf, int *out_size,
+ int *out_offset)
+{
+ int i;
+ int len;
+
+ for (i = 0; i<REC_NO_INFO; i++)
+ {
+ if (*out_offset + CAST_ZINT_TO_INT(rec->size[i]) + 20 > *out_size)
+ {
+ int new_size = *out_offset + rec->size[i] + 65536;
+ char *np = (char *) xmalloc(new_size);
+ if (*out_offset)
+ memcpy(np, *out_buf, *out_offset);
+ xfree(*out_buf);
+ *out_size = new_size;
+ *out_buf = np;
+ }
+ if (i == 0)
+ {
+ rec_encode_zint(rec_sysno_to_int(rec->sysno),
+ (unsigned char *) *out_buf + *out_offset, &len);
+ (*out_offset) += len;
+ }
+ if (rec->size[i] == 0)
+ {
+ rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
+ &len);
+ (*out_offset) += len;
+ }
+ else if (last_rec && rec->size[i] == last_rec->size[i] &&
+ !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
+ {
+ rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
+ &len);
+ (*out_offset) += len;
+ }
+ else
+ {
+ rec_encode_unsigned(rec->size[i]+1,
+ (unsigned char *) *out_buf + *out_offset,
+ &len);
+ (*out_offset) += len;
+ memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
+ (*out_offset) += rec->size[i];
+ }
+ }
+}
+
+static ZEBRA_RES rec_write_multiple(Records p, int saveCount)
+{
+ int i;
+ short ref_count = 0;
+ char compression_method;
+ Record last_rec = 0;
+ int out_size = 1000;
+ int out_offset = 0;
+ char *out_buf = (char *) xmalloc(out_size);
+ SYSNO *sysnos = (SYSNO *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
+ SYSNO *sysnop = sysnos;
+ ZEBRA_RES ret = ZEBRA_OK;