| | varnish-cache/vmod/vmod_blob_id.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright 2015-2016 UPLEX - Nils Goroll Systemoptimierung |
| 2 |
|
* All rights reserved. |
| 3 |
|
* |
| 4 |
|
* Authors: Nils Goroll <nils.goroll@uplex.de> |
| 5 |
|
* Geoffrey Simmons <geoffrey.simmons@uplex.de> |
| 6 |
|
* |
| 7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 8 |
|
* |
| 9 |
|
* Redistribution and use in source and binary forms, with or without |
| 10 |
|
* modification, are permitted provided that the following conditions are met: |
| 11 |
|
* 1. Redistributions of source code must retain the above copyright notice, |
| 12 |
|
* this list of conditions and the following disclaimer. |
| 13 |
|
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 |
|
* this list of conditions and the following disclaimer in the documentation |
| 15 |
|
* and/or other materials provided with the distribution. |
| 16 |
|
* |
| 17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY |
| 18 |
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 |
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 |
|
* DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY |
| 21 |
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 |
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 |
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 |
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 |
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 26 |
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
* |
| 28 |
|
*/ |
| 29 |
|
|
| 30 |
|
#include "config.h" |
| 31 |
|
|
| 32 |
|
#include <string.h> |
| 33 |
|
|
| 34 |
|
#include "vdef.h" |
| 35 |
|
#include "vrt.h" |
| 36 |
|
#include "vas.h" |
| 37 |
|
#include "miniobj.h" |
| 38 |
|
|
| 39 |
|
#include "vmod_blob.h" |
| 40 |
|
|
| 41 |
|
size_t |
| 42 |
320 |
id_encode_l(size_t l) |
| 43 |
|
{ |
| 44 |
320 |
return (l + 1); |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
size_t |
| 48 |
1600 |
id_decode_l(size_t l) |
| 49 |
|
{ |
| 50 |
1600 |
return (l); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
ssize_t |
| 54 |
6680 |
id_encode(const enum encoding enc, const enum case_e kase, |
| 55 |
|
blob_dest_t buf, blob_len_t buflen, |
| 56 |
|
blob_src_t in, blob_len_t inlen) |
| 57 |
|
{ |
| 58 |
6680 |
(void) enc; |
| 59 |
6680 |
(void) kase; |
| 60 |
6680 |
AN(buf); |
| 61 |
|
|
| 62 |
6680 |
if (buflen < inlen + 1) |
| 63 |
160 |
return (-1); |
| 64 |
6520 |
if (in == NULL || inlen == 0) |
| 65 |
960 |
return (0); |
| 66 |
|
|
| 67 |
5560 |
memcpy(buf, in, inlen); |
| 68 |
5560 |
return (inlen); |
| 69 |
6680 |
} |
| 70 |
|
|
| 71 |
|
ssize_t |
| 72 |
4640 |
id_decode(const enum encoding enc, blob_dest_t buf, |
| 73 |
|
blob_len_t buflen, ssize_t n, VCL_STRANDS strings) |
| 74 |
|
{ |
| 75 |
|
const char *s; |
| 76 |
4640 |
char *dest = buf; |
| 77 |
4640 |
size_t len, outlen = 0, c = SIZE_MAX; |
| 78 |
|
int i; |
| 79 |
|
|
| 80 |
4640 |
(void)enc; |
| 81 |
4640 |
AN(buf); |
| 82 |
4640 |
CHECK_OBJ_NOTNULL(strings, STRANDS_MAGIC); |
| 83 |
|
|
| 84 |
4640 |
if (n >= 0) |
| 85 |
1000 |
c = n; |
| 86 |
|
|
| 87 |
11360 |
for (i = 0; c > 0 && i < strings->n; i++) { |
| 88 |
6760 |
s = strings->p[i]; |
| 89 |
6760 |
if (s == NULL || *s == '\0') |
| 90 |
2320 |
continue; |
| 91 |
4440 |
len = strlen(s); |
| 92 |
4440 |
if (len > c) |
| 93 |
800 |
len = c; |
| 94 |
4440 |
c -= len; |
| 95 |
4440 |
if ((outlen += len) > buflen) { |
| 96 |
40 |
errno = ENOMEM; |
| 97 |
40 |
return (-1); |
| 98 |
|
} |
| 99 |
4400 |
memcpy(dest, s, len); |
| 100 |
4400 |
dest += len; |
| 101 |
4400 |
} |
| 102 |
|
|
| 103 |
4600 |
return (outlen); |
| 104 |
4640 |
} |