Esempio n. 1
0
compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
{
  j_lossless_c_ptr losslsc = (j_lossless_c_ptr) cinfo->codec;
  c_diff_ptr diff = (c_diff_ptr) losslsc->diff_private;
  /* JDIMENSION MCU_col_num; */	/* index of current MCU within row */
  /* JDIMENSION MCU_count; */	/* number of MCUs encoded */
  int comp, ci /* , yoffset */ ;
  JSAMPARRAY buffer[MAX_COMPONENTS];
  jpeg_component_info *compptr;

  /* Align the virtual buffers for the components used in this scan.
   * NB: during first pass, this is safe only because the buffers will
   * already be aligned properly, so jmemmgr.c won't need to do any I/O.
   */
  for (comp = 0; comp < cinfo->comps_in_scan; comp++) {
    compptr = cinfo->cur_comp_info[comp];
    ci = compptr->component_index;
    buffer[ci] = (*cinfo->mem->access_virt_sarray)
      ((j_common_ptr) cinfo, diff->whole_image[ci],
       diff->iMCU_row_num * compptr->v_samp_factor,
       (JDIMENSION) compptr->v_samp_factor, FALSE);
  }

  return compress_data(cinfo, buffer);
}
/*
 * One 4-byte pointer per block and then the actual blocked
 * output. The first block does not need an offset pointer,
 * as it will start immediately after the pointer block;
 * so the i'th pointer points to the end of the i'th block
 * (i.e. the start of the (i+1)'th block or past EOF).
 *
 * Note that size > 0, as a zero-sized file wouldn't ever
 * have gotten here in the first place.
 */
static unsigned int do_compress(char *base, unsigned int offset, char const *name, char *uncompressed, unsigned int size)
{
	unsigned long original_size = size;
	unsigned long original_offset = offset;
	unsigned long new_size;
	unsigned long blocks = (size - 1) / blksize + 1;
	unsigned long curr = offset + 4 * blocks;
	int change;

	total_blocks += blocks;

	do {
		unsigned long len = 2 * blksize;
		unsigned int input = size;
		int err;

		if (input > blksize)
			input = blksize;
		size -= input;
		if (!(opt_holes && is_zero (uncompressed, input))) {
			err = compress_data(base + curr, &len, uncompressed,
				input, Z_BEST_COMPRESSION);
			if (err != Z_OK) {
				die(MKFS_ERROR, 0, "compression error: %s", zError(err));
			}
			curr += len;
		}
		uncompressed += input;

		if (len > blksize*2) {
			/* (I don't think this can happen with zlib.) */
			die(MKFS_ERROR, 0, "AIEEE: block \"compressed\" to > 2*blocklength (%ld)", len);
		}

		*(u32 *) (base + offset) = CRAMFS_32(curr);
		offset += 4;
	} while (size);

	curr = (curr + 3) & ~3;
	new_size = curr - original_offset;
	/* TODO: Arguably, original_size in these 2 lines should be
	   st_blocks * 512.  But if you say that then perhaps
	   administrative data should also be included in both. */
	change = new_size - original_size;
	if (opt_verbose > 1) {
		printf("%6.2f%% (%+d bytes)\t%s\n",
		       (change * 100) / (double) original_size, change, name);
	}

	return curr;
}
Esempio n. 3
0
int PT_find_exProb(PT_exProb *pep, int) {
    POS_TREE *pt = ptstruct.pt; // start search at root
    GBS_strstruct *gbs_str = GBS_stropen(pep->numget * (pep->plength + 1) + 1);
    bool first = true;

    for (int c = 0; c < pep->numget; ++c) {
        bool found = false;

        if (pep->restart) {
            pep->restart = 0;

            char *probe = (char*) malloc(pep->plength + 1);
            memset(probe, 'N', pep->plength);
            probe[pep->plength] = 0; // EOS marker

            compress_data(probe);

            pep->next_probe.data = probe;
            pep->next_probe.size = pep->plength + 1;

            found = findLeftmostProbe(pt, pep->next_probe.data, pep->plength,
                    0);

            assert(pep->next_probe.data[pep->plength] == 0);
            assert(strlen(pep->next_probe.data) == (size_t) pep->plength);
        }

        if (!found) {
            found = findNextProbe(pt, pep->next_probe.data, pep->plength, 0);

            assert(pep->next_probe.data[pep->plength] == 0);
            assert(strlen(pep->next_probe.data) == (size_t) pep->plength);
        }
        if (!found)
            break;

        // append the probe to the probe list

        if (!first)
            GBS_strcat(gbs_str, ";");
        first = false;
        GBS_strcat(gbs_str, pep->next_probe.data);
    }

    pep->result = GBS_strclose(gbs_str);

    return 0;
}
Esempio n. 4
0
	int write_tensor(datatensor& tensor, FILE* file){
		// write tensor's name
		uint8_t namelen = tensor.name ? strlen(tensor.name) : 0;
		fwrite(&namelen, sizeof(uint8_t), 1, file);
		if(tensor.name){
if(VERBOSE){ std::cout << "#    name : " << tensor.name << std::endl; }
			fwrite(tensor.name, sizeof(char), namelen, file);
		}
		// write the tensor's rank
if(VERBOSE){ std::cout << "#    rank : " << ((int)tensor.rank) << std::endl; }
		fwrite(&(tensor.rank), sizeof(uint8_t), 1, file);
		// write the length of each dimmension 
if(VERBOSE){ std::cout << "#    lengths : "; for(int i=0; i < tensor.rank; i++){ std::cout << tensor.lengths[i] << "   "; } std::cout << std::endl; }
		fwrite(tensor.lengths, sizeof(unsigned int), tensor.rank, file);
		// write the element type stuct
		if(!write_tensor_type(tensor.type, file)){
			return UNSUPPORTED_ELEMENT_TYPE;
		}
		// write the data (possibly compressed)
		{
			bool cdata_is_temp=false;
			unsigned int cdata_length=0;
			uint8_t* cdata=0;
			if(!tensor.data){
				return INVALID_STRUCT;
			}
			if(!compress_data(tensor, &cdata, &cdata_length, &cdata_is_temp)){
if(VERBOSE){ std::cout << "#    cdata size:  " << cdata_length << std::endl; }
				return UNSUPPORTED_COMPRESS_TYPE;
			}
			uint8_t ctype = (uint8_t)tensor.compression;
			fwrite(&ctype, sizeof(uint8_t), 1, file);
if(VERBOSE){ std::cout << "#    cdata size:  " << cdata_length << std::endl; }
			fwrite(&cdata_length, sizeof(unsigned int), 1, file);
if(VERBOSE){ std::cout << "#    cdata :  " << ((void*)cdata) << std::endl; }
			fwrite(cdata, cdata_length, 1, file);
			if(cdata_is_temp){
if(VERBOSE){ std::cout << "#    deleting temp cdata...  " << cdata << std::endl; }
				delete[] cdata;
			}
		}
		return SUCCESS;
	}
