int buffer_copy_memory(buffer *b, const char *s, size_t s_len) { if (!s || !b) return -1; b->used = 0; return buffer_append_memory(b, s, s_len); }
//复制s指向的内存区域中的数据到b中 int buffer_copy_memory(buffer *b, const char *s, size_t s_len) { if (!s || !b) { return -1; } //将b的数据长度设为0,调用buffer_append_memory覆盖原来的数据。 b -> used = 0; return buffer_append_memory(b, s, s_len); }
/** * byte_writer_reset_and_get_buffer: * @writer: #ByteWriter instance * * Resets @writer and returns the current data as buffer. * * Free-function: buffer_unref * * Returns: (transfer full): the current data as buffer. buffer_unref() * after usage. */ Buffer * byte_writer_reset_and_get_buffer (ByteWriter * writer) { Buffer *buffer; void* data; size_t size; g_return_val_if_fail (writer != NULL, NULL); size = writer->parent.size; data = byte_writer_reset_and_get_data (writer); buffer = buffer_new (); if (data != NULL) { buffer_append_memory (buffer, memory_new_wrapped (0, data, size, 0, size, data, g_free)); } return buffer; }
static int proxy_create_env(server *srv, handler_ctx *hctx) { size_t i; connection *con = hctx->remote_conn; buffer *b; /* build header */ b = chunkqueue_get_append_buffer(hctx->wb); /* request line */ buffer_copy_string(b, get_http_method_name(con->request.http_method)); buffer_append_string_len(b, CONST_STR_LEN(" ")); buffer_append_string_buffer(b, con->request.uri); buffer_append_string_len(b, CONST_STR_LEN(" HTTP/1.0\r\n")); proxy_append_header(con, "X-Forwarded-For", (char *)inet_ntop_cache_get_ip(srv, &(con->dst_addr))); /* http_host is NOT is just a pointer to a buffer * which is NULL if it is not set */ if (con->request.http_host && !buffer_is_empty(con->request.http_host)) { proxy_set_header(con, "X-Host", con->request.http_host->ptr); } proxy_set_header(con, "X-Forwarded-Proto", con->conf.is_ssl ? "https" : "http"); /* request header */ for (i = 0; i < con->request.headers->used; i++) { data_string *ds; ds = (data_string *)con->request.headers->data[i]; if (ds->value->used && ds->key->used) { if (buffer_is_equal_string(ds->key, CONST_STR_LEN("Connection"))) continue; if (buffer_is_equal_string(ds->key, CONST_STR_LEN("Proxy-Connection"))) continue; buffer_append_string_buffer(b, ds->key); buffer_append_string_len(b, CONST_STR_LEN(": ")); buffer_append_string_buffer(b, ds->value); buffer_append_string_len(b, CONST_STR_LEN("\r\n")); } } buffer_append_string_len(b, CONST_STR_LEN("\r\n")); hctx->wb->bytes_in += b->used - 1; /* body */ if (con->request.content_length) { chunkqueue *req_cq = con->request_content_queue; chunk *req_c; off_t offset; /* something to send ? */ for (offset = 0, req_c = req_cq->first; offset != req_cq->bytes_in; req_c = req_c->next) { off_t weWant = req_cq->bytes_in - offset; off_t weHave = 0; /* we announce toWrite octects * now take all the request_content chunk that we need to fill this request * */ switch (req_c->type) { case FILE_CHUNK: weHave = req_c->file.length - req_c->offset; if (weHave > weWant) weHave = weWant; chunkqueue_append_file(hctx->wb, req_c->file.name, req_c->offset, weHave); req_c->offset += weHave; req_cq->bytes_out += weHave; hctx->wb->bytes_in += weHave; break; case MEM_CHUNK: /* append to the buffer */ weHave = req_c->mem->used - 1 - req_c->offset; if (weHave > weWant) weHave = weWant; b = chunkqueue_get_append_buffer(hctx->wb); buffer_append_memory(b, req_c->mem->ptr + req_c->offset, weHave); b->used++; /* add virtual \0 */ req_c->offset += weHave; req_cq->bytes_out += weHave; hctx->wb->bytes_in += weHave; break; default: break; } offset += weHave; } } return 0; }
int read_umd_chapter_content(const char *umdfile, u_int index, p_umd_chapter pchapter /*,size_t file_offset,size_t length */ , buffer ** pbuf) { int ret = -1; SceUID fd = -1; SceIoStat sta; char buf[9] = { 0 }; size_t stlen = 0; size_t stoutlen = 0; size_t stUnzipSize = 0; buffer *pzbuf = NULL; buffer *puzbuf = NULL; bool bok = false; struct t_chapter *pchap; size_t chunk_pos; size_t chunk_offset; size_t length; if (!umdfile || !pchapter || index + 1 > pchapter->chapter_count || !pchapter->pchapters || !(pchapter->pchapters + index) || !pbuf || !(*pbuf)) return -1; pchap = pchapter->pchapters + index; chunk_pos = pchap->chunk_pos; chunk_offset = pchap->chunk_offset; length = pchap->length; do { char *p; struct UMDHeaderDataEx *pEx; if (sceIoGetstat(umdfile, &sta) < 0 || sta.st_size < chunk_pos) { return -1; } if ((fd = sceIoOpen(umdfile, PSP_O_RDONLY, 0777)) < 0) { return -2; } if (sceIoLseek(fd, chunk_pos, PSP_SEEK_SET) < 0) { break; } if ((stlen = sceIoRead(fd, buf, 9)) < 0) { //dbg_printf(d, "%s read umd file head chunk error!",__func__); break; } p = &buf[0]; pzbuf = buffer_init(); if (pzbuf == NULL) break; puzbuf = buffer_init(); if (puzbuf == NULL) break; pEx = (struct UMDHeaderDataEx *) p; if (!pEx || pEx->Mark != '$' || pEx->Length < 9) break; stlen = pEx->Length - 9; stoutlen = stlen * 2; buf_offset = 0; umdfile_offset = chunk_pos + 9; umdfile_remain = sta.st_size - umdfile_offset; if (stlen < length + chunk_offset) { bool bfirst_chunk = true; size_t stHeadSize = sizeof(struct UMDHeaderData); size_t stHeadExSize = sizeof(struct UMDHeaderDataEx); //buffer_prepare_copy(pzbuf,stlen); buffer_prepare_copy(pzbuf, umdfile_remain); pzbuf->used = pzbuf->size; if (0 > read_umd_buf(fd, &pzbuf)) { //dbg_printf(d, "%s not start with 0xde9a9b89,that umd must be corrupted!",__func__); break; } buffer_prepare_copy(*pbuf, length); while (*p == '$') { bok = false; pEx = (struct UMDHeaderDataEx *) p; stlen = pEx->Length; if (stlen < 9) { ////dbg_printf(d,"%s zipLength %d < 9",__func__,ZipLength); break; } //pchap[i++].pos = umdfile_offset - 9; stlen -= 9; stoutlen = stlen * 2; buffer_prepare_copy(puzbuf, stoutlen); if (0 > get_chunk_buf(fd, &pzbuf, &p, stlen)) break; stUnzipSize = umd_inflate((Byte *) p, (Byte *) puzbuf->ptr, stlen, stoutlen); if (stUnzipSize < 0 || stUnzipSize > stoutlen) { printf("stUnzipSize %d not in limit size:%d", stUnzipSize, stoutlen); break; } if (bfirst_chunk) { if (length <= stUnzipSize - chunk_offset) { buffer_append_memory(*pbuf, puzbuf->ptr + chunk_offset, length); ret = length; bok = true; break; } buffer_append_memory(*pbuf, puzbuf->ptr + chunk_offset, stUnzipSize - chunk_offset); length -= (stUnzipSize - chunk_offset); chunk_offset = 0; bfirst_chunk = false; } else if (length > stUnzipSize) { buffer_append_memory(*pbuf, puzbuf->ptr, stUnzipSize); length -= stUnzipSize; } else { buffer_append_memory(*pbuf, puzbuf->ptr + chunk_offset, stUnzipSize - chunk_offset); ret = pchap->length; bok = true; break; } if (*(pzbuf->ptr + buf_offset) == '#') { bok = true; while (*(pzbuf->ptr + buf_offset) == '#') { struct UMDHeaderData *pHead; if (0 > get_chunk_buf(fd, &pzbuf, &p, stHeadSize)) { bok = false; break; } pHead = (struct UMDHeaderData *) p; if (pHead->hdType == 0xf1 || pHead->hdType == 10) { if (*(pzbuf->ptr + buf_offset + pHead->Length - stHeadSize) == '#') { if (0 > get_chunk_buf(fd, &pzbuf, &p, pHead->Length - stHeadSize)) { bok = false; break; } } else { if (0 > get_offset_chunk_buf(fd, &pzbuf, &p, pHead->Length - stHeadSize, stHeadExSize)) bok = false; break; } } else if (pHead->hdType == 0x81) { printf("you should never come here fileoffset:%d\n", umdfile_offset); if (pzbuf) buffer_free(pzbuf); if (puzbuf) buffer_free(puzbuf); sceIoClose(fd); return 0; } } if (!bok) break; } else { if (0 > get_chunk_buf(fd, &pzbuf, &p, stHeadExSize)) break; } } } else { buffer_prepare_copy(pzbuf, stlen); pzbuf->used = pzbuf->size; if (0 > read_umd_buf(fd, &pzbuf)) { //dbg_printf(d, "%s not start with 0xde9a9b89,that umd must be corrupted!",__func__); break; } if (0 > get_chunk_buf(fd, &pzbuf, &p, stlen)) break; buffer_prepare_copy(puzbuf, stoutlen); stUnzipSize = umd_inflate((Byte *) p, (Byte *) puzbuf->ptr, stlen, stoutlen); if (stUnzipSize < 0 || stUnzipSize > stoutlen) { printf("stUnzipSize %d not in limit size:%d", stUnzipSize, stoutlen); break; } buffer_copy_string_len(*pbuf, puzbuf->ptr + chunk_offset, length); ret = length; } } while (false); if (pzbuf) buffer_free(pzbuf); if (puzbuf) buffer_free(puzbuf); if (fd) sceIoClose(fd); return ret; }
int ReadAdditionalSession(char **pSe, buffer ** buf, p_umd_chapter * pchapter, const u_short hdType, bool b_get_chapter) { u_int ulTotalLength = 0; struct UMDHeaderDataEx *pHead; u_int i = 0; if (!pSe || !(*pSe)) return -1; if (b_get_chapter && (!pchapter || !(*pchapter))) return -2; pHead = (struct UMDHeaderDataEx *) (*pSe); if (!pHead || pHead->Length < 9) { //cout << "invalid additional length:" << pHead->Length << endl; return -2; } pHead->Length -= 9; switch (hdType) { case 14: case 15: { /*if((14 == hdType && 2 != (*pchapter)->umd_mode) || (15 == hdType && 3 != (*pchapter)->umd_mode) || *(*pSe) != '$') { //dbg_printf(d,"%s umd mode:%d not fit for hdType %d or not start from $\n",__func__,(*pchapter)->umd_mode,hdType); break; } */ buffer *pRaw = (buffer *) calloc(1, sizeof(*pRaw)); if (pRaw == NULL) return -1; while (*(*pSe) == '$') { struct UMDHeaderDataEx *pEx = (struct UMDHeaderDataEx *) (*pSe); u_int ZipLength = pEx->Length; u_int len; if (ZipLength < 9) { //dbg_printf(d,"%s zipLength %d < 9",__func__,ZipLength); break; } (*pSe) += 9; len = ZipLength - 9; buffer_append_memory(*buf, *pSe, len); (*pSe) += len; ulTotalLength += len; if (**pSe == '#') { struct UMDHeaderData *ps = (struct UMDHeaderData *) (*pSe); if (ps->hdType == 10) (*pSe) += ps->Length; else if (ps->hdType == 0x81) { buffer_free(pRaw); return 1; } } } buffer_free(pRaw); break; } /* case 130: break; case 0x81: pHead->Length /= 4; //cout << "total " << pHead->Length << " blocks" << endl; for(i = 0;i < pHead->Length;i++) { //cout << "block " << i << " is " << *(u_int*)(*pSe) << endl; (*pSe) += 4; } break; */ case 0x83: (*pSe) += 9; pHead->Length /= 4; //cout << "total " << pHead->Length << " chapters" << endl; if (b_get_chapter) { struct t_chapter *p; (*pchapter)->chapter_count = pHead->Length; (*pchapter)->pchapters = (struct t_chapter *) calloc(pHead->Length, sizeof(struct t_chapter)); if (!(*pchapter)->pchapters) return -4; p = (*pchapter)->pchapters; for (i = 0; i < pHead->Length; i++) { //cout << "chapter " << i << " is " << *(u_int*)(*pSe) << endl; if (!(p[i].name = buffer_init())) return -4; memcpy(&p[i].chunk_pos, *pSe, 4); (*pSe) += 4; } } else { for (i = 0; i < pHead->Length; i++) { //cout << "chapter " << i << " is " << *(u_int*)(*pSe) << endl; (*pSe) += 4; } } break; case 0x84: { (*pSe) += 9; if (b_get_chapter) { //cout << "total " << pHead->Length << " chapters" << endl; size_t stlen = (*pchapter)->chapter_count; size_t chapter_len = 0; struct t_chapter *p = (*pchapter)->pchapters; for (i = 0; i < stlen; i++) { chapter_len = *(u8 *) (*pSe); (*pSe) += 1; buffer_copy_string_len(p[i].name, *pSe, (chapter_len > 256) ? 256 : chapter_len); //dbg_printf(d,"%dth chapter name:%s\n",i,p[i].name->ptr); (*pSe) += chapter_len; } return stlen; } else { size_t stUnzipSize = 0; Byte *p = NULL; buffer *pRaw; (*pSe) = (*pSe) + pHead->Length; if (pchapter && 1 != (*pchapter)->umd_mode) break; if (*(*pSe) != '$') { //printlog("ms0:/PSP/GAME371/xReader/xlog.txt","not $\n"); break; } pRaw = buffer_init(); if (pRaw == NULL) return -1; while (*(*pSe) == '$') { struct UMDHeaderDataEx *pEx = (struct UMDHeaderDataEx *) (*pSe); u_int len; u_int outlen; u_int ZipLength = pEx->Length; if (ZipLength < 9) { //printlog("ms0:/PSP/GAME371/xReader/xlog.txt","\nzipLength < 9"); //printlog("ms0:/PSP/GAME371/xReader/xlog.txt",&ZipLength); break; } (*pSe) += 9; len = ZipLength - 9; outlen = len * 2; buffer_prepare_copy(pRaw, outlen); stUnzipSize = umd_inflate((Byte *) ((*pSe)), (Byte *) pRaw->ptr, len, outlen); if (stUnzipSize < 0) { buffer_free(pRaw); return -1; } p = (Byte *) pRaw->ptr; buffer_append_memory(*buf, pRaw->ptr, stUnzipSize); (*pSe) += len; ulTotalLength += outlen; if (**pSe == '#') { struct UMDHeaderData *ps = (struct UMDHeaderData *) (*pSe); if (ps->hdType == 0xf1 || ps->hdType == 10) (*pSe) += ps->Length; else if (ps->hdType == 0x81) { buffer_free(pRaw); return 1; } } } buffer_free(pRaw); } } break; default: (*pSe) += 9; (*pSe) += pHead->Length; break; } return 1; }
int buffer_append_trailing_null(buffer_t * b) { return buffer_append_memory(b, "\0", 1); }
int buffer_append_raw_int64(buffer_t * b, int64_t arg) { return buffer_append_memory(b, &arg, 8); }
int buffer_append_raw_int32(buffer_t * b, int32_t arg) { return buffer_append_memory(b, &arg, 4); }