void HandleWadinclude() { // Code somewhat copied from TEX_InitFromWad() char szTmpPath[MAXTOKEN]; char szNewWad[MAXTOKEN]; char* pszWadFile; bool bExcludeThisWad; const char* path = ValueForKey(&g_entities[0], "wad"); szNewWad[0] = 0; safe_strncpy(szTmpPath, path, MAXTOKEN); // temporary kludge so we don't have to deal with no occurances of a semicolon // in the path name .. if (strchr(szTmpPath, ';') == NULL) { safe_strncat(szTmpPath, ";", MAXTOKEN); } pszWadFile = strtok(szTmpPath, ";"); while (pszWadFile) { bExcludeThisWad = false; // look and see if we're supposed to include the textures from this WAD in the bsp. WadInclude_i it; for (it = g_WadInclude.begin(); it != g_WadInclude.end(); it++) { if (stristr(pszWadFile, it->c_str())) { Log("Embedding textures from WAD File [%s] into BSP\n", pszWadFile); bExcludeThisWad = true; } } if (!bExcludeThisWad) { safe_strncat(szNewWad, pszWadFile, MAXTOKEN); safe_strncat(szNewWad, ";", MAXTOKEN); } if (!bExcludeThisWad) { Log("Using WAD File: %s\n", pszWadFile); } // next wad file pszWadFile = strtok(NULL, ";"); } SetKeyValue(&g_entities[0], "wad", szNewWad); }
int getDynamicLoadPaths(char *main, int mainLen, char *lib, int libLen, char *env, int envLen) { int rc = 0; #ifdef HAVE_DLADDR char *lastslash, *_env; Dl_info info; memset(main, 0, mainLen); memset(lib, 0, libLen); memset(env, 0, envLen); memset(&info, 0, sizeof(info)); rc = dladdr((void *)&welcome, &info); if(rc == 0) return(-2); strncpy(main, info.dli_fname, mainLen); lastslash = strrchr(main, '/'); if(lastslash != NULL) lastslash[0] = '\0'; rc = dladdr((void *)&getDynamicLoadPaths, &info); if(rc == 0) return(-3); strncpy(lib, info.dli_fname, libLen); lastslash = strrchr(lib, '/'); if(lastslash != NULL) lastslash[0] = '\0'; #ifdef DARWIN _env = getenv("DYLD_LIBRARY_PATH"); if((_env != NULL) && (_env[0] != '\0')) { strncpy(env, "DYLD_LIBRARY_PATH: ", envLen); safe_strncat(env, envLen, _env); } #endif _env = getenv("LD_LIBRARY_PATH"); if((_env != NULL) && (_env[0] != '\0')) { safe_strncat(env, envLen, "LD_LIBRARY_PATH "); safe_strncat(env, envLen, _env); } return(0); #else return(-1); /* "failed" */ #endif /* HAVE_DLADDR */ }
int mutt_bcache_del(body_cache_t *bcache, const char *id) { char path[_POSIX_PATH_MAX]; if (!id || !*id || !bcache) return -1; path[0] = '\0'; safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen); safe_strncat (path, sizeof (path), id, mutt_strlen (id)); dprint (3, (debugfile, "bcache: del: '%s'\n", path)); return unlink (path); }
static PRBool CheckINIHeader(const char *aHeader, void *aClosure) { nsresult rv; INIClosure *c = reinterpret_cast<INIClosure *>(aClosure); if (!CheckVersion(aHeader, c->versions, c->versionsLength)) return PR_TRUE; const GREProperty *properties = c->properties; const GREProperty *endProperties = properties + c->propertiesLength; for (; properties < endProperties; ++properties) { char buffer[MAXPATHLEN]; rv = c->parser->GetString(aHeader, properties->property, buffer, sizeof(buffer)); if (NS_FAILED(rv)) return PR_TRUE; if (strcmp(buffer, properties->value)) return PR_TRUE; } rv = c->parser->GetString(aHeader, "GRE_PATH", c->pathBuffer, c->buflen); if (NS_FAILED(rv)) return PR_TRUE; if (!safe_strncat(c->pathBuffer, "/" XPCOM_DLL, c->buflen) || access(c->pathBuffer, R_OK)) return PR_TRUE; // We found a good GRE! Stop looking. c->found = PR_TRUE; return PR_FALSE; }
// ===================================================================================== // DisplayDeveloperLevel // ===================================================================================== static void DisplayDeveloperLevel() { char message[MAX_MESSAGE]; safe_strncpy(message, "Developer messages enabled : [", MAX_MESSAGE); if (g_developer >= DEVELOPER_LEVEL_MEGASPAM) { safe_strncat(message, "MegaSpam ", MAX_MESSAGE); } if (g_developer >= DEVELOPER_LEVEL_SPAM) { safe_strncat(message, "Spam ", MAX_MESSAGE); } if (g_developer >= DEVELOPER_LEVEL_FLUFF) { safe_strncat(message, "Fluff ", MAX_MESSAGE); } if (g_developer >= DEVELOPER_LEVEL_MESSAGE) { safe_strncat(message, "Message ", MAX_MESSAGE); } if (g_developer >= DEVELOPER_LEVEL_WARNING) { safe_strncat(message, "Warning ", MAX_MESSAGE); } if (g_developer >= DEVELOPER_LEVEL_ERROR) { safe_strncat(message, "Error", MAX_MESSAGE); } if (g_developer) { safe_strncat(message, "]\n", MAX_MESSAGE); Log(message); } }
int get_catalog_file(struct domainbinding *binding, I18N_STRING catalog_file) { int rc = I18N_SUCCESS; I18N_STRING locale; if ((rc = get_locale(locale)) == I18N_SUCCESS) { I18N_STRING separator; get_separator(separator); char *domain = textdomain(NULL); safe_strncpy(catalog_file, I18N_STRING_LEN, binding->path, I18N_STRING_LEN); safe_strncat(catalog_file, I18N_STRING_LEN, separator, I18N_STRING_LEN); safe_strncat(catalog_file, I18N_STRING_LEN, locale, I18N_STRING_LEN); safe_strncat(catalog_file, I18N_STRING_LEN, separator, I18N_STRING_LEN); safe_strncat(catalog_file, I18N_STRING_LEN, messages, I18N_STRING_LEN); safe_strncat(catalog_file, I18N_STRING_LEN, separator, I18N_STRING_LEN); safe_strncat(catalog_file, I18N_STRING_LEN, domain, I18N_STRING_LEN); safe_strncat(catalog_file, I18N_STRING_LEN, ".mo", I18N_STRING_LEN); } return rc; }
FILE* mutt_bcache_get(body_cache_t *bcache, const char *id) { char path[_POSIX_PATH_MAX]; FILE* fp = NULL; if (!id || !*id || !bcache) return NULL; path[0] = '\0'; safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen); safe_strncat (path, sizeof (path), id, mutt_strlen (id)); fp = safe_fopen (path, "r"); dprint (3, (debugfile, "bcache: get: '%s': %s\n", path, fp == NULL ? "no" : "yes")); return fp; }
/* --------- FILE COPY -------- */ int unlinkdir(char * dirpath) { int ret, stat; DIR * dp; struct dirent * dirstat; char * filename = (char *)calloc(5, sizeof(char)); ret = 0; stat = 0; if((dp = opendir(dirpath))!=NULL){ for( ;(dirstat = readdir(dp))!=(struct dirent *)NULL; ){ if(0 != self_memcmp(dirstat->d_name, ".")){ if(0 != self_memcmp(dirstat->d_name, "..")){ filename = safe_memcpy(filename, dirpath, BUF_MAX); filename = safe_strncat(filename, "/", BUF_MAX); filename = safe_strncat(filename, dirstat->d_name, BUF_MAX); stat = unlink(filename); if(stat < 0){ fprintf(stderr,"unlinkdir(): not delete file name. <%s>\n", filename); ret = ret - 1; } else{ #ifdef UNLINK_DIR fprintf(stderr,"unlinkdir(): delete file. <%s>\n", filename); #endif } } } } closedir(dp); } else { fprintf(stderr,"unlinkdir(): open dir. <%s>\n", dirpath); ret = -1; } free(filename); return(ret); }
static void set_err_msg( int code, char *msgbuf, int len) { char *msg = NULL; char *msg_tmp; /* see if there is an error message associated with the code */ *msgbuf = '\0'; if (code == PBSE_SYSTEM) { snprintf(msgbuf, len, "%s%s", msg_daemonname, pbse_to_txt(PBSE_SYSTEM)); msg_tmp = strerror(errno); if (msg_tmp) safe_strncat(msgbuf, strerror(errno), len - strlen(msgbuf)); else safe_strncat(msgbuf, "Unknown error", len - strlen(msgbuf)); } else if (code > PBSE_) { msg = pbse_to_txt(code); } else { msg = strerror(code); } if (msg) { snprintf(msgbuf, len, "%s", msg); } return; } /* END set_err_msg() */
int mutt_bcache_exists(body_cache_t *bcache, const char *id) { char path[_POSIX_PATH_MAX]; struct stat st; int rc = 0; if (!id || !*id || !bcache) return -1; path[0] = '\0'; safe_strncat (path, sizeof (path), bcache->path, bcache->pathlen); safe_strncat (path, sizeof (path), id, mutt_strlen (id)); if (stat (path, &st) < 0) rc = -1; else rc = S_ISREG(st.st_mode) && st.st_size != 0 ? 0 : -1; dprint (3, (debugfile, "bcache: exists: '%s': %s\n", path, rc == 0 ? "yes" : "no")); return rc; }
int get_catalog_file(struct domainbinding *binding, I18N_STRING catalog_file) { int rc = I18N_SUCCESS; I18N_STRING locale; get_locale(locale); char *separator = get_separator(); safe_strncpy(catalog_file, I18N_STRING_LEN, binding->path, I18N_STRING_LEN); safe_strncat(catalog_file, I18N_STRING_LEN, separator, I18N_STRING_LEN); safe_strncat(catalog_file, I18N_STRING_LEN, "lang", I18N_STRING_LEN); safe_strncat(catalog_file, I18N_STRING_LEN, separator, I18N_STRING_LEN); safe_strncat(catalog_file, I18N_STRING_LEN, locale, safe_strnlen(locale, I18N_STRING_LEN)); safe_strncat(catalog_file, I18N_STRING_LEN, ".mo", I18N_STRING_LEN); return rc; }
END_TEST START_TEST(safe_strncat_test) { char *names[] = { (char *)"slartibartfast", (char *)"arthur dent", (char *)"trillian", (char *)"zaphod beeblebrox", (char *)"ford prefect", (char *)"marvin the robot" }; /*int name_lens[] = {15, 12, 9, 18, 13, 17};*/ char small_buf[32]; int ret; strcpy(small_buf, names[0]); ret = safe_strncat(small_buf, names[1], sizeof(small_buf) - strlen(small_buf) - 1); fail_unless(ret == PBSE_NONE, "reported not enough space when space exists"); ret = safe_strncat(small_buf, names[2], sizeof(small_buf) - strlen(small_buf) - 1); fail_unless(ret == -1, "reported enough space when space does not exist"); memset(small_buf, 0, sizeof(small_buf)); strcpy(small_buf, names[3]); ret = safe_strncat(small_buf, names[4], sizeof(small_buf) - strlen(small_buf) - 1); fail_unless(ret == PBSE_NONE, "reported not enough space when space exists (round 2)"); ret = safe_strncat(small_buf, names[5], sizeof(small_buf) - strlen(small_buf) - 1); fail_unless(ret == -1, "reported enough space when space does not exist (round 2)"); }
int mutt_smtp_send (const ADDRESS* from, const ADDRESS* to, const ADDRESS* cc, const ADDRESS* bcc, const char *msgfile, int eightbit) { CONNECTION *conn; ACCOUNT account; const char* envfrom; char buf[1024]; int ret = -1; /* it might be better to synthesize an envelope from from user and host * but this condition is most likely arrived at accidentally */ if (EnvFrom) envfrom = EnvFrom->mailbox; else if (from) envfrom = from->mailbox; else { mutt_error (_("No from address given")); return -1; } if (smtp_fill_account (&account) < 0) return ret; if (!(conn = mutt_conn_find (NULL, &account))) return -1; Esmtp = eightbit; do { /* send our greeting */ if (( ret = smtp_open (conn))) break; FREE (&AuthMechs); /* send the sender's address */ ret = snprintf (buf, sizeof (buf), "MAIL FROM:<%s>", envfrom); if (eightbit && mutt_bit_isset (Capabilities, EIGHTBITMIME)) { safe_strncat (buf, sizeof (buf), " BODY=8BITMIME", 15); ret += 14; } if (DsnReturn && mutt_bit_isset (Capabilities, DSN)) ret += snprintf (buf + ret, sizeof (buf) - ret, " RET=%s", DsnReturn); safe_strncat (buf, sizeof (buf), "\r\n", 3); if (mutt_socket_write (conn, buf) == -1) { ret = smtp_err_write; break; } if ((ret = smtp_get_resp (conn))) break; /* send the recipient list */ if ((ret = smtp_rcpt_to (conn, to)) || (ret = smtp_rcpt_to (conn, cc)) || (ret = smtp_rcpt_to (conn, bcc))) break; /* send the message data */ if ((ret = smtp_data (conn, msgfile))) break; mutt_socket_write (conn, "QUIT\r\n"); ret = 0; } while (0); if (conn) mutt_socket_close (conn); if (ret == smtp_err_read) mutt_error (_("SMTP session failed: read error")); else if (ret == smtp_err_write) mutt_error (_("SMTP session failed: write error")); else if (ret == smtp_err_code) mutt_error (_("Invalid server response")); return ret; }
int main(int argc, char *argv[]) { const char *usage = "usage: flactimer [-1 | -2 | -o outputfile] command\n"; FILE *fout = stderr; if(argc == 1 || (argc > 1 && 0 == strcmp(argv[1], "-h"))) { fprintf(stderr, usage); return 0; } argv++; argc--; if(0 == strcmp(argv[0], "-1") || 0 == strcmp(argv[0], "/1")) { fout = stdout; argv++; argc--; } else if(0 == strcmp(argv[0], "-2") || 0 == strcmp(argv[0], "/2")) { fout = stdout; argv++; argc--; } else if(0 == strcmp(argv[0], "-o")) { if(argc < 2) { fprintf(stderr, usage); return 1; } fout = fopen(argv[1], "w"); if(!fout) { fprintf(fout, "ERROR opening file %s for writing\n", argv[1]); return 1; } argv += 2; argc -= 2; } if(argc <= 0) { fprintf(fout, "ERROR, no command!\n\n"); fprintf(fout, usage); fclose(fout); return 1; } // improvement: double-quote all args int i, n = 0; for(i = 0; i < argc; i++) { if(i > 0) n++; n += strlen(argv[i]); } char *args = (char*)malloc(n+1); if(!args) { fprintf(fout, "ERROR, no memory\n"); fclose(fout); return 1; } args[0] = '\0'; for(i = 0; i < argc; i++) { if(i > 0) safe_strncat(args, " ", sizeof(args)); safe_strncat(args, argv[i], sizeof(args)); } //fprintf(stderr, "@@@ cmd=[%s] args=[%s]\n", argv[0], args); STARTUPINFO si; GetStartupInfo(&si); DWORD wallclock_msec = GetTickCount(); PROCESS_INFORMATION pi; BOOL ok = CreateProcess( argv[0], // lpApplicationName args, // lpCommandLine NULL, // lpProcessAttributes NULL, // lpThreadAttributes FALSE, // bInheritHandles 0, // dwCreationFlags NULL, // lpEnvironment NULL, // lpCurrentDirectory &si, // lpStartupInfo (inherit from this proc?) &pi // lpProcessInformation ); if(!ok) { fprintf(fout, "ERROR running command\n"); free(args); //@@@ ok to free here or have to wait to wait till process is reaped? fclose(fout); return 1; } //fprintf(stderr, "@@@ waiting...\n"); WaitForSingleObject(pi.hProcess, INFINITE); //fprintf(stderr, "@@@ done\n"); wallclock_msec = GetTickCount() - wallclock_msec; FILETIME creation_time; FILETIME exit_time; FILETIME kernel_time; FILETIME user_time; if(!GetProcessTimes(pi.hProcess, &creation_time, &exit_time, &kernel_time, &user_time)) { fprintf(fout, "ERROR getting time info\n"); free(args); //@@@ ok to free here or have to wait to wait till process is reaped? fclose(fout); return 1; } uint64_t kernel_nsec = time2nsec(kernel_time); uint64_t user_nsec = time2nsec(user_time); fprintf(fout, "Kernel Time = "); printtime(fout, kernel_nsec, (uint64_t)wallclock_msec * 1000000); fprintf(fout, "User Time = "); printtime(fout, user_nsec, (uint64_t)wallclock_msec * 1000000); fprintf(fout, "Process Time = "); printtime(fout, kernel_nsec+user_nsec, (uint64_t)wallclock_msec * 1000000); fprintf(fout, "Global Time = "); printtime(fout, (uint64_t)wallclock_msec * 1000000, (uint64_t)wallclock_msec * 1000000); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); free(args); //@@@ always causes crash, maybe CreateProcess takes ownership? fclose(fout); return 0; }
static void bld_device_status_str(char *buf, int len, char *label, int device, int sd_max_size, int locked) { char sd_max[SD_MAX]; safe_strncat(buf, label, len); switch (device) { case BCMLOG_OUTDEV_NONE: safe_strncat(buf, "-> disabled", len); break; case BCMLOG_OUTDEV_PANIC: safe_strncat(buf, "-> flash", len); break; case BCMLOG_OUTDEV_RNDIS: safe_strncat(buf, "-> RNDIS", len); break; case BCMLOG_OUTDEV_SDCARD: if (sd_max_size == 0) { safe_strncat(buf, "-> SD card", len); } else { snprintf(sd_max, SD_MAX, "%d", sd_max_size); safe_strncat(buf, "-> SD card", len); safe_strncat(buf, " (Max file size : ", len); safe_strncat(buf, sd_max, len); safe_strncat(buf, " MB)", len); } break; case BCMLOG_OUTDEV_UART: safe_strncat(buf, "-> UART", len); break; case BCMLOG_OUTDEV_ACM: safe_strncat(buf, "-> ACM", len); break; case BCMLOG_OUTDEV_STM: safe_strncat(buf, "-> STM", len); break; case BCMLOG_OUTDEV_CUSTOM: safe_strncat(buf, "-> CUSTOM", len); break; default: safe_strncat(buf, "-> ERROR", len); break; } if (locked) safe_strncat(buf, " locked\n", len); else safe_strncat(buf, " unlocked\n", len); }
int main(int argc, char *argv[]) { int flac_return_val = 0, opt_arg = 1, from_arg = -1, to_arg = -1, flac_level = 5, i; char prog[MAX_PATH], cmdline[MAX_PATH*2], from[MAX_PATH], to[MAX_PATH], macdir[MAX_PATH], options[256], *p; enum { WAVPACK, RKAU, SHORTEN } codec; /* get the directory where MAC external codecs reside */ if(0 != (p = strrchr(argv[0],'\\'))) { safe_strncpy(macdir, argv[0], sizeof(macdir)); *(strrchr(macdir,'\\')+1) = '\0'; } else { safe_strncpy(macdir, "", sizeof(macdir)); } /* determine which codec we were called as and parse the options */ if(p == 0) p = argv[0]; else p++; if(0 == strnicmp(p, "short", 5)) { codec = SHORTEN; } else if(0 == strnicmp(p, "rkau", 4)) { codec = RKAU; if(argv[1][0] == '-' && argv[1][1] == 'l') { opt_arg = 2; switch(argv[1][2]) { case '1': flac_level = 1; break; case '2': flac_level = 5; break; case '3': flac_level = 8; break; } } } else if(0 == strnicmp(p, "wavpack", 7)) { codec = WAVPACK; if(argv[1][0] == '-') { opt_arg = 2; switch(argv[1][1]) { case 'f': flac_level = 1; break; case 'h': flac_level = 8; break; default: opt_arg = 1; } } } else { return -5; } /* figure out which arguments are the source and destination files */ for(i = 1; i < argc; i++) if(argv[i][0] != '-') { from_arg = i++; break; } for( ; i < argc; i++) if(argv[i][0] != '-') { to_arg = i++; break; } if(to_arg < 0) return -4; /* build the command to call flac with */ flac_snprintf(prog, sizeof (prog), "%sflac.exe", macdir); flac_snprintf(options, sizeof (options), "-%d", flac_level); for(i = opt_arg; i < argc; i++) if(argv[i][0] == '-') { safe_strncat(options, " ", sizeof(options)); safe_strncat(options, argv[i], sizeof(options)); } flac_snprintf(cmdline, sizeof (cmdline), "\"%s\" %s -o \"%s\" \"%s\"", prog, options, argv[to_arg], argv[from_arg]); flac_return_val = execit(prog, cmdline); /* * Now that flac has finished, we need to fork a process that will rename * the resulting file with the correct extension once MAC has moved it to * it's final resting place. */ if(0 == flac_return_val) { char *cptr; /* get the destination directory, if any */ if(0 != (p = strchr(argv[to_arg],'\\'))) { safe_strncpy(from, argv[to_arg], sizeof(from)); *(strrchr(from,'\\')+1) = '\0'; } else { safe_strncpy(from, "", sizeof(from)); } /* for the full 'from' and 'to' paths for the renamer process */ p = strrchr(argv[from_arg],'\\'); safe_strncat(from, p? p+1 : argv[from_arg], sizeof(from)); safe_strncpy(to, from, sizeof(to)); cptr = strrchr(from,'.'); if(cptr == NULL) return -3; cptr [0] = 0; switch(codec) { case SHORTEN: safe_strncat(from, ".shn", sizeof (from)); break; case WAVPACK: safe_strncat(from, ".wv", sizeof (from)); break; case RKAU: safe_strncpy(from, ".rka", sizeof (from)); break; default: return -4; } cptr = strrchr(to,'.'); if(cptr == NULL) return -3; cptr [0] = 0; safe_strncat(to, ".flac", sizeof(to)); flac_snprintf(prog, sizeof (prog), "%sflac_ren.exe", macdir); flac_snprintf(cmdline, sizeof (cmdline), "\"%s\" \"%s\" \"%s\"", prog, from, to); flac_return_val = forkit(prog, cmdline); } return flac_return_val; }
int iconvert(const char *fromcode, const char *tocode, const char *from, size_t fromlen, char **to, size_t *tolen) { int ret = 0; iconv_t cd1, cd2; char *ib; char *ob; char *utfbuf = 0, *outbuf, *newbuf; size_t utflen, outlen, ibl, obl, k; char tbuf[2048]; cd1 = iconv_open("UTF-8", fromcode); if (cd1 == (iconv_t)(-1)) return -1; cd2 = (iconv_t)(-1); /* Don't use strcasecmp() as it's locale-dependent. */ if (!strchr("Uu", tocode[0]) || !strchr("Tt", tocode[1]) || !strchr("Ff", tocode[2]) || tocode[3] != '-' || tocode[4] != '8' || tocode[5] != '\0') { char *tocode1; size_t dest_len = strlen(tocode) + 11; /* * Try using this non-standard feature of glibc and libiconv. * This is deliberately not a config option as people often * change their iconv library without rebuilding applications. */ tocode1 = safe_malloc_(dest_len); if (!tocode1) goto fail; safe_strncpy(tocode1, tocode, dest_len); safe_strncat(tocode1, "//TRANSLIT", dest_len); cd2 = iconv_open(tocode1, "UTF-8"); free(tocode1); if (cd2 == (iconv_t)(-1)) cd2 = iconv_open(tocode, fromcode); if (cd2 == (iconv_t)(-1)) { iconv_close(cd1); return -1; } } utflen = 1; /*fromlen * 2 + 1; XXX */ utfbuf = malloc(utflen); if (!utfbuf) goto fail; /* Convert to UTF-8 */ ib = (char *)from; ibl = fromlen; ob = utfbuf; obl = utflen; for (;;) { k = iconv(cd1, &ib, &ibl, &ob, &obl); assert((!k && !ibl) || (k == (size_t)(-1) && errno == E2BIG && ibl && obl < 6) || (k == (size_t)(-1) && (errno == EILSEQ || errno == EINVAL) && ibl)); if (!ibl) break; if (obl < 6) { /* Enlarge the buffer */ if(utflen*2 < utflen) /* overflow check */ goto fail; utflen *= 2; newbuf = realloc(utfbuf, utflen); if (!newbuf) goto fail; ob = (ob - utfbuf) + newbuf; obl = utflen - (ob - newbuf); utfbuf = newbuf; } else { /* Invalid input */ ib++, ibl--; *ob++ = '#', obl--; ret = 2; iconv(cd1, 0, 0, 0, 0); } } if (cd2 == (iconv_t)(-1)) { /* The target encoding was UTF-8 */ if (tolen) *tolen = ob - utfbuf; if (!to) { free(utfbuf); iconv_close(cd1); return ret; } newbuf = safe_realloc_add_2op_(utfbuf, (ob - utfbuf), /*+*/1); if (!newbuf) goto fail; ob = (ob - utfbuf) + newbuf; *ob = '\0'; *to = newbuf; iconv_close(cd1); return ret; } /* Truncate the buffer to be tidy */ utflen = ob - utfbuf; newbuf = realloc(utfbuf, utflen); if (!newbuf) goto fail; utfbuf = newbuf; /* Convert from UTF-8 to discover how long the output is */ outlen = 0; ib = utfbuf; ibl = utflen; while (ibl) { ob = tbuf; obl = sizeof(tbuf); k = iconv(cd2, &ib, &ibl, &ob, &obl); assert((k != (size_t)(-1) && !ibl) || (k == (size_t)(-1) && errno == E2BIG && ibl) || (k == (size_t)(-1) && errno == EILSEQ && ibl)); if (ibl && !(k == (size_t)(-1) && errno == E2BIG)) { /* Replace one character */ char *tb = "?"; size_t tbl = 1; outlen += ob - tbuf; ob = tbuf; obl = sizeof(tbuf); k = iconv(cd2, &tb, &tbl, &ob, &obl); assert((!k && !tbl) || (k == (size_t)(-1) && errno == EILSEQ && tbl)); for (++ib, --ibl; ibl && (*ib & 0x80); ib++, ibl--) ; } outlen += ob - tbuf; } ob = tbuf; obl = sizeof(tbuf); k = iconv(cd2, 0, 0, &ob, &obl); assert(!k); outlen += ob - tbuf; /* Convert from UTF-8 for real */ outbuf = safe_malloc_add_2op_(outlen, /*+*/1); if (!outbuf) goto fail; ib = utfbuf; ibl = utflen; ob = outbuf; obl = outlen; while (ibl) { k = iconv(cd2, &ib, &ibl, &ob, &obl); assert((k != (size_t)(-1) && !ibl) || (k == (size_t)(-1) && errno == EILSEQ && ibl)); if (k && !ret) ret = 1; if (ibl && !(k == (size_t)(-1) && errno == E2BIG)) { /* Replace one character */ char *tb = "?"; size_t tbl = 1; k = iconv(cd2, &tb, &tbl, &ob, &obl); assert((!k && !tbl) || (k == (size_t)(-1) && errno == EILSEQ && tbl)); for (++ib, --ibl; ibl && (*ib & 0x80); ib++, ibl--) ; } } k = iconv(cd2, 0, 0, &ob, &obl); assert(!k); assert(!obl); *ob = '\0'; free(utfbuf); iconv_close(cd1); iconv_close(cd2); if (tolen) *tolen = outlen; if (!to) { free(outbuf); return ret; } *to = outbuf; return ret; fail: if(0 != utfbuf) free(utfbuf); iconv_close(cd1); if (cd2 != (iconv_t)(-1)) iconv_close(cd2); return -2; }
PRBool GRE_GetPathFromRegKey(HKEY aRegKey, const GREVersionRange *versions, PRUint32 versionsLength, const GREProperty *properties, PRUint32 propertiesLength, char* aBuffer, PRUint32 aBufLen) { // Formerly, GREs were registered at the registry key // HKLM/Software/mozilla.org/GRE/<version> valuepair GreHome=Path. // Nowadays, they are registered in any subkey of // Software/mozilla.org/GRE, with the following valuepairs: // Version=<version> (REG_SZ) // GreHome=<path> (REG_SZ or REG_EXPAND_SZ) // <Property>=<value> (REG_SZ) // // Additional meta-info may be available in the future, including // localization info and other information which might be pertinent // to selecting one GRE over another. // // When a GRE is being registered, it should try to register itself at // HKLM/Software/mozilla.org/GRE/<Version> first, to preserve compatibility // with older glue. If this key is already taken (i.e. there is more than // one GRE of that version installed), it should append a unique number to // the version, for example: // 1.1 (already in use), 1.1_1, 1.1_2, etc... DWORD i = 0; while (PR_TRUE) { char name[MAXPATHLEN + 1]; DWORD nameLen = MAXPATHLEN; if (::RegEnumKeyEx(aRegKey, i, name, &nameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) { break; } HKEY subKey = NULL; if (::RegOpenKeyEx(aRegKey, name, 0, KEY_QUERY_VALUE, &subKey) != ERROR_SUCCESS) { continue; } char version[40]; DWORD versionlen = 40; char pathbuf[MAXPATHLEN]; DWORD pathlen; DWORD pathtype; PRBool ok = PR_FALSE; if (::RegQueryValueEx(subKey, "Version", NULL, NULL, (BYTE*) version, &versionlen) == ERROR_SUCCESS && CheckVersion(version, versions, versionsLength)) { ok = PR_TRUE; const GREProperty *props = properties; const GREProperty *propsEnd = properties + propertiesLength; for (; ok && props < propsEnd; ++props) { pathlen = sizeof(pathbuf); if (::RegQueryValueEx(subKey, props->property, NULL, &pathtype, (BYTE*) pathbuf, &pathlen) != ERROR_SUCCESS || strcmp(pathbuf, props->value)) ok = PR_FALSE; } pathlen = sizeof(pathbuf); if (ok && (!::RegQueryValueEx(subKey, "GreHome", NULL, &pathtype, (BYTE*) pathbuf, &pathlen) == ERROR_SUCCESS || !*pathbuf || !CopyWithEnvExpansion(aBuffer, pathbuf, aBufLen, pathtype))) { ok = PR_FALSE; } else if (!safe_strncat(aBuffer, "\\" XPCOM_DLL, aBufLen) || access(aBuffer, R_OK)) { ok = PR_FALSE; } } RegCloseKey(subKey); if (ok) return PR_TRUE; ++i; } aBuffer[0] = '\0'; return PR_FALSE; }
char * mime_ujtoj(char * from) { int i, len, c, kanji_flag, flag; char * head = "=?ISO-2022-jp?B?"; char * tail = "?="; char ato[128]; char * to = ato; char * data = (char *)calloc(2, sizeof(char)); char * swap = (char *)calloc(2, sizeof(char)); memset(to, 0x00, 128); len = (int)strlen(from); c = 0; kanji_flag = 0; flag = 0; for(i = 0; i < len; i++){ if(iseuckanji(from[i])){ // kanji if(kanji_flag == 0){ to[c] = 0x1b; c++; to[c] = 0x24; c++; to[c] = 0x42; c++; kanji_flag = 1; } to[c] = (from[i]&0x7f); c++; to[c] = (from[i+1]&0x7f); c++; i++; #ifdef MIME_BASE64_BUG printf("MIME_BASE64_BUG: mime_base64.c: kanji %d\n", c); #endif if(c > (76 - 17 * 3 / 4)){ to[c] = 0x1b; c++; to[c] = 0x28; c++; to[c] = 0x4a; c++; to[c] = 0x00; c = 0; swap = safe_memcpy(swap, to, BUF_MAX); #ifdef MIME_BASE64_BUG printf("MIME_BASE64_BUG: mime_base64.c: kanji <%x> %s \n",swap[0], swap); #endif swap = safe_base64(swap); if(flag == 0){ data = safe_memcpy(data, head, BUF_MAX); } else{ data = safe_strncat(data, "\t ", BUF_MAX); data = safe_strncat(data, head, BUF_MAX); } data = safe_strncat(data, swap, BUF_MAX); data = safe_strncat(data, tail, BUF_MAX); data = safe_strncat(data, "\r\n", BUF_MAX); memset(to, 0x00, 128); flag = 1; kanji_flag = 0; #ifdef MIME_BASE64_BUG printf("MIME_BASE64_BUG: mime_base64.c: kanji %s\n",data); #endif } } else{ // not kanji if(kanji_flag == 1){ to[c] = 0x1b; c++; to[c] = 0x28; c++; to[c] = 0x4a; c++; kanji_flag = 0; } to[c] = from[i]; c++; #ifdef MIME_BASE64_BUG printf("MIME_BASE64_BUG: mime_base64.c: not kanji %d\n", c); #endif if(c > (76 - 17 * 3 / 4)){ to[c] = 0x00; c =0; swap = safe_memcpy(swap, to, BUF_MAX); #ifdef MIME_BASE64_BUG printf("MIME_BASE64_BUG: mime_base64.c: not kanji <%x> %s \n",swap[0], swap); #endif swap = safe_base64(swap); if(flag == 0){ data = safe_memcpy(data, head, BUF_MAX); } else{ data = safe_strncat(data, "\t ", BUF_MAX); data = safe_strncat(data, head, BUF_MAX); } data = safe_strncat(data, swap, BUF_MAX); data = safe_strncat(data, tail, BUF_MAX); data = safe_strncat(data, "\r\n", BUF_MAX); memset(to, 0x00, 128); flag = 1; kanji_flag = 1; #ifdef MIME_BASE64_BUG printf("MIME_BASE64_BUG: mime_base64.c: not kanji %s\n",data); #endif } } } to[c] = 0x1b; c++; to[c] = 0x28; c++; to[c] = 0x4a; c++; to[c] = '\0'; swap = safe_memcpy(swap, to, BUF_MAX); #ifdef MIME_BASE64_BUG printf("MIME_BASE64_BUG: mime_base64.c: owari <%x> %s \n",swap[0], swap); #endif swap = safe_base64(swap); if(flag == 0){ data = safe_memcpy(data, head, BUF_MAX); } else{ data = safe_strncat(data, "\t ", BUF_MAX); data = safe_strncat(data, head, BUF_MAX); } data = safe_strncat(data, swap, BUF_MAX); data = safe_strncat(data, tail, BUF_MAX); #ifdef MIME_BASE64_BUG printf("MIME_BASE64_BUG: mime_base64.c: owari %s\n",data); #endif from = safe_memcpy(from, data, BUF_MAX); free(swap); free(data); return from; }