/** * @brief Find next separate node in a sub-trie * * @param d : the double-array structure * @param root : the sub-trie root to search from * @param sep : the current separate node * @param keybuff : the TrieString buffer for incrementally calcuating key * * @return index to the next separate node; TRIE_INDEX_ERROR if no more * separate node is found * * Find the next separate node under a sub-trie rooted at @a root starting * from the current separate node @a sep. * * On return, @a keybuff is incrementally updated from the key which walks * to previous separate node to the one which walks to the new separate node. * So, it is assumed to be initialized by at least one da_first_separate() * call before. This incremental key calculation is more efficient than later * totally reconstructing key from the given separate node. * * Available since: 0.2.6 */ TrieIndex da_next_separate (DArray *d, TrieIndex root, TrieIndex sep, TrieString *keybuff) { TrieIndex parent; TrieIndex base; TrieIndex c, max_c; while (sep != root) { parent = da_get_check (d, sep); base = da_get_base (d, parent); c = sep - base; trie_string_cut_last (keybuff); /* find next sibling of sep */ max_c = MIN_VAL (TRIE_CHAR_MAX, d->num_cells - base); while (++c <= max_c) { if (da_get_check (d, base + c) == parent) { trie_string_append_char (keybuff, c); return da_first_separate (d, base + c, keybuff); } } sep = parent; } return TRIE_INDEX_ERROR; }
static INT buffRdbkVerify(struct bcm_mini_adapter *Adapter, PUCHAR mappedbuffer, UINT u32FirmwareLength, ULONG u32StartingAddress) { UINT len = u32FirmwareLength; INT retval = STATUS_SUCCESS; PUCHAR readbackbuff = kzalloc(MAX_TRANSFER_CTRL_BYTE_USB, GFP_KERNEL); int bytes; if (NULL == readbackbuff) { BCM_DEBUG_PRINT(Adapter, DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "MEMORY ALLOCATION FAILED"); return -ENOMEM; } while (u32FirmwareLength && !retval) { len = MIN_VAL(u32FirmwareLength, MAX_TRANSFER_CTRL_BYTE_USB); bytes = rdm(Adapter, u32StartingAddress, readbackbuff, len); if (bytes < 0) { retval = bytes; BCM_DEBUG_PRINT(Adapter, DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "rdm failed with status %d", retval); break; } retval = bcm_compare_buff_contents(readbackbuff, mappedbuffer, len); if (STATUS_SUCCESS != retval) break; u32StartingAddress += len; u32FirmwareLength -= len; mappedbuffer += len; } /* end of while (u32FirmwareLength && !retval) */ kfree(readbackbuff); return retval; }
static INT buffRdbkVerify(struct bcm_mini_adapter *Adapter, PUCHAR mappedbuffer, UINT u32FirmwareLength, ULONG u32StartingAddress) { UINT len = u32FirmwareLength; INT retval = STATUS_SUCCESS; PUCHAR readbackbuff = kzalloc(MAX_TRANSFER_CTRL_BYTE_USB, GFP_KERNEL); int bytes; if (NULL == readbackbuff) return -ENOMEM; while (u32FirmwareLength && !retval) { len = MIN_VAL(u32FirmwareLength, MAX_TRANSFER_CTRL_BYTE_USB); bytes = rdm(Adapter, u32StartingAddress, readbackbuff, len); if (bytes < 0) { retval = bytes; break; } if (memcmp(readbackbuff, mappedbuffer, len) != 0) { pr_err("%s() failed. The firmware doesn't match what was written", __func__); retval = -EIO; } u32StartingAddress += len; u32FirmwareLength -= len; mappedbuffer += len; } /* end of while (u32FirmwareLength && !retval) */ kfree(readbackbuff); return retval; }
static void da_relocate_base (DArray *d, TrieIndex s, TrieIndex new_base) { TrieIndex old_base; Symbols *symbols; int i; old_base = da_get_base (d, s); symbols = da_output_symbols (d, s); for (i = 0; i < symbols_num (symbols); i++) { TrieIndex old_next, new_next, old_next_base; old_next = old_base + symbols_get (symbols, i); new_next = new_base + symbols_get (symbols, i); old_next_base = da_get_base (d, old_next); /* allocate new next node and copy BASE value */ da_alloc_cell (d, new_next); da_set_check (d, new_next, s); da_set_base (d, new_next, old_next_base); /* old_next node is now moved to new_next * so, all cells belonging to old_next * must be given to new_next */ /* preventing the case of TAIL pointer */ if (old_next_base > 0) { TrieIndex c, max_c; max_c = MIN_VAL (TRIE_CHAR_MAX, TRIE_INDEX_MAX - old_next_base); for (c = 0; c < max_c; c++) { if (da_get_check (d, old_next_base + c) == old_next) da_set_check (d, old_next_base + c, new_next); } } /* free old_next node */ da_free_cell (d, old_next); } symbols_free (symbols); /* finally, make BASE[s] point to new_base */ da_set_base (d, s, new_base); }
static Symbols * da_output_symbols (const DArray *d, TrieIndex s) { Symbols *syms; TrieIndex base; TrieIndex c, max_c; syms = symbols_new (); base = da_get_base (d, s); max_c = MIN_VAL (TRIE_CHAR_MAX, TRIE_INDEX_MAX - base); for (c = 0; c < max_c; c++) { if (da_get_check (d, base + c) == s) symbols_add_fast (syms, (TrieChar) c); } return syms; }
static Bool da_has_children (DArray *d, TrieIndex s) { TrieIndex base; TrieIndex c, max_c; base = da_get_base (d, s); if (TRIE_INDEX_ERROR == base || base < 0) return FALSE; max_c = MIN_VAL (TRIE_CHAR_MAX, TRIE_INDEX_MAX - base); for (c = 0; c < max_c; c++) { if (da_get_check (d, base + c) == s) return TRUE; } return FALSE; }
static INT buffDnld(struct bcm_mini_adapter *Adapter, PUCHAR mappedbuffer, UINT u32FirmwareLength, ULONG u32StartingAddress) { unsigned int len = 0; int retval = STATUS_SUCCESS; len = u32FirmwareLength; while (u32FirmwareLength) { len = MIN_VAL(u32FirmwareLength, MAX_TRANSFER_CTRL_BYTE_USB); retval = wrm(Adapter, u32StartingAddress, mappedbuffer, len); if (retval) { BCM_DEBUG_PRINT(Adapter, DBG_TYPE_INITEXIT, MP_INIT, DBG_LVL_ALL, "wrm failed with status :%d", retval); break; } u32StartingAddress += len; u32FirmwareLength -= len; mappedbuffer += len; } return retval; }
static INT buffDnld(struct bcm_mini_adapter *Adapter, PUCHAR mappedbuffer, UINT u32FirmwareLength, ULONG u32StartingAddress) { unsigned int len = 0; int retval = STATUS_SUCCESS; len = u32FirmwareLength; while (u32FirmwareLength) { len = MIN_VAL(u32FirmwareLength, MAX_TRANSFER_CTRL_BYTE_USB); retval = wrm(Adapter, u32StartingAddress, mappedbuffer, len); if (retval) break; u32StartingAddress += len; u32FirmwareLength -= len; mappedbuffer += len; } return retval; }
/** * @brief Find first separate node in a sub-trie * * @param d : the double-array structure * @param root : the sub-trie root to search from * @param keybuff : the TrieString buffer for incrementally calcuating key * * @return index to the first separate node; TRIE_INDEX_ERROR on any failure * * Find the first separate node under a sub-trie rooted at @a root. * * On return, @a keybuff is appended with the key characters which walk from * @a root to the separate node. This is for incrementally calculating the * transition key, which is more efficient than later totally reconstructing * key from the given separate node. * * Available since: 0.2.6 */ TrieIndex da_first_separate (DArray *d, TrieIndex root, TrieString *keybuff) { TrieIndex base; TrieIndex c, max_c; while ((base = da_get_base (d, root)) >= 0) { max_c = MIN_VAL (TRIE_CHAR_MAX, d->num_cells - base); for (c = 0; c <= max_c; c++) { if (da_get_check (d, base + c) == root) break; } if (c == max_c) return TRIE_INDEX_ERROR; trie_string_append_char (keybuff, c); root = base + c; } return root; }
/* RFC 6189 p 4.5.1 */ soter_status_t soter_kdf(const void *key, size_t key_length, const char *label, const soter_kdf_context_buf_t *context, size_t context_count, void *output, size_t output_length) { soter_hmac_ctx_t hmac_ctx; soter_status_t soter_status; soter_status_t res = SOTER_SUCCESS; uint8_t out[MAX_HMAC_SIZE] = {0, 0, 0, 1}; size_t out_length = sizeof(out); size_t i; size_t j; uint8_t implicit_key[32]; /* If key is not specified, we will generate it from other information (useful for using this kdf for generating data from non-secret parameters such as session_id) */ if (!key) { memset(implicit_key, 0, sizeof(implicit_key)); memcpy(implicit_key, label, MIN_VAL(sizeof(implicit_key), strlen(label))); for (i = 0; i < context_count; i++) { if (context[i].data) { for (j = 0; j < MIN_VAL(sizeof(implicit_key), context[i].length); j++) { implicit_key[j] ^= context[i].data[j]; } } } key = implicit_key; key_length = sizeof(implicit_key); } soter_status = soter_hmac_init(&hmac_ctx, SOTER_HASH_SHA256, key, key_length); if (SOTER_SUCCESS != soter_status) { return soter_status; } /* i (counter) */ soter_status = soter_hmac_update(&hmac_ctx, out, 4); if (SOTER_SUCCESS != soter_status) { res = soter_status; goto err; } /* label */ soter_status = soter_hmac_update(&hmac_ctx, label, strlen(label)); if (SOTER_SUCCESS != soter_status) { res = soter_status; goto err; } /* 0x00 delimiter */ soter_status = soter_hmac_update(&hmac_ctx, out, 1); if (SOTER_SUCCESS != soter_status) { res = soter_status; goto err; } /* context */ for (i = 0; i < context_count; i++) { if (context[i].data) { soter_status = soter_hmac_update(&hmac_ctx, context[i].data, context[i].length); if (SOTER_SUCCESS != soter_status) { res = soter_status; goto err; } } } soter_status = soter_hmac_final(&hmac_ctx, out, &out_length); if (SOTER_SUCCESS != soter_status) { res = soter_status; goto err; } if (output_length > out_length) { res = SOTER_INVALID_PARAMETER; goto err; } memcpy(output, out, output_length); err: memset(out, 0, sizeof(out)); soter_hmac_cleanup(&hmac_ctx); return res; }
CMP_FN MIN_CMP(const Pixel_t * a, const Pixel_t * b) { return MIN_VAL(a) - MIN_VAL(b); }
int main(int argc, char* argv[]) { if (TIMER_RESOLUTION_IN_MS>0) { TIMECAPS tc; UINT wTimerRes; if (timeGetDevCaps(&tc,sizeof(TIMECAPS))==TIMERR_NOERROR) { wTimerRes=MIN_VAL(MAX_VAL(tc.wPeriodMin,TIMER_RESOLUTION_IN_MS),tc.wPeriodMax); timeBeginPeriod(wTimerRes); } } char curDirAndFile[1024]; GetModuleFileName(NULL,curDirAndFile,1023); PathRemoveFileSpec(curDirAndFile); std::string currentDirAndPath(curDirAndFile); std::string temp(currentDirAndPath); temp+="\\v_rep.dll"; LIBRARY lib=loadVrepLibrary(temp.c_str()); bool wasRunning=false; if (lib!=NULL) { if (getVrepProcAddresses(lib)!=0) { int guiItems=sim_gui_all; for (int i=1;i<argc;i++) { std::string arg(argv[i]); if (arg[0]=='-') { if (arg.length()>=2) { if (arg[1]=='h') guiItems=sim_gui_headless; if (arg[1]=='s') { autoStart=true; simStopDelay=0; unsigned int p=2; while (arg.length()>p) { simStopDelay*=10; if ((arg[p]>='0')&&(arg[p]<='9')) simStopDelay+=arg[p]-'0'; else { simStopDelay=0; break; } p++; } } if (arg[1]=='q') autoQuit=true; if ((arg[1]=='a')&&(arg.length()>2)) { std::string tmp; tmp.assign(arg.begin()+2,arg.end()); simSetStringParameter(sim_stringparam_additional_addonscript_firstscene,tmp.c_str()); // normally, never call API functions before simRunSimulator!! } if ((arg[1]=='b')&&(arg.length()>2)) { std::string tmp; tmp.assign(arg.begin()+2,arg.end()); simSetStringParameter(sim_stringparam_additional_addonscript,tmp.c_str()); // normally, never call API functions before simRunSimulator!! } if ((arg[1]=='g')&&(arg.length()>2)) { static int cnt=0; std::string tmp; tmp.assign(arg.begin()+2,arg.end()); if (cnt<9) simSetStringParameter(sim_stringparam_app_arg1+cnt,tmp.c_str()); // normally, never call API functions before simRunSimulator!! cnt++; } } } else { if (sceneOrModelOrUiToLoad.length()==0) sceneOrModelOrUiToLoad=arg; } } if (simRunSimulator("V-REP",guiItems,simulatorInit,simulatorLoop,simulatorDeinit)!=1) printf("Failed initializing and running V-REP\n"); else wasRunning=true; } else printf("Error: could not find all required functions in v_rep.dll\n"); unloadVrepLibrary(lib); } else printf("Error: could not find or correctly load v_rep.dll\n"); if (!wasRunning) getchar(); return(0); }
/*=================================================== Test body ========================== */ static int fmaNormMask(void) { /* Some Variables */ int nx, ny, n, n4, it, ir, nr, nerr=0, coi, step, step4; int xoff, yoff, roiw, roih, nerrt[9]; CvSize size; AtsRandState state1, state2, state3, state4; IplImage *A_8uC1, *A_8sC1, *A_32fC1, *B_8uC1, *B_8sC1, *B_32fC1; IplImage *A_8uC3, *A_8sC3, *A_32fC3, *B_8uC3, *B_8sC3, *B_32fC3; IplImage *mask; IplROI r, rm; double norm, testnorm, err; double Mask_density, d1, d2; trsiRead( &Min_Image_Width, "1", "Minimal image width" ); trsiRead( &Max_Image_Width, "32", "Maximal image width" ); trsiRead( &Max_ROI_Offset, "8", "Maximal ROI offset" ); trsdRead( &Mask_density, "0.5", "Mask density (0 - 1)" ); if( Min_Image_Width < 1 ) Min_Image_Width = 1; if( Max_Image_Width < Min_Image_Width ) Max_Image_Width = Min_Image_Width; if( Max_ROI_Offset < 0 ) Max_ROI_Offset = 0; if( Mask_density < 0.0 ) Mask_density = 0.0; if( Mask_density >= 1.0 ) { d1 = 2.0; d2 = 4.0; } else { d1 = 0.0; d2 = 1.0/(1.0-Mask_density); } if(d2>256.0) d2=256.0; atsRandInit( &state1, MIN_VAL(IPL_DEPTH_8U), MAX_VAL(IPL_DEPTH_8U), 13 ); atsRandInit( &state2, MIN_VAL(IPL_DEPTH_8S), MAX_VAL(IPL_DEPTH_8S), 14 ); atsRandInit( &state3, MIN_VAL(IPL_DEPTH_32F), MAX_VAL(IPL_DEPTH_32F), 15 ); atsRandInit( &state4, d1, d2, 16 ); for( it=0; it<9; it++ ) nerrt[it] = 0; /* Image size cycle starts ________________________ */ for( nx = Min_Image_Width; nx<=Max_Image_Width; nx++ ) { ny = nx; /*if(nx>1)ny=(int)(0.7*nx); // Non-square images test */ size.width = nx; size.height = ny; /* Initial images allocating & random filling */ A_8uC1 = cvCreateImage( size, IPL_DEPTH_8U, 1 ); A_8sC1 = cvCreateImage( size, IPL_DEPTH_8S, 1 ); A_32fC1 = cvCreateImage( size, IPL_DEPTH_32F, 1 ); B_8uC1 = cvCreateImage( size, IPL_DEPTH_8U, 1 ); B_8sC1 = cvCreateImage( size, IPL_DEPTH_8S, 1 ); B_32fC1 = cvCreateImage( size, IPL_DEPTH_32F, 1 ); A_8uC3 = cvCreateImage( size, IPL_DEPTH_8U, 3 ); A_8sC3 = cvCreateImage( size, IPL_DEPTH_8S, 3 ); A_32fC3 = cvCreateImage( size, IPL_DEPTH_32F, 3 ); B_8uC3 = cvCreateImage( size, IPL_DEPTH_8U, 3 ); B_8sC3 = cvCreateImage( size, IPL_DEPTH_8S, 3 ); B_32fC3 = cvCreateImage( size, IPL_DEPTH_32F, 3 ); mask = cvCreateImage( size, IPL_DEPTH_8U, 1 ); step = A_8uC1->widthStep; step4 = (A_32fC1->widthStep)/4; n = ny*step; n4 = ny*step4; atsbRand8u ( &state1, (uchar*)A_8uC1->imageData, n ); atsbRand8s ( &state2, A_8sC1->imageData, n ); atsbRand32f( &state3, (float*)A_32fC1->imageData, n4); atsbRand8u ( &state1, (uchar*)B_8uC1->imageData, n ); atsbRand8s ( &state2, B_8sC1->imageData, n ); atsbRand32f( &state3, (float*)B_32fC1->imageData, n4); atsbRand8u ( &state4, (uchar*)mask->imageData, n ); for(ir=0; ir<n; ir++) if((mask->imageData)[ir]>1) (mask->imageData)[ir]=1; (mask->imageData)[0] = 1; step = A_8uC3->widthStep; step4 = (A_32fC3->widthStep)/4; n = ny*step; n4 = ny*step4; atsbRand8u ( &state1, (uchar*)A_8uC3->imageData, n ); atsbRand8s ( &state2, A_8sC3->imageData, n ); atsbRand32f( &state3, (float*)A_32fC3->imageData, n4); atsbRand8u ( &state1, (uchar*)B_8uC3->imageData, n ); atsbRand8s ( &state2, B_8sC3->imageData, n ); atsbRand32f( &state3, (float*)B_32fC3->imageData, n4); nr = (ny-1)/2>Max_ROI_Offset ? Max_ROI_Offset : (ny-1)/2; A_8uC1->roi = A_8sC1->roi = A_32fC1->roi = A_8uC3->roi = A_8sC3->roi = A_32fC3->roi = B_8uC1->roi = B_8sC1->roi = B_32fC1->roi = B_8uC3->roi = B_8sC3->roi = B_32fC3->roi = &r; for( ir = 0; ir<=nr; ir++) /* ROI size cycle starts ----------------- */ { /* IPL ROI structures filling */ xoff = ir/11; yoff = ir; roiw = nx - (int)(1.2*xoff); roih = ny - (int)(1.5*yoff); r.xOffset = xoff; r.yOffset = yoff; r.width = roiw; r.height = roih; rm = r; rm.coi = 0; mask->roi = &rm; /* T E S T I N G */ for(it = 0; it<9; it++) { IplImage* B; r.coi = 0; //if( it >= 3 ) // continue; B = it<3 ? NULL : B_8uC1; A_8uC1->maskROI = B_8uC1->maskROI = NULL; norm = cvNormMask( A_8uC1, B, mask, cvlType[it] ); testnorm = TestNormMask( A_8uC1, B, mask, cvlType[it] ); err = fabs((norm-testnorm)/testnorm); if( err > EPS ) { nerrt[it]++; printf(" 8uC1 %d norm fail: %f %f\n", it+1, norm, testnorm); } B = it<3 ? NULL : B_8sC1; A_8sC1->maskROI = B_8sC1->maskROI = NULL; norm = cvNormMask( A_8sC1, B, mask, cvlType[it] ); testnorm = TestNormMask( A_8sC1, B, mask, cvlType[it] ); err = fabs((norm-testnorm)/testnorm); if( err > EPS ) { nerrt[it]++; printf(" 8sC1 %d norm fail: %f %f\n", it+1, norm, testnorm); } B = it<3 ? NULL : B_32fC1; A_32fC1->maskROI = B_32fC1->maskROI = NULL; norm = cvNormMask( A_32fC1, B, mask, cvlType[it] ); testnorm = TestNormMask( A_32fC1, B, mask, cvlType[it] ); err = fabs((norm-testnorm)/testnorm); if( err > FEPS ) { nerrt[it]++; printf(" 32fC1 %d norm fail: %f %f\n", it+1, norm, testnorm); } B = it<3 ? NULL : B_8uC3; for( coi=1; coi<4; coi++ ) { r.coi = coi; A_8uC3->maskROI = B_8uC3->maskROI = NULL; norm = cvNormMask( A_8uC3, B, mask, cvlType[it] ); testnorm = TestNormMask( A_8uC3, B, mask, cvlType[it] ); err = fabs((norm-testnorm)/testnorm); if( err > EPS ) { nerrt[it]++; printf(" 8uC3 %d norm fail: %f %f, coi = %d\n", it+1, norm, testnorm, coi); } } B = it<3 ? NULL : B_8sC3; for( coi=1; coi<4; coi++ ) { r.coi = coi; A_8sC3->maskROI = B_8sC3->maskROI = NULL; norm = cvNormMask( A_8sC3, B, mask, cvlType[it] ); testnorm = TestNormMask( A_8sC3, B, mask, cvlType[it] ); err = fabs((norm-testnorm)/testnorm); if( err > EPS ) { nerrt[it]++; printf(" 8sC3 %d norm fail: %f %f, coi = %d\n", it+1, norm, testnorm, coi); } } B = it<3 ? NULL : B_32fC3; for( coi=1; coi<4; coi++ ) { r.coi = coi; A_32fC3->maskROI = B_32fC3->maskROI = NULL; norm = cvNormMask( A_32fC3, B, mask, cvlType[it] ); testnorm = TestNormMask( A_32fC3, B, mask, cvlType[it] ); err = fabs((norm-testnorm)/testnorm); if( err > FEPS ) { nerrt[it]++; printf(" 32fC3 %d norm fail: %f %f, coi = %d\n", it+1, norm, testnorm, coi); } } } /* norm type */ for( it=0; it<9; it++ ) nerr += nerrt[it]; } /* ROI */ A_8uC1->roi = A_8sC1->roi = A_32fC1->roi = A_8uC3->roi = A_8sC3->roi = A_32fC3->roi = B_8uC1->roi = B_8sC1->roi = B_32fC1->roi = B_8uC3->roi = B_8sC3->roi = B_32fC3->roi = 0; mask->roi = 0; A_8uC1->maskROI = A_8sC1->maskROI = A_32fC1->maskROI = A_8uC3->maskROI = A_8sC3->maskROI = A_32fC3->maskROI = B_8uC1->maskROI = B_8sC1->maskROI = B_32fC1->maskROI = B_8uC3->maskROI = B_8sC3->maskROI = B_32fC3->maskROI = 0; mask->maskROI = 0; cvReleaseImage( &A_8uC1 ); cvReleaseImage( &A_8sC1 ); cvReleaseImage( &A_32fC1 ); cvReleaseImage( &B_8uC1 ); cvReleaseImage( &B_8sC1 ); cvReleaseImage( &B_32fC1 ); cvReleaseImage( &A_8uC3 ); cvReleaseImage( &A_8sC3 ); cvReleaseImage( &A_32fC3 ); cvReleaseImage( &B_8uC3 ); cvReleaseImage( &B_8sC3 ); cvReleaseImage( &B_32fC3 ); cvReleaseImage( &mask ); } /* Nx */ /*trsWrite (TW_RUN|TW_CON|TW_SUM," %d norm %s%s flavor fail: %g %g\n", it+1, f1, f2, norm, testnorm);*/ if(nerr) return trsResult( TRS_FAIL, "Algorithm test has passed. %d errors.", nerr ); else return trsResult( TRS_OK, "Algorithm test has passed successfully" ); } /* fmaNorm */
static char* prependcode(const char* s, int code) { char* sdup = strdup(s); char* nextline = sdup; char* buffer; char* end, *replaceend, *bufferend, *bufferstart; char dashp = '-'; const char* const linefeed = "\r\n"; const char* const empty = ""; const char* append = linefeed; int count = 0; int i, length; size_t bufsize; enough_mem(sdup); replace_not_larger(sdup, "\\r", "\r"); replace_not_larger(sdup, "\\n", "\n"); if (code < 0) { code = -code; } if (code > 999) { code = code % 1000; } if (code < 100) { code += 100; } /* count the newlines */ while ((nextline = strstr(nextline, "\r\n"))) { count++; nextline++; /* thus this position does not match again */ } /* memory: (count + 1) * strlen("xxx ") + 4 (to terminate for sure * with "\r\n") + 1 (Terminator) * * memory = (count + 1) * 4 + 1 */ bufsize = strlen(sdup) + (count + 1) * 4 + 4 + 1; bufferstart = buffer = (char*) malloc(bufsize); enough_mem(buffer); bufferend = buffer + bufsize - 1; end = nextline = sdup; while (strlen(end)) { end = strstr(nextline, "\r\n"); if (!end) { end = nextline + strlen(nextline); replaceend = end; dashp = ' '; append = linefeed; } else { /* don't replace the first '\' */ replaceend = end - 1; end += strlen("\r\n"); if (end == nextline + strlen(nextline)) { /* at the very end */ dashp = ' '; } else { dashp = '-'; } append = empty; } i = 0; while ((nextline + i) < replaceend) { if (nextline[i] == '%') { /* Remove the '%' */ nextline[i] = '/'; } /* remove any special characters between nextline * and end */ if ((unsigned char) nextline[i] < 32) { nextline[i] = '_'; } i++; } length = MIN_VAL( bufferend - bufferstart, strlen("xxx ") + (end - nextline) + strlen(append) + 1 ); snprintf(bufferstart, length, "%d%c%s%s", code, dashp, nextline, append); bufferstart += strlen(bufferstart); if (!*nextline) { /* end */ nextline = 0; } nextline = strstr(nextline, "\r\n") + strlen("\r\n"); } free(sdup); return buffer; }