media/mtransport/third_party/nrappkit/src/util/p_buf.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /**
michael@0 2 p_buf.c
michael@0 3
michael@0 4
michael@0 5 Copyright (C) 2003, Network Resonance, Inc.
michael@0 6 Copyright (C) 2006, Network Resonance, Inc.
michael@0 7 All Rights Reserved
michael@0 8
michael@0 9 Redistribution and use in source and binary forms, with or without
michael@0 10 modification, are permitted provided that the following conditions
michael@0 11 are met:
michael@0 12
michael@0 13 1. Redistributions of source code must retain the above copyright
michael@0 14 notice, this list of conditions and the following disclaimer.
michael@0 15 2. Redistributions in binary form must reproduce the above copyright
michael@0 16 notice, this list of conditions and the following disclaimer in the
michael@0 17 documentation and/or other materials provided with the distribution.
michael@0 18 3. Neither the name of Network Resonance, Inc. nor the name of any
michael@0 19 contributors to this software may be used to endorse or promote
michael@0 20 products derived from this software without specific prior written
michael@0 21 permission.
michael@0 22
michael@0 23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
michael@0 24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
michael@0 26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
michael@0 27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
michael@0 28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
michael@0 29 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
michael@0 30 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
michael@0 31 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
michael@0 32 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
michael@0 33 POSSIBILITY OF SUCH DAMAGE.
michael@0 34
michael@0 35
michael@0 36 All Rights Reserved.
michael@0 37
michael@0 38 ekr@rtfm.com Tue Nov 25 16:33:08 2003
michael@0 39 */
michael@0 40
michael@0 41 static char *RCSSTRING __UNUSED__ ="Id: p_buf.c,v 1.3 2004/01/03 22:13:53 ekr Exp $";
michael@0 42
michael@0 43 #include <string.h>
michael@0 44 #include <stddef.h>
michael@0 45 #include "nr_common.h"
michael@0 46 #include "p_buf.h"
michael@0 47
michael@0 48
michael@0 49 static int nr_p_buf_destroy_chain(nr_p_buf_head *head);
michael@0 50 static int nr_p_buf_destroy(nr_p_buf *buf);
michael@0 51
michael@0 52 int nr_p_buf_ctx_create(size,ctxp)
michael@0 53 int size;
michael@0 54 nr_p_buf_ctx **ctxp;
michael@0 55 {
michael@0 56 int _status;
michael@0 57 nr_p_buf_ctx *ctx=0;
michael@0 58
michael@0 59 if(!(ctx=(nr_p_buf_ctx *)RCALLOC(sizeof(nr_p_buf_ctx))))
michael@0 60 ABORT(R_NO_MEMORY);
michael@0 61
michael@0 62 ctx->buf_size=size;
michael@0 63 STAILQ_INIT(&ctx->free_list);
michael@0 64
michael@0 65 *ctxp=ctx;
michael@0 66 _status=0;
michael@0 67 abort:
michael@0 68 if(_status){
michael@0 69 nr_p_buf_ctx_destroy(&ctx);
michael@0 70 }
michael@0 71 return(_status);
michael@0 72 }
michael@0 73
michael@0 74 int nr_p_buf_ctx_destroy(ctxp)
michael@0 75 nr_p_buf_ctx **ctxp;
michael@0 76 {
michael@0 77 nr_p_buf_ctx *ctx;
michael@0 78
michael@0 79 if(!ctxp || !*ctxp)
michael@0 80 return(0);
michael@0 81
michael@0 82 ctx=*ctxp;
michael@0 83
michael@0 84 nr_p_buf_destroy_chain(&ctx->free_list);
michael@0 85
michael@0 86 RFREE(ctx);
michael@0 87 *ctxp=0;
michael@0 88
michael@0 89 return(0);
michael@0 90 }
michael@0 91
michael@0 92 int nr_p_buf_alloc(ctx,bufp)
michael@0 93 nr_p_buf_ctx *ctx;
michael@0 94 nr_p_buf **bufp;
michael@0 95 {
michael@0 96 int _status;
michael@0 97 nr_p_buf *buf=0;
michael@0 98
michael@0 99 if(!STAILQ_EMPTY(&ctx->free_list)){
michael@0 100 buf=STAILQ_FIRST(&ctx->free_list);
michael@0 101 STAILQ_REMOVE_HEAD(&ctx->free_list,entry);
michael@0 102 goto ok;
michael@0 103 }
michael@0 104 else {
michael@0 105 if(!(buf=(nr_p_buf *)RCALLOC(sizeof(nr_p_buf))))
michael@0 106 ABORT(R_NO_MEMORY);
michael@0 107 if(!(buf->data=(UCHAR *)RMALLOC(ctx->buf_size)))
michael@0 108 ABORT(R_NO_MEMORY);
michael@0 109 buf->size=ctx->buf_size;
michael@0 110 }
michael@0 111
michael@0 112 ok:
michael@0 113 buf->r_offset=0;
michael@0 114 buf->length=0;
michael@0 115
michael@0 116 *bufp=buf;
michael@0 117 _status=0;
michael@0 118 abort:
michael@0 119 if(_status){
michael@0 120 nr_p_buf_destroy(buf);
michael@0 121 }
michael@0 122 return(_status);
michael@0 123 }
michael@0 124
michael@0 125 int nr_p_buf_free(ctx,buf)
michael@0 126 nr_p_buf_ctx *ctx;
michael@0 127 nr_p_buf *buf;
michael@0 128 {
michael@0 129 STAILQ_INSERT_TAIL(&ctx->free_list,buf,entry);
michael@0 130
michael@0 131 return(0);
michael@0 132 }
michael@0 133
michael@0 134 int nr_p_buf_free_chain(ctx,head)
michael@0 135 nr_p_buf_ctx *ctx;
michael@0 136 nr_p_buf_head *head;
michael@0 137 {
michael@0 138 nr_p_buf *n1,*n2;
michael@0 139
michael@0 140 n1=STAILQ_FIRST(head);
michael@0 141 while(n1){
michael@0 142 n2=STAILQ_NEXT(n1,entry);
michael@0 143
michael@0 144 nr_p_buf_free(ctx,n1);
michael@0 145
michael@0 146 n1=n2;
michael@0 147 }
michael@0 148
michael@0 149 return(0);
michael@0 150 }
michael@0 151
michael@0 152
michael@0 153 int nr_p_buf_write_to_chain(ctx,chain,data,len)
michael@0 154 nr_p_buf_ctx *ctx;
michael@0 155 nr_p_buf_head *chain;
michael@0 156 UCHAR *data;
michael@0 157 UINT4 len;
michael@0 158 {
michael@0 159 int r,_status;
michael@0 160 nr_p_buf *buf;
michael@0 161
michael@0 162 buf=STAILQ_LAST(chain,nr_p_buf_,entry);
michael@0 163 while(len){
michael@0 164 int towrite;
michael@0 165
michael@0 166 if(!buf){
michael@0 167 if(r=nr_p_buf_alloc(ctx,&buf))
michael@0 168 ABORT(r);
michael@0 169 STAILQ_INSERT_TAIL(chain,buf,entry);
michael@0 170 }
michael@0 171
michael@0 172 towrite=MIN(len,(buf->size-(buf->length+buf->r_offset)));
michael@0 173
michael@0 174 memcpy(buf->data+buf->length+buf->r_offset,data,towrite);
michael@0 175 len-=towrite;
michael@0 176 data+=towrite;
michael@0 177 buf->length+=towrite;
michael@0 178
michael@0 179 r_log(LOG_COMMON,LOG_DEBUG,"Wrote %d bytes to buffer %p",towrite,buf);
michael@0 180 buf=0;
michael@0 181 }
michael@0 182
michael@0 183 _status=0;
michael@0 184 abort:
michael@0 185 return(_status);
michael@0 186 }
michael@0 187
michael@0 188 static int nr_p_buf_destroy_chain(head)
michael@0 189 nr_p_buf_head *head;
michael@0 190 {
michael@0 191 nr_p_buf *n1,*n2;
michael@0 192
michael@0 193 n1=STAILQ_FIRST(head);
michael@0 194 while(n1){
michael@0 195 n2=STAILQ_NEXT(n1,entry);
michael@0 196
michael@0 197 nr_p_buf_destroy(n1);
michael@0 198
michael@0 199 n1=n2;
michael@0 200 }
michael@0 201
michael@0 202 return(0);
michael@0 203 }
michael@0 204
michael@0 205 static int nr_p_buf_destroy(buf)
michael@0 206 nr_p_buf *buf;
michael@0 207 {
michael@0 208 if(!buf)
michael@0 209 return(0);
michael@0 210
michael@0 211 RFREE(buf->data);
michael@0 212 RFREE(buf);
michael@0 213
michael@0 214 return(0);
michael@0 215 }
michael@0 216
michael@0 217

mercurial