]> cygwin.com Git - cygwin-apps/setup.git/blame - KeysSetting.cc
Add command-line option help-text localization
[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
dbfe3c19 24#include <stdlib.h>
ef3be327 25#include <string.h>
dbfe3c19
DK
26#include "UserSettings.h"
27#include "io_stream.h"
28#include "KeysSetting.h"
29
f26f525f
CF
30ExtraKeysSetting *ExtraKeysSetting::global;
31
32ExtraKeysSetting::ExtraKeysSetting ():
33 keybuffer (NULL), bufsize (0), numkeys (0)
dbfe3c19 34{
f26f525f
CF
35 global = this;
36 const char *p = UserSettings::instance().get ("extrakeys");
37 if (p)
dbfe3c19 38 {
e8c42399 39 bufsize = strlen (p) + 1; // Include final NUL.
f26f525f 40 keybuffer = strdup (p);
e8c42399
KB
41 // Replace final NUL by LF.
42 keybuffer[bufsize - 1] = 0x0a;
f26f525f
CF
43 // Calling count_keys gets the count but also sizes the buffer
44 // correctly, discarding any trailing non-LF-terminated data.
45 bufsize = count_keys ();
dbfe3c19 46 }
dbfe3c19
DK
47}
48
f26f525f 49ExtraKeysSetting::~ExtraKeysSetting ()
dbfe3c19 50{
f26f525f 51 if (keybuffer)
e8c42399
KB
52 {
53 // Replace final LF by NUL.
54 keybuffer[bufsize - 1] = '\0';
55 UserSettings::instance().set ("extrakeys", keybuffer);
56 }
dbfe3c19
DK
57}
58
59void
60ExtraKeysSetting::flush (void)
61{
62 if (bufsize)
63 delete [] keybuffer;
64 keybuffer = 0;
65 bufsize = 0;
66 numkeys = 0;
67}
68
69void
70ExtraKeysSetting::realloc (size_t newbufsize)
71{
72 char *newbuf = new char[newbufsize];
73 if (bufsize)
74 {
75 memcpy (newbuf, keybuffer, newbufsize < bufsize ? newbufsize : bufsize);
76 delete [] keybuffer;
77 }
78 keybuffer = newbuf;
79 bufsize = newbufsize;
80}
81
82size_t
83ExtraKeysSetting::count_keys (void)
84{
85 size_t offs = 0, size = 0;
86 numkeys = 0;
87 while (offs < bufsize)
88 if (keybuffer[offs++] == 0x0a)
89 {
90 size = offs;
91 ++numkeys;
92 }
93 return size;
94}
95
96size_t
97ExtraKeysSetting::num_keys (void)
98{
99 return numkeys;
100}
101
102const char *
103ExtraKeysSetting::get_key (size_t num, size_t *size)
104{
105 if (!numkeys || (num >= numkeys))
106 return NULL;
107
108 const char *ptr = keybuffer;
109 while (num--)
110 while (*ptr++ != 0x0a);
111
112 // Count its size if requested.
113 if (size)
114 {
115 const char *ptr2 = ptr;
116 while (num--)
117 if (*ptr2 != 0x0a)
118 ++ptr2;
119 else
120 break;
121 *size = ptr2 - ptr;
122 }
123 return ptr;
124}
125
126void
127ExtraKeysSetting::add_unique_key (const char *key)
128{
129 size_t osize = bufsize;
130 realloc (bufsize + strlen (key) + 1);
131 strcpy (keybuffer + osize, key);
132 keybuffer[bufsize - 1] = 0x0a;
133 ++numkeys;
134}
135
136void
137ExtraKeysSetting::add_key (const char *key)
138{
139 /* Only add key if we don't already have it. */
140 const char *ptr = keybuffer;
141 size_t remain = bufsize;
142 size_t keylen = strlen (key);
143
144 while (remain >= keylen)
145 {
146 if (memcmp (ptr, key, keylen) == 0)
147 return;
148
149 while (remain--)
150 if (*ptr++ == 0x0a)
151 break;
152 }
153 add_unique_key (key);
154}
This page took 0.105809 seconds and 5 git commands to generate.