]> cygwin.com Git - cygwin-apps/setup.git/blob - KeysSetting.cc
* prereq.cc (PrereqChecker::getUnmetString): Improve dependency list
[cygwin-apps/setup.git] / KeysSetting.cc
1 /*
2 * Copyright (c) 2008, Dave Korn.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * This implements the ExtraKeysSetting class, which persists and reads
13 * in (and saves) extra public DSA signing keys for the verification process.
14 * It stores them all in a contiguous memory buffer. Each is one line of
15 * ASCII text terminated by LF. THERE IS NO NUL-TERMINATION HERE, TAKE CARE!
16 * The buffer is sized to the exact size of the content including the terminating
17 * LF of the last entry. There is no zero after it. After reading the file,
18 * any partial last line is truncated.
19 *
20 * Written by Dave Korn <dave.korn.cygwin@gmail.com>
21 *
22 */
23
24 #if 0
25 static const char *cvsid =
26 "\n%%% $Id$\n";
27 #endif
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include "UserSettings.h"
32 #include "io_stream.h"
33 #include "KeysSetting.h"
34
35 ExtraKeysSetting *ExtraKeysSetting::global;
36
37 ExtraKeysSetting::ExtraKeysSetting ():
38 keybuffer (NULL), bufsize (0), numkeys (0)
39 {
40 global = this;
41 const char *p = UserSettings::instance().get ("extrakeys");
42 if (p)
43 {
44 keybuffer = strdup (p);
45 // Calling count_keys gets the count but also sizes the buffer
46 // correctly, discarding any trailing non-LF-terminated data.
47 bufsize = count_keys ();
48 }
49 }
50
51 ExtraKeysSetting::~ExtraKeysSetting ()
52 {
53 if (keybuffer)
54 UserSettings::instance().set ("extrakeys", keybuffer);
55 }
56
57 void
58 ExtraKeysSetting::flush (void)
59 {
60 if (bufsize)
61 delete [] keybuffer;
62 keybuffer = 0;
63 bufsize = 0;
64 numkeys = 0;
65 }
66
67 void
68 ExtraKeysSetting::realloc (size_t newbufsize)
69 {
70 char *newbuf = new char[newbufsize];
71 if (bufsize)
72 {
73 memcpy (newbuf, keybuffer, newbufsize < bufsize ? newbufsize : bufsize);
74 delete [] keybuffer;
75 }
76 keybuffer = newbuf;
77 bufsize = newbufsize;
78 }
79
80 size_t
81 ExtraKeysSetting::count_keys (void)
82 {
83 size_t offs = 0, size = 0;
84 numkeys = 0;
85 while (offs < bufsize)
86 if (keybuffer[offs++] == 0x0a)
87 {
88 size = offs;
89 ++numkeys;
90 }
91 return size;
92 }
93
94 size_t
95 ExtraKeysSetting::num_keys (void)
96 {
97 return numkeys;
98 }
99
100 const char *
101 ExtraKeysSetting::get_key (size_t num, size_t *size)
102 {
103 if (!numkeys || (num >= numkeys))
104 return NULL;
105
106 const char *ptr = keybuffer;
107 while (num--)
108 while (*ptr++ != 0x0a);
109
110 // Count its size if requested.
111 if (size)
112 {
113 const char *ptr2 = ptr;
114 while (num--)
115 if (*ptr2 != 0x0a)
116 ++ptr2;
117 else
118 break;
119 *size = ptr2 - ptr;
120 }
121 return ptr;
122 }
123
124 void
125 ExtraKeysSetting::add_unique_key (const char *key)
126 {
127 size_t osize = bufsize;
128 realloc (bufsize + strlen (key) + 1);
129 strcpy (keybuffer + osize, key);
130 keybuffer[bufsize - 1] = 0x0a;
131 ++numkeys;
132 }
133
134 void
135 ExtraKeysSetting::add_key (const char *key)
136 {
137 /* Only add key if we don't already have it. */
138 const char *ptr = keybuffer;
139 size_t remain = bufsize;
140 size_t keylen = strlen (key);
141
142 while (remain >= keylen)
143 {
144 if (memcmp (ptr, key, keylen) == 0)
145 return;
146
147 while (remain--)
148 if (*ptr++ == 0x0a)
149 break;
150 }
151 add_unique_key (key);
152 }
This page took 0.042523 seconds and 5 git commands to generate.