Esempio n. 5
0
static void send_trace_log (std::map<MonoMethod*, CallInfo> const &dict) {
    MemWriter sbuf(0x100000);
    MemWriter writer(0x100000);
    std::map<MonoMethod*, CallInfo>::const_iterator p;
    for (p = dict.begin (); p != dict.end (); p++) {
        MonoMethod *method = p->first;
        CallInfo call_info = p->second;
        char const *m = get_method_image_name (method);
        char *n = mono_method_full_name (method, 0);
        char str[256];
        sbuf.sprintf ("[%s] %s [%08X]|%d|%d\n", m, n, mono_method_get_token (method), call_info.times, call_info.order);
        g_free (n);
    }
    if (!compress_data (sbuf.getBuffPtr (), sbuf.getBuffSize (), &writer)) {
        LOGD ("compress_data err!");
        return;
    }
    /*Fixme : 这里写在使用protocolbuf之前, 因此非常不河蟹的出现了不使用pb的封包*/
    ecmd_send (XMONO_ID_TRACE_REPORT, writer.getBuffPtr (), writer.getBuffSize ());
    return;
}
Esempio n. 6
0
/*
 * Handle the data just read and send it to the SD after doing any postprocessing needed.
 */
static inline bool send_data_to_sd(b_ctx *bctx)
{
   BSOCK *sd = bctx->jcr->store_bsock;
   bool need_more_data;

   /*
    * Check for sparse blocks
    */
   if (bit_is_set(FO_SPARSE, bctx->ff_pkt->flags)) {
      bool allZeros;
      ser_declare;

      allZeros = false;
      if ((sd->msglen == bctx->rsize &&
          (bctx->fileAddr + sd->msglen < (uint64_t)bctx->ff_pkt->statp.st_size)) ||
          ((bctx->ff_pkt->type == FT_RAW ||
            bctx->ff_pkt->type == FT_FIFO) &&
          ((uint64_t)bctx->ff_pkt->statp.st_size == 0))) {
         allZeros = is_buf_zero(bctx->rbuf, bctx->rsize);
      }

      if (!allZeros) {
         /*
          * Put file address as first data in buffer
          */
         ser_begin(bctx->wbuf, OFFSET_FADDR_SIZE);
         ser_uint64(bctx->fileAddr); /* store fileAddr in begin of buffer */
      }

      bctx->fileAddr += sd->msglen; /* update file address */

      /*
       * Skip block of all zeros
       */
      if (allZeros) {
         return true;
      }
   } else if (bit_is_set(FO_OFFSETS, bctx->ff_pkt->flags)) {
      ser_declare;
      ser_begin(bctx->wbuf, OFFSET_FADDR_SIZE);
      ser_uint64(bctx->ff_pkt->bfd.offset); /* store offset in begin of buffer */
   }

   bctx->jcr->ReadBytes += sd->msglen; /* count bytes read */

   /*
    * Uncompressed cipher input length
    */
   bctx->cipher_input_len = sd->msglen;

   /*
    * Update checksum if requested
    */
   if (bctx->digest) {
      crypto_digest_update(bctx->digest, (uint8_t *)bctx->rbuf, sd->msglen);
   }

   /*
    * Update signing digest if requested
    */
   if (bctx->signing_digest) {
      crypto_digest_update(bctx->signing_digest, (uint8_t *)bctx->rbuf, sd->msglen);
   }

   /*
    * Compress the data.
    */
   if (bit_is_set(FO_COMPRESS, bctx->ff_pkt->flags)) {
      if (!compress_data(bctx->jcr, bctx->ff_pkt->Compress_algo, bctx->rbuf,
                         bctx->jcr->store_bsock->msglen, bctx->cbuf,
                         bctx->max_compress_len, &bctx->compress_len)) {
         return false;
      }

      /*
       * See if we need to generate a compression header.
       */
      if (bctx->chead) {
         ser_declare;

         /*
          * Complete header
          */
         ser_begin(bctx->chead, sizeof(comp_stream_header));
         ser_uint32(bctx->ch.magic);
         ser_uint32(bctx->compress_len);
         ser_uint16(bctx->ch.level);
         ser_uint16(bctx->ch.version);
         ser_end(bctx->chead, sizeof(comp_stream_header));

         bctx->compress_len += sizeof(comp_stream_header); /* add size of header */
      }

      bctx->jcr->store_bsock->msglen = bctx->compress_len; /* set compressed length */
      bctx->cipher_input_len = bctx->compress_len;
   }

   /*
    * Encrypt the data.
    */
   need_more_data = false;
   if (bit_is_set(FO_ENCRYPT, bctx->ff_pkt->flags) && !encrypt_data(bctx, &need_more_data)) {
      if (need_more_data) {
         return true;
      }
      return false;
   }

   /*
    * Send the buffer to the Storage daemon
    */
   if (bit_is_set(FO_SPARSE, bctx->ff_pkt->flags) || bit_is_set(FO_OFFSETS, bctx->ff_pkt->flags)) {
      sd->msglen += OFFSET_FADDR_SIZE; /* include fileAddr in size */
   }
   sd->msg = bctx->wbuf; /* set correct write buffer */

   if (!sd->send()) {
      if (!bctx->jcr->is_job_canceled()) {
         Jmsg1(bctx->jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"), sd->bstrerror());
      }
      return false;
   }

   Dmsg1(130, "Send data to SD len=%d\n", sd->msglen);
   bctx->jcr->JobBytes += sd->msglen; /* count bytes saved possibly compressed/encrypted */
   sd->msg = bctx->msgsave; /* restore read buffer */

   return true;
}
Esempio n. 7
0
int udpsuccess(rr *where, int id, int sock, struct sockaddr_in *client,
               js_string *query, void **rotate_point, int show_cname_a,
               int rd_val, conn *ect, int force_authoritative,int ra_value) {
    js_string *most = 0; /* Most of the data */
    js_string *ar = 0; /* Then the additional records */

    uint16 first_rr_type;
    int in_ns = 0;
    int ns_delegation = 0;
    int length_save;
    int len_inet = sizeof(struct sockaddr);
    rr *ipwhere = 0;
    /* The following are used for round robin rotation */
    rr *rotate_1st = 0, *rotate_2nd = 0, *rotate_last = 0;
    /* Counters in ensure that we don't give out more than the maximum
       number of A, AN (A), chain, or total records */
    int a_count = 0, an_count = 0;
    fila *zap_point;
    /* These two variables are added to handle PTR records */
    int seen_ptr_record = 0;
    rr *top = where;
    int is_auth = 0;
    int compress_error_happened = 0;

    q_header header;

    /* Initialize the total count */
    total_count = 0;
    /* Initialize the js_string objects */
    if((most = js_create(1024,1)) == 0)
        return JS_ERROR;
#ifdef AUTHONLY
    if((ar = js_create(5000,1)) == 0) {
        js_destroy(most);
        return JS_ERROR;
        }
#else
    if((ar = js_create(600,1)) == 0) {
        js_destroy(most);
        return JS_ERROR;
        }
#endif

    /* Make the header a placeholder for now */
    init_header(&header);
    header.id = id;
    if(make_hdr(&header,most) == JS_ERROR)
        goto giveerror;

    /* Sanity check */
    if(where == 0) {
        goto giveerror;
        }
    if(where->query == 0) {
        goto giveerror;
        }
    if(js_has_sanity(where->query) == JS_ERROR) {
        goto giveerror;
        }
    if(where->data == 0) {
        goto giveerror;
        }
    if(js_has_sanity(where->data) == JS_ERROR) {
        goto giveerror;
        }
    if(js_has_sanity(query) == JS_ERROR) {
        goto giveerror;
        }
    first_rr_type = get_rtype(query);

    /* Somewhat hacky way to determine that this is a NS delegation */
    ns_delegation = 0;
    if(first_rr_type != RR_NS && where->rr_type == RR_NS) {
        ns_delegation = 1;
        }

    /* With the cache, this may be a rtype of RR_ANY.  We need to handle
       this special case */

    /* We have to add this header here--authoritative depends on the
       authorative status of the first record we find */
    if(force_authoritative != 1) {
        header.aa = where->authoritative;
        }
    else {
        header.aa = 1;
        }
    is_auth = where->authoritative;

    /* The data must be between 0 and 65535 bytes in length (16-bit
       unsigned value) */
    if(where->data->unit_count < 0 || where->data->unit_count > 65535) {
        goto giveerror;
        }

    /* Append the question to the answer */
    if(js_append(query,most) == JS_ERROR) {
        goto giveerror;
        }

    /* Append the class (in) to the answer */
    if(js_adduint16(most,1) == JS_ERROR) {
        goto giveerror;
        }

    /* We will increment the ancount, nscount, an arcount, starting at 0 */
    header.ancount = 0;
    header.nscount = 0;
    header.arcount = 0;

    /* Initialize some temporary pointers used for round robin rotation */
    rotate_1st = where;
    rotate_2nd = where->next;
    /* We do not round robin if there is but a single record */
    if(rotate_2nd != 0 && first_rr_type != RR_NS &&
       rotate_2nd->rr_type == RR_NS)
        rotate_2nd = 0;

    /* OK, we now add the answers */
    while(where != 0) {
        /* Increment the number of answers -or- ns records */
        if(first_rr_type != RR_NS && where->rr_type == RR_NS && in_ns == 0) {
            /* Due to the data structure MaraDNS currently uses, the behavior
               is buggy if we round-robin rotate data when we allow more than
               one additional record to be create per answer/authoritative
               record.  */
            if(rotate_2nd != 0 && max_ar_chain == 1 && rotate_last != 0 &&
               first_rr_type != RR_NS) {
                /* If it makes sense to do a round-robin rotation, do so.
                 * Make rotate_1st, which was the first record, the last
                 * record; make rotate_2nd, which was the second record,
                 * the first record; and make rotate_last, which is the last
                 * record in the chain, have its next record be the first
                 * record */
                rotate_1st->next = where;
                rotate_last->next = rotate_1st;
                *rotate_point = rotate_2nd;
                rotate_2nd = 0; /* Make sure we can not rotate again */
                }
            in_ns = 1;
            a_count = 0; /* The NS chain is different than the AN
                            chain of answers: If we only allow eight
                            answers in a chain, we can still have 16
                            answers: 8 records in the answer section then
                            8 records in the authority section */
            }
        if(a_count < max_chain && total_count < max_total && (in_ns == 0
           || is_auth == 1 || ns_delegation == 1)) {
            a_count++;
            total_count++;
            if(!in_ns) {
                header.ancount++;
                }
            else {
                header.nscount++;
                }
            /* Append the name for this answer to the answer */
            if(js_append(where->query,most) == JS_ERROR)
                goto giveerror;
            /* Append the class (in) to the answer */
            if(js_adduint16(most,1) == JS_ERROR)
                goto giveerror;
            /* Append the ttl to the answer */
            if(js_adduint32(most,determine_ttl(where->expire,where->ttl))
               == JS_ERROR)
                goto giveerror;
            /* Add the rdlength to the answer */
            if(js_adduint16(most,where->data->unit_count) == JS_ERROR)
                goto giveerror;
            /* Add the record itself to the answer */
            if(js_append(where->data,most) == JS_ERROR)
                goto giveerror;
            /* If there is an IP, and this is *not* a CNAME record,
               append the IP of the answer to the AR section */
            if(where->ip != 0 && where->rr_type != RR_CNAME) {
                /* Reset the number of an records we have seen */
                an_count = 0;
                ipwhere = where->ip;
                while(ipwhere != 0 && ipwhere->rr_type != RR_NS) {
                    /* We only show a given additional record once */
                    if(ipwhere->seen == 1) { /* If we have displayed this RR
                                                already */
                        /* Go to the next link in the linked list */
                        ipwhere = ipwhere->next;
                        continue;
                        }
                    /* Stop showing records if we have exceeded our limit */
                    if(an_count >= max_ar_chain || total_count >= max_total)
                        break;
                    an_count++;
                    total_count++;
                    /* Increment the number of additional records */
                    header.arcount++;
                    /* Append the name for this answer to the ip */
                    if(js_append(ipwhere->query,ar) == JS_ERROR)
                        goto giveerror;
                    /* Append the class (in) to the ip */
                    if(js_adduint16(ar,1) == JS_ERROR)
                        goto giveerror;
                    /* Append the TTL to the ip */
                    if(js_adduint32(ar,
                      determine_ttl(ipwhere->expire,ipwhere->ttl)) == JS_ERROR)
                        goto giveerror;
                    /* Add the rdlength to the ip */
                    if(js_adduint16(ar,ipwhere->data->unit_count) == JS_ERROR)
                        goto giveerror;
                    /* Add the record itself to the ip */
                    if(js_append(ipwhere->data,ar) == JS_ERROR)
                        goto giveerror;
                    /* Mark that we have seen this record already */
                    if(seenlist_where < 250) {
                        ipwhere->seen = 1;
                        seenlist[seenlist_where] = ipwhere;
                        seenlist_where++;
                        }
                    /* Go to the next link in the linked list */
                    ipwhere = ipwhere->next;
                    }
                }
#ifdef IPV6
              if(where->ip6 != 0 && where->rr_type != RR_CNAME) {
                /* Reset the number of an records we have seen */
                an_count = 0;
                ipwhere = where->ip6;
                while(ipwhere != 0 && ipwhere->rr_type != RR_NS) {
                    /* We only show a given additional record once */
                    if(ipwhere->seen == 1) { /* If we have displayed this RR
                                                already */
                        /* Go to the next link in the linked list */
                        ipwhere = ipwhere->next;
                        continue;
                        }
                    /* Stop showing records if we have exceeded our limit */
                    if(an_count >= max_ar_chain || total_count >= max_total)
                        break;
                    an_count++;
                    total_count++;
                    /* Increment the number of additional records */
                    header.arcount++;
                    /* Append the name for this answer to the ip */
                    if(js_append(ipwhere->query,ar) == JS_ERROR)
                        goto giveerror;
                    /* Append the class (in) to the ip */
                    if(js_adduint16(ar,1) == JS_ERROR)
                        goto giveerror;
                    /* Append the TTL to the ip */
                    if(js_adduint32(ar,
                      determine_ttl(ipwhere->expire,ipwhere->ttl)) == JS_ERROR)
                        goto giveerror;
                    /* Add the rdlength to the ip */
                    if(js_adduint16(ar,ipwhere->data->unit_count) == JS_ERROR)
                        goto giveerror;
                    /* Add the record itself to the ip */
                    if(js_append(ipwhere->data,ar) == JS_ERROR)
                        goto giveerror;
                    /* Mark that we have seen this record already */
                    if(seenlist_where < 250) {
                        ipwhere->seen = 1;
                        seenlist[seenlist_where] = ipwhere;
                        seenlist_where++;
                        }
                    /* Go to the next link in the linked list */
                    ipwhere = ipwhere->next;
                    }
                }
#endif
            /* This code is only used by the recursive code; the
             * authoritative code now uses where->list to attach
             * a CNAME record to its corresponding rddata
             * If there is an IP, and this is a CNAME record, and
             * show_cname_a is set to one (argument to this function)
             * append the IP in question to the answer section */
            if(where->ip != 0 && where->rr_type == RR_CNAME
               && show_cname_a == RR_A && where->list == 0) {
                /* Reset the number of an records we have seen */
                an_count = 0;
                ipwhere = where->ip;
                while(ipwhere != 0 && ipwhere->rr_type != RR_NS) {
                    /* We only show a given additional record once */
                    if(ipwhere->seen == 1) { /* If we have displayed this RR
                                                already */
                        /* Go to the next link in the linked list */
                        ipwhere = ipwhere->next;
                        continue;
                        }
                    /* If the IP in question is 255.255.255.255, we do
                       not show the data in question */
                    if(ipwhere->rr_type == RR_A &&
                       ipwhere->data->unit_count == 4 &&
                       *(ipwhere->data->string) == 0xff &&
                       *(ipwhere->data->string + 1) == 0xff &&
                       *(ipwhere->data->string + 2) == 0xff &&
                       *(ipwhere->data->string + 3) == 0xff) {
                        ipwhere = ipwhere->next;
                        continue;
                        }
                    /* Stop showing records if we have exceeded our limit */
                    if(an_count >= max_ar_chain || total_count >= max_total)
                        break;
                    an_count++;
                    total_count++;
                    /* Increment the number of answer records */
                    header.ancount++;
                    /* Append the name for this answer to the ip */
                    if(js_append(ipwhere->query,most) == JS_ERROR)
                        goto giveerror;
                    /* Append the class (in) to the ip */
                    if(js_adduint16(most,1) == JS_ERROR)
                        goto giveerror;
                    /* Append the TTL to the ip */
                    if(js_adduint32(most,
                      determine_ttl(ipwhere->expire,ipwhere->ttl)) == JS_ERROR)
                        goto giveerror;
                    /* Add the rdlength to the ip */
                    if(js_adduint16(most,ipwhere->data->unit_count)
                       == JS_ERROR)
                        goto giveerror;
                    /* Add the record itself to the ip */
                    if(js_append(ipwhere->data,most) == JS_ERROR)
                        goto giveerror;
                    /* Mark that we have seen this record already */
                    if(seenlist_where < 250) {
                        ipwhere->seen = 1;
                        seenlist[seenlist_where] = ipwhere;
                        seenlist_where++;
                        }
                    /* Go to the next link in the linked list */
                    ipwhere = ipwhere->next;
                    }
                }
            /* Again, this code is only used by the recursive half.
             * The authoritative half now has a way of showing the record
             * that correspods to any RR type attached to a CNAME record.
             *
             * If there is an PTR, and this is a CNAME record, and
             * show_cname_a is set to one (argument to this function)
             * append the IP in question to the answer section */
            else if(top->ptr != 0 && top->rr_type == RR_CNAME
               && show_cname_a == RR_PTR && seen_ptr_record == 0
               && where->list == 0) {
                    /* Mark that we have seen this record already */
                    seen_ptr_record = 1;
                    /* Increment the total number of answers seen */
                    total_count++;
                    /* Increment the number of answer records */
                    header.ancount++;
                    /* Append the name for this answer to the ip */
                    if(js_append(top->data,most) == JS_ERROR)
                        goto giveerror;
                    /* Append the type for this query */
                    if(js_adduint16(most,RR_PTR) == JS_ERROR)
                        goto giveerror;
                    /* Append the class (in) to the ip */
                    if(js_adduint16(most,1) == JS_ERROR)
                        goto giveerror;
                    /* Append the TTL to the ip */
                    if(js_adduint32(most,
                      determine_ttl(top->expire,top->ttl)) == JS_ERROR)
                        goto giveerror;
                    /* Add the rdlength to the ip */
                    if(js_adduint16(most,top->ptr->unit_count)
                       == JS_ERROR)
                        goto giveerror;
                    /* Add the record itself to the ip */
                    if(js_append(top->ptr,most) == JS_ERROR)
                        goto giveerror;
               }
            /* This code is currently only used by the authoritative half;
             * bascially, if we see a CNAME record we see if we have a list
             * which lists all record types for the host name the CNAME
             * is pointing to.  We used this list to add, in the answer
             * section, the answer the person is looking for. */
            else if(where->rr_type == RR_CNAME && where->list != 0) {
               struct rr_list *any_list = 0;
               uint16 this_rr_type = 0;
               int counter = 0;
               /* Start at the top of list of RRs */
               any_list = where->list;
               this_rr_type = first_rr_type; /* The rtype they want */
               /* We go down the list until we find the desired rr_type */
               for(counter = 0; counter < 1000; counter++) {
                   if(any_list->rr_type == this_rr_type) {
                       break;
                       }
                   if(any_list->next == 0) {
                       break;
                       }
                   any_list = any_list->next;
                   }
               /* If we found the desired record type... */
               if(any_list->rr_type == this_rr_type && any_list->data != 0) {
                   rr *answer;
                   int counter = 0;
                   answer = any_list->data;
                   while(answer != 0 && answer->rr_type == this_rr_type &&
                         counter < 100) {
                       /* Then we add this information to the AN section of
                        * the answer */
                       /* Increase the number of answers in the header */
                       header.ancount++;
                       /* RFC 1035 section 3.2.1 header */
                       /* Name + Type */
                       if(js_append(answer->query,most) == JS_ERROR)
                           goto giveerror;
                       /* Class */
                       if(js_adduint16(most,1) == JS_ERROR)
                           goto giveerror;
                       /* TTL */
                       /* Since this currently only returns authoritative
                        * data, answer->expire should always be zero and
                        * determine_ttl is a redundant call.  However, we'll
                        * keep it that way just in case things change in
                        * the future */
                       if(js_adduint32(most,determine_ttl(answer->expire,
                              answer->ttl)) == JS_ERROR)
                           goto giveerror;
                       /* Rdlength */
                       if(answer->data == 0)
                           goto giveerror;
                       if(answer->data->unit_count < 0 ||
                          answer->data->unit_count > 65535)
                           goto giveerror;
                       if(js_adduint16(most,answer->data->unit_count) ==
                           JS_ERROR)
                           goto giveerror;
                       /* rdata */
                       if(js_append(answer->data,most) == JS_ERROR)
                           goto giveerror;
                       counter++;
                       answer = answer->next;
                       }
                   }
               }
            }
        /* Go on to the next record in the linked list */
        rotate_last = where;
        where = where->next;
        /* If it makes sense to do a round-robin rotation, do so */
        if(where == 0 && rotate_2nd != 0 && max_ar_chain == 1 &&
           first_rr_type != RR_NS) {
            /* For records in the cache, we need to make sure that
               the custodian properly points to the first record
               in the chain or we will leak memory */
            if(rotate_1st->zap != 0) {
                zap_point = rotate_1st->zap;
                rotate_1st->zap = 0;
                rotate_2nd->zap = zap_point;
                zap_point->record = rotate_2nd;
                }
            rotate_1st->next = 0;
            rotate_last->next = rotate_1st;
            *rotate_point = rotate_2nd;
            rotate_2nd = 0; /* Make sure we can not rotate again */
            }
        }

    /* Customize the header */
    /* header.id already set */
    header.qr = 1;
    header.opcode = 0;
    header.tc = 0;
    header.rd = rd_val; /* RDBUG udpsuccess */
    header.ra = calc_ra_value(ra_value);
    header.z = 0;
    header.rcode = 0; /* No error */
    header.qdcount = 1;

    /* OBhack: Tack on the header at the beginning without molesting the
       rest of the string */
    length_save = most->unit_count;
    make_hdr(&header,most);
    most->unit_count = length_save;

    /* Add the ar records to the end */
    if(js_append(ar,most) == JS_ERROR) {
        goto giveerror;
        }

    compress_error_happened = 0;
    if(compress_data(most,ar) == JS_ERROR) {
        compress_error_happened = 1;
    }

    /* Check to make sure the data fits in under 512 bytes (4096 bytes
     * if it's a long_packet_ipv4 address) truncate if not */
    if(ar->unit_count > 512 || compress_error_happened == 1) {
        int x;
#ifdef AUTHONLY

        /* If this is an ipv4 connection and we didn't get a compress error */
        if(ect->type == 4 && compress_error_happened == 0) {
            struct sockaddr_in *dq;
            uint32 ip_test;
            dq = (struct sockaddr_in *)(ect->d);
            ip_test = ntohl(dq->sin_addr.s_addr);
            /* See if we are allowed to send a long packet up to
             * 4096 bytes to this ip address */
            if(check_ipv4_acl(ip_test,long_packet) == 1) {
                if(ar->unit_count < 4096) {
                    goto long_packet_ok;
                    }
                }
            }
#endif

        for(x = 0; x < 20; x++) {
                compress_error_happened = 0;
                /* OK, try to squeeze the packet in by removing records */
                if(squeeze_to_fit(most) == 0) {
                        goto giveerror;
                }
                if(most->unit_count > 12) {
                        if(compress_data(most,ar) == JS_ERROR) {
                                compress_error_happened = 1;
                        }
                        if(ar->unit_count <= 512 &&
                            compress_error_happened == 0) {
                                break;
                        }
                } else if(most->unit_count == 12) {
                        if(js_copy(most,ar) == JS_ERROR) {
                                goto giveerror;
                        }
                } else {
                        goto giveerror;
                }
            }
        }

#ifdef AUTHONLY
long_packet_ok:
#endif

    if(compress_error_happened == 1) {
        goto giveerror;
    }

    /* Success! Put out the good data */
    if(ect == 0) {
        sendto(sock,ar->string,ar->unit_count,0,
            (struct sockaddr *)client,len_inet);
    } else {
        mara_send(ect,sock,ar);
    }

    js_destroy(most);
    js_destroy(ar);

    /* Clean up the seenlist_where list */
    while(seenlist_where > 0) {
        seenlist_where--;
        if(seenlist[seenlist_where] != 0)
            (seenlist[seenlist_where])->seen = 0;
        }

    return JS_SUCCESS;

    /* We use gotos to make up for C's lack of error trapping */
    giveerror:
        js_destroy(ar);
        udperror(sock,most,client,0,SERVER_FAIL,"giveerror in udpsuccess",2,
                        rd_val,ect,1);
        js_destroy(most);

        /* Clean up the seenlist_where list */
        while(seenlist_where > 0) {
            seenlist_where--;
            if(seenlist[seenlist_where] != 0)
                (seenlist[seenlist_where])->seen = 0;
            }
        return JS_ERROR;

    }
Esempio n. 8
0
int main(int argc,char *argv[])
{
  FILE *fpout;
  char *tag;
  int   c;
  int   level;
  
  fpout = stdout;
  tag   = "data";
  level = 0;
  
  while((c = getopt(argc,argv,"o:t:hz0123456789")) != EOF)
  {
    switch(c)
    {
      default:
      case 'h': 
           fprintf(stderr,"usage: %s [-o output] [-t tag] [-z] [-h] files... \n",argv[0]);
           return EXIT_FAILURE;
      case 't':
           tag = optarg;
           for ( ; *optarg ; optarg++)
             if (*optarg == '-') 
               *optarg = '_';
           break;
           
      case 'o':
           fpout = fopen(optarg,"wb");
           if (fpout == NULL)
           {
             perror(optarg);
             exit(EXIT_FAILURE);
           }
           break;
      case 'z':
           level = Z_DEFAULT_COMPRESSION;
           break;
           
      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
           level = c - '0';
           break;
    }
  }
  
  if (optind == argc)
  {
    fprintf(stderr,"usage: %s [-o output] [-t tag] [-z] [-h] files... \n",argv[0]);
    return EXIT_FAILURE;
  }
  else
  {
    for (int i = optind ; i < argc ; i++)
    {
      unsigned char *data;
      size_t         datasize;
      size_t         realdatasize;
      int            rc;
      
      rc = load_file(argv[i],&data,&realdatasize);
      if (rc != 0)
      {
        fprintf(stderr,"%s: %s\n",argv[i],strerror(rc));
        free(data);
        return EXIT_FAILURE;
      }
      
      rc = compress_data(level,&data,realdatasize,&datasize);
      if (rc != 0)
      {
        fprintf(stderr,"%s: %s\n",argv[1],strerror(rc));
        free(data);
        return EXIT_FAILURE;
      }

      process(fpout,data,datasize,realdatasize,tag);
      free(data);
    }
  }

  return EXIT_SUCCESS;
}
Esempio n. 9
0
/*
 * Perform automatic compression of certain stream types when enabled in the config.
 */
static bool auto_deflate_record(DCR *dcr)
{
   ser_declare;
   comp_stream_header ch;
   DEV_RECORD *rec, *nrec;
   bool retval = false;
   bool intermediate_value = false;
   unsigned int max_compression_length = 0;
   unsigned char *data = NULL;

   /*
    * See what our starting point is. When dcr->after_rec is set we already have
    * a translated record by an other SD plugin. Then we use that translated record
    * as the starting point otherwise we start at dcr->before_rec. When an earlier
    * translation already happened we can free that record when we have a success
    * full translation here as that record is of no use anymore.
    */
   if (dcr->after_rec) {
      rec = dcr->after_rec;
      intermediate_value = true;
   } else {
      rec = dcr->before_rec;
   }

   /*
    * We only do autocompression for the following stream types:
    *
    * - STREAM_FILE_DATA
    * - STREAM_WIN32_DATA
    * - STREAM_SPARSE_DATA
    */
   switch (rec->maskedStream) {
   case STREAM_FILE_DATA:
   case STREAM_WIN32_DATA:
   case STREAM_SPARSE_DATA:
      break;
   default:
      goto bail_out;
   }

   /*
    * Clone the data from the original DEV_RECORD to the converted one.
    * As we use the compression buffers for the data we need a new
    * DEV_RECORD without a new memory buffer so we call new_record here
    * with the with_data boolean set explicitly to false.
    */
   nrec = bfuncs->new_record(false);
   bfuncs->copy_record_state(nrec, rec);

   /*
    * Setup the converted DEV_RECORD to point with its data buffer to the compression buffer.
    */
   nrec->data = dcr->jcr->compress.deflate_buffer;
   switch (rec->maskedStream) {
   case STREAM_FILE_DATA:
   case STREAM_WIN32_DATA:
      data = (unsigned char *)nrec->data + sizeof(comp_stream_header);
      max_compression_length = dcr->jcr->compress.deflate_buffer_size - sizeof(comp_stream_header);
      break;
   case STREAM_SPARSE_DATA:
      data = (unsigned char *)nrec->data + OFFSET_FADDR_SIZE + sizeof(comp_stream_header);
      max_compression_length = dcr->jcr->compress.deflate_buffer_size - OFFSET_FADDR_SIZE - sizeof(comp_stream_header);
      break;
   }

   /*
    * Compress the data using the configured compression algorithm.
    */
   if (!compress_data(dcr->jcr, dcr->device->autodeflate_algorithm, rec->data, rec->data_len,
                      data, max_compression_length, &nrec->data_len)) {
      bfuncs->free_record(nrec);
      goto bail_out;
   }

   /*
    * Map the streams.
    */
   switch (rec->maskedStream) {
   case STREAM_FILE_DATA:
      nrec->Stream = STREAM_COMPRESSED_DATA;
      nrec->maskedStream = STREAM_COMPRESSED_DATA;
      break;
   case STREAM_WIN32_DATA:
      nrec->Stream = STREAM_WIN32_COMPRESSED_DATA;
      nrec->maskedStream = STREAM_WIN32_COMPRESSED_DATA;
      break;
   case STREAM_SPARSE_DATA:
      nrec->Stream = STREAM_SPARSE_COMPRESSED_DATA;
      nrec->maskedStream = STREAM_SPARSE_COMPRESSED_DATA;
      break;
   default:
      break;
   }

   /*
    * Generate a compression header.
    */
   ch.magic = dcr->device->autodeflate_algorithm;
   ch.level = dcr->device->autodeflate_level;
   ch.version = COMP_HEAD_VERSION;
   ch.size = nrec->data_len;

   switch (nrec->maskedStream) {
   case STREAM_COMPRESSED_DATA:
   case STREAM_WIN32_COMPRESSED_DATA:
      ser_begin(nrec->data, sizeof(comp_stream_header));
      ser_uint32(ch.magic);
      ser_uint32(ch.size);
      ser_uint16(ch.level);
      ser_uint16(ch.version);
      ser_end(nrec->data, sizeof(comp_stream_header));
      nrec->data_len += sizeof(comp_stream_header);
      break;
   case STREAM_SPARSE_COMPRESSED_DATA:
      /*
       * Copy the sparse offset from the original.
       */
      memcpy(nrec->data, rec->data, OFFSET_FADDR_SIZE);
      ser_begin(nrec->data + OFFSET_FADDR_SIZE, sizeof(comp_stream_header));
      ser_uint32(ch.magic);
      ser_uint32(ch.size);
      ser_uint16(ch.level);
      ser_uint16(ch.version);
      ser_end(nrec->data + OFFSET_FADDR_SIZE, sizeof(comp_stream_header));
      nrec->data_len += OFFSET_FADDR_SIZE + sizeof(comp_stream_header);
      break;
   }

   Dmsg4(400, "auto_deflate_record: From datastream %d to %d from original size %ld to %ld\n",
         rec->maskedStream, nrec->maskedStream, rec->data_len, nrec->data_len);

   /*
    * If the input is just an intermediate value free it now.
    */
   if (intermediate_value) {
      bfuncs->free_record(dcr->after_rec);
   }
   dcr->after_rec = nrec;
   retval = true;

bail_out:
   return retval;
}