You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

757 lines
22 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /* JSON_parser.c */
  2. /* 2005-12-30 */
  3. /*
  4. Copyright (c) 2005 JSON.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. The Software shall be used for Good, not Evil.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. */
  22. #include "JSON_parser.h"
  23. #include <stdio.h>
  24. #define true 1
  25. #define false 0
  26. /*
  27. Characters are mapped into these 32 symbol classes. This allows for
  28. significant reductions in the size of the state transition table.
  29. */
  30. /* error */
  31. #define S_ERR -1
  32. /* space */
  33. #define S_SPA 0
  34. /* other whitespace */
  35. #define S_WSP 1
  36. /* { */
  37. #define S_LBE 2
  38. /* } */
  39. #define S_RBE 3
  40. /* [ */
  41. #define S_LBT 4
  42. /* ] */
  43. #define S_RBT 5
  44. /* : */
  45. #define S_COL 6
  46. /* , */
  47. #define S_COM 7
  48. /* " */
  49. #define S_QUO 8
  50. /* \ */
  51. #define S_BAC 9
  52. /* / */
  53. #define S_SLA 10
  54. /* + */
  55. #define S_PLU 11
  56. /* - */
  57. #define S_MIN 12
  58. /* . */
  59. #define S_DOT 13
  60. /* 0 */
  61. #define S_ZER 14
  62. /* 123456789 */
  63. #define S_DIG 15
  64. /* a */
  65. #define S__A_ 16
  66. /* b */
  67. #define S__B_ 17
  68. /* c */
  69. #define S__C_ 18
  70. /* d */
  71. #define S__D_ 19
  72. /* e */
  73. #define S__E_ 20
  74. /* f */
  75. #define S__F_ 21
  76. /* l */
  77. #define S__L_ 22
  78. /* n */
  79. #define S__N_ 23
  80. /* r */
  81. #define S__R_ 24
  82. /* s */
  83. #define S__S_ 25
  84. /* t */
  85. #define S__T_ 26
  86. /* u */
  87. #define S__U_ 27
  88. /* ABCDF */
  89. #define S_A_F 28
  90. /* E */
  91. #define S_E 29
  92. /* everything else */
  93. #define S_ETC 30
  94. /*
  95. This table maps the 128 ASCII characters into the 32 character classes.
  96. The remaining Unicode characters should be mapped to S_ETC.
  97. */
  98. static const int ascii_class[128] = {
  99. S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR,
  100. S_ERR, S_WSP, S_WSP, S_ERR, S_ERR, S_WSP, S_ERR, S_ERR,
  101. S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR,
  102. S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR,
  103. S_SPA, S_ETC, S_QUO, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
  104. S_ETC, S_ETC, S_ETC, S_PLU, S_COM, S_MIN, S_DOT, S_SLA,
  105. S_ZER, S_DIG, S_DIG, S_DIG, S_DIG, S_DIG, S_DIG, S_DIG,
  106. S_DIG, S_DIG, S_COL, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
  107. S_ETC, S_A_F, S_A_F, S_A_F, S_A_F, S_E , S_A_F, S_ETC,
  108. S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
  109. S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
  110. S_ETC, S_ETC, S_ETC, S_LBT, S_BAC, S_RBT, S_ETC, S_ETC,
  111. S_ETC, S__A_, S__B_, S__C_, S__D_, S__E_, S__F_, S_ETC,
  112. S_ETC, S_ETC, S_ETC, S_ETC, S__L_, S_ETC, S__N_, S_ETC,
  113. S_ETC, S_ETC, S__R_, S__S_, S__T_, S__U_, S_ETC, S_ETC,
  114. S_ETC, S_ETC, S_ETC, S_LBE, S_ETC, S_RBE, S_ETC, S_ETC
  115. };
  116. /*
  117. The state transition table takes the current state and the current symbol,
  118. and returns either a new state or an action. A new state is a number between
  119. 0 and 29. An action is a negative number between -1 and -9. A JSON text is
  120. accepted if the end of the text is in state 9 and mode is MODE_DONE.
  121. */
  122. static const int state_transition_table[30][31] = {
  123. /* 0*/ { 0, 0,-8,-1,-6,-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},
  124. /* 1*/ { 1, 1,-1,-9,-1,-1,-1,-1, 3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  125. /* 2*/ { 2, 2,-8,-1,-6,-5,-1,-1, 3,-1,-1,-1,20,-1,21,22,-1,-1,-1,-1,-1,13,-1,17,-1,-1,10,-1,-1,-1,-1},
  126. /* 3*/ { 3,-1, 3, 3, 3, 3, 3, 3,-4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
  127. /* 4*/ {-1,-1,-1,-1,-1,-1,-1,-1, 3, 3, 3,-1,-1,-1,-1,-1,-1, 3,-1,-1,-1, 3,-1, 3, 3,-1, 3, 5,-1,-1,-1},
  128. /* 5*/ {-1,-1,-1,-1,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{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,26,26,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  149. /*26*/ { 9, 9,-1,-7,-1,-5,-1,-3,-1,-1,-1,-1,-1,-1,26,26,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  150. /*27*/ {27,27,-1,-1,-1,-1,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  151. /*28*/ {28,28,-8,-1,-6,-1,-1,-1, 3,-1,-1,-1,20,-1,21,22,-1,-1,-1,-1,-1,13,-1,17,-1,-1,10,-1,-1,-1,-1},
  152. /*29*/ {29,29,-1,-1,-1,-1,-1,-1, 3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
  153. };
  154. #define JSON_PARSER_MAX_DEPTH 20
  155. /*
  156. A stack maintains the states of nested structures.
  157. */
  158. typedef struct json_parser
  159. {
  160. int the_stack[JSON_PARSER_MAX_DEPTH];
  161. zval *the_zstack[JSON_PARSER_MAX_DEPTH];
  162. int the_top;
  163. } json_parser;
  164. /*
  165. These modes can be pushed on the PDA stack.
  166. */
  167. #define MODE_DONE 1
  168. #define MODE_KEY 2
  169. #define MODE_OBJECT 3
  170. #define MODE_ARRAY 4
  171. /*
  172. Push a mode onto the stack. Return false if there is overflow.
  173. */
  174. static int
  175. push(json_parser *json, zval *z, int mode)
  176. {
  177. json->the_top += 1;
  178. if (json->the_top >= JSON_PARSER_MAX_DEPTH) {
  179. return false;
  180. }
  181. json->the_stack[json->the_top] = mode;
  182. return true;
  183. }
  184. /*
  185. Pop the stack, assuring that the current mode matches the expectation.
  186. Return false if there is underflow or if the modes mismatch.
  187. */
  188. static int
  189. pop(json_parser *json, zval *z, int mode)
  190. {
  191. if (json->the_top < 0 || json->the_stack[json->the_top] != mode) {
  192. return false;
  193. }
  194. json->the_stack[json->the_top] = 0;
  195. json->the_top -= 1;
  196. return true;
  197. }
  198. static int dehexchar(char c)
  199. {
  200. if (c >= '0' && c <= '9')
  201. {
  202. return c - '0';
  203. }
  204. else if (c >= 'A' && c <= 'F')
  205. {
  206. return c - ('A' - 10);
  207. }
  208. else if (c >= 'a' && c <= 'f')
  209. {
  210. return c - ('a' - 10);
  211. }
  212. else
  213. {
  214. return -1;
  215. }
  216. }
  217. static void json_create_zval(zval **z, smart_str *buf, int type TSRMLS_DC)
  218. {
  219. ALLOC_INIT_ZVAL(*z);
  220. if (type == IS_LONG)
  221. {
  222. ZVAL_LONG(*z, atol(buf->c));
  223. }
  224. else if (type == IS_DOUBLE)
  225. {
  226. ZVAL_DOUBLE(*z, atof(buf->c));
  227. }
  228. else if (type == IS_STRING)
  229. {
  230. ZVAL_UTF8_STRINGL(*z, buf->c, buf->len, ZSTR_DUPLICATE);
  231. }
  232. else if (type == IS_BOOL)
  233. {
  234. ZVAL_BOOL(*z, (*(buf->c) == 't'));
  235. }
  236. else /* type == IS_NULL) || type unknown */
  237. {
  238. ZVAL_NULL(*z);
  239. }
  240. }
  241. static void utf16_to_utf8(smart_str *buf, unsigned short utf16)
  242. {
  243. if (utf16 < 0x80)
  244. {
  245. smart_str_appendc(buf, (unsigned char) utf16);
  246. }
  247. else if (utf16 < 0x800)
  248. {
  249. smart_str_appendc(buf, 0xc0 | (utf16 >> 6));
  250. smart_str_appendc(buf, 0x80 | (utf16 & 0x3f));
  251. }
  252. else
  253. {
  254. smart_str_appendc(buf, 0xe0 | (utf16 >> 12));
  255. smart_str_appendc(buf, 0x80 | ((utf16 >> 6) & 0x3f));
  256. smart_str_appendc(buf, 0x80 | (utf16 & 0x3f));
  257. }
  258. }
  259. static void attach_zval(json_parser *json, int up, int cur, smart_str *key, int assoc TSRMLS_DC)
  260. {
  261. zval *root = json->the_zstack[up];
  262. zval *child = json->the_zstack[cur];
  263. int up_mode = json->the_stack[up];
  264. if (up_mode == MODE_ARRAY)
  265. {
  266. add_next_index_zval(root, child);
  267. }
  268. else if (up_mode == MODE_OBJECT)
  269. {
  270. if (!assoc)
  271. {
  272. add_utf8_property_zval_ex(root, (key->len ? key->c : "_empty_"), (key->len ? (key->len + 1) : sizeof("_empty_")), child TSRMLS_CC);
  273. #if PHP_MAJOR_VERSION >= 5
  274. ZVAL_DELREF(child);
  275. #endif
  276. }
  277. else
  278. {
  279. add_utf8_assoc_zval_ex(root, (key->len ? key->c : "_empty_"), (key->len ? (key->len + 1) : sizeof("_empty_")), child);
  280. }
  281. key->len = 0;
  282. }
  283. }
  284. #define FREE_BUFFERS() do { smart_str_free(&buf); smart_str_free(&key); } while (0);
  285. #define SWAP_BUFFERS(from, to) do { \
  286. char *t1 = from.c; \
  287. int t2 = from.a; \
  288. from.c = to.c; \
  289. from.a = to.a; \
  290. to.c = t1; \
  291. to.a = t2; \
  292. to.len = from.len; \
  293. from.len = 0; \
  294. } while(0);
  295. #define JSON_RESET_TYPE() do { type = -1; } while(0);
  296. #define JSON(x) the_json.x
  297. /*
  298. The JSON_parser takes a UTF-16 encoded string and determines if it is a
  299. syntactically correct JSON text. Along the way, it creates a PHP variable.
  300. It is implemented as a Pushdown Automaton; that means it is a finite state
  301. machine with a stack.
  302. */
  303. int
  304. JSON_parser(zval *z, unsigned short p[], int length, int assoc TSRMLS_DC)
  305. {
  306. int b; /* the next character */
  307. int c; /* the next character class */
  308. int s; /* the next state */
  309. json_parser the_json; /* the parser state */
  310. int the_state = 0;
  311. int the_index;
  312. smart_str buf = {0};
  313. smart_str key = {0};
  314. int type = -1;
  315. unsigned short utf16 = 0;
  316. JSON(the_top) = -1;
  317. push(&the_json, z, MODE_DONE);
  318. for (the_index = 0; the_index < length; the_index += 1) {
  319. b = p[the_index];
  320. if ((b & 127) == b) {
  321. c = ascii_class[b];
  322. if (c <= S_ERR) {
  323. FREE_BUFFERS();
  324. return false;
  325. }
  326. } else {
  327. c = S_ETC;
  328. }
  329. /*
  330. Get the next state from the transition table.
  331. */
  332. s = state_transition_table[the_state][c];
  333. if (s < 0) {
  334. /*
  335. Perform one of the predefined actions.
  336. */
  337. switch (s) {
  338. /*
  339. empty }
  340. */
  341. case -9:
  342. if (!pop(&the_json, z, MODE_KEY)) {
  343. FREE_BUFFERS();
  344. return false;
  345. }
  346. the_state = 9;
  347. break;
  348. /*
  349. {
  350. */
  351. case -8:
  352. if (!push(&the_json, z, MODE_KEY)) {
  353. FREE_BUFFERS();
  354. return false;
  355. }
  356. the_state = 1;
  357. if (JSON(the_top) > 0)
  358. {
  359. zval *obj;
  360. if (JSON(the_top) == 1)
  361. {
  362. obj = z;
  363. }
  364. else
  365. {
  366. ALLOC_INIT_ZVAL(obj);
  367. }
  368. if (!assoc)
  369. {
  370. object_init(obj);
  371. }
  372. else
  373. {
  374. array_init(obj);
  375. }
  376. JSON(the_zstack)[JSON(the_top)] = obj;
  377. if (JSON(the_top) > 1)
  378. {
  379. attach_zval(&the_json, JSON(the_top-1), JSON(the_top), &key, assoc TSRMLS_CC);
  380. }
  381. JSON_RESET_TYPE();
  382. }
  383. break;
  384. /*
  385. }
  386. */
  387. case -7:
  388. if (type != -1 &&
  389. (JSON(the_stack)[JSON(the_top)] == MODE_OBJECT ||
  390. JSON(the_stack)[JSON(the_top)] == MODE_ARRAY))
  391. {
  392. zval *mval;
  393. smart_str_0(&buf);
  394. json_create_zval(&mval, &buf, type TSRMLS_CC);
  395. if (!assoc)
  396. {
  397. add_utf8_property_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), (key.len ? (key.len + 1) : sizeof("_empty_")), mval TSRMLS_CC);
  398. #if PHP_MAJOR_VERSION >= 5
  399. ZVAL_DELREF(mval);
  400. #endif
  401. }
  402. else
  403. {
  404. add_utf8_assoc_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), (key.len ? (key.len + 1) : sizeof("_empty_")), mval);
  405. }
  406. key.len = 0;
  407. buf.len = 0;
  408. JSON_RESET_TYPE();
  409. }
  410. if (!pop(&the_json, z, MODE_OBJECT)) {
  411. FREE_BUFFERS();
  412. return false;
  413. }
  414. the_state = 9;
  415. break;
  416. /*
  417. [
  418. */
  419. case -6:
  420. if (!push(&the_json, z, MODE_ARRAY)) {
  421. FREE_BUFFERS();
  422. return false;
  423. }
  424. the_state = 2;
  425. if (JSON(the_top) > 0)
  426. {
  427. zval *arr;
  428. if (JSON(the_top) == 1)
  429. {
  430. arr = z;
  431. }
  432. else
  433. {
  434. ALLOC_INIT_ZVAL(arr);
  435. }
  436. array_init(arr);
  437. JSON(the_zstack)[JSON(the_top)] = arr;
  438. if (JSON(the_top) > 1)
  439. {
  440. attach_zval(&the_json, JSON(the_top-1), JSON(the_top), &key, assoc TSRMLS_CC);
  441. }
  442. JSON_RESET_TYPE();
  443. }
  444. break;
  445. /*
  446. ]
  447. */
  448. case -5:
  449. {
  450. if (type != -1 &&
  451. (JSON(the_stack)[JSON(the_top)] == MODE_OBJECT ||
  452. JSON(the_stack)[JSON(the_top)] == MODE_ARRAY))
  453. {
  454. zval *mval;
  455. smart_str_0(&buf);
  456. json_create_zval(&mval, &buf, type TSRMLS_CC);
  457. add_next_index_zval(JSON(the_zstack)[JSON(the_top)], mval);
  458. buf.len = 0;
  459. JSON_RESET_TYPE();
  460. }
  461. if (!pop(&the_json, z, MODE_ARRAY)) {
  462. FREE_BUFFERS();
  463. return false;
  464. }
  465. the_state = 9;
  466. }
  467. break;
  468. /*
  469. "
  470. */
  471. case -4:
  472. switch (JSON(the_stack)[JSON(the_top)]) {
  473. case MODE_KEY:
  474. the_state = 27;
  475. smart_str_0(&buf);
  476. SWAP_BUFFERS(buf, key);
  477. JSON_RESET_TYPE();
  478. break;
  479. case MODE_ARRAY:
  480. case MODE_OBJECT:
  481. the_state = 9;
  482. break;
  483. default:
  484. FREE_BUFFERS();
  485. return false;
  486. }
  487. break;
  488. /*
  489. ,
  490. */
  491. case -3:
  492. {
  493. zval *mval;
  494. if (type != -1 &&
  495. (JSON(the_stack)[JSON(the_top)] == MODE_OBJECT ||
  496. JSON(the_stack[JSON(the_top)]) == MODE_ARRAY))
  497. {
  498. smart_str_0(&buf);
  499. json_create_zval(&mval, &buf, type TSRMLS_CC);
  500. }
  501. switch (JSON(the_stack)[JSON(the_top)]) {
  502. case MODE_OBJECT:
  503. if (pop(&the_json, z, MODE_OBJECT) && push(&the_json, z, MODE_KEY)) {
  504. if (type != -1)
  505. {
  506. if (!assoc)
  507. {
  508. add_utf8_property_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), (key.len ? (key.len + 1) : sizeof("_empty_")), mval TSRMLS_CC);
  509. #if PHP_MAJOR_VERSION >= 5
  510. ZVAL_DELREF(mval);
  511. #endif
  512. }
  513. else
  514. {
  515. add_utf8_assoc_zval_ex(JSON(the_zstack)[JSON(the_top)], (key.len ? key.c : "_empty_"), (key.len ? (key.len + 1) : sizeof("_empty_")), mval);
  516. }
  517. key.len = 0;
  518. }
  519. the_state = 29;
  520. }
  521. break;
  522. case MODE_ARRAY:
  523. if (type != -1)
  524. {
  525. add_next_index_zval(JSON(the_zstack)[JSON(the_top)], mval);
  526. }
  527. the_state = 28;
  528. break;
  529. default:
  530. FREE_BUFFERS();
  531. return false;
  532. }
  533. buf.len = 0;
  534. JSON_RESET_TYPE();
  535. }
  536. break;
  537. /*
  538. :
  539. */
  540. case -2:
  541. if (pop(&the_json, z, MODE_KEY) && push(&the_json, z, MODE_OBJECT)) {
  542. the_state = 28;
  543. break;
  544. }
  545. /*
  546. syntax error
  547. */
  548. case -1:
  549. {
  550. FREE_BUFFERS();
  551. return false;
  552. }
  553. }
  554. } else {
  555. /*
  556. Change the state and iterate.
  557. */
  558. if (type == IS_STRING)
  559. {
  560. if (s == 3 && the_state != 8)
  561. {
  562. if (the_state != 4)
  563. {
  564. utf16_to_utf8(&buf, b);
  565. }
  566. else
  567. {
  568. switch (b)
  569. {
  570. case 'b':
  571. smart_str_appendc(&buf, '\b');
  572. break;
  573. case 't':
  574. smart_str_appendc(&buf, '\t');
  575. break;
  576. case 'n':
  577. smart_str_appendc(&buf, '\n');
  578. break;
  579. case 'f':
  580. smart_str_appendc(&buf, '\f');
  581. break;
  582. case 'r':
  583. smart_str_appendc(&buf, '\r');
  584. break;
  585. default:
  586. utf16_to_utf8(&buf, b);
  587. break;
  588. }
  589. }
  590. }
  591. else if (s == 6)
  592. {
  593. utf16 = dehexchar(b) << 12;
  594. }
  595. else if (s == 7)
  596. {
  597. utf16 += dehexchar(b) << 8;
  598. }
  599. else if (s == 8)
  600. {
  601. utf16 += dehexchar(b) << 4;
  602. }
  603. else if (s == 3 && the_state == 8)
  604. {
  605. utf16 += dehexchar(b);
  606. utf16_to_utf8(&buf, utf16);
  607. }
  608. }
  609. else if (type < IS_LONG && (c == S_DIG || c == S_ZER))
  610. {
  611. type = IS_LONG;
  612. smart_str_appendc(&buf, b);
  613. }
  614. else if (type == IS_LONG && s == 24)
  615. {
  616. type = IS_DOUBLE;
  617. smart_str_appendc(&buf, b);
  618. }
  619. else if (type < IS_DOUBLE && c == S_DOT)
  620. {
  621. type = IS_DOUBLE;
  622. smart_str_appendc(&buf, b);
  623. }
  624. else if (type < IS_STRING && c == S_QUO)
  625. {
  626. type = IS_STRING;
  627. }
  628. else if (type < IS_BOOL && ((the_state == 12 && s == 9) || (the_state == 16 && s == 9)))
  629. {
  630. type = IS_BOOL;
  631. }
  632. else if (type < IS_NULL && the_state == 19 && s == 9)
  633. {
  634. type = IS_NULL;
  635. }
  636. else if (type != IS_STRING && c > S_WSP)
  637. {
  638. utf16_to_utf8(&buf, b);
  639. }
  640. the_state = s;
  641. }
  642. }
  643. FREE_BUFFERS();
  644. return the_state == 9 && pop(&the_json, z, MODE_DONE);
  645. }
  646. /*
  647. * Local variables:
  648. * tab-width: 4
  649. * c-basic-offset: 4
  650. * End:
  651. * vim600: noet sw=4 ts=4
  652. * vim<600: noet sw=4 ts=4
  653. */