~ubuntu-branches/ubuntu/trusty/rhash/trusty

« back to all changes in this revision

Viewing changes to librhash/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Aleksey Kravchenko
  • Date: 2011-08-14 20:35:00 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110814203500-rvnovipwcpqkm91u
Tags: 1.2.7-1
* New upstream release version 1.2.7
 - Fixes security issue (Closes: #632280)
 - changed author/mantainer name to the way I prefer s/Alexey S/Aleksey/
 - simplified desriptions of binary packages
 - wriiten rules in more now standard way, using CFLAGS/LDFLAGS
 - reworded README.Debian a bit
 - aplied patch from upstream to fix possible segfault
 - aplied patch from upstream to fix glibc runtime warning
 - aplied patch from upstream to repair -openssl option handling

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#include <stdarg.h>
16
16
#include <stdio.h>
17
17
#include <string.h>
 
18
#include <wchar.h>
18
19
#include "util.h"
19
20
 
20
 
/* program control and error reporting functions */
 
21
/* program exit and error reporting functions */
21
22
 
22
 
static void report_error_default(const char* srcfile, int srcline, const char* format, ...);
 
23
static void report_error_default(const char* srcfile, int srcline,
 
24
        const char* format, ...);
23
25
 
24
26
void (*rsh_exit)(int code) = exit;
25
 
void (*rsh_report_error)(const char* srcfile, int srcline, const char* format, ...) = report_error_default;
 
27
void (*rsh_report_error)(const char* srcfile, int srcline,
 
28
        const char* format, ...) = report_error_default;
26
29
 
27
30
/**
28
 
 * Print given library failure to stderr
 
31
 * Print given library failure to stderr.
 
32
 *
 
33
 * @param srcfile source file to report error on fail
 
34
 * @param srcline source code line to be reported on fail
 
35
 * @param format printf-formated error message
29
36
 */
30
37
static void report_error_default(const char* srcfile, int srcline, const char* format, ...)
31
38
{
39
46
/* MEMORY FUNCTIONS */
40
47
 
41
48
/**
42
 
 * Allocates a buffer via malloc with reporting memory errors to stderr.
 
49
 * Allocates a buffer via malloc with reporting memory error to stderr.
43
50
 *
44
51
 * @param size size of the block to allocate
45
52
 * @param srcfile source file to report error on fail
50
57
{
51
58
        void* res = malloc(size);
52
59
        if(!res) {
53
 
                rsh_report_error(srcfile, srcline, "malloc(%u) failed\n", (unsigned)size);
54
 
                rsh_exit(2);
55
 
        }
56
 
        return res;
57
 
}
58
 
 
59
 
/**
60
 
 * Duplicate c-string with reporting memory errors to stderr.
 
60
                rsh_report_error(srcfile, srcline, "%s(%u) failed\n", "malloc", (unsigned)size);
 
61
                rsh_exit(2);
 
62
        }
 
63
        return res;
 
64
}
 
65
 
 
66
/**
 
67
 * Allocates a buffer via calloc with reporting memory error to stderr.
 
68
 *
 
69
 * @param num number of elements to be allocated
 
70
 * @param size size of elements
 
71
 * @param srcfile source file to report error on fail
 
72
 * @param srcline source code line to be reported on fail
 
73
 * @return allocated block
 
74
 */
 
75
void* rhash_calloc(size_t num, size_t size, const char* srcfile, int srcline)
 
76
{
 
77
        void* res = calloc(num, size);
 
78
        if(!res) {
 
79
                rsh_report_error(srcfile, srcline, "calloc(%u, %u) failed\n", (unsigned)num, (unsigned)size);
 
80
                rsh_exit(2);
 
81
        }
 
82
        return res;
 
83
}
 
84
 
 
85
 
 
86
/**
 
87
 * Duplicate c-string with reporting memory error to stderr.
61
88
 *
62
89
 * @param str the zero-terminated string to duplicate
63
90
 * @param srcfile source file to report error on fail
69
96
#ifndef __STRICT_ANSI__
70
97
        char* res = strdup(str);
71
98
#else
72
 
        char* res = malloc(strlen(str)+1);
 
99
        char* res = (char*)malloc(strlen(str)+1);
73
100
        if(res) strcpy(res, str);
74
101
#endif
75
102
 
81
108
}
82
109
 
83
110
/**
84
 
 * Reallocates a buffer via realloc with reporting memory errors to stderr.
 
111
 * Duplicate wide string with reporting memory error to stderr.
 
112
 *
 
113
 * @param str the zero-terminated string to duplicate
 
114
 * @param srcfile source file to report error on fail
 
115
 * @param srcline source code line to be reported on fail
 
116
 * @return allocated memory buffer with copied string
 
117
 */
 
118
wchar_t* rhash_wcsdup(const wchar_t* str, const char* srcfile, int srcline)
 
119
{
 
120
#ifndef __STRICT_ANSI__
 
121
        wchar_t* res = wcsdup(str);
 
122
#else
 
123
        wchar_t* res = (wchar_t*)malloc((wcslen(str) + 1) * sizeof(wchar_t));
 
124
        if(res) wcscpy(res, str);
 
125
#endif
 
126
 
 
127
        if(!res) {
 
128
                rsh_report_error(srcfile, srcline, "wcsdup(\"%u\") failed\n", (wcslen(str) + 1));
 
129
                rsh_exit(2);
 
130
        }
 
131
        return res;
 
132
}
 
133
 
 
134
/**
 
135
 * Reallocates a buffer via realloc with reporting memory error to stderr.
85
136
 *
86
137
 * @param mem a memory block to re-allocate
 
138
 * @param size the new size of the block
87
139
 * @param srcfile source file to report error on fail
88
140
 * @param srcline source code line to be reported on fail
89
141
 * @return re-allocated memory buffer
103
155
/**
104
156
 * Allocate an empty vector.
105
157
 *
106
 
 * @param destructor pointer to the cleanup/deallocate function called 
 
158
 * @param destructor pointer to the cleanup/deallocate function called
107
159
 *                   on each element when the vector is destructed
108
160
 * @return allocated vector
109
161
 */
130
182
 * Release memory allocated by vector, but the vector structure itself.
131
183
 *
132
184
 * @param vect the vector to free
133
 
 * @param destructor function to free vector items, 
 
185
 * @param destructor function to free vector items,
134
186
 *                   NULL if items doesn't need to be freed
135
187
 */
136
188
void rsh_vector_destroy(vector_t* vect)
149
201
 * Release all memory allocated by vector.
150
202
 *
151
203
 * @param vect the vector to free
152
 
 * @param destructor function to free vector items, 
 
204
 * @param destructor function to free vector items,
153
205
 *                   NULL if items doesn't need to be freed
154
206
 */
155
207
void rsh_vector_free(vector_t* vect)
183
235
 * @param vect pointer to the vector to add item to
184
236
 * @param item the size of a vector item
185
237
 */
186
 
void rsh_vector_item_add_empty(struct vector_t* vect, size_t item_size)
 
238
void rsh_vector_add_empty(struct vector_t* vect, size_t item_size)
187
239
{
188
240
        /* check if vect contains enough space for next item */
189
241
        if(vect->size >= vect->allocated) {
263
315
 * Append a sequence of single-byte characters of the specified length to
264
316
 * string buffer. The array is fully copied even if it contains the '\0'
265
317
 * character. The function ensures the string buffer still contains
266
 
 * null-termited string.
 
318
 * null-terminated string.
267
319
 *
268
320
 * @param str pointer to the string buffer
269
321
 * @param text the text to append