Skip to content

Commit c33848b

Browse files
committed
PR comments - #1
1 parent 8f52f5e commit c33848b

File tree

7 files changed

+340
-301
lines changed

7 files changed

+340
-301
lines changed

src/rd.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,80 @@ typedef struct rd_chariov_s {
438438
size_t size;
439439
} rd_chariov_t;
440440

441+
/**
442+
* @brief Read a file in binary mode and return its contents.
443+
* The returned buffer is NULL-terminated if the file is text,
444+
* but the size parameter will contain the actual file size.
445+
*
446+
* @param file_path Path to the file to read
447+
* @param size Pointer to store the file size (excluding NULL terminator if present)
448+
* @param max_size Optional maximum file size to read (0 for no limit)
449+
*
450+
* @returns Newly allocated buffer containing the file contents.
451+
* NULL on error (file not found, too large, etc).
452+
* Caller must free with rd_free().
453+
*
454+
* @locality Any thread
455+
*/
456+
static RD_INLINE RD_UNUSED void *rd_read_file(const char *file_path,
457+
size_t *size,
458+
size_t max_size) {
459+
FILE *file;
460+
void *buf;
461+
long file_size;
462+
size_t read_size;
463+
464+
if (!file_path || !size) {
465+
return NULL;
466+
}
467+
468+
file = fopen(file_path, "rb");
469+
if (!file) {
470+
return NULL;
471+
}
472+
473+
if (fseek(file, 0, SEEK_END) != 0) {
474+
fclose(file);
475+
return NULL;
476+
}
477+
478+
file_size = ftell(file);
479+
if (file_size < 0) {
480+
fclose(file);
481+
return NULL;
482+
}
483+
484+
if (fseek(file, 0, SEEK_SET) != 0) {
485+
fclose(file);
486+
return NULL;
487+
}
488+
489+
/* Check if file is too large */
490+
if (max_size > 0 && (size_t)file_size > max_size) {
491+
fclose(file);
492+
return NULL;
493+
}
494+
495+
/* Allocate buffer with extra byte for NULL terminator */
496+
buf = rd_malloc(file_size + 1);
497+
if (!buf) {
498+
fclose(file);
499+
return NULL;
500+
}
501+
502+
read_size = fread(buf, 1, file_size, file);
503+
fclose(file);
504+
505+
if (read_size != (size_t)file_size) {
506+
rd_free(buf);
507+
return NULL;
508+
}
509+
510+
/* NULL terminate the buffer */
511+
((char *)buf)[file_size] = '\0';
512+
*size = file_size;
513+
514+
return buf;
515+
}
516+
441517
#endif /* _RD_H_ */

src/rdbase64.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,36 @@ char *rd_base64_encode_str(const rd_chariov_t *in) {
119119
return out.ptr;
120120
}
121121

122+
/**
123+
* @brief Base64 encode binary input \p in and return a newly allocated,
124+
* base64-encoded string with URL-safe characters.
125+
* @returns a newly allocated, base64-encoded string or NULL in case of some
126+
* issue with the conversion or the conversion is not supported.
127+
*
128+
* @remark Returned string must be freed after use.
129+
*/
130+
char *rd_base64_encode_str_urlsafe(const rd_chariov_t *in) {
131+
rd_chariov_t out;
132+
rd_base64_encode(in, &out);
133+
134+
/* Replace + with - and / with _ */
135+
for (char *p = out.ptr; *p; p++) {
136+
if (*p == '+')
137+
*p = '-';
138+
else if (*p == '/')
139+
*p = '_';
140+
}
141+
142+
/* Remove padding '=' characters */
143+
int newlen = strlen(out.ptr);
144+
while (newlen > 0 && out.ptr[newlen - 1] == '=') {
145+
out.ptr[newlen - 1] = '\0';
146+
newlen--;
147+
}
148+
149+
out.size = newlen;
150+
return out.ptr;
151+
}
122152

123153
/**
124154
* @brief Base64 decode input string \p in. Ignores leading and trailing

src/rdbase64.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ void rd_base64_encode(const rd_chariov_t *in, rd_chariov_t *out);
3636

3737
char *rd_base64_encode_str(const rd_chariov_t *in);
3838

39+
char *rd_base64_encode_str_urlsafe(const rd_chariov_t *in);
40+
3941
int rd_base64_decode(const rd_chariov_t *in, rd_chariov_t *out);
4042

4143
#endif /* _RDBASE64_H_ */

src/rdkafka.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2434,7 +2434,7 @@ rd_kafka_t *rd_kafka_new(rd_kafka_type_t type,
24342434
!rk->rk_conf.sasl.oauthbearer.token_refresh_cb) {
24352435
/* Use JWT bearer */
24362436
if (rk->rk_conf.sasl.oauthbearer.grant_type ==
2437-
RD_KAFKA_SASL_OAUTHBEARER_GRANT_TYPE_OIDC) {
2437+
RD_KAFKA_SASL_OAUTHBEARER_GRANT_TYPE_CLIENT_CREDENTIALS) {
24382438
rd_kafka_conf_set_oauthbearer_token_refresh_cb(
24392439
&rk->rk_conf, rd_kafka_oidc_token_refresh_cb);
24402440
} else {

0 commit comments

Comments
 (0)