Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

#### 83 lines 2.2 KiB Raw Blame History

 `#include "parse.h"` `static const size_t max_ssize_t = (((size_t)1) << (sizeof(size_t)*8-1))-1;` ``` ``` `/* Read n bytes from stream. Return n.` ` * Set stream to error state if not enough space or I/O error. */` `ssize_t prs_readblob(struct bytestream* bs,unsigned char* dest,size_t destlen) {` ` ssize_t r;` ` if (destlen > max_ssize_t)` ` destlen = max_ssize_t;` ` if (!bs_capacityassert(bs,destlen)) return -1;` ` r = destlen;` ``` ``` ` switch (bs->type) {` ``` ``` ` case MEMBUF:` ` memcpy(dest, bs->u.base+bs->cur, destlen);` ` bs->cur += destlen;` ` break;` ``` ``` ` case IOBUF:` ` buffer_get(bs->u.b, (char*)dest, destlen);` ` break;` ``` ``` ` case BSTREAM:` ` bs->cur += destlen;` ` r=prs_readblob(bs->u.bs, dest, destlen);` ` if (r != (ssize_t)destlen) {` ` bs->cur = 1; // set bytestream to error state` ` bs->max = 0;` ` return -1;` ` }` ` break;` ``` ``` ` }` ``` ``` ` return r;` `}` ``` ``` `#ifdef UNITTEST` `#include ` `#undef UNITTEST` `#include "buffer/bs_get.c"` `#include "buffer/bs_err.c"` `#include "buffer/bs_capacityassert.c"` `#include "buffer/bs_init_iobuf_size.c"` `#include "buffer/bs_init_membuf.c"` `#include "buffer/buffer_get.c"` `#include "buffer/buffer_getc.c"` `#include "buffer/buffer_feed.c"` `#include "buffer/buffer_stubborn2.c"` `#include "byte/byte_copy.c"` `#include "open/open_read.c"` `int main() {` ` struct bytestream bs = BS_FROM_MEMBUF("\x34\x12", 2);` ` char buf;` ` assert(prs_readblob(&bs, buf, 2) == 2);` ` assert(bs_err(&bs) == 0);` ` assert(!memcmp(buf,"\x34\x12",2));` ``` ``` ` assert(prs_readblob(&bs, buf, 1) == -1);` ` assert(bs_err(&bs));` ``` ``` ` // now try to provoke integer overflow` ` bs_init_membuf(&bs, "\x34\x12", 2);` ` assert(bs_get(&bs) == 0x34);` ` assert(prs_readblob(&bs, buf, (size_t)-1) == -1);` ` // a bad implementation would add 0xffffffff to the 1 we already read and wrap to 0` ` // our code makes sure not to wrap and also limits the length of the` ` // blob to max_ssize_t (0x7fffffff).` ` assert(bs_err(&bs));` ``` ``` ` int fd = open_read("Makefile");` ` assert(fd!=-1);` ` buffer b = BUFFER_INIT(read, fd, buf+32, 10);` ` bs_init_iobuf_size(&bs, &b, 100);` ` assert(prs_readblob(&bs, buf, 20) == 20); // make sure we can read a 20 byte blob at once even if the iobuf buffer size is smaller` ` // strace will show two read calls here` `}` ```#endif ``` ``` ```
{"ft_lang_label":"__label__en","ft_lang_prob":0.50733596,"math_prob":0.7142984,"size":2509,"snap":"2021-43-2021-49","text_gpt3_token_len":825,"char_repetition_ratio":0.1756487,"word_repetition_ratio":0.010309278,"special_character_ratio":0.382224,"punctuation_ratio":0.21108742,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97545177,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-11-27T02:05:39Z\",\"WARC-Record-ID\":\"<urn:uuid:f296d5b7-5a29-4f4c-bd14-bcad8a775adf>\",\"Content-Length\":\"62793\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ce576bf2-be52-46e9-baa8-3897195dbf8f>\",\"WARC-Concurrent-To\":\"<urn:uuid:0d5f572d-e1cc-4c32-9230-7c7fcf9bd6d4>\",\"WARC-IP-Address\":\"5.9.70.195\",\"WARC-Target-URI\":\"https://git.lighttpd.net/mirrors/libowfat/src/commit/5ce2f8743c034a73e79d061e680a3533fd847fbb/buffer/prs_readblob.c\",\"WARC-Payload-Digest\":\"sha1:EZDMDFCUQS25NOPRIXVIDOO5DNP7HRHS\",\"WARC-Block-Digest\":\"sha1:QB2TL53DN6TG3HJ5RODBXTOLT6H4CCZF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964358078.2_warc_CC-MAIN-20211127013935-20211127043935-00216.warc.gz\"}"}