diff --git a/pclsync/papi.c b/pclsync/papi.c index c70d5dc..8b3277c 100644 --- a/pclsync/papi.c +++ b/pclsync/papi.c @@ -378,8 +378,8 @@ static binresult *parse_result(unsigned char *data, size_t datalen) { retlen = calc_ret_len(&datac, &datalenc, &strcnt); if (retlen == -1) return NULL; - datac = psync_new_cnt(unsigned char, retlen); - strings = psync_new_cnt(binresult *, strcnt); + datac = malloc(sizeof(unsigned char) * retlen); + strings = malloc(sizeof(binresult *) * strcnt); strcnt = 0; res = do_parse_result(&data, &datac, strings, &strcnt); free(strings); diff --git a/pclsync/pcrypto.c b/pclsync/pcrypto.c index 819363c..c226c5e 100644 --- a/pclsync/pcrypto.c +++ b/pclsync/pcrypto.c @@ -182,7 +182,7 @@ pcrypto_ctr_encdec_t pcrypto_ctr_encdec_create(psync_symmetric_key_t key) { if (pdbg_unlikely(enc == PSYNC_INVALID_ENCODER)) { return PSYNC_CRYPTO_INVALID_ENCODER; } - ret = psync_new(pcrypto_key_t); + ret = malloc(sizeof(pcrypto_key_t)); ret->encoder = enc; memcpy(ret->iv, key->key + PSYNC_AES256_KEY_SIZE, PSYNC_AES256_BLOCK_SIZE); return ret; @@ -286,7 +286,7 @@ void pcrypto_encode_text(pcrypto_textenc_t enc, aesdst = aessrc + PSYNC_AES256_BLOCK_SIZE; hmac = aessrc + PSYNC_AES256_BLOCK_SIZE * 2; ol = ALIGN_A256_BS(txtlen); - outptr = psync_new_cnt(unsigned char, ol); + outptr = malloc(sizeof(unsigned char) * ol); *out = outptr; *outlen = ol; if (txtlen <= PSYNC_AES256_BLOCK_SIZE) { @@ -327,7 +327,7 @@ pcrypto_decode_text(pcrypto_textdec_t enc, return NULL; aessrc = ALIGN_PTR_A256_BS(buff); aesdst = aessrc + PSYNC_AES256_BLOCK_SIZE; - outptr = psync_new_cnt(unsigned char, datalen + 1); + outptr = malloc(sizeof(unsigned char) * (datalen + 1)); ret = outptr; datalen /= PSYNC_AES256_BLOCK_SIZE; if (datalen == 1) { diff --git a/pclsync/pcryptofolder.c b/pclsync/pcryptofolder.c index 2ee97c0..be7d7cc 100644 --- a/pclsync/pcryptofolder.c +++ b/pclsync/pcryptofolder.c @@ -795,7 +795,7 @@ static void save_folder_key(psync_folderid_t folderid, // we are likely holding (few) read locks on the database, so executing here // will deadlock insert_folder_key_task *t; - t = psync_new(insert_folder_key_task); + t = malloc(sizeof(insert_folder_key_task)); t->key = psymkey_copy_encrypted(enckey); t->id = folderid; prun_thread1("save folder key to db task", ptask_cfldr_save_fldrkey, t); @@ -804,7 +804,7 @@ static void save_folder_key(psync_folderid_t folderid, static void save_file_key_to_db(psync_fileid_t fileid, uint64_t hash, psync_encrypted_symmetric_key_t enckey) { insert_file_key_task *t; - t = psync_new(insert_file_key_task); + t = malloc(sizeof(insert_file_key_task)); t->key = psymkey_copy_encrypted(enckey); t->id = fileid; t->hash = hash; diff --git a/pclsync/pdeflate.c b/pclsync/pdeflate.c index b9eb0c1..7266938 100644 --- a/pclsync/pdeflate.c +++ b/pclsync/pdeflate.c @@ -55,7 +55,7 @@ struct _psync_deflate_t { psync_deflate_t *pdeflate_init(int level) { psync_deflate_t *def; int ret; - def = psync_new(psync_deflate_t); + def = malloc(sizeof(psync_deflate_t)); memset(&def->stream, 0, sizeof(def->stream)); def->flushbuff = NULL; def->bufferstartoff = 0; @@ -137,7 +137,7 @@ static int psync_deflate_finish_flush_add_buffer(psync_deflate_t *def, uint32_t alloced, used, current; int ret; alloced = 4096; - buff = psync_new_cnt(unsigned char, alloced); + buff = malloc(sizeof(unsigned char) * alloced); current = alloced; used = 0; def->flags &= ~FLAG_MORE_DATA; diff --git a/pclsync/pdiff.c b/pclsync/pdiff.c index b7a31a1..b31eed5 100644 --- a/pclsync/pdiff.c +++ b/pclsync/pdiff.c @@ -170,7 +170,7 @@ static void do_send_eventdata(void *param); static void psync_notify_cache_change(psync_changetype_t event) { paccount_cache_callback_t callback; - psync_changetype_t *chtype = psync_new(psync_changetype_t); + psync_changetype_t *chtype = malloc(sizeof(psync_changetype_t)); *chtype = event; callback = psync_cache_callback; if (callback) @@ -229,7 +229,7 @@ static binresult *get_userinfo_user_pass(psock_t *sock, const char *username, pdbg_logf(D_NOTICE, "got digest %s", dig->str); ul = strlen(username); - uc = psync_new_cnt(unsigned char, ul); + uc = malloc(sizeof(unsigned char) * ul); for (i = 0; i < ul; i++) uc[i] = tolower(username[i]); @@ -2758,7 +2758,7 @@ static void psync_diff_refresh_fs(const binresult *entries) { } if (!refresh_last) return; - ptr = psync_new(refresh_folders_ptr_t); + ptr = malloc(sizeof(refresh_folders_ptr_t)); ptr->refresh_folders = refresh_folders; ptr->refresh_last = refresh_last; prun_thread1("fs folder refresh", psync_diff_refresh_thread, ptr); @@ -2787,7 +2787,7 @@ static void psync_run_analyze_if_needed() { else tablecnt = 0; psync_sql_free_result(res); - tablenames = psync_new_cnt(char *, tablecnt); + tablenames = malloc(sizeof(char *) * tablecnt); res = psync_sql_query_rdlock( "SELECT name FROM sqlite_master WHERE type='table' LIMIT ?"); psync_sql_bind_uint(res, 1, tablecnt); diff --git a/pclsync/pfile.c b/pclsync/pfile.c index 43c7712..3025ca1 100644 --- a/pclsync/pfile.c +++ b/pclsync/pfile.c @@ -275,7 +275,7 @@ int pfile_preread(int fd, uint64_t offset, size_t count) { cfd = pfile_dup(fd); if (cfd == INVALID_HANDLE_VALUE) return -1; - pr = psync_new(psync_file_preread_t); + pr = malloc(sizeof(psync_file_preread_t)); pr->offset = offset; pr->count = count; pr->fd = cfd; diff --git a/pclsync/pfolder.c b/pclsync/pfolder.c index 3c8473c..15d5ab4 100644 --- a/pclsync/pfolder.c +++ b/pclsync/pfolder.c @@ -729,7 +729,7 @@ pentry_t *pfolder_stat(const char *remotepath) { if (remotepath[0] != '/') return NULL; if (remotepath[1] == 0) { - ret = psync_new(pentry_t); + ret = malloc(sizeof(pentry_t)); ret->name = "/"; ret->namelen = 1; ret->isfolder = 1; @@ -744,7 +744,7 @@ pentry_t *pfolder_stat(const char *remotepath) { if (len == 0) folderid = 0; else { - cremotepath = psync_new_cnt(char, len + 1); + cremotepath = malloc(sizeof(char) * (len + 1)); memcpy(cremotepath, remotepath, len + 1); cremotepath[len] = 0; folderid = pfolder_id(cremotepath); diff --git a/pclsync/pfs.c b/pclsync/pfs.c index add4b18..8c8644b 100644 --- a/pclsync/pfs.c +++ b/pclsync/pfs.c @@ -1020,7 +1020,7 @@ psync_fs_create_file(psync_fsfileid_t fileid, psync_fsfileid_t remotefileid, fl = (psync_openfile_t *)malloc(offsetof(psync_openfile_t, encoder)); memset(fl, 0, offsetof(psync_openfile_t, encoder)); } else { - fl = psync_new(psync_openfile_t); + fl = malloc(sizeof(psync_openfile_t)); memset(fl, 0, sizeof(psync_openfile_t)); size = pfscrypto_plain_size(size); } @@ -1846,7 +1846,7 @@ static void psync_fs_write_timer(psync_timer_t timer, void *ptr) { goto unlock_ex; } of->releasedforupload = 1; - ofw = psync_new(psync_openfile_writeid_t); + ofw = malloc(sizeof(psync_openfile_writeid_t)); ofw->of = of; ofw->writeid = of->writeid; pthread_mutex_unlock(&of->mutex); diff --git a/pclsync/pfscrypto.c b/pclsync/pfscrypto.c index 12384ca..0e42ce3 100644 --- a/pclsync/pfscrypto.c +++ b/pclsync/pfscrypto.c @@ -573,7 +573,7 @@ psync_fs_crypto_set_sector_log_offset(psync_openfile_t *of, return; } } - ntr = psync_new(psync_sector_inlog_t); + ntr = malloc(sizeof(psync_sector_inlog_t)); *pe = &ntr->tree; ntr->sectorid = sectorid; ntr->logoffset = offset; @@ -1605,8 +1605,8 @@ retry: if (!icnt) psync_interval_tree_free(needtodwl); else { - ranges = psync_new_cnt(psync_pagecache_read_range, icnt); - tmpbuf = psync_new_cnt(char, isize); + ranges = malloc(sizeof(psync_pagecache_read_range) * icnt); + tmpbuf = malloc(sizeof(char) * isize); icnt = 0; isize = 0; itr = psync_interval_tree_get_first(needtodwl); @@ -1881,7 +1881,7 @@ static int psync_fs_crypto_run_extender(psync_openfile_t *of, uint64_t size) { pdbg_assert(!of->extender); pdbg_logf(D_NOTICE, "will run extender thread to extend from %lu to %lu", (unsigned long)of->currentsize, (unsigned long)size); - ext = psync_new(psync_enc_file_extender_t); + ext = malloc(sizeof(psync_enc_file_extender_t)); pthread_cond_init(&ext->cond, NULL); ext->extendto = size; ext->extendedto = of->currentsize; diff --git a/pclsync/pfsfolder.c b/pclsync/pfsfolder.c index 484cbfe..441019a 100644 --- a/pclsync/pfsfolder.c +++ b/pclsync/pfsfolder.c @@ -97,7 +97,7 @@ static psync_fspath_t *ret_folder_data(psync_fsfolderid_t folderid, ret->permissions = permissions; ret->flags = flags; } else { - ret = psync_new(psync_fspath_t); + ret = malloc(sizeof(psync_fspath_t)); ret->folderid = folderid; ret->name = name; ret->shareid = shareid; diff --git a/pclsync/pfstasks.c b/pclsync/pfstasks.c index 91a2d68..af3ed42 100644 --- a/pclsync/pfstasks.c +++ b/pclsync/pfstasks.c @@ -127,7 +127,7 @@ psync_fstask_get_or_create_folder_tasks_locked(psync_fsfolderid_t folderid) { } } - folder = psync_new(psync_fstask_folder_t); + folder = malloc(sizeof(psync_fstask_folder_t)); memset(folder, 0, sizeof(psync_fstask_folder_t)); if (d < 0) diff --git a/pclsync/pfsupload.c b/pclsync/pfsupload.c index ae6883c..8c6f95c 100644 --- a/pclsync/pfsupload.c +++ b/pclsync/pfsupload.c @@ -1170,7 +1170,7 @@ static void large_upload() { else { cname = psync_get_lstring(row[4], &len); len++; - key = psync_new_cnt(char, len); + key = malloc(sizeof(char) * len); memcpy(key, cname, len); } cname = psync_get_lstring(row[3], &len); @@ -1178,7 +1178,7 @@ static void large_upload() { fileid = psync_get_number(row[6]); hash = psync_get_number_or_null(row[7]); len++; - name = psync_new_cnt(char, len); + name = malloc(sizeof(char) * len); memcpy(name, cname, len); current_upload_taskid = taskid; stop_current_upload = 0; @@ -1598,7 +1598,7 @@ static void change_folder_name(fsupload_task_t *task) { while (et > task->text1 + 2 && isdigit(et[0])) et--; if (et > task->text1 && et[0] == '(' && atol(et + 1) < 20) { - nn = psync_new_cnt(char, et - task->text1 + 7); + nn = malloc(sizeof(char) * (et - task->text1 + 7)); memcpy(nn, task->text1, et - task->text1); psync_slprintf(nn + (et - task->text1), 7, " (%d)", atoi(et + 1) + 1); } diff --git a/pclsync/pintervaltree.c b/pclsync/pintervaltree.c index 2860366..d043eaf 100644 --- a/pclsync/pintervaltree.c +++ b/pclsync/pintervaltree.c @@ -34,7 +34,7 @@ static psync_interval_tree_t *psync_interval_tree_new(uint64_t from, uint64_t to) { - psync_interval_tree_t *tree = psync_new(psync_interval_tree_t); + psync_interval_tree_t *tree = malloc(sizeof(psync_interval_tree_t)); tree->from = from; tree->to = to; return psync_interval_tree_element( @@ -42,7 +42,7 @@ static psync_interval_tree_t *psync_interval_tree_new(uint64_t from, } static psync_interval_tree_t *psync_interval_new(uint64_t from, uint64_t to) { - psync_interval_tree_t *tree = psync_new(psync_interval_tree_t); + psync_interval_tree_t *tree = malloc(sizeof(psync_interval_tree_t)); tree->from = from; tree->to = to; return tree; diff --git a/pclsync/plibs.c b/pclsync/plibs.c index c81e73c..b8dda12 100644 --- a/pclsync/plibs.c +++ b/pclsync/plibs.c @@ -85,31 +85,7 @@ static const uint8_t __hex_lookupl[513] = {"000102030405060708090a0b0c0d0e0f" "e0e1e2e3e4e5e6e7e8e9eaebecedeeef" "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"}; -const char base64_table[] = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'}; - -static const char base64_reverse_table[256] = { - -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2, -2, -2, -2, - -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -2, -2, -2, - -2, -2, -2, -2, -2, 62, -2, 62, -2, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, - 61, -2, -2, -2, -1, -2, -2, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, - 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, - 63, -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, - 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, - -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, - -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, - -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, - -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, - -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, - -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, - -2, -2, -2, -2, -2, -2, -2, -2, -2}; - uint16_t const *__hex_lookup = (uint16_t *)__hex_lookupl; -static char normalize_table[256]; const static char *psync_typenames[] = { "[invalid type]", "[number]", "[string]", "[float]", "[null]", "[bool]"}; @@ -135,244 +111,6 @@ static int in_transaction = 0; static int transaction_failed = 0; static psync_list tran_callbacks; -char *psync_strdup(const char *str) { - size_t len; - len = strlen(str) + 1; - return (char *)memcpy(psync_new_cnt(char, len), str, len); -} - -char *psync_strnormalize_filename(const char *str) { - size_t len, i; - char *ptr; - len = strlen(str) + 1; - ptr = psync_new_cnt(char, len); - for (i = 0; i < len; i++) - ptr[i] = normalize_table[(unsigned char)str[i]]; - return ptr; -} - -char *psync_strndup(const char *str, size_t len) { - char *ptr; - ptr = (char *)memcpy(psync_new_cnt(char, len + 1), str, len); - ptr[len] = 0; - return ptr; -} - -char *psync_strcat(const char *str, ...) { - size_t i, size, len; - const char *strs[64]; - size_t lengths[64]; - const char *ptr; - char *ptr2, *ptr3; - va_list ap; - va_start(ap, str); - strs[0] = str; - len = strlen(str); - lengths[0] = len; - size = len + 1; - i = 1; - while ((ptr = va_arg(ap, const char *))) { - pdbg_assert(i < ARRAY_SIZE(strs)); - len = strlen(ptr); - lengths[i] = len; - strs[i++] = ptr; - size += len; - } - va_end(ap); - ptr2 = ptr3 = (char *)malloc(size); - for (size = 0; size < i; size++) { - memcpy(ptr2, strs[size], lengths[size]); - ptr2 += lengths[size]; - } - *ptr2 = 0; - return ptr3; -} - -int psync_slprintf(char *str, size_t size, const char *format, ...) { - va_list ap; - int ret; - va_start(ap, format); - ret = vsnprintf(str, size, format, ap); - va_end(ap); - if (pdbg_unlikely(ret >= size)) - str[size - 1] = 0; - return ret; -} - -unsigned char *psync_base32_encode(const unsigned char *str, size_t length, - size_t *ret_length) { - static const unsigned char *table = - (const unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; - unsigned char *result; - unsigned char *p; - uint32_t bits, buff; - - result = (unsigned char *)malloc(((length + 4) / 5) * 8 + 1); - p = result; - - bits = 0; - buff = 0; // don't really have to initialize this one, but a compiler that - // will detect that this is safe is yet to be born - - while (length) { - if (bits < 5) { - buff = (buff << 8) | (*str++); - length--; - bits += 8; - } - bits -= 5; - *p++ = table[0x1f & (buff >> bits)]; - } - - while (bits) { - if (bits < 5) { - buff <<= (5 - bits); - bits = 5; - } - bits -= 5; - *p++ = table[0x1f & (buff >> bits)]; - } - - *ret_length = p - result; - *p = 0; - return result; -} - -unsigned char *psync_base32_decode(const unsigned char *str, size_t length, - size_t *ret_length) { - unsigned char *result, *p; - uint32_t bits, buff; - unsigned char ch; - result = (unsigned char *)malloc((length + 7) / 8 * 5 + 1); - p = result; - bits = 0; - buff = 0; - while (length) { - ch = *str++; - length--; - if (ch >= 'A' && ch <= 'Z') - ch = (ch & 0x1f) - 1; - else if (ch >= '2' && ch <= '7') - ch -= '2' - 26; - else { - free(result); - return NULL; - } - buff = (buff << 5) + ch; - bits += 5; - if (bits >= 8) { - bits -= 8; - *p++ = buff >> bits; - } - } - *p = 0; - *ret_length = p - result; - return result; -} - -unsigned char *psync_base64_encode(const unsigned char *str, size_t length, - size_t *ret_length) { - const unsigned char *current = str; - unsigned char *p; - unsigned char *result; - - result = (unsigned char *)malloc(((length + 2) / 3) * 4 + 1); - p = result; - - while (length > 2) { - *p++ = base64_table[current[0] >> 2]; - *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; - *p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)]; - *p++ = base64_table[current[2] & 0x3f]; - current += 3; - length -= 3; - } - - if (length != 0) { - *p++ = base64_table[current[0] >> 2]; - if (length > 1) { - *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; - *p++ = base64_table[(current[1] & 0x0f) << 2]; - } else - *p++ = base64_table[(current[0] & 0x03) << 4]; - } - - *ret_length = p - result; - *p = 0; - return result; -} - -unsigned char *psync_base64_decode(const unsigned char *str, size_t length, - size_t *ret_length) { - const unsigned char *current = str; - unsigned char *result; - size_t i = 0, j = 0; - ssize_t ch; - - result = (unsigned char *)malloc((length + 3) / 4 * 3 + 1); - - while (length-- > 0) { - ch = base64_reverse_table[*current++]; - if (ch == -1) - continue; - else if (ch == -2) { - free(result); - return NULL; - } - switch (i % 4) { - case 0: - result[j] = ch << 2; - break; - case 1: - result[j++] |= ch >> 4; - result[j] = (ch & 0x0f) << 4; - break; - case 2: - result[j++] |= ch >> 2; - result[j] = (ch & 0x03) << 6; - break; - case 3: - result[j++] |= ch; - break; - } - i++; - } - *ret_length = j; - result[j] = 0; - return result; -} - -int psync_is_valid_utf8(const char *str) { - static const int8_t trailing[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, -1, -1, -1, -1, - -1, -1, -1, -1}; - int8_t t; - while (*str) { - t = trailing[(unsigned char)*str++]; - if (unlikely(t)) { - if (t < 0) - return 0; - while (t--) - if ((((unsigned char)*str++) & 0xc0) != 0x80) - return 0; - } - } - return 1; -} - void psync_sql_err_callback(void *ptr, int code, const char *msg) { pdbg_logf(D_WARNING, "database warning %d: %s", code, msg); } @@ -589,7 +327,7 @@ static void record_wrunlock() { static void record_rdlock(const char *file, unsigned line, struct timespec *tm) { rd_lock_data *lock; - lock = psync_new(rd_lock_data); + lock = malloc(sizeof(rd_lock_data)); lock->file = file; lock->thread = psync_thread_name; lock->line = line; @@ -611,7 +349,6 @@ static rd_lock_data *record_rdunlock() { return lock; } - void psync_sql_dump_locks() { rd_lock_data *lock; char dttime[36]; @@ -901,7 +638,7 @@ void psync_sql_transation_add_callbacks( psync_transaction_callback_t rollback_callback, void *ptr) { tran_callback_t *cb; pdbg_assert(in_transaction); - cb = psync_new(tran_callback_t); + cb = malloc(sizeof(tran_callback_t)); cb->commit_callback = commit_callback; cb->rollback_callback = rollback_callback; cb->ptr = ptr; @@ -939,7 +676,7 @@ static void psync_sql_do_check_query_plan(const char *sql) { static psync_tree *psync_sql_new_tree_node(const char *sql) { psync_sql_tree_t *node; - node = psync_new(psync_sql_tree_t); + node = malloc(sizeof(psync_sql_tree_t)); node->sql = sql; return &node->tree; } @@ -1467,7 +1204,7 @@ psync_sql_res *psync_sql_prep_statement_nocache(const char *sql) { #endif return NULL; } - res = psync_new(psync_sql_res); + res = malloc(sizeof(psync_sql_res)); res->stmt = stmt; res->sql = sql; #if IS_DEBUG @@ -1750,15 +1487,6 @@ int psync_rename_conflicted_file(const char *path) { } } -void psync_libs_init() { - unsigned long i; - for (i = 0; i < 256; i++) - normalize_table[i] = i; - normalize_table[':'] = '_'; - normalize_table['/'] = '_'; - normalize_table['\\'] = '_'; -} - static void run_after_sec(psync_timer_t timer, void *ptr) { struct run_after_ptr *fp = (struct run_after_ptr *)ptr; ptimer_stop(timer); @@ -1768,19 +1496,19 @@ static void run_after_sec(psync_timer_t timer, void *ptr) { void psync_run_after_sec(psync_run_after_t run, void *ptr, uint32_t seconds) { struct run_after_ptr *fp; - fp = psync_new(struct run_after_ptr); + fp = malloc(sizeof(struct run_after_ptr)); fp->run = run; fp->ptr = ptr; ptimer_register(run_after_sec, seconds, fp); } -static void free_after_sec(psync_timer_t timer, void *ptr) { +static void proc_free_after(psync_timer_t timer, void *ptr) { ptimer_stop(timer); free(ptr); } void psync_free_after_sec(void *ptr, uint32_t seconds) { - ptimer_register(free_after_sec, seconds, ptr); + ptimer_register(proc_free_after, seconds, ptr); } int psync_match_pattern(const char *name, const char *pattern, size_t plen) { @@ -1816,20 +1544,6 @@ int psync_match_pattern(const char *name, const char *pattern, size_t plen) { return name[i] == 0; } -uint64_t psync_ato64(const char *str) { - uint64_t n = 0; - while (*str >= '0' && *str <= '9') - n = n * 10 + (*str++) - '0'; - return n; -} - -uint32_t psync_ato32(const char *str) { - uint32_t n = 0; - while (*str >= '0' && *str <= '9') - n = n * 10 + (*str++) - '0'; - return n; -} - typedef struct { psync_list list; unsigned long used; @@ -1868,7 +1582,7 @@ struct psync_list_builder_t_ { psync_list_builder_t *psync_list_builder_create(size_t element_size, size_t offset) { psync_list_builder_t *builder; - builder = psync_new(psync_list_builder_t); + builder = malloc(sizeof(psync_list_builder_t)); builder->element_size = element_size; builder->elements_offset = offset; if (element_size <= 200) @@ -1890,7 +1604,7 @@ static uint32_t *psync_list_bulder_push_num(psync_list_builder_t *builder) { if (!builder->last_numbers || builder->last_numbers->used >= sizeof(builder->last_numbers->numbers) / sizeof(uint32_t)) { - psync_list_num_list *l = psync_new(psync_list_num_list); + psync_list_num_list *l = malloc(sizeof(psync_list_num_list)); l->used = 0; builder->last_numbers = l; psync_list_add_tail(&builder->number_list, &l->list); @@ -2008,7 +1722,7 @@ void *psync_list_builder_finalize(psync_list_builder_t *builder) { builder->stringalloc; pdbg_logf(D_NOTICE, "allocating %lu bytes, %lu of which for strings", (unsigned long)sz, (unsigned long)builder->stringalloc); - ret = psync_new_cnt(char, sz); + ret = malloc(sizeof(char) * sz); if (builder->elements_offset <= sizeof(builder->cnt)) memcpy(ret, &builder->cnt, builder->elements_offset); else @@ -2199,7 +1913,6 @@ int psync_task_complete(void *h, void *data) { return ret; } -#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) static uint32_t pq_rnd() { static uint32_t a = 0x95ae3d25, b = 0xe225d755, c = 0xc63a2ae7, d = 0xe4556265; diff --git a/pclsync/plibs.h b/pclsync/plibs.h index b215f0a..fd3c159 100644 --- a/pclsync/plibs.h +++ b/pclsync/plibs.h @@ -129,9 +129,6 @@ extern "C" { } while (0) #endif -#define psync_new(type) (type *)malloc(sizeof(type)) -#define psync_new_cnt(type, cnt) (type *)malloc(sizeof(type) * (cnt)) - #define psync_binhex(dst, src, cnt) \ do { \ size_t it__; \ @@ -201,27 +198,6 @@ extern uint64_t psync_my_userid; extern pthread_mutex_t psync_my_auth_mutex; extern PSYNC_THREAD uint32_t psync_error; extern uint16_t const *__hex_lookup; -extern const char base64_table[]; - -char *psync_strdup(const char *str) PSYNC_MALLOC PSYNC_NONNULL(1); -char *psync_strnormalize_filename(const char *str) PSYNC_MALLOC - PSYNC_NONNULL(1); -char *psync_strndup(const char *str, size_t len) PSYNC_MALLOC PSYNC_NONNULL(1); -char *psync_strcat(const char *str, ...) PSYNC_MALLOC PSYNC_SENTINEL; -int psync_slprintf(char *str, size_t size, const char *format, ...) - PSYNC_NONNULL(1, 3); - -unsigned char *psync_base32_encode(const unsigned char *str, size_t length, - size_t *ret_length); -unsigned char *psync_base32_decode(const unsigned char *str, size_t length, - size_t *ret_length); - -unsigned char *psync_base64_encode(const unsigned char *str, size_t length, - size_t *ret_length); -unsigned char *psync_base64_decode(const unsigned char *str, size_t length, - size_t *ret_length); - -int psync_is_valid_utf8(const char *str); int psync_sql_connect(const char *db) PSYNC_NONNULL(1); int psync_sql_close(); @@ -317,15 +293,11 @@ uint64_t psync_sql_insertid() PSYNC_PURE; int psync_rename_conflicted_file(const char *path); -void psync_libs_init(); void psync_run_after_sec(psync_run_after_t run, void *ptr, uint32_t seconds); void psync_free_after_sec(void *ptr, uint32_t seconds); int psync_match_pattern(const char *name, const char *pattern, size_t plen); -uint64_t psync_ato64(const char *str); -uint32_t psync_ato32(const char *str); - psync_list_builder_t *psync_list_builder_create(size_t element_size, size_t offset); void psync_list_bulder_add_sql(psync_list_builder_t *builder, @@ -395,20 +367,6 @@ static inline void psync_get_string_id2(char *dst, const char *prefix, *dst = 0; } -static inline size_t psync_strlcpy(char *dst, const char *src, size_t size) { - size_t len; - len = strlen(src); - if (pdbg_likely(len < size)) { - memcpy(dst, src, len + 1); - return len; - } else if (pdbg_likely(size)) { - memcpy(dst, src, size - 1); - dst[size - 1] = 0; - return size - 1; - } else - return 0; -} - #ifdef __cplusplus } #endif diff --git a/pclsync/pnetlibs.c b/pclsync/pnetlibs.c index b1d54c9..ba14238 100644 --- a/pclsync/pnetlibs.c +++ b/pclsync/pnetlibs.c @@ -1742,7 +1742,7 @@ static psync_block_checksum **psync_net_get_sorted_checksums(psync_file_checksums *checksums){ psync_block_checksum **ret; uint32_t i; - ret=psync_new_cnt(psync_block_checksum *, checksums->blockcnt); + ret=malloc(sizeof(psync_block_checksum *) * checksums->blockcnt); for (i=0; iblockcnt; i++) ret[i]=&checksums->blocks[i]; qsort(ret, checksums->blockcnt, sizeof(psync_block_checksum *), @@ -2092,12 +2092,12 @@ int psync_net_download_ranges(psync_list *ranges, psync_fileid_t fileid, return PSYNC_NET_TEMPFAIL; } hash = psync_net_create_hash(checksums); - blockactions = psync_new_cnt(psync_block_action, checksums->blockcnt); + blockactions = malloc(sizeof(psync_block_action) * checksums->blockcnt); memset(blockactions, 0, sizeof(psync_block_action) * checksums->blockcnt); for (i = 0; i < filecnt; i++) psync_net_check_file_for_blocks(files[i], checksums, hash, blockactions, i); free(hash); - range = psync_new(psync_range_list_t); + range = malloc(sizeof(psync_range_list_t)); range->len = checksums->blocksize; range->type = blockactions[0].type; if (range->type == PSYNC_RANGE_COPY) { @@ -2117,7 +2117,7 @@ int psync_net_download_ranges(psync_list *ranges, psync_fileid_t fileid, (range->type == PSYNC_RANGE_COPY && (range->filename != files[blockactions[i].idx] || range->off + range->len != blockactions[i].off))) { - range = psync_new(psync_range_list_t); + range = malloc(sizeof(psync_range_list_t)); range->len = bs; range->type = blockactions[i].type; if (range->type == PSYNC_RANGE_COPY) { @@ -2133,7 +2133,7 @@ int psync_net_download_ranges(psync_list *ranges, psync_fileid_t fileid, free(blockactions); return PSYNC_NET_OK; fulldownload: - range = psync_new(psync_range_list_t); + range = malloc(sizeof(psync_range_list_t)); range->off = 0; range->len = filesize; range->type = PSYNC_RANGE_TRANSFER; @@ -2204,7 +2204,7 @@ static int check_range_for_blocks(psync_file_checksums *checksums, ur->uploadoffset + ur->len == off + buffoff + outbyteoff) ur->len += blen; else { - ur = psync_new(psync_upload_range_list_t); + ur = malloc(sizeof(psync_upload_range_list_t)); ur->uploadoffset = off + buffoff + outbyteoff; ur->off = (uint64_t)(blockidx - 1) * checksums->blocksize; ur->len = blen; @@ -2309,7 +2309,7 @@ static void merge_list_to_element(psync_upload_range_list_t *le, psync_list_add_after(&le->list, &ur->list); le->len -= ur->len; } else { - n = psync_new(psync_upload_range_list_t); + n = malloc(sizeof(psync_upload_range_list_t)); n->uploadoffset = n->off = ur->uploadoffset + ur->len; pdbg_assertw(le->len > ur->uploadoffset - le->uploadoffset + ur->len); n->len = le->len - (ur->uploadoffset - le->uploadoffset) - ur->len; diff --git a/pclsync/pp2p.c b/pclsync/pp2p.c index aae94e8..d77c55d 100644 --- a/pclsync/pp2p.c +++ b/pclsync/pp2p.c @@ -352,7 +352,7 @@ static void psync_p2p_tcphandler(void *ptr) { free(binpubrsa); goto err0; } - token = psync_new_cnt(char, packet.tokenlen); + token = malloc(sizeof(char) * packet.tokenlen); if (pdbg_unlikely(socket_read_all(sock, token, packet.tokenlen)) || pdbg_unlikely(!check_token(token, packet.tokenlen, binpubrsa->data, packet.keylen, hashhex))) { @@ -508,7 +508,7 @@ static void psync_p2p_thread() { else psys_sleep_milliseconds(1); } else if (sret == 1) { - inconn = psync_new(int); + inconn = malloc(sizeof(int)); *inconn = accept(tcpsock, NULL, NULL); if (pdbg_unlikely(*inconn == INVALID_SOCKET)) free(inconn); @@ -761,7 +761,7 @@ int pp2p_check_download(psync_fileid_t fileid, psync_binhex(pct1.genhash, hashbin, PSYNC_HASH_DIGEST_LEN); memcpy(pct1.computername, computername, PSYNC_HASH_DIGEST_HEXLEN); il = psock_list_adapters(); - sockets = psync_new_cnt(int, il->interfacecnt); + sockets = malloc(sizeof(int) * il->interfacecnt); FD_ZERO(&rfds); msock = 0; for (i = 0; i < il->interfacecnt; i++) { diff --git a/pclsync/ppagecache.c b/pclsync/ppagecache.c index 64dea7c..7ee03f0 100644 --- a/pclsync/ppagecache.c +++ b/pclsync/ppagecache.c @@ -482,7 +482,7 @@ static int wait_shared_api() { psock_t *capi; int ret; capi = sharedapi; - waiter = psync_new(shared_api_waiter_t); + waiter = malloc(sizeof(shared_api_waiter_t)); pthread_cond_init(&waiter->cond, NULL); waiter->api = NULL; psync_list_add_tail(&sharedapiwaiters, &waiter->list); @@ -696,7 +696,7 @@ static psync_urls_t *get_urls_for_request(psync_request_t *req) { pthread_mutex_unlock(&url_cache_mutex); return NULL; } - urls = psync_new(psync_urls_t); + urls = malloc(sizeof(psync_urls_t)); urls->hash = req->hash; urls->refcnt = 0; urls->status = 0; @@ -786,7 +786,7 @@ static unsigned char *has_pages_in_db(uint64_t hash, uint64_t pageid, uint32_t fcnt; if (unlikely(!pagecnt)) return NULL; - ret = psync_new_cnt(unsigned char, pagecnt); + ret = malloc(sizeof(unsigned char) * pagecnt); memset(ret, 0, pagecnt); fromid = 0; fcnt = 0; @@ -2284,7 +2284,7 @@ static void check_or_request_page(uint64_t fileid, uint64_t hash, break; } if (!found) { - pw = psync_new(psync_page_wait_t); + pw = malloc(sizeof(psync_page_wait_t)); psync_list_add_tail(&wait_page_hash[h], &pw->list); psync_list_init(&pw->waiters); pw->hash = hash; @@ -2297,7 +2297,7 @@ static void check_or_request_page(uint64_t fileid, uint64_t hash, if (range && range->offset + range->length == pageid * PSYNC_FS_PAGE_SIZE) range->length += PSYNC_FS_PAGE_SIZE; else { - range = psync_new(psync_request_range_t); + range = malloc(sizeof(psync_request_range_t)); psync_list_add_tail(ranges, &range->list); range->offset = pageid * PSYNC_FS_PAGE_SIZE; range->length = PSYNC_FS_PAGE_SIZE; @@ -2454,7 +2454,7 @@ static void psync_pagecache_read_unmodified_readahead( if (found) continue; // pdbg_logf(D_NOTICE, "read-aheading page %lu", first_page_id+i); - pw = psync_new(psync_page_wait_t); + pw = malloc(sizeof(psync_page_wait_t)); psync_list_add_tail(&wait_page_hash[h], &pw->list); psync_list_init(&pw->waiters); pw->hash = hash; @@ -2468,7 +2468,7 @@ static void psync_pagecache_read_unmodified_readahead( (first_page_id + i) * PSYNC_FS_PAGE_SIZE) range->length += PSYNC_FS_PAGE_SIZE; else { - range = psync_new(psync_request_range_t); + range = malloc(sizeof(psync_request_range_t)); psync_list_add_tail(ranges, &range->list); range->offset = (first_page_id + i) * PSYNC_FS_PAGE_SIZE; range->length = PSYNC_FS_PAGE_SIZE; @@ -2496,7 +2496,7 @@ add_page_waiter(psync_list *wait_list, psync_list *range_list, uint64_t hash, psync_page_wait_t *pw; psync_request_range_t *range; unsigned long h; - pwt = psync_new(psync_page_waiter_t); + pwt = malloc(sizeof(psync_page_waiter_t)); pthread_cond_init(&pwt->cond, NULL); pwt->buff = buff; pwt->pageidx = pageidx; @@ -2510,7 +2510,7 @@ add_page_waiter(psync_list *wait_list, psync_list *range_list, uint64_t hash, list) if (pw->hash == hash && pw->pageid == pageid) goto found; pdbg_logf(D_NOTICE, "page %lu not found", (unsigned long)pageid); - pw = psync_new(psync_page_wait_t); + pw = malloc(sizeof(psync_page_wait_t)); psync_list_add_tail(&wait_page_hash[h], &pw->list); psync_list_init(&pw->waiters); pw->hash = hash; @@ -2523,7 +2523,7 @@ add_page_waiter(psync_list *wait_list, psync_list *range_list, uint64_t hash, if (range && range->offset + range->length == pageid * PSYNC_FS_PAGE_SIZE) range->length += PSYNC_FS_PAGE_SIZE; else { - range = psync_new(psync_request_range_t); + range = malloc(sizeof(psync_request_range_t)); psync_list_add_tail(range_list, &range->list); range->offset = pageid * PSYNC_FS_PAGE_SIZE; range->length = PSYNC_FS_PAGE_SIZE; @@ -2575,7 +2575,7 @@ int ppagecache_read_unmod_locked(psync_openfile_t *of, char *buf, pagecnt = psize / PSYNC_FS_PAGE_SIZE; first_page_id = poffset / PSYNC_FS_PAGE_SIZE; psync_list_init(&waiting); - rq = psync_new(psync_request_t); + rq = malloc(sizeof(psync_request_t)); psync_list_init(&rq->ranges); lock_wait(hash); if (pagecnt > 1 && pagecnt <= sizeof(dbread) * 8 && psize == size) { @@ -2789,11 +2789,11 @@ int ppagecache_read_unmod_enc_locked(psync_openfile_t *of, psize = size_round_up_to_page(size + pageoff); pagecnt = psize / PSYNC_FS_PAGE_SIZE; first_page_id = poffset / PSYNC_FS_PAGE_SIZE; - rq = psync_new(psync_request_t); + rq = malloc(sizeof(psync_request_t)); psync_list_init(&rq->ranges); psync_list_init(&waiting); psync_list_init(&auth_pages); - dp = psync_new_cnt(psync_crypto_data_page, pagecnt); + dp = malloc(sizeof(psync_crypto_data_page) * pagecnt); memset(dp, 0, sizeof(psync_crypto_data_page) * pagecnt); ap = NULL; lock_wait(hash); @@ -2805,7 +2805,7 @@ int ppagecache_read_unmod_enc_locked(psync_openfile_t *of, } pfscrypto_get_auth_off(first_page_id + i, 0, &offsets, &aoffset, &asize, &aoff); - ap = psync_new(psync_crypto_auth_page); + ap = malloc(sizeof(psync_crypto_auth_page)); ap->waiter = NULL; ap->parent = NULL; ap->firstpageid = (first_page_id + i) / PSYNC_CRYPTO_HASH_TREE_SECTORS * @@ -2825,7 +2825,7 @@ int ppagecache_read_unmod_enc_locked(psync_openfile_t *of, for (l = 1; l <= offsets.treelevels; l++) { pfscrypto_get_auth_off(first_page_id + i, l, &offsets, &aoffset, &asize, &aoff); - cap = psync_new(psync_crypto_auth_page); + cap = malloc(sizeof(psync_crypto_auth_page)); cap->waiter = NULL; cap->parent = NULL; cap->firstpageid = 0; @@ -2858,7 +2858,7 @@ int ppagecache_read_unmod_enc_locked(psync_openfile_t *of, } else pbuff = buf + i * PSYNC_FS_PAGE_SIZE - pageoff; if (!pbuff) { - pbuff = psync_new_cnt(char, apsize); + pbuff = malloc(sizeof(char) * apsize); dp[i].freebuff = 1; } dp[i].buff = pbuff; @@ -3048,7 +3048,7 @@ int ppagecache_readv_locked(psync_openfile_t *of, } else needkey = 0; pthread_mutex_unlock(&of->mutex); - rq = psync_new(psync_request_t); + rq = malloc(sizeof(psync_request_t)); psync_list_init(&rq->ranges); psync_list_init(&waiting); for (i = 0; i < cnt; i++) { diff --git a/pclsync/ppassword.c b/pclsync/ppassword.c index 26fe9c9..3e2f855 100644 --- a/pclsync/ppassword.c +++ b/pclsync/ppassword.c @@ -326,8 +326,8 @@ uint64_t ppassword_score(const char *cpassword) { } if (!plen) return score; - lpwd = psync_new_cnt(unsigned char, plen); - ldpwd = psync_new_cnt(unsigned char, plen); + lpwd = malloc(sizeof(unsigned char) * plen); + ldpwd = malloc(sizeof(unsigned char) * plen); for (nlen = 0; nlen < plen; nlen++) { lpwd[nlen] = tolower(password[nlen]); if (lpwd[nlen] == '0') diff --git a/pclsync/ppathstatus.c b/pclsync/ppathstatus.c index 2ba1be5..84c1dab 100644 --- a/pclsync/ppathstatus.c +++ b/pclsync/ppathstatus.c @@ -284,7 +284,7 @@ static folder_tasks_t *get_folder_tasks(psync_folderid_t folderid, int create) { } } if (create) { - ft = psync_new(folder_tasks_t); + ft = malloc(sizeof(folder_tasks_t)); *atr = &ft->tree; ptree_added_at(&folder_tasks, tr, &ft->tree); ft->folderid = folderid; @@ -424,7 +424,7 @@ void ppathstatus_fldr_moved(psync_folderid_t folderid, ppathstatus_del_from_parent_cache(folderid); if (!get_folder_tasks(folderid, 0)) return; - mp = psync_new(folder_moved_params_t); + mp = malloc(sizeof(folder_moved_params_t)); mp->folderid = folderid; mp->old_parent_folderid = old_parent_folderid; mp->new_parent_folderid = new_parent_folderid; @@ -483,7 +483,7 @@ static sync_data_t *get_sync_data(psync_syncid_t syncid, int create) { return NULL; } else { size_t i; - sd = psync_new(sync_data_t); + sd = malloc(sizeof(sync_data_t)); sd->syncid = syncid; *atr = &sd->tree; ptree_added_at(&sync_data, tr, &sd->tree); @@ -529,7 +529,7 @@ get_sync_folder_tasks(sync_data_t *sd, psync_folderid_t folderid, int create) { } } if (create) { - ft = psync_new(folder_tasks_t); + ft = malloc(sizeof(folder_tasks_t)); *atr = &ft->tree; ptree_added_at(&sd->folder_tasks, tr, &ft->tree); ft->folderid = folderid; @@ -773,7 +773,7 @@ void ppathstatus_syncfldr_moved(psync_folderid_t folderid, sync_del_from_parent_cache(sd, folderid); if (!get_folder_tasks(folderid, 0)) return; - mp = psync_new(sync_folder_moved_params_t); + mp = malloc(sizeof(sync_folder_moved_params_t)); mp->folderid = folderid; mp->old_parent_folderid = old_parent_folderid; mp->new_parent_folderid = new_parent_folderid; diff --git a/pclsync/pqevent.c b/pclsync/pqevent.c index af895fd..f346cb4 100644 --- a/pclsync/pqevent.c +++ b/pclsync/pqevent.c @@ -416,7 +416,7 @@ void pqevent_queue_eventid(psync_eventtype_t eventid) { if (eventthreadrunning) { event_list_t *event; - event = psync_new(event_list_t); + event = malloc(sizeof(event_list_t)); event->data.ptr = NULL; event->event = eventid; event->freedata = 0; @@ -432,7 +432,7 @@ void pqevent_queue_event(psync_eventtype_t eventid, void *eventdata) { if (eventthreadrunning) { event_list_t *event; - event = psync_new(event_list_t); + event = malloc(sizeof(event_list_t)); event->data.ptr = eventdata; event->event = eventid; event->freedata = 1; diff --git a/pclsync/prun.c b/pclsync/prun.c index c9db002..38744e1 100644 --- a/pclsync/prun.c +++ b/pclsync/prun.c @@ -34,7 +34,7 @@ static void start_thread(const char *name, void *run, void *ptr) { pthread_t thread; pthread_attr_t attr; - data = psync_new(thread_data); + data = malloc(sizeof(thread_data)); data->run = run; data->ptr = ptr; data->name = name; diff --git a/pclsync/prunthrottled.c b/pclsync/prunthrottled.c index c20156a..e8f8492 100644 --- a/pclsync/prunthrottled.c +++ b/pclsync/prunthrottled.c @@ -112,7 +112,7 @@ void prun_throttled(const char *name, prun_throttle_cb call, node->scheduled = 1; } } else { - node = psync_new(psync_rr_tree_node); + node = malloc(sizeof(psync_rr_tree_node)); node->call = call; node->name = name; node->scheduled = 0; diff --git a/pclsync/psock.c b/pclsync/psock.c index a4c1db0..d71e1a7 100644 --- a/pclsync/psock.c +++ b/pclsync/psock.c @@ -481,7 +481,7 @@ psock_t *psock_connect(const char *host, int unsigned port, int ssl) { } else { sslc = NULL; } - ret = psync_new(psock_t); + ret = malloc(sizeof(psock_t)); ret->ssl = sslc; ret->buffer = NULL; ret->sock = sock; diff --git a/pclsync/pssl.c b/pclsync/pssl.c index 496140d..cc3049b 100644 --- a/pclsync/pssl.c +++ b/pclsync/pssl.c @@ -275,7 +275,7 @@ static void free_session(void *ptr) { static void save_session(ssl_connection_t *conn) { mbedtls_ssl_session *sess; - sess = psync_new(mbedtls_ssl_session); + sess = malloc(sizeof(mbedtls_ssl_session)); // mbedtls_ssl_get_session seems to copy all elements, instead of referencing // them, therefore it is thread safe to add session upon connect memset(sess, 0, sizeof(mbedtls_ssl_session)); @@ -477,7 +477,7 @@ void pssl_rand_strong(unsigned char *buf, int num) { psync_rsa_t pssl_gen_rsa(int bits) { mbedtls_rsa_context *ctx; - ctx = psync_new(mbedtls_rsa_context); + ctx = malloc(sizeof(mbedtls_rsa_context)); mbedtls_rsa_init(ctx); mbedtls_rsa_set_padding(ctx, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1); @@ -512,7 +512,7 @@ void prsa_free_public(psync_rsa_publickey_t key) { psync_rsa_privatekey_t prsa_get_private(psync_rsa_t rsa) { mbedtls_rsa_context *ctx; - ctx = psync_new(mbedtls_rsa_context); + ctx = malloc(sizeof(mbedtls_rsa_context)); mbedtls_rsa_init(ctx); mbedtls_rsa_set_padding(ctx, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1); @@ -584,7 +584,7 @@ psync_rsa_publickey_t prsa_load_public(const unsigned char *keydata, pdbg_logf(D_WARNING, "pk_parse_public_key failed with code %d (-0x%04x); resorting to " "mbedtls 1.x RSA fallback", ret, -ret); return PSYNC_INVALID_RSA; } - rsa = psync_new(mbedtls_rsa_context); + rsa = malloc(sizeof(mbedtls_rsa_context)); mbedtls_rsa_init(rsa); mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1); ret = mbedtls_rsa_copy(rsa, mbedtls_pk_rsa(ctx)); @@ -639,7 +639,7 @@ psync_rsa_privatekey_t prsa_load_private(const unsigned char *keydata, size_t ke ssl_pdbg_logf(D_WARNING, ret, "pk_parse_key failed"); return PSYNC_INVALID_RSA; } - rsa = psync_new(mbedtls_rsa_context); + rsa = malloc(sizeof(mbedtls_rsa_context)); mbedtls_rsa_init(rsa); mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA1); ret = mbedtls_rsa_copy(rsa, mbedtls_pk_rsa(ctx)); @@ -691,7 +691,7 @@ char *psymkey_derive(const char *username, mbedtls_md_context_t ctx; size_t userlen, i; userlen = strlen(username); - usercopy = psync_new_cnt(unsigned char, userlen); + usercopy = malloc(sizeof(unsigned char) * userlen); for (i = 0; i < userlen; i++) if ((unsigned char)username[i] <= 127) usercopy[i] = tolower((unsigned char)username[i]); @@ -767,7 +767,7 @@ psync_aes256_encoder paes_create_encoder(psync_symmetric_key_t key) { mbedtls_aes_context *aes; pdbg_assert(key->keylen >= PSYNC_AES256_KEY_SIZE); - aes = psync_new(mbedtls_aes_context); + aes = malloc(sizeof(mbedtls_aes_context)); mbedtls_aes_setkey_enc(aes, key->key, 256); return aes; } @@ -781,7 +781,7 @@ psync_aes256_encoder paes_create_decoder(psync_symmetric_key_t key) { mbedtls_aes_context *aes; pdbg_assert(key->keylen >= PSYNC_AES256_KEY_SIZE); - aes = psync_new(mbedtls_aes_context); + aes = malloc(sizeof(mbedtls_aes_context)); mbedtls_aes_setkey_dec(aes, key->key, 256); return aes; } diff --git a/pclsync/psuggest.c b/pclsync/psuggest.c index 32de8f0..4d4bd31 100644 --- a/pclsync/psuggest.c +++ b/pclsync/psuggest.c @@ -162,7 +162,7 @@ static void suggest_folders(scan_folder *f, psync_list *suggestions) { if (sum >= PSYNC_SCANNER_MIN_FILES && sum >= (f->filecnt[0] + sum) * PSYNC_SCANNER_PERCENT / 100) { // pdbg_logf(D_NOTICE, "suggesting %s sum %u", f->path, sum); - s = psync_new(suggested_folder); + s = malloc(sizeof(suggested_folder)); s->folder = f; s->filecnt = sum; psync_list_add_tail(suggestions, &s->list); @@ -199,7 +199,7 @@ psuggested_folders_t *psuggest_scan_folder(const char *path) { size_t descslen[PSYNC_SCANNER_MAX_SUGGESTIONS]; char buff[256]; uint32_t scnt[PSYNC_SCAN_TYPES_CNT][2]; - f = psync_new(scan_folder); + f = malloc(sizeof(scan_folder)); psync_list_init(&f->nextfolder); psync_list_init(&f->subfolders); f->path = path; diff --git a/pclsync/psyncer.c b/pclsync/psyncer.c index 832a17b..06a609a 100644 --- a/pclsync/psyncer.c +++ b/pclsync/psyncer.c @@ -64,7 +64,7 @@ static psync_tree *synced_down_folders = PSYNC_TREE_EMPTY; static pthread_mutex_t sync_down_mutex = PTHREAD_MUTEX_INITIALIZER; static psync_tree *psync_new_sd_folder(psync_folderid_t folderid) { - synced_down_folder *f = psync_new(synced_down_folder); + synced_down_folder *f = malloc(sizeof(synced_down_folder)); f->folderid = folderid; return &f->tree; } @@ -350,7 +350,7 @@ static void psync_do_sync_thread(void *ptr) { } void psyncer_create(psync_syncid_t syncid) { - psync_syncid_t *psid = psync_new(psync_syncid_t); + psync_syncid_t *psid = malloc(sizeof(psync_syncid_t)); *psid = syncid; prun_thread1("syncer", psync_do_sync_thread, psid); } diff --git a/pclsync/psynclib.c b/pclsync/psynclib.c index 30908cb..a725462 100644 --- a/pclsync/psynclib.c +++ b/pclsync/psynclib.c @@ -193,7 +193,6 @@ int psync_init() { return -1; } - psync_libs_init(); psync_settings_init(); pstatus_init(); ptimer_sleep_handler(psync_stop_crypto_on_sleep); @@ -1921,7 +1920,7 @@ psync_folderid_t *psync_crypto_folderids() { size_t alloc, l; alloc = 2; l = 0; - ret = psync_new_cnt(psync_folderid_t, alloc); + ret = malloc(sizeof(psync_folderid_t) * alloc); res = psync_sql_query_rdlock( "SELECT f1.id FROM folder f1, folder f2 WHERE f1.parentfolderid=f2.id " "AND " diff --git a/pclsync/psys.c b/pclsync/psys.c index aab8816..07b18d9 100644 --- a/pclsync/psys.c +++ b/pclsync/psys.c @@ -47,7 +47,7 @@ void psys_init() { psync_uid = getuid(); psync_gid = getgid(); psync_gids_cnt = getgroups(0, NULL); - psync_gids = psync_new_cnt(gid_t, psync_gids_cnt); + psync_gids = malloc(sizeof(gid_t) * psync_gids_cnt); if (pdbg_unlikely(getgroups(psync_gids_cnt, psync_gids) != psync_gids_cnt)) psync_gids_cnt = 0; pdbg_logf(D_NOTICE, "detected page size %ld", sysconf(_SC_PAGESIZE)); diff --git a/pclsync/ptask.c b/pclsync/ptask.c index f09c841..4b209aa 100644 --- a/pclsync/ptask.c +++ b/pclsync/ptask.c @@ -655,7 +655,7 @@ static int proc_start_async_transfer() { pdbg_logf(D_NOTICE, "pdeflate_init() failed"); goto err3; } - tparams = psync_new(async_params_t); + tparams = malloc(sizeof(async_params_t)); memset(tparams, 0, sizeof(async_params_t)); tparams->enc = enc; tparams->dec = dec; diff --git a/pclsync/ptevent.c b/pclsync/ptevent.c index 31ab73b..3795bc9 100644 --- a/pclsync/ptevent.c +++ b/pclsync/ptevent.c @@ -27,7 +27,7 @@ void ptevent_process(event_data_struct *data) { event_data_struct *event_data; if (data_event_fptr) { - event_data = psync_new(event_data_struct); + event_data = malloc(sizeof(event_data_struct)); event_data->eventid = data->eventid; event_data->uint1 = data->uint1; event_data->uint2 = data->uint2; diff --git a/pclsync/ptimer.c b/pclsync/ptimer.c index e0361c0..3f2d448 100644 --- a/pclsync/ptimer.c +++ b/pclsync/ptimer.c @@ -193,7 +193,7 @@ psync_timer_t ptimer_register(psync_timer_callback func, time_t numsec, psync_timer_t timer; uint32_t i; time_t n; - timer = psync_new(psync_timer_structure_t); + timer = malloc(sizeof(psync_timer_structure_t)); timer->call = func; timer->param = param; n = TIMER_ARRAY_SIZE; @@ -242,7 +242,7 @@ int ptimer_stop(psync_timer_t timer) { void ptimer_exception_handler(psync_exception_callback func) { struct exception_list *t; - t = psync_new(struct exception_list); + t = malloc(sizeof(struct exception_list)); t->next = NULL; t->func = func; t->threadid = pthread_self(); @@ -254,7 +254,7 @@ void ptimer_exception_handler(psync_exception_callback func) { void ptimer_sleep_handler(psync_exception_callback func) { struct exception_list *t; - t = psync_new(struct exception_list); + t = malloc(sizeof(struct exception_list)); t->next = NULL; t->func = func; t->threadid = pthread_self(); diff --git a/pclsync/pupload.c b/pclsync/pupload.c index 73b2524..66225c4 100644 --- a/pclsync/pupload.c +++ b/pclsync/pupload.c @@ -364,7 +364,7 @@ int handle_api_errors(sync_err_struct *err_struct) { pdbg_logf(D_NOTICE, "Got sync path: [%s] Sync folder: [%s]", syncFolder, folder); - event_data = psync_new(event_data_struct); + event_data = malloc(sizeof(event_data_struct)); event_data->eventid = PEVENT_SYNC_RENAME_F; event_data->str1 = strdup(err_struct->newName); event_data->str2 = folder; @@ -1019,7 +1019,7 @@ static int upload_big_file(const char *localpath, const unsigned char *hashhex, } psync_list_init(&rlist); if (likely(uploadoffset < fsize)) { - le = psync_new(psync_upload_range_list_t); + le = malloc(sizeof(psync_upload_range_list_t)); le->uploadoffset = uploadoffset; le->off = uploadoffset; le->len = fsize - uploadoffset; @@ -1119,14 +1119,14 @@ static int upload_big_file(const char *localpath, const unsigned char *hashhex, list) if ((le->type == PSYNC_URANGE_COPY_FILE || le->type == PSYNC_URANGE_COPY_UPLOAD) && le->len > PSYNC_MAX_COPY_FROM_REQ) { - le2 = psync_new(psync_upload_range_list_t); + le2 = malloc(sizeof(psync_upload_range_list_t)); *le2 = *le; le->len = PSYNC_MAX_COPY_FROM_REQ; le2->off += PSYNC_MAX_COPY_FROM_REQ; le2->len -= PSYNC_MAX_COPY_FROM_REQ; psync_list_add_after(&le->list, &le2->list); } - le = psync_new(psync_upload_range_list_t); + le = malloc(sizeof(psync_upload_range_list_t)); le->type = PSYNC_URANGE_LAST; psync_list_add_tail(&rlist, &le->list); psync_list_for_each_element(le, &rlist, psync_upload_range_list_t, list) { diff --git a/pclsync/putil.c b/pclsync/putil.c index 737f657..76f446f 100644 --- a/pclsync/putil.c +++ b/pclsync/putil.c @@ -1,5 +1,7 @@ #include +#include #include +#include #include #include #include @@ -9,6 +11,42 @@ #include "pdbg.h" #include "putil.h" + +static char normalize_table[256]; + +const char base64_table[] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'}; + +static const char base64_reverse_table[256] = { + -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2, -2, -2, -2, + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -2, -2, -2, + -2, -2, -2, -2, -2, 62, -2, 62, -2, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, + 61, -2, -2, -2, -1, -2, -2, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, + 63, -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, + 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, + -2, -2, -2, -2, -2, -2, -2, -2, -2}; + +__attribute((constructor)) static void init() { + unsigned long i; + for (i = 0; i < 256; i++) { + normalize_table[i] = i; + } + normalize_table[':'] = '_'; + normalize_table['/'] = '_'; + normalize_table['\\'] = '_'; +} + // wipe a segment of memory mem of size sz using a DoD 5220.22-M compliant // 3-pass wipe. the first pass overwrites the memory with zeroes, the second // with ones, and the third with random data using the urandom entropy @@ -82,3 +120,254 @@ void time_format(time_t tm, unsigned long ns, char *result) { memcpy(result, " +0000", 7); // copies the null byte } +char *psync_strdup(const char *str) { + size_t len; + len = strlen(str) + 1; + return (char *)memcpy(malloc(sizeof(char) * len), str, len); +} + +char *psync_strnormalize_filename(const char *str) { + size_t len, i; + char *ptr; + len = strlen(str) + 1; + ptr = malloc(sizeof(char) * len); + for (i = 0; i < len; i++) + ptr[i] = normalize_table[(unsigned char)str[i]]; + return ptr; +} + +char *psync_strndup(const char *str, size_t len) { + char *ptr; + ptr = (char *)memcpy(malloc(sizeof(char) * len + 1), str, len); + ptr[len] = 0; + return ptr; +} + +char *psync_strcat(const char *str, ...) { + size_t i, size, len; + const char *strs[64]; + size_t lengths[64]; + const char *ptr; + char *ptr2, *ptr3; + va_list ap; + va_start(ap, str); + strs[0] = str; + len = strlen(str); + lengths[0] = len; + size = len + 1; + i = 1; + while ((ptr = va_arg(ap, const char *))) { + pdbg_assert(i < ARRAY_SIZE(strs)); + len = strlen(ptr); + lengths[i] = len; + strs[i++] = ptr; + size += len; + } + va_end(ap); + ptr2 = ptr3 = (char *)malloc(size); + for (size = 0; size < i; size++) { + memcpy(ptr2, strs[size], lengths[size]); + ptr2 += lengths[size]; + } + *ptr2 = 0; + return ptr3; +} + +int psync_slprintf(char *str, size_t size, const char *format, ...) { + va_list ap; + int ret; + va_start(ap, format); + ret = vsnprintf(str, size, format, ap); + va_end(ap); + if (pdbg_unlikely(ret >= size)) + str[size - 1] = 0; + return ret; +} + +unsigned char *psync_base32_encode(const unsigned char *str, size_t length, + size_t *ret_length) { + static const unsigned char *table = + (const unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + unsigned char *result; + unsigned char *p; + uint32_t bits, buff; + + result = (unsigned char *)malloc(((length + 4) / 5) * 8 + 1); + p = result; + + bits = 0; + buff = 0; // don't really have to initialize this one, but a compiler that + // will detect that this is safe is yet to be born + + while (length) { + if (bits < 5) { + buff = (buff << 8) | (*str++); + length--; + bits += 8; + } + bits -= 5; + *p++ = table[0x1f & (buff >> bits)]; + } + + while (bits) { + if (bits < 5) { + buff <<= (5 - bits); + bits = 5; + } + bits -= 5; + *p++ = table[0x1f & (buff >> bits)]; + } + + *ret_length = p - result; + *p = 0; + return result; +} + +unsigned char *psync_base32_decode(const unsigned char *str, size_t length, + size_t *ret_length) { + unsigned char *result, *p; + uint32_t bits, buff; + unsigned char ch; + result = (unsigned char *)malloc((length + 7) / 8 * 5 + 1); + p = result; + bits = 0; + buff = 0; + while (length) { + ch = *str++; + length--; + if (ch >= 'A' && ch <= 'Z') + ch = (ch & 0x1f) - 1; + else if (ch >= '2' && ch <= '7') + ch -= '2' - 26; + else { + free(result); + return NULL; + } + buff = (buff << 5) + ch; + bits += 5; + if (bits >= 8) { + bits -= 8; + *p++ = buff >> bits; + } + } + *p = 0; + *ret_length = p - result; + return result; +} + +unsigned char *psync_base64_encode(const unsigned char *str, size_t length, + size_t *ret_length) { + const unsigned char *current = str; + unsigned char *p; + unsigned char *result; + + result = (unsigned char *)malloc(((length + 2) / 3) * 4 + 1); + p = result; + + while (length > 2) { + *p++ = base64_table[current[0] >> 2]; + *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; + *p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)]; + *p++ = base64_table[current[2] & 0x3f]; + current += 3; + length -= 3; + } + + if (length != 0) { + *p++ = base64_table[current[0] >> 2]; + if (length > 1) { + *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; + *p++ = base64_table[(current[1] & 0x0f) << 2]; + } else + *p++ = base64_table[(current[0] & 0x03) << 4]; + } + + *ret_length = p - result; + *p = 0; + return result; +} + +unsigned char *psync_base64_decode(const unsigned char *str, size_t length, + size_t *ret_length) { + const unsigned char *current = str; + unsigned char *result; + size_t i = 0, j = 0; + ssize_t ch; + + result = (unsigned char *)malloc((length + 3) / 4 * 3 + 1); + + while (length-- > 0) { + ch = base64_reverse_table[*current++]; + if (ch == -1) + continue; + else if (ch == -2) { + free(result); + return NULL; + } + switch (i % 4) { + case 0: + result[j] = ch << 2; + break; + case 1: + result[j++] |= ch >> 4; + result[j] = (ch & 0x0f) << 4; + break; + case 2: + result[j++] |= ch >> 2; + result[j] = (ch & 0x03) << 6; + break; + case 3: + result[j++] |= ch; + break; + } + i++; + } + *ret_length = j; + result[j] = 0; + return result; +} + +int psync_is_valid_utf8(const char *str) { + static const int8_t trailing[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, -1, -1, -1, -1, + -1, -1, -1, -1}; + int8_t t; + while (*str) { + t = trailing[(unsigned char)*str++]; + if (unlikely(t)) { + if (t < 0) + return 0; + while (t--) + if ((((unsigned char)*str++) & 0xc0) != 0x80) + return 0; + } + } + return 1; +} + +uint64_t psync_ato64(const char *str) { + uint64_t n = 0; + while (*str >= '0' && *str <= '9') + n = n * 10 + (*str++) - '0'; + return n; +} + +uint32_t psync_ato32(const char *str) { + uint32_t n = 0; + while (*str >= '0' && *str <= '9') + n = n * 10 + (*str++) - '0'; + return n; +} diff --git a/pclsync/putil.h b/pclsync/putil.h index be9f72c..33812c4 100644 --- a/pclsync/putil.h +++ b/pclsync/putil.h @@ -1,6 +1,8 @@ #ifndef __PUTIL_H #define __PUTIL_H +#include +#include #ifdef __cplusplus extern "C" { #endif @@ -8,13 +10,45 @@ extern "C" { #include #include +#include "pcompiler.h" + #define NTO_STR(s) TO_STR(s) #define TO_STR(s) #s #define VAR_ARRAY(name, type, size) type name[size] #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) + +extern const char base64_table[]; void putil_wipe(void *mem, size_t sz); void time_format(time_t tm, unsigned long ns, char *result); +char *psync_strdup(const char *str) PSYNC_MALLOC PSYNC_NONNULL(1); +char *psync_strnormalize_filename(const char *str) PSYNC_MALLOC PSYNC_NONNULL(1); +char *psync_strndup(const char *str, size_t len) PSYNC_MALLOC PSYNC_NONNULL(1); +char *psync_strcat(const char *str, ...) PSYNC_MALLOC PSYNC_SENTINEL; +int psync_slprintf(char *str, size_t size, const char *format, ...) PSYNC_NONNULL(1, 3); +unsigned char *psync_base32_encode(const unsigned char *str, size_t length, size_t *ret_length); +unsigned char *psync_base32_decode(const unsigned char *str, size_t length, size_t *ret_length); +unsigned char *psync_base64_encode(const unsigned char *str, size_t length, size_t *ret_length); +unsigned char *psync_base64_decode(const unsigned char *str, size_t length, size_t *ret_length); +int psync_is_valid_utf8(const char *str); +uint64_t psync_ato64(const char *str); +uint32_t psync_ato32(const char *str); + +static inline size_t psync_strlcpy(char *dst, const char *src, size_t size) { + size_t len; + len = strlen(src); + if (likely(len < size)) { + memcpy(dst, src, len + 1); + return len; + } else if (likely(size)) { + memcpy(dst, src, size - 1); + dst[size - 1] = 0; + return size - 1; + } else + return 0; +} + #ifdef __cplusplus }