Tue, 06 Jan 2015 21:39:09 +0100
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 | hex.c |
michael@0 | 3 | |
michael@0 | 4 | |
michael@0 | 5 | Copyright (C) 2001-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 | briank@network-resonance.com |
michael@0 | 37 | */ |
michael@0 | 38 | |
michael@0 | 39 | |
michael@0 | 40 | static char *RCSSTRING __UNUSED__ ="$Id: hex.c,v 1.3 2007/06/26 22:37:56 adamcain Exp $"; |
michael@0 | 41 | |
michael@0 | 42 | #include <sys/types.h> |
michael@0 | 43 | #include <string.h> |
michael@0 | 44 | #include <ctype.h> |
michael@0 | 45 | #include "r_common.h" |
michael@0 | 46 | #include "hex.h" |
michael@0 | 47 | #include "r_log.h" |
michael@0 | 48 | |
michael@0 | 49 | static char bin2hex_map[][3] = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c", "1d", "1e", "1f", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2a", "2b", "2c", "2d", "2e", "2f", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3a", "3b", "3c", "3d", "3e", "3f", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4a", "4b", "4c", "4d", "4e", "4f", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5a", "5b", "5c", "5d", "5e", "5f", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6a", "6b", "6c", "6d", "6e", "6f", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7a", "7b", "7c", "7d", "7e", "7f", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8a", "8b", "8c", "8d", "8e", "8f", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9a", "9b", "9c", "9d", "9e", "9f", "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "aa", "ab", "ac", "ad", "ae", "af", "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "ba", "bb", "bc", "bd", "be", "bf", "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "ca", "cb", "cc", "cd", "ce", "cf", "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "da", "db", "dc", "dd", "de", "df", "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "ea", "eb", "ec", "ed", "ee", "ef", "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "fa", "fb", "fc", "fd", "fe", "ff" }; |
michael@0 | 50 | |
michael@0 | 51 | static int hex2bin_map[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0 /* '0' */, 1 /* '1' */, 2 /* '2' */, 3 /* '3' */, 4 /* '4' */, 5 /* '5' */, 6 /* '6' */, 7 /* '7' */, 8 /* '8' */, 9 /* '9' */, -1, -1, -1, -1, -1, -1, -1, 10 /* 'A' */, 11 /* 'B' */, 12 /* 'C' */, 13 /* 'D' */, 14 /* 'E' */, 15 /* 'F' */, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10 /* 'a' */, 11 /* 'b' */, 12 /* 'c' */, 13 /* 'd' */, 14 /* 'e' */, 15 /* 'f' */, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; |
michael@0 | 52 | |
michael@0 | 53 | int |
michael@0 | 54 | nr_nbin2hex(UCHAR *bin, int binlen, char hex[], size_t size, int *len) |
michael@0 | 55 | { |
michael@0 | 56 | int _status; |
michael@0 | 57 | int i; |
michael@0 | 58 | |
michael@0 | 59 | if (size < (2*binlen)) |
michael@0 | 60 | ABORT(R_BAD_ARGS); |
michael@0 | 61 | |
michael@0 | 62 | for (i = 0; i < binlen; ++i) { |
michael@0 | 63 | *hex++ = bin2hex_map[bin[i]][0]; |
michael@0 | 64 | *hex++ = bin2hex_map[bin[i]][1]; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | if (size >= (2*binlen)+1) |
michael@0 | 68 | *hex = '\0'; |
michael@0 | 69 | |
michael@0 | 70 | *len = (2*binlen); |
michael@0 | 71 | |
michael@0 | 72 | _status=0; |
michael@0 | 73 | abort: |
michael@0 | 74 | return(_status); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | |
michael@0 | 78 | int |
michael@0 | 79 | nr_nhex2bin(char *hex, int hexlen, UCHAR bin[], size_t size, int *len) |
michael@0 | 80 | { |
michael@0 | 81 | int _status; |
michael@0 | 82 | int binlen; |
michael@0 | 83 | int h1; |
michael@0 | 84 | int h2; |
michael@0 | 85 | int i; |
michael@0 | 86 | |
michael@0 | 87 | if (hexlen % 2) |
michael@0 | 88 | ABORT(R_BAD_ARGS); |
michael@0 | 89 | |
michael@0 | 90 | binlen = hexlen/2; |
michael@0 | 91 | |
michael@0 | 92 | if (size < binlen) |
michael@0 | 93 | ABORT(R_BAD_ARGS); |
michael@0 | 94 | |
michael@0 | 95 | for (i = 0; i < binlen; ++i) { |
michael@0 | 96 | h1 = hex2bin_map[(int)*hex++]; |
michael@0 | 97 | h2 = hex2bin_map[(int)*hex++]; |
michael@0 | 98 | |
michael@0 | 99 | if (h1 == -1 || h2 == -1) |
michael@0 | 100 | ABORT(R_BAD_ARGS); |
michael@0 | 101 | |
michael@0 | 102 | bin[i] = (h1 << 4) | h2; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | *len = binlen; |
michael@0 | 106 | |
michael@0 | 107 | _status=0; |
michael@0 | 108 | abort: |
michael@0 | 109 | return(_status); |
michael@0 | 110 | } |