]> cygwin.com Git - cygwin-apps/setup.git/blame - KeysSetting.cc
* res.rc (SETUP_STANDARD_DIALOG_W, SETUP_STANDARD_DIALOG_H,
[cygwin-apps/setup.git] / KeysSetting.cc
CommitLineData
dbfe3c19
DK
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
25static const char *cvsid =
26 "\n%%% $Id$\n";
27#endif
28
29#include <stdlib.h>
ef3be327 30#include <string.h>
dbfe3c19
DK
31#include "UserSettings.h"
32#include "io_stream.h"
33#include "KeysSetting.h"
34
35void
36ExtraKeysSetting::load()
37{
38 static int inited = 0;
39 if (inited)
40 return;
41 io_stream *f = UserSettings::Instance().settingFileForLoad("last-extrakeys");
42 if (f)
43 {
44 bufsize = f->get_size ();
45 if (bufsize)
46 {
47 keybuffer = new char[bufsize];
48 f->read (keybuffer, bufsize);
49 // Calling count_keys gets the count but also sizes the buffer
50 // correctly, discarding any trailing non-LF-terminated data.
51 bufsize = count_keys ();
52 }
53 delete f;
54 }
55 inited = 1;
56}
57
58void
59ExtraKeysSetting::save()
60{
61 io_stream *f = UserSettings::Instance().settingFileForSave("last-extrakeys");
62 if (f)
63 {
64 if (bufsize)
65 f->write(keybuffer, bufsize);
66 delete f;
67 }
68}
69
70void
71ExtraKeysSetting::flush (void)
72{
73 if (bufsize)
74 delete [] keybuffer;
75 keybuffer = 0;
76 bufsize = 0;
77 numkeys = 0;
78}
79
80void
81ExtraKeysSetting::realloc (size_t newbufsize)
82{
83 char *newbuf = new char[newbufsize];
84 if (bufsize)
85 {
86 memcpy (newbuf, keybuffer, newbufsize < bufsize ? newbufsize : bufsize);
87 delete [] keybuffer;
88 }
89 keybuffer = newbuf;
90 bufsize = newbufsize;
91}
92
93size_t
94ExtraKeysSetting::count_keys (void)
95{
96 size_t offs = 0, size = 0;
97 numkeys = 0;
98 while (offs < bufsize)
99 if (keybuffer[offs++] == 0x0a)
100 {
101 size = offs;
102 ++numkeys;
103 }
104 return size;
105}
106
107size_t
108ExtraKeysSetting::num_keys (void)
109{
110 return numkeys;
111}
112
113const char *
114ExtraKeysSetting::get_key (size_t num, size_t *size)
115{
116 if (!numkeys || (num >= numkeys))
117 return NULL;
118
119 const char *ptr = keybuffer;
120 while (num--)
121 while (*ptr++ != 0x0a);
122
123 // Count its size if requested.
124 if (size)
125 {
126 const char *ptr2 = ptr;
127 while (num--)
128 if (*ptr2 != 0x0a)
129 ++ptr2;
130 else
131 break;
132 *size = ptr2 - ptr;
133 }
134 return ptr;
135}
136
137void
138ExtraKeysSetting::add_unique_key (const char *key)
139{
140 size_t osize = bufsize;
141 realloc (bufsize + strlen (key) + 1);
142 strcpy (keybuffer + osize, key);
143 keybuffer[bufsize - 1] = 0x0a;
144 ++numkeys;
145}
146
147void
148ExtraKeysSetting::add_key (const char *key)
149{
150 /* Only add key if we don't already have it. */
151 const char *ptr = keybuffer;
152 size_t remain = bufsize;
153 size_t keylen = strlen (key);
154
155 while (remain >= keylen)
156 {
157 if (memcmp (ptr, key, keylen) == 0)
158 return;
159
160 while (remain--)
161 if (*ptr++ == 0x0a)
162 break;
163 }
164 add_unique_key (key);
165}
166
167
This page took 0.036234 seconds and 5 git commands to generate.